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> |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 23 | #include <forward_list> |
Ian Rogers | cf7f191 | 2014-10-22 22:06:39 -0700 | [diff] [blame] | 24 | #include <iostream> |
Vladimir Marko | 2130053 | 2017-01-24 18:06:55 +0000 | [diff] [blame] | 25 | #include <map> |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 26 | #include <memory> |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 27 | #include <queue> |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 28 | #include <string> |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 29 | #include <string_view> |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 30 | #include <tuple> |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 31 | #include <unordered_map> |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 32 | #include <utility> |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 33 | #include <vector> |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 34 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 35 | #include "android-base/stringprintf.h" |
| 36 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 37 | #include "art_field-inl.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 38 | #include "art_method-inl.h" |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 39 | #include "barrier.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 40 | #include "base/arena_allocator.h" |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 41 | #include "base/casts.h" |
Andreas Gampe | 19f5416 | 2019-05-14 16:16:28 -0700 | [diff] [blame] | 42 | #include "base/file_utils.h" |
David Sehr | 67bf42e | 2018-02-26 16:43:04 -0800 | [diff] [blame] | 43 | #include "base/leb128.h" |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 44 | #include "base/logging.h" |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 45 | #include "base/mutex-inl.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 46 | #include "base/os.h" |
| 47 | #include "base/quasi_atomic.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 48 | #include "base/scoped_arena_containers.h" |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 49 | #include "base/scoped_flock.h" |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 50 | #include "base/stl_util.h" |
Vladimir Marko | b9c29f6 | 2019-03-20 14:22:51 +0000 | [diff] [blame] | 51 | #include "base/string_view_cpp20.h" |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 52 | #include "base/systrace.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 53 | #include "base/time_utils.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 54 | #include "base/unix_file/fd_file.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 55 | #include "base/utils.h" |
Andreas Gampe | b9aec2c | 2015-04-23 22:23:47 -0700 | [diff] [blame] | 56 | #include "base/value_object.h" |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 57 | #include "cha.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 58 | #include "class_linker-inl.h" |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 59 | #include "class_loader_utils.h" |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 60 | #include "class_root.h" |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 61 | #include "class_table-inl.h" |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 62 | #include "compiler_callbacks.h" |
Vladimir Marko | 606adb3 | 2018-04-05 14:49:24 +0100 | [diff] [blame] | 63 | #include "debug_print.h" |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 64 | #include "debugger.h" |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 65 | #include "dex/class_accessor-inl.h" |
David Sehr | b2ec9f5 | 2018-02-21 13:20:31 -0800 | [diff] [blame] | 66 | #include "dex/descriptors_names.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 67 | #include "dex/dex_file-inl.h" |
| 68 | #include "dex/dex_file_exception_helpers.h" |
| 69 | #include "dex/dex_file_loader.h" |
Andreas Gampe | ad1aa63 | 2019-01-02 10:30:54 -0800 | [diff] [blame] | 70 | #include "dex/signature-inl.h" |
David Sehr | 0225f8e | 2018-01-31 08:52:24 +0000 | [diff] [blame] | 71 | #include "dex/utf.h" |
Vladimir Marko | 5115a4d | 2019-10-17 14:56:47 +0100 | [diff] [blame^] | 72 | #include "entrypoints/entrypoint_utils-inl.h" |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 73 | #include "entrypoints/runtime_asm_entrypoints.h" |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 74 | #include "experimental_flags.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 75 | #include "gc/accounting/card_table-inl.h" |
Mathieu Chartier | 03c1dd9 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 76 | #include "gc/accounting/heap_bitmap-inl.h" |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 77 | #include "gc/accounting/space_bitmap-inl.h" |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 78 | #include "gc/heap-visit-objects-inl.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 79 | #include "gc/heap.h" |
Mathieu Chartier | 1b1e31f | 2016-05-19 10:13:04 -0700 | [diff] [blame] | 80 | #include "gc/scoped_gc_critical_section.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 81 | #include "gc/space/image_space.h" |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 82 | #include "gc/space/space-inl.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 83 | #include "gc_root-inl.h" |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 84 | #include "handle_scope-inl.h" |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 85 | #include "hidden_api.h" |
Mathieu Chartier | 4a26f17 | 2016-01-26 14:26:18 -0800 | [diff] [blame] | 86 | #include "image-inl.h" |
Andreas Gampe | 75a7db6 | 2016-09-26 12:04:26 -0700 | [diff] [blame] | 87 | #include "imt_conflict_table.h" |
| 88 | #include "imtable-inl.h" |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 89 | #include "intern_table-inl.h" |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 90 | #include "interpreter/interpreter.h" |
David Srbecky | fb3de3d | 2018-01-29 16:11:49 +0000 | [diff] [blame] | 91 | #include "jit/debugger_interface.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 92 | #include "jit/jit.h" |
| 93 | #include "jit/jit_code_cache.h" |
Vladimir Marko | a3ad0cd | 2018-05-04 10:06:38 +0100 | [diff] [blame] | 94 | #include "jni/java_vm_ext.h" |
| 95 | #include "jni/jni_internal.h" |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 96 | #include "linear_alloc.h" |
Andreas Gampe | 8e0f043 | 2018-10-24 13:38:03 -0700 | [diff] [blame] | 97 | #include "mirror/array-alloc-inl.h" |
| 98 | #include "mirror/array-inl.h" |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 99 | #include "mirror/call_site.h" |
Andreas Gampe | 70f5fd0 | 2018-10-24 19:58:37 -0700 | [diff] [blame] | 100 | #include "mirror/class-alloc-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 101 | #include "mirror/class-inl.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 102 | #include "mirror/class.h" |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 103 | #include "mirror/class_ext.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 104 | #include "mirror/class_loader.h" |
Ian Rogers | 39ebcb8 | 2013-05-30 16:57:23 -0700 | [diff] [blame] | 105 | #include "mirror/dex_cache-inl.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 106 | #include "mirror/dex_cache.h" |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 107 | #include "mirror/emulated_stack_frame.h" |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 108 | #include "mirror/field.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 109 | #include "mirror/iftable-inl.h" |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 110 | #include "mirror/method.h" |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 111 | #include "mirror/method_handle_impl.h" |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 112 | #include "mirror/method_handles_lookup.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 113 | #include "mirror/method_type.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 114 | #include "mirror/object-inl.h" |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 115 | #include "mirror/object-refvisitor-inl.h" |
Andreas Gampe | 52ecb65 | 2018-10-24 15:18:21 -0700 | [diff] [blame] | 116 | #include "mirror/object_array-alloc-inl.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 117 | #include "mirror/object_array-inl.h" |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 118 | #include "mirror/object_reference.h" |
| 119 | #include "mirror/object_reference-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 120 | #include "mirror/proxy.h" |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 121 | #include "mirror/reference-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 122 | #include "mirror/stack_trace_element.h" |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 123 | #include "mirror/string-inl.h" |
Andreas Gampe | 501c3b0 | 2019-04-17 21:54:27 +0000 | [diff] [blame] | 124 | #include "mirror/throwable.h" |
Orion Hodson | 005ac51 | 2017-10-24 15:43:43 +0100 | [diff] [blame] | 125 | #include "mirror/var_handle.h" |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 126 | #include "native/dalvik_system_DexFile.h" |
Andreas Gampe | 373a9b5 | 2017-10-18 09:01:57 -0700 | [diff] [blame] | 127 | #include "nativehelper/scoped_local_ref.h" |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 128 | #include "oat.h" |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 129 | #include "oat_file-inl.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 130 | #include "oat_file.h" |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 131 | #include "oat_file_assistant.h" |
| 132 | #include "oat_file_manager.h" |
| 133 | #include "object_lock.h" |
David Sehr | 82d046e | 2018-04-23 08:14:19 -0700 | [diff] [blame] | 134 | #include "profile/profile_compilation_info.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 135 | #include "runtime.h" |
Andreas Gampe | ac30fa2 | 2017-01-18 21:02:36 -0800 | [diff] [blame] | 136 | #include "runtime_callbacks.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 137 | #include "scoped_thread_state_change-inl.h" |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 138 | #include "thread-inl.h" |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 139 | #include "thread_list.h" |
Mathieu Chartier | 7778b88 | 2015-10-05 16:41:10 -0700 | [diff] [blame] | 140 | #include "trace.h" |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 141 | #include "utils/dex_cache_arrays_layout-inl.h" |
Andreas Gampe | a43ba3d | 2019-03-13 15:49:20 -0700 | [diff] [blame] | 142 | #include "verifier/class_verifier.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 143 | #include "well_known_classes.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 144 | |
| 145 | namespace art { |
| 146 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 147 | using android::base::StringPrintf; |
| 148 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 149 | static constexpr bool kSanityCheckObjects = kIsDebugBuild; |
Mathieu Chartier | 8790c7f | 2016-03-31 15:05:45 -0700 | [diff] [blame] | 150 | static constexpr bool kVerifyArtMethodDeclaringClasses = kIsDebugBuild; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 151 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 152 | static void ThrowNoClassDefFoundError(const char* fmt, ...) |
| 153 | __attribute__((__format__(__printf__, 1, 2))) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 154 | REQUIRES_SHARED(Locks::mutator_lock_); |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 155 | static void ThrowNoClassDefFoundError(const char* fmt, ...) { |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 156 | va_list args; |
| 157 | va_start(args, fmt); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 158 | Thread* self = Thread::Current(); |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 159 | self->ThrowNewExceptionV("Ljava/lang/NoClassDefFoundError;", fmt, args); |
Ian Rogers | cab0101 | 2012-01-10 17:35:46 -0800 | [diff] [blame] | 160 | va_end(args); |
| 161 | } |
| 162 | |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 163 | static bool HasInitWithString(Thread* self, ClassLinker* class_linker, const char* descriptor) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 164 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 165 | ArtMethod* method = self->GetCurrentMethod(nullptr); |
Andreas Gampe | bfdcdc1 | 2015-04-22 18:10:36 -0700 | [diff] [blame] | 166 | StackHandleScope<1> hs(self); |
| 167 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(method != nullptr ? |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 168 | method->GetDeclaringClass()->GetClassLoader() : nullptr)); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 169 | ObjPtr<mirror::Class> exception_class = class_linker->FindClass(self, descriptor, class_loader); |
Andreas Gampe | bfdcdc1 | 2015-04-22 18:10:36 -0700 | [diff] [blame] | 170 | |
| 171 | if (exception_class == nullptr) { |
| 172 | // No exc class ~ no <init>-with-string. |
| 173 | CHECK(self->IsExceptionPending()); |
| 174 | self->ClearException(); |
| 175 | return false; |
| 176 | } |
| 177 | |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 178 | ArtMethod* exception_init_method = exception_class->FindConstructor( |
| 179 | "(Ljava/lang/String;)V", class_linker->GetImagePointerSize()); |
Andreas Gampe | bfdcdc1 | 2015-04-22 18:10:36 -0700 | [diff] [blame] | 180 | return exception_init_method != nullptr; |
| 181 | } |
| 182 | |
Vladimir Marko | bb206de | 2019-03-28 10:30:32 +0000 | [diff] [blame] | 183 | static ObjPtr<mirror::Object> GetVerifyError(ObjPtr<mirror::Class> c) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 184 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 185 | ObjPtr<mirror::ClassExt> ext(c->GetExtData()); |
| 186 | if (ext == nullptr) { |
| 187 | return nullptr; |
| 188 | } else { |
| 189 | return ext->GetVerifyError(); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | // Helper for ThrowEarlierClassFailure. Throws the stored error. |
| 194 | static void HandleEarlierVerifyError(Thread* self, |
| 195 | ClassLinker* class_linker, |
| 196 | ObjPtr<mirror::Class> c) |
| 197 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 198 | ObjPtr<mirror::Object> obj = GetVerifyError(c); |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 199 | DCHECK(obj != nullptr); |
| 200 | self->AssertNoPendingException(); |
| 201 | if (obj->IsClass()) { |
| 202 | // Previous error has been stored as class. Create a new exception of that type. |
| 203 | |
| 204 | // It's possible the exception doesn't have a <init>(String). |
| 205 | std::string temp; |
| 206 | const char* descriptor = obj->AsClass()->GetDescriptor(&temp); |
| 207 | |
| 208 | if (HasInitWithString(self, class_linker, descriptor)) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 209 | self->ThrowNewException(descriptor, c->PrettyDescriptor().c_str()); |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 210 | } else { |
| 211 | self->ThrowNewException(descriptor, nullptr); |
| 212 | } |
| 213 | } else { |
| 214 | // Previous error has been stored as an instance. Just rethrow. |
Vladimir Marko | c13fbd8 | 2018-06-04 16:16:28 +0100 | [diff] [blame] | 215 | ObjPtr<mirror::Class> throwable_class = GetClassRoot<mirror::Throwable>(class_linker); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 216 | ObjPtr<mirror::Class> error_class = obj->GetClass(); |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 217 | CHECK(throwable_class->IsAssignableFrom(error_class)); |
| 218 | self->SetException(obj->AsThrowable()); |
| 219 | } |
| 220 | self->AssertPendingException(); |
| 221 | } |
| 222 | |
Andreas Gampe | 5b20b35 | 2018-10-11 19:03:20 -0700 | [diff] [blame] | 223 | // Ensures that methods have the kAccSkipAccessChecks bit set. We use the |
| 224 | // kAccVerificationAttempted bit on the class access flags to determine whether this has been done |
| 225 | // before. |
| 226 | template <bool kNeedsVerified = false> |
| 227 | static void EnsureSkipAccessChecksMethods(Handle<mirror::Class> klass, PointerSize pointer_size) |
| 228 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 229 | if (kNeedsVerified) { |
| 230 | // To not fail access-flags access checks, push a minimal state. |
| 231 | mirror::Class::SetStatus(klass, ClassStatus::kVerified, Thread::Current()); |
| 232 | } |
| 233 | if (!klass->WasVerificationAttempted()) { |
| 234 | klass->SetSkipAccessChecksFlagOnAllMethods(pointer_size); |
| 235 | klass->SetVerificationAttempted(); |
| 236 | } |
| 237 | } |
| 238 | |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 239 | // Callback responsible for making a batch of classes visibly initialized |
| 240 | // after all threads have called it from a checkpoint, ensuring visibility. |
| 241 | class ClassLinker::VisiblyInitializedCallback final |
| 242 | : public Closure, public IntrusiveForwardListNode<VisiblyInitializedCallback> { |
| 243 | public: |
| 244 | explicit VisiblyInitializedCallback(ClassLinker* class_linker) |
| 245 | : class_linker_(class_linker), |
| 246 | num_classes_(0u), |
| 247 | thread_visibility_counter_(0), |
| 248 | barriers_() { |
| 249 | std::fill_n(classes_, kMaxClasses, nullptr); |
| 250 | } |
| 251 | |
| 252 | bool IsEmpty() const { |
| 253 | DCHECK_LE(num_classes_, kMaxClasses); |
| 254 | return num_classes_ == 0u; |
| 255 | } |
| 256 | |
| 257 | bool IsFull() const { |
| 258 | DCHECK_LE(num_classes_, kMaxClasses); |
| 259 | return num_classes_ == kMaxClasses; |
| 260 | } |
| 261 | |
| 262 | void AddClass(Thread* self, ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 263 | DCHECK_EQ(klass->GetStatus(), ClassStatus::kInitialized); |
| 264 | DCHECK(!IsFull()); |
| 265 | classes_[num_classes_] = self->GetJniEnv()->GetVm()->AddWeakGlobalRef(self, klass); |
| 266 | ++num_classes_; |
| 267 | } |
| 268 | |
| 269 | void AddBarrier(Barrier* barrier) { |
| 270 | barriers_.push_front(barrier); |
| 271 | } |
| 272 | |
| 273 | std::forward_list<Barrier*> GetAndClearBarriers() { |
| 274 | std::forward_list<Barrier*> result; |
| 275 | result.swap(barriers_); |
| 276 | result.reverse(); // Return barriers in insertion order. |
| 277 | return result; |
| 278 | } |
| 279 | |
| 280 | void MakeVisible(Thread* self) { |
| 281 | DCHECK_EQ(thread_visibility_counter_.load(std::memory_order_relaxed), 0); |
| 282 | size_t count = Runtime::Current()->GetThreadList()->RunCheckpoint(this); |
| 283 | AdjustThreadVisibilityCounter(self, count); |
| 284 | } |
| 285 | |
| 286 | void Run(Thread* self) override { |
| 287 | self->ClearMakeVisiblyInitializedCounter(); |
| 288 | AdjustThreadVisibilityCounter(self, -1); |
| 289 | } |
| 290 | |
| 291 | private: |
| 292 | void AdjustThreadVisibilityCounter(Thread* self, ssize_t adjustment) { |
| 293 | ssize_t old = thread_visibility_counter_.fetch_add(adjustment, std::memory_order_relaxed); |
| 294 | if (old + adjustment == 0) { |
| 295 | // All threads passed the checkpoint. Mark classes as visibly initialized. |
| 296 | { |
| 297 | ScopedObjectAccess soa(self); |
| 298 | StackHandleScope<1u> hs(self); |
| 299 | MutableHandle<mirror::Class> klass = hs.NewHandle<mirror::Class>(nullptr); |
| 300 | JavaVMExt* vm = self->GetJniEnv()->GetVm(); |
| 301 | for (size_t i = 0, num = num_classes_; i != num; ++i) { |
| 302 | klass.Assign(ObjPtr<mirror::Class>::DownCast(self->DecodeJObject(classes_[i]))); |
| 303 | vm->DeleteWeakGlobalRef(self, classes_[i]); |
| 304 | if (klass != nullptr) { |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 305 | mirror::Class::SetStatus(klass, ClassStatus::kVisiblyInitialized, self); |
Vladimir Marko | cce414f | 2019-10-07 08:51:33 +0100 | [diff] [blame] | 306 | class_linker_->FixupStaticTrampolines(klass.Get()); |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 307 | } |
| 308 | } |
| 309 | num_classes_ = 0u; |
| 310 | } |
| 311 | class_linker_->VisiblyInitializedCallbackDone(self, this); |
| 312 | } |
| 313 | } |
| 314 | |
Vladimir Marko | 9f18fbc | 2019-07-31 15:06:12 +0100 | [diff] [blame] | 315 | static constexpr size_t kMaxClasses = 16; |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 316 | |
| 317 | ClassLinker* const class_linker_; |
| 318 | size_t num_classes_; |
| 319 | jweak classes_[kMaxClasses]; |
| 320 | |
| 321 | // The thread visibility counter starts at 0 and it is incremented by the number of |
| 322 | // threads that need to run this callback (by the thread that request the callback |
| 323 | // to be run) and decremented once for each `Run()` execution. When it reaches 0, |
| 324 | // whether after the increment or after a decrement, we know that `Run()` was executed |
| 325 | // for all threads and therefore we can mark the classes as visibly initialized. |
| 326 | std::atomic<ssize_t> thread_visibility_counter_; |
| 327 | |
| 328 | // List of barries to `Pass()` for threads that wait for the callback to complete. |
| 329 | std::forward_list<Barrier*> barriers_; |
| 330 | }; |
| 331 | |
| 332 | void ClassLinker::MakeInitializedClassesVisiblyInitialized(Thread* self, bool wait) { |
| 333 | if (kRuntimeISA == InstructionSet::kX86 || kRuntimeISA == InstructionSet::kX86_64) { |
| 334 | return; // Nothing to do. Thanks to the x86 memory model classes skip the initialized status. |
| 335 | } |
| 336 | std::optional<Barrier> maybe_barrier; // Avoid constructing the Barrier for `wait == false`. |
| 337 | if (wait) { |
| 338 | maybe_barrier.emplace(0); |
| 339 | } |
| 340 | int wait_count = 0; |
| 341 | VisiblyInitializedCallback* callback = nullptr; |
| 342 | { |
| 343 | MutexLock lock(self, visibly_initialized_callback_lock_); |
| 344 | if (visibly_initialized_callback_ != nullptr && !visibly_initialized_callback_->IsEmpty()) { |
| 345 | callback = visibly_initialized_callback_.release(); |
| 346 | running_visibly_initialized_callbacks_.push_front(*callback); |
| 347 | } |
| 348 | if (wait) { |
| 349 | DCHECK(maybe_barrier.has_value()); |
| 350 | Barrier* barrier = std::addressof(*maybe_barrier); |
| 351 | for (VisiblyInitializedCallback& cb : running_visibly_initialized_callbacks_) { |
| 352 | cb.AddBarrier(barrier); |
| 353 | ++wait_count; |
| 354 | } |
| 355 | } |
| 356 | } |
| 357 | if (callback != nullptr) { |
| 358 | callback->MakeVisible(self); |
| 359 | } |
| 360 | if (wait_count != 0) { |
| 361 | DCHECK(maybe_barrier.has_value()); |
| 362 | maybe_barrier->Increment(self, wait_count); |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | void ClassLinker::VisiblyInitializedCallbackDone(Thread* self, |
| 367 | VisiblyInitializedCallback* callback) { |
| 368 | MutexLock lock(self, visibly_initialized_callback_lock_); |
| 369 | // Pass the barriers if requested. |
| 370 | for (Barrier* barrier : callback->GetAndClearBarriers()) { |
| 371 | barrier->Pass(self); |
| 372 | } |
| 373 | // Remove the callback from the list of running callbacks. |
| 374 | auto before = running_visibly_initialized_callbacks_.before_begin(); |
| 375 | auto it = running_visibly_initialized_callbacks_.begin(); |
| 376 | DCHECK(it != running_visibly_initialized_callbacks_.end()); |
| 377 | while (std::addressof(*it) != callback) { |
| 378 | before = it; |
| 379 | ++it; |
| 380 | DCHECK(it != running_visibly_initialized_callbacks_.end()); |
| 381 | } |
| 382 | running_visibly_initialized_callbacks_.erase_after(before); |
| 383 | // Reuse or destroy the callback object. |
| 384 | if (visibly_initialized_callback_ == nullptr) { |
| 385 | visibly_initialized_callback_.reset(callback); |
| 386 | } else { |
| 387 | delete callback; |
| 388 | } |
| 389 | } |
| 390 | |
Alex Light | fb11957 | 2019-09-18 15:04:53 -0700 | [diff] [blame] | 391 | void ClassLinker::ForceClassInitialized(Thread* self, Handle<mirror::Class> klass) { |
| 392 | ClassLinker::VisiblyInitializedCallback* cb = MarkClassInitialized(self, klass); |
| 393 | if (cb != nullptr) { |
| 394 | cb->MakeVisible(self); |
| 395 | } |
| 396 | ScopedThreadSuspension sts(self, ThreadState::kSuspended); |
| 397 | MakeInitializedClassesVisiblyInitialized(self, /*wait=*/true); |
| 398 | } |
| 399 | |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 400 | ClassLinker::VisiblyInitializedCallback* ClassLinker::MarkClassInitialized( |
| 401 | Thread* self, Handle<mirror::Class> klass) { |
| 402 | if (kRuntimeISA == InstructionSet::kX86 || kRuntimeISA == InstructionSet::kX86_64) { |
| 403 | // Thanks to the x86 memory model, we do not need any memory fences and |
| 404 | // we can immediately mark the class as visibly initialized. |
| 405 | mirror::Class::SetStatus(klass, ClassStatus::kVisiblyInitialized, self); |
Vladimir Marko | cce414f | 2019-10-07 08:51:33 +0100 | [diff] [blame] | 406 | FixupStaticTrampolines(klass.Get()); |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 407 | return nullptr; |
| 408 | } |
| 409 | if (Runtime::Current()->IsActiveTransaction()) { |
| 410 | // Transactions are single-threaded, so we can mark the class as visibly intialized. |
| 411 | // (Otherwise we'd need to track the callback's entry in the transaction for rollback.) |
| 412 | mirror::Class::SetStatus(klass, ClassStatus::kVisiblyInitialized, self); |
Vladimir Marko | cce414f | 2019-10-07 08:51:33 +0100 | [diff] [blame] | 413 | FixupStaticTrampolines(klass.Get()); |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 414 | return nullptr; |
| 415 | } |
| 416 | mirror::Class::SetStatus(klass, ClassStatus::kInitialized, self); |
| 417 | MutexLock lock(self, visibly_initialized_callback_lock_); |
| 418 | if (visibly_initialized_callback_ == nullptr) { |
| 419 | visibly_initialized_callback_.reset(new VisiblyInitializedCallback(this)); |
| 420 | } |
| 421 | DCHECK(!visibly_initialized_callback_->IsFull()); |
| 422 | visibly_initialized_callback_->AddClass(self, klass.Get()); |
| 423 | |
| 424 | if (visibly_initialized_callback_->IsFull()) { |
| 425 | VisiblyInitializedCallback* callback = visibly_initialized_callback_.release(); |
| 426 | running_visibly_initialized_callbacks_.push_front(*callback); |
| 427 | return callback; |
| 428 | } else { |
| 429 | return nullptr; |
| 430 | } |
| 431 | } |
| 432 | |
Andreas Gampe | 7b3063b | 2019-01-07 14:12:52 -0800 | [diff] [blame] | 433 | void ClassLinker::ThrowEarlierClassFailure(ObjPtr<mirror::Class> c, |
| 434 | bool wrap_in_no_class_def, |
| 435 | bool log) { |
Elliott Hughes | 5c59994 | 2012-06-13 16:45:05 -0700 | [diff] [blame] | 436 | // The class failed to initialize on a previous attempt, so we want to throw |
| 437 | // a NoClassDefFoundError (v2 2.17.5). The exception to this rule is if we |
| 438 | // failed in verification, in which case v2 5.4.1 says we need to re-throw |
| 439 | // the previous error. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 440 | Runtime* const runtime = Runtime::Current(); |
| 441 | if (!runtime->IsAotCompiler()) { // Give info if this occurs at runtime. |
Andreas Gampe | 3d6b470 | 2015-09-21 08:35:52 -0700 | [diff] [blame] | 442 | std::string extra; |
Vladimir Marko | bb206de | 2019-03-28 10:30:32 +0000 | [diff] [blame] | 443 | ObjPtr<mirror::Object> verify_error = GetVerifyError(c); |
| 444 | if (verify_error != nullptr) { |
Andreas Gampe | 369c851 | 2016-01-28 15:31:39 -0800 | [diff] [blame] | 445 | if (verify_error->IsClass()) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 446 | extra = mirror::Class::PrettyDescriptor(verify_error->AsClass()); |
Andreas Gampe | 369c851 | 2016-01-28 15:31:39 -0800 | [diff] [blame] | 447 | } else { |
| 448 | extra = verify_error->AsThrowable()->Dump(); |
| 449 | } |
Andreas Gampe | 3d6b470 | 2015-09-21 08:35:52 -0700 | [diff] [blame] | 450 | } |
Andreas Gampe | 7b3063b | 2019-01-07 14:12:52 -0800 | [diff] [blame] | 451 | if (log) { |
| 452 | LOG(INFO) << "Rejecting re-init on previously-failed class " << c->PrettyClass() |
| 453 | << ": " << extra; |
| 454 | } |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 455 | } |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 456 | |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 457 | CHECK(c->IsErroneous()) << c->PrettyClass() << " " << c->GetStatus(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 458 | Thread* self = Thread::Current(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 459 | if (runtime->IsAotCompiler()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 460 | // At compile time, accurate errors and NCDFE are disabled to speed compilation. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 461 | ObjPtr<mirror::Throwable> pre_allocated = runtime->GetPreAllocatedNoClassDefFoundError(); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 462 | self->SetException(pre_allocated); |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 463 | } else { |
Vladimir Marko | bb206de | 2019-03-28 10:30:32 +0000 | [diff] [blame] | 464 | ObjPtr<mirror::Object> verify_error = GetVerifyError(c); |
| 465 | if (verify_error != nullptr) { |
Andreas Gampe | cb08695 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 466 | // Rethrow stored error. |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 467 | HandleEarlierVerifyError(self, this, c); |
Andreas Gampe | cb08695 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 468 | } |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 469 | // TODO This might be wrong if we hit an OOME while allocating the ClassExt. In that case we |
| 470 | // might have meant to go down the earlier if statement with the original error but it got |
| 471 | // swallowed by the OOM so we end up here. |
Vladimir Marko | bb206de | 2019-03-28 10:30:32 +0000 | [diff] [blame] | 472 | if (verify_error == nullptr || wrap_in_no_class_def) { |
Andreas Gampe | cb08695 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 473 | // If there isn't a recorded earlier error, or this is a repeat throw from initialization, |
| 474 | // the top-level exception must be a NoClassDefFoundError. The potentially already pending |
| 475 | // exception will be a cause. |
| 476 | self->ThrowNewWrappedException("Ljava/lang/NoClassDefFoundError;", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 477 | c->PrettyDescriptor().c_str()); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 478 | } |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 479 | } |
| 480 | } |
| 481 | |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 482 | static void VlogClassInitializationFailure(Handle<mirror::Class> klass) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 483 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 484 | if (VLOG_IS_ON(class_linker)) { |
| 485 | std::string temp; |
| 486 | LOG(INFO) << "Failed to initialize class " << klass->GetDescriptor(&temp) << " from " |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 487 | << klass->GetLocation() << "\n" << Thread::Current()->GetException()->Dump(); |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 488 | } |
| 489 | } |
| 490 | |
| 491 | static void WrapExceptionInInitializer(Handle<mirror::Class> klass) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 492 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 493 | Thread* self = Thread::Current(); |
| 494 | JNIEnv* env = self->GetJniEnv(); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 495 | |
| 496 | ScopedLocalRef<jthrowable> cause(env, env->ExceptionOccurred()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 497 | CHECK(cause.get() != nullptr); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 498 | |
Andreas Gampe | 1e8a395 | 2016-11-30 10:13:19 -0800 | [diff] [blame] | 499 | // Boot classpath classes should not fail initialization. This is a sanity debug check. This |
| 500 | // cannot in general be guaranteed, but in all likelihood leads to breakage down the line. |
| 501 | if (klass->GetClassLoader() == nullptr && !Runtime::Current()->IsAotCompiler()) { |
Andreas Gampe | 22f71d2 | 2016-11-21 10:10:08 -0800 | [diff] [blame] | 502 | std::string tmp; |
Alex Light | 5047d9f | 2018-03-09 15:44:31 -0800 | [diff] [blame] | 503 | // We want to LOG(FATAL) on debug builds since this really shouldn't be happening but we need to |
| 504 | // make sure to only do it if we don't have AsyncExceptions being thrown around since those |
| 505 | // could have caused the error. |
| 506 | bool known_impossible = kIsDebugBuild && !Runtime::Current()->AreAsyncExceptionsThrown(); |
| 507 | LOG(known_impossible ? FATAL : WARNING) << klass->GetDescriptor(&tmp) |
| 508 | << " failed initialization: " |
| 509 | << self->GetException()->Dump(); |
Andreas Gampe | 22f71d2 | 2016-11-21 10:10:08 -0800 | [diff] [blame] | 510 | } |
| 511 | |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 512 | env->ExceptionClear(); |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 513 | bool is_error = env->IsInstanceOf(cause.get(), WellKnownClasses::java_lang_Error); |
| 514 | env->Throw(cause.get()); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 515 | |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 516 | // We only wrap non-Error exceptions; an Error can just be used as-is. |
| 517 | if (!is_error) { |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 518 | self->ThrowNewWrappedException("Ljava/lang/ExceptionInInitializerError;", nullptr); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 519 | } |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 520 | VlogClassInitializationFailure(klass); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 521 | } |
| 522 | |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 523 | // Gap between two fields in object layout. |
| 524 | struct FieldGap { |
| 525 | uint32_t start_offset; // The offset from the start of the object. |
| 526 | uint32_t size; // The gap size of 1, 2, or 4 bytes. |
| 527 | }; |
| 528 | struct FieldGapsComparator { |
Igor Murashkin | 2ffb703 | 2017-11-08 13:35:21 -0800 | [diff] [blame] | 529 | FieldGapsComparator() { |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 530 | } |
| 531 | bool operator() (const FieldGap& lhs, const FieldGap& rhs) |
| 532 | NO_THREAD_SAFETY_ANALYSIS { |
Andreas Gampe | f52857f | 2015-02-18 15:38:57 -0800 | [diff] [blame] | 533 | // Sort by gap size, largest first. Secondary sort by starting offset. |
Richard Uhler | fab6788 | 2015-07-13 17:00:35 -0700 | [diff] [blame] | 534 | // Note that the priority queue returns the largest element, so operator() |
| 535 | // should return true if lhs is less than rhs. |
| 536 | 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] | 537 | } |
| 538 | }; |
Andreas Gampe | c55bb39 | 2018-09-21 00:02:02 +0000 | [diff] [blame] | 539 | using FieldGaps = std::priority_queue<FieldGap, std::vector<FieldGap>, FieldGapsComparator>; |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 540 | |
| 541 | // Adds largest aligned gaps to queue of gaps. |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 542 | 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] | 543 | DCHECK(gaps != nullptr); |
| 544 | |
| 545 | uint32_t current_offset = gap_start; |
| 546 | while (current_offset != gap_end) { |
| 547 | size_t remaining = gap_end - current_offset; |
| 548 | if (remaining >= sizeof(uint32_t) && IsAligned<4>(current_offset)) { |
| 549 | gaps->push(FieldGap {current_offset, sizeof(uint32_t)}); |
| 550 | current_offset += sizeof(uint32_t); |
| 551 | } else if (remaining >= sizeof(uint16_t) && IsAligned<2>(current_offset)) { |
| 552 | gaps->push(FieldGap {current_offset, sizeof(uint16_t)}); |
| 553 | current_offset += sizeof(uint16_t); |
| 554 | } else { |
| 555 | gaps->push(FieldGap {current_offset, sizeof(uint8_t)}); |
| 556 | current_offset += sizeof(uint8_t); |
| 557 | } |
| 558 | DCHECK_LE(current_offset, gap_end) << "Overran gap"; |
| 559 | } |
| 560 | } |
| 561 | // Shuffle fields forward, making use of gaps whenever possible. |
| 562 | template<int n> |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 563 | static void ShuffleForward(size_t* current_field_idx, |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 564 | MemberOffset* field_offset, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 565 | std::deque<ArtField*>* grouped_and_sorted_fields, |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 566 | FieldGaps* gaps) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 567 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 568 | DCHECK(current_field_idx != nullptr); |
| 569 | DCHECK(grouped_and_sorted_fields != nullptr); |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 570 | DCHECK(gaps != nullptr); |
| 571 | DCHECK(field_offset != nullptr); |
| 572 | |
| 573 | DCHECK(IsPowerOfTwo(n)); |
| 574 | while (!grouped_and_sorted_fields->empty()) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 575 | ArtField* field = grouped_and_sorted_fields->front(); |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 576 | Primitive::Type type = field->GetTypeAsPrimitiveType(); |
| 577 | if (Primitive::ComponentSize(type) < n) { |
| 578 | break; |
| 579 | } |
| 580 | if (!IsAligned<n>(field_offset->Uint32Value())) { |
| 581 | MemberOffset old_offset = *field_offset; |
| 582 | *field_offset = MemberOffset(RoundUp(field_offset->Uint32Value(), n)); |
| 583 | AddFieldGap(old_offset.Uint32Value(), field_offset->Uint32Value(), gaps); |
| 584 | } |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 585 | CHECK(type != Primitive::kPrimNot) << field->PrettyField(); // should be primitive types |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 586 | grouped_and_sorted_fields->pop_front(); |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 587 | if (!gaps->empty() && gaps->top().size >= n) { |
| 588 | FieldGap gap = gaps->top(); |
| 589 | gaps->pop(); |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 590 | DCHECK_ALIGNED(gap.start_offset, n); |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 591 | field->SetOffset(MemberOffset(gap.start_offset)); |
| 592 | if (gap.size > n) { |
| 593 | AddFieldGap(gap.start_offset + n, gap.start_offset + gap.size, gaps); |
| 594 | } |
| 595 | } else { |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 596 | DCHECK_ALIGNED(field_offset->Uint32Value(), n); |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 597 | field->SetOffset(*field_offset); |
| 598 | *field_offset = MemberOffset(field_offset->Uint32Value() + n); |
| 599 | } |
| 600 | ++(*current_field_idx); |
| 601 | } |
| 602 | } |
| 603 | |
Andreas Gampe | 87658f3 | 2019-04-18 18:39:02 +0000 | [diff] [blame] | 604 | ClassLinker::ClassLinker(InternTable* intern_table, bool fast_class_not_found_exceptions) |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 605 | : boot_class_table_(new ClassTable()), |
| 606 | failed_dex_cache_class_lookups_(0), |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 607 | class_roots_(nullptr), |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 608 | find_array_class_cache_next_victim_(0), |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 609 | init_done_(false), |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 610 | log_new_roots_(false), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 611 | intern_table_(intern_table), |
Andreas Gampe | 87658f3 | 2019-04-18 18:39:02 +0000 | [diff] [blame] | 612 | fast_class_not_found_exceptions_(fast_class_not_found_exceptions), |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 613 | quick_resolution_trampoline_(nullptr), |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 614 | quick_imt_conflict_trampoline_(nullptr), |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 615 | quick_generic_jni_trampoline_(nullptr), |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 616 | quick_to_interpreter_bridge_trampoline_(nullptr), |
Andreas Gampe | c1ac9ee | 2017-07-24 22:35:49 -0700 | [diff] [blame] | 617 | image_pointer_size_(kRuntimePointerSize), |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 618 | visibly_initialized_callback_lock_("visibly initialized callback lock"), |
| 619 | visibly_initialized_callback_(nullptr), |
Andreas Gampe | 7dface3 | 2017-07-25 21:32:59 -0700 | [diff] [blame] | 620 | cha_(Runtime::Current()->IsAotCompiler() ? nullptr : new ClassHierarchyAnalysis()) { |
| 621 | // For CHA disabled during Aot, see b/34193647. |
| 622 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 623 | CHECK(intern_table_ != nullptr); |
Andreas Gampe | 8ac7595 | 2015-06-02 21:01:45 -0700 | [diff] [blame] | 624 | static_assert(kFindArrayCacheSize == arraysize(find_array_class_cache_), |
| 625 | "Array cache size wrong."); |
| 626 | std::fill_n(find_array_class_cache_, kFindArrayCacheSize, GcRoot<mirror::Class>(nullptr)); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 627 | } |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 628 | |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 629 | void ClassLinker::CheckSystemClass(Thread* self, Handle<mirror::Class> c1, const char* descriptor) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 630 | ObjPtr<mirror::Class> c2 = FindSystemClass(self, descriptor); |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 631 | if (c2 == nullptr) { |
| 632 | LOG(FATAL) << "Could not find class " << descriptor; |
| 633 | UNREACHABLE(); |
| 634 | } |
| 635 | if (c1.Get() != c2) { |
| 636 | std::ostringstream os1, os2; |
| 637 | c1->DumpClass(os1, mirror::Class::kDumpClassFullDetail); |
| 638 | c2->DumpClass(os2, mirror::Class::kDumpClassFullDetail); |
| 639 | LOG(FATAL) << "InitWithoutImage: Class mismatch for " << descriptor |
| 640 | << ". This is most likely the result of a broken build. Make sure that " |
| 641 | << "libcore and art projects match.\n\n" |
| 642 | << os1.str() << "\n\n" << os2.str(); |
| 643 | UNREACHABLE(); |
| 644 | } |
| 645 | } |
| 646 | |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 647 | bool ClassLinker::InitWithoutImage(std::vector<std::unique_ptr<const DexFile>> boot_class_path, |
| 648 | std::string* error_msg) { |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 649 | VLOG(startup) << "ClassLinker::Init"; |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 650 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 651 | Thread* const self = Thread::Current(); |
| 652 | Runtime* const runtime = Runtime::Current(); |
| 653 | gc::Heap* const heap = runtime->GetHeap(); |
| 654 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 655 | CHECK(!heap->HasBootImageSpace()) << "Runtime has image. We should use it."; |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 656 | CHECK(!init_done_); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 657 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 658 | // Use the pointer size from the runtime since we are probably creating the image. |
| 659 | image_pointer_size_ = InstructionSetPointerSize(runtime->GetInstructionSet()); |
| 660 | |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 661 | // java_lang_Class comes first, it's needed for AllocClass |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 662 | // 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] | 663 | heap->IncrementDisableMovingGC(self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 664 | StackHandleScope<64> hs(self); // 64 is picked arbitrarily. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 665 | auto class_class_size = mirror::Class::ClassClassSize(image_pointer_size_); |
Mathieu Chartier | d7a7f2f | 2018-09-07 11:57:18 -0700 | [diff] [blame] | 666 | // Allocate the object as non-movable so that there are no cases where Object::IsClass returns |
| 667 | // the incorrect result when comparing to-space vs from-space. |
Vladimir Marko | d7e9bbf | 2019-03-28 13:18:57 +0000 | [diff] [blame] | 668 | Handle<mirror::Class> java_lang_Class(hs.NewHandle(ObjPtr<mirror::Class>::DownCast( |
Vladimir Marko | 991cd5c | 2019-05-30 14:23:39 +0100 | [diff] [blame] | 669 | heap->AllocNonMovableObject(self, nullptr, class_class_size, VoidFunctor())))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 670 | CHECK(java_lang_Class != nullptr); |
Vladimir Marko | 317892b | 2018-05-31 11:11:32 +0100 | [diff] [blame] | 671 | java_lang_Class->SetClassFlags(mirror::kClassFlagClass); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 672 | java_lang_Class->SetClass(java_lang_Class.Get()); |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 673 | if (kUseBakerReadBarrier) { |
| 674 | java_lang_Class->AssertReadBarrierState(); |
Hiroshi Yamauchi | 9d04a20 | 2014-01-31 13:35:49 -0800 | [diff] [blame] | 675 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 676 | java_lang_Class->SetClassSize(class_class_size); |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 677 | java_lang_Class->SetPrimitiveType(Primitive::kPrimNot); |
Mathieu Chartier | 1d27b34 | 2014-01-28 12:51:09 -0800 | [diff] [blame] | 678 | heap->DecrementDisableMovingGC(self); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 679 | // AllocClass(ObjPtr<mirror::Class>) can now be used |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 680 | |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 681 | // Class[] is used for reflection support. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 682 | 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] | 683 | Handle<mirror::Class> class_array_class(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 684 | AllocClass(self, java_lang_Class.Get(), class_array_class_size))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 685 | class_array_class->SetComponentType(java_lang_Class.Get()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 686 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 687 | // 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] | 688 | Handle<mirror::Class> java_lang_Object(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 689 | AllocClass(self, java_lang_Class.Get(), mirror::Object::ClassSize(image_pointer_size_)))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 690 | CHECK(java_lang_Object != nullptr); |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 691 | // backfill Object as the super class of Class. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 692 | java_lang_Class->SetSuperClass(java_lang_Object.Get()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 693 | mirror::Class::SetStatus(java_lang_Object, ClassStatus::kLoaded, self); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 694 | |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 695 | java_lang_Object->SetObjectSize(sizeof(mirror::Object)); |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 696 | // Allocate in non-movable so that it's possible to check if a JNI weak global ref has been |
| 697 | // cleared without triggering the read barrier and unintentionally mark the sentinel alive. |
Vladimir Marko | 991cd5c | 2019-05-30 14:23:39 +0100 | [diff] [blame] | 698 | runtime->SetSentinel(heap->AllocNonMovableObject(self, |
| 699 | java_lang_Object.Get(), |
| 700 | java_lang_Object->GetObjectSize(), |
| 701 | VoidFunctor())); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 702 | |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 703 | // Initialize the SubtypeCheck bitstring for java.lang.Object and java.lang.Class. |
Vladimir Marko | 305c38b | 2018-02-14 11:50:07 +0000 | [diff] [blame] | 704 | if (kBitstringSubtypeCheckEnabled) { |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 705 | // It might seem the lock here is unnecessary, however all the SubtypeCheck |
| 706 | // functions are annotated to require locks all the way down. |
| 707 | // |
| 708 | // We take the lock here to avoid using NO_THREAD_SAFETY_ANALYSIS. |
| 709 | MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_); |
Vladimir Marko | 38b8b25 | 2018-01-02 19:07:06 +0000 | [diff] [blame] | 710 | SubtypeCheck<ObjPtr<mirror::Class>>::EnsureInitialized(java_lang_Object.Get()); |
| 711 | SubtypeCheck<ObjPtr<mirror::Class>>::EnsureInitialized(java_lang_Class.Get()); |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 712 | } |
| 713 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 714 | // Object[] next to hold class roots. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 715 | Handle<mirror::Class> object_array_class(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 716 | AllocClass(self, java_lang_Class.Get(), |
| 717 | mirror::ObjectArray<mirror::Object>::ClassSize(image_pointer_size_)))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 718 | object_array_class->SetComponentType(java_lang_Object.Get()); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 719 | |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 720 | // Setup java.lang.String. |
| 721 | // |
| 722 | // We make this class non-movable for the unlikely case where it were to be |
| 723 | // moved by a sticky-bit (minor) collection when using the Generational |
| 724 | // Concurrent Copying (CC) collector, potentially creating a stale reference |
| 725 | // in the `klass_` field of one of its instances allocated in the Large-Object |
| 726 | // Space (LOS) -- see the comment about the dirty card scanning logic in |
| 727 | // art::gc::collector::ConcurrentCopying::MarkingPhase. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 728 | Handle<mirror::Class> java_lang_String(hs.NewHandle( |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 729 | AllocClass</* kMovable= */ false>( |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 730 | self, java_lang_Class.Get(), mirror::String::ClassSize(image_pointer_size_)))); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 731 | java_lang_String->SetStringClass(); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 732 | mirror::Class::SetStatus(java_lang_String, ClassStatus::kResolved, self); |
Jesse Wilson | 1415074 | 2011-07-29 19:04:44 -0400 | [diff] [blame] | 733 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 734 | // Setup java.lang.ref.Reference. |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 735 | Handle<mirror::Class> java_lang_ref_Reference(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 736 | AllocClass(self, java_lang_Class.Get(), mirror::Reference::ClassSize(image_pointer_size_)))); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 737 | java_lang_ref_Reference->SetObjectSize(mirror::Reference::InstanceSize()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 738 | mirror::Class::SetStatus(java_lang_ref_Reference, ClassStatus::kResolved, self); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 739 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 740 | // 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] | 741 | class_roots_ = GcRoot<mirror::ObjectArray<mirror::Class>>( |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 742 | mirror::ObjectArray<mirror::Class>::Alloc(self, |
| 743 | object_array_class.Get(), |
| 744 | static_cast<int32_t>(ClassRoot::kMax))); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 745 | CHECK(!class_roots_.IsNull()); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 746 | SetClassRoot(ClassRoot::kJavaLangClass, java_lang_Class.Get()); |
| 747 | SetClassRoot(ClassRoot::kJavaLangObject, java_lang_Object.Get()); |
| 748 | SetClassRoot(ClassRoot::kClassArrayClass, class_array_class.Get()); |
| 749 | SetClassRoot(ClassRoot::kObjectArrayClass, object_array_class.Get()); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 750 | SetClassRoot(ClassRoot::kJavaLangString, java_lang_String.Get()); |
| 751 | SetClassRoot(ClassRoot::kJavaLangRefReference, java_lang_ref_Reference.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 752 | |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 753 | // Fill in the empty iftable. Needs to be done after the kObjectArrayClass root is set. |
| 754 | java_lang_Object->SetIfTable(AllocIfTable(self, 0)); |
| 755 | |
Vladimir Marko | 0261055 | 2018-06-04 14:38:00 +0100 | [diff] [blame] | 756 | // Create array interface entries to populate once we can load system classes. |
| 757 | object_array_class->SetIfTable(AllocIfTable(self, 2)); |
| 758 | DCHECK_EQ(GetArrayIfTable(), object_array_class->GetIfTable()); |
| 759 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 760 | // Setup the primitive type classes. |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 761 | CreatePrimitiveClass(self, Primitive::kPrimBoolean, ClassRoot::kPrimitiveBoolean); |
| 762 | CreatePrimitiveClass(self, Primitive::kPrimByte, ClassRoot::kPrimitiveByte); |
| 763 | CreatePrimitiveClass(self, Primitive::kPrimChar, ClassRoot::kPrimitiveChar); |
| 764 | CreatePrimitiveClass(self, Primitive::kPrimShort, ClassRoot::kPrimitiveShort); |
| 765 | CreatePrimitiveClass(self, Primitive::kPrimInt, ClassRoot::kPrimitiveInt); |
| 766 | CreatePrimitiveClass(self, Primitive::kPrimLong, ClassRoot::kPrimitiveLong); |
| 767 | CreatePrimitiveClass(self, Primitive::kPrimFloat, ClassRoot::kPrimitiveFloat); |
| 768 | CreatePrimitiveClass(self, Primitive::kPrimDouble, ClassRoot::kPrimitiveDouble); |
| 769 | CreatePrimitiveClass(self, Primitive::kPrimVoid, ClassRoot::kPrimitiveVoid); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 770 | |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 771 | // Allocate the primitive array classes. We need only the native pointer |
| 772 | // array at this point (int[] or long[], depending on architecture) but |
| 773 | // we shall perform the same setup steps for all primitive array classes. |
| 774 | AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveBoolean, ClassRoot::kBooleanArrayClass); |
| 775 | AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveByte, ClassRoot::kByteArrayClass); |
| 776 | AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveChar, ClassRoot::kCharArrayClass); |
| 777 | AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveShort, ClassRoot::kShortArrayClass); |
| 778 | AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveInt, ClassRoot::kIntArrayClass); |
| 779 | AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveLong, ClassRoot::kLongArrayClass); |
| 780 | AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveFloat, ClassRoot::kFloatArrayClass); |
| 781 | AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveDouble, ClassRoot::kDoubleArrayClass); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 782 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 783 | // now that these are registered, we can use AllocClass() and AllocObjectArray |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 784 | |
Ian Rogers | 52813c9 | 2012-10-11 11:50:38 -0700 | [diff] [blame] | 785 | // Set up DexCache. This cannot be done later since AppendToBootClassPath calls AllocDexCache. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 786 | Handle<mirror::Class> java_lang_DexCache(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 787 | AllocClass(self, java_lang_Class.Get(), mirror::DexCache::ClassSize(image_pointer_size_)))); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 788 | SetClassRoot(ClassRoot::kJavaLangDexCache, java_lang_DexCache.Get()); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 789 | java_lang_DexCache->SetDexCacheClass(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 790 | java_lang_DexCache->SetObjectSize(mirror::DexCache::InstanceSize()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 791 | mirror::Class::SetStatus(java_lang_DexCache, ClassStatus::kResolved, self); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 792 | |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 793 | |
| 794 | // Setup dalvik.system.ClassExt |
| 795 | Handle<mirror::Class> dalvik_system_ClassExt(hs.NewHandle( |
| 796 | AllocClass(self, java_lang_Class.Get(), mirror::ClassExt::ClassSize(image_pointer_size_)))); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 797 | SetClassRoot(ClassRoot::kDalvikSystemClassExt, dalvik_system_ClassExt.Get()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 798 | mirror::Class::SetStatus(dalvik_system_ClassExt, ClassStatus::kResolved, self); |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 799 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 800 | // Set up array classes for string, field, method |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 801 | Handle<mirror::Class> object_array_string(hs.NewHandle( |
| 802 | AllocClass(self, java_lang_Class.Get(), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 803 | mirror::ObjectArray<mirror::String>::ClassSize(image_pointer_size_)))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 804 | object_array_string->SetComponentType(java_lang_String.Get()); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 805 | SetClassRoot(ClassRoot::kJavaLangStringArrayClass, object_array_string.Get()); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 806 | |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 807 | LinearAlloc* linear_alloc = runtime->GetLinearAlloc(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 808 | // Create runtime resolution and imt conflict methods. |
| 809 | runtime->SetResolutionMethod(runtime->CreateResolutionMethod()); |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 810 | runtime->SetImtConflictMethod(runtime->CreateImtConflictMethod(linear_alloc)); |
| 811 | runtime->SetImtUnimplementedMethod(runtime->CreateImtConflictMethod(linear_alloc)); |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 812 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 813 | // Setup boot_class_path_ and register class_path now that we can use AllocObjectArray to create |
| 814 | // DexCache instances. Needs to be after String, Field, Method arrays since AllocDexCache uses |
| 815 | // these roots. |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 816 | if (boot_class_path.empty()) { |
| 817 | *error_msg = "Boot classpath is empty."; |
| 818 | return false; |
| 819 | } |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 820 | for (auto& dex_file : boot_class_path) { |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 821 | if (dex_file.get() == nullptr) { |
| 822 | *error_msg = "Null dex file."; |
| 823 | return false; |
| 824 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 825 | AppendToBootClassPath(self, *dex_file); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 826 | boot_dex_files_.push_back(std::move(dex_file)); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 827 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 828 | |
| 829 | // now we can use FindSystemClass |
| 830 | |
Dmitry Petrochenko | f0972a4 | 2014-05-16 17:43:39 +0700 | [diff] [blame] | 831 | // Set up GenericJNI entrypoint. That is mainly a hack for common_compiler_test.h so that |
| 832 | // we do not need friend classes or a publicly exposed setter. |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 833 | quick_generic_jni_trampoline_ = GetQuickGenericJniStub(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 834 | if (!runtime->IsAotCompiler()) { |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 835 | // 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] | 836 | quick_resolution_trampoline_ = GetQuickResolutionStub(); |
| 837 | quick_imt_conflict_trampoline_ = GetQuickImtConflictStub(); |
| 838 | quick_to_interpreter_bridge_trampoline_ = GetQuickToInterpreterBridge(); |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 839 | } |
Dmitry Petrochenko | f0972a4 | 2014-05-16 17:43:39 +0700 | [diff] [blame] | 840 | |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 841 | // 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] | 842 | mirror::Class::SetStatus(java_lang_Object, ClassStatus::kNotReady, self); |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 843 | CheckSystemClass(self, java_lang_Object, "Ljava/lang/Object;"); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 844 | CHECK_EQ(java_lang_Object->GetObjectSize(), mirror::Object::InstanceSize()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 845 | mirror::Class::SetStatus(java_lang_String, ClassStatus::kNotReady, self); |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 846 | CheckSystemClass(self, java_lang_String, "Ljava/lang/String;"); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 847 | mirror::Class::SetStatus(java_lang_DexCache, ClassStatus::kNotReady, self); |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 848 | CheckSystemClass(self, java_lang_DexCache, "Ljava/lang/DexCache;"); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 849 | CHECK_EQ(java_lang_DexCache->GetObjectSize(), mirror::DexCache::InstanceSize()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 850 | mirror::Class::SetStatus(dalvik_system_ClassExt, ClassStatus::kNotReady, self); |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 851 | CheckSystemClass(self, dalvik_system_ClassExt, "Ldalvik/system/ClassExt;"); |
| 852 | CHECK_EQ(dalvik_system_ClassExt->GetObjectSize(), mirror::ClassExt::InstanceSize()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 853 | |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 854 | // Run Class through FindSystemClass. This initializes the dex_cache_ fields and register it |
| 855 | // in class_table_. |
| 856 | CheckSystemClass(self, java_lang_Class, "Ljava/lang/Class;"); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 857 | |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 858 | // Setup core array classes, i.e. Object[], String[] and Class[] and primitive |
| 859 | // arrays - can't be done until Object has a vtable and component classes are loaded. |
| 860 | FinishCoreArrayClassSetup(ClassRoot::kObjectArrayClass); |
| 861 | FinishCoreArrayClassSetup(ClassRoot::kClassArrayClass); |
| 862 | FinishCoreArrayClassSetup(ClassRoot::kJavaLangStringArrayClass); |
| 863 | FinishCoreArrayClassSetup(ClassRoot::kBooleanArrayClass); |
| 864 | FinishCoreArrayClassSetup(ClassRoot::kByteArrayClass); |
| 865 | FinishCoreArrayClassSetup(ClassRoot::kCharArrayClass); |
| 866 | FinishCoreArrayClassSetup(ClassRoot::kShortArrayClass); |
| 867 | FinishCoreArrayClassSetup(ClassRoot::kIntArrayClass); |
| 868 | FinishCoreArrayClassSetup(ClassRoot::kLongArrayClass); |
| 869 | FinishCoreArrayClassSetup(ClassRoot::kFloatArrayClass); |
| 870 | FinishCoreArrayClassSetup(ClassRoot::kDoubleArrayClass); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 871 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 872 | // Setup the single, global copy of "iftable". |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 873 | auto java_lang_Cloneable = hs.NewHandle(FindSystemClass(self, "Ljava/lang/Cloneable;")); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 874 | CHECK(java_lang_Cloneable != nullptr); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 875 | auto java_io_Serializable = hs.NewHandle(FindSystemClass(self, "Ljava/io/Serializable;")); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 876 | CHECK(java_io_Serializable != nullptr); |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 877 | // We assume that Cloneable/Serializable don't have superinterfaces -- normally we'd have to |
| 878 | // crawl up and explicitly list all of the supers as well. |
Vladimir Marko | 0261055 | 2018-06-04 14:38:00 +0100 | [diff] [blame] | 879 | object_array_class->GetIfTable()->SetInterface(0, java_lang_Cloneable.Get()); |
| 880 | object_array_class->GetIfTable()->SetInterface(1, java_io_Serializable.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 881 | |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 882 | // Sanity check Class[] and Object[]'s interfaces. GetDirectInterface may cause thread |
| 883 | // suspension. |
| 884 | CHECK_EQ(java_lang_Cloneable.Get(), |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 885 | mirror::Class::GetDirectInterface(self, class_array_class.Get(), 0)); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 886 | CHECK_EQ(java_io_Serializable.Get(), |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 887 | mirror::Class::GetDirectInterface(self, class_array_class.Get(), 1)); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 888 | CHECK_EQ(java_lang_Cloneable.Get(), |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 889 | mirror::Class::GetDirectInterface(self, object_array_class.Get(), 0)); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 890 | CHECK_EQ(java_io_Serializable.Get(), |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 891 | mirror::Class::GetDirectInterface(self, object_array_class.Get(), 1)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 892 | |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 893 | CHECK_EQ(object_array_string.Get(), |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 894 | FindSystemClass(self, GetClassRootDescriptor(ClassRoot::kJavaLangStringArrayClass))); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 895 | |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 896 | // 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] | 897 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 898 | // Create java.lang.reflect.Proxy root. |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 899 | SetClassRoot(ClassRoot::kJavaLangReflectProxy, |
| 900 | FindSystemClass(self, "Ljava/lang/reflect/Proxy;")); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 901 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 902 | // Create java.lang.reflect.Field.class root. |
Vladimir Marko | acb906d | 2018-05-30 10:23:49 +0100 | [diff] [blame] | 903 | ObjPtr<mirror::Class> class_root = FindSystemClass(self, "Ljava/lang/reflect/Field;"); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 904 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 905 | SetClassRoot(ClassRoot::kJavaLangReflectField, class_root); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 906 | |
| 907 | // Create java.lang.reflect.Field array root. |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 908 | class_root = FindSystemClass(self, "[Ljava/lang/reflect/Field;"); |
| 909 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 910 | SetClassRoot(ClassRoot::kJavaLangReflectFieldArrayClass, class_root); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 911 | |
| 912 | // Create java.lang.reflect.Constructor.class root and array root. |
| 913 | class_root = FindSystemClass(self, "Ljava/lang/reflect/Constructor;"); |
| 914 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 915 | SetClassRoot(ClassRoot::kJavaLangReflectConstructor, class_root); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 916 | class_root = FindSystemClass(self, "[Ljava/lang/reflect/Constructor;"); |
| 917 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 918 | SetClassRoot(ClassRoot::kJavaLangReflectConstructorArrayClass, class_root); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 919 | |
| 920 | // Create java.lang.reflect.Method.class root and array root. |
| 921 | class_root = FindSystemClass(self, "Ljava/lang/reflect/Method;"); |
| 922 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 923 | SetClassRoot(ClassRoot::kJavaLangReflectMethod, class_root); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 924 | class_root = FindSystemClass(self, "[Ljava/lang/reflect/Method;"); |
| 925 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 926 | SetClassRoot(ClassRoot::kJavaLangReflectMethodArrayClass, class_root); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 927 | |
Orion Hodson | 005ac51 | 2017-10-24 15:43:43 +0100 | [diff] [blame] | 928 | // Create java.lang.invoke.CallSite.class root |
| 929 | class_root = FindSystemClass(self, "Ljava/lang/invoke/CallSite;"); |
| 930 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 931 | SetClassRoot(ClassRoot::kJavaLangInvokeCallSite, class_root); |
Orion Hodson | 005ac51 | 2017-10-24 15:43:43 +0100 | [diff] [blame] | 932 | |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 933 | // Create java.lang.invoke.MethodType.class root |
| 934 | class_root = FindSystemClass(self, "Ljava/lang/invoke/MethodType;"); |
| 935 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 936 | SetClassRoot(ClassRoot::kJavaLangInvokeMethodType, class_root); |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 937 | |
| 938 | // Create java.lang.invoke.MethodHandleImpl.class root |
| 939 | class_root = FindSystemClass(self, "Ljava/lang/invoke/MethodHandleImpl;"); |
| 940 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 941 | SetClassRoot(ClassRoot::kJavaLangInvokeMethodHandleImpl, class_root); |
Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 942 | SetClassRoot(ClassRoot::kJavaLangInvokeMethodHandle, class_root->GetSuperClass()); |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 943 | |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 944 | // Create java.lang.invoke.MethodHandles.Lookup.class root |
| 945 | class_root = FindSystemClass(self, "Ljava/lang/invoke/MethodHandles$Lookup;"); |
| 946 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 947 | SetClassRoot(ClassRoot::kJavaLangInvokeMethodHandlesLookup, class_root); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 948 | |
Orion Hodson | 005ac51 | 2017-10-24 15:43:43 +0100 | [diff] [blame] | 949 | // Create java.lang.invoke.VarHandle.class root |
| 950 | class_root = FindSystemClass(self, "Ljava/lang/invoke/VarHandle;"); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 951 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 952 | SetClassRoot(ClassRoot::kJavaLangInvokeVarHandle, class_root); |
Orion Hodson | 005ac51 | 2017-10-24 15:43:43 +0100 | [diff] [blame] | 953 | |
| 954 | // Create java.lang.invoke.FieldVarHandle.class root |
| 955 | class_root = FindSystemClass(self, "Ljava/lang/invoke/FieldVarHandle;"); |
| 956 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 957 | SetClassRoot(ClassRoot::kJavaLangInvokeFieldVarHandle, class_root); |
Orion Hodson | 005ac51 | 2017-10-24 15:43:43 +0100 | [diff] [blame] | 958 | |
| 959 | // Create java.lang.invoke.ArrayElementVarHandle.class root |
| 960 | class_root = FindSystemClass(self, "Ljava/lang/invoke/ArrayElementVarHandle;"); |
| 961 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 962 | SetClassRoot(ClassRoot::kJavaLangInvokeArrayElementVarHandle, class_root); |
Orion Hodson | 005ac51 | 2017-10-24 15:43:43 +0100 | [diff] [blame] | 963 | |
| 964 | // Create java.lang.invoke.ByteArrayViewVarHandle.class root |
| 965 | class_root = FindSystemClass(self, "Ljava/lang/invoke/ByteArrayViewVarHandle;"); |
| 966 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 967 | SetClassRoot(ClassRoot::kJavaLangInvokeByteArrayViewVarHandle, class_root); |
Orion Hodson | 005ac51 | 2017-10-24 15:43:43 +0100 | [diff] [blame] | 968 | |
| 969 | // Create java.lang.invoke.ByteBufferViewVarHandle.class root |
| 970 | class_root = FindSystemClass(self, "Ljava/lang/invoke/ByteBufferViewVarHandle;"); |
| 971 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 972 | SetClassRoot(ClassRoot::kJavaLangInvokeByteBufferViewVarHandle, class_root); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 973 | |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 974 | class_root = FindSystemClass(self, "Ldalvik/system/EmulatedStackFrame;"); |
| 975 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 976 | SetClassRoot(ClassRoot::kDalvikSystemEmulatedStackFrame, class_root); |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 977 | |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 978 | // 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] | 979 | // finish initializing Reference class |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 980 | mirror::Class::SetStatus(java_lang_ref_Reference, ClassStatus::kNotReady, self); |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 981 | CheckSystemClass(self, java_lang_ref_Reference, "Ljava/lang/ref/Reference;"); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 982 | CHECK_EQ(java_lang_ref_Reference->GetObjectSize(), mirror::Reference::InstanceSize()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 983 | CHECK_EQ(java_lang_ref_Reference->GetClassSize(), |
| 984 | mirror::Reference::ClassSize(image_pointer_size_)); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 985 | class_root = FindSystemClass(self, "Ljava/lang/ref/FinalizerReference;"); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 986 | CHECK_EQ(class_root->GetClassFlags(), mirror::kClassFlagNormal); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 987 | class_root->SetClassFlags(class_root->GetClassFlags() | mirror::kClassFlagFinalizerReference); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 988 | class_root = FindSystemClass(self, "Ljava/lang/ref/PhantomReference;"); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 989 | CHECK_EQ(class_root->GetClassFlags(), mirror::kClassFlagNormal); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 990 | class_root->SetClassFlags(class_root->GetClassFlags() | mirror::kClassFlagPhantomReference); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 991 | class_root = FindSystemClass(self, "Ljava/lang/ref/SoftReference;"); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 992 | CHECK_EQ(class_root->GetClassFlags(), mirror::kClassFlagNormal); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 993 | class_root->SetClassFlags(class_root->GetClassFlags() | mirror::kClassFlagSoftReference); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 994 | class_root = FindSystemClass(self, "Ljava/lang/ref/WeakReference;"); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 995 | CHECK_EQ(class_root->GetClassFlags(), mirror::kClassFlagNormal); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 996 | class_root->SetClassFlags(class_root->GetClassFlags() | mirror::kClassFlagWeakReference); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 997 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 998 | // Setup the ClassLoader, verifying the object_size_. |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 999 | class_root = FindSystemClass(self, "Ljava/lang/ClassLoader;"); |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 1000 | class_root->SetClassLoaderClass(); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 1001 | CHECK_EQ(class_root->GetObjectSize(), mirror::ClassLoader::InstanceSize()); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 1002 | SetClassRoot(ClassRoot::kJavaLangClassLoader, class_root); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1003 | |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 1004 | // Set up java.lang.Throwable, java.lang.ClassNotFoundException, and |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 1005 | // java.lang.StackTraceElement as a convenience. |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 1006 | SetClassRoot(ClassRoot::kJavaLangThrowable, FindSystemClass(self, "Ljava/lang/Throwable;")); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 1007 | SetClassRoot(ClassRoot::kJavaLangClassNotFoundException, |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 1008 | FindSystemClass(self, "Ljava/lang/ClassNotFoundException;")); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 1009 | SetClassRoot(ClassRoot::kJavaLangStackTraceElement, |
| 1010 | FindSystemClass(self, "Ljava/lang/StackTraceElement;")); |
| 1011 | SetClassRoot(ClassRoot::kJavaLangStackTraceElementArrayClass, |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 1012 | FindSystemClass(self, "[Ljava/lang/StackTraceElement;")); |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 1013 | SetClassRoot(ClassRoot::kJavaLangClassLoaderArrayClass, |
| 1014 | FindSystemClass(self, "[Ljava/lang/ClassLoader;")); |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 1015 | |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 1016 | // Create conflict tables that depend on the class linker. |
| 1017 | runtime->FixupConflictTables(); |
| 1018 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1019 | FinishInit(self); |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 1020 | |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 1021 | VLOG(startup) << "ClassLinker::InitFromCompiler exiting"; |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 1022 | |
| 1023 | return true; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1024 | } |
| 1025 | |
Andreas Gampe | 9abc31e | 2018-05-17 11:47:09 -0700 | [diff] [blame] | 1026 | static void CreateStringInitBindings(Thread* self, ClassLinker* class_linker) |
| 1027 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1028 | // Find String.<init> -> StringFactory bindings. |
| 1029 | ObjPtr<mirror::Class> string_factory_class = |
| 1030 | class_linker->FindSystemClass(self, "Ljava/lang/StringFactory;"); |
| 1031 | CHECK(string_factory_class != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 1032 | ObjPtr<mirror::Class> string_class = GetClassRoot<mirror::String>(class_linker); |
Andreas Gampe | 9abc31e | 2018-05-17 11:47:09 -0700 | [diff] [blame] | 1033 | WellKnownClasses::InitStringInit(string_class, string_factory_class); |
| 1034 | // Update the primordial thread. |
| 1035 | self->InitStringEntryPoints(); |
| 1036 | } |
| 1037 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1038 | void ClassLinker::FinishInit(Thread* self) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1039 | VLOG(startup) << "ClassLinker::FinishInit entering"; |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 1040 | |
Andreas Gampe | 9abc31e | 2018-05-17 11:47:09 -0700 | [diff] [blame] | 1041 | CreateStringInitBindings(self, this); |
| 1042 | |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 1043 | // Let the heap know some key offsets into java.lang.ref instances |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 1044 | // Note: we hard code the field indexes here rather than using FindInstanceField |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 1045 | // as the types of the field can't be resolved prior to the runtime being |
| 1046 | // fully initialized |
Andreas Gampe | 7b2450e | 2018-06-19 10:45:54 -0700 | [diff] [blame] | 1047 | StackHandleScope<3> hs(self); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 1048 | Handle<mirror::Class> java_lang_ref_Reference = |
| 1049 | hs.NewHandle(GetClassRoot<mirror::Reference>(this)); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1050 | Handle<mirror::Class> java_lang_ref_FinalizerReference = |
| 1051 | hs.NewHandle(FindSystemClass(self, "Ljava/lang/ref/FinalizerReference;")); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1052 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1053 | ArtField* pendingNext = java_lang_ref_Reference->GetInstanceField(0); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1054 | CHECK_STREQ(pendingNext->GetName(), "pendingNext"); |
| 1055 | CHECK_STREQ(pendingNext->GetTypeDescriptor(), "Ljava/lang/ref/Reference;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 1056 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1057 | ArtField* queue = java_lang_ref_Reference->GetInstanceField(1); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1058 | CHECK_STREQ(queue->GetName(), "queue"); |
| 1059 | CHECK_STREQ(queue->GetTypeDescriptor(), "Ljava/lang/ref/ReferenceQueue;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 1060 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1061 | ArtField* queueNext = java_lang_ref_Reference->GetInstanceField(2); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1062 | CHECK_STREQ(queueNext->GetName(), "queueNext"); |
| 1063 | CHECK_STREQ(queueNext->GetTypeDescriptor(), "Ljava/lang/ref/Reference;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 1064 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1065 | ArtField* referent = java_lang_ref_Reference->GetInstanceField(3); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1066 | CHECK_STREQ(referent->GetName(), "referent"); |
| 1067 | CHECK_STREQ(referent->GetTypeDescriptor(), "Ljava/lang/Object;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 1068 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1069 | ArtField* zombie = java_lang_ref_FinalizerReference->GetInstanceField(2); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1070 | CHECK_STREQ(zombie->GetName(), "zombie"); |
| 1071 | CHECK_STREQ(zombie->GetTypeDescriptor(), "Ljava/lang/Object;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 1072 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1073 | // ensure all class_roots_ are initialized |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 1074 | 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] | 1075 | ClassRoot class_root = static_cast<ClassRoot>(i); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1076 | ObjPtr<mirror::Class> klass = GetClassRoot(class_root); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1077 | CHECK(klass != nullptr); |
| 1078 | DCHECK(klass->IsArrayClass() || klass->IsPrimitive() || klass->GetDexCache() != nullptr); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1079 | // note SetClassRoot does additional validation. |
| 1080 | // if possible add new checks there to catch errors early |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 1081 | } |
| 1082 | |
Vladimir Marko | 0261055 | 2018-06-04 14:38:00 +0100 | [diff] [blame] | 1083 | CHECK(GetArrayIfTable() != nullptr); |
Elliott Hughes | 92f14b2 | 2011-10-06 12:29:54 -0700 | [diff] [blame] | 1084 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 1085 | // disable the slow paths in FindClass and CreatePrimitiveClass now |
| 1086 | // that Object, Class, and Object[] are setup |
| 1087 | init_done_ = true; |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 1088 | |
Andreas Gampe | 7b2450e | 2018-06-19 10:45:54 -0700 | [diff] [blame] | 1089 | // Under sanitization, the small carve-out to handle stack overflow might not be enough to |
| 1090 | // initialize the StackOverflowError class (as it might require running the verifier). Instead, |
| 1091 | // ensure that the class will be initialized. |
| 1092 | if (kMemoryToolIsAvailable && !Runtime::Current()->IsAotCompiler()) { |
Andreas Gampe | e0bbab9 | 2019-07-25 12:28:22 -0700 | [diff] [blame] | 1093 | verifier::ClassVerifier::Init(this); // Need to prepare the verifier. |
Andreas Gampe | 7b2450e | 2018-06-19 10:45:54 -0700 | [diff] [blame] | 1094 | |
| 1095 | ObjPtr<mirror::Class> soe_klass = FindSystemClass(self, "Ljava/lang/StackOverflowError;"); |
| 1096 | if (soe_klass == nullptr || !EnsureInitialized(self, hs.NewHandle(soe_klass), true, true)) { |
| 1097 | // Strange, but don't crash. |
| 1098 | LOG(WARNING) << "Could not prepare StackOverflowError."; |
| 1099 | self->ClearException(); |
| 1100 | } |
| 1101 | } |
| 1102 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1103 | VLOG(startup) << "ClassLinker::FinishInit exiting"; |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 1104 | } |
| 1105 | |
Vladimir Marko | dcfcce4 | 2018-06-27 10:00:28 +0000 | [diff] [blame] | 1106 | void ClassLinker::RunRootClinits(Thread* self) { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 1107 | for (size_t i = 0; i < static_cast<size_t>(ClassRoot::kMax); ++i) { |
| 1108 | ObjPtr<mirror::Class> c = GetClassRoot(ClassRoot(i), this); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 1109 | if (!c->IsArrayClass() && !c->IsPrimitive()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1110 | StackHandleScope<1> hs(self); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 1111 | Handle<mirror::Class> h_class(hs.NewHandle(c)); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 1112 | EnsureInitialized(self, h_class, true, true); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1113 | self->AssertNoPendingException(); |
Vladimir Marko | dcfcce4 | 2018-06-27 10:00:28 +0000 | [diff] [blame] | 1114 | } else { |
| 1115 | DCHECK(c->IsInitialized()); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 1116 | } |
| 1117 | } |
| 1118 | } |
| 1119 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1120 | struct TrampolineCheckData { |
| 1121 | const void* quick_resolution_trampoline; |
| 1122 | const void* quick_imt_conflict_trampoline; |
| 1123 | const void* quick_generic_jni_trampoline; |
| 1124 | const void* quick_to_interpreter_bridge_trampoline; |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1125 | PointerSize pointer_size; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1126 | ArtMethod* m; |
| 1127 | bool error; |
| 1128 | }; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1129 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1130 | bool ClassLinker::InitFromBootImage(std::string* error_msg) { |
| 1131 | VLOG(startup) << __FUNCTION__ << " entering"; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1132 | CHECK(!init_done_); |
| 1133 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 1134 | Runtime* const runtime = Runtime::Current(); |
| 1135 | Thread* const self = Thread::Current(); |
| 1136 | gc::Heap* const heap = runtime->GetHeap(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1137 | std::vector<gc::space::ImageSpace*> spaces = heap->GetBootImageSpaces(); |
| 1138 | CHECK(!spaces.empty()); |
Vladimir Marko | 024d69f | 2019-06-13 10:52:32 +0100 | [diff] [blame] | 1139 | const ImageHeader& image_header = spaces[0]->GetImageHeader(); |
| 1140 | uint32_t pointer_size_unchecked = image_header.GetPointerSizeUnchecked(); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1141 | if (!ValidPointerSize(pointer_size_unchecked)) { |
| 1142 | *error_msg = StringPrintf("Invalid image pointer size: %u", pointer_size_unchecked); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1143 | return false; |
| 1144 | } |
Vladimir Marko | 3364d18 | 2019-03-13 13:55:01 +0000 | [diff] [blame] | 1145 | image_pointer_size_ = image_header.GetPointerSize(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1146 | if (!runtime->IsAotCompiler()) { |
| 1147 | // Only the Aot compiler supports having an image with a different pointer size than the |
| 1148 | // runtime. This happens on the host for compiling 32 bit tests since we use a 64 bit libart |
| 1149 | // 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] | 1150 | if (image_pointer_size_ != kRuntimePointerSize) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1151 | *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] | 1152 | static_cast<size_t>(image_pointer_size_), |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1153 | sizeof(void*)); |
| 1154 | return false; |
| 1155 | } |
| 1156 | } |
Vladimir Marko | 3364d18 | 2019-03-13 13:55:01 +0000 | [diff] [blame] | 1157 | DCHECK(!runtime->HasResolutionMethod()); |
| 1158 | runtime->SetResolutionMethod(image_header.GetImageMethod(ImageHeader::kResolutionMethod)); |
| 1159 | runtime->SetImtConflictMethod(image_header.GetImageMethod(ImageHeader::kImtConflictMethod)); |
| 1160 | runtime->SetImtUnimplementedMethod( |
| 1161 | image_header.GetImageMethod(ImageHeader::kImtUnimplementedMethod)); |
| 1162 | runtime->SetCalleeSaveMethod( |
| 1163 | image_header.GetImageMethod(ImageHeader::kSaveAllCalleeSavesMethod), |
| 1164 | CalleeSaveType::kSaveAllCalleeSaves); |
| 1165 | runtime->SetCalleeSaveMethod( |
| 1166 | image_header.GetImageMethod(ImageHeader::kSaveRefsOnlyMethod), |
| 1167 | CalleeSaveType::kSaveRefsOnly); |
| 1168 | runtime->SetCalleeSaveMethod( |
| 1169 | image_header.GetImageMethod(ImageHeader::kSaveRefsAndArgsMethod), |
| 1170 | CalleeSaveType::kSaveRefsAndArgs); |
| 1171 | runtime->SetCalleeSaveMethod( |
| 1172 | image_header.GetImageMethod(ImageHeader::kSaveEverythingMethod), |
| 1173 | CalleeSaveType::kSaveEverything); |
| 1174 | runtime->SetCalleeSaveMethod( |
| 1175 | image_header.GetImageMethod(ImageHeader::kSaveEverythingMethodForClinit), |
| 1176 | CalleeSaveType::kSaveEverythingForClinit); |
| 1177 | runtime->SetCalleeSaveMethod( |
| 1178 | image_header.GetImageMethod(ImageHeader::kSaveEverythingMethodForSuspendCheck), |
| 1179 | CalleeSaveType::kSaveEverythingForSuspendCheck); |
| 1180 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1181 | std::vector<const OatFile*> oat_files = |
| 1182 | runtime->GetOatFileManager().RegisterImageOatFiles(spaces); |
| 1183 | DCHECK(!oat_files.empty()); |
| 1184 | const OatHeader& default_oat_header = oat_files[0]->GetOatHeader(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1185 | quick_resolution_trampoline_ = default_oat_header.GetQuickResolutionTrampoline(); |
| 1186 | quick_imt_conflict_trampoline_ = default_oat_header.GetQuickImtConflictTrampoline(); |
| 1187 | quick_generic_jni_trampoline_ = default_oat_header.GetQuickGenericJniTrampoline(); |
| 1188 | quick_to_interpreter_bridge_trampoline_ = default_oat_header.GetQuickToInterpreterBridge(); |
| 1189 | if (kIsDebugBuild) { |
| 1190 | // Check that the other images use the same trampoline. |
| 1191 | for (size_t i = 1; i < oat_files.size(); ++i) { |
| 1192 | const OatHeader& ith_oat_header = oat_files[i]->GetOatHeader(); |
| 1193 | const void* ith_quick_resolution_trampoline = |
| 1194 | ith_oat_header.GetQuickResolutionTrampoline(); |
| 1195 | const void* ith_quick_imt_conflict_trampoline = |
| 1196 | ith_oat_header.GetQuickImtConflictTrampoline(); |
| 1197 | const void* ith_quick_generic_jni_trampoline = |
| 1198 | ith_oat_header.GetQuickGenericJniTrampoline(); |
| 1199 | const void* ith_quick_to_interpreter_bridge_trampoline = |
| 1200 | ith_oat_header.GetQuickToInterpreterBridge(); |
| 1201 | if (ith_quick_resolution_trampoline != quick_resolution_trampoline_ || |
| 1202 | ith_quick_imt_conflict_trampoline != quick_imt_conflict_trampoline_ || |
| 1203 | ith_quick_generic_jni_trampoline != quick_generic_jni_trampoline_ || |
| 1204 | ith_quick_to_interpreter_bridge_trampoline != quick_to_interpreter_bridge_trampoline_) { |
| 1205 | // Make sure that all methods in this image do not contain those trampolines as |
| 1206 | // entrypoints. Otherwise the class-linker won't be able to work with a single set. |
| 1207 | TrampolineCheckData data; |
| 1208 | data.error = false; |
| 1209 | data.pointer_size = GetImagePointerSize(); |
| 1210 | data.quick_resolution_trampoline = ith_quick_resolution_trampoline; |
| 1211 | data.quick_imt_conflict_trampoline = ith_quick_imt_conflict_trampoline; |
| 1212 | data.quick_generic_jni_trampoline = ith_quick_generic_jni_trampoline; |
| 1213 | data.quick_to_interpreter_bridge_trampoline = ith_quick_to_interpreter_bridge_trampoline; |
| 1214 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Andreas Gampe | 0c18338 | 2017-07-13 22:26:24 -0700 | [diff] [blame] | 1215 | auto visitor = [&](mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1216 | if (obj->IsClass()) { |
| 1217 | ObjPtr<mirror::Class> klass = obj->AsClass(); |
| 1218 | for (ArtMethod& m : klass->GetMethods(data.pointer_size)) { |
| 1219 | const void* entrypoint = |
| 1220 | m.GetEntryPointFromQuickCompiledCodePtrSize(data.pointer_size); |
| 1221 | if (entrypoint == data.quick_resolution_trampoline || |
| 1222 | entrypoint == data.quick_imt_conflict_trampoline || |
| 1223 | entrypoint == data.quick_generic_jni_trampoline || |
| 1224 | entrypoint == data.quick_to_interpreter_bridge_trampoline) { |
| 1225 | data.m = &m; |
| 1226 | data.error = true; |
| 1227 | return; |
| 1228 | } |
| 1229 | } |
| 1230 | } |
| 1231 | }; |
| 1232 | spaces[i]->GetLiveBitmap()->Walk(visitor); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1233 | if (data.error) { |
| 1234 | ArtMethod* m = data.m; |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1235 | LOG(ERROR) << "Found a broken ArtMethod: " << ArtMethod::PrettyMethod(m); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1236 | *error_msg = "Found an ArtMethod with a bad entrypoint"; |
| 1237 | return false; |
| 1238 | } |
| 1239 | } |
| 1240 | } |
| 1241 | } |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 1242 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1243 | class_roots_ = GcRoot<mirror::ObjectArray<mirror::Class>>( |
Vladimir Marko | d7e9bbf | 2019-03-28 13:18:57 +0000 | [diff] [blame] | 1244 | ObjPtr<mirror::ObjectArray<mirror::Class>>::DownCast( |
Vladimir Marko | 024d69f | 2019-06-13 10:52:32 +0100 | [diff] [blame] | 1245 | image_header.GetImageRoot(ImageHeader::kClassRoots))); |
Vladimir Marko | f75613c | 2018-06-05 12:51:04 +0100 | [diff] [blame] | 1246 | DCHECK_EQ(GetClassRoot<mirror::Class>(this)->GetClassFlags(), mirror::kClassFlagClass); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1247 | |
Vladimir Marko | 024d69f | 2019-06-13 10:52:32 +0100 | [diff] [blame] | 1248 | DCHECK_EQ(GetClassRoot<mirror::Object>(this)->GetObjectSize(), sizeof(mirror::Object)); |
| 1249 | ObjPtr<mirror::ObjectArray<mirror::Object>> boot_image_live_objects = |
| 1250 | ObjPtr<mirror::ObjectArray<mirror::Object>>::DownCast( |
| 1251 | image_header.GetImageRoot(ImageHeader::kBootImageLiveObjects)); |
| 1252 | runtime->SetSentinel(boot_image_live_objects->Get(ImageHeader::kClearedJniWeakSentinel)); |
| 1253 | DCHECK(runtime->GetSentinel().Read()->GetClass() == GetClassRoot<mirror::Object>(this)); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 1254 | |
Vladimir Marko | 4433c43 | 2018-12-04 14:57:47 +0000 | [diff] [blame] | 1255 | const std::vector<std::string>& boot_class_path_locations = runtime->GetBootClassPathLocations(); |
| 1256 | CHECK_LE(spaces.size(), boot_class_path_locations.size()); |
Vladimir Marko | d190851 | 2018-11-22 14:57:28 +0000 | [diff] [blame] | 1257 | for (size_t i = 0u, size = spaces.size(); i != size; ++i) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1258 | // Boot class loader, use a null handle. |
| 1259 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
Vladimir Marko | d190851 | 2018-11-22 14:57:28 +0000 | [diff] [blame] | 1260 | if (!AddImageSpace(spaces[i], |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1261 | ScopedNullHandle<mirror::ClassLoader>(), |
Vladimir Marko | d190851 | 2018-11-22 14:57:28 +0000 | [diff] [blame] | 1262 | /*dex_elements=*/ nullptr, |
Vladimir Marko | 4433c43 | 2018-12-04 14:57:47 +0000 | [diff] [blame] | 1263 | /*dex_location=*/ boot_class_path_locations[i].c_str(), |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1264 | /*out*/&dex_files, |
| 1265 | error_msg)) { |
| 1266 | return false; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1267 | } |
David Brazdil | 3e8aae0 | 2019-03-26 18:48:02 +0000 | [diff] [blame] | 1268 | // Assert that if absolute boot classpath locations were provided, they were |
| 1269 | // assigned to the loaded dex files. |
| 1270 | if (kIsDebugBuild && IsAbsoluteLocation(boot_class_path_locations[i])) { |
| 1271 | for (const auto& dex_file : dex_files) { |
| 1272 | DCHECK_EQ(DexFileLoader::GetBaseLocation(dex_file->GetLocation()), |
| 1273 | boot_class_path_locations[i]); |
| 1274 | } |
| 1275 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1276 | // Append opened dex files at the end. |
| 1277 | boot_dex_files_.insert(boot_dex_files_.end(), |
| 1278 | std::make_move_iterator(dex_files.begin()), |
| 1279 | std::make_move_iterator(dex_files.end())); |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 1280 | } |
Mathieu Chartier | be8303d | 2017-08-17 17:39:39 -0700 | [diff] [blame] | 1281 | for (const std::unique_ptr<const DexFile>& dex_file : boot_dex_files_) { |
| 1282 | OatDexFile::MadviseDexFile(*dex_file, MadviseState::kMadviseStateAtLoad); |
| 1283 | } |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1284 | FinishInit(self); |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 1285 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1286 | VLOG(startup) << __FUNCTION__ << " exiting"; |
| 1287 | return true; |
| 1288 | } |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 1289 | |
Vladimir Marko | 4433c43 | 2018-12-04 14:57:47 +0000 | [diff] [blame] | 1290 | void ClassLinker::AddExtraBootDexFiles( |
| 1291 | Thread* self, |
| 1292 | std::vector<std::unique_ptr<const DexFile>>&& additional_dex_files) { |
| 1293 | for (std::unique_ptr<const DexFile>& dex_file : additional_dex_files) { |
| 1294 | AppendToBootClassPath(self, *dex_file); |
| 1295 | boot_dex_files_.push_back(std::move(dex_file)); |
| 1296 | } |
| 1297 | } |
| 1298 | |
Jeff Hao | 5872d7c | 2016-04-27 11:07:41 -0700 | [diff] [blame] | 1299 | bool ClassLinker::IsBootClassLoader(ScopedObjectAccessAlreadyRunnable& soa, |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1300 | ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1301 | return class_loader == nullptr || |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 1302 | soa.Decode<mirror::Class>(WellKnownClasses::java_lang_BootClassLoader) == |
| 1303 | class_loader->GetClass(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1304 | } |
| 1305 | |
Mathieu Chartier | 8a1691f | 2017-03-02 12:02:13 -0800 | [diff] [blame] | 1306 | static bool GetDexPathListElementName(ObjPtr<mirror::Object> element, |
| 1307 | ObjPtr<mirror::String>* out_name) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1308 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1309 | ArtField* const dex_file_field = |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 1310 | jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1311 | ArtField* const dex_file_name_field = |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 1312 | jni::DecodeArtField(WellKnownClasses::dalvik_system_DexFile_fileName); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1313 | DCHECK(dex_file_field != nullptr); |
| 1314 | DCHECK(dex_file_name_field != nullptr); |
| 1315 | DCHECK(element != nullptr); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1316 | CHECK_EQ(dex_file_field->GetDeclaringClass(), element->GetClass()) << element->PrettyTypeOf(); |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 1317 | ObjPtr<mirror::Object> dex_file = dex_file_field->GetObject(element); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1318 | if (dex_file == nullptr) { |
Mathieu Chartier | 8a1691f | 2017-03-02 12:02:13 -0800 | [diff] [blame] | 1319 | // Null dex file means it was probably a jar with no dex files, return a null string. |
| 1320 | *out_name = nullptr; |
| 1321 | return true; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1322 | } |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 1323 | ObjPtr<mirror::Object> name_object = dex_file_name_field->GetObject(dex_file); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1324 | if (name_object != nullptr) { |
Mathieu Chartier | 8a1691f | 2017-03-02 12:02:13 -0800 | [diff] [blame] | 1325 | *out_name = name_object->AsString(); |
| 1326 | return true; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1327 | } |
Mathieu Chartier | 8a1691f | 2017-03-02 12:02:13 -0800 | [diff] [blame] | 1328 | return false; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1329 | } |
| 1330 | |
Nicolas Geoffray | f0d3002 | 2018-11-20 17:45:38 +0000 | [diff] [blame] | 1331 | static bool GetDexFileNames(ScopedObjectAccessUnchecked& soa, |
| 1332 | ObjPtr<mirror::ClassLoader> class_loader, |
| 1333 | /*out*/std::list<ObjPtr<mirror::String>>* dex_files, |
| 1334 | /*out*/std::string* error_msg) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1335 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | b8e7c37 | 2018-02-20 18:24:55 -0800 | [diff] [blame] | 1336 | StackHandleScope<1> hs(soa.Self()); |
| 1337 | Handle<mirror::ClassLoader> handle(hs.NewHandle(class_loader)); |
Nicolas Geoffray | f0d3002 | 2018-11-20 17:45:38 +0000 | [diff] [blame] | 1338 | // Get element names. Sets error to true on failure. |
| 1339 | auto add_element_names = [&](ObjPtr<mirror::Object> element, bool* error) |
| 1340 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1341 | if (element == nullptr) { |
| 1342 | *error_msg = "Null dex element"; |
| 1343 | *error = true; // Null element is a critical error. |
| 1344 | return false; // Had an error, stop the visit. |
| 1345 | } |
| 1346 | ObjPtr<mirror::String> name; |
| 1347 | if (!GetDexPathListElementName(element, &name)) { |
| 1348 | *error_msg = "Invalid dex path list element"; |
| 1349 | *error = true; // Invalid element, make it a critical error. |
| 1350 | return false; // Stop the visit. |
| 1351 | } |
| 1352 | if (name != nullptr) { |
| 1353 | dex_files->push_front(name); |
| 1354 | } |
| 1355 | return true; // Continue with the next Element. |
| 1356 | }; |
| 1357 | bool error = VisitClassLoaderDexElements(soa, |
| 1358 | handle, |
| 1359 | add_element_names, |
| 1360 | /*defaultReturn=*/ false); |
| 1361 | return !error; |
| 1362 | } |
| 1363 | |
| 1364 | static bool CompareClassLoaderTypes(ScopedObjectAccessUnchecked& soa, |
| 1365 | ObjPtr<mirror::ClassLoader> image_class_loader, |
| 1366 | ObjPtr<mirror::ClassLoader> class_loader, |
| 1367 | std::string* error_msg) |
| 1368 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1369 | if (ClassLinker::IsBootClassLoader(soa, class_loader)) { |
| 1370 | if (!ClassLinker::IsBootClassLoader(soa, image_class_loader)) { |
| 1371 | *error_msg = "Hierarchies don't match"; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1372 | return false; |
| 1373 | } |
Nicolas Geoffray | f0d3002 | 2018-11-20 17:45:38 +0000 | [diff] [blame] | 1374 | } else if (ClassLinker::IsBootClassLoader(soa, image_class_loader)) { |
| 1375 | *error_msg = "Hierarchies don't match"; |
| 1376 | return false; |
| 1377 | } else if (class_loader->GetClass() != image_class_loader->GetClass()) { |
| 1378 | *error_msg = StringPrintf("Class loader types don't match %s and %s", |
| 1379 | image_class_loader->PrettyTypeOf().c_str(), |
| 1380 | class_loader->PrettyTypeOf().c_str()); |
| 1381 | return false; |
| 1382 | } else if (soa.Decode<mirror::Class>(WellKnownClasses::dalvik_system_PathClassLoader) != |
| 1383 | class_loader->GetClass()) { |
| 1384 | *error_msg = StringPrintf("Unknown class loader type %s", |
| 1385 | class_loader->PrettyTypeOf().c_str()); |
| 1386 | // Unsupported class loader. |
| 1387 | return false; |
| 1388 | } |
| 1389 | return true; |
| 1390 | } |
| 1391 | |
| 1392 | static bool CompareDexFiles(const std::list<ObjPtr<mirror::String>>& image_dex_files, |
| 1393 | const std::list<ObjPtr<mirror::String>>& loader_dex_files, |
| 1394 | std::string* error_msg) |
| 1395 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1396 | bool equal = (image_dex_files.size() == loader_dex_files.size()) && |
| 1397 | std::equal(image_dex_files.begin(), |
| 1398 | image_dex_files.end(), |
| 1399 | loader_dex_files.begin(), |
| 1400 | [](ObjPtr<mirror::String> lhs, ObjPtr<mirror::String> rhs) |
| 1401 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1402 | return lhs->Equals(rhs); |
| 1403 | }); |
| 1404 | if (!equal) { |
| 1405 | VLOG(image) << "Image dex files " << image_dex_files.size(); |
| 1406 | for (ObjPtr<mirror::String> name : image_dex_files) { |
| 1407 | VLOG(image) << name->ToModifiedUtf8(); |
| 1408 | } |
| 1409 | VLOG(image) << "Loader dex files " << loader_dex_files.size(); |
| 1410 | for (ObjPtr<mirror::String> name : loader_dex_files) { |
| 1411 | VLOG(image) << name->ToModifiedUtf8(); |
| 1412 | } |
| 1413 | *error_msg = "Mismatch in dex files"; |
| 1414 | } |
| 1415 | return equal; |
| 1416 | } |
| 1417 | |
| 1418 | static bool CompareClassLoaders(ScopedObjectAccessUnchecked& soa, |
| 1419 | ObjPtr<mirror::ClassLoader> image_class_loader, |
| 1420 | ObjPtr<mirror::ClassLoader> class_loader, |
| 1421 | bool check_dex_file_names, |
| 1422 | std::string* error_msg) |
| 1423 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1424 | if (!CompareClassLoaderTypes(soa, image_class_loader, class_loader, error_msg)) { |
| 1425 | return false; |
| 1426 | } |
| 1427 | |
| 1428 | if (ClassLinker::IsBootClassLoader(soa, class_loader)) { |
| 1429 | // No need to check further. |
| 1430 | return true; |
| 1431 | } |
| 1432 | |
| 1433 | if (check_dex_file_names) { |
| 1434 | std::list<ObjPtr<mirror::String>> image_dex_files; |
| 1435 | if (!GetDexFileNames(soa, image_class_loader, &image_dex_files, error_msg)) { |
Andreas Gampe | b8e7c37 | 2018-02-20 18:24:55 -0800 | [diff] [blame] | 1436 | return false; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1437 | } |
Nicolas Geoffray | f0d3002 | 2018-11-20 17:45:38 +0000 | [diff] [blame] | 1438 | |
| 1439 | std::list<ObjPtr<mirror::String>> loader_dex_files; |
| 1440 | if (!GetDexFileNames(soa, class_loader, &loader_dex_files, error_msg)) { |
| 1441 | return false; |
| 1442 | } |
| 1443 | |
| 1444 | if (!CompareDexFiles(image_dex_files, loader_dex_files, error_msg)) { |
| 1445 | return false; |
| 1446 | } |
| 1447 | } |
| 1448 | |
| 1449 | ArtField* field = |
| 1450 | jni::DecodeArtField(WellKnownClasses::dalvik_system_BaseDexClassLoader_sharedLibraryLoaders); |
| 1451 | ObjPtr<mirror::Object> shared_libraries_image_loader = field->GetObject(image_class_loader.Ptr()); |
| 1452 | ObjPtr<mirror::Object> shared_libraries_loader = field->GetObject(class_loader.Ptr()); |
| 1453 | if (shared_libraries_image_loader == nullptr) { |
| 1454 | if (shared_libraries_loader != nullptr) { |
| 1455 | *error_msg = "Mismatch in shared libraries"; |
| 1456 | return false; |
| 1457 | } |
| 1458 | } else if (shared_libraries_loader == nullptr) { |
| 1459 | *error_msg = "Mismatch in shared libraries"; |
| 1460 | return false; |
| 1461 | } else { |
| 1462 | ObjPtr<mirror::ObjectArray<mirror::ClassLoader>> array1 = |
| 1463 | shared_libraries_image_loader->AsObjectArray<mirror::ClassLoader>(); |
| 1464 | ObjPtr<mirror::ObjectArray<mirror::ClassLoader>> array2 = |
| 1465 | shared_libraries_loader->AsObjectArray<mirror::ClassLoader>(); |
| 1466 | if (array1->GetLength() != array2->GetLength()) { |
| 1467 | *error_msg = "Mismatch in number of shared libraries"; |
| 1468 | return false; |
| 1469 | } |
| 1470 | |
| 1471 | for (int32_t i = 0; i < array1->GetLength(); ++i) { |
| 1472 | // Do a full comparison of the class loaders, including comparing their dex files. |
| 1473 | if (!CompareClassLoaders(soa, |
| 1474 | array1->Get(i), |
| 1475 | array2->Get(i), |
| 1476 | /*check_dex_file_names=*/ true, |
| 1477 | error_msg)) { |
| 1478 | return false; |
| 1479 | } |
| 1480 | } |
| 1481 | } |
| 1482 | |
| 1483 | // Do a full comparison of the class loaders, including comparing their dex files. |
| 1484 | if (!CompareClassLoaders(soa, |
| 1485 | image_class_loader->GetParent(), |
| 1486 | class_loader->GetParent(), |
| 1487 | /*check_dex_file_names=*/ true, |
| 1488 | error_msg)) { |
| 1489 | return false; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1490 | } |
| 1491 | return true; |
| 1492 | } |
| 1493 | |
Alexey Grebenkin | be4c2bd | 2018-02-01 19:09:59 +0300 | [diff] [blame] | 1494 | class CHAOnDeleteUpdateClassVisitor { |
| 1495 | public: |
| 1496 | explicit CHAOnDeleteUpdateClassVisitor(LinearAlloc* alloc) |
| 1497 | : allocator_(alloc), cha_(Runtime::Current()->GetClassLinker()->GetClassHierarchyAnalysis()), |
| 1498 | pointer_size_(Runtime::Current()->GetClassLinker()->GetImagePointerSize()), |
| 1499 | self_(Thread::Current()) {} |
| 1500 | |
| 1501 | bool operator()(ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1502 | // This class is going to be unloaded. Tell CHA about it. |
| 1503 | cha_->ResetSingleImplementationInHierarchy(klass, allocator_, pointer_size_); |
| 1504 | return true; |
| 1505 | } |
| 1506 | private: |
| 1507 | const LinearAlloc* allocator_; |
| 1508 | const ClassHierarchyAnalysis* cha_; |
| 1509 | const PointerSize pointer_size_; |
| 1510 | const Thread* self_; |
| 1511 | }; |
| 1512 | |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1513 | /* |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1514 | * A class used to ensure that all references to strings interned in an AppImage have been |
| 1515 | * properly recorded in the interned references list, and is only ever run in debug mode. |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1516 | */ |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1517 | class CountInternedStringReferencesVisitor { |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1518 | public: |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1519 | CountInternedStringReferencesVisitor(const gc::space::ImageSpace& space, |
| 1520 | const InternTable::UnorderedSet& image_interns) |
| 1521 | : space_(space), |
| 1522 | image_interns_(image_interns), |
| 1523 | count_(0u) {} |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1524 | |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1525 | void TestObject(ObjPtr<mirror::Object> referred_obj) const |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1526 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1527 | if (referred_obj != nullptr && |
| 1528 | space_.HasAddress(referred_obj.Ptr()) && |
| 1529 | referred_obj->IsString()) { |
| 1530 | ObjPtr<mirror::String> referred_str = referred_obj->AsString(); |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1531 | auto it = image_interns_.find(GcRoot<mirror::String>(referred_str)); |
| 1532 | if (it != image_interns_.end() && it->Read() == referred_str) { |
| 1533 | ++count_; |
Chris Wailes | fbeef46 | 2018-10-19 14:16:35 -0700 | [diff] [blame] | 1534 | } |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1535 | } |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1536 | } |
| 1537 | |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1538 | void VisitRootIfNonNull( |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1539 | mirror::CompressedReference<mirror::Object>* root) const |
| 1540 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1541 | if (!root->IsNull()) { |
| 1542 | VisitRoot(root); |
| 1543 | } |
| 1544 | } |
| 1545 | |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1546 | void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1547 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1548 | TestObject(root->AsMirrorPtr()); |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1549 | } |
| 1550 | |
| 1551 | // Visit Class Fields |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1552 | void operator()(ObjPtr<mirror::Object> obj, |
| 1553 | MemberOffset offset, |
| 1554 | bool is_static ATTRIBUTE_UNUSED) const |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1555 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1556 | // References within image or across images don't need a read barrier. |
| 1557 | ObjPtr<mirror::Object> referred_obj = |
| 1558 | obj->GetFieldObject<mirror::Object, kVerifyNone, kWithoutReadBarrier>(offset); |
| 1559 | TestObject(referred_obj); |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1560 | } |
| 1561 | |
| 1562 | void operator()(ObjPtr<mirror::Class> klass ATTRIBUTE_UNUSED, |
| 1563 | ObjPtr<mirror::Reference> ref) const |
| 1564 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_) { |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1565 | operator()(ref, mirror::Reference::ReferentOffset(), /*is_static=*/ false); |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1566 | } |
| 1567 | |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1568 | size_t GetCount() const { |
| 1569 | return count_; |
| 1570 | } |
| 1571 | |
| 1572 | private: |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1573 | const gc::space::ImageSpace& space_; |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1574 | const InternTable::UnorderedSet& image_interns_; |
| 1575 | mutable size_t count_; // Modified from the `const` callbacks. |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1576 | }; |
| 1577 | |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1578 | /* |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1579 | * This function counts references to strings interned in the AppImage. |
| 1580 | * This is used in debug build to check against the number of the recorded references. |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1581 | */ |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1582 | size_t CountInternedStringReferences(gc::space::ImageSpace& space, |
| 1583 | const InternTable::UnorderedSet& image_interns) |
| 1584 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1585 | const gc::accounting::ContinuousSpaceBitmap* bitmap = space.GetMarkBitmap(); |
| 1586 | const ImageHeader& image_header = space.GetImageHeader(); |
| 1587 | const uint8_t* target_base = space.GetMemMap()->Begin(); |
| 1588 | const ImageSection& objects_section = image_header.GetObjectsSection(); |
Chris Wailes | fbeef46 | 2018-10-19 14:16:35 -0700 | [diff] [blame] | 1589 | |
| 1590 | auto objects_begin = reinterpret_cast<uintptr_t>(target_base + objects_section.Offset()); |
| 1591 | auto objects_end = reinterpret_cast<uintptr_t>(target_base + objects_section.End()); |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1592 | |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1593 | CountInternedStringReferencesVisitor visitor(space, image_interns); |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1594 | bitmap->VisitMarkedRange(objects_begin, |
| 1595 | objects_end, |
| 1596 | [&space, &visitor](mirror::Object* obj) |
| 1597 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1598 | if (space.HasAddress(obj)) { |
| 1599 | if (obj->IsDexCache()) { |
Chris Wailes | fbeef46 | 2018-10-19 14:16:35 -0700 | [diff] [blame] | 1600 | obj->VisitReferences</* kVisitNativeRoots= */ true, |
| 1601 | kVerifyNone, |
| 1602 | kWithoutReadBarrier>(visitor, visitor); |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1603 | } else { |
| 1604 | // Don't visit native roots for non-dex-cache as they can't contain |
| 1605 | // native references to strings. This is verified during compilation |
| 1606 | // by ImageWriter::VerifyNativeGCRootInvariants. |
Chris Wailes | fbeef46 | 2018-10-19 14:16:35 -0700 | [diff] [blame] | 1607 | obj->VisitReferences</* kVisitNativeRoots= */ false, |
| 1608 | kVerifyNone, |
| 1609 | kWithoutReadBarrier>(visitor, visitor); |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1610 | } |
| 1611 | } |
| 1612 | }); |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1613 | return visitor.GetCount(); |
| 1614 | } |
| 1615 | |
| 1616 | template <typename Visitor> |
| 1617 | static void VisitInternedStringReferences( |
| 1618 | gc::space::ImageSpace* space, |
| 1619 | bool use_preresolved_strings, |
| 1620 | const Visitor& visitor) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1621 | const uint8_t* target_base = space->Begin(); |
| 1622 | const ImageSection& sro_section = |
| 1623 | space->GetImageHeader().GetImageStringReferenceOffsetsSection(); |
| 1624 | const size_t num_string_offsets = sro_section.Size() / sizeof(AppImageReferenceOffsetInfo); |
| 1625 | |
| 1626 | VLOG(image) |
| 1627 | << "ClassLinker:AppImage:InternStrings:imageStringReferenceOffsetCount = " |
| 1628 | << num_string_offsets; |
| 1629 | |
| 1630 | const auto* sro_base = |
| 1631 | reinterpret_cast<const AppImageReferenceOffsetInfo*>(target_base + sro_section.Offset()); |
| 1632 | |
| 1633 | for (size_t offset_index = 0; offset_index < num_string_offsets; ++offset_index) { |
| 1634 | uint32_t base_offset = sro_base[offset_index].first; |
| 1635 | |
| 1636 | if (HasDexCacheStringNativeRefTag(base_offset)) { |
| 1637 | base_offset = ClearDexCacheNativeRefTags(base_offset); |
| 1638 | DCHECK_ALIGNED(base_offset, 2); |
| 1639 | |
| 1640 | ObjPtr<mirror::DexCache> dex_cache = |
| 1641 | reinterpret_cast<mirror::DexCache*>(space->Begin() + base_offset); |
| 1642 | uint32_t string_slot_index = sro_base[offset_index].second; |
| 1643 | |
| 1644 | mirror::StringDexCachePair source = |
| 1645 | dex_cache->GetStrings()[string_slot_index].load(std::memory_order_relaxed); |
| 1646 | ObjPtr<mirror::String> referred_string = source.object.Read(); |
| 1647 | DCHECK(referred_string != nullptr); |
| 1648 | |
| 1649 | ObjPtr<mirror::String> visited = visitor(referred_string); |
| 1650 | if (visited != referred_string) { |
| 1651 | // Because we are not using a helper function we need to mark the GC card manually. |
| 1652 | WriteBarrier::ForEveryFieldWrite(dex_cache); |
| 1653 | dex_cache->GetStrings()[string_slot_index].store( |
| 1654 | mirror::StringDexCachePair(visited, source.index), std::memory_order_relaxed); |
| 1655 | } |
| 1656 | } else if (HasDexCachePreResolvedStringNativeRefTag(base_offset)) { |
| 1657 | if (use_preresolved_strings) { |
| 1658 | base_offset = ClearDexCacheNativeRefTags(base_offset); |
| 1659 | DCHECK_ALIGNED(base_offset, 2); |
| 1660 | |
| 1661 | ObjPtr<mirror::DexCache> dex_cache = |
| 1662 | reinterpret_cast<mirror::DexCache*>(space->Begin() + base_offset); |
| 1663 | uint32_t string_index = sro_base[offset_index].second; |
| 1664 | |
Mathieu Chartier | 77f84fc | 2019-06-14 12:49:54 -0700 | [diff] [blame] | 1665 | GcRoot<mirror::String>* preresolved_strings = |
| 1666 | dex_cache->GetPreResolvedStrings(); |
| 1667 | // Handle calls to ClearPreResolvedStrings that might occur concurrently by the profile |
| 1668 | // saver that runs shortly after startup. In case the strings are cleared, there is nothing |
| 1669 | // to fix up. |
| 1670 | if (preresolved_strings != nullptr) { |
| 1671 | ObjPtr<mirror::String> referred_string = |
| 1672 | preresolved_strings[string_index].Read(); |
| 1673 | if (referred_string != nullptr) { |
| 1674 | ObjPtr<mirror::String> visited = visitor(referred_string); |
| 1675 | if (visited != referred_string) { |
| 1676 | // Because we are not using a helper function we need to mark the GC card manually. |
| 1677 | WriteBarrier::ForEveryFieldWrite(dex_cache); |
| 1678 | preresolved_strings[string_index] = GcRoot<mirror::String>(visited); |
| 1679 | } |
| 1680 | } |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1681 | } |
| 1682 | } |
| 1683 | } else { |
| 1684 | uint32_t raw_member_offset = sro_base[offset_index].second; |
| 1685 | DCHECK_ALIGNED(base_offset, 2); |
| 1686 | DCHECK_ALIGNED(raw_member_offset, 2); |
| 1687 | |
| 1688 | ObjPtr<mirror::Object> obj_ptr = |
| 1689 | reinterpret_cast<mirror::Object*>(space->Begin() + base_offset); |
| 1690 | MemberOffset member_offset(raw_member_offset); |
| 1691 | ObjPtr<mirror::String> referred_string = |
| 1692 | obj_ptr->GetFieldObject<mirror::String, |
| 1693 | kVerifyNone, |
| 1694 | kWithoutReadBarrier, |
| 1695 | /* kIsVolatile= */ false>(member_offset); |
| 1696 | DCHECK(referred_string != nullptr); |
| 1697 | |
| 1698 | ObjPtr<mirror::String> visited = visitor(referred_string); |
| 1699 | if (visited != referred_string) { |
| 1700 | obj_ptr->SetFieldObject</* kTransactionActive= */ false, |
| 1701 | /* kCheckTransaction= */ false, |
| 1702 | kVerifyNone, |
| 1703 | /* kIsVolatile= */ false>(member_offset, visited); |
| 1704 | } |
| 1705 | } |
| 1706 | } |
| 1707 | } |
| 1708 | |
| 1709 | static void VerifyInternedStringReferences(gc::space::ImageSpace* space) |
| 1710 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1711 | InternTable::UnorderedSet image_interns; |
| 1712 | const ImageSection& section = space->GetImageHeader().GetInternedStringsSection(); |
| 1713 | if (section.Size() > 0) { |
| 1714 | size_t read_count; |
| 1715 | const uint8_t* data = space->Begin() + section.Offset(); |
| 1716 | InternTable::UnorderedSet image_set(data, /*make_copy_of_data=*/ false, &read_count); |
| 1717 | image_set.swap(image_interns); |
| 1718 | } |
| 1719 | size_t num_recorded_refs = 0u; |
| 1720 | VisitInternedStringReferences( |
| 1721 | space, |
| 1722 | /*use_preresolved_strings=*/ true, |
| 1723 | [&image_interns, &num_recorded_refs](ObjPtr<mirror::String> str) |
| 1724 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1725 | auto it = image_interns.find(GcRoot<mirror::String>(str)); |
| 1726 | CHECK(it != image_interns.end()); |
| 1727 | CHECK(it->Read() == str); |
| 1728 | ++num_recorded_refs; |
| 1729 | return str; |
| 1730 | }); |
| 1731 | size_t num_found_refs = CountInternedStringReferences(*space, image_interns); |
| 1732 | CHECK_EQ(num_recorded_refs, num_found_refs); |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1733 | } |
| 1734 | |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1735 | // new_class_set is the set of classes that were read from the class table section in the image. |
| 1736 | // If there was no class table section, it is null. |
| 1737 | // Note: using a class here to avoid having to make ClassLinker internals public. |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 1738 | class AppImageLoadingHelper { |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1739 | public: |
Vladimir Marko | 0f3c700 | 2017-09-07 14:15:56 +0100 | [diff] [blame] | 1740 | static void Update( |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1741 | ClassLinker* class_linker, |
| 1742 | gc::space::ImageSpace* space, |
| 1743 | Handle<mirror::ClassLoader> class_loader, |
| 1744 | Handle<mirror::ObjectArray<mirror::DexCache>> dex_caches, |
Vladimir Marko | 0f3c700 | 2017-09-07 14:15:56 +0100 | [diff] [blame] | 1745 | ClassTable::ClassSet* new_class_set) |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1746 | REQUIRES(!Locks::dex_lock_) |
| 1747 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 1748 | |
Chris Wailes | fbeef46 | 2018-10-19 14:16:35 -0700 | [diff] [blame] | 1749 | static void HandleAppImageStrings(gc::space::ImageSpace* space) |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 1750 | REQUIRES_SHARED(Locks::mutator_lock_); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1751 | }; |
| 1752 | |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 1753 | void AppImageLoadingHelper::Update( |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1754 | ClassLinker* class_linker, |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1755 | gc::space::ImageSpace* space, |
| 1756 | Handle<mirror::ClassLoader> class_loader, |
| 1757 | Handle<mirror::ObjectArray<mirror::DexCache>> dex_caches, |
Vladimir Marko | 0f3c700 | 2017-09-07 14:15:56 +0100 | [diff] [blame] | 1758 | ClassTable::ClassSet* new_class_set) |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1759 | REQUIRES(!Locks::dex_lock_) |
| 1760 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Chris Wailes | 2386636 | 2018-08-22 16:16:58 -0700 | [diff] [blame] | 1761 | ScopedTrace app_image_timing("AppImage:Updating"); |
| 1762 | |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1763 | if (kIsDebugBuild && ClassLinker::kAppImageMayContainStrings) { |
| 1764 | // In debug build, verify the string references before applying |
| 1765 | // the Runtime::LoadAppImageStartupCache() option. |
| 1766 | VerifyInternedStringReferences(space); |
| 1767 | } |
| 1768 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1769 | Thread* const self = Thread::Current(); |
Mathieu Chartier | a88abfa | 2019-02-04 11:08:29 -0800 | [diff] [blame] | 1770 | Runtime* const runtime = Runtime::Current(); |
| 1771 | gc::Heap* const heap = runtime->GetHeap(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1772 | const ImageHeader& header = space->GetImageHeader(); |
Mathieu Chartier | a88abfa | 2019-02-04 11:08:29 -0800 | [diff] [blame] | 1773 | bool load_app_image_startup_cache = runtime->LoadAppImageStartupCache(); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1774 | { |
Vladimir Marko | 0f3c700 | 2017-09-07 14:15:56 +0100 | [diff] [blame] | 1775 | // Register dex caches with the class loader. |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1776 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1777 | const size_t num_dex_caches = dex_caches->GetLength(); |
| 1778 | for (size_t i = 0; i < num_dex_caches; i++) { |
Vladimir Marko | 1bc4b17 | 2016-10-24 16:53:39 +0000 | [diff] [blame] | 1779 | ObjPtr<mirror::DexCache> dex_cache = dex_caches->Get(i); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1780 | const DexFile* const dex_file = dex_cache->GetDexFile(); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1781 | { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 1782 | WriterMutexLock mu2(self, *Locks::dex_lock_); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1783 | CHECK(!class_linker->FindDexCacheDataLocked(*dex_file).IsValid()); |
| 1784 | class_linker->RegisterDexFileLocked(*dex_file, dex_cache, class_loader.Get()); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1785 | } |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1786 | |
Mathieu Chartier | a88abfa | 2019-02-04 11:08:29 -0800 | [diff] [blame] | 1787 | if (!load_app_image_startup_cache) { |
| 1788 | dex_cache->ClearPreResolvedStrings(); |
| 1789 | } |
| 1790 | |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1791 | if (kIsDebugBuild) { |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 1792 | CHECK(new_class_set != nullptr); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 1793 | mirror::TypeDexCacheType* const types = dex_cache->GetResolvedTypes(); |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 1794 | const size_t num_types = dex_cache->NumResolvedTypes(); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 1795 | for (size_t j = 0; j != num_types; ++j) { |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1796 | // 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] | 1797 | ObjPtr<mirror::Class> klass = types[j].load(std::memory_order_relaxed).object.Read(); |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1798 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1799 | if (space->HasAddress(klass.Ptr())) { |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 1800 | DCHECK(!klass->IsErroneous()) << klass->GetStatus(); |
Vladimir Marko | 54159c6 | 2018-06-20 14:30:08 +0100 | [diff] [blame] | 1801 | auto it = new_class_set->find(ClassTable::TableSlot(klass)); |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 1802 | DCHECK(it != new_class_set->end()); |
| 1803 | DCHECK_EQ(it->Read(), klass); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1804 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1805 | |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 1806 | if (super_class != nullptr && !heap->ObjectIsInBootImageSpace(super_class)) { |
Vladimir Marko | 54159c6 | 2018-06-20 14:30:08 +0100 | [diff] [blame] | 1807 | auto it2 = new_class_set->find(ClassTable::TableSlot(super_class)); |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 1808 | DCHECK(it2 != new_class_set->end()); |
| 1809 | DCHECK_EQ(it2->Read(), super_class); |
| 1810 | } |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1811 | |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 1812 | for (ArtMethod& m : klass->GetDirectMethods(kRuntimePointerSize)) { |
| 1813 | const void* code = m.GetEntryPointFromQuickCompiledCode(); |
Alex Light | fc49fec | 2018-01-16 22:28:36 +0000 | [diff] [blame] | 1814 | const void* oat_code = m.IsInvokable() ? class_linker->GetQuickOatCodeFor(&m) : code; |
| 1815 | if (!class_linker->IsQuickResolutionStub(code) && |
| 1816 | !class_linker->IsQuickGenericJniStub(code) && |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1817 | !class_linker->IsQuickToInterpreterBridge(code) && |
Alex Light | fc49fec | 2018-01-16 22:28:36 +0000 | [diff] [blame] | 1818 | !m.IsNative()) { |
| 1819 | DCHECK_EQ(code, oat_code) << m.PrettyMethod(); |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 1820 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1821 | } |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1822 | |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 1823 | for (ArtMethod& m : klass->GetVirtualMethods(kRuntimePointerSize)) { |
| 1824 | const void* code = m.GetEntryPointFromQuickCompiledCode(); |
Alex Light | fc49fec | 2018-01-16 22:28:36 +0000 | [diff] [blame] | 1825 | const void* oat_code = m.IsInvokable() ? class_linker->GetQuickOatCodeFor(&m) : code; |
| 1826 | if (!class_linker->IsQuickResolutionStub(code) && |
| 1827 | !class_linker->IsQuickGenericJniStub(code) && |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1828 | !class_linker->IsQuickToInterpreterBridge(code) && |
Alex Light | fc49fec | 2018-01-16 22:28:36 +0000 | [diff] [blame] | 1829 | !m.IsNative()) { |
| 1830 | DCHECK_EQ(code, oat_code) << m.PrettyMethod(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1831 | } |
| 1832 | } |
| 1833 | } |
| 1834 | } |
| 1835 | } |
| 1836 | } |
Mathieu Chartier | a0b9521 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 1837 | } |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1838 | |
Mathieu Chartier | 0933cc5 | 2018-03-23 14:25:08 -0700 | [diff] [blame] | 1839 | if (ClassLinker::kAppImageMayContainStrings) { |
Chris Wailes | fbeef46 | 2018-10-19 14:16:35 -0700 | [diff] [blame] | 1840 | HandleAppImageStrings(space); |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1841 | } |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1842 | |
Mathieu Chartier | a0b9521 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 1843 | if (kVerifyArtMethodDeclaringClasses) { |
Chris Wailes | 2386636 | 2018-08-22 16:16:58 -0700 | [diff] [blame] | 1844 | ScopedTrace timing("AppImage:VerifyDeclaringClasses"); |
Mathieu Chartier | a0b9521 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 1845 | ReaderMutexLock rmu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 9d5956a | 2019-03-22 11:29:08 -0700 | [diff] [blame] | 1846 | gc::accounting::HeapBitmap* live_bitmap = heap->GetLiveBitmap(); |
| 1847 | header.VisitPackedArtMethods([&](ArtMethod& method) |
| 1848 | REQUIRES_SHARED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) { |
| 1849 | ObjPtr<mirror::Class> klass = method.GetDeclaringClassUnchecked(); |
| 1850 | if (klass != nullptr) { |
| 1851 | CHECK(live_bitmap->Test(klass.Ptr())) << "Image method has unmarked declaring class"; |
| 1852 | } |
| 1853 | }, space->Begin(), kRuntimePointerSize); |
Mathieu Chartier | 03c1dd9 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 1854 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1855 | } |
| 1856 | |
Chris Wailes | fbeef46 | 2018-10-19 14:16:35 -0700 | [diff] [blame] | 1857 | void AppImageLoadingHelper::HandleAppImageStrings(gc::space::ImageSpace* space) { |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 1858 | // Iterate over the string reference offsets stored in the image and intern |
| 1859 | // the strings they point to. |
| 1860 | ScopedTrace timing("AppImage:InternString"); |
| 1861 | |
Mathieu Chartier | a88abfa | 2019-02-04 11:08:29 -0800 | [diff] [blame] | 1862 | Runtime* const runtime = Runtime::Current(); |
| 1863 | InternTable* const intern_table = runtime->GetInternTable(); |
| 1864 | |
| 1865 | const bool load_startup_cache = runtime->LoadAppImageStartupCache(); |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 1866 | |
| 1867 | // Add the intern table, removing any conflicts. For conflicts, store the new address in a map |
| 1868 | // for faster lookup. |
| 1869 | // TODO: Optimize with a bitmap or bloom filter |
| 1870 | SafeMap<mirror::String*, mirror::String*> intern_remap; |
Mathieu Chartier | a88abfa | 2019-02-04 11:08:29 -0800 | [diff] [blame] | 1871 | auto func = [&](InternTable::UnorderedSet& interns) |
Mathieu Chartier | 41c0808 | 2018-10-31 11:50:26 -0700 | [diff] [blame] | 1872 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 1873 | REQUIRES(Locks::intern_table_lock_) { |
Mathieu Chartier | 8fc7558 | 2018-11-01 14:21:33 -0700 | [diff] [blame] | 1874 | const size_t non_boot_image_strings = intern_table->CountInterns( |
| 1875 | /*visit_boot_images=*/false, |
| 1876 | /*visit_non_boot_images=*/true); |
Chris Wailes | fbeef46 | 2018-10-19 14:16:35 -0700 | [diff] [blame] | 1877 | VLOG(image) << "AppImage:stringsInInternTableSize = " << interns.size(); |
Mathieu Chartier | 8fc7558 | 2018-11-01 14:21:33 -0700 | [diff] [blame] | 1878 | VLOG(image) << "AppImage:nonBootImageInternStrings = " << non_boot_image_strings; |
| 1879 | // Visit the smaller of the two sets to compute the intersection. |
| 1880 | if (interns.size() < non_boot_image_strings) { |
| 1881 | for (auto it = interns.begin(); it != interns.end(); ) { |
| 1882 | ObjPtr<mirror::String> string = it->Read(); |
| 1883 | ObjPtr<mirror::String> existing = intern_table->LookupWeakLocked(string); |
| 1884 | if (existing == nullptr) { |
| 1885 | existing = intern_table->LookupStrongLocked(string); |
| 1886 | } |
| 1887 | if (existing != nullptr) { |
| 1888 | intern_remap.Put(string.Ptr(), existing.Ptr()); |
| 1889 | it = interns.erase(it); |
| 1890 | } else { |
| 1891 | ++it; |
| 1892 | } |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 1893 | } |
Mathieu Chartier | 8fc7558 | 2018-11-01 14:21:33 -0700 | [diff] [blame] | 1894 | } else { |
| 1895 | intern_table->VisitInterns([&](const GcRoot<mirror::String>& root) |
| 1896 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 1897 | REQUIRES(Locks::intern_table_lock_) { |
| 1898 | auto it = interns.find(root); |
| 1899 | if (it != interns.end()) { |
| 1900 | ObjPtr<mirror::String> existing = root.Read(); |
| 1901 | intern_remap.Put(it->Read(), existing.Ptr()); |
| 1902 | it = interns.erase(it); |
| 1903 | } |
| 1904 | }, /*visit_boot_images=*/false, /*visit_non_boot_images=*/true); |
| 1905 | } |
| 1906 | // Sanity check to ensure correctness. |
| 1907 | if (kIsDebugBuild) { |
| 1908 | for (GcRoot<mirror::String>& root : interns) { |
| 1909 | ObjPtr<mirror::String> string = root.Read(); |
| 1910 | CHECK(intern_table->LookupWeakLocked(string) == nullptr) << string->ToModifiedUtf8(); |
| 1911 | CHECK(intern_table->LookupStrongLocked(string) == nullptr) << string->ToModifiedUtf8(); |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 1912 | } |
| 1913 | } |
Mathieu Chartier | a88abfa | 2019-02-04 11:08:29 -0800 | [diff] [blame] | 1914 | }; |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1915 | intern_table->AddImageStringsToTable(space, func); |
| 1916 | if (!intern_remap.empty()) { |
Mathieu Chartier | a88abfa | 2019-02-04 11:08:29 -0800 | [diff] [blame] | 1917 | VLOG(image) << "AppImage:conflictingInternStrings = " << intern_remap.size(); |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1918 | VisitInternedStringReferences( |
| 1919 | space, |
| 1920 | load_startup_cache, |
| 1921 | [&intern_remap](ObjPtr<mirror::String> str) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1922 | auto it = intern_remap.find(str.Ptr()); |
| 1923 | if (it != intern_remap.end()) { |
| 1924 | return ObjPtr<mirror::String>(it->second); |
| 1925 | } |
| 1926 | return str; |
| 1927 | }); |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 1928 | } |
| 1929 | } |
| 1930 | |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1931 | static std::unique_ptr<const DexFile> OpenOatDexFile(const OatFile* oat_file, |
| 1932 | const char* location, |
| 1933 | std::string* error_msg) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1934 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1935 | DCHECK(error_msg != nullptr); |
| 1936 | std::unique_ptr<const DexFile> dex_file; |
Andreas Gampe | b40d361 | 2018-06-26 15:49:42 -0700 | [diff] [blame] | 1937 | const OatDexFile* oat_dex_file = oat_file->GetOatDexFile(location, nullptr, error_msg); |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1938 | if (oat_dex_file == nullptr) { |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1939 | return std::unique_ptr<const DexFile>(); |
| 1940 | } |
| 1941 | std::string inner_error_msg; |
| 1942 | dex_file = oat_dex_file->OpenDexFile(&inner_error_msg); |
| 1943 | if (dex_file == nullptr) { |
| 1944 | *error_msg = StringPrintf("Failed to open dex file %s from within oat file %s error '%s'", |
| 1945 | location, |
| 1946 | oat_file->GetLocation().c_str(), |
| 1947 | inner_error_msg.c_str()); |
| 1948 | return std::unique_ptr<const DexFile>(); |
| 1949 | } |
| 1950 | |
| 1951 | if (dex_file->GetLocationChecksum() != oat_dex_file->GetDexFileLocationChecksum()) { |
| 1952 | *error_msg = StringPrintf("Checksums do not match for %s: %x vs %x", |
| 1953 | location, |
| 1954 | dex_file->GetLocationChecksum(), |
| 1955 | oat_dex_file->GetDexFileLocationChecksum()); |
| 1956 | return std::unique_ptr<const DexFile>(); |
| 1957 | } |
| 1958 | return dex_file; |
| 1959 | } |
| 1960 | |
| 1961 | bool ClassLinker::OpenImageDexFiles(gc::space::ImageSpace* space, |
| 1962 | std::vector<std::unique_ptr<const DexFile>>* out_dex_files, |
| 1963 | std::string* error_msg) { |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 1964 | ScopedAssertNoThreadSuspension nts(__FUNCTION__); |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1965 | const ImageHeader& header = space->GetImageHeader(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1966 | ObjPtr<mirror::Object> dex_caches_object = header.GetImageRoot(ImageHeader::kDexCaches); |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1967 | DCHECK(dex_caches_object != nullptr); |
Vladimir Marko | 4617d58 | 2019-03-28 13:48:31 +0000 | [diff] [blame] | 1968 | ObjPtr<mirror::ObjectArray<mirror::DexCache>> dex_caches = |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1969 | dex_caches_object->AsObjectArray<mirror::DexCache>(); |
| 1970 | const OatFile* oat_file = space->GetOatFile(); |
Vladimir Marko | 4617d58 | 2019-03-28 13:48:31 +0000 | [diff] [blame] | 1971 | for (int32_t i = 0, length = dex_caches->GetLength(); i != length; ++i) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1972 | ObjPtr<mirror::DexCache> dex_cache = dex_caches->Get(i); |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1973 | std::string dex_file_location(dex_cache->GetLocation()->ToModifiedUtf8()); |
| 1974 | std::unique_ptr<const DexFile> dex_file = OpenOatDexFile(oat_file, |
| 1975 | dex_file_location.c_str(), |
| 1976 | error_msg); |
| 1977 | if (dex_file == nullptr) { |
| 1978 | return false; |
| 1979 | } |
| 1980 | dex_cache->SetDexFile(dex_file.get()); |
| 1981 | out_dex_files->push_back(std::move(dex_file)); |
| 1982 | } |
| 1983 | return true; |
| 1984 | } |
| 1985 | |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1986 | // Helper class for ArtMethod checks when adding an image. Keeps all required functionality |
| 1987 | // together and caches some intermediate results. |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 1988 | class ImageSanityChecks final { |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1989 | public: |
| 1990 | static void CheckObjects(gc::Heap* heap, ClassLinker* class_linker) |
| 1991 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1992 | ImageSanityChecks isc(heap, class_linker); |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 1993 | auto visitor = [&](mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1994 | DCHECK(obj != nullptr); |
| 1995 | CHECK(obj->GetClass() != nullptr) << "Null class in object " << obj; |
| 1996 | CHECK(obj->GetClass()->GetClass() != nullptr) << "Null class class " << obj; |
| 1997 | if (obj->IsClass()) { |
| 1998 | auto klass = obj->AsClass(); |
| 1999 | for (ArtField& field : klass->GetIFields()) { |
| 2000 | CHECK_EQ(field.GetDeclaringClass(), klass); |
| 2001 | } |
| 2002 | for (ArtField& field : klass->GetSFields()) { |
| 2003 | CHECK_EQ(field.GetDeclaringClass(), klass); |
| 2004 | } |
Vladimir Marko | c524e9e | 2019-03-26 10:54:50 +0000 | [diff] [blame] | 2005 | const PointerSize pointer_size = isc.pointer_size_; |
| 2006 | for (ArtMethod& m : klass->GetMethods(pointer_size)) { |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 2007 | isc.SanityCheckArtMethod(&m, klass); |
| 2008 | } |
Vladimir Marko | c524e9e | 2019-03-26 10:54:50 +0000 | [diff] [blame] | 2009 | ObjPtr<mirror::PointerArray> vtable = klass->GetVTable(); |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 2010 | if (vtable != nullptr) { |
| 2011 | isc.SanityCheckArtMethodPointerArray(vtable, nullptr); |
| 2012 | } |
| 2013 | if (klass->ShouldHaveImt()) { |
| 2014 | ImTable* imt = klass->GetImt(pointer_size); |
| 2015 | for (size_t i = 0; i < ImTable::kSize; ++i) { |
| 2016 | isc.SanityCheckArtMethod(imt->Get(i, pointer_size), nullptr); |
| 2017 | } |
| 2018 | } |
| 2019 | if (klass->ShouldHaveEmbeddedVTable()) { |
| 2020 | for (int32_t i = 0; i < klass->GetEmbeddedVTableLength(); ++i) { |
| 2021 | isc.SanityCheckArtMethod(klass->GetEmbeddedVTableEntry(i, pointer_size), nullptr); |
| 2022 | } |
| 2023 | } |
Vladimir Marko | c524e9e | 2019-03-26 10:54:50 +0000 | [diff] [blame] | 2024 | ObjPtr<mirror::IfTable> iftable = klass->GetIfTable(); |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 2025 | for (int32_t i = 0; i < klass->GetIfTableCount(); ++i) { |
| 2026 | if (iftable->GetMethodArrayCount(i) > 0) { |
| 2027 | isc.SanityCheckArtMethodPointerArray(iftable->GetMethodArray(i), nullptr); |
| 2028 | } |
| 2029 | } |
| 2030 | } |
| 2031 | }; |
| 2032 | heap->VisitObjects(visitor); |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 2033 | } |
| 2034 | |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 2035 | static void CheckArtMethodDexCacheArray(gc::Heap* heap, |
| 2036 | ClassLinker* class_linker, |
| 2037 | mirror::MethodDexCacheType* arr, |
| 2038 | size_t size) |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 2039 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 2040 | ImageSanityChecks isc(heap, class_linker); |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 2041 | isc.SanityCheckArtMethodDexCacheArray(arr, size); |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 2042 | } |
| 2043 | |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 2044 | private: |
| 2045 | ImageSanityChecks(gc::Heap* heap, ClassLinker* class_linker) |
| 2046 | : spaces_(heap->GetBootImageSpaces()), |
| 2047 | pointer_size_(class_linker->GetImagePointerSize()) { |
| 2048 | space_begin_.reserve(spaces_.size()); |
| 2049 | method_sections_.reserve(spaces_.size()); |
| 2050 | runtime_method_sections_.reserve(spaces_.size()); |
| 2051 | for (gc::space::ImageSpace* space : spaces_) { |
| 2052 | space_begin_.push_back(space->Begin()); |
| 2053 | auto& header = space->GetImageHeader(); |
| 2054 | method_sections_.push_back(&header.GetMethodsSection()); |
| 2055 | runtime_method_sections_.push_back(&header.GetRuntimeMethodsSection()); |
| 2056 | } |
| 2057 | } |
| 2058 | |
| 2059 | void SanityCheckArtMethod(ArtMethod* m, ObjPtr<mirror::Class> expected_class) |
| 2060 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 2061 | if (m->IsRuntimeMethod()) { |
| 2062 | ObjPtr<mirror::Class> declaring_class = m->GetDeclaringClassUnchecked(); |
| 2063 | CHECK(declaring_class == nullptr) << declaring_class << " " << m->PrettyMethod(); |
| 2064 | } else if (m->IsCopied()) { |
| 2065 | CHECK(m->GetDeclaringClass() != nullptr) << m->PrettyMethod(); |
| 2066 | } else if (expected_class != nullptr) { |
| 2067 | CHECK_EQ(m->GetDeclaringClassUnchecked(), expected_class) << m->PrettyMethod(); |
| 2068 | } |
| 2069 | if (!spaces_.empty()) { |
| 2070 | bool contains = false; |
| 2071 | for (size_t i = 0; !contains && i != space_begin_.size(); ++i) { |
| 2072 | const size_t offset = reinterpret_cast<uint8_t*>(m) - space_begin_[i]; |
| 2073 | contains = method_sections_[i]->Contains(offset) || |
| 2074 | runtime_method_sections_[i]->Contains(offset); |
| 2075 | } |
| 2076 | CHECK(contains) << m << " not found"; |
| 2077 | } |
| 2078 | } |
| 2079 | |
| 2080 | void SanityCheckArtMethodPointerArray(ObjPtr<mirror::PointerArray> arr, |
| 2081 | ObjPtr<mirror::Class> expected_class) |
| 2082 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 2083 | CHECK(arr != nullptr); |
| 2084 | for (int32_t j = 0; j < arr->GetLength(); ++j) { |
| 2085 | auto* method = arr->GetElementPtrSize<ArtMethod*>(j, pointer_size_); |
| 2086 | // expected_class == null means we are a dex cache. |
| 2087 | if (expected_class != nullptr) { |
| 2088 | CHECK(method != nullptr); |
| 2089 | } |
| 2090 | if (method != nullptr) { |
| 2091 | SanityCheckArtMethod(method, expected_class); |
| 2092 | } |
| 2093 | } |
| 2094 | } |
| 2095 | |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 2096 | void SanityCheckArtMethodDexCacheArray(mirror::MethodDexCacheType* arr, size_t size) |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 2097 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 2098 | CHECK_EQ(arr != nullptr, size != 0u); |
| 2099 | if (arr != nullptr) { |
| 2100 | bool contains = false; |
| 2101 | for (auto space : spaces_) { |
| 2102 | auto offset = reinterpret_cast<uint8_t*>(arr) - space->Begin(); |
Vladimir Marko | cd87c3e | 2017-09-05 13:11:57 +0100 | [diff] [blame] | 2103 | if (space->GetImageHeader().GetDexCacheArraysSection().Contains(offset)) { |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 2104 | contains = true; |
| 2105 | break; |
| 2106 | } |
| 2107 | } |
| 2108 | CHECK(contains); |
| 2109 | } |
| 2110 | for (size_t j = 0; j < size; ++j) { |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 2111 | auto pair = mirror::DexCache::GetNativePairPtrSize(arr, j, pointer_size_); |
| 2112 | ArtMethod* method = pair.object; |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 2113 | // expected_class == null means we are a dex cache. |
| 2114 | if (method != nullptr) { |
| 2115 | SanityCheckArtMethod(method, nullptr); |
| 2116 | } |
| 2117 | } |
| 2118 | } |
| 2119 | |
| 2120 | const std::vector<gc::space::ImageSpace*>& spaces_; |
| 2121 | const PointerSize pointer_size_; |
| 2122 | |
| 2123 | // Cached sections from the spaces. |
| 2124 | std::vector<const uint8_t*> space_begin_; |
| 2125 | std::vector<const ImageSection*> method_sections_; |
| 2126 | std::vector<const ImageSection*> runtime_method_sections_; |
| 2127 | }; |
| 2128 | |
Andreas Gampe | be7af22 | 2017-07-25 09:57:28 -0700 | [diff] [blame] | 2129 | static void VerifyAppImage(const ImageHeader& header, |
| 2130 | const Handle<mirror::ClassLoader>& class_loader, |
| 2131 | const Handle<mirror::ObjectArray<mirror::DexCache> >& dex_caches, |
| 2132 | ClassTable* class_table, gc::space::ImageSpace* space) |
| 2133 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 9d5956a | 2019-03-22 11:29:08 -0700 | [diff] [blame] | 2134 | header.VisitPackedArtMethods([&](ArtMethod& method) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 2135 | ObjPtr<mirror::Class> klass = method.GetDeclaringClass(); |
| 2136 | if (klass != nullptr && !Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(klass)) { |
| 2137 | CHECK_EQ(class_table->LookupByDescriptor(klass), klass) |
| 2138 | << mirror::Class::PrettyClass(klass); |
| 2139 | } |
| 2140 | }, space->Begin(), kRuntimePointerSize); |
Andreas Gampe | be7af22 | 2017-07-25 09:57:28 -0700 | [diff] [blame] | 2141 | { |
| 2142 | // Verify that all direct interfaces of classes in the class table are also resolved. |
| 2143 | std::vector<ObjPtr<mirror::Class>> classes; |
| 2144 | auto verify_direct_interfaces_in_table = [&](ObjPtr<mirror::Class> klass) |
| 2145 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 2146 | if (!klass->IsPrimitive() && klass->GetClassLoader() == class_loader.Get()) { |
| 2147 | classes.push_back(klass); |
| 2148 | } |
| 2149 | return true; |
| 2150 | }; |
| 2151 | class_table->Visit(verify_direct_interfaces_in_table); |
| 2152 | Thread* self = Thread::Current(); |
| 2153 | for (ObjPtr<mirror::Class> klass : classes) { |
| 2154 | for (uint32_t i = 0, num = klass->NumDirectInterfaces(); i != num; ++i) { |
| 2155 | CHECK(klass->GetDirectInterface(self, klass, i) != nullptr) |
| 2156 | << klass->PrettyDescriptor() << " iface #" << i; |
| 2157 | } |
| 2158 | } |
| 2159 | } |
| 2160 | // Check that all non-primitive classes in dex caches are also in the class table. |
| 2161 | for (int32_t i = 0; i < dex_caches->GetLength(); i++) { |
| 2162 | ObjPtr<mirror::DexCache> dex_cache = dex_caches->Get(i); |
| 2163 | mirror::TypeDexCacheType* const types = dex_cache->GetResolvedTypes(); |
| 2164 | for (int32_t j = 0, num_types = dex_cache->NumResolvedTypes(); j < num_types; j++) { |
| 2165 | ObjPtr<mirror::Class> klass = types[j].load(std::memory_order_relaxed).object.Read(); |
| 2166 | if (klass != nullptr && !klass->IsPrimitive()) { |
| 2167 | CHECK(class_table->Contains(klass)) |
| 2168 | << klass->PrettyDescriptor() << " " << dex_cache->GetDexFile()->GetLocation(); |
| 2169 | } |
| 2170 | } |
| 2171 | } |
| 2172 | } |
| 2173 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2174 | bool ClassLinker::AddImageSpace( |
| 2175 | gc::space::ImageSpace* space, |
| 2176 | Handle<mirror::ClassLoader> class_loader, |
| 2177 | jobjectArray dex_elements, |
| 2178 | const char* dex_location, |
| 2179 | std::vector<std::unique_ptr<const DexFile>>* out_dex_files, |
| 2180 | std::string* error_msg) { |
| 2181 | DCHECK(out_dex_files != nullptr); |
| 2182 | DCHECK(error_msg != nullptr); |
| 2183 | const uint64_t start_time = NanoTime(); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 2184 | const bool app_image = class_loader != nullptr; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2185 | const ImageHeader& header = space->GetImageHeader(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2186 | ObjPtr<mirror::Object> dex_caches_object = header.GetImageRoot(ImageHeader::kDexCaches); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2187 | DCHECK(dex_caches_object != nullptr); |
| 2188 | Runtime* const runtime = Runtime::Current(); |
| 2189 | gc::Heap* const heap = runtime->GetHeap(); |
| 2190 | Thread* const self = Thread::Current(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2191 | // Check that the image is what we are expecting. |
| 2192 | if (image_pointer_size_ != space->GetImageHeader().GetPointerSize()) { |
| 2193 | *error_msg = StringPrintf("Application image pointer size does not match runtime: %zu vs %zu", |
| 2194 | static_cast<size_t>(space->GetImageHeader().GetPointerSize()), |
| 2195 | image_pointer_size_); |
| 2196 | return false; |
| 2197 | } |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 2198 | size_t expected_image_roots = ImageHeader::NumberOfImageRoots(app_image); |
| 2199 | if (static_cast<size_t>(header.GetImageRoots()->GetLength()) != expected_image_roots) { |
| 2200 | *error_msg = StringPrintf("Expected %zu image roots but got %d", |
| 2201 | expected_image_roots, |
| 2202 | header.GetImageRoots()->GetLength()); |
| 2203 | return false; |
| 2204 | } |
| 2205 | StackHandleScope<3> hs(self); |
| 2206 | Handle<mirror::ObjectArray<mirror::DexCache>> dex_caches( |
| 2207 | hs.NewHandle(dex_caches_object->AsObjectArray<mirror::DexCache>())); |
| 2208 | Handle<mirror::ObjectArray<mirror::Class>> class_roots(hs.NewHandle( |
| 2209 | header.GetImageRoot(ImageHeader::kClassRoots)->AsObjectArray<mirror::Class>())); |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 2210 | MutableHandle<mirror::ClassLoader> image_class_loader(hs.NewHandle( |
Vladimir Marko | f75613c | 2018-06-05 12:51:04 +0100 | [diff] [blame] | 2211 | app_image ? header.GetImageRoot(ImageHeader::kAppImageClassLoader)->AsClassLoader() |
| 2212 | : nullptr)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 2213 | DCHECK(class_roots != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 2214 | if (class_roots->GetLength() != static_cast<int32_t>(ClassRoot::kMax)) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2215 | *error_msg = StringPrintf("Expected %d class roots but got %d", |
| 2216 | class_roots->GetLength(), |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 2217 | static_cast<int32_t>(ClassRoot::kMax)); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2218 | return false; |
| 2219 | } |
| 2220 | // 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] | 2221 | ObjPtr<mirror::ObjectArray<mirror::Class>> existing_class_roots = GetClassRoots(); |
| 2222 | for (size_t i = 0; i < static_cast<size_t>(ClassRoot::kMax); i++) { |
| 2223 | 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] | 2224 | *error_msg = "App image class roots must have pointer equality with runtime ones."; |
| 2225 | return false; |
| 2226 | } |
| 2227 | } |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 2228 | const OatFile* oat_file = space->GetOatFile(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2229 | if (oat_file->GetOatHeader().GetDexFileCount() != |
| 2230 | static_cast<uint32_t>(dex_caches->GetLength())) { |
| 2231 | *error_msg = "Dex cache count and dex file count mismatch while trying to initialize from " |
| 2232 | "image"; |
| 2233 | return false; |
| 2234 | } |
| 2235 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2236 | for (int32_t i = 0; i < dex_caches->GetLength(); i++) { |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 2237 | ObjPtr<mirror::DexCache> dex_cache = dex_caches->Get(i); |
David Brazdil | 3e8aae0 | 2019-03-26 18:48:02 +0000 | [diff] [blame] | 2238 | std::string dex_file_location = dex_cache->GetLocation()->ToModifiedUtf8(); |
Andreas Gampe | d5d807b | 2019-04-18 14:46:51 -0700 | [diff] [blame] | 2239 | if (class_loader == nullptr) { |
| 2240 | // For app images, we'll see the relative location. b/130666977. |
| 2241 | DCHECK_EQ(dex_location, DexFileLoader::GetBaseLocation(dex_file_location)); |
| 2242 | } |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 2243 | std::unique_ptr<const DexFile> dex_file = OpenOatDexFile(oat_file, |
| 2244 | dex_file_location.c_str(), |
| 2245 | error_msg); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2246 | if (dex_file == nullptr) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2247 | return false; |
| 2248 | } |
| 2249 | |
| 2250 | if (app_image) { |
| 2251 | // The current dex file field is bogus, overwrite it so that we can get the dex file in the |
| 2252 | // loop below. |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 2253 | dex_cache->SetDexFile(dex_file.get()); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 2254 | mirror::TypeDexCacheType* const types = dex_cache->GetResolvedTypes(); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 2255 | 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] | 2256 | 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] | 2257 | if (klass != nullptr) { |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2258 | DCHECK(!klass->IsErroneous()) << klass->GetStatus(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2259 | } |
| 2260 | } |
| 2261 | } else { |
| 2262 | if (kSanityCheckObjects) { |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 2263 | ImageSanityChecks::CheckArtMethodDexCacheArray(heap, |
| 2264 | this, |
| 2265 | dex_cache->GetResolvedMethods(), |
| 2266 | dex_cache->NumResolvedMethods()); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2267 | } |
| 2268 | // Register dex files, keep track of existing ones that are conflicts. |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 2269 | AppendToBootClassPath(*dex_file.get(), dex_cache); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2270 | } |
| 2271 | out_dex_files->push_back(std::move(dex_file)); |
| 2272 | } |
| 2273 | |
| 2274 | if (app_image) { |
| 2275 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Nicolas Geoffray | f0d3002 | 2018-11-20 17:45:38 +0000 | [diff] [blame] | 2276 | ScopedAssertNoThreadSuspension sants("Checking app image", soa.Self()); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2277 | // Check that the class loader resolves the same way as the ones in the image. |
| 2278 | // Image class loader [A][B][C][image dex files] |
| 2279 | // Class loader = [???][dex_elements][image dex files] |
| 2280 | // Need to ensure that [???][dex_elements] == [A][B][C]. |
David Brazdil | 05909d8 | 2018-12-06 16:25:16 +0000 | [diff] [blame] | 2281 | // For each class loader, PathClassLoader, the loader checks the parent first. Also the logic |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2282 | // for PathClassLoader does this by looping through the array of dex files. To ensure they |
| 2283 | // resolve the same way, simply flatten the hierarchy in the way the resolution order would be, |
| 2284 | // and check that the dex file names are the same. |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 2285 | if (IsBootClassLoader(soa, image_class_loader.Get())) { |
| 2286 | *error_msg = "Unexpected BootClassLoader in app image"; |
| 2287 | return false; |
| 2288 | } |
Nicolas Geoffray | f0d3002 | 2018-11-20 17:45:38 +0000 | [diff] [blame] | 2289 | // The dex files of `class_loader` are not setup yet, so we cannot do a full comparison |
| 2290 | // of `class_loader` and `image_class_loader` in `CompareClassLoaders`. Therefore, we |
| 2291 | // special case the comparison of dex files of the two class loaders, but then do full |
| 2292 | // comparisons for their shared libraries and parent. |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 2293 | auto elements = soa.Decode<mirror::ObjectArray<mirror::Object>>(dex_elements); |
Nicolas Geoffray | f0d3002 | 2018-11-20 17:45:38 +0000 | [diff] [blame] | 2294 | std::list<ObjPtr<mirror::String>> loader_dex_file_names; |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 2295 | for (size_t i = 0, num_elems = elements->GetLength(); i < num_elems; ++i) { |
| 2296 | ObjPtr<mirror::Object> element = elements->GetWithoutChecks(i); |
| 2297 | if (element != nullptr) { |
| 2298 | // 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] | 2299 | ObjPtr<mirror::String> name; |
| 2300 | if (GetDexPathListElementName(element, &name) && name != nullptr) { |
| 2301 | loader_dex_file_names.push_back(name); |
| 2302 | } |
Nicolas Geoffray | 1df3b55 | 2016-04-26 18:30:31 +0100 | [diff] [blame] | 2303 | } |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 2304 | } |
Nicolas Geoffray | f0d3002 | 2018-11-20 17:45:38 +0000 | [diff] [blame] | 2305 | std::string temp_error_msg; |
| 2306 | std::list<ObjPtr<mirror::String>> image_dex_file_names; |
| 2307 | bool success = GetDexFileNames( |
| 2308 | soa, image_class_loader.Get(), &image_dex_file_names, &temp_error_msg); |
| 2309 | if (success) { |
| 2310 | // Ignore the number of image dex files since we are adding those to the class loader anyways. |
| 2311 | CHECK_GE(static_cast<size_t>(image_dex_file_names.size()), |
| 2312 | static_cast<size_t>(dex_caches->GetLength())); |
| 2313 | size_t image_count = image_dex_file_names.size() - dex_caches->GetLength(); |
| 2314 | image_dex_file_names.resize(image_count); |
| 2315 | success = success && CompareDexFiles(image_dex_file_names, |
| 2316 | loader_dex_file_names, |
| 2317 | &temp_error_msg); |
| 2318 | success = success && CompareClassLoaders(soa, |
| 2319 | image_class_loader.Get(), |
| 2320 | class_loader.Get(), |
| 2321 | /*check_dex_file_names=*/ false, |
| 2322 | &temp_error_msg); |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 2323 | } |
Nicolas Geoffray | f0d3002 | 2018-11-20 17:45:38 +0000 | [diff] [blame] | 2324 | if (!success) { |
| 2325 | *error_msg = StringPrintf("Rejecting application image due to class loader mismatch: '%s'", |
| 2326 | temp_error_msg.c_str()); |
| 2327 | return false; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2328 | } |
| 2329 | } |
| 2330 | |
| 2331 | if (kSanityCheckObjects) { |
| 2332 | for (int32_t i = 0; i < dex_caches->GetLength(); i++) { |
Vladimir Marko | 423bebb | 2019-03-26 15:17:21 +0000 | [diff] [blame] | 2333 | ObjPtr<mirror::DexCache> dex_cache = dex_caches->Get(i); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2334 | for (size_t j = 0; j < dex_cache->NumResolvedFields(); ++j) { |
| 2335 | auto* field = dex_cache->GetResolvedField(j, image_pointer_size_); |
| 2336 | if (field != nullptr) { |
| 2337 | CHECK(field->GetDeclaringClass()->GetClass() != nullptr); |
| 2338 | } |
| 2339 | } |
| 2340 | } |
| 2341 | if (!app_image) { |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 2342 | ImageSanityChecks::CheckObjects(heap, this); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2343 | } |
| 2344 | } |
| 2345 | |
| 2346 | // Set entry point to interpreter if in InterpretOnly mode. |
| 2347 | if (!runtime->IsAotCompiler() && runtime->GetInstrumentation()->InterpretOnly()) { |
Mathieu Chartier | 9d5956a | 2019-03-22 11:29:08 -0700 | [diff] [blame] | 2348 | // Set image methods' entry point to interpreter. |
| 2349 | header.VisitPackedArtMethods([&](ArtMethod& method) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 2350 | if (!method.IsRuntimeMethod()) { |
| 2351 | DCHECK(method.GetDeclaringClass() != nullptr); |
| 2352 | if (!method.IsNative() && !method.IsResolutionMethod()) { |
| 2353 | method.SetEntryPointFromQuickCompiledCodePtrSize(GetQuickToInterpreterBridge(), |
| 2354 | image_pointer_size_); |
| 2355 | } |
| 2356 | } |
| 2357 | }, space->Begin(), image_pointer_size_); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2358 | } |
| 2359 | |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 2360 | ClassTable* class_table = nullptr; |
| 2361 | { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2362 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 2363 | class_table = InsertClassTableForClassLoader(class_loader.Get()); |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 2364 | } |
| 2365 | // If we have a class table section, read it and use it for verification in |
| 2366 | // UpdateAppImageClassLoadersAndDexCaches. |
| 2367 | ClassTable::ClassSet temp_set; |
Vladimir Marko | 0f3c700 | 2017-09-07 14:15:56 +0100 | [diff] [blame] | 2368 | const ImageSection& class_table_section = header.GetClassTableSection(); |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 2369 | const bool added_class_table = class_table_section.Size() > 0u; |
| 2370 | if (added_class_table) { |
| 2371 | const uint64_t start_time2 = NanoTime(); |
| 2372 | size_t read_count = 0; |
| 2373 | temp_set = ClassTable::ClassSet(space->Begin() + class_table_section.Offset(), |
| 2374 | /*make copy*/false, |
| 2375 | &read_count); |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 2376 | VLOG(image) << "Adding class table classes took " << PrettyDuration(NanoTime() - start_time2); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2377 | } |
| 2378 | if (app_image) { |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 2379 | AppImageLoadingHelper::Update(this, space, class_loader, dex_caches, &temp_set); |
Mathieu Chartier | 456b492 | 2018-11-06 10:35:48 -0800 | [diff] [blame] | 2380 | |
| 2381 | { |
| 2382 | ScopedTrace trace("AppImage:UpdateClassLoaders"); |
| 2383 | // Update class loader and resolved strings. If added_class_table is false, the resolved |
| 2384 | // strings were forwarded UpdateAppImageClassLoadersAndDexCaches. |
Mathieu Chartier | bc1e0fa | 2018-11-14 16:18:18 -0800 | [diff] [blame] | 2385 | ObjPtr<mirror::ClassLoader> loader(class_loader.Get()); |
Mathieu Chartier | 456b492 | 2018-11-06 10:35:48 -0800 | [diff] [blame] | 2386 | for (const ClassTable::TableSlot& root : temp_set) { |
Mathieu Chartier | bc1e0fa | 2018-11-14 16:18:18 -0800 | [diff] [blame] | 2387 | // Note: We probably don't need the read barrier unless we copy the app image objects into |
| 2388 | // the region space. |
| 2389 | ObjPtr<mirror::Class> klass(root.Read()); |
| 2390 | // Do not update class loader for boot image classes where the app image |
| 2391 | // class loader is only the initiating loader but not the defining loader. |
| 2392 | // Avoid read barrier since we are comparing against null. |
| 2393 | if (klass->GetClassLoader<kDefaultVerifyFlags, kWithoutReadBarrier>() != nullptr) { |
| 2394 | klass->SetClassLoader</*kCheckTransaction=*/ false>(loader); |
| 2395 | } |
Mathieu Chartier | 456b492 | 2018-11-06 10:35:48 -0800 | [diff] [blame] | 2396 | } |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 2397 | } |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 2398 | |
Vladimir Marko | 305c38b | 2018-02-14 11:50:07 +0000 | [diff] [blame] | 2399 | if (kBitstringSubtypeCheckEnabled) { |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 2400 | // Every class in the app image has initially SubtypeCheckInfo in the |
| 2401 | // Uninitialized state. |
| 2402 | // |
| 2403 | // The SubtypeCheck invariants imply that a SubtypeCheckInfo is at least Initialized |
| 2404 | // after class initialization is complete. The app image ClassStatus as-is |
| 2405 | // are almost all ClassStatus::Initialized, and being in the |
| 2406 | // SubtypeCheckInfo::kUninitialized state is violating that invariant. |
| 2407 | // |
| 2408 | // Force every app image class's SubtypeCheck to be at least kIninitialized. |
| 2409 | // |
| 2410 | // See also ImageWriter::FixupClass. |
Chris Wailes | 2386636 | 2018-08-22 16:16:58 -0700 | [diff] [blame] | 2411 | ScopedTrace trace("AppImage:RecacluateSubtypeCheckBitstrings"); |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 2412 | MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_); |
| 2413 | for (const ClassTable::TableSlot& root : temp_set) { |
Vladimir Marko | 38b8b25 | 2018-01-02 19:07:06 +0000 | [diff] [blame] | 2414 | SubtypeCheck<ObjPtr<mirror::Class>>::EnsureInitialized(root.Read()); |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 2415 | } |
| 2416 | } |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 2417 | } |
| 2418 | if (!oat_file->GetBssGcRoots().empty()) { |
| 2419 | // Insert oat file to class table for visiting .bss GC roots. |
| 2420 | class_table->InsertOatFile(oat_file); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2421 | } |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 2422 | |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 2423 | if (added_class_table) { |
| 2424 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 2425 | class_table->AddClassSet(std::move(temp_set)); |
| 2426 | } |
Andreas Gampe | be7af22 | 2017-07-25 09:57:28 -0700 | [diff] [blame] | 2427 | |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 2428 | if (kIsDebugBuild && app_image) { |
| 2429 | // This verification needs to happen after the classes have been added to the class loader. |
| 2430 | // Since it ensures classes are in the class table. |
Chris Wailes | 2386636 | 2018-08-22 16:16:58 -0700 | [diff] [blame] | 2431 | ScopedTrace trace("AppImage:Verify"); |
Andreas Gampe | be7af22 | 2017-07-25 09:57:28 -0700 | [diff] [blame] | 2432 | VerifyAppImage(header, class_loader, dex_caches, class_table, space); |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 2433 | } |
Andreas Gampe | be7af22 | 2017-07-25 09:57:28 -0700 | [diff] [blame] | 2434 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2435 | VLOG(class_linker) << "Adding image space took " << PrettyDuration(NanoTime() - start_time); |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 2436 | return true; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 2437 | } |
| 2438 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2439 | bool ClassLinker::ClassInClassTable(ObjPtr<mirror::Class> klass) { |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 2440 | ClassTable* const class_table = ClassTableForClassLoader(klass->GetClassLoader()); |
| 2441 | return class_table != nullptr && class_table->Contains(klass); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2442 | } |
| 2443 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 2444 | void ClassLinker::VisitClassRoots(RootVisitor* visitor, VisitRootFlags flags) { |
Mathieu Chartier | 7778b88 | 2015-10-05 16:41:10 -0700 | [diff] [blame] | 2445 | // Acquire tracing_enabled before locking class linker lock to prevent lock order violation. Since |
| 2446 | // enabling tracing requires the mutator lock, there are no race conditions here. |
| 2447 | const bool tracing_enabled = Trace::IsTracingEnabled(); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2448 | Thread* const self = Thread::Current(); |
| 2449 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Vladimir Marko | 9b03cb4 | 2017-02-16 16:37:03 +0000 | [diff] [blame] | 2450 | if (kUseReadBarrier) { |
| 2451 | // We do not track new roots for CC. |
| 2452 | DCHECK_EQ(0, flags & (kVisitRootFlagNewRoots | |
| 2453 | kVisitRootFlagClearRootLog | |
| 2454 | kVisitRootFlagStartLoggingNewRoots | |
| 2455 | kVisitRootFlagStopLoggingNewRoots)); |
| 2456 | } |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2457 | if ((flags & kVisitRootFlagAllRoots) != 0) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2458 | // Argument for how root visiting deals with ArtField and ArtMethod roots. |
| 2459 | // There is 3 GC cases to handle: |
| 2460 | // Non moving concurrent: |
| 2461 | // 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] | 2462 | // live by the class and class roots. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2463 | // |
| 2464 | // Moving non-concurrent: |
| 2465 | // This case needs to call visit VisitNativeRoots in case the classes or dex cache arrays move. |
| 2466 | // To prevent missing roots, this case needs to ensure that there is no |
| 2467 | // suspend points between the point which we allocate ArtMethod arrays and place them in a |
| 2468 | // class which is in the class table. |
| 2469 | // |
| 2470 | // Moving concurrent: |
| 2471 | // Need to make sure to not copy ArtMethods without doing read barriers since the roots are |
| 2472 | // 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] | 2473 | // |
| 2474 | // Use an unbuffered visitor since the class table uses a temporary GcRoot for holding decoded |
| 2475 | // ClassTable::TableSlot. The buffered root visiting would access a stale stack location for |
| 2476 | // these objects. |
| 2477 | UnbufferedRootVisitor root_visitor(visitor, RootInfo(kRootStickyClass)); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 2478 | boot_class_table_->VisitRoots(root_visitor); |
Mathieu Chartier | 7778b88 | 2015-10-05 16:41:10 -0700 | [diff] [blame] | 2479 | // 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] | 2480 | if ((flags & kVisitRootFlagClassLoader) != 0 || tracing_enabled) { |
Mathieu Chartier | 7778b88 | 2015-10-05 16:41:10 -0700 | [diff] [blame] | 2481 | for (const ClassLoaderData& data : class_loaders_) { |
| 2482 | GcRoot<mirror::Object> root(GcRoot<mirror::Object>(self->DecodeJObject(data.weak_root))); |
| 2483 | root.VisitRoot(visitor, RootInfo(kRootVMInternal)); |
| 2484 | } |
| 2485 | } |
Vladimir Marko | 9b03cb4 | 2017-02-16 16:37:03 +0000 | [diff] [blame] | 2486 | } else if (!kUseReadBarrier && (flags & kVisitRootFlagNewRoots) != 0) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 2487 | for (auto& root : new_class_roots_) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2488 | ObjPtr<mirror::Class> old_ref = root.Read<kWithoutReadBarrier>(); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 2489 | root.VisitRoot(visitor, RootInfo(kRootStickyClass)); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2490 | ObjPtr<mirror::Class> new_ref = root.Read<kWithoutReadBarrier>(); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 2491 | // Concurrent moving GC marked new roots through the to-space invariant. |
| 2492 | CHECK_EQ(new_ref, old_ref); |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2493 | } |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 2494 | for (const OatFile* oat_file : new_bss_roots_boot_oat_files_) { |
| 2495 | for (GcRoot<mirror::Object>& root : oat_file->GetBssGcRoots()) { |
| 2496 | ObjPtr<mirror::Object> old_ref = root.Read<kWithoutReadBarrier>(); |
| 2497 | if (old_ref != nullptr) { |
| 2498 | DCHECK(old_ref->IsClass()); |
| 2499 | root.VisitRoot(visitor, RootInfo(kRootStickyClass)); |
| 2500 | ObjPtr<mirror::Object> new_ref = root.Read<kWithoutReadBarrier>(); |
| 2501 | // Concurrent moving GC marked new roots through the to-space invariant. |
| 2502 | CHECK_EQ(new_ref, old_ref); |
| 2503 | } |
| 2504 | } |
| 2505 | } |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2506 | } |
Vladimir Marko | 9b03cb4 | 2017-02-16 16:37:03 +0000 | [diff] [blame] | 2507 | if (!kUseReadBarrier && (flags & kVisitRootFlagClearRootLog) != 0) { |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2508 | new_class_roots_.clear(); |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 2509 | new_bss_roots_boot_oat_files_.clear(); |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2510 | } |
Vladimir Marko | 9b03cb4 | 2017-02-16 16:37:03 +0000 | [diff] [blame] | 2511 | if (!kUseReadBarrier && (flags & kVisitRootFlagStartLoggingNewRoots) != 0) { |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 2512 | log_new_roots_ = true; |
Vladimir Marko | 9b03cb4 | 2017-02-16 16:37:03 +0000 | [diff] [blame] | 2513 | } else if (!kUseReadBarrier && (flags & kVisitRootFlagStopLoggingNewRoots) != 0) { |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 2514 | log_new_roots_ = false; |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2515 | } |
| 2516 | // We deliberately ignore the class roots in the image since we |
| 2517 | // handle image roots by using the MS/CMS rescanning of dirty cards. |
| 2518 | } |
| 2519 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 2520 | // Keep in sync with InitCallback. Anything we visit, we need to |
| 2521 | // reinit references to when reinitializing a ClassLinker from a |
| 2522 | // mapped image. |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 2523 | void ClassLinker::VisitRoots(RootVisitor* visitor, VisitRootFlags flags) { |
Mathieu Chartier | 3100080 | 2015-06-14 14:14:37 -0700 | [diff] [blame] | 2524 | class_roots_.VisitRootIfNonNull(visitor, RootInfo(kRootVMInternal)); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 2525 | VisitClassRoots(visitor, flags); |
Mathieu Chartier | 6cfc2c0 | 2015-10-12 15:06:16 -0700 | [diff] [blame] | 2526 | // Instead of visiting the find_array_class_cache_ drop it so that it doesn't prevent class |
| 2527 | // unloading if we are marking roots. |
| 2528 | DropFindArrayClassCache(); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 2529 | } |
| 2530 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2531 | class VisitClassLoaderClassesVisitor : public ClassLoaderVisitor { |
| 2532 | public: |
| 2533 | explicit VisitClassLoaderClassesVisitor(ClassVisitor* visitor) |
| 2534 | : visitor_(visitor), |
| 2535 | done_(false) {} |
| 2536 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2537 | void Visit(ObjPtr<mirror::ClassLoader> class_loader) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 2538 | REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_) override { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2539 | ClassTable* const class_table = class_loader->GetClassTable(); |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2540 | if (!done_ && class_table != nullptr) { |
| 2541 | DefiningClassLoaderFilterVisitor visitor(class_loader, visitor_); |
| 2542 | if (!class_table->Visit(visitor)) { |
| 2543 | // If the visitor ClassTable returns false it means that we don't need to continue. |
| 2544 | done_ = true; |
| 2545 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2546 | } |
| 2547 | } |
| 2548 | |
| 2549 | private: |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2550 | // Class visitor that limits the class visits from a ClassTable to the classes with |
| 2551 | // the provided defining class loader. This filter is used to avoid multiple visits |
| 2552 | // of the same class which can be recorded for multiple initiating class loaders. |
| 2553 | class DefiningClassLoaderFilterVisitor : public ClassVisitor { |
| 2554 | public: |
| 2555 | DefiningClassLoaderFilterVisitor(ObjPtr<mirror::ClassLoader> defining_class_loader, |
| 2556 | ClassVisitor* visitor) |
| 2557 | : defining_class_loader_(defining_class_loader), visitor_(visitor) { } |
| 2558 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 2559 | bool operator()(ObjPtr<mirror::Class> klass) override REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2560 | if (klass->GetClassLoader() != defining_class_loader_) { |
| 2561 | return true; |
| 2562 | } |
| 2563 | return (*visitor_)(klass); |
| 2564 | } |
| 2565 | |
Vladimir Marko | 0984e48 | 2019-03-27 16:41:41 +0000 | [diff] [blame] | 2566 | const ObjPtr<mirror::ClassLoader> defining_class_loader_; |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2567 | ClassVisitor* const visitor_; |
| 2568 | }; |
| 2569 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2570 | ClassVisitor* const visitor_; |
| 2571 | // If done is true then we don't need to do any more visiting. |
| 2572 | bool done_; |
| 2573 | }; |
| 2574 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2575 | void ClassLinker::VisitClassesInternal(ClassVisitor* visitor) { |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 2576 | if (boot_class_table_->Visit(*visitor)) { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2577 | VisitClassLoaderClassesVisitor loader_visitor(visitor); |
| 2578 | VisitClassLoaders(&loader_visitor); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 2579 | } |
| 2580 | } |
| 2581 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2582 | void ClassLinker::VisitClasses(ClassVisitor* visitor) { |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 2583 | Thread* const self = Thread::Current(); |
| 2584 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 2585 | // Not safe to have thread suspension when we are holding a lock. |
| 2586 | if (self != nullptr) { |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 2587 | ScopedAssertNoThreadSuspension nts(__FUNCTION__); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2588 | VisitClassesInternal(visitor); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 2589 | } else { |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2590 | VisitClassesInternal(visitor); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2591 | } |
| 2592 | } |
| 2593 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2594 | class GetClassesInToVector : public ClassVisitor { |
| 2595 | public: |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 2596 | bool operator()(ObjPtr<mirror::Class> klass) override { |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2597 | classes_.push_back(klass); |
| 2598 | return true; |
| 2599 | } |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2600 | std::vector<ObjPtr<mirror::Class>> classes_; |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2601 | }; |
| 2602 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2603 | class GetClassInToObjectArray : public ClassVisitor { |
| 2604 | public: |
| 2605 | explicit GetClassInToObjectArray(mirror::ObjectArray<mirror::Class>* arr) |
| 2606 | : arr_(arr), index_(0) {} |
| 2607 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 2608 | bool operator()(ObjPtr<mirror::Class> klass) override REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2609 | ++index_; |
| 2610 | if (index_ <= arr_->GetLength()) { |
| 2611 | arr_->Set(index_ - 1, klass); |
| 2612 | return true; |
| 2613 | } |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2614 | return false; |
| 2615 | } |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2616 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2617 | bool Succeeded() const REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2618 | return index_ <= arr_->GetLength(); |
| 2619 | } |
| 2620 | |
| 2621 | private: |
| 2622 | mirror::ObjectArray<mirror::Class>* const arr_; |
| 2623 | int32_t index_; |
| 2624 | }; |
| 2625 | |
| 2626 | void ClassLinker::VisitClassesWithoutClassesLock(ClassVisitor* visitor) { |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2627 | // TODO: it may be possible to avoid secondary storage if we iterate over dex caches. The problem |
| 2628 | // is avoiding duplicates. |
| 2629 | if (!kMovingClasses) { |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 2630 | ScopedAssertNoThreadSuspension nts(__FUNCTION__); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2631 | GetClassesInToVector accumulator; |
| 2632 | VisitClasses(&accumulator); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2633 | for (ObjPtr<mirror::Class> klass : accumulator.classes_) { |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 2634 | if (!visitor->operator()(klass)) { |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2635 | return; |
| 2636 | } |
| 2637 | } |
| 2638 | } else { |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 2639 | Thread* const self = Thread::Current(); |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2640 | StackHandleScope<1> hs(self); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2641 | auto classes = hs.NewHandle<mirror::ObjectArray<mirror::Class>>(nullptr); |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2642 | // We size the array assuming classes won't be added to the class table during the visit. |
| 2643 | // If this assumption fails we iterate again. |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2644 | while (true) { |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2645 | size_t class_table_size; |
| 2646 | { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2647 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2648 | // Add 100 in case new classes get loaded when we are filling in the object array. |
| 2649 | class_table_size = NumZygoteClasses() + NumNonZygoteClasses() + 100; |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2650 | } |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 2651 | ObjPtr<mirror::Class> array_of_class = GetClassRoot<mirror::ObjectArray<mirror::Class>>(this); |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2652 | classes.Assign( |
| 2653 | mirror::ObjectArray<mirror::Class>::Alloc(self, array_of_class, class_table_size)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 2654 | CHECK(classes != nullptr); // OOME. |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2655 | GetClassInToObjectArray accumulator(classes.Get()); |
| 2656 | VisitClasses(&accumulator); |
| 2657 | if (accumulator.Succeeded()) { |
| 2658 | break; |
| 2659 | } |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2660 | } |
| 2661 | for (int32_t i = 0; i < classes->GetLength(); ++i) { |
| 2662 | // If the class table shrank during creation of the clases array we expect null elements. If |
| 2663 | // the class table grew then the loop repeats. If classes are created after the loop has |
| 2664 | // finished then we don't visit. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2665 | ObjPtr<mirror::Class> klass = classes->Get(i); |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 2666 | if (klass != nullptr && !visitor->operator()(klass)) { |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2667 | return; |
| 2668 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2669 | } |
| 2670 | } |
| 2671 | } |
| 2672 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 2673 | ClassLinker::~ClassLinker() { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2674 | Thread* const self = Thread::Current(); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 2675 | for (const ClassLoaderData& data : class_loaders_) { |
Alexey Grebenkin | be4c2bd | 2018-02-01 19:09:59 +0300 | [diff] [blame] | 2676 | // CHA unloading analysis is not needed. No negative consequences are expected because |
| 2677 | // all the classloaders are deleted at the same time. |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 2678 | DeleteClassLoader(self, data, /*cleanup_cha=*/ false); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 2679 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2680 | class_loaders_.clear(); |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 2681 | while (!running_visibly_initialized_callbacks_.empty()) { |
| 2682 | std::unique_ptr<VisiblyInitializedCallback> callback( |
| 2683 | std::addressof(running_visibly_initialized_callbacks_.front())); |
| 2684 | running_visibly_initialized_callbacks_.pop_front(); |
| 2685 | } |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 2686 | } |
| 2687 | |
Alexey Grebenkin | be4c2bd | 2018-02-01 19:09:59 +0300 | [diff] [blame] | 2688 | void ClassLinker::DeleteClassLoader(Thread* self, const ClassLoaderData& data, bool cleanup_cha) { |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 2689 | Runtime* const runtime = Runtime::Current(); |
| 2690 | JavaVMExt* const vm = runtime->GetJavaVM(); |
| 2691 | vm->DeleteWeakGlobalRef(self, data.weak_root); |
Calin Juravle | e5de54c | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 2692 | // 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] | 2693 | if (runtime->GetJit() != nullptr) { |
| 2694 | jit::JitCodeCache* code_cache = runtime->GetJit()->GetCodeCache(); |
| 2695 | if (code_cache != nullptr) { |
Mathieu Chartier | cf79cf5 | 2017-07-21 11:17:57 -0700 | [diff] [blame] | 2696 | // For the JIT case, RemoveMethodsIn removes the CHA dependencies. |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 2697 | code_cache->RemoveMethodsIn(self, *data.allocator); |
| 2698 | } |
Andreas Gampe | c1ac9ee | 2017-07-24 22:35:49 -0700 | [diff] [blame] | 2699 | } else if (cha_ != nullptr) { |
Mathieu Chartier | cf79cf5 | 2017-07-21 11:17:57 -0700 | [diff] [blame] | 2700 | // 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] | 2701 | cha_->RemoveDependenciesForLinearAlloc(data.allocator); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 2702 | } |
Alexey Grebenkin | be4c2bd | 2018-02-01 19:09:59 +0300 | [diff] [blame] | 2703 | // Cleanup references to single implementation ArtMethods that will be deleted. |
| 2704 | if (cleanup_cha) { |
| 2705 | CHAOnDeleteUpdateClassVisitor visitor(data.allocator); |
| 2706 | data.class_table->Visit<CHAOnDeleteUpdateClassVisitor, kWithoutReadBarrier>(visitor); |
| 2707 | } |
| 2708 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 2709 | delete data.allocator; |
| 2710 | delete data.class_table; |
| 2711 | } |
| 2712 | |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 2713 | ObjPtr<mirror::PointerArray> ClassLinker::AllocPointerArray(Thread* self, size_t length) { |
| 2714 | return ObjPtr<mirror::PointerArray>::DownCast( |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 2715 | image_pointer_size_ == PointerSize::k64 |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 2716 | ? ObjPtr<mirror::Array>(mirror::LongArray::Alloc(self, length)) |
| 2717 | : ObjPtr<mirror::Array>(mirror::IntArray::Alloc(self, length))); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2718 | } |
| 2719 | |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 2720 | ObjPtr<mirror::DexCache> ClassLinker::AllocDexCache(/*out*/ ObjPtr<mirror::String>* out_location, |
| 2721 | Thread* self, |
| 2722 | const DexFile& dex_file) { |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2723 | StackHandleScope<1> hs(self); |
| 2724 | DCHECK(out_location != nullptr); |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 2725 | auto dex_cache(hs.NewHandle(ObjPtr<mirror::DexCache>::DownCast( |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 2726 | GetClassRoot<mirror::DexCache>(this)->AllocObject(self)))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 2727 | if (dex_cache == nullptr) { |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2728 | self->AssertPendingOOMException(); |
| 2729 | return nullptr; |
| 2730 | } |
Vladimir Marko | 31c3daa | 2019-06-13 12:18:37 +0100 | [diff] [blame] | 2731 | // Use InternWeak() so that the location String can be collected when the ClassLoader |
| 2732 | // with this DexCache is collected. |
| 2733 | ObjPtr<mirror::String> location = intern_table_->InternWeak(dex_file.GetLocation().c_str()); |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2734 | if (location == nullptr) { |
| 2735 | self->AssertPendingOOMException(); |
| 2736 | return nullptr; |
| 2737 | } |
| 2738 | *out_location = location; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2739 | return dex_cache.Get(); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 2740 | } |
| 2741 | |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 2742 | ObjPtr<mirror::DexCache> ClassLinker::AllocAndInitializeDexCache(Thread* self, |
| 2743 | const DexFile& dex_file, |
| 2744 | LinearAlloc* linear_alloc) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2745 | ObjPtr<mirror::String> location = nullptr; |
| 2746 | ObjPtr<mirror::DexCache> dex_cache = AllocDexCache(&location, self, dex_file); |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2747 | if (dex_cache != nullptr) { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 2748 | WriterMutexLock mu(self, *Locks::dex_lock_); |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2749 | DCHECK(location != nullptr); |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 2750 | mirror::DexCache::InitializeDexCache(self, |
| 2751 | dex_cache, |
| 2752 | location, |
| 2753 | &dex_file, |
| 2754 | linear_alloc, |
| 2755 | image_pointer_size_); |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2756 | } |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 2757 | return dex_cache; |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2758 | } |
| 2759 | |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 2760 | template <bool kMovable, typename PreFenceVisitor> |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 2761 | ObjPtr<mirror::Class> ClassLinker::AllocClass(Thread* self, |
| 2762 | ObjPtr<mirror::Class> java_lang_Class, |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 2763 | uint32_t class_size, |
| 2764 | const PreFenceVisitor& pre_fence_visitor) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2765 | DCHECK_GE(class_size, sizeof(mirror::Class)); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 2766 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 2767 | ObjPtr<mirror::Object> k = (kMovingClasses && kMovable) ? |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 2768 | heap->AllocObject(self, java_lang_Class, class_size, pre_fence_visitor) : |
| 2769 | heap->AllocNonMovableObject(self, java_lang_Class, class_size, pre_fence_visitor); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 2770 | if (UNLIKELY(k == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2771 | self->AssertPendingOOMException(); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 2772 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2773 | } |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 2774 | return k->AsClass(); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 2775 | } |
| 2776 | |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 2777 | template <bool kMovable> |
| 2778 | ObjPtr<mirror::Class> ClassLinker::AllocClass(Thread* self, |
| 2779 | ObjPtr<mirror::Class> java_lang_Class, |
| 2780 | uint32_t class_size) { |
| 2781 | mirror::Class::InitializeClassVisitor visitor(class_size); |
| 2782 | return AllocClass<kMovable>(self, java_lang_Class, class_size, visitor); |
| 2783 | } |
| 2784 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 2785 | ObjPtr<mirror::Class> ClassLinker::AllocClass(Thread* self, uint32_t class_size) { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 2786 | return AllocClass(self, GetClassRoot<mirror::Class>(this), class_size); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 2787 | } |
| 2788 | |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 2789 | void ClassLinker::AllocPrimitiveArrayClass(Thread* self, |
| 2790 | ClassRoot primitive_root, |
| 2791 | ClassRoot array_root) { |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 2792 | // We make this class non-movable for the unlikely case where it were to be |
| 2793 | // moved by a sticky-bit (minor) collection when using the Generational |
| 2794 | // Concurrent Copying (CC) collector, potentially creating a stale reference |
| 2795 | // in the `klass_` field of one of its instances allocated in the Large-Object |
| 2796 | // Space (LOS) -- see the comment about the dirty card scanning logic in |
| 2797 | // art::gc::collector::ConcurrentCopying::MarkingPhase. |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 2798 | ObjPtr<mirror::Class> array_class = AllocClass</* kMovable= */ false>( |
| 2799 | self, GetClassRoot<mirror::Class>(this), mirror::Array::ClassSize(image_pointer_size_)); |
| 2800 | ObjPtr<mirror::Class> component_type = GetClassRoot(primitive_root, this); |
| 2801 | DCHECK(component_type->IsPrimitive()); |
| 2802 | array_class->SetComponentType(component_type); |
| 2803 | SetClassRoot(array_root, array_class); |
| 2804 | } |
| 2805 | |
| 2806 | void ClassLinker::FinishArrayClassSetup(ObjPtr<mirror::Class> array_class) { |
| 2807 | ObjPtr<mirror::Class> java_lang_Object = GetClassRoot<mirror::Object>(this); |
| 2808 | array_class->SetSuperClass(java_lang_Object); |
| 2809 | array_class->SetVTable(java_lang_Object->GetVTable()); |
| 2810 | array_class->SetPrimitiveType(Primitive::kPrimNot); |
| 2811 | ObjPtr<mirror::Class> component_type = array_class->GetComponentType(); |
| 2812 | array_class->SetClassFlags(component_type->IsPrimitive() |
| 2813 | ? mirror::kClassFlagNoReferenceFields |
| 2814 | : mirror::kClassFlagObjectArray); |
| 2815 | array_class->SetClassLoader(component_type->GetClassLoader()); |
| 2816 | array_class->SetStatusForPrimitiveOrArray(ClassStatus::kLoaded); |
| 2817 | array_class->PopulateEmbeddedVTable(image_pointer_size_); |
| 2818 | ImTable* object_imt = java_lang_Object->GetImt(image_pointer_size_); |
| 2819 | array_class->SetImt(object_imt, image_pointer_size_); |
| 2820 | // Skip EnsureSkipAccessChecksMethods(). We can skip the verified status, |
| 2821 | // the kAccVerificationAttempted flag is added below, and there are no |
| 2822 | // methods that need the kAccSkipAccessChecks flag. |
| 2823 | DCHECK_EQ(array_class->NumMethods(), 0u); |
| 2824 | |
| 2825 | // don't need to set new_class->SetObjectSize(..) |
| 2826 | // because Object::SizeOf delegates to Array::SizeOf |
| 2827 | |
| 2828 | // All arrays have java/lang/Cloneable and java/io/Serializable as |
| 2829 | // interfaces. We need to set that up here, so that stuff like |
| 2830 | // "instanceof" works right. |
| 2831 | |
| 2832 | // Use the single, global copies of "interfaces" and "iftable" |
| 2833 | // (remember not to free them for arrays). |
| 2834 | { |
| 2835 | ObjPtr<mirror::IfTable> array_iftable = GetArrayIfTable(); |
| 2836 | CHECK(array_iftable != nullptr); |
| 2837 | array_class->SetIfTable(array_iftable); |
| 2838 | } |
| 2839 | |
| 2840 | // Inherit access flags from the component type. |
| 2841 | int access_flags = component_type->GetAccessFlags(); |
| 2842 | // Lose any implementation detail flags; in particular, arrays aren't finalizable. |
| 2843 | access_flags &= kAccJavaFlagsMask; |
| 2844 | // Arrays can't be used as a superclass or interface, so we want to add "abstract final" |
| 2845 | // and remove "interface". |
| 2846 | access_flags |= kAccAbstract | kAccFinal; |
| 2847 | access_flags &= ~kAccInterface; |
| 2848 | // Arrays are access-checks-clean and preverified. |
| 2849 | access_flags |= kAccVerificationAttempted; |
| 2850 | |
| 2851 | array_class->SetAccessFlags(access_flags); |
| 2852 | |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 2853 | // Array classes are fully initialized either during single threaded startup, |
| 2854 | // or from a pre-fence visitor, so visibly initialized. |
| 2855 | array_class->SetStatusForPrimitiveOrArray(ClassStatus::kVisiblyInitialized); |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 2856 | } |
| 2857 | |
| 2858 | void ClassLinker::FinishCoreArrayClassSetup(ClassRoot array_root) { |
| 2859 | // Do not hold lock on the array class object, the initialization of |
| 2860 | // core array classes is done while the process is still single threaded. |
| 2861 | ObjPtr<mirror::Class> array_class = GetClassRoot(array_root, this); |
| 2862 | FinishArrayClassSetup(array_class); |
| 2863 | |
| 2864 | std::string temp; |
| 2865 | const char* descriptor = array_class->GetDescriptor(&temp); |
| 2866 | size_t hash = ComputeModifiedUtf8Hash(descriptor); |
| 2867 | ObjPtr<mirror::Class> existing = InsertClass(descriptor, array_class, hash); |
| 2868 | CHECK(existing == nullptr); |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 2869 | } |
| 2870 | |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 2871 | ObjPtr<mirror::ObjectArray<mirror::StackTraceElement>> ClassLinker::AllocStackTraceElementArray( |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 2872 | Thread* self, |
| 2873 | size_t length) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2874 | return mirror::ObjectArray<mirror::StackTraceElement>::Alloc( |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 2875 | self, GetClassRoot<mirror::ObjectArray<mirror::StackTraceElement>>(this), length); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 2876 | } |
| 2877 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 2878 | ObjPtr<mirror::Class> ClassLinker::EnsureResolved(Thread* self, |
| 2879 | const char* descriptor, |
| 2880 | ObjPtr<mirror::Class> klass) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2881 | DCHECK(klass != nullptr); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2882 | if (kIsDebugBuild) { |
| 2883 | StackHandleScope<1> hs(self); |
| 2884 | HandleWrapperObjPtr<mirror::Class> h = hs.NewHandleWrapper(&klass); |
| 2885 | Thread::PoisonObjectPointersIfDebug(); |
| 2886 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2887 | |
| 2888 | // For temporary classes we must wait for them to be retired. |
| 2889 | if (init_done_ && klass->IsTemp()) { |
| 2890 | CHECK(!klass->IsResolved()); |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2891 | if (klass->IsErroneousUnresolved()) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2892 | ThrowEarlierClassFailure(klass); |
| 2893 | return nullptr; |
| 2894 | } |
| 2895 | StackHandleScope<1> hs(self); |
| 2896 | Handle<mirror::Class> h_class(hs.NewHandle(klass)); |
| 2897 | ObjectLock<mirror::Class> lock(self, h_class); |
| 2898 | // Loop and wait for the resolving thread to retire this class. |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2899 | while (!h_class->IsRetired() && !h_class->IsErroneousUnresolved()) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2900 | lock.WaitIgnoringInterrupts(); |
| 2901 | } |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2902 | if (h_class->IsErroneousUnresolved()) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2903 | ThrowEarlierClassFailure(h_class.Get()); |
| 2904 | return nullptr; |
| 2905 | } |
| 2906 | CHECK(h_class->IsRetired()); |
| 2907 | // Get the updated class from class table. |
Andreas Gampe | 34ee684 | 2014-12-02 15:43:52 -0800 | [diff] [blame] | 2908 | klass = LookupClass(self, descriptor, h_class.Get()->GetClassLoader()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2909 | } |
| 2910 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2911 | // Wait for the class if it has not already been linked. |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 2912 | size_t index = 0; |
| 2913 | // Maximum number of yield iterations until we start sleeping. |
| 2914 | static const size_t kNumYieldIterations = 1000; |
| 2915 | // How long each sleep is in us. |
| 2916 | static const size_t kSleepDurationUS = 1000; // 1 ms. |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2917 | while (!klass->IsResolved() && !klass->IsErroneousUnresolved()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2918 | StackHandleScope<1> hs(self); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2919 | HandleWrapperObjPtr<mirror::Class> h_class(hs.NewHandleWrapper(&klass)); |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 2920 | { |
| 2921 | ObjectTryLock<mirror::Class> lock(self, h_class); |
| 2922 | // Can not use a monitor wait here since it may block when returning and deadlock if another |
| 2923 | // thread has locked klass. |
| 2924 | if (lock.Acquired()) { |
| 2925 | // Check for circular dependencies between classes, the lock is required for SetStatus. |
| 2926 | if (!h_class->IsResolved() && h_class->GetClinitThreadId() == self->GetTid()) { |
| 2927 | ThrowClassCircularityError(h_class.Get()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 2928 | mirror::Class::SetStatus(h_class, ClassStatus::kErrorUnresolved, self); |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 2929 | return nullptr; |
| 2930 | } |
| 2931 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2932 | } |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 2933 | { |
| 2934 | // Handle wrapper deals with klass moving. |
| 2935 | ScopedThreadSuspension sts(self, kSuspended); |
| 2936 | if (index < kNumYieldIterations) { |
| 2937 | sched_yield(); |
| 2938 | } else { |
| 2939 | usleep(kSleepDurationUS); |
| 2940 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2941 | } |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 2942 | ++index; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2943 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2944 | |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2945 | if (klass->IsErroneousUnresolved()) { |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 2946 | ThrowEarlierClassFailure(klass); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 2947 | return nullptr; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2948 | } |
| 2949 | // Return the loaded class. No exceptions should be pending. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 2950 | CHECK(klass->IsResolved()) << klass->PrettyClass(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2951 | self->AssertNoPendingException(); |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 2952 | return klass; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2953 | } |
| 2954 | |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 2955 | using ClassPathEntry = std::pair<const DexFile*, const dex::ClassDef*>; |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2956 | |
| 2957 | // Search a collection of DexFiles for a descriptor |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2958 | ClassPathEntry FindInClassPath(const char* descriptor, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 2959 | size_t hash, const std::vector<const DexFile*>& class_path) { |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2960 | for (const DexFile* dex_file : class_path) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 2961 | const dex::ClassDef* dex_class_def = OatDexFile::FindClassDef(*dex_file, descriptor, hash); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2962 | if (dex_class_def != nullptr) { |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2963 | return ClassPathEntry(dex_file, dex_class_def); |
| 2964 | } |
| 2965 | } |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2966 | return ClassPathEntry(nullptr, nullptr); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2967 | } |
| 2968 | |
Nicolas Geoffray | 80a560c | 2018-10-26 13:48:51 +0100 | [diff] [blame] | 2969 | bool ClassLinker::FindClassInSharedLibraries(ScopedObjectAccessAlreadyRunnable& soa, |
| 2970 | Thread* self, |
| 2971 | const char* descriptor, |
| 2972 | size_t hash, |
| 2973 | Handle<mirror::ClassLoader> class_loader, |
| 2974 | /*out*/ ObjPtr<mirror::Class>* result) { |
| 2975 | ArtField* field = |
| 2976 | jni::DecodeArtField(WellKnownClasses::dalvik_system_BaseDexClassLoader_sharedLibraryLoaders); |
| 2977 | ObjPtr<mirror::Object> raw_shared_libraries = field->GetObject(class_loader.Get()); |
| 2978 | if (raw_shared_libraries == nullptr) { |
| 2979 | return true; |
| 2980 | } |
| 2981 | |
| 2982 | StackHandleScope<2> hs(self); |
| 2983 | Handle<mirror::ObjectArray<mirror::ClassLoader>> shared_libraries( |
| 2984 | hs.NewHandle(raw_shared_libraries->AsObjectArray<mirror::ClassLoader>())); |
| 2985 | MutableHandle<mirror::ClassLoader> temp_loader = hs.NewHandle<mirror::ClassLoader>(nullptr); |
| 2986 | for (int32_t i = 0; i < shared_libraries->GetLength(); ++i) { |
| 2987 | temp_loader.Assign(shared_libraries->Get(i)); |
| 2988 | if (!FindClassInBaseDexClassLoader(soa, self, descriptor, hash, temp_loader, result)) { |
| 2989 | return false; // One of the shared libraries is not supported. |
| 2990 | } |
| 2991 | if (*result != nullptr) { |
| 2992 | return true; // Found the class up the chain. |
| 2993 | } |
| 2994 | } |
| 2995 | return true; |
| 2996 | } |
| 2997 | |
Nicolas Geoffray | 7d8d8ff | 2016-11-02 12:38:05 +0000 | [diff] [blame] | 2998 | bool ClassLinker::FindClassInBaseDexClassLoader(ScopedObjectAccessAlreadyRunnable& soa, |
| 2999 | Thread* self, |
| 3000 | const char* descriptor, |
| 3001 | size_t hash, |
| 3002 | Handle<mirror::ClassLoader> class_loader, |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 3003 | /*out*/ ObjPtr<mirror::Class>* result) { |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 3004 | // Termination case: boot class loader. |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 3005 | if (IsBootClassLoader(soa, class_loader.Get())) { |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 3006 | *result = FindClassInBootClassLoaderClassPath(self, descriptor, hash); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 3007 | return true; |
| 3008 | } |
| 3009 | |
David Brazdil | 05909d8 | 2018-12-06 16:25:16 +0000 | [diff] [blame] | 3010 | if (IsPathOrDexClassLoader(soa, class_loader) || IsInMemoryDexClassLoader(soa, class_loader)) { |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 3011 | // For regular path or dex class loader the search order is: |
| 3012 | // - parent |
Nicolas Geoffray | 80a560c | 2018-10-26 13:48:51 +0100 | [diff] [blame] | 3013 | // - shared libraries |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 3014 | // - class loader dex files |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 3015 | |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 3016 | // Handles as RegisterDexFile may allocate dex caches (and cause thread suspension). |
| 3017 | StackHandleScope<1> hs(self); |
| 3018 | Handle<mirror::ClassLoader> h_parent(hs.NewHandle(class_loader->GetParent())); |
| 3019 | if (!FindClassInBaseDexClassLoader(soa, self, descriptor, hash, h_parent, result)) { |
| 3020 | return false; // One of the parents is not supported. |
| 3021 | } |
| 3022 | if (*result != nullptr) { |
| 3023 | return true; // Found the class up the chain. |
| 3024 | } |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 3025 | |
Nicolas Geoffray | 80a560c | 2018-10-26 13:48:51 +0100 | [diff] [blame] | 3026 | if (!FindClassInSharedLibraries(soa, self, descriptor, hash, class_loader, result)) { |
| 3027 | return false; // One of the shared library loader is not supported. |
| 3028 | } |
| 3029 | if (*result != nullptr) { |
| 3030 | return true; // Found the class in a shared library. |
| 3031 | } |
| 3032 | |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 3033 | // Search the current class loader classpath. |
| 3034 | *result = FindClassInBaseDexClassLoaderClassPath(soa, descriptor, hash, class_loader); |
Andreas Gampe | 501c3b0 | 2019-04-17 21:54:27 +0000 | [diff] [blame] | 3035 | return !soa.Self()->IsExceptionPending(); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 3036 | } |
| 3037 | |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 3038 | if (IsDelegateLastClassLoader(soa, class_loader)) { |
| 3039 | // For delegate last, the search order is: |
| 3040 | // - boot class path |
Nicolas Geoffray | 80a560c | 2018-10-26 13:48:51 +0100 | [diff] [blame] | 3041 | // - shared libraries |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 3042 | // - class loader dex files |
| 3043 | // - parent |
| 3044 | *result = FindClassInBootClassLoaderClassPath(self, descriptor, hash); |
| 3045 | if (*result != nullptr) { |
| 3046 | return true; // The class is part of the boot class path. |
| 3047 | } |
Andreas Gampe | 501c3b0 | 2019-04-17 21:54:27 +0000 | [diff] [blame] | 3048 | if (self->IsExceptionPending()) { |
| 3049 | // Pending exception means there was an error other than ClassNotFound that must be returned |
| 3050 | // to the caller. |
| 3051 | return false; |
| 3052 | } |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 3053 | |
Nicolas Geoffray | 80a560c | 2018-10-26 13:48:51 +0100 | [diff] [blame] | 3054 | if (!FindClassInSharedLibraries(soa, self, descriptor, hash, class_loader, result)) { |
| 3055 | return false; // One of the shared library loader is not supported. |
| 3056 | } |
| 3057 | if (*result != nullptr) { |
| 3058 | return true; // Found the class in a shared library. |
| 3059 | } |
| 3060 | |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 3061 | *result = FindClassInBaseDexClassLoaderClassPath(soa, descriptor, hash, class_loader); |
| 3062 | if (*result != nullptr) { |
| 3063 | return true; // Found the class in the current class loader |
| 3064 | } |
Andreas Gampe | 501c3b0 | 2019-04-17 21:54:27 +0000 | [diff] [blame] | 3065 | if (self->IsExceptionPending()) { |
| 3066 | // Pending exception means there was an error other than ClassNotFound that must be returned |
| 3067 | // to the caller. |
| 3068 | return false; |
| 3069 | } |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 3070 | |
| 3071 | // Handles as RegisterDexFile may allocate dex caches (and cause thread suspension). |
| 3072 | StackHandleScope<1> hs(self); |
| 3073 | Handle<mirror::ClassLoader> h_parent(hs.NewHandle(class_loader->GetParent())); |
| 3074 | return FindClassInBaseDexClassLoader(soa, self, descriptor, hash, h_parent, result); |
| 3075 | } |
| 3076 | |
| 3077 | // Unsupported class loader. |
| 3078 | *result = nullptr; |
| 3079 | return false; |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 3080 | } |
| 3081 | |
Andreas Gampe | 501c3b0 | 2019-04-17 21:54:27 +0000 | [diff] [blame] | 3082 | namespace { |
| 3083 | |
| 3084 | // Matches exceptions caught in DexFile.defineClass. |
| 3085 | ALWAYS_INLINE bool MatchesDexFileCaughtExceptions(ObjPtr<mirror::Throwable> throwable, |
| 3086 | ClassLinker* class_linker) |
| 3087 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 3088 | return |
| 3089 | // ClassNotFoundException. |
| 3090 | throwable->InstanceOf(GetClassRoot(ClassRoot::kJavaLangClassNotFoundException, |
| 3091 | class_linker)) |
| 3092 | || |
| 3093 | // NoClassDefFoundError. TODO: Reconsider this. b/130746382. |
| 3094 | throwable->InstanceOf(Runtime::Current()->GetPreAllocatedNoClassDefFoundError()->GetClass()); |
| 3095 | } |
| 3096 | |
| 3097 | // Clear exceptions caught in DexFile.defineClass. |
| 3098 | ALWAYS_INLINE void FilterDexFileCaughtExceptions(Thread* self, ClassLinker* class_linker) |
| 3099 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 3100 | if (MatchesDexFileCaughtExceptions(self->GetException(), class_linker)) { |
| 3101 | self->ClearException(); |
| 3102 | } |
| 3103 | } |
| 3104 | |
| 3105 | } // namespace |
| 3106 | |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 3107 | // Finds the class in the boot class loader. |
| 3108 | // If the class is found the method returns the resolved class. Otherwise it returns null. |
| 3109 | ObjPtr<mirror::Class> ClassLinker::FindClassInBootClassLoaderClassPath(Thread* self, |
| 3110 | const char* descriptor, |
| 3111 | size_t hash) { |
| 3112 | ObjPtr<mirror::Class> result = nullptr; |
| 3113 | ClassPathEntry pair = FindInClassPath(descriptor, hash, boot_class_path_); |
| 3114 | if (pair.second != nullptr) { |
| 3115 | ObjPtr<mirror::Class> klass = LookupClass(self, descriptor, hash, nullptr); |
| 3116 | if (klass != nullptr) { |
| 3117 | result = EnsureResolved(self, descriptor, klass); |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 3118 | } else { |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 3119 | result = DefineClass(self, |
| 3120 | descriptor, |
| 3121 | hash, |
| 3122 | ScopedNullHandle<mirror::ClassLoader>(), |
| 3123 | *pair.first, |
| 3124 | *pair.second); |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 3125 | } |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 3126 | if (result == nullptr) { |
| 3127 | CHECK(self->IsExceptionPending()) << descriptor; |
Andreas Gampe | 501c3b0 | 2019-04-17 21:54:27 +0000 | [diff] [blame] | 3128 | FilterDexFileCaughtExceptions(self, this); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 3129 | } |
| 3130 | } |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 3131 | return result; |
| 3132 | } |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 3133 | |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 3134 | ObjPtr<mirror::Class> ClassLinker::FindClassInBaseDexClassLoaderClassPath( |
| 3135 | ScopedObjectAccessAlreadyRunnable& soa, |
| 3136 | const char* descriptor, |
| 3137 | size_t hash, |
| 3138 | Handle<mirror::ClassLoader> class_loader) { |
David Brazdil | 05909d8 | 2018-12-06 16:25:16 +0000 | [diff] [blame] | 3139 | DCHECK(IsPathOrDexClassLoader(soa, class_loader) || |
| 3140 | IsInMemoryDexClassLoader(soa, class_loader) || |
| 3141 | IsDelegateLastClassLoader(soa, class_loader)) |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 3142 | << "Unexpected class loader for descriptor " << descriptor; |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 3143 | |
Andreas Gampe | b8e7c37 | 2018-02-20 18:24:55 -0800 | [diff] [blame] | 3144 | ObjPtr<mirror::Class> ret; |
| 3145 | auto define_class = [&](const DexFile* cp_dex_file) REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 3146 | const dex::ClassDef* dex_class_def = OatDexFile::FindClassDef(*cp_dex_file, descriptor, hash); |
Andreas Gampe | b8e7c37 | 2018-02-20 18:24:55 -0800 | [diff] [blame] | 3147 | if (dex_class_def != nullptr) { |
| 3148 | ObjPtr<mirror::Class> klass = DefineClass(soa.Self(), |
| 3149 | descriptor, |
| 3150 | hash, |
| 3151 | class_loader, |
| 3152 | *cp_dex_file, |
| 3153 | *dex_class_def); |
| 3154 | if (klass == nullptr) { |
| 3155 | CHECK(soa.Self()->IsExceptionPending()) << descriptor; |
Andreas Gampe | 501c3b0 | 2019-04-17 21:54:27 +0000 | [diff] [blame] | 3156 | FilterDexFileCaughtExceptions(soa.Self(), this); |
Andreas Gampe | b8e7c37 | 2018-02-20 18:24:55 -0800 | [diff] [blame] | 3157 | // TODO: Is it really right to break here, and not check the other dex files? |
Andreas Gampe | 501c3b0 | 2019-04-17 21:54:27 +0000 | [diff] [blame] | 3158 | } else { |
| 3159 | DCHECK(!soa.Self()->IsExceptionPending()); |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 3160 | } |
Andreas Gampe | b8e7c37 | 2018-02-20 18:24:55 -0800 | [diff] [blame] | 3161 | ret = klass; |
| 3162 | return false; // Found a Class (or error == nullptr), stop visit. |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 3163 | } |
Andreas Gampe | b8e7c37 | 2018-02-20 18:24:55 -0800 | [diff] [blame] | 3164 | return true; // Continue with the next DexFile. |
| 3165 | }; |
| 3166 | |
| 3167 | VisitClassLoaderDexFiles(soa, class_loader, define_class); |
| 3168 | return ret; |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 3169 | } |
| 3170 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 3171 | ObjPtr<mirror::Class> ClassLinker::FindClass(Thread* self, |
| 3172 | const char* descriptor, |
| 3173 | Handle<mirror::ClassLoader> class_loader) { |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 3174 | DCHECK_NE(*descriptor, '\0') << "descriptor is empty string"; |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 3175 | DCHECK(self != nullptr); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3176 | self->AssertNoPendingException(); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 3177 | self->PoisonObjectPointers(); // For DefineClass, CreateArrayClass, etc... |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 3178 | if (descriptor[1] == '\0') { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3179 | // only the descriptors of primitive types should be 1 character long, also avoid class lookup |
| 3180 | // for primitive classes that aren't backed by dex files. |
| 3181 | return FindPrimitiveClass(descriptor[0]); |
| 3182 | } |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 3183 | const size_t hash = ComputeModifiedUtf8Hash(descriptor); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3184 | // Find the class in the loaded classes table. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3185 | ObjPtr<mirror::Class> klass = LookupClass(self, descriptor, hash, class_loader.Get()); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 3186 | if (klass != nullptr) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3187 | return EnsureResolved(self, descriptor, klass); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3188 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3189 | // Class is not yet loaded. |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3190 | if (descriptor[0] != '[' && class_loader == nullptr) { |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 3191 | // 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] | 3192 | ClassPathEntry pair = FindInClassPath(descriptor, hash, boot_class_path_); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 3193 | if (pair.second != nullptr) { |
Mathieu Chartier | 9865bde | 2015-12-21 09:58:16 -0800 | [diff] [blame] | 3194 | return DefineClass(self, |
| 3195 | descriptor, |
| 3196 | hash, |
| 3197 | ScopedNullHandle<mirror::ClassLoader>(), |
| 3198 | *pair.first, |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3199 | *pair.second); |
Ian Rogers | 6355745 | 2014-06-04 16:57:15 -0700 | [diff] [blame] | 3200 | } else { |
| 3201 | // The boot class loader is searched ahead of the application class loader, failures are |
| 3202 | // expected and will be wrapped in a ClassNotFoundException. Use the pre-allocated error to |
| 3203 | // trigger the chaining with a proper stack trace. |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 3204 | ObjPtr<mirror::Throwable> pre_allocated = |
| 3205 | Runtime::Current()->GetPreAllocatedNoClassDefFoundError(); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 3206 | self->SetException(pre_allocated); |
Ian Rogers | 6355745 | 2014-06-04 16:57:15 -0700 | [diff] [blame] | 3207 | return nullptr; |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 3208 | } |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 3209 | } |
| 3210 | ObjPtr<mirror::Class> result_ptr; |
| 3211 | bool descriptor_equals; |
| 3212 | if (descriptor[0] == '[') { |
| 3213 | result_ptr = CreateArrayClass(self, descriptor, hash, class_loader); |
| 3214 | DCHECK_EQ(result_ptr == nullptr, self->IsExceptionPending()); |
| 3215 | DCHECK(result_ptr == nullptr || result_ptr->DescriptorEquals(descriptor)); |
| 3216 | descriptor_equals = true; |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 3217 | } else { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 3218 | ScopedObjectAccessUnchecked soa(self); |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 3219 | bool known_hierarchy = |
| 3220 | FindClassInBaseDexClassLoader(soa, self, descriptor, hash, class_loader, &result_ptr); |
| 3221 | if (result_ptr != nullptr) { |
| 3222 | // The chain was understood and we found the class. We still need to add the class to |
| 3223 | // the class table to protect from racy programs that can try and redefine the path list |
| 3224 | // which would change the Class<?> returned for subsequent evaluation of const-class. |
| 3225 | DCHECK(known_hierarchy); |
| 3226 | DCHECK(result_ptr->DescriptorEquals(descriptor)); |
| 3227 | descriptor_equals = true; |
Andreas Gampe | 501c3b0 | 2019-04-17 21:54:27 +0000 | [diff] [blame] | 3228 | } else if (!self->IsExceptionPending()) { |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 3229 | // Either the chain wasn't understood or the class wasn't found. |
Andreas Gampe | 501c3b0 | 2019-04-17 21:54:27 +0000 | [diff] [blame] | 3230 | // If there is a pending exception we didn't clear, it is a not a ClassNotFoundException and |
| 3231 | // we should return it instead of silently clearing and retrying. |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 3232 | // |
| 3233 | // If the chain was understood but we did not find the class, let the Java-side |
| 3234 | // rediscover all this and throw the exception with the right stack trace. Note that |
| 3235 | // the Java-side could still succeed for racy programs if another thread is actively |
| 3236 | // modifying the class loader's path list. |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 3237 | |
Alex Light | 185a461 | 2018-10-04 15:54:25 -0700 | [diff] [blame] | 3238 | // The runtime is not allowed to call into java from a runtime-thread so just abort. |
Alex Light | e9f6103 | 2018-09-24 16:04:51 -0700 | [diff] [blame] | 3239 | if (self->IsRuntimeThread()) { |
Calin Juravle | ccd5695 | 2016-12-15 17:57:38 +0000 | [diff] [blame] | 3240 | // Oops, we can't call into java so we can't run actual class-loader code. |
| 3241 | // This is true for e.g. for the compiler (jit or aot). |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 3242 | ObjPtr<mirror::Throwable> pre_allocated = |
| 3243 | Runtime::Current()->GetPreAllocatedNoClassDefFoundError(); |
| 3244 | self->SetException(pre_allocated); |
Vladimir Marko | 2c8c6b6 | 2016-12-01 17:42:00 +0000 | [diff] [blame] | 3245 | return nullptr; |
| 3246 | } |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 3247 | |
Vladimir Marko | 5fdd778 | 2017-04-20 11:26:03 +0100 | [diff] [blame] | 3248 | // Inlined DescriptorToDot(descriptor) with extra validation. |
| 3249 | // |
| 3250 | // Throw NoClassDefFoundError early rather than potentially load a class only to fail |
| 3251 | // the DescriptorEquals() check below and give a confusing error message. For example, |
| 3252 | // when native code erroneously calls JNI GetFieldId() with signature "java/lang/String" |
| 3253 | // instead of "Ljava/lang/String;", the message below using the "dot" names would be |
| 3254 | // "class loader [...] returned class java.lang.String instead of java.lang.String". |
| 3255 | size_t descriptor_length = strlen(descriptor); |
| 3256 | if (UNLIKELY(descriptor[0] != 'L') || |
| 3257 | UNLIKELY(descriptor[descriptor_length - 1] != ';') || |
| 3258 | UNLIKELY(memchr(descriptor + 1, '.', descriptor_length - 2) != nullptr)) { |
| 3259 | ThrowNoClassDefFoundError("Invalid descriptor: %s.", descriptor); |
| 3260 | return nullptr; |
| 3261 | } |
Andreas Gampe | 501c3b0 | 2019-04-17 21:54:27 +0000 | [diff] [blame] | 3262 | |
Vladimir Marko | 5fdd778 | 2017-04-20 11:26:03 +0100 | [diff] [blame] | 3263 | std::string class_name_string(descriptor + 1, descriptor_length - 2); |
| 3264 | std::replace(class_name_string.begin(), class_name_string.end(), '/', '.'); |
Andreas Gampe | 87658f3 | 2019-04-18 18:39:02 +0000 | [diff] [blame] | 3265 | if (known_hierarchy && |
| 3266 | fast_class_not_found_exceptions_ && |
| 3267 | !Runtime::Current()->IsJavaDebuggable()) { |
| 3268 | // For known hierarchy, we know that the class is going to throw an exception. If we aren't |
| 3269 | // debuggable, optimize this path by throwing directly here without going back to Java |
| 3270 | // language. This reduces how many ClassNotFoundExceptions happen. |
| 3271 | self->ThrowNewExceptionF("Ljava/lang/ClassNotFoundException;", |
| 3272 | "%s", |
| 3273 | class_name_string.c_str()); |
| 3274 | } else { |
| 3275 | ScopedLocalRef<jobject> class_loader_object( |
| 3276 | soa.Env(), soa.AddLocalReference<jobject>(class_loader.Get())); |
| 3277 | ScopedLocalRef<jobject> result(soa.Env(), nullptr); |
| 3278 | { |
| 3279 | ScopedThreadStateChange tsc(self, kNative); |
| 3280 | ScopedLocalRef<jobject> class_name_object( |
| 3281 | soa.Env(), soa.Env()->NewStringUTF(class_name_string.c_str())); |
| 3282 | if (class_name_object.get() == nullptr) { |
| 3283 | DCHECK(self->IsExceptionPending()); // OOME. |
| 3284 | return nullptr; |
| 3285 | } |
| 3286 | CHECK(class_loader_object.get() != nullptr); |
| 3287 | result.reset(soa.Env()->CallObjectMethod(class_loader_object.get(), |
| 3288 | WellKnownClasses::java_lang_ClassLoader_loadClass, |
| 3289 | class_name_object.get())); |
| 3290 | } |
| 3291 | if (result.get() == nullptr && !self->IsExceptionPending()) { |
| 3292 | // broken loader - throw NPE to be compatible with Dalvik |
| 3293 | ThrowNullPointerException(StringPrintf("ClassLoader.loadClass returned null for %s", |
| 3294 | class_name_string.c_str()).c_str()); |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 3295 | return nullptr; |
| 3296 | } |
Andreas Gampe | 87658f3 | 2019-04-18 18:39:02 +0000 | [diff] [blame] | 3297 | result_ptr = soa.Decode<mirror::Class>(result.get()); |
| 3298 | // Check the name of the returned class. |
| 3299 | descriptor_equals = (result_ptr != nullptr) && result_ptr->DescriptorEquals(descriptor); |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 3300 | } |
Andreas Gampe | 501c3b0 | 2019-04-17 21:54:27 +0000 | [diff] [blame] | 3301 | } else { |
| 3302 | DCHECK(!MatchesDexFileCaughtExceptions(self->GetException(), this)); |
Vladimir Marko | 2c8c6b6 | 2016-12-01 17:42:00 +0000 | [diff] [blame] | 3303 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3304 | } |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 3305 | |
| 3306 | if (self->IsExceptionPending()) { |
| 3307 | // If the ClassLoader threw or array class allocation failed, pass that exception up. |
| 3308 | // However, to comply with the RI behavior, first check if another thread succeeded. |
| 3309 | result_ptr = LookupClass(self, descriptor, hash, class_loader.Get()); |
| 3310 | if (result_ptr != nullptr && !result_ptr->IsErroneous()) { |
| 3311 | self->ClearException(); |
| 3312 | return EnsureResolved(self, descriptor, result_ptr); |
| 3313 | } |
| 3314 | return nullptr; |
| 3315 | } |
| 3316 | |
| 3317 | // Try to insert the class to the class table, checking for mismatch. |
| 3318 | ObjPtr<mirror::Class> old; |
| 3319 | { |
| 3320 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 3321 | ClassTable* const class_table = InsertClassTableForClassLoader(class_loader.Get()); |
| 3322 | old = class_table->Lookup(descriptor, hash); |
| 3323 | if (old == nullptr) { |
| 3324 | old = result_ptr; // For the comparison below, after releasing the lock. |
| 3325 | if (descriptor_equals) { |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 3326 | class_table->InsertWithHash(result_ptr, hash); |
Mathieu Chartier | 88ea61e | 2018-06-20 17:45:41 -0700 | [diff] [blame] | 3327 | WriteBarrier::ForEveryFieldWrite(class_loader.Get()); |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 3328 | } // else throw below, after releasing the lock. |
| 3329 | } |
| 3330 | } |
| 3331 | if (UNLIKELY(old != result_ptr)) { |
| 3332 | // Return `old` (even if `!descriptor_equals`) to mimic the RI behavior for parallel |
| 3333 | // capable class loaders. (All class loaders are considered parallel capable on Android.) |
Vladimir Marko | dfc0de7 | 2019-04-01 10:57:55 +0100 | [diff] [blame] | 3334 | ObjPtr<mirror::Class> loader_class = class_loader->GetClass(); |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 3335 | const char* loader_class_name = |
| 3336 | loader_class->GetDexFile().StringByTypeIdx(loader_class->GetDexTypeIndex()); |
| 3337 | LOG(WARNING) << "Initiating class loader of type " << DescriptorToDot(loader_class_name) |
| 3338 | << " is not well-behaved; it returned a different Class for racing loadClass(\"" |
| 3339 | << DescriptorToDot(descriptor) << "\")."; |
| 3340 | return EnsureResolved(self, descriptor, old); |
| 3341 | } |
| 3342 | if (UNLIKELY(!descriptor_equals)) { |
| 3343 | std::string result_storage; |
| 3344 | const char* result_name = result_ptr->GetDescriptor(&result_storage); |
| 3345 | std::string loader_storage; |
| 3346 | const char* loader_class_name = class_loader->GetClass()->GetDescriptor(&loader_storage); |
| 3347 | ThrowNoClassDefFoundError( |
| 3348 | "Initiating class loader of type %s returned class %s instead of %s.", |
| 3349 | DescriptorToDot(loader_class_name).c_str(), |
| 3350 | DescriptorToDot(result_name).c_str(), |
| 3351 | DescriptorToDot(descriptor).c_str()); |
| 3352 | return nullptr; |
| 3353 | } |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 3354 | // Success. |
| 3355 | return result_ptr; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3356 | } |
| 3357 | |
Vladimir Marko | b9c29f6 | 2019-03-20 14:22:51 +0000 | [diff] [blame] | 3358 | static bool IsReservedBootClassPathDescriptor(const char* descriptor) { |
| 3359 | std::string_view descriptor_sv(descriptor); |
Vladimir Marko | 29885b3 | 2019-04-03 10:09:26 +0100 | [diff] [blame] | 3360 | return |
| 3361 | // Reserved conscrypt packages (includes sub-packages under these paths). |
| 3362 | StartsWith(descriptor_sv, "Landroid/net/ssl/") || |
| 3363 | StartsWith(descriptor_sv, "Lcom/android/org/conscrypt/") || |
| 3364 | // Reserved updatable-media package (includes sub-packages under this path). |
| 3365 | StartsWith(descriptor_sv, "Landroid/media/"); |
Vladimir Marko | b9c29f6 | 2019-03-20 14:22:51 +0000 | [diff] [blame] | 3366 | } |
| 3367 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 3368 | ObjPtr<mirror::Class> ClassLinker::DefineClass(Thread* self, |
| 3369 | const char* descriptor, |
| 3370 | size_t hash, |
| 3371 | Handle<mirror::ClassLoader> class_loader, |
| 3372 | const DexFile& dex_file, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 3373 | const dex::ClassDef& dex_class_def) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3374 | StackHandleScope<3> hs(self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3375 | auto klass = hs.NewHandle<mirror::Class>(nullptr); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3376 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3377 | // Load the class from the dex file. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3378 | if (UNLIKELY(!init_done_)) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3379 | // finish up init of hand crafted class_roots_ |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3380 | if (strcmp(descriptor, "Ljava/lang/Object;") == 0) { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 3381 | klass.Assign(GetClassRoot<mirror::Object>(this)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3382 | } else if (strcmp(descriptor, "Ljava/lang/Class;") == 0) { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 3383 | klass.Assign(GetClassRoot<mirror::Class>(this)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3384 | } else if (strcmp(descriptor, "Ljava/lang/String;") == 0) { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 3385 | klass.Assign(GetClassRoot<mirror::String>(this)); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 3386 | } else if (strcmp(descriptor, "Ljava/lang/ref/Reference;") == 0) { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 3387 | klass.Assign(GetClassRoot<mirror::Reference>(this)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3388 | } else if (strcmp(descriptor, "Ljava/lang/DexCache;") == 0) { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 3389 | klass.Assign(GetClassRoot<mirror::DexCache>(this)); |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 3390 | } else if (strcmp(descriptor, "Ldalvik/system/ClassExt;") == 0) { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 3391 | klass.Assign(GetClassRoot<mirror::ClassExt>(this)); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3392 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3393 | } |
| 3394 | |
Vladimir Marko | b9c29f6 | 2019-03-20 14:22:51 +0000 | [diff] [blame] | 3395 | // For AOT-compilation of an app, we may use a shortened boot class path that excludes |
| 3396 | // some runtime modules. Prevent definition of classes in app class loader that could clash |
| 3397 | // with these modules as these classes could be resolved differently during execution. |
| 3398 | if (class_loader != nullptr && |
| 3399 | Runtime::Current()->IsAotCompiler() && |
| 3400 | IsReservedBootClassPathDescriptor(descriptor)) { |
| 3401 | ObjPtr<mirror::Throwable> pre_allocated = |
| 3402 | Runtime::Current()->GetPreAllocatedNoClassDefFoundError(); |
| 3403 | self->SetException(pre_allocated); |
| 3404 | return nullptr; |
| 3405 | } |
| 3406 | |
Alex Light | e9f6103 | 2018-09-24 16:04:51 -0700 | [diff] [blame] | 3407 | // This is to prevent the calls to ClassLoad and ClassPrepare which can cause java/user-supplied |
| 3408 | // code to be executed. We put it up here so we can avoid all the allocations associated with |
| 3409 | // creating the class. This can happen with (eg) jit threads. |
| 3410 | if (!self->CanLoadClasses()) { |
| 3411 | // Make sure we don't try to load anything, potentially causing an infinite loop. |
| 3412 | ObjPtr<mirror::Throwable> pre_allocated = |
| 3413 | Runtime::Current()->GetPreAllocatedNoClassDefFoundError(); |
| 3414 | self->SetException(pre_allocated); |
| 3415 | return nullptr; |
| 3416 | } |
| 3417 | |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3418 | if (klass == nullptr) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3419 | // Allocate a class with the status of not ready. |
| 3420 | // Interface object should get the right size here. Regular class will |
| 3421 | // figure out the right size later and be replaced with one of the right |
| 3422 | // size when the class becomes resolved. |
Chang Xing | 0c2c222 | 2017-08-04 14:36:17 -0700 | [diff] [blame] | 3423 | if (CanAllocClass()) { |
| 3424 | klass.Assign(AllocClass(self, SizeOfClassWithoutEmbeddedTables(dex_file, dex_class_def))); |
| 3425 | } else { |
| 3426 | return nullptr; |
| 3427 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3428 | } |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3429 | if (UNLIKELY(klass == nullptr)) { |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3430 | self->AssertPendingOOMException(); |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 3431 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3432 | } |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 3433 | // Get the real dex file. This will return the input if there aren't any callbacks or they do |
| 3434 | // nothing. |
| 3435 | DexFile const* new_dex_file = nullptr; |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 3436 | dex::ClassDef const* new_class_def = nullptr; |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 3437 | // TODO We should ideally figure out some way to move this after we get a lock on the klass so it |
| 3438 | // will only be called once. |
| 3439 | Runtime::Current()->GetRuntimeCallbacks()->ClassPreDefine(descriptor, |
| 3440 | klass, |
| 3441 | class_loader, |
| 3442 | dex_file, |
| 3443 | dex_class_def, |
| 3444 | &new_dex_file, |
| 3445 | &new_class_def); |
Alex Light | 440b5d9 | 2017-01-24 15:32:25 -0800 | [diff] [blame] | 3446 | // Check to see if an exception happened during runtime callbacks. Return if so. |
| 3447 | if (self->IsExceptionPending()) { |
| 3448 | return nullptr; |
| 3449 | } |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 3450 | ObjPtr<mirror::DexCache> dex_cache = RegisterDexFile(*new_dex_file, class_loader.Get()); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3451 | if (dex_cache == nullptr) { |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3452 | self->AssertPendingException(); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3453 | return nullptr; |
| 3454 | } |
| 3455 | klass->SetDexCache(dex_cache); |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 3456 | SetupClass(*new_dex_file, *new_class_def, klass, class_loader.Get()); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3457 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 3458 | // Mark the string class by setting its access flag. |
| 3459 | if (UNLIKELY(!init_done_)) { |
| 3460 | if (strcmp(descriptor, "Ljava/lang/String;") == 0) { |
| 3461 | klass->SetStringClass(); |
| 3462 | } |
| 3463 | } |
| 3464 | |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 3465 | ObjectLock<mirror::Class> lock(self, klass); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3466 | klass->SetClinitThreadId(self->GetTid()); |
Mathieu Chartier | 1e4841e | 2016-12-15 14:21:04 -0800 | [diff] [blame] | 3467 | // 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] | 3468 | klass->SetIfTable(GetClassRoot<mirror::Object>(this)->GetIfTable()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3469 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3470 | // Add the newly loaded class to the loaded classes table. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3471 | ObjPtr<mirror::Class> existing = InsertClass(descriptor, klass.Get(), hash); |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 3472 | if (existing != nullptr) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3473 | // We failed to insert because we raced with another thread. Calling EnsureResolved may cause |
| 3474 | // this thread to block. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3475 | return EnsureResolved(self, descriptor, existing); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3476 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3477 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3478 | // Load the fields and other things after we are inserted in the table. This is so that we don't |
| 3479 | // end up allocating unfree-able linear alloc resources and then lose the race condition. The |
| 3480 | // other reason is that the field roots are only visited from the class table. So we need to be |
| 3481 | // inserted before we allocate / fill in these fields. |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 3482 | LoadClass(self, *new_dex_file, *new_class_def, klass); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3483 | if (self->IsExceptionPending()) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 3484 | VLOG(class_linker) << self->GetException()->Dump(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3485 | // An exception occured during load, set status to erroneous while holding klass' lock in case |
| 3486 | // notification is necessary. |
| 3487 | if (!klass->IsErroneous()) { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 3488 | mirror::Class::SetStatus(klass, ClassStatus::kErrorUnresolved, self); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3489 | } |
| 3490 | return nullptr; |
| 3491 | } |
| 3492 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3493 | // Finish loading (if necessary) by finding parents |
| 3494 | CHECK(!klass->IsLoaded()); |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 3495 | if (!LoadSuperAndInterfaces(klass, *new_dex_file)) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3496 | // Loading failed. |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 3497 | if (!klass->IsErroneous()) { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 3498 | mirror::Class::SetStatus(klass, ClassStatus::kErrorUnresolved, self); |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 3499 | } |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 3500 | return nullptr; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3501 | } |
| 3502 | CHECK(klass->IsLoaded()); |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 3503 | |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 3504 | // At this point the class is loaded. Publish a ClassLoad event. |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 3505 | // 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] | 3506 | Runtime::Current()->GetRuntimeCallbacks()->ClassLoad(klass); |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 3507 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3508 | // Link the class (if necessary) |
| 3509 | CHECK(!klass->IsResolved()); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3510 | // TODO: Use fast jobjects? |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3511 | auto interfaces = hs.NewHandle<mirror::ObjectArray<mirror::Class>>(nullptr); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3512 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 3513 | MutableHandle<mirror::Class> h_new_class = hs.NewHandle<mirror::Class>(nullptr); |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 3514 | if (!LinkClass(self, descriptor, klass, interfaces, &h_new_class)) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3515 | // Linking failed. |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 3516 | if (!klass->IsErroneous()) { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 3517 | mirror::Class::SetStatus(klass, ClassStatus::kErrorUnresolved, self); |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 3518 | } |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 3519 | return nullptr; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3520 | } |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 3521 | self->AssertNoPendingException(); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3522 | CHECK(h_new_class != nullptr) << descriptor; |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 3523 | CHECK(h_new_class->IsResolved() && !h_new_class->IsErroneousResolved()) << descriptor; |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 3524 | |
Sebastien Hertz | a8a697f | 2015-01-15 12:28:47 +0100 | [diff] [blame] | 3525 | // Instrumentation may have updated entrypoints for all methods of all |
| 3526 | // classes. However it could not update methods of this class while we |
| 3527 | // were loading it. Now the class is resolved, we can update entrypoints |
| 3528 | // as required by instrumentation. |
| 3529 | if (Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled()) { |
| 3530 | // We must be in the kRunnable state to prevent instrumentation from |
| 3531 | // suspending all threads to update entrypoints while we are doing it |
| 3532 | // for this class. |
| 3533 | DCHECK_EQ(self->GetState(), kRunnable); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 3534 | Runtime::Current()->GetInstrumentation()->InstallStubsForClass(h_new_class.Get()); |
Sebastien Hertz | a8a697f | 2015-01-15 12:28:47 +0100 | [diff] [blame] | 3535 | } |
| 3536 | |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 3537 | /* |
| 3538 | * We send CLASS_PREPARE events to the debugger from here. The |
| 3539 | * definition of "preparation" is creating the static fields for a |
| 3540 | * class and initializing them to the standard default values, but not |
| 3541 | * executing any code (that comes later, during "initialization"). |
| 3542 | * |
| 3543 | * We did the static preparation in LinkClass. |
| 3544 | * |
| 3545 | * The class has been prepared and resolved but possibly not yet verified |
| 3546 | * at this point. |
| 3547 | */ |
Andreas Gampe | ac30fa2 | 2017-01-18 21:02:36 -0800 | [diff] [blame] | 3548 | Runtime::Current()->GetRuntimeCallbacks()->ClassPrepare(klass, h_new_class); |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 3549 | |
Tamas Berghammer | 160e6df | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 3550 | // Notify native debugger of the new class and its layout. |
| 3551 | jit::Jit::NewTypeLoadedIfUsingJit(h_new_class.Get()); |
| 3552 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 3553 | return h_new_class.Get(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3554 | } |
| 3555 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3556 | uint32_t ClassLinker::SizeOfClassWithoutEmbeddedTables(const DexFile& dex_file, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 3557 | const dex::ClassDef& dex_class_def) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 3558 | size_t num_ref = 0; |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 3559 | size_t num_8 = 0; |
| 3560 | size_t num_16 = 0; |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 3561 | size_t num_32 = 0; |
| 3562 | size_t num_64 = 0; |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3563 | ClassAccessor accessor(dex_file, dex_class_def); |
| 3564 | // We allow duplicate definitions of the same field in a class_data_item |
| 3565 | // but ignore the repeated indexes here, b/21868015. |
| 3566 | uint32_t last_field_idx = dex::kDexNoIndex; |
| 3567 | for (const ClassAccessor::Field& field : accessor.GetStaticFields()) { |
| 3568 | uint32_t field_idx = field.GetIndex(); |
| 3569 | // Ordering enforced by DexFileVerifier. |
| 3570 | DCHECK(last_field_idx == dex::kDexNoIndex || last_field_idx <= field_idx); |
| 3571 | if (UNLIKELY(field_idx == last_field_idx)) { |
| 3572 | continue; |
| 3573 | } |
| 3574 | last_field_idx = field_idx; |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 3575 | const dex::FieldId& field_id = dex_file.GetFieldId(field_idx); |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3576 | const char* descriptor = dex_file.GetFieldTypeDescriptor(field_id); |
| 3577 | char c = descriptor[0]; |
| 3578 | switch (c) { |
| 3579 | case 'L': |
| 3580 | case '[': |
| 3581 | num_ref++; |
| 3582 | break; |
| 3583 | case 'J': |
| 3584 | case 'D': |
| 3585 | num_64++; |
| 3586 | break; |
| 3587 | case 'I': |
| 3588 | case 'F': |
| 3589 | num_32++; |
| 3590 | break; |
| 3591 | case 'S': |
| 3592 | case 'C': |
| 3593 | num_16++; |
| 3594 | break; |
| 3595 | case 'B': |
| 3596 | case 'Z': |
| 3597 | num_8++; |
| 3598 | break; |
| 3599 | default: |
| 3600 | LOG(FATAL) << "Unknown descriptor: " << c; |
| 3601 | UNREACHABLE(); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 3602 | } |
| 3603 | } |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3604 | return mirror::Class::ComputeClassSize(false, |
| 3605 | 0, |
| 3606 | num_8, |
| 3607 | num_16, |
| 3608 | num_32, |
| 3609 | num_64, |
| 3610 | num_ref, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3611 | image_pointer_size_); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 3612 | } |
| 3613 | |
Alex Light | fc49fec | 2018-01-16 22:28:36 +0000 | [diff] [blame] | 3614 | // Special case to get oat code without overwriting a trampoline. |
| 3615 | const void* ClassLinker::GetQuickOatCodeFor(ArtMethod* method) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3616 | CHECK(method->IsInvokable()) << method->PrettyMethod(); |
Nicolas Geoffray | a7a4759 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 3617 | if (method->IsProxyMethod()) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 3618 | return GetQuickProxyInvokeHandler(); |
Jeff Hao | 8df6cea | 2013-07-29 13:54:48 -0700 | [diff] [blame] | 3619 | } |
Nicolas Geoffray | 3238440 | 2019-07-17 20:06:44 +0100 | [diff] [blame] | 3620 | const void* code = method->GetOatMethodQuickCode(GetImagePointerSize()); |
Alex Light | fc49fec | 2018-01-16 22:28:36 +0000 | [diff] [blame] | 3621 | if (code != nullptr) { |
| 3622 | return code; |
Mathieu Chartier | 2535abe | 2015-02-17 10:38:49 -0800 | [diff] [blame] | 3623 | } |
Nicolas Geoffray | 3238440 | 2019-07-17 20:06:44 +0100 | [diff] [blame] | 3624 | |
| 3625 | jit::Jit* jit = Runtime::Current()->GetJit(); |
| 3626 | if (jit != nullptr) { |
| 3627 | code = jit->GetCodeCache()->GetSavedEntryPointOfPreCompiledMethod(method); |
| 3628 | if (code != nullptr) { |
| 3629 | return code; |
| 3630 | } |
| 3631 | } |
| 3632 | |
Alex Light | fc49fec | 2018-01-16 22:28:36 +0000 | [diff] [blame] | 3633 | if (method->IsNative()) { |
| 3634 | // No code and native? Use generic trampoline. |
| 3635 | return GetQuickGenericJniStub(); |
| 3636 | } |
| 3637 | return GetQuickToInterpreterBridge(); |
TDYa127 | 8532191 | 2012-04-01 15:24:56 -0700 | [diff] [blame] | 3638 | } |
| 3639 | |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 3640 | bool ClassLinker::ShouldUseInterpreterEntrypoint(ArtMethod* method, const void* quick_code) { |
Alex Light | 2d441b1 | 2018-06-08 15:33:21 -0700 | [diff] [blame] | 3641 | ScopedAssertNoThreadSuspension sants(__FUNCTION__); |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 3642 | if (UNLIKELY(method->IsNative() || method->IsProxyMethod())) { |
| 3643 | return false; |
| 3644 | } |
| 3645 | |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 3646 | if (quick_code == nullptr) { |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 3647 | return true; |
| 3648 | } |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 3649 | |
| 3650 | Runtime* runtime = Runtime::Current(); |
| 3651 | instrumentation::Instrumentation* instr = runtime->GetInstrumentation(); |
| 3652 | if (instr->InterpretOnly()) { |
| 3653 | return true; |
| 3654 | } |
| 3655 | |
| 3656 | if (runtime->GetClassLinker()->IsQuickToInterpreterBridge(quick_code)) { |
| 3657 | // Doing this check avoids doing compiled/interpreter transitions. |
| 3658 | return true; |
| 3659 | } |
| 3660 | |
Alex Light | 3dacdd6 | 2019-03-12 15:45:47 +0000 | [diff] [blame] | 3661 | if (Thread::Current()->IsForceInterpreter() || |
| 3662 | Dbg::IsForcedInterpreterNeededForCalling(Thread::Current(), method)) { |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 3663 | // Force the use of interpreter when it is required by the debugger. |
| 3664 | return true; |
| 3665 | } |
| 3666 | |
Alex Light | 8f34aba | 2017-10-09 13:46:32 -0700 | [diff] [blame] | 3667 | if (Thread::Current()->IsAsyncExceptionPending()) { |
| 3668 | // Force use of interpreter to handle async-exceptions |
| 3669 | return true; |
| 3670 | } |
| 3671 | |
Alex Light | 2d441b1 | 2018-06-08 15:33:21 -0700 | [diff] [blame] | 3672 | if (quick_code == GetQuickInstrumentationEntryPoint()) { |
| 3673 | const void* instr_target = instr->GetCodeForInvoke(method); |
| 3674 | DCHECK_NE(instr_target, GetQuickInstrumentationEntryPoint()) << method->PrettyMethod(); |
| 3675 | return ShouldUseInterpreterEntrypoint(method, instr_target); |
| 3676 | } |
| 3677 | |
Nicolas Geoffray | 433b79a | 2017-01-30 20:54:45 +0000 | [diff] [blame] | 3678 | if (runtime->IsJavaDebuggable()) { |
| 3679 | // For simplicity, we ignore precompiled code and go to the interpreter |
| 3680 | // assuming we don't already have jitted code. |
| 3681 | // We could look at the oat file where `quick_code` is being defined, |
| 3682 | // and check whether it's been compiled debuggable, but we decided to |
| 3683 | // only rely on the JIT for debuggable apps. |
Alex Light | 6b16d89 | 2016-11-11 11:21:04 -0800 | [diff] [blame] | 3684 | jit::Jit* jit = Runtime::Current()->GetJit(); |
| 3685 | return (jit == nullptr) || !jit->GetCodeCache()->ContainsPc(quick_code); |
| 3686 | } |
| 3687 | |
Nicolas Geoffray | c9de61c | 2018-11-27 17:34:31 +0000 | [diff] [blame] | 3688 | if (runtime->IsNativeDebuggable()) { |
Calin Juravle | e5de54c | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 3689 | DCHECK(runtime->UseJitCompilation() && runtime->GetJit()->JitAtFirstUse()); |
David Srbecky | f448016 | 2016-03-16 00:06:24 +0000 | [diff] [blame] | 3690 | // If we are doing native debugging, ignore application's AOT code, |
Nicolas Geoffray | 433b79a | 2017-01-30 20:54:45 +0000 | [diff] [blame] | 3691 | // since we want to JIT it (at first use) with extra stackmaps for native |
| 3692 | // debugging. We keep however all AOT code from the boot image, |
| 3693 | // since the JIT-at-first-use is blocking and would result in non-negligible |
| 3694 | // startup performance impact. |
David Srbecky | f448016 | 2016-03-16 00:06:24 +0000 | [diff] [blame] | 3695 | return !runtime->GetHeap()->IsInBootImageOatFile(quick_code); |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 3696 | } |
| 3697 | |
| 3698 | return false; |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 3699 | } |
| 3700 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3701 | void ClassLinker::FixupStaticTrampolines(ObjPtr<mirror::Class> klass) { |
Alex Light | 2d441b1 | 2018-06-08 15:33:21 -0700 | [diff] [blame] | 3702 | ScopedAssertNoThreadSuspension sants(__FUNCTION__); |
Vladimir Marko | cce414f | 2019-10-07 08:51:33 +0100 | [diff] [blame] | 3703 | DCHECK(klass->IsVisiblyInitialized()) << klass->PrettyDescriptor(); |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 3704 | if (klass->NumDirectMethods() == 0) { |
| 3705 | return; // No direct methods => no static methods. |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3706 | } |
Vladimir Marko | cce414f | 2019-10-07 08:51:33 +0100 | [diff] [blame] | 3707 | if (UNLIKELY(klass->IsProxyClass())) { |
| 3708 | return; |
| 3709 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3710 | Runtime* runtime = Runtime::Current(); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 3711 | if (!runtime->IsStarted()) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 3712 | if (runtime->IsAotCompiler() || runtime->GetHeap()->HasBootImageSpace()) { |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 3713 | return; // OAT file unavailable. |
| 3714 | } |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3715 | } |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 3716 | |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 3717 | const DexFile& dex_file = klass->GetDexFile(); |
Mathieu Chartier | 18e2687 | 2018-06-04 17:19:02 -0700 | [diff] [blame] | 3718 | const uint16_t class_def_idx = klass->GetDexClassDefIndex(); |
| 3719 | CHECK_NE(class_def_idx, DexFile::kDexNoIndex16); |
| 3720 | ClassAccessor accessor(dex_file, class_def_idx); |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 3721 | // There should always be class data if there were direct methods. |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3722 | CHECK(accessor.HasClassData()) << klass->PrettyDescriptor(); |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 3723 | bool has_oat_class; |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3724 | OatFile::OatClass oat_class = OatFile::FindOatClass(dex_file, |
| 3725 | klass->GetDexClassDefIndex(), |
| 3726 | &has_oat_class); |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 3727 | // Link the code of methods skipped by LinkCode. |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3728 | for (size_t method_index = 0; method_index < accessor.NumDirectMethods(); ++method_index) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3729 | ArtMethod* method = klass->GetDirectMethod(method_index, image_pointer_size_); |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 3730 | if (!method->IsStatic()) { |
| 3731 | // Only update static methods. |
| 3732 | continue; |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3733 | } |
Nicolas Geoffray | 5a0b672 | 2019-09-24 15:09:40 +0100 | [diff] [blame] | 3734 | if (!IsQuickResolutionStub(method->GetEntryPointFromQuickCompiledCode())) { |
| 3735 | // Only update methods whose entrypoint is the resolution stub. |
| 3736 | continue; |
| 3737 | } |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 3738 | const void* quick_code = nullptr; |
| 3739 | if (has_oat_class) { |
| 3740 | OatFile::OatMethod oat_method = oat_class.GetOatMethod(method_index); |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 3741 | quick_code = oat_method.GetQuickCode(); |
| 3742 | } |
Nicolas Geoffray | 7989ac9 | 2019-04-10 12:42:30 +0100 | [diff] [blame] | 3743 | // Check if we have JIT compiled code for it. |
Vladimir Marko | cce414f | 2019-10-07 08:51:33 +0100 | [diff] [blame] | 3744 | jit::Jit* jit = runtime->GetJit(); |
Nicolas Geoffray | 3238440 | 2019-07-17 20:06:44 +0100 | [diff] [blame] | 3745 | if (quick_code == nullptr && jit != nullptr) { |
| 3746 | quick_code = jit->GetCodeCache()->GetSavedEntryPointOfPreCompiledMethod(method); |
Nicolas Geoffray | 7989ac9 | 2019-04-10 12:42:30 +0100 | [diff] [blame] | 3747 | } |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 3748 | // Check whether the method is native, in which case it's generic JNI. |
| 3749 | if (quick_code == nullptr && method->IsNative()) { |
| 3750 | quick_code = GetQuickGenericJniStub(); |
| 3751 | } else if (ShouldUseInterpreterEntrypoint(method, quick_code)) { |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 3752 | // Use interpreter entry point. |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 3753 | quick_code = GetQuickToInterpreterBridge(); |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 3754 | } |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 3755 | runtime->GetInstrumentation()->UpdateMethodsCode(method, quick_code); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3756 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3757 | // Ignore virtual methods on the iterator. |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3758 | } |
| 3759 | |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3760 | // Does anything needed to make sure that the compiler will not generate a direct invoke to this |
| 3761 | // method. Should only be called on non-invokable methods. |
| 3762 | inline void EnsureThrowsInvocationError(ClassLinker* class_linker, ArtMethod* method) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 3763 | DCHECK(method != nullptr); |
| 3764 | DCHECK(!method->IsInvokable()); |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3765 | method->SetEntryPointFromQuickCompiledCodePtrSize( |
| 3766 | class_linker->GetQuickToInterpreterBridgeTrampoline(), |
| 3767 | class_linker->GetImagePointerSize()); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 3768 | } |
| 3769 | |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3770 | static void LinkCode(ClassLinker* class_linker, |
| 3771 | ArtMethod* method, |
| 3772 | const OatFile::OatClass* oat_class, |
| 3773 | uint32_t class_def_method_index) REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | 2d441b1 | 2018-06-08 15:33:21 -0700 | [diff] [blame] | 3774 | ScopedAssertNoThreadSuspension sants(__FUNCTION__); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3775 | Runtime* const runtime = Runtime::Current(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 3776 | if (runtime->IsAotCompiler()) { |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 3777 | // The following code only applies to a non-compiler runtime. |
| 3778 | return; |
| 3779 | } |
Nicolas Geoffray | 5ee206f | 2019-10-08 15:09:17 +0100 | [diff] [blame] | 3780 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3781 | // Method shouldn't have already been linked. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 3782 | DCHECK(method->GetEntryPointFromQuickCompiledCode() == nullptr); |
Jeff Hao | 1674363 | 2013-05-08 10:59:04 -0700 | [diff] [blame] | 3783 | |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 3784 | if (!method->IsInvokable()) { |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3785 | EnsureThrowsInvocationError(class_linker, method); |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 3786 | return; |
| 3787 | } |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3788 | |
Nicolas Geoffray | 5ee206f | 2019-10-08 15:09:17 +0100 | [diff] [blame] | 3789 | const void* quick_code = nullptr; |
| 3790 | if (oat_class != nullptr) { |
| 3791 | // Every kind of method should at least get an invoke stub from the oat_method. |
| 3792 | // non-abstract methods also get their code pointers. |
| 3793 | const OatFile::OatMethod oat_method = oat_class->GetOatMethod(class_def_method_index); |
| 3794 | quick_code = oat_method.GetQuickCode(); |
| 3795 | } |
| 3796 | |
| 3797 | bool enter_interpreter = class_linker->ShouldUseInterpreterEntrypoint(method, quick_code); |
| 3798 | |
| 3799 | // Note: this mimics the logic in image_writer.cc that installs the resolution |
| 3800 | // stub only if we have compiled code and the method needs a class initialization |
| 3801 | // check. |
| 3802 | if (quick_code == nullptr) { |
| 3803 | method->SetEntryPointFromQuickCompiledCode( |
| 3804 | method->IsNative() ? GetQuickGenericJniStub() : GetQuickToInterpreterBridge()); |
| 3805 | } else if (enter_interpreter) { |
| 3806 | method->SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge()); |
Vladimir Marko | 5115a4d | 2019-10-17 14:56:47 +0100 | [diff] [blame^] | 3807 | } else if (NeedsClinitCheckBeforeCall(method)) { |
| 3808 | DCHECK(!method->GetDeclaringClass()->IsVisiblyInitialized()); // Actually ClassStatus::Idx. |
| 3809 | // If we do have code but the method needs a class initialization check before calling |
| 3810 | // that code, install the resolution stub that will perform the check. |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 3811 | // It will be replaced by the proper entry point by ClassLinker::FixupStaticTrampolines |
| 3812 | // after initializing class (see ClassLinker::InitializeClass method). |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 3813 | method->SetEntryPointFromQuickCompiledCode(GetQuickResolutionStub()); |
Nicolas Geoffray | 5ee206f | 2019-10-08 15:09:17 +0100 | [diff] [blame] | 3814 | } else { |
| 3815 | method->SetEntryPointFromQuickCompiledCode(quick_code); |
Ian Rogers | 0d6de04 | 2012-02-29 08:50:26 -0800 | [diff] [blame] | 3816 | } |
jeffhao | 26c0a1a | 2012-01-17 16:28:33 -0800 | [diff] [blame] | 3817 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3818 | if (method->IsNative()) { |
| 3819 | // Unregistering restores the dlsym lookup stub. |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 3820 | method->UnregisterNative(); |
Andreas Gampe | 9054683 | 2014-03-12 18:07:19 -0700 | [diff] [blame] | 3821 | |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 3822 | if (enter_interpreter || quick_code == nullptr) { |
Nicolas Geoffray | 5ee206f | 2019-10-08 15:09:17 +0100 | [diff] [blame] | 3823 | // We have a native method here without code. Then it should have the generic JNI |
| 3824 | // trampoline as entrypoint. |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 3825 | // TODO: this doesn't handle all the cases where trampolines may be installed. |
Nicolas Geoffray | 5ee206f | 2019-10-08 15:09:17 +0100 | [diff] [blame] | 3826 | DCHECK(class_linker->IsQuickGenericJniStub(method->GetEntryPointFromQuickCompiledCode())); |
Andreas Gampe | 9054683 | 2014-03-12 18:07:19 -0700 | [diff] [blame] | 3827 | } |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 3828 | } |
| 3829 | } |
| 3830 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3831 | void ClassLinker::SetupClass(const DexFile& dex_file, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 3832 | const dex::ClassDef& dex_class_def, |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3833 | Handle<mirror::Class> klass, |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3834 | ObjPtr<mirror::ClassLoader> class_loader) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3835 | CHECK(klass != nullptr); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3836 | CHECK(klass->GetDexCache() != nullptr); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 3837 | CHECK_EQ(ClassStatus::kNotReady, klass->GetStatus()); |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 3838 | const char* descriptor = dex_file.GetClassDescriptor(dex_class_def); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3839 | CHECK(descriptor != nullptr); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3840 | |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 3841 | klass->SetClass(GetClassRoot<mirror::Class>(this)); |
Andreas Gampe | 5182932 | 2014-08-25 15:05:04 -0700 | [diff] [blame] | 3842 | uint32_t access_flags = dex_class_def.GetJavaAccessFlags(); |
Brian Carlstrom | 8e3fb14 | 2013-10-09 21:00:27 -0700 | [diff] [blame] | 3843 | CHECK_EQ(access_flags & ~kAccJavaFlagsMask, 0U); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3844 | klass->SetAccessFlags(access_flags); |
| 3845 | klass->SetClassLoader(class_loader); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3846 | DCHECK_EQ(klass->GetPrimitiveType(), Primitive::kPrimNot); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 3847 | mirror::Class::SetStatus(klass, ClassStatus::kIdx, nullptr); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3848 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 3849 | klass->SetDexClassDefIndex(dex_file.GetIndexForClassDef(dex_class_def)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3850 | klass->SetDexTypeIndex(dex_class_def.class_idx_); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3851 | } |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3852 | |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3853 | LengthPrefixedArray<ArtField>* ClassLinker::AllocArtFieldArray(Thread* self, |
| 3854 | LinearAlloc* allocator, |
| 3855 | size_t length) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3856 | if (length == 0) { |
| 3857 | return nullptr; |
| 3858 | } |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 3859 | // If the ArtField alignment changes, review all uses of LengthPrefixedArray<ArtField>. |
| 3860 | static_assert(alignof(ArtField) == 4, "ArtField alignment is expected to be 4."); |
| 3861 | size_t storage_size = LengthPrefixedArray<ArtField>::ComputeSize(length); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3862 | void* array_storage = allocator->Alloc(self, storage_size); |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 3863 | auto* ret = new(array_storage) LengthPrefixedArray<ArtField>(length); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3864 | CHECK(ret != nullptr); |
| 3865 | std::uninitialized_fill_n(&ret->At(0), length, ArtField()); |
| 3866 | return ret; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3867 | } |
| 3868 | |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3869 | LengthPrefixedArray<ArtMethod>* ClassLinker::AllocArtMethodArray(Thread* self, |
| 3870 | LinearAlloc* allocator, |
| 3871 | size_t length) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3872 | if (length == 0) { |
| 3873 | return nullptr; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3874 | } |
Vladimir Marko | 1463285 | 2015-08-17 12:07:23 +0100 | [diff] [blame] | 3875 | const size_t method_alignment = ArtMethod::Alignment(image_pointer_size_); |
| 3876 | const size_t method_size = ArtMethod::Size(image_pointer_size_); |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 3877 | const size_t storage_size = |
| 3878 | LengthPrefixedArray<ArtMethod>::ComputeSize(length, method_size, method_alignment); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3879 | void* array_storage = allocator->Alloc(self, storage_size); |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 3880 | auto* ret = new (array_storage) LengthPrefixedArray<ArtMethod>(length); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3881 | CHECK(ret != nullptr); |
| 3882 | for (size_t i = 0; i < length; ++i) { |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 3883 | new(reinterpret_cast<void*>(&ret->At(i, method_size, method_alignment))) ArtMethod; |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3884 | } |
| 3885 | return ret; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3886 | } |
| 3887 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3888 | LinearAlloc* ClassLinker::GetAllocatorForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3889 | if (class_loader == nullptr) { |
| 3890 | return Runtime::Current()->GetLinearAlloc(); |
| 3891 | } |
| 3892 | LinearAlloc* allocator = class_loader->GetAllocator(); |
| 3893 | DCHECK(allocator != nullptr); |
| 3894 | return allocator; |
| 3895 | } |
| 3896 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3897 | LinearAlloc* ClassLinker::GetOrCreateAllocatorForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 3898 | if (class_loader == nullptr) { |
| 3899 | return Runtime::Current()->GetLinearAlloc(); |
| 3900 | } |
| 3901 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
| 3902 | LinearAlloc* allocator = class_loader->GetAllocator(); |
| 3903 | if (allocator == nullptr) { |
Mathieu Chartier | 5b83050 | 2016-03-02 10:30:23 -0800 | [diff] [blame] | 3904 | RegisterClassLoader(class_loader); |
| 3905 | allocator = class_loader->GetAllocator(); |
| 3906 | CHECK(allocator != nullptr); |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 3907 | } |
| 3908 | return allocator; |
| 3909 | } |
| 3910 | |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3911 | void ClassLinker::LoadClass(Thread* self, |
| 3912 | const DexFile& dex_file, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 3913 | const dex::ClassDef& dex_class_def, |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3914 | Handle<mirror::Class> klass) { |
David Brazdil | 20c765f | 2018-10-27 21:45:15 +0000 | [diff] [blame] | 3915 | ClassAccessor accessor(dex_file, |
| 3916 | dex_class_def, |
| 3917 | /* parse_hiddenapi_class_data= */ klass->IsBootStrapClassLoaded()); |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3918 | if (!accessor.HasClassData()) { |
| 3919 | return; |
| 3920 | } |
| 3921 | Runtime* const runtime = Runtime::Current(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3922 | { |
| 3923 | // Note: We cannot have thread suspension until the field and method arrays are setup or else |
| 3924 | // Class::VisitFieldRoots may miss some fields or methods. |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 3925 | ScopedAssertNoThreadSuspension nts(__FUNCTION__); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3926 | // Load static fields. |
Vladimir Marko | 23682bf | 2015-06-24 14:28:03 +0100 | [diff] [blame] | 3927 | // We allow duplicate definitions of the same field in a class_data_item |
| 3928 | // but ignore the repeated indexes here, b/21868015. |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3929 | LinearAlloc* const allocator = GetAllocatorForClassLoader(klass->GetClassLoader()); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3930 | LengthPrefixedArray<ArtField>* sfields = AllocArtFieldArray(self, |
| 3931 | allocator, |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3932 | accessor.NumStaticFields()); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3933 | LengthPrefixedArray<ArtField>* ifields = AllocArtFieldArray(self, |
| 3934 | allocator, |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3935 | accessor.NumInstanceFields()); |
| 3936 | size_t num_sfields = 0u; |
Vladimir Marko | 23682bf | 2015-06-24 14:28:03 +0100 | [diff] [blame] | 3937 | size_t num_ifields = 0u; |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3938 | uint32_t last_static_field_idx = 0u; |
| 3939 | uint32_t last_instance_field_idx = 0u; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 3940 | |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3941 | // Methods |
| 3942 | bool has_oat_class = false; |
| 3943 | const OatFile::OatClass oat_class = (runtime->IsStarted() && !runtime->IsAotCompiler()) |
| 3944 | ? OatFile::FindOatClass(dex_file, klass->GetDexClassDefIndex(), &has_oat_class) |
| 3945 | : OatFile::OatClass::Invalid(); |
| 3946 | const OatFile::OatClass* oat_class_ptr = has_oat_class ? &oat_class : nullptr; |
| 3947 | klass->SetMethodsPtr( |
| 3948 | AllocArtMethodArray(self, allocator, accessor.NumMethods()), |
| 3949 | accessor.NumDirectMethods(), |
| 3950 | accessor.NumVirtualMethods()); |
| 3951 | size_t class_def_method_index = 0; |
| 3952 | uint32_t last_dex_method_index = dex::kDexNoIndex; |
| 3953 | size_t last_class_def_method_index = 0; |
| 3954 | |
| 3955 | // Use the visitor since the ranged based loops are bit slower from seeking. Seeking to the |
| 3956 | // methods needs to decode all of the fields. |
| 3957 | accessor.VisitFieldsAndMethods([&]( |
| 3958 | const ClassAccessor::Field& field) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 3959 | uint32_t field_idx = field.GetIndex(); |
| 3960 | DCHECK_GE(field_idx, last_static_field_idx); // Ordering enforced by DexFileVerifier. |
| 3961 | if (num_sfields == 0 || LIKELY(field_idx > last_static_field_idx)) { |
| 3962 | LoadField(field, klass, &sfields->At(num_sfields)); |
| 3963 | ++num_sfields; |
| 3964 | last_static_field_idx = field_idx; |
| 3965 | } |
| 3966 | }, [&](const ClassAccessor::Field& field) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 3967 | uint32_t field_idx = field.GetIndex(); |
| 3968 | DCHECK_GE(field_idx, last_instance_field_idx); // Ordering enforced by DexFileVerifier. |
| 3969 | if (num_ifields == 0 || LIKELY(field_idx > last_instance_field_idx)) { |
| 3970 | LoadField(field, klass, &ifields->At(num_ifields)); |
| 3971 | ++num_ifields; |
| 3972 | last_instance_field_idx = field_idx; |
| 3973 | } |
| 3974 | }, [&](const ClassAccessor::Method& method) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 3975 | ArtMethod* art_method = klass->GetDirectMethodUnchecked(class_def_method_index, |
| 3976 | image_pointer_size_); |
| 3977 | LoadMethod(dex_file, method, klass, art_method); |
| 3978 | LinkCode(this, art_method, oat_class_ptr, class_def_method_index); |
| 3979 | uint32_t it_method_index = method.GetIndex(); |
| 3980 | if (last_dex_method_index == it_method_index) { |
| 3981 | // duplicate case |
| 3982 | art_method->SetMethodIndex(last_class_def_method_index); |
| 3983 | } else { |
| 3984 | art_method->SetMethodIndex(class_def_method_index); |
| 3985 | last_dex_method_index = it_method_index; |
| 3986 | last_class_def_method_index = class_def_method_index; |
| 3987 | } |
| 3988 | ++class_def_method_index; |
| 3989 | }, [&](const ClassAccessor::Method& method) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 3990 | ArtMethod* art_method = klass->GetVirtualMethodUnchecked( |
| 3991 | class_def_method_index - accessor.NumDirectMethods(), |
| 3992 | image_pointer_size_); |
| 3993 | LoadMethod(dex_file, method, klass, art_method); |
| 3994 | LinkCode(this, art_method, oat_class_ptr, class_def_method_index); |
| 3995 | ++class_def_method_index; |
| 3996 | }); |
| 3997 | |
| 3998 | if (UNLIKELY(num_ifields + num_sfields != accessor.NumFields())) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3999 | LOG(WARNING) << "Duplicate fields in class " << klass->PrettyDescriptor() |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 4000 | << " (unique static fields: " << num_sfields << "/" << accessor.NumStaticFields() |
| 4001 | << ", unique instance fields: " << num_ifields << "/" << accessor.NumInstanceFields() |
| 4002 | << ")"; |
Vladimir Marko | 81819db | 2015-11-05 15:30:12 +0000 | [diff] [blame] | 4003 | // NOTE: Not shrinking the over-allocated sfields/ifields, just setting size. |
| 4004 | if (sfields != nullptr) { |
| 4005 | sfields->SetSize(num_sfields); |
| 4006 | } |
| 4007 | if (ifields != nullptr) { |
| 4008 | ifields->SetSize(num_ifields); |
| 4009 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4010 | } |
Vladimir Marko | 81819db | 2015-11-05 15:30:12 +0000 | [diff] [blame] | 4011 | // Set the field arrays. |
| 4012 | klass->SetSFieldsPtr(sfields); |
| 4013 | DCHECK_EQ(klass->NumStaticFields(), num_sfields); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4014 | klass->SetIFieldsPtr(ifields); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4015 | DCHECK_EQ(klass->NumInstanceFields(), num_ifields); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 4016 | } |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 4017 | // 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] | 4018 | WriteBarrier::ForEveryFieldWrite(klass.Get()); |
Mathieu Chartier | f3f2a7a | 2015-04-14 15:43:10 -0700 | [diff] [blame] | 4019 | self->AllowThreadSuspension(); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 4020 | } |
| 4021 | |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 4022 | void ClassLinker::LoadField(const ClassAccessor::Field& field, |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 4023 | Handle<mirror::Class> klass, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4024 | ArtField* dst) { |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 4025 | const uint32_t field_idx = field.GetIndex(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 4026 | dst->SetDexFieldIndex(field_idx); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4027 | dst->SetDeclaringClass(klass.Get()); |
David Brazdil | f6a8a55 | 2018-01-15 18:10:50 +0000 | [diff] [blame] | 4028 | |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 4029 | // Get access flags from the DexFile and set hiddenapi runtime access flags. |
| 4030 | dst->SetAccessFlags(field.GetAccessFlags() | hiddenapi::CreateRuntimeFlags(field)); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 4031 | } |
| 4032 | |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 4033 | void ClassLinker::LoadMethod(const DexFile& dex_file, |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 4034 | const ClassAccessor::Method& method, |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 4035 | Handle<mirror::Class> klass, |
| 4036 | ArtMethod* dst) { |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 4037 | const uint32_t dex_method_idx = method.GetIndex(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 4038 | const dex::MethodId& method_id = dex_file.GetMethodId(dex_method_idx); |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 4039 | const char* method_name = dex_file.StringDataByIdx(method_id.name_idx_); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 4040 | |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 4041 | ScopedAssertNoThreadSuspension ants("LoadMethod"); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 4042 | dst->SetDexMethodIndex(dex_method_idx); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4043 | dst->SetDeclaringClass(klass.Get()); |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 4044 | dst->SetCodeItemOffset(method.GetCodeItemOffset()); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 4045 | |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 4046 | // Get access flags from the DexFile and set hiddenapi runtime access flags. |
| 4047 | uint32_t access_flags = method.GetAccessFlags() | hiddenapi::CreateRuntimeFlags(method); |
David Brazdil | f6a8a55 | 2018-01-15 18:10:50 +0000 | [diff] [blame] | 4048 | |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 4049 | if (UNLIKELY(strcmp("finalize", method_name) == 0)) { |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 4050 | // Set finalizable flag on declaring class. |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 4051 | if (strcmp("V", dex_file.GetShorty(method_id.proto_idx_)) == 0) { |
| 4052 | // Void return type. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4053 | if (klass->GetClassLoader() != nullptr) { // All non-boot finalizer methods are flagged. |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 4054 | klass->SetFinalizable(); |
| 4055 | } else { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 4056 | std::string temp; |
| 4057 | const char* klass_descriptor = klass->GetDescriptor(&temp); |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 4058 | // The Enum class declares a "final" finalize() method to prevent subclasses from |
| 4059 | // introducing a finalizer. We don't want to set the finalizable flag for Enum or its |
| 4060 | // subclasses, so we exclude it here. |
| 4061 | // We also want to avoid setting the flag on Object, where we know that finalize() is |
| 4062 | // empty. |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 4063 | if (strcmp(klass_descriptor, "Ljava/lang/Object;") != 0 && |
| 4064 | strcmp(klass_descriptor, "Ljava/lang/Enum;") != 0) { |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 4065 | klass->SetFinalizable(); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 4066 | } |
| 4067 | } |
| 4068 | } |
| 4069 | } else if (method_name[0] == '<') { |
| 4070 | // Fix broken access flags for initializers. Bug 11157540. |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 4071 | bool is_init = (strcmp("<init>", method_name) == 0); |
| 4072 | bool is_clinit = !is_init && (strcmp("<clinit>", method_name) == 0); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 4073 | if (UNLIKELY(!is_init && !is_clinit)) { |
| 4074 | LOG(WARNING) << "Unexpected '<' at start of method name " << method_name; |
| 4075 | } else { |
| 4076 | if (UNLIKELY((access_flags & kAccConstructor) == 0)) { |
| 4077 | 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] | 4078 | << klass->PrettyDescriptor() << " in dex file " << dex_file.GetLocation(); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 4079 | access_flags |= kAccConstructor; |
| 4080 | } |
| 4081 | } |
| 4082 | } |
Vladimir Marko | b0a6aee | 2017-10-27 10:34:04 +0100 | [diff] [blame] | 4083 | if (UNLIKELY((access_flags & kAccNative) != 0u)) { |
| 4084 | // Check if the native method is annotated with @FastNative or @CriticalNative. |
| 4085 | access_flags |= annotations::GetNativeMethodAnnotationAccessFlags( |
| 4086 | dex_file, dst->GetClassDef(), dex_method_idx); |
| 4087 | } |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 4088 | dst->SetAccessFlags(access_flags); |
David Srbecky | e36e7f2 | 2018-11-14 14:21:23 +0000 | [diff] [blame] | 4089 | // Must be done after SetAccessFlags since IsAbstract depends on it. |
| 4090 | if (klass->IsInterface() && dst->IsAbstract()) { |
| 4091 | dst->CalculateAndSetImtIndex(); |
| 4092 | } |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 4093 | } |
| 4094 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 4095 | void ClassLinker::AppendToBootClassPath(Thread* self, const DexFile& dex_file) { |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4096 | ObjPtr<mirror::DexCache> dex_cache = AllocAndInitializeDexCache( |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 4097 | self, |
| 4098 | dex_file, |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4099 | Runtime::Current()->GetLinearAlloc()); |
| 4100 | 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] | 4101 | AppendToBootClassPath(dex_file, dex_cache); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 4102 | } |
| 4103 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 4104 | void ClassLinker::AppendToBootClassPath(const DexFile& dex_file, |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4105 | ObjPtr<mirror::DexCache> dex_cache) { |
| 4106 | CHECK(dex_cache != nullptr) << dex_file.GetLocation(); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 4107 | boot_class_path_.push_back(&dex_file); |
Andreas Gampe | be7af22 | 2017-07-25 09:57:28 -0700 | [diff] [blame] | 4108 | WriterMutexLock mu(Thread::Current(), *Locks::dex_lock_); |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 4109 | RegisterDexFileLocked(dex_file, dex_cache, /* class_loader= */ nullptr); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 4110 | } |
| 4111 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 4112 | void ClassLinker::RegisterDexFileLocked(const DexFile& dex_file, |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4113 | ObjPtr<mirror::DexCache> dex_cache, |
| 4114 | ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 4115 | Thread* const self = Thread::Current(); |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 4116 | Locks::dex_lock_->AssertExclusiveHeld(self); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4117 | CHECK(dex_cache != nullptr) << dex_file.GetLocation(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 4118 | // For app images, the dex cache location may be a suffix of the dex file location since the |
| 4119 | // dex file location is an absolute path. |
Mathieu Chartier | 7617216 | 2016-01-26 14:54:06 -0800 | [diff] [blame] | 4120 | const std::string dex_cache_location = dex_cache->GetLocation()->ToModifiedUtf8(); |
| 4121 | const size_t dex_cache_length = dex_cache_location.length(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 4122 | CHECK_GT(dex_cache_length, 0u) << dex_file.GetLocation(); |
| 4123 | std::string dex_file_location = dex_file.GetLocation(); |
Nicolas Geoffray | e3e0f70 | 2019-03-12 07:02:02 +0000 | [diff] [blame] | 4124 | // The following paths checks don't work on preopt when using boot dex files, where the dex |
| 4125 | // cache location is the one on device, and the dex_file's location is the one on host. |
| 4126 | if (!(Runtime::Current()->IsAotCompiler() && class_loader == nullptr && !kIsTargetBuild)) { |
| 4127 | CHECK_GE(dex_file_location.length(), dex_cache_length) |
| 4128 | << dex_cache_location << " " << dex_file.GetLocation(); |
| 4129 | const std::string dex_file_suffix = dex_file_location.substr( |
| 4130 | dex_file_location.length() - dex_cache_length, |
| 4131 | dex_cache_length); |
| 4132 | // Example dex_cache location is SettingsProvider.apk and |
| 4133 | // dex file location is /system/priv-app/SettingsProvider/SettingsProvider.apk |
| 4134 | CHECK_EQ(dex_cache_location, dex_file_suffix); |
| 4135 | } |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 4136 | const OatFile* oat_file = |
| 4137 | (dex_file.GetOatDexFile() != nullptr) ? dex_file.GetOatDexFile()->GetOatFile() : nullptr; |
Vladimir Marko | b066d43 | 2018-01-03 13:14:37 +0000 | [diff] [blame] | 4138 | // Clean up pass to remove null dex caches; null dex caches can occur due to class unloading |
| 4139 | // and we are lazily removing null entries. Also check if we need to initialize OatFile data |
| 4140 | // (.data.bimg.rel.ro and .bss sections) needed for code execution. |
| 4141 | bool initialize_oat_file_data = (oat_file != nullptr) && oat_file->IsExecutable(); |
Ian Rogers | 55256cb | 2017-12-21 17:07:11 -0800 | [diff] [blame] | 4142 | JavaVMExt* const vm = self->GetJniEnv()->GetVm(); |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 4143 | for (auto it = dex_caches_.begin(); it != dex_caches_.end(); ) { |
| 4144 | DexCacheData data = *it; |
| 4145 | if (self->IsJWeakCleared(data.weak_root)) { |
| 4146 | vm->DeleteWeakGlobalRef(self, data.weak_root); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 4147 | it = dex_caches_.erase(it); |
| 4148 | } else { |
Vladimir Marko | b066d43 | 2018-01-03 13:14:37 +0000 | [diff] [blame] | 4149 | if (initialize_oat_file_data && |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 4150 | it->dex_file->GetOatDexFile() != nullptr && |
| 4151 | it->dex_file->GetOatDexFile()->GetOatFile() == oat_file) { |
Vladimir Marko | b066d43 | 2018-01-03 13:14:37 +0000 | [diff] [blame] | 4152 | initialize_oat_file_data = false; // Already initialized. |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 4153 | } |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 4154 | ++it; |
| 4155 | } |
Brian Carlstrom | 81a9087 | 2015-08-28 09:07:14 -0700 | [diff] [blame] | 4156 | } |
Vladimir Marko | b066d43 | 2018-01-03 13:14:37 +0000 | [diff] [blame] | 4157 | if (initialize_oat_file_data) { |
Vladimir Marko | 1cedb4a | 2019-02-06 14:13:28 +0000 | [diff] [blame] | 4158 | oat_file->InitializeRelocations(); |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 4159 | } |
David Brazdil | a5c3a80 | 2019-03-08 14:59:41 +0000 | [diff] [blame] | 4160 | // Let hiddenapi assign a domain to the newly registered dex file. |
| 4161 | hiddenapi::InitializeDexFileDomain(dex_file, class_loader); |
| 4162 | |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4163 | jweak dex_cache_jweak = vm->AddWeakGlobalRef(self, dex_cache); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 4164 | dex_cache->SetDexFile(&dex_file); |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 4165 | DexCacheData data; |
| 4166 | data.weak_root = dex_cache_jweak; |
| 4167 | data.dex_file = dex_cache->GetDexFile(); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4168 | data.class_table = ClassTableForClassLoader(class_loader); |
David Srbecky | afc60cd | 2018-12-05 11:59:31 +0000 | [diff] [blame] | 4169 | AddNativeDebugInfoForDex(self, data.dex_file); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4170 | DCHECK(data.class_table != nullptr); |
Mathieu Chartier | 72041a0 | 2017-07-14 18:23:25 -0700 | [diff] [blame] | 4171 | // Make sure to hold the dex cache live in the class table. This case happens for the boot class |
| 4172 | // path dex caches without an image. |
| 4173 | data.class_table->InsertStrongRoot(dex_cache); |
| 4174 | if (class_loader != nullptr) { |
| 4175 | // Since we added a strong root to the class table, do the write barrier as required for |
| 4176 | // remembered sets and generational GCs. |
Mathieu Chartier | 88ea61e | 2018-06-20 17:45:41 -0700 | [diff] [blame] | 4177 | WriteBarrier::ForEveryFieldWrite(class_loader); |
Mathieu Chartier | 72041a0 | 2017-07-14 18:23:25 -0700 | [diff] [blame] | 4178 | } |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 4179 | dex_caches_.push_back(data); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 4180 | } |
| 4181 | |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4182 | ObjPtr<mirror::DexCache> ClassLinker::DecodeDexCache(Thread* self, const DexCacheData& data) { |
| 4183 | return data.IsValid() |
| 4184 | ? ObjPtr<mirror::DexCache>::DownCast(self->DecodeJObject(data.weak_root)) |
| 4185 | : nullptr; |
| 4186 | } |
| 4187 | |
| 4188 | ObjPtr<mirror::DexCache> ClassLinker::EnsureSameClassLoader( |
| 4189 | Thread* self, |
| 4190 | ObjPtr<mirror::DexCache> dex_cache, |
| 4191 | const DexCacheData& data, |
| 4192 | ObjPtr<mirror::ClassLoader> class_loader) { |
| 4193 | DCHECK_EQ(dex_cache->GetDexFile(), data.dex_file); |
| 4194 | if (data.class_table != ClassTableForClassLoader(class_loader)) { |
| 4195 | self->ThrowNewExceptionF("Ljava/lang/InternalError;", |
| 4196 | "Attempt to register dex file %s with multiple class loaders", |
| 4197 | data.dex_file->GetLocation().c_str()); |
| 4198 | return nullptr; |
| 4199 | } |
| 4200 | return dex_cache; |
| 4201 | } |
| 4202 | |
Alex Light | 07f0621 | 2017-06-01 14:01:43 -0700 | [diff] [blame] | 4203 | void ClassLinker::RegisterExistingDexCache(ObjPtr<mirror::DexCache> dex_cache, |
| 4204 | ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | ed4ee44 | 2018-06-05 14:23:35 -0700 | [diff] [blame] | 4205 | SCOPED_TRACE << __FUNCTION__ << " " << dex_cache->GetDexFile()->GetLocation(); |
Alex Light | 07f0621 | 2017-06-01 14:01:43 -0700 | [diff] [blame] | 4206 | Thread* self = Thread::Current(); |
| 4207 | StackHandleScope<2> hs(self); |
| 4208 | Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(dex_cache)); |
| 4209 | Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader)); |
| 4210 | const DexFile* dex_file = dex_cache->GetDexFile(); |
| 4211 | DCHECK(dex_file != nullptr) << "Attempt to register uninitialized dex_cache object!"; |
| 4212 | if (kIsDebugBuild) { |
| 4213 | DexCacheData old_data; |
| 4214 | { |
| 4215 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
| 4216 | old_data = FindDexCacheDataLocked(*dex_file); |
| 4217 | } |
| 4218 | ObjPtr<mirror::DexCache> old_dex_cache = DecodeDexCache(self, old_data); |
| 4219 | DCHECK(old_dex_cache.IsNull()) << "Attempt to manually register a dex cache thats already " |
| 4220 | << "been registered on dex file " << dex_file->GetLocation(); |
| 4221 | } |
| 4222 | ClassTable* table; |
| 4223 | { |
| 4224 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 4225 | table = InsertClassTableForClassLoader(h_class_loader.Get()); |
| 4226 | } |
Ivan Maidanski | 2b69b9c | 2018-05-14 13:50:48 +0300 | [diff] [blame] | 4227 | // Avoid a deadlock between a garbage collecting thread running a checkpoint, |
| 4228 | // a thread holding the dex lock and blocking on a condition variable regarding |
| 4229 | // weak references access, and a thread blocking on the dex lock. |
Ivan Maidanski | 2b69b9c | 2018-05-14 13:50:48 +0300 | [diff] [blame] | 4230 | gc::ScopedGCCriticalSection gcs(self, gc::kGcCauseClassLinker, gc::kCollectorTypeClassLinker); |
Alex Light | 07f0621 | 2017-06-01 14:01:43 -0700 | [diff] [blame] | 4231 | WriterMutexLock mu(self, *Locks::dex_lock_); |
| 4232 | RegisterDexFileLocked(*dex_file, h_dex_cache.Get(), h_class_loader.Get()); |
| 4233 | table->InsertStrongRoot(h_dex_cache.Get()); |
| 4234 | if (h_class_loader.Get() != nullptr) { |
| 4235 | // Since we added a strong root to the class table, do the write barrier as required for |
| 4236 | // remembered sets and generational GCs. |
Mathieu Chartier | 88ea61e | 2018-06-20 17:45:41 -0700 | [diff] [blame] | 4237 | WriteBarrier::ForEveryFieldWrite(h_class_loader.Get()); |
Alex Light | 07f0621 | 2017-06-01 14:01:43 -0700 | [diff] [blame] | 4238 | } |
| 4239 | } |
| 4240 | |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4241 | ObjPtr<mirror::DexCache> ClassLinker::RegisterDexFile(const DexFile& dex_file, |
| 4242 | ObjPtr<mirror::ClassLoader> class_loader) { |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 4243 | Thread* self = Thread::Current(); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4244 | DexCacheData old_data; |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 4245 | { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 4246 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4247 | old_data = FindDexCacheDataLocked(dex_file); |
| 4248 | } |
| 4249 | ObjPtr<mirror::DexCache> old_dex_cache = DecodeDexCache(self, old_data); |
| 4250 | if (old_dex_cache != nullptr) { |
| 4251 | return EnsureSameClassLoader(self, old_dex_cache, old_data, class_loader); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 4252 | } |
Mathieu Chartier | ed4ee44 | 2018-06-05 14:23:35 -0700 | [diff] [blame] | 4253 | SCOPED_TRACE << __FUNCTION__ << " " << dex_file.GetLocation(); |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 4254 | LinearAlloc* const linear_alloc = GetOrCreateAllocatorForClassLoader(class_loader); |
| 4255 | DCHECK(linear_alloc != nullptr); |
| 4256 | ClassTable* table; |
| 4257 | { |
| 4258 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 4259 | table = InsertClassTableForClassLoader(class_loader); |
| 4260 | } |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 4261 | // Don't alloc while holding the lock, since allocation may need to |
| 4262 | // suspend all threads and another thread may need the dex_lock_ to |
| 4263 | // get to a suspend point. |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4264 | StackHandleScope<3> hs(self); |
| 4265 | Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader)); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4266 | ObjPtr<mirror::String> location; |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 4267 | Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(AllocDexCache(/*out*/&location, |
| 4268 | self, |
| 4269 | dex_file))); |
| 4270 | Handle<mirror::String> h_location(hs.NewHandle(location)); |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 4271 | { |
Ivan Maidanski | 2b69b9c | 2018-05-14 13:50:48 +0300 | [diff] [blame] | 4272 | // Avoid a deadlock between a garbage collecting thread running a checkpoint, |
| 4273 | // a thread holding the dex lock and blocking on a condition variable regarding |
| 4274 | // weak references access, and a thread blocking on the dex lock. |
Ivan Maidanski | 2b69b9c | 2018-05-14 13:50:48 +0300 | [diff] [blame] | 4275 | gc::ScopedGCCriticalSection gcs(self, gc::kGcCauseClassLinker, gc::kCollectorTypeClassLinker); |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 4276 | WriterMutexLock mu(self, *Locks::dex_lock_); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4277 | old_data = FindDexCacheDataLocked(dex_file); |
| 4278 | old_dex_cache = DecodeDexCache(self, old_data); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4279 | if (old_dex_cache == nullptr && h_dex_cache != nullptr) { |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4280 | // Do InitializeDexCache while holding dex lock to make sure two threads don't call it at the |
| 4281 | // same time with the same dex cache. Since the .bss is shared this can cause failing DCHECK |
| 4282 | // that the arrays are null. |
| 4283 | mirror::DexCache::InitializeDexCache(self, |
| 4284 | h_dex_cache.Get(), |
| 4285 | h_location.Get(), |
| 4286 | &dex_file, |
| 4287 | linear_alloc, |
| 4288 | image_pointer_size_); |
| 4289 | RegisterDexFileLocked(dex_file, h_dex_cache.Get(), h_class_loader.Get()); |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 4290 | } |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4291 | } |
| 4292 | if (old_dex_cache != nullptr) { |
| 4293 | // Another thread managed to initialize the dex cache faster, so use that DexCache. |
| 4294 | // If this thread encountered OOME, ignore it. |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4295 | DCHECK_EQ(h_dex_cache == nullptr, self->IsExceptionPending()); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4296 | self->ClearException(); |
| 4297 | // We cannot call EnsureSameClassLoader() while holding the dex_lock_. |
| 4298 | return EnsureSameClassLoader(self, old_dex_cache, old_data, h_class_loader.Get()); |
| 4299 | } |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4300 | if (h_dex_cache == nullptr) { |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4301 | self->AssertPendingOOMException(); |
| 4302 | return nullptr; |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 4303 | } |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 4304 | table->InsertStrongRoot(h_dex_cache.Get()); |
Mathieu Chartier | a1467d0 | 2017-02-22 09:22:50 -0800 | [diff] [blame] | 4305 | if (h_class_loader.Get() != nullptr) { |
| 4306 | // Since we added a strong root to the class table, do the write barrier as required for |
| 4307 | // remembered sets and generational GCs. |
Mathieu Chartier | 88ea61e | 2018-06-20 17:45:41 -0700 | [diff] [blame] | 4308 | WriteBarrier::ForEveryFieldWrite(h_class_loader.Get()); |
Mathieu Chartier | a1467d0 | 2017-02-22 09:22:50 -0800 | [diff] [blame] | 4309 | } |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 4310 | return h_dex_cache.Get(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 4311 | } |
| 4312 | |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4313 | bool ClassLinker::IsDexFileRegistered(Thread* self, const DexFile& dex_file) { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 4314 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4315 | return DecodeDexCache(self, FindDexCacheDataLocked(dex_file)) != nullptr; |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 4316 | } |
| 4317 | |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4318 | ObjPtr<mirror::DexCache> ClassLinker::FindDexCache(Thread* self, const DexFile& dex_file) { |
| 4319 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
Mathieu Chartier | 9e050df | 2017-08-09 10:05:47 -0700 | [diff] [blame] | 4320 | DexCacheData dex_cache_data = FindDexCacheDataLocked(dex_file); |
| 4321 | ObjPtr<mirror::DexCache> dex_cache = DecodeDexCache(self, dex_cache_data); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4322 | if (dex_cache != nullptr) { |
| 4323 | return dex_cache; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 4324 | } |
Brian Carlstrom | 81a9087 | 2015-08-28 09:07:14 -0700 | [diff] [blame] | 4325 | // Failure, dump diagnostic and abort. |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 4326 | for (const DexCacheData& data : dex_caches_) { |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4327 | if (DecodeDexCache(self, data) != nullptr) { |
Andreas Gampe | 37c5846 | 2017-03-27 15:14:27 -0700 | [diff] [blame] | 4328 | LOG(FATAL_WITHOUT_ABORT) << "Registered dex file " << data.dex_file->GetLocation(); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 4329 | } |
Brian Carlstrom | 81a9087 | 2015-08-28 09:07:14 -0700 | [diff] [blame] | 4330 | } |
Mathieu Chartier | 9e050df | 2017-08-09 10:05:47 -0700 | [diff] [blame] | 4331 | LOG(FATAL) << "Failed to find DexCache for DexFile " << dex_file.GetLocation() |
| 4332 | << " " << &dex_file << " " << dex_cache_data.dex_file; |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 4333 | UNREACHABLE(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4334 | } |
| 4335 | |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4336 | ClassTable* ClassLinker::FindClassTable(Thread* self, ObjPtr<mirror::DexCache> dex_cache) { |
| 4337 | const DexFile* dex_file = dex_cache->GetDexFile(); |
| 4338 | DCHECK(dex_file != nullptr); |
| 4339 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
| 4340 | // Search assuming unique-ness of dex file. |
| 4341 | for (const DexCacheData& data : dex_caches_) { |
| 4342 | // Avoid decoding (and read barriers) other unrelated dex caches. |
| 4343 | if (data.dex_file == dex_file) { |
| 4344 | ObjPtr<mirror::DexCache> registered_dex_cache = DecodeDexCache(self, data); |
| 4345 | if (registered_dex_cache != nullptr) { |
| 4346 | CHECK_EQ(registered_dex_cache, dex_cache) << dex_file->GetLocation(); |
| 4347 | return data.class_table; |
| 4348 | } |
| 4349 | } |
| 4350 | } |
| 4351 | return nullptr; |
| 4352 | } |
| 4353 | |
| 4354 | ClassLinker::DexCacheData ClassLinker::FindDexCacheDataLocked(const DexFile& dex_file) { |
| 4355 | // Search assuming unique-ness of dex file. |
| 4356 | for (const DexCacheData& data : dex_caches_) { |
| 4357 | // Avoid decoding (and read barriers) other unrelated dex caches. |
| 4358 | if (data.dex_file == &dex_file) { |
| 4359 | return data; |
| 4360 | } |
| 4361 | } |
| 4362 | return DexCacheData(); |
| 4363 | } |
| 4364 | |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 4365 | void ClassLinker::CreatePrimitiveClass(Thread* self, |
| 4366 | Primitive::Type type, |
| 4367 | ClassRoot primitive_root) { |
Vladimir Marko | acb906d | 2018-05-30 10:23:49 +0100 | [diff] [blame] | 4368 | ObjPtr<mirror::Class> primitive_class = |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 4369 | AllocClass(self, mirror::Class::PrimitiveClassSize(image_pointer_size_)); |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 4370 | CHECK(primitive_class != nullptr) << "OOM for primitive class " << type; |
| 4371 | // Do not hold lock on the primitive class object, the initialization of |
| 4372 | // primitive classes is done while the process is still single threaded. |
| 4373 | primitive_class->SetAccessFlags( |
| 4374 | kAccPublic | kAccFinal | kAccAbstract | kAccVerificationAttempted); |
| 4375 | primitive_class->SetPrimitiveType(type); |
| 4376 | primitive_class->SetIfTable(GetClassRoot<mirror::Object>(this)->GetIfTable()); |
| 4377 | // Skip EnsureSkipAccessChecksMethods(). We can skip the verified status, |
| 4378 | // the kAccVerificationAttempted flag was added above, and there are no |
| 4379 | // methods that need the kAccSkipAccessChecks flag. |
| 4380 | DCHECK_EQ(primitive_class->NumMethods(), 0u); |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 4381 | // Primitive classes are initialized during single threaded startup, so visibly initialized. |
| 4382 | primitive_class->SetStatusForPrimitiveOrArray(ClassStatus::kVisiblyInitialized); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4383 | const char* descriptor = Primitive::Descriptor(type); |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 4384 | ObjPtr<mirror::Class> existing = InsertClass(descriptor, |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 4385 | primitive_class, |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 4386 | ComputeModifiedUtf8Hash(descriptor)); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4387 | CHECK(existing == nullptr) << "InitPrimitiveClass(" << type << ") failed"; |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 4388 | SetClassRoot(primitive_root, primitive_class); |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 4389 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4390 | |
Vladimir Marko | 0261055 | 2018-06-04 14:38:00 +0100 | [diff] [blame] | 4391 | inline ObjPtr<mirror::IfTable> ClassLinker::GetArrayIfTable() { |
| 4392 | return GetClassRoot<mirror::ObjectArray<mirror::Object>>(this)->GetIfTable(); |
| 4393 | } |
| 4394 | |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 4395 | // Create an array class (i.e. the class object for the array, not the |
| 4396 | // array itself). "descriptor" looks like "[C" or "[[[[B" or |
| 4397 | // "[Ljava/lang/String;". |
| 4398 | // |
| 4399 | // If "descriptor" refers to an array of primitives, look up the |
| 4400 | // primitive type's internally-generated class object. |
| 4401 | // |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 4402 | // "class_loader" is the class loader of the class that's referring to |
| 4403 | // us. It's used to ensure that we're looking for the element type in |
| 4404 | // the right context. It does NOT become the class loader for the |
| 4405 | // array class; that always comes from the base element class. |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 4406 | // |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 4407 | // Returns null with an exception raised on failure. |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 4408 | ObjPtr<mirror::Class> ClassLinker::CreateArrayClass(Thread* self, |
| 4409 | const char* descriptor, |
| 4410 | size_t hash, |
| 4411 | Handle<mirror::ClassLoader> class_loader) { |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 4412 | // Identify the underlying component type |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4413 | CHECK_EQ('[', descriptor[0]); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4414 | StackHandleScope<2> hs(self); |
Alex Light | e9f6103 | 2018-09-24 16:04:51 -0700 | [diff] [blame] | 4415 | |
| 4416 | // This is to prevent the calls to ClassLoad and ClassPrepare which can cause java/user-supplied |
| 4417 | // code to be executed. We put it up here so we can avoid all the allocations associated with |
| 4418 | // creating the class. This can happen with (eg) jit threads. |
| 4419 | if (!self->CanLoadClasses()) { |
| 4420 | // Make sure we don't try to load anything, potentially causing an infinite loop. |
| 4421 | ObjPtr<mirror::Throwable> pre_allocated = |
| 4422 | Runtime::Current()->GetPreAllocatedNoClassDefFoundError(); |
| 4423 | self->SetException(pre_allocated); |
| 4424 | return nullptr; |
| 4425 | } |
| 4426 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4427 | MutableHandle<mirror::Class> component_type(hs.NewHandle(FindClass(self, descriptor + 1, |
| 4428 | class_loader))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4429 | if (component_type == nullptr) { |
Mathieu Chartier | c0a9ea4 | 2014-02-03 16:36:49 -0800 | [diff] [blame] | 4430 | DCHECK(self->IsExceptionPending()); |
Andreas Gampe | dc13d7d | 2014-07-23 20:18:36 -0700 | [diff] [blame] | 4431 | // We need to accept erroneous classes as component types. |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 4432 | const size_t component_hash = ComputeModifiedUtf8Hash(descriptor + 1); |
| 4433 | component_type.Assign(LookupClass(self, descriptor + 1, component_hash, class_loader.Get())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4434 | if (component_type == nullptr) { |
Andreas Gampe | dc13d7d | 2014-07-23 20:18:36 -0700 | [diff] [blame] | 4435 | DCHECK(self->IsExceptionPending()); |
| 4436 | return nullptr; |
| 4437 | } else { |
| 4438 | self->ClearException(); |
| 4439 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 4440 | } |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 4441 | if (UNLIKELY(component_type->IsPrimitiveVoid())) { |
| 4442 | ThrowNoClassDefFoundError("Attempt to create array of void primitive type"); |
| 4443 | return nullptr; |
| 4444 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 4445 | // See if the component type is already loaded. Array classes are |
| 4446 | // always associated with the class loader of their underlying |
| 4447 | // element type -- an array of Strings goes with the loader for |
| 4448 | // java/lang/String -- so we need to look for it there. (The |
| 4449 | // caller should have checked for the existence of the class |
| 4450 | // before calling here, but they did so with *their* class loader, |
| 4451 | // not the component type's loader.) |
| 4452 | // |
| 4453 | // If we find it, the caller adds "loader" to the class' initiating |
| 4454 | // loader list, which should prevent us from going through this again. |
| 4455 | // |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4456 | // This call is unnecessary if "loader" and "component_type->GetClassLoader()" |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 4457 | // are the same, because our caller (FindClass) just did the |
| 4458 | // lookup. (Even if we get this wrong we still have correct behavior, |
| 4459 | // because we effectively do this lookup again when we add the new |
| 4460 | // class to the hash table --- necessary because of possible races with |
| 4461 | // other threads.) |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4462 | if (class_loader.Get() != component_type->GetClassLoader()) { |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 4463 | ObjPtr<mirror::Class> new_class = |
| 4464 | LookupClass(self, descriptor, hash, component_type->GetClassLoader()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4465 | if (new_class != nullptr) { |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 4466 | return new_class; |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 4467 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 4468 | } |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 4469 | // Core array classes, i.e. Object[], Class[], String[] and primitive |
| 4470 | // arrays, have special initialization and they should be found above. |
| 4471 | DCHECK(!component_type->IsObjectClass() || |
| 4472 | // Guard from false positives for errors before setting superclass. |
| 4473 | component_type->IsErroneousUnresolved()); |
| 4474 | DCHECK(!component_type->IsStringClass()); |
| 4475 | DCHECK(!component_type->IsClassClass()); |
| 4476 | DCHECK(!component_type->IsPrimitive()); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 4477 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 4478 | // Fill out the fields in the Class. |
| 4479 | // |
| 4480 | // It is possible to execute some methods against arrays, because |
| 4481 | // all arrays are subclasses of java_lang_Object_, so we need to set |
| 4482 | // up a vtable. We can just point at the one in java_lang_Object_. |
| 4483 | // |
| 4484 | // Array classes are simple enough that we don't need to do a full |
| 4485 | // link step. |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 4486 | size_t array_class_size = mirror::Array::ClassSize(image_pointer_size_); |
| 4487 | auto visitor = [this, array_class_size, component_type](ObjPtr<mirror::Object> obj, |
| 4488 | size_t usable_size) |
| 4489 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 4490 | mirror::Class::InitializeClassVisitor init_class(array_class_size); |
| 4491 | init_class(obj, usable_size); |
| 4492 | ObjPtr<mirror::Class> klass = ObjPtr<mirror::Class>::DownCast(obj); |
| 4493 | klass->SetComponentType(component_type.Get()); |
| 4494 | // Do not hold lock for initialization, the fence issued after the visitor |
| 4495 | // returns ensures memory visibility together with the implicit consume |
| 4496 | // semantics (for all supported architectures) for any thread that loads |
| 4497 | // the array class reference from any memory locations afterwards. |
| 4498 | FinishArrayClassSetup(klass); |
| 4499 | }; |
| 4500 | auto new_class = hs.NewHandle<mirror::Class>( |
| 4501 | AllocClass(self, GetClassRoot<mirror::Class>(this), array_class_size, visitor)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4502 | if (new_class == nullptr) { |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 4503 | self->AssertPendingOOMException(); |
| 4504 | return nullptr; |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 4505 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 4506 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4507 | ObjPtr<mirror::Class> existing = InsertClass(descriptor, new_class.Get(), hash); |
Mathieu Chartier | c0a9ea4 | 2014-02-03 16:36:49 -0800 | [diff] [blame] | 4508 | if (existing == nullptr) { |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4509 | // We postpone ClassLoad and ClassPrepare events to this point in time to avoid |
| 4510 | // duplicate events in case of races. Array classes don't really follow dedicated |
| 4511 | // load and prepare, anyways. |
| 4512 | Runtime::Current()->GetRuntimeCallbacks()->ClassLoad(new_class); |
| 4513 | Runtime::Current()->GetRuntimeCallbacks()->ClassPrepare(new_class, new_class); |
| 4514 | |
Tamas Berghammer | 160e6df | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 4515 | jit::Jit::NewTypeLoadedIfUsingJit(new_class.Get()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4516 | return new_class.Get(); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 4517 | } |
| 4518 | // Another thread must have loaded the class after we |
| 4519 | // started but before we finished. Abandon what we've |
| 4520 | // done. |
| 4521 | // |
| 4522 | // (Yes, this happens.) |
| 4523 | |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 4524 | return existing; |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 4525 | } |
| 4526 | |
Vladimir Marko | 9186b18 | 2018-11-06 14:55:54 +0000 | [diff] [blame] | 4527 | ObjPtr<mirror::Class> ClassLinker::LookupPrimitiveClass(char type) { |
| 4528 | ClassRoot class_root; |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 4529 | switch (type) { |
Vladimir Marko | 9186b18 | 2018-11-06 14:55:54 +0000 | [diff] [blame] | 4530 | case 'B': class_root = ClassRoot::kPrimitiveByte; break; |
| 4531 | case 'C': class_root = ClassRoot::kPrimitiveChar; break; |
| 4532 | case 'D': class_root = ClassRoot::kPrimitiveDouble; break; |
| 4533 | case 'F': class_root = ClassRoot::kPrimitiveFloat; break; |
| 4534 | case 'I': class_root = ClassRoot::kPrimitiveInt; break; |
| 4535 | case 'J': class_root = ClassRoot::kPrimitiveLong; break; |
| 4536 | case 'S': class_root = ClassRoot::kPrimitiveShort; break; |
| 4537 | case 'Z': class_root = ClassRoot::kPrimitiveBoolean; break; |
| 4538 | case 'V': class_root = ClassRoot::kPrimitiveVoid; break; |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 4539 | default: |
Vladimir Marko | 9186b18 | 2018-11-06 14:55:54 +0000 | [diff] [blame] | 4540 | return nullptr; |
Carl Shapiro | 744ad05 | 2011-08-06 15:53:36 -0700 | [diff] [blame] | 4541 | } |
Vladimir Marko | 9186b18 | 2018-11-06 14:55:54 +0000 | [diff] [blame] | 4542 | return GetClassRoot(class_root, this); |
| 4543 | } |
| 4544 | |
| 4545 | ObjPtr<mirror::Class> ClassLinker::FindPrimitiveClass(char type) { |
| 4546 | ObjPtr<mirror::Class> result = LookupPrimitiveClass(type); |
| 4547 | if (UNLIKELY(result == nullptr)) { |
| 4548 | std::string printable_type(PrintableChar(type)); |
| 4549 | ThrowNoClassDefFoundError("Not a primitive type: %s", printable_type.c_str()); |
| 4550 | } |
| 4551 | return result; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4552 | } |
| 4553 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 4554 | ObjPtr<mirror::Class> ClassLinker::InsertClass(const char* descriptor, |
| 4555 | ObjPtr<mirror::Class> klass, |
| 4556 | size_t hash) { |
Alex Light | e9f6103 | 2018-09-24 16:04:51 -0700 | [diff] [blame] | 4557 | DCHECK(Thread::Current()->CanLoadClasses()); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 4558 | if (VLOG_IS_ON(class_linker)) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4559 | ObjPtr<mirror::DexCache> dex_cache = klass->GetDexCache(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 4560 | std::string source; |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4561 | if (dex_cache != nullptr) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 4562 | source += " from "; |
| 4563 | source += dex_cache->GetLocation()->ToModifiedUtf8(); |
| 4564 | } |
| 4565 | LOG(INFO) << "Loaded class " << descriptor << source; |
| 4566 | } |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 4567 | { |
| 4568 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Vladimir Marko | 0984e48 | 2019-03-27 16:41:41 +0000 | [diff] [blame] | 4569 | const ObjPtr<mirror::ClassLoader> class_loader = klass->GetClassLoader(); |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 4570 | ClassTable* const class_table = InsertClassTableForClassLoader(class_loader); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4571 | ObjPtr<mirror::Class> existing = class_table->Lookup(descriptor, hash); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4572 | if (existing != nullptr) { |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 4573 | return existing; |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 4574 | } |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 4575 | VerifyObject(klass); |
| 4576 | class_table->InsertWithHash(klass, hash); |
| 4577 | if (class_loader != nullptr) { |
| 4578 | // 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] | 4579 | WriteBarrier::ForEveryFieldWrite(class_loader); |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 4580 | } |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 4581 | if (log_new_roots_) { |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 4582 | new_class_roots_.push_back(GcRoot<mirror::Class>(klass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4583 | } |
| 4584 | } |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 4585 | if (kIsDebugBuild) { |
| 4586 | // Test that copied methods correctly can find their holder. |
| 4587 | for (ArtMethod& method : klass->GetCopiedMethods(image_pointer_size_)) { |
| 4588 | CHECK_EQ(GetHoldingClassOfCopiedMethod(&method), klass); |
| 4589 | } |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 4590 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4591 | return nullptr; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4592 | } |
| 4593 | |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 4594 | void ClassLinker::WriteBarrierForBootOatFileBssRoots(const OatFile* oat_file) { |
Mathieu Chartier | a1467d0 | 2017-02-22 09:22:50 -0800 | [diff] [blame] | 4595 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
| 4596 | DCHECK(!oat_file->GetBssGcRoots().empty()) << oat_file->GetLocation(); |
| 4597 | if (log_new_roots_ && !ContainsElement(new_bss_roots_boot_oat_files_, oat_file)) { |
| 4598 | new_bss_roots_boot_oat_files_.push_back(oat_file); |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 4599 | } |
| 4600 | } |
| 4601 | |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4602 | // TODO This should really be in mirror::Class. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4603 | void ClassLinker::UpdateClassMethods(ObjPtr<mirror::Class> klass, |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4604 | LengthPrefixedArray<ArtMethod>* new_methods) { |
| 4605 | klass->SetMethodsPtrUnchecked(new_methods, |
| 4606 | klass->NumDirectMethods(), |
| 4607 | klass->NumDeclaredVirtualMethods()); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4608 | // 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] | 4609 | WriteBarrier::ForEveryFieldWrite(klass); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4610 | } |
| 4611 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 4612 | ObjPtr<mirror::Class> ClassLinker::LookupClass(Thread* self, |
| 4613 | const char* descriptor, |
| 4614 | ObjPtr<mirror::ClassLoader> class_loader) { |
Andreas Gampe | 2ff3b97 | 2017-06-05 18:14:53 -0700 | [diff] [blame] | 4615 | return LookupClass(self, descriptor, ComputeModifiedUtf8Hash(descriptor), class_loader); |
| 4616 | } |
| 4617 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 4618 | ObjPtr<mirror::Class> ClassLinker::LookupClass(Thread* self, |
| 4619 | const char* descriptor, |
| 4620 | size_t hash, |
| 4621 | ObjPtr<mirror::ClassLoader> class_loader) { |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 4622 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 4623 | ClassTable* const class_table = ClassTableForClassLoader(class_loader); |
| 4624 | if (class_table != nullptr) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4625 | ObjPtr<mirror::Class> result = class_table->Lookup(descriptor, hash); |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 4626 | if (result != nullptr) { |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 4627 | return result; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4628 | } |
Sameer Abu Asal | 2c6de22 | 2013-05-02 17:38:59 -0700 | [diff] [blame] | 4629 | } |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 4630 | return nullptr; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4631 | } |
| 4632 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4633 | class MoveClassTableToPreZygoteVisitor : public ClassLoaderVisitor { |
| 4634 | public: |
Igor Murashkin | 2ffb703 | 2017-11-08 13:35:21 -0800 | [diff] [blame] | 4635 | MoveClassTableToPreZygoteVisitor() {} |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4636 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4637 | void Visit(ObjPtr<mirror::ClassLoader> class_loader) |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4638 | REQUIRES(Locks::classlinker_classes_lock_) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 4639 | REQUIRES_SHARED(Locks::mutator_lock_) override { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4640 | ClassTable* const class_table = class_loader->GetClassTable(); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 4641 | if (class_table != nullptr) { |
| 4642 | class_table->FreezeSnapshot(); |
| 4643 | } |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 4644 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4645 | }; |
| 4646 | |
| 4647 | void ClassLinker::MoveClassTableToPreZygote() { |
| 4648 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 4649 | boot_class_table_->FreezeSnapshot(); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4650 | MoveClassTableToPreZygoteVisitor visitor; |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 4651 | VisitClassLoaders(&visitor); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 4652 | } |
| 4653 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4654 | // Look up classes by hash and descriptor and put all matching ones in the result array. |
| 4655 | class LookupClassesVisitor : public ClassLoaderVisitor { |
| 4656 | public: |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4657 | LookupClassesVisitor(const char* descriptor, |
| 4658 | size_t hash, |
| 4659 | std::vector<ObjPtr<mirror::Class>>* result) |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4660 | : descriptor_(descriptor), |
| 4661 | hash_(hash), |
| 4662 | result_(result) {} |
| 4663 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4664 | void Visit(ObjPtr<mirror::ClassLoader> class_loader) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 4665 | REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_) override { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4666 | ClassTable* const class_table = class_loader->GetClassTable(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4667 | ObjPtr<mirror::Class> klass = class_table->Lookup(descriptor_, hash_); |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 4668 | // Add `klass` only if `class_loader` is its defining (not just initiating) class loader. |
| 4669 | if (klass != nullptr && klass->GetClassLoader() == class_loader) { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4670 | result_->push_back(klass); |
| 4671 | } |
| 4672 | } |
| 4673 | |
| 4674 | private: |
| 4675 | const char* const descriptor_; |
| 4676 | const size_t hash_; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4677 | std::vector<ObjPtr<mirror::Class>>* const result_; |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4678 | }; |
| 4679 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4680 | void ClassLinker::LookupClasses(const char* descriptor, |
| 4681 | std::vector<ObjPtr<mirror::Class>>& result) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4682 | result.clear(); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4683 | Thread* const self = Thread::Current(); |
| 4684 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 4685 | const size_t hash = ComputeModifiedUtf8Hash(descriptor); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 4686 | ObjPtr<mirror::Class> klass = boot_class_table_->Lookup(descriptor, hash); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 4687 | if (klass != nullptr) { |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 4688 | DCHECK(klass->GetClassLoader() == nullptr); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 4689 | result.push_back(klass); |
| 4690 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4691 | LookupClassesVisitor visitor(descriptor, hash, &result); |
| 4692 | VisitClassLoaders(&visitor); |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 4693 | } |
| 4694 | |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4695 | bool ClassLinker::AttemptSupertypeVerification(Thread* self, |
| 4696 | Handle<mirror::Class> klass, |
| 4697 | Handle<mirror::Class> supertype) { |
| 4698 | DCHECK(self != nullptr); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4699 | DCHECK(klass != nullptr); |
| 4700 | DCHECK(supertype != nullptr); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4701 | |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4702 | if (!supertype->IsVerified() && !supertype->IsErroneous()) { |
| 4703 | VerifyClass(self, supertype); |
| 4704 | } |
Nicolas Geoffray | 7cc3ae5 | 2017-03-07 14:33:37 +0000 | [diff] [blame] | 4705 | |
| 4706 | if (supertype->IsVerified() || supertype->ShouldVerifyAtRuntime()) { |
| 4707 | // The supertype is either verified, or we soft failed at AOT time. |
| 4708 | DCHECK(supertype->IsVerified() || Runtime::Current()->IsAotCompiler()); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4709 | return true; |
| 4710 | } |
| 4711 | // If we got this far then we have a hard failure. |
| 4712 | std::string error_msg = |
| 4713 | StringPrintf("Rejecting class %s that attempts to sub-type erroneous class %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4714 | klass->PrettyDescriptor().c_str(), |
| 4715 | supertype->PrettyDescriptor().c_str()); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4716 | LOG(WARNING) << error_msg << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8(); |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4717 | StackHandleScope<1> hs(self); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4718 | Handle<mirror::Throwable> cause(hs.NewHandle(self->GetException())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4719 | if (cause != nullptr) { |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4720 | // Set during VerifyClass call (if at all). |
| 4721 | self->ClearException(); |
| 4722 | } |
| 4723 | // Change into a verify error. |
| 4724 | ThrowVerifyError(klass.Get(), "%s", error_msg.c_str()); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4725 | if (cause != nullptr) { |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4726 | self->GetException()->SetCause(cause.Get()); |
| 4727 | } |
| 4728 | ClassReference ref(klass->GetDexCache()->GetDexFile(), klass->GetDexClassDefIndex()); |
| 4729 | if (Runtime::Current()->IsAotCompiler()) { |
| 4730 | Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref); |
| 4731 | } |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4732 | // Need to grab the lock to change status. |
| 4733 | ObjectLock<mirror::Class> super_lock(self, klass); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4734 | mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4735 | return false; |
| 4736 | } |
| 4737 | |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4738 | verifier::FailureKind ClassLinker::VerifyClass( |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame] | 4739 | Thread* self, Handle<mirror::Class> klass, verifier::HardFailLogMode log_level) { |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4740 | { |
| 4741 | // TODO: assert that the monitor on the Class is held |
| 4742 | ObjectLock<mirror::Class> lock(self, klass); |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 4743 | |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4744 | // Is somebody verifying this now? |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4745 | ClassStatus old_status = klass->GetStatus(); |
| 4746 | while (old_status == ClassStatus::kVerifying || |
| 4747 | old_status == ClassStatus::kVerifyingAtRuntime) { |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4748 | lock.WaitIgnoringInterrupts(); |
Mathieu Chartier | 5ef7020 | 2017-06-29 10:45:10 -0700 | [diff] [blame] | 4749 | // WaitIgnoringInterrupts can still receive an interrupt and return early, in this |
| 4750 | // case we may see the same status again. b/62912904. This is why the check is |
| 4751 | // greater or equal. |
| 4752 | CHECK(klass->IsErroneous() || (klass->GetStatus() >= old_status)) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4753 | << "Class '" << klass->PrettyClass() |
| 4754 | << "' performed an illegal verification state transition from " << old_status |
| 4755 | << " to " << klass->GetStatus(); |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4756 | old_status = klass->GetStatus(); |
| 4757 | } |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 4758 | |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4759 | // The class might already be erroneous, for example at compile time if we attempted to verify |
| 4760 | // this class as a parent to another. |
| 4761 | if (klass->IsErroneous()) { |
| 4762 | ThrowEarlierClassFailure(klass.Get()); |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4763 | return verifier::FailureKind::kHardFailure; |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4764 | } |
Brian Carlstrom | 9b5ee88 | 2012-02-28 09:48:54 -0800 | [diff] [blame] | 4765 | |
Nicolas Geoffray | 7cc3ae5 | 2017-03-07 14:33:37 +0000 | [diff] [blame] | 4766 | // Don't attempt to re-verify if already verified. |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4767 | if (klass->IsVerified()) { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 4768 | EnsureSkipAccessChecksMethods(klass, image_pointer_size_); |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4769 | return verifier::FailureKind::kNoFailure; |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4770 | } |
Nicolas Geoffray | 7cc3ae5 | 2017-03-07 14:33:37 +0000 | [diff] [blame] | 4771 | |
| 4772 | // For AOT, don't attempt to re-verify if we have already found we should |
| 4773 | // verify at runtime. |
| 4774 | if (Runtime::Current()->IsAotCompiler() && klass->ShouldVerifyAtRuntime()) { |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4775 | return verifier::FailureKind::kSoftFailure; |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4776 | } |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 4777 | |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4778 | if (klass->GetStatus() == ClassStatus::kResolved) { |
| 4779 | mirror::Class::SetStatus(klass, ClassStatus::kVerifying, self); |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4780 | } else { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4781 | CHECK_EQ(klass->GetStatus(), ClassStatus::kRetryVerificationAtRuntime) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4782 | << klass->PrettyClass(); |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4783 | CHECK(!Runtime::Current()->IsAotCompiler()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4784 | mirror::Class::SetStatus(klass, ClassStatus::kVerifyingAtRuntime, self); |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4785 | } |
| 4786 | |
| 4787 | // Skip verification if disabled. |
| 4788 | if (!Runtime::Current()->IsVerificationEnabled()) { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4789 | mirror::Class::SetStatus(klass, ClassStatus::kVerified, self); |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 4790 | EnsureSkipAccessChecksMethods(klass, image_pointer_size_); |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4791 | return verifier::FailureKind::kNoFailure; |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4792 | } |
Jeff Hao | 4a200f5 | 2014-04-01 14:58:49 -0700 | [diff] [blame] | 4793 | } |
| 4794 | |
Bharadwaj Kalandhabhatta | 271c1e1 | 2017-06-27 11:14:49 -0700 | [diff] [blame] | 4795 | VLOG(class_linker) << "Beginning verification for class: " |
| 4796 | << klass->PrettyDescriptor() |
| 4797 | << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8(); |
| 4798 | |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 4799 | // Verify super class. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4800 | StackHandleScope<2> hs(self); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4801 | MutableHandle<mirror::Class> supertype(hs.NewHandle(klass->GetSuperClass())); |
| 4802 | // 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] | 4803 | if (supertype != nullptr && !AttemptSupertypeVerification(self, klass, supertype)) { |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4804 | CHECK(self->IsExceptionPending()) << "Verification error should be pending."; |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4805 | return verifier::FailureKind::kHardFailure; |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4806 | } |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 4807 | |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4808 | // Verify all default super-interfaces. |
| 4809 | // |
| 4810 | // (1) Don't bother if the superclass has already had a soft verification failure. |
| 4811 | // |
| 4812 | // (2) Interfaces shouldn't bother to do this recursive verification because they cannot cause |
| 4813 | // recursive initialization by themselves. This is because when an interface is initialized |
| 4814 | // directly it must not initialize its superinterfaces. We are allowed to verify regardless |
| 4815 | // but choose not to for an optimization. If the interfaces is being verified due to a class |
| 4816 | // initialization (which would need all the default interfaces to be verified) the class code |
| 4817 | // will trigger the recursive verification anyway. |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4818 | if ((supertype == nullptr || supertype->IsVerified()) // See (1) |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4819 | && !klass->IsInterface()) { // See (2) |
| 4820 | int32_t iftable_count = klass->GetIfTableCount(); |
| 4821 | MutableHandle<mirror::Class> iface(hs.NewHandle<mirror::Class>(nullptr)); |
| 4822 | // Loop through all interfaces this class has defined. It doesn't matter the order. |
| 4823 | for (int32_t i = 0; i < iftable_count; i++) { |
| 4824 | iface.Assign(klass->GetIfTable()->GetInterface(i)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4825 | DCHECK(iface != nullptr); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4826 | // We only care if we have default interfaces and can skip if we are already verified... |
| 4827 | if (LIKELY(!iface->HasDefaultMethods() || iface->IsVerified())) { |
| 4828 | continue; |
| 4829 | } else if (UNLIKELY(!AttemptSupertypeVerification(self, klass, iface))) { |
| 4830 | // We had a hard failure while verifying this interface. Just return immediately. |
| 4831 | CHECK(self->IsExceptionPending()) << "Verification error should be pending."; |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4832 | return verifier::FailureKind::kHardFailure; |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4833 | } else if (UNLIKELY(!iface->IsVerified())) { |
| 4834 | // We softly failed to verify the iface. Stop checking and clean up. |
| 4835 | // Put the iface into the supertype handle so we know what caused us to fail. |
| 4836 | supertype.Assign(iface.Get()); |
| 4837 | break; |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 4838 | } |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 4839 | } |
| 4840 | } |
| 4841 | |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4842 | // At this point if verification failed, then supertype is the "first" supertype that failed |
| 4843 | // verification (without a specific order). If verification succeeded, then supertype is either |
| 4844 | // null or the original superclass of klass and is verified. |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4845 | DCHECK(supertype == nullptr || |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4846 | supertype.Get() == klass->GetSuperClass() || |
| 4847 | !supertype->IsVerified()); |
| 4848 | |
Elliott Hughes | 634eb2e | 2012-03-22 16:06:28 -0700 | [diff] [blame] | 4849 | // 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] | 4850 | const DexFile& dex_file = *klass->GetDexCache()->GetDexFile(); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4851 | ClassStatus oat_file_class_status(ClassStatus::kNotReady); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4852 | bool preverified = VerifyClassUsingOatFile(dex_file, klass.Get(), oat_file_class_status); |
Bharadwaj Kalandhabhatta | 271c1e1 | 2017-06-27 11:14:49 -0700 | [diff] [blame] | 4853 | |
| 4854 | VLOG(class_linker) << "Class preverified status for class " |
| 4855 | << klass->PrettyDescriptor() |
| 4856 | << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8() |
| 4857 | << ": " |
| 4858 | << preverified; |
| 4859 | |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 4860 | // If the oat file says the class had an error, re-run the verifier. That way we will get a |
| 4861 | // precise error message. To ensure a rerun, test: |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 4862 | // mirror::Class::IsErroneous(oat_file_class_status) => !preverified |
| 4863 | DCHECK(!mirror::Class::IsErroneous(oat_file_class_status) || !preverified); |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 4864 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4865 | std::string error_msg; |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4866 | verifier::FailureKind verifier_failure = verifier::FailureKind::kNoFailure; |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 4867 | if (!preverified) { |
Mathieu Chartier | 9e050df | 2017-08-09 10:05:47 -0700 | [diff] [blame] | 4868 | verifier_failure = PerformClassVerification(self, klass, log_level, &error_msg); |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 4869 | } |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4870 | |
| 4871 | // Verification is done, grab the lock again. |
| 4872 | ObjectLock<mirror::Class> lock(self, klass); |
| 4873 | |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4874 | if (preverified || verifier_failure != verifier::FailureKind::kHardFailure) { |
| 4875 | if (!preverified && verifier_failure != verifier::FailureKind::kNoFailure) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4876 | VLOG(class_linker) << "Soft verification failure in class " |
| 4877 | << klass->PrettyDescriptor() |
| 4878 | << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8() |
| 4879 | << " because: " << error_msg; |
Ian Rogers | 529781d | 2012-07-23 17:24:29 -0700 | [diff] [blame] | 4880 | } |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 4881 | self->AssertNoPendingException(); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 4882 | // Make sure all classes referenced by catch blocks are resolved. |
Alex Light | 5a55986 | 2016-01-29 12:24:48 -0800 | [diff] [blame] | 4883 | ResolveClassExceptionHandlerTypes(klass); |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4884 | if (verifier_failure == verifier::FailureKind::kNoFailure) { |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4885 | // Even though there were no verifier failures we need to respect whether the super-class and |
| 4886 | // super-default-interfaces were verified or requiring runtime reverification. |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4887 | if (supertype == nullptr || supertype->IsVerified()) { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4888 | mirror::Class::SetStatus(klass, ClassStatus::kVerified, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4889 | } else { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4890 | CHECK_EQ(supertype->GetStatus(), ClassStatus::kRetryVerificationAtRuntime); |
| 4891 | mirror::Class::SetStatus(klass, ClassStatus::kRetryVerificationAtRuntime, self); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4892 | // 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] | 4893 | verifier_failure = verifier::FailureKind::kSoftFailure; |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4894 | } |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 4895 | } else { |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4896 | CHECK_EQ(verifier_failure, verifier::FailureKind::kSoftFailure); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 4897 | // Soft failures at compile time should be retried at runtime. Soft |
| 4898 | // failures at runtime will be handled by slow paths in the generated |
| 4899 | // code. Set status accordingly. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 4900 | if (Runtime::Current()->IsAotCompiler()) { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4901 | mirror::Class::SetStatus(klass, ClassStatus::kRetryVerificationAtRuntime, self); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 4902 | } else { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4903 | mirror::Class::SetStatus(klass, ClassStatus::kVerified, self); |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 4904 | // As this is a fake verified status, make sure the methods are _not_ marked |
| 4905 | // kAccSkipAccessChecks later. |
| 4906 | klass->SetVerificationAttempted(); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 4907 | } |
| 4908 | } |
jeffhao | 5cfd6fb | 2011-09-27 13:54:29 -0700 | [diff] [blame] | 4909 | } else { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4910 | VLOG(verifier) << "Verification failed on class " << klass->PrettyDescriptor() |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 4911 | << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8() |
| 4912 | << " because: " << error_msg; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4913 | self->AssertNoPendingException(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4914 | ThrowVerifyError(klass.Get(), "%s", error_msg.c_str()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4915 | mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self); |
jeffhao | 5cfd6fb | 2011-09-27 13:54:29 -0700 | [diff] [blame] | 4916 | } |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4917 | if (preverified || verifier_failure == verifier::FailureKind::kNoFailure) { |
Brian Carlstrom | 6d3f72c | 2013-08-21 18:06:34 -0700 | [diff] [blame] | 4918 | // 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] | 4919 | // Let the interpreter know it by setting the kAccSkipAccessChecks flag onto each |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 4920 | // method. |
| 4921 | // 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] | 4922 | // kAccSkipAccessChecks flag when compiling image classes, the flag is recorded |
Sebastien Hertz | 233ea8e | 2013-06-06 11:57:09 +0200 | [diff] [blame] | 4923 | // in the image and is set when loading the image. |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 4924 | |
| 4925 | if (UNLIKELY(Runtime::Current()->IsVerificationSoftFail())) { |
| 4926 | // Never skip access checks if the verification soft fail is forced. |
| 4927 | // Mark the class as having a verification attempt to avoid re-running the verifier. |
| 4928 | klass->SetVerificationAttempted(); |
| 4929 | } else { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 4930 | EnsureSkipAccessChecksMethods(klass, image_pointer_size_); |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 4931 | } |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 4932 | } |
Nicolas Geoffray | 486dda0 | 2017-09-11 14:15:52 +0100 | [diff] [blame] | 4933 | // Done verifying. Notify the compiler about the verification status, in case the class |
| 4934 | // was verified implicitly (eg super class of a compiled class). |
| 4935 | if (Runtime::Current()->IsAotCompiler()) { |
| 4936 | Runtime::Current()->GetCompilerCallbacks()->UpdateClassState( |
| 4937 | ClassReference(&klass->GetDexFile(), klass->GetDexClassDefIndex()), klass->GetStatus()); |
| 4938 | } |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame] | 4939 | return verifier_failure; |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 4940 | } |
| 4941 | |
Mathieu Chartier | 9e050df | 2017-08-09 10:05:47 -0700 | [diff] [blame] | 4942 | verifier::FailureKind ClassLinker::PerformClassVerification(Thread* self, |
| 4943 | Handle<mirror::Class> klass, |
| 4944 | verifier::HardFailLogMode log_level, |
| 4945 | std::string* error_msg) { |
| 4946 | Runtime* const runtime = Runtime::Current(); |
Andreas Gampe | a43ba3d | 2019-03-13 15:49:20 -0700 | [diff] [blame] | 4947 | return verifier::ClassVerifier::VerifyClass(self, |
| 4948 | klass.Get(), |
| 4949 | runtime->GetCompilerCallbacks(), |
| 4950 | runtime->IsAotCompiler(), |
| 4951 | log_level, |
| 4952 | Runtime::Current()->GetTargetSdkVersion(), |
| 4953 | error_msg); |
Mathieu Chartier | 9e050df | 2017-08-09 10:05:47 -0700 | [diff] [blame] | 4954 | } |
| 4955 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 4956 | bool ClassLinker::VerifyClassUsingOatFile(const DexFile& dex_file, |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4957 | ObjPtr<mirror::Class> klass, |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4958 | ClassStatus& oat_file_class_status) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 4959 | // If we're compiling, we can only verify the class using the oat file if |
| 4960 | // 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] | 4961 | // the compilation unit (app - dependencies). We will let the compiler callback |
| 4962 | // tell us about the latter. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 4963 | if (Runtime::Current()->IsAotCompiler()) { |
Andreas Gampe | e993458 | 2018-01-19 21:23:04 -0800 | [diff] [blame] | 4964 | CompilerCallbacks* callbacks = Runtime::Current()->GetCompilerCallbacks(); |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 4965 | // Are we compiling the bootclasspath? |
Andreas Gampe | e993458 | 2018-01-19 21:23:04 -0800 | [diff] [blame] | 4966 | if (callbacks->IsBootImage()) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 4967 | return false; |
| 4968 | } |
| 4969 | // We are compiling an app (not the image). |
Andreas Gampe | e993458 | 2018-01-19 21:23:04 -0800 | [diff] [blame] | 4970 | if (!callbacks->CanUseOatStatusForVerification(klass.Ptr())) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 4971 | return false; |
| 4972 | } |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4973 | } |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 4974 | |
Andreas Gampe | b40d361 | 2018-06-26 15:49:42 -0700 | [diff] [blame] | 4975 | const OatDexFile* oat_dex_file = dex_file.GetOatDexFile(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4976 | // 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] | 4977 | if (oat_dex_file == nullptr || oat_dex_file->GetOatFile() == nullptr) { |
Nicolas Geoffray | 144f82c | 2019-02-21 09:43:18 +0000 | [diff] [blame] | 4978 | if (!kIsDebugBuild && klass->GetClassLoader() == nullptr) { |
| 4979 | // For boot classpath classes in the case we're not using a default boot image: |
| 4980 | // we don't have the infrastructure yet to query verification data on individual |
| 4981 | // boot vdex files, so it's simpler for now to consider all boot classpath classes |
| 4982 | // verified. This should be taken into account when measuring boot time and app |
| 4983 | // startup compare to the (current) production system where both: |
| 4984 | // 1) updatable boot classpath classes, and |
| 4985 | // 2) classes in /system referencing updatable classes |
| 4986 | // will be verified at runtime. |
David Srbecky | 3db3d37 | 2019-04-17 18:19:17 +0100 | [diff] [blame] | 4987 | if (Runtime::Current()->IsUsingApexBootImageLocation()) { |
Nicolas Geoffray | 144f82c | 2019-02-21 09:43:18 +0000 | [diff] [blame] | 4988 | oat_file_class_status = ClassStatus::kVerified; |
| 4989 | return true; |
| 4990 | } |
| 4991 | } |
Anwar Ghuloum | ad256bb | 2013-07-18 14:58:55 -0700 | [diff] [blame] | 4992 | return false; |
| 4993 | } |
| 4994 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 4995 | uint16_t class_def_index = klass->GetDexClassDefIndex(); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 4996 | oat_file_class_status = oat_dex_file->GetOatClass(class_def_index).GetStatus(); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4997 | if (oat_file_class_status >= ClassStatus::kVerified) { |
Mathieu Chartier | a079e3a | 2016-03-16 19:08:31 -0700 | [diff] [blame] | 4998 | return true; |
| 4999 | } |
| 5000 | // If we only verified a subset of the classes at compile time, we can end up with classes that |
| 5001 | // were resolved by the verifier. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5002 | if (oat_file_class_status == ClassStatus::kResolved) { |
Mathieu Chartier | a079e3a | 2016-03-16 19:08:31 -0700 | [diff] [blame] | 5003 | return false; |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 5004 | } |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5005 | if (oat_file_class_status == ClassStatus::kRetryVerificationAtRuntime) { |
jeffhao | 1ac2944 | 2012-03-26 11:37:32 -0700 | [diff] [blame] | 5006 | // Compile time verification failed with a soft error. Compile time verification can fail |
| 5007 | // because we have incomplete type information. Consider the following: |
Ian Rogers | c476227 | 2012-02-01 15:55:55 -0800 | [diff] [blame] | 5008 | // class ... { |
| 5009 | // Foo x; |
| 5010 | // .... () { |
| 5011 | // if (...) { |
| 5012 | // v1 gets assigned a type of resolved class Foo |
| 5013 | // } else { |
| 5014 | // v1 gets assigned a type of unresolved class Bar |
| 5015 | // } |
| 5016 | // iput x = v1 |
| 5017 | // } } |
| 5018 | // when we merge v1 following the if-the-else it results in Conflict |
| 5019 | // (see verifier::RegType::Merge) as we can't know the type of Bar and we could possibly be |
| 5020 | // allowing an unsafe assignment to the field x in the iput (javac may have compiled this as |
| 5021 | // it knew Bar was a sub-class of Foo, but for us this may have been moved into a separate apk |
| 5022 | // at compile time). |
| 5023 | return false; |
| 5024 | } |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 5025 | if (mirror::Class::IsErroneous(oat_file_class_status)) { |
jeffhao | 1ac2944 | 2012-03-26 11:37:32 -0700 | [diff] [blame] | 5026 | // Compile time verification failed with a hard error. This is caused by invalid instructions |
| 5027 | // in the class. These errors are unrecoverable. |
| 5028 | return false; |
| 5029 | } |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5030 | if (oat_file_class_status == ClassStatus::kNotReady) { |
Ian Rogers | c476227 | 2012-02-01 15:55:55 -0800 | [diff] [blame] | 5031 | // Status is uninitialized if we couldn't determine the status at compile time, for example, |
| 5032 | // not loading the class. |
| 5033 | // TODO: when the verifier doesn't rely on Class-es failing to resolve/load the type hierarchy |
| 5034 | // isn't a problem and this case shouldn't occur |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 5035 | return false; |
| 5036 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 5037 | std::string temp; |
Elliott Hughes | 634eb2e | 2012-03-22 16:06:28 -0700 | [diff] [blame] | 5038 | LOG(FATAL) << "Unexpected class status: " << oat_file_class_status |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5039 | << " " << dex_file.GetLocation() << " " << klass->PrettyClass() << " " |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 5040 | << klass->GetDescriptor(&temp); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 5041 | UNREACHABLE(); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 5042 | } |
| 5043 | |
Alex Light | 5a55986 | 2016-01-29 12:24:48 -0800 | [diff] [blame] | 5044 | void ClassLinker::ResolveClassExceptionHandlerTypes(Handle<mirror::Class> klass) { |
Alex Light | 51a64d5 | 2015-12-17 13:55:59 -0800 | [diff] [blame] | 5045 | for (ArtMethod& method : klass->GetMethods(image_pointer_size_)) { |
Alex Light | 5a55986 | 2016-01-29 12:24:48 -0800 | [diff] [blame] | 5046 | ResolveMethodExceptionHandlerTypes(&method); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 5047 | } |
| 5048 | } |
| 5049 | |
Alex Light | 5a55986 | 2016-01-29 12:24:48 -0800 | [diff] [blame] | 5050 | void ClassLinker::ResolveMethodExceptionHandlerTypes(ArtMethod* method) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 5051 | // similar to DexVerifier::ScanTryCatchBlocks and dex2oat's ResolveExceptionsForMethod. |
David Sehr | 0225f8e | 2018-01-31 08:52:24 +0000 | [diff] [blame] | 5052 | CodeItemDataAccessor accessor(method->DexInstructionData()); |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 5053 | if (!accessor.HasCodeItem()) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 5054 | return; // native or abstract method |
| 5055 | } |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 5056 | if (accessor.TriesSize() == 0) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 5057 | return; // nothing to process |
| 5058 | } |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 5059 | const uint8_t* handlers_ptr = accessor.GetCatchHandlerData(0); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 5060 | uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 5061 | for (uint32_t idx = 0; idx < handlers_size; idx++) { |
| 5062 | CatchHandlerIterator iterator(handlers_ptr); |
| 5063 | for (; iterator.HasNext(); iterator.Next()) { |
| 5064 | // Ensure exception types are resolved so that they don't need resolution to be delivered, |
| 5065 | // unresolved exception types will be ignored by exception delivery |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 5066 | if (iterator.GetHandlerTypeIndex().IsValid()) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5067 | ObjPtr<mirror::Class> exception_type = ResolveType(iterator.GetHandlerTypeIndex(), method); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5068 | if (exception_type == nullptr) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 5069 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 5070 | Thread::Current()->ClearException(); |
| 5071 | } |
| 5072 | } |
| 5073 | } |
| 5074 | handlers_ptr = iterator.EndDataPointer(); |
| 5075 | } |
| 5076 | } |
| 5077 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 5078 | ObjPtr<mirror::Class> ClassLinker::CreateProxyClass(ScopedObjectAccessAlreadyRunnable& soa, |
| 5079 | jstring name, |
| 5080 | jobjectArray interfaces, |
| 5081 | jobject loader, |
| 5082 | jobjectArray methods, |
| 5083 | jobjectArray throws) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 5084 | Thread* self = soa.Self(); |
Alex Light | e9f6103 | 2018-09-24 16:04:51 -0700 | [diff] [blame] | 5085 | |
| 5086 | // This is to prevent the calls to ClassLoad and ClassPrepare which can cause java/user-supplied |
| 5087 | // code to be executed. We put it up here so we can avoid all the allocations associated with |
| 5088 | // creating the class. This can happen with (eg) jit-threads. |
| 5089 | if (!self->CanLoadClasses()) { |
| 5090 | // Make sure we don't try to load anything, potentially causing an infinite loop. |
| 5091 | ObjPtr<mirror::Throwable> pre_allocated = |
| 5092 | Runtime::Current()->GetPreAllocatedNoClassDefFoundError(); |
| 5093 | self->SetException(pre_allocated); |
| 5094 | return nullptr; |
| 5095 | } |
| 5096 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 5097 | StackHandleScope<10> hs(self); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 5098 | MutableHandle<mirror::Class> temp_klass(hs.NewHandle( |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 5099 | AllocClass(self, GetClassRoot<mirror::Class>(this), sizeof(mirror::Class)))); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 5100 | if (temp_klass == nullptr) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 5101 | CHECK(self->IsExceptionPending()); // OOME. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5102 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 5103 | } |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 5104 | DCHECK(temp_klass->GetClass() != nullptr); |
| 5105 | temp_klass->SetObjectSize(sizeof(mirror::Proxy)); |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 5106 | // Set the class access flags incl. VerificationAttempted, so we do not try to set the flag on |
| 5107 | // the methods. |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 5108 | temp_klass->SetAccessFlags(kAccClassIsProxy | kAccPublic | kAccFinal | kAccVerificationAttempted); |
| 5109 | temp_klass->SetClassLoader(soa.Decode<mirror::ClassLoader>(loader)); |
| 5110 | DCHECK_EQ(temp_klass->GetPrimitiveType(), Primitive::kPrimNot); |
| 5111 | temp_klass->SetName(soa.Decode<mirror::String>(name)); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 5112 | temp_klass->SetDexCache(GetClassRoot<mirror::Proxy>(this)->GetDexCache()); |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 5113 | // Object has an empty iftable, copy it for that reason. |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 5114 | temp_klass->SetIfTable(GetClassRoot<mirror::Object>(this)->GetIfTable()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5115 | mirror::Class::SetStatus(temp_klass, ClassStatus::kIdx, self); |
Vladimir Marko | 3892e62 | 2019-03-15 15:22:18 +0000 | [diff] [blame] | 5116 | std::string storage; |
| 5117 | const char* descriptor = temp_klass->GetDescriptor(&storage); |
| 5118 | const size_t hash = ComputeModifiedUtf8Hash(descriptor); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 5119 | |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 5120 | // 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] | 5121 | LinearAlloc* const allocator = GetOrCreateAllocatorForClassLoader(temp_klass->GetClassLoader()); |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 5122 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 5123 | // Insert the class before loading the fields as the field roots |
| 5124 | // (ArtField::declaring_class_) are only visited from the class |
| 5125 | // table. There can't be any suspend points between inserting the |
| 5126 | // class and setting the field arrays below. |
Vladimir Marko | 3892e62 | 2019-03-15 15:22:18 +0000 | [diff] [blame] | 5127 | ObjPtr<mirror::Class> existing = InsertClass(descriptor, temp_klass.Get(), hash); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 5128 | CHECK(existing == nullptr); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 5129 | |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 5130 | // Instance fields are inherited, but we add a couple of static fields... |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 5131 | const size_t num_fields = 2; |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 5132 | LengthPrefixedArray<ArtField>* sfields = AllocArtFieldArray(self, allocator, num_fields); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 5133 | temp_klass->SetSFieldsPtr(sfields); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 5134 | |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 5135 | // 1. Create a static field 'interfaces' that holds the _declared_ interfaces implemented by |
| 5136 | // our proxy, so Class.getInterfaces doesn't return the flattened set. |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 5137 | ArtField& interfaces_sfield = sfields->At(0); |
| 5138 | interfaces_sfield.SetDexFieldIndex(0); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 5139 | interfaces_sfield.SetDeclaringClass(temp_klass.Get()); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 5140 | interfaces_sfield.SetAccessFlags(kAccStatic | kAccPublic | kAccFinal); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 5141 | |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 5142 | // 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] | 5143 | ArtField& throws_sfield = sfields->At(1); |
| 5144 | throws_sfield.SetDexFieldIndex(1); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 5145 | throws_sfield.SetDeclaringClass(temp_klass.Get()); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 5146 | throws_sfield.SetAccessFlags(kAccStatic | kAccPublic | kAccFinal); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 5147 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 5148 | // Proxies have 1 direct method, the constructor |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 5149 | const size_t num_direct_methods = 1; |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 5150 | |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 5151 | // They have as many virtual methods as the array |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 5152 | auto h_methods = hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::Method>>(methods)); |
Vladimir Marko | 679730e | 2018-05-25 15:06:48 +0100 | [diff] [blame] | 5153 | DCHECK_EQ(h_methods->GetClass(), GetClassRoot<mirror::ObjectArray<mirror::Method>>()) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5154 | << mirror::Class::PrettyClass(h_methods->GetClass()); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 5155 | const size_t num_virtual_methods = h_methods->GetLength(); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 5156 | |
| 5157 | // Create the methods array. |
| 5158 | LengthPrefixedArray<ArtMethod>* proxy_class_methods = AllocArtMethodArray( |
| 5159 | self, allocator, num_direct_methods + num_virtual_methods); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5160 | // Currently AllocArtMethodArray cannot return null, but the OOM logic is left there in case we |
| 5161 | // want to throw OOM in the future. |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 5162 | if (UNLIKELY(proxy_class_methods == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5163 | self->AssertPendingOOMException(); |
| 5164 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 5165 | } |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 5166 | temp_klass->SetMethodsPtr(proxy_class_methods, num_direct_methods, num_virtual_methods); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 5167 | |
| 5168 | // Create the single direct method. |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 5169 | CreateProxyConstructor(temp_klass, temp_klass->GetDirectMethodUnchecked(0, image_pointer_size_)); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 5170 | |
| 5171 | // Create virtual method using specified prototypes. |
| 5172 | // TODO These should really use the iterators. |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 5173 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 5174 | auto* virtual_method = temp_klass->GetVirtualMethodUnchecked(i, image_pointer_size_); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5175 | auto* prototype = h_methods->Get(i)->GetArtMethod(); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 5176 | CreateProxyMethod(temp_klass, prototype, virtual_method); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5177 | DCHECK(virtual_method->GetDeclaringClass() != nullptr); |
| 5178 | DCHECK(prototype->GetDeclaringClass() != nullptr); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 5179 | } |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 5180 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 5181 | // The super class is java.lang.reflect.Proxy |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 5182 | temp_klass->SetSuperClass(GetClassRoot<mirror::Proxy>(this)); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 5183 | // Now effectively in the loaded state. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5184 | mirror::Class::SetStatus(temp_klass, ClassStatus::kLoaded, self); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 5185 | self->AssertNoPendingException(); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 5186 | |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 5187 | // At this point the class is loaded. Publish a ClassLoad event. |
| 5188 | // Note: this may be a temporary class. It is a listener's responsibility to handle this. |
| 5189 | Runtime::Current()->GetRuntimeCallbacks()->ClassLoad(temp_klass); |
| 5190 | |
| 5191 | MutableHandle<mirror::Class> klass = hs.NewHandle<mirror::Class>(nullptr); |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 5192 | { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5193 | // Must hold lock on object when resolved. |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 5194 | ObjectLock<mirror::Class> resolution_lock(self, temp_klass); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 5195 | // Link the fields and virtual methods, creating vtable and iftables. |
| 5196 | // The new class will replace the old one in the class table. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5197 | Handle<mirror::ObjectArray<mirror::Class>> h_interfaces( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 5198 | hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::Class>>(interfaces))); |
Vladimir Marko | 3892e62 | 2019-03-15 15:22:18 +0000 | [diff] [blame] | 5199 | if (!LinkClass(self, descriptor, temp_klass, h_interfaces, &klass)) { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5200 | mirror::Class::SetStatus(temp_klass, ClassStatus::kErrorUnresolved, self); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 5201 | return nullptr; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 5202 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5203 | } |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 5204 | CHECK(temp_klass->IsRetired()); |
| 5205 | CHECK_NE(temp_klass.Get(), klass.Get()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5206 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 5207 | CHECK_EQ(interfaces_sfield.GetDeclaringClass(), klass.Get()); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 5208 | interfaces_sfield.SetObject<false>( |
| 5209 | klass.Get(), |
Mathieu Chartier | f8ac97f | 2016-10-05 15:56:52 -0700 | [diff] [blame] | 5210 | soa.Decode<mirror::ObjectArray<mirror::Class>>(interfaces)); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 5211 | CHECK_EQ(throws_sfield.GetDeclaringClass(), klass.Get()); |
| 5212 | throws_sfield.SetObject<false>( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 5213 | klass.Get(), |
Mathieu Chartier | f8ac97f | 2016-10-05 15:56:52 -0700 | [diff] [blame] | 5214 | soa.Decode<mirror::ObjectArray<mirror::ObjectArray<mirror::Class>>>(throws)); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5215 | |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 5216 | Runtime::Current()->GetRuntimeCallbacks()->ClassPrepare(temp_klass, klass); |
| 5217 | |
Vladimir Marko | 305c38b | 2018-02-14 11:50:07 +0000 | [diff] [blame] | 5218 | // SubtypeCheckInfo::Initialized must happen-before any new-instance for that type. |
| 5219 | // See also ClassLinker::EnsureInitialized(). |
| 5220 | if (kBitstringSubtypeCheckEnabled) { |
| 5221 | MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_); |
| 5222 | SubtypeCheck<ObjPtr<mirror::Class>>::EnsureInitialized(klass.Get()); |
| 5223 | // TODO: Avoid taking subtype_check_lock_ if SubtypeCheck for j.l.r.Proxy is already assigned. |
| 5224 | } |
| 5225 | |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 5226 | VisiblyInitializedCallback* callback = nullptr; |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5227 | { |
| 5228 | // Lock on klass is released. Lock new class object. |
| 5229 | ObjectLock<mirror::Class> initialization_lock(self, klass); |
Andreas Gampe | 5b20b35 | 2018-10-11 19:03:20 -0700 | [diff] [blame] | 5230 | EnsureSkipAccessChecksMethods(klass, image_pointer_size_); |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 5231 | // Conservatively go through the ClassStatus::kInitialized state. |
| 5232 | callback = MarkClassInitialized(self, klass); |
| 5233 | } |
| 5234 | if (callback != nullptr) { |
| 5235 | callback->MakeVisible(self); |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 5236 | } |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 5237 | |
| 5238 | // sanity checks |
Elliott Hughes | 67d9200 | 2012-03-26 15:08:51 -0700 | [diff] [blame] | 5239 | if (kIsDebugBuild) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 5240 | CHECK(klass->GetIFieldsPtr() == nullptr); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5241 | CheckProxyConstructor(klass->GetDirectMethod(0, image_pointer_size_)); |
| 5242 | |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 5243 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5244 | auto* virtual_method = klass->GetVirtualMethodUnchecked(i, image_pointer_size_); |
| 5245 | auto* prototype = h_methods->Get(i++)->GetArtMethod(); |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 5246 | CheckProxyMethod(virtual_method, prototype); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 5247 | } |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 5248 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 5249 | StackHandleScope<1> hs2(self); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 5250 | Handle<mirror::String> decoded_name = hs2.NewHandle(soa.Decode<mirror::String>(name)); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 5251 | std::string interfaces_field_name(StringPrintf("java.lang.Class[] %s.interfaces", |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 5252 | decoded_name->ToModifiedUtf8().c_str())); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5253 | CHECK_EQ(ArtField::PrettyField(klass->GetStaticField(0)), interfaces_field_name); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 5254 | |
| 5255 | std::string throws_field_name(StringPrintf("java.lang.Class[][] %s.throws", |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 5256 | decoded_name->ToModifiedUtf8().c_str())); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5257 | CHECK_EQ(ArtField::PrettyField(klass->GetStaticField(1)), throws_field_name); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 5258 | |
Narayan Kamath | 6b2dc31 | 2017-03-14 13:26:12 +0000 | [diff] [blame] | 5259 | CHECK_EQ(klass.Get()->GetProxyInterfaces(), |
Mathieu Chartier | f8ac97f | 2016-10-05 15:56:52 -0700 | [diff] [blame] | 5260 | soa.Decode<mirror::ObjectArray<mirror::Class>>(interfaces)); |
Narayan Kamath | 6b2dc31 | 2017-03-14 13:26:12 +0000 | [diff] [blame] | 5261 | CHECK_EQ(klass.Get()->GetProxyThrows(), |
Mathieu Chartier | f8ac97f | 2016-10-05 15:56:52 -0700 | [diff] [blame] | 5262 | soa.Decode<mirror::ObjectArray<mirror::ObjectArray<mirror::Class>>>(throws)); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 5263 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5264 | return klass.Get(); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 5265 | } |
| 5266 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5267 | void ClassLinker::CreateProxyConstructor(Handle<mirror::Class> klass, ArtMethod* out) { |
| 5268 | // Create constructor for Proxy that must initialize the method. |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 5269 | ObjPtr<mirror::Class> proxy_class = GetClassRoot<mirror::Proxy>(this); |
| 5270 | CHECK_EQ(proxy_class->NumDirectMethods(), 21u); |
Przemyslaw Szczepaniak | f11cd29 | 2016-08-17 17:46:38 +0100 | [diff] [blame] | 5271 | |
Igor Murashkin | 9d3d752 | 2017-02-27 10:39:49 -0800 | [diff] [blame] | 5272 | // Find the <init>(InvocationHandler)V method. The exact method offset varies depending |
| 5273 | // 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] | 5274 | ArtMethod* proxy_constructor = |
| 5275 | jni::DecodeArtMethod(WellKnownClasses::java_lang_reflect_Proxy_init); |
Igor Murashkin | 9d3d752 | 2017-02-27 10:39:49 -0800 | [diff] [blame] | 5276 | DCHECK(proxy_constructor != nullptr) |
| 5277 | << "Could not find <init> method in java.lang.reflect.Proxy"; |
| 5278 | |
Jeff Hao | db8a664 | 2014-08-14 17:18:52 -0700 | [diff] [blame] | 5279 | // Clone the existing constructor of Proxy (our constructor would just invoke it so steal its |
| 5280 | // code_ too) |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5281 | DCHECK(out != nullptr); |
| 5282 | out->CopyFrom(proxy_constructor, image_pointer_size_); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 5283 | // 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] | 5284 | // 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] | 5285 | // 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] | 5286 | out->SetAccessFlags((out->GetAccessFlags() & ~kAccProtected) | |
| 5287 | kAccPublic | |
| 5288 | kAccCompileDontBother); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5289 | out->SetDeclaringClass(klass.Get()); |
Vladimir Marko | d1ee20f | 2017-08-17 09:21:16 +0000 | [diff] [blame] | 5290 | |
| 5291 | // Set the original constructor method. |
| 5292 | out->SetDataPtrSize(proxy_constructor, image_pointer_size_); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 5293 | } |
| 5294 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5295 | void ClassLinker::CheckProxyConstructor(ArtMethod* constructor) const { |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 5296 | CHECK(constructor->IsConstructor()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5297 | auto* np = constructor->GetInterfaceMethodIfProxy(image_pointer_size_); |
| 5298 | CHECK_STREQ(np->GetName(), "<init>"); |
| 5299 | CHECK_STREQ(np->GetSignature().ToString().c_str(), "(Ljava/lang/reflect/InvocationHandler;)V"); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 5300 | DCHECK(constructor->IsPublic()); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 5301 | } |
| 5302 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5303 | void ClassLinker::CreateProxyMethod(Handle<mirror::Class> klass, ArtMethod* prototype, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5304 | ArtMethod* out) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 5305 | // 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] | 5306 | // as necessary |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5307 | DCHECK(out != nullptr); |
| 5308 | out->CopyFrom(prototype, image_pointer_size_); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 5309 | |
Alex Light | e9dd04f | 2016-03-16 16:09:45 -0700 | [diff] [blame] | 5310 | // Set class to be the concrete proxy class. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5311 | out->SetDeclaringClass(klass.Get()); |
Alex Light | e9dd04f | 2016-03-16 16:09:45 -0700 | [diff] [blame] | 5312 | // Clear the abstract, default and conflict flags to ensure that defaults aren't picked in |
| 5313 | // preference to the invocation handler. |
| 5314 | const uint32_t kRemoveFlags = kAccAbstract | kAccDefault | kAccDefaultConflict; |
| 5315 | // Make the method final. |
Mathieu Chartier | 201e297 | 2017-06-05 18:34:53 -0700 | [diff] [blame] | 5316 | // Mark kAccCompileDontBother so that we don't take JIT samples for the method. b/62349349 |
| 5317 | const uint32_t kAddFlags = kAccFinal | kAccCompileDontBother; |
Alex Light | e9dd04f | 2016-03-16 16:09:45 -0700 | [diff] [blame] | 5318 | out->SetAccessFlags((out->GetAccessFlags() & ~kRemoveFlags) | kAddFlags); |
| 5319 | |
| 5320 | // Clear the dex_code_item_offset_. It needs to be 0 since proxy methods have no CodeItems but the |
| 5321 | // method they copy might (if it's a default method). |
| 5322 | out->SetCodeItemOffset(0); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 5323 | |
Vladimir Marko | d1ee20f | 2017-08-17 09:21:16 +0000 | [diff] [blame] | 5324 | // Set the original interface method. |
| 5325 | out->SetDataPtrSize(prototype, image_pointer_size_); |
| 5326 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 5327 | // At runtime the method looks like a reference and argument saving method, clone the code |
| 5328 | // related parameters from this method. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5329 | out->SetEntryPointFromQuickCompiledCode(GetQuickProxyInvokeHandler()); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 5330 | } |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 5331 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5332 | void ClassLinker::CheckProxyMethod(ArtMethod* method, ArtMethod* prototype) const { |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 5333 | // Basic sanity |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 5334 | CHECK(!prototype->IsFinal()); |
| 5335 | CHECK(method->IsFinal()); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5336 | CHECK(method->IsInvokable()); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 5337 | |
| 5338 | // The proxy method doesn't have its own dex cache or dex file and so it steals those of its |
| 5339 | // 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] | 5340 | CHECK_EQ(prototype->GetDexMethodIndex(), method->GetDexMethodIndex()); |
Vladimir Marko | 5c3e9d1 | 2017-08-30 16:43:54 +0100 | [diff] [blame] | 5341 | CHECK_EQ(prototype, method->GetInterfaceMethodIfProxy(image_pointer_size_)); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 5342 | } |
| 5343 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5344 | bool ClassLinker::CanWeInitializeClass(ObjPtr<mirror::Class> klass, bool can_init_statics, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5345 | bool can_init_parents) { |
Brian Carlstrom | 610e49f | 2013-11-04 17:07:22 -0800 | [diff] [blame] | 5346 | if (can_init_statics && can_init_parents) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5347 | return true; |
| 5348 | } |
| 5349 | if (!can_init_statics) { |
| 5350 | // Check if there's a class initializer. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5351 | ArtMethod* clinit = klass->FindClassInitializer(image_pointer_size_); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5352 | if (clinit != nullptr) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5353 | return false; |
| 5354 | } |
| 5355 | // Check if there are encoded static values needing initialization. |
| 5356 | if (klass->NumStaticFields() != 0) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 5357 | const dex::ClassDef* dex_class_def = klass->GetClassDef(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5358 | DCHECK(dex_class_def != nullptr); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5359 | if (dex_class_def->static_values_off_ != 0) { |
| 5360 | return false; |
| 5361 | } |
| 5362 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5363 | // If we are a class we need to initialize all interfaces with default methods when we are |
| 5364 | // initialized. Check all of them. |
| 5365 | if (!klass->IsInterface()) { |
| 5366 | size_t num_interfaces = klass->GetIfTableCount(); |
| 5367 | for (size_t i = 0; i < num_interfaces; i++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5368 | ObjPtr<mirror::Class> iface = klass->GetIfTable()->GetInterface(i); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5369 | if (iface->HasDefaultMethods() && |
| 5370 | !CanWeInitializeClass(iface, can_init_statics, can_init_parents)) { |
| 5371 | return false; |
| 5372 | } |
| 5373 | } |
| 5374 | } |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5375 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5376 | if (klass->IsInterface() || !klass->HasSuperClass()) { |
| 5377 | return true; |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5378 | } |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5379 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5380 | if (!can_init_parents && !super_class->IsInitialized()) { |
| 5381 | return false; |
| 5382 | } |
| 5383 | return CanWeInitializeClass(super_class, can_init_statics, can_init_parents); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5384 | } |
| 5385 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5386 | bool ClassLinker::InitializeClass(Thread* self, Handle<mirror::Class> klass, |
| 5387 | bool can_init_statics, bool can_init_parents) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5388 | // see JLS 3rd edition, 12.4.2 "Detailed Initialization Procedure" for the locking protocol |
| 5389 | |
| 5390 | // Are we already initialized and therefore done? |
| 5391 | // Note: we differ from the JLS here as we don't do this under the lock, this is benign as |
| 5392 | // an initialized class will never change its state. |
| 5393 | if (klass->IsInitialized()) { |
| 5394 | return true; |
| 5395 | } |
| 5396 | |
| 5397 | // 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] | 5398 | if (!CanWeInitializeClass(klass.Get(), can_init_statics, can_init_parents)) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5399 | return false; |
| 5400 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5401 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5402 | self->AllowThreadSuspension(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5403 | uint64_t t0; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5404 | { |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 5405 | ObjectLock<mirror::Class> lock(self, klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5406 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5407 | // Re-check under the lock in case another thread initialized ahead of us. |
| 5408 | if (klass->IsInitialized()) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5409 | return true; |
| 5410 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5411 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5412 | // 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] | 5413 | if (klass->IsErroneous()) { |
Andreas Gampe | 7b3063b | 2019-01-07 14:12:52 -0800 | [diff] [blame] | 5414 | ThrowEarlierClassFailure(klass.Get(), true, /* log= */ true); |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 5415 | VlogClassInitializationFailure(klass); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5416 | return false; |
| 5417 | } |
| 5418 | |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 5419 | CHECK(klass->IsResolved() && !klass->IsErroneousResolved()) |
| 5420 | << klass->PrettyClass() << ": state=" << klass->GetStatus(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5421 | |
| 5422 | if (!klass->IsVerified()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5423 | VerifyClass(self, klass); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5424 | if (!klass->IsVerified()) { |
| 5425 | // We failed to verify, expect either the klass to be erroneous or verification failed at |
| 5426 | // compile time. |
| 5427 | if (klass->IsErroneous()) { |
Andreas Gampe | fc49fa0 | 2016-04-21 12:21:55 -0700 | [diff] [blame] | 5428 | // The class is erroneous. This may be a verifier error, or another thread attempted |
| 5429 | // verification and/or initialization and failed. We can distinguish those cases by |
| 5430 | // whether an exception is already pending. |
| 5431 | if (self->IsExceptionPending()) { |
| 5432 | // Check that it's a VerifyError. |
| 5433 | DCHECK_EQ("java.lang.Class<java.lang.VerifyError>", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5434 | mirror::Class::PrettyClass(self->GetException()->GetClass())); |
Andreas Gampe | fc49fa0 | 2016-04-21 12:21:55 -0700 | [diff] [blame] | 5435 | } else { |
| 5436 | // Check that another thread attempted initialization. |
| 5437 | DCHECK_NE(0, klass->GetClinitThreadId()); |
| 5438 | DCHECK_NE(self->GetTid(), klass->GetClinitThreadId()); |
| 5439 | // Need to rethrow the previous failure now. |
| 5440 | ThrowEarlierClassFailure(klass.Get(), true); |
| 5441 | } |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 5442 | VlogClassInitializationFailure(klass); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5443 | } else { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 5444 | CHECK(Runtime::Current()->IsAotCompiler()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5445 | CHECK_EQ(klass->GetStatus(), ClassStatus::kRetryVerificationAtRuntime); |
Vladimir Marko | d79b37b | 2018-11-02 13:06:22 +0000 | [diff] [blame] | 5446 | self->AssertNoPendingException(); |
| 5447 | self->SetException(Runtime::Current()->GetPreAllocatedNoClassDefFoundError()); |
jeffhao | a9b3bf4 | 2012-06-06 17:18:39 -0700 | [diff] [blame] | 5448 | } |
Vladimir Marko | d79b37b | 2018-11-02 13:06:22 +0000 | [diff] [blame] | 5449 | self->AssertPendingException(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5450 | return false; |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 5451 | } else { |
| 5452 | self->AssertNoPendingException(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5453 | } |
Andreas Gampe | fc49fa0 | 2016-04-21 12:21:55 -0700 | [diff] [blame] | 5454 | |
| 5455 | // A separate thread could have moved us all the way to initialized. A "simple" example |
| 5456 | // involves a subclass of the current class being initialized at the same time (which |
| 5457 | // will implicitly initialize the superclass, if scheduled that way). b/28254258 |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 5458 | DCHECK(!klass->IsErroneous()) << klass->GetStatus(); |
Andreas Gampe | fc49fa0 | 2016-04-21 12:21:55 -0700 | [diff] [blame] | 5459 | if (klass->IsInitialized()) { |
| 5460 | return true; |
| 5461 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5462 | } |
| 5463 | |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5464 | // If the class is ClassStatus::kInitializing, either this thread is |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5465 | // initializing higher up the stack or another thread has beat us |
| 5466 | // to initializing and we need to wait. Either way, this |
| 5467 | // invocation of InitializeClass will not be responsible for |
| 5468 | // running <clinit> and will return. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5469 | if (klass->GetStatus() == ClassStatus::kInitializing) { |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 5470 | // Could have got an exception during verification. |
| 5471 | if (self->IsExceptionPending()) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 5472 | VlogClassInitializationFailure(klass); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 5473 | return false; |
| 5474 | } |
Elliott Hughes | 005ab2e | 2011-09-11 17:15:31 -0700 | [diff] [blame] | 5475 | // We caught somebody else in the act; was it us? |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 5476 | if (klass->GetClinitThreadId() == self->GetTid()) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5477 | // Yes. That's fine. Return so we can continue initializing. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5478 | return true; |
| 5479 | } |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5480 | // No. That's fine. Wait for another thread to finish initializing. |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5481 | return WaitForInitializeClass(klass, self, lock); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5482 | } |
| 5483 | |
Jeff Hao | e2e4034 | 2017-07-19 10:45:18 -0700 | [diff] [blame] | 5484 | // Try to get the oat class's status for this class if the oat file is present. The compiler |
| 5485 | // tries to validate superclass descriptors, and writes the result into the oat file. |
| 5486 | // Runtime correctness is guaranteed by classpath checks done on loading. If the classpath |
| 5487 | // is different at runtime than it was at compile time, the oat file is rejected. So if the |
| 5488 | // 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] | 5489 | bool has_oat_class = false; |
Jeff Hao | e2e4034 | 2017-07-19 10:45:18 -0700 | [diff] [blame] | 5490 | const Runtime* runtime = Runtime::Current(); |
| 5491 | const OatFile::OatClass oat_class = (runtime->IsStarted() && !runtime->IsAotCompiler()) |
| 5492 | ? OatFile::FindOatClass(klass->GetDexFile(), klass->GetDexClassDefIndex(), &has_oat_class) |
| 5493 | : OatFile::OatClass::Invalid(); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5494 | if (oat_class.GetStatus() < ClassStatus::kSuperclassValidated && |
Jeff Hao | 0cb1728 | 2017-07-12 14:51:49 -0700 | [diff] [blame] | 5495 | !ValidateSuperClassDescriptors(klass)) { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5496 | mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5497 | return false; |
| 5498 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5499 | self->AllowThreadSuspension(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5500 | |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5501 | CHECK_EQ(klass->GetStatus(), ClassStatus::kVerified) << klass->PrettyClass() |
Andreas Gampe | 9510ccd | 2016-04-20 09:55:25 -0700 | [diff] [blame] | 5502 | << " self.tid=" << self->GetTid() << " clinit.tid=" << klass->GetClinitThreadId(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5503 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5504 | // From here out other threads may observe that we're initializing and so changes of state |
| 5505 | // require the a notification. |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 5506 | klass->SetClinitThreadId(self->GetTid()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5507 | mirror::Class::SetStatus(klass, ClassStatus::kInitializing, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5508 | |
| 5509 | t0 = NanoTime(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5510 | } |
| 5511 | |
Andreas Gampe | af86470 | 2019-07-23 14:05:35 -0700 | [diff] [blame] | 5512 | uint64_t t_sub = 0; |
| 5513 | |
Brian Carlstrom | 6d3f72c | 2013-08-21 18:06:34 -0700 | [diff] [blame] | 5514 | // Initialize super classes, must be done while initializing for the JLS. |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5515 | if (!klass->IsInterface() && klass->HasSuperClass()) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5516 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5517 | if (!super_class->IsInitialized()) { |
| 5518 | CHECK(!super_class->IsInterface()); |
| 5519 | CHECK(can_init_parents); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5520 | StackHandleScope<1> hs(self); |
| 5521 | Handle<mirror::Class> handle_scope_super(hs.NewHandle(super_class)); |
Andreas Gampe | af86470 | 2019-07-23 14:05:35 -0700 | [diff] [blame] | 5522 | uint64_t super_t0 = NanoTime(); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5523 | bool super_initialized = InitializeClass(self, handle_scope_super, can_init_statics, true); |
Andreas Gampe | af86470 | 2019-07-23 14:05:35 -0700 | [diff] [blame] | 5524 | uint64_t super_t1 = NanoTime(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5525 | if (!super_initialized) { |
| 5526 | // The super class was verified ahead of entering initializing, we should only be here if |
| 5527 | // the super class became erroneous due to initialization. |
Chang Xing | adbb91c | 2017-07-17 11:23:55 -0700 | [diff] [blame] | 5528 | // For the case of aot compiler, the super class might also be initializing but we don't |
| 5529 | // want to process circular dependencies in pre-compile. |
| 5530 | CHECK(self->IsExceptionPending()) |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 5531 | << "Super class initialization failed for " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5532 | << handle_scope_super->PrettyDescriptor() |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5533 | << " that has unexpected status " << handle_scope_super->GetStatus() |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5534 | << "\nPending exception:\n" |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 5535 | << (self->GetException() != nullptr ? self->GetException()->Dump() : ""); |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 5536 | ObjectLock<mirror::Class> lock(self, klass); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5537 | // Initialization failed because the super-class is erroneous. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5538 | mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5539 | return false; |
| 5540 | } |
Andreas Gampe | af86470 | 2019-07-23 14:05:35 -0700 | [diff] [blame] | 5541 | t_sub = super_t1 - super_t0; |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 5542 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5543 | } |
| 5544 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5545 | if (!klass->IsInterface()) { |
| 5546 | // Initialize interfaces with default methods for the JLS. |
| 5547 | size_t num_direct_interfaces = klass->NumDirectInterfaces(); |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 5548 | // Only setup the (expensive) handle scope if we actually need to. |
| 5549 | if (UNLIKELY(num_direct_interfaces > 0)) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5550 | StackHandleScope<1> hs_iface(self); |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 5551 | MutableHandle<mirror::Class> handle_scope_iface(hs_iface.NewHandle<mirror::Class>(nullptr)); |
| 5552 | for (size_t i = 0; i < num_direct_interfaces; i++) { |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 5553 | handle_scope_iface.Assign(mirror::Class::GetDirectInterface(self, klass.Get(), i)); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 5554 | CHECK(handle_scope_iface != nullptr) << klass->PrettyDescriptor() << " iface #" << i; |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 5555 | CHECK(handle_scope_iface->IsInterface()); |
| 5556 | if (handle_scope_iface->HasBeenRecursivelyInitialized()) { |
| 5557 | // We have already done this for this interface. Skip it. |
| 5558 | continue; |
| 5559 | } |
| 5560 | // We cannot just call initialize class directly because we need to ensure that ALL |
| 5561 | // interfaces with default methods are initialized. Non-default interface initialization |
| 5562 | // will not affect other non-default super-interfaces. |
Andreas Gampe | af86470 | 2019-07-23 14:05:35 -0700 | [diff] [blame] | 5563 | uint64_t inf_t0 = NanoTime(); // This is not very precise, misses all walking. |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 5564 | bool iface_initialized = InitializeDefaultInterfaceRecursive(self, |
| 5565 | handle_scope_iface, |
| 5566 | can_init_statics, |
| 5567 | can_init_parents); |
Andreas Gampe | af86470 | 2019-07-23 14:05:35 -0700 | [diff] [blame] | 5568 | uint64_t inf_t1 = NanoTime(); |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 5569 | if (!iface_initialized) { |
| 5570 | ObjectLock<mirror::Class> lock(self, klass); |
| 5571 | // Initialization failed because one of our interfaces with default methods is erroneous. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5572 | mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self); |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 5573 | return false; |
| 5574 | } |
Andreas Gampe | af86470 | 2019-07-23 14:05:35 -0700 | [diff] [blame] | 5575 | t_sub += inf_t1 - inf_t0; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5576 | } |
| 5577 | } |
| 5578 | } |
| 5579 | |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 5580 | const size_t num_static_fields = klass->NumStaticFields(); |
| 5581 | if (num_static_fields > 0) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 5582 | const dex::ClassDef* dex_class_def = klass->GetClassDef(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5583 | CHECK(dex_class_def != nullptr); |
Hiroshi Yamauchi | 67ef46a | 2014-08-21 15:59:43 -0700 | [diff] [blame] | 5584 | StackHandleScope<3> hs(self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5585 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader())); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 5586 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache())); |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 5587 | |
| 5588 | // Eagerly fill in static fields so that the we don't have to do as many expensive |
| 5589 | // Class::FindStaticField in ResolveField. |
| 5590 | for (size_t i = 0; i < num_static_fields; ++i) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 5591 | ArtField* field = klass->GetStaticField(i); |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 5592 | const uint32_t field_idx = field->GetDexFieldIndex(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 5593 | ArtField* resolved_field = dex_cache->GetResolvedField(field_idx, image_pointer_size_); |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 5594 | if (resolved_field == nullptr) { |
David Brazdil | 1ab0fa8 | 2018-05-04 11:28:03 +0100 | [diff] [blame] | 5595 | // Populating cache of a dex file which defines `klass` should always be allowed. |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 5596 | DCHECK(!hiddenapi::ShouldDenyAccessToMember( |
| 5597 | field, |
| 5598 | hiddenapi::AccessContext(class_loader.Get(), dex_cache.Get()), |
| 5599 | hiddenapi::AccessMethod::kNone)); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 5600 | dex_cache->SetResolvedField(field_idx, field, image_pointer_size_); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5601 | } else { |
| 5602 | DCHECK_EQ(field, resolved_field); |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 5603 | } |
| 5604 | } |
| 5605 | |
Vladimir Marko | e11dd50 | 2017-12-08 14:09:45 +0000 | [diff] [blame] | 5606 | annotations::RuntimeEncodedStaticFieldValueIterator value_it(dex_cache, |
| 5607 | class_loader, |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 5608 | this, |
| 5609 | *dex_class_def); |
Vladimir Marko | e11dd50 | 2017-12-08 14:09:45 +0000 | [diff] [blame] | 5610 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 5611 | |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 5612 | if (value_it.HasNext()) { |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 5613 | ClassAccessor accessor(dex_file, *dex_class_def); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5614 | CHECK(can_init_statics); |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 5615 | for (const ClassAccessor::Field& field : accessor.GetStaticFields()) { |
| 5616 | if (!value_it.HasNext()) { |
| 5617 | break; |
| 5618 | } |
| 5619 | ArtField* art_field = ResolveField(field.GetIndex(), |
| 5620 | dex_cache, |
| 5621 | class_loader, |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 5622 | /* is_static= */ true); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 5623 | if (Runtime::Current()->IsActiveTransaction()) { |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 5624 | value_it.ReadValueToField<true>(art_field); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 5625 | } else { |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 5626 | value_it.ReadValueToField<false>(art_field); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 5627 | } |
Mathieu Chartier | da595be | 2016-08-10 13:57:39 -0700 | [diff] [blame] | 5628 | if (self->IsExceptionPending()) { |
| 5629 | break; |
| 5630 | } |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 5631 | value_it.Next(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5632 | } |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 5633 | DCHECK(self->IsExceptionPending() || !value_it.HasNext()); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5634 | } |
| 5635 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5636 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5637 | |
Mathieu Chartier | da595be | 2016-08-10 13:57:39 -0700 | [diff] [blame] | 5638 | if (!self->IsExceptionPending()) { |
| 5639 | ArtMethod* clinit = klass->FindClassInitializer(image_pointer_size_); |
| 5640 | if (clinit != nullptr) { |
| 5641 | CHECK(can_init_statics); |
| 5642 | JValue result; |
| 5643 | clinit->Invoke(self, nullptr, 0, &result, "V"); |
| 5644 | } |
| 5645 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5646 | self->AllowThreadSuspension(); |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 5647 | uint64_t t1 = NanoTime(); |
| 5648 | |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 5649 | VisiblyInitializedCallback* callback = nullptr; |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 5650 | bool success = true; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5651 | { |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 5652 | ObjectLock<mirror::Class> lock(self, klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5653 | |
| 5654 | if (self->IsExceptionPending()) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 5655 | WrapExceptionInInitializer(klass); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5656 | mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self); |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 5657 | success = false; |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 5658 | } else if (Runtime::Current()->IsTransactionAborted()) { |
| 5659 | // The exception thrown when the transaction aborted has been caught and cleared |
| 5660 | // so we need to throw it again now. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5661 | VLOG(compiler) << "Return from class initializer of " |
| 5662 | << mirror::Class::PrettyDescriptor(klass.Get()) |
Sebastien Hertz | bd9cf9f | 2015-03-03 12:16:13 +0100 | [diff] [blame] | 5663 | << " without exception while transaction was aborted: re-throw it now."; |
Sebastien Hertz | 2fd7e69 | 2015-04-02 11:11:19 +0200 | [diff] [blame] | 5664 | Runtime::Current()->ThrowTransactionAbortError(self); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5665 | mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self); |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 5666 | success = false; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5667 | } else { |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 5668 | RuntimeStats* global_stats = Runtime::Current()->GetStats(); |
| 5669 | RuntimeStats* thread_stats = self->GetStats(); |
| 5670 | ++global_stats->class_init_count; |
| 5671 | ++thread_stats->class_init_count; |
Andreas Gampe | af86470 | 2019-07-23 14:05:35 -0700 | [diff] [blame] | 5672 | global_stats->class_init_time_ns += (t1 - t0 - t_sub); |
| 5673 | thread_stats->class_init_time_ns += (t1 - t0 - t_sub); |
Ian Rogers | e6bb3b2 | 2013-08-19 21:51:45 -0700 | [diff] [blame] | 5674 | // Set the class as initialized except if failed to initialize static fields. |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 5675 | callback = MarkClassInitialized(self, klass); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5676 | if (VLOG_IS_ON(class_linker)) { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 5677 | std::string temp; |
| 5678 | LOG(INFO) << "Initialized class " << klass->GetDescriptor(&temp) << " from " << |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 5679 | klass->GetLocation(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 5680 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5681 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5682 | } |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 5683 | if (callback != nullptr) { |
| 5684 | callback->MakeVisible(self); |
| 5685 | } |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 5686 | return success; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5687 | } |
| 5688 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5689 | // We recursively run down the tree of interfaces. We need to do this in the order they are declared |
| 5690 | // and perform the initialization only on those interfaces that contain default methods. |
| 5691 | bool ClassLinker::InitializeDefaultInterfaceRecursive(Thread* self, |
| 5692 | Handle<mirror::Class> iface, |
| 5693 | bool can_init_statics, |
| 5694 | bool can_init_parents) { |
| 5695 | CHECK(iface->IsInterface()); |
| 5696 | size_t num_direct_ifaces = iface->NumDirectInterfaces(); |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 5697 | // Only create the (expensive) handle scope if we need it. |
| 5698 | if (UNLIKELY(num_direct_ifaces > 0)) { |
| 5699 | StackHandleScope<1> hs(self); |
| 5700 | MutableHandle<mirror::Class> handle_super_iface(hs.NewHandle<mirror::Class>(nullptr)); |
| 5701 | // First we initialize all of iface's super-interfaces recursively. |
| 5702 | for (size_t i = 0; i < num_direct_ifaces; i++) { |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 5703 | ObjPtr<mirror::Class> super_iface = mirror::Class::GetDirectInterface(self, iface.Get(), i); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 5704 | CHECK(super_iface != nullptr) << iface->PrettyDescriptor() << " iface #" << i; |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 5705 | if (!super_iface->HasBeenRecursivelyInitialized()) { |
| 5706 | // Recursive step |
| 5707 | handle_super_iface.Assign(super_iface); |
| 5708 | if (!InitializeDefaultInterfaceRecursive(self, |
| 5709 | handle_super_iface, |
| 5710 | can_init_statics, |
| 5711 | can_init_parents)) { |
| 5712 | return false; |
| 5713 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5714 | } |
| 5715 | } |
| 5716 | } |
| 5717 | |
| 5718 | bool result = true; |
| 5719 | // Then we initialize 'iface' if it has default methods. We do not need to (and in fact must not) |
| 5720 | // initialize if we don't have default methods. |
| 5721 | if (iface->HasDefaultMethods()) { |
| 5722 | result = EnsureInitialized(self, iface, can_init_statics, can_init_parents); |
| 5723 | } |
| 5724 | |
| 5725 | // Mark that this interface has undergone recursive default interface initialization so we know we |
| 5726 | // can skip it on any later class initializations. We do this even if we are not a default |
| 5727 | // interface since we can still avoid the traversal. This is purely a performance optimization. |
| 5728 | if (result) { |
| 5729 | // TODO This should be done in a better way |
Andreas Gampe | 976b298 | 2018-03-02 17:54:22 -0800 | [diff] [blame] | 5730 | // Note: Use a try-lock to avoid blocking when someone else is holding the lock on this |
| 5731 | // interface. It is bad (Java) style, but not impossible. Marking the recursive |
| 5732 | // initialization is a performance optimization (to avoid another idempotent visit |
| 5733 | // for other implementing classes/interfaces), and can be revisited later. |
| 5734 | ObjectTryLock<mirror::Class> lock(self, iface); |
| 5735 | if (lock.Acquired()) { |
| 5736 | iface->SetRecursivelyInitialized(); |
| 5737 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5738 | } |
| 5739 | return result; |
| 5740 | } |
| 5741 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5742 | bool ClassLinker::WaitForInitializeClass(Handle<mirror::Class> klass, |
| 5743 | Thread* self, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5744 | ObjectLock<mirror::Class>& lock) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5745 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5746 | while (true) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 5747 | self->AssertNoPendingException(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5748 | CHECK(!klass->IsInitialized()); |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5749 | lock.WaitIgnoringInterrupts(); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5750 | |
| 5751 | // When we wake up, repeat the test for init-in-progress. If |
| 5752 | // there's an exception pending (only possible if |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 5753 | // we were not using WaitIgnoringInterrupts), bail out. |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5754 | if (self->IsExceptionPending()) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 5755 | WrapExceptionInInitializer(klass); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5756 | mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5757 | return false; |
| 5758 | } |
| 5759 | // Spurious wakeup? Go back to waiting. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5760 | if (klass->GetStatus() == ClassStatus::kInitializing) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5761 | continue; |
| 5762 | } |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5763 | if (klass->GetStatus() == ClassStatus::kVerified && |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 5764 | Runtime::Current()->IsAotCompiler()) { |
Ian Rogers | 3d1548d | 2012-09-24 14:08:03 -0700 | [diff] [blame] | 5765 | // Compile time initialization failed. |
| 5766 | return false; |
| 5767 | } |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5768 | if (klass->IsErroneous()) { |
| 5769 | // The caller wants an exception, but it was thrown in a |
| 5770 | // different thread. Synthesize one here. |
Brian Carlstrom | df14324 | 2011-10-10 18:05:34 -0700 | [diff] [blame] | 5771 | ThrowNoClassDefFoundError("<clinit> failed for class %s; see exception in other thread", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5772 | klass->PrettyDescriptor().c_str()); |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 5773 | VlogClassInitializationFailure(klass); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5774 | return false; |
| 5775 | } |
| 5776 | if (klass->IsInitialized()) { |
| 5777 | return true; |
| 5778 | } |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5779 | LOG(FATAL) << "Unexpected class status. " << klass->PrettyClass() << " is " |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 5780 | << klass->GetStatus(); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5781 | } |
Ian Rogers | 0714083 | 2014-09-30 15:43:59 -0700 | [diff] [blame] | 5782 | UNREACHABLE(); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5783 | } |
| 5784 | |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5785 | static void ThrowSignatureCheckResolveReturnTypeException(Handle<mirror::Class> klass, |
| 5786 | Handle<mirror::Class> super_klass, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5787 | ArtMethod* method, |
| 5788 | ArtMethod* m) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5789 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5790 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 5791 | DCHECK(!m->IsProxyMethod()); |
| 5792 | const DexFile* dex_file = m->GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 5793 | const dex::MethodId& method_id = dex_file->GetMethodId(m->GetDexMethodIndex()); |
| 5794 | const dex::ProtoId& proto_id = dex_file->GetMethodPrototype(method_id); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 5795 | dex::TypeIndex return_type_idx = proto_id.return_type_idx_; |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5796 | std::string return_type = dex_file->PrettyType(return_type_idx); |
| 5797 | std::string class_loader = mirror::Object::PrettyTypeOf(m->GetDeclaringClass()->GetClassLoader()); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5798 | ThrowWrappedLinkageError(klass.Get(), |
| 5799 | "While checking class %s method %s signature against %s %s: " |
| 5800 | "Failed to resolve return type %s with %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5801 | mirror::Class::PrettyDescriptor(klass.Get()).c_str(), |
| 5802 | ArtMethod::PrettyMethod(method).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5803 | super_klass->IsInterface() ? "interface" : "superclass", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5804 | mirror::Class::PrettyDescriptor(super_klass.Get()).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5805 | return_type.c_str(), class_loader.c_str()); |
| 5806 | } |
| 5807 | |
| 5808 | static void ThrowSignatureCheckResolveArgException(Handle<mirror::Class> klass, |
| 5809 | Handle<mirror::Class> super_klass, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5810 | ArtMethod* method, |
| 5811 | ArtMethod* m, |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5812 | uint32_t index, |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 5813 | dex::TypeIndex arg_type_idx) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5814 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5815 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 5816 | DCHECK(!m->IsProxyMethod()); |
| 5817 | const DexFile* dex_file = m->GetDexFile(); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5818 | std::string arg_type = dex_file->PrettyType(arg_type_idx); |
| 5819 | std::string class_loader = mirror::Object::PrettyTypeOf(m->GetDeclaringClass()->GetClassLoader()); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5820 | ThrowWrappedLinkageError(klass.Get(), |
| 5821 | "While checking class %s method %s signature against %s %s: " |
| 5822 | "Failed to resolve arg %u type %s with %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5823 | mirror::Class::PrettyDescriptor(klass.Get()).c_str(), |
| 5824 | ArtMethod::PrettyMethod(method).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5825 | super_klass->IsInterface() ? "interface" : "superclass", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5826 | mirror::Class::PrettyDescriptor(super_klass.Get()).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5827 | index, arg_type.c_str(), class_loader.c_str()); |
| 5828 | } |
| 5829 | |
| 5830 | static void ThrowSignatureMismatch(Handle<mirror::Class> klass, |
| 5831 | Handle<mirror::Class> super_klass, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5832 | ArtMethod* method, |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5833 | const std::string& error_msg) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5834 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5835 | ThrowLinkageError(klass.Get(), |
| 5836 | "Class %s method %s resolves differently in %s %s: %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5837 | mirror::Class::PrettyDescriptor(klass.Get()).c_str(), |
| 5838 | ArtMethod::PrettyMethod(method).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5839 | super_klass->IsInterface() ? "interface" : "superclass", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5840 | mirror::Class::PrettyDescriptor(super_klass.Get()).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5841 | error_msg.c_str()); |
| 5842 | } |
| 5843 | |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5844 | static bool HasSameSignatureWithDifferentClassLoaders(Thread* self, |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5845 | Handle<mirror::Class> klass, |
| 5846 | Handle<mirror::Class> super_klass, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5847 | ArtMethod* method1, |
| 5848 | ArtMethod* method2) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5849 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5850 | { |
| 5851 | StackHandleScope<1> hs(self); |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 5852 | Handle<mirror::Class> return_type(hs.NewHandle(method1->ResolveReturnType())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 5853 | if (UNLIKELY(return_type == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5854 | ThrowSignatureCheckResolveReturnTypeException(klass, super_klass, method1, method1); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5855 | return false; |
| 5856 | } |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 5857 | ObjPtr<mirror::Class> other_return_type = method2->ResolveReturnType(); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5858 | if (UNLIKELY(other_return_type == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5859 | ThrowSignatureCheckResolveReturnTypeException(klass, super_klass, method1, method2); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5860 | return false; |
| 5861 | } |
Vladimir Marko | 862f43c | 2015-02-10 18:22:57 +0000 | [diff] [blame] | 5862 | if (UNLIKELY(other_return_type != return_type.Get())) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5863 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 5864 | StringPrintf("Return types mismatch: %s(%p) vs %s(%p)", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5865 | return_type->PrettyClassAndClassLoader().c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5866 | return_type.Get(), |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5867 | other_return_type->PrettyClassAndClassLoader().c_str(), |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5868 | other_return_type.Ptr())); |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5869 | return false; |
| 5870 | } |
| 5871 | } |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 5872 | const dex::TypeList* types1 = method1->GetParameterTypeList(); |
| 5873 | const dex::TypeList* types2 = method2->GetParameterTypeList(); |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5874 | if (types1 == nullptr) { |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5875 | if (types2 != nullptr && types2->Size() != 0) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5876 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 5877 | StringPrintf("Type list mismatch with %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5878 | method2->PrettyMethod(true).c_str())); |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5879 | return false; |
| 5880 | } |
| 5881 | return true; |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5882 | } else if (UNLIKELY(types2 == nullptr)) { |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5883 | if (types1->Size() != 0) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5884 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 5885 | StringPrintf("Type list mismatch with %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5886 | method2->PrettyMethod(true).c_str())); |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5887 | return false; |
| 5888 | } |
| 5889 | return true; |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5890 | } |
| 5891 | uint32_t num_types = types1->Size(); |
| 5892 | if (UNLIKELY(num_types != types2->Size())) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5893 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 5894 | StringPrintf("Type list mismatch with %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5895 | method2->PrettyMethod(true).c_str())); |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5896 | return false; |
| 5897 | } |
| 5898 | for (uint32_t i = 0; i < num_types; ++i) { |
Vladimir Marko | 862f43c | 2015-02-10 18:22:57 +0000 | [diff] [blame] | 5899 | StackHandleScope<1> hs(self); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 5900 | dex::TypeIndex param_type_idx = types1->GetTypeItem(i).type_idx_; |
Vladimir Marko | 862f43c | 2015-02-10 18:22:57 +0000 | [diff] [blame] | 5901 | Handle<mirror::Class> param_type(hs.NewHandle( |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 5902 | method1->ResolveClassFromTypeIndex(param_type_idx))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 5903 | if (UNLIKELY(param_type == nullptr)) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5904 | ThrowSignatureCheckResolveArgException(klass, super_klass, method1, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5905 | method1, i, param_type_idx); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5906 | return false; |
| 5907 | } |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 5908 | dex::TypeIndex other_param_type_idx = types2->GetTypeItem(i).type_idx_; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5909 | ObjPtr<mirror::Class> other_param_type = |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 5910 | method2->ResolveClassFromTypeIndex(other_param_type_idx); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5911 | if (UNLIKELY(other_param_type == nullptr)) { |
| 5912 | ThrowSignatureCheckResolveArgException(klass, super_klass, method1, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5913 | method2, i, other_param_type_idx); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5914 | return false; |
| 5915 | } |
Vladimir Marko | 862f43c | 2015-02-10 18:22:57 +0000 | [diff] [blame] | 5916 | if (UNLIKELY(param_type.Get() != other_param_type)) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5917 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 5918 | StringPrintf("Parameter %u type mismatch: %s(%p) vs %s(%p)", |
| 5919 | i, |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5920 | param_type->PrettyClassAndClassLoader().c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5921 | param_type.Get(), |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5922 | other_param_type->PrettyClassAndClassLoader().c_str(), |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5923 | other_param_type.Ptr())); |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5924 | return false; |
| 5925 | } |
| 5926 | } |
| 5927 | return true; |
| 5928 | } |
| 5929 | |
| 5930 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5931 | bool ClassLinker::ValidateSuperClassDescriptors(Handle<mirror::Class> klass) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5932 | if (klass->IsInterface()) { |
| 5933 | return true; |
| 5934 | } |
Ian Rogers | 151f221 | 2014-05-06 11:27:27 -0700 | [diff] [blame] | 5935 | // Begin with the methods local to the superclass. |
Ian Rogers | ded66a0 | 2014-10-28 18:12:55 -0700 | [diff] [blame] | 5936 | Thread* self = Thread::Current(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5937 | StackHandleScope<1> hs(self); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5938 | MutableHandle<mirror::Class> super_klass(hs.NewHandle<mirror::Class>(nullptr)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5939 | if (klass->HasSuperClass() && |
| 5940 | klass->GetClassLoader() != klass->GetSuperClass()->GetClassLoader()) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5941 | super_klass.Assign(klass->GetSuperClass()); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 5942 | for (int i = klass->GetSuperClass()->GetVTableLength() - 1; i >= 0; --i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5943 | auto* m = klass->GetVTableEntry(i, image_pointer_size_); |
| 5944 | auto* super_m = klass->GetSuperClass()->GetVTableEntry(i, image_pointer_size_); |
| 5945 | if (m != super_m) { |
Vladimir Marko | 942fd31 | 2017-01-16 20:52:19 +0000 | [diff] [blame] | 5946 | if (UNLIKELY(!HasSameSignatureWithDifferentClassLoaders(self, |
| 5947 | klass, |
| 5948 | super_klass, |
| 5949 | m, |
| 5950 | super_m))) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5951 | self->AssertPendingException(); |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5952 | return false; |
| 5953 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5954 | } |
| 5955 | } |
| 5956 | } |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 5957 | for (int32_t i = 0; i < klass->GetIfTableCount(); ++i) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5958 | super_klass.Assign(klass->GetIfTable()->GetInterface(i)); |
| 5959 | if (klass->GetClassLoader() != super_klass->GetClassLoader()) { |
| 5960 | uint32_t num_methods = super_klass->NumVirtualMethods(); |
Ian Rogers | 151f221 | 2014-05-06 11:27:27 -0700 | [diff] [blame] | 5961 | for (uint32_t j = 0; j < num_methods; ++j) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5962 | auto* m = klass->GetIfTable()->GetMethodArray(i)->GetElementPtrSize<ArtMethod*>( |
| 5963 | j, image_pointer_size_); |
| 5964 | auto* super_m = super_klass->GetVirtualMethod(j, image_pointer_size_); |
| 5965 | if (m != super_m) { |
Vladimir Marko | 942fd31 | 2017-01-16 20:52:19 +0000 | [diff] [blame] | 5966 | if (UNLIKELY(!HasSameSignatureWithDifferentClassLoaders(self, |
| 5967 | klass, |
| 5968 | super_klass, |
| 5969 | m, |
| 5970 | super_m))) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5971 | self->AssertPendingException(); |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5972 | return false; |
| 5973 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5974 | } |
| 5975 | } |
| 5976 | } |
| 5977 | } |
| 5978 | return true; |
| 5979 | } |
| 5980 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5981 | bool ClassLinker::EnsureInitialized(Thread* self, |
| 5982 | Handle<mirror::Class> c, |
| 5983 | bool can_init_fields, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5984 | bool can_init_parents) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 5985 | DCHECK(c != nullptr); |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 5986 | |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 5987 | if (c->IsInitialized()) { |
Vladimir Marko | 8e11065 | 2019-07-30 10:14:41 +0100 | [diff] [blame] | 5988 | // If we've seen an initialized but not visibly initialized class |
| 5989 | // many times, request visible initialization. |
| 5990 | if (kRuntimeISA == InstructionSet::kX86 || kRuntimeISA == InstructionSet::kX86_64) { |
| 5991 | // Thanks to the x86 memory model classes skip the initialized status. |
| 5992 | DCHECK(c->IsVisiblyInitialized()); |
| 5993 | } else if (UNLIKELY(!c->IsVisiblyInitialized())) { |
| 5994 | if (self->IncrementMakeVisiblyInitializedCounter()) { |
| 5995 | MakeInitializedClassesVisiblyInitialized(self, /*wait=*/ false); |
| 5996 | } |
| 5997 | } |
Andreas Gampe | 5b20b35 | 2018-10-11 19:03:20 -0700 | [diff] [blame] | 5998 | DCHECK(c->WasVerificationAttempted()) << c->PrettyClassAndClassLoader(); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 5999 | return true; |
| 6000 | } |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 6001 | // SubtypeCheckInfo::Initialized must happen-before any new-instance for that type. |
| 6002 | // |
| 6003 | // Ensure the bitstring is initialized before any of the class initialization |
| 6004 | // logic occurs. Once a class initializer starts running, objects can |
| 6005 | // escape into the heap and use the subtype checking code. |
| 6006 | // |
| 6007 | // Note: A class whose SubtypeCheckInfo is at least Initialized means it |
| 6008 | // can be used as a source for the IsSubClass check, and that all ancestors |
| 6009 | // of the class are Assigned (can be used as a target for IsSubClass check) |
| 6010 | // or Overflowed (can be used as a source for IsSubClass check). |
Vladimir Marko | 305c38b | 2018-02-14 11:50:07 +0000 | [diff] [blame] | 6011 | if (kBitstringSubtypeCheckEnabled) { |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 6012 | MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_); |
Vladimir Marko | 38b8b25 | 2018-01-02 19:07:06 +0000 | [diff] [blame] | 6013 | SubtypeCheck<ObjPtr<mirror::Class>>::EnsureInitialized(c.Get()); |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 6014 | // TODO: Avoid taking subtype_check_lock_ if SubtypeCheck is already initialized. |
| 6015 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 6016 | const bool success = InitializeClass(self, c, can_init_fields, can_init_parents); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 6017 | if (!success) { |
| 6018 | if (can_init_fields && can_init_parents) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6019 | CHECK(self->IsExceptionPending()) << c->PrettyClass(); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 6020 | } |
| 6021 | } else { |
| 6022 | self->AssertNoPendingException(); |
Ian Rogers | 595799e | 2012-01-11 17:32:51 -0800 | [diff] [blame] | 6023 | } |
| 6024 | return success; |
Elliott Hughes | f4c21c9 | 2011-08-19 17:31:31 -0700 | [diff] [blame] | 6025 | } |
| 6026 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6027 | void ClassLinker::FixupTemporaryDeclaringClass(ObjPtr<mirror::Class> temp_class, |
| 6028 | ObjPtr<mirror::Class> new_class) { |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 6029 | DCHECK_EQ(temp_class->NumInstanceFields(), 0u); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 6030 | for (ArtField& field : new_class->GetIFields()) { |
| 6031 | if (field.GetDeclaringClass() == temp_class) { |
| 6032 | field.SetDeclaringClass(new_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 6033 | } |
| 6034 | } |
| 6035 | |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 6036 | DCHECK_EQ(temp_class->NumStaticFields(), 0u); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 6037 | for (ArtField& field : new_class->GetSFields()) { |
| 6038 | if (field.GetDeclaringClass() == temp_class) { |
| 6039 | field.SetDeclaringClass(new_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 6040 | } |
| 6041 | } |
| 6042 | |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 6043 | DCHECK_EQ(temp_class->NumDirectMethods(), 0u); |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 6044 | DCHECK_EQ(temp_class->NumVirtualMethods(), 0u); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6045 | for (auto& method : new_class->GetMethods(image_pointer_size_)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6046 | if (method.GetDeclaringClass() == temp_class) { |
| 6047 | method.SetDeclaringClass(new_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 6048 | } |
| 6049 | } |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 6050 | |
| 6051 | // Make sure the remembered set and mod-union tables know that we updated some of the native |
| 6052 | // roots. |
Mathieu Chartier | 88ea61e | 2018-06-20 17:45:41 -0700 | [diff] [blame] | 6053 | WriteBarrier::ForEveryFieldWrite(new_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 6054 | } |
| 6055 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6056 | void ClassLinker::RegisterClassLoader(ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | 5b83050 | 2016-03-02 10:30:23 -0800 | [diff] [blame] | 6057 | CHECK(class_loader->GetAllocator() == nullptr); |
| 6058 | CHECK(class_loader->GetClassTable() == nullptr); |
| 6059 | Thread* const self = Thread::Current(); |
| 6060 | ClassLoaderData data; |
Ian Rogers | 55256cb | 2017-12-21 17:07:11 -0800 | [diff] [blame] | 6061 | data.weak_root = self->GetJniEnv()->GetVm()->AddWeakGlobalRef(self, class_loader); |
Mathieu Chartier | 5b83050 | 2016-03-02 10:30:23 -0800 | [diff] [blame] | 6062 | // Create and set the class table. |
| 6063 | data.class_table = new ClassTable; |
| 6064 | class_loader->SetClassTable(data.class_table); |
| 6065 | // Create and set the linear allocator. |
| 6066 | data.allocator = Runtime::Current()->CreateLinearAlloc(); |
| 6067 | class_loader->SetAllocator(data.allocator); |
| 6068 | // Add to the list so that we know to free the data later. |
| 6069 | class_loaders_.push_back(data); |
| 6070 | } |
| 6071 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6072 | ClassTable* ClassLinker::InsertClassTableForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 6073 | if (class_loader == nullptr) { |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 6074 | return boot_class_table_.get(); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 6075 | } |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 6076 | ClassTable* class_table = class_loader->GetClassTable(); |
| 6077 | if (class_table == nullptr) { |
Mathieu Chartier | 5b83050 | 2016-03-02 10:30:23 -0800 | [diff] [blame] | 6078 | RegisterClassLoader(class_loader); |
| 6079 | class_table = class_loader->GetClassTable(); |
| 6080 | DCHECK(class_table != nullptr); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 6081 | } |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 6082 | return class_table; |
| 6083 | } |
| 6084 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6085 | ClassTable* ClassLinker::ClassTableForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) { |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 6086 | return class_loader == nullptr ? boot_class_table_.get() : class_loader->GetClassTable(); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 6087 | } |
| 6088 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6089 | static ImTable* FindSuperImt(ObjPtr<mirror::Class> klass, PointerSize pointer_size) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 6090 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6091 | while (klass->HasSuperClass()) { |
| 6092 | klass = klass->GetSuperClass(); |
| 6093 | if (klass->ShouldHaveImt()) { |
| 6094 | return klass->GetImt(pointer_size); |
| 6095 | } |
| 6096 | } |
| 6097 | return nullptr; |
| 6098 | } |
| 6099 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 6100 | bool ClassLinker::LinkClass(Thread* self, |
| 6101 | const char* descriptor, |
| 6102 | Handle<mirror::Class> klass, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 6103 | Handle<mirror::ObjectArray<mirror::Class>> interfaces, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 6104 | MutableHandle<mirror::Class>* h_new_class_out) { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 6105 | CHECK_EQ(ClassStatus::kLoaded, klass->GetStatus()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 6106 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6107 | if (!LinkSuperClass(klass)) { |
| 6108 | return false; |
| 6109 | } |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6110 | ArtMethod* imt_data[ImTable::kSize]; |
| 6111 | // If there are any new conflicts compared to super class. |
| 6112 | bool new_conflict = false; |
Nicolas Geoffray | 918dcea | 2017-07-21 07:58:14 +0000 | [diff] [blame] | 6113 | std::fill_n(imt_data, arraysize(imt_data), Runtime::Current()->GetImtUnimplementedMethod()); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6114 | if (!LinkMethods(self, klass, interfaces, &new_conflict, imt_data)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6115 | return false; |
| 6116 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 6117 | if (!LinkInstanceFields(self, klass)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6118 | return false; |
| 6119 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 6120 | size_t class_size; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 6121 | if (!LinkStaticFields(self, klass, &class_size)) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 6122 | return false; |
| 6123 | } |
| 6124 | CreateReferenceInstanceOffsets(klass); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 6125 | CHECK_EQ(ClassStatus::kLoaded, klass->GetStatus()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 6126 | |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6127 | ImTable* imt = nullptr; |
| 6128 | if (klass->ShouldHaveImt()) { |
| 6129 | // If there are any new conflicts compared to the super class we can not make a copy. There |
| 6130 | // can be cases where both will have a conflict method at the same slot without having the same |
| 6131 | // set of conflicts. In this case, we can not share the IMT since the conflict table slow path |
| 6132 | // will possibly create a table that is incorrect for either of the classes. |
| 6133 | // Same IMT with new_conflict does not happen very often. |
| 6134 | if (!new_conflict) { |
| 6135 | ImTable* super_imt = FindSuperImt(klass.Get(), image_pointer_size_); |
| 6136 | if (super_imt != nullptr) { |
| 6137 | bool imt_equals = true; |
| 6138 | for (size_t i = 0; i < ImTable::kSize && imt_equals; ++i) { |
| 6139 | imt_equals = imt_equals && (super_imt->Get(i, image_pointer_size_) == imt_data[i]); |
| 6140 | } |
| 6141 | if (imt_equals) { |
| 6142 | imt = super_imt; |
| 6143 | } |
| 6144 | } |
| 6145 | } |
| 6146 | if (imt == nullptr) { |
| 6147 | LinearAlloc* allocator = GetAllocatorForClassLoader(klass->GetClassLoader()); |
| 6148 | imt = reinterpret_cast<ImTable*>( |
| 6149 | allocator->Alloc(self, ImTable::SizeInBytes(image_pointer_size_))); |
| 6150 | if (imt == nullptr) { |
| 6151 | return false; |
| 6152 | } |
| 6153 | imt->Populate(imt_data, image_pointer_size_); |
| 6154 | } |
| 6155 | } |
| 6156 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 6157 | if (!klass->IsTemp() || (!init_done_ && klass->GetClassSize() == class_size)) { |
| 6158 | // We don't need to retire this class as it has no embedded tables or it was created the |
| 6159 | // correct size during class linker initialization. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6160 | CHECK_EQ(klass->GetClassSize(), class_size) << klass->PrettyDescriptor(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 6161 | |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6162 | if (klass->ShouldHaveEmbeddedVTable()) { |
| 6163 | klass->PopulateEmbeddedVTable(image_pointer_size_); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 6164 | } |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6165 | if (klass->ShouldHaveImt()) { |
| 6166 | klass->SetImt(imt, image_pointer_size_); |
| 6167 | } |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 6168 | |
| 6169 | // Update CHA info based on whether we override methods. |
| 6170 | // Have to do this before setting the class as resolved which allows |
| 6171 | // instantiation of klass. |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 6172 | if (LIKELY(descriptor != nullptr) && cha_ != nullptr) { |
Andreas Gampe | c1ac9ee | 2017-07-24 22:35:49 -0700 | [diff] [blame] | 6173 | cha_->UpdateAfterLoadingOf(klass); |
| 6174 | } |
Nicolas Geoffray | 918dcea | 2017-07-21 07:58:14 +0000 | [diff] [blame] | 6175 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 6176 | // This will notify waiters on klass that saw the not yet resolved |
| 6177 | // class in the class_table_ during EnsureResolved. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 6178 | mirror::Class::SetStatus(klass, ClassStatus::kResolved, self); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 6179 | h_new_class_out->Assign(klass.Get()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 6180 | } else { |
| 6181 | CHECK(!klass->IsResolved()); |
| 6182 | // Retire the temporary class and create the correctly sized resolved class. |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 6183 | StackHandleScope<1> hs(self); |
Vladimir Marko | 3068d58 | 2019-05-28 16:39:29 +0100 | [diff] [blame] | 6184 | Handle<mirror::Class> h_new_class = |
| 6185 | hs.NewHandle(mirror::Class::CopyOf(klass, self, class_size, imt, image_pointer_size_)); |
Mathieu Chartier | 3ee25bb | 2015-08-10 10:13:02 -0700 | [diff] [blame] | 6186 | // Set arrays to null since we don't want to have multiple classes with the same ArtField or |
| 6187 | // ArtMethod array pointers. If this occurs, it causes bugs in remembered sets since the GC |
| 6188 | // may not see any references to the target space and clean the card for a class if another |
| 6189 | // class had the same array pointer. |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6190 | klass->SetMethodsPtrUnchecked(nullptr, 0, 0); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 6191 | klass->SetSFieldsPtrUnchecked(nullptr); |
| 6192 | klass->SetIFieldsPtrUnchecked(nullptr); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 6193 | if (UNLIKELY(h_new_class == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6194 | self->AssertPendingOOMException(); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 6195 | mirror::Class::SetStatus(klass, ClassStatus::kErrorUnresolved, self); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 6196 | return false; |
| 6197 | } |
| 6198 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 6199 | CHECK_EQ(h_new_class->GetClassSize(), class_size); |
| 6200 | ObjectLock<mirror::Class> lock(self, h_new_class); |
| 6201 | FixupTemporaryDeclaringClass(klass.Get(), h_new_class.Get()); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 6202 | |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 6203 | if (LIKELY(descriptor != nullptr)) { |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 6204 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Vladimir Marko | 0984e48 | 2019-03-27 16:41:41 +0000 | [diff] [blame] | 6205 | const ObjPtr<mirror::ClassLoader> class_loader = h_new_class.Get()->GetClassLoader(); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 6206 | ClassTable* const table = InsertClassTableForClassLoader(class_loader); |
Vladimir Marko | 0984e48 | 2019-03-27 16:41:41 +0000 | [diff] [blame] | 6207 | const ObjPtr<mirror::Class> existing = |
| 6208 | table->UpdateClass(descriptor, h_new_class.Get(), ComputeModifiedUtf8Hash(descriptor)); |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 6209 | if (class_loader != nullptr) { |
| 6210 | // We updated the class in the class table, perform the write barrier so that the GC knows |
| 6211 | // about the change. |
Mathieu Chartier | 88ea61e | 2018-06-20 17:45:41 -0700 | [diff] [blame] | 6212 | WriteBarrier::ForEveryFieldWrite(class_loader); |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 6213 | } |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 6214 | CHECK_EQ(existing, klass.Get()); |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 6215 | if (log_new_roots_) { |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 6216 | new_class_roots_.push_back(GcRoot<mirror::Class>(h_new_class.Get())); |
| 6217 | } |
| 6218 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 6219 | |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 6220 | // Update CHA info based on whether we override methods. |
| 6221 | // Have to do this before setting the class as resolved which allows |
| 6222 | // instantiation of klass. |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 6223 | if (LIKELY(descriptor != nullptr) && cha_ != nullptr) { |
Andreas Gampe | c1ac9ee | 2017-07-24 22:35:49 -0700 | [diff] [blame] | 6224 | cha_->UpdateAfterLoadingOf(h_new_class); |
| 6225 | } |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 6226 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 6227 | // This will notify waiters on temp class that saw the not yet resolved class in the |
| 6228 | // class_table_ during EnsureResolved. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 6229 | mirror::Class::SetStatus(klass, ClassStatus::kRetired, self); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 6230 | |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 6231 | CHECK_EQ(h_new_class->GetStatus(), ClassStatus::kResolving); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 6232 | // This will notify waiters on new_class that saw the not yet resolved |
| 6233 | // class in the class_table_ during EnsureResolved. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 6234 | mirror::Class::SetStatus(h_new_class, ClassStatus::kResolved, self); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 6235 | // Return the new class. |
| 6236 | h_new_class_out->Assign(h_new_class.Get()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 6237 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6238 | return true; |
| 6239 | } |
| 6240 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 6241 | bool ClassLinker::LoadSuperAndInterfaces(Handle<mirror::Class> klass, const DexFile& dex_file) { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 6242 | CHECK_EQ(ClassStatus::kIdx, klass->GetStatus()); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 6243 | const dex::ClassDef& class_def = dex_file.GetClassDef(klass->GetDexClassDefIndex()); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 6244 | dex::TypeIndex super_class_idx = class_def.superclass_idx_; |
| 6245 | if (super_class_idx.IsValid()) { |
Roland Levillain | 90328ac | 2016-05-18 12:25:38 +0100 | [diff] [blame] | 6246 | // Check that a class does not inherit from itself directly. |
| 6247 | // |
| 6248 | // TODO: This is a cheap check to detect the straightforward case |
| 6249 | // of a class extending itself (b/28685551), but we should do a |
| 6250 | // proper cycle detection on loaded classes, to detect all cases |
| 6251 | // of class circularity errors (b/28830038). |
| 6252 | if (super_class_idx == class_def.class_idx_) { |
| 6253 | ThrowClassCircularityError(klass.Get(), |
| 6254 | "Class %s extends itself", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6255 | klass->PrettyDescriptor().c_str()); |
Roland Levillain | 90328ac | 2016-05-18 12:25:38 +0100 | [diff] [blame] | 6256 | return false; |
| 6257 | } |
| 6258 | |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 6259 | ObjPtr<mirror::Class> super_class = ResolveType(super_class_idx, klass.Get()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 6260 | if (super_class == nullptr) { |
Brian Carlstrom | 65ca077 | 2011-09-24 16:03:08 -0700 | [diff] [blame] | 6261 | DCHECK(Thread::Current()->IsExceptionPending()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6262 | return false; |
| 6263 | } |
Ian Rogers | be125a9 | 2012-01-11 15:19:49 -0800 | [diff] [blame] | 6264 | // Verify |
| 6265 | if (!klass->CanAccess(super_class)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 6266 | ThrowIllegalAccessError(klass.Get(), "Class %s extended by class %s is inaccessible", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6267 | super_class->PrettyDescriptor().c_str(), |
| 6268 | klass->PrettyDescriptor().c_str()); |
Ian Rogers | be125a9 | 2012-01-11 15:19:49 -0800 | [diff] [blame] | 6269 | return false; |
| 6270 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 6271 | CHECK(super_class->IsResolved()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 6272 | klass->SetSuperClass(super_class); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6273 | } |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 6274 | const dex::TypeList* interfaces = dex_file.GetInterfacesList(class_def); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 6275 | if (interfaces != nullptr) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 6276 | for (size_t i = 0; i < interfaces->Size(); i++) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 6277 | dex::TypeIndex idx = interfaces->GetTypeItem(i).type_idx_; |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 6278 | ObjPtr<mirror::Class> interface = ResolveType(idx, klass.Get()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 6279 | if (interface == nullptr) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 6280 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 6281 | return false; |
| 6282 | } |
| 6283 | // Verify |
| 6284 | if (!klass->CanAccess(interface)) { |
| 6285 | // TODO: the RI seemed to ignore this in my testing. |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 6286 | ThrowIllegalAccessError(klass.Get(), |
| 6287 | "Interface %s implemented by class %s is inaccessible", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6288 | interface->PrettyDescriptor().c_str(), |
| 6289 | klass->PrettyDescriptor().c_str()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 6290 | return false; |
| 6291 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6292 | } |
| 6293 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 6294 | // Mark the class as loaded. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 6295 | mirror::Class::SetStatus(klass, ClassStatus::kLoaded, nullptr); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6296 | return true; |
| 6297 | } |
| 6298 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 6299 | bool ClassLinker::LinkSuperClass(Handle<mirror::Class> klass) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6300 | CHECK(!klass->IsPrimitive()); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6301 | ObjPtr<mirror::Class> super = klass->GetSuperClass(); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 6302 | ObjPtr<mirror::Class> object_class = GetClassRoot<mirror::Object>(this); |
| 6303 | if (klass.Get() == object_class) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 6304 | if (super != nullptr) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 6305 | ThrowClassFormatError(klass.Get(), "java.lang.Object must not have a superclass"); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6306 | return false; |
| 6307 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6308 | return true; |
| 6309 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 6310 | if (super == nullptr) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 6311 | ThrowLinkageError(klass.Get(), "No superclass defined for class %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6312 | klass->PrettyDescriptor().c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6313 | return false; |
| 6314 | } |
| 6315 | // Verify |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 6316 | if (klass->IsInterface() && super != object_class) { |
Vladimir Marko | 1fcae9f | 2017-11-28 14:14:19 +0000 | [diff] [blame] | 6317 | ThrowClassFormatError(klass.Get(), "Interfaces must have java.lang.Object as superclass"); |
| 6318 | return false; |
| 6319 | } |
Vladimir Marko | b43b2d8 | 2017-07-18 17:46:38 +0100 | [diff] [blame] | 6320 | if (super->IsFinal()) { |
| 6321 | ThrowVerifyError(klass.Get(), |
| 6322 | "Superclass %s of %s is declared final", |
| 6323 | super->PrettyDescriptor().c_str(), |
| 6324 | klass->PrettyDescriptor().c_str()); |
| 6325 | return false; |
| 6326 | } |
| 6327 | if (super->IsInterface()) { |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 6328 | ThrowIncompatibleClassChangeError(klass.Get(), |
Vladimir Marko | b43b2d8 | 2017-07-18 17:46:38 +0100 | [diff] [blame] | 6329 | "Superclass %s of %s is an interface", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6330 | super->PrettyDescriptor().c_str(), |
Vladimir Marko | b43b2d8 | 2017-07-18 17:46:38 +0100 | [diff] [blame] | 6331 | klass->PrettyDescriptor().c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6332 | return false; |
| 6333 | } |
| 6334 | if (!klass->CanAccess(super)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 6335 | ThrowIllegalAccessError(klass.Get(), "Superclass %s is inaccessible to class %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6336 | super->PrettyDescriptor().c_str(), |
| 6337 | klass->PrettyDescriptor().c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6338 | return false; |
| 6339 | } |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 6340 | |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 6341 | // Inherit kAccClassIsFinalizable from the superclass in case this |
| 6342 | // class doesn't override finalize. |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 6343 | if (super->IsFinalizable()) { |
| 6344 | klass->SetFinalizable(); |
| 6345 | } |
| 6346 | |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 6347 | // Inherit class loader flag form super class. |
| 6348 | if (super->IsClassLoaderClass()) { |
| 6349 | klass->SetClassLoaderClass(); |
| 6350 | } |
| 6351 | |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 6352 | // Inherit reference flags (if any) from the superclass. |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 6353 | uint32_t reference_flags = (super->GetClassFlags() & mirror::kClassFlagReference); |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 6354 | if (reference_flags != 0) { |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 6355 | CHECK_EQ(klass->GetClassFlags(), 0u); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 6356 | klass->SetClassFlags(klass->GetClassFlags() | reference_flags); |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 6357 | } |
Elliott Hughes | 72ee0ae | 2011-10-10 17:31:28 -0700 | [diff] [blame] | 6358 | // Disallow custom direct subclasses of java.lang.ref.Reference. |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 6359 | if (init_done_ && super == GetClassRoot<mirror::Reference>(this)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 6360 | ThrowLinkageError(klass.Get(), |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 6361 | "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] | 6362 | klass->PrettyDescriptor().c_str()); |
Elliott Hughes | 72ee0ae | 2011-10-10 17:31:28 -0700 | [diff] [blame] | 6363 | return false; |
| 6364 | } |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 6365 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 6366 | if (kIsDebugBuild) { |
| 6367 | // Ensure super classes are fully resolved prior to resolving fields.. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 6368 | while (super != nullptr) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 6369 | CHECK(super->IsResolved()); |
| 6370 | super = super->GetSuperClass(); |
| 6371 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 6372 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6373 | return true; |
| 6374 | } |
| 6375 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 6376 | // Populate the class vtable and itable. Compute return type indices. |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 6377 | bool ClassLinker::LinkMethods(Thread* self, |
| 6378 | Handle<mirror::Class> klass, |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6379 | Handle<mirror::ObjectArray<mirror::Class>> interfaces, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6380 | bool* out_new_conflict, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 6381 | ArtMethod** out_imt) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 6382 | self->AllowThreadSuspension(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6383 | // A map from vtable indexes to the method they need to be updated to point to. Used because we |
| 6384 | // need to have default methods be in the virtuals array of each class but we don't set that up |
| 6385 | // until LinkInterfaceMethods. |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6386 | std::unordered_map<size_t, ClassLinker::MethodTranslation> default_translations; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6387 | // Link virtual methods then interface methods. |
| 6388 | // We set up the interface lookup table first because we need it to determine if we need to update |
| 6389 | // any vtable entries with new default method implementations. |
| 6390 | return SetupInterfaceLookupTable(self, klass, interfaces) |
| 6391 | && LinkVirtualMethods(self, klass, /*out*/ &default_translations) |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6392 | && LinkInterfaceMethods(self, klass, default_translations, out_new_conflict, out_imt); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6393 | } |
| 6394 | |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 6395 | // Comparator for name and signature of a method, used in finding overriding methods. Implementation |
| 6396 | // avoids the use of handles, if it didn't then rather than compare dex files we could compare dex |
| 6397 | // caches in the implementation below. |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 6398 | class MethodNameAndSignatureComparator final : public ValueObject { |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 6399 | public: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6400 | explicit MethodNameAndSignatureComparator(ArtMethod* method) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 6401 | REQUIRES_SHARED(Locks::mutator_lock_) : |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 6402 | dex_file_(method->GetDexFile()), mid_(&dex_file_->GetMethodId(method->GetDexMethodIndex())), |
| 6403 | name_(nullptr), name_len_(0) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6404 | DCHECK(!method->IsProxyMethod()) << method->PrettyMethod(); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6405 | } |
| 6406 | |
| 6407 | const char* GetName() { |
| 6408 | if (name_ == nullptr) { |
| 6409 | name_ = dex_file_->StringDataAndUtf16LengthByIdx(mid_->name_idx_, &name_len_); |
| 6410 | } |
| 6411 | return name_; |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 6412 | } |
| 6413 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6414 | bool HasSameNameAndSignature(ArtMethod* other) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 6415 | REQUIRES_SHARED(Locks::mutator_lock_) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6416 | DCHECK(!other->IsProxyMethod()) << other->PrettyMethod(); |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 6417 | const DexFile* other_dex_file = other->GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 6418 | const dex::MethodId& other_mid = other_dex_file->GetMethodId(other->GetDexMethodIndex()); |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 6419 | if (dex_file_ == other_dex_file) { |
| 6420 | return mid_->name_idx_ == other_mid.name_idx_ && mid_->proto_idx_ == other_mid.proto_idx_; |
| 6421 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6422 | GetName(); // Only used to make sure its calculated. |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 6423 | uint32_t other_name_len; |
| 6424 | const char* other_name = other_dex_file->StringDataAndUtf16LengthByIdx(other_mid.name_idx_, |
| 6425 | &other_name_len); |
| 6426 | if (name_len_ != other_name_len || strcmp(name_, other_name) != 0) { |
| 6427 | return false; |
| 6428 | } |
| 6429 | return dex_file_->GetMethodSignature(*mid_) == other_dex_file->GetMethodSignature(other_mid); |
| 6430 | } |
| 6431 | |
| 6432 | private: |
| 6433 | // Dex file for the method to compare against. |
| 6434 | const DexFile* const dex_file_; |
| 6435 | // MethodId for the method to compare against. |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 6436 | const dex::MethodId* const mid_; |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 6437 | // Lazily computed name from the dex file's strings. |
| 6438 | const char* name_; |
| 6439 | // Lazily computed name length. |
| 6440 | uint32_t name_len_; |
| 6441 | }; |
| 6442 | |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6443 | class LinkVirtualHashTable { |
| 6444 | public: |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 6445 | LinkVirtualHashTable(Handle<mirror::Class> klass, |
| 6446 | size_t hash_size, |
| 6447 | uint32_t* hash_table, |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 6448 | PointerSize image_pointer_size) |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 6449 | : klass_(klass), |
| 6450 | hash_size_(hash_size), |
| 6451 | hash_table_(hash_table), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6452 | image_pointer_size_(image_pointer_size) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6453 | std::fill(hash_table_, hash_table_ + hash_size_, invalid_index_); |
| 6454 | } |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 6455 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 6456 | void Add(uint32_t virtual_method_index) REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6457 | ArtMethod* local_method = klass_->GetVirtualMethodDuringLinking( |
| 6458 | virtual_method_index, image_pointer_size_); |
| 6459 | const char* name = local_method->GetInterfaceMethodIfProxy(image_pointer_size_)->GetName(); |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 6460 | uint32_t hash = ComputeModifiedUtf8Hash(name); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6461 | uint32_t index = hash % hash_size_; |
| 6462 | // Linear probe until we have an empty slot. |
| 6463 | while (hash_table_[index] != invalid_index_) { |
| 6464 | if (++index == hash_size_) { |
| 6465 | index = 0; |
| 6466 | } |
| 6467 | } |
| 6468 | hash_table_[index] = virtual_method_index; |
| 6469 | } |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 6470 | |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6471 | uint32_t FindAndRemove(MethodNameAndSignatureComparator* comparator) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 6472 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6473 | const char* name = comparator->GetName(); |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 6474 | uint32_t hash = ComputeModifiedUtf8Hash(name); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6475 | size_t index = hash % hash_size_; |
| 6476 | while (true) { |
| 6477 | const uint32_t value = hash_table_[index]; |
| 6478 | // Since linear probe makes continuous blocks, hitting an invalid index means we are done |
| 6479 | // the block and can safely assume not found. |
| 6480 | if (value == invalid_index_) { |
| 6481 | break; |
| 6482 | } |
| 6483 | if (value != removed_index_) { // This signifies not already overriden. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6484 | ArtMethod* virtual_method = |
| 6485 | klass_->GetVirtualMethodDuringLinking(value, image_pointer_size_); |
| 6486 | if (comparator->HasSameNameAndSignature( |
| 6487 | virtual_method->GetInterfaceMethodIfProxy(image_pointer_size_))) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6488 | hash_table_[index] = removed_index_; |
| 6489 | return value; |
| 6490 | } |
| 6491 | } |
| 6492 | if (++index == hash_size_) { |
| 6493 | index = 0; |
| 6494 | } |
| 6495 | } |
| 6496 | return GetNotFoundIndex(); |
| 6497 | } |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 6498 | |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6499 | static uint32_t GetNotFoundIndex() { |
| 6500 | return invalid_index_; |
| 6501 | } |
| 6502 | |
| 6503 | private: |
| 6504 | static const uint32_t invalid_index_; |
| 6505 | static const uint32_t removed_index_; |
| 6506 | |
| 6507 | Handle<mirror::Class> klass_; |
| 6508 | const size_t hash_size_; |
| 6509 | uint32_t* const hash_table_; |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 6510 | const PointerSize image_pointer_size_; |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6511 | }; |
| 6512 | |
| 6513 | const uint32_t LinkVirtualHashTable::invalid_index_ = std::numeric_limits<uint32_t>::max(); |
| 6514 | const uint32_t LinkVirtualHashTable::removed_index_ = std::numeric_limits<uint32_t>::max() - 1; |
| 6515 | |
Stephen Hines | 1ddd913 | 2017-02-08 01:51:18 -0800 | [diff] [blame] | 6516 | bool ClassLinker::LinkVirtualMethods( |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6517 | Thread* self, |
| 6518 | Handle<mirror::Class> klass, |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6519 | /*out*/std::unordered_map<size_t, ClassLinker::MethodTranslation>* default_translations) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6520 | const size_t num_virtual_methods = klass->NumVirtualMethods(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6521 | if (klass->IsInterface()) { |
| 6522 | // No vtable. |
| 6523 | if (!IsUint<16>(num_virtual_methods)) { |
| 6524 | ThrowClassFormatError(klass.Get(), "Too many methods on interface: %zu", num_virtual_methods); |
| 6525 | return false; |
| 6526 | } |
| 6527 | bool has_defaults = false; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6528 | // Assign each method an IMT index and set the default flag. |
| 6529 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
| 6530 | ArtMethod* m = klass->GetVirtualMethodDuringLinking(i, image_pointer_size_); |
| 6531 | m->SetMethodIndex(i); |
| 6532 | if (!m->IsAbstract()) { |
| 6533 | m->SetAccessFlags(m->GetAccessFlags() | kAccDefault); |
| 6534 | has_defaults = true; |
| 6535 | } |
| 6536 | } |
| 6537 | // Mark that we have default methods so that we won't need to scan the virtual_methods_ array |
| 6538 | // during initialization. This is a performance optimization. We could simply traverse the |
| 6539 | // virtual_methods_ array again during initialization. |
| 6540 | if (has_defaults) { |
| 6541 | klass->SetHasDefaultMethods(); |
| 6542 | } |
| 6543 | return true; |
| 6544 | } else if (klass->HasSuperClass()) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6545 | const size_t super_vtable_length = klass->GetSuperClass()->GetVTableLength(); |
| 6546 | const size_t max_count = num_virtual_methods + super_vtable_length; |
Vladimir Marko | 3068d58 | 2019-05-28 16:39:29 +0100 | [diff] [blame] | 6547 | StackHandleScope<3> hs(self); |
Mingyao Yang | 38eecb0 | 2014-08-13 14:51:03 -0700 | [diff] [blame] | 6548 | Handle<mirror::Class> super_class(hs.NewHandle(klass->GetSuperClass())); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6549 | MutableHandle<mirror::PointerArray> vtable; |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6550 | if (super_class->ShouldHaveEmbeddedVTable()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6551 | vtable = hs.NewHandle(AllocPointerArray(self, max_count)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 6552 | if (UNLIKELY(vtable == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6553 | self->AssertPendingOOMException(); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 6554 | return false; |
| 6555 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6556 | for (size_t i = 0; i < super_vtable_length; i++) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6557 | vtable->SetElementPtrSize( |
| 6558 | i, super_class->GetEmbeddedVTableEntry(i, image_pointer_size_), image_pointer_size_); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 6559 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6560 | // We might need to change vtable if we have new virtual methods or new interfaces (since that |
| 6561 | // might give us new default methods). If no new interfaces then we can skip the rest since |
| 6562 | // the class cannot override any of the super-class's methods. This is required for |
| 6563 | // correctness since without it we might not update overridden default method vtable entries |
| 6564 | // correctly. |
| 6565 | if (num_virtual_methods == 0 && super_class->GetIfTableCount() == klass->GetIfTableCount()) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6566 | klass->SetVTable(vtable.Get()); |
| 6567 | return true; |
| 6568 | } |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 6569 | } else { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6570 | DCHECK(super_class->IsAbstract() && !super_class->IsArrayClass()); |
Vladimir Marko | 3068d58 | 2019-05-28 16:39:29 +0100 | [diff] [blame] | 6571 | Handle<mirror::PointerArray> super_vtable = hs.NewHandle(super_class->GetVTable()); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6572 | CHECK(super_vtable != nullptr) << super_class->PrettyClass(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6573 | // We might need to change vtable if we have new virtual methods or new interfaces (since that |
| 6574 | // might give us new default methods). See comment above. |
| 6575 | if (num_virtual_methods == 0 && super_class->GetIfTableCount() == klass->GetIfTableCount()) { |
Vladimir Marko | 3068d58 | 2019-05-28 16:39:29 +0100 | [diff] [blame] | 6576 | klass->SetVTable(super_vtable.Get()); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6577 | return true; |
| 6578 | } |
Vladimir Marko | 3068d58 | 2019-05-28 16:39:29 +0100 | [diff] [blame] | 6579 | vtable = hs.NewHandle(ObjPtr<mirror::PointerArray>::DownCast( |
| 6580 | mirror::Array::CopyOf(super_vtable, self, max_count))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 6581 | if (UNLIKELY(vtable == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6582 | self->AssertPendingOOMException(); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 6583 | return false; |
| 6584 | } |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 6585 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6586 | // How the algorithm works: |
| 6587 | // 1. Populate hash table by adding num_virtual_methods from klass. The values in the hash |
| 6588 | // table are: invalid_index for unused slots, index super_vtable_length + i for a virtual |
| 6589 | // method which has not been matched to a vtable method, and j if the virtual method at the |
| 6590 | // index overrode the super virtual method at index j. |
| 6591 | // 2. Loop through super virtual methods, if they overwrite, update hash table to j |
| 6592 | // (j < super_vtable_length) to avoid redundant checks. (TODO maybe use this info for reducing |
| 6593 | // the need for the initial vtable which we later shrink back down). |
| 6594 | // 3. Add non overridden methods to the end of the vtable. |
| 6595 | static constexpr size_t kMaxStackHash = 250; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6596 | // + 1 so that even if we only have new default methods we will still be able to use this hash |
| 6597 | // table (i.e. it will never have 0 size). |
| 6598 | const size_t hash_table_size = num_virtual_methods * 3 + 1; |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6599 | uint32_t* hash_table_ptr; |
| 6600 | std::unique_ptr<uint32_t[]> hash_heap_storage; |
| 6601 | if (hash_table_size <= kMaxStackHash) { |
| 6602 | hash_table_ptr = reinterpret_cast<uint32_t*>( |
| 6603 | alloca(hash_table_size * sizeof(*hash_table_ptr))); |
| 6604 | } else { |
| 6605 | hash_heap_storage.reset(new uint32_t[hash_table_size]); |
| 6606 | hash_table_ptr = hash_heap_storage.get(); |
| 6607 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6608 | 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] | 6609 | // Add virtual methods to the hash table. |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6610 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6611 | DCHECK(klass->GetVirtualMethodDuringLinking( |
| 6612 | i, image_pointer_size_)->GetDeclaringClass() != nullptr); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6613 | hash_table.Add(i); |
| 6614 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6615 | // 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] | 6616 | // the hash table. |
| 6617 | for (size_t j = 0; j < super_vtable_length; ++j) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6618 | // 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] | 6619 | ArtMethod* super_method = vtable->GetElementPtrSize<ArtMethod*>(j, image_pointer_size_); |
Alex Light | c7a420c | 2016-10-18 14:33:18 -0700 | [diff] [blame] | 6620 | if (!klass->CanAccessMember(super_method->GetDeclaringClass(), |
| 6621 | super_method->GetAccessFlags())) { |
| 6622 | // Continue on to the next method since this one is package private and canot be overridden. |
| 6623 | // Before Android 4.1, the package-private method super_method might have been incorrectly |
| 6624 | // overridden. |
| 6625 | continue; |
| 6626 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6627 | MethodNameAndSignatureComparator super_method_name_comparator( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6628 | super_method->GetInterfaceMethodIfProxy(image_pointer_size_)); |
Alex Light | c7a420c | 2016-10-18 14:33:18 -0700 | [diff] [blame] | 6629 | // We remove the method so that subsequent lookups will be faster by making the hash-map |
| 6630 | // smaller as we go on. |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6631 | uint32_t hash_index = hash_table.FindAndRemove(&super_method_name_comparator); |
| 6632 | if (hash_index != hash_table.GetNotFoundIndex()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6633 | ArtMethod* virtual_method = klass->GetVirtualMethodDuringLinking( |
| 6634 | hash_index, image_pointer_size_); |
Alex Light | c7a420c | 2016-10-18 14:33:18 -0700 | [diff] [blame] | 6635 | if (super_method->IsFinal()) { |
| 6636 | ThrowLinkageError(klass.Get(), "Method %s overrides final method in class %s", |
| 6637 | virtual_method->PrettyMethod().c_str(), |
| 6638 | super_method->GetDeclaringClassDescriptor()); |
| 6639 | return false; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6640 | } |
Alex Light | c7a420c | 2016-10-18 14:33:18 -0700 | [diff] [blame] | 6641 | vtable->SetElementPtrSize(j, virtual_method, image_pointer_size_); |
| 6642 | virtual_method->SetMethodIndex(j); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6643 | } else if (super_method->IsOverridableByDefaultMethod()) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6644 | // We didn't directly override this method but we might through default methods... |
| 6645 | // Check for default method update. |
| 6646 | ArtMethod* default_method = nullptr; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6647 | switch (FindDefaultMethodImplementation(self, |
| 6648 | super_method, |
| 6649 | klass, |
| 6650 | /*out*/&default_method)) { |
| 6651 | case DefaultMethodSearchResult::kDefaultConflict: { |
| 6652 | // A conflict was found looking for default methods. Note this (assuming it wasn't |
| 6653 | // pre-existing) in the translations map. |
| 6654 | if (UNLIKELY(!super_method->IsDefaultConflicting())) { |
| 6655 | // Don't generate another conflict method to reduce memory use as an optimization. |
| 6656 | default_translations->insert( |
| 6657 | {j, ClassLinker::MethodTranslation::CreateConflictingMethod()}); |
| 6658 | } |
| 6659 | break; |
| 6660 | } |
| 6661 | case DefaultMethodSearchResult::kAbstractFound: { |
| 6662 | // No conflict but method is abstract. |
| 6663 | // We note that this vtable entry must be made abstract. |
| 6664 | if (UNLIKELY(!super_method->IsAbstract())) { |
| 6665 | default_translations->insert( |
| 6666 | {j, ClassLinker::MethodTranslation::CreateAbstractMethod()}); |
| 6667 | } |
| 6668 | break; |
| 6669 | } |
| 6670 | case DefaultMethodSearchResult::kDefaultFound: { |
| 6671 | if (UNLIKELY(super_method->IsDefaultConflicting() || |
| 6672 | default_method->GetDeclaringClass() != super_method->GetDeclaringClass())) { |
| 6673 | // Found a default method implementation that is new. |
| 6674 | // TODO Refactor this add default methods to virtuals here and not in |
| 6675 | // LinkInterfaceMethods maybe. |
| 6676 | // The problem is default methods might override previously present |
| 6677 | // default-method or miranda-method vtable entries from the superclass. |
| 6678 | // Unfortunately we need these to be entries in this class's virtuals. We do not |
| 6679 | // give these entries there until LinkInterfaceMethods so we pass this map around |
| 6680 | // to let it know which vtable entries need to be updated. |
| 6681 | // Make a note that vtable entry j must be updated, store what it needs to be updated |
| 6682 | // to. We will allocate a virtual method slot in LinkInterfaceMethods and fix it up |
| 6683 | // then. |
| 6684 | default_translations->insert( |
| 6685 | {j, ClassLinker::MethodTranslation::CreateTranslatedMethod(default_method)}); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6686 | VLOG(class_linker) << "Method " << super_method->PrettyMethod() |
| 6687 | << " overridden by default " |
| 6688 | << default_method->PrettyMethod() |
| 6689 | << " in " << mirror::Class::PrettyClass(klass.Get()); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6690 | } |
| 6691 | break; |
| 6692 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6693 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6694 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6695 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6696 | size_t actual_count = super_vtable_length; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6697 | // Add the non-overridden methods at the end. |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6698 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6699 | ArtMethod* local_method = klass->GetVirtualMethodDuringLinking(i, image_pointer_size_); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6700 | size_t method_idx = local_method->GetMethodIndexDuringLinking(); |
| 6701 | if (method_idx < super_vtable_length && |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6702 | local_method == vtable->GetElementPtrSize<ArtMethod*>(method_idx, image_pointer_size_)) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6703 | continue; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6704 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6705 | vtable->SetElementPtrSize(actual_count, local_method, image_pointer_size_); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6706 | local_method->SetMethodIndex(actual_count); |
| 6707 | ++actual_count; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6708 | } |
Andreas Gampe | ab1eb0d | 2015-02-13 19:23:55 -0800 | [diff] [blame] | 6709 | if (!IsUint<16>(actual_count)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 6710 | ThrowClassFormatError(klass.Get(), "Too many methods defined on class: %zd", actual_count); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6711 | return false; |
| 6712 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 6713 | // Shrink vtable if possible |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6714 | CHECK_LE(actual_count, max_count); |
| 6715 | if (actual_count < max_count) { |
Vladimir Marko | 3068d58 | 2019-05-28 16:39:29 +0100 | [diff] [blame] | 6716 | vtable.Assign(ObjPtr<mirror::PointerArray>::DownCast( |
| 6717 | mirror::Array::CopyOf(vtable, self, actual_count))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 6718 | if (UNLIKELY(vtable == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6719 | self->AssertPendingOOMException(); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 6720 | return false; |
| 6721 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6722 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 6723 | klass->SetVTable(vtable.Get()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6724 | } else { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 6725 | CHECK_EQ(klass.Get(), GetClassRoot<mirror::Object>(this)); |
Andreas Gampe | ab1eb0d | 2015-02-13 19:23:55 -0800 | [diff] [blame] | 6726 | if (!IsUint<16>(num_virtual_methods)) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6727 | ThrowClassFormatError(klass.Get(), "Too many methods: %d", |
| 6728 | static_cast<int>(num_virtual_methods)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6729 | return false; |
| 6730 | } |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 6731 | ObjPtr<mirror::PointerArray> vtable = AllocPointerArray(self, num_virtual_methods); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6732 | if (UNLIKELY(vtable == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6733 | self->AssertPendingOOMException(); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 6734 | return false; |
| 6735 | } |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 6736 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6737 | ArtMethod* virtual_method = klass->GetVirtualMethodDuringLinking(i, image_pointer_size_); |
| 6738 | vtable->SetElementPtrSize(i, virtual_method, image_pointer_size_); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 6739 | virtual_method->SetMethodIndex(i & 0xFFFF); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6740 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6741 | klass->SetVTable(vtable); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6742 | } |
| 6743 | return true; |
| 6744 | } |
| 6745 | |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6746 | // Determine if the given iface has any subinterface in the given list that declares the method |
| 6747 | // specified by 'target'. |
| 6748 | // |
| 6749 | // Arguments |
| 6750 | // - self: The thread we are running on |
| 6751 | // - target: A comparator that will match any method that overrides the method we are checking for |
| 6752 | // - iftable: The iftable we are searching for an overriding method on. |
| 6753 | // - ifstart: The index of the interface we are checking to see if anything overrides |
| 6754 | // - iface: The interface we are checking to see if anything overrides. |
| 6755 | // - image_pointer_size: |
| 6756 | // The image pointer size. |
| 6757 | // |
| 6758 | // Returns |
| 6759 | // - True: There is some method that matches the target comparator defined in an interface that |
| 6760 | // is a subtype of iface. |
| 6761 | // - False: There is no method that matches the target comparator in any interface that is a subtype |
| 6762 | // of iface. |
| 6763 | static bool ContainsOverridingMethodOf(Thread* self, |
| 6764 | MethodNameAndSignatureComparator& target, |
| 6765 | Handle<mirror::IfTable> iftable, |
| 6766 | size_t ifstart, |
| 6767 | Handle<mirror::Class> iface, |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 6768 | PointerSize image_pointer_size) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 6769 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6770 | DCHECK(self != nullptr); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 6771 | DCHECK(iface != nullptr); |
| 6772 | DCHECK(iftable != nullptr); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6773 | DCHECK_GE(ifstart, 0u); |
| 6774 | DCHECK_LT(ifstart, iftable->Count()); |
| 6775 | DCHECK_EQ(iface.Get(), iftable->GetInterface(ifstart)); |
| 6776 | DCHECK(iface->IsInterface()); |
| 6777 | |
| 6778 | size_t iftable_count = iftable->Count(); |
| 6779 | StackHandleScope<1> hs(self); |
| 6780 | MutableHandle<mirror::Class> current_iface(hs.NewHandle<mirror::Class>(nullptr)); |
| 6781 | for (size_t k = ifstart + 1; k < iftable_count; k++) { |
| 6782 | // Skip ifstart since our current interface obviously cannot override itself. |
| 6783 | current_iface.Assign(iftable->GetInterface(k)); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6784 | // Iterate through every method on this interface. The order does not matter. |
| 6785 | for (ArtMethod& current_method : current_iface->GetDeclaredVirtualMethods(image_pointer_size)) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6786 | if (UNLIKELY(target.HasSameNameAndSignature( |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6787 | current_method.GetInterfaceMethodIfProxy(image_pointer_size)))) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6788 | // Check if the i'th interface is a subtype of this one. |
| 6789 | if (iface->IsAssignableFrom(current_iface.Get())) { |
| 6790 | return true; |
| 6791 | } |
| 6792 | break; |
| 6793 | } |
| 6794 | } |
| 6795 | } |
| 6796 | return false; |
| 6797 | } |
| 6798 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6799 | // 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] | 6800 | // out_default_method and returns kDefaultFound on success. If no default method was found return |
| 6801 | // kAbstractFound and store nullptr into out_default_method. If an error occurs (such as a |
| 6802 | // default_method conflict) it will return kDefaultConflict. |
| 6803 | ClassLinker::DefaultMethodSearchResult ClassLinker::FindDefaultMethodImplementation( |
| 6804 | Thread* self, |
| 6805 | ArtMethod* target_method, |
| 6806 | Handle<mirror::Class> klass, |
| 6807 | /*out*/ArtMethod** out_default_method) const { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6808 | DCHECK(self != nullptr); |
| 6809 | DCHECK(target_method != nullptr); |
| 6810 | DCHECK(out_default_method != nullptr); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6811 | |
| 6812 | *out_default_method = nullptr; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6813 | |
| 6814 | // We organize the interface table so that, for interface I any subinterfaces J follow it in the |
| 6815 | // table. This lets us walk the table backwards when searching for default methods. The first one |
| 6816 | // we encounter is the best candidate since it is the most specific. Once we have found it we keep |
| 6817 | // track of it and then continue checking all other interfaces, since we need to throw an error if |
| 6818 | // we encounter conflicting default method implementations (one is not a subtype of the other). |
| 6819 | // |
| 6820 | // The order of unrelated interfaces does not matter and is not defined. |
| 6821 | size_t iftable_count = klass->GetIfTableCount(); |
| 6822 | if (iftable_count == 0) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6823 | // No interfaces. We have already reset out to null so just return kAbstractFound. |
| 6824 | return DefaultMethodSearchResult::kAbstractFound; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6825 | } |
| 6826 | |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6827 | StackHandleScope<3> hs(self); |
| 6828 | MutableHandle<mirror::Class> chosen_iface(hs.NewHandle<mirror::Class>(nullptr)); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6829 | MutableHandle<mirror::IfTable> iftable(hs.NewHandle(klass->GetIfTable())); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6830 | MutableHandle<mirror::Class> iface(hs.NewHandle<mirror::Class>(nullptr)); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6831 | MethodNameAndSignatureComparator target_name_comparator( |
| 6832 | target_method->GetInterfaceMethodIfProxy(image_pointer_size_)); |
| 6833 | // Iterates over the klass's iftable in reverse |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6834 | for (size_t k = iftable_count; k != 0; ) { |
| 6835 | --k; |
| 6836 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6837 | DCHECK_LT(k, iftable->Count()); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6838 | |
| 6839 | iface.Assign(iftable->GetInterface(k)); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6840 | // Iterate through every declared method on this interface. The order does not matter. |
| 6841 | for (auto& method_iter : iface->GetDeclaredVirtualMethods(image_pointer_size_)) { |
| 6842 | ArtMethod* current_method = &method_iter; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6843 | // Skip abstract methods and methods with different names. |
| 6844 | if (current_method->IsAbstract() || |
| 6845 | !target_name_comparator.HasSameNameAndSignature( |
| 6846 | current_method->GetInterfaceMethodIfProxy(image_pointer_size_))) { |
| 6847 | continue; |
Alex Light | d7c10c2 | 2016-03-31 10:03:07 -0700 | [diff] [blame] | 6848 | } else if (!current_method->IsPublic()) { |
| 6849 | // The verifier should have caught the non-public method for dex version 37. Just warn and |
| 6850 | // skip it since this is from before default-methods so we don't really need to care that it |
| 6851 | // has code. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6852 | LOG(WARNING) << "Interface method " << current_method->PrettyMethod() |
| 6853 | << " is not public! " |
Alex Light | d7c10c2 | 2016-03-31 10:03:07 -0700 | [diff] [blame] | 6854 | << "This will be a fatal error in subsequent versions of android. " |
| 6855 | << "Continuing anyway."; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6856 | } |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 6857 | if (UNLIKELY(chosen_iface != nullptr)) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6858 | // We have multiple default impls of the same method. This is a potential default conflict. |
| 6859 | // We need to check if this possibly conflicting method is either a superclass of the chosen |
| 6860 | // default implementation or is overridden by a non-default interface method. In either case |
| 6861 | // there is no conflict. |
| 6862 | if (!iface->IsAssignableFrom(chosen_iface.Get()) && |
| 6863 | !ContainsOverridingMethodOf(self, |
| 6864 | target_name_comparator, |
| 6865 | iftable, |
| 6866 | k, |
| 6867 | iface, |
| 6868 | image_pointer_size_)) { |
Nicolas Geoffray | 7f3e0db | 2016-01-28 09:29:31 +0000 | [diff] [blame] | 6869 | VLOG(class_linker) << "Conflicting default method implementations found: " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6870 | << current_method->PrettyMethod() << " and " |
| 6871 | << ArtMethod::PrettyMethod(*out_default_method) << " in class " |
| 6872 | << klass->PrettyClass() << " conflict."; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6873 | *out_default_method = nullptr; |
| 6874 | return DefaultMethodSearchResult::kDefaultConflict; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6875 | } else { |
| 6876 | break; // Continue checking at the next interface. |
| 6877 | } |
| 6878 | } else { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6879 | // chosen_iface == null |
| 6880 | if (!ContainsOverridingMethodOf(self, |
| 6881 | target_name_comparator, |
| 6882 | iftable, |
| 6883 | k, |
| 6884 | iface, |
| 6885 | image_pointer_size_)) { |
| 6886 | // Don't set this as the chosen interface if something else is overriding it (because that |
| 6887 | // other interface would be potentially chosen instead if it was default). If the other |
| 6888 | // interface was abstract then we wouldn't select this interface as chosen anyway since |
| 6889 | // the abstract method masks it. |
| 6890 | *out_default_method = current_method; |
| 6891 | chosen_iface.Assign(iface.Get()); |
| 6892 | // We should now finish traversing the graph to find if we have default methods that |
| 6893 | // conflict. |
| 6894 | } else { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6895 | VLOG(class_linker) << "A default method '" << current_method->PrettyMethod() |
| 6896 | << "' was " |
| 6897 | << "skipped because it was overridden by an abstract method in a " |
| 6898 | << "subinterface on class '" << klass->PrettyClass() << "'"; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6899 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6900 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6901 | break; |
| 6902 | } |
| 6903 | } |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6904 | if (*out_default_method != nullptr) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6905 | VLOG(class_linker) << "Default method '" << (*out_default_method)->PrettyMethod() |
| 6906 | << "' selected " |
| 6907 | << "as the implementation for '" << target_method->PrettyMethod() |
| 6908 | << "' in '" << klass->PrettyClass() << "'"; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6909 | return DefaultMethodSearchResult::kDefaultFound; |
| 6910 | } else { |
| 6911 | return DefaultMethodSearchResult::kAbstractFound; |
| 6912 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6913 | } |
| 6914 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6915 | ArtMethod* ClassLinker::AddMethodToConflictTable(ObjPtr<mirror::Class> klass, |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6916 | ArtMethod* conflict_method, |
| 6917 | ArtMethod* interface_method, |
| 6918 | ArtMethod* method, |
| 6919 | bool force_new_conflict_method) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 6920 | ImtConflictTable* current_table = conflict_method->GetImtConflictTable(kRuntimePointerSize); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6921 | Runtime* const runtime = Runtime::Current(); |
| 6922 | LinearAlloc* linear_alloc = GetAllocatorForClassLoader(klass->GetClassLoader()); |
| 6923 | bool new_entry = conflict_method == runtime->GetImtConflictMethod() || force_new_conflict_method; |
| 6924 | |
| 6925 | // Create a new entry if the existing one is the shared conflict method. |
| 6926 | ArtMethod* new_conflict_method = new_entry |
| 6927 | ? runtime->CreateImtConflictMethod(linear_alloc) |
| 6928 | : conflict_method; |
| 6929 | |
| 6930 | // Allocate a new table. Note that we will leak this table at the next conflict, |
| 6931 | // but that's a tradeoff compared to making the table fixed size. |
| 6932 | void* data = linear_alloc->Alloc( |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6933 | Thread::Current(), ImtConflictTable::ComputeSizeWithOneMoreEntry(current_table, |
| 6934 | image_pointer_size_)); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6935 | if (data == nullptr) { |
| 6936 | LOG(ERROR) << "Failed to allocate conflict table"; |
| 6937 | return conflict_method; |
| 6938 | } |
| 6939 | ImtConflictTable* new_table = new (data) ImtConflictTable(current_table, |
| 6940 | interface_method, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6941 | method, |
| 6942 | image_pointer_size_); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6943 | |
| 6944 | // Do a fence to ensure threads see the data in the table before it is assigned |
| 6945 | // to the conflict method. |
| 6946 | // Note that there is a race in the presence of multiple threads and we may leak |
| 6947 | // memory from the LinearAlloc, but that's a tradeoff compared to using |
| 6948 | // atomic operations. |
Orion Hodson | 27b9676 | 2018-03-13 16:06:57 +0000 | [diff] [blame] | 6949 | std::atomic_thread_fence(std::memory_order_release); |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6950 | new_conflict_method->SetImtConflictTable(new_table, image_pointer_size_); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6951 | return new_conflict_method; |
| 6952 | } |
| 6953 | |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 6954 | bool ClassLinker::AllocateIfTableMethodArrays(Thread* self, |
| 6955 | Handle<mirror::Class> klass, |
| 6956 | Handle<mirror::IfTable> iftable) { |
| 6957 | DCHECK(!klass->IsInterface()); |
| 6958 | const bool has_superclass = klass->HasSuperClass(); |
| 6959 | const bool extend_super_iftable = has_superclass; |
| 6960 | const size_t ifcount = klass->GetIfTableCount(); |
| 6961 | const size_t super_ifcount = has_superclass ? klass->GetSuperClass()->GetIfTableCount() : 0U; |
| 6962 | for (size_t i = 0; i < ifcount; ++i) { |
| 6963 | size_t num_methods = iftable->GetInterface(i)->NumDeclaredVirtualMethods(); |
| 6964 | if (num_methods > 0) { |
| 6965 | const bool is_super = i < super_ifcount; |
| 6966 | // This is an interface implemented by a super-class. Therefore we can just copy the method |
| 6967 | // array from the superclass. |
| 6968 | const bool super_interface = is_super && extend_super_iftable; |
| 6969 | ObjPtr<mirror::PointerArray> method_array; |
| 6970 | if (super_interface) { |
| 6971 | ObjPtr<mirror::IfTable> if_table = klass->GetSuperClass()->GetIfTable(); |
| 6972 | DCHECK(if_table != nullptr); |
| 6973 | DCHECK(if_table->GetMethodArray(i) != nullptr); |
| 6974 | // If we are working on a super interface, try extending the existing method array. |
Vladimir Marko | 3068d58 | 2019-05-28 16:39:29 +0100 | [diff] [blame] | 6975 | StackHandleScope<1u> hs(self); |
| 6976 | Handle<mirror::PointerArray> old_array = hs.NewHandle(if_table->GetMethodArray(i)); |
| 6977 | method_array = |
| 6978 | ObjPtr<mirror::PointerArray>::DownCast(mirror::Object::Clone(old_array, self)); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 6979 | } else { |
| 6980 | method_array = AllocPointerArray(self, num_methods); |
| 6981 | } |
| 6982 | if (UNLIKELY(method_array == nullptr)) { |
| 6983 | self->AssertPendingOOMException(); |
| 6984 | return false; |
| 6985 | } |
| 6986 | iftable->SetMethodArray(i, method_array); |
| 6987 | } |
| 6988 | } |
| 6989 | return true; |
| 6990 | } |
| 6991 | |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6992 | void ClassLinker::SetIMTRef(ArtMethod* unimplemented_method, |
| 6993 | ArtMethod* imt_conflict_method, |
| 6994 | ArtMethod* current_method, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6995 | /*out*/bool* new_conflict, |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6996 | /*out*/ArtMethod** imt_ref) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6997 | // Place method in imt if entry is empty, place conflict otherwise. |
| 6998 | if (*imt_ref == unimplemented_method) { |
| 6999 | *imt_ref = current_method; |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 7000 | } else if (!(*imt_ref)->IsRuntimeMethod()) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7001 | // If we are not a conflict and we have the same signature and name as the imt |
| 7002 | // entry, it must be that we overwrote a superclass vtable entry. |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 7003 | // Note that we have checked IsRuntimeMethod, as there may be multiple different |
| 7004 | // conflict methods. |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7005 | MethodNameAndSignatureComparator imt_comparator( |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 7006 | (*imt_ref)->GetInterfaceMethodIfProxy(image_pointer_size_)); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7007 | if (imt_comparator.HasSameNameAndSignature( |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 7008 | current_method->GetInterfaceMethodIfProxy(image_pointer_size_))) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7009 | *imt_ref = current_method; |
| 7010 | } else { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7011 | *imt_ref = imt_conflict_method; |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 7012 | *new_conflict = true; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7013 | } |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 7014 | } else { |
| 7015 | // Place the default conflict method. Note that there may be an existing conflict |
| 7016 | // method in the IMT, but it could be one tailored to the super class, with a |
| 7017 | // specific ImtConflictTable. |
| 7018 | *imt_ref = imt_conflict_method; |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 7019 | *new_conflict = true; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7020 | } |
| 7021 | } |
| 7022 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7023 | void ClassLinker::FillIMTAndConflictTables(ObjPtr<mirror::Class> klass) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 7024 | DCHECK(klass->ShouldHaveImt()) << klass->PrettyClass(); |
| 7025 | DCHECK(!klass->IsTemp()) << klass->PrettyClass(); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 7026 | ArtMethod* imt_data[ImTable::kSize]; |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 7027 | Runtime* const runtime = Runtime::Current(); |
| 7028 | ArtMethod* const unimplemented_method = runtime->GetImtUnimplementedMethod(); |
| 7029 | ArtMethod* const conflict_method = runtime->GetImtConflictMethod(); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 7030 | std::fill_n(imt_data, arraysize(imt_data), unimplemented_method); |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 7031 | if (klass->GetIfTable() != nullptr) { |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 7032 | bool new_conflict = false; |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 7033 | FillIMTFromIfTable(klass->GetIfTable(), |
| 7034 | unimplemented_method, |
| 7035 | conflict_method, |
| 7036 | klass, |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 7037 | /*create_conflict_tables=*/true, |
| 7038 | /*ignore_copied_methods=*/false, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 7039 | &new_conflict, |
| 7040 | &imt_data[0]); |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 7041 | } |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 7042 | if (!klass->ShouldHaveImt()) { |
| 7043 | return; |
| 7044 | } |
| 7045 | // Compare the IMT with the super class including the conflict methods. If they are equivalent, |
| 7046 | // we can just use the same pointer. |
| 7047 | ImTable* imt = nullptr; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7048 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 7049 | if (super_class != nullptr && super_class->ShouldHaveImt()) { |
| 7050 | ImTable* super_imt = super_class->GetImt(image_pointer_size_); |
| 7051 | bool same = true; |
| 7052 | for (size_t i = 0; same && i < ImTable::kSize; ++i) { |
| 7053 | ArtMethod* method = imt_data[i]; |
| 7054 | ArtMethod* super_method = super_imt->Get(i, image_pointer_size_); |
| 7055 | if (method != super_method) { |
| 7056 | bool is_conflict_table = method->IsRuntimeMethod() && |
| 7057 | method != unimplemented_method && |
| 7058 | method != conflict_method; |
| 7059 | // Verify conflict contents. |
| 7060 | bool super_conflict_table = super_method->IsRuntimeMethod() && |
| 7061 | super_method != unimplemented_method && |
| 7062 | super_method != conflict_method; |
| 7063 | if (!is_conflict_table || !super_conflict_table) { |
| 7064 | same = false; |
| 7065 | } else { |
| 7066 | ImtConflictTable* table1 = method->GetImtConflictTable(image_pointer_size_); |
| 7067 | ImtConflictTable* table2 = super_method->GetImtConflictTable(image_pointer_size_); |
| 7068 | same = same && table1->Equals(table2, image_pointer_size_); |
| 7069 | } |
| 7070 | } |
| 7071 | } |
| 7072 | if (same) { |
| 7073 | imt = super_imt; |
| 7074 | } |
| 7075 | } |
| 7076 | if (imt == nullptr) { |
| 7077 | imt = klass->GetImt(image_pointer_size_); |
| 7078 | DCHECK(imt != nullptr); |
| 7079 | imt->Populate(imt_data, image_pointer_size_); |
| 7080 | } else { |
| 7081 | klass->SetImt(imt, image_pointer_size_); |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 7082 | } |
| 7083 | } |
| 7084 | |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 7085 | ImtConflictTable* ClassLinker::CreateImtConflictTable(size_t count, |
| 7086 | LinearAlloc* linear_alloc, |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 7087 | PointerSize image_pointer_size) { |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 7088 | void* data = linear_alloc->Alloc(Thread::Current(), |
| 7089 | ImtConflictTable::ComputeSize(count, |
| 7090 | image_pointer_size)); |
| 7091 | return (data != nullptr) ? new (data) ImtConflictTable(count, image_pointer_size) : nullptr; |
| 7092 | } |
| 7093 | |
| 7094 | ImtConflictTable* ClassLinker::CreateImtConflictTable(size_t count, LinearAlloc* linear_alloc) { |
| 7095 | return CreateImtConflictTable(count, linear_alloc, image_pointer_size_); |
| 7096 | } |
| 7097 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7098 | void ClassLinker::FillIMTFromIfTable(ObjPtr<mirror::IfTable> if_table, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 7099 | ArtMethod* unimplemented_method, |
| 7100 | ArtMethod* imt_conflict_method, |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7101 | ObjPtr<mirror::Class> klass, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 7102 | bool create_conflict_tables, |
| 7103 | bool ignore_copied_methods, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 7104 | /*out*/bool* new_conflict, |
| 7105 | /*out*/ArtMethod** imt) { |
| 7106 | uint32_t conflict_counts[ImTable::kSize] = {}; |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 7107 | for (size_t i = 0, length = if_table->Count(); i < length; ++i) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7108 | ObjPtr<mirror::Class> interface = if_table->GetInterface(i); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 7109 | const size_t num_virtuals = interface->NumVirtualMethods(); |
| 7110 | const size_t method_array_count = if_table->GetMethodArrayCount(i); |
| 7111 | // Virtual methods can be larger than the if table methods if there are default methods. |
| 7112 | DCHECK_GE(num_virtuals, method_array_count); |
| 7113 | if (kIsDebugBuild) { |
| 7114 | if (klass->IsInterface()) { |
| 7115 | DCHECK_EQ(method_array_count, 0u); |
| 7116 | } else { |
| 7117 | DCHECK_EQ(interface->NumDeclaredVirtualMethods(), method_array_count); |
| 7118 | } |
| 7119 | } |
| 7120 | if (method_array_count == 0) { |
| 7121 | continue; |
| 7122 | } |
Vladimir Marko | 557fece | 2019-03-26 14:29:41 +0000 | [diff] [blame] | 7123 | ObjPtr<mirror::PointerArray> method_array = if_table->GetMethodArray(i); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 7124 | for (size_t j = 0; j < method_array_count; ++j) { |
| 7125 | ArtMethod* implementation_method = |
| 7126 | method_array->GetElementPtrSize<ArtMethod*>(j, image_pointer_size_); |
| 7127 | if (ignore_copied_methods && implementation_method->IsCopied()) { |
| 7128 | continue; |
| 7129 | } |
| 7130 | DCHECK(implementation_method != nullptr); |
| 7131 | // Miranda methods cannot be used to implement an interface method, but they are safe to put |
| 7132 | // in the IMT since their entrypoint is the interface trampoline. If we put any copied methods |
| 7133 | // or interface methods in the IMT here they will not create extra conflicts since we compare |
| 7134 | // names and signatures in SetIMTRef. |
| 7135 | ArtMethod* interface_method = interface->GetVirtualMethod(j, image_pointer_size_); |
David Srbecky | e36e7f2 | 2018-11-14 14:21:23 +0000 | [diff] [blame] | 7136 | const uint32_t imt_index = interface_method->GetImtIndex(); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 7137 | |
| 7138 | // There is only any conflicts if all of the interface methods for an IMT slot don't have |
| 7139 | // the same implementation method, keep track of this to avoid creating a conflict table in |
| 7140 | // this case. |
| 7141 | |
| 7142 | // Conflict table size for each IMT slot. |
| 7143 | ++conflict_counts[imt_index]; |
| 7144 | |
| 7145 | SetIMTRef(unimplemented_method, |
| 7146 | imt_conflict_method, |
| 7147 | implementation_method, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 7148 | /*out*/new_conflict, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 7149 | /*out*/&imt[imt_index]); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 7150 | } |
| 7151 | } |
| 7152 | |
| 7153 | if (create_conflict_tables) { |
| 7154 | // Create the conflict tables. |
| 7155 | LinearAlloc* linear_alloc = GetAllocatorForClassLoader(klass->GetClassLoader()); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 7156 | for (size_t i = 0; i < ImTable::kSize; ++i) { |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 7157 | size_t conflicts = conflict_counts[i]; |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 7158 | if (imt[i] == imt_conflict_method) { |
| 7159 | ImtConflictTable* new_table = CreateImtConflictTable(conflicts, linear_alloc); |
| 7160 | if (new_table != nullptr) { |
| 7161 | ArtMethod* new_conflict_method = |
| 7162 | Runtime::Current()->CreateImtConflictMethod(linear_alloc); |
| 7163 | new_conflict_method->SetImtConflictTable(new_table, image_pointer_size_); |
| 7164 | imt[i] = new_conflict_method; |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 7165 | } else { |
| 7166 | LOG(ERROR) << "Failed to allocate conflict table"; |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 7167 | imt[i] = imt_conflict_method; |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 7168 | } |
| 7169 | } else { |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 7170 | DCHECK_NE(imt[i], imt_conflict_method); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 7171 | } |
| 7172 | } |
| 7173 | |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 7174 | for (size_t i = 0, length = if_table->Count(); i < length; ++i) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7175 | ObjPtr<mirror::Class> interface = if_table->GetInterface(i); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 7176 | const size_t method_array_count = if_table->GetMethodArrayCount(i); |
| 7177 | // Virtual methods can be larger than the if table methods if there are default methods. |
| 7178 | if (method_array_count == 0) { |
| 7179 | continue; |
| 7180 | } |
Vladimir Marko | 557fece | 2019-03-26 14:29:41 +0000 | [diff] [blame] | 7181 | ObjPtr<mirror::PointerArray> method_array = if_table->GetMethodArray(i); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 7182 | for (size_t j = 0; j < method_array_count; ++j) { |
| 7183 | ArtMethod* implementation_method = |
| 7184 | method_array->GetElementPtrSize<ArtMethod*>(j, image_pointer_size_); |
| 7185 | if (ignore_copied_methods && implementation_method->IsCopied()) { |
| 7186 | continue; |
| 7187 | } |
| 7188 | DCHECK(implementation_method != nullptr); |
| 7189 | ArtMethod* interface_method = interface->GetVirtualMethod(j, image_pointer_size_); |
David Srbecky | e36e7f2 | 2018-11-14 14:21:23 +0000 | [diff] [blame] | 7190 | const uint32_t imt_index = interface_method->GetImtIndex(); |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 7191 | if (!imt[imt_index]->IsRuntimeMethod() || |
| 7192 | imt[imt_index] == unimplemented_method || |
| 7193 | imt[imt_index] == imt_conflict_method) { |
| 7194 | continue; |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 7195 | } |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 7196 | ImtConflictTable* table = imt[imt_index]->GetImtConflictTable(image_pointer_size_); |
| 7197 | const size_t num_entries = table->NumEntries(image_pointer_size_); |
| 7198 | table->SetInterfaceMethod(num_entries, image_pointer_size_, interface_method); |
| 7199 | table->SetImplementationMethod(num_entries, image_pointer_size_, implementation_method); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 7200 | } |
| 7201 | } |
| 7202 | } |
| 7203 | } |
| 7204 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7205 | // Simple helper function that checks that no subtypes of 'val' are contained within the 'classes' |
| 7206 | // set. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7207 | static bool NotSubinterfaceOfAny( |
| 7208 | const std::unordered_set<ObjPtr<mirror::Class>, HashObjPtr>& classes, |
| 7209 | ObjPtr<mirror::Class> val) |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7210 | REQUIRES(Roles::uninterruptible_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 7211 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7212 | DCHECK(val != nullptr); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7213 | for (ObjPtr<mirror::Class> c : classes) { |
| 7214 | if (val->IsAssignableFrom(c)) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7215 | return false; |
| 7216 | } |
| 7217 | } |
| 7218 | return true; |
| 7219 | } |
| 7220 | |
| 7221 | // Fills in and flattens the interface inheritance hierarchy. |
| 7222 | // |
| 7223 | // By the end of this function all interfaces in the transitive closure of to_process are added to |
| 7224 | // the iftable and every interface precedes all of its sub-interfaces in this list. |
| 7225 | // |
| 7226 | // all I, J: Interface | I <: J implies J precedes I |
| 7227 | // |
| 7228 | // (note A <: B means that A is a subtype of B) |
| 7229 | // |
| 7230 | // This returns the total number of items in the iftable. The iftable might be resized down after |
| 7231 | // this call. |
| 7232 | // |
| 7233 | // We order this backwards so that we do not need to reorder superclass interfaces when new |
| 7234 | // interfaces are added in subclass's interface tables. |
| 7235 | // |
| 7236 | // Upon entry into this function iftable is a copy of the superclass's iftable with the first |
| 7237 | // super_ifcount entries filled in with the transitive closure of the interfaces of the superclass. |
| 7238 | // The other entries are uninitialized. We will fill in the remaining entries in this function. The |
| 7239 | // 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] | 7240 | static size_t FillIfTable(ObjPtr<mirror::IfTable> iftable, |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7241 | size_t super_ifcount, |
Stephen Hines | 48ba197 | 2018-09-24 13:35:54 -0700 | [diff] [blame] | 7242 | const std::vector<ObjPtr<mirror::Class>>& to_process) |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7243 | REQUIRES(Roles::uninterruptible_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 7244 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7245 | // This is the set of all class's already in the iftable. Used to make checking if a class has |
| 7246 | // already been added quicker. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7247 | std::unordered_set<ObjPtr<mirror::Class>, HashObjPtr> classes_in_iftable; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7248 | // The first super_ifcount elements are from the superclass. We note that they are already added. |
| 7249 | for (size_t i = 0; i < super_ifcount; i++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7250 | ObjPtr<mirror::Class> iface = iftable->GetInterface(i); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7251 | DCHECK(NotSubinterfaceOfAny(classes_in_iftable, iface)) << "Bad ordering."; |
| 7252 | classes_in_iftable.insert(iface); |
| 7253 | } |
| 7254 | size_t filled_ifcount = super_ifcount; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7255 | for (ObjPtr<mirror::Class> interface : to_process) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7256 | // Let us call the first filled_ifcount elements of iftable the current-iface-list. |
| 7257 | // At this point in the loop current-iface-list has the invariant that: |
| 7258 | // for every pair of interfaces I,J within it: |
| 7259 | // if index_of(I) < index_of(J) then I is not a subtype of J |
| 7260 | |
| 7261 | // If we have already seen this element then all of its super-interfaces must already be in the |
| 7262 | // current-iface-list so we can skip adding it. |
| 7263 | if (!ContainsElement(classes_in_iftable, interface)) { |
| 7264 | // We haven't seen this interface so add all of its super-interfaces onto the |
| 7265 | // current-iface-list, skipping those already on it. |
| 7266 | int32_t ifcount = interface->GetIfTableCount(); |
| 7267 | for (int32_t j = 0; j < ifcount; j++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7268 | ObjPtr<mirror::Class> super_interface = interface->GetIfTable()->GetInterface(j); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7269 | if (!ContainsElement(classes_in_iftable, super_interface)) { |
| 7270 | DCHECK(NotSubinterfaceOfAny(classes_in_iftable, super_interface)) << "Bad ordering."; |
| 7271 | classes_in_iftable.insert(super_interface); |
| 7272 | iftable->SetInterface(filled_ifcount, super_interface); |
| 7273 | filled_ifcount++; |
| 7274 | } |
| 7275 | } |
| 7276 | DCHECK(NotSubinterfaceOfAny(classes_in_iftable, interface)) << "Bad ordering"; |
| 7277 | // Place this interface onto the current-iface-list after all of its super-interfaces. |
| 7278 | classes_in_iftable.insert(interface); |
| 7279 | iftable->SetInterface(filled_ifcount, interface); |
| 7280 | filled_ifcount++; |
| 7281 | } else if (kIsDebugBuild) { |
| 7282 | // Check all super-interfaces are already in the list. |
| 7283 | int32_t ifcount = interface->GetIfTableCount(); |
| 7284 | for (int32_t j = 0; j < ifcount; j++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7285 | ObjPtr<mirror::Class> super_interface = interface->GetIfTable()->GetInterface(j); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7286 | DCHECK(ContainsElement(classes_in_iftable, super_interface)) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 7287 | << "Iftable does not contain " << mirror::Class::PrettyClass(super_interface) |
| 7288 | << ", a superinterface of " << interface->PrettyClass(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7289 | } |
| 7290 | } |
| 7291 | } |
| 7292 | if (kIsDebugBuild) { |
| 7293 | // Check that the iftable is ordered correctly. |
| 7294 | for (size_t i = 0; i < filled_ifcount; i++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7295 | ObjPtr<mirror::Class> if_a = iftable->GetInterface(i); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7296 | for (size_t j = i + 1; j < filled_ifcount; j++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7297 | ObjPtr<mirror::Class> if_b = iftable->GetInterface(j); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7298 | // !(if_a <: if_b) |
| 7299 | CHECK(!if_b->IsAssignableFrom(if_a)) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 7300 | << "Bad interface order: " << mirror::Class::PrettyClass(if_a) << " (index " << i |
| 7301 | << ") extends " |
| 7302 | << 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] | 7303 | << "interface list."; |
| 7304 | } |
| 7305 | } |
| 7306 | } |
| 7307 | return filled_ifcount; |
| 7308 | } |
| 7309 | |
| 7310 | bool ClassLinker::SetupInterfaceLookupTable(Thread* self, Handle<mirror::Class> klass, |
| 7311 | Handle<mirror::ObjectArray<mirror::Class>> interfaces) { |
| 7312 | StackHandleScope<1> hs(self); |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 7313 | const bool has_superclass = klass->HasSuperClass(); |
| 7314 | const size_t super_ifcount = has_superclass ? klass->GetSuperClass()->GetIfTableCount() : 0U; |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 7315 | const bool have_interfaces = interfaces != nullptr; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7316 | const size_t num_interfaces = |
| 7317 | have_interfaces ? interfaces->GetLength() : klass->NumDirectInterfaces(); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 7318 | if (num_interfaces == 0) { |
| 7319 | if (super_ifcount == 0) { |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 7320 | if (LIKELY(has_superclass)) { |
| 7321 | klass->SetIfTable(klass->GetSuperClass()->GetIfTable()); |
| 7322 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 7323 | // Class implements no interfaces. |
| 7324 | DCHECK_EQ(klass->GetIfTableCount(), 0); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 7325 | return true; |
| 7326 | } |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 7327 | // Class implements same interfaces as parent, are any of these not marker interfaces? |
| 7328 | bool has_non_marker_interface = false; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7329 | ObjPtr<mirror::IfTable> super_iftable = klass->GetSuperClass()->GetIfTable(); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 7330 | for (size_t i = 0; i < super_ifcount; ++i) { |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 7331 | if (super_iftable->GetMethodArrayCount(i) > 0) { |
| 7332 | has_non_marker_interface = true; |
| 7333 | break; |
| 7334 | } |
| 7335 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 7336 | // Class just inherits marker interfaces from parent so recycle parent's iftable. |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 7337 | if (!has_non_marker_interface) { |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 7338 | klass->SetIfTable(super_iftable); |
| 7339 | return true; |
| 7340 | } |
| 7341 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 7342 | size_t ifcount = super_ifcount + num_interfaces; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7343 | // Check that every class being implemented is an interface. |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 7344 | for (size_t i = 0; i < num_interfaces; i++) { |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 7345 | ObjPtr<mirror::Class> interface = have_interfaces |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 7346 | ? interfaces->GetWithoutChecks(i) |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 7347 | : mirror::Class::GetDirectInterface(self, klass.Get(), i); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 7348 | DCHECK(interface != nullptr); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 7349 | if (UNLIKELY(!interface->IsInterface())) { |
| 7350 | std::string temp; |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 7351 | ThrowIncompatibleClassChangeError(klass.Get(), |
| 7352 | "Class %s implements non-interface class %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 7353 | klass->PrettyDescriptor().c_str(), |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 7354 | PrettyDescriptor(interface->GetDescriptor(&temp)).c_str()); |
| 7355 | return false; |
| 7356 | } |
| 7357 | ifcount += interface->GetIfTableCount(); |
| 7358 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7359 | // Create the interface function table. |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 7360 | MutableHandle<mirror::IfTable> iftable(hs.NewHandle(AllocIfTable(self, ifcount))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 7361 | if (UNLIKELY(iftable == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7362 | self->AssertPendingOOMException(); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 7363 | return false; |
| 7364 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7365 | // Fill in table with superclass's iftable. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7366 | if (super_ifcount != 0) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7367 | ObjPtr<mirror::IfTable> super_iftable = klass->GetSuperClass()->GetIfTable(); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 7368 | for (size_t i = 0; i < super_ifcount; i++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7369 | ObjPtr<mirror::Class> super_interface = super_iftable->GetInterface(i); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 7370 | iftable->SetInterface(i, super_interface); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 7371 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7372 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7373 | |
| 7374 | // Note that AllowThreadSuspension is to thread suspension as pthread_testcancel is to pthread |
| 7375 | // cancellation. That is it will suspend if one has a pending suspend request but otherwise |
| 7376 | // doesn't really do anything. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 7377 | self->AllowThreadSuspension(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7378 | |
| 7379 | size_t new_ifcount; |
| 7380 | { |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 7381 | ScopedAssertNoThreadSuspension nts("Copying mirror::Class*'s for FillIfTable"); |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 7382 | std::vector<ObjPtr<mirror::Class>> to_add; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7383 | for (size_t i = 0; i < num_interfaces; i++) { |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 7384 | ObjPtr<mirror::Class> interface = have_interfaces ? interfaces->Get(i) : |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 7385 | mirror::Class::GetDirectInterface(self, klass.Get(), i); |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 7386 | to_add.push_back(interface); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 7387 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7388 | |
| 7389 | new_ifcount = FillIfTable(iftable.Get(), super_ifcount, std::move(to_add)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7390 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7391 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 7392 | self->AllowThreadSuspension(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7393 | |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 7394 | // Shrink iftable in case duplicates were found |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7395 | if (new_ifcount < ifcount) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 7396 | DCHECK_NE(num_interfaces, 0U); |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 7397 | iftable.Assign(ObjPtr<mirror::IfTable>::DownCast( |
Vladimir Marko | 3068d58 | 2019-05-28 16:39:29 +0100 | [diff] [blame] | 7398 | mirror::IfTable::CopyOf(iftable, self, new_ifcount * mirror::IfTable::kMax))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 7399 | if (UNLIKELY(iftable == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7400 | self->AssertPendingOOMException(); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 7401 | return false; |
| 7402 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7403 | ifcount = new_ifcount; |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 7404 | } else { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7405 | DCHECK_EQ(new_ifcount, ifcount); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 7406 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 7407 | klass->SetIfTable(iftable.Get()); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7408 | return true; |
| 7409 | } |
| 7410 | |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 7411 | // Finds the method with a name/signature that matches cmp in the given lists of methods. The list |
| 7412 | // of methods must be unique. |
| 7413 | static ArtMethod* FindSameNameAndSignature(MethodNameAndSignatureComparator& cmp ATTRIBUTE_UNUSED) { |
| 7414 | return nullptr; |
| 7415 | } |
| 7416 | |
| 7417 | template <typename ... Types> |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7418 | static ArtMethod* FindSameNameAndSignature(MethodNameAndSignatureComparator& cmp, |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 7419 | const ScopedArenaVector<ArtMethod*>& list, |
| 7420 | const Types& ... rest) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 7421 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7422 | for (ArtMethod* method : list) { |
| 7423 | if (cmp.HasSameNameAndSignature(method)) { |
| 7424 | return method; |
| 7425 | } |
| 7426 | } |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 7427 | return FindSameNameAndSignature(cmp, rest...); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7428 | } |
| 7429 | |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 7430 | namespace { |
| 7431 | |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 7432 | // Check that all vtable entries are present in this class's virtuals or are the same as a |
| 7433 | // superclasses vtable entry. |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 7434 | void CheckClassOwnsVTableEntries(Thread* self, |
| 7435 | Handle<mirror::Class> klass, |
| 7436 | PointerSize pointer_size) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 7437 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 7438 | StackHandleScope<2> hs(self); |
| 7439 | Handle<mirror::PointerArray> check_vtable(hs.NewHandle(klass->GetVTableDuringLinking())); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7440 | ObjPtr<mirror::Class> super_temp = (klass->HasSuperClass()) ? klass->GetSuperClass() : nullptr; |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 7441 | Handle<mirror::Class> superclass(hs.NewHandle(super_temp)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 7442 | int32_t super_vtable_length = (superclass != nullptr) ? superclass->GetVTableLength() : 0; |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 7443 | for (int32_t i = 0; i < check_vtable->GetLength(); ++i) { |
| 7444 | ArtMethod* m = check_vtable->GetElementPtrSize<ArtMethod*>(i, pointer_size); |
| 7445 | CHECK(m != nullptr); |
| 7446 | |
Alex Light | a41a3078 | 2017-03-29 11:33:19 -0700 | [diff] [blame] | 7447 | if (m->GetMethodIndexDuringLinking() != i) { |
| 7448 | LOG(WARNING) << m->PrettyMethod() |
| 7449 | << " has an unexpected method index for its spot in the vtable for class" |
| 7450 | << klass->PrettyClass(); |
| 7451 | } |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 7452 | ArraySlice<ArtMethod> virtuals = klass->GetVirtualMethodsSliceUnchecked(pointer_size); |
| 7453 | auto is_same_method = [m] (const ArtMethod& meth) { |
| 7454 | return &meth == m; |
| 7455 | }; |
Alex Light | 3f98053 | 2017-03-17 15:10:32 -0700 | [diff] [blame] | 7456 | if (!((super_vtable_length > i && superclass->GetVTableEntry(i, pointer_size) == m) || |
| 7457 | std::find_if(virtuals.begin(), virtuals.end(), is_same_method) != virtuals.end())) { |
| 7458 | LOG(WARNING) << m->PrettyMethod() << " does not seem to be owned by current class " |
| 7459 | << klass->PrettyClass() << " or any of its superclasses!"; |
| 7460 | } |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 7461 | } |
| 7462 | } |
| 7463 | |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 7464 | // Check to make sure the vtable does not have duplicates. Duplicates could cause problems when a |
| 7465 | // method is overridden in a subclass. |
Andreas Gampe | a2fed08 | 2019-02-01 09:34:43 -0800 | [diff] [blame] | 7466 | template <PointerSize kPointerSize> |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 7467 | void CheckVTableHasNoDuplicates(Thread* self, Handle<mirror::Class> klass) |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 7468 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 7469 | StackHandleScope<1> hs(self); |
| 7470 | Handle<mirror::PointerArray> vtable(hs.NewHandle(klass->GetVTableDuringLinking())); |
| 7471 | int32_t num_entries = vtable->GetLength(); |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 7472 | |
| 7473 | // Observations: |
| 7474 | // * The older implementation was O(n^2) and got too expensive for apps with larger classes. |
| 7475 | // * Many classes do not override Object functions (e.g., equals/hashCode/toString). Thus, |
| 7476 | // for many classes outside of libcore a cross-dexfile check has to be run anyways. |
| 7477 | // * In the cross-dexfile case, with the O(n^2), in the best case O(n) cross checks would have |
| 7478 | // to be done. It is thus OK in a single-pass algorithm to read all data, anyways. |
| 7479 | // * The single-pass algorithm will trade memory for speed, but that is OK. |
| 7480 | |
| 7481 | CHECK_GT(num_entries, 0); |
| 7482 | |
| 7483 | auto log_fn = [&vtable, &klass](int32_t i, int32_t j) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 7484 | ArtMethod* m1 = vtable->GetElementPtrSize<ArtMethod*, kPointerSize>(i); |
| 7485 | ArtMethod* m2 = vtable->GetElementPtrSize<ArtMethod*, kPointerSize>(j); |
| 7486 | LOG(WARNING) << "vtable entries " << i << " and " << j << " are identical for " |
| 7487 | << klass->PrettyClass() << " in method " << m1->PrettyMethod() |
| 7488 | << " (0x" << std::hex << reinterpret_cast<uintptr_t>(m2) << ") and " |
| 7489 | << m2->PrettyMethod() << " (0x" << std::hex |
| 7490 | << reinterpret_cast<uintptr_t>(m2) << ")"; |
| 7491 | }; |
| 7492 | struct BaseHashType { |
| 7493 | static size_t HashCombine(size_t seed, size_t val) { |
| 7494 | return seed ^ (val + 0x9e3779b9 + (seed << 6) + (seed >> 2)); |
| 7495 | } |
| 7496 | }; |
| 7497 | |
| 7498 | // Check assuming all entries come from the same dex file. |
| 7499 | { |
| 7500 | // Find the first interesting method and its dex file. |
| 7501 | int32_t start = 0; |
| 7502 | for (; start < num_entries; ++start) { |
| 7503 | ArtMethod* vtable_entry = vtable->GetElementPtrSize<ArtMethod*, kPointerSize>(start); |
| 7504 | // Don't bother if we cannot 'see' the vtable entry (i.e. it is a package-private member |
| 7505 | // maybe). |
| 7506 | if (!klass->CanAccessMember(vtable_entry->GetDeclaringClass(), |
| 7507 | vtable_entry->GetAccessFlags())) { |
| 7508 | continue; |
| 7509 | } |
| 7510 | break; |
| 7511 | } |
| 7512 | if (start == num_entries) { |
| 7513 | return; |
| 7514 | } |
| 7515 | const DexFile* dex_file = |
| 7516 | vtable->GetElementPtrSize<ArtMethod*, kPointerSize>(start)-> |
| 7517 | GetInterfaceMethodIfProxy(kPointerSize)->GetDexFile(); |
| 7518 | |
| 7519 | // Helper function to avoid logging if we have to run the cross-file checks. |
| 7520 | auto check_fn = [&](bool log_warn) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 7521 | // Use a map to store seen entries, as the storage space is too large for a bitvector. |
| 7522 | using PairType = std::pair<uint32_t, uint16_t>; |
| 7523 | struct PairHash : BaseHashType { |
| 7524 | size_t operator()(const PairType& key) const { |
| 7525 | return BaseHashType::HashCombine(BaseHashType::HashCombine(0, key.first), key.second); |
| 7526 | } |
| 7527 | }; |
| 7528 | std::unordered_map<PairType, int32_t, PairHash> seen; |
| 7529 | seen.reserve(2 * num_entries); |
| 7530 | bool need_slow_path = false; |
| 7531 | bool found_dup = false; |
| 7532 | for (int i = start; i < num_entries; ++i) { |
| 7533 | // Can use Unchecked here as the start loop already ensured that the arrays are correct |
| 7534 | // wrt/ kPointerSize. |
| 7535 | ArtMethod* vtable_entry = vtable->GetElementPtrSizeUnchecked<ArtMethod*, kPointerSize>(i); |
| 7536 | if (!klass->CanAccessMember(vtable_entry->GetDeclaringClass(), |
| 7537 | vtable_entry->GetAccessFlags())) { |
| 7538 | continue; |
| 7539 | } |
| 7540 | ArtMethod* m = vtable_entry->GetInterfaceMethodIfProxy(kPointerSize); |
| 7541 | if (dex_file != m->GetDexFile()) { |
| 7542 | need_slow_path = true; |
| 7543 | break; |
| 7544 | } |
| 7545 | const dex::MethodId* m_mid = &dex_file->GetMethodId(m->GetDexMethodIndex()); |
| 7546 | PairType pair = std::make_pair(m_mid->name_idx_.index_, m_mid->proto_idx_.index_); |
| 7547 | auto it = seen.find(pair); |
| 7548 | if (it != seen.end()) { |
| 7549 | found_dup = true; |
| 7550 | if (log_warn) { |
| 7551 | log_fn(it->second, i); |
| 7552 | } |
| 7553 | } else { |
| 7554 | seen.emplace(pair, i); |
| 7555 | } |
| 7556 | } |
| 7557 | return std::make_pair(need_slow_path, found_dup); |
| 7558 | }; |
| 7559 | std::pair<bool, bool> result = check_fn(/* log_warn= */ false); |
| 7560 | if (!result.first) { |
| 7561 | if (result.second) { |
| 7562 | check_fn(/* log_warn= */ true); |
| 7563 | } |
| 7564 | return; |
| 7565 | } |
| 7566 | } |
| 7567 | |
| 7568 | // Need to check across dex files. |
| 7569 | struct Entry { |
| 7570 | size_t cached_hash = 0; |
| 7571 | const char* name = nullptr; |
| 7572 | Signature signature = Signature::NoSignature(); |
| 7573 | uint32_t name_len = 0; |
| 7574 | |
| 7575 | Entry(const DexFile* dex_file, const dex::MethodId& mid) |
| 7576 | : name(dex_file->StringDataAndUtf16LengthByIdx(mid.name_idx_, &name_len)), |
| 7577 | signature(dex_file->GetMethodSignature(mid)) { |
| 7578 | } |
| 7579 | |
| 7580 | bool operator==(const Entry& other) const { |
| 7581 | if (name_len != other.name_len || strcmp(name, other.name) != 0) { |
| 7582 | return false; |
| 7583 | } |
| 7584 | return signature == other.signature; |
| 7585 | } |
| 7586 | }; |
| 7587 | struct EntryHash { |
| 7588 | size_t operator()(const Entry& key) const { |
| 7589 | return key.cached_hash; |
| 7590 | } |
| 7591 | }; |
| 7592 | std::unordered_map<Entry, int32_t, EntryHash> map; |
| 7593 | for (int32_t i = 0; i < num_entries; ++i) { |
| 7594 | // Can use Unchecked here as the first loop already ensured that the arrays are correct |
| 7595 | // wrt/ kPointerSize. |
| 7596 | ArtMethod* vtable_entry = vtable->GetElementPtrSizeUnchecked<ArtMethod*, kPointerSize>(i); |
| 7597 | // Don't bother if we cannot 'see' the vtable entry (i.e. it is a package-private member |
| 7598 | // maybe). |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 7599 | if (!klass->CanAccessMember(vtable_entry->GetDeclaringClass(), |
| 7600 | vtable_entry->GetAccessFlags())) { |
| 7601 | continue; |
| 7602 | } |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 7603 | ArtMethod* m = vtable_entry->GetInterfaceMethodIfProxy(kPointerSize); |
| 7604 | const DexFile* dex_file = m->GetDexFile(); |
| 7605 | const dex::MethodId& mid = dex_file->GetMethodId(m->GetDexMethodIndex()); |
| 7606 | |
| 7607 | Entry e(dex_file, mid); |
| 7608 | |
| 7609 | size_t string_hash = std::hash<std::string_view>()(std::string_view(e.name, e.name_len)); |
| 7610 | size_t sig_hash = std::hash<std::string>()(e.signature.ToString()); |
| 7611 | e.cached_hash = BaseHashType::HashCombine(BaseHashType::HashCombine(0u, string_hash), |
| 7612 | sig_hash); |
| 7613 | |
| 7614 | auto it = map.find(e); |
| 7615 | if (it != map.end()) { |
| 7616 | log_fn(it->second, i); |
| 7617 | } else { |
| 7618 | map.emplace(e, i); |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 7619 | } |
| 7620 | } |
| 7621 | } |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 7622 | |
| 7623 | void CheckVTableHasNoDuplicates(Thread* self, |
| 7624 | Handle<mirror::Class> klass, |
| 7625 | PointerSize pointer_size) |
Andreas Gampe | a2fed08 | 2019-02-01 09:34:43 -0800 | [diff] [blame] | 7626 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 7627 | switch (pointer_size) { |
| 7628 | case PointerSize::k64: |
| 7629 | CheckVTableHasNoDuplicates<PointerSize::k64>(self, klass); |
| 7630 | break; |
| 7631 | case PointerSize::k32: |
| 7632 | CheckVTableHasNoDuplicates<PointerSize::k32>(self, klass); |
| 7633 | break; |
| 7634 | } |
| 7635 | } |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 7636 | |
| 7637 | static void SanityCheckVTable(Thread* self, Handle<mirror::Class> klass, PointerSize pointer_size) |
| 7638 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 7639 | CheckClassOwnsVTableEntries(self, klass, pointer_size); |
| 7640 | CheckVTableHasNoDuplicates(self, klass, pointer_size); |
| 7641 | } |
| 7642 | |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 7643 | } // namespace |
| 7644 | |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 7645 | void ClassLinker::FillImtFromSuperClass(Handle<mirror::Class> klass, |
| 7646 | ArtMethod* unimplemented_method, |
| 7647 | ArtMethod* imt_conflict_method, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 7648 | bool* new_conflict, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 7649 | ArtMethod** imt) { |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7650 | DCHECK(klass->HasSuperClass()); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7651 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 7652 | if (super_class->ShouldHaveImt()) { |
| 7653 | ImTable* super_imt = super_class->GetImt(image_pointer_size_); |
| 7654 | for (size_t i = 0; i < ImTable::kSize; ++i) { |
| 7655 | imt[i] = super_imt->Get(i, image_pointer_size_); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7656 | } |
| 7657 | } else { |
| 7658 | // No imt in the super class, need to reconstruct from the iftable. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7659 | ObjPtr<mirror::IfTable> if_table = super_class->GetIfTable(); |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 7660 | if (if_table->Count() != 0) { |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 7661 | // Ignore copied methods since we will handle these in LinkInterfaceMethods. |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 7662 | FillIMTFromIfTable(if_table, |
| 7663 | unimplemented_method, |
| 7664 | imt_conflict_method, |
| 7665 | klass.Get(), |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 7666 | /*create_conflict_tables=*/false, |
| 7667 | /*ignore_copied_methods=*/true, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 7668 | /*out*/new_conflict, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 7669 | /*out*/imt); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7670 | } |
| 7671 | } |
| 7672 | } |
| 7673 | |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7674 | class ClassLinker::LinkInterfaceMethodsHelper { |
| 7675 | public: |
| 7676 | LinkInterfaceMethodsHelper(ClassLinker* class_linker, |
| 7677 | Handle<mirror::Class> klass, |
| 7678 | Thread* self, |
| 7679 | Runtime* runtime) |
| 7680 | : class_linker_(class_linker), |
| 7681 | klass_(klass), |
| 7682 | method_alignment_(ArtMethod::Alignment(class_linker->GetImagePointerSize())), |
| 7683 | method_size_(ArtMethod::Size(class_linker->GetImagePointerSize())), |
| 7684 | self_(self), |
| 7685 | stack_(runtime->GetLinearAlloc()->GetArenaPool()), |
| 7686 | allocator_(&stack_), |
| 7687 | default_conflict_methods_(allocator_.Adapter()), |
| 7688 | overriding_default_conflict_methods_(allocator_.Adapter()), |
| 7689 | miranda_methods_(allocator_.Adapter()), |
| 7690 | default_methods_(allocator_.Adapter()), |
| 7691 | overriding_default_methods_(allocator_.Adapter()), |
| 7692 | move_table_(allocator_.Adapter()) { |
| 7693 | } |
| 7694 | |
| 7695 | ArtMethod* FindMethod(ArtMethod* interface_method, |
| 7696 | MethodNameAndSignatureComparator& interface_name_comparator, |
| 7697 | ArtMethod* vtable_impl) |
| 7698 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 7699 | |
| 7700 | ArtMethod* GetOrCreateMirandaMethod(ArtMethod* interface_method, |
| 7701 | MethodNameAndSignatureComparator& interface_name_comparator) |
| 7702 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 7703 | |
| 7704 | bool HasNewVirtuals() const { |
| 7705 | return !(miranda_methods_.empty() && |
| 7706 | default_methods_.empty() && |
| 7707 | overriding_default_methods_.empty() && |
| 7708 | overriding_default_conflict_methods_.empty() && |
| 7709 | default_conflict_methods_.empty()); |
| 7710 | } |
| 7711 | |
| 7712 | void ReallocMethods() REQUIRES_SHARED(Locks::mutator_lock_); |
| 7713 | |
| 7714 | ObjPtr<mirror::PointerArray> UpdateVtable( |
| 7715 | const std::unordered_map<size_t, ClassLinker::MethodTranslation>& default_translations, |
Vladimir Marko | 3068d58 | 2019-05-28 16:39:29 +0100 | [diff] [blame] | 7716 | Handle<mirror::PointerArray> old_vtable) REQUIRES_SHARED(Locks::mutator_lock_); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7717 | |
| 7718 | void UpdateIfTable(Handle<mirror::IfTable> iftable) REQUIRES_SHARED(Locks::mutator_lock_); |
| 7719 | |
| 7720 | void UpdateIMT(ArtMethod** out_imt); |
| 7721 | |
| 7722 | void CheckNoStaleMethodsInDexCache() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 7723 | if (kIsDebugBuild) { |
| 7724 | PointerSize pointer_size = class_linker_->GetImagePointerSize(); |
| 7725 | // Check that there are no stale methods are in the dex cache array. |
| 7726 | auto* resolved_methods = klass_->GetDexCache()->GetResolvedMethods(); |
| 7727 | for (size_t i = 0, count = klass_->GetDexCache()->NumResolvedMethods(); i < count; ++i) { |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 7728 | auto pair = mirror::DexCache::GetNativePairPtrSize(resolved_methods, i, pointer_size); |
| 7729 | ArtMethod* m = pair.object; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7730 | CHECK(move_table_.find(m) == move_table_.end() || |
| 7731 | // The original versions of copied methods will still be present so allow those too. |
| 7732 | // Note that if the first check passes this might fail to GetDeclaringClass(). |
| 7733 | std::find_if(m->GetDeclaringClass()->GetMethods(pointer_size).begin(), |
| 7734 | m->GetDeclaringClass()->GetMethods(pointer_size).end(), |
| 7735 | [m] (ArtMethod& meth) { |
| 7736 | return &meth == m; |
| 7737 | }) != m->GetDeclaringClass()->GetMethods(pointer_size).end()) |
| 7738 | << "Obsolete method " << m->PrettyMethod() << " is in dex cache!"; |
| 7739 | } |
| 7740 | } |
| 7741 | } |
| 7742 | |
| 7743 | void ClobberOldMethods(LengthPrefixedArray<ArtMethod>* old_methods, |
| 7744 | LengthPrefixedArray<ArtMethod>* methods) { |
| 7745 | if (kIsDebugBuild) { |
| 7746 | CHECK(methods != nullptr); |
| 7747 | // Put some random garbage in old methods to help find stale pointers. |
| 7748 | if (methods != old_methods && old_methods != nullptr) { |
| 7749 | // Need to make sure the GC is not running since it could be scanning the methods we are |
| 7750 | // about to overwrite. |
| 7751 | ScopedThreadStateChange tsc(self_, kSuspended); |
| 7752 | gc::ScopedGCCriticalSection gcs(self_, |
| 7753 | gc::kGcCauseClassLinker, |
| 7754 | gc::kCollectorTypeClassLinker); |
| 7755 | const size_t old_size = LengthPrefixedArray<ArtMethod>::ComputeSize(old_methods->size(), |
| 7756 | method_size_, |
| 7757 | method_alignment_); |
| 7758 | memset(old_methods, 0xFEu, old_size); |
| 7759 | } |
| 7760 | } |
| 7761 | } |
| 7762 | |
| 7763 | private: |
| 7764 | size_t NumberOfNewVirtuals() const { |
| 7765 | return miranda_methods_.size() + |
| 7766 | default_methods_.size() + |
| 7767 | overriding_default_conflict_methods_.size() + |
| 7768 | overriding_default_methods_.size() + |
| 7769 | default_conflict_methods_.size(); |
| 7770 | } |
| 7771 | |
| 7772 | bool FillTables() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 7773 | return !klass_->IsInterface(); |
| 7774 | } |
| 7775 | |
| 7776 | void LogNewVirtuals() const REQUIRES_SHARED(Locks::mutator_lock_) { |
| 7777 | DCHECK(!klass_->IsInterface() || (default_methods_.empty() && miranda_methods_.empty())) |
| 7778 | << "Interfaces should only have default-conflict methods appended to them."; |
| 7779 | VLOG(class_linker) << mirror::Class::PrettyClass(klass_.Get()) << ": miranda_methods=" |
| 7780 | << miranda_methods_.size() |
| 7781 | << " default_methods=" << default_methods_.size() |
| 7782 | << " overriding_default_methods=" << overriding_default_methods_.size() |
| 7783 | << " default_conflict_methods=" << default_conflict_methods_.size() |
| 7784 | << " overriding_default_conflict_methods=" |
| 7785 | << overriding_default_conflict_methods_.size(); |
| 7786 | } |
| 7787 | |
| 7788 | ClassLinker* class_linker_; |
| 7789 | Handle<mirror::Class> klass_; |
| 7790 | size_t method_alignment_; |
| 7791 | size_t method_size_; |
| 7792 | Thread* const self_; |
| 7793 | |
| 7794 | // These are allocated on the heap to begin, we then transfer to linear alloc when we re-create |
| 7795 | // the virtual methods array. |
| 7796 | // Need to use low 4GB arenas for compiler or else the pointers wont fit in 32 bit method array |
| 7797 | // during cross compilation. |
| 7798 | // Use the linear alloc pool since this one is in the low 4gb for the compiler. |
| 7799 | ArenaStack stack_; |
| 7800 | ScopedArenaAllocator allocator_; |
| 7801 | |
| 7802 | ScopedArenaVector<ArtMethod*> default_conflict_methods_; |
| 7803 | ScopedArenaVector<ArtMethod*> overriding_default_conflict_methods_; |
| 7804 | ScopedArenaVector<ArtMethod*> miranda_methods_; |
| 7805 | ScopedArenaVector<ArtMethod*> default_methods_; |
| 7806 | ScopedArenaVector<ArtMethod*> overriding_default_methods_; |
| 7807 | |
| 7808 | ScopedArenaUnorderedMap<ArtMethod*, ArtMethod*> move_table_; |
| 7809 | }; |
| 7810 | |
| 7811 | ArtMethod* ClassLinker::LinkInterfaceMethodsHelper::FindMethod( |
| 7812 | ArtMethod* interface_method, |
| 7813 | MethodNameAndSignatureComparator& interface_name_comparator, |
| 7814 | ArtMethod* vtable_impl) { |
| 7815 | ArtMethod* current_method = nullptr; |
| 7816 | switch (class_linker_->FindDefaultMethodImplementation(self_, |
| 7817 | interface_method, |
| 7818 | klass_, |
| 7819 | /*out*/¤t_method)) { |
| 7820 | case DefaultMethodSearchResult::kDefaultConflict: { |
| 7821 | // Default method conflict. |
| 7822 | DCHECK(current_method == nullptr); |
| 7823 | ArtMethod* default_conflict_method = nullptr; |
| 7824 | if (vtable_impl != nullptr && vtable_impl->IsDefaultConflicting()) { |
| 7825 | // We can reuse the method from the superclass, don't bother adding it to virtuals. |
| 7826 | default_conflict_method = vtable_impl; |
| 7827 | } else { |
| 7828 | // See if we already have a conflict method for this method. |
| 7829 | ArtMethod* preexisting_conflict = FindSameNameAndSignature( |
| 7830 | interface_name_comparator, |
| 7831 | default_conflict_methods_, |
| 7832 | overriding_default_conflict_methods_); |
| 7833 | if (LIKELY(preexisting_conflict != nullptr)) { |
| 7834 | // We already have another conflict we can reuse. |
| 7835 | default_conflict_method = preexisting_conflict; |
| 7836 | } else { |
| 7837 | // Note that we do this even if we are an interface since we need to create this and |
| 7838 | // cannot reuse another classes. |
| 7839 | // Create a new conflict method for this to use. |
| 7840 | default_conflict_method = reinterpret_cast<ArtMethod*>(allocator_.Alloc(method_size_)); |
| 7841 | new(default_conflict_method) ArtMethod(interface_method, |
| 7842 | class_linker_->GetImagePointerSize()); |
| 7843 | if (vtable_impl == nullptr) { |
| 7844 | // Save the conflict method. We need to add it to the vtable. |
| 7845 | default_conflict_methods_.push_back(default_conflict_method); |
| 7846 | } else { |
| 7847 | // Save the conflict method but it is already in the vtable. |
| 7848 | overriding_default_conflict_methods_.push_back(default_conflict_method); |
| 7849 | } |
| 7850 | } |
| 7851 | } |
| 7852 | current_method = default_conflict_method; |
| 7853 | break; |
| 7854 | } // case kDefaultConflict |
| 7855 | case DefaultMethodSearchResult::kDefaultFound: { |
| 7856 | DCHECK(current_method != nullptr); |
| 7857 | // Found a default method. |
| 7858 | if (vtable_impl != nullptr && |
| 7859 | current_method->GetDeclaringClass() == vtable_impl->GetDeclaringClass()) { |
| 7860 | // We found a default method but it was the same one we already have from our |
| 7861 | // superclass. Don't bother adding it to our vtable again. |
| 7862 | current_method = vtable_impl; |
| 7863 | } else if (LIKELY(FillTables())) { |
| 7864 | // Interfaces don't need to copy default methods since they don't have vtables. |
| 7865 | // Only record this default method if it is new to save space. |
| 7866 | // TODO It might be worthwhile to copy default methods on interfaces anyway since it |
| 7867 | // would make lookup for interface super much faster. (We would only need to scan |
| 7868 | // the iftable to find if there is a NSME or AME.) |
| 7869 | ArtMethod* old = FindSameNameAndSignature(interface_name_comparator, |
| 7870 | default_methods_, |
| 7871 | overriding_default_methods_); |
| 7872 | if (old == nullptr) { |
| 7873 | // We found a default method implementation and there were no conflicts. |
| 7874 | if (vtable_impl == nullptr) { |
| 7875 | // Save the default method. We need to add it to the vtable. |
| 7876 | default_methods_.push_back(current_method); |
| 7877 | } else { |
| 7878 | // Save the default method but it is already in the vtable. |
| 7879 | overriding_default_methods_.push_back(current_method); |
| 7880 | } |
| 7881 | } else { |
| 7882 | CHECK(old == current_method) << "Multiple default implementations selected!"; |
| 7883 | } |
| 7884 | } |
| 7885 | break; |
| 7886 | } // case kDefaultFound |
| 7887 | case DefaultMethodSearchResult::kAbstractFound: { |
| 7888 | DCHECK(current_method == nullptr); |
| 7889 | // Abstract method masks all defaults. |
| 7890 | if (vtable_impl != nullptr && |
| 7891 | vtable_impl->IsAbstract() && |
| 7892 | !vtable_impl->IsDefaultConflicting()) { |
| 7893 | // We need to make this an abstract method but the version in the vtable already is so |
| 7894 | // don't do anything. |
| 7895 | current_method = vtable_impl; |
| 7896 | } |
| 7897 | break; |
| 7898 | } // case kAbstractFound |
| 7899 | } |
| 7900 | return current_method; |
| 7901 | } |
| 7902 | |
| 7903 | ArtMethod* ClassLinker::LinkInterfaceMethodsHelper::GetOrCreateMirandaMethod( |
| 7904 | ArtMethod* interface_method, |
| 7905 | MethodNameAndSignatureComparator& interface_name_comparator) { |
| 7906 | // Find out if there is already a miranda method we can use. |
| 7907 | ArtMethod* miranda_method = FindSameNameAndSignature(interface_name_comparator, |
| 7908 | miranda_methods_); |
| 7909 | if (miranda_method == nullptr) { |
| 7910 | DCHECK(interface_method->IsAbstract()) << interface_method->PrettyMethod(); |
| 7911 | miranda_method = reinterpret_cast<ArtMethod*>(allocator_.Alloc(method_size_)); |
| 7912 | CHECK(miranda_method != nullptr); |
| 7913 | // Point the interface table at a phantom slot. |
| 7914 | new(miranda_method) ArtMethod(interface_method, class_linker_->GetImagePointerSize()); |
| 7915 | miranda_methods_.push_back(miranda_method); |
| 7916 | } |
| 7917 | return miranda_method; |
| 7918 | } |
| 7919 | |
| 7920 | void ClassLinker::LinkInterfaceMethodsHelper::ReallocMethods() { |
| 7921 | LogNewVirtuals(); |
| 7922 | |
| 7923 | const size_t old_method_count = klass_->NumMethods(); |
| 7924 | const size_t new_method_count = old_method_count + NumberOfNewVirtuals(); |
| 7925 | DCHECK_NE(old_method_count, new_method_count); |
| 7926 | |
| 7927 | // Attempt to realloc to save RAM if possible. |
| 7928 | LengthPrefixedArray<ArtMethod>* old_methods = klass_->GetMethodsPtr(); |
| 7929 | // The Realloced virtual methods aren't visible from the class roots, so there is no issue |
| 7930 | // where GCs could attempt to mark stale pointers due to memcpy. And since we overwrite the |
| 7931 | // realloced memory with out->CopyFrom, we are guaranteed to have objects in the to space since |
| 7932 | // CopyFrom has internal read barriers. |
| 7933 | // |
| 7934 | // TODO We should maybe move some of this into mirror::Class or at least into another method. |
| 7935 | const size_t old_size = LengthPrefixedArray<ArtMethod>::ComputeSize(old_method_count, |
| 7936 | method_size_, |
| 7937 | method_alignment_); |
| 7938 | const size_t new_size = LengthPrefixedArray<ArtMethod>::ComputeSize(new_method_count, |
| 7939 | method_size_, |
| 7940 | method_alignment_); |
| 7941 | const size_t old_methods_ptr_size = (old_methods != nullptr) ? old_size : 0; |
| 7942 | auto* methods = reinterpret_cast<LengthPrefixedArray<ArtMethod>*>( |
Nicolas Geoffray | 48b40cc | 2017-08-07 16:52:40 +0100 | [diff] [blame] | 7943 | class_linker_->GetAllocatorForClassLoader(klass_->GetClassLoader())->Realloc( |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7944 | self_, old_methods, old_methods_ptr_size, new_size)); |
| 7945 | CHECK(methods != nullptr); // Native allocation failure aborts. |
| 7946 | |
| 7947 | PointerSize pointer_size = class_linker_->GetImagePointerSize(); |
| 7948 | if (methods != old_methods) { |
| 7949 | // Maps from heap allocated miranda method to linear alloc miranda method. |
| 7950 | StrideIterator<ArtMethod> out = methods->begin(method_size_, method_alignment_); |
| 7951 | // Copy over the old methods. |
| 7952 | for (auto& m : klass_->GetMethods(pointer_size)) { |
| 7953 | move_table_.emplace(&m, &*out); |
| 7954 | // The CopyFrom is only necessary to not miss read barriers since Realloc won't do read |
| 7955 | // barriers when it copies. |
| 7956 | out->CopyFrom(&m, pointer_size); |
| 7957 | ++out; |
| 7958 | } |
| 7959 | } |
| 7960 | StrideIterator<ArtMethod> out(methods->begin(method_size_, method_alignment_) + old_method_count); |
| 7961 | // Copy over miranda methods before copying vtable since CopyOf may cause thread suspension and |
| 7962 | // we want the roots of the miranda methods to get visited. |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7963 | for (size_t i = 0; i < miranda_methods_.size(); ++i) { |
| 7964 | ArtMethod* mir_method = miranda_methods_[i]; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7965 | ArtMethod& new_method = *out; |
| 7966 | new_method.CopyFrom(mir_method, pointer_size); |
| 7967 | new_method.SetAccessFlags(new_method.GetAccessFlags() | kAccMiranda | kAccCopied); |
| 7968 | DCHECK_NE(new_method.GetAccessFlags() & kAccAbstract, 0u) |
| 7969 | << "Miranda method should be abstract!"; |
| 7970 | move_table_.emplace(mir_method, &new_method); |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7971 | // Update the entry in the method array, as the array will be used for future lookups, |
| 7972 | // where thread suspension is allowed. |
| 7973 | // As such, the array should not contain locally allocated ArtMethod, otherwise the GC |
| 7974 | // would not see them. |
| 7975 | miranda_methods_[i] = &new_method; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7976 | ++out; |
| 7977 | } |
| 7978 | // We need to copy the default methods into our own method table since the runtime requires that |
| 7979 | // every method on a class's vtable be in that respective class's virtual method table. |
| 7980 | // NOTE This means that two classes might have the same implementation of a method from the same |
| 7981 | // interface but will have different ArtMethod*s for them. This also means we cannot compare a |
| 7982 | // default method found on a class with one found on the declaring interface directly and must |
| 7983 | // look at the declaring class to determine if they are the same. |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7984 | for (ScopedArenaVector<ArtMethod*>* methods_vec : {&default_methods_, |
| 7985 | &overriding_default_methods_}) { |
| 7986 | for (size_t i = 0; i < methods_vec->size(); ++i) { |
| 7987 | ArtMethod* def_method = (*methods_vec)[i]; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7988 | ArtMethod& new_method = *out; |
| 7989 | new_method.CopyFrom(def_method, pointer_size); |
| 7990 | // Clear the kAccSkipAccessChecks flag if it is present. Since this class hasn't been |
| 7991 | // verified yet it shouldn't have methods that are skipping access checks. |
| 7992 | // TODO This is rather arbitrary. We should maybe support classes where only some of its |
| 7993 | // methods are skip_access_checks. |
Vladimir Marko | b0a6aee | 2017-10-27 10:34:04 +0100 | [diff] [blame] | 7994 | DCHECK_EQ(new_method.GetAccessFlags() & kAccNative, 0u); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7995 | constexpr uint32_t kSetFlags = kAccDefault | kAccCopied; |
| 7996 | constexpr uint32_t kMaskFlags = ~kAccSkipAccessChecks; |
| 7997 | new_method.SetAccessFlags((new_method.GetAccessFlags() | kSetFlags) & kMaskFlags); |
| 7998 | move_table_.emplace(def_method, &new_method); |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7999 | // Update the entry in the method array, as the array will be used for future lookups, |
| 8000 | // where thread suspension is allowed. |
| 8001 | // As such, the array should not contain locally allocated ArtMethod, otherwise the GC |
| 8002 | // would not see them. |
| 8003 | (*methods_vec)[i] = &new_method; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 8004 | ++out; |
| 8005 | } |
| 8006 | } |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 8007 | for (ScopedArenaVector<ArtMethod*>* methods_vec : {&default_conflict_methods_, |
| 8008 | &overriding_default_conflict_methods_}) { |
| 8009 | for (size_t i = 0; i < methods_vec->size(); ++i) { |
| 8010 | ArtMethod* conf_method = (*methods_vec)[i]; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 8011 | ArtMethod& new_method = *out; |
| 8012 | new_method.CopyFrom(conf_method, pointer_size); |
| 8013 | // This is a type of default method (there are default method impls, just a conflict) so |
| 8014 | // mark this as a default, non-abstract method, since thats what it is. Also clear the |
| 8015 | // kAccSkipAccessChecks bit since this class hasn't been verified yet it shouldn't have |
| 8016 | // methods that are skipping access checks. |
Nicolas Geoffray | 7aca9d5 | 2018-09-07 11:13:33 +0100 | [diff] [blame] | 8017 | // Also clear potential kAccSingleImplementation to avoid CHA trying to inline |
| 8018 | // the default method. |
Vladimir Marko | b0a6aee | 2017-10-27 10:34:04 +0100 | [diff] [blame] | 8019 | DCHECK_EQ(new_method.GetAccessFlags() & kAccNative, 0u); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 8020 | constexpr uint32_t kSetFlags = kAccDefault | kAccDefaultConflict | kAccCopied; |
Nicolas Geoffray | 7aca9d5 | 2018-09-07 11:13:33 +0100 | [diff] [blame] | 8021 | constexpr uint32_t kMaskFlags = |
| 8022 | ~(kAccAbstract | kAccSkipAccessChecks | kAccSingleImplementation); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 8023 | new_method.SetAccessFlags((new_method.GetAccessFlags() | kSetFlags) & kMaskFlags); |
| 8024 | DCHECK(new_method.IsDefaultConflicting()); |
| 8025 | // The actual method might or might not be marked abstract since we just copied it from a |
| 8026 | // (possibly default) interface method. We need to set it entry point to be the bridge so |
| 8027 | // that the compiler will not invoke the implementation of whatever method we copied from. |
| 8028 | EnsureThrowsInvocationError(class_linker_, &new_method); |
| 8029 | move_table_.emplace(conf_method, &new_method); |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 8030 | // Update the entry in the method array, as the array will be used for future lookups, |
| 8031 | // where thread suspension is allowed. |
| 8032 | // As such, the array should not contain locally allocated ArtMethod, otherwise the GC |
| 8033 | // would not see them. |
| 8034 | (*methods_vec)[i] = &new_method; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 8035 | ++out; |
| 8036 | } |
| 8037 | } |
| 8038 | methods->SetSize(new_method_count); |
| 8039 | class_linker_->UpdateClassMethods(klass_.Get(), methods); |
| 8040 | } |
| 8041 | |
| 8042 | ObjPtr<mirror::PointerArray> ClassLinker::LinkInterfaceMethodsHelper::UpdateVtable( |
| 8043 | const std::unordered_map<size_t, ClassLinker::MethodTranslation>& default_translations, |
Vladimir Marko | 3068d58 | 2019-05-28 16:39:29 +0100 | [diff] [blame] | 8044 | Handle<mirror::PointerArray> old_vtable) { |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 8045 | // Update the vtable to the new method structures. We can skip this for interfaces since they |
| 8046 | // do not have vtables. |
| 8047 | const size_t old_vtable_count = old_vtable->GetLength(); |
| 8048 | const size_t new_vtable_count = old_vtable_count + |
| 8049 | miranda_methods_.size() + |
| 8050 | default_methods_.size() + |
| 8051 | default_conflict_methods_.size(); |
| 8052 | |
Vladimir Marko | 3068d58 | 2019-05-28 16:39:29 +0100 | [diff] [blame] | 8053 | ObjPtr<mirror::PointerArray> vtable = ObjPtr<mirror::PointerArray>::DownCast( |
| 8054 | mirror::Array::CopyOf(old_vtable, self_, new_vtable_count)); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 8055 | if (UNLIKELY(vtable == nullptr)) { |
| 8056 | self_->AssertPendingOOMException(); |
| 8057 | return nullptr; |
| 8058 | } |
| 8059 | |
| 8060 | size_t vtable_pos = old_vtable_count; |
| 8061 | PointerSize pointer_size = class_linker_->GetImagePointerSize(); |
| 8062 | // Update all the newly copied method's indexes so they denote their placement in the vtable. |
| 8063 | for (const ScopedArenaVector<ArtMethod*>& methods_vec : {default_methods_, |
| 8064 | default_conflict_methods_, |
| 8065 | miranda_methods_}) { |
| 8066 | // These are the functions that are not already in the vtable! |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 8067 | for (ArtMethod* new_vtable_method : methods_vec) { |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 8068 | // Leave the declaring class alone the method's dex_code_item_offset_ and dex_method_index_ |
| 8069 | // fields are references into the dex file the method was defined in. Since the ArtMethod |
| 8070 | // does not store that information it uses declaring_class_->dex_cache_. |
| 8071 | new_vtable_method->SetMethodIndex(0xFFFF & vtable_pos); |
| 8072 | vtable->SetElementPtrSize(vtable_pos, new_vtable_method, pointer_size); |
| 8073 | ++vtable_pos; |
| 8074 | } |
| 8075 | } |
| 8076 | DCHECK_EQ(vtable_pos, new_vtable_count); |
| 8077 | |
| 8078 | // Update old vtable methods. We use the default_translations map to figure out what each |
| 8079 | // vtable entry should be updated to, if they need to be at all. |
| 8080 | for (size_t i = 0; i < old_vtable_count; ++i) { |
| 8081 | ArtMethod* translated_method = vtable->GetElementPtrSize<ArtMethod*>(i, pointer_size); |
| 8082 | // Try and find what we need to change this method to. |
| 8083 | auto translation_it = default_translations.find(i); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 8084 | if (translation_it != default_translations.end()) { |
| 8085 | if (translation_it->second.IsInConflict()) { |
| 8086 | // Find which conflict method we are to use for this method. |
| 8087 | MethodNameAndSignatureComparator old_method_comparator( |
| 8088 | translated_method->GetInterfaceMethodIfProxy(pointer_size)); |
| 8089 | // We only need to look through overriding_default_conflict_methods since this is an |
| 8090 | // overridden method we are fixing up here. |
| 8091 | ArtMethod* new_conflict_method = FindSameNameAndSignature( |
| 8092 | old_method_comparator, overriding_default_conflict_methods_); |
| 8093 | CHECK(new_conflict_method != nullptr) << "Expected a conflict method!"; |
| 8094 | translated_method = new_conflict_method; |
| 8095 | } else if (translation_it->second.IsAbstract()) { |
| 8096 | // Find which miranda method we are to use for this method. |
| 8097 | MethodNameAndSignatureComparator old_method_comparator( |
| 8098 | translated_method->GetInterfaceMethodIfProxy(pointer_size)); |
| 8099 | ArtMethod* miranda_method = FindSameNameAndSignature(old_method_comparator, |
| 8100 | miranda_methods_); |
| 8101 | DCHECK(miranda_method != nullptr); |
| 8102 | translated_method = miranda_method; |
| 8103 | } else { |
| 8104 | // Normal default method (changed from an older default or abstract interface method). |
| 8105 | DCHECK(translation_it->second.IsTranslation()); |
| 8106 | translated_method = translation_it->second.GetTranslation(); |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 8107 | auto it = move_table_.find(translated_method); |
| 8108 | DCHECK(it != move_table_.end()); |
| 8109 | translated_method = it->second; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 8110 | } |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 8111 | } else { |
| 8112 | auto it = move_table_.find(translated_method); |
| 8113 | translated_method = (it != move_table_.end()) ? it->second : nullptr; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 8114 | } |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 8115 | |
| 8116 | if (translated_method != nullptr) { |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 8117 | // Make sure the new_methods index is set. |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 8118 | if (translated_method->GetMethodIndexDuringLinking() != i) { |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 8119 | if (kIsDebugBuild) { |
| 8120 | auto* methods = klass_->GetMethodsPtr(); |
| 8121 | CHECK_LE(reinterpret_cast<uintptr_t>(&*methods->begin(method_size_, method_alignment_)), |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 8122 | reinterpret_cast<uintptr_t>(translated_method)); |
| 8123 | CHECK_LT(reinterpret_cast<uintptr_t>(translated_method), |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 8124 | reinterpret_cast<uintptr_t>(&*methods->end(method_size_, method_alignment_))); |
| 8125 | } |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 8126 | translated_method->SetMethodIndex(0xFFFF & i); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 8127 | } |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 8128 | vtable->SetElementPtrSize(i, translated_method, pointer_size); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 8129 | } |
| 8130 | } |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 8131 | klass_->SetVTable(vtable); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 8132 | return vtable; |
| 8133 | } |
| 8134 | |
| 8135 | void ClassLinker::LinkInterfaceMethodsHelper::UpdateIfTable(Handle<mirror::IfTable> iftable) { |
| 8136 | PointerSize pointer_size = class_linker_->GetImagePointerSize(); |
| 8137 | const size_t ifcount = klass_->GetIfTableCount(); |
| 8138 | // Go fix up all the stale iftable pointers. |
| 8139 | for (size_t i = 0; i < ifcount; ++i) { |
| 8140 | for (size_t j = 0, count = iftable->GetMethodArrayCount(i); j < count; ++j) { |
Vladimir Marko | 557fece | 2019-03-26 14:29:41 +0000 | [diff] [blame] | 8141 | ObjPtr<mirror::PointerArray> method_array = iftable->GetMethodArray(i); |
| 8142 | ArtMethod* m = method_array->GetElementPtrSize<ArtMethod*>(j, pointer_size); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 8143 | DCHECK(m != nullptr) << klass_->PrettyClass(); |
| 8144 | auto it = move_table_.find(m); |
| 8145 | if (it != move_table_.end()) { |
| 8146 | auto* new_m = it->second; |
| 8147 | DCHECK(new_m != nullptr) << klass_->PrettyClass(); |
| 8148 | method_array->SetElementPtrSize(j, new_m, pointer_size); |
| 8149 | } |
| 8150 | } |
| 8151 | } |
| 8152 | } |
| 8153 | |
| 8154 | void ClassLinker::LinkInterfaceMethodsHelper::UpdateIMT(ArtMethod** out_imt) { |
| 8155 | // Fix up IMT next. |
| 8156 | for (size_t i = 0; i < ImTable::kSize; ++i) { |
| 8157 | auto it = move_table_.find(out_imt[i]); |
| 8158 | if (it != move_table_.end()) { |
| 8159 | out_imt[i] = it->second; |
| 8160 | } |
| 8161 | } |
| 8162 | } |
| 8163 | |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8164 | // TODO This method needs to be split up into several smaller methods. |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 8165 | bool ClassLinker::LinkInterfaceMethods( |
| 8166 | Thread* self, |
| 8167 | Handle<mirror::Class> klass, |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 8168 | const std::unordered_map<size_t, ClassLinker::MethodTranslation>& default_translations, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 8169 | bool* out_new_conflict, |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 8170 | ArtMethod** out_imt) { |
| 8171 | StackHandleScope<3> hs(self); |
| 8172 | Runtime* const runtime = Runtime::Current(); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8173 | |
| 8174 | const bool is_interface = klass->IsInterface(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 8175 | const bool has_superclass = klass->HasSuperClass(); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8176 | const bool fill_tables = !is_interface; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 8177 | const size_t super_ifcount = has_superclass ? klass->GetSuperClass()->GetIfTableCount() : 0U; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 8178 | const size_t ifcount = klass->GetIfTableCount(); |
| 8179 | |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 8180 | Handle<mirror::IfTable> iftable(hs.NewHandle(klass->GetIfTable())); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8181 | |
| 8182 | MutableHandle<mirror::PointerArray> vtable(hs.NewHandle(klass->GetVTableDuringLinking())); |
| 8183 | ArtMethod* const unimplemented_method = runtime->GetImtUnimplementedMethod(); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 8184 | ArtMethod* const imt_conflict_method = runtime->GetImtConflictMethod(); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 8185 | // Copy the IMT from the super class if possible. |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8186 | const bool extend_super_iftable = has_superclass; |
| 8187 | if (has_superclass && fill_tables) { |
| 8188 | FillImtFromSuperClass(klass, |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8189 | unimplemented_method, |
| 8190 | imt_conflict_method, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 8191 | out_new_conflict, |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 8192 | out_imt); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 8193 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8194 | // Allocate method arrays before since we don't want miss visiting miranda method roots due to |
| 8195 | // thread suspension. |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8196 | if (fill_tables) { |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 8197 | if (!AllocateIfTableMethodArrays(self, klass, iftable)) { |
| 8198 | return false; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8199 | } |
| 8200 | } |
| 8201 | |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 8202 | LinkInterfaceMethodsHelper helper(this, klass, self, runtime); |
| 8203 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 8204 | auto* old_cause = self->StartAssertNoThreadSuspension( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8205 | "Copying ArtMethods for LinkInterfaceMethods"); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 8206 | // Going in reverse to ensure that we will hit abstract methods that override defaults before the |
| 8207 | // defaults. This means we don't need to do any trickery when creating the Miranda methods, since |
| 8208 | // they will already be null. This has the additional benefit that the declarer of a miranda |
| 8209 | // method will actually declare an abstract method. |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 8210 | for (size_t i = ifcount; i != 0u; ) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 8211 | --i; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 8212 | DCHECK_LT(i, ifcount); |
| 8213 | |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8214 | size_t num_methods = iftable->GetInterface(i)->NumDeclaredVirtualMethods(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8215 | if (num_methods > 0) { |
| 8216 | StackHandleScope<2> hs2(self); |
| 8217 | const bool is_super = i < super_ifcount; |
| 8218 | const bool super_interface = is_super && extend_super_iftable; |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8219 | // We don't actually create or fill these tables for interfaces, we just copy some methods for |
| 8220 | // conflict methods. Just set this as nullptr in those cases. |
| 8221 | Handle<mirror::PointerArray> method_array(fill_tables |
| 8222 | ? hs2.NewHandle(iftable->GetMethodArray(i)) |
| 8223 | : hs2.NewHandle<mirror::PointerArray>(nullptr)); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8224 | |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 8225 | ArraySlice<ArtMethod> input_virtual_methods; |
Mathieu Chartier | 9865bde | 2015-12-21 09:58:16 -0800 | [diff] [blame] | 8226 | ScopedNullHandle<mirror::PointerArray> null_handle; |
| 8227 | Handle<mirror::PointerArray> input_vtable_array(null_handle); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8228 | int32_t input_array_length = 0; |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 8229 | |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 8230 | // TODO Cleanup Needed: In the presence of default methods this optimization is rather dirty |
| 8231 | // and confusing. Default methods should always look through all the superclasses |
| 8232 | // because they are the last choice of an implementation. We get around this by looking |
| 8233 | // at the super-classes iftable methods (copied into method_array previously) when we are |
| 8234 | // 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] | 8235 | bool using_virtuals; |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8236 | if (super_interface || is_interface) { |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 8237 | // 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] | 8238 | // whole vtable. |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 8239 | using_virtuals = true; |
| 8240 | input_virtual_methods = klass->GetDeclaredMethodsSlice(image_pointer_size_); |
| 8241 | input_array_length = input_virtual_methods.size(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8242 | } else { |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 8243 | // For a new interface, however, we need the whole vtable in case a new |
| 8244 | // interface method is implemented in the whole superclass. |
| 8245 | using_virtuals = false; |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8246 | DCHECK(vtable != nullptr); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8247 | input_vtable_array = vtable; |
| 8248 | input_array_length = input_vtable_array->GetLength(); |
| 8249 | } |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 8250 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 8251 | // For each method in interface |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 8252 | for (size_t j = 0; j < num_methods; ++j) { |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 8253 | auto* interface_method = iftable->GetInterface(i)->GetVirtualMethod(j, image_pointer_size_); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 8254 | MethodNameAndSignatureComparator interface_name_comparator( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8255 | interface_method->GetInterfaceMethodIfProxy(image_pointer_size_)); |
David Srbecky | e36e7f2 | 2018-11-14 14:21:23 +0000 | [diff] [blame] | 8256 | uint32_t imt_index = interface_method->GetImtIndex(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 8257 | ArtMethod** imt_ptr = &out_imt[imt_index]; |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 8258 | // For each method listed in the interface's method list, find the |
| 8259 | // matching method in our class's method list. We want to favor the |
| 8260 | // subclass over the superclass, which just requires walking |
| 8261 | // back from the end of the vtable. (This only matters if the |
| 8262 | // superclass defines a private method and this class redefines |
| 8263 | // it -- otherwise it would use the same vtable slot. In .dex files |
| 8264 | // those don't end up in the virtual method table, so it shouldn't |
| 8265 | // matter which direction we go. We walk it backward anyway.) |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 8266 | // |
| 8267 | // To find defaults we need to do the same but also go over interfaces. |
| 8268 | bool found_impl = false; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 8269 | ArtMethod* vtable_impl = nullptr; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 8270 | for (int32_t k = input_array_length - 1; k >= 0; --k) { |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 8271 | ArtMethod* vtable_method = using_virtuals ? |
| 8272 | &input_virtual_methods[k] : |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8273 | input_vtable_array->GetElementPtrSize<ArtMethod*>(k, image_pointer_size_); |
| 8274 | ArtMethod* vtable_method_for_name_comparison = |
| 8275 | vtable_method->GetInterfaceMethodIfProxy(image_pointer_size_); |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 8276 | if (interface_name_comparator.HasSameNameAndSignature( |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 8277 | vtable_method_for_name_comparison)) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 8278 | if (!vtable_method->IsAbstract() && !vtable_method->IsPublic()) { |
Mathieu Chartier | 4d122c1 | 2015-06-17 14:14:36 -0700 | [diff] [blame] | 8279 | // Must do EndAssertNoThreadSuspension before throw since the throw can cause |
| 8280 | // allocations. |
| 8281 | self->EndAssertNoThreadSuspension(old_cause); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8282 | ThrowIllegalAccessError(klass.Get(), |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 8283 | "Method '%s' implementing interface method '%s' is not public", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 8284 | vtable_method->PrettyMethod().c_str(), |
| 8285 | interface_method->PrettyMethod().c_str()); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 8286 | return false; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 8287 | } else if (UNLIKELY(vtable_method->IsOverridableByDefaultMethod())) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 8288 | // We might have a newer, better, default method for this, so we just skip it. If we |
| 8289 | // are still using this we will select it again when scanning for default methods. To |
| 8290 | // obviate the need to copy the method again we will make a note that we already found |
| 8291 | // a default here. |
| 8292 | // TODO This should be much cleaner. |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 8293 | vtable_impl = vtable_method; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 8294 | break; |
| 8295 | } else { |
| 8296 | found_impl = true; |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8297 | if (LIKELY(fill_tables)) { |
| 8298 | method_array->SetElementPtrSize(j, vtable_method, image_pointer_size_); |
| 8299 | // Place method in imt if entry is empty, place conflict otherwise. |
| 8300 | SetIMTRef(unimplemented_method, |
| 8301 | imt_conflict_method, |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8302 | vtable_method, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 8303 | /*out*/out_new_conflict, |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8304 | /*out*/imt_ptr); |
| 8305 | } |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 8306 | break; |
| 8307 | } |
| 8308 | } |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 8309 | } |
| 8310 | // Continue on to the next method if we are done. |
| 8311 | if (LIKELY(found_impl)) { |
| 8312 | continue; |
| 8313 | } else if (LIKELY(super_interface)) { |
| 8314 | // Don't look for a default implementation when the super-method is implemented directly |
| 8315 | // by the class. |
| 8316 | // |
| 8317 | // See if we can use the superclasses method and skip searching everything else. |
| 8318 | // Note: !found_impl && super_interface |
| 8319 | CHECK(extend_super_iftable); |
| 8320 | // If this is a super_interface method it is possible we shouldn't override it because a |
| 8321 | // superclass could have implemented it directly. We get the method the superclass used |
| 8322 | // to implement this to know if we can override it with a default method. Doing this is |
| 8323 | // safe since we know that the super_iftable is filled in so we can simply pull it from |
| 8324 | // there. We don't bother if this is not a super-classes interface since in that case we |
| 8325 | // have scanned the entire vtable anyway and would have found it. |
| 8326 | // TODO This is rather dirty but it is faster than searching through the entire vtable |
| 8327 | // every time. |
| 8328 | ArtMethod* supers_method = |
| 8329 | method_array->GetElementPtrSize<ArtMethod*>(j, image_pointer_size_); |
| 8330 | DCHECK(supers_method != nullptr); |
| 8331 | DCHECK(interface_name_comparator.HasSameNameAndSignature(supers_method)); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8332 | if (LIKELY(!supers_method->IsOverridableByDefaultMethod())) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 8333 | // The method is not overridable by a default method (i.e. it is directly implemented |
| 8334 | // in some class). Therefore move onto the next interface method. |
| 8335 | continue; |
Alex Light | d6c2bfa | 2016-05-02 18:51:34 -0700 | [diff] [blame] | 8336 | } else { |
| 8337 | // If the super-classes method is override-able by a default method we need to keep |
| 8338 | // track of it since though it is override-able it is not guaranteed to be 'overridden'. |
| 8339 | // 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] | 8340 | // to the vtable twice, causing corruption (vtable entries having inconsistent and |
| 8341 | // illegal states, incorrect vtable size, and incorrect or inconsistent iftable entries) |
| 8342 | // in this class and any subclasses. |
Alex Light | d6c2bfa | 2016-05-02 18:51:34 -0700 | [diff] [blame] | 8343 | DCHECK(vtable_impl == nullptr || vtable_impl == supers_method) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 8344 | << "vtable_impl was " << ArtMethod::PrettyMethod(vtable_impl) |
| 8345 | << " and not 'nullptr' or " |
| 8346 | << supers_method->PrettyMethod() |
| 8347 | << " as expected. IFTable appears to be corrupt!"; |
Alex Light | d6c2bfa | 2016-05-02 18:51:34 -0700 | [diff] [blame] | 8348 | vtable_impl = supers_method; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 8349 | } |
| 8350 | } |
| 8351 | // 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] | 8352 | ArtMethod* current_method = helper.FindMethod(interface_method, |
| 8353 | interface_name_comparator, |
| 8354 | vtable_impl); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8355 | if (LIKELY(fill_tables)) { |
Alex Light | 1277108 | 2016-01-26 16:07:41 -0800 | [diff] [blame] | 8356 | if (current_method == nullptr && !super_interface) { |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8357 | // We could not find an implementation for this method and since it is a brand new |
| 8358 | // interface we searched the entire vtable (and all default methods) for an |
| 8359 | // 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] | 8360 | current_method = helper.GetOrCreateMirandaMethod(interface_method, |
| 8361 | interface_name_comparator); |
Alex Light | 1277108 | 2016-01-26 16:07:41 -0800 | [diff] [blame] | 8362 | } |
| 8363 | |
| 8364 | if (current_method != nullptr) { |
| 8365 | // We found a default method implementation. Record it in the iftable and IMT. |
| 8366 | method_array->SetElementPtrSize(j, current_method, image_pointer_size_); |
| 8367 | SetIMTRef(unimplemented_method, |
| 8368 | imt_conflict_method, |
Alex Light | 1277108 | 2016-01-26 16:07:41 -0800 | [diff] [blame] | 8369 | current_method, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 8370 | /*out*/out_new_conflict, |
Alex Light | 1277108 | 2016-01-26 16:07:41 -0800 | [diff] [blame] | 8371 | /*out*/imt_ptr); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 8372 | } |
| 8373 | } |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8374 | } // For each method in interface end. |
| 8375 | } // if (num_methods > 0) |
| 8376 | } // For each interface. |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8377 | // TODO don't extend virtuals of interface unless necessary (when is it?). |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 8378 | if (helper.HasNewVirtuals()) { |
| 8379 | LengthPrefixedArray<ArtMethod>* old_methods = kIsDebugBuild ? klass->GetMethodsPtr() : nullptr; |
| 8380 | helper.ReallocMethods(); // No return value to check. Native allocation failure aborts. |
| 8381 | LengthPrefixedArray<ArtMethod>* methods = kIsDebugBuild ? klass->GetMethodsPtr() : nullptr; |
| 8382 | |
Mathieu Chartier | d4d83b8 | 2015-06-19 20:24:45 -0700 | [diff] [blame] | 8383 | // 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] | 8384 | // suspension assert. |
| 8385 | self->EndAssertNoThreadSuspension(old_cause); |
Mathieu Chartier | d4d83b8 | 2015-06-19 20:24:45 -0700 | [diff] [blame] | 8386 | |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8387 | if (fill_tables) { |
Vladimir Marko | 3068d58 | 2019-05-28 16:39:29 +0100 | [diff] [blame] | 8388 | vtable.Assign(helper.UpdateVtable(default_translations, vtable)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8389 | if (UNLIKELY(vtable == nullptr)) { |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 8390 | // The helper has already called self->AssertPendingOOMException(); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8391 | return false; |
| 8392 | } |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 8393 | helper.UpdateIfTable(iftable); |
| 8394 | helper.UpdateIMT(out_imt); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8395 | } |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8396 | |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 8397 | helper.CheckNoStaleMethodsInDexCache(); |
| 8398 | helper.ClobberOldMethods(old_methods, methods); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8399 | } else { |
| 8400 | self->EndAssertNoThreadSuspension(old_cause); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 8401 | } |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8402 | if (kIsDebugBuild && !is_interface) { |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 8403 | SanityCheckVTable(self, klass, image_pointer_size_); |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 8404 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 8405 | return true; |
| 8406 | } |
| 8407 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 8408 | bool ClassLinker::LinkInstanceFields(Thread* self, Handle<mirror::Class> klass) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8409 | CHECK(klass != nullptr); |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 8410 | return LinkFields(self, klass, false, nullptr); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 8411 | } |
| 8412 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 8413 | 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] | 8414 | CHECK(klass != nullptr); |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 8415 | return LinkFields(self, klass, true, class_size); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 8416 | } |
| 8417 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 8418 | struct LinkFieldsComparator { |
Igor Murashkin | 2ffb703 | 2017-11-08 13:35:21 -0800 | [diff] [blame] | 8419 | LinkFieldsComparator() REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 8420 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 8421 | // 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] | 8422 | bool operator()(ArtField* field1, ArtField* field2) |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 8423 | NO_THREAD_SAFETY_ANALYSIS { |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 8424 | // 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] | 8425 | Primitive::Type type1 = field1->GetTypeAsPrimitiveType(); |
| 8426 | Primitive::Type type2 = field2->GetTypeAsPrimitiveType(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 8427 | if (type1 != type2) { |
Vladimir Marko | d577748 | 2014-11-12 17:02:02 +0000 | [diff] [blame] | 8428 | if (type1 == Primitive::kPrimNot) { |
| 8429 | // Reference always goes first. |
| 8430 | return true; |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 8431 | } |
Vladimir Marko | d577748 | 2014-11-12 17:02:02 +0000 | [diff] [blame] | 8432 | if (type2 == Primitive::kPrimNot) { |
| 8433 | // Reference always goes first. |
| 8434 | return false; |
| 8435 | } |
| 8436 | size_t size1 = Primitive::ComponentSize(type1); |
| 8437 | size_t size2 = Primitive::ComponentSize(type2); |
| 8438 | if (size1 != size2) { |
| 8439 | // Larger primitive types go first. |
| 8440 | return size1 > size2; |
| 8441 | } |
| 8442 | // Primitive types differ but sizes match. Arbitrarily order by primitive type. |
| 8443 | return type1 < type2; |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 8444 | } |
Vladimir Marko | 7a7c1db | 2014-11-17 15:13:34 +0000 | [diff] [blame] | 8445 | // Same basic group? Then sort by dex field index. This is guaranteed to be sorted |
| 8446 | // by name and for equal names by type id index. |
| 8447 | // NOTE: This works also for proxies. Their static fields are assigned appropriate indexes. |
| 8448 | return field1->GetDexFieldIndex() < field2->GetDexFieldIndex(); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 8449 | } |
| 8450 | }; |
| 8451 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 8452 | bool ClassLinker::LinkFields(Thread* self, |
| 8453 | Handle<mirror::Class> klass, |
| 8454 | bool is_static, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 8455 | size_t* class_size) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 8456 | self->AllowThreadSuspension(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8457 | const size_t num_fields = is_static ? klass->NumStaticFields() : klass->NumInstanceFields(); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 8458 | LengthPrefixedArray<ArtField>* const fields = is_static ? klass->GetSFieldsPtr() : |
| 8459 | klass->GetIFieldsPtr(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8460 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 8461 | // Initialize field_offset |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 8462 | MemberOffset field_offset(0); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 8463 | if (is_static) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8464 | field_offset = klass->GetFirstReferenceStaticFieldOffsetDuringLinking(image_pointer_size_); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 8465 | } else { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 8466 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 8467 | if (super_class != nullptr) { |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 8468 | CHECK(super_class->IsResolved()) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 8469 | << klass->PrettyClass() << " " << super_class->PrettyClass(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8470 | field_offset = MemberOffset(super_class->GetObjectSize()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8471 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8472 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8473 | |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 8474 | CHECK_EQ(num_fields == 0, fields == nullptr) << klass->PrettyClass(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8475 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 8476 | // we want a relatively stable order so that adding new fields |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 8477 | // minimizes disruption of C++ version such as Class and Method. |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 8478 | // |
| 8479 | // The overall sort order order is: |
| 8480 | // 1) All object reference fields, sorted alphabetically. |
| 8481 | // 2) All java long (64-bit) integer fields, sorted alphabetically. |
| 8482 | // 3) All java double (64-bit) floating point fields, sorted alphabetically. |
| 8483 | // 4) All java int (32-bit) integer fields, sorted alphabetically. |
| 8484 | // 5) All java float (32-bit) floating point fields, sorted alphabetically. |
| 8485 | // 6) All java char (16-bit) integer fields, sorted alphabetically. |
| 8486 | // 7) All java short (16-bit) integer fields, sorted alphabetically. |
| 8487 | // 8) All java boolean (8-bit) integer fields, sorted alphabetically. |
| 8488 | // 9) All java byte (8-bit) integer fields, sorted alphabetically. |
| 8489 | // |
| 8490 | // Once the fields are sorted in this order we will attempt to fill any gaps that might be present |
| 8491 | // 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] | 8492 | std::deque<ArtField*> grouped_and_sorted_fields; |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 8493 | const char* old_no_suspend_cause = self->StartAssertNoThreadSuspension( |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 8494 | "Naked ArtField references in deque"); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 8495 | for (size_t i = 0; i < num_fields; i++) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 8496 | grouped_and_sorted_fields.push_back(&fields->At(i)); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 8497 | } |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 8498 | std::sort(grouped_and_sorted_fields.begin(), grouped_and_sorted_fields.end(), |
| 8499 | LinkFieldsComparator()); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 8500 | |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 8501 | // References should be at the front. |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 8502 | size_t current_field = 0; |
| 8503 | size_t num_reference_fields = 0; |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 8504 | FieldGaps gaps; |
| 8505 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 8506 | for (; current_field < num_fields; current_field++) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8507 | ArtField* field = grouped_and_sorted_fields.front(); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 8508 | Primitive::Type type = field->GetTypeAsPrimitiveType(); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 8509 | bool isPrimitive = type != Primitive::kPrimNot; |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 8510 | if (isPrimitive) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 8511 | break; // past last reference, move on to the next phase |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 8512 | } |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 8513 | if (UNLIKELY(!IsAligned<sizeof(mirror::HeapReference<mirror::Object>)>( |
| 8514 | field_offset.Uint32Value()))) { |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 8515 | MemberOffset old_offset = field_offset; |
| 8516 | field_offset = MemberOffset(RoundUp(field_offset.Uint32Value(), 4)); |
| 8517 | AddFieldGap(old_offset.Uint32Value(), field_offset.Uint32Value(), &gaps); |
| 8518 | } |
Roland Levillain | 14d9057 | 2015-07-16 10:52:26 +0100 | [diff] [blame] | 8519 | DCHECK_ALIGNED(field_offset.Uint32Value(), sizeof(mirror::HeapReference<mirror::Object>)); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 8520 | grouped_and_sorted_fields.pop_front(); |
| 8521 | num_reference_fields++; |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8522 | field->SetOffset(field_offset); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 8523 | field_offset = MemberOffset(field_offset.Uint32Value() + |
| 8524 | sizeof(mirror::HeapReference<mirror::Object>)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 8525 | } |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 8526 | // Gaps are stored as a max heap which means that we must shuffle from largest to smallest |
| 8527 | // otherwise we could end up with suboptimal gap fills. |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 8528 | ShuffleForward<8>(¤t_field, &field_offset, &grouped_and_sorted_fields, &gaps); |
| 8529 | ShuffleForward<4>(¤t_field, &field_offset, &grouped_and_sorted_fields, &gaps); |
| 8530 | ShuffleForward<2>(¤t_field, &field_offset, &grouped_and_sorted_fields, &gaps); |
| 8531 | ShuffleForward<1>(¤t_field, &field_offset, &grouped_and_sorted_fields, &gaps); |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 8532 | CHECK(grouped_and_sorted_fields.empty()) << "Missed " << grouped_and_sorted_fields.size() << |
| 8533 | " fields."; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 8534 | self->EndAssertNoThreadSuspension(old_no_suspend_cause); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 8535 | |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 8536 | // 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] | 8537 | if (!is_static && klass->DescriptorEquals("Ljava/lang/ref/Reference;")) { |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 8538 | // We know there are no non-reference fields in the Reference classes, and we know |
| 8539 | // that 'referent' is alphabetically last, so this is easy... |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 8540 | CHECK_EQ(num_reference_fields, num_fields) << klass->PrettyClass(); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 8541 | CHECK_STREQ(fields->At(num_fields - 1).GetName(), "referent") |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 8542 | << klass->PrettyClass(); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 8543 | --num_reference_fields; |
| 8544 | } |
| 8545 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 8546 | size_t size = field_offset.Uint32Value(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8547 | // Update klass |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 8548 | if (is_static) { |
| 8549 | klass->SetNumReferenceStaticFields(num_reference_fields); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 8550 | *class_size = size; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 8551 | } else { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8552 | klass->SetNumReferenceInstanceFields(num_reference_fields); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 8553 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 8554 | if (num_reference_fields == 0 || super_class == nullptr) { |
| 8555 | // 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] | 8556 | // super_class is null iff the class is java.lang.Object. |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 8557 | if (super_class == nullptr || |
| 8558 | (super_class->GetClassFlags() & mirror::kClassFlagNoReferenceFields) != 0) { |
| 8559 | klass->SetClassFlags(klass->GetClassFlags() | mirror::kClassFlagNoReferenceFields); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 8560 | } |
| 8561 | } |
| 8562 | if (kIsDebugBuild) { |
| 8563 | DCHECK_EQ(super_class == nullptr, klass->DescriptorEquals("Ljava/lang/Object;")); |
| 8564 | size_t total_reference_instance_fields = 0; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 8565 | ObjPtr<mirror::Class> cur_super = klass.Get(); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 8566 | while (cur_super != nullptr) { |
| 8567 | total_reference_instance_fields += cur_super->NumReferenceInstanceFieldsDuringLinking(); |
| 8568 | cur_super = cur_super->GetSuperClass(); |
| 8569 | } |
| 8570 | if (super_class == nullptr) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 8571 | CHECK_EQ(total_reference_instance_fields, 1u) << klass->PrettyDescriptor(); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 8572 | } else { |
| 8573 | // Check that there is at least num_reference_fields other than Object.class. |
| 8574 | CHECK_GE(total_reference_instance_fields, 1u + num_reference_fields) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 8575 | << klass->PrettyClass(); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 8576 | } |
| 8577 | } |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 8578 | if (!klass->IsVariableSize()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8579 | std::string temp; |
| 8580 | DCHECK_GE(size, sizeof(mirror::Object)) << klass->GetDescriptor(&temp); |
| 8581 | size_t previous_size = klass->GetObjectSize(); |
| 8582 | if (previous_size != 0) { |
| 8583 | // Make sure that we didn't originally have an incorrect size. |
| 8584 | CHECK_EQ(previous_size, size) << klass->GetDescriptor(&temp); |
Mathieu Chartier | 79b4f38 | 2013-10-23 15:21:37 -0700 | [diff] [blame] | 8585 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8586 | klass->SetObjectSize(size); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8587 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8588 | } |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 8589 | |
| 8590 | if (kIsDebugBuild) { |
| 8591 | // Make sure that the fields array is ordered by name but all reference |
| 8592 | // offsets are at the beginning as far as alignment allows. |
| 8593 | MemberOffset start_ref_offset = is_static |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8594 | ? klass->GetFirstReferenceStaticFieldOffsetDuringLinking(image_pointer_size_) |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 8595 | : klass->GetFirstReferenceInstanceFieldOffset(); |
| 8596 | MemberOffset end_ref_offset(start_ref_offset.Uint32Value() + |
| 8597 | num_reference_fields * |
| 8598 | sizeof(mirror::HeapReference<mirror::Object>)); |
| 8599 | MemberOffset current_ref_offset = start_ref_offset; |
| 8600 | for (size_t i = 0; i < num_fields; i++) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 8601 | ArtField* field = &fields->At(i); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8602 | VLOG(class_linker) << "LinkFields: " << (is_static ? "static" : "instance") |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 8603 | << " class=" << klass->PrettyClass() << " field=" << field->PrettyField() |
| 8604 | << " offset=" << field->GetOffsetDuringLinking(); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 8605 | if (i != 0) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 8606 | ArtField* const prev_field = &fields->At(i - 1); |
Vladimir Marko | 7a7c1db | 2014-11-17 15:13:34 +0000 | [diff] [blame] | 8607 | // NOTE: The field names can be the same. This is not possible in the Java language |
| 8608 | // 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] | 8609 | DCHECK_LE(strcmp(prev_field->GetName(), field->GetName()), 0); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 8610 | } |
| 8611 | Primitive::Type type = field->GetTypeAsPrimitiveType(); |
| 8612 | bool is_primitive = type != Primitive::kPrimNot; |
| 8613 | if (klass->DescriptorEquals("Ljava/lang/ref/Reference;") && |
| 8614 | strcmp("referent", field->GetName()) == 0) { |
| 8615 | is_primitive = true; // We lied above, so we have to expect a lie here. |
| 8616 | } |
| 8617 | MemberOffset offset = field->GetOffsetDuringLinking(); |
| 8618 | if (is_primitive) { |
| 8619 | if (offset.Uint32Value() < end_ref_offset.Uint32Value()) { |
| 8620 | // Shuffled before references. |
| 8621 | size_t type_size = Primitive::ComponentSize(type); |
| 8622 | CHECK_LT(type_size, sizeof(mirror::HeapReference<mirror::Object>)); |
| 8623 | CHECK_LT(offset.Uint32Value(), start_ref_offset.Uint32Value()); |
| 8624 | CHECK_LE(offset.Uint32Value() + type_size, start_ref_offset.Uint32Value()); |
| 8625 | CHECK(!IsAligned<sizeof(mirror::HeapReference<mirror::Object>)>(offset.Uint32Value())); |
| 8626 | } |
| 8627 | } else { |
| 8628 | CHECK_EQ(current_ref_offset.Uint32Value(), offset.Uint32Value()); |
| 8629 | current_ref_offset = MemberOffset(current_ref_offset.Uint32Value() + |
| 8630 | sizeof(mirror::HeapReference<mirror::Object>)); |
| 8631 | } |
| 8632 | } |
| 8633 | CHECK_EQ(current_ref_offset.Uint32Value(), end_ref_offset.Uint32Value()); |
| 8634 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 8635 | return true; |
| 8636 | } |
| 8637 | |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 8638 | // Set the bitmap of reference instance field offsets. |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 8639 | void ClassLinker::CreateReferenceInstanceOffsets(Handle<mirror::Class> klass) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8640 | uint32_t reference_offsets = 0; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 8641 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 8642 | // 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] | 8643 | if (super_class != nullptr) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8644 | reference_offsets = super_class->GetReferenceInstanceOffsets(); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 8645 | // Compute reference offsets unless our superclass overflowed. |
| 8646 | if (reference_offsets != mirror::Class::kClassWalkSuper) { |
| 8647 | size_t num_reference_fields = klass->NumReferenceInstanceFieldsDuringLinking(); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 8648 | if (num_reference_fields != 0u) { |
| 8649 | // All of the fields that contain object references are guaranteed be grouped in memory |
| 8650 | // starting at an appropriately aligned address after super class object data. |
| 8651 | uint32_t start_offset = RoundUp(super_class->GetObjectSize(), |
| 8652 | sizeof(mirror::HeapReference<mirror::Object>)); |
| 8653 | uint32_t start_bit = (start_offset - mirror::kObjectHeaderSize) / |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 8654 | sizeof(mirror::HeapReference<mirror::Object>); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 8655 | if (start_bit + num_reference_fields > 32) { |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 8656 | reference_offsets = mirror::Class::kClassWalkSuper; |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 8657 | } else { |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 8658 | reference_offsets |= (0xffffffffu << start_bit) & |
| 8659 | (0xffffffffu >> (32 - (start_bit + num_reference_fields))); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 8660 | } |
| 8661 | } |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 8662 | } |
| 8663 | } |
Mingyao Yang | faff0f0 | 2014-09-10 12:03:22 -0700 | [diff] [blame] | 8664 | klass->SetReferenceInstanceOffsets(reference_offsets); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 8665 | } |
| 8666 | |
Vladimir Marko | 18090d1 | 2018-06-01 16:53:12 +0100 | [diff] [blame] | 8667 | ObjPtr<mirror::String> ClassLinker::DoResolveString(dex::StringIndex string_idx, |
| 8668 | ObjPtr<mirror::DexCache> dex_cache) { |
| 8669 | StackHandleScope<1> hs(Thread::Current()); |
| 8670 | Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(dex_cache)); |
| 8671 | return DoResolveString(string_idx, h_dex_cache); |
| 8672 | } |
| 8673 | |
| 8674 | ObjPtr<mirror::String> ClassLinker::DoResolveString(dex::StringIndex string_idx, |
| 8675 | Handle<mirror::DexCache> dex_cache) { |
Vladimir Marko | a64b52d | 2017-12-08 16:27:49 +0000 | [diff] [blame] | 8676 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 8677 | uint32_t utf16_length; |
| 8678 | const char* utf8_data = dex_file.StringDataAndUtf16LengthByIdx(string_idx, &utf16_length); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 8679 | ObjPtr<mirror::String> string = intern_table_->InternStrong(utf16_length, utf8_data); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 8680 | if (string != nullptr) { |
| 8681 | dex_cache->SetResolvedString(string_idx, string); |
| 8682 | } |
Vladimir Marko | 28e012a | 2017-12-07 11:22:59 +0000 | [diff] [blame] | 8683 | return string; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 8684 | } |
| 8685 | |
Vladimir Marko | 18090d1 | 2018-06-01 16:53:12 +0100 | [diff] [blame] | 8686 | ObjPtr<mirror::String> ClassLinker::DoLookupString(dex::StringIndex string_idx, |
| 8687 | ObjPtr<mirror::DexCache> dex_cache) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8688 | DCHECK(dex_cache != nullptr); |
Vladimir Marko | a64b52d | 2017-12-08 16:27:49 +0000 | [diff] [blame] | 8689 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 8690 | uint32_t utf16_length; |
| 8691 | const char* utf8_data = dex_file.StringDataAndUtf16LengthByIdx(string_idx, &utf16_length); |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 8692 | ObjPtr<mirror::String> string = |
| 8693 | intern_table_->LookupStrong(Thread::Current(), utf16_length, utf8_data); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 8694 | if (string != nullptr) { |
| 8695 | dex_cache->SetResolvedString(string_idx, string); |
| 8696 | } |
Vladimir Marko | 28e012a | 2017-12-07 11:22:59 +0000 | [diff] [blame] | 8697 | return string; |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 8698 | } |
| 8699 | |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 8700 | ObjPtr<mirror::Class> ClassLinker::DoLookupResolvedType(dex::TypeIndex type_idx, |
Vladimir Marko | 09c5ca4 | 2018-05-31 15:15:31 +0100 | [diff] [blame] | 8701 | ObjPtr<mirror::Class> referrer) { |
| 8702 | return DoLookupResolvedType(type_idx, referrer->GetDexCache(), referrer->GetClassLoader()); |
| 8703 | } |
| 8704 | |
| 8705 | ObjPtr<mirror::Class> ClassLinker::DoLookupResolvedType(dex::TypeIndex type_idx, |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 8706 | ObjPtr<mirror::DexCache> dex_cache, |
| 8707 | ObjPtr<mirror::ClassLoader> class_loader) { |
| 8708 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
| 8709 | const char* descriptor = dex_file.StringByTypeIdx(type_idx); |
| 8710 | DCHECK_NE(*descriptor, '\0') << "descriptor is empty string"; |
| 8711 | ObjPtr<mirror::Class> type = nullptr; |
| 8712 | if (descriptor[1] == '\0') { |
| 8713 | // only the descriptors of primitive types should be 1 character long, also avoid class lookup |
| 8714 | // for primitive classes that aren't backed by dex files. |
Vladimir Marko | 9186b18 | 2018-11-06 14:55:54 +0000 | [diff] [blame] | 8715 | type = LookupPrimitiveClass(descriptor[0]); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 8716 | } else { |
| 8717 | Thread* const self = Thread::Current(); |
| 8718 | DCHECK(self != nullptr); |
| 8719 | const size_t hash = ComputeModifiedUtf8Hash(descriptor); |
| 8720 | // Find the class in the loaded classes table. |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 8721 | type = LookupClass(self, descriptor, hash, class_loader); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 8722 | } |
| 8723 | if (type != nullptr) { |
| 8724 | if (type->IsResolved()) { |
| 8725 | dex_cache->SetResolvedType(type_idx, type); |
Mathieu Chartier | b890130 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 8726 | } else { |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 8727 | type = nullptr; |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 8728 | } |
Mathieu Chartier | b890130 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 8729 | } |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 8730 | return type; |
Mathieu Chartier | b890130 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 8731 | } |
| 8732 | |
Andreas Gampe | b0625e0 | 2019-05-01 12:43:31 -0700 | [diff] [blame] | 8733 | template <typename RefType> |
| 8734 | ObjPtr<mirror::Class> ClassLinker::DoResolveType(dex::TypeIndex type_idx, RefType referrer) { |
Vladimir Marko | 09c5ca4 | 2018-05-31 15:15:31 +0100 | [diff] [blame] | 8735 | StackHandleScope<2> hs(Thread::Current()); |
| 8736 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache())); |
| 8737 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(referrer->GetClassLoader())); |
| 8738 | return DoResolveType(type_idx, dex_cache, class_loader); |
| 8739 | } |
| 8740 | |
Andreas Gampe | 4835d21 | 2018-11-21 14:55:10 -0800 | [diff] [blame] | 8741 | // Instantiate the above. |
| 8742 | template ObjPtr<mirror::Class> ClassLinker::DoResolveType(dex::TypeIndex type_idx, |
| 8743 | ArtField* referrer); |
| 8744 | template ObjPtr<mirror::Class> ClassLinker::DoResolveType(dex::TypeIndex type_idx, |
| 8745 | ArtMethod* referrer); |
| 8746 | template ObjPtr<mirror::Class> ClassLinker::DoResolveType(dex::TypeIndex type_idx, |
| 8747 | ObjPtr<mirror::Class> referrer); |
| 8748 | |
Vladimir Marko | 09c5ca4 | 2018-05-31 15:15:31 +0100 | [diff] [blame] | 8749 | ObjPtr<mirror::Class> ClassLinker::DoResolveType(dex::TypeIndex type_idx, |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 8750 | Handle<mirror::DexCache> dex_cache, |
| 8751 | Handle<mirror::ClassLoader> class_loader) { |
| 8752 | Thread* self = Thread::Current(); |
| 8753 | const char* descriptor = dex_cache->GetDexFile()->StringByTypeIdx(type_idx); |
| 8754 | ObjPtr<mirror::Class> resolved = FindClass(self, descriptor, class_loader); |
| 8755 | if (resolved != nullptr) { |
| 8756 | // TODO: we used to throw here if resolved's class loader was not the |
| 8757 | // boot class loader. This was to permit different classes with the |
| 8758 | // same name to be loaded simultaneously by different loaders |
| 8759 | dex_cache->SetResolvedType(type_idx, resolved); |
| 8760 | } else { |
| 8761 | CHECK(self->IsExceptionPending()) |
| 8762 | << "Expected pending exception for failed resolution of: " << descriptor; |
| 8763 | // Convert a ClassNotFoundException to a NoClassDefFoundError. |
| 8764 | StackHandleScope<1> hs(self); |
| 8765 | Handle<mirror::Throwable> cause(hs.NewHandle(self->GetException())); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 8766 | if (cause->InstanceOf(GetClassRoot(ClassRoot::kJavaLangClassNotFoundException, this))) { |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 8767 | DCHECK(resolved == nullptr); // No Handle needed to preserve resolved. |
| 8768 | self->ClearException(); |
| 8769 | ThrowNoClassDefFoundError("Failed resolution of: %s", descriptor); |
| 8770 | self->GetException()->SetCause(cause.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8771 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 8772 | } |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 8773 | DCHECK((resolved == nullptr) || resolved->IsResolved()) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 8774 | << resolved->PrettyDescriptor() << " " << resolved->GetStatus(); |
Vladimir Marko | 28e012a | 2017-12-07 11:22:59 +0000 | [diff] [blame] | 8775 | return resolved; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 8776 | } |
| 8777 | |
Nicolas Geoffray | ea179f4 | 2018-02-08 22:30:18 +0000 | [diff] [blame] | 8778 | ArtMethod* ClassLinker::FindResolvedMethod(ObjPtr<mirror::Class> klass, |
| 8779 | ObjPtr<mirror::DexCache> dex_cache, |
| 8780 | ObjPtr<mirror::ClassLoader> class_loader, |
| 8781 | uint32_t method_idx) { |
| 8782 | // Search for the method using dex_cache and method_idx. The Class::Find*Method() |
| 8783 | // functions can optimize the search if the dex_cache is the same as the DexCache |
| 8784 | // of the class, with fall-back to name and signature search otherwise. |
| 8785 | ArtMethod* resolved = nullptr; |
| 8786 | if (klass->IsInterface()) { |
| 8787 | resolved = klass->FindInterfaceMethod(dex_cache, method_idx, image_pointer_size_); |
| 8788 | } else { |
| 8789 | resolved = klass->FindClassMethod(dex_cache, method_idx, image_pointer_size_); |
| 8790 | } |
| 8791 | DCHECK(resolved == nullptr || resolved->GetDeclaringClassUnchecked() != nullptr); |
David Brazdil | 8ce3bfa | 2018-03-12 18:01:18 +0000 | [diff] [blame] | 8792 | if (resolved != nullptr && |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 8793 | hiddenapi::ShouldDenyAccessToMember(resolved, |
| 8794 | hiddenapi::AccessContext(class_loader, dex_cache), |
| 8795 | hiddenapi::AccessMethod::kLinking)) { |
David Brazdil | 8ce3bfa | 2018-03-12 18:01:18 +0000 | [diff] [blame] | 8796 | resolved = nullptr; |
| 8797 | } |
Nicolas Geoffray | ea179f4 | 2018-02-08 22:30:18 +0000 | [diff] [blame] | 8798 | if (resolved != nullptr) { |
| 8799 | // In case of jmvti, the dex file gets verified before being registered, so first |
| 8800 | // check if it's registered before checking class tables. |
| 8801 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Nicolas Geoffray | befa309 | 2018-02-22 14:50:01 +0000 | [diff] [blame] | 8802 | DCHECK(!IsDexFileRegistered(Thread::Current(), dex_file) || |
| 8803 | FindClassTable(Thread::Current(), dex_cache) == ClassTableForClassLoader(class_loader)) |
Nicolas Geoffray | ea179f4 | 2018-02-08 22:30:18 +0000 | [diff] [blame] | 8804 | << "DexFile referrer: " << dex_file.GetLocation() |
| 8805 | << " ClassLoader: " << DescribeLoaders(class_loader, ""); |
| 8806 | // Be a good citizen and update the dex cache to speed subsequent calls. |
| 8807 | dex_cache->SetResolvedMethod(method_idx, resolved, image_pointer_size_); |
Nicolas Geoffray | befa309 | 2018-02-22 14:50:01 +0000 | [diff] [blame] | 8808 | // Disable the following invariant check as the verifier breaks it. b/73760543 |
| 8809 | // const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx); |
| 8810 | // DCHECK(LookupResolvedType(method_id.class_idx_, dex_cache, class_loader) != nullptr) |
| 8811 | // << "Method: " << resolved->PrettyMethod() << ", " |
| 8812 | // << "Class: " << klass->PrettyClass() << " (" << klass->GetStatus() << "), " |
| 8813 | // << "DexFile referrer: " << dex_file.GetLocation(); |
Nicolas Geoffray | ea179f4 | 2018-02-08 22:30:18 +0000 | [diff] [blame] | 8814 | } |
| 8815 | return resolved; |
| 8816 | } |
| 8817 | |
David Brazdil | 4525e0b | 2018-04-05 16:57:32 +0100 | [diff] [blame] | 8818 | // Returns true if `method` is either null or hidden. |
| 8819 | // Does not print any warnings if it is hidden. |
| 8820 | static bool CheckNoSuchMethod(ArtMethod* method, |
| 8821 | ObjPtr<mirror::DexCache> dex_cache, |
| 8822 | ObjPtr<mirror::ClassLoader> class_loader) |
| 8823 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 8824 | return method == nullptr || |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 8825 | hiddenapi::ShouldDenyAccessToMember(method, |
| 8826 | hiddenapi::AccessContext(class_loader, dex_cache), |
| 8827 | hiddenapi::AccessMethod::kNone); // no warnings |
David Brazdil | 4525e0b | 2018-04-05 16:57:32 +0100 | [diff] [blame] | 8828 | } |
| 8829 | |
| 8830 | ArtMethod* ClassLinker::FindIncompatibleMethod(ObjPtr<mirror::Class> klass, |
| 8831 | ObjPtr<mirror::DexCache> dex_cache, |
| 8832 | ObjPtr<mirror::ClassLoader> class_loader, |
| 8833 | uint32_t method_idx) { |
| 8834 | if (klass->IsInterface()) { |
| 8835 | ArtMethod* method = klass->FindClassMethod(dex_cache, method_idx, image_pointer_size_); |
| 8836 | return CheckNoSuchMethod(method, dex_cache, class_loader) ? nullptr : method; |
| 8837 | } else { |
| 8838 | // If there was an interface method with the same signature, we would have |
| 8839 | // found it in the "copied" methods. Only DCHECK that the interface method |
| 8840 | // really does not exist. |
| 8841 | if (kIsDebugBuild) { |
| 8842 | ArtMethod* method = |
| 8843 | klass->FindInterfaceMethod(dex_cache, method_idx, image_pointer_size_); |
| 8844 | DCHECK(CheckNoSuchMethod(method, dex_cache, class_loader)); |
| 8845 | } |
| 8846 | return nullptr; |
| 8847 | } |
| 8848 | } |
| 8849 | |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 8850 | template <ClassLinker::ResolveMode kResolveMode> |
Vladimir Marko | 8901119 | 2017-12-11 13:45:05 +0000 | [diff] [blame] | 8851 | ArtMethod* ClassLinker::ResolveMethod(uint32_t method_idx, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8852 | Handle<mirror::DexCache> dex_cache, |
| 8853 | Handle<mirror::ClassLoader> class_loader, |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 8854 | ArtMethod* referrer, |
| 8855 | InvokeType type) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8856 | DCHECK(dex_cache != nullptr); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 8857 | DCHECK(referrer == nullptr || !referrer->IsProxyMethod()); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 8858 | // Check for hit in the dex cache. |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 8859 | PointerSize pointer_size = image_pointer_size_; |
| 8860 | ArtMethod* resolved = dex_cache->GetResolvedMethod(method_idx, pointer_size); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 8861 | Thread::PoisonObjectPointersIfDebug(); |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 8862 | DCHECK(resolved == nullptr || !resolved->IsRuntimeMethod()); |
| 8863 | bool valid_dex_cache_method = resolved != nullptr; |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 8864 | if (kResolveMode == ResolveMode::kNoChecks && valid_dex_cache_method) { |
| 8865 | // 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] | 8866 | DCHECK(resolved->GetDeclaringClassUnchecked() != nullptr) << resolved->GetDexMethodIndex(); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 8867 | return resolved; |
| 8868 | } |
Vladimir Marko | 8901119 | 2017-12-11 13:45:05 +0000 | [diff] [blame] | 8869 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 8870 | const dex::MethodId& method_id = dex_file.GetMethodId(method_idx); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 8871 | ObjPtr<mirror::Class> klass = nullptr; |
| 8872 | if (valid_dex_cache_method) { |
| 8873 | // We have a valid method from the DexCache but we need to perform ICCE and IAE checks. |
| 8874 | DCHECK(resolved->GetDeclaringClassUnchecked() != nullptr) << resolved->GetDexMethodIndex(); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 8875 | klass = LookupResolvedType(method_id.class_idx_, dex_cache.Get(), class_loader.Get()); |
Vladimir Marko | 6f1bd46 | 2017-12-06 17:45:03 +0000 | [diff] [blame] | 8876 | if (UNLIKELY(klass == nullptr)) { |
Nicolas Geoffray | befa309 | 2018-02-22 14:50:01 +0000 | [diff] [blame] | 8877 | // We normaly should not end up here. However the verifier currently doesn't guarantee |
| 8878 | // the invariant of having the klass in the class table. b/73760543 |
| 8879 | klass = ResolveType(method_id.class_idx_, dex_cache, class_loader); |
Vladimir Marko | 6f1bd46 | 2017-12-06 17:45:03 +0000 | [diff] [blame] | 8880 | } |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 8881 | } else { |
| 8882 | // The method was not in the DexCache, resolve the declaring class. |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 8883 | klass = ResolveType(method_id.class_idx_, dex_cache, class_loader); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 8884 | if (klass == nullptr) { |
| 8885 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 8886 | return nullptr; |
| 8887 | } |
| 8888 | } |
| 8889 | |
| 8890 | // Check if the invoke type matches the class type. |
| 8891 | if (kResolveMode == ResolveMode::kCheckICCEAndIAE && |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 8892 | CheckInvokeClassMismatch</* kThrow= */ true>( |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 8893 | dex_cache.Get(), type, [klass]() { return klass; })) { |
Elliott Hughes | cc5f9a9 | 2011-09-28 19:17:29 -0700 | [diff] [blame] | 8894 | DCHECK(Thread::Current()->IsExceptionPending()); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 8895 | return nullptr; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 8896 | } |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 8897 | |
| 8898 | if (!valid_dex_cache_method) { |
Nicolas Geoffray | ea179f4 | 2018-02-08 22:30:18 +0000 | [diff] [blame] | 8899 | resolved = FindResolvedMethod(klass, dex_cache.Get(), class_loader.Get(), method_idx); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 8900 | } |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 8901 | |
| 8902 | // Note: We can check for IllegalAccessError only if we have a referrer. |
| 8903 | if (kResolveMode == ResolveMode::kCheckICCEAndIAE && resolved != nullptr && referrer != nullptr) { |
| 8904 | ObjPtr<mirror::Class> methods_class = resolved->GetDeclaringClass(); |
| 8905 | ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass(); |
| 8906 | if (!referring_class->CheckResolvedMethodAccess(methods_class, |
| 8907 | resolved, |
| 8908 | dex_cache.Get(), |
| 8909 | method_idx, |
| 8910 | type)) { |
| 8911 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 8912 | return nullptr; |
| 8913 | } |
| 8914 | } |
| 8915 | |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 8916 | // If we found a method, check for incompatible class changes. |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 8917 | if (LIKELY(resolved != nullptr) && |
| 8918 | LIKELY(kResolveMode == ResolveMode::kNoChecks || |
| 8919 | !resolved->CheckIncompatibleClassChange(type))) { |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 8920 | return resolved; |
| 8921 | } else { |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 8922 | // If we had a method, or if we can find one with another lookup type, |
| 8923 | // it's an incompatible-class-change error. |
| 8924 | if (resolved == nullptr) { |
David Brazdil | 4525e0b | 2018-04-05 16:57:32 +0100 | [diff] [blame] | 8925 | resolved = FindIncompatibleMethod(klass, dex_cache.Get(), class_loader.Get(), method_idx); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 8926 | } |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 8927 | if (resolved != nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8928 | ThrowIncompatibleClassChangeError(type, resolved->GetInvokeType(), resolved, referrer); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 8929 | } else { |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 8930 | // 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] | 8931 | const char* name = dex_file.StringDataByIdx(method_id.name_idx_); |
| 8932 | const Signature signature = dex_file.GetMethodSignature(method_id); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 8933 | ThrowNoSuchMethodError(type, klass, name, signature); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 8934 | } |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 8935 | Thread::Current()->AssertPendingException(); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 8936 | return nullptr; |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 8937 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 8938 | } |
| 8939 | |
Vladimir Marko | 8901119 | 2017-12-11 13:45:05 +0000 | [diff] [blame] | 8940 | ArtMethod* ClassLinker::ResolveMethodWithoutInvokeType(uint32_t method_idx, |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 8941 | Handle<mirror::DexCache> dex_cache, |
| 8942 | Handle<mirror::ClassLoader> class_loader) { |
| 8943 | ArtMethod* resolved = dex_cache->GetResolvedMethod(method_idx, image_pointer_size_); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 8944 | Thread::PoisonObjectPointersIfDebug(); |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 8945 | if (resolved != nullptr) { |
| 8946 | DCHECK(!resolved->IsRuntimeMethod()); |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 8947 | DCHECK(resolved->GetDeclaringClassUnchecked() != nullptr) << resolved->GetDexMethodIndex(); |
| 8948 | return resolved; |
| 8949 | } |
| 8950 | // Fail, get the declaring class. |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 8951 | const dex::MethodId& method_id = dex_cache->GetDexFile()->GetMethodId(method_idx); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 8952 | 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] | 8953 | if (klass == nullptr) { |
| 8954 | Thread::Current()->AssertPendingException(); |
| 8955 | return nullptr; |
| 8956 | } |
| 8957 | if (klass->IsInterface()) { |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 8958 | resolved = klass->FindInterfaceMethod(dex_cache.Get(), method_idx, image_pointer_size_); |
| 8959 | } else { |
| 8960 | resolved = klass->FindClassMethod(dex_cache.Get(), method_idx, image_pointer_size_); |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 8961 | } |
David Brazdil | 8ce3bfa | 2018-03-12 18:01:18 +0000 | [diff] [blame] | 8962 | if (resolved != nullptr && |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 8963 | hiddenapi::ShouldDenyAccessToMember( |
| 8964 | resolved, |
| 8965 | hiddenapi::AccessContext(class_loader.Get(), dex_cache.Get()), |
| 8966 | hiddenapi::AccessMethod::kLinking)) { |
David Brazdil | 8ce3bfa | 2018-03-12 18:01:18 +0000 | [diff] [blame] | 8967 | resolved = nullptr; |
| 8968 | } |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 8969 | return resolved; |
| 8970 | } |
| 8971 | |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 8972 | ArtField* ClassLinker::LookupResolvedField(uint32_t field_idx, |
| 8973 | ObjPtr<mirror::DexCache> dex_cache, |
| 8974 | ObjPtr<mirror::ClassLoader> class_loader, |
| 8975 | bool is_static) { |
| 8976 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 8977 | const dex::FieldId& field_id = dex_file.GetFieldId(field_idx); |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 8978 | ObjPtr<mirror::Class> klass = dex_cache->GetResolvedType(field_id.class_idx_); |
| 8979 | if (klass == nullptr) { |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 8980 | klass = LookupResolvedType(field_id.class_idx_, dex_cache, class_loader); |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 8981 | } |
| 8982 | if (klass == nullptr) { |
| 8983 | // The class has not been resolved yet, so the field is also unresolved. |
| 8984 | return nullptr; |
| 8985 | } |
| 8986 | DCHECK(klass->IsResolved()); |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 8987 | |
David Brazdil | 1ab0fa8 | 2018-05-04 11:28:03 +0100 | [diff] [blame] | 8988 | return FindResolvedField(klass, dex_cache, class_loader, field_idx, is_static); |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 8989 | } |
| 8990 | |
Vladimir Marko | e11dd50 | 2017-12-08 14:09:45 +0000 | [diff] [blame] | 8991 | ArtField* ClassLinker::ResolveField(uint32_t field_idx, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8992 | Handle<mirror::DexCache> dex_cache, |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 8993 | Handle<mirror::ClassLoader> class_loader, |
| 8994 | bool is_static) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8995 | DCHECK(dex_cache != nullptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8996 | ArtField* resolved = dex_cache->GetResolvedField(field_idx, image_pointer_size_); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 8997 | Thread::PoisonObjectPointersIfDebug(); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 8998 | if (resolved != nullptr) { |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 8999 | return resolved; |
| 9000 | } |
Vladimir Marko | e11dd50 | 2017-12-08 14:09:45 +0000 | [diff] [blame] | 9001 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9002 | const dex::FieldId& field_id = dex_file.GetFieldId(field_idx); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 9003 | 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] | 9004 | if (klass == nullptr) { |
Ian Rogers | 9f1ab12 | 2011-12-12 08:52:43 -0800 | [diff] [blame] | 9005 | DCHECK(Thread::Current()->IsExceptionPending()); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 9006 | return nullptr; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 9007 | } |
| 9008 | |
David Brazdil | 1ab0fa8 | 2018-05-04 11:28:03 +0100 | [diff] [blame] | 9009 | 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] | 9010 | if (resolved == nullptr) { |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 9011 | const char* name = dex_file.GetFieldName(field_id); |
| 9012 | const char* type = dex_file.GetFieldTypeDescriptor(field_id); |
David Brazdil | 8ce3bfa | 2018-03-12 18:01:18 +0000 | [diff] [blame] | 9013 | ThrowNoSuchFieldError(is_static ? "static " : "instance ", klass, type, name); |
David Brazdil | 8ce3bfa | 2018-03-12 18:01:18 +0000 | [diff] [blame] | 9014 | } |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 9015 | return resolved; |
| 9016 | } |
| 9017 | |
Vladimir Marko | e11dd50 | 2017-12-08 14:09:45 +0000 | [diff] [blame] | 9018 | ArtField* ClassLinker::ResolveFieldJLS(uint32_t field_idx, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 9019 | Handle<mirror::DexCache> dex_cache, |
| 9020 | Handle<mirror::ClassLoader> class_loader) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 9021 | DCHECK(dex_cache != nullptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 9022 | ArtField* resolved = dex_cache->GetResolvedField(field_idx, image_pointer_size_); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 9023 | Thread::PoisonObjectPointersIfDebug(); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 9024 | if (resolved != nullptr) { |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 9025 | return resolved; |
| 9026 | } |
Vladimir Marko | e11dd50 | 2017-12-08 14:09:45 +0000 | [diff] [blame] | 9027 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9028 | const dex::FieldId& field_id = dex_file.GetFieldId(field_idx); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 9029 | 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] | 9030 | if (klass == nullptr) { |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 9031 | DCHECK(Thread::Current()->IsExceptionPending()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 9032 | return nullptr; |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 9033 | } |
| 9034 | |
David Brazdil | 1ab0fa8 | 2018-05-04 11:28:03 +0100 | [diff] [blame] | 9035 | resolved = FindResolvedFieldJLS(klass, dex_cache.Get(), class_loader.Get(), field_idx); |
| 9036 | if (resolved == nullptr) { |
| 9037 | const char* name = dex_file.GetFieldName(field_id); |
| 9038 | const char* type = dex_file.GetFieldTypeDescriptor(field_id); |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 9039 | ThrowNoSuchFieldError("", klass, type, name); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 9040 | } |
| 9041 | return resolved; |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 9042 | } |
| 9043 | |
David Brazdil | 1ab0fa8 | 2018-05-04 11:28:03 +0100 | [diff] [blame] | 9044 | ArtField* ClassLinker::FindResolvedField(ObjPtr<mirror::Class> klass, |
| 9045 | ObjPtr<mirror::DexCache> dex_cache, |
| 9046 | ObjPtr<mirror::ClassLoader> class_loader, |
| 9047 | uint32_t field_idx, |
| 9048 | bool is_static) { |
| 9049 | ArtField* resolved = nullptr; |
| 9050 | Thread* self = is_static ? Thread::Current() : nullptr; |
| 9051 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
| 9052 | |
| 9053 | resolved = is_static ? mirror::Class::FindStaticField(self, klass, dex_cache, field_idx) |
| 9054 | : klass->FindInstanceField(dex_cache, field_idx); |
| 9055 | |
| 9056 | if (resolved == nullptr) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9057 | const dex::FieldId& field_id = dex_file.GetFieldId(field_idx); |
David Brazdil | 1ab0fa8 | 2018-05-04 11:28:03 +0100 | [diff] [blame] | 9058 | const char* name = dex_file.GetFieldName(field_id); |
| 9059 | const char* type = dex_file.GetFieldTypeDescriptor(field_id); |
| 9060 | resolved = is_static ? mirror::Class::FindStaticField(self, klass, name, type) |
| 9061 | : klass->FindInstanceField(name, type); |
| 9062 | } |
| 9063 | |
| 9064 | if (resolved != nullptr && |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 9065 | hiddenapi::ShouldDenyAccessToMember(resolved, |
| 9066 | hiddenapi::AccessContext(class_loader, dex_cache), |
| 9067 | hiddenapi::AccessMethod::kLinking)) { |
David Brazdil | 1ab0fa8 | 2018-05-04 11:28:03 +0100 | [diff] [blame] | 9068 | resolved = nullptr; |
| 9069 | } |
| 9070 | |
| 9071 | if (resolved != nullptr) { |
| 9072 | dex_cache->SetResolvedField(field_idx, resolved, image_pointer_size_); |
| 9073 | } |
| 9074 | |
| 9075 | return resolved; |
| 9076 | } |
| 9077 | |
| 9078 | ArtField* ClassLinker::FindResolvedFieldJLS(ObjPtr<mirror::Class> klass, |
| 9079 | ObjPtr<mirror::DexCache> dex_cache, |
| 9080 | ObjPtr<mirror::ClassLoader> class_loader, |
| 9081 | uint32_t field_idx) { |
| 9082 | ArtField* resolved = nullptr; |
| 9083 | Thread* self = Thread::Current(); |
| 9084 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9085 | const dex::FieldId& field_id = dex_file.GetFieldId(field_idx); |
David Brazdil | 1ab0fa8 | 2018-05-04 11:28:03 +0100 | [diff] [blame] | 9086 | |
| 9087 | const char* name = dex_file.GetFieldName(field_id); |
| 9088 | const char* type = dex_file.GetFieldTypeDescriptor(field_id); |
| 9089 | resolved = mirror::Class::FindField(self, klass, name, type); |
| 9090 | |
| 9091 | if (resolved != nullptr && |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 9092 | hiddenapi::ShouldDenyAccessToMember(resolved, |
| 9093 | hiddenapi::AccessContext(class_loader, dex_cache), |
| 9094 | hiddenapi::AccessMethod::kLinking)) { |
David Brazdil | 1ab0fa8 | 2018-05-04 11:28:03 +0100 | [diff] [blame] | 9095 | resolved = nullptr; |
| 9096 | } |
| 9097 | |
| 9098 | if (resolved != nullptr) { |
| 9099 | dex_cache->SetResolvedField(field_idx, resolved, image_pointer_size_); |
| 9100 | } |
| 9101 | |
| 9102 | return resolved; |
| 9103 | } |
| 9104 | |
Vladimir Marko | af94020 | 2017-12-08 15:01:18 +0000 | [diff] [blame] | 9105 | ObjPtr<mirror::MethodType> ClassLinker::ResolveMethodType( |
| 9106 | Thread* self, |
Orion Hodson | 06d10a7 | 2018-05-14 08:53:38 +0100 | [diff] [blame] | 9107 | dex::ProtoIndex proto_idx, |
Vladimir Marko | af94020 | 2017-12-08 15:01:18 +0000 | [diff] [blame] | 9108 | Handle<mirror::DexCache> dex_cache, |
| 9109 | Handle<mirror::ClassLoader> class_loader) { |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9110 | DCHECK(Runtime::Current()->IsMethodHandlesEnabled()); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 9111 | DCHECK(dex_cache != nullptr); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9112 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 9113 | ObjPtr<mirror::MethodType> resolved = dex_cache->GetResolvedMethodType(proto_idx); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9114 | if (resolved != nullptr) { |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 9115 | return resolved; |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9116 | } |
| 9117 | |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9118 | StackHandleScope<4> hs(self); |
| 9119 | |
| 9120 | // First resolve the return type. |
Vladimir Marko | af94020 | 2017-12-08 15:01:18 +0000 | [diff] [blame] | 9121 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9122 | const dex::ProtoId& proto_id = dex_file.GetProtoId(proto_idx); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9123 | Handle<mirror::Class> return_type(hs.NewHandle( |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 9124 | ResolveType(proto_id.return_type_idx_, dex_cache, class_loader))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 9125 | if (return_type == nullptr) { |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9126 | DCHECK(self->IsExceptionPending()); |
| 9127 | return nullptr; |
| 9128 | } |
| 9129 | |
| 9130 | // Then resolve the argument types. |
| 9131 | // |
| 9132 | // TODO: Is there a better way to figure out the number of method arguments |
| 9133 | // other than by looking at the shorty ? |
| 9134 | const size_t num_method_args = strlen(dex_file.StringDataByIdx(proto_id.shorty_idx_)) - 1; |
| 9135 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 9136 | ObjPtr<mirror::Class> array_of_class = GetClassRoot<mirror::ObjectArray<mirror::Class>>(this); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9137 | Handle<mirror::ObjectArray<mirror::Class>> method_params(hs.NewHandle( |
| 9138 | mirror::ObjectArray<mirror::Class>::Alloc(self, array_of_class, num_method_args))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 9139 | if (method_params == nullptr) { |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9140 | DCHECK(self->IsExceptionPending()); |
| 9141 | return nullptr; |
| 9142 | } |
| 9143 | |
| 9144 | DexFileParameterIterator it(dex_file, proto_id); |
| 9145 | int32_t i = 0; |
| 9146 | MutableHandle<mirror::Class> param_class = hs.NewHandle<mirror::Class>(nullptr); |
| 9147 | for (; it.HasNext(); it.Next()) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 9148 | const dex::TypeIndex type_idx = it.GetTypeIdx(); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 9149 | param_class.Assign(ResolveType(type_idx, dex_cache, class_loader)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 9150 | if (param_class == nullptr) { |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9151 | DCHECK(self->IsExceptionPending()); |
| 9152 | return nullptr; |
| 9153 | } |
| 9154 | |
| 9155 | method_params->Set(i++, param_class.Get()); |
| 9156 | } |
| 9157 | |
| 9158 | DCHECK(!it.HasNext()); |
| 9159 | |
| 9160 | Handle<mirror::MethodType> type = hs.NewHandle( |
| 9161 | mirror::MethodType::Create(self, return_type, method_params)); |
| 9162 | dex_cache->SetResolvedMethodType(proto_idx, type.Get()); |
| 9163 | |
| 9164 | return type.Get(); |
| 9165 | } |
| 9166 | |
Vladimir Marko | af94020 | 2017-12-08 15:01:18 +0000 | [diff] [blame] | 9167 | ObjPtr<mirror::MethodType> ClassLinker::ResolveMethodType(Thread* self, |
Orion Hodson | 06d10a7 | 2018-05-14 08:53:38 +0100 | [diff] [blame] | 9168 | dex::ProtoIndex proto_idx, |
Vladimir Marko | af94020 | 2017-12-08 15:01:18 +0000 | [diff] [blame] | 9169 | ArtMethod* referrer) { |
Orion Hodson | 2e59994 | 2017-09-22 16:17:41 +0100 | [diff] [blame] | 9170 | StackHandleScope<2> hs(self); |
Orion Hodson | 2e59994 | 2017-09-22 16:17:41 +0100 | [diff] [blame] | 9171 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache())); |
| 9172 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(referrer->GetClassLoader())); |
Vladimir Marko | af94020 | 2017-12-08 15:01:18 +0000 | [diff] [blame] | 9173 | return ResolveMethodType(self, proto_idx, dex_cache, class_loader); |
Orion Hodson | 2e59994 | 2017-09-22 16:17:41 +0100 | [diff] [blame] | 9174 | } |
| 9175 | |
Vladimir Marko | 5aead70 | 2019-03-27 11:00:36 +0000 | [diff] [blame] | 9176 | ObjPtr<mirror::MethodHandle> ClassLinker::ResolveMethodHandleForField( |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9177 | Thread* self, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9178 | const dex::MethodHandleItem& method_handle, |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9179 | ArtMethod* referrer) { |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9180 | DexFile::MethodHandleType handle_type = |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9181 | static_cast<DexFile::MethodHandleType>(method_handle.method_handle_type_); |
| 9182 | mirror::MethodHandle::Kind kind; |
Orion Hodson | fd7b2c2 | 2018-03-15 15:38:38 +0000 | [diff] [blame] | 9183 | bool is_put; |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9184 | bool is_static; |
| 9185 | int32_t num_params; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9186 | switch (handle_type) { |
| 9187 | case DexFile::MethodHandleType::kStaticPut: { |
Orion Hodson | 82b351f | 2017-07-05 14:34:25 +0100 | [diff] [blame] | 9188 | kind = mirror::MethodHandle::Kind::kStaticPut; |
Orion Hodson | fd7b2c2 | 2018-03-15 15:38:38 +0000 | [diff] [blame] | 9189 | is_put = true; |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9190 | is_static = true; |
| 9191 | num_params = 1; |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 9192 | break; |
| 9193 | } |
| 9194 | case DexFile::MethodHandleType::kStaticGet: { |
Orion Hodson | 82b351f | 2017-07-05 14:34:25 +0100 | [diff] [blame] | 9195 | kind = mirror::MethodHandle::Kind::kStaticGet; |
Orion Hodson | fd7b2c2 | 2018-03-15 15:38:38 +0000 | [diff] [blame] | 9196 | is_put = false; |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9197 | is_static = true; |
| 9198 | num_params = 0; |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 9199 | break; |
| 9200 | } |
| 9201 | case DexFile::MethodHandleType::kInstancePut: { |
Orion Hodson | 82b351f | 2017-07-05 14:34:25 +0100 | [diff] [blame] | 9202 | kind = mirror::MethodHandle::Kind::kInstancePut; |
Orion Hodson | fd7b2c2 | 2018-03-15 15:38:38 +0000 | [diff] [blame] | 9203 | is_put = true; |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9204 | is_static = false; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9205 | num_params = 2; |
| 9206 | break; |
| 9207 | } |
| 9208 | case DexFile::MethodHandleType::kInstanceGet: { |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9209 | kind = mirror::MethodHandle::Kind::kInstanceGet; |
Orion Hodson | fd7b2c2 | 2018-03-15 15:38:38 +0000 | [diff] [blame] | 9210 | is_put = false; |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9211 | is_static = false; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9212 | num_params = 1; |
| 9213 | break; |
| 9214 | } |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9215 | case DexFile::MethodHandleType::kInvokeStatic: |
Orion Hodson | 82b351f | 2017-07-05 14:34:25 +0100 | [diff] [blame] | 9216 | case DexFile::MethodHandleType::kInvokeInstance: |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9217 | case DexFile::MethodHandleType::kInvokeConstructor: |
Orion Hodson | 82b351f | 2017-07-05 14:34:25 +0100 | [diff] [blame] | 9218 | case DexFile::MethodHandleType::kInvokeDirect: |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9219 | case DexFile::MethodHandleType::kInvokeInterface: |
| 9220 | UNREACHABLE(); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9221 | } |
| 9222 | |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9223 | ArtField* target_field = |
| 9224 | ResolveField(method_handle.field_or_method_idx_, referrer, is_static); |
| 9225 | if (LIKELY(target_field != nullptr)) { |
| 9226 | ObjPtr<mirror::Class> target_class = target_field->GetDeclaringClass(); |
| 9227 | ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass(); |
| 9228 | if (UNLIKELY(!referring_class->CanAccessMember(target_class, target_field->GetAccessFlags()))) { |
| 9229 | ThrowIllegalAccessErrorField(referring_class, target_field); |
| 9230 | return nullptr; |
| 9231 | } |
Orion Hodson | fd7b2c2 | 2018-03-15 15:38:38 +0000 | [diff] [blame] | 9232 | if (UNLIKELY(is_put && target_field->IsFinal())) { |
| 9233 | ThrowIllegalAccessErrorField(referring_class, target_field); |
| 9234 | return nullptr; |
| 9235 | } |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9236 | } else { |
| 9237 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 9238 | return nullptr; |
| 9239 | } |
| 9240 | |
| 9241 | StackHandleScope<4> hs(self); |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 9242 | ObjPtr<mirror::Class> array_of_class = GetClassRoot<mirror::ObjectArray<mirror::Class>>(this); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9243 | Handle<mirror::ObjectArray<mirror::Class>> method_params(hs.NewHandle( |
| 9244 | mirror::ObjectArray<mirror::Class>::Alloc(self, array_of_class, num_params))); |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 9245 | if (UNLIKELY(method_params == nullptr)) { |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9246 | DCHECK(self->IsExceptionPending()); |
| 9247 | return nullptr; |
| 9248 | } |
| 9249 | |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9250 | Handle<mirror::Class> constructor_class; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9251 | Handle<mirror::Class> return_type; |
| 9252 | switch (handle_type) { |
| 9253 | case DexFile::MethodHandleType::kStaticPut: { |
Vladimir Marko | 4098a7a | 2017-11-06 16:00:51 +0000 | [diff] [blame] | 9254 | method_params->Set(0, target_field->ResolveType()); |
Vladimir Marko | 9186b18 | 2018-11-06 14:55:54 +0000 | [diff] [blame] | 9255 | return_type = hs.NewHandle(GetClassRoot(ClassRoot::kPrimitiveVoid, this)); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9256 | break; |
| 9257 | } |
| 9258 | case DexFile::MethodHandleType::kStaticGet: { |
Vladimir Marko | 4098a7a | 2017-11-06 16:00:51 +0000 | [diff] [blame] | 9259 | return_type = hs.NewHandle(target_field->ResolveType()); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9260 | break; |
| 9261 | } |
| 9262 | case DexFile::MethodHandleType::kInstancePut: { |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 9263 | method_params->Set(0, target_field->GetDeclaringClass()); |
Vladimir Marko | 4098a7a | 2017-11-06 16:00:51 +0000 | [diff] [blame] | 9264 | method_params->Set(1, target_field->ResolveType()); |
Vladimir Marko | 9186b18 | 2018-11-06 14:55:54 +0000 | [diff] [blame] | 9265 | return_type = hs.NewHandle(GetClassRoot(ClassRoot::kPrimitiveVoid, this)); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9266 | break; |
| 9267 | } |
| 9268 | case DexFile::MethodHandleType::kInstanceGet: { |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 9269 | method_params->Set(0, target_field->GetDeclaringClass()); |
Vladimir Marko | 4098a7a | 2017-11-06 16:00:51 +0000 | [diff] [blame] | 9270 | return_type = hs.NewHandle(target_field->ResolveType()); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9271 | break; |
| 9272 | } |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9273 | case DexFile::MethodHandleType::kInvokeStatic: |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 9274 | case DexFile::MethodHandleType::kInvokeInstance: |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9275 | case DexFile::MethodHandleType::kInvokeConstructor: |
| 9276 | case DexFile::MethodHandleType::kInvokeDirect: |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 9277 | case DexFile::MethodHandleType::kInvokeInterface: |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9278 | UNREACHABLE(); |
| 9279 | } |
| 9280 | |
| 9281 | for (int32_t i = 0; i < num_params; ++i) { |
| 9282 | if (UNLIKELY(method_params->Get(i) == nullptr)) { |
| 9283 | DCHECK(self->IsExceptionPending()); |
| 9284 | return nullptr; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9285 | } |
| 9286 | } |
| 9287 | |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9288 | if (UNLIKELY(return_type.IsNull())) { |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9289 | DCHECK(self->IsExceptionPending()); |
| 9290 | return nullptr; |
| 9291 | } |
| 9292 | |
| 9293 | Handle<mirror::MethodType> |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9294 | method_type(hs.NewHandle(mirror::MethodType::Create(self, return_type, method_params))); |
| 9295 | if (UNLIKELY(method_type.IsNull())) { |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9296 | DCHECK(self->IsExceptionPending()); |
| 9297 | return nullptr; |
| 9298 | } |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 9299 | |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9300 | uintptr_t target = reinterpret_cast<uintptr_t>(target_field); |
| 9301 | return mirror::MethodHandleImpl::Create(self, target, kind, method_type); |
| 9302 | } |
| 9303 | |
Vladimir Marko | 5aead70 | 2019-03-27 11:00:36 +0000 | [diff] [blame] | 9304 | ObjPtr<mirror::MethodHandle> ClassLinker::ResolveMethodHandleForMethod( |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9305 | Thread* self, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9306 | const dex::MethodHandleItem& method_handle, |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9307 | ArtMethod* referrer) { |
| 9308 | DexFile::MethodHandleType handle_type = |
| 9309 | static_cast<DexFile::MethodHandleType>(method_handle.method_handle_type_); |
| 9310 | mirror::MethodHandle::Kind kind; |
| 9311 | uint32_t receiver_count = 0; |
| 9312 | ArtMethod* target_method = nullptr; |
| 9313 | switch (handle_type) { |
| 9314 | case DexFile::MethodHandleType::kStaticPut: |
| 9315 | case DexFile::MethodHandleType::kStaticGet: |
| 9316 | case DexFile::MethodHandleType::kInstancePut: |
| 9317 | case DexFile::MethodHandleType::kInstanceGet: |
| 9318 | UNREACHABLE(); |
| 9319 | case DexFile::MethodHandleType::kInvokeStatic: { |
| 9320 | kind = mirror::MethodHandle::Kind::kInvokeStatic; |
| 9321 | receiver_count = 0; |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9322 | target_method = ResolveMethod<ResolveMode::kNoChecks>(self, |
| 9323 | method_handle.field_or_method_idx_, |
| 9324 | referrer, |
| 9325 | InvokeType::kStatic); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9326 | break; |
| 9327 | } |
| 9328 | case DexFile::MethodHandleType::kInvokeInstance: { |
| 9329 | kind = mirror::MethodHandle::Kind::kInvokeVirtual; |
| 9330 | receiver_count = 1; |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9331 | target_method = ResolveMethod<ResolveMode::kNoChecks>(self, |
| 9332 | method_handle.field_or_method_idx_, |
| 9333 | referrer, |
| 9334 | InvokeType::kVirtual); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9335 | break; |
| 9336 | } |
| 9337 | case DexFile::MethodHandleType::kInvokeConstructor: { |
| 9338 | // Constructors are currently implemented as a transform. They |
| 9339 | // are special cased later in this method. |
| 9340 | kind = mirror::MethodHandle::Kind::kInvokeTransform; |
| 9341 | receiver_count = 0; |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9342 | target_method = ResolveMethod<ResolveMode::kNoChecks>(self, |
| 9343 | method_handle.field_or_method_idx_, |
| 9344 | referrer, |
| 9345 | InvokeType::kDirect); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9346 | break; |
| 9347 | } |
| 9348 | case DexFile::MethodHandleType::kInvokeDirect: { |
| 9349 | kind = mirror::MethodHandle::Kind::kInvokeDirect; |
| 9350 | receiver_count = 1; |
| 9351 | StackHandleScope<2> hs(self); |
| 9352 | // A constant method handle with type kInvokeDirect can refer to |
| 9353 | // a method that is private or to a method in a super class. To |
| 9354 | // disambiguate the two options, we resolve the method ignoring |
| 9355 | // the invocation type to determine if the method is private. We |
| 9356 | // then resolve again specifying the intended invocation type to |
| 9357 | // force the appropriate checks. |
Vladimir Marko | 8901119 | 2017-12-11 13:45:05 +0000 | [diff] [blame] | 9358 | target_method = ResolveMethodWithoutInvokeType(method_handle.field_or_method_idx_, |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9359 | hs.NewHandle(referrer->GetDexCache()), |
| 9360 | hs.NewHandle(referrer->GetClassLoader())); |
| 9361 | if (UNLIKELY(target_method == nullptr)) { |
| 9362 | break; |
| 9363 | } |
| 9364 | |
| 9365 | if (target_method->IsPrivate()) { |
| 9366 | kind = mirror::MethodHandle::Kind::kInvokeDirect; |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9367 | target_method = ResolveMethod<ResolveMode::kNoChecks>(self, |
| 9368 | method_handle.field_or_method_idx_, |
| 9369 | referrer, |
| 9370 | InvokeType::kDirect); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9371 | } else { |
| 9372 | kind = mirror::MethodHandle::Kind::kInvokeSuper; |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9373 | target_method = ResolveMethod<ResolveMode::kNoChecks>(self, |
| 9374 | method_handle.field_or_method_idx_, |
| 9375 | referrer, |
| 9376 | InvokeType::kSuper); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9377 | if (UNLIKELY(target_method == nullptr)) { |
| 9378 | break; |
| 9379 | } |
| 9380 | // Find the method specified in the parent in referring class |
| 9381 | // so invoke-super invokes the method in the parent of the |
| 9382 | // referrer. |
| 9383 | target_method = |
| 9384 | referrer->GetDeclaringClass()->FindVirtualMethodForVirtual(target_method, |
| 9385 | kRuntimePointerSize); |
| 9386 | } |
| 9387 | break; |
| 9388 | } |
| 9389 | case DexFile::MethodHandleType::kInvokeInterface: { |
| 9390 | kind = mirror::MethodHandle::Kind::kInvokeInterface; |
| 9391 | receiver_count = 1; |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9392 | target_method = ResolveMethod<ResolveMode::kNoChecks>(self, |
| 9393 | method_handle.field_or_method_idx_, |
| 9394 | referrer, |
| 9395 | InvokeType::kInterface); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9396 | break; |
| 9397 | } |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 9398 | } |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9399 | |
| 9400 | if (UNLIKELY(target_method == nullptr)) { |
| 9401 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 9402 | return nullptr; |
| 9403 | } |
| 9404 | |
| 9405 | ObjPtr<mirror::Class> target_class = target_method->GetDeclaringClass(); |
| 9406 | ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass(); |
| 9407 | uint32_t access_flags = target_method->GetAccessFlags(); |
| 9408 | if (UNLIKELY(!referring_class->CanAccessMember(target_class, access_flags))) { |
| 9409 | ThrowIllegalAccessErrorMethod(referring_class, target_method); |
| 9410 | return nullptr; |
| 9411 | } |
| 9412 | |
| 9413 | // Calculate the number of parameters from the method shorty. We add the |
| 9414 | // receiver count (0 or 1) and deduct one for the return value. |
| 9415 | uint32_t shorty_length; |
| 9416 | target_method->GetShorty(&shorty_length); |
| 9417 | int32_t num_params = static_cast<int32_t>(shorty_length + receiver_count - 1); |
| 9418 | |
Orion Hodson | ecd5856 | 2018-09-24 11:27:33 +0100 | [diff] [blame] | 9419 | StackHandleScope<5> hs(self); |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 9420 | ObjPtr<mirror::Class> array_of_class = GetClassRoot<mirror::ObjectArray<mirror::Class>>(this); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9421 | Handle<mirror::ObjectArray<mirror::Class>> method_params(hs.NewHandle( |
| 9422 | mirror::ObjectArray<mirror::Class>::Alloc(self, array_of_class, num_params))); |
| 9423 | if (method_params.Get() == nullptr) { |
| 9424 | DCHECK(self->IsExceptionPending()); |
| 9425 | return nullptr; |
| 9426 | } |
| 9427 | |
Orion Hodson | ecd5856 | 2018-09-24 11:27:33 +0100 | [diff] [blame] | 9428 | const DexFile* dex_file = referrer->GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9429 | const dex::MethodId& method_id = dex_file->GetMethodId(method_handle.field_or_method_idx_); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9430 | int32_t index = 0; |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9431 | if (receiver_count != 0) { |
Orion Hodson | ecd5856 | 2018-09-24 11:27:33 +0100 | [diff] [blame] | 9432 | // Insert receiver. Use the class identified in the method handle rather than the declaring |
| 9433 | // class of the resolved method which may be super class or default interface method |
| 9434 | // (b/115964401). |
| 9435 | ObjPtr<mirror::Class> receiver_class = LookupResolvedType(method_id.class_idx_, referrer); |
| 9436 | // receiver_class should have been resolved when resolving the target method. |
| 9437 | DCHECK(receiver_class != nullptr); |
| 9438 | method_params->Set(index++, receiver_class); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9439 | } |
Orion Hodson | ecd5856 | 2018-09-24 11:27:33 +0100 | [diff] [blame] | 9440 | |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9441 | const dex::ProtoId& proto_id = dex_file->GetProtoId(method_id.proto_idx_); |
Orion Hodson | ecd5856 | 2018-09-24 11:27:33 +0100 | [diff] [blame] | 9442 | DexFileParameterIterator it(*dex_file, proto_id); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9443 | while (it.HasNext()) { |
Orion Hodson | da1cdd0 | 2018-01-31 18:08:28 +0000 | [diff] [blame] | 9444 | DCHECK_LT(index, num_params); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9445 | const dex::TypeIndex type_idx = it.GetTypeIdx(); |
Orion Hodson | ecd5856 | 2018-09-24 11:27:33 +0100 | [diff] [blame] | 9446 | ObjPtr<mirror::Class> klass = ResolveType(type_idx, referrer); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9447 | if (nullptr == klass) { |
| 9448 | DCHECK(self->IsExceptionPending()); |
| 9449 | return nullptr; |
| 9450 | } |
| 9451 | method_params->Set(index++, klass); |
| 9452 | it.Next(); |
| 9453 | } |
| 9454 | |
Orion Hodson | ecd5856 | 2018-09-24 11:27:33 +0100 | [diff] [blame] | 9455 | Handle<mirror::Class> return_type = |
| 9456 | hs.NewHandle(ResolveType(proto_id.return_type_idx_, referrer)); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9457 | if (UNLIKELY(return_type.IsNull())) { |
| 9458 | DCHECK(self->IsExceptionPending()); |
| 9459 | return nullptr; |
| 9460 | } |
| 9461 | |
| 9462 | Handle<mirror::MethodType> |
| 9463 | method_type(hs.NewHandle(mirror::MethodType::Create(self, return_type, method_params))); |
| 9464 | if (UNLIKELY(method_type.IsNull())) { |
| 9465 | DCHECK(self->IsExceptionPending()); |
| 9466 | return nullptr; |
| 9467 | } |
| 9468 | |
| 9469 | if (UNLIKELY(handle_type == DexFile::MethodHandleType::kInvokeConstructor)) { |
| 9470 | Handle<mirror::Class> constructor_class = hs.NewHandle(target_method->GetDeclaringClass()); |
| 9471 | Handle<mirror::MethodHandlesLookup> lookup = |
| 9472 | hs.NewHandle(mirror::MethodHandlesLookup::GetDefault(self)); |
| 9473 | return lookup->FindConstructor(self, constructor_class, method_type); |
| 9474 | } |
| 9475 | |
| 9476 | uintptr_t target = reinterpret_cast<uintptr_t>(target_method); |
| 9477 | return mirror::MethodHandleImpl::Create(self, target, kind, method_type); |
| 9478 | } |
| 9479 | |
Vladimir Marko | af94020 | 2017-12-08 15:01:18 +0000 | [diff] [blame] | 9480 | ObjPtr<mirror::MethodHandle> ClassLinker::ResolveMethodHandle(Thread* self, |
| 9481 | uint32_t method_handle_idx, |
| 9482 | ArtMethod* referrer) |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9483 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9484 | const DexFile* const dex_file = referrer->GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9485 | const dex::MethodHandleItem& method_handle = dex_file->GetMethodHandle(method_handle_idx); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9486 | switch (static_cast<DexFile::MethodHandleType>(method_handle.method_handle_type_)) { |
| 9487 | case DexFile::MethodHandleType::kStaticPut: |
| 9488 | case DexFile::MethodHandleType::kStaticGet: |
| 9489 | case DexFile::MethodHandleType::kInstancePut: |
| 9490 | case DexFile::MethodHandleType::kInstanceGet: |
| 9491 | return ResolveMethodHandleForField(self, method_handle, referrer); |
| 9492 | case DexFile::MethodHandleType::kInvokeStatic: |
| 9493 | case DexFile::MethodHandleType::kInvokeInstance: |
| 9494 | case DexFile::MethodHandleType::kInvokeConstructor: |
| 9495 | case DexFile::MethodHandleType::kInvokeDirect: |
| 9496 | case DexFile::MethodHandleType::kInvokeInterface: |
Orion Hodson | da1cdd0 | 2018-01-31 18:08:28 +0000 | [diff] [blame] | 9497 | return ResolveMethodHandleForMethod(self, method_handle, referrer); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9498 | } |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9499 | } |
| 9500 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 9501 | bool ClassLinker::IsQuickResolutionStub(const void* entry_point) const { |
| 9502 | return (entry_point == GetQuickResolutionStub()) || |
| 9503 | (quick_resolution_trampoline_ == entry_point); |
| 9504 | } |
| 9505 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 9506 | bool ClassLinker::IsQuickToInterpreterBridge(const void* entry_point) const { |
| 9507 | return (entry_point == GetQuickToInterpreterBridge()) || |
| 9508 | (quick_to_interpreter_bridge_trampoline_ == entry_point); |
| 9509 | } |
| 9510 | |
| 9511 | bool ClassLinker::IsQuickGenericJniStub(const void* entry_point) const { |
| 9512 | return (entry_point == GetQuickGenericJniStub()) || |
| 9513 | (quick_generic_jni_trampoline_ == entry_point); |
| 9514 | } |
| 9515 | |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 9516 | bool ClassLinker::IsJniDlsymLookupStub(const void* entry_point) const { |
| 9517 | return entry_point == GetJniDlsymLookupStub(); |
| 9518 | } |
| 9519 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 9520 | const void* ClassLinker::GetRuntimeQuickGenericJniStub() const { |
| 9521 | return GetQuickGenericJniStub(); |
| 9522 | } |
| 9523 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 9524 | void ClassLinker::SetEntryPointsToInterpreter(ArtMethod* method) const { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 9525 | if (!method->IsNative()) { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 9526 | method->SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge()); |
| 9527 | } else { |
Goran Jakovljevic | c16268f | 2017-07-27 10:03:32 +0200 | [diff] [blame] | 9528 | method->SetEntryPointFromQuickCompiledCode(GetQuickGenericJniStub()); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 9529 | } |
| 9530 | } |
| 9531 | |
Alex Light | db01a09 | 2017-04-03 15:39:55 -0700 | [diff] [blame] | 9532 | void ClassLinker::SetEntryPointsForObsoleteMethod(ArtMethod* method) const { |
| 9533 | DCHECK(method->IsObsolete()); |
| 9534 | // We cannot mess with the entrypoints of native methods because they are used to determine how |
| 9535 | // large the method's quick stack frame is. Without this information we cannot walk the stacks. |
| 9536 | if (!method->IsNative()) { |
| 9537 | method->SetEntryPointFromQuickCompiledCode(GetInvokeObsoleteMethodStub()); |
| 9538 | } |
| 9539 | } |
| 9540 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 9541 | void ClassLinker::DumpForSigQuit(std::ostream& os) { |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 9542 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 9543 | ReaderMutexLock mu(soa.Self(), *Locks::classlinker_classes_lock_); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 9544 | os << "Zygote loaded classes=" << NumZygoteClasses() << " post zygote classes=" |
| 9545 | << NumNonZygoteClasses() << "\n"; |
Nicolas Geoffray | a90c922 | 2018-09-07 13:19:19 +0100 | [diff] [blame] | 9546 | ReaderMutexLock mu2(soa.Self(), *Locks::dex_lock_); |
| 9547 | os << "Dumping registered class loaders\n"; |
| 9548 | size_t class_loader_index = 0; |
| 9549 | for (const ClassLoaderData& class_loader : class_loaders_) { |
| 9550 | ObjPtr<mirror::ClassLoader> loader = |
| 9551 | ObjPtr<mirror::ClassLoader>::DownCast(soa.Self()->DecodeJObject(class_loader.weak_root)); |
| 9552 | if (loader != nullptr) { |
| 9553 | os << "#" << class_loader_index++ << " " << loader->GetClass()->PrettyDescriptor() << ": ["; |
| 9554 | bool saw_one_dex_file = false; |
| 9555 | for (const DexCacheData& dex_cache : dex_caches_) { |
| 9556 | if (dex_cache.IsValid() && dex_cache.class_table == class_loader.class_table) { |
| 9557 | if (saw_one_dex_file) { |
| 9558 | os << ":"; |
| 9559 | } |
| 9560 | saw_one_dex_file = true; |
| 9561 | os << dex_cache.dex_file->GetLocation(); |
| 9562 | } |
| 9563 | } |
| 9564 | os << "]"; |
| 9565 | bool found_parent = false; |
| 9566 | if (loader->GetParent() != nullptr) { |
| 9567 | size_t parent_index = 0; |
| 9568 | for (const ClassLoaderData& class_loader2 : class_loaders_) { |
| 9569 | ObjPtr<mirror::ClassLoader> loader2 = ObjPtr<mirror::ClassLoader>::DownCast( |
| 9570 | soa.Self()->DecodeJObject(class_loader2.weak_root)); |
| 9571 | if (loader2 == loader->GetParent()) { |
| 9572 | os << ", parent #" << parent_index; |
| 9573 | found_parent = true; |
| 9574 | break; |
| 9575 | } |
| 9576 | parent_index++; |
| 9577 | } |
| 9578 | if (!found_parent) { |
| 9579 | os << ", unregistered parent of type " |
| 9580 | << loader->GetParent()->GetClass()->PrettyDescriptor(); |
| 9581 | } |
| 9582 | } else { |
| 9583 | os << ", no parent"; |
| 9584 | } |
| 9585 | os << "\n"; |
| 9586 | } |
| 9587 | } |
| 9588 | os << "Done dumping class loaders\n"; |
Andreas Gampe | 9b7f8b5 | 2019-06-07 08:59:29 -0700 | [diff] [blame] | 9589 | Runtime* runtime = Runtime::Current(); |
| 9590 | os << "Classes initialized: " << runtime->GetStat(KIND_GLOBAL_CLASS_INIT_COUNT) << " in " |
| 9591 | << PrettyDuration(runtime->GetStat(KIND_GLOBAL_CLASS_INIT_TIME)) << "\n"; |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 9592 | } |
| 9593 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 9594 | class CountClassesVisitor : public ClassLoaderVisitor { |
| 9595 | public: |
| 9596 | CountClassesVisitor() : num_zygote_classes(0), num_non_zygote_classes(0) {} |
| 9597 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 9598 | void Visit(ObjPtr<mirror::ClassLoader> class_loader) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 9599 | REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_) override { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 9600 | ClassTable* const class_table = class_loader->GetClassTable(); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 9601 | if (class_table != nullptr) { |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 9602 | num_zygote_classes += class_table->NumZygoteClasses(class_loader); |
| 9603 | num_non_zygote_classes += class_table->NumNonZygoteClasses(class_loader); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 9604 | } |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 9605 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 9606 | |
| 9607 | size_t num_zygote_classes; |
| 9608 | size_t num_non_zygote_classes; |
| 9609 | }; |
| 9610 | |
| 9611 | size_t ClassLinker::NumZygoteClasses() const { |
| 9612 | CountClassesVisitor visitor; |
| 9613 | VisitClassLoaders(&visitor); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 9614 | return visitor.num_zygote_classes + boot_class_table_->NumZygoteClasses(nullptr); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 9615 | } |
| 9616 | |
| 9617 | size_t ClassLinker::NumNonZygoteClasses() const { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 9618 | CountClassesVisitor visitor; |
| 9619 | VisitClassLoaders(&visitor); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 9620 | return visitor.num_non_zygote_classes + boot_class_table_->NumNonZygoteClasses(nullptr); |
Elliott Hughes | cac6cc7 | 2011-11-03 20:31:21 -0700 | [diff] [blame] | 9621 | } |
| 9622 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 9623 | size_t ClassLinker::NumLoadedClasses() { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 9624 | ReaderMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 9625 | // 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] | 9626 | return NumNonZygoteClasses(); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 9627 | } |
| 9628 | |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 9629 | pid_t ClassLinker::GetClassesLockOwner() { |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 9630 | return Locks::classlinker_classes_lock_->GetExclusiveOwnerTid(); |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 9631 | } |
| 9632 | |
| 9633 | pid_t ClassLinker::GetDexLockOwner() { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 9634 | return Locks::dex_lock_->GetExclusiveOwnerTid(); |
Brian Carlstrom | 24a3c2e | 2011-10-17 18:07:52 -0700 | [diff] [blame] | 9635 | } |
| 9636 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 9637 | void ClassLinker::SetClassRoot(ClassRoot class_root, ObjPtr<mirror::Class> klass) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 9638 | DCHECK(!init_done_); |
| 9639 | |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 9640 | DCHECK(klass != nullptr); |
| 9641 | DCHECK(klass->GetClassLoader() == nullptr); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 9642 | |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 9643 | mirror::ObjectArray<mirror::Class>* class_roots = class_roots_.Read(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 9644 | DCHECK(class_roots != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 9645 | DCHECK_LT(static_cast<uint32_t>(class_root), static_cast<uint32_t>(ClassRoot::kMax)); |
| 9646 | int32_t index = static_cast<int32_t>(class_root); |
| 9647 | DCHECK(class_roots->Get(index) == nullptr); |
| 9648 | class_roots->Set<false>(index, klass); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 9649 | } |
| 9650 | |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 9651 | ObjPtr<mirror::ClassLoader> ClassLinker::CreateWellKnownClassLoader( |
| 9652 | Thread* self, |
| 9653 | const std::vector<const DexFile*>& dex_files, |
| 9654 | Handle<mirror::Class> loader_class, |
Nicolas Geoffray | e167273 | 2018-11-30 01:09:49 +0000 | [diff] [blame] | 9655 | Handle<mirror::ClassLoader> parent_loader, |
| 9656 | Handle<mirror::ObjectArray<mirror::ClassLoader>> shared_libraries) { |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 9657 | |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 9658 | StackHandleScope<5> hs(self); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9659 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 9660 | ArtField* dex_elements_field = |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 9661 | jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList_dexElements); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9662 | |
Vladimir Marko | 4098a7a | 2017-11-06 16:00:51 +0000 | [diff] [blame] | 9663 | Handle<mirror::Class> dex_elements_class(hs.NewHandle(dex_elements_field->ResolveType())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 9664 | DCHECK(dex_elements_class != nullptr); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9665 | DCHECK(dex_elements_class->IsArrayClass()); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 9666 | Handle<mirror::ObjectArray<mirror::Object>> h_dex_elements(hs.NewHandle( |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 9667 | mirror::ObjectArray<mirror::Object>::Alloc(self, |
| 9668 | dex_elements_class.Get(), |
| 9669 | dex_files.size()))); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9670 | Handle<mirror::Class> h_dex_element_class = |
| 9671 | hs.NewHandle(dex_elements_class->GetComponentType()); |
| 9672 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 9673 | ArtField* element_file_field = |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 9674 | jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 9675 | DCHECK_EQ(h_dex_element_class.Get(), element_file_field->GetDeclaringClass()); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9676 | |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 9677 | ArtField* cookie_field = jni::DecodeArtField(WellKnownClasses::dalvik_system_DexFile_cookie); |
Vladimir Marko | 208f670 | 2017-12-08 12:00:50 +0000 | [diff] [blame] | 9678 | DCHECK_EQ(cookie_field->GetDeclaringClass(), element_file_field->LookupResolvedType()); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9679 | |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 9680 | ArtField* file_name_field = jni::DecodeArtField(WellKnownClasses::dalvik_system_DexFile_fileName); |
Vladimir Marko | 208f670 | 2017-12-08 12:00:50 +0000 | [diff] [blame] | 9681 | DCHECK_EQ(file_name_field->GetDeclaringClass(), element_file_field->LookupResolvedType()); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 9682 | |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9683 | // Fill the elements array. |
| 9684 | int32_t index = 0; |
| 9685 | for (const DexFile* dex_file : dex_files) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 9686 | StackHandleScope<4> hs2(self); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9687 | |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 9688 | // CreateWellKnownClassLoader is only used by gtests and compiler. |
| 9689 | // 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] | 9690 | Handle<mirror::LongArray> h_long_array = hs2.NewHandle(mirror::LongArray::Alloc( |
| 9691 | self, |
| 9692 | kDexFileIndexStart + 1)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 9693 | DCHECK(h_long_array != nullptr); |
Vladimir Marko | 78baed5 | 2018-10-11 10:44:58 +0100 | [diff] [blame] | 9694 | h_long_array->Set(kDexFileIndexStart, reinterpret_cast64<int64_t>(dex_file)); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9695 | |
Mathieu Chartier | 3738e98 | 2017-05-12 16:07:28 -0700 | [diff] [blame] | 9696 | // Note that this creates a finalizable dalvik.system.DexFile object and a corresponding |
| 9697 | // FinalizerReference which will never get cleaned up without a started runtime. |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9698 | Handle<mirror::Object> h_dex_file = hs2.NewHandle( |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 9699 | cookie_field->GetDeclaringClass()->AllocObject(self)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 9700 | DCHECK(h_dex_file != nullptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 9701 | cookie_field->SetObject<false>(h_dex_file.Get(), h_long_array.Get()); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9702 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 9703 | Handle<mirror::String> h_file_name = hs2.NewHandle( |
| 9704 | mirror::String::AllocFromModifiedUtf8(self, dex_file->GetLocation().c_str())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 9705 | DCHECK(h_file_name != nullptr); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 9706 | file_name_field->SetObject<false>(h_dex_file.Get(), h_file_name.Get()); |
| 9707 | |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9708 | 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] | 9709 | DCHECK(h_element != nullptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 9710 | element_file_field->SetObject<false>(h_element.Get(), h_dex_file.Get()); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9711 | |
| 9712 | h_dex_elements->Set(index, h_element.Get()); |
| 9713 | index++; |
| 9714 | } |
| 9715 | DCHECK_EQ(index, h_dex_elements->GetLength()); |
| 9716 | |
| 9717 | // Create DexPathList. |
| 9718 | Handle<mirror::Object> h_dex_path_list = hs.NewHandle( |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 9719 | dex_elements_field->GetDeclaringClass()->AllocObject(self)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 9720 | DCHECK(h_dex_path_list != nullptr); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9721 | // Set elements. |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 9722 | 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] | 9723 | // Create an empty List for the "nativeLibraryDirectories," required for native tests. |
| 9724 | // Note: this code is uncommon(oatdump)/testing-only, so don't add further WellKnownClasses |
| 9725 | // elements. |
| 9726 | { |
| 9727 | ArtField* native_lib_dirs = dex_elements_field->GetDeclaringClass()-> |
| 9728 | FindDeclaredInstanceField("nativeLibraryDirectories", "Ljava/util/List;"); |
| 9729 | DCHECK(native_lib_dirs != nullptr); |
| 9730 | ObjPtr<mirror::Class> list_class = FindSystemClass(self, "Ljava/util/ArrayList;"); |
| 9731 | DCHECK(list_class != nullptr); |
| 9732 | { |
| 9733 | StackHandleScope<1> h_list_scope(self); |
| 9734 | Handle<mirror::Class> h_list_class(h_list_scope.NewHandle<mirror::Class>(list_class)); |
| 9735 | bool list_init = EnsureInitialized(self, h_list_class, true, true); |
| 9736 | DCHECK(list_init); |
| 9737 | list_class = h_list_class.Get(); |
| 9738 | } |
| 9739 | ObjPtr<mirror::Object> list_object = list_class->AllocObject(self); |
| 9740 | // Note: we leave the object uninitialized. This must never leak into any non-testing code, but |
| 9741 | // is fine for testing. While it violates a Java-code invariant (the elementData field is |
| 9742 | // normally never null), as long as one does not try to add elements, this will still |
| 9743 | // work. |
| 9744 | native_lib_dirs->SetObject<false>(h_dex_path_list.Get(), list_object); |
| 9745 | } |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9746 | |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 9747 | // Create the class loader.. |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 9748 | Handle<mirror::ClassLoader> h_class_loader = hs.NewHandle<mirror::ClassLoader>( |
| 9749 | ObjPtr<mirror::ClassLoader>::DownCast(loader_class->AllocObject(self))); |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 9750 | DCHECK(h_class_loader != nullptr); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9751 | // Set DexPathList. |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 9752 | ArtField* path_list_field = |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 9753 | jni::DecodeArtField(WellKnownClasses::dalvik_system_BaseDexClassLoader_pathList); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 9754 | DCHECK(path_list_field != nullptr); |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 9755 | 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] | 9756 | |
| 9757 | // Make a pretend boot-classpath. |
| 9758 | // TODO: Should we scan the image? |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 9759 | ArtField* const parent_field = |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 9760 | mirror::Class::FindField(self, |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 9761 | h_class_loader->GetClass(), |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 9762 | "parent", |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 9763 | "Ljava/lang/ClassLoader;"); |
Roland Levillain | f39c9eb | 2015-05-26 15:02:07 +0100 | [diff] [blame] | 9764 | DCHECK(parent_field != nullptr); |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 9765 | if (parent_loader.Get() == nullptr) { |
| 9766 | ScopedObjectAccessUnchecked soa(self); |
| 9767 | ObjPtr<mirror::Object> boot_loader(soa.Decode<mirror::Class>( |
| 9768 | WellKnownClasses::java_lang_BootClassLoader)->AllocObject(self)); |
| 9769 | parent_field->SetObject<false>(h_class_loader.Get(), boot_loader); |
| 9770 | } else { |
| 9771 | parent_field->SetObject<false>(h_class_loader.Get(), parent_loader.Get()); |
| 9772 | } |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 9773 | |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 9774 | ArtField* shared_libraries_field = |
| 9775 | jni::DecodeArtField(WellKnownClasses::dalvik_system_BaseDexClassLoader_sharedLibraryLoaders); |
| 9776 | DCHECK(shared_libraries_field != nullptr); |
| 9777 | shared_libraries_field->SetObject<false>(h_class_loader.Get(), shared_libraries.Get()); |
| 9778 | |
| 9779 | return h_class_loader.Get(); |
| 9780 | } |
| 9781 | |
| 9782 | jobject ClassLinker::CreateWellKnownClassLoader(Thread* self, |
| 9783 | const std::vector<const DexFile*>& dex_files, |
| 9784 | jclass loader_class, |
Nicolas Geoffray | e167273 | 2018-11-30 01:09:49 +0000 | [diff] [blame] | 9785 | jobject parent_loader, |
| 9786 | jobject shared_libraries) { |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 9787 | CHECK(self->GetJniEnv()->IsSameObject(loader_class, |
| 9788 | WellKnownClasses::dalvik_system_PathClassLoader) || |
| 9789 | self->GetJniEnv()->IsSameObject(loader_class, |
David Brazdil | 1a9ac53 | 2019-03-05 11:57:13 +0000 | [diff] [blame] | 9790 | WellKnownClasses::dalvik_system_DelegateLastClassLoader) || |
| 9791 | self->GetJniEnv()->IsSameObject(loader_class, |
| 9792 | WellKnownClasses::dalvik_system_InMemoryDexClassLoader)); |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 9793 | |
| 9794 | // SOAAlreadyRunnable is protected, and we need something to add a global reference. |
| 9795 | // We could move the jobject to the callers, but all call-sites do this... |
| 9796 | ScopedObjectAccessUnchecked soa(self); |
| 9797 | |
| 9798 | // For now, create a libcore-level DexFile for each ART DexFile. This "explodes" multidex. |
Nicolas Geoffray | e167273 | 2018-11-30 01:09:49 +0000 | [diff] [blame] | 9799 | StackHandleScope<4> hs(self); |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 9800 | |
| 9801 | Handle<mirror::Class> h_loader_class = |
| 9802 | hs.NewHandle<mirror::Class>(soa.Decode<mirror::Class>(loader_class)); |
Nicolas Geoffray | e167273 | 2018-11-30 01:09:49 +0000 | [diff] [blame] | 9803 | Handle<mirror::ClassLoader> h_parent = |
| 9804 | hs.NewHandle<mirror::ClassLoader>(soa.Decode<mirror::ClassLoader>(parent_loader)); |
| 9805 | Handle<mirror::ObjectArray<mirror::ClassLoader>> h_shared_libraries = |
| 9806 | hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::ClassLoader>>(shared_libraries)); |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 9807 | |
| 9808 | ObjPtr<mirror::ClassLoader> loader = CreateWellKnownClassLoader( |
| 9809 | self, |
| 9810 | dex_files, |
| 9811 | h_loader_class, |
Nicolas Geoffray | e167273 | 2018-11-30 01:09:49 +0000 | [diff] [blame] | 9812 | h_parent, |
| 9813 | h_shared_libraries); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9814 | |
| 9815 | // Make it a global ref and return. |
| 9816 | ScopedLocalRef<jobject> local_ref( |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 9817 | soa.Env(), soa.Env()->AddLocalReference<jobject>(loader)); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9818 | return soa.Env()->NewGlobalRef(local_ref.get()); |
| 9819 | } |
| 9820 | |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 9821 | jobject ClassLinker::CreatePathClassLoader(Thread* self, |
| 9822 | const std::vector<const DexFile*>& dex_files) { |
| 9823 | return CreateWellKnownClassLoader(self, |
| 9824 | dex_files, |
| 9825 | WellKnownClasses::dalvik_system_PathClassLoader, |
| 9826 | nullptr); |
| 9827 | } |
| 9828 | |
Andreas Gampe | 8ac7595 | 2015-06-02 21:01:45 -0700 | [diff] [blame] | 9829 | void ClassLinker::DropFindArrayClassCache() { |
| 9830 | std::fill_n(find_array_class_cache_, kFindArrayCacheSize, GcRoot<mirror::Class>(nullptr)); |
| 9831 | find_array_class_cache_next_victim_ = 0; |
| 9832 | } |
| 9833 | |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 9834 | void ClassLinker::VisitClassLoaders(ClassLoaderVisitor* visitor) const { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 9835 | Thread* const self = Thread::Current(); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 9836 | for (const ClassLoaderData& data : class_loaders_) { |
Mathieu Chartier | 4843bd5 | 2015-10-01 17:08:44 -0700 | [diff] [blame] | 9837 | // 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] | 9838 | ObjPtr<mirror::ClassLoader> class_loader = ObjPtr<mirror::ClassLoader>::DownCast( |
| 9839 | self->DecodeJObject(data.weak_root)); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 9840 | if (class_loader != nullptr) { |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 9841 | visitor->Visit(class_loader); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 9842 | } |
| 9843 | } |
| 9844 | } |
| 9845 | |
Alexey Grebenkin | 252a4e4 | 2018-04-02 18:18:01 +0300 | [diff] [blame] | 9846 | void ClassLinker::VisitAllocators(AllocatorVisitor* visitor) const { |
| 9847 | for (const ClassLoaderData& data : class_loaders_) { |
| 9848 | LinearAlloc* alloc = data.allocator; |
| 9849 | if (alloc != nullptr && !visitor->Visit(alloc)) { |
| 9850 | break; |
| 9851 | } |
| 9852 | } |
| 9853 | } |
| 9854 | |
Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 9855 | void ClassLinker::InsertDexFileInToClassLoader(ObjPtr<mirror::Object> dex_file, |
| 9856 | ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 9857 | DCHECK(dex_file != nullptr); |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 9858 | Thread* const self = Thread::Current(); |
| 9859 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 9860 | ClassTable* const table = ClassTableForClassLoader(class_loader); |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 9861 | DCHECK(table != nullptr); |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 9862 | if (table->InsertStrongRoot(dex_file) && class_loader != nullptr) { |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 9863 | // It was not already inserted, perform the write barrier to let the GC know the class loader's |
| 9864 | // class table was modified. |
Mathieu Chartier | 88ea61e | 2018-06-20 17:45:41 -0700 | [diff] [blame] | 9865 | WriteBarrier::ForEveryFieldWrite(class_loader); |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 9866 | } |
| 9867 | } |
| 9868 | |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 9869 | void ClassLinker::CleanupClassLoaders() { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 9870 | Thread* const self = Thread::Current(); |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 9871 | std::vector<ClassLoaderData> to_delete; |
| 9872 | // Do the delete outside the lock to avoid lock violation in jit code cache. |
| 9873 | { |
| 9874 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 9875 | for (auto it = class_loaders_.begin(); it != class_loaders_.end(); ) { |
| 9876 | const ClassLoaderData& data = *it; |
| 9877 | // 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] | 9878 | ObjPtr<mirror::ClassLoader> class_loader = |
| 9879 | ObjPtr<mirror::ClassLoader>::DownCast(self->DecodeJObject(data.weak_root)); |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 9880 | if (class_loader != nullptr) { |
| 9881 | ++it; |
| 9882 | } else { |
| 9883 | VLOG(class_linker) << "Freeing class loader"; |
| 9884 | to_delete.push_back(data); |
| 9885 | it = class_loaders_.erase(it); |
| 9886 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 9887 | } |
| 9888 | } |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 9889 | for (ClassLoaderData& data : to_delete) { |
Alexey Grebenkin | be4c2bd | 2018-02-01 19:09:59 +0300 | [diff] [blame] | 9890 | // CHA unloading analysis and SingleImplementaion cleanups are required. |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 9891 | DeleteClassLoader(self, data, /*cleanup_cha=*/ true); |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 9892 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 9893 | } |
| 9894 | |
Vladimir Marko | 2130053 | 2017-01-24 18:06:55 +0000 | [diff] [blame] | 9895 | class GetResolvedClassesVisitor : public ClassVisitor { |
| 9896 | public: |
| 9897 | GetResolvedClassesVisitor(std::set<DexCacheResolvedClasses>* result, bool ignore_boot_classes) |
| 9898 | : result_(result), |
| 9899 | ignore_boot_classes_(ignore_boot_classes), |
| 9900 | last_resolved_classes_(result->end()), |
| 9901 | last_dex_file_(nullptr), |
| 9902 | vlog_is_on_(VLOG_IS_ON(class_linker)), |
| 9903 | extra_stats_(), |
| 9904 | last_extra_stats_(extra_stats_.end()) { } |
| 9905 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 9906 | bool operator()(ObjPtr<mirror::Class> klass) override REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | 2130053 | 2017-01-24 18:06:55 +0000 | [diff] [blame] | 9907 | if (!klass->IsProxyClass() && |
| 9908 | !klass->IsArrayClass() && |
| 9909 | klass->IsResolved() && |
| 9910 | !klass->IsErroneousResolved() && |
| 9911 | (!ignore_boot_classes_ || klass->GetClassLoader() != nullptr)) { |
| 9912 | const DexFile& dex_file = klass->GetDexFile(); |
| 9913 | if (&dex_file != last_dex_file_) { |
| 9914 | last_dex_file_ = &dex_file; |
Mathieu Chartier | 79c87da | 2017-10-10 11:54:29 -0700 | [diff] [blame] | 9915 | DexCacheResolvedClasses resolved_classes( |
| 9916 | dex_file.GetLocation(), |
| 9917 | DexFileLoader::GetBaseLocation(dex_file.GetLocation()), |
| 9918 | dex_file.GetLocationChecksum(), |
| 9919 | dex_file.NumMethodIds()); |
Vladimir Marko | 2130053 | 2017-01-24 18:06:55 +0000 | [diff] [blame] | 9920 | last_resolved_classes_ = result_->find(resolved_classes); |
| 9921 | if (last_resolved_classes_ == result_->end()) { |
| 9922 | last_resolved_classes_ = result_->insert(resolved_classes).first; |
| 9923 | } |
| 9924 | } |
| 9925 | bool added = last_resolved_classes_->AddClass(klass->GetDexTypeIndex()); |
| 9926 | if (UNLIKELY(vlog_is_on_) && added) { |
| 9927 | const DexCacheResolvedClasses* resolved_classes = std::addressof(*last_resolved_classes_); |
| 9928 | if (last_extra_stats_ == extra_stats_.end() || |
| 9929 | last_extra_stats_->first != resolved_classes) { |
| 9930 | last_extra_stats_ = extra_stats_.find(resolved_classes); |
| 9931 | if (last_extra_stats_ == extra_stats_.end()) { |
| 9932 | last_extra_stats_ = |
| 9933 | extra_stats_.emplace(resolved_classes, ExtraStats(dex_file.NumClassDefs())).first; |
| 9934 | } |
| 9935 | } |
| 9936 | } |
| 9937 | } |
| 9938 | return true; |
| 9939 | } |
| 9940 | |
| 9941 | void PrintStatistics() const { |
| 9942 | if (vlog_is_on_) { |
| 9943 | for (const DexCacheResolvedClasses& resolved_classes : *result_) { |
| 9944 | auto it = extra_stats_.find(std::addressof(resolved_classes)); |
| 9945 | DCHECK(it != extra_stats_.end()); |
| 9946 | const ExtraStats& extra_stats = it->second; |
| 9947 | LOG(INFO) << "Dex location " << resolved_classes.GetDexLocation() |
| 9948 | << " has " << resolved_classes.GetClasses().size() << " / " |
| 9949 | << extra_stats.number_of_class_defs_ << " resolved classes"; |
| 9950 | } |
| 9951 | } |
| 9952 | } |
| 9953 | |
| 9954 | private: |
| 9955 | struct ExtraStats { |
| 9956 | explicit ExtraStats(uint32_t number_of_class_defs) |
| 9957 | : number_of_class_defs_(number_of_class_defs) {} |
| 9958 | uint32_t number_of_class_defs_; |
| 9959 | }; |
| 9960 | |
| 9961 | std::set<DexCacheResolvedClasses>* result_; |
| 9962 | bool ignore_boot_classes_; |
| 9963 | std::set<DexCacheResolvedClasses>::iterator last_resolved_classes_; |
| 9964 | const DexFile* last_dex_file_; |
| 9965 | |
| 9966 | // Statistics. |
| 9967 | bool vlog_is_on_; |
| 9968 | std::map<const DexCacheResolvedClasses*, ExtraStats> extra_stats_; |
| 9969 | std::map<const DexCacheResolvedClasses*, ExtraStats>::iterator last_extra_stats_; |
| 9970 | }; |
| 9971 | |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 9972 | std::set<DexCacheResolvedClasses> ClassLinker::GetResolvedClasses(bool ignore_boot_classes) { |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 9973 | ScopedTrace trace(__PRETTY_FUNCTION__); |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 9974 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 9975 | ScopedAssertNoThreadSuspension ants(__FUNCTION__); |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 9976 | std::set<DexCacheResolvedClasses> ret; |
| 9977 | VLOG(class_linker) << "Collecting resolved classes"; |
| 9978 | const uint64_t start_time = NanoTime(); |
Vladimir Marko | 2130053 | 2017-01-24 18:06:55 +0000 | [diff] [blame] | 9979 | GetResolvedClassesVisitor visitor(&ret, ignore_boot_classes); |
| 9980 | VisitClasses(&visitor); |
| 9981 | if (VLOG_IS_ON(class_linker)) { |
| 9982 | visitor.PrintStatistics(); |
| 9983 | LOG(INFO) << "Collecting class profile took " << PrettyDuration(NanoTime() - start_time); |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 9984 | } |
Mathieu Chartier | c5dd319 | 2015-12-09 16:38:30 -0800 | [diff] [blame] | 9985 | return ret; |
| 9986 | } |
| 9987 | |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 9988 | class ClassLinker::FindVirtualMethodHolderVisitor : public ClassVisitor { |
| 9989 | public: |
| 9990 | FindVirtualMethodHolderVisitor(const ArtMethod* method, PointerSize pointer_size) |
| 9991 | : method_(method), |
| 9992 | pointer_size_(pointer_size) {} |
| 9993 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 9994 | bool operator()(ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_) override { |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 9995 | if (klass->GetVirtualMethodsSliceUnchecked(pointer_size_).Contains(method_)) { |
| 9996 | holder_ = klass; |
| 9997 | } |
| 9998 | // Return false to stop searching if holder_ is not null. |
| 9999 | return holder_ == nullptr; |
| 10000 | } |
| 10001 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 10002 | ObjPtr<mirror::Class> holder_ = nullptr; |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 10003 | const ArtMethod* const method_; |
| 10004 | const PointerSize pointer_size_; |
| 10005 | }; |
| 10006 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 10007 | ObjPtr<mirror::Class> ClassLinker::GetHoldingClassOfCopiedMethod(ArtMethod* method) { |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 10008 | ScopedTrace trace(__FUNCTION__); // Since this function is slow, have a trace to notify people. |
| 10009 | CHECK(method->IsCopied()); |
| 10010 | FindVirtualMethodHolderVisitor visitor(method, image_pointer_size_); |
| 10011 | VisitClasses(&visitor); |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 10012 | return visitor.holder_; |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 10013 | } |
| 10014 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 10015 | ObjPtr<mirror::IfTable> ClassLinker::AllocIfTable(Thread* self, size_t ifcount) { |
| 10016 | return ObjPtr<mirror::IfTable>::DownCast(ObjPtr<mirror::ObjectArray<mirror::Object>>( |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 10017 | mirror::IfTable::Alloc(self, |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 10018 | GetClassRoot<mirror::ObjectArray<mirror::Object>>(this), |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 10019 | ifcount * mirror::IfTable::kMax))); |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 10020 | } |
| 10021 | |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 10022 | // Instantiate ClassLinker::ResolveMethod. |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 10023 | template ArtMethod* ClassLinker::ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>( |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 10024 | uint32_t method_idx, |
| 10025 | Handle<mirror::DexCache> dex_cache, |
| 10026 | Handle<mirror::ClassLoader> class_loader, |
| 10027 | ArtMethod* referrer, |
| 10028 | InvokeType type); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 10029 | template ArtMethod* ClassLinker::ResolveMethod<ClassLinker::ResolveMode::kNoChecks>( |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 10030 | uint32_t method_idx, |
| 10031 | Handle<mirror::DexCache> dex_cache, |
| 10032 | Handle<mirror::ClassLoader> class_loader, |
| 10033 | ArtMethod* referrer, |
| 10034 | InvokeType type); |
| 10035 | |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 10036 | // Instantiate ClassLinker::AllocClass. |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 10037 | template ObjPtr<mirror::Class> ClassLinker::AllocClass</* kMovable= */ true>( |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 10038 | Thread* self, |
| 10039 | ObjPtr<mirror::Class> java_lang_Class, |
| 10040 | uint32_t class_size); |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 10041 | template ObjPtr<mirror::Class> ClassLinker::AllocClass</* kMovable= */ false>( |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 10042 | Thread* self, |
| 10043 | ObjPtr<mirror::Class> java_lang_Class, |
| 10044 | uint32_t class_size); |
| 10045 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 10046 | } // namespace art |