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 | |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 19 | #include <unistd.h> |
| 20 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 21 | #include <algorithm> |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 22 | #include <deque> |
Ian Rogers | cf7f191 | 2014-10-22 22:06:39 -0700 | [diff] [blame] | 23 | #include <iostream> |
Vladimir Marko | 2130053 | 2017-01-24 18:06:55 +0000 | [diff] [blame] | 24 | #include <map> |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 25 | #include <memory> |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 26 | #include <queue> |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 27 | #include <string> |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 28 | #include <tuple> |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 29 | #include <unordered_map> |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 30 | #include <utility> |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 31 | #include <vector> |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 32 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 33 | #include "android-base/stringprintf.h" |
| 34 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 35 | #include "art_field-inl.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 36 | #include "art_method-inl.h" |
| 37 | #include "base/arena_allocator.h" |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 38 | #include "base/casts.h" |
David Sehr | 67bf42e | 2018-02-26 16:43:04 -0800 | [diff] [blame] | 39 | #include "base/leb128.h" |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 40 | #include "base/logging.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 41 | #include "base/os.h" |
| 42 | #include "base/quasi_atomic.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 43 | #include "base/scoped_arena_containers.h" |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 44 | #include "base/scoped_flock.h" |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 45 | #include "base/stl_util.h" |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 46 | #include "base/systrace.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 47 | #include "base/time_utils.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 48 | #include "base/unix_file/fd_file.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 49 | #include "base/utils.h" |
Andreas Gampe | b9aec2c | 2015-04-23 22:23:47 -0700 | [diff] [blame] | 50 | #include "base/value_object.h" |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 51 | #include "cha.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 52 | #include "class_linker-inl.h" |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 53 | #include "class_loader_utils.h" |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 54 | #include "class_root.h" |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 55 | #include "class_table-inl.h" |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 56 | #include "compiler_callbacks.h" |
Vladimir Marko | 606adb3 | 2018-04-05 14:49:24 +0100 | [diff] [blame] | 57 | #include "debug_print.h" |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 58 | #include "debugger.h" |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 59 | #include "dex/class_accessor-inl.h" |
David Sehr | b2ec9f5 | 2018-02-21 13:20:31 -0800 | [diff] [blame] | 60 | #include "dex/descriptors_names.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 61 | #include "dex/dex_file-inl.h" |
| 62 | #include "dex/dex_file_exception_helpers.h" |
| 63 | #include "dex/dex_file_loader.h" |
David Sehr | 0225f8e | 2018-01-31 08:52:24 +0000 | [diff] [blame] | 64 | #include "dex/utf.h" |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 65 | #include "entrypoints/entrypoint_utils.h" |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 66 | #include "entrypoints/runtime_asm_entrypoints.h" |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 67 | #include "experimental_flags.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 68 | #include "gc/accounting/card_table-inl.h" |
Mathieu Chartier | 03c1dd9 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 69 | #include "gc/accounting/heap_bitmap-inl.h" |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 70 | #include "gc/accounting/space_bitmap-inl.h" |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 71 | #include "gc/heap-visit-objects-inl.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 72 | #include "gc/heap.h" |
Mathieu Chartier | 1b1e31f | 2016-05-19 10:13:04 -0700 | [diff] [blame] | 73 | #include "gc/scoped_gc_critical_section.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 74 | #include "gc/space/image_space.h" |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 75 | #include "gc/space/space-inl.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 76 | #include "gc_root-inl.h" |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 77 | #include "handle_scope-inl.h" |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 78 | #include "hidden_api.h" |
Mathieu Chartier | 4a26f17 | 2016-01-26 14:26:18 -0800 | [diff] [blame] | 79 | #include "image-inl.h" |
Andreas Gampe | 75a7db6 | 2016-09-26 12:04:26 -0700 | [diff] [blame] | 80 | #include "imt_conflict_table.h" |
| 81 | #include "imtable-inl.h" |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 82 | #include "intern_table.h" |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 83 | #include "interpreter/interpreter.h" |
David Srbecky | fb3de3d | 2018-01-29 16:11:49 +0000 | [diff] [blame] | 84 | #include "jit/debugger_interface.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 85 | #include "jit/jit.h" |
| 86 | #include "jit/jit_code_cache.h" |
Vladimir Marko | a3ad0cd | 2018-05-04 10:06:38 +0100 | [diff] [blame] | 87 | #include "jni/java_vm_ext.h" |
| 88 | #include "jni/jni_internal.h" |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 89 | #include "linear_alloc.h" |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 90 | #include "mirror/call_site.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 91 | #include "mirror/class-inl.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 92 | #include "mirror/class.h" |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 93 | #include "mirror/class_ext.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 94 | #include "mirror/class_loader.h" |
Ian Rogers | 39ebcb8 | 2013-05-30 16:57:23 -0700 | [diff] [blame] | 95 | #include "mirror/dex_cache-inl.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 96 | #include "mirror/dex_cache.h" |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 97 | #include "mirror/emulated_stack_frame.h" |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 98 | #include "mirror/field.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 99 | #include "mirror/iftable-inl.h" |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 100 | #include "mirror/method.h" |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 101 | #include "mirror/method_handle_impl.h" |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 102 | #include "mirror/method_handles_lookup.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 103 | #include "mirror/method_type.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 104 | #include "mirror/object-inl.h" |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 105 | #include "mirror/object-refvisitor-inl.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 106 | #include "mirror/object_array-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 107 | #include "mirror/proxy.h" |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 108 | #include "mirror/reference-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 109 | #include "mirror/stack_trace_element.h" |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 110 | #include "mirror/string-inl.h" |
Orion Hodson | 005ac51 | 2017-10-24 15:43:43 +0100 | [diff] [blame] | 111 | #include "mirror/var_handle.h" |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 112 | #include "native/dalvik_system_DexFile.h" |
Andreas Gampe | 373a9b5 | 2017-10-18 09:01:57 -0700 | [diff] [blame] | 113 | #include "nativehelper/scoped_local_ref.h" |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 114 | #include "oat.h" |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 115 | #include "oat_file-inl.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 116 | #include "oat_file.h" |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 117 | #include "oat_file_assistant.h" |
| 118 | #include "oat_file_manager.h" |
| 119 | #include "object_lock.h" |
David Sehr | 82d046e | 2018-04-23 08:14:19 -0700 | [diff] [blame] | 120 | #include "profile/profile_compilation_info.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 121 | #include "runtime.h" |
Andreas Gampe | ac30fa2 | 2017-01-18 21:02:36 -0800 | [diff] [blame] | 122 | #include "runtime_callbacks.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 123 | #include "scoped_thread_state_change-inl.h" |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 124 | #include "thread-inl.h" |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 125 | #include "thread_list.h" |
Mathieu Chartier | 7778b88 | 2015-10-05 16:41:10 -0700 | [diff] [blame] | 126 | #include "trace.h" |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 127 | #include "utils/dex_cache_arrays_layout-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 128 | #include "verifier/method_verifier.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 129 | #include "well_known_classes.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 130 | |
| 131 | namespace art { |
| 132 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 133 | using android::base::StringPrintf; |
| 134 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 135 | static constexpr bool kSanityCheckObjects = kIsDebugBuild; |
Mathieu Chartier | 8790c7f | 2016-03-31 15:05:45 -0700 | [diff] [blame] | 136 | static constexpr bool kVerifyArtMethodDeclaringClasses = kIsDebugBuild; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 137 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 138 | static void ThrowNoClassDefFoundError(const char* fmt, ...) |
| 139 | __attribute__((__format__(__printf__, 1, 2))) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 140 | REQUIRES_SHARED(Locks::mutator_lock_); |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 141 | static void ThrowNoClassDefFoundError(const char* fmt, ...) { |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 142 | va_list args; |
| 143 | va_start(args, fmt); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 144 | Thread* self = Thread::Current(); |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 145 | self->ThrowNewExceptionV("Ljava/lang/NoClassDefFoundError;", fmt, args); |
Ian Rogers | cab0101 | 2012-01-10 17:35:46 -0800 | [diff] [blame] | 146 | va_end(args); |
| 147 | } |
| 148 | |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 149 | static bool HasInitWithString(Thread* self, ClassLinker* class_linker, const char* descriptor) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 150 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 151 | ArtMethod* method = self->GetCurrentMethod(nullptr); |
Andreas Gampe | bfdcdc1 | 2015-04-22 18:10:36 -0700 | [diff] [blame] | 152 | StackHandleScope<1> hs(self); |
| 153 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(method != nullptr ? |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 154 | method->GetDeclaringClass()->GetClassLoader() : nullptr)); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 155 | ObjPtr<mirror::Class> exception_class = class_linker->FindClass(self, descriptor, class_loader); |
Andreas Gampe | bfdcdc1 | 2015-04-22 18:10:36 -0700 | [diff] [blame] | 156 | |
| 157 | if (exception_class == nullptr) { |
| 158 | // No exc class ~ no <init>-with-string. |
| 159 | CHECK(self->IsExceptionPending()); |
| 160 | self->ClearException(); |
| 161 | return false; |
| 162 | } |
| 163 | |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 164 | ArtMethod* exception_init_method = exception_class->FindConstructor( |
| 165 | "(Ljava/lang/String;)V", class_linker->GetImagePointerSize()); |
Andreas Gampe | bfdcdc1 | 2015-04-22 18:10:36 -0700 | [diff] [blame] | 166 | return exception_init_method != nullptr; |
| 167 | } |
| 168 | |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 169 | static mirror::Object* GetVerifyError(ObjPtr<mirror::Class> c) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 170 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 171 | ObjPtr<mirror::ClassExt> ext(c->GetExtData()); |
| 172 | if (ext == nullptr) { |
| 173 | return nullptr; |
| 174 | } else { |
| 175 | return ext->GetVerifyError(); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | // Helper for ThrowEarlierClassFailure. Throws the stored error. |
| 180 | static void HandleEarlierVerifyError(Thread* self, |
| 181 | ClassLinker* class_linker, |
| 182 | ObjPtr<mirror::Class> c) |
| 183 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 184 | ObjPtr<mirror::Object> obj = GetVerifyError(c); |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 185 | DCHECK(obj != nullptr); |
| 186 | self->AssertNoPendingException(); |
| 187 | if (obj->IsClass()) { |
| 188 | // Previous error has been stored as class. Create a new exception of that type. |
| 189 | |
| 190 | // It's possible the exception doesn't have a <init>(String). |
| 191 | std::string temp; |
| 192 | const char* descriptor = obj->AsClass()->GetDescriptor(&temp); |
| 193 | |
| 194 | if (HasInitWithString(self, class_linker, descriptor)) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 195 | self->ThrowNewException(descriptor, c->PrettyDescriptor().c_str()); |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 196 | } else { |
| 197 | self->ThrowNewException(descriptor, nullptr); |
| 198 | } |
| 199 | } else { |
| 200 | // Previous error has been stored as an instance. Just rethrow. |
Vladimir Marko | c13fbd8 | 2018-06-04 16:16:28 +0100 | [diff] [blame] | 201 | ObjPtr<mirror::Class> throwable_class = GetClassRoot<mirror::Throwable>(class_linker); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 202 | ObjPtr<mirror::Class> error_class = obj->GetClass(); |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 203 | CHECK(throwable_class->IsAssignableFrom(error_class)); |
| 204 | self->SetException(obj->AsThrowable()); |
| 205 | } |
| 206 | self->AssertPendingException(); |
| 207 | } |
| 208 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 209 | 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] | 210 | // The class failed to initialize on a previous attempt, so we want to throw |
| 211 | // a NoClassDefFoundError (v2 2.17.5). The exception to this rule is if we |
| 212 | // failed in verification, in which case v2 5.4.1 says we need to re-throw |
| 213 | // the previous error. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 214 | Runtime* const runtime = Runtime::Current(); |
| 215 | if (!runtime->IsAotCompiler()) { // Give info if this occurs at runtime. |
Andreas Gampe | 3d6b470 | 2015-09-21 08:35:52 -0700 | [diff] [blame] | 216 | std::string extra; |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 217 | if (GetVerifyError(c) != nullptr) { |
| 218 | ObjPtr<mirror::Object> verify_error = GetVerifyError(c); |
Andreas Gampe | 369c851 | 2016-01-28 15:31:39 -0800 | [diff] [blame] | 219 | if (verify_error->IsClass()) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 220 | extra = mirror::Class::PrettyDescriptor(verify_error->AsClass()); |
Andreas Gampe | 369c851 | 2016-01-28 15:31:39 -0800 | [diff] [blame] | 221 | } else { |
| 222 | extra = verify_error->AsThrowable()->Dump(); |
| 223 | } |
Andreas Gampe | 3d6b470 | 2015-09-21 08:35:52 -0700 | [diff] [blame] | 224 | } |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 225 | LOG(INFO) << "Rejecting re-init on previously-failed class " << c->PrettyClass() |
| 226 | << ": " << extra; |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 227 | } |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 228 | |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 229 | CHECK(c->IsErroneous()) << c->PrettyClass() << " " << c->GetStatus(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 230 | Thread* self = Thread::Current(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 231 | if (runtime->IsAotCompiler()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 232 | // At compile time, accurate errors and NCDFE are disabled to speed compilation. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 233 | ObjPtr<mirror::Throwable> pre_allocated = runtime->GetPreAllocatedNoClassDefFoundError(); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 234 | self->SetException(pre_allocated); |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 235 | } else { |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 236 | if (GetVerifyError(c) != nullptr) { |
Andreas Gampe | cb08695 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 237 | // Rethrow stored error. |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 238 | HandleEarlierVerifyError(self, this, c); |
Andreas Gampe | cb08695 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 239 | } |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 240 | // TODO This might be wrong if we hit an OOME while allocating the ClassExt. In that case we |
| 241 | // might have meant to go down the earlier if statement with the original error but it got |
| 242 | // swallowed by the OOM so we end up here. |
| 243 | if (GetVerifyError(c) == nullptr || wrap_in_no_class_def) { |
Andreas Gampe | cb08695 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 244 | // If there isn't a recorded earlier error, or this is a repeat throw from initialization, |
| 245 | // the top-level exception must be a NoClassDefFoundError. The potentially already pending |
| 246 | // exception will be a cause. |
| 247 | self->ThrowNewWrappedException("Ljava/lang/NoClassDefFoundError;", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 248 | c->PrettyDescriptor().c_str()); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 249 | } |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 253 | static void VlogClassInitializationFailure(Handle<mirror::Class> klass) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 254 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 255 | if (VLOG_IS_ON(class_linker)) { |
| 256 | std::string temp; |
| 257 | LOG(INFO) << "Failed to initialize class " << klass->GetDescriptor(&temp) << " from " |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 258 | << klass->GetLocation() << "\n" << Thread::Current()->GetException()->Dump(); |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | |
| 262 | static void WrapExceptionInInitializer(Handle<mirror::Class> klass) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 263 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 264 | Thread* self = Thread::Current(); |
| 265 | JNIEnv* env = self->GetJniEnv(); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 266 | |
| 267 | ScopedLocalRef<jthrowable> cause(env, env->ExceptionOccurred()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 268 | CHECK(cause.get() != nullptr); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 269 | |
Andreas Gampe | 1e8a395 | 2016-11-30 10:13:19 -0800 | [diff] [blame] | 270 | // Boot classpath classes should not fail initialization. This is a sanity debug check. This |
| 271 | // cannot in general be guaranteed, but in all likelihood leads to breakage down the line. |
| 272 | if (klass->GetClassLoader() == nullptr && !Runtime::Current()->IsAotCompiler()) { |
Andreas Gampe | 22f71d2 | 2016-11-21 10:10:08 -0800 | [diff] [blame] | 273 | std::string tmp; |
Alex Light | 5047d9f | 2018-03-09 15:44:31 -0800 | [diff] [blame] | 274 | // We want to LOG(FATAL) on debug builds since this really shouldn't be happening but we need to |
| 275 | // make sure to only do it if we don't have AsyncExceptions being thrown around since those |
| 276 | // could have caused the error. |
| 277 | bool known_impossible = kIsDebugBuild && !Runtime::Current()->AreAsyncExceptionsThrown(); |
| 278 | LOG(known_impossible ? FATAL : WARNING) << klass->GetDescriptor(&tmp) |
| 279 | << " failed initialization: " |
| 280 | << self->GetException()->Dump(); |
Andreas Gampe | 22f71d2 | 2016-11-21 10:10:08 -0800 | [diff] [blame] | 281 | } |
| 282 | |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 283 | env->ExceptionClear(); |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 284 | bool is_error = env->IsInstanceOf(cause.get(), WellKnownClasses::java_lang_Error); |
| 285 | env->Throw(cause.get()); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 286 | |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 287 | // We only wrap non-Error exceptions; an Error can just be used as-is. |
| 288 | if (!is_error) { |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 289 | self->ThrowNewWrappedException("Ljava/lang/ExceptionInInitializerError;", nullptr); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 290 | } |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 291 | VlogClassInitializationFailure(klass); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 292 | } |
| 293 | |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 294 | // Gap between two fields in object layout. |
| 295 | struct FieldGap { |
| 296 | uint32_t start_offset; // The offset from the start of the object. |
| 297 | uint32_t size; // The gap size of 1, 2, or 4 bytes. |
| 298 | }; |
| 299 | struct FieldGapsComparator { |
Igor Murashkin | 2ffb703 | 2017-11-08 13:35:21 -0800 | [diff] [blame] | 300 | FieldGapsComparator() { |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 301 | } |
| 302 | bool operator() (const FieldGap& lhs, const FieldGap& rhs) |
| 303 | NO_THREAD_SAFETY_ANALYSIS { |
Andreas Gampe | f52857f | 2015-02-18 15:38:57 -0800 | [diff] [blame] | 304 | // Sort by gap size, largest first. Secondary sort by starting offset. |
Richard Uhler | fab6788 | 2015-07-13 17:00:35 -0700 | [diff] [blame] | 305 | // Note that the priority queue returns the largest element, so operator() |
| 306 | // should return true if lhs is less than rhs. |
| 307 | 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] | 308 | } |
| 309 | }; |
Andreas Gampe | ee07743 | 2018-09-20 15:05:17 +0000 | [diff] [blame] | 310 | typedef std::priority_queue<FieldGap, std::vector<FieldGap>, FieldGapsComparator> FieldGaps; |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 311 | |
| 312 | // Adds largest aligned gaps to queue of gaps. |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 313 | 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] | 314 | DCHECK(gaps != nullptr); |
| 315 | |
| 316 | uint32_t current_offset = gap_start; |
| 317 | while (current_offset != gap_end) { |
| 318 | size_t remaining = gap_end - current_offset; |
| 319 | if (remaining >= sizeof(uint32_t) && IsAligned<4>(current_offset)) { |
| 320 | gaps->push(FieldGap {current_offset, sizeof(uint32_t)}); |
| 321 | current_offset += sizeof(uint32_t); |
| 322 | } else if (remaining >= sizeof(uint16_t) && IsAligned<2>(current_offset)) { |
| 323 | gaps->push(FieldGap {current_offset, sizeof(uint16_t)}); |
| 324 | current_offset += sizeof(uint16_t); |
| 325 | } else { |
| 326 | gaps->push(FieldGap {current_offset, sizeof(uint8_t)}); |
| 327 | current_offset += sizeof(uint8_t); |
| 328 | } |
| 329 | DCHECK_LE(current_offset, gap_end) << "Overran gap"; |
| 330 | } |
| 331 | } |
| 332 | // Shuffle fields forward, making use of gaps whenever possible. |
| 333 | template<int n> |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 334 | static void ShuffleForward(size_t* current_field_idx, |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 335 | MemberOffset* field_offset, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 336 | std::deque<ArtField*>* grouped_and_sorted_fields, |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 337 | FieldGaps* gaps) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 338 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 339 | DCHECK(current_field_idx != nullptr); |
| 340 | DCHECK(grouped_and_sorted_fields != nullptr); |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 341 | DCHECK(gaps != nullptr); |
| 342 | DCHECK(field_offset != nullptr); |
| 343 | |
| 344 | DCHECK(IsPowerOfTwo(n)); |
| 345 | while (!grouped_and_sorted_fields->empty()) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 346 | ArtField* field = grouped_and_sorted_fields->front(); |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 347 | Primitive::Type type = field->GetTypeAsPrimitiveType(); |
| 348 | if (Primitive::ComponentSize(type) < n) { |
| 349 | break; |
| 350 | } |
| 351 | if (!IsAligned<n>(field_offset->Uint32Value())) { |
| 352 | MemberOffset old_offset = *field_offset; |
| 353 | *field_offset = MemberOffset(RoundUp(field_offset->Uint32Value(), n)); |
| 354 | AddFieldGap(old_offset.Uint32Value(), field_offset->Uint32Value(), gaps); |
| 355 | } |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 356 | CHECK(type != Primitive::kPrimNot) << field->PrettyField(); // should be primitive types |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 357 | grouped_and_sorted_fields->pop_front(); |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 358 | if (!gaps->empty() && gaps->top().size >= n) { |
| 359 | FieldGap gap = gaps->top(); |
| 360 | gaps->pop(); |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 361 | DCHECK_ALIGNED(gap.start_offset, n); |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 362 | field->SetOffset(MemberOffset(gap.start_offset)); |
| 363 | if (gap.size > n) { |
| 364 | AddFieldGap(gap.start_offset + n, gap.start_offset + gap.size, gaps); |
| 365 | } |
| 366 | } else { |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 367 | DCHECK_ALIGNED(field_offset->Uint32Value(), n); |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 368 | field->SetOffset(*field_offset); |
| 369 | *field_offset = MemberOffset(field_offset->Uint32Value() + n); |
| 370 | } |
| 371 | ++(*current_field_idx); |
| 372 | } |
| 373 | } |
| 374 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 375 | ClassLinker::ClassLinker(InternTable* intern_table) |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 376 | : boot_class_table_(new ClassTable()), |
| 377 | failed_dex_cache_class_lookups_(0), |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 378 | class_roots_(nullptr), |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 379 | find_array_class_cache_next_victim_(0), |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 380 | init_done_(false), |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 381 | log_new_roots_(false), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 382 | intern_table_(intern_table), |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 383 | quick_resolution_trampoline_(nullptr), |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 384 | quick_imt_conflict_trampoline_(nullptr), |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 385 | quick_generic_jni_trampoline_(nullptr), |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 386 | quick_to_interpreter_bridge_trampoline_(nullptr), |
Andreas Gampe | c1ac9ee | 2017-07-24 22:35:49 -0700 | [diff] [blame] | 387 | image_pointer_size_(kRuntimePointerSize), |
Andreas Gampe | 7dface3 | 2017-07-25 21:32:59 -0700 | [diff] [blame] | 388 | cha_(Runtime::Current()->IsAotCompiler() ? nullptr : new ClassHierarchyAnalysis()) { |
| 389 | // For CHA disabled during Aot, see b/34193647. |
| 390 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 391 | CHECK(intern_table_ != nullptr); |
Andreas Gampe | 8ac7595 | 2015-06-02 21:01:45 -0700 | [diff] [blame] | 392 | static_assert(kFindArrayCacheSize == arraysize(find_array_class_cache_), |
| 393 | "Array cache size wrong."); |
| 394 | std::fill_n(find_array_class_cache_, kFindArrayCacheSize, GcRoot<mirror::Class>(nullptr)); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 395 | } |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 396 | |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 397 | void ClassLinker::CheckSystemClass(Thread* self, Handle<mirror::Class> c1, const char* descriptor) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 398 | ObjPtr<mirror::Class> c2 = FindSystemClass(self, descriptor); |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 399 | if (c2 == nullptr) { |
| 400 | LOG(FATAL) << "Could not find class " << descriptor; |
| 401 | UNREACHABLE(); |
| 402 | } |
| 403 | if (c1.Get() != c2) { |
| 404 | std::ostringstream os1, os2; |
| 405 | c1->DumpClass(os1, mirror::Class::kDumpClassFullDetail); |
| 406 | c2->DumpClass(os2, mirror::Class::kDumpClassFullDetail); |
| 407 | LOG(FATAL) << "InitWithoutImage: Class mismatch for " << descriptor |
| 408 | << ". This is most likely the result of a broken build. Make sure that " |
| 409 | << "libcore and art projects match.\n\n" |
| 410 | << os1.str() << "\n\n" << os2.str(); |
| 411 | UNREACHABLE(); |
| 412 | } |
| 413 | } |
| 414 | |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 415 | bool ClassLinker::InitWithoutImage(std::vector<std::unique_ptr<const DexFile>> boot_class_path, |
| 416 | std::string* error_msg) { |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 417 | VLOG(startup) << "ClassLinker::Init"; |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 418 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 419 | Thread* const self = Thread::Current(); |
| 420 | Runtime* const runtime = Runtime::Current(); |
| 421 | gc::Heap* const heap = runtime->GetHeap(); |
| 422 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 423 | CHECK(!heap->HasBootImageSpace()) << "Runtime has image. We should use it."; |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 424 | CHECK(!init_done_); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 425 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 426 | // Use the pointer size from the runtime since we are probably creating the image. |
| 427 | image_pointer_size_ = InstructionSetPointerSize(runtime->GetInstructionSet()); |
| 428 | |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 429 | // java_lang_Class comes first, it's needed for AllocClass |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 430 | // 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] | 431 | heap->IncrementDisableMovingGC(self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 432 | StackHandleScope<64> hs(self); // 64 is picked arbitrarily. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 433 | auto class_class_size = mirror::Class::ClassClassSize(image_pointer_size_); |
Mathieu Chartier | d7a7f2f | 2018-09-07 11:57:18 -0700 | [diff] [blame] | 434 | // Allocate the object as non-movable so that there are no cases where Object::IsClass returns |
| 435 | // the incorrect result when comparing to-space vs from-space. |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 436 | Handle<mirror::Class> java_lang_Class(hs.NewHandle(ObjPtr<mirror::Class>::DownCast(MakeObjPtr( |
| 437 | heap->AllocNonMovableObject<true>(self, nullptr, class_class_size, VoidFunctor()))))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 438 | CHECK(java_lang_Class != nullptr); |
Vladimir Marko | 317892b | 2018-05-31 11:11:32 +0100 | [diff] [blame] | 439 | java_lang_Class->SetClassFlags(mirror::kClassFlagClass); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 440 | java_lang_Class->SetClass(java_lang_Class.Get()); |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 441 | if (kUseBakerReadBarrier) { |
| 442 | java_lang_Class->AssertReadBarrierState(); |
Hiroshi Yamauchi | 9d04a20 | 2014-01-31 13:35:49 -0800 | [diff] [blame] | 443 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 444 | java_lang_Class->SetClassSize(class_class_size); |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 445 | java_lang_Class->SetPrimitiveType(Primitive::kPrimNot); |
Mathieu Chartier | 1d27b34 | 2014-01-28 12:51:09 -0800 | [diff] [blame] | 446 | heap->DecrementDisableMovingGC(self); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 447 | // AllocClass(ObjPtr<mirror::Class>) can now be used |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 448 | |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 449 | // Class[] is used for reflection support. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 450 | 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] | 451 | Handle<mirror::Class> class_array_class(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 452 | AllocClass(self, java_lang_Class.Get(), class_array_class_size))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 453 | class_array_class->SetComponentType(java_lang_Class.Get()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 454 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 455 | // 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] | 456 | Handle<mirror::Class> java_lang_Object(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 457 | AllocClass(self, java_lang_Class.Get(), mirror::Object::ClassSize(image_pointer_size_)))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 458 | CHECK(java_lang_Object != nullptr); |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 459 | // backfill Object as the super class of Class. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 460 | java_lang_Class->SetSuperClass(java_lang_Object.Get()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 461 | mirror::Class::SetStatus(java_lang_Object, ClassStatus::kLoaded, self); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 462 | |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 463 | java_lang_Object->SetObjectSize(sizeof(mirror::Object)); |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 464 | // Allocate in non-movable so that it's possible to check if a JNI weak global ref has been |
| 465 | // cleared without triggering the read barrier and unintentionally mark the sentinel alive. |
| 466 | runtime->SetSentinel(heap->AllocNonMovableObject<true>(self, |
| 467 | java_lang_Object.Get(), |
| 468 | java_lang_Object->GetObjectSize(), |
| 469 | VoidFunctor())); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 470 | |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 471 | // Initialize the SubtypeCheck bitstring for java.lang.Object and java.lang.Class. |
Vladimir Marko | 305c38b | 2018-02-14 11:50:07 +0000 | [diff] [blame] | 472 | if (kBitstringSubtypeCheckEnabled) { |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 473 | // It might seem the lock here is unnecessary, however all the SubtypeCheck |
| 474 | // functions are annotated to require locks all the way down. |
| 475 | // |
| 476 | // We take the lock here to avoid using NO_THREAD_SAFETY_ANALYSIS. |
| 477 | MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_); |
Vladimir Marko | 38b8b25 | 2018-01-02 19:07:06 +0000 | [diff] [blame] | 478 | SubtypeCheck<ObjPtr<mirror::Class>>::EnsureInitialized(java_lang_Object.Get()); |
| 479 | SubtypeCheck<ObjPtr<mirror::Class>>::EnsureInitialized(java_lang_Class.Get()); |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 480 | } |
| 481 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 482 | // Object[] next to hold class roots. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 483 | Handle<mirror::Class> object_array_class(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 484 | AllocClass(self, java_lang_Class.Get(), |
| 485 | mirror::ObjectArray<mirror::Object>::ClassSize(image_pointer_size_)))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 486 | object_array_class->SetComponentType(java_lang_Object.Get()); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 487 | |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 488 | // Setup java.lang.String. |
| 489 | // |
| 490 | // We make this class non-movable for the unlikely case where it were to be |
| 491 | // moved by a sticky-bit (minor) collection when using the Generational |
| 492 | // Concurrent Copying (CC) collector, potentially creating a stale reference |
| 493 | // in the `klass_` field of one of its instances allocated in the Large-Object |
| 494 | // Space (LOS) -- see the comment about the dirty card scanning logic in |
| 495 | // art::gc::collector::ConcurrentCopying::MarkingPhase. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 496 | Handle<mirror::Class> java_lang_String(hs.NewHandle( |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 497 | AllocClass</* kMovable */ false>( |
| 498 | self, java_lang_Class.Get(), mirror::String::ClassSize(image_pointer_size_)))); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 499 | java_lang_String->SetStringClass(); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 500 | mirror::Class::SetStatus(java_lang_String, ClassStatus::kResolved, self); |
Jesse Wilson | 1415074 | 2011-07-29 19:04:44 -0400 | [diff] [blame] | 501 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 502 | // Setup java.lang.ref.Reference. |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 503 | Handle<mirror::Class> java_lang_ref_Reference(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 504 | AllocClass(self, java_lang_Class.Get(), mirror::Reference::ClassSize(image_pointer_size_)))); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 505 | java_lang_ref_Reference->SetObjectSize(mirror::Reference::InstanceSize()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 506 | mirror::Class::SetStatus(java_lang_ref_Reference, ClassStatus::kResolved, self); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 507 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 508 | // 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] | 509 | class_roots_ = GcRoot<mirror::ObjectArray<mirror::Class>>( |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 510 | mirror::ObjectArray<mirror::Class>::Alloc(self, |
| 511 | object_array_class.Get(), |
| 512 | static_cast<int32_t>(ClassRoot::kMax))); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 513 | CHECK(!class_roots_.IsNull()); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 514 | SetClassRoot(ClassRoot::kJavaLangClass, java_lang_Class.Get()); |
| 515 | SetClassRoot(ClassRoot::kJavaLangObject, java_lang_Object.Get()); |
| 516 | SetClassRoot(ClassRoot::kClassArrayClass, class_array_class.Get()); |
| 517 | SetClassRoot(ClassRoot::kObjectArrayClass, object_array_class.Get()); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 518 | SetClassRoot(ClassRoot::kJavaLangString, java_lang_String.Get()); |
| 519 | SetClassRoot(ClassRoot::kJavaLangRefReference, java_lang_ref_Reference.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 520 | |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 521 | // Fill in the empty iftable. Needs to be done after the kObjectArrayClass root is set. |
| 522 | java_lang_Object->SetIfTable(AllocIfTable(self, 0)); |
| 523 | |
Vladimir Marko | 0261055 | 2018-06-04 14:38:00 +0100 | [diff] [blame] | 524 | // Create array interface entries to populate once we can load system classes. |
| 525 | object_array_class->SetIfTable(AllocIfTable(self, 2)); |
| 526 | DCHECK_EQ(GetArrayIfTable(), object_array_class->GetIfTable()); |
| 527 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 528 | // Setup the primitive type classes. |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 529 | SetClassRoot(ClassRoot::kPrimitiveBoolean, CreatePrimitiveClass(self, Primitive::kPrimBoolean)); |
| 530 | SetClassRoot(ClassRoot::kPrimitiveByte, CreatePrimitiveClass(self, Primitive::kPrimByte)); |
Vladimir Marko | acb906d | 2018-05-30 10:23:49 +0100 | [diff] [blame] | 531 | SetClassRoot(ClassRoot::kPrimitiveChar, CreatePrimitiveClass(self, Primitive::kPrimChar)); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 532 | SetClassRoot(ClassRoot::kPrimitiveShort, CreatePrimitiveClass(self, Primitive::kPrimShort)); |
| 533 | SetClassRoot(ClassRoot::kPrimitiveInt, CreatePrimitiveClass(self, Primitive::kPrimInt)); |
| 534 | SetClassRoot(ClassRoot::kPrimitiveLong, CreatePrimitiveClass(self, Primitive::kPrimLong)); |
| 535 | SetClassRoot(ClassRoot::kPrimitiveFloat, CreatePrimitiveClass(self, Primitive::kPrimFloat)); |
| 536 | SetClassRoot(ClassRoot::kPrimitiveDouble, CreatePrimitiveClass(self, Primitive::kPrimDouble)); |
| 537 | SetClassRoot(ClassRoot::kPrimitiveVoid, CreatePrimitiveClass(self, Primitive::kPrimVoid)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 538 | |
Vladimir Marko | acb906d | 2018-05-30 10:23:49 +0100 | [diff] [blame] | 539 | // Create int array type for native pointer arrays (for example vtables) on 32-bit archs. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 540 | Handle<mirror::Class> int_array_class(hs.NewHandle( |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 541 | AllocPrimitiveArrayClass(self, java_lang_Class.Get()))); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 542 | int_array_class->SetComponentType(GetClassRoot(ClassRoot::kPrimitiveInt, this)); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 543 | SetClassRoot(ClassRoot::kIntArrayClass, int_array_class.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 544 | |
Vladimir Marko | acb906d | 2018-05-30 10:23:49 +0100 | [diff] [blame] | 545 | // Create long array type for native pointer arrays (for example vtables) on 64-bit archs. |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 546 | Handle<mirror::Class> long_array_class(hs.NewHandle( |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 547 | AllocPrimitiveArrayClass(self, java_lang_Class.Get()))); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 548 | long_array_class->SetComponentType(GetClassRoot(ClassRoot::kPrimitiveLong, this)); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 549 | SetClassRoot(ClassRoot::kLongArrayClass, long_array_class.Get()); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 550 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 551 | // now that these are registered, we can use AllocClass() and AllocObjectArray |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 552 | |
Ian Rogers | 52813c9 | 2012-10-11 11:50:38 -0700 | [diff] [blame] | 553 | // Set up DexCache. This cannot be done later since AppendToBootClassPath calls AllocDexCache. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 554 | Handle<mirror::Class> java_lang_DexCache(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 555 | AllocClass(self, java_lang_Class.Get(), mirror::DexCache::ClassSize(image_pointer_size_)))); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 556 | SetClassRoot(ClassRoot::kJavaLangDexCache, java_lang_DexCache.Get()); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 557 | java_lang_DexCache->SetDexCacheClass(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 558 | java_lang_DexCache->SetObjectSize(mirror::DexCache::InstanceSize()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 559 | mirror::Class::SetStatus(java_lang_DexCache, ClassStatus::kResolved, self); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 560 | |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 561 | |
| 562 | // Setup dalvik.system.ClassExt |
| 563 | Handle<mirror::Class> dalvik_system_ClassExt(hs.NewHandle( |
| 564 | AllocClass(self, java_lang_Class.Get(), mirror::ClassExt::ClassSize(image_pointer_size_)))); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 565 | SetClassRoot(ClassRoot::kDalvikSystemClassExt, dalvik_system_ClassExt.Get()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 566 | mirror::Class::SetStatus(dalvik_system_ClassExt, ClassStatus::kResolved, self); |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 567 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 568 | // Set up array classes for string, field, method |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 569 | Handle<mirror::Class> object_array_string(hs.NewHandle( |
| 570 | AllocClass(self, java_lang_Class.Get(), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 571 | mirror::ObjectArray<mirror::String>::ClassSize(image_pointer_size_)))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 572 | object_array_string->SetComponentType(java_lang_String.Get()); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 573 | SetClassRoot(ClassRoot::kJavaLangStringArrayClass, object_array_string.Get()); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 574 | |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 575 | LinearAlloc* linear_alloc = runtime->GetLinearAlloc(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 576 | // Create runtime resolution and imt conflict methods. |
| 577 | runtime->SetResolutionMethod(runtime->CreateResolutionMethod()); |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 578 | runtime->SetImtConflictMethod(runtime->CreateImtConflictMethod(linear_alloc)); |
| 579 | runtime->SetImtUnimplementedMethod(runtime->CreateImtConflictMethod(linear_alloc)); |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 580 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 581 | // Setup boot_class_path_ and register class_path now that we can use AllocObjectArray to create |
| 582 | // DexCache instances. Needs to be after String, Field, Method arrays since AllocDexCache uses |
| 583 | // these roots. |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 584 | if (boot_class_path.empty()) { |
| 585 | *error_msg = "Boot classpath is empty."; |
| 586 | return false; |
| 587 | } |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 588 | for (auto& dex_file : boot_class_path) { |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 589 | if (dex_file.get() == nullptr) { |
| 590 | *error_msg = "Null dex file."; |
| 591 | return false; |
| 592 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 593 | AppendToBootClassPath(self, *dex_file); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 594 | boot_dex_files_.push_back(std::move(dex_file)); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 595 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 596 | |
| 597 | // now we can use FindSystemClass |
| 598 | |
Dmitry Petrochenko | f0972a4 | 2014-05-16 17:43:39 +0700 | [diff] [blame] | 599 | // Set up GenericJNI entrypoint. That is mainly a hack for common_compiler_test.h so that |
| 600 | // we do not need friend classes or a publicly exposed setter. |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 601 | quick_generic_jni_trampoline_ = GetQuickGenericJniStub(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 602 | if (!runtime->IsAotCompiler()) { |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 603 | // 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] | 604 | quick_resolution_trampoline_ = GetQuickResolutionStub(); |
| 605 | quick_imt_conflict_trampoline_ = GetQuickImtConflictStub(); |
| 606 | quick_to_interpreter_bridge_trampoline_ = GetQuickToInterpreterBridge(); |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 607 | } |
Dmitry Petrochenko | f0972a4 | 2014-05-16 17:43:39 +0700 | [diff] [blame] | 608 | |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 609 | // Object, String, ClassExt and DexCache need to be rerun through FindSystemClass to finish init |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 610 | mirror::Class::SetStatus(java_lang_Object, ClassStatus::kNotReady, self); |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 611 | CheckSystemClass(self, java_lang_Object, "Ljava/lang/Object;"); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 612 | CHECK_EQ(java_lang_Object->GetObjectSize(), mirror::Object::InstanceSize()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 613 | mirror::Class::SetStatus(java_lang_String, ClassStatus::kNotReady, self); |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 614 | CheckSystemClass(self, java_lang_String, "Ljava/lang/String;"); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 615 | mirror::Class::SetStatus(java_lang_DexCache, ClassStatus::kNotReady, self); |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 616 | CheckSystemClass(self, java_lang_DexCache, "Ljava/lang/DexCache;"); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 617 | CHECK_EQ(java_lang_DexCache->GetObjectSize(), mirror::DexCache::InstanceSize()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 618 | mirror::Class::SetStatus(dalvik_system_ClassExt, ClassStatus::kNotReady, self); |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 619 | CheckSystemClass(self, dalvik_system_ClassExt, "Ldalvik/system/ClassExt;"); |
| 620 | CHECK_EQ(dalvik_system_ClassExt->GetObjectSize(), mirror::ClassExt::InstanceSize()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 621 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 622 | // Setup the primitive array type classes - can't be done until Object has a vtable. |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 623 | AllocAndSetPrimitiveArrayClassRoot(self, |
| 624 | java_lang_Class.Get(), |
| 625 | ClassRoot::kBooleanArrayClass, |
| 626 | ClassRoot::kPrimitiveBoolean, |
| 627 | "[Z"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 628 | |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 629 | AllocAndSetPrimitiveArrayClassRoot( |
| 630 | self, java_lang_Class.Get(), ClassRoot::kByteArrayClass, ClassRoot::kPrimitiveByte, "[B"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 631 | |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 632 | AllocAndSetPrimitiveArrayClassRoot( |
| 633 | self, java_lang_Class.Get(), ClassRoot::kCharArrayClass, ClassRoot::kPrimitiveChar, "[C"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 634 | |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 635 | AllocAndSetPrimitiveArrayClassRoot( |
| 636 | self, java_lang_Class.Get(), ClassRoot::kShortArrayClass, ClassRoot::kPrimitiveShort, "[S"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 637 | |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 638 | CheckSystemClass(self, int_array_class, "[I"); |
| 639 | CheckSystemClass(self, long_array_class, "[J"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 640 | |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 641 | AllocAndSetPrimitiveArrayClassRoot( |
| 642 | self, java_lang_Class.Get(), ClassRoot::kFloatArrayClass, ClassRoot::kPrimitiveFloat, "[F"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 643 | |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 644 | AllocAndSetPrimitiveArrayClassRoot( |
| 645 | self, java_lang_Class.Get(), ClassRoot::kDoubleArrayClass, ClassRoot::kPrimitiveDouble, "[D"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 646 | |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 647 | // Run Class through FindSystemClass. This initializes the dex_cache_ fields and register it |
| 648 | // in class_table_. |
| 649 | CheckSystemClass(self, java_lang_Class, "Ljava/lang/Class;"); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 650 | |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 651 | CheckSystemClass(self, class_array_class, "[Ljava/lang/Class;"); |
| 652 | CheckSystemClass(self, object_array_class, "[Ljava/lang/Object;"); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 653 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 654 | // Setup the single, global copy of "iftable". |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 655 | auto java_lang_Cloneable = hs.NewHandle(FindSystemClass(self, "Ljava/lang/Cloneable;")); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 656 | CHECK(java_lang_Cloneable != nullptr); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 657 | auto java_io_Serializable = hs.NewHandle(FindSystemClass(self, "Ljava/io/Serializable;")); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 658 | CHECK(java_io_Serializable != nullptr); |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 659 | // We assume that Cloneable/Serializable don't have superinterfaces -- normally we'd have to |
| 660 | // crawl up and explicitly list all of the supers as well. |
Vladimir Marko | 0261055 | 2018-06-04 14:38:00 +0100 | [diff] [blame] | 661 | object_array_class->GetIfTable()->SetInterface(0, java_lang_Cloneable.Get()); |
| 662 | object_array_class->GetIfTable()->SetInterface(1, java_io_Serializable.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 663 | |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 664 | // Sanity check Class[] and Object[]'s interfaces. GetDirectInterface may cause thread |
| 665 | // suspension. |
| 666 | CHECK_EQ(java_lang_Cloneable.Get(), |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 667 | mirror::Class::GetDirectInterface(self, class_array_class.Get(), 0)); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 668 | CHECK_EQ(java_io_Serializable.Get(), |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 669 | mirror::Class::GetDirectInterface(self, class_array_class.Get(), 1)); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 670 | CHECK_EQ(java_lang_Cloneable.Get(), |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 671 | mirror::Class::GetDirectInterface(self, object_array_class.Get(), 0)); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 672 | CHECK_EQ(java_io_Serializable.Get(), |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 673 | mirror::Class::GetDirectInterface(self, object_array_class.Get(), 1)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 674 | |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 675 | CHECK_EQ(object_array_string.Get(), |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 676 | FindSystemClass(self, GetClassRootDescriptor(ClassRoot::kJavaLangStringArrayClass))); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 677 | |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 678 | // 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] | 679 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 680 | // Create java.lang.reflect.Proxy root. |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 681 | SetClassRoot(ClassRoot::kJavaLangReflectProxy, |
| 682 | FindSystemClass(self, "Ljava/lang/reflect/Proxy;")); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 683 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 684 | // Create java.lang.reflect.Field.class root. |
Vladimir Marko | acb906d | 2018-05-30 10:23:49 +0100 | [diff] [blame] | 685 | ObjPtr<mirror::Class> class_root = FindSystemClass(self, "Ljava/lang/reflect/Field;"); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 686 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 687 | SetClassRoot(ClassRoot::kJavaLangReflectField, class_root); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 688 | |
| 689 | // Create java.lang.reflect.Field array root. |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 690 | class_root = FindSystemClass(self, "[Ljava/lang/reflect/Field;"); |
| 691 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 692 | SetClassRoot(ClassRoot::kJavaLangReflectFieldArrayClass, class_root); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 693 | |
| 694 | // Create java.lang.reflect.Constructor.class root and array root. |
| 695 | class_root = FindSystemClass(self, "Ljava/lang/reflect/Constructor;"); |
| 696 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 697 | SetClassRoot(ClassRoot::kJavaLangReflectConstructor, class_root); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 698 | class_root = FindSystemClass(self, "[Ljava/lang/reflect/Constructor;"); |
| 699 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 700 | SetClassRoot(ClassRoot::kJavaLangReflectConstructorArrayClass, class_root); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 701 | |
| 702 | // Create java.lang.reflect.Method.class root and array root. |
| 703 | class_root = FindSystemClass(self, "Ljava/lang/reflect/Method;"); |
| 704 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 705 | SetClassRoot(ClassRoot::kJavaLangReflectMethod, class_root); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 706 | class_root = FindSystemClass(self, "[Ljava/lang/reflect/Method;"); |
| 707 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 708 | SetClassRoot(ClassRoot::kJavaLangReflectMethodArrayClass, class_root); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 709 | |
Orion Hodson | 005ac51 | 2017-10-24 15:43:43 +0100 | [diff] [blame] | 710 | // Create java.lang.invoke.CallSite.class root |
| 711 | class_root = FindSystemClass(self, "Ljava/lang/invoke/CallSite;"); |
| 712 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 713 | SetClassRoot(ClassRoot::kJavaLangInvokeCallSite, class_root); |
Orion Hodson | 005ac51 | 2017-10-24 15:43:43 +0100 | [diff] [blame] | 714 | |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 715 | // Create java.lang.invoke.MethodType.class root |
| 716 | class_root = FindSystemClass(self, "Ljava/lang/invoke/MethodType;"); |
| 717 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 718 | SetClassRoot(ClassRoot::kJavaLangInvokeMethodType, class_root); |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 719 | |
| 720 | // Create java.lang.invoke.MethodHandleImpl.class root |
| 721 | class_root = FindSystemClass(self, "Ljava/lang/invoke/MethodHandleImpl;"); |
| 722 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 723 | SetClassRoot(ClassRoot::kJavaLangInvokeMethodHandleImpl, class_root); |
Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 724 | SetClassRoot(ClassRoot::kJavaLangInvokeMethodHandle, class_root->GetSuperClass()); |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 725 | |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 726 | // Create java.lang.invoke.MethodHandles.Lookup.class root |
| 727 | class_root = FindSystemClass(self, "Ljava/lang/invoke/MethodHandles$Lookup;"); |
| 728 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 729 | SetClassRoot(ClassRoot::kJavaLangInvokeMethodHandlesLookup, class_root); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 730 | |
Orion Hodson | 005ac51 | 2017-10-24 15:43:43 +0100 | [diff] [blame] | 731 | // Create java.lang.invoke.VarHandle.class root |
| 732 | class_root = FindSystemClass(self, "Ljava/lang/invoke/VarHandle;"); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 733 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 734 | SetClassRoot(ClassRoot::kJavaLangInvokeVarHandle, class_root); |
Orion Hodson | 005ac51 | 2017-10-24 15:43:43 +0100 | [diff] [blame] | 735 | |
| 736 | // Create java.lang.invoke.FieldVarHandle.class root |
| 737 | class_root = FindSystemClass(self, "Ljava/lang/invoke/FieldVarHandle;"); |
| 738 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 739 | SetClassRoot(ClassRoot::kJavaLangInvokeFieldVarHandle, class_root); |
Orion Hodson | 005ac51 | 2017-10-24 15:43:43 +0100 | [diff] [blame] | 740 | |
| 741 | // Create java.lang.invoke.ArrayElementVarHandle.class root |
| 742 | class_root = FindSystemClass(self, "Ljava/lang/invoke/ArrayElementVarHandle;"); |
| 743 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 744 | SetClassRoot(ClassRoot::kJavaLangInvokeArrayElementVarHandle, class_root); |
Orion Hodson | 005ac51 | 2017-10-24 15:43:43 +0100 | [diff] [blame] | 745 | |
| 746 | // Create java.lang.invoke.ByteArrayViewVarHandle.class root |
| 747 | class_root = FindSystemClass(self, "Ljava/lang/invoke/ByteArrayViewVarHandle;"); |
| 748 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 749 | SetClassRoot(ClassRoot::kJavaLangInvokeByteArrayViewVarHandle, class_root); |
Orion Hodson | 005ac51 | 2017-10-24 15:43:43 +0100 | [diff] [blame] | 750 | |
| 751 | // Create java.lang.invoke.ByteBufferViewVarHandle.class root |
| 752 | class_root = FindSystemClass(self, "Ljava/lang/invoke/ByteBufferViewVarHandle;"); |
| 753 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 754 | SetClassRoot(ClassRoot::kJavaLangInvokeByteBufferViewVarHandle, class_root); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 755 | |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 756 | class_root = FindSystemClass(self, "Ldalvik/system/EmulatedStackFrame;"); |
| 757 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 758 | SetClassRoot(ClassRoot::kDalvikSystemEmulatedStackFrame, class_root); |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 759 | |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 760 | // 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] | 761 | // finish initializing Reference class |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 762 | mirror::Class::SetStatus(java_lang_ref_Reference, ClassStatus::kNotReady, self); |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 763 | CheckSystemClass(self, java_lang_ref_Reference, "Ljava/lang/ref/Reference;"); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 764 | CHECK_EQ(java_lang_ref_Reference->GetObjectSize(), mirror::Reference::InstanceSize()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 765 | CHECK_EQ(java_lang_ref_Reference->GetClassSize(), |
| 766 | mirror::Reference::ClassSize(image_pointer_size_)); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 767 | class_root = FindSystemClass(self, "Ljava/lang/ref/FinalizerReference;"); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 768 | CHECK_EQ(class_root->GetClassFlags(), mirror::kClassFlagNormal); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 769 | class_root->SetClassFlags(class_root->GetClassFlags() | mirror::kClassFlagFinalizerReference); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 770 | class_root = FindSystemClass(self, "Ljava/lang/ref/PhantomReference;"); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 771 | CHECK_EQ(class_root->GetClassFlags(), mirror::kClassFlagNormal); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 772 | class_root->SetClassFlags(class_root->GetClassFlags() | mirror::kClassFlagPhantomReference); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 773 | class_root = FindSystemClass(self, "Ljava/lang/ref/SoftReference;"); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 774 | CHECK_EQ(class_root->GetClassFlags(), mirror::kClassFlagNormal); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 775 | class_root->SetClassFlags(class_root->GetClassFlags() | mirror::kClassFlagSoftReference); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 776 | class_root = FindSystemClass(self, "Ljava/lang/ref/WeakReference;"); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 777 | CHECK_EQ(class_root->GetClassFlags(), mirror::kClassFlagNormal); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 778 | class_root->SetClassFlags(class_root->GetClassFlags() | mirror::kClassFlagWeakReference); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 779 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 780 | // Setup the ClassLoader, verifying the object_size_. |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 781 | class_root = FindSystemClass(self, "Ljava/lang/ClassLoader;"); |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 782 | class_root->SetClassLoaderClass(); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 783 | CHECK_EQ(class_root->GetObjectSize(), mirror::ClassLoader::InstanceSize()); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 784 | SetClassRoot(ClassRoot::kJavaLangClassLoader, class_root); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 785 | |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 786 | // Set up java.lang.Throwable, java.lang.ClassNotFoundException, and |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 787 | // java.lang.StackTraceElement as a convenience. |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 788 | SetClassRoot(ClassRoot::kJavaLangThrowable, FindSystemClass(self, "Ljava/lang/Throwable;")); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 789 | SetClassRoot(ClassRoot::kJavaLangClassNotFoundException, |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 790 | FindSystemClass(self, "Ljava/lang/ClassNotFoundException;")); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 791 | SetClassRoot(ClassRoot::kJavaLangStackTraceElement, |
| 792 | FindSystemClass(self, "Ljava/lang/StackTraceElement;")); |
| 793 | SetClassRoot(ClassRoot::kJavaLangStackTraceElementArrayClass, |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 794 | FindSystemClass(self, "[Ljava/lang/StackTraceElement;")); |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 795 | |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 796 | // Create conflict tables that depend on the class linker. |
| 797 | runtime->FixupConflictTables(); |
| 798 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 799 | FinishInit(self); |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 800 | |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 801 | VLOG(startup) << "ClassLinker::InitFromCompiler exiting"; |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 802 | |
| 803 | return true; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 804 | } |
| 805 | |
Andreas Gampe | 9abc31e | 2018-05-17 11:47:09 -0700 | [diff] [blame] | 806 | static void CreateStringInitBindings(Thread* self, ClassLinker* class_linker) |
| 807 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 808 | // Find String.<init> -> StringFactory bindings. |
| 809 | ObjPtr<mirror::Class> string_factory_class = |
| 810 | class_linker->FindSystemClass(self, "Ljava/lang/StringFactory;"); |
| 811 | CHECK(string_factory_class != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 812 | ObjPtr<mirror::Class> string_class = GetClassRoot<mirror::String>(class_linker); |
Andreas Gampe | 9abc31e | 2018-05-17 11:47:09 -0700 | [diff] [blame] | 813 | WellKnownClasses::InitStringInit(string_class, string_factory_class); |
| 814 | // Update the primordial thread. |
| 815 | self->InitStringEntryPoints(); |
| 816 | } |
| 817 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 818 | void ClassLinker::FinishInit(Thread* self) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 819 | VLOG(startup) << "ClassLinker::FinishInit entering"; |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 820 | |
Andreas Gampe | 9abc31e | 2018-05-17 11:47:09 -0700 | [diff] [blame] | 821 | CreateStringInitBindings(self, this); |
| 822 | |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 823 | // Let the heap know some key offsets into java.lang.ref instances |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 824 | // Note: we hard code the field indexes here rather than using FindInstanceField |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 825 | // as the types of the field can't be resolved prior to the runtime being |
| 826 | // fully initialized |
Andreas Gampe | 7b2450e | 2018-06-19 10:45:54 -0700 | [diff] [blame] | 827 | StackHandleScope<3> hs(self); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 828 | Handle<mirror::Class> java_lang_ref_Reference = |
| 829 | hs.NewHandle(GetClassRoot<mirror::Reference>(this)); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 830 | Handle<mirror::Class> java_lang_ref_FinalizerReference = |
| 831 | hs.NewHandle(FindSystemClass(self, "Ljava/lang/ref/FinalizerReference;")); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 832 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 833 | ArtField* pendingNext = java_lang_ref_Reference->GetInstanceField(0); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 834 | CHECK_STREQ(pendingNext->GetName(), "pendingNext"); |
| 835 | CHECK_STREQ(pendingNext->GetTypeDescriptor(), "Ljava/lang/ref/Reference;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 836 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 837 | ArtField* queue = java_lang_ref_Reference->GetInstanceField(1); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 838 | CHECK_STREQ(queue->GetName(), "queue"); |
| 839 | CHECK_STREQ(queue->GetTypeDescriptor(), "Ljava/lang/ref/ReferenceQueue;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 840 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 841 | ArtField* queueNext = java_lang_ref_Reference->GetInstanceField(2); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 842 | CHECK_STREQ(queueNext->GetName(), "queueNext"); |
| 843 | CHECK_STREQ(queueNext->GetTypeDescriptor(), "Ljava/lang/ref/Reference;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 844 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 845 | ArtField* referent = java_lang_ref_Reference->GetInstanceField(3); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 846 | CHECK_STREQ(referent->GetName(), "referent"); |
| 847 | CHECK_STREQ(referent->GetTypeDescriptor(), "Ljava/lang/Object;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 848 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 849 | ArtField* zombie = java_lang_ref_FinalizerReference->GetInstanceField(2); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 850 | CHECK_STREQ(zombie->GetName(), "zombie"); |
| 851 | CHECK_STREQ(zombie->GetTypeDescriptor(), "Ljava/lang/Object;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 852 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 853 | // ensure all class_roots_ are initialized |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 854 | for (size_t i = 0; i < static_cast<size_t>(ClassRoot::kMax); i++) { |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 855 | ClassRoot class_root = static_cast<ClassRoot>(i); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 856 | ObjPtr<mirror::Class> klass = GetClassRoot(class_root); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 857 | CHECK(klass != nullptr); |
| 858 | DCHECK(klass->IsArrayClass() || klass->IsPrimitive() || klass->GetDexCache() != nullptr); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 859 | // note SetClassRoot does additional validation. |
| 860 | // if possible add new checks there to catch errors early |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 861 | } |
| 862 | |
Vladimir Marko | 0261055 | 2018-06-04 14:38:00 +0100 | [diff] [blame] | 863 | CHECK(GetArrayIfTable() != nullptr); |
Elliott Hughes | 92f14b2 | 2011-10-06 12:29:54 -0700 | [diff] [blame] | 864 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 865 | // disable the slow paths in FindClass and CreatePrimitiveClass now |
| 866 | // that Object, Class, and Object[] are setup |
| 867 | init_done_ = true; |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 868 | |
Andreas Gampe | 7b2450e | 2018-06-19 10:45:54 -0700 | [diff] [blame] | 869 | // Under sanitization, the small carve-out to handle stack overflow might not be enough to |
| 870 | // initialize the StackOverflowError class (as it might require running the verifier). Instead, |
| 871 | // ensure that the class will be initialized. |
| 872 | if (kMemoryToolIsAvailable && !Runtime::Current()->IsAotCompiler()) { |
| 873 | verifier::MethodVerifier::Init(); // Need to prepare the verifier. |
| 874 | |
| 875 | ObjPtr<mirror::Class> soe_klass = FindSystemClass(self, "Ljava/lang/StackOverflowError;"); |
| 876 | if (soe_klass == nullptr || !EnsureInitialized(self, hs.NewHandle(soe_klass), true, true)) { |
| 877 | // Strange, but don't crash. |
| 878 | LOG(WARNING) << "Could not prepare StackOverflowError."; |
| 879 | self->ClearException(); |
| 880 | } |
| 881 | } |
| 882 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 883 | VLOG(startup) << "ClassLinker::FinishInit exiting"; |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 884 | } |
| 885 | |
Vladimir Marko | dcfcce4 | 2018-06-27 10:00:28 +0000 | [diff] [blame] | 886 | void ClassLinker::RunRootClinits(Thread* self) { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 887 | for (size_t i = 0; i < static_cast<size_t>(ClassRoot::kMax); ++i) { |
| 888 | ObjPtr<mirror::Class> c = GetClassRoot(ClassRoot(i), this); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 889 | if (!c->IsArrayClass() && !c->IsPrimitive()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 890 | StackHandleScope<1> hs(self); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 891 | Handle<mirror::Class> h_class(hs.NewHandle(c)); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 892 | EnsureInitialized(self, h_class, true, true); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 893 | self->AssertNoPendingException(); |
Vladimir Marko | dcfcce4 | 2018-06-27 10:00:28 +0000 | [diff] [blame] | 894 | } else { |
| 895 | DCHECK(c->IsInitialized()); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 896 | } |
| 897 | } |
| 898 | } |
| 899 | |
Sebastien Hertz | 46e857a | 2015-08-06 12:52:43 +0200 | [diff] [blame] | 900 | // Set image methods' entry point to interpreter. |
| 901 | class SetInterpreterEntrypointArtMethodVisitor : public ArtMethodVisitor { |
| 902 | public: |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 903 | explicit SetInterpreterEntrypointArtMethodVisitor(PointerSize image_pointer_size) |
Sebastien Hertz | 46e857a | 2015-08-06 12:52:43 +0200 | [diff] [blame] | 904 | : image_pointer_size_(image_pointer_size) {} |
| 905 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 906 | void Visit(ArtMethod* method) override REQUIRES_SHARED(Locks::mutator_lock_) { |
Sebastien Hertz | 46e857a | 2015-08-06 12:52:43 +0200 | [diff] [blame] | 907 | if (kIsDebugBuild && !method->IsRuntimeMethod()) { |
| 908 | CHECK(method->GetDeclaringClass() != nullptr); |
| 909 | } |
| 910 | if (!method->IsNative() && !method->IsRuntimeMethod() && !method->IsResolutionMethod()) { |
| 911 | method->SetEntryPointFromQuickCompiledCodePtrSize(GetQuickToInterpreterBridge(), |
| 912 | image_pointer_size_); |
| 913 | } |
| 914 | } |
| 915 | |
| 916 | private: |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 917 | const PointerSize image_pointer_size_; |
Sebastien Hertz | 46e857a | 2015-08-06 12:52:43 +0200 | [diff] [blame] | 918 | |
| 919 | DISALLOW_COPY_AND_ASSIGN(SetInterpreterEntrypointArtMethodVisitor); |
| 920 | }; |
| 921 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 922 | struct TrampolineCheckData { |
| 923 | const void* quick_resolution_trampoline; |
| 924 | const void* quick_imt_conflict_trampoline; |
| 925 | const void* quick_generic_jni_trampoline; |
| 926 | const void* quick_to_interpreter_bridge_trampoline; |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 927 | PointerSize pointer_size; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 928 | ArtMethod* m; |
| 929 | bool error; |
| 930 | }; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 931 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 932 | bool ClassLinker::InitFromBootImage(std::string* error_msg) { |
| 933 | VLOG(startup) << __FUNCTION__ << " entering"; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 934 | CHECK(!init_done_); |
| 935 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 936 | Runtime* const runtime = Runtime::Current(); |
| 937 | Thread* const self = Thread::Current(); |
| 938 | gc::Heap* const heap = runtime->GetHeap(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 939 | std::vector<gc::space::ImageSpace*> spaces = heap->GetBootImageSpaces(); |
| 940 | CHECK(!spaces.empty()); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 941 | uint32_t pointer_size_unchecked = spaces[0]->GetImageHeader().GetPointerSizeUnchecked(); |
| 942 | if (!ValidPointerSize(pointer_size_unchecked)) { |
| 943 | *error_msg = StringPrintf("Invalid image pointer size: %u", pointer_size_unchecked); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 944 | return false; |
| 945 | } |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 946 | image_pointer_size_ = spaces[0]->GetImageHeader().GetPointerSize(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 947 | if (!runtime->IsAotCompiler()) { |
| 948 | // Only the Aot compiler supports having an image with a different pointer size than the |
| 949 | // runtime. This happens on the host for compiling 32 bit tests since we use a 64 bit libart |
| 950 | // 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] | 951 | if (image_pointer_size_ != kRuntimePointerSize) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 952 | *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] | 953 | static_cast<size_t>(image_pointer_size_), |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 954 | sizeof(void*)); |
| 955 | return false; |
| 956 | } |
| 957 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 958 | std::vector<const OatFile*> oat_files = |
| 959 | runtime->GetOatFileManager().RegisterImageOatFiles(spaces); |
| 960 | DCHECK(!oat_files.empty()); |
| 961 | const OatHeader& default_oat_header = oat_files[0]->GetOatHeader(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 962 | const char* image_file_location = oat_files[0]->GetOatHeader(). |
Andreas Gampe | 22f8e5c | 2014-07-09 11:38:21 -0700 | [diff] [blame] | 963 | GetStoreValueByKey(OatHeader::kImageLocationKey); |
| 964 | CHECK(image_file_location == nullptr || *image_file_location == 0); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 965 | quick_resolution_trampoline_ = default_oat_header.GetQuickResolutionTrampoline(); |
| 966 | quick_imt_conflict_trampoline_ = default_oat_header.GetQuickImtConflictTrampoline(); |
| 967 | quick_generic_jni_trampoline_ = default_oat_header.GetQuickGenericJniTrampoline(); |
| 968 | quick_to_interpreter_bridge_trampoline_ = default_oat_header.GetQuickToInterpreterBridge(); |
| 969 | if (kIsDebugBuild) { |
| 970 | // Check that the other images use the same trampoline. |
| 971 | for (size_t i = 1; i < oat_files.size(); ++i) { |
| 972 | const OatHeader& ith_oat_header = oat_files[i]->GetOatHeader(); |
| 973 | const void* ith_quick_resolution_trampoline = |
| 974 | ith_oat_header.GetQuickResolutionTrampoline(); |
| 975 | const void* ith_quick_imt_conflict_trampoline = |
| 976 | ith_oat_header.GetQuickImtConflictTrampoline(); |
| 977 | const void* ith_quick_generic_jni_trampoline = |
| 978 | ith_oat_header.GetQuickGenericJniTrampoline(); |
| 979 | const void* ith_quick_to_interpreter_bridge_trampoline = |
| 980 | ith_oat_header.GetQuickToInterpreterBridge(); |
| 981 | if (ith_quick_resolution_trampoline != quick_resolution_trampoline_ || |
| 982 | ith_quick_imt_conflict_trampoline != quick_imt_conflict_trampoline_ || |
| 983 | ith_quick_generic_jni_trampoline != quick_generic_jni_trampoline_ || |
| 984 | ith_quick_to_interpreter_bridge_trampoline != quick_to_interpreter_bridge_trampoline_) { |
| 985 | // Make sure that all methods in this image do not contain those trampolines as |
| 986 | // entrypoints. Otherwise the class-linker won't be able to work with a single set. |
| 987 | TrampolineCheckData data; |
| 988 | data.error = false; |
| 989 | data.pointer_size = GetImagePointerSize(); |
| 990 | data.quick_resolution_trampoline = ith_quick_resolution_trampoline; |
| 991 | data.quick_imt_conflict_trampoline = ith_quick_imt_conflict_trampoline; |
| 992 | data.quick_generic_jni_trampoline = ith_quick_generic_jni_trampoline; |
| 993 | data.quick_to_interpreter_bridge_trampoline = ith_quick_to_interpreter_bridge_trampoline; |
| 994 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Andreas Gampe | 0c18338 | 2017-07-13 22:26:24 -0700 | [diff] [blame] | 995 | auto visitor = [&](mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 996 | if (obj->IsClass()) { |
| 997 | ObjPtr<mirror::Class> klass = obj->AsClass(); |
| 998 | for (ArtMethod& m : klass->GetMethods(data.pointer_size)) { |
| 999 | const void* entrypoint = |
| 1000 | m.GetEntryPointFromQuickCompiledCodePtrSize(data.pointer_size); |
| 1001 | if (entrypoint == data.quick_resolution_trampoline || |
| 1002 | entrypoint == data.quick_imt_conflict_trampoline || |
| 1003 | entrypoint == data.quick_generic_jni_trampoline || |
| 1004 | entrypoint == data.quick_to_interpreter_bridge_trampoline) { |
| 1005 | data.m = &m; |
| 1006 | data.error = true; |
| 1007 | return; |
| 1008 | } |
| 1009 | } |
| 1010 | } |
| 1011 | }; |
| 1012 | spaces[i]->GetLiveBitmap()->Walk(visitor); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1013 | if (data.error) { |
| 1014 | ArtMethod* m = data.m; |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1015 | LOG(ERROR) << "Found a broken ArtMethod: " << ArtMethod::PrettyMethod(m); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1016 | *error_msg = "Found an ArtMethod with a bad entrypoint"; |
| 1017 | return false; |
| 1018 | } |
| 1019 | } |
| 1020 | } |
| 1021 | } |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 1022 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1023 | class_roots_ = GcRoot<mirror::ObjectArray<mirror::Class>>( |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 1024 | ObjPtr<mirror::ObjectArray<mirror::Class>>::DownCast(MakeObjPtr( |
| 1025 | spaces[0]->GetImageHeader().GetImageRoot(ImageHeader::kClassRoots)))); |
Vladimir Marko | f75613c | 2018-06-05 12:51:04 +0100 | [diff] [blame] | 1026 | DCHECK_EQ(GetClassRoot<mirror::Class>(this)->GetClassFlags(), mirror::kClassFlagClass); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1027 | |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 1028 | ObjPtr<mirror::Class> java_lang_Object = GetClassRoot<mirror::Object>(this); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 1029 | java_lang_Object->SetObjectSize(sizeof(mirror::Object)); |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 1030 | // Allocate in non-movable so that it's possible to check if a JNI weak global ref has been |
| 1031 | // cleared without triggering the read barrier and unintentionally mark the sentinel alive. |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1032 | runtime->SetSentinel(heap->AllocNonMovableObject<true>( |
| 1033 | self, java_lang_Object, java_lang_Object->GetObjectSize(), VoidFunctor())); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 1034 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1035 | for (gc::space::ImageSpace* image_space : spaces) { |
| 1036 | // Boot class loader, use a null handle. |
| 1037 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 1038 | if (!AddImageSpace(image_space, |
| 1039 | ScopedNullHandle<mirror::ClassLoader>(), |
| 1040 | /*dex_elements*/nullptr, |
| 1041 | /*dex_location*/nullptr, |
| 1042 | /*out*/&dex_files, |
| 1043 | error_msg)) { |
| 1044 | return false; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1045 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1046 | // Append opened dex files at the end. |
| 1047 | boot_dex_files_.insert(boot_dex_files_.end(), |
| 1048 | std::make_move_iterator(dex_files.begin()), |
| 1049 | std::make_move_iterator(dex_files.end())); |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 1050 | } |
Mathieu Chartier | be8303d | 2017-08-17 17:39:39 -0700 | [diff] [blame] | 1051 | for (const std::unique_ptr<const DexFile>& dex_file : boot_dex_files_) { |
| 1052 | OatDexFile::MadviseDexFile(*dex_file, MadviseState::kMadviseStateAtLoad); |
| 1053 | } |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1054 | FinishInit(self); |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 1055 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1056 | VLOG(startup) << __FUNCTION__ << " exiting"; |
| 1057 | return true; |
| 1058 | } |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 1059 | |
Jeff Hao | 5872d7c | 2016-04-27 11:07:41 -0700 | [diff] [blame] | 1060 | bool ClassLinker::IsBootClassLoader(ScopedObjectAccessAlreadyRunnable& soa, |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1061 | ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1062 | return class_loader == nullptr || |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 1063 | soa.Decode<mirror::Class>(WellKnownClasses::java_lang_BootClassLoader) == |
| 1064 | class_loader->GetClass(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1065 | } |
| 1066 | |
Mathieu Chartier | 8a1691f | 2017-03-02 12:02:13 -0800 | [diff] [blame] | 1067 | static bool GetDexPathListElementName(ObjPtr<mirror::Object> element, |
| 1068 | ObjPtr<mirror::String>* out_name) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1069 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1070 | ArtField* const dex_file_field = |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 1071 | jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1072 | ArtField* const dex_file_name_field = |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 1073 | jni::DecodeArtField(WellKnownClasses::dalvik_system_DexFile_fileName); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1074 | DCHECK(dex_file_field != nullptr); |
| 1075 | DCHECK(dex_file_name_field != nullptr); |
| 1076 | DCHECK(element != nullptr); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1077 | CHECK_EQ(dex_file_field->GetDeclaringClass(), element->GetClass()) << element->PrettyTypeOf(); |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 1078 | ObjPtr<mirror::Object> dex_file = dex_file_field->GetObject(element); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1079 | if (dex_file == nullptr) { |
Mathieu Chartier | 8a1691f | 2017-03-02 12:02:13 -0800 | [diff] [blame] | 1080 | // Null dex file means it was probably a jar with no dex files, return a null string. |
| 1081 | *out_name = nullptr; |
| 1082 | return true; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1083 | } |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 1084 | ObjPtr<mirror::Object> name_object = dex_file_name_field->GetObject(dex_file); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1085 | if (name_object != nullptr) { |
Mathieu Chartier | 8a1691f | 2017-03-02 12:02:13 -0800 | [diff] [blame] | 1086 | *out_name = name_object->AsString(); |
| 1087 | return true; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1088 | } |
Mathieu Chartier | 8a1691f | 2017-03-02 12:02:13 -0800 | [diff] [blame] | 1089 | return false; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1090 | } |
| 1091 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1092 | static bool FlattenPathClassLoader(ObjPtr<mirror::ClassLoader> class_loader, |
Mathieu Chartier | 8a1691f | 2017-03-02 12:02:13 -0800 | [diff] [blame] | 1093 | std::list<ObjPtr<mirror::String>>* out_dex_file_names, |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1094 | std::string* error_msg) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1095 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1096 | DCHECK(out_dex_file_names != nullptr); |
| 1097 | DCHECK(error_msg != nullptr); |
| 1098 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Andreas Gampe | b8e7c37 | 2018-02-20 18:24:55 -0800 | [diff] [blame] | 1099 | StackHandleScope<1> hs(soa.Self()); |
| 1100 | Handle<mirror::ClassLoader> handle(hs.NewHandle(class_loader)); |
Jeff Hao | 5872d7c | 2016-04-27 11:07:41 -0700 | [diff] [blame] | 1101 | while (!ClassLinker::IsBootClassLoader(soa, class_loader)) { |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 1102 | if (soa.Decode<mirror::Class>(WellKnownClasses::dalvik_system_PathClassLoader) != |
| 1103 | class_loader->GetClass()) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1104 | *error_msg = StringPrintf("Unknown class loader type %s", |
| 1105 | class_loader->PrettyTypeOf().c_str()); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1106 | // Unsupported class loader. |
| 1107 | return false; |
| 1108 | } |
Andreas Gampe | b8e7c37 | 2018-02-20 18:24:55 -0800 | [diff] [blame] | 1109 | // Get element names. Sets error to true on failure. |
| 1110 | auto add_element_names = [&](ObjPtr<mirror::Object> element, bool* error) |
| 1111 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1112 | if (element == nullptr) { |
| 1113 | *error_msg = "Null dex element"; |
| 1114 | *error = true; // Null element is a critical error. |
| 1115 | return false; // Had an error, stop the visit. |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1116 | } |
Andreas Gampe | b8e7c37 | 2018-02-20 18:24:55 -0800 | [diff] [blame] | 1117 | ObjPtr<mirror::String> name; |
| 1118 | if (!GetDexPathListElementName(element, &name)) { |
| 1119 | *error_msg = "Invalid dex path list element"; |
| 1120 | *error = false; // Invalid element is not a critical error. |
| 1121 | return false; // Stop the visit. |
| 1122 | } |
| 1123 | if (name != nullptr) { |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 1124 | out_dex_file_names->push_front(name); |
Andreas Gampe | b8e7c37 | 2018-02-20 18:24:55 -0800 | [diff] [blame] | 1125 | } |
| 1126 | return true; // Continue with the next Element. |
| 1127 | }; |
| 1128 | bool error = VisitClassLoaderDexElements(soa, handle, add_element_names, /* error */ false); |
| 1129 | if (error) { |
| 1130 | // An error occurred during DexPathList Element visiting. |
| 1131 | return false; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1132 | } |
| 1133 | class_loader = class_loader->GetParent(); |
| 1134 | } |
| 1135 | return true; |
| 1136 | } |
| 1137 | |
Alexey Grebenkin | be4c2bd | 2018-02-01 19:09:59 +0300 | [diff] [blame] | 1138 | class CHAOnDeleteUpdateClassVisitor { |
| 1139 | public: |
| 1140 | explicit CHAOnDeleteUpdateClassVisitor(LinearAlloc* alloc) |
| 1141 | : allocator_(alloc), cha_(Runtime::Current()->GetClassLinker()->GetClassHierarchyAnalysis()), |
| 1142 | pointer_size_(Runtime::Current()->GetClassLinker()->GetImagePointerSize()), |
| 1143 | self_(Thread::Current()) {} |
| 1144 | |
| 1145 | bool operator()(ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1146 | // This class is going to be unloaded. Tell CHA about it. |
| 1147 | cha_->ResetSingleImplementationInHierarchy(klass, allocator_, pointer_size_); |
| 1148 | return true; |
| 1149 | } |
| 1150 | private: |
| 1151 | const LinearAlloc* allocator_; |
| 1152 | const ClassHierarchyAnalysis* cha_; |
| 1153 | const PointerSize pointer_size_; |
| 1154 | const Thread* self_; |
| 1155 | }; |
| 1156 | |
Mathieu Chartier | 03c1dd9 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 1157 | class VerifyDeclaringClassVisitor : public ArtMethodVisitor { |
| 1158 | public: |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1159 | VerifyDeclaringClassVisitor() REQUIRES_SHARED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) |
Mathieu Chartier | 03c1dd9 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 1160 | : live_bitmap_(Runtime::Current()->GetHeap()->GetLiveBitmap()) {} |
| 1161 | |
Andreas Gampe | fa6a1b0 | 2018-09-07 08:11:55 -0700 | [diff] [blame] | 1162 | void Visit(ArtMethod* method) override |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1163 | REQUIRES_SHARED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1164 | ObjPtr<mirror::Class> klass = method->GetDeclaringClassUnchecked(); |
Mathieu Chartier | 03c1dd9 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 1165 | if (klass != nullptr) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1166 | CHECK(live_bitmap_->Test(klass.Ptr())) << "Image method has unmarked declaring class"; |
Mathieu Chartier | 03c1dd9 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 1167 | } |
| 1168 | } |
| 1169 | |
| 1170 | private: |
| 1171 | gc::accounting::HeapBitmap* const live_bitmap_; |
| 1172 | }; |
| 1173 | |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1174 | class FixupInternVisitor { |
| 1175 | public: |
| 1176 | ALWAYS_INLINE ObjPtr<mirror::Object> TryInsertIntern(mirror::Object* obj) const |
| 1177 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1178 | if (obj != nullptr && obj->IsString()) { |
| 1179 | const auto intern = Runtime::Current()->GetInternTable()->InternStrong(obj->AsString()); |
| 1180 | return intern; |
| 1181 | } |
| 1182 | return obj; |
| 1183 | } |
| 1184 | |
| 1185 | ALWAYS_INLINE void VisitRootIfNonNull( |
| 1186 | mirror::CompressedReference<mirror::Object>* root) const |
| 1187 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1188 | if (!root->IsNull()) { |
| 1189 | VisitRoot(root); |
| 1190 | } |
| 1191 | } |
| 1192 | |
| 1193 | ALWAYS_INLINE void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const |
| 1194 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1195 | root->Assign(TryInsertIntern(root->AsMirrorPtr())); |
| 1196 | } |
| 1197 | |
| 1198 | // Visit Class Fields |
| 1199 | ALWAYS_INLINE void operator()(ObjPtr<mirror::Object> obj, |
| 1200 | MemberOffset offset, |
| 1201 | bool is_static ATTRIBUTE_UNUSED) const |
| 1202 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1203 | // There could be overlap between ranges, we must avoid visiting the same reference twice. |
| 1204 | // Avoid the class field since we already fixed it up in FixupClassVisitor. |
| 1205 | if (offset.Uint32Value() != mirror::Object::ClassOffset().Uint32Value()) { |
| 1206 | // Updating images, don't do a read barrier. |
| 1207 | // Only string fields are fixed, don't do a verify. |
| 1208 | mirror::Object* ref = obj->GetFieldObject<mirror::Object, kVerifyNone, kWithoutReadBarrier>( |
| 1209 | offset); |
| 1210 | obj->SetFieldObject<false, false>(offset, TryInsertIntern(ref)); |
| 1211 | } |
| 1212 | } |
| 1213 | |
| 1214 | void operator()(ObjPtr<mirror::Class> klass ATTRIBUTE_UNUSED, |
| 1215 | ObjPtr<mirror::Reference> ref) const |
| 1216 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_) { |
| 1217 | this->operator()(ref, mirror::Reference::ReferentOffset(), false); |
| 1218 | } |
| 1219 | |
| 1220 | void operator()(mirror::Object* obj) const |
| 1221 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1222 | if (obj->IsDexCache()) { |
| 1223 | obj->VisitReferences<true, kVerifyNone, kWithoutReadBarrier>(*this, *this); |
| 1224 | } else { |
| 1225 | // Don't visit native roots for non-dex-cache |
| 1226 | obj->VisitReferences<false, kVerifyNone, kWithoutReadBarrier>(*this, *this); |
| 1227 | } |
| 1228 | } |
| 1229 | }; |
| 1230 | |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1231 | // new_class_set is the set of classes that were read from the class table section in the image. |
| 1232 | // If there was no class table section, it is null. |
| 1233 | // Note: using a class here to avoid having to make ClassLinker internals public. |
| 1234 | class AppImageClassLoadersAndDexCachesHelper { |
| 1235 | public: |
Vladimir Marko | 0f3c700 | 2017-09-07 14:15:56 +0100 | [diff] [blame] | 1236 | static void Update( |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1237 | ClassLinker* class_linker, |
| 1238 | gc::space::ImageSpace* space, |
| 1239 | Handle<mirror::ClassLoader> class_loader, |
| 1240 | Handle<mirror::ObjectArray<mirror::DexCache>> dex_caches, |
Vladimir Marko | 0f3c700 | 2017-09-07 14:15:56 +0100 | [diff] [blame] | 1241 | ClassTable::ClassSet* new_class_set) |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1242 | REQUIRES(!Locks::dex_lock_) |
| 1243 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 1244 | }; |
| 1245 | |
Vladimir Marko | 0f3c700 | 2017-09-07 14:15:56 +0100 | [diff] [blame] | 1246 | void AppImageClassLoadersAndDexCachesHelper::Update( |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1247 | ClassLinker* class_linker, |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1248 | gc::space::ImageSpace* space, |
| 1249 | Handle<mirror::ClassLoader> class_loader, |
| 1250 | Handle<mirror::ObjectArray<mirror::DexCache>> dex_caches, |
Vladimir Marko | 0f3c700 | 2017-09-07 14:15:56 +0100 | [diff] [blame] | 1251 | ClassTable::ClassSet* new_class_set) |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1252 | REQUIRES(!Locks::dex_lock_) |
| 1253 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1254 | Thread* const self = Thread::Current(); |
| 1255 | gc::Heap* const heap = Runtime::Current()->GetHeap(); |
| 1256 | const ImageHeader& header = space->GetImageHeader(); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1257 | { |
Vladimir Marko | 0f3c700 | 2017-09-07 14:15:56 +0100 | [diff] [blame] | 1258 | // Register dex caches with the class loader. |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1259 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1260 | const size_t num_dex_caches = dex_caches->GetLength(); |
| 1261 | for (size_t i = 0; i < num_dex_caches; i++) { |
Vladimir Marko | 1bc4b17 | 2016-10-24 16:53:39 +0000 | [diff] [blame] | 1262 | ObjPtr<mirror::DexCache> dex_cache = dex_caches->Get(i); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1263 | const DexFile* const dex_file = dex_cache->GetDexFile(); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1264 | { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 1265 | WriterMutexLock mu2(self, *Locks::dex_lock_); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1266 | CHECK(!class_linker->FindDexCacheDataLocked(*dex_file).IsValid()); |
| 1267 | class_linker->RegisterDexFileLocked(*dex_file, dex_cache, class_loader.Get()); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1268 | } |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1269 | if (kIsDebugBuild) { |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 1270 | CHECK(new_class_set != nullptr); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 1271 | mirror::TypeDexCacheType* const types = dex_cache->GetResolvedTypes(); |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 1272 | const size_t num_types = dex_cache->NumResolvedTypes(); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 1273 | for (size_t j = 0; j != num_types; ++j) { |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1274 | // 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] | 1275 | 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] | 1276 | if (space->HasAddress(klass.Ptr())) { |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 1277 | DCHECK(!klass->IsErroneous()) << klass->GetStatus(); |
Vladimir Marko | 54159c6 | 2018-06-20 14:30:08 +0100 | [diff] [blame] | 1278 | auto it = new_class_set->find(ClassTable::TableSlot(klass)); |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 1279 | DCHECK(it != new_class_set->end()); |
| 1280 | DCHECK_EQ(it->Read(), klass); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1281 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 1282 | if (super_class != nullptr && !heap->ObjectIsInBootImageSpace(super_class)) { |
Vladimir Marko | 54159c6 | 2018-06-20 14:30:08 +0100 | [diff] [blame] | 1283 | auto it2 = new_class_set->find(ClassTable::TableSlot(super_class)); |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 1284 | DCHECK(it2 != new_class_set->end()); |
| 1285 | DCHECK_EQ(it2->Read(), super_class); |
| 1286 | } |
| 1287 | for (ArtMethod& m : klass->GetDirectMethods(kRuntimePointerSize)) { |
| 1288 | const void* code = m.GetEntryPointFromQuickCompiledCode(); |
Alex Light | fc49fec | 2018-01-16 22:28:36 +0000 | [diff] [blame] | 1289 | const void* oat_code = m.IsInvokable() ? class_linker->GetQuickOatCodeFor(&m) : code; |
| 1290 | if (!class_linker->IsQuickResolutionStub(code) && |
| 1291 | !class_linker->IsQuickGenericJniStub(code) && |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1292 | !class_linker->IsQuickToInterpreterBridge(code) && |
Alex Light | fc49fec | 2018-01-16 22:28:36 +0000 | [diff] [blame] | 1293 | !m.IsNative()) { |
| 1294 | DCHECK_EQ(code, oat_code) << m.PrettyMethod(); |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 1295 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1296 | } |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 1297 | for (ArtMethod& m : klass->GetVirtualMethods(kRuntimePointerSize)) { |
| 1298 | const void* code = m.GetEntryPointFromQuickCompiledCode(); |
Alex Light | fc49fec | 2018-01-16 22:28:36 +0000 | [diff] [blame] | 1299 | const void* oat_code = m.IsInvokable() ? class_linker->GetQuickOatCodeFor(&m) : code; |
| 1300 | if (!class_linker->IsQuickResolutionStub(code) && |
| 1301 | !class_linker->IsQuickGenericJniStub(code) && |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1302 | !class_linker->IsQuickToInterpreterBridge(code) && |
Alex Light | fc49fec | 2018-01-16 22:28:36 +0000 | [diff] [blame] | 1303 | !m.IsNative()) { |
| 1304 | DCHECK_EQ(code, oat_code) << m.PrettyMethod(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1305 | } |
| 1306 | } |
| 1307 | } |
| 1308 | } |
| 1309 | } |
| 1310 | } |
Mathieu Chartier | a0b9521 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 1311 | } |
Mathieu Chartier | 0933cc5 | 2018-03-23 14:25:08 -0700 | [diff] [blame] | 1312 | if (ClassLinker::kAppImageMayContainStrings) { |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1313 | // Fixup all the literal strings happens at app images which are supposed to be interned. |
| 1314 | ScopedTrace timing("Fixup String Intern in image and dex_cache"); |
| 1315 | const auto& image_header = space->GetImageHeader(); |
| 1316 | const auto bitmap = space->GetMarkBitmap(); // bitmap of objects |
| 1317 | const uint8_t* target_base = space->GetMemMap()->Begin(); |
Vladimir Marko | cd87c3e | 2017-09-05 13:11:57 +0100 | [diff] [blame] | 1318 | const ImageSection& objects_section = image_header.GetObjectsSection(); |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1319 | |
| 1320 | uintptr_t objects_begin = reinterpret_cast<uintptr_t>(target_base + objects_section.Offset()); |
| 1321 | uintptr_t objects_end = reinterpret_cast<uintptr_t>(target_base + objects_section.End()); |
| 1322 | |
| 1323 | FixupInternVisitor fixup_intern_visitor; |
| 1324 | bitmap->VisitMarkedRange(objects_begin, objects_end, fixup_intern_visitor); |
| 1325 | } |
Mathieu Chartier | a0b9521 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 1326 | if (kVerifyArtMethodDeclaringClasses) { |
| 1327 | ScopedTrace timing("Verify declaring classes"); |
| 1328 | ReaderMutexLock rmu(self, *Locks::heap_bitmap_lock_); |
| 1329 | VerifyDeclaringClassVisitor visitor; |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1330 | header.VisitPackedArtMethods(&visitor, space->Begin(), kRuntimePointerSize); |
Mathieu Chartier | 03c1dd9 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 1331 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1332 | } |
| 1333 | |
Nicolas Geoffray | f9bf250 | 2016-12-14 14:59:04 +0000 | [diff] [blame] | 1334 | // Update the class loader. Should only be used on classes in the image space. |
| 1335 | class UpdateClassLoaderVisitor { |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1336 | public: |
Nicolas Geoffray | f9bf250 | 2016-12-14 14:59:04 +0000 | [diff] [blame] | 1337 | UpdateClassLoaderVisitor(gc::space::ImageSpace* space, ObjPtr<mirror::ClassLoader> class_loader) |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1338 | : space_(space), |
Nicolas Geoffray | f9bf250 | 2016-12-14 14:59:04 +0000 | [diff] [blame] | 1339 | class_loader_(class_loader) {} |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1340 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1341 | bool operator()(ObjPtr<mirror::Class> klass) const REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 1342 | // Do not update class loader for boot image classes where the app image |
| 1343 | // class loader is only the initiating loader but not the defining loader. |
| 1344 | if (klass->GetClassLoader() != nullptr) { |
| 1345 | klass->SetClassLoader(class_loader_); |
| 1346 | } |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1347 | return true; |
| 1348 | } |
| 1349 | |
| 1350 | gc::space::ImageSpace* const space_; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1351 | ObjPtr<mirror::ClassLoader> const class_loader_; |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1352 | }; |
| 1353 | |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1354 | static std::unique_ptr<const DexFile> OpenOatDexFile(const OatFile* oat_file, |
| 1355 | const char* location, |
| 1356 | std::string* error_msg) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1357 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1358 | DCHECK(error_msg != nullptr); |
| 1359 | std::unique_ptr<const DexFile> dex_file; |
Andreas Gampe | b40d361 | 2018-06-26 15:49:42 -0700 | [diff] [blame] | 1360 | const OatDexFile* oat_dex_file = oat_file->GetOatDexFile(location, nullptr, error_msg); |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1361 | if (oat_dex_file == nullptr) { |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1362 | return std::unique_ptr<const DexFile>(); |
| 1363 | } |
| 1364 | std::string inner_error_msg; |
| 1365 | dex_file = oat_dex_file->OpenDexFile(&inner_error_msg); |
| 1366 | if (dex_file == nullptr) { |
| 1367 | *error_msg = StringPrintf("Failed to open dex file %s from within oat file %s error '%s'", |
| 1368 | location, |
| 1369 | oat_file->GetLocation().c_str(), |
| 1370 | inner_error_msg.c_str()); |
| 1371 | return std::unique_ptr<const DexFile>(); |
| 1372 | } |
| 1373 | |
| 1374 | if (dex_file->GetLocationChecksum() != oat_dex_file->GetDexFileLocationChecksum()) { |
| 1375 | *error_msg = StringPrintf("Checksums do not match for %s: %x vs %x", |
| 1376 | location, |
| 1377 | dex_file->GetLocationChecksum(), |
| 1378 | oat_dex_file->GetDexFileLocationChecksum()); |
| 1379 | return std::unique_ptr<const DexFile>(); |
| 1380 | } |
| 1381 | return dex_file; |
| 1382 | } |
| 1383 | |
| 1384 | bool ClassLinker::OpenImageDexFiles(gc::space::ImageSpace* space, |
| 1385 | std::vector<std::unique_ptr<const DexFile>>* out_dex_files, |
| 1386 | std::string* error_msg) { |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 1387 | ScopedAssertNoThreadSuspension nts(__FUNCTION__); |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1388 | const ImageHeader& header = space->GetImageHeader(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1389 | ObjPtr<mirror::Object> dex_caches_object = header.GetImageRoot(ImageHeader::kDexCaches); |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1390 | DCHECK(dex_caches_object != nullptr); |
| 1391 | mirror::ObjectArray<mirror::DexCache>* dex_caches = |
| 1392 | dex_caches_object->AsObjectArray<mirror::DexCache>(); |
| 1393 | const OatFile* oat_file = space->GetOatFile(); |
| 1394 | for (int32_t i = 0; i < dex_caches->GetLength(); i++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1395 | ObjPtr<mirror::DexCache> dex_cache = dex_caches->Get(i); |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1396 | std::string dex_file_location(dex_cache->GetLocation()->ToModifiedUtf8()); |
| 1397 | std::unique_ptr<const DexFile> dex_file = OpenOatDexFile(oat_file, |
| 1398 | dex_file_location.c_str(), |
| 1399 | error_msg); |
| 1400 | if (dex_file == nullptr) { |
| 1401 | return false; |
| 1402 | } |
| 1403 | dex_cache->SetDexFile(dex_file.get()); |
| 1404 | out_dex_files->push_back(std::move(dex_file)); |
| 1405 | } |
| 1406 | return true; |
| 1407 | } |
| 1408 | |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1409 | // Helper class for ArtMethod checks when adding an image. Keeps all required functionality |
| 1410 | // together and caches some intermediate results. |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 1411 | class ImageSanityChecks final { |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1412 | public: |
| 1413 | static void CheckObjects(gc::Heap* heap, ClassLinker* class_linker) |
| 1414 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1415 | ImageSanityChecks isc(heap, class_linker); |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 1416 | auto visitor = [&](mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1417 | DCHECK(obj != nullptr); |
| 1418 | CHECK(obj->GetClass() != nullptr) << "Null class in object " << obj; |
| 1419 | CHECK(obj->GetClass()->GetClass() != nullptr) << "Null class class " << obj; |
| 1420 | if (obj->IsClass()) { |
| 1421 | auto klass = obj->AsClass(); |
| 1422 | for (ArtField& field : klass->GetIFields()) { |
| 1423 | CHECK_EQ(field.GetDeclaringClass(), klass); |
| 1424 | } |
| 1425 | for (ArtField& field : klass->GetSFields()) { |
| 1426 | CHECK_EQ(field.GetDeclaringClass(), klass); |
| 1427 | } |
| 1428 | const auto pointer_size = isc.pointer_size_; |
| 1429 | for (auto& m : klass->GetMethods(pointer_size)) { |
| 1430 | isc.SanityCheckArtMethod(&m, klass); |
| 1431 | } |
| 1432 | auto* vtable = klass->GetVTable(); |
| 1433 | if (vtable != nullptr) { |
| 1434 | isc.SanityCheckArtMethodPointerArray(vtable, nullptr); |
| 1435 | } |
| 1436 | if (klass->ShouldHaveImt()) { |
| 1437 | ImTable* imt = klass->GetImt(pointer_size); |
| 1438 | for (size_t i = 0; i < ImTable::kSize; ++i) { |
| 1439 | isc.SanityCheckArtMethod(imt->Get(i, pointer_size), nullptr); |
| 1440 | } |
| 1441 | } |
| 1442 | if (klass->ShouldHaveEmbeddedVTable()) { |
| 1443 | for (int32_t i = 0; i < klass->GetEmbeddedVTableLength(); ++i) { |
| 1444 | isc.SanityCheckArtMethod(klass->GetEmbeddedVTableEntry(i, pointer_size), nullptr); |
| 1445 | } |
| 1446 | } |
| 1447 | mirror::IfTable* iftable = klass->GetIfTable(); |
| 1448 | for (int32_t i = 0; i < klass->GetIfTableCount(); ++i) { |
| 1449 | if (iftable->GetMethodArrayCount(i) > 0) { |
| 1450 | isc.SanityCheckArtMethodPointerArray(iftable->GetMethodArray(i), nullptr); |
| 1451 | } |
| 1452 | } |
| 1453 | } |
| 1454 | }; |
| 1455 | heap->VisitObjects(visitor); |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1456 | } |
| 1457 | |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 1458 | static void CheckArtMethodDexCacheArray(gc::Heap* heap, |
| 1459 | ClassLinker* class_linker, |
| 1460 | mirror::MethodDexCacheType* arr, |
| 1461 | size_t size) |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1462 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1463 | ImageSanityChecks isc(heap, class_linker); |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 1464 | isc.SanityCheckArtMethodDexCacheArray(arr, size); |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1465 | } |
| 1466 | |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1467 | private: |
| 1468 | ImageSanityChecks(gc::Heap* heap, ClassLinker* class_linker) |
| 1469 | : spaces_(heap->GetBootImageSpaces()), |
| 1470 | pointer_size_(class_linker->GetImagePointerSize()) { |
| 1471 | space_begin_.reserve(spaces_.size()); |
| 1472 | method_sections_.reserve(spaces_.size()); |
| 1473 | runtime_method_sections_.reserve(spaces_.size()); |
| 1474 | for (gc::space::ImageSpace* space : spaces_) { |
| 1475 | space_begin_.push_back(space->Begin()); |
| 1476 | auto& header = space->GetImageHeader(); |
| 1477 | method_sections_.push_back(&header.GetMethodsSection()); |
| 1478 | runtime_method_sections_.push_back(&header.GetRuntimeMethodsSection()); |
| 1479 | } |
| 1480 | } |
| 1481 | |
| 1482 | void SanityCheckArtMethod(ArtMethod* m, ObjPtr<mirror::Class> expected_class) |
| 1483 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1484 | if (m->IsRuntimeMethod()) { |
| 1485 | ObjPtr<mirror::Class> declaring_class = m->GetDeclaringClassUnchecked(); |
| 1486 | CHECK(declaring_class == nullptr) << declaring_class << " " << m->PrettyMethod(); |
| 1487 | } else if (m->IsCopied()) { |
| 1488 | CHECK(m->GetDeclaringClass() != nullptr) << m->PrettyMethod(); |
| 1489 | } else if (expected_class != nullptr) { |
| 1490 | CHECK_EQ(m->GetDeclaringClassUnchecked(), expected_class) << m->PrettyMethod(); |
| 1491 | } |
| 1492 | if (!spaces_.empty()) { |
| 1493 | bool contains = false; |
| 1494 | for (size_t i = 0; !contains && i != space_begin_.size(); ++i) { |
| 1495 | const size_t offset = reinterpret_cast<uint8_t*>(m) - space_begin_[i]; |
| 1496 | contains = method_sections_[i]->Contains(offset) || |
| 1497 | runtime_method_sections_[i]->Contains(offset); |
| 1498 | } |
| 1499 | CHECK(contains) << m << " not found"; |
| 1500 | } |
| 1501 | } |
| 1502 | |
| 1503 | void SanityCheckArtMethodPointerArray(ObjPtr<mirror::PointerArray> arr, |
| 1504 | ObjPtr<mirror::Class> expected_class) |
| 1505 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1506 | CHECK(arr != nullptr); |
| 1507 | for (int32_t j = 0; j < arr->GetLength(); ++j) { |
| 1508 | auto* method = arr->GetElementPtrSize<ArtMethod*>(j, pointer_size_); |
| 1509 | // expected_class == null means we are a dex cache. |
| 1510 | if (expected_class != nullptr) { |
| 1511 | CHECK(method != nullptr); |
| 1512 | } |
| 1513 | if (method != nullptr) { |
| 1514 | SanityCheckArtMethod(method, expected_class); |
| 1515 | } |
| 1516 | } |
| 1517 | } |
| 1518 | |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 1519 | void SanityCheckArtMethodDexCacheArray(mirror::MethodDexCacheType* arr, size_t size) |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1520 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1521 | CHECK_EQ(arr != nullptr, size != 0u); |
| 1522 | if (arr != nullptr) { |
| 1523 | bool contains = false; |
| 1524 | for (auto space : spaces_) { |
| 1525 | auto offset = reinterpret_cast<uint8_t*>(arr) - space->Begin(); |
Vladimir Marko | cd87c3e | 2017-09-05 13:11:57 +0100 | [diff] [blame] | 1526 | if (space->GetImageHeader().GetDexCacheArraysSection().Contains(offset)) { |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1527 | contains = true; |
| 1528 | break; |
| 1529 | } |
| 1530 | } |
| 1531 | CHECK(contains); |
| 1532 | } |
| 1533 | for (size_t j = 0; j < size; ++j) { |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 1534 | auto pair = mirror::DexCache::GetNativePairPtrSize(arr, j, pointer_size_); |
| 1535 | ArtMethod* method = pair.object; |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1536 | // expected_class == null means we are a dex cache. |
| 1537 | if (method != nullptr) { |
| 1538 | SanityCheckArtMethod(method, nullptr); |
| 1539 | } |
| 1540 | } |
| 1541 | } |
| 1542 | |
| 1543 | const std::vector<gc::space::ImageSpace*>& spaces_; |
| 1544 | const PointerSize pointer_size_; |
| 1545 | |
| 1546 | // Cached sections from the spaces. |
| 1547 | std::vector<const uint8_t*> space_begin_; |
| 1548 | std::vector<const ImageSection*> method_sections_; |
| 1549 | std::vector<const ImageSection*> runtime_method_sections_; |
| 1550 | }; |
| 1551 | |
Andreas Gampe | be7af22 | 2017-07-25 09:57:28 -0700 | [diff] [blame] | 1552 | static void VerifyAppImage(const ImageHeader& header, |
| 1553 | const Handle<mirror::ClassLoader>& class_loader, |
| 1554 | const Handle<mirror::ObjectArray<mirror::DexCache> >& dex_caches, |
| 1555 | ClassTable* class_table, gc::space::ImageSpace* space) |
| 1556 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1557 | { |
| 1558 | class VerifyClassInTableArtMethodVisitor : public ArtMethodVisitor { |
| 1559 | public: |
| 1560 | explicit VerifyClassInTableArtMethodVisitor(ClassTable* table) : table_(table) {} |
| 1561 | |
Andreas Gampe | fa6a1b0 | 2018-09-07 08:11:55 -0700 | [diff] [blame] | 1562 | void Visit(ArtMethod* method) override |
Andreas Gampe | be7af22 | 2017-07-25 09:57:28 -0700 | [diff] [blame] | 1563 | REQUIRES_SHARED(Locks::mutator_lock_, Locks::classlinker_classes_lock_) { |
| 1564 | ObjPtr<mirror::Class> klass = method->GetDeclaringClass(); |
| 1565 | if (klass != nullptr && !Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(klass)) { |
| 1566 | CHECK_EQ(table_->LookupByDescriptor(klass), klass) << mirror::Class::PrettyClass(klass); |
| 1567 | } |
| 1568 | } |
| 1569 | |
| 1570 | private: |
| 1571 | ClassTable* const table_; |
| 1572 | }; |
| 1573 | VerifyClassInTableArtMethodVisitor visitor(class_table); |
| 1574 | header.VisitPackedArtMethods(&visitor, space->Begin(), kRuntimePointerSize); |
| 1575 | } |
| 1576 | { |
| 1577 | // Verify that all direct interfaces of classes in the class table are also resolved. |
| 1578 | std::vector<ObjPtr<mirror::Class>> classes; |
| 1579 | auto verify_direct_interfaces_in_table = [&](ObjPtr<mirror::Class> klass) |
| 1580 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1581 | if (!klass->IsPrimitive() && klass->GetClassLoader() == class_loader.Get()) { |
| 1582 | classes.push_back(klass); |
| 1583 | } |
| 1584 | return true; |
| 1585 | }; |
| 1586 | class_table->Visit(verify_direct_interfaces_in_table); |
| 1587 | Thread* self = Thread::Current(); |
| 1588 | for (ObjPtr<mirror::Class> klass : classes) { |
| 1589 | for (uint32_t i = 0, num = klass->NumDirectInterfaces(); i != num; ++i) { |
| 1590 | CHECK(klass->GetDirectInterface(self, klass, i) != nullptr) |
| 1591 | << klass->PrettyDescriptor() << " iface #" << i; |
| 1592 | } |
| 1593 | } |
| 1594 | } |
| 1595 | // Check that all non-primitive classes in dex caches are also in the class table. |
| 1596 | for (int32_t i = 0; i < dex_caches->GetLength(); i++) { |
| 1597 | ObjPtr<mirror::DexCache> dex_cache = dex_caches->Get(i); |
| 1598 | mirror::TypeDexCacheType* const types = dex_cache->GetResolvedTypes(); |
| 1599 | for (int32_t j = 0, num_types = dex_cache->NumResolvedTypes(); j < num_types; j++) { |
| 1600 | ObjPtr<mirror::Class> klass = types[j].load(std::memory_order_relaxed).object.Read(); |
| 1601 | if (klass != nullptr && !klass->IsPrimitive()) { |
| 1602 | CHECK(class_table->Contains(klass)) |
| 1603 | << klass->PrettyDescriptor() << " " << dex_cache->GetDexFile()->GetLocation(); |
| 1604 | } |
| 1605 | } |
| 1606 | } |
| 1607 | } |
| 1608 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1609 | bool ClassLinker::AddImageSpace( |
| 1610 | gc::space::ImageSpace* space, |
| 1611 | Handle<mirror::ClassLoader> class_loader, |
| 1612 | jobjectArray dex_elements, |
| 1613 | const char* dex_location, |
| 1614 | std::vector<std::unique_ptr<const DexFile>>* out_dex_files, |
| 1615 | std::string* error_msg) { |
| 1616 | DCHECK(out_dex_files != nullptr); |
| 1617 | DCHECK(error_msg != nullptr); |
| 1618 | const uint64_t start_time = NanoTime(); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 1619 | const bool app_image = class_loader != nullptr; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1620 | const ImageHeader& header = space->GetImageHeader(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1621 | ObjPtr<mirror::Object> dex_caches_object = header.GetImageRoot(ImageHeader::kDexCaches); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1622 | DCHECK(dex_caches_object != nullptr); |
| 1623 | Runtime* const runtime = Runtime::Current(); |
| 1624 | gc::Heap* const heap = runtime->GetHeap(); |
| 1625 | Thread* const self = Thread::Current(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1626 | // Check that the image is what we are expecting. |
| 1627 | if (image_pointer_size_ != space->GetImageHeader().GetPointerSize()) { |
| 1628 | *error_msg = StringPrintf("Application image pointer size does not match runtime: %zu vs %zu", |
| 1629 | static_cast<size_t>(space->GetImageHeader().GetPointerSize()), |
| 1630 | image_pointer_size_); |
| 1631 | return false; |
| 1632 | } |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1633 | size_t expected_image_roots = ImageHeader::NumberOfImageRoots(app_image); |
| 1634 | if (static_cast<size_t>(header.GetImageRoots()->GetLength()) != expected_image_roots) { |
| 1635 | *error_msg = StringPrintf("Expected %zu image roots but got %d", |
| 1636 | expected_image_roots, |
| 1637 | header.GetImageRoots()->GetLength()); |
| 1638 | return false; |
| 1639 | } |
| 1640 | StackHandleScope<3> hs(self); |
| 1641 | Handle<mirror::ObjectArray<mirror::DexCache>> dex_caches( |
| 1642 | hs.NewHandle(dex_caches_object->AsObjectArray<mirror::DexCache>())); |
| 1643 | Handle<mirror::ObjectArray<mirror::Class>> class_roots(hs.NewHandle( |
| 1644 | header.GetImageRoot(ImageHeader::kClassRoots)->AsObjectArray<mirror::Class>())); |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1645 | MutableHandle<mirror::ClassLoader> image_class_loader(hs.NewHandle( |
Vladimir Marko | f75613c | 2018-06-05 12:51:04 +0100 | [diff] [blame] | 1646 | app_image ? header.GetImageRoot(ImageHeader::kAppImageClassLoader)->AsClassLoader() |
| 1647 | : nullptr)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 1648 | DCHECK(class_roots != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 1649 | if (class_roots->GetLength() != static_cast<int32_t>(ClassRoot::kMax)) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1650 | *error_msg = StringPrintf("Expected %d class roots but got %d", |
| 1651 | class_roots->GetLength(), |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 1652 | static_cast<int32_t>(ClassRoot::kMax)); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1653 | return false; |
| 1654 | } |
| 1655 | // Check against existing class roots to make sure they match the ones in the boot image. |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 1656 | ObjPtr<mirror::ObjectArray<mirror::Class>> existing_class_roots = GetClassRoots(); |
| 1657 | for (size_t i = 0; i < static_cast<size_t>(ClassRoot::kMax); i++) { |
| 1658 | if (class_roots->Get(i) != GetClassRoot(static_cast<ClassRoot>(i), existing_class_roots)) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1659 | *error_msg = "App image class roots must have pointer equality with runtime ones."; |
| 1660 | return false; |
| 1661 | } |
| 1662 | } |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1663 | const OatFile* oat_file = space->GetOatFile(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1664 | if (oat_file->GetOatHeader().GetDexFileCount() != |
| 1665 | static_cast<uint32_t>(dex_caches->GetLength())) { |
| 1666 | *error_msg = "Dex cache count and dex file count mismatch while trying to initialize from " |
| 1667 | "image"; |
| 1668 | return false; |
| 1669 | } |
| 1670 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1671 | for (int32_t i = 0; i < dex_caches->GetLength(); i++) { |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 1672 | ObjPtr<mirror::DexCache> dex_cache = dex_caches->Get(i); |
| 1673 | std::string dex_file_location(dex_cache->GetLocation()->ToModifiedUtf8()); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1674 | // TODO: Only store qualified paths. |
| 1675 | // If non qualified, qualify it. |
| 1676 | if (dex_file_location.find('/') == std::string::npos) { |
| 1677 | std::string dex_location_path = dex_location; |
| 1678 | const size_t pos = dex_location_path.find_last_of('/'); |
| 1679 | CHECK_NE(pos, std::string::npos); |
| 1680 | dex_location_path = dex_location_path.substr(0, pos + 1); // Keep trailing '/' |
| 1681 | dex_file_location = dex_location_path + dex_file_location; |
| 1682 | } |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1683 | std::unique_ptr<const DexFile> dex_file = OpenOatDexFile(oat_file, |
| 1684 | dex_file_location.c_str(), |
| 1685 | error_msg); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1686 | if (dex_file == nullptr) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1687 | return false; |
| 1688 | } |
| 1689 | |
| 1690 | if (app_image) { |
| 1691 | // The current dex file field is bogus, overwrite it so that we can get the dex file in the |
| 1692 | // loop below. |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 1693 | dex_cache->SetDexFile(dex_file.get()); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 1694 | mirror::TypeDexCacheType* const types = dex_cache->GetResolvedTypes(); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 1695 | 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] | 1696 | 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] | 1697 | if (klass != nullptr) { |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 1698 | DCHECK(!klass->IsErroneous()) << klass->GetStatus(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1699 | } |
| 1700 | } |
| 1701 | } else { |
| 1702 | if (kSanityCheckObjects) { |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 1703 | ImageSanityChecks::CheckArtMethodDexCacheArray(heap, |
| 1704 | this, |
| 1705 | dex_cache->GetResolvedMethods(), |
| 1706 | dex_cache->NumResolvedMethods()); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1707 | } |
| 1708 | // Register dex files, keep track of existing ones that are conflicts. |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 1709 | AppendToBootClassPath(*dex_file.get(), dex_cache); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1710 | } |
| 1711 | out_dex_files->push_back(std::move(dex_file)); |
| 1712 | } |
| 1713 | |
| 1714 | if (app_image) { |
| 1715 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 1716 | // Check that the class loader resolves the same way as the ones in the image. |
| 1717 | // Image class loader [A][B][C][image dex files] |
| 1718 | // Class loader = [???][dex_elements][image dex files] |
| 1719 | // Need to ensure that [???][dex_elements] == [A][B][C]. |
| 1720 | // For each class loader, PathClassLoader, the laoder checks the parent first. Also the logic |
| 1721 | // for PathClassLoader does this by looping through the array of dex files. To ensure they |
| 1722 | // resolve the same way, simply flatten the hierarchy in the way the resolution order would be, |
| 1723 | // and check that the dex file names are the same. |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1724 | if (IsBootClassLoader(soa, image_class_loader.Get())) { |
| 1725 | *error_msg = "Unexpected BootClassLoader in app image"; |
| 1726 | return false; |
| 1727 | } |
Mathieu Chartier | 8a1691f | 2017-03-02 12:02:13 -0800 | [diff] [blame] | 1728 | std::list<ObjPtr<mirror::String>> image_dex_file_names; |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1729 | std::string temp_error_msg; |
| 1730 | if (!FlattenPathClassLoader(image_class_loader.Get(), &image_dex_file_names, &temp_error_msg)) { |
| 1731 | *error_msg = StringPrintf("Failed to flatten image class loader hierarchy '%s'", |
| 1732 | temp_error_msg.c_str()); |
| 1733 | return false; |
| 1734 | } |
Mathieu Chartier | 8a1691f | 2017-03-02 12:02:13 -0800 | [diff] [blame] | 1735 | std::list<ObjPtr<mirror::String>> loader_dex_file_names; |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1736 | if (!FlattenPathClassLoader(class_loader.Get(), &loader_dex_file_names, &temp_error_msg)) { |
| 1737 | *error_msg = StringPrintf("Failed to flatten class loader hierarchy '%s'", |
| 1738 | temp_error_msg.c_str()); |
| 1739 | return false; |
| 1740 | } |
| 1741 | // Add the temporary dex path list elements at the end. |
| 1742 | auto elements = soa.Decode<mirror::ObjectArray<mirror::Object>>(dex_elements); |
| 1743 | for (size_t i = 0, num_elems = elements->GetLength(); i < num_elems; ++i) { |
| 1744 | ObjPtr<mirror::Object> element = elements->GetWithoutChecks(i); |
| 1745 | if (element != nullptr) { |
| 1746 | // 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] | 1747 | ObjPtr<mirror::String> name; |
| 1748 | if (GetDexPathListElementName(element, &name) && name != nullptr) { |
| 1749 | loader_dex_file_names.push_back(name); |
| 1750 | } |
Nicolas Geoffray | 1df3b55 | 2016-04-26 18:30:31 +0100 | [diff] [blame] | 1751 | } |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1752 | } |
| 1753 | // Ignore the number of image dex files since we are adding those to the class loader anyways. |
| 1754 | CHECK_GE(static_cast<size_t>(image_dex_file_names.size()), |
| 1755 | static_cast<size_t>(dex_caches->GetLength())); |
| 1756 | size_t image_count = image_dex_file_names.size() - dex_caches->GetLength(); |
| 1757 | // Check that the dex file names match. |
| 1758 | bool equal = image_count == loader_dex_file_names.size(); |
| 1759 | if (equal) { |
| 1760 | auto it1 = image_dex_file_names.begin(); |
| 1761 | auto it2 = loader_dex_file_names.begin(); |
| 1762 | for (size_t i = 0; equal && i < image_count; ++i, ++it1, ++it2) { |
| 1763 | equal = equal && (*it1)->Equals(*it2); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1764 | } |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1765 | } |
| 1766 | if (!equal) { |
| 1767 | VLOG(image) << "Image dex files " << image_dex_file_names.size(); |
| 1768 | for (ObjPtr<mirror::String> name : image_dex_file_names) { |
| 1769 | VLOG(image) << name->ToModifiedUtf8(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1770 | } |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1771 | VLOG(image) << "Loader dex files " << loader_dex_file_names.size(); |
| 1772 | for (ObjPtr<mirror::String> name : loader_dex_file_names) { |
| 1773 | VLOG(image) << name->ToModifiedUtf8(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1774 | } |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1775 | *error_msg = "Rejecting application image due to class loader mismatch"; |
| 1776 | // Ignore class loader mismatch for now since these would just use possibly incorrect |
| 1777 | // 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] | 1778 | } |
| 1779 | } |
| 1780 | |
| 1781 | if (kSanityCheckObjects) { |
| 1782 | for (int32_t i = 0; i < dex_caches->GetLength(); i++) { |
| 1783 | auto* dex_cache = dex_caches->Get(i); |
| 1784 | for (size_t j = 0; j < dex_cache->NumResolvedFields(); ++j) { |
| 1785 | auto* field = dex_cache->GetResolvedField(j, image_pointer_size_); |
| 1786 | if (field != nullptr) { |
| 1787 | CHECK(field->GetDeclaringClass()->GetClass() != nullptr); |
| 1788 | } |
| 1789 | } |
| 1790 | } |
| 1791 | if (!app_image) { |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1792 | ImageSanityChecks::CheckObjects(heap, this); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1793 | } |
| 1794 | } |
| 1795 | |
| 1796 | // Set entry point to interpreter if in InterpretOnly mode. |
| 1797 | if (!runtime->IsAotCompiler() && runtime->GetInstrumentation()->InterpretOnly()) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1798 | SetInterpreterEntrypointArtMethodVisitor visitor(image_pointer_size_); |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 1799 | header.VisitPackedArtMethods(&visitor, space->Begin(), image_pointer_size_); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1800 | } |
| 1801 | |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1802 | ClassTable* class_table = nullptr; |
| 1803 | { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1804 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1805 | class_table = InsertClassTableForClassLoader(class_loader.Get()); |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 1806 | } |
| 1807 | // If we have a class table section, read it and use it for verification in |
| 1808 | // UpdateAppImageClassLoadersAndDexCaches. |
| 1809 | ClassTable::ClassSet temp_set; |
Vladimir Marko | 0f3c700 | 2017-09-07 14:15:56 +0100 | [diff] [blame] | 1810 | const ImageSection& class_table_section = header.GetClassTableSection(); |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 1811 | const bool added_class_table = class_table_section.Size() > 0u; |
| 1812 | if (added_class_table) { |
| 1813 | const uint64_t start_time2 = NanoTime(); |
| 1814 | size_t read_count = 0; |
| 1815 | temp_set = ClassTable::ClassSet(space->Begin() + class_table_section.Offset(), |
| 1816 | /*make copy*/false, |
| 1817 | &read_count); |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 1818 | VLOG(image) << "Adding class table classes took " << PrettyDuration(NanoTime() - start_time2); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1819 | } |
| 1820 | if (app_image) { |
Vladimir Marko | 0f3c700 | 2017-09-07 14:15:56 +0100 | [diff] [blame] | 1821 | AppImageClassLoadersAndDexCachesHelper::Update(this, |
| 1822 | space, |
| 1823 | class_loader, |
| 1824 | dex_caches, |
| 1825 | &temp_set); |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 1826 | // Update class loader and resolved strings. If added_class_table is false, the resolved |
| 1827 | // strings were forwarded UpdateAppImageClassLoadersAndDexCaches. |
Nicolas Geoffray | f9bf250 | 2016-12-14 14:59:04 +0000 | [diff] [blame] | 1828 | UpdateClassLoaderVisitor visitor(space, class_loader.Get()); |
| 1829 | for (const ClassTable::TableSlot& root : temp_set) { |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 1830 | visitor(root.Read()); |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 1831 | } |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 1832 | |
Vladimir Marko | 305c38b | 2018-02-14 11:50:07 +0000 | [diff] [blame] | 1833 | if (kBitstringSubtypeCheckEnabled) { |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 1834 | // Every class in the app image has initially SubtypeCheckInfo in the |
| 1835 | // Uninitialized state. |
| 1836 | // |
| 1837 | // The SubtypeCheck invariants imply that a SubtypeCheckInfo is at least Initialized |
| 1838 | // after class initialization is complete. The app image ClassStatus as-is |
| 1839 | // are almost all ClassStatus::Initialized, and being in the |
| 1840 | // SubtypeCheckInfo::kUninitialized state is violating that invariant. |
| 1841 | // |
| 1842 | // Force every app image class's SubtypeCheck to be at least kIninitialized. |
| 1843 | // |
| 1844 | // See also ImageWriter::FixupClass. |
| 1845 | ScopedTrace trace("Recalculate app image SubtypeCheck bitstrings"); |
| 1846 | MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_); |
| 1847 | for (const ClassTable::TableSlot& root : temp_set) { |
Vladimir Marko | 38b8b25 | 2018-01-02 19:07:06 +0000 | [diff] [blame] | 1848 | SubtypeCheck<ObjPtr<mirror::Class>>::EnsureInitialized(root.Read()); |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 1849 | } |
| 1850 | } |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 1851 | } |
| 1852 | if (!oat_file->GetBssGcRoots().empty()) { |
| 1853 | // Insert oat file to class table for visiting .bss GC roots. |
| 1854 | class_table->InsertOatFile(oat_file); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1855 | } |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 1856 | |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 1857 | if (added_class_table) { |
| 1858 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 1859 | class_table->AddClassSet(std::move(temp_set)); |
| 1860 | } |
Andreas Gampe | be7af22 | 2017-07-25 09:57:28 -0700 | [diff] [blame] | 1861 | |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 1862 | if (kIsDebugBuild && app_image) { |
| 1863 | // This verification needs to happen after the classes have been added to the class loader. |
| 1864 | // Since it ensures classes are in the class table. |
Andreas Gampe | acbd98b | 2017-10-12 10:44:11 -0700 | [diff] [blame] | 1865 | ScopedTrace trace("VerifyAppImage"); |
Andreas Gampe | be7af22 | 2017-07-25 09:57:28 -0700 | [diff] [blame] | 1866 | VerifyAppImage(header, class_loader, dex_caches, class_table, space); |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 1867 | } |
Andreas Gampe | be7af22 | 2017-07-25 09:57:28 -0700 | [diff] [blame] | 1868 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1869 | VLOG(class_linker) << "Adding image space took " << PrettyDuration(NanoTime() - start_time); |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 1870 | return true; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1871 | } |
| 1872 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1873 | bool ClassLinker::ClassInClassTable(ObjPtr<mirror::Class> klass) { |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 1874 | ClassTable* const class_table = ClassTableForClassLoader(klass->GetClassLoader()); |
| 1875 | return class_table != nullptr && class_table->Contains(klass); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1876 | } |
| 1877 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 1878 | void ClassLinker::VisitClassRoots(RootVisitor* visitor, VisitRootFlags flags) { |
Mathieu Chartier | 7778b88 | 2015-10-05 16:41:10 -0700 | [diff] [blame] | 1879 | // Acquire tracing_enabled before locking class linker lock to prevent lock order violation. Since |
| 1880 | // enabling tracing requires the mutator lock, there are no race conditions here. |
| 1881 | const bool tracing_enabled = Trace::IsTracingEnabled(); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 1882 | Thread* const self = Thread::Current(); |
| 1883 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Vladimir Marko | 9b03cb4 | 2017-02-16 16:37:03 +0000 | [diff] [blame] | 1884 | if (kUseReadBarrier) { |
| 1885 | // We do not track new roots for CC. |
| 1886 | DCHECK_EQ(0, flags & (kVisitRootFlagNewRoots | |
| 1887 | kVisitRootFlagClearRootLog | |
| 1888 | kVisitRootFlagStartLoggingNewRoots | |
| 1889 | kVisitRootFlagStopLoggingNewRoots)); |
| 1890 | } |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 1891 | if ((flags & kVisitRootFlagAllRoots) != 0) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1892 | // Argument for how root visiting deals with ArtField and ArtMethod roots. |
| 1893 | // There is 3 GC cases to handle: |
| 1894 | // Non moving concurrent: |
| 1895 | // 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] | 1896 | // live by the class and class roots. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1897 | // |
| 1898 | // Moving non-concurrent: |
| 1899 | // This case needs to call visit VisitNativeRoots in case the classes or dex cache arrays move. |
| 1900 | // To prevent missing roots, this case needs to ensure that there is no |
| 1901 | // suspend points between the point which we allocate ArtMethod arrays and place them in a |
| 1902 | // class which is in the class table. |
| 1903 | // |
| 1904 | // Moving concurrent: |
| 1905 | // Need to make sure to not copy ArtMethods without doing read barriers since the roots are |
| 1906 | // 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] | 1907 | // |
| 1908 | // Use an unbuffered visitor since the class table uses a temporary GcRoot for holding decoded |
| 1909 | // ClassTable::TableSlot. The buffered root visiting would access a stale stack location for |
| 1910 | // these objects. |
| 1911 | UnbufferedRootVisitor root_visitor(visitor, RootInfo(kRootStickyClass)); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1912 | boot_class_table_->VisitRoots(root_visitor); |
Mathieu Chartier | 7778b88 | 2015-10-05 16:41:10 -0700 | [diff] [blame] | 1913 | // 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] | 1914 | if ((flags & kVisitRootFlagClassLoader) != 0 || tracing_enabled) { |
Mathieu Chartier | 7778b88 | 2015-10-05 16:41:10 -0700 | [diff] [blame] | 1915 | for (const ClassLoaderData& data : class_loaders_) { |
| 1916 | GcRoot<mirror::Object> root(GcRoot<mirror::Object>(self->DecodeJObject(data.weak_root))); |
| 1917 | root.VisitRoot(visitor, RootInfo(kRootVMInternal)); |
| 1918 | } |
| 1919 | } |
Vladimir Marko | 9b03cb4 | 2017-02-16 16:37:03 +0000 | [diff] [blame] | 1920 | } else if (!kUseReadBarrier && (flags & kVisitRootFlagNewRoots) != 0) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 1921 | for (auto& root : new_class_roots_) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1922 | ObjPtr<mirror::Class> old_ref = root.Read<kWithoutReadBarrier>(); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 1923 | root.VisitRoot(visitor, RootInfo(kRootStickyClass)); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1924 | ObjPtr<mirror::Class> new_ref = root.Read<kWithoutReadBarrier>(); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 1925 | // Concurrent moving GC marked new roots through the to-space invariant. |
| 1926 | CHECK_EQ(new_ref, old_ref); |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 1927 | } |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 1928 | for (const OatFile* oat_file : new_bss_roots_boot_oat_files_) { |
| 1929 | for (GcRoot<mirror::Object>& root : oat_file->GetBssGcRoots()) { |
| 1930 | ObjPtr<mirror::Object> old_ref = root.Read<kWithoutReadBarrier>(); |
| 1931 | if (old_ref != nullptr) { |
| 1932 | DCHECK(old_ref->IsClass()); |
| 1933 | root.VisitRoot(visitor, RootInfo(kRootStickyClass)); |
| 1934 | ObjPtr<mirror::Object> new_ref = root.Read<kWithoutReadBarrier>(); |
| 1935 | // Concurrent moving GC marked new roots through the to-space invariant. |
| 1936 | CHECK_EQ(new_ref, old_ref); |
| 1937 | } |
| 1938 | } |
| 1939 | } |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 1940 | } |
Vladimir Marko | 9b03cb4 | 2017-02-16 16:37:03 +0000 | [diff] [blame] | 1941 | if (!kUseReadBarrier && (flags & kVisitRootFlagClearRootLog) != 0) { |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 1942 | new_class_roots_.clear(); |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 1943 | new_bss_roots_boot_oat_files_.clear(); |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 1944 | } |
Vladimir Marko | 9b03cb4 | 2017-02-16 16:37:03 +0000 | [diff] [blame] | 1945 | if (!kUseReadBarrier && (flags & kVisitRootFlagStartLoggingNewRoots) != 0) { |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 1946 | log_new_roots_ = true; |
Vladimir Marko | 9b03cb4 | 2017-02-16 16:37:03 +0000 | [diff] [blame] | 1947 | } else if (!kUseReadBarrier && (flags & kVisitRootFlagStopLoggingNewRoots) != 0) { |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 1948 | log_new_roots_ = false; |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 1949 | } |
| 1950 | // We deliberately ignore the class roots in the image since we |
| 1951 | // handle image roots by using the MS/CMS rescanning of dirty cards. |
| 1952 | } |
| 1953 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1954 | // Keep in sync with InitCallback. Anything we visit, we need to |
| 1955 | // reinit references to when reinitializing a ClassLinker from a |
| 1956 | // mapped image. |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 1957 | void ClassLinker::VisitRoots(RootVisitor* visitor, VisitRootFlags flags) { |
Mathieu Chartier | 3100080 | 2015-06-14 14:14:37 -0700 | [diff] [blame] | 1958 | class_roots_.VisitRootIfNonNull(visitor, RootInfo(kRootVMInternal)); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 1959 | VisitClassRoots(visitor, flags); |
Mathieu Chartier | 6cfc2c0 | 2015-10-12 15:06:16 -0700 | [diff] [blame] | 1960 | // Instead of visiting the find_array_class_cache_ drop it so that it doesn't prevent class |
| 1961 | // unloading if we are marking roots. |
| 1962 | DropFindArrayClassCache(); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 1963 | } |
| 1964 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 1965 | class VisitClassLoaderClassesVisitor : public ClassLoaderVisitor { |
| 1966 | public: |
| 1967 | explicit VisitClassLoaderClassesVisitor(ClassVisitor* visitor) |
| 1968 | : visitor_(visitor), |
| 1969 | done_(false) {} |
| 1970 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1971 | void Visit(ObjPtr<mirror::ClassLoader> class_loader) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 1972 | REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_) override { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 1973 | ClassTable* const class_table = class_loader->GetClassTable(); |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 1974 | if (!done_ && class_table != nullptr) { |
| 1975 | DefiningClassLoaderFilterVisitor visitor(class_loader, visitor_); |
| 1976 | if (!class_table->Visit(visitor)) { |
| 1977 | // If the visitor ClassTable returns false it means that we don't need to continue. |
| 1978 | done_ = true; |
| 1979 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 1980 | } |
| 1981 | } |
| 1982 | |
| 1983 | private: |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 1984 | // Class visitor that limits the class visits from a ClassTable to the classes with |
| 1985 | // the provided defining class loader. This filter is used to avoid multiple visits |
| 1986 | // of the same class which can be recorded for multiple initiating class loaders. |
| 1987 | class DefiningClassLoaderFilterVisitor : public ClassVisitor { |
| 1988 | public: |
| 1989 | DefiningClassLoaderFilterVisitor(ObjPtr<mirror::ClassLoader> defining_class_loader, |
| 1990 | ClassVisitor* visitor) |
| 1991 | : defining_class_loader_(defining_class_loader), visitor_(visitor) { } |
| 1992 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 1993 | bool operator()(ObjPtr<mirror::Class> klass) override REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 1994 | if (klass->GetClassLoader() != defining_class_loader_) { |
| 1995 | return true; |
| 1996 | } |
| 1997 | return (*visitor_)(klass); |
| 1998 | } |
| 1999 | |
| 2000 | ObjPtr<mirror::ClassLoader> const defining_class_loader_; |
| 2001 | ClassVisitor* const visitor_; |
| 2002 | }; |
| 2003 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2004 | ClassVisitor* const visitor_; |
| 2005 | // If done is true then we don't need to do any more visiting. |
| 2006 | bool done_; |
| 2007 | }; |
| 2008 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2009 | void ClassLinker::VisitClassesInternal(ClassVisitor* visitor) { |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 2010 | if (boot_class_table_->Visit(*visitor)) { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2011 | VisitClassLoaderClassesVisitor loader_visitor(visitor); |
| 2012 | VisitClassLoaders(&loader_visitor); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 2013 | } |
| 2014 | } |
| 2015 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2016 | void ClassLinker::VisitClasses(ClassVisitor* visitor) { |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 2017 | Thread* const self = Thread::Current(); |
| 2018 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 2019 | // Not safe to have thread suspension when we are holding a lock. |
| 2020 | if (self != nullptr) { |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 2021 | ScopedAssertNoThreadSuspension nts(__FUNCTION__); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2022 | VisitClassesInternal(visitor); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 2023 | } else { |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2024 | VisitClassesInternal(visitor); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2025 | } |
| 2026 | } |
| 2027 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2028 | class GetClassesInToVector : public ClassVisitor { |
| 2029 | public: |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 2030 | bool operator()(ObjPtr<mirror::Class> klass) override { |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2031 | classes_.push_back(klass); |
| 2032 | return true; |
| 2033 | } |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2034 | std::vector<ObjPtr<mirror::Class>> classes_; |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2035 | }; |
| 2036 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2037 | class GetClassInToObjectArray : public ClassVisitor { |
| 2038 | public: |
| 2039 | explicit GetClassInToObjectArray(mirror::ObjectArray<mirror::Class>* arr) |
| 2040 | : arr_(arr), index_(0) {} |
| 2041 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 2042 | bool operator()(ObjPtr<mirror::Class> klass) override REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2043 | ++index_; |
| 2044 | if (index_ <= arr_->GetLength()) { |
| 2045 | arr_->Set(index_ - 1, klass); |
| 2046 | return true; |
| 2047 | } |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2048 | return false; |
| 2049 | } |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2050 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2051 | bool Succeeded() const REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2052 | return index_ <= arr_->GetLength(); |
| 2053 | } |
| 2054 | |
| 2055 | private: |
| 2056 | mirror::ObjectArray<mirror::Class>* const arr_; |
| 2057 | int32_t index_; |
| 2058 | }; |
| 2059 | |
| 2060 | void ClassLinker::VisitClassesWithoutClassesLock(ClassVisitor* visitor) { |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2061 | // TODO: it may be possible to avoid secondary storage if we iterate over dex caches. The problem |
| 2062 | // is avoiding duplicates. |
| 2063 | if (!kMovingClasses) { |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 2064 | ScopedAssertNoThreadSuspension nts(__FUNCTION__); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2065 | GetClassesInToVector accumulator; |
| 2066 | VisitClasses(&accumulator); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2067 | for (ObjPtr<mirror::Class> klass : accumulator.classes_) { |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 2068 | if (!visitor->operator()(klass)) { |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2069 | return; |
| 2070 | } |
| 2071 | } |
| 2072 | } else { |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 2073 | Thread* const self = Thread::Current(); |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2074 | StackHandleScope<1> hs(self); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2075 | auto classes = hs.NewHandle<mirror::ObjectArray<mirror::Class>>(nullptr); |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2076 | // We size the array assuming classes won't be added to the class table during the visit. |
| 2077 | // If this assumption fails we iterate again. |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2078 | while (true) { |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2079 | size_t class_table_size; |
| 2080 | { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2081 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2082 | // Add 100 in case new classes get loaded when we are filling in the object array. |
| 2083 | class_table_size = NumZygoteClasses() + NumNonZygoteClasses() + 100; |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2084 | } |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 2085 | ObjPtr<mirror::Class> array_of_class = GetClassRoot<mirror::ObjectArray<mirror::Class>>(this); |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2086 | classes.Assign( |
| 2087 | mirror::ObjectArray<mirror::Class>::Alloc(self, array_of_class, class_table_size)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 2088 | CHECK(classes != nullptr); // OOME. |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2089 | GetClassInToObjectArray accumulator(classes.Get()); |
| 2090 | VisitClasses(&accumulator); |
| 2091 | if (accumulator.Succeeded()) { |
| 2092 | break; |
| 2093 | } |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2094 | } |
| 2095 | for (int32_t i = 0; i < classes->GetLength(); ++i) { |
| 2096 | // If the class table shrank during creation of the clases array we expect null elements. If |
| 2097 | // the class table grew then the loop repeats. If classes are created after the loop has |
| 2098 | // finished then we don't visit. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2099 | ObjPtr<mirror::Class> klass = classes->Get(i); |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 2100 | if (klass != nullptr && !visitor->operator()(klass)) { |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2101 | return; |
| 2102 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2103 | } |
| 2104 | } |
| 2105 | } |
| 2106 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 2107 | ClassLinker::~ClassLinker() { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2108 | Thread* const self = Thread::Current(); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 2109 | for (const ClassLoaderData& data : class_loaders_) { |
Alexey Grebenkin | be4c2bd | 2018-02-01 19:09:59 +0300 | [diff] [blame] | 2110 | // CHA unloading analysis is not needed. No negative consequences are expected because |
| 2111 | // all the classloaders are deleted at the same time. |
| 2112 | DeleteClassLoader(self, data, false /*cleanup_cha*/); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 2113 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2114 | class_loaders_.clear(); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 2115 | } |
| 2116 | |
Alexey Grebenkin | be4c2bd | 2018-02-01 19:09:59 +0300 | [diff] [blame] | 2117 | void ClassLinker::DeleteClassLoader(Thread* self, const ClassLoaderData& data, bool cleanup_cha) { |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 2118 | Runtime* const runtime = Runtime::Current(); |
| 2119 | JavaVMExt* const vm = runtime->GetJavaVM(); |
| 2120 | vm->DeleteWeakGlobalRef(self, data.weak_root); |
Calin Juravle | e5de54c | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 2121 | // 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] | 2122 | if (runtime->GetJit() != nullptr) { |
| 2123 | jit::JitCodeCache* code_cache = runtime->GetJit()->GetCodeCache(); |
| 2124 | if (code_cache != nullptr) { |
Mathieu Chartier | cf79cf5 | 2017-07-21 11:17:57 -0700 | [diff] [blame] | 2125 | // For the JIT case, RemoveMethodsIn removes the CHA dependencies. |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 2126 | code_cache->RemoveMethodsIn(self, *data.allocator); |
| 2127 | } |
Andreas Gampe | c1ac9ee | 2017-07-24 22:35:49 -0700 | [diff] [blame] | 2128 | } else if (cha_ != nullptr) { |
Mathieu Chartier | cf79cf5 | 2017-07-21 11:17:57 -0700 | [diff] [blame] | 2129 | // If we don't have a JIT, we need to manually remove the CHA dependencies manually. |
Andreas Gampe | c1ac9ee | 2017-07-24 22:35:49 -0700 | [diff] [blame] | 2130 | cha_->RemoveDependenciesForLinearAlloc(data.allocator); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 2131 | } |
Alexey Grebenkin | be4c2bd | 2018-02-01 19:09:59 +0300 | [diff] [blame] | 2132 | // Cleanup references to single implementation ArtMethods that will be deleted. |
| 2133 | if (cleanup_cha) { |
| 2134 | CHAOnDeleteUpdateClassVisitor visitor(data.allocator); |
| 2135 | data.class_table->Visit<CHAOnDeleteUpdateClassVisitor, kWithoutReadBarrier>(visitor); |
| 2136 | } |
| 2137 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 2138 | delete data.allocator; |
| 2139 | delete data.class_table; |
| 2140 | } |
| 2141 | |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 2142 | ObjPtr<mirror::PointerArray> ClassLinker::AllocPointerArray(Thread* self, size_t length) { |
| 2143 | return ObjPtr<mirror::PointerArray>::DownCast( |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 2144 | image_pointer_size_ == PointerSize::k64 |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 2145 | ? ObjPtr<mirror::Array>(mirror::LongArray::Alloc(self, length)) |
| 2146 | : ObjPtr<mirror::Array>(mirror::IntArray::Alloc(self, length))); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2147 | } |
| 2148 | |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 2149 | ObjPtr<mirror::DexCache> ClassLinker::AllocDexCache(/*out*/ ObjPtr<mirror::String>* out_location, |
| 2150 | Thread* self, |
| 2151 | const DexFile& dex_file) { |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2152 | StackHandleScope<1> hs(self); |
| 2153 | DCHECK(out_location != nullptr); |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 2154 | auto dex_cache(hs.NewHandle(ObjPtr<mirror::DexCache>::DownCast( |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 2155 | GetClassRoot<mirror::DexCache>(this)->AllocObject(self)))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 2156 | if (dex_cache == nullptr) { |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2157 | self->AssertPendingOOMException(); |
| 2158 | return nullptr; |
| 2159 | } |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2160 | ObjPtr<mirror::String> location = intern_table_->InternStrong(dex_file.GetLocation().c_str()); |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2161 | if (location == nullptr) { |
| 2162 | self->AssertPendingOOMException(); |
| 2163 | return nullptr; |
| 2164 | } |
| 2165 | *out_location = location; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2166 | return dex_cache.Get(); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 2167 | } |
| 2168 | |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 2169 | ObjPtr<mirror::DexCache> ClassLinker::AllocAndInitializeDexCache(Thread* self, |
| 2170 | const DexFile& dex_file, |
| 2171 | LinearAlloc* linear_alloc) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2172 | ObjPtr<mirror::String> location = nullptr; |
| 2173 | ObjPtr<mirror::DexCache> dex_cache = AllocDexCache(&location, self, dex_file); |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2174 | if (dex_cache != nullptr) { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 2175 | WriterMutexLock mu(self, *Locks::dex_lock_); |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2176 | DCHECK(location != nullptr); |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 2177 | mirror::DexCache::InitializeDexCache(self, |
| 2178 | dex_cache, |
| 2179 | location, |
| 2180 | &dex_file, |
| 2181 | linear_alloc, |
| 2182 | image_pointer_size_); |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2183 | } |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 2184 | return dex_cache; |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2185 | } |
| 2186 | |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 2187 | template <bool kMovable> |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 2188 | ObjPtr<mirror::Class> ClassLinker::AllocClass(Thread* self, |
| 2189 | ObjPtr<mirror::Class> java_lang_Class, |
| 2190 | uint32_t class_size) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2191 | DCHECK_GE(class_size, sizeof(mirror::Class)); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 2192 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2193 | mirror::Class::InitializeClassVisitor visitor(class_size); |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 2194 | ObjPtr<mirror::Object> k = (kMovingClasses && kMovable) ? |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 2195 | heap->AllocObject<true>(self, java_lang_Class, class_size, visitor) : |
| 2196 | heap->AllocNonMovableObject<true>(self, java_lang_Class, class_size, visitor); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 2197 | if (UNLIKELY(k == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2198 | self->AssertPendingOOMException(); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 2199 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2200 | } |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 2201 | return k->AsClass(); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 2202 | } |
| 2203 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 2204 | ObjPtr<mirror::Class> ClassLinker::AllocClass(Thread* self, uint32_t class_size) { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 2205 | return AllocClass(self, GetClassRoot<mirror::Class>(this), class_size); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 2206 | } |
| 2207 | |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 2208 | ObjPtr<mirror::Class> ClassLinker::AllocPrimitiveArrayClass(Thread* self, |
| 2209 | ObjPtr<mirror::Class> java_lang_Class) { |
| 2210 | // We make this class non-movable for the unlikely case where it were to be |
| 2211 | // moved by a sticky-bit (minor) collection when using the Generational |
| 2212 | // Concurrent Copying (CC) collector, potentially creating a stale reference |
| 2213 | // in the `klass_` field of one of its instances allocated in the Large-Object |
| 2214 | // Space (LOS) -- see the comment about the dirty card scanning logic in |
| 2215 | // art::gc::collector::ConcurrentCopying::MarkingPhase. |
| 2216 | return AllocClass</* kMovable */ false>( |
| 2217 | self, java_lang_Class, mirror::Array::ClassSize(image_pointer_size_)); |
| 2218 | } |
| 2219 | |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 2220 | ObjPtr<mirror::ObjectArray<mirror::StackTraceElement>> ClassLinker::AllocStackTraceElementArray( |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 2221 | Thread* self, |
| 2222 | size_t length) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2223 | return mirror::ObjectArray<mirror::StackTraceElement>::Alloc( |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 2224 | self, GetClassRoot<mirror::ObjectArray<mirror::StackTraceElement>>(this), length); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 2225 | } |
| 2226 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 2227 | ObjPtr<mirror::Class> ClassLinker::EnsureResolved(Thread* self, |
| 2228 | const char* descriptor, |
| 2229 | ObjPtr<mirror::Class> klass) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2230 | DCHECK(klass != nullptr); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2231 | if (kIsDebugBuild) { |
| 2232 | StackHandleScope<1> hs(self); |
| 2233 | HandleWrapperObjPtr<mirror::Class> h = hs.NewHandleWrapper(&klass); |
| 2234 | Thread::PoisonObjectPointersIfDebug(); |
| 2235 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2236 | |
| 2237 | // For temporary classes we must wait for them to be retired. |
| 2238 | if (init_done_ && klass->IsTemp()) { |
| 2239 | CHECK(!klass->IsResolved()); |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2240 | if (klass->IsErroneousUnresolved()) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2241 | ThrowEarlierClassFailure(klass); |
| 2242 | return nullptr; |
| 2243 | } |
| 2244 | StackHandleScope<1> hs(self); |
| 2245 | Handle<mirror::Class> h_class(hs.NewHandle(klass)); |
| 2246 | ObjectLock<mirror::Class> lock(self, h_class); |
| 2247 | // Loop and wait for the resolving thread to retire this class. |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2248 | while (!h_class->IsRetired() && !h_class->IsErroneousUnresolved()) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2249 | lock.WaitIgnoringInterrupts(); |
| 2250 | } |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2251 | if (h_class->IsErroneousUnresolved()) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2252 | ThrowEarlierClassFailure(h_class.Get()); |
| 2253 | return nullptr; |
| 2254 | } |
| 2255 | CHECK(h_class->IsRetired()); |
| 2256 | // Get the updated class from class table. |
Andreas Gampe | 34ee684 | 2014-12-02 15:43:52 -0800 | [diff] [blame] | 2257 | klass = LookupClass(self, descriptor, h_class.Get()->GetClassLoader()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2258 | } |
| 2259 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2260 | // Wait for the class if it has not already been linked. |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 2261 | size_t index = 0; |
| 2262 | // Maximum number of yield iterations until we start sleeping. |
| 2263 | static const size_t kNumYieldIterations = 1000; |
| 2264 | // How long each sleep is in us. |
| 2265 | static const size_t kSleepDurationUS = 1000; // 1 ms. |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2266 | while (!klass->IsResolved() && !klass->IsErroneousUnresolved()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2267 | StackHandleScope<1> hs(self); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2268 | HandleWrapperObjPtr<mirror::Class> h_class(hs.NewHandleWrapper(&klass)); |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 2269 | { |
| 2270 | ObjectTryLock<mirror::Class> lock(self, h_class); |
| 2271 | // Can not use a monitor wait here since it may block when returning and deadlock if another |
| 2272 | // thread has locked klass. |
| 2273 | if (lock.Acquired()) { |
| 2274 | // Check for circular dependencies between classes, the lock is required for SetStatus. |
| 2275 | if (!h_class->IsResolved() && h_class->GetClinitThreadId() == self->GetTid()) { |
| 2276 | ThrowClassCircularityError(h_class.Get()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 2277 | mirror::Class::SetStatus(h_class, ClassStatus::kErrorUnresolved, self); |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 2278 | return nullptr; |
| 2279 | } |
| 2280 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2281 | } |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 2282 | { |
| 2283 | // Handle wrapper deals with klass moving. |
| 2284 | ScopedThreadSuspension sts(self, kSuspended); |
| 2285 | if (index < kNumYieldIterations) { |
| 2286 | sched_yield(); |
| 2287 | } else { |
| 2288 | usleep(kSleepDurationUS); |
| 2289 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2290 | } |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 2291 | ++index; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2292 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2293 | |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2294 | if (klass->IsErroneousUnresolved()) { |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 2295 | ThrowEarlierClassFailure(klass); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 2296 | return nullptr; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2297 | } |
| 2298 | // Return the loaded class. No exceptions should be pending. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 2299 | CHECK(klass->IsResolved()) << klass->PrettyClass(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2300 | self->AssertNoPendingException(); |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 2301 | return klass; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2302 | } |
| 2303 | |
Andreas Gampe | ee07743 | 2018-09-20 15:05:17 +0000 | [diff] [blame] | 2304 | typedef std::pair<const DexFile*, const DexFile::ClassDef*> ClassPathEntry; |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2305 | |
| 2306 | // Search a collection of DexFiles for a descriptor |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2307 | ClassPathEntry FindInClassPath(const char* descriptor, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 2308 | size_t hash, const std::vector<const DexFile*>& class_path) { |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2309 | for (const DexFile* dex_file : class_path) { |
David Sehr | 9aa352e | 2016-09-15 18:13:52 -0700 | [diff] [blame] | 2310 | const DexFile::ClassDef* dex_class_def = OatDexFile::FindClassDef(*dex_file, descriptor, hash); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2311 | if (dex_class_def != nullptr) { |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2312 | return ClassPathEntry(dex_file, dex_class_def); |
| 2313 | } |
| 2314 | } |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2315 | return ClassPathEntry(nullptr, nullptr); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2316 | } |
| 2317 | |
Nicolas Geoffray | 7d8d8ff | 2016-11-02 12:38:05 +0000 | [diff] [blame] | 2318 | bool ClassLinker::FindClassInBaseDexClassLoader(ScopedObjectAccessAlreadyRunnable& soa, |
| 2319 | Thread* self, |
| 2320 | const char* descriptor, |
| 2321 | size_t hash, |
| 2322 | Handle<mirror::ClassLoader> class_loader, |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 2323 | /*out*/ ObjPtr<mirror::Class>* result) { |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 2324 | // Termination case: boot class loader. |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2325 | if (IsBootClassLoader(soa, class_loader.Get())) { |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 2326 | *result = FindClassInBootClassLoaderClassPath(self, descriptor, hash); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2327 | return true; |
| 2328 | } |
| 2329 | |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 2330 | if (IsPathOrDexClassLoader(soa, class_loader)) { |
| 2331 | // For regular path or dex class loader the search order is: |
| 2332 | // - parent |
| 2333 | // - class loader dex files |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2334 | |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 2335 | // Handles as RegisterDexFile may allocate dex caches (and cause thread suspension). |
| 2336 | StackHandleScope<1> hs(self); |
| 2337 | Handle<mirror::ClassLoader> h_parent(hs.NewHandle(class_loader->GetParent())); |
| 2338 | if (!FindClassInBaseDexClassLoader(soa, self, descriptor, hash, h_parent, result)) { |
| 2339 | return false; // One of the parents is not supported. |
| 2340 | } |
| 2341 | if (*result != nullptr) { |
| 2342 | return true; // Found the class up the chain. |
| 2343 | } |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2344 | |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 2345 | // Search the current class loader classpath. |
| 2346 | *result = FindClassInBaseDexClassLoaderClassPath(soa, descriptor, hash, class_loader); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2347 | return true; |
| 2348 | } |
| 2349 | |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 2350 | if (IsDelegateLastClassLoader(soa, class_loader)) { |
| 2351 | // For delegate last, the search order is: |
| 2352 | // - boot class path |
| 2353 | // - class loader dex files |
| 2354 | // - parent |
| 2355 | *result = FindClassInBootClassLoaderClassPath(self, descriptor, hash); |
| 2356 | if (*result != nullptr) { |
| 2357 | return true; // The class is part of the boot class path. |
| 2358 | } |
| 2359 | |
| 2360 | *result = FindClassInBaseDexClassLoaderClassPath(soa, descriptor, hash, class_loader); |
| 2361 | if (*result != nullptr) { |
| 2362 | return true; // Found the class in the current class loader |
| 2363 | } |
| 2364 | |
| 2365 | // Handles as RegisterDexFile may allocate dex caches (and cause thread suspension). |
| 2366 | StackHandleScope<1> hs(self); |
| 2367 | Handle<mirror::ClassLoader> h_parent(hs.NewHandle(class_loader->GetParent())); |
| 2368 | return FindClassInBaseDexClassLoader(soa, self, descriptor, hash, h_parent, result); |
| 2369 | } |
| 2370 | |
| 2371 | // Unsupported class loader. |
| 2372 | *result = nullptr; |
| 2373 | return false; |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 2374 | } |
| 2375 | |
| 2376 | // Finds the class in the boot class loader. |
| 2377 | // If the class is found the method returns the resolved class. Otherwise it returns null. |
| 2378 | ObjPtr<mirror::Class> ClassLinker::FindClassInBootClassLoaderClassPath(Thread* self, |
| 2379 | const char* descriptor, |
| 2380 | size_t hash) { |
| 2381 | ObjPtr<mirror::Class> result = nullptr; |
| 2382 | ClassPathEntry pair = FindInClassPath(descriptor, hash, boot_class_path_); |
| 2383 | if (pair.second != nullptr) { |
| 2384 | ObjPtr<mirror::Class> klass = LookupClass(self, descriptor, hash, nullptr); |
| 2385 | if (klass != nullptr) { |
| 2386 | result = EnsureResolved(self, descriptor, klass); |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2387 | } else { |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 2388 | result = DefineClass(self, |
| 2389 | descriptor, |
| 2390 | hash, |
| 2391 | ScopedNullHandle<mirror::ClassLoader>(), |
| 2392 | *pair.first, |
| 2393 | *pair.second); |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2394 | } |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 2395 | if (result == nullptr) { |
| 2396 | CHECK(self->IsExceptionPending()) << descriptor; |
| 2397 | self->ClearException(); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2398 | } |
| 2399 | } |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 2400 | return result; |
| 2401 | } |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2402 | |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 2403 | ObjPtr<mirror::Class> ClassLinker::FindClassInBaseDexClassLoaderClassPath( |
| 2404 | ScopedObjectAccessAlreadyRunnable& soa, |
| 2405 | const char* descriptor, |
| 2406 | size_t hash, |
| 2407 | Handle<mirror::ClassLoader> class_loader) { |
Andreas Gampe | b8e7c37 | 2018-02-20 18:24:55 -0800 | [diff] [blame] | 2408 | DCHECK(IsPathOrDexClassLoader(soa, class_loader) || IsDelegateLastClassLoader(soa, class_loader)) |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 2409 | << "Unexpected class loader for descriptor " << descriptor; |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2410 | |
Andreas Gampe | b8e7c37 | 2018-02-20 18:24:55 -0800 | [diff] [blame] | 2411 | ObjPtr<mirror::Class> ret; |
| 2412 | auto define_class = [&](const DexFile* cp_dex_file) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 2413 | const DexFile::ClassDef* dex_class_def = |
| 2414 | OatDexFile::FindClassDef(*cp_dex_file, descriptor, hash); |
| 2415 | if (dex_class_def != nullptr) { |
| 2416 | ObjPtr<mirror::Class> klass = DefineClass(soa.Self(), |
| 2417 | descriptor, |
| 2418 | hash, |
| 2419 | class_loader, |
| 2420 | *cp_dex_file, |
| 2421 | *dex_class_def); |
| 2422 | if (klass == nullptr) { |
| 2423 | CHECK(soa.Self()->IsExceptionPending()) << descriptor; |
| 2424 | soa.Self()->ClearException(); |
| 2425 | // TODO: Is it really right to break here, and not check the other dex files? |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2426 | } |
Andreas Gampe | b8e7c37 | 2018-02-20 18:24:55 -0800 | [diff] [blame] | 2427 | ret = klass; |
| 2428 | return false; // Found a Class (or error == nullptr), stop visit. |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2429 | } |
Andreas Gampe | b8e7c37 | 2018-02-20 18:24:55 -0800 | [diff] [blame] | 2430 | return true; // Continue with the next DexFile. |
| 2431 | }; |
| 2432 | |
| 2433 | VisitClassLoaderDexFiles(soa, class_loader, define_class); |
| 2434 | return ret; |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2435 | } |
| 2436 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 2437 | ObjPtr<mirror::Class> ClassLinker::FindClass(Thread* self, |
| 2438 | const char* descriptor, |
| 2439 | Handle<mirror::ClassLoader> class_loader) { |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 2440 | DCHECK_NE(*descriptor, '\0') << "descriptor is empty string"; |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 2441 | DCHECK(self != nullptr); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2442 | self->AssertNoPendingException(); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 2443 | self->PoisonObjectPointers(); // For DefineClass, CreateArrayClass, etc... |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 2444 | if (descriptor[1] == '\0') { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2445 | // only the descriptors of primitive types should be 1 character long, also avoid class lookup |
| 2446 | // for primitive classes that aren't backed by dex files. |
| 2447 | return FindPrimitiveClass(descriptor[0]); |
| 2448 | } |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2449 | const size_t hash = ComputeModifiedUtf8Hash(descriptor); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2450 | // Find the class in the loaded classes table. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2451 | ObjPtr<mirror::Class> klass = LookupClass(self, descriptor, hash, class_loader.Get()); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2452 | if (klass != nullptr) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2453 | return EnsureResolved(self, descriptor, klass); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2454 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2455 | // Class is not yet loaded. |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 2456 | if (descriptor[0] != '[' && class_loader == nullptr) { |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 2457 | // 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] | 2458 | ClassPathEntry pair = FindInClassPath(descriptor, hash, boot_class_path_); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2459 | if (pair.second != nullptr) { |
Mathieu Chartier | 9865bde | 2015-12-21 09:58:16 -0800 | [diff] [blame] | 2460 | return DefineClass(self, |
| 2461 | descriptor, |
| 2462 | hash, |
| 2463 | ScopedNullHandle<mirror::ClassLoader>(), |
| 2464 | *pair.first, |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2465 | *pair.second); |
Ian Rogers | 6355745 | 2014-06-04 16:57:15 -0700 | [diff] [blame] | 2466 | } else { |
| 2467 | // The boot class loader is searched ahead of the application class loader, failures are |
| 2468 | // expected and will be wrapped in a ClassNotFoundException. Use the pre-allocated error to |
| 2469 | // trigger the chaining with a proper stack trace. |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 2470 | ObjPtr<mirror::Throwable> pre_allocated = |
| 2471 | Runtime::Current()->GetPreAllocatedNoClassDefFoundError(); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2472 | self->SetException(pre_allocated); |
Ian Rogers | 6355745 | 2014-06-04 16:57:15 -0700 | [diff] [blame] | 2473 | return nullptr; |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 2474 | } |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 2475 | } |
| 2476 | ObjPtr<mirror::Class> result_ptr; |
| 2477 | bool descriptor_equals; |
| 2478 | if (descriptor[0] == '[') { |
| 2479 | result_ptr = CreateArrayClass(self, descriptor, hash, class_loader); |
| 2480 | DCHECK_EQ(result_ptr == nullptr, self->IsExceptionPending()); |
| 2481 | DCHECK(result_ptr == nullptr || result_ptr->DescriptorEquals(descriptor)); |
| 2482 | descriptor_equals = true; |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 2483 | } else { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 2484 | ScopedObjectAccessUnchecked soa(self); |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2485 | bool known_hierarchy = |
| 2486 | FindClassInBaseDexClassLoader(soa, self, descriptor, hash, class_loader, &result_ptr); |
| 2487 | if (result_ptr != nullptr) { |
| 2488 | // The chain was understood and we found the class. We still need to add the class to |
| 2489 | // the class table to protect from racy programs that can try and redefine the path list |
| 2490 | // which would change the Class<?> returned for subsequent evaluation of const-class. |
| 2491 | DCHECK(known_hierarchy); |
| 2492 | DCHECK(result_ptr->DescriptorEquals(descriptor)); |
| 2493 | descriptor_equals = true; |
| 2494 | } else { |
| 2495 | // Either the chain wasn't understood or the class wasn't found. |
| 2496 | // |
| 2497 | // If the chain was understood but we did not find the class, let the Java-side |
| 2498 | // rediscover all this and throw the exception with the right stack trace. Note that |
| 2499 | // the Java-side could still succeed for racy programs if another thread is actively |
| 2500 | // modifying the class loader's path list. |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2501 | |
Calin Juravle | ccd5695 | 2016-12-15 17:57:38 +0000 | [diff] [blame] | 2502 | if (!self->CanCallIntoJava()) { |
| 2503 | // Oops, we can't call into java so we can't run actual class-loader code. |
| 2504 | // This is true for e.g. for the compiler (jit or aot). |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2505 | ObjPtr<mirror::Throwable> pre_allocated = |
| 2506 | Runtime::Current()->GetPreAllocatedNoClassDefFoundError(); |
| 2507 | self->SetException(pre_allocated); |
Vladimir Marko | 2c8c6b6 | 2016-12-01 17:42:00 +0000 | [diff] [blame] | 2508 | return nullptr; |
| 2509 | } |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2510 | |
Vladimir Marko | 5fdd778 | 2017-04-20 11:26:03 +0100 | [diff] [blame] | 2511 | // Inlined DescriptorToDot(descriptor) with extra validation. |
| 2512 | // |
| 2513 | // Throw NoClassDefFoundError early rather than potentially load a class only to fail |
| 2514 | // the DescriptorEquals() check below and give a confusing error message. For example, |
| 2515 | // when native code erroneously calls JNI GetFieldId() with signature "java/lang/String" |
| 2516 | // instead of "Ljava/lang/String;", the message below using the "dot" names would be |
| 2517 | // "class loader [...] returned class java.lang.String instead of java.lang.String". |
| 2518 | size_t descriptor_length = strlen(descriptor); |
| 2519 | if (UNLIKELY(descriptor[0] != 'L') || |
| 2520 | UNLIKELY(descriptor[descriptor_length - 1] != ';') || |
| 2521 | UNLIKELY(memchr(descriptor + 1, '.', descriptor_length - 2) != nullptr)) { |
| 2522 | ThrowNoClassDefFoundError("Invalid descriptor: %s.", descriptor); |
| 2523 | return nullptr; |
| 2524 | } |
| 2525 | std::string class_name_string(descriptor + 1, descriptor_length - 2); |
| 2526 | std::replace(class_name_string.begin(), class_name_string.end(), '/', '.'); |
| 2527 | |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2528 | ScopedLocalRef<jobject> class_loader_object( |
| 2529 | soa.Env(), soa.AddLocalReference<jobject>(class_loader.Get())); |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2530 | ScopedLocalRef<jobject> result(soa.Env(), nullptr); |
| 2531 | { |
| 2532 | ScopedThreadStateChange tsc(self, kNative); |
| 2533 | ScopedLocalRef<jobject> class_name_object( |
| 2534 | soa.Env(), soa.Env()->NewStringUTF(class_name_string.c_str())); |
| 2535 | if (class_name_object.get() == nullptr) { |
| 2536 | DCHECK(self->IsExceptionPending()); // OOME. |
| 2537 | return nullptr; |
| 2538 | } |
| 2539 | CHECK(class_loader_object.get() != nullptr); |
| 2540 | result.reset(soa.Env()->CallObjectMethod(class_loader_object.get(), |
| 2541 | WellKnownClasses::java_lang_ClassLoader_loadClass, |
| 2542 | class_name_object.get())); |
| 2543 | } |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 2544 | if (result.get() == nullptr && !self->IsExceptionPending()) { |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2545 | // broken loader - throw NPE to be compatible with Dalvik |
| 2546 | ThrowNullPointerException(StringPrintf("ClassLoader.loadClass returned null for %s", |
| 2547 | class_name_string.c_str()).c_str()); |
| 2548 | return nullptr; |
| 2549 | } |
| 2550 | result_ptr = soa.Decode<mirror::Class>(result.get()); |
| 2551 | // Check the name of the returned class. |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 2552 | descriptor_equals = (result_ptr != nullptr) && result_ptr->DescriptorEquals(descriptor); |
Vladimir Marko | 2c8c6b6 | 2016-12-01 17:42:00 +0000 | [diff] [blame] | 2553 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2554 | } |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 2555 | |
| 2556 | if (self->IsExceptionPending()) { |
| 2557 | // If the ClassLoader threw or array class allocation failed, pass that exception up. |
| 2558 | // However, to comply with the RI behavior, first check if another thread succeeded. |
| 2559 | result_ptr = LookupClass(self, descriptor, hash, class_loader.Get()); |
| 2560 | if (result_ptr != nullptr && !result_ptr->IsErroneous()) { |
| 2561 | self->ClearException(); |
| 2562 | return EnsureResolved(self, descriptor, result_ptr); |
| 2563 | } |
| 2564 | return nullptr; |
| 2565 | } |
| 2566 | |
| 2567 | // Try to insert the class to the class table, checking for mismatch. |
| 2568 | ObjPtr<mirror::Class> old; |
| 2569 | { |
| 2570 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 2571 | ClassTable* const class_table = InsertClassTableForClassLoader(class_loader.Get()); |
| 2572 | old = class_table->Lookup(descriptor, hash); |
| 2573 | if (old == nullptr) { |
| 2574 | old = result_ptr; // For the comparison below, after releasing the lock. |
| 2575 | if (descriptor_equals) { |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 2576 | class_table->InsertWithHash(result_ptr, hash); |
Mathieu Chartier | 88ea61e | 2018-06-20 17:45:41 -0700 | [diff] [blame] | 2577 | WriteBarrier::ForEveryFieldWrite(class_loader.Get()); |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 2578 | } // else throw below, after releasing the lock. |
| 2579 | } |
| 2580 | } |
| 2581 | if (UNLIKELY(old != result_ptr)) { |
| 2582 | // Return `old` (even if `!descriptor_equals`) to mimic the RI behavior for parallel |
| 2583 | // capable class loaders. (All class loaders are considered parallel capable on Android.) |
| 2584 | mirror::Class* loader_class = class_loader->GetClass(); |
| 2585 | const char* loader_class_name = |
| 2586 | loader_class->GetDexFile().StringByTypeIdx(loader_class->GetDexTypeIndex()); |
| 2587 | LOG(WARNING) << "Initiating class loader of type " << DescriptorToDot(loader_class_name) |
| 2588 | << " is not well-behaved; it returned a different Class for racing loadClass(\"" |
| 2589 | << DescriptorToDot(descriptor) << "\")."; |
| 2590 | return EnsureResolved(self, descriptor, old); |
| 2591 | } |
| 2592 | if (UNLIKELY(!descriptor_equals)) { |
| 2593 | std::string result_storage; |
| 2594 | const char* result_name = result_ptr->GetDescriptor(&result_storage); |
| 2595 | std::string loader_storage; |
| 2596 | const char* loader_class_name = class_loader->GetClass()->GetDescriptor(&loader_storage); |
| 2597 | ThrowNoClassDefFoundError( |
| 2598 | "Initiating class loader of type %s returned class %s instead of %s.", |
| 2599 | DescriptorToDot(loader_class_name).c_str(), |
| 2600 | DescriptorToDot(result_name).c_str(), |
| 2601 | DescriptorToDot(descriptor).c_str()); |
| 2602 | return nullptr; |
| 2603 | } |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 2604 | // Success. |
| 2605 | return result_ptr; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2606 | } |
| 2607 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 2608 | ObjPtr<mirror::Class> ClassLinker::DefineClass(Thread* self, |
| 2609 | const char* descriptor, |
| 2610 | size_t hash, |
| 2611 | Handle<mirror::ClassLoader> class_loader, |
| 2612 | const DexFile& dex_file, |
| 2613 | const DexFile::ClassDef& dex_class_def) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2614 | StackHandleScope<3> hs(self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2615 | auto klass = hs.NewHandle<mirror::Class>(nullptr); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2616 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2617 | // Load the class from the dex file. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2618 | if (UNLIKELY(!init_done_)) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2619 | // finish up init of hand crafted class_roots_ |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2620 | if (strcmp(descriptor, "Ljava/lang/Object;") == 0) { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 2621 | klass.Assign(GetClassRoot<mirror::Object>(this)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2622 | } else if (strcmp(descriptor, "Ljava/lang/Class;") == 0) { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 2623 | klass.Assign(GetClassRoot<mirror::Class>(this)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2624 | } else if (strcmp(descriptor, "Ljava/lang/String;") == 0) { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 2625 | klass.Assign(GetClassRoot<mirror::String>(this)); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 2626 | } else if (strcmp(descriptor, "Ljava/lang/ref/Reference;") == 0) { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 2627 | klass.Assign(GetClassRoot<mirror::Reference>(this)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 2628 | } else if (strcmp(descriptor, "Ljava/lang/DexCache;") == 0) { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 2629 | klass.Assign(GetClassRoot<mirror::DexCache>(this)); |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 2630 | } else if (strcmp(descriptor, "Ldalvik/system/ClassExt;") == 0) { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 2631 | klass.Assign(GetClassRoot<mirror::ClassExt>(this)); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2632 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2633 | } |
| 2634 | |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 2635 | if (klass == nullptr) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2636 | // Allocate a class with the status of not ready. |
| 2637 | // Interface object should get the right size here. Regular class will |
| 2638 | // figure out the right size later and be replaced with one of the right |
| 2639 | // size when the class becomes resolved. |
Nicolas Geoffray | abadf02 | 2017-08-03 08:25:41 +0000 | [diff] [blame] | 2640 | klass.Assign(AllocClass(self, SizeOfClassWithoutEmbeddedTables(dex_file, dex_class_def))); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2641 | } |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 2642 | if (UNLIKELY(klass == nullptr)) { |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 2643 | self->AssertPendingOOMException(); |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 2644 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2645 | } |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 2646 | // Get the real dex file. This will return the input if there aren't any callbacks or they do |
| 2647 | // nothing. |
| 2648 | DexFile const* new_dex_file = nullptr; |
| 2649 | DexFile::ClassDef const* new_class_def = nullptr; |
| 2650 | // TODO We should ideally figure out some way to move this after we get a lock on the klass so it |
| 2651 | // will only be called once. |
| 2652 | Runtime::Current()->GetRuntimeCallbacks()->ClassPreDefine(descriptor, |
| 2653 | klass, |
| 2654 | class_loader, |
| 2655 | dex_file, |
| 2656 | dex_class_def, |
| 2657 | &new_dex_file, |
| 2658 | &new_class_def); |
Alex Light | 440b5d9 | 2017-01-24 15:32:25 -0800 | [diff] [blame] | 2659 | // Check to see if an exception happened during runtime callbacks. Return if so. |
| 2660 | if (self->IsExceptionPending()) { |
| 2661 | return nullptr; |
| 2662 | } |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 2663 | ObjPtr<mirror::DexCache> dex_cache = RegisterDexFile(*new_dex_file, class_loader.Get()); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 2664 | if (dex_cache == nullptr) { |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 2665 | self->AssertPendingException(); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 2666 | return nullptr; |
| 2667 | } |
| 2668 | klass->SetDexCache(dex_cache); |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 2669 | SetupClass(*new_dex_file, *new_class_def, klass, class_loader.Get()); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2670 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 2671 | // Mark the string class by setting its access flag. |
| 2672 | if (UNLIKELY(!init_done_)) { |
| 2673 | if (strcmp(descriptor, "Ljava/lang/String;") == 0) { |
| 2674 | klass->SetStringClass(); |
| 2675 | } |
| 2676 | } |
| 2677 | |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 2678 | ObjectLock<mirror::Class> lock(self, klass); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2679 | klass->SetClinitThreadId(self->GetTid()); |
Mathieu Chartier | 1e4841e | 2016-12-15 14:21:04 -0800 | [diff] [blame] | 2680 | // Make sure we have a valid empty iftable even if there are errors. |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 2681 | klass->SetIfTable(GetClassRoot<mirror::Object>(this)->GetIfTable()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2682 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2683 | // Add the newly loaded class to the loaded classes table. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2684 | ObjPtr<mirror::Class> existing = InsertClass(descriptor, klass.Get(), hash); |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 2685 | if (existing != nullptr) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2686 | // We failed to insert because we raced with another thread. Calling EnsureResolved may cause |
| 2687 | // this thread to block. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2688 | return EnsureResolved(self, descriptor, existing); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2689 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2690 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2691 | // Load the fields and other things after we are inserted in the table. This is so that we don't |
| 2692 | // end up allocating unfree-able linear alloc resources and then lose the race condition. The |
| 2693 | // other reason is that the field roots are only visited from the class table. So we need to be |
| 2694 | // inserted before we allocate / fill in these fields. |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 2695 | LoadClass(self, *new_dex_file, *new_class_def, klass); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2696 | if (self->IsExceptionPending()) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2697 | VLOG(class_linker) << self->GetException()->Dump(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2698 | // An exception occured during load, set status to erroneous while holding klass' lock in case |
| 2699 | // notification is necessary. |
| 2700 | if (!klass->IsErroneous()) { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 2701 | mirror::Class::SetStatus(klass, ClassStatus::kErrorUnresolved, self); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 2702 | } |
| 2703 | return nullptr; |
| 2704 | } |
| 2705 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2706 | // Finish loading (if necessary) by finding parents |
| 2707 | CHECK(!klass->IsLoaded()); |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 2708 | if (!LoadSuperAndInterfaces(klass, *new_dex_file)) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2709 | // Loading failed. |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 2710 | if (!klass->IsErroneous()) { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 2711 | mirror::Class::SetStatus(klass, ClassStatus::kErrorUnresolved, self); |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 2712 | } |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 2713 | return nullptr; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2714 | } |
| 2715 | CHECK(klass->IsLoaded()); |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 2716 | |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 2717 | // At this point the class is loaded. Publish a ClassLoad event. |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 2718 | // 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] | 2719 | Runtime::Current()->GetRuntimeCallbacks()->ClassLoad(klass); |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 2720 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2721 | // Link the class (if necessary) |
| 2722 | CHECK(!klass->IsResolved()); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2723 | // TODO: Use fast jobjects? |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2724 | auto interfaces = hs.NewHandle<mirror::ObjectArray<mirror::Class>>(nullptr); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2725 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 2726 | MutableHandle<mirror::Class> h_new_class = hs.NewHandle<mirror::Class>(nullptr); |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 2727 | if (!LinkClass(self, descriptor, klass, interfaces, &h_new_class)) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2728 | // Linking failed. |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 2729 | if (!klass->IsErroneous()) { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 2730 | mirror::Class::SetStatus(klass, ClassStatus::kErrorUnresolved, self); |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 2731 | } |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 2732 | return nullptr; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2733 | } |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 2734 | self->AssertNoPendingException(); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 2735 | CHECK(h_new_class != nullptr) << descriptor; |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2736 | CHECK(h_new_class->IsResolved() && !h_new_class->IsErroneousResolved()) << descriptor; |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 2737 | |
Sebastien Hertz | a8a697f | 2015-01-15 12:28:47 +0100 | [diff] [blame] | 2738 | // Instrumentation may have updated entrypoints for all methods of all |
| 2739 | // classes. However it could not update methods of this class while we |
| 2740 | // were loading it. Now the class is resolved, we can update entrypoints |
| 2741 | // as required by instrumentation. |
| 2742 | if (Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled()) { |
| 2743 | // We must be in the kRunnable state to prevent instrumentation from |
| 2744 | // suspending all threads to update entrypoints while we are doing it |
| 2745 | // for this class. |
| 2746 | DCHECK_EQ(self->GetState(), kRunnable); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 2747 | Runtime::Current()->GetInstrumentation()->InstallStubsForClass(h_new_class.Get()); |
Sebastien Hertz | a8a697f | 2015-01-15 12:28:47 +0100 | [diff] [blame] | 2748 | } |
| 2749 | |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 2750 | /* |
| 2751 | * We send CLASS_PREPARE events to the debugger from here. The |
| 2752 | * definition of "preparation" is creating the static fields for a |
| 2753 | * class and initializing them to the standard default values, but not |
| 2754 | * executing any code (that comes later, during "initialization"). |
| 2755 | * |
| 2756 | * We did the static preparation in LinkClass. |
| 2757 | * |
| 2758 | * The class has been prepared and resolved but possibly not yet verified |
| 2759 | * at this point. |
| 2760 | */ |
Andreas Gampe | ac30fa2 | 2017-01-18 21:02:36 -0800 | [diff] [blame] | 2761 | Runtime::Current()->GetRuntimeCallbacks()->ClassPrepare(klass, h_new_class); |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 2762 | |
Tamas Berghammer | 160e6df | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 2763 | // Notify native debugger of the new class and its layout. |
| 2764 | jit::Jit::NewTypeLoadedIfUsingJit(h_new_class.Get()); |
| 2765 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 2766 | return h_new_class.Get(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2767 | } |
| 2768 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2769 | uint32_t ClassLinker::SizeOfClassWithoutEmbeddedTables(const DexFile& dex_file, |
| 2770 | const DexFile::ClassDef& dex_class_def) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2771 | size_t num_ref = 0; |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 2772 | size_t num_8 = 0; |
| 2773 | size_t num_16 = 0; |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2774 | size_t num_32 = 0; |
| 2775 | size_t num_64 = 0; |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 2776 | ClassAccessor accessor(dex_file, dex_class_def); |
| 2777 | // We allow duplicate definitions of the same field in a class_data_item |
| 2778 | // but ignore the repeated indexes here, b/21868015. |
| 2779 | uint32_t last_field_idx = dex::kDexNoIndex; |
| 2780 | for (const ClassAccessor::Field& field : accessor.GetStaticFields()) { |
| 2781 | uint32_t field_idx = field.GetIndex(); |
| 2782 | // Ordering enforced by DexFileVerifier. |
| 2783 | DCHECK(last_field_idx == dex::kDexNoIndex || last_field_idx <= field_idx); |
| 2784 | if (UNLIKELY(field_idx == last_field_idx)) { |
| 2785 | continue; |
| 2786 | } |
| 2787 | last_field_idx = field_idx; |
| 2788 | const DexFile::FieldId& field_id = dex_file.GetFieldId(field_idx); |
| 2789 | const char* descriptor = dex_file.GetFieldTypeDescriptor(field_id); |
| 2790 | char c = descriptor[0]; |
| 2791 | switch (c) { |
| 2792 | case 'L': |
| 2793 | case '[': |
| 2794 | num_ref++; |
| 2795 | break; |
| 2796 | case 'J': |
| 2797 | case 'D': |
| 2798 | num_64++; |
| 2799 | break; |
| 2800 | case 'I': |
| 2801 | case 'F': |
| 2802 | num_32++; |
| 2803 | break; |
| 2804 | case 'S': |
| 2805 | case 'C': |
| 2806 | num_16++; |
| 2807 | break; |
| 2808 | case 'B': |
| 2809 | case 'Z': |
| 2810 | num_8++; |
| 2811 | break; |
| 2812 | default: |
| 2813 | LOG(FATAL) << "Unknown descriptor: " << c; |
| 2814 | UNREACHABLE(); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2815 | } |
| 2816 | } |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 2817 | return mirror::Class::ComputeClassSize(false, |
| 2818 | 0, |
| 2819 | num_8, |
| 2820 | num_16, |
| 2821 | num_32, |
| 2822 | num_64, |
| 2823 | num_ref, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2824 | image_pointer_size_); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 2825 | } |
| 2826 | |
Alex Light | fc49fec | 2018-01-16 22:28:36 +0000 | [diff] [blame] | 2827 | // Special case to get oat code without overwriting a trampoline. |
| 2828 | const void* ClassLinker::GetQuickOatCodeFor(ArtMethod* method) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 2829 | CHECK(method->IsInvokable()) << method->PrettyMethod(); |
Nicolas Geoffray | a7a4759 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 2830 | if (method->IsProxyMethod()) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2831 | return GetQuickProxyInvokeHandler(); |
Jeff Hao | 8df6cea | 2013-07-29 13:54:48 -0700 | [diff] [blame] | 2832 | } |
Alex Light | fc49fec | 2018-01-16 22:28:36 +0000 | [diff] [blame] | 2833 | auto* code = method->GetOatMethodQuickCode(GetImagePointerSize()); |
| 2834 | if (code != nullptr) { |
| 2835 | return code; |
Mathieu Chartier | 2535abe | 2015-02-17 10:38:49 -0800 | [diff] [blame] | 2836 | } |
Alex Light | fc49fec | 2018-01-16 22:28:36 +0000 | [diff] [blame] | 2837 | if (method->IsNative()) { |
| 2838 | // No code and native? Use generic trampoline. |
| 2839 | return GetQuickGenericJniStub(); |
| 2840 | } |
| 2841 | return GetQuickToInterpreterBridge(); |
TDYa127 | 8532191 | 2012-04-01 15:24:56 -0700 | [diff] [blame] | 2842 | } |
| 2843 | |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 2844 | bool ClassLinker::ShouldUseInterpreterEntrypoint(ArtMethod* method, const void* quick_code) { |
Alex Light | 2d441b1 | 2018-06-08 15:33:21 -0700 | [diff] [blame] | 2845 | ScopedAssertNoThreadSuspension sants(__FUNCTION__); |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 2846 | if (UNLIKELY(method->IsNative() || method->IsProxyMethod())) { |
| 2847 | return false; |
| 2848 | } |
| 2849 | |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 2850 | if (quick_code == nullptr) { |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2851 | return true; |
| 2852 | } |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 2853 | |
| 2854 | Runtime* runtime = Runtime::Current(); |
| 2855 | instrumentation::Instrumentation* instr = runtime->GetInstrumentation(); |
| 2856 | if (instr->InterpretOnly()) { |
| 2857 | return true; |
| 2858 | } |
| 2859 | |
| 2860 | if (runtime->GetClassLinker()->IsQuickToInterpreterBridge(quick_code)) { |
| 2861 | // Doing this check avoids doing compiled/interpreter transitions. |
| 2862 | return true; |
| 2863 | } |
| 2864 | |
| 2865 | if (Dbg::IsForcedInterpreterNeededForCalling(Thread::Current(), method)) { |
| 2866 | // Force the use of interpreter when it is required by the debugger. |
| 2867 | return true; |
| 2868 | } |
| 2869 | |
Alex Light | 8f34aba | 2017-10-09 13:46:32 -0700 | [diff] [blame] | 2870 | if (Thread::Current()->IsAsyncExceptionPending()) { |
| 2871 | // Force use of interpreter to handle async-exceptions |
| 2872 | return true; |
| 2873 | } |
| 2874 | |
Alex Light | 2d441b1 | 2018-06-08 15:33:21 -0700 | [diff] [blame] | 2875 | if (quick_code == GetQuickInstrumentationEntryPoint()) { |
| 2876 | const void* instr_target = instr->GetCodeForInvoke(method); |
| 2877 | DCHECK_NE(instr_target, GetQuickInstrumentationEntryPoint()) << method->PrettyMethod(); |
| 2878 | return ShouldUseInterpreterEntrypoint(method, instr_target); |
| 2879 | } |
| 2880 | |
Nicolas Geoffray | 433b79a | 2017-01-30 20:54:45 +0000 | [diff] [blame] | 2881 | if (runtime->IsJavaDebuggable()) { |
| 2882 | // For simplicity, we ignore precompiled code and go to the interpreter |
| 2883 | // assuming we don't already have jitted code. |
| 2884 | // We could look at the oat file where `quick_code` is being defined, |
| 2885 | // and check whether it's been compiled debuggable, but we decided to |
| 2886 | // only rely on the JIT for debuggable apps. |
Alex Light | 6b16d89 | 2016-11-11 11:21:04 -0800 | [diff] [blame] | 2887 | jit::Jit* jit = Runtime::Current()->GetJit(); |
| 2888 | return (jit == nullptr) || !jit->GetCodeCache()->ContainsPc(quick_code); |
| 2889 | } |
| 2890 | |
David Srbecky | f448016 | 2016-03-16 00:06:24 +0000 | [diff] [blame] | 2891 | if (runtime->IsNativeDebuggable()) { |
Calin Juravle | e5de54c | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 2892 | DCHECK(runtime->UseJitCompilation() && runtime->GetJit()->JitAtFirstUse()); |
David Srbecky | f448016 | 2016-03-16 00:06:24 +0000 | [diff] [blame] | 2893 | // If we are doing native debugging, ignore application's AOT code, |
Nicolas Geoffray | 433b79a | 2017-01-30 20:54:45 +0000 | [diff] [blame] | 2894 | // since we want to JIT it (at first use) with extra stackmaps for native |
| 2895 | // debugging. We keep however all AOT code from the boot image, |
| 2896 | // since the JIT-at-first-use is blocking and would result in non-negligible |
| 2897 | // startup performance impact. |
David Srbecky | f448016 | 2016-03-16 00:06:24 +0000 | [diff] [blame] | 2898 | return !runtime->GetHeap()->IsInBootImageOatFile(quick_code); |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 2899 | } |
| 2900 | |
| 2901 | return false; |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2902 | } |
| 2903 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2904 | void ClassLinker::FixupStaticTrampolines(ObjPtr<mirror::Class> klass) { |
Alex Light | 2d441b1 | 2018-06-08 15:33:21 -0700 | [diff] [blame] | 2905 | ScopedAssertNoThreadSuspension sants(__FUNCTION__); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 2906 | DCHECK(klass->IsInitialized()) << klass->PrettyDescriptor(); |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 2907 | if (klass->NumDirectMethods() == 0) { |
| 2908 | return; // No direct methods => no static methods. |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2909 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2910 | Runtime* runtime = Runtime::Current(); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 2911 | if (!runtime->IsStarted()) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 2912 | if (runtime->IsAotCompiler() || runtime->GetHeap()->HasBootImageSpace()) { |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 2913 | return; // OAT file unavailable. |
| 2914 | } |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2915 | } |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 2916 | |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 2917 | const DexFile& dex_file = klass->GetDexFile(); |
Mathieu Chartier | 18e2687 | 2018-06-04 17:19:02 -0700 | [diff] [blame] | 2918 | const uint16_t class_def_idx = klass->GetDexClassDefIndex(); |
| 2919 | CHECK_NE(class_def_idx, DexFile::kDexNoIndex16); |
| 2920 | ClassAccessor accessor(dex_file, class_def_idx); |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 2921 | // There should always be class data if there were direct methods. |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 2922 | CHECK(accessor.HasClassData()) << klass->PrettyDescriptor(); |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 2923 | bool has_oat_class; |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 2924 | OatFile::OatClass oat_class = OatFile::FindOatClass(dex_file, |
| 2925 | klass->GetDexClassDefIndex(), |
| 2926 | &has_oat_class); |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 2927 | // Link the code of methods skipped by LinkCode. |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 2928 | for (size_t method_index = 0; method_index < accessor.NumDirectMethods(); ++method_index) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2929 | ArtMethod* method = klass->GetDirectMethod(method_index, image_pointer_size_); |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2930 | if (!method->IsStatic()) { |
| 2931 | // Only update static methods. |
| 2932 | continue; |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2933 | } |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2934 | const void* quick_code = nullptr; |
| 2935 | if (has_oat_class) { |
| 2936 | OatFile::OatMethod oat_method = oat_class.GetOatMethod(method_index); |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2937 | quick_code = oat_method.GetQuickCode(); |
| 2938 | } |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 2939 | // Check whether the method is native, in which case it's generic JNI. |
| 2940 | if (quick_code == nullptr && method->IsNative()) { |
| 2941 | quick_code = GetQuickGenericJniStub(); |
| 2942 | } else if (ShouldUseInterpreterEntrypoint(method, quick_code)) { |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2943 | // Use interpreter entry point. |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 2944 | quick_code = GetQuickToInterpreterBridge(); |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2945 | } |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 2946 | runtime->GetInstrumentation()->UpdateMethodsCode(method, quick_code); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2947 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2948 | // Ignore virtual methods on the iterator. |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2949 | } |
| 2950 | |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 2951 | // Does anything needed to make sure that the compiler will not generate a direct invoke to this |
| 2952 | // method. Should only be called on non-invokable methods. |
| 2953 | inline void EnsureThrowsInvocationError(ClassLinker* class_linker, ArtMethod* method) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 2954 | DCHECK(method != nullptr); |
| 2955 | DCHECK(!method->IsInvokable()); |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 2956 | method->SetEntryPointFromQuickCompiledCodePtrSize( |
| 2957 | class_linker->GetQuickToInterpreterBridgeTrampoline(), |
| 2958 | class_linker->GetImagePointerSize()); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 2959 | } |
| 2960 | |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 2961 | static void LinkCode(ClassLinker* class_linker, |
| 2962 | ArtMethod* method, |
| 2963 | const OatFile::OatClass* oat_class, |
| 2964 | uint32_t class_def_method_index) REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | 2d441b1 | 2018-06-08 15:33:21 -0700 | [diff] [blame] | 2965 | ScopedAssertNoThreadSuspension sants(__FUNCTION__); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2966 | Runtime* const runtime = Runtime::Current(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 2967 | if (runtime->IsAotCompiler()) { |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2968 | // The following code only applies to a non-compiler runtime. |
| 2969 | return; |
| 2970 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2971 | // Method shouldn't have already been linked. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 2972 | DCHECK(method->GetEntryPointFromQuickCompiledCode() == nullptr); |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2973 | if (oat_class != nullptr) { |
| 2974 | // Every kind of method should at least get an invoke stub from the oat_method. |
| 2975 | // non-abstract methods also get their code pointers. |
Ian Rogers | 6a3c1fc | 2014-10-31 00:33:20 -0700 | [diff] [blame] | 2976 | const OatFile::OatMethod oat_method = oat_class->GetOatMethod(class_def_method_index); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2977 | oat_method.LinkMethod(method); |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 2978 | } |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 2979 | |
Tamas Berghammer | 3a98aae | 2016-02-08 20:21:54 +0000 | [diff] [blame] | 2980 | // Install entry point from interpreter. |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 2981 | const void* quick_code = method->GetEntryPointFromQuickCompiledCode(); |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 2982 | bool enter_interpreter = class_linker->ShouldUseInterpreterEntrypoint(method, quick_code); |
Jeff Hao | 1674363 | 2013-05-08 10:59:04 -0700 | [diff] [blame] | 2983 | |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 2984 | if (!method->IsInvokable()) { |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 2985 | EnsureThrowsInvocationError(class_linker, method); |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 2986 | return; |
| 2987 | } |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 2988 | |
| 2989 | if (method->IsStatic() && !method->IsConstructor()) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2990 | // For static methods excluding the class initializer, install the trampoline. |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 2991 | // It will be replaced by the proper entry point by ClassLinker::FixupStaticTrampolines |
| 2992 | // after initializing class (see ClassLinker::InitializeClass method). |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 2993 | method->SetEntryPointFromQuickCompiledCode(GetQuickResolutionStub()); |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 2994 | } else if (quick_code == nullptr && method->IsNative()) { |
| 2995 | method->SetEntryPointFromQuickCompiledCode(GetQuickGenericJniStub()); |
Tamas Berghammer | 3a98aae | 2016-02-08 20:21:54 +0000 | [diff] [blame] | 2996 | } else if (enter_interpreter) { |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 2997 | // Set entry point from compiled code if there's no code or in interpreter only mode. |
| 2998 | method->SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge()); |
Ian Rogers | 0d6de04 | 2012-02-29 08:50:26 -0800 | [diff] [blame] | 2999 | } |
jeffhao | 26c0a1a | 2012-01-17 16:28:33 -0800 | [diff] [blame] | 3000 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3001 | if (method->IsNative()) { |
| 3002 | // Unregistering restores the dlsym lookup stub. |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 3003 | method->UnregisterNative(); |
Andreas Gampe | 9054683 | 2014-03-12 18:07:19 -0700 | [diff] [blame] | 3004 | |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 3005 | if (enter_interpreter || quick_code == nullptr) { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 3006 | // We have a native method here without code. Then it should have either the generic JNI |
| 3007 | // trampoline as entrypoint (non-static), or the resolution trampoline (static). |
| 3008 | // TODO: this doesn't handle all the cases where trampolines may be installed. |
| 3009 | const void* entry_point = method->GetEntryPointFromQuickCompiledCode(); |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3010 | DCHECK(class_linker->IsQuickGenericJniStub(entry_point) || |
| 3011 | class_linker->IsQuickResolutionStub(entry_point)); |
Andreas Gampe | 9054683 | 2014-03-12 18:07:19 -0700 | [diff] [blame] | 3012 | } |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 3013 | } |
| 3014 | } |
| 3015 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3016 | void ClassLinker::SetupClass(const DexFile& dex_file, |
| 3017 | const DexFile::ClassDef& dex_class_def, |
| 3018 | Handle<mirror::Class> klass, |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3019 | ObjPtr<mirror::ClassLoader> class_loader) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3020 | CHECK(klass != nullptr); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3021 | CHECK(klass->GetDexCache() != nullptr); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 3022 | CHECK_EQ(ClassStatus::kNotReady, klass->GetStatus()); |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 3023 | const char* descriptor = dex_file.GetClassDescriptor(dex_class_def); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3024 | CHECK(descriptor != nullptr); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3025 | |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 3026 | klass->SetClass(GetClassRoot<mirror::Class>(this)); |
Andreas Gampe | 5182932 | 2014-08-25 15:05:04 -0700 | [diff] [blame] | 3027 | uint32_t access_flags = dex_class_def.GetJavaAccessFlags(); |
Brian Carlstrom | 8e3fb14 | 2013-10-09 21:00:27 -0700 | [diff] [blame] | 3028 | CHECK_EQ(access_flags & ~kAccJavaFlagsMask, 0U); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3029 | klass->SetAccessFlags(access_flags); |
| 3030 | klass->SetClassLoader(class_loader); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3031 | DCHECK_EQ(klass->GetPrimitiveType(), Primitive::kPrimNot); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 3032 | mirror::Class::SetStatus(klass, ClassStatus::kIdx, nullptr); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3033 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 3034 | klass->SetDexClassDefIndex(dex_file.GetIndexForClassDef(dex_class_def)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3035 | klass->SetDexTypeIndex(dex_class_def.class_idx_); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3036 | } |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3037 | |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3038 | LengthPrefixedArray<ArtField>* ClassLinker::AllocArtFieldArray(Thread* self, |
| 3039 | LinearAlloc* allocator, |
| 3040 | size_t length) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3041 | if (length == 0) { |
| 3042 | return nullptr; |
| 3043 | } |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 3044 | // If the ArtField alignment changes, review all uses of LengthPrefixedArray<ArtField>. |
| 3045 | static_assert(alignof(ArtField) == 4, "ArtField alignment is expected to be 4."); |
| 3046 | size_t storage_size = LengthPrefixedArray<ArtField>::ComputeSize(length); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3047 | void* array_storage = allocator->Alloc(self, storage_size); |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 3048 | auto* ret = new(array_storage) LengthPrefixedArray<ArtField>(length); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3049 | CHECK(ret != nullptr); |
| 3050 | std::uninitialized_fill_n(&ret->At(0), length, ArtField()); |
| 3051 | return ret; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3052 | } |
| 3053 | |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3054 | LengthPrefixedArray<ArtMethod>* ClassLinker::AllocArtMethodArray(Thread* self, |
| 3055 | LinearAlloc* allocator, |
| 3056 | size_t length) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3057 | if (length == 0) { |
| 3058 | return nullptr; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3059 | } |
Vladimir Marko | 1463285 | 2015-08-17 12:07:23 +0100 | [diff] [blame] | 3060 | const size_t method_alignment = ArtMethod::Alignment(image_pointer_size_); |
| 3061 | const size_t method_size = ArtMethod::Size(image_pointer_size_); |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 3062 | const size_t storage_size = |
| 3063 | LengthPrefixedArray<ArtMethod>::ComputeSize(length, method_size, method_alignment); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3064 | void* array_storage = allocator->Alloc(self, storage_size); |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 3065 | auto* ret = new (array_storage) LengthPrefixedArray<ArtMethod>(length); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3066 | CHECK(ret != nullptr); |
| 3067 | for (size_t i = 0; i < length; ++i) { |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 3068 | new(reinterpret_cast<void*>(&ret->At(i, method_size, method_alignment))) ArtMethod; |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3069 | } |
| 3070 | return ret; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3071 | } |
| 3072 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3073 | LinearAlloc* ClassLinker::GetAllocatorForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3074 | if (class_loader == nullptr) { |
| 3075 | return Runtime::Current()->GetLinearAlloc(); |
| 3076 | } |
| 3077 | LinearAlloc* allocator = class_loader->GetAllocator(); |
| 3078 | DCHECK(allocator != nullptr); |
| 3079 | return allocator; |
| 3080 | } |
| 3081 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3082 | LinearAlloc* ClassLinker::GetOrCreateAllocatorForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 3083 | if (class_loader == nullptr) { |
| 3084 | return Runtime::Current()->GetLinearAlloc(); |
| 3085 | } |
| 3086 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
| 3087 | LinearAlloc* allocator = class_loader->GetAllocator(); |
| 3088 | if (allocator == nullptr) { |
Mathieu Chartier | 5b83050 | 2016-03-02 10:30:23 -0800 | [diff] [blame] | 3089 | RegisterClassLoader(class_loader); |
| 3090 | allocator = class_loader->GetAllocator(); |
| 3091 | CHECK(allocator != nullptr); |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 3092 | } |
| 3093 | return allocator; |
| 3094 | } |
| 3095 | |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3096 | void ClassLinker::LoadClass(Thread* self, |
| 3097 | const DexFile& dex_file, |
| 3098 | const DexFile::ClassDef& dex_class_def, |
| 3099 | Handle<mirror::Class> klass) { |
| 3100 | ClassAccessor accessor(dex_file, dex_class_def); |
| 3101 | if (!accessor.HasClassData()) { |
| 3102 | return; |
| 3103 | } |
| 3104 | Runtime* const runtime = Runtime::Current(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3105 | { |
| 3106 | // Note: We cannot have thread suspension until the field and method arrays are setup or else |
| 3107 | // Class::VisitFieldRoots may miss some fields or methods. |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 3108 | ScopedAssertNoThreadSuspension nts(__FUNCTION__); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3109 | // Load static fields. |
Vladimir Marko | 23682bf | 2015-06-24 14:28:03 +0100 | [diff] [blame] | 3110 | // We allow duplicate definitions of the same field in a class_data_item |
| 3111 | // but ignore the repeated indexes here, b/21868015. |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3112 | LinearAlloc* const allocator = GetAllocatorForClassLoader(klass->GetClassLoader()); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3113 | LengthPrefixedArray<ArtField>* sfields = AllocArtFieldArray(self, |
| 3114 | allocator, |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3115 | accessor.NumStaticFields()); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3116 | LengthPrefixedArray<ArtField>* ifields = AllocArtFieldArray(self, |
| 3117 | allocator, |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3118 | accessor.NumInstanceFields()); |
| 3119 | size_t num_sfields = 0u; |
Vladimir Marko | 23682bf | 2015-06-24 14:28:03 +0100 | [diff] [blame] | 3120 | size_t num_ifields = 0u; |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3121 | uint32_t last_static_field_idx = 0u; |
| 3122 | uint32_t last_instance_field_idx = 0u; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 3123 | |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3124 | // Methods |
| 3125 | bool has_oat_class = false; |
| 3126 | const OatFile::OatClass oat_class = (runtime->IsStarted() && !runtime->IsAotCompiler()) |
| 3127 | ? OatFile::FindOatClass(dex_file, klass->GetDexClassDefIndex(), &has_oat_class) |
| 3128 | : OatFile::OatClass::Invalid(); |
| 3129 | const OatFile::OatClass* oat_class_ptr = has_oat_class ? &oat_class : nullptr; |
| 3130 | klass->SetMethodsPtr( |
| 3131 | AllocArtMethodArray(self, allocator, accessor.NumMethods()), |
| 3132 | accessor.NumDirectMethods(), |
| 3133 | accessor.NumVirtualMethods()); |
| 3134 | size_t class_def_method_index = 0; |
| 3135 | uint32_t last_dex_method_index = dex::kDexNoIndex; |
| 3136 | size_t last_class_def_method_index = 0; |
| 3137 | |
| 3138 | // Use the visitor since the ranged based loops are bit slower from seeking. Seeking to the |
| 3139 | // methods needs to decode all of the fields. |
| 3140 | accessor.VisitFieldsAndMethods([&]( |
| 3141 | const ClassAccessor::Field& field) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 3142 | uint32_t field_idx = field.GetIndex(); |
| 3143 | DCHECK_GE(field_idx, last_static_field_idx); // Ordering enforced by DexFileVerifier. |
| 3144 | if (num_sfields == 0 || LIKELY(field_idx > last_static_field_idx)) { |
| 3145 | LoadField(field, klass, &sfields->At(num_sfields)); |
| 3146 | ++num_sfields; |
| 3147 | last_static_field_idx = field_idx; |
| 3148 | } |
| 3149 | }, [&](const ClassAccessor::Field& field) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 3150 | uint32_t field_idx = field.GetIndex(); |
| 3151 | DCHECK_GE(field_idx, last_instance_field_idx); // Ordering enforced by DexFileVerifier. |
| 3152 | if (num_ifields == 0 || LIKELY(field_idx > last_instance_field_idx)) { |
| 3153 | LoadField(field, klass, &ifields->At(num_ifields)); |
| 3154 | ++num_ifields; |
| 3155 | last_instance_field_idx = field_idx; |
| 3156 | } |
| 3157 | }, [&](const ClassAccessor::Method& method) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 3158 | ArtMethod* art_method = klass->GetDirectMethodUnchecked(class_def_method_index, |
| 3159 | image_pointer_size_); |
| 3160 | LoadMethod(dex_file, method, klass, art_method); |
| 3161 | LinkCode(this, art_method, oat_class_ptr, class_def_method_index); |
| 3162 | uint32_t it_method_index = method.GetIndex(); |
| 3163 | if (last_dex_method_index == it_method_index) { |
| 3164 | // duplicate case |
| 3165 | art_method->SetMethodIndex(last_class_def_method_index); |
| 3166 | } else { |
| 3167 | art_method->SetMethodIndex(class_def_method_index); |
| 3168 | last_dex_method_index = it_method_index; |
| 3169 | last_class_def_method_index = class_def_method_index; |
| 3170 | } |
| 3171 | ++class_def_method_index; |
| 3172 | }, [&](const ClassAccessor::Method& method) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 3173 | ArtMethod* art_method = klass->GetVirtualMethodUnchecked( |
| 3174 | class_def_method_index - accessor.NumDirectMethods(), |
| 3175 | image_pointer_size_); |
| 3176 | LoadMethod(dex_file, method, klass, art_method); |
| 3177 | LinkCode(this, art_method, oat_class_ptr, class_def_method_index); |
| 3178 | ++class_def_method_index; |
| 3179 | }); |
| 3180 | |
| 3181 | if (UNLIKELY(num_ifields + num_sfields != accessor.NumFields())) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3182 | LOG(WARNING) << "Duplicate fields in class " << klass->PrettyDescriptor() |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3183 | << " (unique static fields: " << num_sfields << "/" << accessor.NumStaticFields() |
| 3184 | << ", unique instance fields: " << num_ifields << "/" << accessor.NumInstanceFields() |
| 3185 | << ")"; |
Vladimir Marko | 81819db | 2015-11-05 15:30:12 +0000 | [diff] [blame] | 3186 | // NOTE: Not shrinking the over-allocated sfields/ifields, just setting size. |
| 3187 | if (sfields != nullptr) { |
| 3188 | sfields->SetSize(num_sfields); |
| 3189 | } |
| 3190 | if (ifields != nullptr) { |
| 3191 | ifields->SetSize(num_ifields); |
| 3192 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3193 | } |
Vladimir Marko | 81819db | 2015-11-05 15:30:12 +0000 | [diff] [blame] | 3194 | // Set the field arrays. |
| 3195 | klass->SetSFieldsPtr(sfields); |
| 3196 | DCHECK_EQ(klass->NumStaticFields(), num_sfields); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3197 | klass->SetIFieldsPtr(ifields); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3198 | DCHECK_EQ(klass->NumInstanceFields(), num_ifields); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3199 | } |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 3200 | // Ensure that the card is marked so that remembered sets pick up native roots. |
Mathieu Chartier | 88ea61e | 2018-06-20 17:45:41 -0700 | [diff] [blame] | 3201 | WriteBarrier::ForEveryFieldWrite(klass.Get()); |
Mathieu Chartier | f3f2a7a | 2015-04-14 15:43:10 -0700 | [diff] [blame] | 3202 | self->AllowThreadSuspension(); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3203 | } |
| 3204 | |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3205 | void ClassLinker::LoadField(const ClassAccessor::Field& field, |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3206 | Handle<mirror::Class> klass, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3207 | ArtField* dst) { |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3208 | const uint32_t field_idx = field.GetIndex(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3209 | dst->SetDexFieldIndex(field_idx); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3210 | dst->SetDeclaringClass(klass.Get()); |
David Brazdil | f6a8a55 | 2018-01-15 18:10:50 +0000 | [diff] [blame] | 3211 | |
| 3212 | // Get access flags from the DexFile. If this is a boot class path class, |
| 3213 | // also set its runtime hidden API access flags. |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3214 | uint32_t access_flags = field.GetAccessFlags(); |
David Brazdil | f6a8a55 | 2018-01-15 18:10:50 +0000 | [diff] [blame] | 3215 | if (klass->IsBootStrapClassLoaded()) { |
| 3216 | access_flags = |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3217 | HiddenApiAccessFlags::EncodeForRuntime(access_flags, field.DecodeHiddenAccessFlags()); |
David Brazdil | f6a8a55 | 2018-01-15 18:10:50 +0000 | [diff] [blame] | 3218 | } |
| 3219 | dst->SetAccessFlags(access_flags); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3220 | } |
| 3221 | |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 3222 | void ClassLinker::LoadMethod(const DexFile& dex_file, |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3223 | const ClassAccessor::Method& method, |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3224 | Handle<mirror::Class> klass, |
| 3225 | ArtMethod* dst) { |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3226 | const uint32_t dex_method_idx = method.GetIndex(); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3227 | const DexFile::MethodId& method_id = dex_file.GetMethodId(dex_method_idx); |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 3228 | const char* method_name = dex_file.StringDataByIdx(method_id.name_idx_); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 3229 | |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 3230 | ScopedAssertNoThreadSuspension ants("LoadMethod"); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 3231 | dst->SetDexMethodIndex(dex_method_idx); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3232 | dst->SetDeclaringClass(klass.Get()); |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3233 | dst->SetCodeItemOffset(method.GetCodeItemOffset()); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3234 | |
David Brazdil | f6a8a55 | 2018-01-15 18:10:50 +0000 | [diff] [blame] | 3235 | // Get access flags from the DexFile. If this is a boot class path class, |
| 3236 | // also set its runtime hidden API access flags. |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3237 | uint32_t access_flags = method.GetAccessFlags(); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3238 | |
David Brazdil | f6a8a55 | 2018-01-15 18:10:50 +0000 | [diff] [blame] | 3239 | if (klass->IsBootStrapClassLoaded()) { |
| 3240 | access_flags = |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3241 | HiddenApiAccessFlags::EncodeForRuntime(access_flags, method.DecodeHiddenAccessFlags()); |
David Brazdil | f6a8a55 | 2018-01-15 18:10:50 +0000 | [diff] [blame] | 3242 | } |
| 3243 | |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 3244 | if (UNLIKELY(strcmp("finalize", method_name) == 0)) { |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3245 | // Set finalizable flag on declaring class. |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 3246 | if (strcmp("V", dex_file.GetShorty(method_id.proto_idx_)) == 0) { |
| 3247 | // Void return type. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3248 | if (klass->GetClassLoader() != nullptr) { // All non-boot finalizer methods are flagged. |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 3249 | klass->SetFinalizable(); |
| 3250 | } else { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3251 | std::string temp; |
| 3252 | const char* klass_descriptor = klass->GetDescriptor(&temp); |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 3253 | // The Enum class declares a "final" finalize() method to prevent subclasses from |
| 3254 | // introducing a finalizer. We don't want to set the finalizable flag for Enum or its |
| 3255 | // subclasses, so we exclude it here. |
| 3256 | // We also want to avoid setting the flag on Object, where we know that finalize() is |
| 3257 | // empty. |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3258 | if (strcmp(klass_descriptor, "Ljava/lang/Object;") != 0 && |
| 3259 | strcmp(klass_descriptor, "Ljava/lang/Enum;") != 0) { |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3260 | klass->SetFinalizable(); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3261 | } |
| 3262 | } |
| 3263 | } |
| 3264 | } else if (method_name[0] == '<') { |
| 3265 | // Fix broken access flags for initializers. Bug 11157540. |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 3266 | bool is_init = (strcmp("<init>", method_name) == 0); |
| 3267 | bool is_clinit = !is_init && (strcmp("<clinit>", method_name) == 0); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3268 | if (UNLIKELY(!is_init && !is_clinit)) { |
| 3269 | LOG(WARNING) << "Unexpected '<' at start of method name " << method_name; |
| 3270 | } else { |
| 3271 | if (UNLIKELY((access_flags & kAccConstructor) == 0)) { |
| 3272 | 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] | 3273 | << klass->PrettyDescriptor() << " in dex file " << dex_file.GetLocation(); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3274 | access_flags |= kAccConstructor; |
| 3275 | } |
| 3276 | } |
| 3277 | } |
Vladimir Marko | b0a6aee | 2017-10-27 10:34:04 +0100 | [diff] [blame] | 3278 | if (UNLIKELY((access_flags & kAccNative) != 0u)) { |
| 3279 | // Check if the native method is annotated with @FastNative or @CriticalNative. |
| 3280 | access_flags |= annotations::GetNativeMethodAnnotationAccessFlags( |
| 3281 | dex_file, dst->GetClassDef(), dex_method_idx); |
| 3282 | } |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3283 | dst->SetAccessFlags(access_flags); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3284 | } |
| 3285 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3286 | void ClassLinker::AppendToBootClassPath(Thread* self, const DexFile& dex_file) { |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3287 | ObjPtr<mirror::DexCache> dex_cache = AllocAndInitializeDexCache( |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 3288 | self, |
| 3289 | dex_file, |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3290 | Runtime::Current()->GetLinearAlloc()); |
| 3291 | 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] | 3292 | AppendToBootClassPath(dex_file, dex_cache); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 3293 | } |
| 3294 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 3295 | void ClassLinker::AppendToBootClassPath(const DexFile& dex_file, |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3296 | ObjPtr<mirror::DexCache> dex_cache) { |
| 3297 | CHECK(dex_cache != nullptr) << dex_file.GetLocation(); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 3298 | boot_class_path_.push_back(&dex_file); |
Andreas Gampe | be7af22 | 2017-07-25 09:57:28 -0700 | [diff] [blame] | 3299 | WriterMutexLock mu(Thread::Current(), *Locks::dex_lock_); |
| 3300 | RegisterDexFileLocked(dex_file, dex_cache, /* class_loader */ nullptr); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 3301 | } |
| 3302 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 3303 | void ClassLinker::RegisterDexFileLocked(const DexFile& dex_file, |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3304 | ObjPtr<mirror::DexCache> dex_cache, |
| 3305 | ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3306 | Thread* const self = Thread::Current(); |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 3307 | Locks::dex_lock_->AssertExclusiveHeld(self); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3308 | CHECK(dex_cache != nullptr) << dex_file.GetLocation(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 3309 | // For app images, the dex cache location may be a suffix of the dex file location since the |
| 3310 | // dex file location is an absolute path. |
Mathieu Chartier | 7617216 | 2016-01-26 14:54:06 -0800 | [diff] [blame] | 3311 | const std::string dex_cache_location = dex_cache->GetLocation()->ToModifiedUtf8(); |
| 3312 | const size_t dex_cache_length = dex_cache_location.length(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 3313 | CHECK_GT(dex_cache_length, 0u) << dex_file.GetLocation(); |
| 3314 | std::string dex_file_location = dex_file.GetLocation(); |
| 3315 | CHECK_GE(dex_file_location.length(), dex_cache_length) |
Mathieu Chartier | 7617216 | 2016-01-26 14:54:06 -0800 | [diff] [blame] | 3316 | << dex_cache_location << " " << dex_file.GetLocation(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 3317 | // Take suffix. |
| 3318 | const std::string dex_file_suffix = dex_file_location.substr( |
| 3319 | dex_file_location.length() - dex_cache_length, |
| 3320 | dex_cache_length); |
| 3321 | // Example dex_cache location is SettingsProvider.apk and |
| 3322 | // dex file location is /system/priv-app/SettingsProvider/SettingsProvider.apk |
Mathieu Chartier | 7617216 | 2016-01-26 14:54:06 -0800 | [diff] [blame] | 3323 | CHECK_EQ(dex_cache_location, dex_file_suffix); |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 3324 | const OatFile* oat_file = |
| 3325 | (dex_file.GetOatDexFile() != nullptr) ? dex_file.GetOatDexFile()->GetOatFile() : nullptr; |
Vladimir Marko | b066d43 | 2018-01-03 13:14:37 +0000 | [diff] [blame] | 3326 | // Clean up pass to remove null dex caches; null dex caches can occur due to class unloading |
| 3327 | // and we are lazily removing null entries. Also check if we need to initialize OatFile data |
| 3328 | // (.data.bimg.rel.ro and .bss sections) needed for code execution. |
| 3329 | bool initialize_oat_file_data = (oat_file != nullptr) && oat_file->IsExecutable(); |
Ian Rogers | 55256cb | 2017-12-21 17:07:11 -0800 | [diff] [blame] | 3330 | JavaVMExt* const vm = self->GetJniEnv()->GetVm(); |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 3331 | for (auto it = dex_caches_.begin(); it != dex_caches_.end(); ) { |
| 3332 | DexCacheData data = *it; |
| 3333 | if (self->IsJWeakCleared(data.weak_root)) { |
| 3334 | vm->DeleteWeakGlobalRef(self, data.weak_root); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3335 | it = dex_caches_.erase(it); |
| 3336 | } else { |
Vladimir Marko | b066d43 | 2018-01-03 13:14:37 +0000 | [diff] [blame] | 3337 | if (initialize_oat_file_data && |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 3338 | it->dex_file->GetOatDexFile() != nullptr && |
| 3339 | it->dex_file->GetOatDexFile()->GetOatFile() == oat_file) { |
Vladimir Marko | b066d43 | 2018-01-03 13:14:37 +0000 | [diff] [blame] | 3340 | initialize_oat_file_data = false; // Already initialized. |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 3341 | } |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3342 | ++it; |
| 3343 | } |
Brian Carlstrom | 81a9087 | 2015-08-28 09:07:14 -0700 | [diff] [blame] | 3344 | } |
Vladimir Marko | b066d43 | 2018-01-03 13:14:37 +0000 | [diff] [blame] | 3345 | if (initialize_oat_file_data) { |
| 3346 | // Initialize the .data.bimg.rel.ro section. |
| 3347 | if (!oat_file->GetBootImageRelocations().empty()) { |
| 3348 | uint8_t* reloc_begin = const_cast<uint8_t*>(oat_file->DataBimgRelRoBegin()); |
| 3349 | CheckedCall(mprotect, |
| 3350 | "un-protect boot image relocations", |
| 3351 | reloc_begin, |
| 3352 | oat_file->DataBimgRelRoSize(), |
| 3353 | PROT_READ | PROT_WRITE); |
| 3354 | uint32_t boot_image_begin = dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>( |
| 3355 | Runtime::Current()->GetHeap()->GetBootImageSpaces().front()->Begin())); |
| 3356 | for (const uint32_t& relocation : oat_file->GetBootImageRelocations()) { |
| 3357 | const_cast<uint32_t&>(relocation) += boot_image_begin; |
| 3358 | } |
| 3359 | CheckedCall(mprotect, |
| 3360 | "protect boot image relocations", |
| 3361 | reloc_begin, |
| 3362 | oat_file->DataBimgRelRoSize(), |
| 3363 | PROT_READ); |
| 3364 | } |
| 3365 | |
| 3366 | // Initialize the .bss section. |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 3367 | // TODO: Pre-initialize from boot/app image? |
| 3368 | ArtMethod* resolution_method = Runtime::Current()->GetResolutionMethod(); |
| 3369 | for (ArtMethod*& entry : oat_file->GetBssMethods()) { |
| 3370 | entry = resolution_method; |
| 3371 | } |
| 3372 | } |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3373 | jweak dex_cache_jweak = vm->AddWeakGlobalRef(self, dex_cache); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3374 | dex_cache->SetDexFile(&dex_file); |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 3375 | DexCacheData data; |
| 3376 | data.weak_root = dex_cache_jweak; |
| 3377 | data.dex_file = dex_cache->GetDexFile(); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3378 | data.class_table = ClassTableForClassLoader(class_loader); |
David Srbecky | 440a9b3 | 2018-02-15 17:47:29 +0000 | [diff] [blame] | 3379 | AddNativeDebugInfoForDex(self, ArrayRef<const uint8_t>(data.dex_file->Begin(), |
| 3380 | data.dex_file->Size())); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3381 | DCHECK(data.class_table != nullptr); |
Mathieu Chartier | 72041a0 | 2017-07-14 18:23:25 -0700 | [diff] [blame] | 3382 | // Make sure to hold the dex cache live in the class table. This case happens for the boot class |
| 3383 | // path dex caches without an image. |
| 3384 | data.class_table->InsertStrongRoot(dex_cache); |
| 3385 | if (class_loader != nullptr) { |
| 3386 | // Since we added a strong root to the class table, do the write barrier as required for |
| 3387 | // remembered sets and generational GCs. |
Mathieu Chartier | 88ea61e | 2018-06-20 17:45:41 -0700 | [diff] [blame] | 3388 | WriteBarrier::ForEveryFieldWrite(class_loader); |
Mathieu Chartier | 72041a0 | 2017-07-14 18:23:25 -0700 | [diff] [blame] | 3389 | } |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 3390 | dex_caches_.push_back(data); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 3391 | } |
| 3392 | |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3393 | ObjPtr<mirror::DexCache> ClassLinker::DecodeDexCache(Thread* self, const DexCacheData& data) { |
| 3394 | return data.IsValid() |
| 3395 | ? ObjPtr<mirror::DexCache>::DownCast(self->DecodeJObject(data.weak_root)) |
| 3396 | : nullptr; |
| 3397 | } |
| 3398 | |
| 3399 | ObjPtr<mirror::DexCache> ClassLinker::EnsureSameClassLoader( |
| 3400 | Thread* self, |
| 3401 | ObjPtr<mirror::DexCache> dex_cache, |
| 3402 | const DexCacheData& data, |
| 3403 | ObjPtr<mirror::ClassLoader> class_loader) { |
| 3404 | DCHECK_EQ(dex_cache->GetDexFile(), data.dex_file); |
| 3405 | if (data.class_table != ClassTableForClassLoader(class_loader)) { |
| 3406 | self->ThrowNewExceptionF("Ljava/lang/InternalError;", |
| 3407 | "Attempt to register dex file %s with multiple class loaders", |
| 3408 | data.dex_file->GetLocation().c_str()); |
| 3409 | return nullptr; |
| 3410 | } |
| 3411 | return dex_cache; |
| 3412 | } |
| 3413 | |
Alex Light | 07f0621 | 2017-06-01 14:01:43 -0700 | [diff] [blame] | 3414 | void ClassLinker::RegisterExistingDexCache(ObjPtr<mirror::DexCache> dex_cache, |
| 3415 | ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | ed4ee44 | 2018-06-05 14:23:35 -0700 | [diff] [blame] | 3416 | SCOPED_TRACE << __FUNCTION__ << " " << dex_cache->GetDexFile()->GetLocation(); |
Alex Light | 07f0621 | 2017-06-01 14:01:43 -0700 | [diff] [blame] | 3417 | Thread* self = Thread::Current(); |
| 3418 | StackHandleScope<2> hs(self); |
| 3419 | Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(dex_cache)); |
| 3420 | Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader)); |
| 3421 | const DexFile* dex_file = dex_cache->GetDexFile(); |
| 3422 | DCHECK(dex_file != nullptr) << "Attempt to register uninitialized dex_cache object!"; |
| 3423 | if (kIsDebugBuild) { |
| 3424 | DexCacheData old_data; |
| 3425 | { |
| 3426 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
| 3427 | old_data = FindDexCacheDataLocked(*dex_file); |
| 3428 | } |
| 3429 | ObjPtr<mirror::DexCache> old_dex_cache = DecodeDexCache(self, old_data); |
| 3430 | DCHECK(old_dex_cache.IsNull()) << "Attempt to manually register a dex cache thats already " |
| 3431 | << "been registered on dex file " << dex_file->GetLocation(); |
| 3432 | } |
| 3433 | ClassTable* table; |
| 3434 | { |
| 3435 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 3436 | table = InsertClassTableForClassLoader(h_class_loader.Get()); |
| 3437 | } |
Ivan Maidanski | 2b69b9c | 2018-05-14 13:50:48 +0300 | [diff] [blame] | 3438 | // Avoid a deadlock between a garbage collecting thread running a checkpoint, |
| 3439 | // a thread holding the dex lock and blocking on a condition variable regarding |
| 3440 | // weak references access, and a thread blocking on the dex lock. |
Ivan Maidanski | 2b69b9c | 2018-05-14 13:50:48 +0300 | [diff] [blame] | 3441 | gc::ScopedGCCriticalSection gcs(self, gc::kGcCauseClassLinker, gc::kCollectorTypeClassLinker); |
Alex Light | 07f0621 | 2017-06-01 14:01:43 -0700 | [diff] [blame] | 3442 | WriterMutexLock mu(self, *Locks::dex_lock_); |
| 3443 | RegisterDexFileLocked(*dex_file, h_dex_cache.Get(), h_class_loader.Get()); |
| 3444 | table->InsertStrongRoot(h_dex_cache.Get()); |
| 3445 | if (h_class_loader.Get() != nullptr) { |
| 3446 | // Since we added a strong root to the class table, do the write barrier as required for |
| 3447 | // remembered sets and generational GCs. |
Mathieu Chartier | 88ea61e | 2018-06-20 17:45:41 -0700 | [diff] [blame] | 3448 | WriteBarrier::ForEveryFieldWrite(h_class_loader.Get()); |
Alex Light | 07f0621 | 2017-06-01 14:01:43 -0700 | [diff] [blame] | 3449 | } |
| 3450 | } |
| 3451 | |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3452 | ObjPtr<mirror::DexCache> ClassLinker::RegisterDexFile(const DexFile& dex_file, |
| 3453 | ObjPtr<mirror::ClassLoader> class_loader) { |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 3454 | Thread* self = Thread::Current(); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3455 | DexCacheData old_data; |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 3456 | { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 3457 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3458 | old_data = FindDexCacheDataLocked(dex_file); |
| 3459 | } |
| 3460 | ObjPtr<mirror::DexCache> old_dex_cache = DecodeDexCache(self, old_data); |
| 3461 | if (old_dex_cache != nullptr) { |
| 3462 | return EnsureSameClassLoader(self, old_dex_cache, old_data, class_loader); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3463 | } |
Mathieu Chartier | ed4ee44 | 2018-06-05 14:23:35 -0700 | [diff] [blame] | 3464 | SCOPED_TRACE << __FUNCTION__ << " " << dex_file.GetLocation(); |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 3465 | LinearAlloc* const linear_alloc = GetOrCreateAllocatorForClassLoader(class_loader); |
| 3466 | DCHECK(linear_alloc != nullptr); |
| 3467 | ClassTable* table; |
| 3468 | { |
| 3469 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 3470 | table = InsertClassTableForClassLoader(class_loader); |
| 3471 | } |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 3472 | // Don't alloc while holding the lock, since allocation may need to |
| 3473 | // suspend all threads and another thread may need the dex_lock_ to |
| 3474 | // get to a suspend point. |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3475 | StackHandleScope<3> hs(self); |
| 3476 | Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader)); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3477 | ObjPtr<mirror::String> location; |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 3478 | Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(AllocDexCache(/*out*/&location, |
| 3479 | self, |
| 3480 | dex_file))); |
| 3481 | Handle<mirror::String> h_location(hs.NewHandle(location)); |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 3482 | { |
Ivan Maidanski | 2b69b9c | 2018-05-14 13:50:48 +0300 | [diff] [blame] | 3483 | // Avoid a deadlock between a garbage collecting thread running a checkpoint, |
| 3484 | // a thread holding the dex lock and blocking on a condition variable regarding |
| 3485 | // weak references access, and a thread blocking on the dex lock. |
Ivan Maidanski | 2b69b9c | 2018-05-14 13:50:48 +0300 | [diff] [blame] | 3486 | gc::ScopedGCCriticalSection gcs(self, gc::kGcCauseClassLinker, gc::kCollectorTypeClassLinker); |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 3487 | WriterMutexLock mu(self, *Locks::dex_lock_); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3488 | old_data = FindDexCacheDataLocked(dex_file); |
| 3489 | old_dex_cache = DecodeDexCache(self, old_data); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3490 | if (old_dex_cache == nullptr && h_dex_cache != nullptr) { |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3491 | // Do InitializeDexCache while holding dex lock to make sure two threads don't call it at the |
| 3492 | // same time with the same dex cache. Since the .bss is shared this can cause failing DCHECK |
| 3493 | // that the arrays are null. |
| 3494 | mirror::DexCache::InitializeDexCache(self, |
| 3495 | h_dex_cache.Get(), |
| 3496 | h_location.Get(), |
| 3497 | &dex_file, |
| 3498 | linear_alloc, |
| 3499 | image_pointer_size_); |
| 3500 | RegisterDexFileLocked(dex_file, h_dex_cache.Get(), h_class_loader.Get()); |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 3501 | } |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3502 | } |
| 3503 | if (old_dex_cache != nullptr) { |
| 3504 | // Another thread managed to initialize the dex cache faster, so use that DexCache. |
| 3505 | // If this thread encountered OOME, ignore it. |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3506 | DCHECK_EQ(h_dex_cache == nullptr, self->IsExceptionPending()); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3507 | self->ClearException(); |
| 3508 | // We cannot call EnsureSameClassLoader() while holding the dex_lock_. |
| 3509 | return EnsureSameClassLoader(self, old_dex_cache, old_data, h_class_loader.Get()); |
| 3510 | } |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3511 | if (h_dex_cache == nullptr) { |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3512 | self->AssertPendingOOMException(); |
| 3513 | return nullptr; |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 3514 | } |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 3515 | table->InsertStrongRoot(h_dex_cache.Get()); |
Mathieu Chartier | a1467d0 | 2017-02-22 09:22:50 -0800 | [diff] [blame] | 3516 | if (h_class_loader.Get() != nullptr) { |
| 3517 | // Since we added a strong root to the class table, do the write barrier as required for |
| 3518 | // remembered sets and generational GCs. |
Mathieu Chartier | 88ea61e | 2018-06-20 17:45:41 -0700 | [diff] [blame] | 3519 | WriteBarrier::ForEveryFieldWrite(h_class_loader.Get()); |
Mathieu Chartier | a1467d0 | 2017-02-22 09:22:50 -0800 | [diff] [blame] | 3520 | } |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3521 | return h_dex_cache.Get(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3522 | } |
| 3523 | |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3524 | bool ClassLinker::IsDexFileRegistered(Thread* self, const DexFile& dex_file) { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 3525 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3526 | return DecodeDexCache(self, FindDexCacheDataLocked(dex_file)) != nullptr; |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3527 | } |
| 3528 | |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3529 | ObjPtr<mirror::DexCache> ClassLinker::FindDexCache(Thread* self, const DexFile& dex_file) { |
| 3530 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
Mathieu Chartier | 9e050df | 2017-08-09 10:05:47 -0700 | [diff] [blame] | 3531 | DexCacheData dex_cache_data = FindDexCacheDataLocked(dex_file); |
| 3532 | ObjPtr<mirror::DexCache> dex_cache = DecodeDexCache(self, dex_cache_data); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3533 | if (dex_cache != nullptr) { |
| 3534 | return dex_cache; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 3535 | } |
Brian Carlstrom | 81a9087 | 2015-08-28 09:07:14 -0700 | [diff] [blame] | 3536 | // Failure, dump diagnostic and abort. |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 3537 | for (const DexCacheData& data : dex_caches_) { |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3538 | if (DecodeDexCache(self, data) != nullptr) { |
Andreas Gampe | 37c5846 | 2017-03-27 15:14:27 -0700 | [diff] [blame] | 3539 | LOG(FATAL_WITHOUT_ABORT) << "Registered dex file " << data.dex_file->GetLocation(); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3540 | } |
Brian Carlstrom | 81a9087 | 2015-08-28 09:07:14 -0700 | [diff] [blame] | 3541 | } |
Mathieu Chartier | 9e050df | 2017-08-09 10:05:47 -0700 | [diff] [blame] | 3542 | LOG(FATAL) << "Failed to find DexCache for DexFile " << dex_file.GetLocation() |
| 3543 | << " " << &dex_file << " " << dex_cache_data.dex_file; |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 3544 | UNREACHABLE(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3545 | } |
| 3546 | |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3547 | ClassTable* ClassLinker::FindClassTable(Thread* self, ObjPtr<mirror::DexCache> dex_cache) { |
| 3548 | const DexFile* dex_file = dex_cache->GetDexFile(); |
| 3549 | DCHECK(dex_file != nullptr); |
| 3550 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
| 3551 | // Search assuming unique-ness of dex file. |
| 3552 | for (const DexCacheData& data : dex_caches_) { |
| 3553 | // Avoid decoding (and read barriers) other unrelated dex caches. |
| 3554 | if (data.dex_file == dex_file) { |
| 3555 | ObjPtr<mirror::DexCache> registered_dex_cache = DecodeDexCache(self, data); |
| 3556 | if (registered_dex_cache != nullptr) { |
| 3557 | CHECK_EQ(registered_dex_cache, dex_cache) << dex_file->GetLocation(); |
| 3558 | return data.class_table; |
| 3559 | } |
| 3560 | } |
| 3561 | } |
| 3562 | return nullptr; |
| 3563 | } |
| 3564 | |
| 3565 | ClassLinker::DexCacheData ClassLinker::FindDexCacheDataLocked(const DexFile& dex_file) { |
| 3566 | // Search assuming unique-ness of dex file. |
| 3567 | for (const DexCacheData& data : dex_caches_) { |
| 3568 | // Avoid decoding (and read barriers) other unrelated dex caches. |
| 3569 | if (data.dex_file == &dex_file) { |
| 3570 | return data; |
| 3571 | } |
| 3572 | } |
| 3573 | return DexCacheData(); |
| 3574 | } |
| 3575 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 3576 | ObjPtr<mirror::Class> ClassLinker::CreatePrimitiveClass(Thread* self, Primitive::Type type) { |
Vladimir Marko | acb906d | 2018-05-30 10:23:49 +0100 | [diff] [blame] | 3577 | ObjPtr<mirror::Class> primitive_class = |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 3578 | AllocClass(self, mirror::Class::PrimitiveClassSize(image_pointer_size_)); |
Vladimir Marko | acb906d | 2018-05-30 10:23:49 +0100 | [diff] [blame] | 3579 | if (UNLIKELY(primitive_class == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3580 | self->AssertPendingOOMException(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3581 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3582 | } |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 3583 | // Must hold lock on object when initializing. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3584 | StackHandleScope<1> hs(self); |
| 3585 | Handle<mirror::Class> h_class(hs.NewHandle(primitive_class)); |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 3586 | ObjectLock<mirror::Class> lock(self, h_class); |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3587 | h_class->SetAccessFlags(kAccPublic | kAccFinal | kAccAbstract); |
| 3588 | h_class->SetPrimitiveType(type); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 3589 | h_class->SetIfTable(GetClassRoot<mirror::Object>(this)->GetIfTable()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 3590 | mirror::Class::SetStatus(h_class, ClassStatus::kInitialized, self); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3591 | const char* descriptor = Primitive::Descriptor(type); |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 3592 | ObjPtr<mirror::Class> existing = InsertClass(descriptor, |
| 3593 | h_class.Get(), |
| 3594 | ComputeModifiedUtf8Hash(descriptor)); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3595 | CHECK(existing == nullptr) << "InitPrimitiveClass(" << type << ") failed"; |
Hiroshi Yamauchi | 5b783e6 | 2015-03-18 17:20:11 -0700 | [diff] [blame] | 3596 | return h_class.Get(); |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 3597 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3598 | |
Vladimir Marko | 0261055 | 2018-06-04 14:38:00 +0100 | [diff] [blame] | 3599 | inline ObjPtr<mirror::IfTable> ClassLinker::GetArrayIfTable() { |
| 3600 | return GetClassRoot<mirror::ObjectArray<mirror::Object>>(this)->GetIfTable(); |
| 3601 | } |
| 3602 | |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 3603 | // Create an array class (i.e. the class object for the array, not the |
| 3604 | // array itself). "descriptor" looks like "[C" or "[[[[B" or |
| 3605 | // "[Ljava/lang/String;". |
| 3606 | // |
| 3607 | // If "descriptor" refers to an array of primitives, look up the |
| 3608 | // primitive type's internally-generated class object. |
| 3609 | // |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 3610 | // "class_loader" is the class loader of the class that's referring to |
| 3611 | // us. It's used to ensure that we're looking for the element type in |
| 3612 | // the right context. It does NOT become the class loader for the |
| 3613 | // array class; that always comes from the base element class. |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 3614 | // |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 3615 | // Returns null with an exception raised on failure. |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 3616 | ObjPtr<mirror::Class> ClassLinker::CreateArrayClass(Thread* self, |
| 3617 | const char* descriptor, |
| 3618 | size_t hash, |
| 3619 | Handle<mirror::ClassLoader> class_loader) { |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 3620 | // Identify the underlying component type |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3621 | CHECK_EQ('[', descriptor[0]); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3622 | StackHandleScope<2> hs(self); |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 3623 | MutableHandle<mirror::Class> component_type(hs.NewHandle(FindClass(self, descriptor + 1, |
| 3624 | class_loader))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3625 | if (component_type == nullptr) { |
Mathieu Chartier | c0a9ea4 | 2014-02-03 16:36:49 -0800 | [diff] [blame] | 3626 | DCHECK(self->IsExceptionPending()); |
Andreas Gampe | dc13d7d | 2014-07-23 20:18:36 -0700 | [diff] [blame] | 3627 | // We need to accept erroneous classes as component types. |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 3628 | const size_t component_hash = ComputeModifiedUtf8Hash(descriptor + 1); |
| 3629 | component_type.Assign(LookupClass(self, descriptor + 1, component_hash, class_loader.Get())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3630 | if (component_type == nullptr) { |
Andreas Gampe | dc13d7d | 2014-07-23 20:18:36 -0700 | [diff] [blame] | 3631 | DCHECK(self->IsExceptionPending()); |
| 3632 | return nullptr; |
| 3633 | } else { |
| 3634 | self->ClearException(); |
| 3635 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3636 | } |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 3637 | if (UNLIKELY(component_type->IsPrimitiveVoid())) { |
| 3638 | ThrowNoClassDefFoundError("Attempt to create array of void primitive type"); |
| 3639 | return nullptr; |
| 3640 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3641 | // See if the component type is already loaded. Array classes are |
| 3642 | // always associated with the class loader of their underlying |
| 3643 | // element type -- an array of Strings goes with the loader for |
| 3644 | // java/lang/String -- so we need to look for it there. (The |
| 3645 | // caller should have checked for the existence of the class |
| 3646 | // before calling here, but they did so with *their* class loader, |
| 3647 | // not the component type's loader.) |
| 3648 | // |
| 3649 | // If we find it, the caller adds "loader" to the class' initiating |
| 3650 | // loader list, which should prevent us from going through this again. |
| 3651 | // |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3652 | // This call is unnecessary if "loader" and "component_type->GetClassLoader()" |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3653 | // are the same, because our caller (FindClass) just did the |
| 3654 | // lookup. (Even if we get this wrong we still have correct behavior, |
| 3655 | // because we effectively do this lookup again when we add the new |
| 3656 | // class to the hash table --- necessary because of possible races with |
| 3657 | // other threads.) |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3658 | if (class_loader.Get() != component_type->GetClassLoader()) { |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 3659 | ObjPtr<mirror::Class> new_class = |
| 3660 | LookupClass(self, descriptor, hash, component_type->GetClassLoader()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3661 | if (new_class != nullptr) { |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 3662 | return new_class; |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 3663 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3664 | } |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 3665 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3666 | // Fill out the fields in the Class. |
| 3667 | // |
| 3668 | // It is possible to execute some methods against arrays, because |
| 3669 | // all arrays are subclasses of java_lang_Object_, so we need to set |
| 3670 | // up a vtable. We can just point at the one in java_lang_Object_. |
| 3671 | // |
| 3672 | // Array classes are simple enough that we don't need to do a full |
| 3673 | // link step. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3674 | auto new_class = hs.NewHandle<mirror::Class>(nullptr); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3675 | if (UNLIKELY(!init_done_)) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3676 | // Classes that were hand created, ie not by FindSystemClass |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3677 | if (strcmp(descriptor, "[Ljava/lang/Class;") == 0) { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 3678 | new_class.Assign(GetClassRoot<mirror::ObjectArray<mirror::Class>>(this)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3679 | } else if (strcmp(descriptor, "[Ljava/lang/Object;") == 0) { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 3680 | new_class.Assign(GetClassRoot<mirror::ObjectArray<mirror::Object>>(this)); |
| 3681 | } else if (strcmp(descriptor, "[Ljava/lang/String;") == 0) { |
| 3682 | new_class.Assign(GetClassRoot<mirror::ObjectArray<mirror::String>>(this)); |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 3683 | } else if (strcmp(descriptor, "[Z") == 0) { |
| 3684 | new_class.Assign(GetClassRoot<mirror::BooleanArray>(this)); |
| 3685 | } else if (strcmp(descriptor, "[B") == 0) { |
| 3686 | new_class.Assign(GetClassRoot<mirror::ByteArray>(this)); |
| 3687 | } else if (strcmp(descriptor, "[C") == 0) { |
| 3688 | new_class.Assign(GetClassRoot<mirror::CharArray>(this)); |
| 3689 | } else if (strcmp(descriptor, "[S") == 0) { |
| 3690 | new_class.Assign(GetClassRoot<mirror::ShortArray>(this)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3691 | } else if (strcmp(descriptor, "[I") == 0) { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 3692 | new_class.Assign(GetClassRoot<mirror::IntArray>(this)); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3693 | } else if (strcmp(descriptor, "[J") == 0) { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 3694 | new_class.Assign(GetClassRoot<mirror::LongArray>(this)); |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 3695 | } else if (strcmp(descriptor, "[F") == 0) { |
| 3696 | new_class.Assign(GetClassRoot<mirror::FloatArray>(this)); |
| 3697 | } else if (strcmp(descriptor, "[D") == 0) { |
| 3698 | new_class.Assign(GetClassRoot<mirror::DoubleArray>(this)); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3699 | } |
| 3700 | } |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3701 | if (new_class == nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3702 | new_class.Assign(AllocClass(self, mirror::Array::ClassSize(image_pointer_size_))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3703 | if (new_class == nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3704 | self->AssertPendingOOMException(); |
Mathieu Chartier | c0a9ea4 | 2014-02-03 16:36:49 -0800 | [diff] [blame] | 3705 | return nullptr; |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3706 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3707 | new_class->SetComponentType(component_type.Get()); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3708 | } |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 3709 | 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] | 3710 | DCHECK(new_class->GetComponentType() != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 3711 | ObjPtr<mirror::Class> java_lang_Object = GetClassRoot<mirror::Object>(this); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3712 | new_class->SetSuperClass(java_lang_Object); |
| 3713 | new_class->SetVTable(java_lang_Object->GetVTable()); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 3714 | new_class->SetPrimitiveType(Primitive::kPrimNot); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3715 | new_class->SetClassLoader(component_type->GetClassLoader()); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 3716 | if (component_type->IsPrimitive()) { |
| 3717 | new_class->SetClassFlags(mirror::kClassFlagNoReferenceFields); |
| 3718 | } else { |
| 3719 | new_class->SetClassFlags(mirror::kClassFlagObjectArray); |
| 3720 | } |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 3721 | mirror::Class::SetStatus(new_class, ClassStatus::kLoaded, self); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 3722 | new_class->PopulateEmbeddedVTable(image_pointer_size_); |
| 3723 | ImTable* object_imt = java_lang_Object->GetImt(image_pointer_size_); |
| 3724 | new_class->SetImt(object_imt, image_pointer_size_); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 3725 | mirror::Class::SetStatus(new_class, ClassStatus::kInitialized, self); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3726 | // don't need to set new_class->SetObjectSize(..) |
Brian Carlstrom | 9cff8e1 | 2011-08-18 16:47:29 -0700 | [diff] [blame] | 3727 | // because Object::SizeOf delegates to Array::SizeOf |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3728 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3729 | // All arrays have java/lang/Cloneable and java/io/Serializable as |
| 3730 | // interfaces. We need to set that up here, so that stuff like |
| 3731 | // "instanceof" works right. |
| 3732 | // |
| 3733 | // Note: The GC could run during the call to FindSystemClass, |
| 3734 | // so we need to make sure the class object is GC-valid while we're in |
| 3735 | // there. Do this by clearing the interface list so the GC will just |
| 3736 | // think that the entries are null. |
| 3737 | |
| 3738 | |
| 3739 | // Use the single, global copies of "interfaces" and "iftable" |
| 3740 | // (remember not to free them for arrays). |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 3741 | { |
Vladimir Marko | 0261055 | 2018-06-04 14:38:00 +0100 | [diff] [blame] | 3742 | ObjPtr<mirror::IfTable> array_iftable = GetArrayIfTable(); |
Hiroshi Yamauchi | e9e3e69 | 2014-06-24 14:31:37 -0700 | [diff] [blame] | 3743 | CHECK(array_iftable != nullptr); |
| 3744 | new_class->SetIfTable(array_iftable); |
| 3745 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3746 | |
Elliott Hughes | 00626c2 | 2013-06-14 15:04:14 -0700 | [diff] [blame] | 3747 | // Inherit access flags from the component type. |
| 3748 | int access_flags = new_class->GetComponentType()->GetAccessFlags(); |
| 3749 | // Lose any implementation detail flags; in particular, arrays aren't finalizable. |
| 3750 | access_flags &= kAccJavaFlagsMask; |
| 3751 | // 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] | 3752 | // and remove "interface". |
Elliott Hughes | 00626c2 | 2013-06-14 15:04:14 -0700 | [diff] [blame] | 3753 | access_flags |= kAccAbstract | kAccFinal; |
| 3754 | access_flags &= ~kAccInterface; |
| 3755 | |
| 3756 | new_class->SetAccessFlags(access_flags); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3757 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3758 | ObjPtr<mirror::Class> existing = InsertClass(descriptor, new_class.Get(), hash); |
Mathieu Chartier | c0a9ea4 | 2014-02-03 16:36:49 -0800 | [diff] [blame] | 3759 | if (existing == nullptr) { |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 3760 | // We postpone ClassLoad and ClassPrepare events to this point in time to avoid |
| 3761 | // duplicate events in case of races. Array classes don't really follow dedicated |
| 3762 | // load and prepare, anyways. |
| 3763 | Runtime::Current()->GetRuntimeCallbacks()->ClassLoad(new_class); |
| 3764 | Runtime::Current()->GetRuntimeCallbacks()->ClassPrepare(new_class, new_class); |
| 3765 | |
Tamas Berghammer | 160e6df | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 3766 | jit::Jit::NewTypeLoadedIfUsingJit(new_class.Get()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3767 | return new_class.Get(); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 3768 | } |
| 3769 | // Another thread must have loaded the class after we |
| 3770 | // started but before we finished. Abandon what we've |
| 3771 | // done. |
| 3772 | // |
| 3773 | // (Yes, this happens.) |
| 3774 | |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 3775 | return existing; |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 3776 | } |
| 3777 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 3778 | ObjPtr<mirror::Class> ClassLinker::FindPrimitiveClass(char type) { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 3779 | ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots = GetClassRoots(); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3780 | switch (type) { |
| 3781 | case 'B': |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 3782 | return GetClassRoot(ClassRoot::kPrimitiveByte, class_roots); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3783 | case 'C': |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 3784 | return GetClassRoot(ClassRoot::kPrimitiveChar, class_roots); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3785 | case 'D': |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 3786 | return GetClassRoot(ClassRoot::kPrimitiveDouble, class_roots); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3787 | case 'F': |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 3788 | return GetClassRoot(ClassRoot::kPrimitiveFloat, class_roots); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3789 | case 'I': |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 3790 | return GetClassRoot(ClassRoot::kPrimitiveInt, class_roots); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3791 | case 'J': |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 3792 | return GetClassRoot(ClassRoot::kPrimitiveLong, class_roots); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3793 | case 'S': |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 3794 | return GetClassRoot(ClassRoot::kPrimitiveShort, class_roots); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3795 | case 'Z': |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 3796 | return GetClassRoot(ClassRoot::kPrimitiveBoolean, class_roots); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3797 | case 'V': |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 3798 | return GetClassRoot(ClassRoot::kPrimitiveVoid, class_roots); |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 3799 | default: |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 3800 | break; |
Carl Shapiro | 744ad05 | 2011-08-06 15:53:36 -0700 | [diff] [blame] | 3801 | } |
Elliott Hughes | bd93599 | 2011-08-22 11:59:34 -0700 | [diff] [blame] | 3802 | std::string printable_type(PrintableChar(type)); |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 3803 | ThrowNoClassDefFoundError("Not a primitive type: %s", printable_type.c_str()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3804 | return nullptr; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3805 | } |
| 3806 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 3807 | ObjPtr<mirror::Class> ClassLinker::InsertClass(const char* descriptor, |
| 3808 | ObjPtr<mirror::Class> klass, |
| 3809 | size_t hash) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 3810 | if (VLOG_IS_ON(class_linker)) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3811 | ObjPtr<mirror::DexCache> dex_cache = klass->GetDexCache(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 3812 | std::string source; |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3813 | if (dex_cache != nullptr) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 3814 | source += " from "; |
| 3815 | source += dex_cache->GetLocation()->ToModifiedUtf8(); |
| 3816 | } |
| 3817 | LOG(INFO) << "Loaded class " << descriptor << source; |
| 3818 | } |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 3819 | { |
| 3820 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3821 | ObjPtr<mirror::ClassLoader> const class_loader = klass->GetClassLoader(); |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 3822 | ClassTable* const class_table = InsertClassTableForClassLoader(class_loader); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3823 | ObjPtr<mirror::Class> existing = class_table->Lookup(descriptor, hash); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3824 | if (existing != nullptr) { |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 3825 | return existing; |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 3826 | } |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 3827 | VerifyObject(klass); |
| 3828 | class_table->InsertWithHash(klass, hash); |
| 3829 | if (class_loader != nullptr) { |
| 3830 | // This is necessary because we need to have the card dirtied for remembered sets. |
Mathieu Chartier | 88ea61e | 2018-06-20 17:45:41 -0700 | [diff] [blame] | 3831 | WriteBarrier::ForEveryFieldWrite(class_loader); |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 3832 | } |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 3833 | if (log_new_roots_) { |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 3834 | new_class_roots_.push_back(GcRoot<mirror::Class>(klass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3835 | } |
| 3836 | } |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 3837 | if (kIsDebugBuild) { |
| 3838 | // Test that copied methods correctly can find their holder. |
| 3839 | for (ArtMethod& method : klass->GetCopiedMethods(image_pointer_size_)) { |
| 3840 | CHECK_EQ(GetHoldingClassOfCopiedMethod(&method), klass); |
| 3841 | } |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 3842 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3843 | return nullptr; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3844 | } |
| 3845 | |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 3846 | void ClassLinker::WriteBarrierForBootOatFileBssRoots(const OatFile* oat_file) { |
Mathieu Chartier | a1467d0 | 2017-02-22 09:22:50 -0800 | [diff] [blame] | 3847 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
| 3848 | DCHECK(!oat_file->GetBssGcRoots().empty()) << oat_file->GetLocation(); |
| 3849 | if (log_new_roots_ && !ContainsElement(new_bss_roots_boot_oat_files_, oat_file)) { |
| 3850 | new_bss_roots_boot_oat_files_.push_back(oat_file); |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 3851 | } |
| 3852 | } |
| 3853 | |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 3854 | // TODO This should really be in mirror::Class. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3855 | void ClassLinker::UpdateClassMethods(ObjPtr<mirror::Class> klass, |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 3856 | LengthPrefixedArray<ArtMethod>* new_methods) { |
| 3857 | klass->SetMethodsPtrUnchecked(new_methods, |
| 3858 | klass->NumDirectMethods(), |
| 3859 | klass->NumDeclaredVirtualMethods()); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3860 | // Need to mark the card so that the remembered sets and mod union tables get updated. |
Mathieu Chartier | 88ea61e | 2018-06-20 17:45:41 -0700 | [diff] [blame] | 3861 | WriteBarrier::ForEveryFieldWrite(klass); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3862 | } |
| 3863 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 3864 | ObjPtr<mirror::Class> ClassLinker::LookupClass(Thread* self, |
| 3865 | const char* descriptor, |
| 3866 | ObjPtr<mirror::ClassLoader> class_loader) { |
Andreas Gampe | 2ff3b97 | 2017-06-05 18:14:53 -0700 | [diff] [blame] | 3867 | return LookupClass(self, descriptor, ComputeModifiedUtf8Hash(descriptor), class_loader); |
| 3868 | } |
| 3869 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 3870 | ObjPtr<mirror::Class> ClassLinker::LookupClass(Thread* self, |
| 3871 | const char* descriptor, |
| 3872 | size_t hash, |
| 3873 | ObjPtr<mirror::ClassLoader> class_loader) { |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 3874 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 3875 | ClassTable* const class_table = ClassTableForClassLoader(class_loader); |
| 3876 | if (class_table != nullptr) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3877 | ObjPtr<mirror::Class> result = class_table->Lookup(descriptor, hash); |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 3878 | if (result != nullptr) { |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 3879 | return result; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3880 | } |
Sameer Abu Asal | 2c6de22 | 2013-05-02 17:38:59 -0700 | [diff] [blame] | 3881 | } |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 3882 | return nullptr; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3883 | } |
| 3884 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3885 | class MoveClassTableToPreZygoteVisitor : public ClassLoaderVisitor { |
| 3886 | public: |
Igor Murashkin | 2ffb703 | 2017-11-08 13:35:21 -0800 | [diff] [blame] | 3887 | MoveClassTableToPreZygoteVisitor() {} |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3888 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3889 | void Visit(ObjPtr<mirror::ClassLoader> class_loader) |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3890 | REQUIRES(Locks::classlinker_classes_lock_) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 3891 | REQUIRES_SHARED(Locks::mutator_lock_) override { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3892 | ClassTable* const class_table = class_loader->GetClassTable(); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 3893 | if (class_table != nullptr) { |
| 3894 | class_table->FreezeSnapshot(); |
| 3895 | } |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 3896 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3897 | }; |
| 3898 | |
| 3899 | void ClassLinker::MoveClassTableToPreZygote() { |
| 3900 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 3901 | boot_class_table_->FreezeSnapshot(); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3902 | MoveClassTableToPreZygoteVisitor visitor; |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3903 | VisitClassLoaders(&visitor); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 3904 | } |
| 3905 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3906 | // Look up classes by hash and descriptor and put all matching ones in the result array. |
| 3907 | class LookupClassesVisitor : public ClassLoaderVisitor { |
| 3908 | public: |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3909 | LookupClassesVisitor(const char* descriptor, |
| 3910 | size_t hash, |
| 3911 | std::vector<ObjPtr<mirror::Class>>* result) |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3912 | : descriptor_(descriptor), |
| 3913 | hash_(hash), |
| 3914 | result_(result) {} |
| 3915 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3916 | void Visit(ObjPtr<mirror::ClassLoader> class_loader) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 3917 | REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_) override { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3918 | ClassTable* const class_table = class_loader->GetClassTable(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3919 | ObjPtr<mirror::Class> klass = class_table->Lookup(descriptor_, hash_); |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 3920 | // Add `klass` only if `class_loader` is its defining (not just initiating) class loader. |
| 3921 | if (klass != nullptr && klass->GetClassLoader() == class_loader) { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3922 | result_->push_back(klass); |
| 3923 | } |
| 3924 | } |
| 3925 | |
| 3926 | private: |
| 3927 | const char* const descriptor_; |
| 3928 | const size_t hash_; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3929 | std::vector<ObjPtr<mirror::Class>>* const result_; |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3930 | }; |
| 3931 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3932 | void ClassLinker::LookupClasses(const char* descriptor, |
| 3933 | std::vector<ObjPtr<mirror::Class>>& result) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3934 | result.clear(); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3935 | Thread* const self = Thread::Current(); |
| 3936 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 3937 | const size_t hash = ComputeModifiedUtf8Hash(descriptor); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 3938 | ObjPtr<mirror::Class> klass = boot_class_table_->Lookup(descriptor, hash); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 3939 | if (klass != nullptr) { |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 3940 | DCHECK(klass->GetClassLoader() == nullptr); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 3941 | result.push_back(klass); |
| 3942 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 3943 | LookupClassesVisitor visitor(descriptor, hash, &result); |
| 3944 | VisitClassLoaders(&visitor); |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 3945 | } |
| 3946 | |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 3947 | bool ClassLinker::AttemptSupertypeVerification(Thread* self, |
| 3948 | Handle<mirror::Class> klass, |
| 3949 | Handle<mirror::Class> supertype) { |
| 3950 | DCHECK(self != nullptr); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3951 | DCHECK(klass != nullptr); |
| 3952 | DCHECK(supertype != nullptr); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 3953 | |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 3954 | if (!supertype->IsVerified() && !supertype->IsErroneous()) { |
| 3955 | VerifyClass(self, supertype); |
| 3956 | } |
Nicolas Geoffray | 7cc3ae5 | 2017-03-07 14:33:37 +0000 | [diff] [blame] | 3957 | |
| 3958 | if (supertype->IsVerified() || supertype->ShouldVerifyAtRuntime()) { |
| 3959 | // The supertype is either verified, or we soft failed at AOT time. |
| 3960 | DCHECK(supertype->IsVerified() || Runtime::Current()->IsAotCompiler()); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 3961 | return true; |
| 3962 | } |
| 3963 | // If we got this far then we have a hard failure. |
| 3964 | std::string error_msg = |
| 3965 | StringPrintf("Rejecting class %s that attempts to sub-type erroneous class %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3966 | klass->PrettyDescriptor().c_str(), |
| 3967 | supertype->PrettyDescriptor().c_str()); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 3968 | LOG(WARNING) << error_msg << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8(); |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 3969 | StackHandleScope<1> hs(self); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 3970 | Handle<mirror::Throwable> cause(hs.NewHandle(self->GetException())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3971 | if (cause != nullptr) { |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 3972 | // Set during VerifyClass call (if at all). |
| 3973 | self->ClearException(); |
| 3974 | } |
| 3975 | // Change into a verify error. |
| 3976 | ThrowVerifyError(klass.Get(), "%s", error_msg.c_str()); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3977 | if (cause != nullptr) { |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 3978 | self->GetException()->SetCause(cause.Get()); |
| 3979 | } |
| 3980 | ClassReference ref(klass->GetDexCache()->GetDexFile(), klass->GetDexClassDefIndex()); |
| 3981 | if (Runtime::Current()->IsAotCompiler()) { |
| 3982 | Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref); |
| 3983 | } |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 3984 | // Need to grab the lock to change status. |
| 3985 | ObjectLock<mirror::Class> super_lock(self, klass); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 3986 | mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 3987 | return false; |
| 3988 | } |
| 3989 | |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 3990 | // Ensures that methods have the kAccSkipAccessChecks bit set. We use the |
| 3991 | // kAccVerificationAttempted bit on the class access flags to determine whether this has been done |
| 3992 | // before. |
| 3993 | static void EnsureSkipAccessChecksMethods(Handle<mirror::Class> klass, PointerSize pointer_size) |
| 3994 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 3995 | if (!klass->WasVerificationAttempted()) { |
| 3996 | klass->SetSkipAccessChecksFlagOnAllMethods(pointer_size); |
| 3997 | klass->SetVerificationAttempted(); |
| 3998 | } |
| 3999 | } |
| 4000 | |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4001 | verifier::FailureKind ClassLinker::VerifyClass( |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame] | 4002 | Thread* self, Handle<mirror::Class> klass, verifier::HardFailLogMode log_level) { |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4003 | { |
| 4004 | // TODO: assert that the monitor on the Class is held |
| 4005 | ObjectLock<mirror::Class> lock(self, klass); |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 4006 | |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4007 | // Is somebody verifying this now? |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4008 | ClassStatus old_status = klass->GetStatus(); |
| 4009 | while (old_status == ClassStatus::kVerifying || |
| 4010 | old_status == ClassStatus::kVerifyingAtRuntime) { |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4011 | lock.WaitIgnoringInterrupts(); |
Mathieu Chartier | 5ef7020 | 2017-06-29 10:45:10 -0700 | [diff] [blame] | 4012 | // WaitIgnoringInterrupts can still receive an interrupt and return early, in this |
| 4013 | // case we may see the same status again. b/62912904. This is why the check is |
| 4014 | // greater or equal. |
| 4015 | CHECK(klass->IsErroneous() || (klass->GetStatus() >= old_status)) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4016 | << "Class '" << klass->PrettyClass() |
| 4017 | << "' performed an illegal verification state transition from " << old_status |
| 4018 | << " to " << klass->GetStatus(); |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4019 | old_status = klass->GetStatus(); |
| 4020 | } |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 4021 | |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4022 | // The class might already be erroneous, for example at compile time if we attempted to verify |
| 4023 | // this class as a parent to another. |
| 4024 | if (klass->IsErroneous()) { |
| 4025 | ThrowEarlierClassFailure(klass.Get()); |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4026 | return verifier::FailureKind::kHardFailure; |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4027 | } |
Brian Carlstrom | 9b5ee88 | 2012-02-28 09:48:54 -0800 | [diff] [blame] | 4028 | |
Nicolas Geoffray | 7cc3ae5 | 2017-03-07 14:33:37 +0000 | [diff] [blame] | 4029 | // Don't attempt to re-verify if already verified. |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4030 | if (klass->IsVerified()) { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 4031 | EnsureSkipAccessChecksMethods(klass, image_pointer_size_); |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4032 | return verifier::FailureKind::kNoFailure; |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4033 | } |
Nicolas Geoffray | 7cc3ae5 | 2017-03-07 14:33:37 +0000 | [diff] [blame] | 4034 | |
| 4035 | // For AOT, don't attempt to re-verify if we have already found we should |
| 4036 | // verify at runtime. |
| 4037 | if (Runtime::Current()->IsAotCompiler() && klass->ShouldVerifyAtRuntime()) { |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4038 | return verifier::FailureKind::kSoftFailure; |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4039 | } |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 4040 | |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4041 | if (klass->GetStatus() == ClassStatus::kResolved) { |
| 4042 | mirror::Class::SetStatus(klass, ClassStatus::kVerifying, self); |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4043 | } else { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4044 | CHECK_EQ(klass->GetStatus(), ClassStatus::kRetryVerificationAtRuntime) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4045 | << klass->PrettyClass(); |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4046 | CHECK(!Runtime::Current()->IsAotCompiler()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4047 | mirror::Class::SetStatus(klass, ClassStatus::kVerifyingAtRuntime, self); |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4048 | } |
| 4049 | |
| 4050 | // Skip verification if disabled. |
| 4051 | if (!Runtime::Current()->IsVerificationEnabled()) { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4052 | mirror::Class::SetStatus(klass, ClassStatus::kVerified, self); |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 4053 | EnsureSkipAccessChecksMethods(klass, image_pointer_size_); |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4054 | return verifier::FailureKind::kNoFailure; |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4055 | } |
Jeff Hao | 4a200f5 | 2014-04-01 14:58:49 -0700 | [diff] [blame] | 4056 | } |
| 4057 | |
Bharadwaj Kalandhabhatta | 271c1e1 | 2017-06-27 11:14:49 -0700 | [diff] [blame] | 4058 | VLOG(class_linker) << "Beginning verification for class: " |
| 4059 | << klass->PrettyDescriptor() |
| 4060 | << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8(); |
| 4061 | |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 4062 | // Verify super class. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4063 | StackHandleScope<2> hs(self); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4064 | MutableHandle<mirror::Class> supertype(hs.NewHandle(klass->GetSuperClass())); |
| 4065 | // 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] | 4066 | if (supertype != nullptr && !AttemptSupertypeVerification(self, klass, supertype)) { |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4067 | CHECK(self->IsExceptionPending()) << "Verification error should be pending."; |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4068 | return verifier::FailureKind::kHardFailure; |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4069 | } |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 4070 | |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4071 | // Verify all default super-interfaces. |
| 4072 | // |
| 4073 | // (1) Don't bother if the superclass has already had a soft verification failure. |
| 4074 | // |
| 4075 | // (2) Interfaces shouldn't bother to do this recursive verification because they cannot cause |
| 4076 | // recursive initialization by themselves. This is because when an interface is initialized |
| 4077 | // directly it must not initialize its superinterfaces. We are allowed to verify regardless |
| 4078 | // but choose not to for an optimization. If the interfaces is being verified due to a class |
| 4079 | // initialization (which would need all the default interfaces to be verified) the class code |
| 4080 | // will trigger the recursive verification anyway. |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4081 | if ((supertype == nullptr || supertype->IsVerified()) // See (1) |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4082 | && !klass->IsInterface()) { // See (2) |
| 4083 | int32_t iftable_count = klass->GetIfTableCount(); |
| 4084 | MutableHandle<mirror::Class> iface(hs.NewHandle<mirror::Class>(nullptr)); |
| 4085 | // Loop through all interfaces this class has defined. It doesn't matter the order. |
| 4086 | for (int32_t i = 0; i < iftable_count; i++) { |
| 4087 | iface.Assign(klass->GetIfTable()->GetInterface(i)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4088 | DCHECK(iface != nullptr); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4089 | // We only care if we have default interfaces and can skip if we are already verified... |
| 4090 | if (LIKELY(!iface->HasDefaultMethods() || iface->IsVerified())) { |
| 4091 | continue; |
| 4092 | } else if (UNLIKELY(!AttemptSupertypeVerification(self, klass, iface))) { |
| 4093 | // We had a hard failure while verifying this interface. Just return immediately. |
| 4094 | CHECK(self->IsExceptionPending()) << "Verification error should be pending."; |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4095 | return verifier::FailureKind::kHardFailure; |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4096 | } else if (UNLIKELY(!iface->IsVerified())) { |
| 4097 | // We softly failed to verify the iface. Stop checking and clean up. |
| 4098 | // Put the iface into the supertype handle so we know what caused us to fail. |
| 4099 | supertype.Assign(iface.Get()); |
| 4100 | break; |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 4101 | } |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 4102 | } |
| 4103 | } |
| 4104 | |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4105 | // At this point if verification failed, then supertype is the "first" supertype that failed |
| 4106 | // verification (without a specific order). If verification succeeded, then supertype is either |
| 4107 | // null or the original superclass of klass and is verified. |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4108 | DCHECK(supertype == nullptr || |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4109 | supertype.Get() == klass->GetSuperClass() || |
| 4110 | !supertype->IsVerified()); |
| 4111 | |
Elliott Hughes | 634eb2e | 2012-03-22 16:06:28 -0700 | [diff] [blame] | 4112 | // 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] | 4113 | const DexFile& dex_file = *klass->GetDexCache()->GetDexFile(); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4114 | ClassStatus oat_file_class_status(ClassStatus::kNotReady); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4115 | bool preverified = VerifyClassUsingOatFile(dex_file, klass.Get(), oat_file_class_status); |
Bharadwaj Kalandhabhatta | 271c1e1 | 2017-06-27 11:14:49 -0700 | [diff] [blame] | 4116 | |
| 4117 | VLOG(class_linker) << "Class preverified status for class " |
| 4118 | << klass->PrettyDescriptor() |
| 4119 | << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8() |
| 4120 | << ": " |
| 4121 | << preverified; |
| 4122 | |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 4123 | // If the oat file says the class had an error, re-run the verifier. That way we will get a |
| 4124 | // precise error message. To ensure a rerun, test: |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 4125 | // mirror::Class::IsErroneous(oat_file_class_status) => !preverified |
| 4126 | DCHECK(!mirror::Class::IsErroneous(oat_file_class_status) || !preverified); |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 4127 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4128 | std::string error_msg; |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4129 | verifier::FailureKind verifier_failure = verifier::FailureKind::kNoFailure; |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 4130 | if (!preverified) { |
Mathieu Chartier | 9e050df | 2017-08-09 10:05:47 -0700 | [diff] [blame] | 4131 | verifier_failure = PerformClassVerification(self, klass, log_level, &error_msg); |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 4132 | } |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4133 | |
| 4134 | // Verification is done, grab the lock again. |
| 4135 | ObjectLock<mirror::Class> lock(self, klass); |
| 4136 | |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4137 | if (preverified || verifier_failure != verifier::FailureKind::kHardFailure) { |
| 4138 | if (!preverified && verifier_failure != verifier::FailureKind::kNoFailure) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4139 | VLOG(class_linker) << "Soft verification failure in class " |
| 4140 | << klass->PrettyDescriptor() |
| 4141 | << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8() |
| 4142 | << " because: " << error_msg; |
Ian Rogers | 529781d | 2012-07-23 17:24:29 -0700 | [diff] [blame] | 4143 | } |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 4144 | self->AssertNoPendingException(); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 4145 | // Make sure all classes referenced by catch blocks are resolved. |
Alex Light | 5a55986 | 2016-01-29 12:24:48 -0800 | [diff] [blame] | 4146 | ResolveClassExceptionHandlerTypes(klass); |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4147 | if (verifier_failure == verifier::FailureKind::kNoFailure) { |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4148 | // Even though there were no verifier failures we need to respect whether the super-class and |
| 4149 | // super-default-interfaces were verified or requiring runtime reverification. |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4150 | if (supertype == nullptr || supertype->IsVerified()) { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4151 | mirror::Class::SetStatus(klass, ClassStatus::kVerified, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4152 | } else { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4153 | CHECK_EQ(supertype->GetStatus(), ClassStatus::kRetryVerificationAtRuntime); |
| 4154 | mirror::Class::SetStatus(klass, ClassStatus::kRetryVerificationAtRuntime, self); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4155 | // Pretend a soft failure occurred so that we don't consider the class verified below. |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4156 | verifier_failure = verifier::FailureKind::kSoftFailure; |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4157 | } |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 4158 | } else { |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4159 | CHECK_EQ(verifier_failure, verifier::FailureKind::kSoftFailure); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 4160 | // Soft failures at compile time should be retried at runtime. Soft |
| 4161 | // failures at runtime will be handled by slow paths in the generated |
| 4162 | // code. Set status accordingly. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 4163 | if (Runtime::Current()->IsAotCompiler()) { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4164 | mirror::Class::SetStatus(klass, ClassStatus::kRetryVerificationAtRuntime, self); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 4165 | } else { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4166 | mirror::Class::SetStatus(klass, ClassStatus::kVerified, self); |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 4167 | // As this is a fake verified status, make sure the methods are _not_ marked |
| 4168 | // kAccSkipAccessChecks later. |
| 4169 | klass->SetVerificationAttempted(); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 4170 | } |
| 4171 | } |
jeffhao | 5cfd6fb | 2011-09-27 13:54:29 -0700 | [diff] [blame] | 4172 | } else { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4173 | VLOG(verifier) << "Verification failed on class " << klass->PrettyDescriptor() |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 4174 | << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8() |
| 4175 | << " because: " << error_msg; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4176 | self->AssertNoPendingException(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4177 | ThrowVerifyError(klass.Get(), "%s", error_msg.c_str()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4178 | mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self); |
jeffhao | 5cfd6fb | 2011-09-27 13:54:29 -0700 | [diff] [blame] | 4179 | } |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4180 | if (preverified || verifier_failure == verifier::FailureKind::kNoFailure) { |
Brian Carlstrom | 6d3f72c | 2013-08-21 18:06:34 -0700 | [diff] [blame] | 4181 | // 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] | 4182 | // Let the interpreter know it by setting the kAccSkipAccessChecks flag onto each |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 4183 | // method. |
| 4184 | // 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] | 4185 | // kAccSkipAccessChecks flag when compiling image classes, the flag is recorded |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 4186 | // in the image and is set when loading the image. |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 4187 | |
| 4188 | if (UNLIKELY(Runtime::Current()->IsVerificationSoftFail())) { |
| 4189 | // Never skip access checks if the verification soft fail is forced. |
| 4190 | // Mark the class as having a verification attempt to avoid re-running the verifier. |
| 4191 | klass->SetVerificationAttempted(); |
| 4192 | } else { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 4193 | EnsureSkipAccessChecksMethods(klass, image_pointer_size_); |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 4194 | } |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 4195 | } |
Nicolas Geoffray | 486dda0 | 2017-09-11 14:15:52 +0100 | [diff] [blame] | 4196 | // Done verifying. Notify the compiler about the verification status, in case the class |
| 4197 | // was verified implicitly (eg super class of a compiled class). |
| 4198 | if (Runtime::Current()->IsAotCompiler()) { |
| 4199 | Runtime::Current()->GetCompilerCallbacks()->UpdateClassState( |
| 4200 | ClassReference(&klass->GetDexFile(), klass->GetDexClassDefIndex()), klass->GetStatus()); |
| 4201 | } |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame] | 4202 | return verifier_failure; |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 4203 | } |
| 4204 | |
Mathieu Chartier | 9e050df | 2017-08-09 10:05:47 -0700 | [diff] [blame] | 4205 | verifier::FailureKind ClassLinker::PerformClassVerification(Thread* self, |
| 4206 | Handle<mirror::Class> klass, |
| 4207 | verifier::HardFailLogMode log_level, |
| 4208 | std::string* error_msg) { |
| 4209 | Runtime* const runtime = Runtime::Current(); |
| 4210 | return verifier::MethodVerifier::VerifyClass(self, |
| 4211 | klass.Get(), |
| 4212 | runtime->GetCompilerCallbacks(), |
| 4213 | runtime->IsAotCompiler(), |
| 4214 | log_level, |
Andreas Gampe | 6cc23ac | 2018-08-24 15:22:43 -0700 | [diff] [blame] | 4215 | Runtime::Current()->GetTargetSdkVersion(), |
Mathieu Chartier | 9e050df | 2017-08-09 10:05:47 -0700 | [diff] [blame] | 4216 | error_msg); |
| 4217 | } |
| 4218 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 4219 | bool ClassLinker::VerifyClassUsingOatFile(const DexFile& dex_file, |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4220 | ObjPtr<mirror::Class> klass, |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4221 | ClassStatus& oat_file_class_status) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 4222 | // If we're compiling, we can only verify the class using the oat file if |
| 4223 | // we are not compiling the image or if the class we're verifying is not part of |
Andreas Gampe | e993458 | 2018-01-19 21:23:04 -0800 | [diff] [blame] | 4224 | // the compilation unit (app - dependencies). We will let the compiler callback |
| 4225 | // tell us about the latter. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 4226 | if (Runtime::Current()->IsAotCompiler()) { |
Andreas Gampe | e993458 | 2018-01-19 21:23:04 -0800 | [diff] [blame] | 4227 | CompilerCallbacks* callbacks = Runtime::Current()->GetCompilerCallbacks(); |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 4228 | // Are we compiling the bootclasspath? |
Andreas Gampe | e993458 | 2018-01-19 21:23:04 -0800 | [diff] [blame] | 4229 | if (callbacks->IsBootImage()) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 4230 | return false; |
| 4231 | } |
| 4232 | // We are compiling an app (not the image). |
Andreas Gampe | e993458 | 2018-01-19 21:23:04 -0800 | [diff] [blame] | 4233 | if (!callbacks->CanUseOatStatusForVerification(klass.Ptr())) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 4234 | return false; |
| 4235 | } |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4236 | } |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 4237 | |
Andreas Gampe | b40d361 | 2018-06-26 15:49:42 -0700 | [diff] [blame] | 4238 | const OatDexFile* oat_dex_file = dex_file.GetOatDexFile(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4239 | // 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] | 4240 | if (oat_dex_file == nullptr || oat_dex_file->GetOatFile() == nullptr) { |
Anwar Ghuloum | ad256bb | 2013-07-18 14:58:55 -0700 | [diff] [blame] | 4241 | return false; |
| 4242 | } |
| 4243 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 4244 | uint16_t class_def_index = klass->GetDexClassDefIndex(); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 4245 | oat_file_class_status = oat_dex_file->GetOatClass(class_def_index).GetStatus(); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4246 | if (oat_file_class_status >= ClassStatus::kVerified) { |
Mathieu Chartier | a079e3a | 2016-03-16 19:08:31 -0700 | [diff] [blame] | 4247 | return true; |
| 4248 | } |
| 4249 | // If we only verified a subset of the classes at compile time, we can end up with classes that |
| 4250 | // were resolved by the verifier. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4251 | if (oat_file_class_status == ClassStatus::kResolved) { |
Mathieu Chartier | a079e3a | 2016-03-16 19:08:31 -0700 | [diff] [blame] | 4252 | return false; |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4253 | } |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4254 | if (oat_file_class_status == ClassStatus::kRetryVerificationAtRuntime) { |
jeffhao | 1ac2944 | 2012-03-26 11:37:32 -0700 | [diff] [blame] | 4255 | // Compile time verification failed with a soft error. Compile time verification can fail |
| 4256 | // because we have incomplete type information. Consider the following: |
Ian Rogers | c476227 | 2012-02-01 15:55:55 -0800 | [diff] [blame] | 4257 | // class ... { |
| 4258 | // Foo x; |
| 4259 | // .... () { |
| 4260 | // if (...) { |
| 4261 | // v1 gets assigned a type of resolved class Foo |
| 4262 | // } else { |
| 4263 | // v1 gets assigned a type of unresolved class Bar |
| 4264 | // } |
| 4265 | // iput x = v1 |
| 4266 | // } } |
| 4267 | // when we merge v1 following the if-the-else it results in Conflict |
| 4268 | // (see verifier::RegType::Merge) as we can't know the type of Bar and we could possibly be |
| 4269 | // allowing an unsafe assignment to the field x in the iput (javac may have compiled this as |
| 4270 | // it knew Bar was a sub-class of Foo, but for us this may have been moved into a separate apk |
| 4271 | // at compile time). |
| 4272 | return false; |
| 4273 | } |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 4274 | if (mirror::Class::IsErroneous(oat_file_class_status)) { |
jeffhao | 1ac2944 | 2012-03-26 11:37:32 -0700 | [diff] [blame] | 4275 | // Compile time verification failed with a hard error. This is caused by invalid instructions |
| 4276 | // in the class. These errors are unrecoverable. |
| 4277 | return false; |
| 4278 | } |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4279 | if (oat_file_class_status == ClassStatus::kNotReady) { |
Ian Rogers | c476227 | 2012-02-01 15:55:55 -0800 | [diff] [blame] | 4280 | // Status is uninitialized if we couldn't determine the status at compile time, for example, |
| 4281 | // not loading the class. |
| 4282 | // TODO: when the verifier doesn't rely on Class-es failing to resolve/load the type hierarchy |
| 4283 | // isn't a problem and this case shouldn't occur |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4284 | return false; |
| 4285 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 4286 | std::string temp; |
Elliott Hughes | 634eb2e | 2012-03-22 16:06:28 -0700 | [diff] [blame] | 4287 | LOG(FATAL) << "Unexpected class status: " << oat_file_class_status |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4288 | << " " << dex_file.GetLocation() << " " << klass->PrettyClass() << " " |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 4289 | << klass->GetDescriptor(&temp); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 4290 | UNREACHABLE(); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4291 | } |
| 4292 | |
Alex Light | 5a55986 | 2016-01-29 12:24:48 -0800 | [diff] [blame] | 4293 | void ClassLinker::ResolveClassExceptionHandlerTypes(Handle<mirror::Class> klass) { |
Alex Light | 51a64d5 | 2015-12-17 13:55:59 -0800 | [diff] [blame] | 4294 | for (ArtMethod& method : klass->GetMethods(image_pointer_size_)) { |
Alex Light | 5a55986 | 2016-01-29 12:24:48 -0800 | [diff] [blame] | 4295 | ResolveMethodExceptionHandlerTypes(&method); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4296 | } |
| 4297 | } |
| 4298 | |
Alex Light | 5a55986 | 2016-01-29 12:24:48 -0800 | [diff] [blame] | 4299 | void ClassLinker::ResolveMethodExceptionHandlerTypes(ArtMethod* method) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4300 | // similar to DexVerifier::ScanTryCatchBlocks and dex2oat's ResolveExceptionsForMethod. |
David Sehr | 0225f8e | 2018-01-31 08:52:24 +0000 | [diff] [blame] | 4301 | CodeItemDataAccessor accessor(method->DexInstructionData()); |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 4302 | if (!accessor.HasCodeItem()) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4303 | return; // native or abstract method |
| 4304 | } |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 4305 | if (accessor.TriesSize() == 0) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4306 | return; // nothing to process |
| 4307 | } |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 4308 | const uint8_t* handlers_ptr = accessor.GetCatchHandlerData(0); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4309 | uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4310 | for (uint32_t idx = 0; idx < handlers_size; idx++) { |
| 4311 | CatchHandlerIterator iterator(handlers_ptr); |
| 4312 | for (; iterator.HasNext(); iterator.Next()) { |
| 4313 | // Ensure exception types are resolved so that they don't need resolution to be delivered, |
| 4314 | // unresolved exception types will be ignored by exception delivery |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 4315 | if (iterator.GetHandlerTypeIndex().IsValid()) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4316 | ObjPtr<mirror::Class> exception_type = ResolveType(iterator.GetHandlerTypeIndex(), method); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4317 | if (exception_type == nullptr) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4318 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 4319 | Thread::Current()->ClearException(); |
| 4320 | } |
| 4321 | } |
| 4322 | } |
| 4323 | handlers_ptr = iterator.EndDataPointer(); |
| 4324 | } |
| 4325 | } |
| 4326 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 4327 | ObjPtr<mirror::Class> ClassLinker::CreateProxyClass(ScopedObjectAccessAlreadyRunnable& soa, |
| 4328 | jstring name, |
| 4329 | jobjectArray interfaces, |
| 4330 | jobject loader, |
| 4331 | jobjectArray methods, |
| 4332 | jobjectArray throws) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4333 | Thread* self = soa.Self(); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4334 | StackHandleScope<10> hs(self); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4335 | MutableHandle<mirror::Class> temp_klass(hs.NewHandle( |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 4336 | AllocClass(self, GetClassRoot<mirror::Class>(this), sizeof(mirror::Class)))); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4337 | if (temp_klass == nullptr) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4338 | CHECK(self->IsExceptionPending()); // OOME. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4339 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4340 | } |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4341 | DCHECK(temp_klass->GetClass() != nullptr); |
| 4342 | temp_klass->SetObjectSize(sizeof(mirror::Proxy)); |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 4343 | // Set the class access flags incl. VerificationAttempted, so we do not try to set the flag on |
| 4344 | // the methods. |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4345 | temp_klass->SetAccessFlags(kAccClassIsProxy | kAccPublic | kAccFinal | kAccVerificationAttempted); |
| 4346 | temp_klass->SetClassLoader(soa.Decode<mirror::ClassLoader>(loader)); |
| 4347 | DCHECK_EQ(temp_klass->GetPrimitiveType(), Primitive::kPrimNot); |
| 4348 | temp_klass->SetName(soa.Decode<mirror::String>(name)); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 4349 | temp_klass->SetDexCache(GetClassRoot<mirror::Proxy>(this)->GetDexCache()); |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 4350 | // Object has an empty iftable, copy it for that reason. |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 4351 | temp_klass->SetIfTable(GetClassRoot<mirror::Object>(this)->GetIfTable()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4352 | mirror::Class::SetStatus(temp_klass, ClassStatus::kIdx, self); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4353 | std::string descriptor(GetDescriptorForProxy(temp_klass.Get())); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 4354 | const size_t hash = ComputeModifiedUtf8Hash(descriptor.c_str()); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4355 | |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 4356 | // Needs to be before we insert the class so that the allocator field is set. |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4357 | LinearAlloc* const allocator = GetOrCreateAllocatorForClassLoader(temp_klass->GetClassLoader()); |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 4358 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4359 | // Insert the class before loading the fields as the field roots |
| 4360 | // (ArtField::declaring_class_) are only visited from the class |
| 4361 | // table. There can't be any suspend points between inserting the |
| 4362 | // class and setting the field arrays below. |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4363 | ObjPtr<mirror::Class> existing = InsertClass(descriptor.c_str(), temp_klass.Get(), hash); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4364 | CHECK(existing == nullptr); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4365 | |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 4366 | // Instance fields are inherited, but we add a couple of static fields... |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4367 | const size_t num_fields = 2; |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 4368 | LengthPrefixedArray<ArtField>* sfields = AllocArtFieldArray(self, allocator, num_fields); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4369 | temp_klass->SetSFieldsPtr(sfields); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4370 | |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 4371 | // 1. Create a static field 'interfaces' that holds the _declared_ interfaces implemented by |
| 4372 | // our proxy, so Class.getInterfaces doesn't return the flattened set. |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4373 | ArtField& interfaces_sfield = sfields->At(0); |
| 4374 | interfaces_sfield.SetDexFieldIndex(0); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4375 | interfaces_sfield.SetDeclaringClass(temp_klass.Get()); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4376 | interfaces_sfield.SetAccessFlags(kAccStatic | kAccPublic | kAccFinal); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4377 | |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 4378 | // 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] | 4379 | ArtField& throws_sfield = sfields->At(1); |
| 4380 | throws_sfield.SetDexFieldIndex(1); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4381 | throws_sfield.SetDeclaringClass(temp_klass.Get()); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4382 | throws_sfield.SetAccessFlags(kAccStatic | kAccPublic | kAccFinal); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4383 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4384 | // Proxies have 1 direct method, the constructor |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4385 | const size_t num_direct_methods = 1; |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4386 | |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4387 | // They have as many virtual methods as the array |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 4388 | auto h_methods = hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::Method>>(methods)); |
Vladimir Marko | 679730e | 2018-05-25 15:06:48 +0100 | [diff] [blame] | 4389 | DCHECK_EQ(h_methods->GetClass(), GetClassRoot<mirror::ObjectArray<mirror::Method>>()) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4390 | << mirror::Class::PrettyClass(h_methods->GetClass()); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 4391 | const size_t num_virtual_methods = h_methods->GetLength(); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4392 | |
| 4393 | // Create the methods array. |
| 4394 | LengthPrefixedArray<ArtMethod>* proxy_class_methods = AllocArtMethodArray( |
| 4395 | self, allocator, num_direct_methods + num_virtual_methods); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4396 | // Currently AllocArtMethodArray cannot return null, but the OOM logic is left there in case we |
| 4397 | // want to throw OOM in the future. |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4398 | if (UNLIKELY(proxy_class_methods == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4399 | self->AssertPendingOOMException(); |
| 4400 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4401 | } |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4402 | temp_klass->SetMethodsPtr(proxy_class_methods, num_direct_methods, num_virtual_methods); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4403 | |
| 4404 | // Create the single direct method. |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4405 | CreateProxyConstructor(temp_klass, temp_klass->GetDirectMethodUnchecked(0, image_pointer_size_)); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4406 | |
| 4407 | // Create virtual method using specified prototypes. |
| 4408 | // TODO These should really use the iterators. |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4409 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4410 | auto* virtual_method = temp_klass->GetVirtualMethodUnchecked(i, image_pointer_size_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4411 | auto* prototype = h_methods->Get(i)->GetArtMethod(); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4412 | CreateProxyMethod(temp_klass, prototype, virtual_method); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4413 | DCHECK(virtual_method->GetDeclaringClass() != nullptr); |
| 4414 | DCHECK(prototype->GetDeclaringClass() != nullptr); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4415 | } |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4416 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4417 | // The super class is java.lang.reflect.Proxy |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 4418 | temp_klass->SetSuperClass(GetClassRoot<mirror::Proxy>(this)); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4419 | // Now effectively in the loaded state. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4420 | mirror::Class::SetStatus(temp_klass, ClassStatus::kLoaded, self); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4421 | self->AssertNoPendingException(); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4422 | |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4423 | // At this point the class is loaded. Publish a ClassLoad event. |
| 4424 | // Note: this may be a temporary class. It is a listener's responsibility to handle this. |
| 4425 | Runtime::Current()->GetRuntimeCallbacks()->ClassLoad(temp_klass); |
| 4426 | |
| 4427 | MutableHandle<mirror::Class> klass = hs.NewHandle<mirror::Class>(nullptr); |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 4428 | { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4429 | // Must hold lock on object when resolved. |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4430 | ObjectLock<mirror::Class> resolution_lock(self, temp_klass); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4431 | // Link the fields and virtual methods, creating vtable and iftables. |
| 4432 | // The new class will replace the old one in the class table. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4433 | Handle<mirror::ObjectArray<mirror::Class>> h_interfaces( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 4434 | hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::Class>>(interfaces))); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4435 | if (!LinkClass(self, descriptor.c_str(), temp_klass, h_interfaces, &klass)) { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4436 | mirror::Class::SetStatus(temp_klass, ClassStatus::kErrorUnresolved, self); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 4437 | return nullptr; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4438 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4439 | } |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4440 | CHECK(temp_klass->IsRetired()); |
| 4441 | CHECK_NE(temp_klass.Get(), klass.Get()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4442 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4443 | CHECK_EQ(interfaces_sfield.GetDeclaringClass(), klass.Get()); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 4444 | interfaces_sfield.SetObject<false>( |
| 4445 | klass.Get(), |
Mathieu Chartier | f8ac97f | 2016-10-05 15:56:52 -0700 | [diff] [blame] | 4446 | soa.Decode<mirror::ObjectArray<mirror::Class>>(interfaces)); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4447 | CHECK_EQ(throws_sfield.GetDeclaringClass(), klass.Get()); |
| 4448 | throws_sfield.SetObject<false>( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 4449 | klass.Get(), |
Mathieu Chartier | f8ac97f | 2016-10-05 15:56:52 -0700 | [diff] [blame] | 4450 | soa.Decode<mirror::ObjectArray<mirror::ObjectArray<mirror::Class>>>(throws)); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4451 | |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4452 | Runtime::Current()->GetRuntimeCallbacks()->ClassPrepare(temp_klass, klass); |
| 4453 | |
Vladimir Marko | 305c38b | 2018-02-14 11:50:07 +0000 | [diff] [blame] | 4454 | // SubtypeCheckInfo::Initialized must happen-before any new-instance for that type. |
| 4455 | // See also ClassLinker::EnsureInitialized(). |
| 4456 | if (kBitstringSubtypeCheckEnabled) { |
| 4457 | MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_); |
| 4458 | SubtypeCheck<ObjPtr<mirror::Class>>::EnsureInitialized(klass.Get()); |
| 4459 | // TODO: Avoid taking subtype_check_lock_ if SubtypeCheck for j.l.r.Proxy is already assigned. |
| 4460 | } |
| 4461 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4462 | { |
| 4463 | // Lock on klass is released. Lock new class object. |
| 4464 | ObjectLock<mirror::Class> initialization_lock(self, klass); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4465 | mirror::Class::SetStatus(klass, ClassStatus::kInitialized, self); |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 4466 | } |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4467 | |
| 4468 | // sanity checks |
Elliott Hughes | 67d9200 | 2012-03-26 15:08:51 -0700 | [diff] [blame] | 4469 | if (kIsDebugBuild) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4470 | CHECK(klass->GetIFieldsPtr() == nullptr); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4471 | CheckProxyConstructor(klass->GetDirectMethod(0, image_pointer_size_)); |
| 4472 | |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4473 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4474 | auto* virtual_method = klass->GetVirtualMethodUnchecked(i, image_pointer_size_); |
| 4475 | auto* prototype = h_methods->Get(i++)->GetArtMethod(); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 4476 | CheckProxyMethod(virtual_method, prototype); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4477 | } |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 4478 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4479 | StackHandleScope<1> hs2(self); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 4480 | Handle<mirror::String> decoded_name = hs2.NewHandle(soa.Decode<mirror::String>(name)); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 4481 | std::string interfaces_field_name(StringPrintf("java.lang.Class[] %s.interfaces", |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4482 | decoded_name->ToModifiedUtf8().c_str())); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4483 | CHECK_EQ(ArtField::PrettyField(klass->GetStaticField(0)), interfaces_field_name); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 4484 | |
| 4485 | std::string throws_field_name(StringPrintf("java.lang.Class[][] %s.throws", |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4486 | decoded_name->ToModifiedUtf8().c_str())); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4487 | CHECK_EQ(ArtField::PrettyField(klass->GetStaticField(1)), throws_field_name); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4488 | |
Narayan Kamath | 6b2dc31 | 2017-03-14 13:26:12 +0000 | [diff] [blame] | 4489 | CHECK_EQ(klass.Get()->GetProxyInterfaces(), |
Mathieu Chartier | f8ac97f | 2016-10-05 15:56:52 -0700 | [diff] [blame] | 4490 | soa.Decode<mirror::ObjectArray<mirror::Class>>(interfaces)); |
Narayan Kamath | 6b2dc31 | 2017-03-14 13:26:12 +0000 | [diff] [blame] | 4491 | CHECK_EQ(klass.Get()->GetProxyThrows(), |
Mathieu Chartier | f8ac97f | 2016-10-05 15:56:52 -0700 | [diff] [blame] | 4492 | soa.Decode<mirror::ObjectArray<mirror::ObjectArray<mirror::Class>>>(throws)); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4493 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4494 | return klass.Get(); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4495 | } |
| 4496 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4497 | std::string ClassLinker::GetDescriptorForProxy(ObjPtr<mirror::Class> proxy_class) { |
Nicolas Geoffray | a7a4759 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 4498 | DCHECK(proxy_class->IsProxyClass()); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4499 | ObjPtr<mirror::String> name = proxy_class->GetName(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4500 | DCHECK(name != nullptr); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4501 | return DotToDescriptor(name->ToModifiedUtf8().c_str()); |
| 4502 | } |
| 4503 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4504 | void ClassLinker::CreateProxyConstructor(Handle<mirror::Class> klass, ArtMethod* out) { |
| 4505 | // Create constructor for Proxy that must initialize the method. |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 4506 | ObjPtr<mirror::Class> proxy_class = GetClassRoot<mirror::Proxy>(this); |
| 4507 | CHECK_EQ(proxy_class->NumDirectMethods(), 21u); |
Przemyslaw Szczepaniak | f11cd29 | 2016-08-17 17:46:38 +0100 | [diff] [blame] | 4508 | |
Igor Murashkin | 9d3d752 | 2017-02-27 10:39:49 -0800 | [diff] [blame] | 4509 | // Find the <init>(InvocationHandler)V method. The exact method offset varies depending |
| 4510 | // on which front-end compiler was used to build the libcore DEX files. |
Alex Light | 6cae5ea | 2018-06-07 17:07:02 -0700 | [diff] [blame] | 4511 | ArtMethod* proxy_constructor = |
| 4512 | jni::DecodeArtMethod(WellKnownClasses::java_lang_reflect_Proxy_init); |
Igor Murashkin | 9d3d752 | 2017-02-27 10:39:49 -0800 | [diff] [blame] | 4513 | DCHECK(proxy_constructor != nullptr) |
| 4514 | << "Could not find <init> method in java.lang.reflect.Proxy"; |
| 4515 | |
Jeff Hao | db8a664 | 2014-08-14 17:18:52 -0700 | [diff] [blame] | 4516 | // Clone the existing constructor of Proxy (our constructor would just invoke it so steal its |
| 4517 | // code_ too) |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4518 | DCHECK(out != nullptr); |
| 4519 | out->CopyFrom(proxy_constructor, image_pointer_size_); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 4520 | // Make this constructor public and fix the class to be our Proxy version. |
Mathieu Chartier | 201e297 | 2017-06-05 18:34:53 -0700 | [diff] [blame] | 4521 | // Mark kAccCompileDontBother so that we don't take JIT samples for the method. b/62349349 |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 4522 | // Note that the compiler calls a ResolveMethod() overload that does not handle a Proxy referrer. |
Mathieu Chartier | 201e297 | 2017-06-05 18:34:53 -0700 | [diff] [blame] | 4523 | out->SetAccessFlags((out->GetAccessFlags() & ~kAccProtected) | |
| 4524 | kAccPublic | |
| 4525 | kAccCompileDontBother); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4526 | out->SetDeclaringClass(klass.Get()); |
Vladimir Marko | d1ee20f | 2017-08-17 09:21:16 +0000 | [diff] [blame] | 4527 | |
| 4528 | // Set the original constructor method. |
| 4529 | out->SetDataPtrSize(proxy_constructor, image_pointer_size_); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4530 | } |
| 4531 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4532 | void ClassLinker::CheckProxyConstructor(ArtMethod* constructor) const { |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4533 | CHECK(constructor->IsConstructor()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4534 | auto* np = constructor->GetInterfaceMethodIfProxy(image_pointer_size_); |
| 4535 | CHECK_STREQ(np->GetName(), "<init>"); |
| 4536 | CHECK_STREQ(np->GetSignature().ToString().c_str(), "(Ljava/lang/reflect/InvocationHandler;)V"); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4537 | DCHECK(constructor->IsPublic()); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4538 | } |
| 4539 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 4540 | void ClassLinker::CreateProxyMethod(Handle<mirror::Class> klass, ArtMethod* prototype, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4541 | ArtMethod* out) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4542 | // 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] | 4543 | // as necessary |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4544 | DCHECK(out != nullptr); |
| 4545 | out->CopyFrom(prototype, image_pointer_size_); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4546 | |
Alex Light | e9dd04f | 2016-03-16 16:09:45 -0700 | [diff] [blame] | 4547 | // Set class to be the concrete proxy class. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4548 | out->SetDeclaringClass(klass.Get()); |
Alex Light | e9dd04f | 2016-03-16 16:09:45 -0700 | [diff] [blame] | 4549 | // Clear the abstract, default and conflict flags to ensure that defaults aren't picked in |
| 4550 | // preference to the invocation handler. |
| 4551 | const uint32_t kRemoveFlags = kAccAbstract | kAccDefault | kAccDefaultConflict; |
| 4552 | // Make the method final. |
Mathieu Chartier | 201e297 | 2017-06-05 18:34:53 -0700 | [diff] [blame] | 4553 | // Mark kAccCompileDontBother so that we don't take JIT samples for the method. b/62349349 |
| 4554 | const uint32_t kAddFlags = kAccFinal | kAccCompileDontBother; |
Alex Light | e9dd04f | 2016-03-16 16:09:45 -0700 | [diff] [blame] | 4555 | out->SetAccessFlags((out->GetAccessFlags() & ~kRemoveFlags) | kAddFlags); |
| 4556 | |
| 4557 | // Clear the dex_code_item_offset_. It needs to be 0 since proxy methods have no CodeItems but the |
| 4558 | // method they copy might (if it's a default method). |
| 4559 | out->SetCodeItemOffset(0); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4560 | |
Vladimir Marko | d1ee20f | 2017-08-17 09:21:16 +0000 | [diff] [blame] | 4561 | // Set the original interface method. |
| 4562 | out->SetDataPtrSize(prototype, image_pointer_size_); |
| 4563 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4564 | // At runtime the method looks like a reference and argument saving method, clone the code |
| 4565 | // related parameters from this method. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4566 | out->SetEntryPointFromQuickCompiledCode(GetQuickProxyInvokeHandler()); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4567 | } |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4568 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4569 | void ClassLinker::CheckProxyMethod(ArtMethod* method, ArtMethod* prototype) const { |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4570 | // Basic sanity |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4571 | CHECK(!prototype->IsFinal()); |
| 4572 | CHECK(method->IsFinal()); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 4573 | CHECK(method->IsInvokable()); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 4574 | |
| 4575 | // The proxy method doesn't have its own dex cache or dex file and so it steals those of its |
| 4576 | // interface prototype. The exception to this are Constructors and the Class of the Proxy itself. |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 4577 | CHECK_EQ(prototype->GetDexMethodIndex(), method->GetDexMethodIndex()); |
Vladimir Marko | 5c3e9d1 | 2017-08-30 16:43:54 +0100 | [diff] [blame] | 4578 | CHECK_EQ(prototype, method->GetInterfaceMethodIfProxy(image_pointer_size_)); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4579 | } |
| 4580 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4581 | bool ClassLinker::CanWeInitializeClass(ObjPtr<mirror::Class> klass, bool can_init_statics, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4582 | bool can_init_parents) { |
Brian Carlstrom | 610e49f | 2013-11-04 17:07:22 -0800 | [diff] [blame] | 4583 | if (can_init_statics && can_init_parents) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4584 | return true; |
| 4585 | } |
| 4586 | if (!can_init_statics) { |
| 4587 | // Check if there's a class initializer. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4588 | ArtMethod* clinit = klass->FindClassInitializer(image_pointer_size_); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4589 | if (clinit != nullptr) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4590 | return false; |
| 4591 | } |
| 4592 | // Check if there are encoded static values needing initialization. |
| 4593 | if (klass->NumStaticFields() != 0) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4594 | const DexFile::ClassDef* dex_class_def = klass->GetClassDef(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4595 | DCHECK(dex_class_def != nullptr); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4596 | if (dex_class_def->static_values_off_ != 0) { |
| 4597 | return false; |
| 4598 | } |
| 4599 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 4600 | // If we are a class we need to initialize all interfaces with default methods when we are |
| 4601 | // initialized. Check all of them. |
| 4602 | if (!klass->IsInterface()) { |
| 4603 | size_t num_interfaces = klass->GetIfTableCount(); |
| 4604 | for (size_t i = 0; i < num_interfaces; i++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4605 | ObjPtr<mirror::Class> iface = klass->GetIfTable()->GetInterface(i); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 4606 | if (iface->HasDefaultMethods() && |
| 4607 | !CanWeInitializeClass(iface, can_init_statics, can_init_parents)) { |
| 4608 | return false; |
| 4609 | } |
| 4610 | } |
| 4611 | } |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4612 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4613 | if (klass->IsInterface() || !klass->HasSuperClass()) { |
| 4614 | return true; |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4615 | } |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4616 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4617 | if (!can_init_parents && !super_class->IsInitialized()) { |
| 4618 | return false; |
| 4619 | } |
| 4620 | return CanWeInitializeClass(super_class, can_init_statics, can_init_parents); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4621 | } |
| 4622 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 4623 | bool ClassLinker::InitializeClass(Thread* self, Handle<mirror::Class> klass, |
| 4624 | bool can_init_statics, bool can_init_parents) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4625 | // see JLS 3rd edition, 12.4.2 "Detailed Initialization Procedure" for the locking protocol |
| 4626 | |
| 4627 | // Are we already initialized and therefore done? |
| 4628 | // Note: we differ from the JLS here as we don't do this under the lock, this is benign as |
| 4629 | // an initialized class will never change its state. |
| 4630 | if (klass->IsInitialized()) { |
| 4631 | return true; |
| 4632 | } |
| 4633 | |
| 4634 | // 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] | 4635 | if (!CanWeInitializeClass(klass.Get(), can_init_statics, can_init_parents)) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4636 | return false; |
| 4637 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4638 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4639 | self->AllowThreadSuspension(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4640 | uint64_t t0; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4641 | { |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 4642 | ObjectLock<mirror::Class> lock(self, klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4643 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4644 | // Re-check under the lock in case another thread initialized ahead of us. |
| 4645 | if (klass->IsInitialized()) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4646 | return true; |
| 4647 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4648 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4649 | // 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] | 4650 | if (klass->IsErroneous()) { |
Andreas Gampe | cb08695 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 4651 | ThrowEarlierClassFailure(klass.Get(), true); |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 4652 | VlogClassInitializationFailure(klass); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4653 | return false; |
| 4654 | } |
| 4655 | |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 4656 | CHECK(klass->IsResolved() && !klass->IsErroneousResolved()) |
| 4657 | << klass->PrettyClass() << ": state=" << klass->GetStatus(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4658 | |
| 4659 | if (!klass->IsVerified()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4660 | VerifyClass(self, klass); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4661 | if (!klass->IsVerified()) { |
| 4662 | // We failed to verify, expect either the klass to be erroneous or verification failed at |
| 4663 | // compile time. |
| 4664 | if (klass->IsErroneous()) { |
Andreas Gampe | fc49fa0 | 2016-04-21 12:21:55 -0700 | [diff] [blame] | 4665 | // The class is erroneous. This may be a verifier error, or another thread attempted |
| 4666 | // verification and/or initialization and failed. We can distinguish those cases by |
| 4667 | // whether an exception is already pending. |
| 4668 | if (self->IsExceptionPending()) { |
| 4669 | // Check that it's a VerifyError. |
| 4670 | DCHECK_EQ("java.lang.Class<java.lang.VerifyError>", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4671 | mirror::Class::PrettyClass(self->GetException()->GetClass())); |
Andreas Gampe | fc49fa0 | 2016-04-21 12:21:55 -0700 | [diff] [blame] | 4672 | } else { |
| 4673 | // Check that another thread attempted initialization. |
| 4674 | DCHECK_NE(0, klass->GetClinitThreadId()); |
| 4675 | DCHECK_NE(self->GetTid(), klass->GetClinitThreadId()); |
| 4676 | // Need to rethrow the previous failure now. |
| 4677 | ThrowEarlierClassFailure(klass.Get(), true); |
| 4678 | } |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 4679 | VlogClassInitializationFailure(klass); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4680 | } else { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 4681 | CHECK(Runtime::Current()->IsAotCompiler()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4682 | CHECK_EQ(klass->GetStatus(), ClassStatus::kRetryVerificationAtRuntime); |
jeffhao | a9b3bf4 | 2012-06-06 17:18:39 -0700 | [diff] [blame] | 4683 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4684 | return false; |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 4685 | } else { |
| 4686 | self->AssertNoPendingException(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4687 | } |
Andreas Gampe | fc49fa0 | 2016-04-21 12:21:55 -0700 | [diff] [blame] | 4688 | |
| 4689 | // A separate thread could have moved us all the way to initialized. A "simple" example |
| 4690 | // involves a subclass of the current class being initialized at the same time (which |
| 4691 | // will implicitly initialize the superclass, if scheduled that way). b/28254258 |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 4692 | DCHECK(!klass->IsErroneous()) << klass->GetStatus(); |
Andreas Gampe | fc49fa0 | 2016-04-21 12:21:55 -0700 | [diff] [blame] | 4693 | if (klass->IsInitialized()) { |
| 4694 | return true; |
| 4695 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4696 | } |
| 4697 | |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4698 | // If the class is ClassStatus::kInitializing, either this thread is |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4699 | // initializing higher up the stack or another thread has beat us |
| 4700 | // to initializing and we need to wait. Either way, this |
| 4701 | // invocation of InitializeClass will not be responsible for |
| 4702 | // running <clinit> and will return. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4703 | if (klass->GetStatus() == ClassStatus::kInitializing) { |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 4704 | // Could have got an exception during verification. |
| 4705 | if (self->IsExceptionPending()) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 4706 | VlogClassInitializationFailure(klass); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 4707 | return false; |
| 4708 | } |
Elliott Hughes | 005ab2e | 2011-09-11 17:15:31 -0700 | [diff] [blame] | 4709 | // We caught somebody else in the act; was it us? |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 4710 | if (klass->GetClinitThreadId() == self->GetTid()) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4711 | // Yes. That's fine. Return so we can continue initializing. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4712 | return true; |
| 4713 | } |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4714 | // No. That's fine. Wait for another thread to finish initializing. |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 4715 | return WaitForInitializeClass(klass, self, lock); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4716 | } |
| 4717 | |
Jeff Hao | e2e4034 | 2017-07-19 10:45:18 -0700 | [diff] [blame] | 4718 | // Try to get the oat class's status for this class if the oat file is present. The compiler |
| 4719 | // tries to validate superclass descriptors, and writes the result into the oat file. |
| 4720 | // Runtime correctness is guaranteed by classpath checks done on loading. If the classpath |
| 4721 | // is different at runtime than it was at compile time, the oat file is rejected. So if the |
| 4722 | // oat file is present, the classpaths must match, and the runtime time check can be skipped. |
Jeff Hao | 0cb1728 | 2017-07-12 14:51:49 -0700 | [diff] [blame] | 4723 | bool has_oat_class = false; |
Jeff Hao | e2e4034 | 2017-07-19 10:45:18 -0700 | [diff] [blame] | 4724 | const Runtime* runtime = Runtime::Current(); |
| 4725 | const OatFile::OatClass oat_class = (runtime->IsStarted() && !runtime->IsAotCompiler()) |
| 4726 | ? OatFile::FindOatClass(klass->GetDexFile(), klass->GetDexClassDefIndex(), &has_oat_class) |
| 4727 | : OatFile::OatClass::Invalid(); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4728 | if (oat_class.GetStatus() < ClassStatus::kSuperclassValidated && |
Jeff Hao | 0cb1728 | 2017-07-12 14:51:49 -0700 | [diff] [blame] | 4729 | !ValidateSuperClassDescriptors(klass)) { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4730 | mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4731 | return false; |
| 4732 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4733 | self->AllowThreadSuspension(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4734 | |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4735 | CHECK_EQ(klass->GetStatus(), ClassStatus::kVerified) << klass->PrettyClass() |
Andreas Gampe | 9510ccd | 2016-04-20 09:55:25 -0700 | [diff] [blame] | 4736 | << " self.tid=" << self->GetTid() << " clinit.tid=" << klass->GetClinitThreadId(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4737 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4738 | // From here out other threads may observe that we're initializing and so changes of state |
| 4739 | // require the a notification. |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 4740 | klass->SetClinitThreadId(self->GetTid()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4741 | mirror::Class::SetStatus(klass, ClassStatus::kInitializing, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4742 | |
| 4743 | t0 = NanoTime(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4744 | } |
| 4745 | |
Brian Carlstrom | 6d3f72c | 2013-08-21 18:06:34 -0700 | [diff] [blame] | 4746 | // Initialize super classes, must be done while initializing for the JLS. |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4747 | if (!klass->IsInterface() && klass->HasSuperClass()) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4748 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4749 | if (!super_class->IsInitialized()) { |
| 4750 | CHECK(!super_class->IsInterface()); |
| 4751 | CHECK(can_init_parents); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4752 | StackHandleScope<1> hs(self); |
| 4753 | Handle<mirror::Class> handle_scope_super(hs.NewHandle(super_class)); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4754 | bool super_initialized = InitializeClass(self, handle_scope_super, can_init_statics, true); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4755 | if (!super_initialized) { |
| 4756 | // The super class was verified ahead of entering initializing, we should only be here if |
| 4757 | // the super class became erroneous due to initialization. |
Chang Xing | adbb91c | 2017-07-17 11:23:55 -0700 | [diff] [blame] | 4758 | // For the case of aot compiler, the super class might also be initializing but we don't |
| 4759 | // want to process circular dependencies in pre-compile. |
| 4760 | CHECK(self->IsExceptionPending()) |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 4761 | << "Super class initialization failed for " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4762 | << handle_scope_super->PrettyDescriptor() |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4763 | << " that has unexpected status " << handle_scope_super->GetStatus() |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4764 | << "\nPending exception:\n" |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 4765 | << (self->GetException() != nullptr ? self->GetException()->Dump() : ""); |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 4766 | ObjectLock<mirror::Class> lock(self, klass); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4767 | // Initialization failed because the super-class is erroneous. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4768 | mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4769 | return false; |
| 4770 | } |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 4771 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4772 | } |
| 4773 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 4774 | if (!klass->IsInterface()) { |
| 4775 | // Initialize interfaces with default methods for the JLS. |
| 4776 | size_t num_direct_interfaces = klass->NumDirectInterfaces(); |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 4777 | // Only setup the (expensive) handle scope if we actually need to. |
| 4778 | if (UNLIKELY(num_direct_interfaces > 0)) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 4779 | StackHandleScope<1> hs_iface(self); |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 4780 | MutableHandle<mirror::Class> handle_scope_iface(hs_iface.NewHandle<mirror::Class>(nullptr)); |
| 4781 | for (size_t i = 0; i < num_direct_interfaces; i++) { |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 4782 | handle_scope_iface.Assign(mirror::Class::GetDirectInterface(self, klass.Get(), i)); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 4783 | CHECK(handle_scope_iface != nullptr) << klass->PrettyDescriptor() << " iface #" << i; |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 4784 | CHECK(handle_scope_iface->IsInterface()); |
| 4785 | if (handle_scope_iface->HasBeenRecursivelyInitialized()) { |
| 4786 | // We have already done this for this interface. Skip it. |
| 4787 | continue; |
| 4788 | } |
| 4789 | // We cannot just call initialize class directly because we need to ensure that ALL |
| 4790 | // interfaces with default methods are initialized. Non-default interface initialization |
| 4791 | // will not affect other non-default super-interfaces. |
| 4792 | bool iface_initialized = InitializeDefaultInterfaceRecursive(self, |
| 4793 | handle_scope_iface, |
| 4794 | can_init_statics, |
| 4795 | can_init_parents); |
| 4796 | if (!iface_initialized) { |
| 4797 | ObjectLock<mirror::Class> lock(self, klass); |
| 4798 | // Initialization failed because one of our interfaces with default methods is erroneous. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4799 | mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self); |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 4800 | return false; |
| 4801 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 4802 | } |
| 4803 | } |
| 4804 | } |
| 4805 | |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 4806 | const size_t num_static_fields = klass->NumStaticFields(); |
| 4807 | if (num_static_fields > 0) { |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4808 | const DexFile::ClassDef* dex_class_def = klass->GetClassDef(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4809 | CHECK(dex_class_def != nullptr); |
Hiroshi Yamauchi | 67ef46a | 2014-08-21 15:59:43 -0700 | [diff] [blame] | 4810 | StackHandleScope<3> hs(self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4811 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader())); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4812 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache())); |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 4813 | |
| 4814 | // Eagerly fill in static fields so that the we don't have to do as many expensive |
| 4815 | // Class::FindStaticField in ResolveField. |
| 4816 | for (size_t i = 0; i < num_static_fields; ++i) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4817 | ArtField* field = klass->GetStaticField(i); |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 4818 | const uint32_t field_idx = field->GetDexFieldIndex(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4819 | ArtField* resolved_field = dex_cache->GetResolvedField(field_idx, image_pointer_size_); |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 4820 | if (resolved_field == nullptr) { |
David Brazdil | 1ab0fa8 | 2018-05-04 11:28:03 +0100 | [diff] [blame] | 4821 | // Populating cache of a dex file which defines `klass` should always be allowed. |
| 4822 | DCHECK_EQ(hiddenapi::GetMemberAction( |
| 4823 | field, class_loader.Get(), dex_cache.Get(), hiddenapi::kNone), hiddenapi::kAllow); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4824 | dex_cache->SetResolvedField(field_idx, field, image_pointer_size_); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 4825 | } else { |
| 4826 | DCHECK_EQ(field, resolved_field); |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 4827 | } |
| 4828 | } |
| 4829 | |
Vladimir Marko | e11dd50 | 2017-12-08 14:09:45 +0000 | [diff] [blame] | 4830 | annotations::RuntimeEncodedStaticFieldValueIterator value_it(dex_cache, |
| 4831 | class_loader, |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 4832 | this, |
| 4833 | *dex_class_def); |
Vladimir Marko | e11dd50 | 2017-12-08 14:09:45 +0000 | [diff] [blame] | 4834 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 4835 | |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 4836 | if (value_it.HasNext()) { |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 4837 | ClassAccessor accessor(dex_file, *dex_class_def); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4838 | CHECK(can_init_statics); |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 4839 | for (const ClassAccessor::Field& field : accessor.GetStaticFields()) { |
| 4840 | if (!value_it.HasNext()) { |
| 4841 | break; |
| 4842 | } |
| 4843 | ArtField* art_field = ResolveField(field.GetIndex(), |
| 4844 | dex_cache, |
| 4845 | class_loader, |
| 4846 | /* is_static */ true); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 4847 | if (Runtime::Current()->IsActiveTransaction()) { |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 4848 | value_it.ReadValueToField<true>(art_field); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 4849 | } else { |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 4850 | value_it.ReadValueToField<false>(art_field); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 4851 | } |
Mathieu Chartier | da595be | 2016-08-10 13:57:39 -0700 | [diff] [blame] | 4852 | if (self->IsExceptionPending()) { |
| 4853 | break; |
| 4854 | } |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 4855 | value_it.Next(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4856 | } |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 4857 | DCHECK(self->IsExceptionPending() || !value_it.HasNext()); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4858 | } |
| 4859 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4860 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4861 | |
Mathieu Chartier | da595be | 2016-08-10 13:57:39 -0700 | [diff] [blame] | 4862 | if (!self->IsExceptionPending()) { |
| 4863 | ArtMethod* clinit = klass->FindClassInitializer(image_pointer_size_); |
| 4864 | if (clinit != nullptr) { |
| 4865 | CHECK(can_init_statics); |
| 4866 | JValue result; |
| 4867 | clinit->Invoke(self, nullptr, 0, &result, "V"); |
| 4868 | } |
| 4869 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4870 | self->AllowThreadSuspension(); |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 4871 | uint64_t t1 = NanoTime(); |
| 4872 | |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 4873 | bool success = true; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4874 | { |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 4875 | ObjectLock<mirror::Class> lock(self, klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4876 | |
| 4877 | if (self->IsExceptionPending()) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 4878 | WrapExceptionInInitializer(klass); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4879 | mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self); |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 4880 | success = false; |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 4881 | } else if (Runtime::Current()->IsTransactionAborted()) { |
| 4882 | // The exception thrown when the transaction aborted has been caught and cleared |
| 4883 | // so we need to throw it again now. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4884 | VLOG(compiler) << "Return from class initializer of " |
| 4885 | << mirror::Class::PrettyDescriptor(klass.Get()) |
Sebastien Hertz | bd9cf9f | 2015-03-03 12:16:13 +0100 | [diff] [blame] | 4886 | << " without exception while transaction was aborted: re-throw it now."; |
Sebastien Hertz | 2fd7e69 | 2015-04-02 11:11:19 +0200 | [diff] [blame] | 4887 | Runtime::Current()->ThrowTransactionAbortError(self); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4888 | mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self); |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 4889 | success = false; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4890 | } else { |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 4891 | RuntimeStats* global_stats = Runtime::Current()->GetStats(); |
| 4892 | RuntimeStats* thread_stats = self->GetStats(); |
| 4893 | ++global_stats->class_init_count; |
| 4894 | ++thread_stats->class_init_count; |
| 4895 | global_stats->class_init_time_ns += (t1 - t0); |
| 4896 | thread_stats->class_init_time_ns += (t1 - t0); |
Ian Rogers | e6bb3b2 | 2013-08-19 21:51:45 -0700 | [diff] [blame] | 4897 | // Set the class as initialized except if failed to initialize static fields. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4898 | mirror::Class::SetStatus(klass, ClassStatus::kInitialized, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4899 | if (VLOG_IS_ON(class_linker)) { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 4900 | std::string temp; |
| 4901 | LOG(INFO) << "Initialized class " << klass->GetDescriptor(&temp) << " from " << |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 4902 | klass->GetLocation(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 4903 | } |
Brian Carlstrom | 073278c | 2014-02-19 15:21:21 -0800 | [diff] [blame] | 4904 | // Opportunistically set static method trampolines to their destination. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4905 | FixupStaticTrampolines(klass.Get()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4906 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4907 | } |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 4908 | return success; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4909 | } |
| 4910 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 4911 | // We recursively run down the tree of interfaces. We need to do this in the order they are declared |
| 4912 | // and perform the initialization only on those interfaces that contain default methods. |
| 4913 | bool ClassLinker::InitializeDefaultInterfaceRecursive(Thread* self, |
| 4914 | Handle<mirror::Class> iface, |
| 4915 | bool can_init_statics, |
| 4916 | bool can_init_parents) { |
| 4917 | CHECK(iface->IsInterface()); |
| 4918 | size_t num_direct_ifaces = iface->NumDirectInterfaces(); |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 4919 | // Only create the (expensive) handle scope if we need it. |
| 4920 | if (UNLIKELY(num_direct_ifaces > 0)) { |
| 4921 | StackHandleScope<1> hs(self); |
| 4922 | MutableHandle<mirror::Class> handle_super_iface(hs.NewHandle<mirror::Class>(nullptr)); |
| 4923 | // First we initialize all of iface's super-interfaces recursively. |
| 4924 | for (size_t i = 0; i < num_direct_ifaces; i++) { |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 4925 | ObjPtr<mirror::Class> super_iface = mirror::Class::GetDirectInterface(self, iface.Get(), i); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 4926 | CHECK(super_iface != nullptr) << iface->PrettyDescriptor() << " iface #" << i; |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 4927 | if (!super_iface->HasBeenRecursivelyInitialized()) { |
| 4928 | // Recursive step |
| 4929 | handle_super_iface.Assign(super_iface); |
| 4930 | if (!InitializeDefaultInterfaceRecursive(self, |
| 4931 | handle_super_iface, |
| 4932 | can_init_statics, |
| 4933 | can_init_parents)) { |
| 4934 | return false; |
| 4935 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 4936 | } |
| 4937 | } |
| 4938 | } |
| 4939 | |
| 4940 | bool result = true; |
| 4941 | // Then we initialize 'iface' if it has default methods. We do not need to (and in fact must not) |
| 4942 | // initialize if we don't have default methods. |
| 4943 | if (iface->HasDefaultMethods()) { |
| 4944 | result = EnsureInitialized(self, iface, can_init_statics, can_init_parents); |
| 4945 | } |
| 4946 | |
| 4947 | // Mark that this interface has undergone recursive default interface initialization so we know we |
| 4948 | // can skip it on any later class initializations. We do this even if we are not a default |
| 4949 | // interface since we can still avoid the traversal. This is purely a performance optimization. |
| 4950 | if (result) { |
| 4951 | // TODO This should be done in a better way |
Andreas Gampe | 976b298 | 2018-03-02 17:54:22 -0800 | [diff] [blame] | 4952 | // Note: Use a try-lock to avoid blocking when someone else is holding the lock on this |
| 4953 | // interface. It is bad (Java) style, but not impossible. Marking the recursive |
| 4954 | // initialization is a performance optimization (to avoid another idempotent visit |
| 4955 | // for other implementing classes/interfaces), and can be revisited later. |
| 4956 | ObjectTryLock<mirror::Class> lock(self, iface); |
| 4957 | if (lock.Acquired()) { |
| 4958 | iface->SetRecursivelyInitialized(); |
| 4959 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 4960 | } |
| 4961 | return result; |
| 4962 | } |
| 4963 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 4964 | bool ClassLinker::WaitForInitializeClass(Handle<mirror::Class> klass, |
| 4965 | Thread* self, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 4966 | ObjectLock<mirror::Class>& lock) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 4967 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4968 | while (true) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4969 | self->AssertNoPendingException(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4970 | CHECK(!klass->IsInitialized()); |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 4971 | lock.WaitIgnoringInterrupts(); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4972 | |
| 4973 | // When we wake up, repeat the test for init-in-progress. If |
| 4974 | // there's an exception pending (only possible if |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 4975 | // we were not using WaitIgnoringInterrupts), bail out. |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4976 | if (self->IsExceptionPending()) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 4977 | WrapExceptionInInitializer(klass); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4978 | mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4979 | return false; |
| 4980 | } |
| 4981 | // Spurious wakeup? Go back to waiting. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4982 | if (klass->GetStatus() == ClassStatus::kInitializing) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4983 | continue; |
| 4984 | } |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4985 | if (klass->GetStatus() == ClassStatus::kVerified && |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 4986 | Runtime::Current()->IsAotCompiler()) { |
Ian Rogers | 3d1548d | 2012-09-24 14:08:03 -0700 | [diff] [blame] | 4987 | // Compile time initialization failed. |
| 4988 | return false; |
| 4989 | } |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4990 | if (klass->IsErroneous()) { |
| 4991 | // The caller wants an exception, but it was thrown in a |
| 4992 | // different thread. Synthesize one here. |
Brian Carlstrom | df14324 | 2011-10-10 18:05:34 -0700 | [diff] [blame] | 4993 | ThrowNoClassDefFoundError("<clinit> failed for class %s; see exception in other thread", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4994 | klass->PrettyDescriptor().c_str()); |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 4995 | VlogClassInitializationFailure(klass); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 4996 | return false; |
| 4997 | } |
| 4998 | if (klass->IsInitialized()) { |
| 4999 | return true; |
| 5000 | } |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5001 | LOG(FATAL) << "Unexpected class status. " << klass->PrettyClass() << " is " |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 5002 | << klass->GetStatus(); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5003 | } |
Ian Rogers | 0714083 | 2014-09-30 15:43:59 -0700 | [diff] [blame] | 5004 | UNREACHABLE(); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5005 | } |
| 5006 | |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5007 | static void ThrowSignatureCheckResolveReturnTypeException(Handle<mirror::Class> klass, |
| 5008 | Handle<mirror::Class> super_klass, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5009 | ArtMethod* method, |
| 5010 | ArtMethod* m) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5011 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5012 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 5013 | DCHECK(!m->IsProxyMethod()); |
| 5014 | const DexFile* dex_file = m->GetDexFile(); |
| 5015 | const DexFile::MethodId& method_id = dex_file->GetMethodId(m->GetDexMethodIndex()); |
| 5016 | const DexFile::ProtoId& proto_id = dex_file->GetMethodPrototype(method_id); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 5017 | dex::TypeIndex return_type_idx = proto_id.return_type_idx_; |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5018 | std::string return_type = dex_file->PrettyType(return_type_idx); |
| 5019 | std::string class_loader = mirror::Object::PrettyTypeOf(m->GetDeclaringClass()->GetClassLoader()); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5020 | ThrowWrappedLinkageError(klass.Get(), |
| 5021 | "While checking class %s method %s signature against %s %s: " |
| 5022 | "Failed to resolve return type %s with %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5023 | mirror::Class::PrettyDescriptor(klass.Get()).c_str(), |
| 5024 | ArtMethod::PrettyMethod(method).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5025 | super_klass->IsInterface() ? "interface" : "superclass", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5026 | mirror::Class::PrettyDescriptor(super_klass.Get()).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5027 | return_type.c_str(), class_loader.c_str()); |
| 5028 | } |
| 5029 | |
| 5030 | static void ThrowSignatureCheckResolveArgException(Handle<mirror::Class> klass, |
| 5031 | Handle<mirror::Class> super_klass, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5032 | ArtMethod* method, |
| 5033 | ArtMethod* m, |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5034 | uint32_t index, |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 5035 | dex::TypeIndex arg_type_idx) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5036 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5037 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 5038 | DCHECK(!m->IsProxyMethod()); |
| 5039 | const DexFile* dex_file = m->GetDexFile(); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5040 | std::string arg_type = dex_file->PrettyType(arg_type_idx); |
| 5041 | std::string class_loader = mirror::Object::PrettyTypeOf(m->GetDeclaringClass()->GetClassLoader()); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5042 | ThrowWrappedLinkageError(klass.Get(), |
| 5043 | "While checking class %s method %s signature against %s %s: " |
| 5044 | "Failed to resolve arg %u type %s with %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5045 | mirror::Class::PrettyDescriptor(klass.Get()).c_str(), |
| 5046 | ArtMethod::PrettyMethod(method).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5047 | super_klass->IsInterface() ? "interface" : "superclass", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5048 | mirror::Class::PrettyDescriptor(super_klass.Get()).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5049 | index, arg_type.c_str(), class_loader.c_str()); |
| 5050 | } |
| 5051 | |
| 5052 | static void ThrowSignatureMismatch(Handle<mirror::Class> klass, |
| 5053 | Handle<mirror::Class> super_klass, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5054 | ArtMethod* method, |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5055 | const std::string& error_msg) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5056 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5057 | ThrowLinkageError(klass.Get(), |
| 5058 | "Class %s method %s resolves differently in %s %s: %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5059 | mirror::Class::PrettyDescriptor(klass.Get()).c_str(), |
| 5060 | ArtMethod::PrettyMethod(method).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5061 | super_klass->IsInterface() ? "interface" : "superclass", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5062 | mirror::Class::PrettyDescriptor(super_klass.Get()).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5063 | error_msg.c_str()); |
| 5064 | } |
| 5065 | |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5066 | static bool HasSameSignatureWithDifferentClassLoaders(Thread* self, |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5067 | Handle<mirror::Class> klass, |
| 5068 | Handle<mirror::Class> super_klass, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5069 | ArtMethod* method1, |
| 5070 | ArtMethod* method2) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5071 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5072 | { |
| 5073 | StackHandleScope<1> hs(self); |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 5074 | Handle<mirror::Class> return_type(hs.NewHandle(method1->ResolveReturnType())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 5075 | if (UNLIKELY(return_type == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5076 | ThrowSignatureCheckResolveReturnTypeException(klass, super_klass, method1, method1); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5077 | return false; |
| 5078 | } |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 5079 | ObjPtr<mirror::Class> other_return_type = method2->ResolveReturnType(); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5080 | if (UNLIKELY(other_return_type == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5081 | ThrowSignatureCheckResolveReturnTypeException(klass, super_klass, method1, method2); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5082 | return false; |
| 5083 | } |
Vladimir Marko | 862f43c | 2015-02-10 18:22:57 +0000 | [diff] [blame] | 5084 | if (UNLIKELY(other_return_type != return_type.Get())) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5085 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 5086 | StringPrintf("Return types mismatch: %s(%p) vs %s(%p)", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5087 | return_type->PrettyClassAndClassLoader().c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5088 | return_type.Get(), |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5089 | other_return_type->PrettyClassAndClassLoader().c_str(), |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5090 | other_return_type.Ptr())); |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5091 | return false; |
| 5092 | } |
| 5093 | } |
| 5094 | const DexFile::TypeList* types1 = method1->GetParameterTypeList(); |
| 5095 | const DexFile::TypeList* types2 = method2->GetParameterTypeList(); |
| 5096 | if (types1 == nullptr) { |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5097 | if (types2 != nullptr && types2->Size() != 0) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5098 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 5099 | StringPrintf("Type list mismatch with %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5100 | method2->PrettyMethod(true).c_str())); |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5101 | return false; |
| 5102 | } |
| 5103 | return true; |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5104 | } else if (UNLIKELY(types2 == nullptr)) { |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5105 | if (types1->Size() != 0) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5106 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 5107 | StringPrintf("Type list mismatch with %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5108 | method2->PrettyMethod(true).c_str())); |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5109 | return false; |
| 5110 | } |
| 5111 | return true; |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5112 | } |
| 5113 | uint32_t num_types = types1->Size(); |
| 5114 | if (UNLIKELY(num_types != types2->Size())) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5115 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 5116 | StringPrintf("Type list mismatch with %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5117 | method2->PrettyMethod(true).c_str())); |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5118 | return false; |
| 5119 | } |
| 5120 | for (uint32_t i = 0; i < num_types; ++i) { |
Vladimir Marko | 862f43c | 2015-02-10 18:22:57 +0000 | [diff] [blame] | 5121 | StackHandleScope<1> hs(self); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 5122 | dex::TypeIndex param_type_idx = types1->GetTypeItem(i).type_idx_; |
Vladimir Marko | 862f43c | 2015-02-10 18:22:57 +0000 | [diff] [blame] | 5123 | Handle<mirror::Class> param_type(hs.NewHandle( |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 5124 | method1->ResolveClassFromTypeIndex(param_type_idx))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 5125 | if (UNLIKELY(param_type == nullptr)) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5126 | ThrowSignatureCheckResolveArgException(klass, super_klass, method1, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5127 | method1, i, param_type_idx); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5128 | return false; |
| 5129 | } |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 5130 | dex::TypeIndex other_param_type_idx = types2->GetTypeItem(i).type_idx_; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5131 | ObjPtr<mirror::Class> other_param_type = |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 5132 | method2->ResolveClassFromTypeIndex(other_param_type_idx); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5133 | if (UNLIKELY(other_param_type == nullptr)) { |
| 5134 | ThrowSignatureCheckResolveArgException(klass, super_klass, method1, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5135 | method2, i, other_param_type_idx); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5136 | return false; |
| 5137 | } |
Vladimir Marko | 862f43c | 2015-02-10 18:22:57 +0000 | [diff] [blame] | 5138 | if (UNLIKELY(param_type.Get() != other_param_type)) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5139 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 5140 | StringPrintf("Parameter %u type mismatch: %s(%p) vs %s(%p)", |
| 5141 | i, |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5142 | param_type->PrettyClassAndClassLoader().c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5143 | param_type.Get(), |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5144 | other_param_type->PrettyClassAndClassLoader().c_str(), |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5145 | other_param_type.Ptr())); |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5146 | return false; |
| 5147 | } |
| 5148 | } |
| 5149 | return true; |
| 5150 | } |
| 5151 | |
| 5152 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5153 | bool ClassLinker::ValidateSuperClassDescriptors(Handle<mirror::Class> klass) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5154 | if (klass->IsInterface()) { |
| 5155 | return true; |
| 5156 | } |
Ian Rogers | 151f221 | 2014-05-06 11:27:27 -0700 | [diff] [blame] | 5157 | // Begin with the methods local to the superclass. |
Ian Rogers | ded66a0 | 2014-10-28 18:12:55 -0700 | [diff] [blame] | 5158 | Thread* self = Thread::Current(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5159 | StackHandleScope<1> hs(self); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5160 | MutableHandle<mirror::Class> super_klass(hs.NewHandle<mirror::Class>(nullptr)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5161 | if (klass->HasSuperClass() && |
| 5162 | klass->GetClassLoader() != klass->GetSuperClass()->GetClassLoader()) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5163 | super_klass.Assign(klass->GetSuperClass()); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 5164 | for (int i = klass->GetSuperClass()->GetVTableLength() - 1; i >= 0; --i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5165 | auto* m = klass->GetVTableEntry(i, image_pointer_size_); |
| 5166 | auto* super_m = klass->GetSuperClass()->GetVTableEntry(i, image_pointer_size_); |
| 5167 | if (m != super_m) { |
Vladimir Marko | 942fd31 | 2017-01-16 20:52:19 +0000 | [diff] [blame] | 5168 | if (UNLIKELY(!HasSameSignatureWithDifferentClassLoaders(self, |
| 5169 | klass, |
| 5170 | super_klass, |
| 5171 | m, |
| 5172 | super_m))) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5173 | self->AssertPendingException(); |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5174 | return false; |
| 5175 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5176 | } |
| 5177 | } |
| 5178 | } |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 5179 | for (int32_t i = 0; i < klass->GetIfTableCount(); ++i) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5180 | super_klass.Assign(klass->GetIfTable()->GetInterface(i)); |
| 5181 | if (klass->GetClassLoader() != super_klass->GetClassLoader()) { |
| 5182 | uint32_t num_methods = super_klass->NumVirtualMethods(); |
Ian Rogers | 151f221 | 2014-05-06 11:27:27 -0700 | [diff] [blame] | 5183 | for (uint32_t j = 0; j < num_methods; ++j) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5184 | auto* m = klass->GetIfTable()->GetMethodArray(i)->GetElementPtrSize<ArtMethod*>( |
| 5185 | j, image_pointer_size_); |
| 5186 | auto* super_m = super_klass->GetVirtualMethod(j, image_pointer_size_); |
| 5187 | if (m != super_m) { |
Vladimir Marko | 942fd31 | 2017-01-16 20:52:19 +0000 | [diff] [blame] | 5188 | if (UNLIKELY(!HasSameSignatureWithDifferentClassLoaders(self, |
| 5189 | klass, |
| 5190 | super_klass, |
| 5191 | m, |
| 5192 | super_m))) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5193 | self->AssertPendingException(); |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5194 | return false; |
| 5195 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5196 | } |
| 5197 | } |
| 5198 | } |
| 5199 | } |
| 5200 | return true; |
| 5201 | } |
| 5202 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5203 | bool ClassLinker::EnsureInitialized(Thread* self, |
| 5204 | Handle<mirror::Class> c, |
| 5205 | bool can_init_fields, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5206 | bool can_init_parents) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 5207 | DCHECK(c != nullptr); |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 5208 | |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 5209 | if (c->IsInitialized()) { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 5210 | EnsureSkipAccessChecksMethods(c, image_pointer_size_); |
Mathieu Chartier | 8502f72 | 2016-06-08 15:09:08 -0700 | [diff] [blame] | 5211 | self->AssertNoPendingException(); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 5212 | return true; |
| 5213 | } |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 5214 | // SubtypeCheckInfo::Initialized must happen-before any new-instance for that type. |
| 5215 | // |
| 5216 | // Ensure the bitstring is initialized before any of the class initialization |
| 5217 | // logic occurs. Once a class initializer starts running, objects can |
| 5218 | // escape into the heap and use the subtype checking code. |
| 5219 | // |
| 5220 | // Note: A class whose SubtypeCheckInfo is at least Initialized means it |
| 5221 | // can be used as a source for the IsSubClass check, and that all ancestors |
| 5222 | // of the class are Assigned (can be used as a target for IsSubClass check) |
| 5223 | // or Overflowed (can be used as a source for IsSubClass check). |
Vladimir Marko | 305c38b | 2018-02-14 11:50:07 +0000 | [diff] [blame] | 5224 | if (kBitstringSubtypeCheckEnabled) { |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 5225 | MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_); |
Vladimir Marko | 38b8b25 | 2018-01-02 19:07:06 +0000 | [diff] [blame] | 5226 | SubtypeCheck<ObjPtr<mirror::Class>>::EnsureInitialized(c.Get()); |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 5227 | // TODO: Avoid taking subtype_check_lock_ if SubtypeCheck is already initialized. |
| 5228 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5229 | const bool success = InitializeClass(self, c, can_init_fields, can_init_parents); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 5230 | if (!success) { |
| 5231 | if (can_init_fields && can_init_parents) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5232 | CHECK(self->IsExceptionPending()) << c->PrettyClass(); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 5233 | } |
| 5234 | } else { |
| 5235 | self->AssertNoPendingException(); |
Ian Rogers | 595799e | 2012-01-11 17:32:51 -0800 | [diff] [blame] | 5236 | } |
| 5237 | return success; |
Elliott Hughes | f4c21c9 | 2011-08-19 17:31:31 -0700 | [diff] [blame] | 5238 | } |
| 5239 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5240 | void ClassLinker::FixupTemporaryDeclaringClass(ObjPtr<mirror::Class> temp_class, |
| 5241 | ObjPtr<mirror::Class> new_class) { |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 5242 | DCHECK_EQ(temp_class->NumInstanceFields(), 0u); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 5243 | for (ArtField& field : new_class->GetIFields()) { |
| 5244 | if (field.GetDeclaringClass() == temp_class) { |
| 5245 | field.SetDeclaringClass(new_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5246 | } |
| 5247 | } |
| 5248 | |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 5249 | DCHECK_EQ(temp_class->NumStaticFields(), 0u); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 5250 | for (ArtField& field : new_class->GetSFields()) { |
| 5251 | if (field.GetDeclaringClass() == temp_class) { |
| 5252 | field.SetDeclaringClass(new_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5253 | } |
| 5254 | } |
| 5255 | |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 5256 | DCHECK_EQ(temp_class->NumDirectMethods(), 0u); |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 5257 | DCHECK_EQ(temp_class->NumVirtualMethods(), 0u); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 5258 | for (auto& method : new_class->GetMethods(image_pointer_size_)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5259 | if (method.GetDeclaringClass() == temp_class) { |
| 5260 | method.SetDeclaringClass(new_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5261 | } |
| 5262 | } |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 5263 | |
| 5264 | // Make sure the remembered set and mod-union tables know that we updated some of the native |
| 5265 | // roots. |
Mathieu Chartier | 88ea61e | 2018-06-20 17:45:41 -0700 | [diff] [blame] | 5266 | WriteBarrier::ForEveryFieldWrite(new_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5267 | } |
| 5268 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5269 | void ClassLinker::RegisterClassLoader(ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | 5b83050 | 2016-03-02 10:30:23 -0800 | [diff] [blame] | 5270 | CHECK(class_loader->GetAllocator() == nullptr); |
| 5271 | CHECK(class_loader->GetClassTable() == nullptr); |
| 5272 | Thread* const self = Thread::Current(); |
| 5273 | ClassLoaderData data; |
Ian Rogers | 55256cb | 2017-12-21 17:07:11 -0800 | [diff] [blame] | 5274 | data.weak_root = self->GetJniEnv()->GetVm()->AddWeakGlobalRef(self, class_loader); |
Mathieu Chartier | 5b83050 | 2016-03-02 10:30:23 -0800 | [diff] [blame] | 5275 | // Create and set the class table. |
| 5276 | data.class_table = new ClassTable; |
| 5277 | class_loader->SetClassTable(data.class_table); |
| 5278 | // Create and set the linear allocator. |
| 5279 | data.allocator = Runtime::Current()->CreateLinearAlloc(); |
| 5280 | class_loader->SetAllocator(data.allocator); |
| 5281 | // Add to the list so that we know to free the data later. |
| 5282 | class_loaders_.push_back(data); |
| 5283 | } |
| 5284 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5285 | ClassTable* ClassLinker::InsertClassTableForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 5286 | if (class_loader == nullptr) { |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 5287 | return boot_class_table_.get(); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5288 | } |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 5289 | ClassTable* class_table = class_loader->GetClassTable(); |
| 5290 | if (class_table == nullptr) { |
Mathieu Chartier | 5b83050 | 2016-03-02 10:30:23 -0800 | [diff] [blame] | 5291 | RegisterClassLoader(class_loader); |
| 5292 | class_table = class_loader->GetClassTable(); |
| 5293 | DCHECK(class_table != nullptr); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 5294 | } |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5295 | return class_table; |
| 5296 | } |
| 5297 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5298 | ClassTable* ClassLinker::ClassTableForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) { |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 5299 | return class_loader == nullptr ? boot_class_table_.get() : class_loader->GetClassTable(); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5300 | } |
| 5301 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5302 | static ImTable* FindSuperImt(ObjPtr<mirror::Class> klass, PointerSize pointer_size) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5303 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5304 | while (klass->HasSuperClass()) { |
| 5305 | klass = klass->GetSuperClass(); |
| 5306 | if (klass->ShouldHaveImt()) { |
| 5307 | return klass->GetImt(pointer_size); |
| 5308 | } |
| 5309 | } |
| 5310 | return nullptr; |
| 5311 | } |
| 5312 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5313 | bool ClassLinker::LinkClass(Thread* self, |
| 5314 | const char* descriptor, |
| 5315 | Handle<mirror::Class> klass, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5316 | Handle<mirror::ObjectArray<mirror::Class>> interfaces, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5317 | MutableHandle<mirror::Class>* h_new_class_out) { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5318 | CHECK_EQ(ClassStatus::kLoaded, klass->GetStatus()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5319 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5320 | if (!LinkSuperClass(klass)) { |
| 5321 | return false; |
| 5322 | } |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5323 | ArtMethod* imt_data[ImTable::kSize]; |
| 5324 | // If there are any new conflicts compared to super class. |
| 5325 | bool new_conflict = false; |
Nicolas Geoffray | 918dcea | 2017-07-21 07:58:14 +0000 | [diff] [blame] | 5326 | std::fill_n(imt_data, arraysize(imt_data), Runtime::Current()->GetImtUnimplementedMethod()); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5327 | if (!LinkMethods(self, klass, interfaces, &new_conflict, imt_data)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5328 | return false; |
| 5329 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5330 | if (!LinkInstanceFields(self, klass)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5331 | return false; |
| 5332 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5333 | size_t class_size; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5334 | if (!LinkStaticFields(self, klass, &class_size)) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 5335 | return false; |
| 5336 | } |
| 5337 | CreateReferenceInstanceOffsets(klass); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5338 | CHECK_EQ(ClassStatus::kLoaded, klass->GetStatus()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5339 | |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5340 | ImTable* imt = nullptr; |
| 5341 | if (klass->ShouldHaveImt()) { |
| 5342 | // If there are any new conflicts compared to the super class we can not make a copy. There |
| 5343 | // can be cases where both will have a conflict method at the same slot without having the same |
| 5344 | // set of conflicts. In this case, we can not share the IMT since the conflict table slow path |
| 5345 | // will possibly create a table that is incorrect for either of the classes. |
| 5346 | // Same IMT with new_conflict does not happen very often. |
| 5347 | if (!new_conflict) { |
| 5348 | ImTable* super_imt = FindSuperImt(klass.Get(), image_pointer_size_); |
| 5349 | if (super_imt != nullptr) { |
| 5350 | bool imt_equals = true; |
| 5351 | for (size_t i = 0; i < ImTable::kSize && imt_equals; ++i) { |
| 5352 | imt_equals = imt_equals && (super_imt->Get(i, image_pointer_size_) == imt_data[i]); |
| 5353 | } |
| 5354 | if (imt_equals) { |
| 5355 | imt = super_imt; |
| 5356 | } |
| 5357 | } |
| 5358 | } |
| 5359 | if (imt == nullptr) { |
| 5360 | LinearAlloc* allocator = GetAllocatorForClassLoader(klass->GetClassLoader()); |
| 5361 | imt = reinterpret_cast<ImTable*>( |
| 5362 | allocator->Alloc(self, ImTable::SizeInBytes(image_pointer_size_))); |
| 5363 | if (imt == nullptr) { |
| 5364 | return false; |
| 5365 | } |
| 5366 | imt->Populate(imt_data, image_pointer_size_); |
| 5367 | } |
| 5368 | } |
| 5369 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5370 | if (!klass->IsTemp() || (!init_done_ && klass->GetClassSize() == class_size)) { |
| 5371 | // We don't need to retire this class as it has no embedded tables or it was created the |
| 5372 | // correct size during class linker initialization. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5373 | CHECK_EQ(klass->GetClassSize(), class_size) << klass->PrettyDescriptor(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5374 | |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5375 | if (klass->ShouldHaveEmbeddedVTable()) { |
| 5376 | klass->PopulateEmbeddedVTable(image_pointer_size_); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5377 | } |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5378 | if (klass->ShouldHaveImt()) { |
| 5379 | klass->SetImt(imt, image_pointer_size_); |
| 5380 | } |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 5381 | |
| 5382 | // Update CHA info based on whether we override methods. |
| 5383 | // Have to do this before setting the class as resolved which allows |
| 5384 | // instantiation of klass. |
Andreas Gampe | c1ac9ee | 2017-07-24 22:35:49 -0700 | [diff] [blame] | 5385 | if (cha_ != nullptr) { |
| 5386 | cha_->UpdateAfterLoadingOf(klass); |
| 5387 | } |
Nicolas Geoffray | 918dcea | 2017-07-21 07:58:14 +0000 | [diff] [blame] | 5388 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5389 | // This will notify waiters on klass that saw the not yet resolved |
| 5390 | // class in the class_table_ during EnsureResolved. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5391 | mirror::Class::SetStatus(klass, ClassStatus::kResolved, self); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 5392 | h_new_class_out->Assign(klass.Get()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5393 | } else { |
| 5394 | CHECK(!klass->IsResolved()); |
| 5395 | // Retire the temporary class and create the correctly sized resolved class. |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 5396 | StackHandleScope<1> hs(self); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5397 | 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] | 5398 | // Set arrays to null since we don't want to have multiple classes with the same ArtField or |
| 5399 | // ArtMethod array pointers. If this occurs, it causes bugs in remembered sets since the GC |
| 5400 | // may not see any references to the target space and clean the card for a class if another |
| 5401 | // class had the same array pointer. |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 5402 | klass->SetMethodsPtrUnchecked(nullptr, 0, 0); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 5403 | klass->SetSFieldsPtrUnchecked(nullptr); |
| 5404 | klass->SetIFieldsPtrUnchecked(nullptr); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 5405 | if (UNLIKELY(h_new_class == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5406 | self->AssertPendingOOMException(); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5407 | mirror::Class::SetStatus(klass, ClassStatus::kErrorUnresolved, self); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5408 | return false; |
| 5409 | } |
| 5410 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 5411 | CHECK_EQ(h_new_class->GetClassSize(), class_size); |
| 5412 | ObjectLock<mirror::Class> lock(self, h_new_class); |
| 5413 | FixupTemporaryDeclaringClass(klass.Get(), h_new_class.Get()); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5414 | |
| 5415 | { |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 5416 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5417 | ObjPtr<mirror::ClassLoader> const class_loader = h_new_class.Get()->GetClassLoader(); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5418 | ClassTable* const table = InsertClassTableForClassLoader(class_loader); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5419 | ObjPtr<mirror::Class> existing = table->UpdateClass(descriptor, h_new_class.Get(), |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5420 | ComputeModifiedUtf8Hash(descriptor)); |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 5421 | if (class_loader != nullptr) { |
| 5422 | // We updated the class in the class table, perform the write barrier so that the GC knows |
| 5423 | // about the change. |
Mathieu Chartier | 88ea61e | 2018-06-20 17:45:41 -0700 | [diff] [blame] | 5424 | WriteBarrier::ForEveryFieldWrite(class_loader); |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 5425 | } |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5426 | CHECK_EQ(existing, klass.Get()); |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 5427 | if (log_new_roots_) { |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5428 | new_class_roots_.push_back(GcRoot<mirror::Class>(h_new_class.Get())); |
| 5429 | } |
| 5430 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5431 | |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 5432 | // Update CHA info based on whether we override methods. |
| 5433 | // Have to do this before setting the class as resolved which allows |
| 5434 | // instantiation of klass. |
Andreas Gampe | c1ac9ee | 2017-07-24 22:35:49 -0700 | [diff] [blame] | 5435 | if (cha_ != nullptr) { |
| 5436 | cha_->UpdateAfterLoadingOf(h_new_class); |
| 5437 | } |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 5438 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5439 | // This will notify waiters on temp class that saw the not yet resolved class in the |
| 5440 | // class_table_ during EnsureResolved. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5441 | mirror::Class::SetStatus(klass, ClassStatus::kRetired, self); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5442 | |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5443 | CHECK_EQ(h_new_class->GetStatus(), ClassStatus::kResolving); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5444 | // This will notify waiters on new_class that saw the not yet resolved |
| 5445 | // class in the class_table_ during EnsureResolved. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5446 | mirror::Class::SetStatus(h_new_class, ClassStatus::kResolved, self); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 5447 | // Return the new class. |
| 5448 | h_new_class_out->Assign(h_new_class.Get()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5449 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5450 | return true; |
| 5451 | } |
| 5452 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5453 | bool ClassLinker::LoadSuperAndInterfaces(Handle<mirror::Class> klass, const DexFile& dex_file) { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5454 | CHECK_EQ(ClassStatus::kIdx, klass->GetStatus()); |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 5455 | const DexFile::ClassDef& class_def = dex_file.GetClassDef(klass->GetDexClassDefIndex()); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 5456 | dex::TypeIndex super_class_idx = class_def.superclass_idx_; |
| 5457 | if (super_class_idx.IsValid()) { |
Roland Levillain | 90328ac | 2016-05-18 12:25:38 +0100 | [diff] [blame] | 5458 | // Check that a class does not inherit from itself directly. |
| 5459 | // |
| 5460 | // TODO: This is a cheap check to detect the straightforward case |
| 5461 | // of a class extending itself (b/28685551), but we should do a |
| 5462 | // proper cycle detection on loaded classes, to detect all cases |
| 5463 | // of class circularity errors (b/28830038). |
| 5464 | if (super_class_idx == class_def.class_idx_) { |
| 5465 | ThrowClassCircularityError(klass.Get(), |
| 5466 | "Class %s extends itself", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5467 | klass->PrettyDescriptor().c_str()); |
Roland Levillain | 90328ac | 2016-05-18 12:25:38 +0100 | [diff] [blame] | 5468 | return false; |
| 5469 | } |
| 5470 | |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 5471 | ObjPtr<mirror::Class> super_class = ResolveType(super_class_idx, klass.Get()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5472 | if (super_class == nullptr) { |
Brian Carlstrom | 65ca077 | 2011-09-24 16:03:08 -0700 | [diff] [blame] | 5473 | DCHECK(Thread::Current()->IsExceptionPending()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5474 | return false; |
| 5475 | } |
Ian Rogers | be125a9 | 2012-01-11 15:19:49 -0800 | [diff] [blame] | 5476 | // Verify |
| 5477 | if (!klass->CanAccess(super_class)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5478 | ThrowIllegalAccessError(klass.Get(), "Class %s extended by class %s is inaccessible", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5479 | super_class->PrettyDescriptor().c_str(), |
| 5480 | klass->PrettyDescriptor().c_str()); |
Ian Rogers | be125a9 | 2012-01-11 15:19:49 -0800 | [diff] [blame] | 5481 | return false; |
| 5482 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5483 | CHECK(super_class->IsResolved()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5484 | klass->SetSuperClass(super_class); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5485 | } |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 5486 | const DexFile::TypeList* interfaces = dex_file.GetInterfacesList(class_def); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5487 | if (interfaces != nullptr) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 5488 | for (size_t i = 0; i < interfaces->Size(); i++) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 5489 | dex::TypeIndex idx = interfaces->GetTypeItem(i).type_idx_; |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 5490 | ObjPtr<mirror::Class> interface = ResolveType(idx, klass.Get()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5491 | if (interface == nullptr) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 5492 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 5493 | return false; |
| 5494 | } |
| 5495 | // Verify |
| 5496 | if (!klass->CanAccess(interface)) { |
| 5497 | // TODO: the RI seemed to ignore this in my testing. |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5498 | ThrowIllegalAccessError(klass.Get(), |
| 5499 | "Interface %s implemented by class %s is inaccessible", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5500 | interface->PrettyDescriptor().c_str(), |
| 5501 | klass->PrettyDescriptor().c_str()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 5502 | return false; |
| 5503 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5504 | } |
| 5505 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 5506 | // Mark the class as loaded. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5507 | mirror::Class::SetStatus(klass, ClassStatus::kLoaded, nullptr); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5508 | return true; |
| 5509 | } |
| 5510 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5511 | bool ClassLinker::LinkSuperClass(Handle<mirror::Class> klass) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5512 | CHECK(!klass->IsPrimitive()); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5513 | ObjPtr<mirror::Class> super = klass->GetSuperClass(); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 5514 | ObjPtr<mirror::Class> object_class = GetClassRoot<mirror::Object>(this); |
| 5515 | if (klass.Get() == object_class) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5516 | if (super != nullptr) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5517 | ThrowClassFormatError(klass.Get(), "java.lang.Object must not have a superclass"); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5518 | return false; |
| 5519 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5520 | return true; |
| 5521 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5522 | if (super == nullptr) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5523 | ThrowLinkageError(klass.Get(), "No superclass defined for class %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5524 | klass->PrettyDescriptor().c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5525 | return false; |
| 5526 | } |
| 5527 | // Verify |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 5528 | if (klass->IsInterface() && super != object_class) { |
Vladimir Marko | 1fcae9f | 2017-11-28 14:14:19 +0000 | [diff] [blame] | 5529 | ThrowClassFormatError(klass.Get(), "Interfaces must have java.lang.Object as superclass"); |
| 5530 | return false; |
| 5531 | } |
Vladimir Marko | b43b2d8 | 2017-07-18 17:46:38 +0100 | [diff] [blame] | 5532 | if (super->IsFinal()) { |
| 5533 | ThrowVerifyError(klass.Get(), |
| 5534 | "Superclass %s of %s is declared final", |
| 5535 | super->PrettyDescriptor().c_str(), |
| 5536 | klass->PrettyDescriptor().c_str()); |
| 5537 | return false; |
| 5538 | } |
| 5539 | if (super->IsInterface()) { |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5540 | ThrowIncompatibleClassChangeError(klass.Get(), |
Vladimir Marko | b43b2d8 | 2017-07-18 17:46:38 +0100 | [diff] [blame] | 5541 | "Superclass %s of %s is an interface", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5542 | super->PrettyDescriptor().c_str(), |
Vladimir Marko | b43b2d8 | 2017-07-18 17:46:38 +0100 | [diff] [blame] | 5543 | klass->PrettyDescriptor().c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5544 | return false; |
| 5545 | } |
| 5546 | if (!klass->CanAccess(super)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5547 | ThrowIllegalAccessError(klass.Get(), "Superclass %s is inaccessible to class %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5548 | super->PrettyDescriptor().c_str(), |
| 5549 | klass->PrettyDescriptor().c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5550 | return false; |
| 5551 | } |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 5552 | |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 5553 | // Inherit kAccClassIsFinalizable from the superclass in case this |
| 5554 | // class doesn't override finalize. |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 5555 | if (super->IsFinalizable()) { |
| 5556 | klass->SetFinalizable(); |
| 5557 | } |
| 5558 | |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 5559 | // Inherit class loader flag form super class. |
| 5560 | if (super->IsClassLoaderClass()) { |
| 5561 | klass->SetClassLoaderClass(); |
| 5562 | } |
| 5563 | |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 5564 | // Inherit reference flags (if any) from the superclass. |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 5565 | uint32_t reference_flags = (super->GetClassFlags() & mirror::kClassFlagReference); |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 5566 | if (reference_flags != 0) { |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 5567 | CHECK_EQ(klass->GetClassFlags(), 0u); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 5568 | klass->SetClassFlags(klass->GetClassFlags() | reference_flags); |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 5569 | } |
Elliott Hughes | 72ee0ae | 2011-10-10 17:31:28 -0700 | [diff] [blame] | 5570 | // Disallow custom direct subclasses of java.lang.ref.Reference. |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 5571 | if (init_done_ && super == GetClassRoot<mirror::Reference>(this)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5572 | ThrowLinkageError(klass.Get(), |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 5573 | "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] | 5574 | klass->PrettyDescriptor().c_str()); |
Elliott Hughes | 72ee0ae | 2011-10-10 17:31:28 -0700 | [diff] [blame] | 5575 | return false; |
| 5576 | } |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 5577 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 5578 | if (kIsDebugBuild) { |
| 5579 | // Ensure super classes are fully resolved prior to resolving fields.. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5580 | while (super != nullptr) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 5581 | CHECK(super->IsResolved()); |
| 5582 | super = super->GetSuperClass(); |
| 5583 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5584 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5585 | return true; |
| 5586 | } |
| 5587 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5588 | // Populate the class vtable and itable. Compute return type indices. |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5589 | bool ClassLinker::LinkMethods(Thread* self, |
| 5590 | Handle<mirror::Class> klass, |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5591 | Handle<mirror::ObjectArray<mirror::Class>> interfaces, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5592 | bool* out_new_conflict, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5593 | ArtMethod** out_imt) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5594 | self->AllowThreadSuspension(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5595 | // A map from vtable indexes to the method they need to be updated to point to. Used because we |
| 5596 | // need to have default methods be in the virtuals array of each class but we don't set that up |
| 5597 | // until LinkInterfaceMethods. |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5598 | std::unordered_map<size_t, ClassLinker::MethodTranslation> default_translations; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5599 | // Link virtual methods then interface methods. |
| 5600 | // We set up the interface lookup table first because we need it to determine if we need to update |
| 5601 | // any vtable entries with new default method implementations. |
| 5602 | return SetupInterfaceLookupTable(self, klass, interfaces) |
| 5603 | && LinkVirtualMethods(self, klass, /*out*/ &default_translations) |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5604 | && LinkInterfaceMethods(self, klass, default_translations, out_new_conflict, out_imt); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5605 | } |
| 5606 | |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 5607 | // Comparator for name and signature of a method, used in finding overriding methods. Implementation |
| 5608 | // avoids the use of handles, if it didn't then rather than compare dex files we could compare dex |
| 5609 | // caches in the implementation below. |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 5610 | class MethodNameAndSignatureComparator final : public ValueObject { |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 5611 | public: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5612 | explicit MethodNameAndSignatureComparator(ArtMethod* method) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5613 | REQUIRES_SHARED(Locks::mutator_lock_) : |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 5614 | dex_file_(method->GetDexFile()), mid_(&dex_file_->GetMethodId(method->GetDexMethodIndex())), |
| 5615 | name_(nullptr), name_len_(0) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5616 | DCHECK(!method->IsProxyMethod()) << method->PrettyMethod(); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5617 | } |
| 5618 | |
| 5619 | const char* GetName() { |
| 5620 | if (name_ == nullptr) { |
| 5621 | name_ = dex_file_->StringDataAndUtf16LengthByIdx(mid_->name_idx_, &name_len_); |
| 5622 | } |
| 5623 | return name_; |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 5624 | } |
| 5625 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5626 | bool HasSameNameAndSignature(ArtMethod* other) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5627 | REQUIRES_SHARED(Locks::mutator_lock_) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5628 | DCHECK(!other->IsProxyMethod()) << other->PrettyMethod(); |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 5629 | const DexFile* other_dex_file = other->GetDexFile(); |
| 5630 | const DexFile::MethodId& other_mid = other_dex_file->GetMethodId(other->GetDexMethodIndex()); |
| 5631 | if (dex_file_ == other_dex_file) { |
| 5632 | return mid_->name_idx_ == other_mid.name_idx_ && mid_->proto_idx_ == other_mid.proto_idx_; |
| 5633 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5634 | GetName(); // Only used to make sure its calculated. |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 5635 | uint32_t other_name_len; |
| 5636 | const char* other_name = other_dex_file->StringDataAndUtf16LengthByIdx(other_mid.name_idx_, |
| 5637 | &other_name_len); |
| 5638 | if (name_len_ != other_name_len || strcmp(name_, other_name) != 0) { |
| 5639 | return false; |
| 5640 | } |
| 5641 | return dex_file_->GetMethodSignature(*mid_) == other_dex_file->GetMethodSignature(other_mid); |
| 5642 | } |
| 5643 | |
| 5644 | private: |
| 5645 | // Dex file for the method to compare against. |
| 5646 | const DexFile* const dex_file_; |
| 5647 | // MethodId for the method to compare against. |
| 5648 | const DexFile::MethodId* const mid_; |
| 5649 | // Lazily computed name from the dex file's strings. |
| 5650 | const char* name_; |
| 5651 | // Lazily computed name length. |
| 5652 | uint32_t name_len_; |
| 5653 | }; |
| 5654 | |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5655 | class LinkVirtualHashTable { |
| 5656 | public: |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5657 | LinkVirtualHashTable(Handle<mirror::Class> klass, |
| 5658 | size_t hash_size, |
| 5659 | uint32_t* hash_table, |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 5660 | PointerSize image_pointer_size) |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5661 | : klass_(klass), |
| 5662 | hash_size_(hash_size), |
| 5663 | hash_table_(hash_table), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5664 | image_pointer_size_(image_pointer_size) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5665 | std::fill(hash_table_, hash_table_ + hash_size_, invalid_index_); |
| 5666 | } |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5667 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5668 | void Add(uint32_t virtual_method_index) REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5669 | ArtMethod* local_method = klass_->GetVirtualMethodDuringLinking( |
| 5670 | virtual_method_index, image_pointer_size_); |
| 5671 | const char* name = local_method->GetInterfaceMethodIfProxy(image_pointer_size_)->GetName(); |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 5672 | uint32_t hash = ComputeModifiedUtf8Hash(name); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5673 | uint32_t index = hash % hash_size_; |
| 5674 | // Linear probe until we have an empty slot. |
| 5675 | while (hash_table_[index] != invalid_index_) { |
| 5676 | if (++index == hash_size_) { |
| 5677 | index = 0; |
| 5678 | } |
| 5679 | } |
| 5680 | hash_table_[index] = virtual_method_index; |
| 5681 | } |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5682 | |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5683 | uint32_t FindAndRemove(MethodNameAndSignatureComparator* comparator) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5684 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5685 | const char* name = comparator->GetName(); |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 5686 | uint32_t hash = ComputeModifiedUtf8Hash(name); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5687 | size_t index = hash % hash_size_; |
| 5688 | while (true) { |
| 5689 | const uint32_t value = hash_table_[index]; |
| 5690 | // Since linear probe makes continuous blocks, hitting an invalid index means we are done |
| 5691 | // the block and can safely assume not found. |
| 5692 | if (value == invalid_index_) { |
| 5693 | break; |
| 5694 | } |
| 5695 | if (value != removed_index_) { // This signifies not already overriden. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5696 | ArtMethod* virtual_method = |
| 5697 | klass_->GetVirtualMethodDuringLinking(value, image_pointer_size_); |
| 5698 | if (comparator->HasSameNameAndSignature( |
| 5699 | virtual_method->GetInterfaceMethodIfProxy(image_pointer_size_))) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5700 | hash_table_[index] = removed_index_; |
| 5701 | return value; |
| 5702 | } |
| 5703 | } |
| 5704 | if (++index == hash_size_) { |
| 5705 | index = 0; |
| 5706 | } |
| 5707 | } |
| 5708 | return GetNotFoundIndex(); |
| 5709 | } |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5710 | |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5711 | static uint32_t GetNotFoundIndex() { |
| 5712 | return invalid_index_; |
| 5713 | } |
| 5714 | |
| 5715 | private: |
| 5716 | static const uint32_t invalid_index_; |
| 5717 | static const uint32_t removed_index_; |
| 5718 | |
| 5719 | Handle<mirror::Class> klass_; |
| 5720 | const size_t hash_size_; |
| 5721 | uint32_t* const hash_table_; |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 5722 | const PointerSize image_pointer_size_; |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5723 | }; |
| 5724 | |
| 5725 | const uint32_t LinkVirtualHashTable::invalid_index_ = std::numeric_limits<uint32_t>::max(); |
| 5726 | const uint32_t LinkVirtualHashTable::removed_index_ = std::numeric_limits<uint32_t>::max() - 1; |
| 5727 | |
Stephen Hines | 1ddd913 | 2017-02-08 01:51:18 -0800 | [diff] [blame] | 5728 | bool ClassLinker::LinkVirtualMethods( |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5729 | Thread* self, |
| 5730 | Handle<mirror::Class> klass, |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5731 | /*out*/std::unordered_map<size_t, ClassLinker::MethodTranslation>* default_translations) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5732 | const size_t num_virtual_methods = klass->NumVirtualMethods(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5733 | if (klass->IsInterface()) { |
| 5734 | // No vtable. |
| 5735 | if (!IsUint<16>(num_virtual_methods)) { |
| 5736 | ThrowClassFormatError(klass.Get(), "Too many methods on interface: %zu", num_virtual_methods); |
| 5737 | return false; |
| 5738 | } |
| 5739 | bool has_defaults = false; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5740 | // Assign each method an IMT index and set the default flag. |
| 5741 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
| 5742 | ArtMethod* m = klass->GetVirtualMethodDuringLinking(i, image_pointer_size_); |
| 5743 | m->SetMethodIndex(i); |
| 5744 | if (!m->IsAbstract()) { |
| 5745 | m->SetAccessFlags(m->GetAccessFlags() | kAccDefault); |
| 5746 | has_defaults = true; |
| 5747 | } |
| 5748 | } |
| 5749 | // Mark that we have default methods so that we won't need to scan the virtual_methods_ array |
| 5750 | // during initialization. This is a performance optimization. We could simply traverse the |
| 5751 | // virtual_methods_ array again during initialization. |
| 5752 | if (has_defaults) { |
| 5753 | klass->SetHasDefaultMethods(); |
| 5754 | } |
| 5755 | return true; |
| 5756 | } else if (klass->HasSuperClass()) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5757 | const size_t super_vtable_length = klass->GetSuperClass()->GetVTableLength(); |
| 5758 | const size_t max_count = num_virtual_methods + super_vtable_length; |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5759 | StackHandleScope<2> hs(self); |
Mingyao Yang | 38eecb0 | 2014-08-13 14:51:03 -0700 | [diff] [blame] | 5760 | Handle<mirror::Class> super_class(hs.NewHandle(klass->GetSuperClass())); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5761 | MutableHandle<mirror::PointerArray> vtable; |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5762 | if (super_class->ShouldHaveEmbeddedVTable()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5763 | vtable = hs.NewHandle(AllocPointerArray(self, max_count)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 5764 | if (UNLIKELY(vtable == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5765 | self->AssertPendingOOMException(); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 5766 | return false; |
| 5767 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5768 | for (size_t i = 0; i < super_vtable_length; i++) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5769 | vtable->SetElementPtrSize( |
| 5770 | i, super_class->GetEmbeddedVTableEntry(i, image_pointer_size_), image_pointer_size_); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 5771 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5772 | // We might need to change vtable if we have new virtual methods or new interfaces (since that |
| 5773 | // might give us new default methods). If no new interfaces then we can skip the rest since |
| 5774 | // the class cannot override any of the super-class's methods. This is required for |
| 5775 | // correctness since without it we might not update overridden default method vtable entries |
| 5776 | // correctly. |
| 5777 | if (num_virtual_methods == 0 && super_class->GetIfTableCount() == klass->GetIfTableCount()) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5778 | klass->SetVTable(vtable.Get()); |
| 5779 | return true; |
| 5780 | } |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 5781 | } else { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5782 | DCHECK(super_class->IsAbstract() && !super_class->IsArrayClass()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5783 | auto* super_vtable = super_class->GetVTable(); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5784 | CHECK(super_vtable != nullptr) << super_class->PrettyClass(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5785 | // We might need to change vtable if we have new virtual methods or new interfaces (since that |
| 5786 | // might give us new default methods). See comment above. |
| 5787 | if (num_virtual_methods == 0 && super_class->GetIfTableCount() == klass->GetIfTableCount()) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5788 | klass->SetVTable(super_vtable); |
| 5789 | return true; |
| 5790 | } |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 5791 | vtable = hs.NewHandle( |
| 5792 | ObjPtr<mirror::PointerArray>::DownCast(super_vtable->CopyOf(self, max_count))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 5793 | if (UNLIKELY(vtable == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5794 | self->AssertPendingOOMException(); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 5795 | return false; |
| 5796 | } |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 5797 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5798 | // How the algorithm works: |
| 5799 | // 1. Populate hash table by adding num_virtual_methods from klass. The values in the hash |
| 5800 | // table are: invalid_index for unused slots, index super_vtable_length + i for a virtual |
| 5801 | // method which has not been matched to a vtable method, and j if the virtual method at the |
| 5802 | // index overrode the super virtual method at index j. |
| 5803 | // 2. Loop through super virtual methods, if they overwrite, update hash table to j |
| 5804 | // (j < super_vtable_length) to avoid redundant checks. (TODO maybe use this info for reducing |
| 5805 | // the need for the initial vtable which we later shrink back down). |
| 5806 | // 3. Add non overridden methods to the end of the vtable. |
| 5807 | static constexpr size_t kMaxStackHash = 250; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5808 | // + 1 so that even if we only have new default methods we will still be able to use this hash |
| 5809 | // table (i.e. it will never have 0 size). |
| 5810 | const size_t hash_table_size = num_virtual_methods * 3 + 1; |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5811 | uint32_t* hash_table_ptr; |
| 5812 | std::unique_ptr<uint32_t[]> hash_heap_storage; |
| 5813 | if (hash_table_size <= kMaxStackHash) { |
| 5814 | hash_table_ptr = reinterpret_cast<uint32_t*>( |
| 5815 | alloca(hash_table_size * sizeof(*hash_table_ptr))); |
| 5816 | } else { |
| 5817 | hash_heap_storage.reset(new uint32_t[hash_table_size]); |
| 5818 | hash_table_ptr = hash_heap_storage.get(); |
| 5819 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5820 | 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] | 5821 | // Add virtual methods to the hash table. |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5822 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5823 | DCHECK(klass->GetVirtualMethodDuringLinking( |
| 5824 | i, image_pointer_size_)->GetDeclaringClass() != nullptr); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5825 | hash_table.Add(i); |
| 5826 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5827 | // 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] | 5828 | // the hash table. |
| 5829 | for (size_t j = 0; j < super_vtable_length; ++j) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5830 | // 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] | 5831 | ArtMethod* super_method = vtable->GetElementPtrSize<ArtMethod*>(j, image_pointer_size_); |
Alex Light | c7a420c | 2016-10-18 14:33:18 -0700 | [diff] [blame] | 5832 | if (!klass->CanAccessMember(super_method->GetDeclaringClass(), |
| 5833 | super_method->GetAccessFlags())) { |
| 5834 | // Continue on to the next method since this one is package private and canot be overridden. |
| 5835 | // Before Android 4.1, the package-private method super_method might have been incorrectly |
| 5836 | // overridden. |
| 5837 | continue; |
| 5838 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5839 | MethodNameAndSignatureComparator super_method_name_comparator( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5840 | super_method->GetInterfaceMethodIfProxy(image_pointer_size_)); |
Alex Light | c7a420c | 2016-10-18 14:33:18 -0700 | [diff] [blame] | 5841 | // We remove the method so that subsequent lookups will be faster by making the hash-map |
| 5842 | // smaller as we go on. |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5843 | uint32_t hash_index = hash_table.FindAndRemove(&super_method_name_comparator); |
| 5844 | if (hash_index != hash_table.GetNotFoundIndex()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5845 | ArtMethod* virtual_method = klass->GetVirtualMethodDuringLinking( |
| 5846 | hash_index, image_pointer_size_); |
Alex Light | c7a420c | 2016-10-18 14:33:18 -0700 | [diff] [blame] | 5847 | if (super_method->IsFinal()) { |
| 5848 | ThrowLinkageError(klass.Get(), "Method %s overrides final method in class %s", |
| 5849 | virtual_method->PrettyMethod().c_str(), |
| 5850 | super_method->GetDeclaringClassDescriptor()); |
| 5851 | return false; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5852 | } |
Alex Light | c7a420c | 2016-10-18 14:33:18 -0700 | [diff] [blame] | 5853 | vtable->SetElementPtrSize(j, virtual_method, image_pointer_size_); |
| 5854 | virtual_method->SetMethodIndex(j); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5855 | } else if (super_method->IsOverridableByDefaultMethod()) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5856 | // We didn't directly override this method but we might through default methods... |
| 5857 | // Check for default method update. |
| 5858 | ArtMethod* default_method = nullptr; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5859 | switch (FindDefaultMethodImplementation(self, |
| 5860 | super_method, |
| 5861 | klass, |
| 5862 | /*out*/&default_method)) { |
| 5863 | case DefaultMethodSearchResult::kDefaultConflict: { |
| 5864 | // A conflict was found looking for default methods. Note this (assuming it wasn't |
| 5865 | // pre-existing) in the translations map. |
| 5866 | if (UNLIKELY(!super_method->IsDefaultConflicting())) { |
| 5867 | // Don't generate another conflict method to reduce memory use as an optimization. |
| 5868 | default_translations->insert( |
| 5869 | {j, ClassLinker::MethodTranslation::CreateConflictingMethod()}); |
| 5870 | } |
| 5871 | break; |
| 5872 | } |
| 5873 | case DefaultMethodSearchResult::kAbstractFound: { |
| 5874 | // No conflict but method is abstract. |
| 5875 | // We note that this vtable entry must be made abstract. |
| 5876 | if (UNLIKELY(!super_method->IsAbstract())) { |
| 5877 | default_translations->insert( |
| 5878 | {j, ClassLinker::MethodTranslation::CreateAbstractMethod()}); |
| 5879 | } |
| 5880 | break; |
| 5881 | } |
| 5882 | case DefaultMethodSearchResult::kDefaultFound: { |
| 5883 | if (UNLIKELY(super_method->IsDefaultConflicting() || |
| 5884 | default_method->GetDeclaringClass() != super_method->GetDeclaringClass())) { |
| 5885 | // Found a default method implementation that is new. |
| 5886 | // TODO Refactor this add default methods to virtuals here and not in |
| 5887 | // LinkInterfaceMethods maybe. |
| 5888 | // The problem is default methods might override previously present |
| 5889 | // default-method or miranda-method vtable entries from the superclass. |
| 5890 | // Unfortunately we need these to be entries in this class's virtuals. We do not |
| 5891 | // give these entries there until LinkInterfaceMethods so we pass this map around |
| 5892 | // to let it know which vtable entries need to be updated. |
| 5893 | // Make a note that vtable entry j must be updated, store what it needs to be updated |
| 5894 | // to. We will allocate a virtual method slot in LinkInterfaceMethods and fix it up |
| 5895 | // then. |
| 5896 | default_translations->insert( |
| 5897 | {j, ClassLinker::MethodTranslation::CreateTranslatedMethod(default_method)}); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5898 | VLOG(class_linker) << "Method " << super_method->PrettyMethod() |
| 5899 | << " overridden by default " |
| 5900 | << default_method->PrettyMethod() |
| 5901 | << " in " << mirror::Class::PrettyClass(klass.Get()); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5902 | } |
| 5903 | break; |
| 5904 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5905 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5906 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5907 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5908 | size_t actual_count = super_vtable_length; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5909 | // Add the non-overridden methods at the end. |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5910 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5911 | ArtMethod* local_method = klass->GetVirtualMethodDuringLinking(i, image_pointer_size_); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5912 | size_t method_idx = local_method->GetMethodIndexDuringLinking(); |
| 5913 | if (method_idx < super_vtable_length && |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5914 | local_method == vtable->GetElementPtrSize<ArtMethod*>(method_idx, image_pointer_size_)) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5915 | continue; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5916 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5917 | vtable->SetElementPtrSize(actual_count, local_method, image_pointer_size_); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5918 | local_method->SetMethodIndex(actual_count); |
| 5919 | ++actual_count; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5920 | } |
Andreas Gampe | ab1eb0d | 2015-02-13 19:23:55 -0800 | [diff] [blame] | 5921 | if (!IsUint<16>(actual_count)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5922 | ThrowClassFormatError(klass.Get(), "Too many methods defined on class: %zd", actual_count); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5923 | return false; |
| 5924 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5925 | // Shrink vtable if possible |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5926 | CHECK_LE(actual_count, max_count); |
| 5927 | if (actual_count < max_count) { |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 5928 | vtable.Assign(ObjPtr<mirror::PointerArray>::DownCast(vtable->CopyOf(self, actual_count))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 5929 | if (UNLIKELY(vtable == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5930 | self->AssertPendingOOMException(); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 5931 | return false; |
| 5932 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5933 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5934 | klass->SetVTable(vtable.Get()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5935 | } else { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 5936 | CHECK_EQ(klass.Get(), GetClassRoot<mirror::Object>(this)); |
Andreas Gampe | ab1eb0d | 2015-02-13 19:23:55 -0800 | [diff] [blame] | 5937 | if (!IsUint<16>(num_virtual_methods)) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5938 | ThrowClassFormatError(klass.Get(), "Too many methods: %d", |
| 5939 | static_cast<int>(num_virtual_methods)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5940 | return false; |
| 5941 | } |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 5942 | ObjPtr<mirror::PointerArray> vtable = AllocPointerArray(self, num_virtual_methods); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5943 | if (UNLIKELY(vtable == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5944 | self->AssertPendingOOMException(); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 5945 | return false; |
| 5946 | } |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 5947 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5948 | ArtMethod* virtual_method = klass->GetVirtualMethodDuringLinking(i, image_pointer_size_); |
| 5949 | vtable->SetElementPtrSize(i, virtual_method, image_pointer_size_); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5950 | virtual_method->SetMethodIndex(i & 0xFFFF); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5951 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 5952 | klass->SetVTable(vtable); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5953 | } |
| 5954 | return true; |
| 5955 | } |
| 5956 | |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5957 | // Determine if the given iface has any subinterface in the given list that declares the method |
| 5958 | // specified by 'target'. |
| 5959 | // |
| 5960 | // Arguments |
| 5961 | // - self: The thread we are running on |
| 5962 | // - target: A comparator that will match any method that overrides the method we are checking for |
| 5963 | // - iftable: The iftable we are searching for an overriding method on. |
| 5964 | // - ifstart: The index of the interface we are checking to see if anything overrides |
| 5965 | // - iface: The interface we are checking to see if anything overrides. |
| 5966 | // - image_pointer_size: |
| 5967 | // The image pointer size. |
| 5968 | // |
| 5969 | // Returns |
| 5970 | // - True: There is some method that matches the target comparator defined in an interface that |
| 5971 | // is a subtype of iface. |
| 5972 | // - False: There is no method that matches the target comparator in any interface that is a subtype |
| 5973 | // of iface. |
| 5974 | static bool ContainsOverridingMethodOf(Thread* self, |
| 5975 | MethodNameAndSignatureComparator& target, |
| 5976 | Handle<mirror::IfTable> iftable, |
| 5977 | size_t ifstart, |
| 5978 | Handle<mirror::Class> iface, |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 5979 | PointerSize image_pointer_size) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5980 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5981 | DCHECK(self != nullptr); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 5982 | DCHECK(iface != nullptr); |
| 5983 | DCHECK(iftable != nullptr); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5984 | DCHECK_GE(ifstart, 0u); |
| 5985 | DCHECK_LT(ifstart, iftable->Count()); |
| 5986 | DCHECK_EQ(iface.Get(), iftable->GetInterface(ifstart)); |
| 5987 | DCHECK(iface->IsInterface()); |
| 5988 | |
| 5989 | size_t iftable_count = iftable->Count(); |
| 5990 | StackHandleScope<1> hs(self); |
| 5991 | MutableHandle<mirror::Class> current_iface(hs.NewHandle<mirror::Class>(nullptr)); |
| 5992 | for (size_t k = ifstart + 1; k < iftable_count; k++) { |
| 5993 | // Skip ifstart since our current interface obviously cannot override itself. |
| 5994 | current_iface.Assign(iftable->GetInterface(k)); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 5995 | // Iterate through every method on this interface. The order does not matter. |
| 5996 | for (ArtMethod& current_method : current_iface->GetDeclaredVirtualMethods(image_pointer_size)) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5997 | if (UNLIKELY(target.HasSameNameAndSignature( |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 5998 | current_method.GetInterfaceMethodIfProxy(image_pointer_size)))) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5999 | // Check if the i'th interface is a subtype of this one. |
| 6000 | if (iface->IsAssignableFrom(current_iface.Get())) { |
| 6001 | return true; |
| 6002 | } |
| 6003 | break; |
| 6004 | } |
| 6005 | } |
| 6006 | } |
| 6007 | return false; |
| 6008 | } |
| 6009 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6010 | // 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] | 6011 | // out_default_method and returns kDefaultFound on success. If no default method was found return |
| 6012 | // kAbstractFound and store nullptr into out_default_method. If an error occurs (such as a |
| 6013 | // default_method conflict) it will return kDefaultConflict. |
| 6014 | ClassLinker::DefaultMethodSearchResult ClassLinker::FindDefaultMethodImplementation( |
| 6015 | Thread* self, |
| 6016 | ArtMethod* target_method, |
| 6017 | Handle<mirror::Class> klass, |
| 6018 | /*out*/ArtMethod** out_default_method) const { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6019 | DCHECK(self != nullptr); |
| 6020 | DCHECK(target_method != nullptr); |
| 6021 | DCHECK(out_default_method != nullptr); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6022 | |
| 6023 | *out_default_method = nullptr; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6024 | |
| 6025 | // We organize the interface table so that, for interface I any subinterfaces J follow it in the |
| 6026 | // table. This lets us walk the table backwards when searching for default methods. The first one |
| 6027 | // we encounter is the best candidate since it is the most specific. Once we have found it we keep |
| 6028 | // track of it and then continue checking all other interfaces, since we need to throw an error if |
| 6029 | // we encounter conflicting default method implementations (one is not a subtype of the other). |
| 6030 | // |
| 6031 | // The order of unrelated interfaces does not matter and is not defined. |
| 6032 | size_t iftable_count = klass->GetIfTableCount(); |
| 6033 | if (iftable_count == 0) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6034 | // No interfaces. We have already reset out to null so just return kAbstractFound. |
| 6035 | return DefaultMethodSearchResult::kAbstractFound; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6036 | } |
| 6037 | |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6038 | StackHandleScope<3> hs(self); |
| 6039 | MutableHandle<mirror::Class> chosen_iface(hs.NewHandle<mirror::Class>(nullptr)); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6040 | MutableHandle<mirror::IfTable> iftable(hs.NewHandle(klass->GetIfTable())); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6041 | MutableHandle<mirror::Class> iface(hs.NewHandle<mirror::Class>(nullptr)); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6042 | MethodNameAndSignatureComparator target_name_comparator( |
| 6043 | target_method->GetInterfaceMethodIfProxy(image_pointer_size_)); |
| 6044 | // Iterates over the klass's iftable in reverse |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6045 | for (size_t k = iftable_count; k != 0; ) { |
| 6046 | --k; |
| 6047 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6048 | DCHECK_LT(k, iftable->Count()); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6049 | |
| 6050 | iface.Assign(iftable->GetInterface(k)); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6051 | // Iterate through every declared method on this interface. The order does not matter. |
| 6052 | for (auto& method_iter : iface->GetDeclaredVirtualMethods(image_pointer_size_)) { |
| 6053 | ArtMethod* current_method = &method_iter; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6054 | // Skip abstract methods and methods with different names. |
| 6055 | if (current_method->IsAbstract() || |
| 6056 | !target_name_comparator.HasSameNameAndSignature( |
| 6057 | current_method->GetInterfaceMethodIfProxy(image_pointer_size_))) { |
| 6058 | continue; |
Alex Light | d7c10c2 | 2016-03-31 10:03:07 -0700 | [diff] [blame] | 6059 | } else if (!current_method->IsPublic()) { |
| 6060 | // The verifier should have caught the non-public method for dex version 37. Just warn and |
| 6061 | // skip it since this is from before default-methods so we don't really need to care that it |
| 6062 | // has code. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6063 | LOG(WARNING) << "Interface method " << current_method->PrettyMethod() |
| 6064 | << " is not public! " |
Alex Light | d7c10c2 | 2016-03-31 10:03:07 -0700 | [diff] [blame] | 6065 | << "This will be a fatal error in subsequent versions of android. " |
| 6066 | << "Continuing anyway."; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6067 | } |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 6068 | if (UNLIKELY(chosen_iface != nullptr)) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6069 | // We have multiple default impls of the same method. This is a potential default conflict. |
| 6070 | // We need to check if this possibly conflicting method is either a superclass of the chosen |
| 6071 | // default implementation or is overridden by a non-default interface method. In either case |
| 6072 | // there is no conflict. |
| 6073 | if (!iface->IsAssignableFrom(chosen_iface.Get()) && |
| 6074 | !ContainsOverridingMethodOf(self, |
| 6075 | target_name_comparator, |
| 6076 | iftable, |
| 6077 | k, |
| 6078 | iface, |
| 6079 | image_pointer_size_)) { |
Nicolas Geoffray | 7f3e0db | 2016-01-28 09:29:31 +0000 | [diff] [blame] | 6080 | VLOG(class_linker) << "Conflicting default method implementations found: " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6081 | << current_method->PrettyMethod() << " and " |
| 6082 | << ArtMethod::PrettyMethod(*out_default_method) << " in class " |
| 6083 | << klass->PrettyClass() << " conflict."; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6084 | *out_default_method = nullptr; |
| 6085 | return DefaultMethodSearchResult::kDefaultConflict; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6086 | } else { |
| 6087 | break; // Continue checking at the next interface. |
| 6088 | } |
| 6089 | } else { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6090 | // chosen_iface == null |
| 6091 | if (!ContainsOverridingMethodOf(self, |
| 6092 | target_name_comparator, |
| 6093 | iftable, |
| 6094 | k, |
| 6095 | iface, |
| 6096 | image_pointer_size_)) { |
| 6097 | // Don't set this as the chosen interface if something else is overriding it (because that |
| 6098 | // other interface would be potentially chosen instead if it was default). If the other |
| 6099 | // interface was abstract then we wouldn't select this interface as chosen anyway since |
| 6100 | // the abstract method masks it. |
| 6101 | *out_default_method = current_method; |
| 6102 | chosen_iface.Assign(iface.Get()); |
| 6103 | // We should now finish traversing the graph to find if we have default methods that |
| 6104 | // conflict. |
| 6105 | } else { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6106 | VLOG(class_linker) << "A default method '" << current_method->PrettyMethod() |
| 6107 | << "' was " |
| 6108 | << "skipped because it was overridden by an abstract method in a " |
| 6109 | << "subinterface on class '" << klass->PrettyClass() << "'"; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6110 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6111 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6112 | break; |
| 6113 | } |
| 6114 | } |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6115 | if (*out_default_method != nullptr) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6116 | VLOG(class_linker) << "Default method '" << (*out_default_method)->PrettyMethod() |
| 6117 | << "' selected " |
| 6118 | << "as the implementation for '" << target_method->PrettyMethod() |
| 6119 | << "' in '" << klass->PrettyClass() << "'"; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6120 | return DefaultMethodSearchResult::kDefaultFound; |
| 6121 | } else { |
| 6122 | return DefaultMethodSearchResult::kAbstractFound; |
| 6123 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6124 | } |
| 6125 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6126 | ArtMethod* ClassLinker::AddMethodToConflictTable(ObjPtr<mirror::Class> klass, |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6127 | ArtMethod* conflict_method, |
| 6128 | ArtMethod* interface_method, |
| 6129 | ArtMethod* method, |
| 6130 | bool force_new_conflict_method) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 6131 | ImtConflictTable* current_table = conflict_method->GetImtConflictTable(kRuntimePointerSize); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6132 | Runtime* const runtime = Runtime::Current(); |
| 6133 | LinearAlloc* linear_alloc = GetAllocatorForClassLoader(klass->GetClassLoader()); |
| 6134 | bool new_entry = conflict_method == runtime->GetImtConflictMethod() || force_new_conflict_method; |
| 6135 | |
| 6136 | // Create a new entry if the existing one is the shared conflict method. |
| 6137 | ArtMethod* new_conflict_method = new_entry |
| 6138 | ? runtime->CreateImtConflictMethod(linear_alloc) |
| 6139 | : conflict_method; |
| 6140 | |
| 6141 | // Allocate a new table. Note that we will leak this table at the next conflict, |
| 6142 | // but that's a tradeoff compared to making the table fixed size. |
| 6143 | void* data = linear_alloc->Alloc( |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6144 | Thread::Current(), ImtConflictTable::ComputeSizeWithOneMoreEntry(current_table, |
| 6145 | image_pointer_size_)); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6146 | if (data == nullptr) { |
| 6147 | LOG(ERROR) << "Failed to allocate conflict table"; |
| 6148 | return conflict_method; |
| 6149 | } |
| 6150 | ImtConflictTable* new_table = new (data) ImtConflictTable(current_table, |
| 6151 | interface_method, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6152 | method, |
| 6153 | image_pointer_size_); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6154 | |
| 6155 | // Do a fence to ensure threads see the data in the table before it is assigned |
| 6156 | // to the conflict method. |
| 6157 | // Note that there is a race in the presence of multiple threads and we may leak |
| 6158 | // memory from the LinearAlloc, but that's a tradeoff compared to using |
| 6159 | // atomic operations. |
Orion Hodson | 27b9676 | 2018-03-13 16:06:57 +0000 | [diff] [blame] | 6160 | std::atomic_thread_fence(std::memory_order_release); |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6161 | new_conflict_method->SetImtConflictTable(new_table, image_pointer_size_); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6162 | return new_conflict_method; |
| 6163 | } |
| 6164 | |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 6165 | bool ClassLinker::AllocateIfTableMethodArrays(Thread* self, |
| 6166 | Handle<mirror::Class> klass, |
| 6167 | Handle<mirror::IfTable> iftable) { |
| 6168 | DCHECK(!klass->IsInterface()); |
| 6169 | const bool has_superclass = klass->HasSuperClass(); |
| 6170 | const bool extend_super_iftable = has_superclass; |
| 6171 | const size_t ifcount = klass->GetIfTableCount(); |
| 6172 | const size_t super_ifcount = has_superclass ? klass->GetSuperClass()->GetIfTableCount() : 0U; |
| 6173 | for (size_t i = 0; i < ifcount; ++i) { |
| 6174 | size_t num_methods = iftable->GetInterface(i)->NumDeclaredVirtualMethods(); |
| 6175 | if (num_methods > 0) { |
| 6176 | const bool is_super = i < super_ifcount; |
| 6177 | // This is an interface implemented by a super-class. Therefore we can just copy the method |
| 6178 | // array from the superclass. |
| 6179 | const bool super_interface = is_super && extend_super_iftable; |
| 6180 | ObjPtr<mirror::PointerArray> method_array; |
| 6181 | if (super_interface) { |
| 6182 | ObjPtr<mirror::IfTable> if_table = klass->GetSuperClass()->GetIfTable(); |
| 6183 | DCHECK(if_table != nullptr); |
| 6184 | DCHECK(if_table->GetMethodArray(i) != nullptr); |
| 6185 | // If we are working on a super interface, try extending the existing method array. |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 6186 | method_array = ObjPtr<mirror::PointerArray>::DownCast(MakeObjPtr( |
| 6187 | if_table->GetMethodArray(i)->Clone(self))); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 6188 | } else { |
| 6189 | method_array = AllocPointerArray(self, num_methods); |
| 6190 | } |
| 6191 | if (UNLIKELY(method_array == nullptr)) { |
| 6192 | self->AssertPendingOOMException(); |
| 6193 | return false; |
| 6194 | } |
| 6195 | iftable->SetMethodArray(i, method_array); |
| 6196 | } |
| 6197 | } |
| 6198 | return true; |
| 6199 | } |
| 6200 | |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6201 | void ClassLinker::SetIMTRef(ArtMethod* unimplemented_method, |
| 6202 | ArtMethod* imt_conflict_method, |
| 6203 | ArtMethod* current_method, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6204 | /*out*/bool* new_conflict, |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6205 | /*out*/ArtMethod** imt_ref) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6206 | // Place method in imt if entry is empty, place conflict otherwise. |
| 6207 | if (*imt_ref == unimplemented_method) { |
| 6208 | *imt_ref = current_method; |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 6209 | } else if (!(*imt_ref)->IsRuntimeMethod()) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6210 | // If we are not a conflict and we have the same signature and name as the imt |
| 6211 | // entry, it must be that we overwrote a superclass vtable entry. |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 6212 | // Note that we have checked IsRuntimeMethod, as there may be multiple different |
| 6213 | // conflict methods. |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6214 | MethodNameAndSignatureComparator imt_comparator( |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6215 | (*imt_ref)->GetInterfaceMethodIfProxy(image_pointer_size_)); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6216 | if (imt_comparator.HasSameNameAndSignature( |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6217 | current_method->GetInterfaceMethodIfProxy(image_pointer_size_))) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6218 | *imt_ref = current_method; |
| 6219 | } else { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6220 | *imt_ref = imt_conflict_method; |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6221 | *new_conflict = true; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6222 | } |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 6223 | } else { |
| 6224 | // Place the default conflict method. Note that there may be an existing conflict |
| 6225 | // method in the IMT, but it could be one tailored to the super class, with a |
| 6226 | // specific ImtConflictTable. |
| 6227 | *imt_ref = imt_conflict_method; |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6228 | *new_conflict = true; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6229 | } |
| 6230 | } |
| 6231 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6232 | void ClassLinker::FillIMTAndConflictTables(ObjPtr<mirror::Class> klass) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6233 | DCHECK(klass->ShouldHaveImt()) << klass->PrettyClass(); |
| 6234 | DCHECK(!klass->IsTemp()) << klass->PrettyClass(); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6235 | ArtMethod* imt_data[ImTable::kSize]; |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6236 | Runtime* const runtime = Runtime::Current(); |
| 6237 | ArtMethod* const unimplemented_method = runtime->GetImtUnimplementedMethod(); |
| 6238 | ArtMethod* const conflict_method = runtime->GetImtConflictMethod(); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6239 | std::fill_n(imt_data, arraysize(imt_data), unimplemented_method); |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6240 | if (klass->GetIfTable() != nullptr) { |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6241 | bool new_conflict = false; |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6242 | FillIMTFromIfTable(klass->GetIfTable(), |
| 6243 | unimplemented_method, |
| 6244 | conflict_method, |
| 6245 | klass, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6246 | /*create_conflict_tables*/true, |
| 6247 | /*ignore_copied_methods*/false, |
| 6248 | &new_conflict, |
| 6249 | &imt_data[0]); |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6250 | } |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6251 | if (!klass->ShouldHaveImt()) { |
| 6252 | return; |
| 6253 | } |
| 6254 | // Compare the IMT with the super class including the conflict methods. If they are equivalent, |
| 6255 | // we can just use the same pointer. |
| 6256 | ImTable* imt = nullptr; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6257 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6258 | if (super_class != nullptr && super_class->ShouldHaveImt()) { |
| 6259 | ImTable* super_imt = super_class->GetImt(image_pointer_size_); |
| 6260 | bool same = true; |
| 6261 | for (size_t i = 0; same && i < ImTable::kSize; ++i) { |
| 6262 | ArtMethod* method = imt_data[i]; |
| 6263 | ArtMethod* super_method = super_imt->Get(i, image_pointer_size_); |
| 6264 | if (method != super_method) { |
| 6265 | bool is_conflict_table = method->IsRuntimeMethod() && |
| 6266 | method != unimplemented_method && |
| 6267 | method != conflict_method; |
| 6268 | // Verify conflict contents. |
| 6269 | bool super_conflict_table = super_method->IsRuntimeMethod() && |
| 6270 | super_method != unimplemented_method && |
| 6271 | super_method != conflict_method; |
| 6272 | if (!is_conflict_table || !super_conflict_table) { |
| 6273 | same = false; |
| 6274 | } else { |
| 6275 | ImtConflictTable* table1 = method->GetImtConflictTable(image_pointer_size_); |
| 6276 | ImtConflictTable* table2 = super_method->GetImtConflictTable(image_pointer_size_); |
| 6277 | same = same && table1->Equals(table2, image_pointer_size_); |
| 6278 | } |
| 6279 | } |
| 6280 | } |
| 6281 | if (same) { |
| 6282 | imt = super_imt; |
| 6283 | } |
| 6284 | } |
| 6285 | if (imt == nullptr) { |
| 6286 | imt = klass->GetImt(image_pointer_size_); |
| 6287 | DCHECK(imt != nullptr); |
| 6288 | imt->Populate(imt_data, image_pointer_size_); |
| 6289 | } else { |
| 6290 | klass->SetImt(imt, image_pointer_size_); |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6291 | } |
| 6292 | } |
| 6293 | |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6294 | ImtConflictTable* ClassLinker::CreateImtConflictTable(size_t count, |
| 6295 | LinearAlloc* linear_alloc, |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 6296 | PointerSize image_pointer_size) { |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6297 | void* data = linear_alloc->Alloc(Thread::Current(), |
| 6298 | ImtConflictTable::ComputeSize(count, |
| 6299 | image_pointer_size)); |
| 6300 | return (data != nullptr) ? new (data) ImtConflictTable(count, image_pointer_size) : nullptr; |
| 6301 | } |
| 6302 | |
| 6303 | ImtConflictTable* ClassLinker::CreateImtConflictTable(size_t count, LinearAlloc* linear_alloc) { |
| 6304 | return CreateImtConflictTable(count, linear_alloc, image_pointer_size_); |
| 6305 | } |
| 6306 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6307 | void ClassLinker::FillIMTFromIfTable(ObjPtr<mirror::IfTable> if_table, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6308 | ArtMethod* unimplemented_method, |
| 6309 | ArtMethod* imt_conflict_method, |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6310 | ObjPtr<mirror::Class> klass, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6311 | bool create_conflict_tables, |
| 6312 | bool ignore_copied_methods, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6313 | /*out*/bool* new_conflict, |
| 6314 | /*out*/ArtMethod** imt) { |
| 6315 | uint32_t conflict_counts[ImTable::kSize] = {}; |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6316 | for (size_t i = 0, length = if_table->Count(); i < length; ++i) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6317 | ObjPtr<mirror::Class> interface = if_table->GetInterface(i); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6318 | const size_t num_virtuals = interface->NumVirtualMethods(); |
| 6319 | const size_t method_array_count = if_table->GetMethodArrayCount(i); |
| 6320 | // Virtual methods can be larger than the if table methods if there are default methods. |
| 6321 | DCHECK_GE(num_virtuals, method_array_count); |
| 6322 | if (kIsDebugBuild) { |
| 6323 | if (klass->IsInterface()) { |
| 6324 | DCHECK_EQ(method_array_count, 0u); |
| 6325 | } else { |
| 6326 | DCHECK_EQ(interface->NumDeclaredVirtualMethods(), method_array_count); |
| 6327 | } |
| 6328 | } |
| 6329 | if (method_array_count == 0) { |
| 6330 | continue; |
| 6331 | } |
| 6332 | auto* method_array = if_table->GetMethodArray(i); |
| 6333 | for (size_t j = 0; j < method_array_count; ++j) { |
| 6334 | ArtMethod* implementation_method = |
| 6335 | method_array->GetElementPtrSize<ArtMethod*>(j, image_pointer_size_); |
| 6336 | if (ignore_copied_methods && implementation_method->IsCopied()) { |
| 6337 | continue; |
| 6338 | } |
| 6339 | DCHECK(implementation_method != nullptr); |
| 6340 | // Miranda methods cannot be used to implement an interface method, but they are safe to put |
| 6341 | // in the IMT since their entrypoint is the interface trampoline. If we put any copied methods |
| 6342 | // or interface methods in the IMT here they will not create extra conflicts since we compare |
| 6343 | // names and signatures in SetIMTRef. |
| 6344 | ArtMethod* interface_method = interface->GetVirtualMethod(j, image_pointer_size_); |
Andreas Gampe | 75a7db6 | 2016-09-26 12:04:26 -0700 | [diff] [blame] | 6345 | const uint32_t imt_index = ImTable::GetImtIndex(interface_method); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6346 | |
| 6347 | // There is only any conflicts if all of the interface methods for an IMT slot don't have |
| 6348 | // the same implementation method, keep track of this to avoid creating a conflict table in |
| 6349 | // this case. |
| 6350 | |
| 6351 | // Conflict table size for each IMT slot. |
| 6352 | ++conflict_counts[imt_index]; |
| 6353 | |
| 6354 | SetIMTRef(unimplemented_method, |
| 6355 | imt_conflict_method, |
| 6356 | implementation_method, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6357 | /*out*/new_conflict, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6358 | /*out*/&imt[imt_index]); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6359 | } |
| 6360 | } |
| 6361 | |
| 6362 | if (create_conflict_tables) { |
| 6363 | // Create the conflict tables. |
| 6364 | LinearAlloc* linear_alloc = GetAllocatorForClassLoader(klass->GetClassLoader()); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6365 | for (size_t i = 0; i < ImTable::kSize; ++i) { |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6366 | size_t conflicts = conflict_counts[i]; |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6367 | if (imt[i] == imt_conflict_method) { |
| 6368 | ImtConflictTable* new_table = CreateImtConflictTable(conflicts, linear_alloc); |
| 6369 | if (new_table != nullptr) { |
| 6370 | ArtMethod* new_conflict_method = |
| 6371 | Runtime::Current()->CreateImtConflictMethod(linear_alloc); |
| 6372 | new_conflict_method->SetImtConflictTable(new_table, image_pointer_size_); |
| 6373 | imt[i] = new_conflict_method; |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6374 | } else { |
| 6375 | LOG(ERROR) << "Failed to allocate conflict table"; |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6376 | imt[i] = imt_conflict_method; |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6377 | } |
| 6378 | } else { |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6379 | DCHECK_NE(imt[i], imt_conflict_method); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6380 | } |
| 6381 | } |
| 6382 | |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6383 | for (size_t i = 0, length = if_table->Count(); i < length; ++i) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6384 | ObjPtr<mirror::Class> interface = if_table->GetInterface(i); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6385 | const size_t method_array_count = if_table->GetMethodArrayCount(i); |
| 6386 | // Virtual methods can be larger than the if table methods if there are default methods. |
| 6387 | if (method_array_count == 0) { |
| 6388 | continue; |
| 6389 | } |
| 6390 | auto* method_array = if_table->GetMethodArray(i); |
| 6391 | for (size_t j = 0; j < method_array_count; ++j) { |
| 6392 | ArtMethod* implementation_method = |
| 6393 | method_array->GetElementPtrSize<ArtMethod*>(j, image_pointer_size_); |
| 6394 | if (ignore_copied_methods && implementation_method->IsCopied()) { |
| 6395 | continue; |
| 6396 | } |
| 6397 | DCHECK(implementation_method != nullptr); |
| 6398 | ArtMethod* interface_method = interface->GetVirtualMethod(j, image_pointer_size_); |
Andreas Gampe | 75a7db6 | 2016-09-26 12:04:26 -0700 | [diff] [blame] | 6399 | const uint32_t imt_index = ImTable::GetImtIndex(interface_method); |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6400 | if (!imt[imt_index]->IsRuntimeMethod() || |
| 6401 | imt[imt_index] == unimplemented_method || |
| 6402 | imt[imt_index] == imt_conflict_method) { |
| 6403 | continue; |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6404 | } |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6405 | ImtConflictTable* table = imt[imt_index]->GetImtConflictTable(image_pointer_size_); |
| 6406 | const size_t num_entries = table->NumEntries(image_pointer_size_); |
| 6407 | table->SetInterfaceMethod(num_entries, image_pointer_size_, interface_method); |
| 6408 | table->SetImplementationMethod(num_entries, image_pointer_size_, implementation_method); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6409 | } |
| 6410 | } |
| 6411 | } |
| 6412 | } |
| 6413 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6414 | // Simple helper function that checks that no subtypes of 'val' are contained within the 'classes' |
| 6415 | // set. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6416 | static bool NotSubinterfaceOfAny( |
| 6417 | const std::unordered_set<ObjPtr<mirror::Class>, HashObjPtr>& classes, |
| 6418 | ObjPtr<mirror::Class> val) |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6419 | REQUIRES(Roles::uninterruptible_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 6420 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6421 | DCHECK(val != nullptr); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6422 | for (ObjPtr<mirror::Class> c : classes) { |
| 6423 | if (val->IsAssignableFrom(c)) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6424 | return false; |
| 6425 | } |
| 6426 | } |
| 6427 | return true; |
| 6428 | } |
| 6429 | |
| 6430 | // Fills in and flattens the interface inheritance hierarchy. |
| 6431 | // |
| 6432 | // By the end of this function all interfaces in the transitive closure of to_process are added to |
| 6433 | // the iftable and every interface precedes all of its sub-interfaces in this list. |
| 6434 | // |
| 6435 | // all I, J: Interface | I <: J implies J precedes I |
| 6436 | // |
| 6437 | // (note A <: B means that A is a subtype of B) |
| 6438 | // |
| 6439 | // This returns the total number of items in the iftable. The iftable might be resized down after |
| 6440 | // this call. |
| 6441 | // |
| 6442 | // We order this backwards so that we do not need to reorder superclass interfaces when new |
| 6443 | // interfaces are added in subclass's interface tables. |
| 6444 | // |
| 6445 | // Upon entry into this function iftable is a copy of the superclass's iftable with the first |
| 6446 | // super_ifcount entries filled in with the transitive closure of the interfaces of the superclass. |
| 6447 | // The other entries are uninitialized. We will fill in the remaining entries in this function. The |
| 6448 | // 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] | 6449 | static size_t FillIfTable(ObjPtr<mirror::IfTable> iftable, |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6450 | size_t super_ifcount, |
Stephen Hines | 48ba197 | 2018-09-24 13:35:54 -0700 | [diff] [blame^] | 6451 | const std::vector<ObjPtr<mirror::Class>>& to_process) |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6452 | REQUIRES(Roles::uninterruptible_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 6453 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6454 | // This is the set of all class's already in the iftable. Used to make checking if a class has |
| 6455 | // already been added quicker. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6456 | std::unordered_set<ObjPtr<mirror::Class>, HashObjPtr> classes_in_iftable; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6457 | // The first super_ifcount elements are from the superclass. We note that they are already added. |
| 6458 | for (size_t i = 0; i < super_ifcount; i++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6459 | ObjPtr<mirror::Class> iface = iftable->GetInterface(i); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6460 | DCHECK(NotSubinterfaceOfAny(classes_in_iftable, iface)) << "Bad ordering."; |
| 6461 | classes_in_iftable.insert(iface); |
| 6462 | } |
| 6463 | size_t filled_ifcount = super_ifcount; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6464 | for (ObjPtr<mirror::Class> interface : to_process) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6465 | // Let us call the first filled_ifcount elements of iftable the current-iface-list. |
| 6466 | // At this point in the loop current-iface-list has the invariant that: |
| 6467 | // for every pair of interfaces I,J within it: |
| 6468 | // if index_of(I) < index_of(J) then I is not a subtype of J |
| 6469 | |
| 6470 | // If we have already seen this element then all of its super-interfaces must already be in the |
| 6471 | // current-iface-list so we can skip adding it. |
| 6472 | if (!ContainsElement(classes_in_iftable, interface)) { |
| 6473 | // We haven't seen this interface so add all of its super-interfaces onto the |
| 6474 | // current-iface-list, skipping those already on it. |
| 6475 | int32_t ifcount = interface->GetIfTableCount(); |
| 6476 | for (int32_t j = 0; j < ifcount; j++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6477 | ObjPtr<mirror::Class> super_interface = interface->GetIfTable()->GetInterface(j); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6478 | if (!ContainsElement(classes_in_iftable, super_interface)) { |
| 6479 | DCHECK(NotSubinterfaceOfAny(classes_in_iftable, super_interface)) << "Bad ordering."; |
| 6480 | classes_in_iftable.insert(super_interface); |
| 6481 | iftable->SetInterface(filled_ifcount, super_interface); |
| 6482 | filled_ifcount++; |
| 6483 | } |
| 6484 | } |
| 6485 | DCHECK(NotSubinterfaceOfAny(classes_in_iftable, interface)) << "Bad ordering"; |
| 6486 | // Place this interface onto the current-iface-list after all of its super-interfaces. |
| 6487 | classes_in_iftable.insert(interface); |
| 6488 | iftable->SetInterface(filled_ifcount, interface); |
| 6489 | filled_ifcount++; |
| 6490 | } else if (kIsDebugBuild) { |
| 6491 | // Check all super-interfaces are already in the list. |
| 6492 | int32_t ifcount = interface->GetIfTableCount(); |
| 6493 | for (int32_t j = 0; j < ifcount; j++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6494 | ObjPtr<mirror::Class> super_interface = interface->GetIfTable()->GetInterface(j); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6495 | DCHECK(ContainsElement(classes_in_iftable, super_interface)) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6496 | << "Iftable does not contain " << mirror::Class::PrettyClass(super_interface) |
| 6497 | << ", a superinterface of " << interface->PrettyClass(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6498 | } |
| 6499 | } |
| 6500 | } |
| 6501 | if (kIsDebugBuild) { |
| 6502 | // Check that the iftable is ordered correctly. |
| 6503 | for (size_t i = 0; i < filled_ifcount; i++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6504 | ObjPtr<mirror::Class> if_a = iftable->GetInterface(i); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6505 | for (size_t j = i + 1; j < filled_ifcount; j++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6506 | ObjPtr<mirror::Class> if_b = iftable->GetInterface(j); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6507 | // !(if_a <: if_b) |
| 6508 | CHECK(!if_b->IsAssignableFrom(if_a)) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6509 | << "Bad interface order: " << mirror::Class::PrettyClass(if_a) << " (index " << i |
| 6510 | << ") extends " |
| 6511 | << 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] | 6512 | << "interface list."; |
| 6513 | } |
| 6514 | } |
| 6515 | } |
| 6516 | return filled_ifcount; |
| 6517 | } |
| 6518 | |
| 6519 | bool ClassLinker::SetupInterfaceLookupTable(Thread* self, Handle<mirror::Class> klass, |
| 6520 | Handle<mirror::ObjectArray<mirror::Class>> interfaces) { |
| 6521 | StackHandleScope<1> hs(self); |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 6522 | const bool has_superclass = klass->HasSuperClass(); |
| 6523 | const size_t super_ifcount = has_superclass ? klass->GetSuperClass()->GetIfTableCount() : 0U; |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 6524 | const bool have_interfaces = interfaces != nullptr; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6525 | const size_t num_interfaces = |
| 6526 | have_interfaces ? interfaces->GetLength() : klass->NumDirectInterfaces(); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6527 | if (num_interfaces == 0) { |
| 6528 | if (super_ifcount == 0) { |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 6529 | if (LIKELY(has_superclass)) { |
| 6530 | klass->SetIfTable(klass->GetSuperClass()->GetIfTable()); |
| 6531 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6532 | // Class implements no interfaces. |
| 6533 | DCHECK_EQ(klass->GetIfTableCount(), 0); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6534 | return true; |
| 6535 | } |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 6536 | // Class implements same interfaces as parent, are any of these not marker interfaces? |
| 6537 | bool has_non_marker_interface = false; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6538 | ObjPtr<mirror::IfTable> super_iftable = klass->GetSuperClass()->GetIfTable(); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6539 | for (size_t i = 0; i < super_ifcount; ++i) { |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 6540 | if (super_iftable->GetMethodArrayCount(i) > 0) { |
| 6541 | has_non_marker_interface = true; |
| 6542 | break; |
| 6543 | } |
| 6544 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6545 | // Class just inherits marker interfaces from parent so recycle parent's iftable. |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 6546 | if (!has_non_marker_interface) { |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 6547 | klass->SetIfTable(super_iftable); |
| 6548 | return true; |
| 6549 | } |
| 6550 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6551 | size_t ifcount = super_ifcount + num_interfaces; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6552 | // Check that every class being implemented is an interface. |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6553 | for (size_t i = 0; i < num_interfaces; i++) { |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 6554 | ObjPtr<mirror::Class> interface = have_interfaces |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 6555 | ? interfaces->GetWithoutChecks(i) |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 6556 | : mirror::Class::GetDirectInterface(self, klass.Get(), i); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6557 | DCHECK(interface != nullptr); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6558 | if (UNLIKELY(!interface->IsInterface())) { |
| 6559 | std::string temp; |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 6560 | ThrowIncompatibleClassChangeError(klass.Get(), |
| 6561 | "Class %s implements non-interface class %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6562 | klass->PrettyDescriptor().c_str(), |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6563 | PrettyDescriptor(interface->GetDescriptor(&temp)).c_str()); |
| 6564 | return false; |
| 6565 | } |
| 6566 | ifcount += interface->GetIfTableCount(); |
| 6567 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6568 | // Create the interface function table. |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 6569 | MutableHandle<mirror::IfTable> iftable(hs.NewHandle(AllocIfTable(self, ifcount))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 6570 | if (UNLIKELY(iftable == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6571 | self->AssertPendingOOMException(); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 6572 | return false; |
| 6573 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6574 | // Fill in table with superclass's iftable. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6575 | if (super_ifcount != 0) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6576 | ObjPtr<mirror::IfTable> super_iftable = klass->GetSuperClass()->GetIfTable(); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 6577 | for (size_t i = 0; i < super_ifcount; i++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6578 | ObjPtr<mirror::Class> super_interface = super_iftable->GetInterface(i); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 6579 | iftable->SetInterface(i, super_interface); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 6580 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6581 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6582 | |
| 6583 | // Note that AllowThreadSuspension is to thread suspension as pthread_testcancel is to pthread |
| 6584 | // cancellation. That is it will suspend if one has a pending suspend request but otherwise |
| 6585 | // doesn't really do anything. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 6586 | self->AllowThreadSuspension(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6587 | |
| 6588 | size_t new_ifcount; |
| 6589 | { |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 6590 | ScopedAssertNoThreadSuspension nts("Copying mirror::Class*'s for FillIfTable"); |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 6591 | std::vector<ObjPtr<mirror::Class>> to_add; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6592 | for (size_t i = 0; i < num_interfaces; i++) { |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 6593 | ObjPtr<mirror::Class> interface = have_interfaces ? interfaces->Get(i) : |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 6594 | mirror::Class::GetDirectInterface(self, klass.Get(), i); |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 6595 | to_add.push_back(interface); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 6596 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6597 | |
| 6598 | new_ifcount = FillIfTable(iftable.Get(), super_ifcount, std::move(to_add)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6599 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6600 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 6601 | self->AllowThreadSuspension(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6602 | |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 6603 | // Shrink iftable in case duplicates were found |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6604 | if (new_ifcount < ifcount) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6605 | DCHECK_NE(num_interfaces, 0U); |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 6606 | iftable.Assign(ObjPtr<mirror::IfTable>::DownCast( |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6607 | iftable->CopyOf(self, new_ifcount * mirror::IfTable::kMax))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 6608 | if (UNLIKELY(iftable == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6609 | self->AssertPendingOOMException(); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 6610 | return false; |
| 6611 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6612 | ifcount = new_ifcount; |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 6613 | } else { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6614 | DCHECK_EQ(new_ifcount, ifcount); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 6615 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 6616 | klass->SetIfTable(iftable.Get()); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6617 | return true; |
| 6618 | } |
| 6619 | |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6620 | // Finds the method with a name/signature that matches cmp in the given lists of methods. The list |
| 6621 | // of methods must be unique. |
| 6622 | static ArtMethod* FindSameNameAndSignature(MethodNameAndSignatureComparator& cmp ATTRIBUTE_UNUSED) { |
| 6623 | return nullptr; |
| 6624 | } |
| 6625 | |
| 6626 | template <typename ... Types> |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6627 | static ArtMethod* FindSameNameAndSignature(MethodNameAndSignatureComparator& cmp, |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6628 | const ScopedArenaVector<ArtMethod*>& list, |
| 6629 | const Types& ... rest) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 6630 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6631 | for (ArtMethod* method : list) { |
| 6632 | if (cmp.HasSameNameAndSignature(method)) { |
| 6633 | return method; |
| 6634 | } |
| 6635 | } |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6636 | return FindSameNameAndSignature(cmp, rest...); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6637 | } |
| 6638 | |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6639 | // Check that all vtable entries are present in this class's virtuals or are the same as a |
| 6640 | // superclasses vtable entry. |
| 6641 | static void CheckClassOwnsVTableEntries(Thread* self, |
| 6642 | Handle<mirror::Class> klass, |
| 6643 | PointerSize pointer_size) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 6644 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6645 | StackHandleScope<2> hs(self); |
| 6646 | Handle<mirror::PointerArray> check_vtable(hs.NewHandle(klass->GetVTableDuringLinking())); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6647 | ObjPtr<mirror::Class> super_temp = (klass->HasSuperClass()) ? klass->GetSuperClass() : nullptr; |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6648 | Handle<mirror::Class> superclass(hs.NewHandle(super_temp)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 6649 | int32_t super_vtable_length = (superclass != nullptr) ? superclass->GetVTableLength() : 0; |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6650 | for (int32_t i = 0; i < check_vtable->GetLength(); ++i) { |
| 6651 | ArtMethod* m = check_vtable->GetElementPtrSize<ArtMethod*>(i, pointer_size); |
| 6652 | CHECK(m != nullptr); |
| 6653 | |
Alex Light | a41a3078 | 2017-03-29 11:33:19 -0700 | [diff] [blame] | 6654 | if (m->GetMethodIndexDuringLinking() != i) { |
| 6655 | LOG(WARNING) << m->PrettyMethod() |
| 6656 | << " has an unexpected method index for its spot in the vtable for class" |
| 6657 | << klass->PrettyClass(); |
| 6658 | } |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6659 | ArraySlice<ArtMethod> virtuals = klass->GetVirtualMethodsSliceUnchecked(pointer_size); |
| 6660 | auto is_same_method = [m] (const ArtMethod& meth) { |
| 6661 | return &meth == m; |
| 6662 | }; |
Alex Light | 3f98053 | 2017-03-17 15:10:32 -0700 | [diff] [blame] | 6663 | if (!((super_vtable_length > i && superclass->GetVTableEntry(i, pointer_size) == m) || |
| 6664 | std::find_if(virtuals.begin(), virtuals.end(), is_same_method) != virtuals.end())) { |
| 6665 | LOG(WARNING) << m->PrettyMethod() << " does not seem to be owned by current class " |
| 6666 | << klass->PrettyClass() << " or any of its superclasses!"; |
| 6667 | } |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6668 | } |
| 6669 | } |
| 6670 | |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6671 | // Check to make sure the vtable does not have duplicates. Duplicates could cause problems when a |
| 6672 | // method is overridden in a subclass. |
| 6673 | static void CheckVTableHasNoDuplicates(Thread* self, |
| 6674 | Handle<mirror::Class> klass, |
| 6675 | PointerSize pointer_size) |
| 6676 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 6677 | StackHandleScope<1> hs(self); |
| 6678 | Handle<mirror::PointerArray> vtable(hs.NewHandle(klass->GetVTableDuringLinking())); |
| 6679 | int32_t num_entries = vtable->GetLength(); |
| 6680 | for (int32_t i = 0; i < num_entries; i++) { |
| 6681 | ArtMethod* vtable_entry = vtable->GetElementPtrSize<ArtMethod*>(i, pointer_size); |
| 6682 | // Don't bother if we cannot 'see' the vtable entry (i.e. it is a package-private member maybe). |
| 6683 | if (!klass->CanAccessMember(vtable_entry->GetDeclaringClass(), |
| 6684 | vtable_entry->GetAccessFlags())) { |
| 6685 | continue; |
| 6686 | } |
| 6687 | MethodNameAndSignatureComparator name_comparator( |
| 6688 | vtable_entry->GetInterfaceMethodIfProxy(pointer_size)); |
Alex Light | c7a420c | 2016-10-18 14:33:18 -0700 | [diff] [blame] | 6689 | for (int32_t j = i + 1; j < num_entries; j++) { |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6690 | ArtMethod* other_entry = vtable->GetElementPtrSize<ArtMethod*>(j, pointer_size); |
Alex Light | c7a420c | 2016-10-18 14:33:18 -0700 | [diff] [blame] | 6691 | if (!klass->CanAccessMember(other_entry->GetDeclaringClass(), |
| 6692 | other_entry->GetAccessFlags())) { |
| 6693 | continue; |
| 6694 | } |
Alex Light | 3f98053 | 2017-03-17 15:10:32 -0700 | [diff] [blame] | 6695 | if (vtable_entry == other_entry || |
| 6696 | name_comparator.HasSameNameAndSignature( |
| 6697 | other_entry->GetInterfaceMethodIfProxy(pointer_size))) { |
| 6698 | LOG(WARNING) << "vtable entries " << i << " and " << j << " are identical for " |
| 6699 | << klass->PrettyClass() << " in method " << vtable_entry->PrettyMethod() |
| 6700 | << " (0x" << std::hex << reinterpret_cast<uintptr_t>(vtable_entry) << ") and " |
| 6701 | << other_entry->PrettyMethod() << " (0x" << std::hex |
| 6702 | << reinterpret_cast<uintptr_t>(other_entry) << ")"; |
| 6703 | } |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6704 | } |
| 6705 | } |
| 6706 | } |
| 6707 | |
| 6708 | static void SanityCheckVTable(Thread* self, Handle<mirror::Class> klass, PointerSize pointer_size) |
| 6709 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 6710 | CheckClassOwnsVTableEntries(self, klass, pointer_size); |
| 6711 | CheckVTableHasNoDuplicates(self, klass, pointer_size); |
| 6712 | } |
| 6713 | |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6714 | void ClassLinker::FillImtFromSuperClass(Handle<mirror::Class> klass, |
| 6715 | ArtMethod* unimplemented_method, |
| 6716 | ArtMethod* imt_conflict_method, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6717 | bool* new_conflict, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6718 | ArtMethod** imt) { |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 6719 | DCHECK(klass->HasSuperClass()); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6720 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6721 | if (super_class->ShouldHaveImt()) { |
| 6722 | ImTable* super_imt = super_class->GetImt(image_pointer_size_); |
| 6723 | for (size_t i = 0; i < ImTable::kSize; ++i) { |
| 6724 | imt[i] = super_imt->Get(i, image_pointer_size_); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 6725 | } |
| 6726 | } else { |
| 6727 | // No imt in the super class, need to reconstruct from the iftable. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6728 | ObjPtr<mirror::IfTable> if_table = super_class->GetIfTable(); |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 6729 | if (if_table->Count() != 0) { |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6730 | // Ignore copied methods since we will handle these in LinkInterfaceMethods. |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6731 | FillIMTFromIfTable(if_table, |
| 6732 | unimplemented_method, |
| 6733 | imt_conflict_method, |
| 6734 | klass.Get(), |
| 6735 | /*create_conflict_table*/false, |
| 6736 | /*ignore_copied_methods*/true, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6737 | /*out*/new_conflict, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6738 | /*out*/imt); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 6739 | } |
| 6740 | } |
| 6741 | } |
| 6742 | |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 6743 | class ClassLinker::LinkInterfaceMethodsHelper { |
| 6744 | public: |
| 6745 | LinkInterfaceMethodsHelper(ClassLinker* class_linker, |
| 6746 | Handle<mirror::Class> klass, |
| 6747 | Thread* self, |
| 6748 | Runtime* runtime) |
| 6749 | : class_linker_(class_linker), |
| 6750 | klass_(klass), |
| 6751 | method_alignment_(ArtMethod::Alignment(class_linker->GetImagePointerSize())), |
| 6752 | method_size_(ArtMethod::Size(class_linker->GetImagePointerSize())), |
| 6753 | self_(self), |
| 6754 | stack_(runtime->GetLinearAlloc()->GetArenaPool()), |
| 6755 | allocator_(&stack_), |
| 6756 | default_conflict_methods_(allocator_.Adapter()), |
| 6757 | overriding_default_conflict_methods_(allocator_.Adapter()), |
| 6758 | miranda_methods_(allocator_.Adapter()), |
| 6759 | default_methods_(allocator_.Adapter()), |
| 6760 | overriding_default_methods_(allocator_.Adapter()), |
| 6761 | move_table_(allocator_.Adapter()) { |
| 6762 | } |
| 6763 | |
| 6764 | ArtMethod* FindMethod(ArtMethod* interface_method, |
| 6765 | MethodNameAndSignatureComparator& interface_name_comparator, |
| 6766 | ArtMethod* vtable_impl) |
| 6767 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 6768 | |
| 6769 | ArtMethod* GetOrCreateMirandaMethod(ArtMethod* interface_method, |
| 6770 | MethodNameAndSignatureComparator& interface_name_comparator) |
| 6771 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 6772 | |
| 6773 | bool HasNewVirtuals() const { |
| 6774 | return !(miranda_methods_.empty() && |
| 6775 | default_methods_.empty() && |
| 6776 | overriding_default_methods_.empty() && |
| 6777 | overriding_default_conflict_methods_.empty() && |
| 6778 | default_conflict_methods_.empty()); |
| 6779 | } |
| 6780 | |
| 6781 | void ReallocMethods() REQUIRES_SHARED(Locks::mutator_lock_); |
| 6782 | |
| 6783 | ObjPtr<mirror::PointerArray> UpdateVtable( |
| 6784 | const std::unordered_map<size_t, ClassLinker::MethodTranslation>& default_translations, |
| 6785 | ObjPtr<mirror::PointerArray> old_vtable) REQUIRES_SHARED(Locks::mutator_lock_); |
| 6786 | |
| 6787 | void UpdateIfTable(Handle<mirror::IfTable> iftable) REQUIRES_SHARED(Locks::mutator_lock_); |
| 6788 | |
| 6789 | void UpdateIMT(ArtMethod** out_imt); |
| 6790 | |
| 6791 | void CheckNoStaleMethodsInDexCache() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 6792 | if (kIsDebugBuild) { |
| 6793 | PointerSize pointer_size = class_linker_->GetImagePointerSize(); |
| 6794 | // Check that there are no stale methods are in the dex cache array. |
| 6795 | auto* resolved_methods = klass_->GetDexCache()->GetResolvedMethods(); |
| 6796 | for (size_t i = 0, count = klass_->GetDexCache()->NumResolvedMethods(); i < count; ++i) { |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 6797 | auto pair = mirror::DexCache::GetNativePairPtrSize(resolved_methods, i, pointer_size); |
| 6798 | ArtMethod* m = pair.object; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 6799 | CHECK(move_table_.find(m) == move_table_.end() || |
| 6800 | // The original versions of copied methods will still be present so allow those too. |
| 6801 | // Note that if the first check passes this might fail to GetDeclaringClass(). |
| 6802 | std::find_if(m->GetDeclaringClass()->GetMethods(pointer_size).begin(), |
| 6803 | m->GetDeclaringClass()->GetMethods(pointer_size).end(), |
| 6804 | [m] (ArtMethod& meth) { |
| 6805 | return &meth == m; |
| 6806 | }) != m->GetDeclaringClass()->GetMethods(pointer_size).end()) |
| 6807 | << "Obsolete method " << m->PrettyMethod() << " is in dex cache!"; |
| 6808 | } |
| 6809 | } |
| 6810 | } |
| 6811 | |
| 6812 | void ClobberOldMethods(LengthPrefixedArray<ArtMethod>* old_methods, |
| 6813 | LengthPrefixedArray<ArtMethod>* methods) { |
| 6814 | if (kIsDebugBuild) { |
| 6815 | CHECK(methods != nullptr); |
| 6816 | // Put some random garbage in old methods to help find stale pointers. |
| 6817 | if (methods != old_methods && old_methods != nullptr) { |
| 6818 | // Need to make sure the GC is not running since it could be scanning the methods we are |
| 6819 | // about to overwrite. |
| 6820 | ScopedThreadStateChange tsc(self_, kSuspended); |
| 6821 | gc::ScopedGCCriticalSection gcs(self_, |
| 6822 | gc::kGcCauseClassLinker, |
| 6823 | gc::kCollectorTypeClassLinker); |
| 6824 | const size_t old_size = LengthPrefixedArray<ArtMethod>::ComputeSize(old_methods->size(), |
| 6825 | method_size_, |
| 6826 | method_alignment_); |
| 6827 | memset(old_methods, 0xFEu, old_size); |
| 6828 | } |
| 6829 | } |
| 6830 | } |
| 6831 | |
| 6832 | private: |
| 6833 | size_t NumberOfNewVirtuals() const { |
| 6834 | return miranda_methods_.size() + |
| 6835 | default_methods_.size() + |
| 6836 | overriding_default_conflict_methods_.size() + |
| 6837 | overriding_default_methods_.size() + |
| 6838 | default_conflict_methods_.size(); |
| 6839 | } |
| 6840 | |
| 6841 | bool FillTables() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 6842 | return !klass_->IsInterface(); |
| 6843 | } |
| 6844 | |
| 6845 | void LogNewVirtuals() const REQUIRES_SHARED(Locks::mutator_lock_) { |
| 6846 | DCHECK(!klass_->IsInterface() || (default_methods_.empty() && miranda_methods_.empty())) |
| 6847 | << "Interfaces should only have default-conflict methods appended to them."; |
| 6848 | VLOG(class_linker) << mirror::Class::PrettyClass(klass_.Get()) << ": miranda_methods=" |
| 6849 | << miranda_methods_.size() |
| 6850 | << " default_methods=" << default_methods_.size() |
| 6851 | << " overriding_default_methods=" << overriding_default_methods_.size() |
| 6852 | << " default_conflict_methods=" << default_conflict_methods_.size() |
| 6853 | << " overriding_default_conflict_methods=" |
| 6854 | << overriding_default_conflict_methods_.size(); |
| 6855 | } |
| 6856 | |
| 6857 | ClassLinker* class_linker_; |
| 6858 | Handle<mirror::Class> klass_; |
| 6859 | size_t method_alignment_; |
| 6860 | size_t method_size_; |
| 6861 | Thread* const self_; |
| 6862 | |
| 6863 | // These are allocated on the heap to begin, we then transfer to linear alloc when we re-create |
| 6864 | // the virtual methods array. |
| 6865 | // Need to use low 4GB arenas for compiler or else the pointers wont fit in 32 bit method array |
| 6866 | // during cross compilation. |
| 6867 | // Use the linear alloc pool since this one is in the low 4gb for the compiler. |
| 6868 | ArenaStack stack_; |
| 6869 | ScopedArenaAllocator allocator_; |
| 6870 | |
| 6871 | ScopedArenaVector<ArtMethod*> default_conflict_methods_; |
| 6872 | ScopedArenaVector<ArtMethod*> overriding_default_conflict_methods_; |
| 6873 | ScopedArenaVector<ArtMethod*> miranda_methods_; |
| 6874 | ScopedArenaVector<ArtMethod*> default_methods_; |
| 6875 | ScopedArenaVector<ArtMethod*> overriding_default_methods_; |
| 6876 | |
| 6877 | ScopedArenaUnorderedMap<ArtMethod*, ArtMethod*> move_table_; |
| 6878 | }; |
| 6879 | |
| 6880 | ArtMethod* ClassLinker::LinkInterfaceMethodsHelper::FindMethod( |
| 6881 | ArtMethod* interface_method, |
| 6882 | MethodNameAndSignatureComparator& interface_name_comparator, |
| 6883 | ArtMethod* vtable_impl) { |
| 6884 | ArtMethod* current_method = nullptr; |
| 6885 | switch (class_linker_->FindDefaultMethodImplementation(self_, |
| 6886 | interface_method, |
| 6887 | klass_, |
| 6888 | /*out*/¤t_method)) { |
| 6889 | case DefaultMethodSearchResult::kDefaultConflict: { |
| 6890 | // Default method conflict. |
| 6891 | DCHECK(current_method == nullptr); |
| 6892 | ArtMethod* default_conflict_method = nullptr; |
| 6893 | if (vtable_impl != nullptr && vtable_impl->IsDefaultConflicting()) { |
| 6894 | // We can reuse the method from the superclass, don't bother adding it to virtuals. |
| 6895 | default_conflict_method = vtable_impl; |
| 6896 | } else { |
| 6897 | // See if we already have a conflict method for this method. |
| 6898 | ArtMethod* preexisting_conflict = FindSameNameAndSignature( |
| 6899 | interface_name_comparator, |
| 6900 | default_conflict_methods_, |
| 6901 | overriding_default_conflict_methods_); |
| 6902 | if (LIKELY(preexisting_conflict != nullptr)) { |
| 6903 | // We already have another conflict we can reuse. |
| 6904 | default_conflict_method = preexisting_conflict; |
| 6905 | } else { |
| 6906 | // Note that we do this even if we are an interface since we need to create this and |
| 6907 | // cannot reuse another classes. |
| 6908 | // Create a new conflict method for this to use. |
| 6909 | default_conflict_method = reinterpret_cast<ArtMethod*>(allocator_.Alloc(method_size_)); |
| 6910 | new(default_conflict_method) ArtMethod(interface_method, |
| 6911 | class_linker_->GetImagePointerSize()); |
| 6912 | if (vtable_impl == nullptr) { |
| 6913 | // Save the conflict method. We need to add it to the vtable. |
| 6914 | default_conflict_methods_.push_back(default_conflict_method); |
| 6915 | } else { |
| 6916 | // Save the conflict method but it is already in the vtable. |
| 6917 | overriding_default_conflict_methods_.push_back(default_conflict_method); |
| 6918 | } |
| 6919 | } |
| 6920 | } |
| 6921 | current_method = default_conflict_method; |
| 6922 | break; |
| 6923 | } // case kDefaultConflict |
| 6924 | case DefaultMethodSearchResult::kDefaultFound: { |
| 6925 | DCHECK(current_method != nullptr); |
| 6926 | // Found a default method. |
| 6927 | if (vtable_impl != nullptr && |
| 6928 | current_method->GetDeclaringClass() == vtable_impl->GetDeclaringClass()) { |
| 6929 | // We found a default method but it was the same one we already have from our |
| 6930 | // superclass. Don't bother adding it to our vtable again. |
| 6931 | current_method = vtable_impl; |
| 6932 | } else if (LIKELY(FillTables())) { |
| 6933 | // Interfaces don't need to copy default methods since they don't have vtables. |
| 6934 | // Only record this default method if it is new to save space. |
| 6935 | // TODO It might be worthwhile to copy default methods on interfaces anyway since it |
| 6936 | // would make lookup for interface super much faster. (We would only need to scan |
| 6937 | // the iftable to find if there is a NSME or AME.) |
| 6938 | ArtMethod* old = FindSameNameAndSignature(interface_name_comparator, |
| 6939 | default_methods_, |
| 6940 | overriding_default_methods_); |
| 6941 | if (old == nullptr) { |
| 6942 | // We found a default method implementation and there were no conflicts. |
| 6943 | if (vtable_impl == nullptr) { |
| 6944 | // Save the default method. We need to add it to the vtable. |
| 6945 | default_methods_.push_back(current_method); |
| 6946 | } else { |
| 6947 | // Save the default method but it is already in the vtable. |
| 6948 | overriding_default_methods_.push_back(current_method); |
| 6949 | } |
| 6950 | } else { |
| 6951 | CHECK(old == current_method) << "Multiple default implementations selected!"; |
| 6952 | } |
| 6953 | } |
| 6954 | break; |
| 6955 | } // case kDefaultFound |
| 6956 | case DefaultMethodSearchResult::kAbstractFound: { |
| 6957 | DCHECK(current_method == nullptr); |
| 6958 | // Abstract method masks all defaults. |
| 6959 | if (vtable_impl != nullptr && |
| 6960 | vtable_impl->IsAbstract() && |
| 6961 | !vtable_impl->IsDefaultConflicting()) { |
| 6962 | // We need to make this an abstract method but the version in the vtable already is so |
| 6963 | // don't do anything. |
| 6964 | current_method = vtable_impl; |
| 6965 | } |
| 6966 | break; |
| 6967 | } // case kAbstractFound |
| 6968 | } |
| 6969 | return current_method; |
| 6970 | } |
| 6971 | |
| 6972 | ArtMethod* ClassLinker::LinkInterfaceMethodsHelper::GetOrCreateMirandaMethod( |
| 6973 | ArtMethod* interface_method, |
| 6974 | MethodNameAndSignatureComparator& interface_name_comparator) { |
| 6975 | // Find out if there is already a miranda method we can use. |
| 6976 | ArtMethod* miranda_method = FindSameNameAndSignature(interface_name_comparator, |
| 6977 | miranda_methods_); |
| 6978 | if (miranda_method == nullptr) { |
| 6979 | DCHECK(interface_method->IsAbstract()) << interface_method->PrettyMethod(); |
| 6980 | miranda_method = reinterpret_cast<ArtMethod*>(allocator_.Alloc(method_size_)); |
| 6981 | CHECK(miranda_method != nullptr); |
| 6982 | // Point the interface table at a phantom slot. |
| 6983 | new(miranda_method) ArtMethod(interface_method, class_linker_->GetImagePointerSize()); |
| 6984 | miranda_methods_.push_back(miranda_method); |
| 6985 | } |
| 6986 | return miranda_method; |
| 6987 | } |
| 6988 | |
| 6989 | void ClassLinker::LinkInterfaceMethodsHelper::ReallocMethods() { |
| 6990 | LogNewVirtuals(); |
| 6991 | |
| 6992 | const size_t old_method_count = klass_->NumMethods(); |
| 6993 | const size_t new_method_count = old_method_count + NumberOfNewVirtuals(); |
| 6994 | DCHECK_NE(old_method_count, new_method_count); |
| 6995 | |
| 6996 | // Attempt to realloc to save RAM if possible. |
| 6997 | LengthPrefixedArray<ArtMethod>* old_methods = klass_->GetMethodsPtr(); |
| 6998 | // The Realloced virtual methods aren't visible from the class roots, so there is no issue |
| 6999 | // where GCs could attempt to mark stale pointers due to memcpy. And since we overwrite the |
| 7000 | // realloced memory with out->CopyFrom, we are guaranteed to have objects in the to space since |
| 7001 | // CopyFrom has internal read barriers. |
| 7002 | // |
| 7003 | // TODO We should maybe move some of this into mirror::Class or at least into another method. |
| 7004 | const size_t old_size = LengthPrefixedArray<ArtMethod>::ComputeSize(old_method_count, |
| 7005 | method_size_, |
| 7006 | method_alignment_); |
| 7007 | const size_t new_size = LengthPrefixedArray<ArtMethod>::ComputeSize(new_method_count, |
| 7008 | method_size_, |
| 7009 | method_alignment_); |
| 7010 | const size_t old_methods_ptr_size = (old_methods != nullptr) ? old_size : 0; |
| 7011 | auto* methods = reinterpret_cast<LengthPrefixedArray<ArtMethod>*>( |
Nicolas Geoffray | 48b40cc | 2017-08-07 16:52:40 +0100 | [diff] [blame] | 7012 | class_linker_->GetAllocatorForClassLoader(klass_->GetClassLoader())->Realloc( |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7013 | self_, old_methods, old_methods_ptr_size, new_size)); |
| 7014 | CHECK(methods != nullptr); // Native allocation failure aborts. |
| 7015 | |
| 7016 | PointerSize pointer_size = class_linker_->GetImagePointerSize(); |
| 7017 | if (methods != old_methods) { |
| 7018 | // Maps from heap allocated miranda method to linear alloc miranda method. |
| 7019 | StrideIterator<ArtMethod> out = methods->begin(method_size_, method_alignment_); |
| 7020 | // Copy over the old methods. |
| 7021 | for (auto& m : klass_->GetMethods(pointer_size)) { |
| 7022 | move_table_.emplace(&m, &*out); |
| 7023 | // The CopyFrom is only necessary to not miss read barriers since Realloc won't do read |
| 7024 | // barriers when it copies. |
| 7025 | out->CopyFrom(&m, pointer_size); |
| 7026 | ++out; |
| 7027 | } |
| 7028 | } |
| 7029 | StrideIterator<ArtMethod> out(methods->begin(method_size_, method_alignment_) + old_method_count); |
| 7030 | // Copy over miranda methods before copying vtable since CopyOf may cause thread suspension and |
| 7031 | // we want the roots of the miranda methods to get visited. |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7032 | for (size_t i = 0; i < miranda_methods_.size(); ++i) { |
| 7033 | ArtMethod* mir_method = miranda_methods_[i]; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7034 | ArtMethod& new_method = *out; |
| 7035 | new_method.CopyFrom(mir_method, pointer_size); |
| 7036 | new_method.SetAccessFlags(new_method.GetAccessFlags() | kAccMiranda | kAccCopied); |
| 7037 | DCHECK_NE(new_method.GetAccessFlags() & kAccAbstract, 0u) |
| 7038 | << "Miranda method should be abstract!"; |
| 7039 | move_table_.emplace(mir_method, &new_method); |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7040 | // Update the entry in the method array, as the array will be used for future lookups, |
| 7041 | // where thread suspension is allowed. |
| 7042 | // As such, the array should not contain locally allocated ArtMethod, otherwise the GC |
| 7043 | // would not see them. |
| 7044 | miranda_methods_[i] = &new_method; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7045 | ++out; |
| 7046 | } |
| 7047 | // We need to copy the default methods into our own method table since the runtime requires that |
| 7048 | // every method on a class's vtable be in that respective class's virtual method table. |
| 7049 | // NOTE This means that two classes might have the same implementation of a method from the same |
| 7050 | // interface but will have different ArtMethod*s for them. This also means we cannot compare a |
| 7051 | // default method found on a class with one found on the declaring interface directly and must |
| 7052 | // look at the declaring class to determine if they are the same. |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7053 | for (ScopedArenaVector<ArtMethod*>* methods_vec : {&default_methods_, |
| 7054 | &overriding_default_methods_}) { |
| 7055 | for (size_t i = 0; i < methods_vec->size(); ++i) { |
| 7056 | ArtMethod* def_method = (*methods_vec)[i]; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7057 | ArtMethod& new_method = *out; |
| 7058 | new_method.CopyFrom(def_method, pointer_size); |
| 7059 | // Clear the kAccSkipAccessChecks flag if it is present. Since this class hasn't been |
| 7060 | // verified yet it shouldn't have methods that are skipping access checks. |
| 7061 | // TODO This is rather arbitrary. We should maybe support classes where only some of its |
| 7062 | // methods are skip_access_checks. |
Vladimir Marko | b0a6aee | 2017-10-27 10:34:04 +0100 | [diff] [blame] | 7063 | DCHECK_EQ(new_method.GetAccessFlags() & kAccNative, 0u); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7064 | constexpr uint32_t kSetFlags = kAccDefault | kAccCopied; |
| 7065 | constexpr uint32_t kMaskFlags = ~kAccSkipAccessChecks; |
| 7066 | new_method.SetAccessFlags((new_method.GetAccessFlags() | kSetFlags) & kMaskFlags); |
| 7067 | move_table_.emplace(def_method, &new_method); |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7068 | // Update the entry in the method array, as the array will be used for future lookups, |
| 7069 | // where thread suspension is allowed. |
| 7070 | // As such, the array should not contain locally allocated ArtMethod, otherwise the GC |
| 7071 | // would not see them. |
| 7072 | (*methods_vec)[i] = &new_method; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7073 | ++out; |
| 7074 | } |
| 7075 | } |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7076 | for (ScopedArenaVector<ArtMethod*>* methods_vec : {&default_conflict_methods_, |
| 7077 | &overriding_default_conflict_methods_}) { |
| 7078 | for (size_t i = 0; i < methods_vec->size(); ++i) { |
| 7079 | ArtMethod* conf_method = (*methods_vec)[i]; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7080 | ArtMethod& new_method = *out; |
| 7081 | new_method.CopyFrom(conf_method, pointer_size); |
| 7082 | // This is a type of default method (there are default method impls, just a conflict) so |
| 7083 | // mark this as a default, non-abstract method, since thats what it is. Also clear the |
| 7084 | // kAccSkipAccessChecks bit since this class hasn't been verified yet it shouldn't have |
| 7085 | // methods that are skipping access checks. |
Nicolas Geoffray | 7aca9d5 | 2018-09-07 11:13:33 +0100 | [diff] [blame] | 7086 | // Also clear potential kAccSingleImplementation to avoid CHA trying to inline |
| 7087 | // the default method. |
Vladimir Marko | b0a6aee | 2017-10-27 10:34:04 +0100 | [diff] [blame] | 7088 | DCHECK_EQ(new_method.GetAccessFlags() & kAccNative, 0u); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7089 | constexpr uint32_t kSetFlags = kAccDefault | kAccDefaultConflict | kAccCopied; |
Nicolas Geoffray | 7aca9d5 | 2018-09-07 11:13:33 +0100 | [diff] [blame] | 7090 | constexpr uint32_t kMaskFlags = |
| 7091 | ~(kAccAbstract | kAccSkipAccessChecks | kAccSingleImplementation); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7092 | new_method.SetAccessFlags((new_method.GetAccessFlags() | kSetFlags) & kMaskFlags); |
| 7093 | DCHECK(new_method.IsDefaultConflicting()); |
| 7094 | // The actual method might or might not be marked abstract since we just copied it from a |
| 7095 | // (possibly default) interface method. We need to set it entry point to be the bridge so |
| 7096 | // that the compiler will not invoke the implementation of whatever method we copied from. |
| 7097 | EnsureThrowsInvocationError(class_linker_, &new_method); |
| 7098 | move_table_.emplace(conf_method, &new_method); |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7099 | // Update the entry in the method array, as the array will be used for future lookups, |
| 7100 | // where thread suspension is allowed. |
| 7101 | // As such, the array should not contain locally allocated ArtMethod, otherwise the GC |
| 7102 | // would not see them. |
| 7103 | (*methods_vec)[i] = &new_method; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7104 | ++out; |
| 7105 | } |
| 7106 | } |
| 7107 | methods->SetSize(new_method_count); |
| 7108 | class_linker_->UpdateClassMethods(klass_.Get(), methods); |
| 7109 | } |
| 7110 | |
| 7111 | ObjPtr<mirror::PointerArray> ClassLinker::LinkInterfaceMethodsHelper::UpdateVtable( |
| 7112 | const std::unordered_map<size_t, ClassLinker::MethodTranslation>& default_translations, |
| 7113 | ObjPtr<mirror::PointerArray> old_vtable) { |
| 7114 | // Update the vtable to the new method structures. We can skip this for interfaces since they |
| 7115 | // do not have vtables. |
| 7116 | const size_t old_vtable_count = old_vtable->GetLength(); |
| 7117 | const size_t new_vtable_count = old_vtable_count + |
| 7118 | miranda_methods_.size() + |
| 7119 | default_methods_.size() + |
| 7120 | default_conflict_methods_.size(); |
| 7121 | |
| 7122 | ObjPtr<mirror::PointerArray> vtable = |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 7123 | ObjPtr<mirror::PointerArray>::DownCast(old_vtable->CopyOf(self_, new_vtable_count)); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7124 | if (UNLIKELY(vtable == nullptr)) { |
| 7125 | self_->AssertPendingOOMException(); |
| 7126 | return nullptr; |
| 7127 | } |
| 7128 | |
| 7129 | size_t vtable_pos = old_vtable_count; |
| 7130 | PointerSize pointer_size = class_linker_->GetImagePointerSize(); |
| 7131 | // Update all the newly copied method's indexes so they denote their placement in the vtable. |
| 7132 | for (const ScopedArenaVector<ArtMethod*>& methods_vec : {default_methods_, |
| 7133 | default_conflict_methods_, |
| 7134 | miranda_methods_}) { |
| 7135 | // These are the functions that are not already in the vtable! |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7136 | for (ArtMethod* new_vtable_method : methods_vec) { |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7137 | // Leave the declaring class alone the method's dex_code_item_offset_ and dex_method_index_ |
| 7138 | // fields are references into the dex file the method was defined in. Since the ArtMethod |
| 7139 | // does not store that information it uses declaring_class_->dex_cache_. |
| 7140 | new_vtable_method->SetMethodIndex(0xFFFF & vtable_pos); |
| 7141 | vtable->SetElementPtrSize(vtable_pos, new_vtable_method, pointer_size); |
| 7142 | ++vtable_pos; |
| 7143 | } |
| 7144 | } |
| 7145 | DCHECK_EQ(vtable_pos, new_vtable_count); |
| 7146 | |
| 7147 | // Update old vtable methods. We use the default_translations map to figure out what each |
| 7148 | // vtable entry should be updated to, if they need to be at all. |
| 7149 | for (size_t i = 0; i < old_vtable_count; ++i) { |
| 7150 | ArtMethod* translated_method = vtable->GetElementPtrSize<ArtMethod*>(i, pointer_size); |
| 7151 | // Try and find what we need to change this method to. |
| 7152 | auto translation_it = default_translations.find(i); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7153 | if (translation_it != default_translations.end()) { |
| 7154 | if (translation_it->second.IsInConflict()) { |
| 7155 | // Find which conflict method we are to use for this method. |
| 7156 | MethodNameAndSignatureComparator old_method_comparator( |
| 7157 | translated_method->GetInterfaceMethodIfProxy(pointer_size)); |
| 7158 | // We only need to look through overriding_default_conflict_methods since this is an |
| 7159 | // overridden method we are fixing up here. |
| 7160 | ArtMethod* new_conflict_method = FindSameNameAndSignature( |
| 7161 | old_method_comparator, overriding_default_conflict_methods_); |
| 7162 | CHECK(new_conflict_method != nullptr) << "Expected a conflict method!"; |
| 7163 | translated_method = new_conflict_method; |
| 7164 | } else if (translation_it->second.IsAbstract()) { |
| 7165 | // Find which miranda method we are to use for this method. |
| 7166 | MethodNameAndSignatureComparator old_method_comparator( |
| 7167 | translated_method->GetInterfaceMethodIfProxy(pointer_size)); |
| 7168 | ArtMethod* miranda_method = FindSameNameAndSignature(old_method_comparator, |
| 7169 | miranda_methods_); |
| 7170 | DCHECK(miranda_method != nullptr); |
| 7171 | translated_method = miranda_method; |
| 7172 | } else { |
| 7173 | // Normal default method (changed from an older default or abstract interface method). |
| 7174 | DCHECK(translation_it->second.IsTranslation()); |
| 7175 | translated_method = translation_it->second.GetTranslation(); |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7176 | auto it = move_table_.find(translated_method); |
| 7177 | DCHECK(it != move_table_.end()); |
| 7178 | translated_method = it->second; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7179 | } |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7180 | } else { |
| 7181 | auto it = move_table_.find(translated_method); |
| 7182 | translated_method = (it != move_table_.end()) ? it->second : nullptr; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7183 | } |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7184 | |
| 7185 | if (translated_method != nullptr) { |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7186 | // Make sure the new_methods index is set. |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7187 | if (translated_method->GetMethodIndexDuringLinking() != i) { |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7188 | if (kIsDebugBuild) { |
| 7189 | auto* methods = klass_->GetMethodsPtr(); |
| 7190 | CHECK_LE(reinterpret_cast<uintptr_t>(&*methods->begin(method_size_, method_alignment_)), |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7191 | reinterpret_cast<uintptr_t>(translated_method)); |
| 7192 | CHECK_LT(reinterpret_cast<uintptr_t>(translated_method), |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7193 | reinterpret_cast<uintptr_t>(&*methods->end(method_size_, method_alignment_))); |
| 7194 | } |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7195 | translated_method->SetMethodIndex(0xFFFF & i); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7196 | } |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7197 | vtable->SetElementPtrSize(i, translated_method, pointer_size); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7198 | } |
| 7199 | } |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 7200 | klass_->SetVTable(vtable); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7201 | return vtable; |
| 7202 | } |
| 7203 | |
| 7204 | void ClassLinker::LinkInterfaceMethodsHelper::UpdateIfTable(Handle<mirror::IfTable> iftable) { |
| 7205 | PointerSize pointer_size = class_linker_->GetImagePointerSize(); |
| 7206 | const size_t ifcount = klass_->GetIfTableCount(); |
| 7207 | // Go fix up all the stale iftable pointers. |
| 7208 | for (size_t i = 0; i < ifcount; ++i) { |
| 7209 | for (size_t j = 0, count = iftable->GetMethodArrayCount(i); j < count; ++j) { |
| 7210 | auto* method_array = iftable->GetMethodArray(i); |
| 7211 | auto* m = method_array->GetElementPtrSize<ArtMethod*>(j, pointer_size); |
| 7212 | DCHECK(m != nullptr) << klass_->PrettyClass(); |
| 7213 | auto it = move_table_.find(m); |
| 7214 | if (it != move_table_.end()) { |
| 7215 | auto* new_m = it->second; |
| 7216 | DCHECK(new_m != nullptr) << klass_->PrettyClass(); |
| 7217 | method_array->SetElementPtrSize(j, new_m, pointer_size); |
| 7218 | } |
| 7219 | } |
| 7220 | } |
| 7221 | } |
| 7222 | |
| 7223 | void ClassLinker::LinkInterfaceMethodsHelper::UpdateIMT(ArtMethod** out_imt) { |
| 7224 | // Fix up IMT next. |
| 7225 | for (size_t i = 0; i < ImTable::kSize; ++i) { |
| 7226 | auto it = move_table_.find(out_imt[i]); |
| 7227 | if (it != move_table_.end()) { |
| 7228 | out_imt[i] = it->second; |
| 7229 | } |
| 7230 | } |
| 7231 | } |
| 7232 | |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7233 | // TODO This method needs to be split up into several smaller methods. |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7234 | bool ClassLinker::LinkInterfaceMethods( |
| 7235 | Thread* self, |
| 7236 | Handle<mirror::Class> klass, |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7237 | const std::unordered_map<size_t, ClassLinker::MethodTranslation>& default_translations, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 7238 | bool* out_new_conflict, |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7239 | ArtMethod** out_imt) { |
| 7240 | StackHandleScope<3> hs(self); |
| 7241 | Runtime* const runtime = Runtime::Current(); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7242 | |
| 7243 | const bool is_interface = klass->IsInterface(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7244 | const bool has_superclass = klass->HasSuperClass(); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7245 | const bool fill_tables = !is_interface; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7246 | const size_t super_ifcount = has_superclass ? klass->GetSuperClass()->GetIfTableCount() : 0U; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7247 | const size_t ifcount = klass->GetIfTableCount(); |
| 7248 | |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7249 | Handle<mirror::IfTable> iftable(hs.NewHandle(klass->GetIfTable())); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7250 | |
| 7251 | MutableHandle<mirror::PointerArray> vtable(hs.NewHandle(klass->GetVTableDuringLinking())); |
| 7252 | ArtMethod* const unimplemented_method = runtime->GetImtUnimplementedMethod(); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7253 | ArtMethod* const imt_conflict_method = runtime->GetImtConflictMethod(); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 7254 | // Copy the IMT from the super class if possible. |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7255 | const bool extend_super_iftable = has_superclass; |
| 7256 | if (has_superclass && fill_tables) { |
| 7257 | FillImtFromSuperClass(klass, |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7258 | unimplemented_method, |
| 7259 | imt_conflict_method, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 7260 | out_new_conflict, |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 7261 | out_imt); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 7262 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7263 | // Allocate method arrays before since we don't want miss visiting miranda method roots due to |
| 7264 | // thread suspension. |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7265 | if (fill_tables) { |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7266 | if (!AllocateIfTableMethodArrays(self, klass, iftable)) { |
| 7267 | return false; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7268 | } |
| 7269 | } |
| 7270 | |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7271 | LinkInterfaceMethodsHelper helper(this, klass, self, runtime); |
| 7272 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 7273 | auto* old_cause = self->StartAssertNoThreadSuspension( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7274 | "Copying ArtMethods for LinkInterfaceMethods"); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7275 | // Going in reverse to ensure that we will hit abstract methods that override defaults before the |
| 7276 | // defaults. This means we don't need to do any trickery when creating the Miranda methods, since |
| 7277 | // they will already be null. This has the additional benefit that the declarer of a miranda |
| 7278 | // method will actually declare an abstract method. |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 7279 | for (size_t i = ifcount; i != 0u; ) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7280 | --i; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7281 | DCHECK_LT(i, ifcount); |
| 7282 | |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7283 | size_t num_methods = iftable->GetInterface(i)->NumDeclaredVirtualMethods(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7284 | if (num_methods > 0) { |
| 7285 | StackHandleScope<2> hs2(self); |
| 7286 | const bool is_super = i < super_ifcount; |
| 7287 | const bool super_interface = is_super && extend_super_iftable; |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7288 | // We don't actually create or fill these tables for interfaces, we just copy some methods for |
| 7289 | // conflict methods. Just set this as nullptr in those cases. |
| 7290 | Handle<mirror::PointerArray> method_array(fill_tables |
| 7291 | ? hs2.NewHandle(iftable->GetMethodArray(i)) |
| 7292 | : hs2.NewHandle<mirror::PointerArray>(nullptr)); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7293 | |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 7294 | ArraySlice<ArtMethod> input_virtual_methods; |
Mathieu Chartier | 9865bde | 2015-12-21 09:58:16 -0800 | [diff] [blame] | 7295 | ScopedNullHandle<mirror::PointerArray> null_handle; |
| 7296 | Handle<mirror::PointerArray> input_vtable_array(null_handle); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7297 | int32_t input_array_length = 0; |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 7298 | |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7299 | // TODO Cleanup Needed: In the presence of default methods this optimization is rather dirty |
| 7300 | // and confusing. Default methods should always look through all the superclasses |
| 7301 | // because they are the last choice of an implementation. We get around this by looking |
| 7302 | // at the super-classes iftable methods (copied into method_array previously) when we are |
| 7303 | // 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] | 7304 | bool using_virtuals; |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7305 | if (super_interface || is_interface) { |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 7306 | // 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] | 7307 | // whole vtable. |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 7308 | using_virtuals = true; |
| 7309 | input_virtual_methods = klass->GetDeclaredMethodsSlice(image_pointer_size_); |
| 7310 | input_array_length = input_virtual_methods.size(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7311 | } else { |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 7312 | // For a new interface, however, we need the whole vtable in case a new |
| 7313 | // interface method is implemented in the whole superclass. |
| 7314 | using_virtuals = false; |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 7315 | DCHECK(vtable != nullptr); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7316 | input_vtable_array = vtable; |
| 7317 | input_array_length = input_vtable_array->GetLength(); |
| 7318 | } |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 7319 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7320 | // For each method in interface |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 7321 | for (size_t j = 0; j < num_methods; ++j) { |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 7322 | auto* interface_method = iftable->GetInterface(i)->GetVirtualMethod(j, image_pointer_size_); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 7323 | MethodNameAndSignatureComparator interface_name_comparator( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7324 | interface_method->GetInterfaceMethodIfProxy(image_pointer_size_)); |
Andreas Gampe | 75a7db6 | 2016-09-26 12:04:26 -0700 | [diff] [blame] | 7325 | uint32_t imt_index = ImTable::GetImtIndex(interface_method); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7326 | ArtMethod** imt_ptr = &out_imt[imt_index]; |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 7327 | // For each method listed in the interface's method list, find the |
| 7328 | // matching method in our class's method list. We want to favor the |
| 7329 | // subclass over the superclass, which just requires walking |
| 7330 | // back from the end of the vtable. (This only matters if the |
| 7331 | // superclass defines a private method and this class redefines |
| 7332 | // it -- otherwise it would use the same vtable slot. In .dex files |
| 7333 | // those don't end up in the virtual method table, so it shouldn't |
| 7334 | // matter which direction we go. We walk it backward anyway.) |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7335 | // |
| 7336 | // To find defaults we need to do the same but also go over interfaces. |
| 7337 | bool found_impl = false; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7338 | ArtMethod* vtable_impl = nullptr; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7339 | for (int32_t k = input_array_length - 1; k >= 0; --k) { |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 7340 | ArtMethod* vtable_method = using_virtuals ? |
| 7341 | &input_virtual_methods[k] : |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7342 | input_vtable_array->GetElementPtrSize<ArtMethod*>(k, image_pointer_size_); |
| 7343 | ArtMethod* vtable_method_for_name_comparison = |
| 7344 | vtable_method->GetInterfaceMethodIfProxy(image_pointer_size_); |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 7345 | if (interface_name_comparator.HasSameNameAndSignature( |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 7346 | vtable_method_for_name_comparison)) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 7347 | if (!vtable_method->IsAbstract() && !vtable_method->IsPublic()) { |
Mathieu Chartier | 4d122c1 | 2015-06-17 14:14:36 -0700 | [diff] [blame] | 7348 | // Must do EndAssertNoThreadSuspension before throw since the throw can cause |
| 7349 | // allocations. |
| 7350 | self->EndAssertNoThreadSuspension(old_cause); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7351 | ThrowIllegalAccessError(klass.Get(), |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 7352 | "Method '%s' implementing interface method '%s' is not public", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 7353 | vtable_method->PrettyMethod().c_str(), |
| 7354 | interface_method->PrettyMethod().c_str()); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 7355 | return false; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7356 | } else if (UNLIKELY(vtable_method->IsOverridableByDefaultMethod())) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7357 | // We might have a newer, better, default method for this, so we just skip it. If we |
| 7358 | // are still using this we will select it again when scanning for default methods. To |
| 7359 | // obviate the need to copy the method again we will make a note that we already found |
| 7360 | // a default here. |
| 7361 | // TODO This should be much cleaner. |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7362 | vtable_impl = vtable_method; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7363 | break; |
| 7364 | } else { |
| 7365 | found_impl = true; |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7366 | if (LIKELY(fill_tables)) { |
| 7367 | method_array->SetElementPtrSize(j, vtable_method, image_pointer_size_); |
| 7368 | // Place method in imt if entry is empty, place conflict otherwise. |
| 7369 | SetIMTRef(unimplemented_method, |
| 7370 | imt_conflict_method, |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7371 | vtable_method, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 7372 | /*out*/out_new_conflict, |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7373 | /*out*/imt_ptr); |
| 7374 | } |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 7375 | break; |
| 7376 | } |
| 7377 | } |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7378 | } |
| 7379 | // Continue on to the next method if we are done. |
| 7380 | if (LIKELY(found_impl)) { |
| 7381 | continue; |
| 7382 | } else if (LIKELY(super_interface)) { |
| 7383 | // Don't look for a default implementation when the super-method is implemented directly |
| 7384 | // by the class. |
| 7385 | // |
| 7386 | // See if we can use the superclasses method and skip searching everything else. |
| 7387 | // Note: !found_impl && super_interface |
| 7388 | CHECK(extend_super_iftable); |
| 7389 | // If this is a super_interface method it is possible we shouldn't override it because a |
| 7390 | // superclass could have implemented it directly. We get the method the superclass used |
| 7391 | // to implement this to know if we can override it with a default method. Doing this is |
| 7392 | // safe since we know that the super_iftable is filled in so we can simply pull it from |
| 7393 | // there. We don't bother if this is not a super-classes interface since in that case we |
| 7394 | // have scanned the entire vtable anyway and would have found it. |
| 7395 | // TODO This is rather dirty but it is faster than searching through the entire vtable |
| 7396 | // every time. |
| 7397 | ArtMethod* supers_method = |
| 7398 | method_array->GetElementPtrSize<ArtMethod*>(j, image_pointer_size_); |
| 7399 | DCHECK(supers_method != nullptr); |
| 7400 | DCHECK(interface_name_comparator.HasSameNameAndSignature(supers_method)); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7401 | if (LIKELY(!supers_method->IsOverridableByDefaultMethod())) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7402 | // The method is not overridable by a default method (i.e. it is directly implemented |
| 7403 | // in some class). Therefore move onto the next interface method. |
| 7404 | continue; |
Alex Light | d6c2bfa | 2016-05-02 18:51:34 -0700 | [diff] [blame] | 7405 | } else { |
| 7406 | // If the super-classes method is override-able by a default method we need to keep |
| 7407 | // track of it since though it is override-able it is not guaranteed to be 'overridden'. |
| 7408 | // 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] | 7409 | // to the vtable twice, causing corruption (vtable entries having inconsistent and |
| 7410 | // illegal states, incorrect vtable size, and incorrect or inconsistent iftable entries) |
| 7411 | // in this class and any subclasses. |
Alex Light | d6c2bfa | 2016-05-02 18:51:34 -0700 | [diff] [blame] | 7412 | DCHECK(vtable_impl == nullptr || vtable_impl == supers_method) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 7413 | << "vtable_impl was " << ArtMethod::PrettyMethod(vtable_impl) |
| 7414 | << " and not 'nullptr' or " |
| 7415 | << supers_method->PrettyMethod() |
| 7416 | << " as expected. IFTable appears to be corrupt!"; |
Alex Light | d6c2bfa | 2016-05-02 18:51:34 -0700 | [diff] [blame] | 7417 | vtable_impl = supers_method; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7418 | } |
| 7419 | } |
| 7420 | // 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] | 7421 | ArtMethod* current_method = helper.FindMethod(interface_method, |
| 7422 | interface_name_comparator, |
| 7423 | vtable_impl); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7424 | if (LIKELY(fill_tables)) { |
Alex Light | 1277108 | 2016-01-26 16:07:41 -0800 | [diff] [blame] | 7425 | if (current_method == nullptr && !super_interface) { |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7426 | // We could not find an implementation for this method and since it is a brand new |
| 7427 | // interface we searched the entire vtable (and all default methods) for an |
| 7428 | // 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] | 7429 | current_method = helper.GetOrCreateMirandaMethod(interface_method, |
| 7430 | interface_name_comparator); |
Alex Light | 1277108 | 2016-01-26 16:07:41 -0800 | [diff] [blame] | 7431 | } |
| 7432 | |
| 7433 | if (current_method != nullptr) { |
| 7434 | // We found a default method implementation. Record it in the iftable and IMT. |
| 7435 | method_array->SetElementPtrSize(j, current_method, image_pointer_size_); |
| 7436 | SetIMTRef(unimplemented_method, |
| 7437 | imt_conflict_method, |
Alex Light | 1277108 | 2016-01-26 16:07:41 -0800 | [diff] [blame] | 7438 | current_method, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 7439 | /*out*/out_new_conflict, |
Alex Light | 1277108 | 2016-01-26 16:07:41 -0800 | [diff] [blame] | 7440 | /*out*/imt_ptr); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7441 | } |
| 7442 | } |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7443 | } // For each method in interface end. |
| 7444 | } // if (num_methods > 0) |
| 7445 | } // For each interface. |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7446 | // TODO don't extend virtuals of interface unless necessary (when is it?). |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7447 | if (helper.HasNewVirtuals()) { |
| 7448 | LengthPrefixedArray<ArtMethod>* old_methods = kIsDebugBuild ? klass->GetMethodsPtr() : nullptr; |
| 7449 | helper.ReallocMethods(); // No return value to check. Native allocation failure aborts. |
| 7450 | LengthPrefixedArray<ArtMethod>* methods = kIsDebugBuild ? klass->GetMethodsPtr() : nullptr; |
| 7451 | |
Mathieu Chartier | d4d83b8 | 2015-06-19 20:24:45 -0700 | [diff] [blame] | 7452 | // 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] | 7453 | // suspension assert. |
| 7454 | self->EndAssertNoThreadSuspension(old_cause); |
Mathieu Chartier | d4d83b8 | 2015-06-19 20:24:45 -0700 | [diff] [blame] | 7455 | |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7456 | if (fill_tables) { |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7457 | vtable.Assign(helper.UpdateVtable(default_translations, vtable.Get())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 7458 | if (UNLIKELY(vtable == nullptr)) { |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7459 | // The helper has already called self->AssertPendingOOMException(); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7460 | return false; |
| 7461 | } |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7462 | helper.UpdateIfTable(iftable); |
| 7463 | helper.UpdateIMT(out_imt); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7464 | } |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7465 | |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7466 | helper.CheckNoStaleMethodsInDexCache(); |
| 7467 | helper.ClobberOldMethods(old_methods, methods); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7468 | } else { |
| 7469 | self->EndAssertNoThreadSuspension(old_cause); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7470 | } |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7471 | if (kIsDebugBuild && !is_interface) { |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 7472 | SanityCheckVTable(self, klass, image_pointer_size_); |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 7473 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7474 | return true; |
| 7475 | } |
| 7476 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 7477 | bool ClassLinker::LinkInstanceFields(Thread* self, Handle<mirror::Class> klass) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 7478 | CHECK(klass != nullptr); |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 7479 | return LinkFields(self, klass, false, nullptr); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 7480 | } |
| 7481 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 7482 | 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] | 7483 | CHECK(klass != nullptr); |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 7484 | return LinkFields(self, klass, true, class_size); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 7485 | } |
| 7486 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7487 | struct LinkFieldsComparator { |
Igor Murashkin | 2ffb703 | 2017-11-08 13:35:21 -0800 | [diff] [blame] | 7488 | LinkFieldsComparator() REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 7489 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 7490 | // 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] | 7491 | bool operator()(ArtField* field1, ArtField* field2) |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 7492 | NO_THREAD_SAFETY_ANALYSIS { |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 7493 | // 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] | 7494 | Primitive::Type type1 = field1->GetTypeAsPrimitiveType(); |
| 7495 | Primitive::Type type2 = field2->GetTypeAsPrimitiveType(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 7496 | if (type1 != type2) { |
Vladimir Marko | d577748 | 2014-11-12 17:02:02 +0000 | [diff] [blame] | 7497 | if (type1 == Primitive::kPrimNot) { |
| 7498 | // Reference always goes first. |
| 7499 | return true; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 7500 | } |
Vladimir Marko | d577748 | 2014-11-12 17:02:02 +0000 | [diff] [blame] | 7501 | if (type2 == Primitive::kPrimNot) { |
| 7502 | // Reference always goes first. |
| 7503 | return false; |
| 7504 | } |
| 7505 | size_t size1 = Primitive::ComponentSize(type1); |
| 7506 | size_t size2 = Primitive::ComponentSize(type2); |
| 7507 | if (size1 != size2) { |
| 7508 | // Larger primitive types go first. |
| 7509 | return size1 > size2; |
| 7510 | } |
| 7511 | // Primitive types differ but sizes match. Arbitrarily order by primitive type. |
| 7512 | return type1 < type2; |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7513 | } |
Vladimir Marko | 7a7c1db | 2014-11-17 15:13:34 +0000 | [diff] [blame] | 7514 | // Same basic group? Then sort by dex field index. This is guaranteed to be sorted |
| 7515 | // by name and for equal names by type id index. |
| 7516 | // NOTE: This works also for proxies. Their static fields are assigned appropriate indexes. |
| 7517 | return field1->GetDexFieldIndex() < field2->GetDexFieldIndex(); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7518 | } |
| 7519 | }; |
| 7520 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 7521 | bool ClassLinker::LinkFields(Thread* self, |
| 7522 | Handle<mirror::Class> klass, |
| 7523 | bool is_static, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 7524 | size_t* class_size) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 7525 | self->AllowThreadSuspension(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 7526 | const size_t num_fields = is_static ? klass->NumStaticFields() : klass->NumInstanceFields(); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 7527 | LengthPrefixedArray<ArtField>* const fields = is_static ? klass->GetSFieldsPtr() : |
| 7528 | klass->GetIFieldsPtr(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7529 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 7530 | // Initialize field_offset |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 7531 | MemberOffset field_offset(0); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 7532 | if (is_static) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7533 | field_offset = klass->GetFirstReferenceStaticFieldOffsetDuringLinking(image_pointer_size_); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 7534 | } else { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7535 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 7536 | if (super_class != nullptr) { |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 7537 | CHECK(super_class->IsResolved()) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 7538 | << klass->PrettyClass() << " " << super_class->PrettyClass(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7539 | field_offset = MemberOffset(super_class->GetObjectSize()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7540 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7541 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7542 | |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 7543 | CHECK_EQ(num_fields == 0, fields == nullptr) << klass->PrettyClass(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7544 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7545 | // we want a relatively stable order so that adding new fields |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 7546 | // minimizes disruption of C++ version such as Class and Method. |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 7547 | // |
| 7548 | // The overall sort order order is: |
| 7549 | // 1) All object reference fields, sorted alphabetically. |
| 7550 | // 2) All java long (64-bit) integer fields, sorted alphabetically. |
| 7551 | // 3) All java double (64-bit) floating point fields, sorted alphabetically. |
| 7552 | // 4) All java int (32-bit) integer fields, sorted alphabetically. |
| 7553 | // 5) All java float (32-bit) floating point fields, sorted alphabetically. |
| 7554 | // 6) All java char (16-bit) integer fields, sorted alphabetically. |
| 7555 | // 7) All java short (16-bit) integer fields, sorted alphabetically. |
| 7556 | // 8) All java boolean (8-bit) integer fields, sorted alphabetically. |
| 7557 | // 9) All java byte (8-bit) integer fields, sorted alphabetically. |
| 7558 | // |
| 7559 | // Once the fields are sorted in this order we will attempt to fill any gaps that might be present |
| 7560 | // 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] | 7561 | std::deque<ArtField*> grouped_and_sorted_fields; |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 7562 | const char* old_no_suspend_cause = self->StartAssertNoThreadSuspension( |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 7563 | "Naked ArtField references in deque"); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7564 | for (size_t i = 0; i < num_fields; i++) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 7565 | grouped_and_sorted_fields.push_back(&fields->At(i)); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7566 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 7567 | std::sort(grouped_and_sorted_fields.begin(), grouped_and_sorted_fields.end(), |
| 7568 | LinkFieldsComparator()); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7569 | |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 7570 | // References should be at the front. |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7571 | size_t current_field = 0; |
| 7572 | size_t num_reference_fields = 0; |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 7573 | FieldGaps gaps; |
| 7574 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7575 | for (; current_field < num_fields; current_field++) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 7576 | ArtField* field = grouped_and_sorted_fields.front(); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 7577 | Primitive::Type type = field->GetTypeAsPrimitiveType(); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 7578 | bool isPrimitive = type != Primitive::kPrimNot; |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7579 | if (isPrimitive) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 7580 | break; // past last reference, move on to the next phase |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7581 | } |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7582 | if (UNLIKELY(!IsAligned<sizeof(mirror::HeapReference<mirror::Object>)>( |
| 7583 | field_offset.Uint32Value()))) { |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 7584 | MemberOffset old_offset = field_offset; |
| 7585 | field_offset = MemberOffset(RoundUp(field_offset.Uint32Value(), 4)); |
| 7586 | AddFieldGap(old_offset.Uint32Value(), field_offset.Uint32Value(), &gaps); |
| 7587 | } |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 7588 | DCHECK_ALIGNED(field_offset.Uint32Value(), sizeof(mirror::HeapReference<mirror::Object>)); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7589 | grouped_and_sorted_fields.pop_front(); |
| 7590 | num_reference_fields++; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7591 | field->SetOffset(field_offset); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7592 | field_offset = MemberOffset(field_offset.Uint32Value() + |
| 7593 | sizeof(mirror::HeapReference<mirror::Object>)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7594 | } |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 7595 | // Gaps are stored as a max heap which means that we must shuffle from largest to smallest |
| 7596 | // otherwise we could end up with suboptimal gap fills. |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7597 | ShuffleForward<8>(¤t_field, &field_offset, &grouped_and_sorted_fields, &gaps); |
| 7598 | ShuffleForward<4>(¤t_field, &field_offset, &grouped_and_sorted_fields, &gaps); |
| 7599 | ShuffleForward<2>(¤t_field, &field_offset, &grouped_and_sorted_fields, &gaps); |
| 7600 | ShuffleForward<1>(¤t_field, &field_offset, &grouped_and_sorted_fields, &gaps); |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 7601 | CHECK(grouped_and_sorted_fields.empty()) << "Missed " << grouped_and_sorted_fields.size() << |
| 7602 | " fields."; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 7603 | self->EndAssertNoThreadSuspension(old_no_suspend_cause); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7604 | |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 7605 | // 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] | 7606 | if (!is_static && klass->DescriptorEquals("Ljava/lang/ref/Reference;")) { |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 7607 | // We know there are no non-reference fields in the Reference classes, and we know |
| 7608 | // that 'referent' is alphabetically last, so this is easy... |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 7609 | CHECK_EQ(num_reference_fields, num_fields) << klass->PrettyClass(); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 7610 | CHECK_STREQ(fields->At(num_fields - 1).GetName(), "referent") |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 7611 | << klass->PrettyClass(); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 7612 | --num_reference_fields; |
| 7613 | } |
| 7614 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 7615 | size_t size = field_offset.Uint32Value(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7616 | // Update klass |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 7617 | if (is_static) { |
| 7618 | klass->SetNumReferenceStaticFields(num_reference_fields); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 7619 | *class_size = size; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 7620 | } else { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7621 | klass->SetNumReferenceInstanceFields(num_reference_fields); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7622 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 7623 | if (num_reference_fields == 0 || super_class == nullptr) { |
| 7624 | // 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] | 7625 | // super_class is null iff the class is java.lang.Object. |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 7626 | if (super_class == nullptr || |
| 7627 | (super_class->GetClassFlags() & mirror::kClassFlagNoReferenceFields) != 0) { |
| 7628 | klass->SetClassFlags(klass->GetClassFlags() | mirror::kClassFlagNoReferenceFields); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 7629 | } |
| 7630 | } |
| 7631 | if (kIsDebugBuild) { |
| 7632 | DCHECK_EQ(super_class == nullptr, klass->DescriptorEquals("Ljava/lang/Object;")); |
| 7633 | size_t total_reference_instance_fields = 0; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7634 | ObjPtr<mirror::Class> cur_super = klass.Get(); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 7635 | while (cur_super != nullptr) { |
| 7636 | total_reference_instance_fields += cur_super->NumReferenceInstanceFieldsDuringLinking(); |
| 7637 | cur_super = cur_super->GetSuperClass(); |
| 7638 | } |
| 7639 | if (super_class == nullptr) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 7640 | CHECK_EQ(total_reference_instance_fields, 1u) << klass->PrettyDescriptor(); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 7641 | } else { |
| 7642 | // Check that there is at least num_reference_fields other than Object.class. |
| 7643 | CHECK_GE(total_reference_instance_fields, 1u + num_reference_fields) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 7644 | << klass->PrettyClass(); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 7645 | } |
| 7646 | } |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 7647 | if (!klass->IsVariableSize()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7648 | std::string temp; |
| 7649 | DCHECK_GE(size, sizeof(mirror::Object)) << klass->GetDescriptor(&temp); |
| 7650 | size_t previous_size = klass->GetObjectSize(); |
| 7651 | if (previous_size != 0) { |
| 7652 | // Make sure that we didn't originally have an incorrect size. |
| 7653 | CHECK_EQ(previous_size, size) << klass->GetDescriptor(&temp); |
Mathieu Chartier | 79b4f38 | 2013-10-23 15:21:37 -0700 | [diff] [blame] | 7654 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7655 | klass->SetObjectSize(size); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7656 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7657 | } |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7658 | |
| 7659 | if (kIsDebugBuild) { |
| 7660 | // Make sure that the fields array is ordered by name but all reference |
| 7661 | // offsets are at the beginning as far as alignment allows. |
| 7662 | MemberOffset start_ref_offset = is_static |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7663 | ? klass->GetFirstReferenceStaticFieldOffsetDuringLinking(image_pointer_size_) |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7664 | : klass->GetFirstReferenceInstanceFieldOffset(); |
| 7665 | MemberOffset end_ref_offset(start_ref_offset.Uint32Value() + |
| 7666 | num_reference_fields * |
| 7667 | sizeof(mirror::HeapReference<mirror::Object>)); |
| 7668 | MemberOffset current_ref_offset = start_ref_offset; |
| 7669 | for (size_t i = 0; i < num_fields; i++) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 7670 | ArtField* field = &fields->At(i); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 7671 | VLOG(class_linker) << "LinkFields: " << (is_static ? "static" : "instance") |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 7672 | << " class=" << klass->PrettyClass() << " field=" << field->PrettyField() |
| 7673 | << " offset=" << field->GetOffsetDuringLinking(); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7674 | if (i != 0) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 7675 | ArtField* const prev_field = &fields->At(i - 1); |
Vladimir Marko | 7a7c1db | 2014-11-17 15:13:34 +0000 | [diff] [blame] | 7676 | // NOTE: The field names can be the same. This is not possible in the Java language |
| 7677 | // 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] | 7678 | DCHECK_LE(strcmp(prev_field->GetName(), field->GetName()), 0); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7679 | } |
| 7680 | Primitive::Type type = field->GetTypeAsPrimitiveType(); |
| 7681 | bool is_primitive = type != Primitive::kPrimNot; |
| 7682 | if (klass->DescriptorEquals("Ljava/lang/ref/Reference;") && |
| 7683 | strcmp("referent", field->GetName()) == 0) { |
| 7684 | is_primitive = true; // We lied above, so we have to expect a lie here. |
| 7685 | } |
| 7686 | MemberOffset offset = field->GetOffsetDuringLinking(); |
| 7687 | if (is_primitive) { |
| 7688 | if (offset.Uint32Value() < end_ref_offset.Uint32Value()) { |
| 7689 | // Shuffled before references. |
| 7690 | size_t type_size = Primitive::ComponentSize(type); |
| 7691 | CHECK_LT(type_size, sizeof(mirror::HeapReference<mirror::Object>)); |
| 7692 | CHECK_LT(offset.Uint32Value(), start_ref_offset.Uint32Value()); |
| 7693 | CHECK_LE(offset.Uint32Value() + type_size, start_ref_offset.Uint32Value()); |
| 7694 | CHECK(!IsAligned<sizeof(mirror::HeapReference<mirror::Object>)>(offset.Uint32Value())); |
| 7695 | } |
| 7696 | } else { |
| 7697 | CHECK_EQ(current_ref_offset.Uint32Value(), offset.Uint32Value()); |
| 7698 | current_ref_offset = MemberOffset(current_ref_offset.Uint32Value() + |
| 7699 | sizeof(mirror::HeapReference<mirror::Object>)); |
| 7700 | } |
| 7701 | } |
| 7702 | CHECK_EQ(current_ref_offset.Uint32Value(), end_ref_offset.Uint32Value()); |
| 7703 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7704 | return true; |
| 7705 | } |
| 7706 | |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7707 | // Set the bitmap of reference instance field offsets. |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 7708 | void ClassLinker::CreateReferenceInstanceOffsets(Handle<mirror::Class> klass) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7709 | uint32_t reference_offsets = 0; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7710 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 7711 | // 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] | 7712 | if (super_class != nullptr) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7713 | reference_offsets = super_class->GetReferenceInstanceOffsets(); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 7714 | // Compute reference offsets unless our superclass overflowed. |
| 7715 | if (reference_offsets != mirror::Class::kClassWalkSuper) { |
| 7716 | size_t num_reference_fields = klass->NumReferenceInstanceFieldsDuringLinking(); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7717 | if (num_reference_fields != 0u) { |
| 7718 | // All of the fields that contain object references are guaranteed be grouped in memory |
| 7719 | // starting at an appropriately aligned address after super class object data. |
| 7720 | uint32_t start_offset = RoundUp(super_class->GetObjectSize(), |
| 7721 | sizeof(mirror::HeapReference<mirror::Object>)); |
| 7722 | uint32_t start_bit = (start_offset - mirror::kObjectHeaderSize) / |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 7723 | sizeof(mirror::HeapReference<mirror::Object>); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7724 | if (start_bit + num_reference_fields > 32) { |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 7725 | reference_offsets = mirror::Class::kClassWalkSuper; |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 7726 | } else { |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 7727 | reference_offsets |= (0xffffffffu << start_bit) & |
| 7728 | (0xffffffffu >> (32 - (start_bit + num_reference_fields))); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 7729 | } |
| 7730 | } |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 7731 | } |
| 7732 | } |
Mingyao Yang | faff0f0 | 2014-09-10 12:03:22 -0700 | [diff] [blame] | 7733 | klass->SetReferenceInstanceOffsets(reference_offsets); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7734 | } |
| 7735 | |
Vladimir Marko | 18090d1 | 2018-06-01 16:53:12 +0100 | [diff] [blame] | 7736 | ObjPtr<mirror::String> ClassLinker::DoResolveString(dex::StringIndex string_idx, |
| 7737 | ObjPtr<mirror::DexCache> dex_cache) { |
| 7738 | StackHandleScope<1> hs(Thread::Current()); |
| 7739 | Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(dex_cache)); |
| 7740 | return DoResolveString(string_idx, h_dex_cache); |
| 7741 | } |
| 7742 | |
| 7743 | ObjPtr<mirror::String> ClassLinker::DoResolveString(dex::StringIndex string_idx, |
| 7744 | Handle<mirror::DexCache> dex_cache) { |
Vladimir Marko | a64b52d | 2017-12-08 16:27:49 +0000 | [diff] [blame] | 7745 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 7746 | uint32_t utf16_length; |
| 7747 | const char* utf8_data = dex_file.StringDataAndUtf16LengthByIdx(string_idx, &utf16_length); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7748 | ObjPtr<mirror::String> string = intern_table_->InternStrong(utf16_length, utf8_data); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 7749 | if (string != nullptr) { |
| 7750 | dex_cache->SetResolvedString(string_idx, string); |
| 7751 | } |
Vladimir Marko | 28e012a | 2017-12-07 11:22:59 +0000 | [diff] [blame] | 7752 | return string; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 7753 | } |
| 7754 | |
Vladimir Marko | 18090d1 | 2018-06-01 16:53:12 +0100 | [diff] [blame] | 7755 | ObjPtr<mirror::String> ClassLinker::DoLookupString(dex::StringIndex string_idx, |
| 7756 | ObjPtr<mirror::DexCache> dex_cache) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 7757 | DCHECK(dex_cache != nullptr); |
Vladimir Marko | a64b52d | 2017-12-08 16:27:49 +0000 | [diff] [blame] | 7758 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 7759 | uint32_t utf16_length; |
| 7760 | const char* utf8_data = dex_file.StringDataAndUtf16LengthByIdx(string_idx, &utf16_length); |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 7761 | ObjPtr<mirror::String> string = |
| 7762 | intern_table_->LookupStrong(Thread::Current(), utf16_length, utf8_data); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 7763 | if (string != nullptr) { |
| 7764 | dex_cache->SetResolvedString(string_idx, string); |
| 7765 | } |
Vladimir Marko | 28e012a | 2017-12-07 11:22:59 +0000 | [diff] [blame] | 7766 | return string; |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 7767 | } |
| 7768 | |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 7769 | ObjPtr<mirror::Class> ClassLinker::DoLookupResolvedType(dex::TypeIndex type_idx, |
Vladimir Marko | 09c5ca4 | 2018-05-31 15:15:31 +0100 | [diff] [blame] | 7770 | ObjPtr<mirror::Class> referrer) { |
| 7771 | return DoLookupResolvedType(type_idx, referrer->GetDexCache(), referrer->GetClassLoader()); |
| 7772 | } |
| 7773 | |
| 7774 | ObjPtr<mirror::Class> ClassLinker::DoLookupResolvedType(dex::TypeIndex type_idx, |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 7775 | ObjPtr<mirror::DexCache> dex_cache, |
| 7776 | ObjPtr<mirror::ClassLoader> class_loader) { |
| 7777 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
| 7778 | const char* descriptor = dex_file.StringByTypeIdx(type_idx); |
| 7779 | DCHECK_NE(*descriptor, '\0') << "descriptor is empty string"; |
| 7780 | ObjPtr<mirror::Class> type = nullptr; |
| 7781 | if (descriptor[1] == '\0') { |
| 7782 | // only the descriptors of primitive types should be 1 character long, also avoid class lookup |
| 7783 | // for primitive classes that aren't backed by dex files. |
| 7784 | type = FindPrimitiveClass(descriptor[0]); |
| 7785 | } else { |
| 7786 | Thread* const self = Thread::Current(); |
| 7787 | DCHECK(self != nullptr); |
| 7788 | const size_t hash = ComputeModifiedUtf8Hash(descriptor); |
| 7789 | // Find the class in the loaded classes table. |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 7790 | type = LookupClass(self, descriptor, hash, class_loader); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 7791 | } |
| 7792 | if (type != nullptr) { |
| 7793 | if (type->IsResolved()) { |
| 7794 | dex_cache->SetResolvedType(type_idx, type); |
Mathieu Chartier | b890130 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 7795 | } else { |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 7796 | type = nullptr; |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 7797 | } |
Mathieu Chartier | b890130 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 7798 | } |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 7799 | return type; |
Mathieu Chartier | b890130 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 7800 | } |
| 7801 | |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 7802 | ObjPtr<mirror::Class> ClassLinker::DoResolveType(dex::TypeIndex type_idx, |
Vladimir Marko | 09c5ca4 | 2018-05-31 15:15:31 +0100 | [diff] [blame] | 7803 | ObjPtr<mirror::Class> referrer) { |
| 7804 | StackHandleScope<2> hs(Thread::Current()); |
| 7805 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache())); |
| 7806 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(referrer->GetClassLoader())); |
| 7807 | return DoResolveType(type_idx, dex_cache, class_loader); |
| 7808 | } |
| 7809 | |
| 7810 | ObjPtr<mirror::Class> ClassLinker::DoResolveType(dex::TypeIndex type_idx, |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 7811 | Handle<mirror::DexCache> dex_cache, |
| 7812 | Handle<mirror::ClassLoader> class_loader) { |
| 7813 | Thread* self = Thread::Current(); |
| 7814 | const char* descriptor = dex_cache->GetDexFile()->StringByTypeIdx(type_idx); |
| 7815 | ObjPtr<mirror::Class> resolved = FindClass(self, descriptor, class_loader); |
| 7816 | if (resolved != nullptr) { |
| 7817 | // TODO: we used to throw here if resolved's class loader was not the |
| 7818 | // boot class loader. This was to permit different classes with the |
| 7819 | // same name to be loaded simultaneously by different loaders |
| 7820 | dex_cache->SetResolvedType(type_idx, resolved); |
| 7821 | } else { |
| 7822 | CHECK(self->IsExceptionPending()) |
| 7823 | << "Expected pending exception for failed resolution of: " << descriptor; |
| 7824 | // Convert a ClassNotFoundException to a NoClassDefFoundError. |
| 7825 | StackHandleScope<1> hs(self); |
| 7826 | Handle<mirror::Throwable> cause(hs.NewHandle(self->GetException())); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 7827 | if (cause->InstanceOf(GetClassRoot(ClassRoot::kJavaLangClassNotFoundException, this))) { |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 7828 | DCHECK(resolved == nullptr); // No Handle needed to preserve resolved. |
| 7829 | self->ClearException(); |
| 7830 | ThrowNoClassDefFoundError("Failed resolution of: %s", descriptor); |
| 7831 | self->GetException()->SetCause(cause.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 7832 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7833 | } |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 7834 | DCHECK((resolved == nullptr) || resolved->IsResolved()) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 7835 | << resolved->PrettyDescriptor() << " " << resolved->GetStatus(); |
Vladimir Marko | 28e012a | 2017-12-07 11:22:59 +0000 | [diff] [blame] | 7836 | return resolved; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7837 | } |
| 7838 | |
Nicolas Geoffray | ea179f4 | 2018-02-08 22:30:18 +0000 | [diff] [blame] | 7839 | ArtMethod* ClassLinker::FindResolvedMethod(ObjPtr<mirror::Class> klass, |
| 7840 | ObjPtr<mirror::DexCache> dex_cache, |
| 7841 | ObjPtr<mirror::ClassLoader> class_loader, |
| 7842 | uint32_t method_idx) { |
| 7843 | // Search for the method using dex_cache and method_idx. The Class::Find*Method() |
| 7844 | // functions can optimize the search if the dex_cache is the same as the DexCache |
| 7845 | // of the class, with fall-back to name and signature search otherwise. |
| 7846 | ArtMethod* resolved = nullptr; |
| 7847 | if (klass->IsInterface()) { |
| 7848 | resolved = klass->FindInterfaceMethod(dex_cache, method_idx, image_pointer_size_); |
| 7849 | } else { |
| 7850 | resolved = klass->FindClassMethod(dex_cache, method_idx, image_pointer_size_); |
| 7851 | } |
| 7852 | DCHECK(resolved == nullptr || resolved->GetDeclaringClassUnchecked() != nullptr); |
David Brazdil | 8ce3bfa | 2018-03-12 18:01:18 +0000 | [diff] [blame] | 7853 | if (resolved != nullptr && |
Narayan Kamath | f5f1f80 | 2018-04-03 15:23:46 +0100 | [diff] [blame] | 7854 | hiddenapi::GetMemberAction( |
| 7855 | resolved, class_loader, dex_cache, hiddenapi::kLinking) == hiddenapi::kDeny) { |
David Brazdil | 8ce3bfa | 2018-03-12 18:01:18 +0000 | [diff] [blame] | 7856 | resolved = nullptr; |
| 7857 | } |
Nicolas Geoffray | ea179f4 | 2018-02-08 22:30:18 +0000 | [diff] [blame] | 7858 | if (resolved != nullptr) { |
| 7859 | // In case of jmvti, the dex file gets verified before being registered, so first |
| 7860 | // check if it's registered before checking class tables. |
| 7861 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Nicolas Geoffray | befa309 | 2018-02-22 14:50:01 +0000 | [diff] [blame] | 7862 | DCHECK(!IsDexFileRegistered(Thread::Current(), dex_file) || |
| 7863 | FindClassTable(Thread::Current(), dex_cache) == ClassTableForClassLoader(class_loader)) |
Nicolas Geoffray | ea179f4 | 2018-02-08 22:30:18 +0000 | [diff] [blame] | 7864 | << "DexFile referrer: " << dex_file.GetLocation() |
| 7865 | << " ClassLoader: " << DescribeLoaders(class_loader, ""); |
| 7866 | // Be a good citizen and update the dex cache to speed subsequent calls. |
| 7867 | dex_cache->SetResolvedMethod(method_idx, resolved, image_pointer_size_); |
Nicolas Geoffray | befa309 | 2018-02-22 14:50:01 +0000 | [diff] [blame] | 7868 | // Disable the following invariant check as the verifier breaks it. b/73760543 |
| 7869 | // const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx); |
| 7870 | // DCHECK(LookupResolvedType(method_id.class_idx_, dex_cache, class_loader) != nullptr) |
| 7871 | // << "Method: " << resolved->PrettyMethod() << ", " |
| 7872 | // << "Class: " << klass->PrettyClass() << " (" << klass->GetStatus() << "), " |
| 7873 | // << "DexFile referrer: " << dex_file.GetLocation(); |
Nicolas Geoffray | ea179f4 | 2018-02-08 22:30:18 +0000 | [diff] [blame] | 7874 | } |
| 7875 | return resolved; |
| 7876 | } |
| 7877 | |
David Brazdil | 4525e0b | 2018-04-05 16:57:32 +0100 | [diff] [blame] | 7878 | // Returns true if `method` is either null or hidden. |
| 7879 | // Does not print any warnings if it is hidden. |
| 7880 | static bool CheckNoSuchMethod(ArtMethod* method, |
| 7881 | ObjPtr<mirror::DexCache> dex_cache, |
| 7882 | ObjPtr<mirror::ClassLoader> class_loader) |
| 7883 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 7884 | return method == nullptr || |
| 7885 | hiddenapi::GetMemberAction(method, |
| 7886 | class_loader, |
| 7887 | dex_cache, |
| 7888 | hiddenapi::kNone) // do not print warnings |
| 7889 | == hiddenapi::kDeny; |
| 7890 | } |
| 7891 | |
| 7892 | ArtMethod* ClassLinker::FindIncompatibleMethod(ObjPtr<mirror::Class> klass, |
| 7893 | ObjPtr<mirror::DexCache> dex_cache, |
| 7894 | ObjPtr<mirror::ClassLoader> class_loader, |
| 7895 | uint32_t method_idx) { |
| 7896 | if (klass->IsInterface()) { |
| 7897 | ArtMethod* method = klass->FindClassMethod(dex_cache, method_idx, image_pointer_size_); |
| 7898 | return CheckNoSuchMethod(method, dex_cache, class_loader) ? nullptr : method; |
| 7899 | } else { |
| 7900 | // If there was an interface method with the same signature, we would have |
| 7901 | // found it in the "copied" methods. Only DCHECK that the interface method |
| 7902 | // really does not exist. |
| 7903 | if (kIsDebugBuild) { |
| 7904 | ArtMethod* method = |
| 7905 | klass->FindInterfaceMethod(dex_cache, method_idx, image_pointer_size_); |
| 7906 | DCHECK(CheckNoSuchMethod(method, dex_cache, class_loader)); |
| 7907 | } |
| 7908 | return nullptr; |
| 7909 | } |
| 7910 | } |
| 7911 | |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 7912 | template <ClassLinker::ResolveMode kResolveMode> |
Vladimir Marko | 8901119 | 2017-12-11 13:45:05 +0000 | [diff] [blame] | 7913 | ArtMethod* ClassLinker::ResolveMethod(uint32_t method_idx, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7914 | Handle<mirror::DexCache> dex_cache, |
| 7915 | Handle<mirror::ClassLoader> class_loader, |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 7916 | ArtMethod* referrer, |
| 7917 | InvokeType type) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 7918 | DCHECK(dex_cache != nullptr); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 7919 | DCHECK(referrer == nullptr || !referrer->IsProxyMethod()); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 7920 | // Check for hit in the dex cache. |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 7921 | PointerSize pointer_size = image_pointer_size_; |
| 7922 | ArtMethod* resolved = dex_cache->GetResolvedMethod(method_idx, pointer_size); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 7923 | Thread::PoisonObjectPointersIfDebug(); |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 7924 | DCHECK(resolved == nullptr || !resolved->IsRuntimeMethod()); |
| 7925 | bool valid_dex_cache_method = resolved != nullptr; |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 7926 | if (kResolveMode == ResolveMode::kNoChecks && valid_dex_cache_method) { |
| 7927 | // We have a valid method from the DexCache and no checks to perform. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7928 | DCHECK(resolved->GetDeclaringClassUnchecked() != nullptr) << resolved->GetDexMethodIndex(); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 7929 | return resolved; |
| 7930 | } |
Vladimir Marko | 8901119 | 2017-12-11 13:45:05 +0000 | [diff] [blame] | 7931 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 7932 | const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 7933 | ObjPtr<mirror::Class> klass = nullptr; |
| 7934 | if (valid_dex_cache_method) { |
| 7935 | // We have a valid method from the DexCache but we need to perform ICCE and IAE checks. |
| 7936 | DCHECK(resolved->GetDeclaringClassUnchecked() != nullptr) << resolved->GetDexMethodIndex(); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 7937 | klass = LookupResolvedType(method_id.class_idx_, dex_cache.Get(), class_loader.Get()); |
Vladimir Marko | 6f1bd46 | 2017-12-06 17:45:03 +0000 | [diff] [blame] | 7938 | if (UNLIKELY(klass == nullptr)) { |
Nicolas Geoffray | befa309 | 2018-02-22 14:50:01 +0000 | [diff] [blame] | 7939 | // We normaly should not end up here. However the verifier currently doesn't guarantee |
| 7940 | // the invariant of having the klass in the class table. b/73760543 |
| 7941 | klass = ResolveType(method_id.class_idx_, dex_cache, class_loader); |
Vladimir Marko | 6f1bd46 | 2017-12-06 17:45:03 +0000 | [diff] [blame] | 7942 | } |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 7943 | } else { |
| 7944 | // The method was not in the DexCache, resolve the declaring class. |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 7945 | klass = ResolveType(method_id.class_idx_, dex_cache, class_loader); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 7946 | if (klass == nullptr) { |
| 7947 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 7948 | return nullptr; |
| 7949 | } |
| 7950 | } |
| 7951 | |
| 7952 | // Check if the invoke type matches the class type. |
| 7953 | if (kResolveMode == ResolveMode::kCheckICCEAndIAE && |
| 7954 | CheckInvokeClassMismatch</* kThrow */ true>( |
| 7955 | dex_cache.Get(), type, [klass]() { return klass; })) { |
Elliott Hughes | cc5f9a9 | 2011-09-28 19:17:29 -0700 | [diff] [blame] | 7956 | DCHECK(Thread::Current()->IsExceptionPending()); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 7957 | return nullptr; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 7958 | } |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 7959 | |
| 7960 | if (!valid_dex_cache_method) { |
Nicolas Geoffray | ea179f4 | 2018-02-08 22:30:18 +0000 | [diff] [blame] | 7961 | resolved = FindResolvedMethod(klass, dex_cache.Get(), class_loader.Get(), method_idx); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 7962 | } |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 7963 | |
| 7964 | // Note: We can check for IllegalAccessError only if we have a referrer. |
| 7965 | if (kResolveMode == ResolveMode::kCheckICCEAndIAE && resolved != nullptr && referrer != nullptr) { |
| 7966 | ObjPtr<mirror::Class> methods_class = resolved->GetDeclaringClass(); |
| 7967 | ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass(); |
| 7968 | if (!referring_class->CheckResolvedMethodAccess(methods_class, |
| 7969 | resolved, |
| 7970 | dex_cache.Get(), |
| 7971 | method_idx, |
| 7972 | type)) { |
| 7973 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 7974 | return nullptr; |
| 7975 | } |
| 7976 | } |
| 7977 | |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 7978 | // If we found a method, check for incompatible class changes. |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 7979 | if (LIKELY(resolved != nullptr) && |
| 7980 | LIKELY(kResolveMode == ResolveMode::kNoChecks || |
| 7981 | !resolved->CheckIncompatibleClassChange(type))) { |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 7982 | return resolved; |
| 7983 | } else { |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 7984 | // If we had a method, or if we can find one with another lookup type, |
| 7985 | // it's an incompatible-class-change error. |
| 7986 | if (resolved == nullptr) { |
David Brazdil | 4525e0b | 2018-04-05 16:57:32 +0100 | [diff] [blame] | 7987 | resolved = FindIncompatibleMethod(klass, dex_cache.Get(), class_loader.Get(), method_idx); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 7988 | } |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 7989 | if (resolved != nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7990 | ThrowIncompatibleClassChangeError(type, resolved->GetInvokeType(), resolved, referrer); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 7991 | } else { |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 7992 | // We failed to find the method (using all lookup types), so throw a NoSuchMethodError. |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 7993 | const char* name = dex_file.StringDataByIdx(method_id.name_idx_); |
| 7994 | const Signature signature = dex_file.GetMethodSignature(method_id); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 7995 | ThrowNoSuchMethodError(type, klass, name, signature); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 7996 | } |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 7997 | Thread::Current()->AssertPendingException(); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 7998 | return nullptr; |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 7999 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 8000 | } |
| 8001 | |
Vladimir Marko | 8901119 | 2017-12-11 13:45:05 +0000 | [diff] [blame] | 8002 | ArtMethod* ClassLinker::ResolveMethodWithoutInvokeType(uint32_t method_idx, |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 8003 | Handle<mirror::DexCache> dex_cache, |
| 8004 | Handle<mirror::ClassLoader> class_loader) { |
| 8005 | ArtMethod* resolved = dex_cache->GetResolvedMethod(method_idx, image_pointer_size_); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 8006 | Thread::PoisonObjectPointersIfDebug(); |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 8007 | if (resolved != nullptr) { |
| 8008 | DCHECK(!resolved->IsRuntimeMethod()); |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 8009 | DCHECK(resolved->GetDeclaringClassUnchecked() != nullptr) << resolved->GetDexMethodIndex(); |
| 8010 | return resolved; |
| 8011 | } |
| 8012 | // Fail, get the declaring class. |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 8013 | const DexFile::MethodId& method_id = dex_cache->GetDexFile()->GetMethodId(method_idx); |
| 8014 | ObjPtr<mirror::Class> klass = ResolveType(method_id.class_idx_, dex_cache, class_loader); |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 8015 | if (klass == nullptr) { |
| 8016 | Thread::Current()->AssertPendingException(); |
| 8017 | return nullptr; |
| 8018 | } |
| 8019 | if (klass->IsInterface()) { |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 8020 | resolved = klass->FindInterfaceMethod(dex_cache.Get(), method_idx, image_pointer_size_); |
| 8021 | } else { |
| 8022 | resolved = klass->FindClassMethod(dex_cache.Get(), method_idx, image_pointer_size_); |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 8023 | } |
David Brazdil | 8ce3bfa | 2018-03-12 18:01:18 +0000 | [diff] [blame] | 8024 | if (resolved != nullptr && |
Narayan Kamath | f5f1f80 | 2018-04-03 15:23:46 +0100 | [diff] [blame] | 8025 | hiddenapi::GetMemberAction( |
| 8026 | resolved, class_loader.Get(), dex_cache.Get(), hiddenapi::kLinking) == hiddenapi::kDeny) { |
David Brazdil | 8ce3bfa | 2018-03-12 18:01:18 +0000 | [diff] [blame] | 8027 | resolved = nullptr; |
| 8028 | } |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 8029 | return resolved; |
| 8030 | } |
| 8031 | |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 8032 | ArtField* ClassLinker::LookupResolvedField(uint32_t field_idx, |
| 8033 | ObjPtr<mirror::DexCache> dex_cache, |
| 8034 | ObjPtr<mirror::ClassLoader> class_loader, |
| 8035 | bool is_static) { |
| 8036 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
| 8037 | const DexFile::FieldId& field_id = dex_file.GetFieldId(field_idx); |
| 8038 | ObjPtr<mirror::Class> klass = dex_cache->GetResolvedType(field_id.class_idx_); |
| 8039 | if (klass == nullptr) { |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 8040 | klass = LookupResolvedType(field_id.class_idx_, dex_cache, class_loader); |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 8041 | } |
| 8042 | if (klass == nullptr) { |
| 8043 | // The class has not been resolved yet, so the field is also unresolved. |
| 8044 | return nullptr; |
| 8045 | } |
| 8046 | DCHECK(klass->IsResolved()); |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 8047 | |
David Brazdil | 1ab0fa8 | 2018-05-04 11:28:03 +0100 | [diff] [blame] | 8048 | return FindResolvedField(klass, dex_cache, class_loader, field_idx, is_static); |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 8049 | } |
| 8050 | |
Vladimir Marko | e11dd50 | 2017-12-08 14:09:45 +0000 | [diff] [blame] | 8051 | ArtField* ClassLinker::ResolveField(uint32_t field_idx, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8052 | Handle<mirror::DexCache> dex_cache, |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 8053 | Handle<mirror::ClassLoader> class_loader, |
| 8054 | bool is_static) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8055 | DCHECK(dex_cache != nullptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8056 | ArtField* resolved = dex_cache->GetResolvedField(field_idx, image_pointer_size_); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 8057 | Thread::PoisonObjectPointersIfDebug(); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 8058 | if (resolved != nullptr) { |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 8059 | return resolved; |
| 8060 | } |
Vladimir Marko | e11dd50 | 2017-12-08 14:09:45 +0000 | [diff] [blame] | 8061 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 8062 | const DexFile::FieldId& field_id = dex_file.GetFieldId(field_idx); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 8063 | ObjPtr<mirror::Class> klass = ResolveType(field_id.class_idx_, dex_cache, class_loader); |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 8064 | if (klass == nullptr) { |
Ian Rogers | 9f1ab12 | 2011-12-12 08:52:43 -0800 | [diff] [blame] | 8065 | DCHECK(Thread::Current()->IsExceptionPending()); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 8066 | return nullptr; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 8067 | } |
| 8068 | |
David Brazdil | 1ab0fa8 | 2018-05-04 11:28:03 +0100 | [diff] [blame] | 8069 | resolved = FindResolvedField(klass, dex_cache.Get(), class_loader.Get(), field_idx, is_static); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 8070 | if (resolved == nullptr) { |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 8071 | const char* name = dex_file.GetFieldName(field_id); |
| 8072 | const char* type = dex_file.GetFieldTypeDescriptor(field_id); |
David Brazdil | 8ce3bfa | 2018-03-12 18:01:18 +0000 | [diff] [blame] | 8073 | ThrowNoSuchFieldError(is_static ? "static " : "instance ", klass, type, name); |
David Brazdil | 8ce3bfa | 2018-03-12 18:01:18 +0000 | [diff] [blame] | 8074 | } |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 8075 | return resolved; |
| 8076 | } |
| 8077 | |
Vladimir Marko | e11dd50 | 2017-12-08 14:09:45 +0000 | [diff] [blame] | 8078 | ArtField* ClassLinker::ResolveFieldJLS(uint32_t field_idx, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8079 | Handle<mirror::DexCache> dex_cache, |
| 8080 | Handle<mirror::ClassLoader> class_loader) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8081 | DCHECK(dex_cache != nullptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8082 | ArtField* resolved = dex_cache->GetResolvedField(field_idx, image_pointer_size_); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 8083 | Thread::PoisonObjectPointersIfDebug(); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 8084 | if (resolved != nullptr) { |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 8085 | return resolved; |
| 8086 | } |
Vladimir Marko | e11dd50 | 2017-12-08 14:09:45 +0000 | [diff] [blame] | 8087 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 8088 | const DexFile::FieldId& field_id = dex_file.GetFieldId(field_idx); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 8089 | ObjPtr<mirror::Class> klass = ResolveType(field_id.class_idx_, dex_cache, class_loader); |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 8090 | if (klass == nullptr) { |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 8091 | DCHECK(Thread::Current()->IsExceptionPending()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 8092 | return nullptr; |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 8093 | } |
| 8094 | |
David Brazdil | 1ab0fa8 | 2018-05-04 11:28:03 +0100 | [diff] [blame] | 8095 | resolved = FindResolvedFieldJLS(klass, dex_cache.Get(), class_loader.Get(), field_idx); |
| 8096 | if (resolved == nullptr) { |
| 8097 | const char* name = dex_file.GetFieldName(field_id); |
| 8098 | const char* type = dex_file.GetFieldTypeDescriptor(field_id); |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 8099 | ThrowNoSuchFieldError("", klass, type, name); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 8100 | } |
| 8101 | return resolved; |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 8102 | } |
| 8103 | |
David Brazdil | 1ab0fa8 | 2018-05-04 11:28:03 +0100 | [diff] [blame] | 8104 | ArtField* ClassLinker::FindResolvedField(ObjPtr<mirror::Class> klass, |
| 8105 | ObjPtr<mirror::DexCache> dex_cache, |
| 8106 | ObjPtr<mirror::ClassLoader> class_loader, |
| 8107 | uint32_t field_idx, |
| 8108 | bool is_static) { |
| 8109 | ArtField* resolved = nullptr; |
| 8110 | Thread* self = is_static ? Thread::Current() : nullptr; |
| 8111 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
| 8112 | |
| 8113 | resolved = is_static ? mirror::Class::FindStaticField(self, klass, dex_cache, field_idx) |
| 8114 | : klass->FindInstanceField(dex_cache, field_idx); |
| 8115 | |
| 8116 | if (resolved == nullptr) { |
| 8117 | const DexFile::FieldId& field_id = dex_file.GetFieldId(field_idx); |
| 8118 | const char* name = dex_file.GetFieldName(field_id); |
| 8119 | const char* type = dex_file.GetFieldTypeDescriptor(field_id); |
| 8120 | resolved = is_static ? mirror::Class::FindStaticField(self, klass, name, type) |
| 8121 | : klass->FindInstanceField(name, type); |
| 8122 | } |
| 8123 | |
| 8124 | if (resolved != nullptr && |
| 8125 | hiddenapi::GetMemberAction( |
| 8126 | resolved, class_loader, dex_cache, hiddenapi::kLinking) == hiddenapi::kDeny) { |
| 8127 | resolved = nullptr; |
| 8128 | } |
| 8129 | |
| 8130 | if (resolved != nullptr) { |
| 8131 | dex_cache->SetResolvedField(field_idx, resolved, image_pointer_size_); |
| 8132 | } |
| 8133 | |
| 8134 | return resolved; |
| 8135 | } |
| 8136 | |
| 8137 | ArtField* ClassLinker::FindResolvedFieldJLS(ObjPtr<mirror::Class> klass, |
| 8138 | ObjPtr<mirror::DexCache> dex_cache, |
| 8139 | ObjPtr<mirror::ClassLoader> class_loader, |
| 8140 | uint32_t field_idx) { |
| 8141 | ArtField* resolved = nullptr; |
| 8142 | Thread* self = Thread::Current(); |
| 8143 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
| 8144 | const DexFile::FieldId& field_id = dex_file.GetFieldId(field_idx); |
| 8145 | |
| 8146 | const char* name = dex_file.GetFieldName(field_id); |
| 8147 | const char* type = dex_file.GetFieldTypeDescriptor(field_id); |
| 8148 | resolved = mirror::Class::FindField(self, klass, name, type); |
| 8149 | |
| 8150 | if (resolved != nullptr && |
| 8151 | hiddenapi::GetMemberAction( |
| 8152 | resolved, class_loader, dex_cache, hiddenapi::kLinking) == hiddenapi::kDeny) { |
| 8153 | resolved = nullptr; |
| 8154 | } |
| 8155 | |
| 8156 | if (resolved != nullptr) { |
| 8157 | dex_cache->SetResolvedField(field_idx, resolved, image_pointer_size_); |
| 8158 | } |
| 8159 | |
| 8160 | return resolved; |
| 8161 | } |
| 8162 | |
Vladimir Marko | af94020 | 2017-12-08 15:01:18 +0000 | [diff] [blame] | 8163 | ObjPtr<mirror::MethodType> ClassLinker::ResolveMethodType( |
| 8164 | Thread* self, |
Orion Hodson | 06d10a7 | 2018-05-14 08:53:38 +0100 | [diff] [blame] | 8165 | dex::ProtoIndex proto_idx, |
Vladimir Marko | af94020 | 2017-12-08 15:01:18 +0000 | [diff] [blame] | 8166 | Handle<mirror::DexCache> dex_cache, |
| 8167 | Handle<mirror::ClassLoader> class_loader) { |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 8168 | DCHECK(Runtime::Current()->IsMethodHandlesEnabled()); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8169 | DCHECK(dex_cache != nullptr); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 8170 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 8171 | ObjPtr<mirror::MethodType> resolved = dex_cache->GetResolvedMethodType(proto_idx); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 8172 | if (resolved != nullptr) { |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 8173 | return resolved; |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 8174 | } |
| 8175 | |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 8176 | StackHandleScope<4> hs(self); |
| 8177 | |
| 8178 | // First resolve the return type. |
Vladimir Marko | af94020 | 2017-12-08 15:01:18 +0000 | [diff] [blame] | 8179 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 8180 | const DexFile::ProtoId& proto_id = dex_file.GetProtoId(proto_idx); |
| 8181 | Handle<mirror::Class> return_type(hs.NewHandle( |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 8182 | ResolveType(proto_id.return_type_idx_, dex_cache, class_loader))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8183 | if (return_type == nullptr) { |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 8184 | DCHECK(self->IsExceptionPending()); |
| 8185 | return nullptr; |
| 8186 | } |
| 8187 | |
| 8188 | // Then resolve the argument types. |
| 8189 | // |
| 8190 | // TODO: Is there a better way to figure out the number of method arguments |
| 8191 | // other than by looking at the shorty ? |
| 8192 | const size_t num_method_args = strlen(dex_file.StringDataByIdx(proto_id.shorty_idx_)) - 1; |
| 8193 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 8194 | ObjPtr<mirror::Class> array_of_class = GetClassRoot<mirror::ObjectArray<mirror::Class>>(this); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 8195 | Handle<mirror::ObjectArray<mirror::Class>> method_params(hs.NewHandle( |
| 8196 | mirror::ObjectArray<mirror::Class>::Alloc(self, array_of_class, num_method_args))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8197 | if (method_params == nullptr) { |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 8198 | DCHECK(self->IsExceptionPending()); |
| 8199 | return nullptr; |
| 8200 | } |
| 8201 | |
| 8202 | DexFileParameterIterator it(dex_file, proto_id); |
| 8203 | int32_t i = 0; |
| 8204 | MutableHandle<mirror::Class> param_class = hs.NewHandle<mirror::Class>(nullptr); |
| 8205 | for (; it.HasNext(); it.Next()) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 8206 | const dex::TypeIndex type_idx = it.GetTypeIdx(); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 8207 | param_class.Assign(ResolveType(type_idx, dex_cache, class_loader)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8208 | if (param_class == nullptr) { |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 8209 | DCHECK(self->IsExceptionPending()); |
| 8210 | return nullptr; |
| 8211 | } |
| 8212 | |
| 8213 | method_params->Set(i++, param_class.Get()); |
| 8214 | } |
| 8215 | |
| 8216 | DCHECK(!it.HasNext()); |
| 8217 | |
| 8218 | Handle<mirror::MethodType> type = hs.NewHandle( |
| 8219 | mirror::MethodType::Create(self, return_type, method_params)); |
| 8220 | dex_cache->SetResolvedMethodType(proto_idx, type.Get()); |
| 8221 | |
| 8222 | return type.Get(); |
| 8223 | } |
| 8224 | |
Vladimir Marko | af94020 | 2017-12-08 15:01:18 +0000 | [diff] [blame] | 8225 | ObjPtr<mirror::MethodType> ClassLinker::ResolveMethodType(Thread* self, |
Orion Hodson | 06d10a7 | 2018-05-14 08:53:38 +0100 | [diff] [blame] | 8226 | dex::ProtoIndex proto_idx, |
Vladimir Marko | af94020 | 2017-12-08 15:01:18 +0000 | [diff] [blame] | 8227 | ArtMethod* referrer) { |
Orion Hodson | 2e59994 | 2017-09-22 16:17:41 +0100 | [diff] [blame] | 8228 | StackHandleScope<2> hs(self); |
Orion Hodson | 2e59994 | 2017-09-22 16:17:41 +0100 | [diff] [blame] | 8229 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache())); |
| 8230 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(referrer->GetClassLoader())); |
Vladimir Marko | af94020 | 2017-12-08 15:01:18 +0000 | [diff] [blame] | 8231 | return ResolveMethodType(self, proto_idx, dex_cache, class_loader); |
Orion Hodson | 2e59994 | 2017-09-22 16:17:41 +0100 | [diff] [blame] | 8232 | } |
| 8233 | |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8234 | mirror::MethodHandle* ClassLinker::ResolveMethodHandleForField( |
| 8235 | Thread* self, |
| 8236 | const DexFile::MethodHandleItem& method_handle, |
| 8237 | ArtMethod* referrer) { |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8238 | DexFile::MethodHandleType handle_type = |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8239 | static_cast<DexFile::MethodHandleType>(method_handle.method_handle_type_); |
| 8240 | mirror::MethodHandle::Kind kind; |
Orion Hodson | fd7b2c2 | 2018-03-15 15:38:38 +0000 | [diff] [blame] | 8241 | bool is_put; |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8242 | bool is_static; |
| 8243 | int32_t num_params; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8244 | switch (handle_type) { |
| 8245 | case DexFile::MethodHandleType::kStaticPut: { |
Orion Hodson | 82b351f | 2017-07-05 14:34:25 +0100 | [diff] [blame] | 8246 | kind = mirror::MethodHandle::Kind::kStaticPut; |
Orion Hodson | fd7b2c2 | 2018-03-15 15:38:38 +0000 | [diff] [blame] | 8247 | is_put = true; |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8248 | is_static = true; |
| 8249 | num_params = 1; |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 8250 | break; |
| 8251 | } |
| 8252 | case DexFile::MethodHandleType::kStaticGet: { |
Orion Hodson | 82b351f | 2017-07-05 14:34:25 +0100 | [diff] [blame] | 8253 | kind = mirror::MethodHandle::Kind::kStaticGet; |
Orion Hodson | fd7b2c2 | 2018-03-15 15:38:38 +0000 | [diff] [blame] | 8254 | is_put = false; |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8255 | is_static = true; |
| 8256 | num_params = 0; |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 8257 | break; |
| 8258 | } |
| 8259 | case DexFile::MethodHandleType::kInstancePut: { |
Orion Hodson | 82b351f | 2017-07-05 14:34:25 +0100 | [diff] [blame] | 8260 | kind = mirror::MethodHandle::Kind::kInstancePut; |
Orion Hodson | fd7b2c2 | 2018-03-15 15:38:38 +0000 | [diff] [blame] | 8261 | is_put = true; |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8262 | is_static = false; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8263 | num_params = 2; |
| 8264 | break; |
| 8265 | } |
| 8266 | case DexFile::MethodHandleType::kInstanceGet: { |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8267 | kind = mirror::MethodHandle::Kind::kInstanceGet; |
Orion Hodson | fd7b2c2 | 2018-03-15 15:38:38 +0000 | [diff] [blame] | 8268 | is_put = false; |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8269 | is_static = false; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8270 | num_params = 1; |
| 8271 | break; |
| 8272 | } |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8273 | case DexFile::MethodHandleType::kInvokeStatic: |
Orion Hodson | 82b351f | 2017-07-05 14:34:25 +0100 | [diff] [blame] | 8274 | case DexFile::MethodHandleType::kInvokeInstance: |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8275 | case DexFile::MethodHandleType::kInvokeConstructor: |
Orion Hodson | 82b351f | 2017-07-05 14:34:25 +0100 | [diff] [blame] | 8276 | case DexFile::MethodHandleType::kInvokeDirect: |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8277 | case DexFile::MethodHandleType::kInvokeInterface: |
| 8278 | UNREACHABLE(); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8279 | } |
| 8280 | |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8281 | ArtField* target_field = |
| 8282 | ResolveField(method_handle.field_or_method_idx_, referrer, is_static); |
| 8283 | if (LIKELY(target_field != nullptr)) { |
| 8284 | ObjPtr<mirror::Class> target_class = target_field->GetDeclaringClass(); |
| 8285 | ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass(); |
| 8286 | if (UNLIKELY(!referring_class->CanAccessMember(target_class, target_field->GetAccessFlags()))) { |
| 8287 | ThrowIllegalAccessErrorField(referring_class, target_field); |
| 8288 | return nullptr; |
| 8289 | } |
Orion Hodson | fd7b2c2 | 2018-03-15 15:38:38 +0000 | [diff] [blame] | 8290 | if (UNLIKELY(is_put && target_field->IsFinal())) { |
| 8291 | ThrowIllegalAccessErrorField(referring_class, target_field); |
| 8292 | return nullptr; |
| 8293 | } |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8294 | } else { |
| 8295 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 8296 | return nullptr; |
| 8297 | } |
| 8298 | |
| 8299 | StackHandleScope<4> hs(self); |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 8300 | ObjPtr<mirror::Class> array_of_class = GetClassRoot<mirror::ObjectArray<mirror::Class>>(this); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8301 | Handle<mirror::ObjectArray<mirror::Class>> method_params(hs.NewHandle( |
| 8302 | mirror::ObjectArray<mirror::Class>::Alloc(self, array_of_class, num_params))); |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 8303 | if (UNLIKELY(method_params == nullptr)) { |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8304 | DCHECK(self->IsExceptionPending()); |
| 8305 | return nullptr; |
| 8306 | } |
| 8307 | |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8308 | Handle<mirror::Class> constructor_class; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8309 | Handle<mirror::Class> return_type; |
| 8310 | switch (handle_type) { |
| 8311 | case DexFile::MethodHandleType::kStaticPut: { |
Vladimir Marko | 4098a7a | 2017-11-06 16:00:51 +0000 | [diff] [blame] | 8312 | method_params->Set(0, target_field->ResolveType()); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8313 | return_type = hs.NewHandle(FindPrimitiveClass('V')); |
| 8314 | break; |
| 8315 | } |
| 8316 | case DexFile::MethodHandleType::kStaticGet: { |
Vladimir Marko | 4098a7a | 2017-11-06 16:00:51 +0000 | [diff] [blame] | 8317 | return_type = hs.NewHandle(target_field->ResolveType()); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8318 | break; |
| 8319 | } |
| 8320 | case DexFile::MethodHandleType::kInstancePut: { |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 8321 | method_params->Set(0, target_field->GetDeclaringClass()); |
Vladimir Marko | 4098a7a | 2017-11-06 16:00:51 +0000 | [diff] [blame] | 8322 | method_params->Set(1, target_field->ResolveType()); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8323 | return_type = hs.NewHandle(FindPrimitiveClass('V')); |
| 8324 | break; |
| 8325 | } |
| 8326 | case DexFile::MethodHandleType::kInstanceGet: { |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 8327 | method_params->Set(0, target_field->GetDeclaringClass()); |
Vladimir Marko | 4098a7a | 2017-11-06 16:00:51 +0000 | [diff] [blame] | 8328 | return_type = hs.NewHandle(target_field->ResolveType()); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8329 | break; |
| 8330 | } |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8331 | case DexFile::MethodHandleType::kInvokeStatic: |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 8332 | case DexFile::MethodHandleType::kInvokeInstance: |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8333 | case DexFile::MethodHandleType::kInvokeConstructor: |
| 8334 | case DexFile::MethodHandleType::kInvokeDirect: |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 8335 | case DexFile::MethodHandleType::kInvokeInterface: |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8336 | UNREACHABLE(); |
| 8337 | } |
| 8338 | |
| 8339 | for (int32_t i = 0; i < num_params; ++i) { |
| 8340 | if (UNLIKELY(method_params->Get(i) == nullptr)) { |
| 8341 | DCHECK(self->IsExceptionPending()); |
| 8342 | return nullptr; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8343 | } |
| 8344 | } |
| 8345 | |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8346 | if (UNLIKELY(return_type.IsNull())) { |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8347 | DCHECK(self->IsExceptionPending()); |
| 8348 | return nullptr; |
| 8349 | } |
| 8350 | |
| 8351 | Handle<mirror::MethodType> |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8352 | method_type(hs.NewHandle(mirror::MethodType::Create(self, return_type, method_params))); |
| 8353 | if (UNLIKELY(method_type.IsNull())) { |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8354 | DCHECK(self->IsExceptionPending()); |
| 8355 | return nullptr; |
| 8356 | } |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 8357 | |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8358 | uintptr_t target = reinterpret_cast<uintptr_t>(target_field); |
| 8359 | return mirror::MethodHandleImpl::Create(self, target, kind, method_type); |
| 8360 | } |
| 8361 | |
| 8362 | mirror::MethodHandle* ClassLinker::ResolveMethodHandleForMethod( |
| 8363 | Thread* self, |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8364 | const DexFile::MethodHandleItem& method_handle, |
| 8365 | ArtMethod* referrer) { |
| 8366 | DexFile::MethodHandleType handle_type = |
| 8367 | static_cast<DexFile::MethodHandleType>(method_handle.method_handle_type_); |
| 8368 | mirror::MethodHandle::Kind kind; |
| 8369 | uint32_t receiver_count = 0; |
| 8370 | ArtMethod* target_method = nullptr; |
| 8371 | switch (handle_type) { |
| 8372 | case DexFile::MethodHandleType::kStaticPut: |
| 8373 | case DexFile::MethodHandleType::kStaticGet: |
| 8374 | case DexFile::MethodHandleType::kInstancePut: |
| 8375 | case DexFile::MethodHandleType::kInstanceGet: |
| 8376 | UNREACHABLE(); |
| 8377 | case DexFile::MethodHandleType::kInvokeStatic: { |
| 8378 | kind = mirror::MethodHandle::Kind::kInvokeStatic; |
| 8379 | receiver_count = 0; |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 8380 | target_method = ResolveMethod<ResolveMode::kNoChecks>(self, |
| 8381 | method_handle.field_or_method_idx_, |
| 8382 | referrer, |
| 8383 | InvokeType::kStatic); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8384 | break; |
| 8385 | } |
| 8386 | case DexFile::MethodHandleType::kInvokeInstance: { |
| 8387 | kind = mirror::MethodHandle::Kind::kInvokeVirtual; |
| 8388 | receiver_count = 1; |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 8389 | target_method = ResolveMethod<ResolveMode::kNoChecks>(self, |
| 8390 | method_handle.field_or_method_idx_, |
| 8391 | referrer, |
| 8392 | InvokeType::kVirtual); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8393 | break; |
| 8394 | } |
| 8395 | case DexFile::MethodHandleType::kInvokeConstructor: { |
| 8396 | // Constructors are currently implemented as a transform. They |
| 8397 | // are special cased later in this method. |
| 8398 | kind = mirror::MethodHandle::Kind::kInvokeTransform; |
| 8399 | receiver_count = 0; |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 8400 | target_method = ResolveMethod<ResolveMode::kNoChecks>(self, |
| 8401 | method_handle.field_or_method_idx_, |
| 8402 | referrer, |
| 8403 | InvokeType::kDirect); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8404 | break; |
| 8405 | } |
| 8406 | case DexFile::MethodHandleType::kInvokeDirect: { |
| 8407 | kind = mirror::MethodHandle::Kind::kInvokeDirect; |
| 8408 | receiver_count = 1; |
| 8409 | StackHandleScope<2> hs(self); |
| 8410 | // A constant method handle with type kInvokeDirect can refer to |
| 8411 | // a method that is private or to a method in a super class. To |
| 8412 | // disambiguate the two options, we resolve the method ignoring |
| 8413 | // the invocation type to determine if the method is private. We |
| 8414 | // then resolve again specifying the intended invocation type to |
| 8415 | // force the appropriate checks. |
Vladimir Marko | 8901119 | 2017-12-11 13:45:05 +0000 | [diff] [blame] | 8416 | target_method = ResolveMethodWithoutInvokeType(method_handle.field_or_method_idx_, |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8417 | hs.NewHandle(referrer->GetDexCache()), |
| 8418 | hs.NewHandle(referrer->GetClassLoader())); |
| 8419 | if (UNLIKELY(target_method == nullptr)) { |
| 8420 | break; |
| 8421 | } |
| 8422 | |
| 8423 | if (target_method->IsPrivate()) { |
| 8424 | kind = mirror::MethodHandle::Kind::kInvokeDirect; |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 8425 | target_method = ResolveMethod<ResolveMode::kNoChecks>(self, |
| 8426 | method_handle.field_or_method_idx_, |
| 8427 | referrer, |
| 8428 | InvokeType::kDirect); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8429 | } else { |
| 8430 | kind = mirror::MethodHandle::Kind::kInvokeSuper; |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 8431 | target_method = ResolveMethod<ResolveMode::kNoChecks>(self, |
| 8432 | method_handle.field_or_method_idx_, |
| 8433 | referrer, |
| 8434 | InvokeType::kSuper); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8435 | if (UNLIKELY(target_method == nullptr)) { |
| 8436 | break; |
| 8437 | } |
| 8438 | // Find the method specified in the parent in referring class |
| 8439 | // so invoke-super invokes the method in the parent of the |
| 8440 | // referrer. |
| 8441 | target_method = |
| 8442 | referrer->GetDeclaringClass()->FindVirtualMethodForVirtual(target_method, |
| 8443 | kRuntimePointerSize); |
| 8444 | } |
| 8445 | break; |
| 8446 | } |
| 8447 | case DexFile::MethodHandleType::kInvokeInterface: { |
| 8448 | kind = mirror::MethodHandle::Kind::kInvokeInterface; |
| 8449 | receiver_count = 1; |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 8450 | target_method = ResolveMethod<ResolveMode::kNoChecks>(self, |
| 8451 | method_handle.field_or_method_idx_, |
| 8452 | referrer, |
| 8453 | InvokeType::kInterface); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8454 | break; |
| 8455 | } |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 8456 | } |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8457 | |
| 8458 | if (UNLIKELY(target_method == nullptr)) { |
| 8459 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 8460 | return nullptr; |
| 8461 | } |
| 8462 | |
| 8463 | ObjPtr<mirror::Class> target_class = target_method->GetDeclaringClass(); |
| 8464 | ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass(); |
| 8465 | uint32_t access_flags = target_method->GetAccessFlags(); |
| 8466 | if (UNLIKELY(!referring_class->CanAccessMember(target_class, access_flags))) { |
| 8467 | ThrowIllegalAccessErrorMethod(referring_class, target_method); |
| 8468 | return nullptr; |
| 8469 | } |
| 8470 | |
| 8471 | // Calculate the number of parameters from the method shorty. We add the |
| 8472 | // receiver count (0 or 1) and deduct one for the return value. |
| 8473 | uint32_t shorty_length; |
| 8474 | target_method->GetShorty(&shorty_length); |
| 8475 | int32_t num_params = static_cast<int32_t>(shorty_length + receiver_count - 1); |
| 8476 | |
| 8477 | StackHandleScope<7> hs(self); |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 8478 | ObjPtr<mirror::Class> array_of_class = GetClassRoot<mirror::ObjectArray<mirror::Class>>(this); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8479 | Handle<mirror::ObjectArray<mirror::Class>> method_params(hs.NewHandle( |
| 8480 | mirror::ObjectArray<mirror::Class>::Alloc(self, array_of_class, num_params))); |
| 8481 | if (method_params.Get() == nullptr) { |
| 8482 | DCHECK(self->IsExceptionPending()); |
| 8483 | return nullptr; |
| 8484 | } |
| 8485 | |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8486 | int32_t index = 0; |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8487 | if (receiver_count != 0) { |
| 8488 | // Insert receiver |
| 8489 | method_params->Set(index++, target_method->GetDeclaringClass()); |
| 8490 | } |
Orion Hodson | da1cdd0 | 2018-01-31 18:08:28 +0000 | [diff] [blame] | 8491 | DexFileParameterIterator it(*target_method->GetDexFile(), target_method->GetPrototype()); |
| 8492 | Handle<mirror::DexCache> target_method_dex_cache(hs.NewHandle(target_method->GetDexCache())); |
| 8493 | Handle<mirror::ClassLoader> target_method_class_loader(hs.NewHandle(target_method->GetClassLoader())); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8494 | while (it.HasNext()) { |
Orion Hodson | da1cdd0 | 2018-01-31 18:08:28 +0000 | [diff] [blame] | 8495 | DCHECK_LT(index, num_params); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8496 | const dex::TypeIndex type_idx = it.GetTypeIdx(); |
Orion Hodson | da1cdd0 | 2018-01-31 18:08:28 +0000 | [diff] [blame] | 8497 | ObjPtr<mirror::Class> klass = ResolveType(type_idx, |
| 8498 | target_method_dex_cache, |
| 8499 | target_method_class_loader); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8500 | if (nullptr == klass) { |
| 8501 | DCHECK(self->IsExceptionPending()); |
| 8502 | return nullptr; |
| 8503 | } |
| 8504 | method_params->Set(index++, klass); |
| 8505 | it.Next(); |
| 8506 | } |
| 8507 | |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 8508 | Handle<mirror::Class> return_type = hs.NewHandle(target_method->ResolveReturnType()); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8509 | if (UNLIKELY(return_type.IsNull())) { |
| 8510 | DCHECK(self->IsExceptionPending()); |
| 8511 | return nullptr; |
| 8512 | } |
| 8513 | |
| 8514 | Handle<mirror::MethodType> |
| 8515 | method_type(hs.NewHandle(mirror::MethodType::Create(self, return_type, method_params))); |
| 8516 | if (UNLIKELY(method_type.IsNull())) { |
| 8517 | DCHECK(self->IsExceptionPending()); |
| 8518 | return nullptr; |
| 8519 | } |
| 8520 | |
| 8521 | if (UNLIKELY(handle_type == DexFile::MethodHandleType::kInvokeConstructor)) { |
| 8522 | Handle<mirror::Class> constructor_class = hs.NewHandle(target_method->GetDeclaringClass()); |
| 8523 | Handle<mirror::MethodHandlesLookup> lookup = |
| 8524 | hs.NewHandle(mirror::MethodHandlesLookup::GetDefault(self)); |
| 8525 | return lookup->FindConstructor(self, constructor_class, method_type); |
| 8526 | } |
| 8527 | |
| 8528 | uintptr_t target = reinterpret_cast<uintptr_t>(target_method); |
| 8529 | return mirror::MethodHandleImpl::Create(self, target, kind, method_type); |
| 8530 | } |
| 8531 | |
Vladimir Marko | af94020 | 2017-12-08 15:01:18 +0000 | [diff] [blame] | 8532 | ObjPtr<mirror::MethodHandle> ClassLinker::ResolveMethodHandle(Thread* self, |
| 8533 | uint32_t method_handle_idx, |
| 8534 | ArtMethod* referrer) |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8535 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8536 | const DexFile* const dex_file = referrer->GetDexFile(); |
| 8537 | const DexFile::MethodHandleItem& method_handle = dex_file->GetMethodHandle(method_handle_idx); |
| 8538 | switch (static_cast<DexFile::MethodHandleType>(method_handle.method_handle_type_)) { |
| 8539 | case DexFile::MethodHandleType::kStaticPut: |
| 8540 | case DexFile::MethodHandleType::kStaticGet: |
| 8541 | case DexFile::MethodHandleType::kInstancePut: |
| 8542 | case DexFile::MethodHandleType::kInstanceGet: |
| 8543 | return ResolveMethodHandleForField(self, method_handle, referrer); |
| 8544 | case DexFile::MethodHandleType::kInvokeStatic: |
| 8545 | case DexFile::MethodHandleType::kInvokeInstance: |
| 8546 | case DexFile::MethodHandleType::kInvokeConstructor: |
| 8547 | case DexFile::MethodHandleType::kInvokeDirect: |
| 8548 | case DexFile::MethodHandleType::kInvokeInterface: |
Orion Hodson | da1cdd0 | 2018-01-31 18:08:28 +0000 | [diff] [blame] | 8549 | return ResolveMethodHandleForMethod(self, method_handle, referrer); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 8550 | } |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 8551 | } |
| 8552 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 8553 | bool ClassLinker::IsQuickResolutionStub(const void* entry_point) const { |
| 8554 | return (entry_point == GetQuickResolutionStub()) || |
| 8555 | (quick_resolution_trampoline_ == entry_point); |
| 8556 | } |
| 8557 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 8558 | bool ClassLinker::IsQuickToInterpreterBridge(const void* entry_point) const { |
| 8559 | return (entry_point == GetQuickToInterpreterBridge()) || |
| 8560 | (quick_to_interpreter_bridge_trampoline_ == entry_point); |
| 8561 | } |
| 8562 | |
| 8563 | bool ClassLinker::IsQuickGenericJniStub(const void* entry_point) const { |
| 8564 | return (entry_point == GetQuickGenericJniStub()) || |
| 8565 | (quick_generic_jni_trampoline_ == entry_point); |
| 8566 | } |
| 8567 | |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 8568 | bool ClassLinker::IsJniDlsymLookupStub(const void* entry_point) const { |
| 8569 | return entry_point == GetJniDlsymLookupStub(); |
| 8570 | } |
| 8571 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 8572 | const void* ClassLinker::GetRuntimeQuickGenericJniStub() const { |
| 8573 | return GetQuickGenericJniStub(); |
| 8574 | } |
| 8575 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8576 | void ClassLinker::SetEntryPointsToInterpreter(ArtMethod* method) const { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 8577 | if (!method->IsNative()) { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 8578 | method->SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge()); |
| 8579 | } else { |
Goran Jakovljevic | c16268f | 2017-07-27 10:03:32 +0200 | [diff] [blame] | 8580 | method->SetEntryPointFromQuickCompiledCode(GetQuickGenericJniStub()); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 8581 | } |
| 8582 | } |
| 8583 | |
Alex Light | db01a09 | 2017-04-03 15:39:55 -0700 | [diff] [blame] | 8584 | void ClassLinker::SetEntryPointsForObsoleteMethod(ArtMethod* method) const { |
| 8585 | DCHECK(method->IsObsolete()); |
| 8586 | // We cannot mess with the entrypoints of native methods because they are used to determine how |
| 8587 | // large the method's quick stack frame is. Without this information we cannot walk the stacks. |
| 8588 | if (!method->IsNative()) { |
| 8589 | method->SetEntryPointFromQuickCompiledCode(GetInvokeObsoleteMethodStub()); |
| 8590 | } |
| 8591 | } |
| 8592 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 8593 | void ClassLinker::DumpForSigQuit(std::ostream& os) { |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 8594 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 8595 | ReaderMutexLock mu(soa.Self(), *Locks::classlinker_classes_lock_); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 8596 | os << "Zygote loaded classes=" << NumZygoteClasses() << " post zygote classes=" |
| 8597 | << NumNonZygoteClasses() << "\n"; |
Nicolas Geoffray | a90c922 | 2018-09-07 13:19:19 +0100 | [diff] [blame] | 8598 | ReaderMutexLock mu2(soa.Self(), *Locks::dex_lock_); |
| 8599 | os << "Dumping registered class loaders\n"; |
| 8600 | size_t class_loader_index = 0; |
| 8601 | for (const ClassLoaderData& class_loader : class_loaders_) { |
| 8602 | ObjPtr<mirror::ClassLoader> loader = |
| 8603 | ObjPtr<mirror::ClassLoader>::DownCast(soa.Self()->DecodeJObject(class_loader.weak_root)); |
| 8604 | if (loader != nullptr) { |
| 8605 | os << "#" << class_loader_index++ << " " << loader->GetClass()->PrettyDescriptor() << ": ["; |
| 8606 | bool saw_one_dex_file = false; |
| 8607 | for (const DexCacheData& dex_cache : dex_caches_) { |
| 8608 | if (dex_cache.IsValid() && dex_cache.class_table == class_loader.class_table) { |
| 8609 | if (saw_one_dex_file) { |
| 8610 | os << ":"; |
| 8611 | } |
| 8612 | saw_one_dex_file = true; |
| 8613 | os << dex_cache.dex_file->GetLocation(); |
| 8614 | } |
| 8615 | } |
| 8616 | os << "]"; |
| 8617 | bool found_parent = false; |
| 8618 | if (loader->GetParent() != nullptr) { |
| 8619 | size_t parent_index = 0; |
| 8620 | for (const ClassLoaderData& class_loader2 : class_loaders_) { |
| 8621 | ObjPtr<mirror::ClassLoader> loader2 = ObjPtr<mirror::ClassLoader>::DownCast( |
| 8622 | soa.Self()->DecodeJObject(class_loader2.weak_root)); |
| 8623 | if (loader2 == loader->GetParent()) { |
| 8624 | os << ", parent #" << parent_index; |
| 8625 | found_parent = true; |
| 8626 | break; |
| 8627 | } |
| 8628 | parent_index++; |
| 8629 | } |
| 8630 | if (!found_parent) { |
| 8631 | os << ", unregistered parent of type " |
| 8632 | << loader->GetParent()->GetClass()->PrettyDescriptor(); |
| 8633 | } |
| 8634 | } else { |
| 8635 | os << ", no parent"; |
| 8636 | } |
| 8637 | os << "\n"; |
| 8638 | } |
| 8639 | } |
| 8640 | os << "Done dumping class loaders\n"; |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 8641 | } |
| 8642 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 8643 | class CountClassesVisitor : public ClassLoaderVisitor { |
| 8644 | public: |
| 8645 | CountClassesVisitor() : num_zygote_classes(0), num_non_zygote_classes(0) {} |
| 8646 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 8647 | void Visit(ObjPtr<mirror::ClassLoader> class_loader) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 8648 | REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_) override { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 8649 | ClassTable* const class_table = class_loader->GetClassTable(); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 8650 | if (class_table != nullptr) { |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 8651 | num_zygote_classes += class_table->NumZygoteClasses(class_loader); |
| 8652 | num_non_zygote_classes += class_table->NumNonZygoteClasses(class_loader); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 8653 | } |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 8654 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 8655 | |
| 8656 | size_t num_zygote_classes; |
| 8657 | size_t num_non_zygote_classes; |
| 8658 | }; |
| 8659 | |
| 8660 | size_t ClassLinker::NumZygoteClasses() const { |
| 8661 | CountClassesVisitor visitor; |
| 8662 | VisitClassLoaders(&visitor); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 8663 | return visitor.num_zygote_classes + boot_class_table_->NumZygoteClasses(nullptr); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 8664 | } |
| 8665 | |
| 8666 | size_t ClassLinker::NumNonZygoteClasses() const { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 8667 | CountClassesVisitor visitor; |
| 8668 | VisitClassLoaders(&visitor); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 8669 | return visitor.num_non_zygote_classes + boot_class_table_->NumNonZygoteClasses(nullptr); |
Elliott Hughes | cac6cc7 | 2011-11-03 20:31:21 -0700 | [diff] [blame] | 8670 | } |
| 8671 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 8672 | size_t ClassLinker::NumLoadedClasses() { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 8673 | ReaderMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 8674 | // 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] | 8675 | return NumNonZygoteClasses(); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 8676 | } |
| 8677 | |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 8678 | pid_t ClassLinker::GetClassesLockOwner() { |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 8679 | return Locks::classlinker_classes_lock_->GetExclusiveOwnerTid(); |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 8680 | } |
| 8681 | |
| 8682 | pid_t ClassLinker::GetDexLockOwner() { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 8683 | return Locks::dex_lock_->GetExclusiveOwnerTid(); |
Brian Carlstrom | 24a3c2e | 2011-10-17 18:07:52 -0700 | [diff] [blame] | 8684 | } |
| 8685 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 8686 | void ClassLinker::SetClassRoot(ClassRoot class_root, ObjPtr<mirror::Class> klass) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 8687 | DCHECK(!init_done_); |
| 8688 | |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 8689 | DCHECK(klass != nullptr); |
| 8690 | DCHECK(klass->GetClassLoader() == nullptr); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 8691 | |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 8692 | mirror::ObjectArray<mirror::Class>* class_roots = class_roots_.Read(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 8693 | DCHECK(class_roots != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 8694 | DCHECK_LT(static_cast<uint32_t>(class_root), static_cast<uint32_t>(ClassRoot::kMax)); |
| 8695 | int32_t index = static_cast<int32_t>(class_root); |
| 8696 | DCHECK(class_roots->Get(index) == nullptr); |
| 8697 | class_roots->Set<false>(index, klass); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 8698 | } |
| 8699 | |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 8700 | void ClassLinker::AllocAndSetPrimitiveArrayClassRoot(Thread* self, |
| 8701 | ObjPtr<mirror::Class> java_lang_Class, |
| 8702 | ClassRoot primitive_array_class_root, |
| 8703 | ClassRoot primitive_class_root, |
| 8704 | const char* descriptor) { |
| 8705 | StackHandleScope<1> hs(self); |
| 8706 | Handle<mirror::Class> primitive_array_class(hs.NewHandle( |
| 8707 | AllocPrimitiveArrayClass(self, java_lang_Class))); |
| 8708 | primitive_array_class->SetComponentType(GetClassRoot(primitive_class_root, this)); |
| 8709 | SetClassRoot(primitive_array_class_root, primitive_array_class.Get()); |
| 8710 | CheckSystemClass(self, primitive_array_class, descriptor); |
| 8711 | } |
| 8712 | |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 8713 | jobject ClassLinker::CreateWellKnownClassLoader(Thread* self, |
Andreas Gampe | 473191c | 2017-12-28 16:55:31 -0800 | [diff] [blame] | 8714 | const std::vector<const DexFile*>& dex_files, |
| 8715 | jclass loader_class, |
| 8716 | jobject parent_loader) { |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 8717 | CHECK(self->GetJniEnv()->IsSameObject(loader_class, |
| 8718 | WellKnownClasses::dalvik_system_PathClassLoader) || |
| 8719 | self->GetJniEnv()->IsSameObject(loader_class, |
| 8720 | WellKnownClasses::dalvik_system_DelegateLastClassLoader)); |
| 8721 | |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8722 | // SOAAlreadyRunnable is protected, and we need something to add a global reference. |
| 8723 | // We could move the jobject to the callers, but all call-sites do this... |
| 8724 | ScopedObjectAccessUnchecked soa(self); |
| 8725 | |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8726 | // 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] | 8727 | StackHandleScope<6> hs(self); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8728 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8729 | ArtField* dex_elements_field = |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 8730 | jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList_dexElements); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8731 | |
Vladimir Marko | 4098a7a | 2017-11-06 16:00:51 +0000 | [diff] [blame] | 8732 | Handle<mirror::Class> dex_elements_class(hs.NewHandle(dex_elements_field->ResolveType())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8733 | DCHECK(dex_elements_class != nullptr); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8734 | DCHECK(dex_elements_class->IsArrayClass()); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 8735 | Handle<mirror::ObjectArray<mirror::Object>> h_dex_elements(hs.NewHandle( |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 8736 | mirror::ObjectArray<mirror::Object>::Alloc(self, |
| 8737 | dex_elements_class.Get(), |
| 8738 | dex_files.size()))); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8739 | Handle<mirror::Class> h_dex_element_class = |
| 8740 | hs.NewHandle(dex_elements_class->GetComponentType()); |
| 8741 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8742 | ArtField* element_file_field = |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 8743 | jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8744 | DCHECK_EQ(h_dex_element_class.Get(), element_file_field->GetDeclaringClass()); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8745 | |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 8746 | ArtField* cookie_field = jni::DecodeArtField(WellKnownClasses::dalvik_system_DexFile_cookie); |
Vladimir Marko | 208f670 | 2017-12-08 12:00:50 +0000 | [diff] [blame] | 8747 | DCHECK_EQ(cookie_field->GetDeclaringClass(), element_file_field->LookupResolvedType()); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8748 | |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 8749 | ArtField* file_name_field = jni::DecodeArtField(WellKnownClasses::dalvik_system_DexFile_fileName); |
Vladimir Marko | 208f670 | 2017-12-08 12:00:50 +0000 | [diff] [blame] | 8750 | DCHECK_EQ(file_name_field->GetDeclaringClass(), element_file_field->LookupResolvedType()); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 8751 | |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8752 | // Fill the elements array. |
| 8753 | int32_t index = 0; |
| 8754 | for (const DexFile* dex_file : dex_files) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 8755 | StackHandleScope<4> hs2(self); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8756 | |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 8757 | // CreateWellKnownClassLoader is only used by gtests and compiler. |
| 8758 | // Index 0 of h_long_array is supposed to be the oat file but we can leave it null. |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 8759 | Handle<mirror::LongArray> h_long_array = hs2.NewHandle(mirror::LongArray::Alloc( |
| 8760 | self, |
| 8761 | kDexFileIndexStart + 1)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8762 | DCHECK(h_long_array != nullptr); |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 8763 | h_long_array->Set(kDexFileIndexStart, reinterpret_cast<intptr_t>(dex_file)); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8764 | |
Mathieu Chartier | 3738e98 | 2017-05-12 16:07:28 -0700 | [diff] [blame] | 8765 | // Note that this creates a finalizable dalvik.system.DexFile object and a corresponding |
| 8766 | // FinalizerReference which will never get cleaned up without a started runtime. |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8767 | Handle<mirror::Object> h_dex_file = hs2.NewHandle( |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8768 | cookie_field->GetDeclaringClass()->AllocObject(self)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8769 | DCHECK(h_dex_file != nullptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8770 | cookie_field->SetObject<false>(h_dex_file.Get(), h_long_array.Get()); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8771 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 8772 | Handle<mirror::String> h_file_name = hs2.NewHandle( |
| 8773 | mirror::String::AllocFromModifiedUtf8(self, dex_file->GetLocation().c_str())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8774 | DCHECK(h_file_name != nullptr); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 8775 | file_name_field->SetObject<false>(h_dex_file.Get(), h_file_name.Get()); |
| 8776 | |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8777 | 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] | 8778 | DCHECK(h_element != nullptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8779 | element_file_field->SetObject<false>(h_element.Get(), h_dex_file.Get()); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8780 | |
| 8781 | h_dex_elements->Set(index, h_element.Get()); |
| 8782 | index++; |
| 8783 | } |
| 8784 | DCHECK_EQ(index, h_dex_elements->GetLength()); |
| 8785 | |
| 8786 | // Create DexPathList. |
| 8787 | Handle<mirror::Object> h_dex_path_list = hs.NewHandle( |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8788 | dex_elements_field->GetDeclaringClass()->AllocObject(self)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8789 | DCHECK(h_dex_path_list != nullptr); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8790 | // Set elements. |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8791 | dex_elements_field->SetObject<false>(h_dex_path_list.Get(), h_dex_elements.Get()); |
Andreas Gampe | 473191c | 2017-12-28 16:55:31 -0800 | [diff] [blame] | 8792 | // Create an empty List for the "nativeLibraryDirectories," required for native tests. |
| 8793 | // Note: this code is uncommon(oatdump)/testing-only, so don't add further WellKnownClasses |
| 8794 | // elements. |
| 8795 | { |
| 8796 | ArtField* native_lib_dirs = dex_elements_field->GetDeclaringClass()-> |
| 8797 | FindDeclaredInstanceField("nativeLibraryDirectories", "Ljava/util/List;"); |
| 8798 | DCHECK(native_lib_dirs != nullptr); |
| 8799 | ObjPtr<mirror::Class> list_class = FindSystemClass(self, "Ljava/util/ArrayList;"); |
| 8800 | DCHECK(list_class != nullptr); |
| 8801 | { |
| 8802 | StackHandleScope<1> h_list_scope(self); |
| 8803 | Handle<mirror::Class> h_list_class(h_list_scope.NewHandle<mirror::Class>(list_class)); |
| 8804 | bool list_init = EnsureInitialized(self, h_list_class, true, true); |
| 8805 | DCHECK(list_init); |
| 8806 | list_class = h_list_class.Get(); |
| 8807 | } |
| 8808 | ObjPtr<mirror::Object> list_object = list_class->AllocObject(self); |
| 8809 | // Note: we leave the object uninitialized. This must never leak into any non-testing code, but |
| 8810 | // is fine for testing. While it violates a Java-code invariant (the elementData field is |
| 8811 | // normally never null), as long as one does not try to add elements, this will still |
| 8812 | // work. |
| 8813 | native_lib_dirs->SetObject<false>(h_dex_path_list.Get(), list_object); |
| 8814 | } |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8815 | |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 8816 | // Create the class loader.. |
| 8817 | Handle<mirror::Class> h_loader_class = hs.NewHandle(soa.Decode<mirror::Class>(loader_class)); |
| 8818 | Handle<mirror::Object> h_class_loader = hs.NewHandle(h_loader_class->AllocObject(self)); |
| 8819 | DCHECK(h_class_loader != nullptr); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8820 | // Set DexPathList. |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8821 | ArtField* path_list_field = |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 8822 | jni::DecodeArtField(WellKnownClasses::dalvik_system_BaseDexClassLoader_pathList); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8823 | DCHECK(path_list_field != nullptr); |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 8824 | path_list_field->SetObject<false>(h_class_loader.Get(), h_dex_path_list.Get()); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8825 | |
| 8826 | // Make a pretend boot-classpath. |
| 8827 | // TODO: Should we scan the image? |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8828 | ArtField* const parent_field = |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 8829 | mirror::Class::FindField(self, |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 8830 | h_class_loader->GetClass(), |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 8831 | "parent", |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8832 | "Ljava/lang/ClassLoader;"); |
Roland Levillain | f39c9eb | 2015-05-26 15:02:07 +0100 | [diff] [blame] | 8833 | DCHECK(parent_field != nullptr); |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 8834 | |
| 8835 | ObjPtr<mirror::Object> parent = (parent_loader != nullptr) |
| 8836 | ? soa.Decode<mirror::ClassLoader>(parent_loader) |
| 8837 | : soa.Decode<mirror::Class>(WellKnownClasses::java_lang_BootClassLoader)->AllocObject(self); |
| 8838 | parent_field->SetObject<false>(h_class_loader.Get(), parent); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8839 | |
| 8840 | // Make it a global ref and return. |
| 8841 | ScopedLocalRef<jobject> local_ref( |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 8842 | soa.Env(), soa.Env()->AddLocalReference<jobject>(h_class_loader.Get())); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 8843 | return soa.Env()->NewGlobalRef(local_ref.get()); |
| 8844 | } |
| 8845 | |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 8846 | jobject ClassLinker::CreatePathClassLoader(Thread* self, |
| 8847 | const std::vector<const DexFile*>& dex_files) { |
| 8848 | return CreateWellKnownClassLoader(self, |
| 8849 | dex_files, |
| 8850 | WellKnownClasses::dalvik_system_PathClassLoader, |
| 8851 | nullptr); |
| 8852 | } |
| 8853 | |
Andreas Gampe | 8ac7595 | 2015-06-02 21:01:45 -0700 | [diff] [blame] | 8854 | void ClassLinker::DropFindArrayClassCache() { |
| 8855 | std::fill_n(find_array_class_cache_, kFindArrayCacheSize, GcRoot<mirror::Class>(nullptr)); |
| 8856 | find_array_class_cache_next_victim_ = 0; |
| 8857 | } |
| 8858 | |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 8859 | void ClassLinker::VisitClassLoaders(ClassLoaderVisitor* visitor) const { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 8860 | Thread* const self = Thread::Current(); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 8861 | for (const ClassLoaderData& data : class_loaders_) { |
Mathieu Chartier | 4843bd5 | 2015-10-01 17:08:44 -0700 | [diff] [blame] | 8862 | // 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] | 8863 | ObjPtr<mirror::ClassLoader> class_loader = ObjPtr<mirror::ClassLoader>::DownCast( |
| 8864 | self->DecodeJObject(data.weak_root)); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 8865 | if (class_loader != nullptr) { |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 8866 | visitor->Visit(class_loader); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 8867 | } |
| 8868 | } |
| 8869 | } |
| 8870 | |
Alexey Grebenkin | 252a4e4 | 2018-04-02 18:18:01 +0300 | [diff] [blame] | 8871 | void ClassLinker::VisitAllocators(AllocatorVisitor* visitor) const { |
| 8872 | for (const ClassLoaderData& data : class_loaders_) { |
| 8873 | LinearAlloc* alloc = data.allocator; |
| 8874 | if (alloc != nullptr && !visitor->Visit(alloc)) { |
| 8875 | break; |
| 8876 | } |
| 8877 | } |
| 8878 | } |
| 8879 | |
Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 8880 | void ClassLinker::InsertDexFileInToClassLoader(ObjPtr<mirror::Object> dex_file, |
| 8881 | ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 8882 | DCHECK(dex_file != nullptr); |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 8883 | Thread* const self = Thread::Current(); |
| 8884 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 8885 | ClassTable* const table = ClassTableForClassLoader(class_loader); |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 8886 | DCHECK(table != nullptr); |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 8887 | if (table->InsertStrongRoot(dex_file) && class_loader != nullptr) { |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 8888 | // It was not already inserted, perform the write barrier to let the GC know the class loader's |
| 8889 | // class table was modified. |
Mathieu Chartier | 88ea61e | 2018-06-20 17:45:41 -0700 | [diff] [blame] | 8890 | WriteBarrier::ForEveryFieldWrite(class_loader); |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 8891 | } |
| 8892 | } |
| 8893 | |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 8894 | void ClassLinker::CleanupClassLoaders() { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 8895 | Thread* const self = Thread::Current(); |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 8896 | std::vector<ClassLoaderData> to_delete; |
| 8897 | // Do the delete outside the lock to avoid lock violation in jit code cache. |
| 8898 | { |
| 8899 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 8900 | for (auto it = class_loaders_.begin(); it != class_loaders_.end(); ) { |
| 8901 | const ClassLoaderData& data = *it; |
| 8902 | // 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] | 8903 | ObjPtr<mirror::ClassLoader> class_loader = |
| 8904 | ObjPtr<mirror::ClassLoader>::DownCast(self->DecodeJObject(data.weak_root)); |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 8905 | if (class_loader != nullptr) { |
| 8906 | ++it; |
| 8907 | } else { |
| 8908 | VLOG(class_linker) << "Freeing class loader"; |
| 8909 | to_delete.push_back(data); |
| 8910 | it = class_loaders_.erase(it); |
| 8911 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 8912 | } |
| 8913 | } |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 8914 | for (ClassLoaderData& data : to_delete) { |
Alexey Grebenkin | be4c2bd | 2018-02-01 19:09:59 +0300 | [diff] [blame] | 8915 | // CHA unloading analysis and SingleImplementaion cleanups are required. |
| 8916 | DeleteClassLoader(self, data, true /*cleanup_cha*/); |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 8917 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 8918 | } |
| 8919 | |
Vladimir Marko | 2130053 | 2017-01-24 18:06:55 +0000 | [diff] [blame] | 8920 | class GetResolvedClassesVisitor : public ClassVisitor { |
| 8921 | public: |
| 8922 | GetResolvedClassesVisitor(std::set<DexCacheResolvedClasses>* result, bool ignore_boot_classes) |
| 8923 | : result_(result), |
| 8924 | ignore_boot_classes_(ignore_boot_classes), |
| 8925 | last_resolved_classes_(result->end()), |
| 8926 | last_dex_file_(nullptr), |
| 8927 | vlog_is_on_(VLOG_IS_ON(class_linker)), |
| 8928 | extra_stats_(), |
| 8929 | last_extra_stats_(extra_stats_.end()) { } |
| 8930 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 8931 | bool operator()(ObjPtr<mirror::Class> klass) override REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | 2130053 | 2017-01-24 18:06:55 +0000 | [diff] [blame] | 8932 | if (!klass->IsProxyClass() && |
| 8933 | !klass->IsArrayClass() && |
| 8934 | klass->IsResolved() && |
| 8935 | !klass->IsErroneousResolved() && |
| 8936 | (!ignore_boot_classes_ || klass->GetClassLoader() != nullptr)) { |
| 8937 | const DexFile& dex_file = klass->GetDexFile(); |
| 8938 | if (&dex_file != last_dex_file_) { |
| 8939 | last_dex_file_ = &dex_file; |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 8940 | DexCacheResolvedClasses resolved_classes( |
| 8941 | dex_file.GetLocation(), |
| 8942 | DexFileLoader::GetBaseLocation(dex_file.GetLocation()), |
| 8943 | dex_file.GetLocationChecksum(), |
| 8944 | dex_file.NumMethodIds()); |
Vladimir Marko | 2130053 | 2017-01-24 18:06:55 +0000 | [diff] [blame] | 8945 | last_resolved_classes_ = result_->find(resolved_classes); |
| 8946 | if (last_resolved_classes_ == result_->end()) { |
| 8947 | last_resolved_classes_ = result_->insert(resolved_classes).first; |
| 8948 | } |
| 8949 | } |
| 8950 | bool added = last_resolved_classes_->AddClass(klass->GetDexTypeIndex()); |
| 8951 | if (UNLIKELY(vlog_is_on_) && added) { |
| 8952 | const DexCacheResolvedClasses* resolved_classes = std::addressof(*last_resolved_classes_); |
| 8953 | if (last_extra_stats_ == extra_stats_.end() || |
| 8954 | last_extra_stats_->first != resolved_classes) { |
| 8955 | last_extra_stats_ = extra_stats_.find(resolved_classes); |
| 8956 | if (last_extra_stats_ == extra_stats_.end()) { |
| 8957 | last_extra_stats_ = |
| 8958 | extra_stats_.emplace(resolved_classes, ExtraStats(dex_file.NumClassDefs())).first; |
| 8959 | } |
| 8960 | } |
| 8961 | } |
| 8962 | } |
| 8963 | return true; |
| 8964 | } |
| 8965 | |
| 8966 | void PrintStatistics() const { |
| 8967 | if (vlog_is_on_) { |
| 8968 | for (const DexCacheResolvedClasses& resolved_classes : *result_) { |
| 8969 | auto it = extra_stats_.find(std::addressof(resolved_classes)); |
| 8970 | DCHECK(it != extra_stats_.end()); |
| 8971 | const ExtraStats& extra_stats = it->second; |
| 8972 | LOG(INFO) << "Dex location " << resolved_classes.GetDexLocation() |
| 8973 | << " has " << resolved_classes.GetClasses().size() << " / " |
| 8974 | << extra_stats.number_of_class_defs_ << " resolved classes"; |
| 8975 | } |
| 8976 | } |
| 8977 | } |
| 8978 | |
| 8979 | private: |
| 8980 | struct ExtraStats { |
| 8981 | explicit ExtraStats(uint32_t number_of_class_defs) |
| 8982 | : number_of_class_defs_(number_of_class_defs) {} |
| 8983 | uint32_t number_of_class_defs_; |
| 8984 | }; |
| 8985 | |
| 8986 | std::set<DexCacheResolvedClasses>* result_; |
| 8987 | bool ignore_boot_classes_; |
| 8988 | std::set<DexCacheResolvedClasses>::iterator last_resolved_classes_; |
| 8989 | const DexFile* last_dex_file_; |
| 8990 | |
| 8991 | // Statistics. |
| 8992 | bool vlog_is_on_; |
| 8993 | std::map<const DexCacheResolvedClasses*, ExtraStats> extra_stats_; |
| 8994 | std::map<const DexCacheResolvedClasses*, ExtraStats>::iterator last_extra_stats_; |
| 8995 | }; |
| 8996 | |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 8997 | std::set<DexCacheResolvedClasses> ClassLinker::GetResolvedClasses(bool ignore_boot_classes) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 8998 | ScopedTrace trace(__PRETTY_FUNCTION__); |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 8999 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 9000 | ScopedAssertNoThreadSuspension ants(__FUNCTION__); |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 9001 | std::set<DexCacheResolvedClasses> ret; |
| 9002 | VLOG(class_linker) << "Collecting resolved classes"; |
| 9003 | const uint64_t start_time = NanoTime(); |
Vladimir Marko | 2130053 | 2017-01-24 18:06:55 +0000 | [diff] [blame] | 9004 | GetResolvedClassesVisitor visitor(&ret, ignore_boot_classes); |
| 9005 | VisitClasses(&visitor); |
| 9006 | if (VLOG_IS_ON(class_linker)) { |
| 9007 | visitor.PrintStatistics(); |
| 9008 | LOG(INFO) << "Collecting class profile took " << PrettyDuration(NanoTime() - start_time); |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 9009 | } |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 9010 | return ret; |
| 9011 | } |
| 9012 | |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 9013 | class ClassLinker::FindVirtualMethodHolderVisitor : public ClassVisitor { |
| 9014 | public: |
| 9015 | FindVirtualMethodHolderVisitor(const ArtMethod* method, PointerSize pointer_size) |
| 9016 | : method_(method), |
| 9017 | pointer_size_(pointer_size) {} |
| 9018 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 9019 | bool operator()(ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_) override { |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 9020 | if (klass->GetVirtualMethodsSliceUnchecked(pointer_size_).Contains(method_)) { |
| 9021 | holder_ = klass; |
| 9022 | } |
| 9023 | // Return false to stop searching if holder_ is not null. |
| 9024 | return holder_ == nullptr; |
| 9025 | } |
| 9026 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 9027 | ObjPtr<mirror::Class> holder_ = nullptr; |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 9028 | const ArtMethod* const method_; |
| 9029 | const PointerSize pointer_size_; |
| 9030 | }; |
| 9031 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 9032 | ObjPtr<mirror::Class> ClassLinker::GetHoldingClassOfCopiedMethod(ArtMethod* method) { |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 9033 | ScopedTrace trace(__FUNCTION__); // Since this function is slow, have a trace to notify people. |
| 9034 | CHECK(method->IsCopied()); |
| 9035 | FindVirtualMethodHolderVisitor visitor(method, image_pointer_size_); |
| 9036 | VisitClasses(&visitor); |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 9037 | return visitor.holder_; |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 9038 | } |
| 9039 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 9040 | ObjPtr<mirror::IfTable> ClassLinker::AllocIfTable(Thread* self, size_t ifcount) { |
| 9041 | return ObjPtr<mirror::IfTable>::DownCast(ObjPtr<mirror::ObjectArray<mirror::Object>>( |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 9042 | mirror::IfTable::Alloc(self, |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 9043 | GetClassRoot<mirror::ObjectArray<mirror::Object>>(this), |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 9044 | ifcount * mirror::IfTable::kMax))); |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 9045 | } |
| 9046 | |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 9047 | // Instantiate ClassLinker::ResolveMethod. |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9048 | template ArtMethod* ClassLinker::ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>( |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 9049 | uint32_t method_idx, |
| 9050 | Handle<mirror::DexCache> dex_cache, |
| 9051 | Handle<mirror::ClassLoader> class_loader, |
| 9052 | ArtMethod* referrer, |
| 9053 | InvokeType type); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9054 | template ArtMethod* ClassLinker::ResolveMethod<ClassLinker::ResolveMode::kNoChecks>( |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 9055 | uint32_t method_idx, |
| 9056 | Handle<mirror::DexCache> dex_cache, |
| 9057 | Handle<mirror::ClassLoader> class_loader, |
| 9058 | ArtMethod* referrer, |
| 9059 | InvokeType type); |
| 9060 | |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 9061 | // Instantiate ClassLinker::AllocClass. |
| 9062 | template ObjPtr<mirror::Class> ClassLinker::AllocClass</* kMovable */ true>( |
| 9063 | Thread* self, |
| 9064 | ObjPtr<mirror::Class> java_lang_Class, |
| 9065 | uint32_t class_size); |
| 9066 | template ObjPtr<mirror::Class> ClassLinker::AllocClass</* kMovable */ false>( |
| 9067 | Thread* self, |
| 9068 | ObjPtr<mirror::Class> java_lang_Class, |
| 9069 | uint32_t class_size); |
| 9070 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 9071 | } // namespace art |