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> |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 31 | #include <utility> |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 32 | #include <vector> |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 33 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 34 | #include "android-base/stringprintf.h" |
| 35 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 36 | #include "art_field-inl.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 37 | #include "art_method-inl.h" |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 38 | #include "barrier.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 39 | #include "base/arena_allocator.h" |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 40 | #include "base/casts.h" |
Andreas Gampe | 19f5416 | 2019-05-14 16:16:28 -0700 | [diff] [blame] | 41 | #include "base/file_utils.h" |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 42 | #include "base/hash_map.h" |
| 43 | #include "base/hash_set.h" |
David Sehr | 67bf42e | 2018-02-26 16:43:04 -0800 | [diff] [blame] | 44 | #include "base/leb128.h" |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 45 | #include "base/logging.h" |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 46 | #include "base/mutex-inl.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 47 | #include "base/os.h" |
| 48 | #include "base/quasi_atomic.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 49 | #include "base/scoped_arena_containers.h" |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 50 | #include "base/scoped_flock.h" |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 51 | #include "base/stl_util.h" |
Vladimir Marko | b9c29f6 | 2019-03-20 14:22:51 +0000 | [diff] [blame] | 52 | #include "base/string_view_cpp20.h" |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 53 | #include "base/systrace.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 54 | #include "base/time_utils.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 55 | #include "base/unix_file/fd_file.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 56 | #include "base/utils.h" |
Andreas Gampe | b9aec2c | 2015-04-23 22:23:47 -0700 | [diff] [blame] | 57 | #include "base/value_object.h" |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 58 | #include "cha.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 59 | #include "class_linker-inl.h" |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 60 | #include "class_loader_utils.h" |
Vladimir Marko | 5868ada | 2020-05-12 11:50:34 +0100 | [diff] [blame] | 61 | #include "class_root-inl.h" |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 62 | #include "class_table-inl.h" |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 63 | #include "compiler_callbacks.h" |
Vladimir Marko | 606adb3 | 2018-04-05 14:49:24 +0100 | [diff] [blame] | 64 | #include "debug_print.h" |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 65 | #include "debugger.h" |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 66 | #include "dex/class_accessor-inl.h" |
David Sehr | b2ec9f5 | 2018-02-21 13:20:31 -0800 | [diff] [blame] | 67 | #include "dex/descriptors_names.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 68 | #include "dex/dex_file-inl.h" |
| 69 | #include "dex/dex_file_exception_helpers.h" |
| 70 | #include "dex/dex_file_loader.h" |
Andreas Gampe | ad1aa63 | 2019-01-02 10:30:54 -0800 | [diff] [blame] | 71 | #include "dex/signature-inl.h" |
David Sehr | 0225f8e | 2018-01-31 08:52:24 +0000 | [diff] [blame] | 72 | #include "dex/utf.h" |
Vladimir Marko | 5115a4d | 2019-10-17 14:56:47 +0100 | [diff] [blame] | 73 | #include "entrypoints/entrypoint_utils-inl.h" |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 74 | #include "entrypoints/runtime_asm_entrypoints.h" |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 75 | #include "experimental_flags.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 76 | #include "gc/accounting/card_table-inl.h" |
Mathieu Chartier | 03c1dd9 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 77 | #include "gc/accounting/heap_bitmap-inl.h" |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 78 | #include "gc/accounting/space_bitmap-inl.h" |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 79 | #include "gc/heap-visit-objects-inl.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 80 | #include "gc/heap.h" |
Mathieu Chartier | 1b1e31f | 2016-05-19 10:13:04 -0700 | [diff] [blame] | 81 | #include "gc/scoped_gc_critical_section.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 82 | #include "gc/space/image_space.h" |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 83 | #include "gc/space/space-inl.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 84 | #include "gc_root-inl.h" |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 85 | #include "handle_scope-inl.h" |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 86 | #include "hidden_api.h" |
Mathieu Chartier | 4a26f17 | 2016-01-26 14:26:18 -0800 | [diff] [blame] | 87 | #include "image-inl.h" |
Andreas Gampe | 75a7db6 | 2016-09-26 12:04:26 -0700 | [diff] [blame] | 88 | #include "imt_conflict_table.h" |
| 89 | #include "imtable-inl.h" |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 90 | #include "intern_table-inl.h" |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 91 | #include "interpreter/interpreter.h" |
Nicolas Geoffray | 0315efa | 2020-06-26 11:42:39 +0100 | [diff] [blame] | 92 | #include "interpreter/mterp/nterp.h" |
David Srbecky | fb3de3d | 2018-01-29 16:11:49 +0000 | [diff] [blame] | 93 | #include "jit/debugger_interface.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 94 | #include "jit/jit.h" |
| 95 | #include "jit/jit_code_cache.h" |
Vladimir Marko | a3ad0cd | 2018-05-04 10:06:38 +0100 | [diff] [blame] | 96 | #include "jni/java_vm_ext.h" |
| 97 | #include "jni/jni_internal.h" |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 98 | #include "linear_alloc.h" |
Andreas Gampe | 8e0f043 | 2018-10-24 13:38:03 -0700 | [diff] [blame] | 99 | #include "mirror/array-alloc-inl.h" |
| 100 | #include "mirror/array-inl.h" |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 101 | #include "mirror/call_site.h" |
Andreas Gampe | 70f5fd0 | 2018-10-24 19:58:37 -0700 | [diff] [blame] | 102 | #include "mirror/class-alloc-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 103 | #include "mirror/class-inl.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 104 | #include "mirror/class.h" |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 105 | #include "mirror/class_ext.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 106 | #include "mirror/class_loader.h" |
Ian Rogers | 39ebcb8 | 2013-05-30 16:57:23 -0700 | [diff] [blame] | 107 | #include "mirror/dex_cache-inl.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 108 | #include "mirror/dex_cache.h" |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 109 | #include "mirror/emulated_stack_frame.h" |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 110 | #include "mirror/field.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 111 | #include "mirror/iftable-inl.h" |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 112 | #include "mirror/method.h" |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 113 | #include "mirror/method_handle_impl.h" |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 114 | #include "mirror/method_handles_lookup.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 115 | #include "mirror/method_type.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 116 | #include "mirror/object-inl.h" |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 117 | #include "mirror/object-refvisitor-inl.h" |
Alex Light | a9bbc08 | 2019-11-14 14:51:41 -0800 | [diff] [blame] | 118 | #include "mirror/object.h" |
Andreas Gampe | 52ecb65 | 2018-10-24 15:18:21 -0700 | [diff] [blame] | 119 | #include "mirror/object_array-alloc-inl.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 120 | #include "mirror/object_array-inl.h" |
Alex Light | 133987d | 2020-03-26 19:22:12 +0000 | [diff] [blame] | 121 | #include "mirror/object_array.h" |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 122 | #include "mirror/object_reference.h" |
| 123 | #include "mirror/object_reference-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 124 | #include "mirror/proxy.h" |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 125 | #include "mirror/reference-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 126 | #include "mirror/stack_trace_element.h" |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 127 | #include "mirror/string-inl.h" |
Andreas Gampe | 501c3b0 | 2019-04-17 21:54:27 +0000 | [diff] [blame] | 128 | #include "mirror/throwable.h" |
Orion Hodson | 005ac51 | 2017-10-24 15:43:43 +0100 | [diff] [blame] | 129 | #include "mirror/var_handle.h" |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 130 | #include "native/dalvik_system_DexFile.h" |
Andreas Gampe | 373a9b5 | 2017-10-18 09:01:57 -0700 | [diff] [blame] | 131 | #include "nativehelper/scoped_local_ref.h" |
Nicolas Geoffray | c39af94 | 2021-01-25 08:43:57 +0000 | [diff] [blame] | 132 | #include "nterp_helpers.h" |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 133 | #include "oat.h" |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 134 | #include "oat_file-inl.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 135 | #include "oat_file.h" |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 136 | #include "oat_file_assistant.h" |
| 137 | #include "oat_file_manager.h" |
| 138 | #include "object_lock.h" |
David Sehr | 82d046e | 2018-04-23 08:14:19 -0700 | [diff] [blame] | 139 | #include "profile/profile_compilation_info.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 140 | #include "runtime.h" |
Andreas Gampe | ac30fa2 | 2017-01-18 21:02:36 -0800 | [diff] [blame] | 141 | #include "runtime_callbacks.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 142 | #include "scoped_thread_state_change-inl.h" |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 143 | #include "thread-inl.h" |
Alex Light | 133987d | 2020-03-26 19:22:12 +0000 | [diff] [blame] | 144 | #include "thread.h" |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 145 | #include "thread_list.h" |
Mathieu Chartier | 7778b88 | 2015-10-05 16:41:10 -0700 | [diff] [blame] | 146 | #include "trace.h" |
Vladimir Marko | b68bb7a | 2020-03-17 10:55:25 +0000 | [diff] [blame] | 147 | #include "transaction.h" |
Andreas Gampe | a43ba3d | 2019-03-13 15:49:20 -0700 | [diff] [blame] | 148 | #include "verifier/class_verifier.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 149 | #include "well_known_classes.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 150 | |
Nicolas Geoffray | 0039182 | 2019-12-10 10:17:23 +0000 | [diff] [blame] | 151 | #include "interpreter/interpreter_mterp_impl.h" |
| 152 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 153 | namespace art { |
| 154 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 155 | using android::base::StringPrintf; |
| 156 | |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 157 | static constexpr bool kCheckImageObjects = kIsDebugBuild; |
Mathieu Chartier | 8790c7f | 2016-03-31 15:05:45 -0700 | [diff] [blame] | 158 | static constexpr bool kVerifyArtMethodDeclaringClasses = kIsDebugBuild; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 159 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 160 | static void ThrowNoClassDefFoundError(const char* fmt, ...) |
| 161 | __attribute__((__format__(__printf__, 1, 2))) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 162 | REQUIRES_SHARED(Locks::mutator_lock_); |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 163 | static void ThrowNoClassDefFoundError(const char* fmt, ...) { |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 164 | va_list args; |
| 165 | va_start(args, fmt); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 166 | Thread* self = Thread::Current(); |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 167 | self->ThrowNewExceptionV("Ljava/lang/NoClassDefFoundError;", fmt, args); |
Ian Rogers | cab0101 | 2012-01-10 17:35:46 -0800 | [diff] [blame] | 168 | va_end(args); |
| 169 | } |
| 170 | |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 171 | static bool HasInitWithString(Thread* self, ClassLinker* class_linker, const char* descriptor) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 172 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 173 | ArtMethod* method = self->GetCurrentMethod(nullptr); |
Andreas Gampe | bfdcdc1 | 2015-04-22 18:10:36 -0700 | [diff] [blame] | 174 | StackHandleScope<1> hs(self); |
| 175 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(method != nullptr ? |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 176 | method->GetDeclaringClass()->GetClassLoader() : nullptr)); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 177 | ObjPtr<mirror::Class> exception_class = class_linker->FindClass(self, descriptor, class_loader); |
Andreas Gampe | bfdcdc1 | 2015-04-22 18:10:36 -0700 | [diff] [blame] | 178 | |
| 179 | if (exception_class == nullptr) { |
| 180 | // No exc class ~ no <init>-with-string. |
| 181 | CHECK(self->IsExceptionPending()); |
| 182 | self->ClearException(); |
| 183 | return false; |
| 184 | } |
| 185 | |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 186 | ArtMethod* exception_init_method = exception_class->FindConstructor( |
| 187 | "(Ljava/lang/String;)V", class_linker->GetImagePointerSize()); |
Andreas Gampe | bfdcdc1 | 2015-04-22 18:10:36 -0700 | [diff] [blame] | 188 | return exception_init_method != nullptr; |
| 189 | } |
| 190 | |
Vladimir Marko | bb206de | 2019-03-28 10:30:32 +0000 | [diff] [blame] | 191 | static ObjPtr<mirror::Object> GetVerifyError(ObjPtr<mirror::Class> c) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 192 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 193 | ObjPtr<mirror::ClassExt> ext(c->GetExtData()); |
| 194 | if (ext == nullptr) { |
| 195 | return nullptr; |
| 196 | } else { |
| 197 | return ext->GetVerifyError(); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | // Helper for ThrowEarlierClassFailure. Throws the stored error. |
| 202 | static void HandleEarlierVerifyError(Thread* self, |
| 203 | ClassLinker* class_linker, |
| 204 | ObjPtr<mirror::Class> c) |
| 205 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 206 | ObjPtr<mirror::Object> obj = GetVerifyError(c); |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 207 | DCHECK(obj != nullptr); |
| 208 | self->AssertNoPendingException(); |
| 209 | if (obj->IsClass()) { |
| 210 | // Previous error has been stored as class. Create a new exception of that type. |
| 211 | |
| 212 | // It's possible the exception doesn't have a <init>(String). |
| 213 | std::string temp; |
| 214 | const char* descriptor = obj->AsClass()->GetDescriptor(&temp); |
| 215 | |
| 216 | if (HasInitWithString(self, class_linker, descriptor)) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 217 | self->ThrowNewException(descriptor, c->PrettyDescriptor().c_str()); |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 218 | } else { |
| 219 | self->ThrowNewException(descriptor, nullptr); |
| 220 | } |
| 221 | } else { |
| 222 | // Previous error has been stored as an instance. Just rethrow. |
Vladimir Marko | c13fbd8 | 2018-06-04 16:16:28 +0100 | [diff] [blame] | 223 | ObjPtr<mirror::Class> throwable_class = GetClassRoot<mirror::Throwable>(class_linker); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 224 | ObjPtr<mirror::Class> error_class = obj->GetClass(); |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 225 | CHECK(throwable_class->IsAssignableFrom(error_class)); |
| 226 | self->SetException(obj->AsThrowable()); |
| 227 | } |
| 228 | self->AssertPendingException(); |
| 229 | } |
| 230 | |
Nicolas Geoffray | 60d4abc | 2020-07-27 13:58:51 +0000 | [diff] [blame] | 231 | static void ChangeInterpreterBridgeToNterp(ArtMethod* method, ClassLinker* class_linker) |
| 232 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 233 | Runtime* runtime = Runtime::Current(); |
| 234 | if (class_linker->IsQuickToInterpreterBridge(method->GetEntryPointFromQuickCompiledCode()) && |
Nicolas Geoffray | c39af94 | 2021-01-25 08:43:57 +0000 | [diff] [blame] | 235 | CanMethodUseNterp(method)) { |
Nicolas Geoffray | 60d4abc | 2020-07-27 13:58:51 +0000 | [diff] [blame] | 236 | if (method->GetDeclaringClass()->IsVisiblyInitialized() || |
| 237 | !NeedsClinitCheckBeforeCall(method)) { |
| 238 | runtime->GetInstrumentation()->UpdateMethodsCode(method, interpreter::GetNterpEntryPoint()); |
| 239 | } else { |
| 240 | // Put the resolution stub, which will initialize the class and then |
| 241 | // call the method with nterp. |
| 242 | runtime->GetInstrumentation()->UpdateMethodsCode(method, GetQuickResolutionStub()); |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | |
Andreas Gampe | 5b20b35 | 2018-10-11 19:03:20 -0700 | [diff] [blame] | 247 | // Ensures that methods have the kAccSkipAccessChecks bit set. We use the |
| 248 | // kAccVerificationAttempted bit on the class access flags to determine whether this has been done |
| 249 | // before. |
Andreas Gampe | 5b20b35 | 2018-10-11 19:03:20 -0700 | [diff] [blame] | 250 | static void EnsureSkipAccessChecksMethods(Handle<mirror::Class> klass, PointerSize pointer_size) |
| 251 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Nicolas Geoffray | 0039182 | 2019-12-10 10:17:23 +0000 | [diff] [blame] | 252 | Runtime* runtime = Runtime::Current(); |
| 253 | ClassLinker* class_linker = runtime->GetClassLinker(); |
Andreas Gampe | 5b20b35 | 2018-10-11 19:03:20 -0700 | [diff] [blame] | 254 | if (!klass->WasVerificationAttempted()) { |
| 255 | klass->SetSkipAccessChecksFlagOnAllMethods(pointer_size); |
| 256 | klass->SetVerificationAttempted(); |
Nicolas Geoffray | 0039182 | 2019-12-10 10:17:23 +0000 | [diff] [blame] | 257 | // Now that the class has passed verification, try to set nterp entrypoints |
| 258 | // to methods that currently use the switch interpreter. |
| 259 | if (interpreter::CanRuntimeUseNterp()) { |
| 260 | for (ArtMethod& m : klass->GetMethods(pointer_size)) { |
Nicolas Geoffray | 60d4abc | 2020-07-27 13:58:51 +0000 | [diff] [blame] | 261 | ChangeInterpreterBridgeToNterp(&m, class_linker); |
Nicolas Geoffray | 0039182 | 2019-12-10 10:17:23 +0000 | [diff] [blame] | 262 | } |
| 263 | } |
Andreas Gampe | 5b20b35 | 2018-10-11 19:03:20 -0700 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 267 | // Callback responsible for making a batch of classes visibly initialized |
| 268 | // after all threads have called it from a checkpoint, ensuring visibility. |
| 269 | class ClassLinker::VisiblyInitializedCallback final |
| 270 | : public Closure, public IntrusiveForwardListNode<VisiblyInitializedCallback> { |
| 271 | public: |
| 272 | explicit VisiblyInitializedCallback(ClassLinker* class_linker) |
| 273 | : class_linker_(class_linker), |
| 274 | num_classes_(0u), |
| 275 | thread_visibility_counter_(0), |
| 276 | barriers_() { |
| 277 | std::fill_n(classes_, kMaxClasses, nullptr); |
| 278 | } |
| 279 | |
| 280 | bool IsEmpty() const { |
| 281 | DCHECK_LE(num_classes_, kMaxClasses); |
| 282 | return num_classes_ == 0u; |
| 283 | } |
| 284 | |
| 285 | bool IsFull() const { |
| 286 | DCHECK_LE(num_classes_, kMaxClasses); |
| 287 | return num_classes_ == kMaxClasses; |
| 288 | } |
| 289 | |
| 290 | void AddClass(Thread* self, ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 291 | DCHECK_EQ(klass->GetStatus(), ClassStatus::kInitialized); |
| 292 | DCHECK(!IsFull()); |
| 293 | classes_[num_classes_] = self->GetJniEnv()->GetVm()->AddWeakGlobalRef(self, klass); |
| 294 | ++num_classes_; |
| 295 | } |
| 296 | |
| 297 | void AddBarrier(Barrier* barrier) { |
| 298 | barriers_.push_front(barrier); |
| 299 | } |
| 300 | |
| 301 | std::forward_list<Barrier*> GetAndClearBarriers() { |
| 302 | std::forward_list<Barrier*> result; |
| 303 | result.swap(barriers_); |
| 304 | result.reverse(); // Return barriers in insertion order. |
| 305 | return result; |
| 306 | } |
| 307 | |
| 308 | void MakeVisible(Thread* self) { |
| 309 | DCHECK_EQ(thread_visibility_counter_.load(std::memory_order_relaxed), 0); |
| 310 | size_t count = Runtime::Current()->GetThreadList()->RunCheckpoint(this); |
| 311 | AdjustThreadVisibilityCounter(self, count); |
| 312 | } |
| 313 | |
| 314 | void Run(Thread* self) override { |
| 315 | self->ClearMakeVisiblyInitializedCounter(); |
| 316 | AdjustThreadVisibilityCounter(self, -1); |
| 317 | } |
| 318 | |
| 319 | private: |
| 320 | void AdjustThreadVisibilityCounter(Thread* self, ssize_t adjustment) { |
| 321 | ssize_t old = thread_visibility_counter_.fetch_add(adjustment, std::memory_order_relaxed); |
| 322 | if (old + adjustment == 0) { |
| 323 | // All threads passed the checkpoint. Mark classes as visibly initialized. |
| 324 | { |
| 325 | ScopedObjectAccess soa(self); |
| 326 | StackHandleScope<1u> hs(self); |
| 327 | MutableHandle<mirror::Class> klass = hs.NewHandle<mirror::Class>(nullptr); |
| 328 | JavaVMExt* vm = self->GetJniEnv()->GetVm(); |
| 329 | for (size_t i = 0, num = num_classes_; i != num; ++i) { |
| 330 | klass.Assign(ObjPtr<mirror::Class>::DownCast(self->DecodeJObject(classes_[i]))); |
| 331 | vm->DeleteWeakGlobalRef(self, classes_[i]); |
| 332 | if (klass != nullptr) { |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 333 | mirror::Class::SetStatus(klass, ClassStatus::kVisiblyInitialized, self); |
Vladimir Marko | 86c8752 | 2020-05-11 16:55:55 +0100 | [diff] [blame] | 334 | class_linker_->FixupStaticTrampolines(self, klass.Get()); |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 335 | } |
| 336 | } |
| 337 | num_classes_ = 0u; |
| 338 | } |
| 339 | class_linker_->VisiblyInitializedCallbackDone(self, this); |
| 340 | } |
| 341 | } |
| 342 | |
Vladimir Marko | 9f18fbc | 2019-07-31 15:06:12 +0100 | [diff] [blame] | 343 | static constexpr size_t kMaxClasses = 16; |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 344 | |
| 345 | ClassLinker* const class_linker_; |
| 346 | size_t num_classes_; |
| 347 | jweak classes_[kMaxClasses]; |
| 348 | |
| 349 | // The thread visibility counter starts at 0 and it is incremented by the number of |
| 350 | // threads that need to run this callback (by the thread that request the callback |
| 351 | // to be run) and decremented once for each `Run()` execution. When it reaches 0, |
| 352 | // whether after the increment or after a decrement, we know that `Run()` was executed |
| 353 | // for all threads and therefore we can mark the classes as visibly initialized. |
| 354 | std::atomic<ssize_t> thread_visibility_counter_; |
| 355 | |
| 356 | // List of barries to `Pass()` for threads that wait for the callback to complete. |
| 357 | std::forward_list<Barrier*> barriers_; |
| 358 | }; |
| 359 | |
| 360 | void ClassLinker::MakeInitializedClassesVisiblyInitialized(Thread* self, bool wait) { |
| 361 | if (kRuntimeISA == InstructionSet::kX86 || kRuntimeISA == InstructionSet::kX86_64) { |
| 362 | return; // Nothing to do. Thanks to the x86 memory model classes skip the initialized status. |
| 363 | } |
| 364 | std::optional<Barrier> maybe_barrier; // Avoid constructing the Barrier for `wait == false`. |
| 365 | if (wait) { |
| 366 | maybe_barrier.emplace(0); |
| 367 | } |
| 368 | int wait_count = 0; |
| 369 | VisiblyInitializedCallback* callback = nullptr; |
| 370 | { |
| 371 | MutexLock lock(self, visibly_initialized_callback_lock_); |
| 372 | if (visibly_initialized_callback_ != nullptr && !visibly_initialized_callback_->IsEmpty()) { |
| 373 | callback = visibly_initialized_callback_.release(); |
| 374 | running_visibly_initialized_callbacks_.push_front(*callback); |
| 375 | } |
| 376 | if (wait) { |
| 377 | DCHECK(maybe_barrier.has_value()); |
| 378 | Barrier* barrier = std::addressof(*maybe_barrier); |
| 379 | for (VisiblyInitializedCallback& cb : running_visibly_initialized_callbacks_) { |
| 380 | cb.AddBarrier(barrier); |
| 381 | ++wait_count; |
| 382 | } |
| 383 | } |
| 384 | } |
| 385 | if (callback != nullptr) { |
| 386 | callback->MakeVisible(self); |
| 387 | } |
| 388 | if (wait_count != 0) { |
| 389 | DCHECK(maybe_barrier.has_value()); |
| 390 | maybe_barrier->Increment(self, wait_count); |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | void ClassLinker::VisiblyInitializedCallbackDone(Thread* self, |
| 395 | VisiblyInitializedCallback* callback) { |
| 396 | MutexLock lock(self, visibly_initialized_callback_lock_); |
| 397 | // Pass the barriers if requested. |
| 398 | for (Barrier* barrier : callback->GetAndClearBarriers()) { |
| 399 | barrier->Pass(self); |
| 400 | } |
| 401 | // Remove the callback from the list of running callbacks. |
| 402 | auto before = running_visibly_initialized_callbacks_.before_begin(); |
| 403 | auto it = running_visibly_initialized_callbacks_.begin(); |
| 404 | DCHECK(it != running_visibly_initialized_callbacks_.end()); |
| 405 | while (std::addressof(*it) != callback) { |
| 406 | before = it; |
| 407 | ++it; |
| 408 | DCHECK(it != running_visibly_initialized_callbacks_.end()); |
| 409 | } |
| 410 | running_visibly_initialized_callbacks_.erase_after(before); |
| 411 | // Reuse or destroy the callback object. |
| 412 | if (visibly_initialized_callback_ == nullptr) { |
| 413 | visibly_initialized_callback_.reset(callback); |
| 414 | } else { |
| 415 | delete callback; |
| 416 | } |
| 417 | } |
| 418 | |
Alex Light | fb11957 | 2019-09-18 15:04:53 -0700 | [diff] [blame] | 419 | void ClassLinker::ForceClassInitialized(Thread* self, Handle<mirror::Class> klass) { |
| 420 | ClassLinker::VisiblyInitializedCallback* cb = MarkClassInitialized(self, klass); |
| 421 | if (cb != nullptr) { |
| 422 | cb->MakeVisible(self); |
| 423 | } |
| 424 | ScopedThreadSuspension sts(self, ThreadState::kSuspended); |
| 425 | MakeInitializedClassesVisiblyInitialized(self, /*wait=*/true); |
| 426 | } |
| 427 | |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 428 | ClassLinker::VisiblyInitializedCallback* ClassLinker::MarkClassInitialized( |
| 429 | Thread* self, Handle<mirror::Class> klass) { |
| 430 | if (kRuntimeISA == InstructionSet::kX86 || kRuntimeISA == InstructionSet::kX86_64) { |
| 431 | // Thanks to the x86 memory model, we do not need any memory fences and |
| 432 | // we can immediately mark the class as visibly initialized. |
| 433 | mirror::Class::SetStatus(klass, ClassStatus::kVisiblyInitialized, self); |
Vladimir Marko | 86c8752 | 2020-05-11 16:55:55 +0100 | [diff] [blame] | 434 | FixupStaticTrampolines(self, klass.Get()); |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 435 | return nullptr; |
| 436 | } |
| 437 | if (Runtime::Current()->IsActiveTransaction()) { |
| 438 | // Transactions are single-threaded, so we can mark the class as visibly intialized. |
| 439 | // (Otherwise we'd need to track the callback's entry in the transaction for rollback.) |
| 440 | mirror::Class::SetStatus(klass, ClassStatus::kVisiblyInitialized, self); |
Vladimir Marko | 86c8752 | 2020-05-11 16:55:55 +0100 | [diff] [blame] | 441 | FixupStaticTrampolines(self, klass.Get()); |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 442 | return nullptr; |
| 443 | } |
| 444 | mirror::Class::SetStatus(klass, ClassStatus::kInitialized, self); |
| 445 | MutexLock lock(self, visibly_initialized_callback_lock_); |
| 446 | if (visibly_initialized_callback_ == nullptr) { |
| 447 | visibly_initialized_callback_.reset(new VisiblyInitializedCallback(this)); |
| 448 | } |
| 449 | DCHECK(!visibly_initialized_callback_->IsFull()); |
| 450 | visibly_initialized_callback_->AddClass(self, klass.Get()); |
| 451 | |
| 452 | if (visibly_initialized_callback_->IsFull()) { |
| 453 | VisiblyInitializedCallback* callback = visibly_initialized_callback_.release(); |
| 454 | running_visibly_initialized_callbacks_.push_front(*callback); |
| 455 | return callback; |
| 456 | } else { |
| 457 | return nullptr; |
| 458 | } |
| 459 | } |
| 460 | |
Vladimir Marko | 86c8752 | 2020-05-11 16:55:55 +0100 | [diff] [blame] | 461 | const void* ClassLinker::RegisterNative( |
| 462 | Thread* self, ArtMethod* method, const void* native_method) { |
| 463 | CHECK(method->IsNative()) << method->PrettyMethod(); |
| 464 | CHECK(native_method != nullptr) << method->PrettyMethod(); |
| 465 | void* new_native_method = nullptr; |
| 466 | Runtime* runtime = Runtime::Current(); |
| 467 | runtime->GetRuntimeCallbacks()->RegisterNativeMethod(method, |
| 468 | native_method, |
| 469 | /*out*/&new_native_method); |
| 470 | if (method->IsCriticalNative()) { |
| 471 | MutexLock lock(self, critical_native_code_with_clinit_check_lock_); |
| 472 | // Remove old registered method if any. |
| 473 | auto it = critical_native_code_with_clinit_check_.find(method); |
| 474 | if (it != critical_native_code_with_clinit_check_.end()) { |
| 475 | critical_native_code_with_clinit_check_.erase(it); |
| 476 | } |
| 477 | // To ensure correct memory visibility, we need the class to be visibly |
| 478 | // initialized before we can set the JNI entrypoint. |
| 479 | if (method->GetDeclaringClass()->IsVisiblyInitialized()) { |
| 480 | method->SetEntryPointFromJni(new_native_method); |
| 481 | } else { |
| 482 | critical_native_code_with_clinit_check_.emplace(method, new_native_method); |
| 483 | } |
| 484 | } else { |
| 485 | method->SetEntryPointFromJni(new_native_method); |
| 486 | } |
| 487 | return new_native_method; |
| 488 | } |
| 489 | |
| 490 | void ClassLinker::UnregisterNative(Thread* self, ArtMethod* method) { |
| 491 | CHECK(method->IsNative()) << method->PrettyMethod(); |
| 492 | // Restore stub to lookup native pointer via dlsym. |
| 493 | if (method->IsCriticalNative()) { |
| 494 | MutexLock lock(self, critical_native_code_with_clinit_check_lock_); |
| 495 | auto it = critical_native_code_with_clinit_check_.find(method); |
| 496 | if (it != critical_native_code_with_clinit_check_.end()) { |
| 497 | critical_native_code_with_clinit_check_.erase(it); |
| 498 | } |
| 499 | method->SetEntryPointFromJni(GetJniDlsymLookupCriticalStub()); |
| 500 | } else { |
| 501 | method->SetEntryPointFromJni(GetJniDlsymLookupStub()); |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | const void* ClassLinker::GetRegisteredNative(Thread* self, ArtMethod* method) { |
| 506 | if (method->IsCriticalNative()) { |
| 507 | MutexLock lock(self, critical_native_code_with_clinit_check_lock_); |
| 508 | auto it = critical_native_code_with_clinit_check_.find(method); |
| 509 | if (it != critical_native_code_with_clinit_check_.end()) { |
| 510 | return it->second; |
| 511 | } |
| 512 | const void* native_code = method->GetEntryPointFromJni(); |
| 513 | return IsJniDlsymLookupCriticalStub(native_code) ? nullptr : native_code; |
| 514 | } else { |
| 515 | const void* native_code = method->GetEntryPointFromJni(); |
| 516 | return IsJniDlsymLookupStub(native_code) ? nullptr : native_code; |
| 517 | } |
| 518 | } |
| 519 | |
Andreas Gampe | 7b3063b | 2019-01-07 14:12:52 -0800 | [diff] [blame] | 520 | void ClassLinker::ThrowEarlierClassFailure(ObjPtr<mirror::Class> c, |
| 521 | bool wrap_in_no_class_def, |
| 522 | bool log) { |
Elliott Hughes | 5c59994 | 2012-06-13 16:45:05 -0700 | [diff] [blame] | 523 | // The class failed to initialize on a previous attempt, so we want to throw |
| 524 | // a NoClassDefFoundError (v2 2.17.5). The exception to this rule is if we |
| 525 | // failed in verification, in which case v2 5.4.1 says we need to re-throw |
| 526 | // the previous error. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 527 | Runtime* const runtime = Runtime::Current(); |
| 528 | if (!runtime->IsAotCompiler()) { // Give info if this occurs at runtime. |
Andreas Gampe | 3d6b470 | 2015-09-21 08:35:52 -0700 | [diff] [blame] | 529 | std::string extra; |
Vladimir Marko | bb206de | 2019-03-28 10:30:32 +0000 | [diff] [blame] | 530 | ObjPtr<mirror::Object> verify_error = GetVerifyError(c); |
| 531 | if (verify_error != nullptr) { |
Andreas Gampe | 369c851 | 2016-01-28 15:31:39 -0800 | [diff] [blame] | 532 | if (verify_error->IsClass()) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 533 | extra = mirror::Class::PrettyDescriptor(verify_error->AsClass()); |
Andreas Gampe | 369c851 | 2016-01-28 15:31:39 -0800 | [diff] [blame] | 534 | } else { |
| 535 | extra = verify_error->AsThrowable()->Dump(); |
| 536 | } |
Andreas Gampe | 3d6b470 | 2015-09-21 08:35:52 -0700 | [diff] [blame] | 537 | } |
Andreas Gampe | 7b3063b | 2019-01-07 14:12:52 -0800 | [diff] [blame] | 538 | if (log) { |
| 539 | LOG(INFO) << "Rejecting re-init on previously-failed class " << c->PrettyClass() |
| 540 | << ": " << extra; |
| 541 | } |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 542 | } |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 543 | |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 544 | CHECK(c->IsErroneous()) << c->PrettyClass() << " " << c->GetStatus(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 545 | Thread* self = Thread::Current(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 546 | if (runtime->IsAotCompiler()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 547 | // At compile time, accurate errors and NCDFE are disabled to speed compilation. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 548 | ObjPtr<mirror::Throwable> pre_allocated = runtime->GetPreAllocatedNoClassDefFoundError(); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 549 | self->SetException(pre_allocated); |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 550 | } else { |
Vladimir Marko | bb206de | 2019-03-28 10:30:32 +0000 | [diff] [blame] | 551 | ObjPtr<mirror::Object> verify_error = GetVerifyError(c); |
| 552 | if (verify_error != nullptr) { |
Andreas Gampe | cb08695 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 553 | // Rethrow stored error. |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 554 | HandleEarlierVerifyError(self, this, c); |
Andreas Gampe | cb08695 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 555 | } |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 556 | // TODO This might be wrong if we hit an OOME while allocating the ClassExt. In that case we |
| 557 | // might have meant to go down the earlier if statement with the original error but it got |
| 558 | // swallowed by the OOM so we end up here. |
Vladimir Marko | bb206de | 2019-03-28 10:30:32 +0000 | [diff] [blame] | 559 | if (verify_error == nullptr || wrap_in_no_class_def) { |
Andreas Gampe | cb08695 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 560 | // If there isn't a recorded earlier error, or this is a repeat throw from initialization, |
| 561 | // the top-level exception must be a NoClassDefFoundError. The potentially already pending |
| 562 | // exception will be a cause. |
| 563 | self->ThrowNewWrappedException("Ljava/lang/NoClassDefFoundError;", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 564 | c->PrettyDescriptor().c_str()); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 565 | } |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 566 | } |
| 567 | } |
| 568 | |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 569 | static void VlogClassInitializationFailure(Handle<mirror::Class> klass) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 570 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 571 | if (VLOG_IS_ON(class_linker)) { |
| 572 | std::string temp; |
| 573 | LOG(INFO) << "Failed to initialize class " << klass->GetDescriptor(&temp) << " from " |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 574 | << klass->GetLocation() << "\n" << Thread::Current()->GetException()->Dump(); |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 575 | } |
| 576 | } |
| 577 | |
| 578 | static void WrapExceptionInInitializer(Handle<mirror::Class> klass) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 579 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 580 | Thread* self = Thread::Current(); |
| 581 | JNIEnv* env = self->GetJniEnv(); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 582 | |
| 583 | ScopedLocalRef<jthrowable> cause(env, env->ExceptionOccurred()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 584 | CHECK(cause.get() != nullptr); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 585 | |
David Srbecky | 346fd96 | 2020-07-27 16:51:00 +0100 | [diff] [blame] | 586 | // Boot classpath classes should not fail initialization. This is a consistency debug check. |
| 587 | // This cannot in general be guaranteed, but in all likelihood leads to breakage down the line. |
Andreas Gampe | 1e8a395 | 2016-11-30 10:13:19 -0800 | [diff] [blame] | 588 | if (klass->GetClassLoader() == nullptr && !Runtime::Current()->IsAotCompiler()) { |
Andreas Gampe | 22f71d2 | 2016-11-21 10:10:08 -0800 | [diff] [blame] | 589 | std::string tmp; |
Alex Light | 5047d9f | 2018-03-09 15:44:31 -0800 | [diff] [blame] | 590 | // We want to LOG(FATAL) on debug builds since this really shouldn't be happening but we need to |
| 591 | // make sure to only do it if we don't have AsyncExceptions being thrown around since those |
| 592 | // could have caused the error. |
| 593 | bool known_impossible = kIsDebugBuild && !Runtime::Current()->AreAsyncExceptionsThrown(); |
| 594 | LOG(known_impossible ? FATAL : WARNING) << klass->GetDescriptor(&tmp) |
| 595 | << " failed initialization: " |
| 596 | << self->GetException()->Dump(); |
Andreas Gampe | 22f71d2 | 2016-11-21 10:10:08 -0800 | [diff] [blame] | 597 | } |
| 598 | |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 599 | env->ExceptionClear(); |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 600 | bool is_error = env->IsInstanceOf(cause.get(), WellKnownClasses::java_lang_Error); |
| 601 | env->Throw(cause.get()); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 602 | |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 603 | // We only wrap non-Error exceptions; an Error can just be used as-is. |
| 604 | if (!is_error) { |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 605 | self->ThrowNewWrappedException("Ljava/lang/ExceptionInInitializerError;", nullptr); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 606 | } |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 607 | VlogClassInitializationFailure(klass); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 608 | } |
| 609 | |
Andreas Gampe | 87658f3 | 2019-04-18 18:39:02 +0000 | [diff] [blame] | 610 | ClassLinker::ClassLinker(InternTable* intern_table, bool fast_class_not_found_exceptions) |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 611 | : boot_class_table_(new ClassTable()), |
| 612 | failed_dex_cache_class_lookups_(0), |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 613 | class_roots_(nullptr), |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 614 | find_array_class_cache_next_victim_(0), |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 615 | init_done_(false), |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 616 | log_new_roots_(false), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 617 | intern_table_(intern_table), |
Andreas Gampe | 87658f3 | 2019-04-18 18:39:02 +0000 | [diff] [blame] | 618 | fast_class_not_found_exceptions_(fast_class_not_found_exceptions), |
Vladimir Marko | 7dac864 | 2019-11-06 17:09:30 +0000 | [diff] [blame] | 619 | jni_dlsym_lookup_trampoline_(nullptr), |
Vladimir Marko | fa458ac | 2020-02-12 14:08:07 +0000 | [diff] [blame] | 620 | jni_dlsym_lookup_critical_trampoline_(nullptr), |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 621 | quick_resolution_trampoline_(nullptr), |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 622 | quick_imt_conflict_trampoline_(nullptr), |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 623 | quick_generic_jni_trampoline_(nullptr), |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 624 | quick_to_interpreter_bridge_trampoline_(nullptr), |
Nicolas Geoffray | c39af94 | 2021-01-25 08:43:57 +0000 | [diff] [blame] | 625 | nterp_trampoline_(nullptr), |
Andreas Gampe | c1ac9ee | 2017-07-24 22:35:49 -0700 | [diff] [blame] | 626 | image_pointer_size_(kRuntimePointerSize), |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 627 | visibly_initialized_callback_lock_("visibly initialized callback lock"), |
| 628 | visibly_initialized_callback_(nullptr), |
Vladimir Marko | 86c8752 | 2020-05-11 16:55:55 +0100 | [diff] [blame] | 629 | critical_native_code_with_clinit_check_lock_("critical native code with clinit check lock"), |
| 630 | critical_native_code_with_clinit_check_(), |
Andreas Gampe | 7dface3 | 2017-07-25 21:32:59 -0700 | [diff] [blame] | 631 | cha_(Runtime::Current()->IsAotCompiler() ? nullptr : new ClassHierarchyAnalysis()) { |
| 632 | // For CHA disabled during Aot, see b/34193647. |
| 633 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 634 | CHECK(intern_table_ != nullptr); |
Andreas Gampe | 8ac7595 | 2015-06-02 21:01:45 -0700 | [diff] [blame] | 635 | static_assert(kFindArrayCacheSize == arraysize(find_array_class_cache_), |
| 636 | "Array cache size wrong."); |
| 637 | std::fill_n(find_array_class_cache_, kFindArrayCacheSize, GcRoot<mirror::Class>(nullptr)); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 638 | } |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 639 | |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 640 | void ClassLinker::CheckSystemClass(Thread* self, Handle<mirror::Class> c1, const char* descriptor) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 641 | ObjPtr<mirror::Class> c2 = FindSystemClass(self, descriptor); |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 642 | if (c2 == nullptr) { |
| 643 | LOG(FATAL) << "Could not find class " << descriptor; |
| 644 | UNREACHABLE(); |
| 645 | } |
| 646 | if (c1.Get() != c2) { |
| 647 | std::ostringstream os1, os2; |
| 648 | c1->DumpClass(os1, mirror::Class::kDumpClassFullDetail); |
| 649 | c2->DumpClass(os2, mirror::Class::kDumpClassFullDetail); |
| 650 | LOG(FATAL) << "InitWithoutImage: Class mismatch for " << descriptor |
| 651 | << ". This is most likely the result of a broken build. Make sure that " |
| 652 | << "libcore and art projects match.\n\n" |
| 653 | << os1.str() << "\n\n" << os2.str(); |
| 654 | UNREACHABLE(); |
| 655 | } |
| 656 | } |
| 657 | |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 658 | bool ClassLinker::InitWithoutImage(std::vector<std::unique_ptr<const DexFile>> boot_class_path, |
| 659 | std::string* error_msg) { |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 660 | VLOG(startup) << "ClassLinker::Init"; |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 661 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 662 | Thread* const self = Thread::Current(); |
| 663 | Runtime* const runtime = Runtime::Current(); |
| 664 | gc::Heap* const heap = runtime->GetHeap(); |
| 665 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 666 | CHECK(!heap->HasBootImageSpace()) << "Runtime has image. We should use it."; |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 667 | CHECK(!init_done_); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 668 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 669 | // Use the pointer size from the runtime since we are probably creating the image. |
| 670 | image_pointer_size_ = InstructionSetPointerSize(runtime->GetInstructionSet()); |
| 671 | |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 672 | // java_lang_Class comes first, it's needed for AllocClass |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 673 | // 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] | 674 | heap->IncrementDisableMovingGC(self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 675 | StackHandleScope<64> hs(self); // 64 is picked arbitrarily. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 676 | auto class_class_size = mirror::Class::ClassClassSize(image_pointer_size_); |
Mathieu Chartier | d7a7f2f | 2018-09-07 11:57:18 -0700 | [diff] [blame] | 677 | // Allocate the object as non-movable so that there are no cases where Object::IsClass returns |
| 678 | // the incorrect result when comparing to-space vs from-space. |
Vladimir Marko | d7e9bbf | 2019-03-28 13:18:57 +0000 | [diff] [blame] | 679 | Handle<mirror::Class> java_lang_Class(hs.NewHandle(ObjPtr<mirror::Class>::DownCast( |
Vladimir Marko | 991cd5c | 2019-05-30 14:23:39 +0100 | [diff] [blame] | 680 | heap->AllocNonMovableObject(self, nullptr, class_class_size, VoidFunctor())))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 681 | CHECK(java_lang_Class != nullptr); |
Vladimir Marko | 317892b | 2018-05-31 11:11:32 +0100 | [diff] [blame] | 682 | java_lang_Class->SetClassFlags(mirror::kClassFlagClass); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 683 | java_lang_Class->SetClass(java_lang_Class.Get()); |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 684 | if (kUseBakerReadBarrier) { |
| 685 | java_lang_Class->AssertReadBarrierState(); |
Hiroshi Yamauchi | 9d04a20 | 2014-01-31 13:35:49 -0800 | [diff] [blame] | 686 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 687 | java_lang_Class->SetClassSize(class_class_size); |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 688 | java_lang_Class->SetPrimitiveType(Primitive::kPrimNot); |
Mathieu Chartier | 1d27b34 | 2014-01-28 12:51:09 -0800 | [diff] [blame] | 689 | heap->DecrementDisableMovingGC(self); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 690 | // AllocClass(ObjPtr<mirror::Class>) can now be used |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 691 | |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 692 | // Class[] is used for reflection support. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 693 | 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] | 694 | Handle<mirror::Class> class_array_class(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 695 | AllocClass(self, java_lang_Class.Get(), class_array_class_size))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 696 | class_array_class->SetComponentType(java_lang_Class.Get()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 697 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 698 | // 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] | 699 | Handle<mirror::Class> java_lang_Object(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 700 | AllocClass(self, java_lang_Class.Get(), mirror::Object::ClassSize(image_pointer_size_)))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 701 | CHECK(java_lang_Object != nullptr); |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 702 | // backfill Object as the super class of Class. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 703 | java_lang_Class->SetSuperClass(java_lang_Object.Get()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 704 | mirror::Class::SetStatus(java_lang_Object, ClassStatus::kLoaded, self); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 705 | |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 706 | java_lang_Object->SetObjectSize(sizeof(mirror::Object)); |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 707 | // Allocate in non-movable so that it's possible to check if a JNI weak global ref has been |
| 708 | // cleared without triggering the read barrier and unintentionally mark the sentinel alive. |
Vladimir Marko | 991cd5c | 2019-05-30 14:23:39 +0100 | [diff] [blame] | 709 | runtime->SetSentinel(heap->AllocNonMovableObject(self, |
| 710 | java_lang_Object.Get(), |
| 711 | java_lang_Object->GetObjectSize(), |
| 712 | VoidFunctor())); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 713 | |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 714 | // Initialize the SubtypeCheck bitstring for java.lang.Object and java.lang.Class. |
Vladimir Marko | 305c38b | 2018-02-14 11:50:07 +0000 | [diff] [blame] | 715 | if (kBitstringSubtypeCheckEnabled) { |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 716 | // It might seem the lock here is unnecessary, however all the SubtypeCheck |
| 717 | // functions are annotated to require locks all the way down. |
| 718 | // |
| 719 | // We take the lock here to avoid using NO_THREAD_SAFETY_ANALYSIS. |
| 720 | MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_); |
Vladimir Marko | 38b8b25 | 2018-01-02 19:07:06 +0000 | [diff] [blame] | 721 | SubtypeCheck<ObjPtr<mirror::Class>>::EnsureInitialized(java_lang_Object.Get()); |
| 722 | SubtypeCheck<ObjPtr<mirror::Class>>::EnsureInitialized(java_lang_Class.Get()); |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 723 | } |
| 724 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 725 | // Object[] next to hold class roots. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 726 | Handle<mirror::Class> object_array_class(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 727 | AllocClass(self, java_lang_Class.Get(), |
| 728 | mirror::ObjectArray<mirror::Object>::ClassSize(image_pointer_size_)))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 729 | object_array_class->SetComponentType(java_lang_Object.Get()); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 730 | |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 731 | // Setup java.lang.String. |
| 732 | // |
| 733 | // We make this class non-movable for the unlikely case where it were to be |
| 734 | // moved by a sticky-bit (minor) collection when using the Generational |
| 735 | // Concurrent Copying (CC) collector, potentially creating a stale reference |
| 736 | // in the `klass_` field of one of its instances allocated in the Large-Object |
| 737 | // Space (LOS) -- see the comment about the dirty card scanning logic in |
| 738 | // art::gc::collector::ConcurrentCopying::MarkingPhase. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 739 | Handle<mirror::Class> java_lang_String(hs.NewHandle( |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 740 | AllocClass</* kMovable= */ false>( |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 741 | self, java_lang_Class.Get(), mirror::String::ClassSize(image_pointer_size_)))); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 742 | java_lang_String->SetStringClass(); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 743 | mirror::Class::SetStatus(java_lang_String, ClassStatus::kResolved, self); |
Jesse Wilson | 1415074 | 2011-07-29 19:04:44 -0400 | [diff] [blame] | 744 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 745 | // Setup java.lang.ref.Reference. |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 746 | Handle<mirror::Class> java_lang_ref_Reference(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 747 | AllocClass(self, java_lang_Class.Get(), mirror::Reference::ClassSize(image_pointer_size_)))); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 748 | java_lang_ref_Reference->SetObjectSize(mirror::Reference::InstanceSize()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 749 | mirror::Class::SetStatus(java_lang_ref_Reference, ClassStatus::kResolved, self); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 750 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 751 | // 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] | 752 | class_roots_ = GcRoot<mirror::ObjectArray<mirror::Class>>( |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 753 | mirror::ObjectArray<mirror::Class>::Alloc(self, |
| 754 | object_array_class.Get(), |
| 755 | static_cast<int32_t>(ClassRoot::kMax))); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 756 | CHECK(!class_roots_.IsNull()); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 757 | SetClassRoot(ClassRoot::kJavaLangClass, java_lang_Class.Get()); |
| 758 | SetClassRoot(ClassRoot::kJavaLangObject, java_lang_Object.Get()); |
| 759 | SetClassRoot(ClassRoot::kClassArrayClass, class_array_class.Get()); |
| 760 | SetClassRoot(ClassRoot::kObjectArrayClass, object_array_class.Get()); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 761 | SetClassRoot(ClassRoot::kJavaLangString, java_lang_String.Get()); |
| 762 | SetClassRoot(ClassRoot::kJavaLangRefReference, java_lang_ref_Reference.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 763 | |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 764 | // Fill in the empty iftable. Needs to be done after the kObjectArrayClass root is set. |
| 765 | java_lang_Object->SetIfTable(AllocIfTable(self, 0)); |
| 766 | |
Vladimir Marko | 0261055 | 2018-06-04 14:38:00 +0100 | [diff] [blame] | 767 | // Create array interface entries to populate once we can load system classes. |
| 768 | object_array_class->SetIfTable(AllocIfTable(self, 2)); |
| 769 | DCHECK_EQ(GetArrayIfTable(), object_array_class->GetIfTable()); |
| 770 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 771 | // Setup the primitive type classes. |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 772 | CreatePrimitiveClass(self, Primitive::kPrimBoolean, ClassRoot::kPrimitiveBoolean); |
| 773 | CreatePrimitiveClass(self, Primitive::kPrimByte, ClassRoot::kPrimitiveByte); |
| 774 | CreatePrimitiveClass(self, Primitive::kPrimChar, ClassRoot::kPrimitiveChar); |
| 775 | CreatePrimitiveClass(self, Primitive::kPrimShort, ClassRoot::kPrimitiveShort); |
| 776 | CreatePrimitiveClass(self, Primitive::kPrimInt, ClassRoot::kPrimitiveInt); |
| 777 | CreatePrimitiveClass(self, Primitive::kPrimLong, ClassRoot::kPrimitiveLong); |
| 778 | CreatePrimitiveClass(self, Primitive::kPrimFloat, ClassRoot::kPrimitiveFloat); |
| 779 | CreatePrimitiveClass(self, Primitive::kPrimDouble, ClassRoot::kPrimitiveDouble); |
| 780 | CreatePrimitiveClass(self, Primitive::kPrimVoid, ClassRoot::kPrimitiveVoid); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 781 | |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 782 | // Allocate the primitive array classes. We need only the native pointer |
| 783 | // array at this point (int[] or long[], depending on architecture) but |
| 784 | // we shall perform the same setup steps for all primitive array classes. |
| 785 | AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveBoolean, ClassRoot::kBooleanArrayClass); |
| 786 | AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveByte, ClassRoot::kByteArrayClass); |
| 787 | AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveChar, ClassRoot::kCharArrayClass); |
| 788 | AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveShort, ClassRoot::kShortArrayClass); |
| 789 | AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveInt, ClassRoot::kIntArrayClass); |
| 790 | AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveLong, ClassRoot::kLongArrayClass); |
| 791 | AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveFloat, ClassRoot::kFloatArrayClass); |
| 792 | AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveDouble, ClassRoot::kDoubleArrayClass); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 793 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 794 | // now that these are registered, we can use AllocClass() and AllocObjectArray |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 795 | |
Ian Rogers | 52813c9 | 2012-10-11 11:50:38 -0700 | [diff] [blame] | 796 | // Set up DexCache. This cannot be done later since AppendToBootClassPath calls AllocDexCache. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 797 | Handle<mirror::Class> java_lang_DexCache(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 798 | AllocClass(self, java_lang_Class.Get(), mirror::DexCache::ClassSize(image_pointer_size_)))); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 799 | SetClassRoot(ClassRoot::kJavaLangDexCache, java_lang_DexCache.Get()); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 800 | java_lang_DexCache->SetDexCacheClass(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 801 | java_lang_DexCache->SetObjectSize(mirror::DexCache::InstanceSize()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 802 | mirror::Class::SetStatus(java_lang_DexCache, ClassStatus::kResolved, self); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 803 | |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 804 | |
| 805 | // Setup dalvik.system.ClassExt |
| 806 | Handle<mirror::Class> dalvik_system_ClassExt(hs.NewHandle( |
| 807 | AllocClass(self, java_lang_Class.Get(), mirror::ClassExt::ClassSize(image_pointer_size_)))); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 808 | SetClassRoot(ClassRoot::kDalvikSystemClassExt, dalvik_system_ClassExt.Get()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 809 | mirror::Class::SetStatus(dalvik_system_ClassExt, ClassStatus::kResolved, self); |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 810 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 811 | // Set up array classes for string, field, method |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 812 | Handle<mirror::Class> object_array_string(hs.NewHandle( |
| 813 | AllocClass(self, java_lang_Class.Get(), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 814 | mirror::ObjectArray<mirror::String>::ClassSize(image_pointer_size_)))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 815 | object_array_string->SetComponentType(java_lang_String.Get()); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 816 | SetClassRoot(ClassRoot::kJavaLangStringArrayClass, object_array_string.Get()); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 817 | |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 818 | LinearAlloc* linear_alloc = runtime->GetLinearAlloc(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 819 | // Create runtime resolution and imt conflict methods. |
| 820 | runtime->SetResolutionMethod(runtime->CreateResolutionMethod()); |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 821 | runtime->SetImtConflictMethod(runtime->CreateImtConflictMethod(linear_alloc)); |
| 822 | runtime->SetImtUnimplementedMethod(runtime->CreateImtConflictMethod(linear_alloc)); |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 823 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 824 | // Setup boot_class_path_ and register class_path now that we can use AllocObjectArray to create |
| 825 | // DexCache instances. Needs to be after String, Field, Method arrays since AllocDexCache uses |
| 826 | // these roots. |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 827 | if (boot_class_path.empty()) { |
| 828 | *error_msg = "Boot classpath is empty."; |
| 829 | return false; |
| 830 | } |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 831 | for (auto& dex_file : boot_class_path) { |
Mathieu Chartier | 0a19e21 | 2019-11-27 14:35:24 -0800 | [diff] [blame] | 832 | if (dex_file == nullptr) { |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 833 | *error_msg = "Null dex file."; |
| 834 | return false; |
| 835 | } |
Mathieu Chartier | 0a19e21 | 2019-11-27 14:35:24 -0800 | [diff] [blame] | 836 | AppendToBootClassPath(self, dex_file.get()); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 837 | boot_dex_files_.push_back(std::move(dex_file)); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 838 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 839 | |
| 840 | // now we can use FindSystemClass |
| 841 | |
Dmitry Petrochenko | f0972a4 | 2014-05-16 17:43:39 +0700 | [diff] [blame] | 842 | // Set up GenericJNI entrypoint. That is mainly a hack for common_compiler_test.h so that |
| 843 | // we do not need friend classes or a publicly exposed setter. |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 844 | quick_generic_jni_trampoline_ = GetQuickGenericJniStub(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 845 | if (!runtime->IsAotCompiler()) { |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 846 | // We need to set up the generic trampolines since we don't have an image. |
Vladimir Marko | 7dac864 | 2019-11-06 17:09:30 +0000 | [diff] [blame] | 847 | jni_dlsym_lookup_trampoline_ = GetJniDlsymLookupStub(); |
Vladimir Marko | fa458ac | 2020-02-12 14:08:07 +0000 | [diff] [blame] | 848 | jni_dlsym_lookup_critical_trampoline_ = GetJniDlsymLookupCriticalStub(); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 849 | quick_resolution_trampoline_ = GetQuickResolutionStub(); |
| 850 | quick_imt_conflict_trampoline_ = GetQuickImtConflictStub(); |
Vladimir Marko | 7dac864 | 2019-11-06 17:09:30 +0000 | [diff] [blame] | 851 | quick_generic_jni_trampoline_ = GetQuickGenericJniStub(); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 852 | quick_to_interpreter_bridge_trampoline_ = GetQuickToInterpreterBridge(); |
Nicolas Geoffray | c39af94 | 2021-01-25 08:43:57 +0000 | [diff] [blame] | 853 | nterp_trampoline_ = interpreter::GetNterpEntryPoint(); |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 854 | } |
Dmitry Petrochenko | f0972a4 | 2014-05-16 17:43:39 +0700 | [diff] [blame] | 855 | |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 856 | // 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] | 857 | mirror::Class::SetStatus(java_lang_Object, ClassStatus::kNotReady, self); |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 858 | CheckSystemClass(self, java_lang_Object, "Ljava/lang/Object;"); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 859 | CHECK_EQ(java_lang_Object->GetObjectSize(), mirror::Object::InstanceSize()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 860 | mirror::Class::SetStatus(java_lang_String, ClassStatus::kNotReady, self); |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 861 | CheckSystemClass(self, java_lang_String, "Ljava/lang/String;"); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 862 | mirror::Class::SetStatus(java_lang_DexCache, ClassStatus::kNotReady, self); |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 863 | CheckSystemClass(self, java_lang_DexCache, "Ljava/lang/DexCache;"); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 864 | CHECK_EQ(java_lang_DexCache->GetObjectSize(), mirror::DexCache::InstanceSize()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 865 | mirror::Class::SetStatus(dalvik_system_ClassExt, ClassStatus::kNotReady, self); |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 866 | CheckSystemClass(self, dalvik_system_ClassExt, "Ldalvik/system/ClassExt;"); |
| 867 | CHECK_EQ(dalvik_system_ClassExt->GetObjectSize(), mirror::ClassExt::InstanceSize()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 868 | |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 869 | // Run Class through FindSystemClass. This initializes the dex_cache_ fields and register it |
| 870 | // in class_table_. |
| 871 | CheckSystemClass(self, java_lang_Class, "Ljava/lang/Class;"); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 872 | |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 873 | // Setup core array classes, i.e. Object[], String[] and Class[] and primitive |
| 874 | // arrays - can't be done until Object has a vtable and component classes are loaded. |
| 875 | FinishCoreArrayClassSetup(ClassRoot::kObjectArrayClass); |
| 876 | FinishCoreArrayClassSetup(ClassRoot::kClassArrayClass); |
| 877 | FinishCoreArrayClassSetup(ClassRoot::kJavaLangStringArrayClass); |
| 878 | FinishCoreArrayClassSetup(ClassRoot::kBooleanArrayClass); |
| 879 | FinishCoreArrayClassSetup(ClassRoot::kByteArrayClass); |
| 880 | FinishCoreArrayClassSetup(ClassRoot::kCharArrayClass); |
| 881 | FinishCoreArrayClassSetup(ClassRoot::kShortArrayClass); |
| 882 | FinishCoreArrayClassSetup(ClassRoot::kIntArrayClass); |
| 883 | FinishCoreArrayClassSetup(ClassRoot::kLongArrayClass); |
| 884 | FinishCoreArrayClassSetup(ClassRoot::kFloatArrayClass); |
| 885 | FinishCoreArrayClassSetup(ClassRoot::kDoubleArrayClass); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 886 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 887 | // Setup the single, global copy of "iftable". |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 888 | auto java_lang_Cloneable = hs.NewHandle(FindSystemClass(self, "Ljava/lang/Cloneable;")); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 889 | CHECK(java_lang_Cloneable != nullptr); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 890 | auto java_io_Serializable = hs.NewHandle(FindSystemClass(self, "Ljava/io/Serializable;")); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 891 | CHECK(java_io_Serializable != nullptr); |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 892 | // We assume that Cloneable/Serializable don't have superinterfaces -- normally we'd have to |
| 893 | // crawl up and explicitly list all of the supers as well. |
Vladimir Marko | 0261055 | 2018-06-04 14:38:00 +0100 | [diff] [blame] | 894 | object_array_class->GetIfTable()->SetInterface(0, java_lang_Cloneable.Get()); |
| 895 | object_array_class->GetIfTable()->SetInterface(1, java_io_Serializable.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 896 | |
David Srbecky | 346fd96 | 2020-07-27 16:51:00 +0100 | [diff] [blame] | 897 | // Check Class[] and Object[]'s interfaces. GetDirectInterface may cause thread suspension. |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 898 | CHECK_EQ(java_lang_Cloneable.Get(), |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 899 | mirror::Class::GetDirectInterface(self, class_array_class.Get(), 0)); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 900 | CHECK_EQ(java_io_Serializable.Get(), |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 901 | mirror::Class::GetDirectInterface(self, class_array_class.Get(), 1)); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 902 | CHECK_EQ(java_lang_Cloneable.Get(), |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 903 | mirror::Class::GetDirectInterface(self, object_array_class.Get(), 0)); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 904 | CHECK_EQ(java_io_Serializable.Get(), |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 905 | mirror::Class::GetDirectInterface(self, object_array_class.Get(), 1)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 906 | |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 907 | CHECK_EQ(object_array_string.Get(), |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 908 | FindSystemClass(self, GetClassRootDescriptor(ClassRoot::kJavaLangStringArrayClass))); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 909 | |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 910 | // 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] | 911 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 912 | // Create java.lang.reflect.Proxy root. |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 913 | SetClassRoot(ClassRoot::kJavaLangReflectProxy, |
| 914 | FindSystemClass(self, "Ljava/lang/reflect/Proxy;")); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 915 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 916 | // Create java.lang.reflect.Field.class root. |
Vladimir Marko | acb906d | 2018-05-30 10:23:49 +0100 | [diff] [blame] | 917 | ObjPtr<mirror::Class> class_root = FindSystemClass(self, "Ljava/lang/reflect/Field;"); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 918 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 919 | SetClassRoot(ClassRoot::kJavaLangReflectField, class_root); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 920 | |
| 921 | // Create java.lang.reflect.Field array root. |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 922 | class_root = FindSystemClass(self, "[Ljava/lang/reflect/Field;"); |
| 923 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 924 | SetClassRoot(ClassRoot::kJavaLangReflectFieldArrayClass, class_root); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 925 | |
| 926 | // Create java.lang.reflect.Constructor.class root and array root. |
| 927 | class_root = FindSystemClass(self, "Ljava/lang/reflect/Constructor;"); |
| 928 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 929 | SetClassRoot(ClassRoot::kJavaLangReflectConstructor, class_root); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 930 | class_root = FindSystemClass(self, "[Ljava/lang/reflect/Constructor;"); |
| 931 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 932 | SetClassRoot(ClassRoot::kJavaLangReflectConstructorArrayClass, class_root); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 933 | |
| 934 | // Create java.lang.reflect.Method.class root and array root. |
| 935 | class_root = FindSystemClass(self, "Ljava/lang/reflect/Method;"); |
| 936 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 937 | SetClassRoot(ClassRoot::kJavaLangReflectMethod, class_root); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 938 | class_root = FindSystemClass(self, "[Ljava/lang/reflect/Method;"); |
| 939 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 940 | SetClassRoot(ClassRoot::kJavaLangReflectMethodArrayClass, class_root); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 941 | |
Orion Hodson | 005ac51 | 2017-10-24 15:43:43 +0100 | [diff] [blame] | 942 | // Create java.lang.invoke.CallSite.class root |
| 943 | class_root = FindSystemClass(self, "Ljava/lang/invoke/CallSite;"); |
| 944 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 945 | SetClassRoot(ClassRoot::kJavaLangInvokeCallSite, class_root); |
Orion Hodson | 005ac51 | 2017-10-24 15:43:43 +0100 | [diff] [blame] | 946 | |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 947 | // Create java.lang.invoke.MethodType.class root |
| 948 | class_root = FindSystemClass(self, "Ljava/lang/invoke/MethodType;"); |
| 949 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 950 | SetClassRoot(ClassRoot::kJavaLangInvokeMethodType, class_root); |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 951 | |
| 952 | // Create java.lang.invoke.MethodHandleImpl.class root |
| 953 | class_root = FindSystemClass(self, "Ljava/lang/invoke/MethodHandleImpl;"); |
| 954 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 955 | SetClassRoot(ClassRoot::kJavaLangInvokeMethodHandleImpl, class_root); |
Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 956 | SetClassRoot(ClassRoot::kJavaLangInvokeMethodHandle, class_root->GetSuperClass()); |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 957 | |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 958 | // Create java.lang.invoke.MethodHandles.Lookup.class root |
| 959 | class_root = FindSystemClass(self, "Ljava/lang/invoke/MethodHandles$Lookup;"); |
| 960 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 961 | SetClassRoot(ClassRoot::kJavaLangInvokeMethodHandlesLookup, class_root); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 962 | |
Orion Hodson | 005ac51 | 2017-10-24 15:43:43 +0100 | [diff] [blame] | 963 | // Create java.lang.invoke.VarHandle.class root |
| 964 | class_root = FindSystemClass(self, "Ljava/lang/invoke/VarHandle;"); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 965 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 966 | SetClassRoot(ClassRoot::kJavaLangInvokeVarHandle, class_root); |
Orion Hodson | 005ac51 | 2017-10-24 15:43:43 +0100 | [diff] [blame] | 967 | |
| 968 | // Create java.lang.invoke.FieldVarHandle.class root |
| 969 | class_root = FindSystemClass(self, "Ljava/lang/invoke/FieldVarHandle;"); |
| 970 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 971 | SetClassRoot(ClassRoot::kJavaLangInvokeFieldVarHandle, class_root); |
Orion Hodson | 005ac51 | 2017-10-24 15:43:43 +0100 | [diff] [blame] | 972 | |
| 973 | // Create java.lang.invoke.ArrayElementVarHandle.class root |
| 974 | class_root = FindSystemClass(self, "Ljava/lang/invoke/ArrayElementVarHandle;"); |
| 975 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 976 | SetClassRoot(ClassRoot::kJavaLangInvokeArrayElementVarHandle, class_root); |
Orion Hodson | 005ac51 | 2017-10-24 15:43:43 +0100 | [diff] [blame] | 977 | |
| 978 | // Create java.lang.invoke.ByteArrayViewVarHandle.class root |
| 979 | class_root = FindSystemClass(self, "Ljava/lang/invoke/ByteArrayViewVarHandle;"); |
| 980 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 981 | SetClassRoot(ClassRoot::kJavaLangInvokeByteArrayViewVarHandle, class_root); |
Orion Hodson | 005ac51 | 2017-10-24 15:43:43 +0100 | [diff] [blame] | 982 | |
| 983 | // Create java.lang.invoke.ByteBufferViewVarHandle.class root |
| 984 | class_root = FindSystemClass(self, "Ljava/lang/invoke/ByteBufferViewVarHandle;"); |
| 985 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 986 | SetClassRoot(ClassRoot::kJavaLangInvokeByteBufferViewVarHandle, class_root); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 987 | |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 988 | class_root = FindSystemClass(self, "Ldalvik/system/EmulatedStackFrame;"); |
| 989 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 990 | SetClassRoot(ClassRoot::kDalvikSystemEmulatedStackFrame, class_root); |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 991 | |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 992 | // 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] | 993 | // finish initializing Reference class |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 994 | mirror::Class::SetStatus(java_lang_ref_Reference, ClassStatus::kNotReady, self); |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 995 | CheckSystemClass(self, java_lang_ref_Reference, "Ljava/lang/ref/Reference;"); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 996 | CHECK_EQ(java_lang_ref_Reference->GetObjectSize(), mirror::Reference::InstanceSize()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 997 | CHECK_EQ(java_lang_ref_Reference->GetClassSize(), |
| 998 | mirror::Reference::ClassSize(image_pointer_size_)); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 999 | class_root = FindSystemClass(self, "Ljava/lang/ref/FinalizerReference;"); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 1000 | CHECK_EQ(class_root->GetClassFlags(), mirror::kClassFlagNormal); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 1001 | class_root->SetClassFlags(class_root->GetClassFlags() | mirror::kClassFlagFinalizerReference); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 1002 | class_root = FindSystemClass(self, "Ljava/lang/ref/PhantomReference;"); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 1003 | CHECK_EQ(class_root->GetClassFlags(), mirror::kClassFlagNormal); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 1004 | class_root->SetClassFlags(class_root->GetClassFlags() | mirror::kClassFlagPhantomReference); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 1005 | class_root = FindSystemClass(self, "Ljava/lang/ref/SoftReference;"); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 1006 | CHECK_EQ(class_root->GetClassFlags(), mirror::kClassFlagNormal); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 1007 | class_root->SetClassFlags(class_root->GetClassFlags() | mirror::kClassFlagSoftReference); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 1008 | class_root = FindSystemClass(self, "Ljava/lang/ref/WeakReference;"); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 1009 | CHECK_EQ(class_root->GetClassFlags(), mirror::kClassFlagNormal); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 1010 | class_root->SetClassFlags(class_root->GetClassFlags() | mirror::kClassFlagWeakReference); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 1011 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 1012 | // Setup the ClassLoader, verifying the object_size_. |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 1013 | class_root = FindSystemClass(self, "Ljava/lang/ClassLoader;"); |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 1014 | class_root->SetClassLoaderClass(); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 1015 | CHECK_EQ(class_root->GetObjectSize(), mirror::ClassLoader::InstanceSize()); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 1016 | SetClassRoot(ClassRoot::kJavaLangClassLoader, class_root); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1017 | |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 1018 | // Set up java.lang.Throwable, java.lang.ClassNotFoundException, and |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 1019 | // java.lang.StackTraceElement as a convenience. |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 1020 | SetClassRoot(ClassRoot::kJavaLangThrowable, FindSystemClass(self, "Ljava/lang/Throwable;")); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 1021 | SetClassRoot(ClassRoot::kJavaLangClassNotFoundException, |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 1022 | FindSystemClass(self, "Ljava/lang/ClassNotFoundException;")); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 1023 | SetClassRoot(ClassRoot::kJavaLangStackTraceElement, |
| 1024 | FindSystemClass(self, "Ljava/lang/StackTraceElement;")); |
| 1025 | SetClassRoot(ClassRoot::kJavaLangStackTraceElementArrayClass, |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 1026 | FindSystemClass(self, "[Ljava/lang/StackTraceElement;")); |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 1027 | SetClassRoot(ClassRoot::kJavaLangClassLoaderArrayClass, |
| 1028 | FindSystemClass(self, "[Ljava/lang/ClassLoader;")); |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 1029 | |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 1030 | // Create conflict tables that depend on the class linker. |
| 1031 | runtime->FixupConflictTables(); |
| 1032 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1033 | FinishInit(self); |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 1034 | |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 1035 | VLOG(startup) << "ClassLinker::InitFromCompiler exiting"; |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 1036 | |
| 1037 | return true; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1038 | } |
| 1039 | |
Andreas Gampe | 9abc31e | 2018-05-17 11:47:09 -0700 | [diff] [blame] | 1040 | static void CreateStringInitBindings(Thread* self, ClassLinker* class_linker) |
| 1041 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1042 | // Find String.<init> -> StringFactory bindings. |
| 1043 | ObjPtr<mirror::Class> string_factory_class = |
| 1044 | class_linker->FindSystemClass(self, "Ljava/lang/StringFactory;"); |
| 1045 | CHECK(string_factory_class != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 1046 | ObjPtr<mirror::Class> string_class = GetClassRoot<mirror::String>(class_linker); |
Andreas Gampe | 9abc31e | 2018-05-17 11:47:09 -0700 | [diff] [blame] | 1047 | WellKnownClasses::InitStringInit(string_class, string_factory_class); |
| 1048 | // Update the primordial thread. |
| 1049 | self->InitStringEntryPoints(); |
| 1050 | } |
| 1051 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1052 | void ClassLinker::FinishInit(Thread* self) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1053 | VLOG(startup) << "ClassLinker::FinishInit entering"; |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 1054 | |
Andreas Gampe | 9abc31e | 2018-05-17 11:47:09 -0700 | [diff] [blame] | 1055 | CreateStringInitBindings(self, this); |
| 1056 | |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 1057 | // Let the heap know some key offsets into java.lang.ref instances |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 1058 | // Note: we hard code the field indexes here rather than using FindInstanceField |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 1059 | // as the types of the field can't be resolved prior to the runtime being |
| 1060 | // fully initialized |
Andreas Gampe | 7b2450e | 2018-06-19 10:45:54 -0700 | [diff] [blame] | 1061 | StackHandleScope<3> hs(self); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 1062 | Handle<mirror::Class> java_lang_ref_Reference = |
| 1063 | hs.NewHandle(GetClassRoot<mirror::Reference>(this)); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1064 | Handle<mirror::Class> java_lang_ref_FinalizerReference = |
| 1065 | hs.NewHandle(FindSystemClass(self, "Ljava/lang/ref/FinalizerReference;")); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1066 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1067 | ArtField* pendingNext = java_lang_ref_Reference->GetInstanceField(0); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1068 | CHECK_STREQ(pendingNext->GetName(), "pendingNext"); |
| 1069 | CHECK_STREQ(pendingNext->GetTypeDescriptor(), "Ljava/lang/ref/Reference;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 1070 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1071 | ArtField* queue = java_lang_ref_Reference->GetInstanceField(1); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1072 | CHECK_STREQ(queue->GetName(), "queue"); |
| 1073 | CHECK_STREQ(queue->GetTypeDescriptor(), "Ljava/lang/ref/ReferenceQueue;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 1074 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1075 | ArtField* queueNext = java_lang_ref_Reference->GetInstanceField(2); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1076 | CHECK_STREQ(queueNext->GetName(), "queueNext"); |
| 1077 | CHECK_STREQ(queueNext->GetTypeDescriptor(), "Ljava/lang/ref/Reference;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 1078 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1079 | ArtField* referent = java_lang_ref_Reference->GetInstanceField(3); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1080 | CHECK_STREQ(referent->GetName(), "referent"); |
| 1081 | CHECK_STREQ(referent->GetTypeDescriptor(), "Ljava/lang/Object;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 1082 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1083 | ArtField* zombie = java_lang_ref_FinalizerReference->GetInstanceField(2); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1084 | CHECK_STREQ(zombie->GetName(), "zombie"); |
| 1085 | CHECK_STREQ(zombie->GetTypeDescriptor(), "Ljava/lang/Object;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 1086 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1087 | // ensure all class_roots_ are initialized |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 1088 | 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] | 1089 | ClassRoot class_root = static_cast<ClassRoot>(i); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1090 | ObjPtr<mirror::Class> klass = GetClassRoot(class_root); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1091 | CHECK(klass != nullptr); |
| 1092 | DCHECK(klass->IsArrayClass() || klass->IsPrimitive() || klass->GetDexCache() != nullptr); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1093 | // note SetClassRoot does additional validation. |
| 1094 | // if possible add new checks there to catch errors early |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 1095 | } |
| 1096 | |
Vladimir Marko | 0261055 | 2018-06-04 14:38:00 +0100 | [diff] [blame] | 1097 | CHECK(GetArrayIfTable() != nullptr); |
Elliott Hughes | 92f14b2 | 2011-10-06 12:29:54 -0700 | [diff] [blame] | 1098 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 1099 | // disable the slow paths in FindClass and CreatePrimitiveClass now |
| 1100 | // that Object, Class, and Object[] are setup |
| 1101 | init_done_ = true; |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 1102 | |
Andreas Gampe | 7b2450e | 2018-06-19 10:45:54 -0700 | [diff] [blame] | 1103 | // Under sanitization, the small carve-out to handle stack overflow might not be enough to |
| 1104 | // initialize the StackOverflowError class (as it might require running the verifier). Instead, |
| 1105 | // ensure that the class will be initialized. |
| 1106 | if (kMemoryToolIsAvailable && !Runtime::Current()->IsAotCompiler()) { |
Andreas Gampe | e0bbab9 | 2019-07-25 12:28:22 -0700 | [diff] [blame] | 1107 | verifier::ClassVerifier::Init(this); // Need to prepare the verifier. |
Andreas Gampe | 7b2450e | 2018-06-19 10:45:54 -0700 | [diff] [blame] | 1108 | |
| 1109 | ObjPtr<mirror::Class> soe_klass = FindSystemClass(self, "Ljava/lang/StackOverflowError;"); |
| 1110 | if (soe_klass == nullptr || !EnsureInitialized(self, hs.NewHandle(soe_klass), true, true)) { |
| 1111 | // Strange, but don't crash. |
| 1112 | LOG(WARNING) << "Could not prepare StackOverflowError."; |
| 1113 | self->ClearException(); |
| 1114 | } |
| 1115 | } |
| 1116 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1117 | VLOG(startup) << "ClassLinker::FinishInit exiting"; |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 1118 | } |
| 1119 | |
Vladimir Marko | dcfcce4 | 2018-06-27 10:00:28 +0000 | [diff] [blame] | 1120 | void ClassLinker::RunRootClinits(Thread* self) { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 1121 | for (size_t i = 0; i < static_cast<size_t>(ClassRoot::kMax); ++i) { |
| 1122 | ObjPtr<mirror::Class> c = GetClassRoot(ClassRoot(i), this); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 1123 | if (!c->IsArrayClass() && !c->IsPrimitive()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1124 | StackHandleScope<1> hs(self); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 1125 | Handle<mirror::Class> h_class(hs.NewHandle(c)); |
David Srbecky | 08110ef | 2020-05-20 19:33:43 +0100 | [diff] [blame] | 1126 | if (!EnsureInitialized(self, h_class, true, true)) { |
| 1127 | LOG(FATAL) << "Exception when initializing " << h_class->PrettyClass() |
| 1128 | << ": " << self->GetException()->Dump(); |
| 1129 | } |
Vladimir Marko | dcfcce4 | 2018-06-27 10:00:28 +0000 | [diff] [blame] | 1130 | } else { |
| 1131 | DCHECK(c->IsInitialized()); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 1132 | } |
| 1133 | } |
| 1134 | } |
| 1135 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1136 | struct TrampolineCheckData { |
| 1137 | const void* quick_resolution_trampoline; |
| 1138 | const void* quick_imt_conflict_trampoline; |
| 1139 | const void* quick_generic_jni_trampoline; |
| 1140 | const void* quick_to_interpreter_bridge_trampoline; |
Nicolas Geoffray | c39af94 | 2021-01-25 08:43:57 +0000 | [diff] [blame] | 1141 | const void* nterp_trampoline; |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1142 | PointerSize pointer_size; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1143 | ArtMethod* m; |
| 1144 | bool error; |
| 1145 | }; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1146 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1147 | bool ClassLinker::InitFromBootImage(std::string* error_msg) { |
| 1148 | VLOG(startup) << __FUNCTION__ << " entering"; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1149 | CHECK(!init_done_); |
| 1150 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 1151 | Runtime* const runtime = Runtime::Current(); |
| 1152 | Thread* const self = Thread::Current(); |
| 1153 | gc::Heap* const heap = runtime->GetHeap(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1154 | std::vector<gc::space::ImageSpace*> spaces = heap->GetBootImageSpaces(); |
| 1155 | CHECK(!spaces.empty()); |
Vladimir Marko | 024d69f | 2019-06-13 10:52:32 +0100 | [diff] [blame] | 1156 | const ImageHeader& image_header = spaces[0]->GetImageHeader(); |
| 1157 | uint32_t pointer_size_unchecked = image_header.GetPointerSizeUnchecked(); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1158 | if (!ValidPointerSize(pointer_size_unchecked)) { |
| 1159 | *error_msg = StringPrintf("Invalid image pointer size: %u", pointer_size_unchecked); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1160 | return false; |
| 1161 | } |
Vladimir Marko | 3364d18 | 2019-03-13 13:55:01 +0000 | [diff] [blame] | 1162 | image_pointer_size_ = image_header.GetPointerSize(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1163 | if (!runtime->IsAotCompiler()) { |
| 1164 | // Only the Aot compiler supports having an image with a different pointer size than the |
| 1165 | // runtime. This happens on the host for compiling 32 bit tests since we use a 64 bit libart |
| 1166 | // 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] | 1167 | if (image_pointer_size_ != kRuntimePointerSize) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1168 | *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] | 1169 | static_cast<size_t>(image_pointer_size_), |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1170 | sizeof(void*)); |
| 1171 | return false; |
| 1172 | } |
| 1173 | } |
Vladimir Marko | 3364d18 | 2019-03-13 13:55:01 +0000 | [diff] [blame] | 1174 | DCHECK(!runtime->HasResolutionMethod()); |
| 1175 | runtime->SetResolutionMethod(image_header.GetImageMethod(ImageHeader::kResolutionMethod)); |
| 1176 | runtime->SetImtConflictMethod(image_header.GetImageMethod(ImageHeader::kImtConflictMethod)); |
| 1177 | runtime->SetImtUnimplementedMethod( |
| 1178 | image_header.GetImageMethod(ImageHeader::kImtUnimplementedMethod)); |
| 1179 | runtime->SetCalleeSaveMethod( |
| 1180 | image_header.GetImageMethod(ImageHeader::kSaveAllCalleeSavesMethod), |
| 1181 | CalleeSaveType::kSaveAllCalleeSaves); |
| 1182 | runtime->SetCalleeSaveMethod( |
| 1183 | image_header.GetImageMethod(ImageHeader::kSaveRefsOnlyMethod), |
| 1184 | CalleeSaveType::kSaveRefsOnly); |
| 1185 | runtime->SetCalleeSaveMethod( |
| 1186 | image_header.GetImageMethod(ImageHeader::kSaveRefsAndArgsMethod), |
| 1187 | CalleeSaveType::kSaveRefsAndArgs); |
| 1188 | runtime->SetCalleeSaveMethod( |
| 1189 | image_header.GetImageMethod(ImageHeader::kSaveEverythingMethod), |
| 1190 | CalleeSaveType::kSaveEverything); |
| 1191 | runtime->SetCalleeSaveMethod( |
| 1192 | image_header.GetImageMethod(ImageHeader::kSaveEverythingMethodForClinit), |
| 1193 | CalleeSaveType::kSaveEverythingForClinit); |
| 1194 | runtime->SetCalleeSaveMethod( |
| 1195 | image_header.GetImageMethod(ImageHeader::kSaveEverythingMethodForSuspendCheck), |
| 1196 | CalleeSaveType::kSaveEverythingForSuspendCheck); |
| 1197 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1198 | std::vector<const OatFile*> oat_files = |
| 1199 | runtime->GetOatFileManager().RegisterImageOatFiles(spaces); |
| 1200 | DCHECK(!oat_files.empty()); |
| 1201 | const OatHeader& default_oat_header = oat_files[0]->GetOatHeader(); |
Vladimir Marko | 7dac864 | 2019-11-06 17:09:30 +0000 | [diff] [blame] | 1202 | jni_dlsym_lookup_trampoline_ = default_oat_header.GetJniDlsymLookupTrampoline(); |
Vladimir Marko | fa458ac | 2020-02-12 14:08:07 +0000 | [diff] [blame] | 1203 | jni_dlsym_lookup_critical_trampoline_ = default_oat_header.GetJniDlsymLookupCriticalTrampoline(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1204 | quick_resolution_trampoline_ = default_oat_header.GetQuickResolutionTrampoline(); |
| 1205 | quick_imt_conflict_trampoline_ = default_oat_header.GetQuickImtConflictTrampoline(); |
| 1206 | quick_generic_jni_trampoline_ = default_oat_header.GetQuickGenericJniTrampoline(); |
| 1207 | quick_to_interpreter_bridge_trampoline_ = default_oat_header.GetQuickToInterpreterBridge(); |
Nicolas Geoffray | c39af94 | 2021-01-25 08:43:57 +0000 | [diff] [blame] | 1208 | nterp_trampoline_ = default_oat_header.GetNterpTrampoline(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1209 | if (kIsDebugBuild) { |
| 1210 | // Check that the other images use the same trampoline. |
| 1211 | for (size_t i = 1; i < oat_files.size(); ++i) { |
| 1212 | const OatHeader& ith_oat_header = oat_files[i]->GetOatHeader(); |
Vladimir Marko | 7dac864 | 2019-11-06 17:09:30 +0000 | [diff] [blame] | 1213 | const void* ith_jni_dlsym_lookup_trampoline_ = |
| 1214 | ith_oat_header.GetJniDlsymLookupTrampoline(); |
Vladimir Marko | fa458ac | 2020-02-12 14:08:07 +0000 | [diff] [blame] | 1215 | const void* ith_jni_dlsym_lookup_critical_trampoline_ = |
| 1216 | ith_oat_header.GetJniDlsymLookupCriticalTrampoline(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1217 | const void* ith_quick_resolution_trampoline = |
| 1218 | ith_oat_header.GetQuickResolutionTrampoline(); |
| 1219 | const void* ith_quick_imt_conflict_trampoline = |
| 1220 | ith_oat_header.GetQuickImtConflictTrampoline(); |
| 1221 | const void* ith_quick_generic_jni_trampoline = |
| 1222 | ith_oat_header.GetQuickGenericJniTrampoline(); |
| 1223 | const void* ith_quick_to_interpreter_bridge_trampoline = |
| 1224 | ith_oat_header.GetQuickToInterpreterBridge(); |
Nicolas Geoffray | c39af94 | 2021-01-25 08:43:57 +0000 | [diff] [blame] | 1225 | const void* ith_nterp_trampoline = |
| 1226 | ith_oat_header.GetNterpTrampoline(); |
Vladimir Marko | 7dac864 | 2019-11-06 17:09:30 +0000 | [diff] [blame] | 1227 | if (ith_jni_dlsym_lookup_trampoline_ != jni_dlsym_lookup_trampoline_ || |
Vladimir Marko | fa458ac | 2020-02-12 14:08:07 +0000 | [diff] [blame] | 1228 | ith_jni_dlsym_lookup_critical_trampoline_ != jni_dlsym_lookup_critical_trampoline_ || |
Vladimir Marko | 7dac864 | 2019-11-06 17:09:30 +0000 | [diff] [blame] | 1229 | ith_quick_resolution_trampoline != quick_resolution_trampoline_ || |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1230 | ith_quick_imt_conflict_trampoline != quick_imt_conflict_trampoline_ || |
| 1231 | ith_quick_generic_jni_trampoline != quick_generic_jni_trampoline_ || |
Nicolas Geoffray | c39af94 | 2021-01-25 08:43:57 +0000 | [diff] [blame] | 1232 | ith_quick_to_interpreter_bridge_trampoline != quick_to_interpreter_bridge_trampoline_ || |
| 1233 | ith_nterp_trampoline != nterp_trampoline_) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1234 | // Make sure that all methods in this image do not contain those trampolines as |
| 1235 | // entrypoints. Otherwise the class-linker won't be able to work with a single set. |
| 1236 | TrampolineCheckData data; |
| 1237 | data.error = false; |
| 1238 | data.pointer_size = GetImagePointerSize(); |
| 1239 | data.quick_resolution_trampoline = ith_quick_resolution_trampoline; |
| 1240 | data.quick_imt_conflict_trampoline = ith_quick_imt_conflict_trampoline; |
| 1241 | data.quick_generic_jni_trampoline = ith_quick_generic_jni_trampoline; |
| 1242 | data.quick_to_interpreter_bridge_trampoline = ith_quick_to_interpreter_bridge_trampoline; |
Nicolas Geoffray | c39af94 | 2021-01-25 08:43:57 +0000 | [diff] [blame] | 1243 | data.nterp_trampoline = ith_nterp_trampoline; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1244 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Andreas Gampe | 0c18338 | 2017-07-13 22:26:24 -0700 | [diff] [blame] | 1245 | auto visitor = [&](mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1246 | if (obj->IsClass()) { |
| 1247 | ObjPtr<mirror::Class> klass = obj->AsClass(); |
| 1248 | for (ArtMethod& m : klass->GetMethods(data.pointer_size)) { |
| 1249 | const void* entrypoint = |
| 1250 | m.GetEntryPointFromQuickCompiledCodePtrSize(data.pointer_size); |
| 1251 | if (entrypoint == data.quick_resolution_trampoline || |
| 1252 | entrypoint == data.quick_imt_conflict_trampoline || |
| 1253 | entrypoint == data.quick_generic_jni_trampoline || |
| 1254 | entrypoint == data.quick_to_interpreter_bridge_trampoline) { |
| 1255 | data.m = &m; |
| 1256 | data.error = true; |
| 1257 | return; |
| 1258 | } |
| 1259 | } |
| 1260 | } |
| 1261 | }; |
| 1262 | spaces[i]->GetLiveBitmap()->Walk(visitor); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1263 | if (data.error) { |
| 1264 | ArtMethod* m = data.m; |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1265 | LOG(ERROR) << "Found a broken ArtMethod: " << ArtMethod::PrettyMethod(m); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1266 | *error_msg = "Found an ArtMethod with a bad entrypoint"; |
| 1267 | return false; |
| 1268 | } |
| 1269 | } |
| 1270 | } |
| 1271 | } |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 1272 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1273 | class_roots_ = GcRoot<mirror::ObjectArray<mirror::Class>>( |
Vladimir Marko | d7e9bbf | 2019-03-28 13:18:57 +0000 | [diff] [blame] | 1274 | ObjPtr<mirror::ObjectArray<mirror::Class>>::DownCast( |
Vladimir Marko | 024d69f | 2019-06-13 10:52:32 +0100 | [diff] [blame] | 1275 | image_header.GetImageRoot(ImageHeader::kClassRoots))); |
Vladimir Marko | f75613c | 2018-06-05 12:51:04 +0100 | [diff] [blame] | 1276 | DCHECK_EQ(GetClassRoot<mirror::Class>(this)->GetClassFlags(), mirror::kClassFlagClass); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1277 | |
Vladimir Marko | 024d69f | 2019-06-13 10:52:32 +0100 | [diff] [blame] | 1278 | DCHECK_EQ(GetClassRoot<mirror::Object>(this)->GetObjectSize(), sizeof(mirror::Object)); |
| 1279 | ObjPtr<mirror::ObjectArray<mirror::Object>> boot_image_live_objects = |
| 1280 | ObjPtr<mirror::ObjectArray<mirror::Object>>::DownCast( |
| 1281 | image_header.GetImageRoot(ImageHeader::kBootImageLiveObjects)); |
| 1282 | runtime->SetSentinel(boot_image_live_objects->Get(ImageHeader::kClearedJniWeakSentinel)); |
| 1283 | DCHECK(runtime->GetSentinel().Read()->GetClass() == GetClassRoot<mirror::Object>(this)); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 1284 | |
Vladimir Marko | d190851 | 2018-11-22 14:57:28 +0000 | [diff] [blame] | 1285 | for (size_t i = 0u, size = spaces.size(); i != size; ++i) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1286 | // Boot class loader, use a null handle. |
| 1287 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
Vladimir Marko | d190851 | 2018-11-22 14:57:28 +0000 | [diff] [blame] | 1288 | if (!AddImageSpace(spaces[i], |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1289 | ScopedNullHandle<mirror::ClassLoader>(), |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1290 | /*out*/&dex_files, |
| 1291 | error_msg)) { |
| 1292 | return false; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1293 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1294 | // Append opened dex files at the end. |
| 1295 | boot_dex_files_.insert(boot_dex_files_.end(), |
| 1296 | std::make_move_iterator(dex_files.begin()), |
| 1297 | std::make_move_iterator(dex_files.end())); |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 1298 | } |
Mathieu Chartier | be8303d | 2017-08-17 17:39:39 -0700 | [diff] [blame] | 1299 | for (const std::unique_ptr<const DexFile>& dex_file : boot_dex_files_) { |
| 1300 | OatDexFile::MadviseDexFile(*dex_file, MadviseState::kMadviseStateAtLoad); |
| 1301 | } |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1302 | FinishInit(self); |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 1303 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1304 | VLOG(startup) << __FUNCTION__ << " exiting"; |
| 1305 | return true; |
| 1306 | } |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 1307 | |
Vladimir Marko | 4433c43 | 2018-12-04 14:57:47 +0000 | [diff] [blame] | 1308 | void ClassLinker::AddExtraBootDexFiles( |
| 1309 | Thread* self, |
| 1310 | std::vector<std::unique_ptr<const DexFile>>&& additional_dex_files) { |
| 1311 | for (std::unique_ptr<const DexFile>& dex_file : additional_dex_files) { |
Mathieu Chartier | 0a19e21 | 2019-11-27 14:35:24 -0800 | [diff] [blame] | 1312 | AppendToBootClassPath(self, dex_file.get()); |
Orion Hodson | 771708f | 2021-01-06 15:45:16 +0000 | [diff] [blame] | 1313 | if (kIsDebugBuild) { |
| 1314 | for (const auto& boot_dex_file : boot_dex_files_) { |
| 1315 | DCHECK_NE(boot_dex_file->GetLocation(), dex_file->GetLocation()); |
| 1316 | } |
| 1317 | } |
Vladimir Marko | 4433c43 | 2018-12-04 14:57:47 +0000 | [diff] [blame] | 1318 | boot_dex_files_.push_back(std::move(dex_file)); |
| 1319 | } |
| 1320 | } |
| 1321 | |
Jeff Hao | 5872d7c | 2016-04-27 11:07:41 -0700 | [diff] [blame] | 1322 | bool ClassLinker::IsBootClassLoader(ScopedObjectAccessAlreadyRunnable& soa, |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1323 | ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1324 | return class_loader == nullptr || |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 1325 | soa.Decode<mirror::Class>(WellKnownClasses::java_lang_BootClassLoader) == |
| 1326 | class_loader->GetClass(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1327 | } |
| 1328 | |
Alexey Grebenkin | be4c2bd | 2018-02-01 19:09:59 +0300 | [diff] [blame] | 1329 | class CHAOnDeleteUpdateClassVisitor { |
| 1330 | public: |
| 1331 | explicit CHAOnDeleteUpdateClassVisitor(LinearAlloc* alloc) |
| 1332 | : allocator_(alloc), cha_(Runtime::Current()->GetClassLinker()->GetClassHierarchyAnalysis()), |
| 1333 | pointer_size_(Runtime::Current()->GetClassLinker()->GetImagePointerSize()), |
| 1334 | self_(Thread::Current()) {} |
| 1335 | |
| 1336 | bool operator()(ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1337 | // This class is going to be unloaded. Tell CHA about it. |
| 1338 | cha_->ResetSingleImplementationInHierarchy(klass, allocator_, pointer_size_); |
| 1339 | return true; |
| 1340 | } |
| 1341 | private: |
| 1342 | const LinearAlloc* allocator_; |
| 1343 | const ClassHierarchyAnalysis* cha_; |
| 1344 | const PointerSize pointer_size_; |
| 1345 | const Thread* self_; |
| 1346 | }; |
| 1347 | |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1348 | /* |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1349 | * A class used to ensure that all references to strings interned in an AppImage have been |
| 1350 | * 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] | 1351 | */ |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1352 | class CountInternedStringReferencesVisitor { |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1353 | public: |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1354 | CountInternedStringReferencesVisitor(const gc::space::ImageSpace& space, |
| 1355 | const InternTable::UnorderedSet& image_interns) |
| 1356 | : space_(space), |
| 1357 | image_interns_(image_interns), |
| 1358 | count_(0u) {} |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1359 | |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1360 | void TestObject(ObjPtr<mirror::Object> referred_obj) const |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1361 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1362 | if (referred_obj != nullptr && |
| 1363 | space_.HasAddress(referred_obj.Ptr()) && |
| 1364 | referred_obj->IsString()) { |
| 1365 | ObjPtr<mirror::String> referred_str = referred_obj->AsString(); |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1366 | auto it = image_interns_.find(GcRoot<mirror::String>(referred_str)); |
| 1367 | if (it != image_interns_.end() && it->Read() == referred_str) { |
| 1368 | ++count_; |
Chris Wailes | fbeef46 | 2018-10-19 14:16:35 -0700 | [diff] [blame] | 1369 | } |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1370 | } |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1371 | } |
| 1372 | |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1373 | void VisitRootIfNonNull( |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1374 | mirror::CompressedReference<mirror::Object>* root) const |
| 1375 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1376 | if (!root->IsNull()) { |
| 1377 | VisitRoot(root); |
| 1378 | } |
| 1379 | } |
| 1380 | |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1381 | void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1382 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1383 | TestObject(root->AsMirrorPtr()); |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1384 | } |
| 1385 | |
| 1386 | // Visit Class Fields |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1387 | void operator()(ObjPtr<mirror::Object> obj, |
| 1388 | MemberOffset offset, |
| 1389 | bool is_static ATTRIBUTE_UNUSED) const |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1390 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1391 | // References within image or across images don't need a read barrier. |
| 1392 | ObjPtr<mirror::Object> referred_obj = |
| 1393 | obj->GetFieldObject<mirror::Object, kVerifyNone, kWithoutReadBarrier>(offset); |
| 1394 | TestObject(referred_obj); |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1395 | } |
| 1396 | |
| 1397 | void operator()(ObjPtr<mirror::Class> klass ATTRIBUTE_UNUSED, |
| 1398 | ObjPtr<mirror::Reference> ref) const |
| 1399 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_) { |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1400 | operator()(ref, mirror::Reference::ReferentOffset(), /*is_static=*/ false); |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1401 | } |
| 1402 | |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1403 | size_t GetCount() const { |
| 1404 | return count_; |
| 1405 | } |
| 1406 | |
| 1407 | private: |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1408 | const gc::space::ImageSpace& space_; |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1409 | const InternTable::UnorderedSet& image_interns_; |
| 1410 | mutable size_t count_; // Modified from the `const` callbacks. |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1411 | }; |
| 1412 | |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1413 | /* |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1414 | * This function counts references to strings interned in the AppImage. |
| 1415 | * 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] | 1416 | */ |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1417 | size_t CountInternedStringReferences(gc::space::ImageSpace& space, |
| 1418 | const InternTable::UnorderedSet& image_interns) |
| 1419 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1420 | const gc::accounting::ContinuousSpaceBitmap* bitmap = space.GetMarkBitmap(); |
| 1421 | const ImageHeader& image_header = space.GetImageHeader(); |
| 1422 | const uint8_t* target_base = space.GetMemMap()->Begin(); |
| 1423 | const ImageSection& objects_section = image_header.GetObjectsSection(); |
Chris Wailes | fbeef46 | 2018-10-19 14:16:35 -0700 | [diff] [blame] | 1424 | |
| 1425 | auto objects_begin = reinterpret_cast<uintptr_t>(target_base + objects_section.Offset()); |
| 1426 | auto objects_end = reinterpret_cast<uintptr_t>(target_base + objects_section.End()); |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1427 | |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1428 | CountInternedStringReferencesVisitor visitor(space, image_interns); |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1429 | bitmap->VisitMarkedRange(objects_begin, |
| 1430 | objects_end, |
| 1431 | [&space, &visitor](mirror::Object* obj) |
| 1432 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1433 | if (space.HasAddress(obj)) { |
| 1434 | if (obj->IsDexCache()) { |
Chris Wailes | fbeef46 | 2018-10-19 14:16:35 -0700 | [diff] [blame] | 1435 | obj->VisitReferences</* kVisitNativeRoots= */ true, |
| 1436 | kVerifyNone, |
| 1437 | kWithoutReadBarrier>(visitor, visitor); |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1438 | } else { |
| 1439 | // Don't visit native roots for non-dex-cache as they can't contain |
| 1440 | // native references to strings. This is verified during compilation |
| 1441 | // by ImageWriter::VerifyNativeGCRootInvariants. |
Chris Wailes | fbeef46 | 2018-10-19 14:16:35 -0700 | [diff] [blame] | 1442 | obj->VisitReferences</* kVisitNativeRoots= */ false, |
| 1443 | kVerifyNone, |
| 1444 | kWithoutReadBarrier>(visitor, visitor); |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1445 | } |
| 1446 | } |
| 1447 | }); |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1448 | return visitor.GetCount(); |
| 1449 | } |
| 1450 | |
| 1451 | template <typename Visitor> |
| 1452 | static void VisitInternedStringReferences( |
| 1453 | gc::space::ImageSpace* space, |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1454 | const Visitor& visitor) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1455 | const uint8_t* target_base = space->Begin(); |
| 1456 | const ImageSection& sro_section = |
| 1457 | space->GetImageHeader().GetImageStringReferenceOffsetsSection(); |
| 1458 | const size_t num_string_offsets = sro_section.Size() / sizeof(AppImageReferenceOffsetInfo); |
| 1459 | |
| 1460 | VLOG(image) |
| 1461 | << "ClassLinker:AppImage:InternStrings:imageStringReferenceOffsetCount = " |
| 1462 | << num_string_offsets; |
| 1463 | |
| 1464 | const auto* sro_base = |
| 1465 | reinterpret_cast<const AppImageReferenceOffsetInfo*>(target_base + sro_section.Offset()); |
| 1466 | |
| 1467 | for (size_t offset_index = 0; offset_index < num_string_offsets; ++offset_index) { |
| 1468 | uint32_t base_offset = sro_base[offset_index].first; |
| 1469 | |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 1470 | uint32_t raw_member_offset = sro_base[offset_index].second; |
| 1471 | DCHECK_ALIGNED(base_offset, 2); |
| 1472 | DCHECK_ALIGNED(raw_member_offset, 2); |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1473 | |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 1474 | ObjPtr<mirror::Object> obj_ptr = |
| 1475 | reinterpret_cast<mirror::Object*>(space->Begin() + base_offset); |
| 1476 | MemberOffset member_offset(raw_member_offset); |
| 1477 | ObjPtr<mirror::String> referred_string = |
| 1478 | obj_ptr->GetFieldObject<mirror::String, |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1479 | kVerifyNone, |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 1480 | kWithoutReadBarrier, |
| 1481 | /* kIsVolatile= */ false>(member_offset); |
| 1482 | DCHECK(referred_string != nullptr); |
| 1483 | |
| 1484 | ObjPtr<mirror::String> visited = visitor(referred_string); |
| 1485 | if (visited != referred_string) { |
| 1486 | obj_ptr->SetFieldObject</* kTransactionActive= */ false, |
| 1487 | /* kCheckTransaction= */ false, |
| 1488 | kVerifyNone, |
| 1489 | /* kIsVolatile= */ false>(member_offset, visited); |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1490 | } |
| 1491 | } |
| 1492 | } |
| 1493 | |
| 1494 | static void VerifyInternedStringReferences(gc::space::ImageSpace* space) |
| 1495 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1496 | InternTable::UnorderedSet image_interns; |
| 1497 | const ImageSection& section = space->GetImageHeader().GetInternedStringsSection(); |
| 1498 | if (section.Size() > 0) { |
| 1499 | size_t read_count; |
| 1500 | const uint8_t* data = space->Begin() + section.Offset(); |
| 1501 | InternTable::UnorderedSet image_set(data, /*make_copy_of_data=*/ false, &read_count); |
| 1502 | image_set.swap(image_interns); |
| 1503 | } |
| 1504 | size_t num_recorded_refs = 0u; |
| 1505 | VisitInternedStringReferences( |
| 1506 | space, |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1507 | [&image_interns, &num_recorded_refs](ObjPtr<mirror::String> str) |
| 1508 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1509 | auto it = image_interns.find(GcRoot<mirror::String>(str)); |
| 1510 | CHECK(it != image_interns.end()); |
| 1511 | CHECK(it->Read() == str); |
| 1512 | ++num_recorded_refs; |
| 1513 | return str; |
| 1514 | }); |
| 1515 | size_t num_found_refs = CountInternedStringReferences(*space, image_interns); |
| 1516 | CHECK_EQ(num_recorded_refs, num_found_refs); |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1517 | } |
| 1518 | |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1519 | // new_class_set is the set of classes that were read from the class table section in the image. |
| 1520 | // If there was no class table section, it is null. |
| 1521 | // 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] | 1522 | class AppImageLoadingHelper { |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1523 | public: |
Vladimir Marko | 0f3c700 | 2017-09-07 14:15:56 +0100 | [diff] [blame] | 1524 | static void Update( |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1525 | ClassLinker* class_linker, |
| 1526 | gc::space::ImageSpace* space, |
| 1527 | Handle<mirror::ClassLoader> class_loader, |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 1528 | Handle<mirror::ObjectArray<mirror::DexCache>> dex_caches) |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1529 | REQUIRES(!Locks::dex_lock_) |
| 1530 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 1531 | |
Chris Wailes | fbeef46 | 2018-10-19 14:16:35 -0700 | [diff] [blame] | 1532 | static void HandleAppImageStrings(gc::space::ImageSpace* space) |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 1533 | REQUIRES_SHARED(Locks::mutator_lock_); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1534 | }; |
| 1535 | |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 1536 | void AppImageLoadingHelper::Update( |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1537 | ClassLinker* class_linker, |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1538 | gc::space::ImageSpace* space, |
| 1539 | Handle<mirror::ClassLoader> class_loader, |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 1540 | Handle<mirror::ObjectArray<mirror::DexCache>> dex_caches) |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1541 | REQUIRES(!Locks::dex_lock_) |
| 1542 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Chris Wailes | 2386636 | 2018-08-22 16:16:58 -0700 | [diff] [blame] | 1543 | ScopedTrace app_image_timing("AppImage:Updating"); |
| 1544 | |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1545 | if (kIsDebugBuild && ClassLinker::kAppImageMayContainStrings) { |
| 1546 | // In debug build, verify the string references before applying |
| 1547 | // the Runtime::LoadAppImageStartupCache() option. |
| 1548 | VerifyInternedStringReferences(space); |
| 1549 | } |
| 1550 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1551 | Thread* const self = Thread::Current(); |
Mathieu Chartier | a88abfa | 2019-02-04 11:08:29 -0800 | [diff] [blame] | 1552 | Runtime* const runtime = Runtime::Current(); |
| 1553 | gc::Heap* const heap = runtime->GetHeap(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1554 | const ImageHeader& header = space->GetImageHeader(); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1555 | { |
Vladimir Marko | 0f3c700 | 2017-09-07 14:15:56 +0100 | [diff] [blame] | 1556 | // Register dex caches with the class loader. |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1557 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Alex Light | a9bbc08 | 2019-11-14 14:51:41 -0800 | [diff] [blame] | 1558 | for (auto dex_cache : dex_caches.Iterate<mirror::DexCache>()) { |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1559 | const DexFile* const dex_file = dex_cache->GetDexFile(); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1560 | { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 1561 | WriterMutexLock mu2(self, *Locks::dex_lock_); |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 1562 | CHECK(class_linker->FindDexCacheDataLocked(*dex_file) == nullptr); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1563 | class_linker->RegisterDexFileLocked(*dex_file, dex_cache, class_loader.Get()); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1564 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1565 | } |
Mathieu Chartier | a0b9521 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 1566 | } |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1567 | |
Mathieu Chartier | 0933cc5 | 2018-03-23 14:25:08 -0700 | [diff] [blame] | 1568 | if (ClassLinker::kAppImageMayContainStrings) { |
Chris Wailes | fbeef46 | 2018-10-19 14:16:35 -0700 | [diff] [blame] | 1569 | HandleAppImageStrings(space); |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1570 | } |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1571 | |
Mathieu Chartier | a0b9521 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 1572 | if (kVerifyArtMethodDeclaringClasses) { |
Chris Wailes | 2386636 | 2018-08-22 16:16:58 -0700 | [diff] [blame] | 1573 | ScopedTrace timing("AppImage:VerifyDeclaringClasses"); |
Mathieu Chartier | a0b9521 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 1574 | ReaderMutexLock rmu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 9d5956a | 2019-03-22 11:29:08 -0700 | [diff] [blame] | 1575 | gc::accounting::HeapBitmap* live_bitmap = heap->GetLiveBitmap(); |
| 1576 | header.VisitPackedArtMethods([&](ArtMethod& method) |
| 1577 | REQUIRES_SHARED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) { |
| 1578 | ObjPtr<mirror::Class> klass = method.GetDeclaringClassUnchecked(); |
| 1579 | if (klass != nullptr) { |
| 1580 | CHECK(live_bitmap->Test(klass.Ptr())) << "Image method has unmarked declaring class"; |
| 1581 | } |
| 1582 | }, space->Begin(), kRuntimePointerSize); |
Mathieu Chartier | 03c1dd9 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 1583 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1584 | } |
| 1585 | |
Chris Wailes | fbeef46 | 2018-10-19 14:16:35 -0700 | [diff] [blame] | 1586 | void AppImageLoadingHelper::HandleAppImageStrings(gc::space::ImageSpace* space) { |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 1587 | // Iterate over the string reference offsets stored in the image and intern |
| 1588 | // the strings they point to. |
| 1589 | ScopedTrace timing("AppImage:InternString"); |
| 1590 | |
Mathieu Chartier | a88abfa | 2019-02-04 11:08:29 -0800 | [diff] [blame] | 1591 | Runtime* const runtime = Runtime::Current(); |
| 1592 | InternTable* const intern_table = runtime->GetInternTable(); |
| 1593 | |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 1594 | // Add the intern table, removing any conflicts. For conflicts, store the new address in a map |
| 1595 | // for faster lookup. |
| 1596 | // TODO: Optimize with a bitmap or bloom filter |
| 1597 | SafeMap<mirror::String*, mirror::String*> intern_remap; |
Mathieu Chartier | a88abfa | 2019-02-04 11:08:29 -0800 | [diff] [blame] | 1598 | auto func = [&](InternTable::UnorderedSet& interns) |
Mathieu Chartier | 41c0808 | 2018-10-31 11:50:26 -0700 | [diff] [blame] | 1599 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 1600 | REQUIRES(Locks::intern_table_lock_) { |
Mathieu Chartier | 8fc7558 | 2018-11-01 14:21:33 -0700 | [diff] [blame] | 1601 | const size_t non_boot_image_strings = intern_table->CountInterns( |
| 1602 | /*visit_boot_images=*/false, |
| 1603 | /*visit_non_boot_images=*/true); |
Chris Wailes | fbeef46 | 2018-10-19 14:16:35 -0700 | [diff] [blame] | 1604 | VLOG(image) << "AppImage:stringsInInternTableSize = " << interns.size(); |
Mathieu Chartier | 8fc7558 | 2018-11-01 14:21:33 -0700 | [diff] [blame] | 1605 | VLOG(image) << "AppImage:nonBootImageInternStrings = " << non_boot_image_strings; |
| 1606 | // Visit the smaller of the two sets to compute the intersection. |
| 1607 | if (interns.size() < non_boot_image_strings) { |
| 1608 | for (auto it = interns.begin(); it != interns.end(); ) { |
| 1609 | ObjPtr<mirror::String> string = it->Read(); |
| 1610 | ObjPtr<mirror::String> existing = intern_table->LookupWeakLocked(string); |
| 1611 | if (existing == nullptr) { |
| 1612 | existing = intern_table->LookupStrongLocked(string); |
| 1613 | } |
| 1614 | if (existing != nullptr) { |
| 1615 | intern_remap.Put(string.Ptr(), existing.Ptr()); |
| 1616 | it = interns.erase(it); |
| 1617 | } else { |
| 1618 | ++it; |
| 1619 | } |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 1620 | } |
Mathieu Chartier | 8fc7558 | 2018-11-01 14:21:33 -0700 | [diff] [blame] | 1621 | } else { |
| 1622 | intern_table->VisitInterns([&](const GcRoot<mirror::String>& root) |
| 1623 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 1624 | REQUIRES(Locks::intern_table_lock_) { |
| 1625 | auto it = interns.find(root); |
| 1626 | if (it != interns.end()) { |
| 1627 | ObjPtr<mirror::String> existing = root.Read(); |
| 1628 | intern_remap.Put(it->Read(), existing.Ptr()); |
| 1629 | it = interns.erase(it); |
| 1630 | } |
| 1631 | }, /*visit_boot_images=*/false, /*visit_non_boot_images=*/true); |
| 1632 | } |
David Srbecky | 346fd96 | 2020-07-27 16:51:00 +0100 | [diff] [blame] | 1633 | // Consistency check to ensure correctness. |
Mathieu Chartier | 8fc7558 | 2018-11-01 14:21:33 -0700 | [diff] [blame] | 1634 | if (kIsDebugBuild) { |
| 1635 | for (GcRoot<mirror::String>& root : interns) { |
| 1636 | ObjPtr<mirror::String> string = root.Read(); |
| 1637 | CHECK(intern_table->LookupWeakLocked(string) == nullptr) << string->ToModifiedUtf8(); |
| 1638 | CHECK(intern_table->LookupStrongLocked(string) == nullptr) << string->ToModifiedUtf8(); |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 1639 | } |
| 1640 | } |
Mathieu Chartier | a88abfa | 2019-02-04 11:08:29 -0800 | [diff] [blame] | 1641 | }; |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1642 | intern_table->AddImageStringsToTable(space, func); |
| 1643 | if (!intern_remap.empty()) { |
Mathieu Chartier | a88abfa | 2019-02-04 11:08:29 -0800 | [diff] [blame] | 1644 | VLOG(image) << "AppImage:conflictingInternStrings = " << intern_remap.size(); |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1645 | VisitInternedStringReferences( |
| 1646 | space, |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1647 | [&intern_remap](ObjPtr<mirror::String> str) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1648 | auto it = intern_remap.find(str.Ptr()); |
| 1649 | if (it != intern_remap.end()) { |
| 1650 | return ObjPtr<mirror::String>(it->second); |
| 1651 | } |
| 1652 | return str; |
| 1653 | }); |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 1654 | } |
| 1655 | } |
| 1656 | |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1657 | static std::unique_ptr<const DexFile> OpenOatDexFile(const OatFile* oat_file, |
| 1658 | const char* location, |
| 1659 | std::string* error_msg) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1660 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1661 | DCHECK(error_msg != nullptr); |
| 1662 | std::unique_ptr<const DexFile> dex_file; |
Andreas Gampe | b40d361 | 2018-06-26 15:49:42 -0700 | [diff] [blame] | 1663 | const OatDexFile* oat_dex_file = oat_file->GetOatDexFile(location, nullptr, error_msg); |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1664 | if (oat_dex_file == nullptr) { |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1665 | return std::unique_ptr<const DexFile>(); |
| 1666 | } |
| 1667 | std::string inner_error_msg; |
| 1668 | dex_file = oat_dex_file->OpenDexFile(&inner_error_msg); |
| 1669 | if (dex_file == nullptr) { |
| 1670 | *error_msg = StringPrintf("Failed to open dex file %s from within oat file %s error '%s'", |
| 1671 | location, |
| 1672 | oat_file->GetLocation().c_str(), |
| 1673 | inner_error_msg.c_str()); |
| 1674 | return std::unique_ptr<const DexFile>(); |
| 1675 | } |
| 1676 | |
| 1677 | if (dex_file->GetLocationChecksum() != oat_dex_file->GetDexFileLocationChecksum()) { |
| 1678 | *error_msg = StringPrintf("Checksums do not match for %s: %x vs %x", |
| 1679 | location, |
| 1680 | dex_file->GetLocationChecksum(), |
| 1681 | oat_dex_file->GetDexFileLocationChecksum()); |
| 1682 | return std::unique_ptr<const DexFile>(); |
| 1683 | } |
| 1684 | return dex_file; |
| 1685 | } |
| 1686 | |
| 1687 | bool ClassLinker::OpenImageDexFiles(gc::space::ImageSpace* space, |
| 1688 | std::vector<std::unique_ptr<const DexFile>>* out_dex_files, |
| 1689 | std::string* error_msg) { |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 1690 | ScopedAssertNoThreadSuspension nts(__FUNCTION__); |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1691 | const ImageHeader& header = space->GetImageHeader(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1692 | ObjPtr<mirror::Object> dex_caches_object = header.GetImageRoot(ImageHeader::kDexCaches); |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1693 | DCHECK(dex_caches_object != nullptr); |
Vladimir Marko | 4617d58 | 2019-03-28 13:48:31 +0000 | [diff] [blame] | 1694 | ObjPtr<mirror::ObjectArray<mirror::DexCache>> dex_caches = |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1695 | dex_caches_object->AsObjectArray<mirror::DexCache>(); |
| 1696 | const OatFile* oat_file = space->GetOatFile(); |
Alex Light | a9bbc08 | 2019-11-14 14:51:41 -0800 | [diff] [blame] | 1697 | for (auto dex_cache : dex_caches->Iterate()) { |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1698 | std::string dex_file_location(dex_cache->GetLocation()->ToModifiedUtf8()); |
| 1699 | std::unique_ptr<const DexFile> dex_file = OpenOatDexFile(oat_file, |
| 1700 | dex_file_location.c_str(), |
| 1701 | error_msg); |
| 1702 | if (dex_file == nullptr) { |
| 1703 | return false; |
| 1704 | } |
| 1705 | dex_cache->SetDexFile(dex_file.get()); |
| 1706 | out_dex_files->push_back(std::move(dex_file)); |
| 1707 | } |
| 1708 | return true; |
| 1709 | } |
| 1710 | |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1711 | // Helper class for ArtMethod checks when adding an image. Keeps all required functionality |
| 1712 | // together and caches some intermediate results. |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 1713 | class ImageChecker final { |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1714 | public: |
| 1715 | static void CheckObjects(gc::Heap* heap, ClassLinker* class_linker) |
| 1716 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 1717 | ImageChecker ic(heap, class_linker); |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 1718 | auto visitor = [&](mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1719 | DCHECK(obj != nullptr); |
| 1720 | CHECK(obj->GetClass() != nullptr) << "Null class in object " << obj; |
| 1721 | CHECK(obj->GetClass()->GetClass() != nullptr) << "Null class class " << obj; |
| 1722 | if (obj->IsClass()) { |
| 1723 | auto klass = obj->AsClass(); |
| 1724 | for (ArtField& field : klass->GetIFields()) { |
| 1725 | CHECK_EQ(field.GetDeclaringClass(), klass); |
| 1726 | } |
| 1727 | for (ArtField& field : klass->GetSFields()) { |
| 1728 | CHECK_EQ(field.GetDeclaringClass(), klass); |
| 1729 | } |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 1730 | const PointerSize pointer_size = ic.pointer_size_; |
Vladimir Marko | c524e9e | 2019-03-26 10:54:50 +0000 | [diff] [blame] | 1731 | for (ArtMethod& m : klass->GetMethods(pointer_size)) { |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 1732 | ic.CheckArtMethod(&m, klass); |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 1733 | } |
Vladimir Marko | c524e9e | 2019-03-26 10:54:50 +0000 | [diff] [blame] | 1734 | ObjPtr<mirror::PointerArray> vtable = klass->GetVTable(); |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 1735 | if (vtable != nullptr) { |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 1736 | ic.CheckArtMethodPointerArray(vtable, nullptr); |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 1737 | } |
| 1738 | if (klass->ShouldHaveImt()) { |
| 1739 | ImTable* imt = klass->GetImt(pointer_size); |
| 1740 | for (size_t i = 0; i < ImTable::kSize; ++i) { |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 1741 | ic.CheckArtMethod(imt->Get(i, pointer_size), nullptr); |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 1742 | } |
| 1743 | } |
| 1744 | if (klass->ShouldHaveEmbeddedVTable()) { |
| 1745 | for (int32_t i = 0; i < klass->GetEmbeddedVTableLength(); ++i) { |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 1746 | ic.CheckArtMethod(klass->GetEmbeddedVTableEntry(i, pointer_size), nullptr); |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 1747 | } |
| 1748 | } |
Vladimir Marko | c524e9e | 2019-03-26 10:54:50 +0000 | [diff] [blame] | 1749 | ObjPtr<mirror::IfTable> iftable = klass->GetIfTable(); |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 1750 | for (int32_t i = 0; i < klass->GetIfTableCount(); ++i) { |
| 1751 | if (iftable->GetMethodArrayCount(i) > 0) { |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 1752 | ic.CheckArtMethodPointerArray(iftable->GetMethodArray(i), nullptr); |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 1753 | } |
| 1754 | } |
| 1755 | } |
| 1756 | }; |
| 1757 | heap->VisitObjects(visitor); |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1758 | } |
| 1759 | |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1760 | private: |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 1761 | ImageChecker(gc::Heap* heap, ClassLinker* class_linker) |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1762 | : spaces_(heap->GetBootImageSpaces()), |
| 1763 | pointer_size_(class_linker->GetImagePointerSize()) { |
| 1764 | space_begin_.reserve(spaces_.size()); |
| 1765 | method_sections_.reserve(spaces_.size()); |
| 1766 | runtime_method_sections_.reserve(spaces_.size()); |
| 1767 | for (gc::space::ImageSpace* space : spaces_) { |
| 1768 | space_begin_.push_back(space->Begin()); |
| 1769 | auto& header = space->GetImageHeader(); |
| 1770 | method_sections_.push_back(&header.GetMethodsSection()); |
| 1771 | runtime_method_sections_.push_back(&header.GetRuntimeMethodsSection()); |
| 1772 | } |
| 1773 | } |
| 1774 | |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 1775 | void CheckArtMethod(ArtMethod* m, ObjPtr<mirror::Class> expected_class) |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1776 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1777 | if (m->IsRuntimeMethod()) { |
| 1778 | ObjPtr<mirror::Class> declaring_class = m->GetDeclaringClassUnchecked(); |
| 1779 | CHECK(declaring_class == nullptr) << declaring_class << " " << m->PrettyMethod(); |
| 1780 | } else if (m->IsCopied()) { |
| 1781 | CHECK(m->GetDeclaringClass() != nullptr) << m->PrettyMethod(); |
| 1782 | } else if (expected_class != nullptr) { |
| 1783 | CHECK_EQ(m->GetDeclaringClassUnchecked(), expected_class) << m->PrettyMethod(); |
| 1784 | } |
| 1785 | if (!spaces_.empty()) { |
| 1786 | bool contains = false; |
| 1787 | for (size_t i = 0; !contains && i != space_begin_.size(); ++i) { |
| 1788 | const size_t offset = reinterpret_cast<uint8_t*>(m) - space_begin_[i]; |
| 1789 | contains = method_sections_[i]->Contains(offset) || |
| 1790 | runtime_method_sections_[i]->Contains(offset); |
| 1791 | } |
| 1792 | CHECK(contains) << m << " not found"; |
| 1793 | } |
| 1794 | } |
| 1795 | |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 1796 | void CheckArtMethodPointerArray(ObjPtr<mirror::PointerArray> arr, |
| 1797 | ObjPtr<mirror::Class> expected_class) |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1798 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1799 | CHECK(arr != nullptr); |
| 1800 | for (int32_t j = 0; j < arr->GetLength(); ++j) { |
| 1801 | auto* method = arr->GetElementPtrSize<ArtMethod*>(j, pointer_size_); |
| 1802 | // expected_class == null means we are a dex cache. |
| 1803 | if (expected_class != nullptr) { |
| 1804 | CHECK(method != nullptr); |
| 1805 | } |
| 1806 | if (method != nullptr) { |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 1807 | CheckArtMethod(method, expected_class); |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1808 | } |
| 1809 | } |
| 1810 | } |
| 1811 | |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1812 | const std::vector<gc::space::ImageSpace*>& spaces_; |
| 1813 | const PointerSize pointer_size_; |
| 1814 | |
| 1815 | // Cached sections from the spaces. |
| 1816 | std::vector<const uint8_t*> space_begin_; |
| 1817 | std::vector<const ImageSection*> method_sections_; |
| 1818 | std::vector<const ImageSection*> runtime_method_sections_; |
| 1819 | }; |
| 1820 | |
Andreas Gampe | be7af22 | 2017-07-25 09:57:28 -0700 | [diff] [blame] | 1821 | static void VerifyAppImage(const ImageHeader& header, |
| 1822 | const Handle<mirror::ClassLoader>& class_loader, |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 1823 | ClassTable* class_table, |
| 1824 | gc::space::ImageSpace* space) |
Andreas Gampe | be7af22 | 2017-07-25 09:57:28 -0700 | [diff] [blame] | 1825 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 9d5956a | 2019-03-22 11:29:08 -0700 | [diff] [blame] | 1826 | header.VisitPackedArtMethods([&](ArtMethod& method) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1827 | ObjPtr<mirror::Class> klass = method.GetDeclaringClass(); |
| 1828 | if (klass != nullptr && !Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(klass)) { |
| 1829 | CHECK_EQ(class_table->LookupByDescriptor(klass), klass) |
| 1830 | << mirror::Class::PrettyClass(klass); |
| 1831 | } |
| 1832 | }, space->Begin(), kRuntimePointerSize); |
Andreas Gampe | be7af22 | 2017-07-25 09:57:28 -0700 | [diff] [blame] | 1833 | { |
| 1834 | // Verify that all direct interfaces of classes in the class table are also resolved. |
| 1835 | std::vector<ObjPtr<mirror::Class>> classes; |
| 1836 | auto verify_direct_interfaces_in_table = [&](ObjPtr<mirror::Class> klass) |
| 1837 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1838 | if (!klass->IsPrimitive() && klass->GetClassLoader() == class_loader.Get()) { |
| 1839 | classes.push_back(klass); |
| 1840 | } |
| 1841 | return true; |
| 1842 | }; |
| 1843 | class_table->Visit(verify_direct_interfaces_in_table); |
| 1844 | Thread* self = Thread::Current(); |
| 1845 | for (ObjPtr<mirror::Class> klass : classes) { |
| 1846 | for (uint32_t i = 0, num = klass->NumDirectInterfaces(); i != num; ++i) { |
| 1847 | CHECK(klass->GetDirectInterface(self, klass, i) != nullptr) |
| 1848 | << klass->PrettyDescriptor() << " iface #" << i; |
| 1849 | } |
| 1850 | } |
| 1851 | } |
Andreas Gampe | be7af22 | 2017-07-25 09:57:28 -0700 | [diff] [blame] | 1852 | } |
| 1853 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1854 | bool ClassLinker::AddImageSpace( |
| 1855 | gc::space::ImageSpace* space, |
| 1856 | Handle<mirror::ClassLoader> class_loader, |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1857 | std::vector<std::unique_ptr<const DexFile>>* out_dex_files, |
| 1858 | std::string* error_msg) { |
| 1859 | DCHECK(out_dex_files != nullptr); |
| 1860 | DCHECK(error_msg != nullptr); |
| 1861 | const uint64_t start_time = NanoTime(); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 1862 | const bool app_image = class_loader != nullptr; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1863 | const ImageHeader& header = space->GetImageHeader(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1864 | ObjPtr<mirror::Object> dex_caches_object = header.GetImageRoot(ImageHeader::kDexCaches); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1865 | DCHECK(dex_caches_object != nullptr); |
| 1866 | Runtime* const runtime = Runtime::Current(); |
| 1867 | gc::Heap* const heap = runtime->GetHeap(); |
| 1868 | Thread* const self = Thread::Current(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1869 | // Check that the image is what we are expecting. |
| 1870 | if (image_pointer_size_ != space->GetImageHeader().GetPointerSize()) { |
| 1871 | *error_msg = StringPrintf("Application image pointer size does not match runtime: %zu vs %zu", |
| 1872 | static_cast<size_t>(space->GetImageHeader().GetPointerSize()), |
| 1873 | image_pointer_size_); |
| 1874 | return false; |
| 1875 | } |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1876 | size_t expected_image_roots = ImageHeader::NumberOfImageRoots(app_image); |
| 1877 | if (static_cast<size_t>(header.GetImageRoots()->GetLength()) != expected_image_roots) { |
| 1878 | *error_msg = StringPrintf("Expected %zu image roots but got %d", |
| 1879 | expected_image_roots, |
| 1880 | header.GetImageRoots()->GetLength()); |
| 1881 | return false; |
| 1882 | } |
| 1883 | StackHandleScope<3> hs(self); |
| 1884 | Handle<mirror::ObjectArray<mirror::DexCache>> dex_caches( |
| 1885 | hs.NewHandle(dex_caches_object->AsObjectArray<mirror::DexCache>())); |
| 1886 | Handle<mirror::ObjectArray<mirror::Class>> class_roots(hs.NewHandle( |
| 1887 | header.GetImageRoot(ImageHeader::kClassRoots)->AsObjectArray<mirror::Class>())); |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1888 | MutableHandle<mirror::ClassLoader> image_class_loader(hs.NewHandle( |
Vladimir Marko | f75613c | 2018-06-05 12:51:04 +0100 | [diff] [blame] | 1889 | app_image ? header.GetImageRoot(ImageHeader::kAppImageClassLoader)->AsClassLoader() |
| 1890 | : nullptr)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 1891 | DCHECK(class_roots != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 1892 | if (class_roots->GetLength() != static_cast<int32_t>(ClassRoot::kMax)) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1893 | *error_msg = StringPrintf("Expected %d class roots but got %d", |
| 1894 | class_roots->GetLength(), |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 1895 | static_cast<int32_t>(ClassRoot::kMax)); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1896 | return false; |
| 1897 | } |
| 1898 | // 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] | 1899 | ObjPtr<mirror::ObjectArray<mirror::Class>> existing_class_roots = GetClassRoots(); |
| 1900 | for (size_t i = 0; i < static_cast<size_t>(ClassRoot::kMax); i++) { |
| 1901 | 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] | 1902 | *error_msg = "App image class roots must have pointer equality with runtime ones."; |
| 1903 | return false; |
| 1904 | } |
| 1905 | } |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1906 | const OatFile* oat_file = space->GetOatFile(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1907 | if (oat_file->GetOatHeader().GetDexFileCount() != |
| 1908 | static_cast<uint32_t>(dex_caches->GetLength())) { |
| 1909 | *error_msg = "Dex cache count and dex file count mismatch while trying to initialize from " |
| 1910 | "image"; |
| 1911 | return false; |
| 1912 | } |
| 1913 | |
Alex Light | a9bbc08 | 2019-11-14 14:51:41 -0800 | [diff] [blame] | 1914 | for (auto dex_cache : dex_caches.Iterate<mirror::DexCache>()) { |
David Brazdil | 3e8aae0 | 2019-03-26 18:48:02 +0000 | [diff] [blame] | 1915 | std::string dex_file_location = dex_cache->GetLocation()->ToModifiedUtf8(); |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1916 | std::unique_ptr<const DexFile> dex_file = OpenOatDexFile(oat_file, |
| 1917 | dex_file_location.c_str(), |
| 1918 | error_msg); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1919 | if (dex_file == nullptr) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1920 | return false; |
| 1921 | } |
| 1922 | |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 1923 | LinearAlloc* linear_alloc = GetOrCreateAllocatorForClassLoader(class_loader.Get()); |
| 1924 | DCHECK(linear_alloc != nullptr); |
| 1925 | DCHECK_EQ(linear_alloc == Runtime::Current()->GetLinearAlloc(), !app_image); |
| 1926 | { |
| 1927 | // Native fields are all null. Initialize them and allocate native memory. |
| 1928 | WriterMutexLock mu(self, *Locks::dex_lock_); |
| 1929 | dex_cache->InitializeNativeFields(dex_file.get(), linear_alloc); |
| 1930 | } |
| 1931 | if (!app_image) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1932 | // Register dex files, keep track of existing ones that are conflicts. |
Mathieu Chartier | 0a19e21 | 2019-11-27 14:35:24 -0800 | [diff] [blame] | 1933 | AppendToBootClassPath(dex_file.get(), dex_cache); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1934 | } |
| 1935 | out_dex_files->push_back(std::move(dex_file)); |
| 1936 | } |
| 1937 | |
| 1938 | if (app_image) { |
| 1939 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Nicolas Geoffray | f0d3002 | 2018-11-20 17:45:38 +0000 | [diff] [blame] | 1940 | ScopedAssertNoThreadSuspension sants("Checking app image", soa.Self()); |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1941 | if (IsBootClassLoader(soa, image_class_loader.Get())) { |
| 1942 | *error_msg = "Unexpected BootClassLoader in app image"; |
| 1943 | return false; |
| 1944 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1945 | } |
| 1946 | |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 1947 | if (kCheckImageObjects) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1948 | if (!app_image) { |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 1949 | ImageChecker::CheckObjects(heap, this); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1950 | } |
| 1951 | } |
| 1952 | |
| 1953 | // Set entry point to interpreter if in InterpretOnly mode. |
| 1954 | if (!runtime->IsAotCompiler() && runtime->GetInstrumentation()->InterpretOnly()) { |
Mathieu Chartier | 9d5956a | 2019-03-22 11:29:08 -0700 | [diff] [blame] | 1955 | // Set image methods' entry point to interpreter. |
| 1956 | header.VisitPackedArtMethods([&](ArtMethod& method) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1957 | if (!method.IsRuntimeMethod()) { |
| 1958 | DCHECK(method.GetDeclaringClass() != nullptr); |
Ulya Trafimovich | 5439f05 | 2020-07-29 10:03:46 +0100 | [diff] [blame] | 1959 | if (!method.IsNative() && !method.IsResolutionMethod()) { |
| 1960 | method.SetEntryPointFromQuickCompiledCodePtrSize(GetQuickToInterpreterBridge(), |
| 1961 | image_pointer_size_); |
Mathieu Chartier | 9d5956a | 2019-03-22 11:29:08 -0700 | [diff] [blame] | 1962 | } |
| 1963 | } |
| 1964 | }, space->Begin(), image_pointer_size_); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1965 | } |
| 1966 | |
Nicolas Geoffray | 4717175 | 2020-08-31 15:03:20 +0100 | [diff] [blame] | 1967 | if (!runtime->IsAotCompiler()) { |
Nicolas Geoffray | bd728b0 | 2021-01-27 13:21:35 +0000 | [diff] [blame] | 1968 | ScopedTrace trace("AppImage:UpdateCodeItemAndNterp"); |
Nicolas Geoffray | 4717175 | 2020-08-31 15:03:20 +0100 | [diff] [blame] | 1969 | bool can_use_nterp = interpreter::CanRuntimeUseNterp(); |
Nicolas Geoffray | 7e2c963 | 2020-01-09 13:41:10 +0000 | [diff] [blame] | 1970 | header.VisitPackedArtMethods([&](ArtMethod& method) REQUIRES_SHARED(Locks::mutator_lock_) { |
Nicolas Geoffray | 4717175 | 2020-08-31 15:03:20 +0100 | [diff] [blame] | 1971 | // In the image, the `data` pointer field of the ArtMethod contains the code |
| 1972 | // item offset. Change this to the actual pointer to the code item. |
| 1973 | if (method.HasCodeItem()) { |
| 1974 | const dex::CodeItem* code_item = method.GetDexFile()->GetCodeItem( |
| 1975 | reinterpret_cast32<uint32_t>(method.GetDataPtrSize(image_pointer_size_))); |
Nicolas Geoffray | e1d2dce | 2020-09-21 10:06:31 +0100 | [diff] [blame] | 1976 | method.SetCodeItem(code_item); |
Nicolas Geoffray | 4717175 | 2020-08-31 15:03:20 +0100 | [diff] [blame] | 1977 | } |
| 1978 | // Set image methods' entry point that point to the interpreter bridge to the |
| 1979 | // nterp entry point. |
Nicolas Geoffray | c39af94 | 2021-01-25 08:43:57 +0000 | [diff] [blame] | 1980 | if (method.GetEntryPointFromQuickCompiledCode() == nterp_trampoline_) { |
| 1981 | if (can_use_nterp) { |
Nicolas Geoffray | b1cf837 | 2021-02-02 13:32:20 +0000 | [diff] [blame] | 1982 | DCHECK(!NeedsClinitCheckBeforeCall(&method) || |
| 1983 | method.GetDeclaringClass()->IsVisiblyInitialized()); |
Nicolas Geoffray | c39af94 | 2021-01-25 08:43:57 +0000 | [diff] [blame] | 1984 | method.SetEntryPointFromQuickCompiledCode(interpreter::GetNterpEntryPoint()); |
| 1985 | } else { |
| 1986 | method.SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge()); |
| 1987 | } |
Nicolas Geoffray | 4717175 | 2020-08-31 15:03:20 +0100 | [diff] [blame] | 1988 | } |
Nicolas Geoffray | 7e2c963 | 2020-01-09 13:41:10 +0000 | [diff] [blame] | 1989 | }, space->Begin(), image_pointer_size_); |
| 1990 | } |
| 1991 | |
Nicolas Geoffray | 8c41a0b | 2020-02-06 16:52:11 +0000 | [diff] [blame] | 1992 | if (runtime->IsVerificationSoftFail()) { |
| 1993 | header.VisitPackedArtMethods([&](ArtMethod& method) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1994 | if (!method.IsNative() && method.IsInvokable()) { |
| 1995 | method.ClearSkipAccessChecks(); |
| 1996 | } |
| 1997 | }, space->Begin(), image_pointer_size_); |
| 1998 | } |
| 1999 | |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 2000 | ClassTable* class_table = nullptr; |
| 2001 | { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2002 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 2003 | class_table = InsertClassTableForClassLoader(class_loader.Get()); |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 2004 | } |
| 2005 | // If we have a class table section, read it and use it for verification in |
| 2006 | // UpdateAppImageClassLoadersAndDexCaches. |
| 2007 | ClassTable::ClassSet temp_set; |
Vladimir Marko | 0f3c700 | 2017-09-07 14:15:56 +0100 | [diff] [blame] | 2008 | const ImageSection& class_table_section = header.GetClassTableSection(); |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 2009 | const bool added_class_table = class_table_section.Size() > 0u; |
| 2010 | if (added_class_table) { |
| 2011 | const uint64_t start_time2 = NanoTime(); |
| 2012 | size_t read_count = 0; |
| 2013 | temp_set = ClassTable::ClassSet(space->Begin() + class_table_section.Offset(), |
| 2014 | /*make copy*/false, |
| 2015 | &read_count); |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 2016 | VLOG(image) << "Adding class table classes took " << PrettyDuration(NanoTime() - start_time2); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2017 | } |
| 2018 | if (app_image) { |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 2019 | AppImageLoadingHelper::Update(this, space, class_loader, dex_caches); |
Mathieu Chartier | 456b492 | 2018-11-06 10:35:48 -0800 | [diff] [blame] | 2020 | |
| 2021 | { |
| 2022 | ScopedTrace trace("AppImage:UpdateClassLoaders"); |
| 2023 | // Update class loader and resolved strings. If added_class_table is false, the resolved |
| 2024 | // strings were forwarded UpdateAppImageClassLoadersAndDexCaches. |
Mathieu Chartier | bc1e0fa | 2018-11-14 16:18:18 -0800 | [diff] [blame] | 2025 | ObjPtr<mirror::ClassLoader> loader(class_loader.Get()); |
Mathieu Chartier | 456b492 | 2018-11-06 10:35:48 -0800 | [diff] [blame] | 2026 | for (const ClassTable::TableSlot& root : temp_set) { |
Mathieu Chartier | bc1e0fa | 2018-11-14 16:18:18 -0800 | [diff] [blame] | 2027 | // Note: We probably don't need the read barrier unless we copy the app image objects into |
| 2028 | // the region space. |
| 2029 | ObjPtr<mirror::Class> klass(root.Read()); |
| 2030 | // Do not update class loader for boot image classes where the app image |
| 2031 | // class loader is only the initiating loader but not the defining loader. |
| 2032 | // Avoid read barrier since we are comparing against null. |
| 2033 | if (klass->GetClassLoader<kDefaultVerifyFlags, kWithoutReadBarrier>() != nullptr) { |
Vladimir Marko | b68bb7a | 2020-03-17 10:55:25 +0000 | [diff] [blame] | 2034 | klass->SetClassLoader(loader); |
Mathieu Chartier | bc1e0fa | 2018-11-14 16:18:18 -0800 | [diff] [blame] | 2035 | } |
Mathieu Chartier | 456b492 | 2018-11-06 10:35:48 -0800 | [diff] [blame] | 2036 | } |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 2037 | } |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 2038 | |
Vladimir Marko | 305c38b | 2018-02-14 11:50:07 +0000 | [diff] [blame] | 2039 | if (kBitstringSubtypeCheckEnabled) { |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 2040 | // Every class in the app image has initially SubtypeCheckInfo in the |
| 2041 | // Uninitialized state. |
| 2042 | // |
| 2043 | // The SubtypeCheck invariants imply that a SubtypeCheckInfo is at least Initialized |
| 2044 | // after class initialization is complete. The app image ClassStatus as-is |
| 2045 | // are almost all ClassStatus::Initialized, and being in the |
| 2046 | // SubtypeCheckInfo::kUninitialized state is violating that invariant. |
| 2047 | // |
| 2048 | // Force every app image class's SubtypeCheck to be at least kIninitialized. |
| 2049 | // |
| 2050 | // See also ImageWriter::FixupClass. |
Chris Wailes | 2386636 | 2018-08-22 16:16:58 -0700 | [diff] [blame] | 2051 | ScopedTrace trace("AppImage:RecacluateSubtypeCheckBitstrings"); |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 2052 | MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_); |
| 2053 | for (const ClassTable::TableSlot& root : temp_set) { |
Vladimir Marko | 38b8b25 | 2018-01-02 19:07:06 +0000 | [diff] [blame] | 2054 | SubtypeCheck<ObjPtr<mirror::Class>>::EnsureInitialized(root.Read()); |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 2055 | } |
| 2056 | } |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 2057 | } |
| 2058 | if (!oat_file->GetBssGcRoots().empty()) { |
| 2059 | // Insert oat file to class table for visiting .bss GC roots. |
| 2060 | class_table->InsertOatFile(oat_file); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2061 | } |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 2062 | |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 2063 | if (added_class_table) { |
| 2064 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 2065 | class_table->AddClassSet(std::move(temp_set)); |
| 2066 | } |
Andreas Gampe | be7af22 | 2017-07-25 09:57:28 -0700 | [diff] [blame] | 2067 | |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 2068 | if (kIsDebugBuild && app_image) { |
| 2069 | // This verification needs to happen after the classes have been added to the class loader. |
| 2070 | // Since it ensures classes are in the class table. |
Chris Wailes | 2386636 | 2018-08-22 16:16:58 -0700 | [diff] [blame] | 2071 | ScopedTrace trace("AppImage:Verify"); |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 2072 | VerifyAppImage(header, class_loader, class_table, space); |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 2073 | } |
Andreas Gampe | be7af22 | 2017-07-25 09:57:28 -0700 | [diff] [blame] | 2074 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2075 | VLOG(class_linker) << "Adding image space took " << PrettyDuration(NanoTime() - start_time); |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 2076 | return true; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 2077 | } |
| 2078 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2079 | bool ClassLinker::ClassInClassTable(ObjPtr<mirror::Class> klass) { |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 2080 | ClassTable* const class_table = ClassTableForClassLoader(klass->GetClassLoader()); |
| 2081 | return class_table != nullptr && class_table->Contains(klass); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2082 | } |
| 2083 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 2084 | void ClassLinker::VisitClassRoots(RootVisitor* visitor, VisitRootFlags flags) { |
Mathieu Chartier | 7778b88 | 2015-10-05 16:41:10 -0700 | [diff] [blame] | 2085 | // Acquire tracing_enabled before locking class linker lock to prevent lock order violation. Since |
| 2086 | // enabling tracing requires the mutator lock, there are no race conditions here. |
| 2087 | const bool tracing_enabled = Trace::IsTracingEnabled(); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2088 | Thread* const self = Thread::Current(); |
| 2089 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Vladimir Marko | 9b03cb4 | 2017-02-16 16:37:03 +0000 | [diff] [blame] | 2090 | if (kUseReadBarrier) { |
| 2091 | // We do not track new roots for CC. |
| 2092 | DCHECK_EQ(0, flags & (kVisitRootFlagNewRoots | |
| 2093 | kVisitRootFlagClearRootLog | |
| 2094 | kVisitRootFlagStartLoggingNewRoots | |
| 2095 | kVisitRootFlagStopLoggingNewRoots)); |
| 2096 | } |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2097 | if ((flags & kVisitRootFlagAllRoots) != 0) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2098 | // Argument for how root visiting deals with ArtField and ArtMethod roots. |
| 2099 | // There is 3 GC cases to handle: |
| 2100 | // Non moving concurrent: |
| 2101 | // 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] | 2102 | // live by the class and class roots. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2103 | // |
| 2104 | // Moving non-concurrent: |
| 2105 | // This case needs to call visit VisitNativeRoots in case the classes or dex cache arrays move. |
| 2106 | // To prevent missing roots, this case needs to ensure that there is no |
| 2107 | // suspend points between the point which we allocate ArtMethod arrays and place them in a |
| 2108 | // class which is in the class table. |
| 2109 | // |
| 2110 | // Moving concurrent: |
| 2111 | // Need to make sure to not copy ArtMethods without doing read barriers since the roots are |
| 2112 | // 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] | 2113 | // |
| 2114 | // Use an unbuffered visitor since the class table uses a temporary GcRoot for holding decoded |
| 2115 | // ClassTable::TableSlot. The buffered root visiting would access a stale stack location for |
| 2116 | // these objects. |
| 2117 | UnbufferedRootVisitor root_visitor(visitor, RootInfo(kRootStickyClass)); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 2118 | boot_class_table_->VisitRoots(root_visitor); |
Mathieu Chartier | 7778b88 | 2015-10-05 16:41:10 -0700 | [diff] [blame] | 2119 | // 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] | 2120 | if ((flags & kVisitRootFlagClassLoader) != 0 || tracing_enabled) { |
Mathieu Chartier | 7778b88 | 2015-10-05 16:41:10 -0700 | [diff] [blame] | 2121 | for (const ClassLoaderData& data : class_loaders_) { |
| 2122 | GcRoot<mirror::Object> root(GcRoot<mirror::Object>(self->DecodeJObject(data.weak_root))); |
| 2123 | root.VisitRoot(visitor, RootInfo(kRootVMInternal)); |
| 2124 | } |
| 2125 | } |
Vladimir Marko | 9b03cb4 | 2017-02-16 16:37:03 +0000 | [diff] [blame] | 2126 | } else if (!kUseReadBarrier && (flags & kVisitRootFlagNewRoots) != 0) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 2127 | for (auto& root : new_class_roots_) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2128 | ObjPtr<mirror::Class> old_ref = root.Read<kWithoutReadBarrier>(); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 2129 | root.VisitRoot(visitor, RootInfo(kRootStickyClass)); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2130 | ObjPtr<mirror::Class> new_ref = root.Read<kWithoutReadBarrier>(); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 2131 | // Concurrent moving GC marked new roots through the to-space invariant. |
| 2132 | CHECK_EQ(new_ref, old_ref); |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2133 | } |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 2134 | for (const OatFile* oat_file : new_bss_roots_boot_oat_files_) { |
| 2135 | for (GcRoot<mirror::Object>& root : oat_file->GetBssGcRoots()) { |
| 2136 | ObjPtr<mirror::Object> old_ref = root.Read<kWithoutReadBarrier>(); |
| 2137 | if (old_ref != nullptr) { |
| 2138 | DCHECK(old_ref->IsClass()); |
| 2139 | root.VisitRoot(visitor, RootInfo(kRootStickyClass)); |
| 2140 | ObjPtr<mirror::Object> new_ref = root.Read<kWithoutReadBarrier>(); |
| 2141 | // Concurrent moving GC marked new roots through the to-space invariant. |
| 2142 | CHECK_EQ(new_ref, old_ref); |
| 2143 | } |
| 2144 | } |
| 2145 | } |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2146 | } |
Vladimir Marko | 9b03cb4 | 2017-02-16 16:37:03 +0000 | [diff] [blame] | 2147 | if (!kUseReadBarrier && (flags & kVisitRootFlagClearRootLog) != 0) { |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2148 | new_class_roots_.clear(); |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 2149 | new_bss_roots_boot_oat_files_.clear(); |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2150 | } |
Vladimir Marko | 9b03cb4 | 2017-02-16 16:37:03 +0000 | [diff] [blame] | 2151 | if (!kUseReadBarrier && (flags & kVisitRootFlagStartLoggingNewRoots) != 0) { |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 2152 | log_new_roots_ = true; |
Vladimir Marko | 9b03cb4 | 2017-02-16 16:37:03 +0000 | [diff] [blame] | 2153 | } else if (!kUseReadBarrier && (flags & kVisitRootFlagStopLoggingNewRoots) != 0) { |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 2154 | log_new_roots_ = false; |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2155 | } |
| 2156 | // We deliberately ignore the class roots in the image since we |
| 2157 | // handle image roots by using the MS/CMS rescanning of dirty cards. |
| 2158 | } |
| 2159 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 2160 | // Keep in sync with InitCallback. Anything we visit, we need to |
| 2161 | // reinit references to when reinitializing a ClassLinker from a |
| 2162 | // mapped image. |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 2163 | void ClassLinker::VisitRoots(RootVisitor* visitor, VisitRootFlags flags) { |
Mathieu Chartier | 3100080 | 2015-06-14 14:14:37 -0700 | [diff] [blame] | 2164 | class_roots_.VisitRootIfNonNull(visitor, RootInfo(kRootVMInternal)); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 2165 | VisitClassRoots(visitor, flags); |
Mathieu Chartier | 6cfc2c0 | 2015-10-12 15:06:16 -0700 | [diff] [blame] | 2166 | // Instead of visiting the find_array_class_cache_ drop it so that it doesn't prevent class |
| 2167 | // unloading if we are marking roots. |
| 2168 | DropFindArrayClassCache(); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 2169 | } |
| 2170 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2171 | class VisitClassLoaderClassesVisitor : public ClassLoaderVisitor { |
| 2172 | public: |
| 2173 | explicit VisitClassLoaderClassesVisitor(ClassVisitor* visitor) |
| 2174 | : visitor_(visitor), |
| 2175 | done_(false) {} |
| 2176 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2177 | void Visit(ObjPtr<mirror::ClassLoader> class_loader) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 2178 | REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_) override { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2179 | ClassTable* const class_table = class_loader->GetClassTable(); |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2180 | if (!done_ && class_table != nullptr) { |
| 2181 | DefiningClassLoaderFilterVisitor visitor(class_loader, visitor_); |
| 2182 | if (!class_table->Visit(visitor)) { |
| 2183 | // If the visitor ClassTable returns false it means that we don't need to continue. |
| 2184 | done_ = true; |
| 2185 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2186 | } |
| 2187 | } |
| 2188 | |
| 2189 | private: |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2190 | // Class visitor that limits the class visits from a ClassTable to the classes with |
| 2191 | // the provided defining class loader. This filter is used to avoid multiple visits |
| 2192 | // of the same class which can be recorded for multiple initiating class loaders. |
| 2193 | class DefiningClassLoaderFilterVisitor : public ClassVisitor { |
| 2194 | public: |
| 2195 | DefiningClassLoaderFilterVisitor(ObjPtr<mirror::ClassLoader> defining_class_loader, |
| 2196 | ClassVisitor* visitor) |
| 2197 | : defining_class_loader_(defining_class_loader), visitor_(visitor) { } |
| 2198 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 2199 | bool operator()(ObjPtr<mirror::Class> klass) override REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2200 | if (klass->GetClassLoader() != defining_class_loader_) { |
| 2201 | return true; |
| 2202 | } |
| 2203 | return (*visitor_)(klass); |
| 2204 | } |
| 2205 | |
Vladimir Marko | 0984e48 | 2019-03-27 16:41:41 +0000 | [diff] [blame] | 2206 | const ObjPtr<mirror::ClassLoader> defining_class_loader_; |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2207 | ClassVisitor* const visitor_; |
| 2208 | }; |
| 2209 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2210 | ClassVisitor* const visitor_; |
| 2211 | // If done is true then we don't need to do any more visiting. |
| 2212 | bool done_; |
| 2213 | }; |
| 2214 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2215 | void ClassLinker::VisitClassesInternal(ClassVisitor* visitor) { |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 2216 | if (boot_class_table_->Visit(*visitor)) { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2217 | VisitClassLoaderClassesVisitor loader_visitor(visitor); |
| 2218 | VisitClassLoaders(&loader_visitor); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 2219 | } |
| 2220 | } |
| 2221 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2222 | void ClassLinker::VisitClasses(ClassVisitor* visitor) { |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 2223 | Thread* const self = Thread::Current(); |
| 2224 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 2225 | // Not safe to have thread suspension when we are holding a lock. |
| 2226 | if (self != nullptr) { |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 2227 | ScopedAssertNoThreadSuspension nts(__FUNCTION__); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2228 | VisitClassesInternal(visitor); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 2229 | } else { |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2230 | VisitClassesInternal(visitor); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2231 | } |
| 2232 | } |
| 2233 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2234 | class GetClassesInToVector : public ClassVisitor { |
| 2235 | public: |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 2236 | bool operator()(ObjPtr<mirror::Class> klass) override { |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2237 | classes_.push_back(klass); |
| 2238 | return true; |
| 2239 | } |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2240 | std::vector<ObjPtr<mirror::Class>> classes_; |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2241 | }; |
| 2242 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2243 | class GetClassInToObjectArray : public ClassVisitor { |
| 2244 | public: |
| 2245 | explicit GetClassInToObjectArray(mirror::ObjectArray<mirror::Class>* arr) |
| 2246 | : arr_(arr), index_(0) {} |
| 2247 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 2248 | bool operator()(ObjPtr<mirror::Class> klass) override REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2249 | ++index_; |
| 2250 | if (index_ <= arr_->GetLength()) { |
| 2251 | arr_->Set(index_ - 1, klass); |
| 2252 | return true; |
| 2253 | } |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2254 | return false; |
| 2255 | } |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2256 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2257 | bool Succeeded() const REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2258 | return index_ <= arr_->GetLength(); |
| 2259 | } |
| 2260 | |
| 2261 | private: |
| 2262 | mirror::ObjectArray<mirror::Class>* const arr_; |
| 2263 | int32_t index_; |
| 2264 | }; |
| 2265 | |
| 2266 | void ClassLinker::VisitClassesWithoutClassesLock(ClassVisitor* visitor) { |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2267 | // TODO: it may be possible to avoid secondary storage if we iterate over dex caches. The problem |
| 2268 | // is avoiding duplicates. |
| 2269 | if (!kMovingClasses) { |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 2270 | ScopedAssertNoThreadSuspension nts(__FUNCTION__); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2271 | GetClassesInToVector accumulator; |
| 2272 | VisitClasses(&accumulator); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2273 | for (ObjPtr<mirror::Class> klass : accumulator.classes_) { |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 2274 | if (!visitor->operator()(klass)) { |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2275 | return; |
| 2276 | } |
| 2277 | } |
| 2278 | } else { |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 2279 | Thread* const self = Thread::Current(); |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2280 | StackHandleScope<1> hs(self); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2281 | auto classes = hs.NewHandle<mirror::ObjectArray<mirror::Class>>(nullptr); |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2282 | // We size the array assuming classes won't be added to the class table during the visit. |
| 2283 | // If this assumption fails we iterate again. |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2284 | while (true) { |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2285 | size_t class_table_size; |
| 2286 | { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2287 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2288 | // Add 100 in case new classes get loaded when we are filling in the object array. |
| 2289 | class_table_size = NumZygoteClasses() + NumNonZygoteClasses() + 100; |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2290 | } |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 2291 | ObjPtr<mirror::Class> array_of_class = GetClassRoot<mirror::ObjectArray<mirror::Class>>(this); |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2292 | classes.Assign( |
| 2293 | mirror::ObjectArray<mirror::Class>::Alloc(self, array_of_class, class_table_size)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 2294 | CHECK(classes != nullptr); // OOME. |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2295 | GetClassInToObjectArray accumulator(classes.Get()); |
| 2296 | VisitClasses(&accumulator); |
| 2297 | if (accumulator.Succeeded()) { |
| 2298 | break; |
| 2299 | } |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2300 | } |
| 2301 | for (int32_t i = 0; i < classes->GetLength(); ++i) { |
| 2302 | // If the class table shrank during creation of the clases array we expect null elements. If |
| 2303 | // the class table grew then the loop repeats. If classes are created after the loop has |
| 2304 | // finished then we don't visit. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2305 | ObjPtr<mirror::Class> klass = classes->Get(i); |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 2306 | if (klass != nullptr && !visitor->operator()(klass)) { |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2307 | return; |
| 2308 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2309 | } |
| 2310 | } |
| 2311 | } |
| 2312 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 2313 | ClassLinker::~ClassLinker() { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2314 | Thread* const self = Thread::Current(); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 2315 | for (const ClassLoaderData& data : class_loaders_) { |
Alexey Grebenkin | be4c2bd | 2018-02-01 19:09:59 +0300 | [diff] [blame] | 2316 | // CHA unloading analysis is not needed. No negative consequences are expected because |
| 2317 | // all the classloaders are deleted at the same time. |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 2318 | DeleteClassLoader(self, data, /*cleanup_cha=*/ false); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 2319 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2320 | class_loaders_.clear(); |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 2321 | while (!running_visibly_initialized_callbacks_.empty()) { |
| 2322 | std::unique_ptr<VisiblyInitializedCallback> callback( |
| 2323 | std::addressof(running_visibly_initialized_callbacks_.front())); |
| 2324 | running_visibly_initialized_callbacks_.pop_front(); |
| 2325 | } |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 2326 | } |
| 2327 | |
Alexey Grebenkin | be4c2bd | 2018-02-01 19:09:59 +0300 | [diff] [blame] | 2328 | void ClassLinker::DeleteClassLoader(Thread* self, const ClassLoaderData& data, bool cleanup_cha) { |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 2329 | Runtime* const runtime = Runtime::Current(); |
| 2330 | JavaVMExt* const vm = runtime->GetJavaVM(); |
| 2331 | vm->DeleteWeakGlobalRef(self, data.weak_root); |
Calin Juravle | e5de54c | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 2332 | // 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] | 2333 | if (runtime->GetJit() != nullptr) { |
| 2334 | jit::JitCodeCache* code_cache = runtime->GetJit()->GetCodeCache(); |
| 2335 | if (code_cache != nullptr) { |
Mathieu Chartier | cf79cf5 | 2017-07-21 11:17:57 -0700 | [diff] [blame] | 2336 | // For the JIT case, RemoveMethodsIn removes the CHA dependencies. |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 2337 | code_cache->RemoveMethodsIn(self, *data.allocator); |
| 2338 | } |
Andreas Gampe | c1ac9ee | 2017-07-24 22:35:49 -0700 | [diff] [blame] | 2339 | } else if (cha_ != nullptr) { |
Mathieu Chartier | cf79cf5 | 2017-07-21 11:17:57 -0700 | [diff] [blame] | 2340 | // 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] | 2341 | cha_->RemoveDependenciesForLinearAlloc(data.allocator); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 2342 | } |
Alexey Grebenkin | be4c2bd | 2018-02-01 19:09:59 +0300 | [diff] [blame] | 2343 | // Cleanup references to single implementation ArtMethods that will be deleted. |
| 2344 | if (cleanup_cha) { |
| 2345 | CHAOnDeleteUpdateClassVisitor visitor(data.allocator); |
| 2346 | data.class_table->Visit<CHAOnDeleteUpdateClassVisitor, kWithoutReadBarrier>(visitor); |
| 2347 | } |
Vladimir Marko | 86c8752 | 2020-05-11 16:55:55 +0100 | [diff] [blame] | 2348 | { |
| 2349 | MutexLock lock(self, critical_native_code_with_clinit_check_lock_); |
| 2350 | auto end = critical_native_code_with_clinit_check_.end(); |
| 2351 | for (auto it = critical_native_code_with_clinit_check_.begin(); it != end; ) { |
| 2352 | if (data.allocator->ContainsUnsafe(it->first)) { |
| 2353 | it = critical_native_code_with_clinit_check_.erase(it); |
| 2354 | } else { |
| 2355 | ++it; |
| 2356 | } |
| 2357 | } |
| 2358 | } |
Alexey Grebenkin | be4c2bd | 2018-02-01 19:09:59 +0300 | [diff] [blame] | 2359 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 2360 | delete data.allocator; |
| 2361 | delete data.class_table; |
| 2362 | } |
| 2363 | |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 2364 | ObjPtr<mirror::PointerArray> ClassLinker::AllocPointerArray(Thread* self, size_t length) { |
| 2365 | return ObjPtr<mirror::PointerArray>::DownCast( |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 2366 | image_pointer_size_ == PointerSize::k64 |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 2367 | ? ObjPtr<mirror::Array>(mirror::LongArray::Alloc(self, length)) |
| 2368 | : ObjPtr<mirror::Array>(mirror::IntArray::Alloc(self, length))); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2369 | } |
| 2370 | |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 2371 | ObjPtr<mirror::DexCache> ClassLinker::AllocDexCache(Thread* self, const DexFile& dex_file) { |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2372 | StackHandleScope<1> hs(self); |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 2373 | auto dex_cache(hs.NewHandle(ObjPtr<mirror::DexCache>::DownCast( |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 2374 | GetClassRoot<mirror::DexCache>(this)->AllocObject(self)))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 2375 | if (dex_cache == nullptr) { |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2376 | self->AssertPendingOOMException(); |
| 2377 | return nullptr; |
| 2378 | } |
Vladimir Marko | 31c3daa | 2019-06-13 12:18:37 +0100 | [diff] [blame] | 2379 | // Use InternWeak() so that the location String can be collected when the ClassLoader |
| 2380 | // with this DexCache is collected. |
| 2381 | ObjPtr<mirror::String> location = intern_table_->InternWeak(dex_file.GetLocation().c_str()); |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2382 | if (location == nullptr) { |
| 2383 | self->AssertPendingOOMException(); |
| 2384 | return nullptr; |
| 2385 | } |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 2386 | dex_cache->SetLocation(location); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2387 | return dex_cache.Get(); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 2388 | } |
| 2389 | |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 2390 | ObjPtr<mirror::DexCache> ClassLinker::AllocAndInitializeDexCache(Thread* self, |
| 2391 | const DexFile& dex_file, |
| 2392 | LinearAlloc* linear_alloc) { |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 2393 | ObjPtr<mirror::DexCache> dex_cache = AllocDexCache(self, dex_file); |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2394 | if (dex_cache != nullptr) { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 2395 | WriterMutexLock mu(self, *Locks::dex_lock_); |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 2396 | dex_cache->InitializeNativeFields(&dex_file, linear_alloc); |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2397 | } |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 2398 | return dex_cache; |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2399 | } |
| 2400 | |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 2401 | template <bool kMovable, typename PreFenceVisitor> |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 2402 | ObjPtr<mirror::Class> ClassLinker::AllocClass(Thread* self, |
| 2403 | ObjPtr<mirror::Class> java_lang_Class, |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 2404 | uint32_t class_size, |
| 2405 | const PreFenceVisitor& pre_fence_visitor) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2406 | DCHECK_GE(class_size, sizeof(mirror::Class)); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 2407 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 2408 | ObjPtr<mirror::Object> k = (kMovingClasses && kMovable) ? |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 2409 | heap->AllocObject(self, java_lang_Class, class_size, pre_fence_visitor) : |
| 2410 | heap->AllocNonMovableObject(self, java_lang_Class, class_size, pre_fence_visitor); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 2411 | if (UNLIKELY(k == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2412 | self->AssertPendingOOMException(); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 2413 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2414 | } |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 2415 | return k->AsClass(); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 2416 | } |
| 2417 | |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 2418 | template <bool kMovable> |
| 2419 | ObjPtr<mirror::Class> ClassLinker::AllocClass(Thread* self, |
| 2420 | ObjPtr<mirror::Class> java_lang_Class, |
| 2421 | uint32_t class_size) { |
| 2422 | mirror::Class::InitializeClassVisitor visitor(class_size); |
| 2423 | return AllocClass<kMovable>(self, java_lang_Class, class_size, visitor); |
| 2424 | } |
| 2425 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 2426 | ObjPtr<mirror::Class> ClassLinker::AllocClass(Thread* self, uint32_t class_size) { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 2427 | return AllocClass(self, GetClassRoot<mirror::Class>(this), class_size); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 2428 | } |
| 2429 | |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 2430 | void ClassLinker::AllocPrimitiveArrayClass(Thread* self, |
| 2431 | ClassRoot primitive_root, |
| 2432 | ClassRoot array_root) { |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 2433 | // We make this class non-movable for the unlikely case where it were to be |
| 2434 | // moved by a sticky-bit (minor) collection when using the Generational |
| 2435 | // Concurrent Copying (CC) collector, potentially creating a stale reference |
| 2436 | // in the `klass_` field of one of its instances allocated in the Large-Object |
| 2437 | // Space (LOS) -- see the comment about the dirty card scanning logic in |
| 2438 | // art::gc::collector::ConcurrentCopying::MarkingPhase. |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 2439 | ObjPtr<mirror::Class> array_class = AllocClass</* kMovable= */ false>( |
| 2440 | self, GetClassRoot<mirror::Class>(this), mirror::Array::ClassSize(image_pointer_size_)); |
| 2441 | ObjPtr<mirror::Class> component_type = GetClassRoot(primitive_root, this); |
| 2442 | DCHECK(component_type->IsPrimitive()); |
| 2443 | array_class->SetComponentType(component_type); |
| 2444 | SetClassRoot(array_root, array_class); |
| 2445 | } |
| 2446 | |
| 2447 | void ClassLinker::FinishArrayClassSetup(ObjPtr<mirror::Class> array_class) { |
| 2448 | ObjPtr<mirror::Class> java_lang_Object = GetClassRoot<mirror::Object>(this); |
| 2449 | array_class->SetSuperClass(java_lang_Object); |
| 2450 | array_class->SetVTable(java_lang_Object->GetVTable()); |
| 2451 | array_class->SetPrimitiveType(Primitive::kPrimNot); |
| 2452 | ObjPtr<mirror::Class> component_type = array_class->GetComponentType(); |
| 2453 | array_class->SetClassFlags(component_type->IsPrimitive() |
| 2454 | ? mirror::kClassFlagNoReferenceFields |
| 2455 | : mirror::kClassFlagObjectArray); |
| 2456 | array_class->SetClassLoader(component_type->GetClassLoader()); |
| 2457 | array_class->SetStatusForPrimitiveOrArray(ClassStatus::kLoaded); |
| 2458 | array_class->PopulateEmbeddedVTable(image_pointer_size_); |
| 2459 | ImTable* object_imt = java_lang_Object->GetImt(image_pointer_size_); |
| 2460 | array_class->SetImt(object_imt, image_pointer_size_); |
| 2461 | // Skip EnsureSkipAccessChecksMethods(). We can skip the verified status, |
| 2462 | // the kAccVerificationAttempted flag is added below, and there are no |
| 2463 | // methods that need the kAccSkipAccessChecks flag. |
| 2464 | DCHECK_EQ(array_class->NumMethods(), 0u); |
| 2465 | |
| 2466 | // don't need to set new_class->SetObjectSize(..) |
| 2467 | // because Object::SizeOf delegates to Array::SizeOf |
| 2468 | |
| 2469 | // All arrays have java/lang/Cloneable and java/io/Serializable as |
| 2470 | // interfaces. We need to set that up here, so that stuff like |
| 2471 | // "instanceof" works right. |
| 2472 | |
| 2473 | // Use the single, global copies of "interfaces" and "iftable" |
| 2474 | // (remember not to free them for arrays). |
| 2475 | { |
| 2476 | ObjPtr<mirror::IfTable> array_iftable = GetArrayIfTable(); |
| 2477 | CHECK(array_iftable != nullptr); |
| 2478 | array_class->SetIfTable(array_iftable); |
| 2479 | } |
| 2480 | |
| 2481 | // Inherit access flags from the component type. |
| 2482 | int access_flags = component_type->GetAccessFlags(); |
| 2483 | // Lose any implementation detail flags; in particular, arrays aren't finalizable. |
| 2484 | access_flags &= kAccJavaFlagsMask; |
| 2485 | // Arrays can't be used as a superclass or interface, so we want to add "abstract final" |
| 2486 | // and remove "interface". |
| 2487 | access_flags |= kAccAbstract | kAccFinal; |
| 2488 | access_flags &= ~kAccInterface; |
| 2489 | // Arrays are access-checks-clean and preverified. |
| 2490 | access_flags |= kAccVerificationAttempted; |
| 2491 | |
Vladimir Marko | b68bb7a | 2020-03-17 10:55:25 +0000 | [diff] [blame] | 2492 | array_class->SetAccessFlagsDuringLinking(access_flags); |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 2493 | |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 2494 | // Array classes are fully initialized either during single threaded startup, |
| 2495 | // or from a pre-fence visitor, so visibly initialized. |
| 2496 | array_class->SetStatusForPrimitiveOrArray(ClassStatus::kVisiblyInitialized); |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 2497 | } |
| 2498 | |
| 2499 | void ClassLinker::FinishCoreArrayClassSetup(ClassRoot array_root) { |
| 2500 | // Do not hold lock on the array class object, the initialization of |
| 2501 | // core array classes is done while the process is still single threaded. |
| 2502 | ObjPtr<mirror::Class> array_class = GetClassRoot(array_root, this); |
| 2503 | FinishArrayClassSetup(array_class); |
| 2504 | |
| 2505 | std::string temp; |
| 2506 | const char* descriptor = array_class->GetDescriptor(&temp); |
| 2507 | size_t hash = ComputeModifiedUtf8Hash(descriptor); |
| 2508 | ObjPtr<mirror::Class> existing = InsertClass(descriptor, array_class, hash); |
| 2509 | CHECK(existing == nullptr); |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 2510 | } |
| 2511 | |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 2512 | ObjPtr<mirror::ObjectArray<mirror::StackTraceElement>> ClassLinker::AllocStackTraceElementArray( |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 2513 | Thread* self, |
| 2514 | size_t length) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2515 | return mirror::ObjectArray<mirror::StackTraceElement>::Alloc( |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 2516 | self, GetClassRoot<mirror::ObjectArray<mirror::StackTraceElement>>(this), length); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 2517 | } |
| 2518 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 2519 | ObjPtr<mirror::Class> ClassLinker::EnsureResolved(Thread* self, |
| 2520 | const char* descriptor, |
| 2521 | ObjPtr<mirror::Class> klass) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2522 | DCHECK(klass != nullptr); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2523 | if (kIsDebugBuild) { |
| 2524 | StackHandleScope<1> hs(self); |
| 2525 | HandleWrapperObjPtr<mirror::Class> h = hs.NewHandleWrapper(&klass); |
| 2526 | Thread::PoisonObjectPointersIfDebug(); |
| 2527 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2528 | |
| 2529 | // For temporary classes we must wait for them to be retired. |
| 2530 | if (init_done_ && klass->IsTemp()) { |
| 2531 | CHECK(!klass->IsResolved()); |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2532 | if (klass->IsErroneousUnresolved()) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2533 | ThrowEarlierClassFailure(klass); |
| 2534 | return nullptr; |
| 2535 | } |
| 2536 | StackHandleScope<1> hs(self); |
| 2537 | Handle<mirror::Class> h_class(hs.NewHandle(klass)); |
| 2538 | ObjectLock<mirror::Class> lock(self, h_class); |
| 2539 | // Loop and wait for the resolving thread to retire this class. |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2540 | while (!h_class->IsRetired() && !h_class->IsErroneousUnresolved()) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2541 | lock.WaitIgnoringInterrupts(); |
| 2542 | } |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2543 | if (h_class->IsErroneousUnresolved()) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2544 | ThrowEarlierClassFailure(h_class.Get()); |
| 2545 | return nullptr; |
| 2546 | } |
| 2547 | CHECK(h_class->IsRetired()); |
| 2548 | // Get the updated class from class table. |
Andreas Gampe | 34ee684 | 2014-12-02 15:43:52 -0800 | [diff] [blame] | 2549 | klass = LookupClass(self, descriptor, h_class.Get()->GetClassLoader()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2550 | } |
| 2551 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2552 | // Wait for the class if it has not already been linked. |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 2553 | size_t index = 0; |
| 2554 | // Maximum number of yield iterations until we start sleeping. |
| 2555 | static const size_t kNumYieldIterations = 1000; |
| 2556 | // How long each sleep is in us. |
| 2557 | static const size_t kSleepDurationUS = 1000; // 1 ms. |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2558 | while (!klass->IsResolved() && !klass->IsErroneousUnresolved()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2559 | StackHandleScope<1> hs(self); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2560 | HandleWrapperObjPtr<mirror::Class> h_class(hs.NewHandleWrapper(&klass)); |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 2561 | { |
| 2562 | ObjectTryLock<mirror::Class> lock(self, h_class); |
| 2563 | // Can not use a monitor wait here since it may block when returning and deadlock if another |
| 2564 | // thread has locked klass. |
| 2565 | if (lock.Acquired()) { |
| 2566 | // Check for circular dependencies between classes, the lock is required for SetStatus. |
| 2567 | if (!h_class->IsResolved() && h_class->GetClinitThreadId() == self->GetTid()) { |
| 2568 | ThrowClassCircularityError(h_class.Get()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 2569 | mirror::Class::SetStatus(h_class, ClassStatus::kErrorUnresolved, self); |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 2570 | return nullptr; |
| 2571 | } |
| 2572 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2573 | } |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 2574 | { |
| 2575 | // Handle wrapper deals with klass moving. |
| 2576 | ScopedThreadSuspension sts(self, kSuspended); |
| 2577 | if (index < kNumYieldIterations) { |
| 2578 | sched_yield(); |
| 2579 | } else { |
| 2580 | usleep(kSleepDurationUS); |
| 2581 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2582 | } |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 2583 | ++index; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2584 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2585 | |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2586 | if (klass->IsErroneousUnresolved()) { |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 2587 | ThrowEarlierClassFailure(klass); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 2588 | return nullptr; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2589 | } |
| 2590 | // Return the loaded class. No exceptions should be pending. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 2591 | CHECK(klass->IsResolved()) << klass->PrettyClass(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2592 | self->AssertNoPendingException(); |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 2593 | return klass; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2594 | } |
| 2595 | |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 2596 | using ClassPathEntry = std::pair<const DexFile*, const dex::ClassDef*>; |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2597 | |
| 2598 | // Search a collection of DexFiles for a descriptor |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2599 | ClassPathEntry FindInClassPath(const char* descriptor, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 2600 | size_t hash, const std::vector<const DexFile*>& class_path) { |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2601 | for (const DexFile* dex_file : class_path) { |
Mathieu Chartier | 0a19e21 | 2019-11-27 14:35:24 -0800 | [diff] [blame] | 2602 | DCHECK(dex_file != nullptr); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 2603 | const dex::ClassDef* dex_class_def = OatDexFile::FindClassDef(*dex_file, descriptor, hash); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2604 | if (dex_class_def != nullptr) { |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2605 | return ClassPathEntry(dex_file, dex_class_def); |
| 2606 | } |
| 2607 | } |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2608 | return ClassPathEntry(nullptr, nullptr); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2609 | } |
| 2610 | |
Nicolas Geoffray | 80a560c | 2018-10-26 13:48:51 +0100 | [diff] [blame] | 2611 | bool ClassLinker::FindClassInSharedLibraries(ScopedObjectAccessAlreadyRunnable& soa, |
| 2612 | Thread* self, |
| 2613 | const char* descriptor, |
| 2614 | size_t hash, |
| 2615 | Handle<mirror::ClassLoader> class_loader, |
| 2616 | /*out*/ ObjPtr<mirror::Class>* result) { |
| 2617 | ArtField* field = |
| 2618 | jni::DecodeArtField(WellKnownClasses::dalvik_system_BaseDexClassLoader_sharedLibraryLoaders); |
| 2619 | ObjPtr<mirror::Object> raw_shared_libraries = field->GetObject(class_loader.Get()); |
| 2620 | if (raw_shared_libraries == nullptr) { |
| 2621 | return true; |
| 2622 | } |
| 2623 | |
| 2624 | StackHandleScope<2> hs(self); |
| 2625 | Handle<mirror::ObjectArray<mirror::ClassLoader>> shared_libraries( |
| 2626 | hs.NewHandle(raw_shared_libraries->AsObjectArray<mirror::ClassLoader>())); |
| 2627 | MutableHandle<mirror::ClassLoader> temp_loader = hs.NewHandle<mirror::ClassLoader>(nullptr); |
Alex Light | a9bbc08 | 2019-11-14 14:51:41 -0800 | [diff] [blame] | 2628 | for (auto loader : shared_libraries.Iterate<mirror::ClassLoader>()) { |
| 2629 | temp_loader.Assign(loader); |
Nicolas Geoffray | 80a560c | 2018-10-26 13:48:51 +0100 | [diff] [blame] | 2630 | if (!FindClassInBaseDexClassLoader(soa, self, descriptor, hash, temp_loader, result)) { |
| 2631 | return false; // One of the shared libraries is not supported. |
| 2632 | } |
| 2633 | if (*result != nullptr) { |
| 2634 | return true; // Found the class up the chain. |
| 2635 | } |
| 2636 | } |
| 2637 | return true; |
| 2638 | } |
| 2639 | |
Nicolas Geoffray | 7d8d8ff | 2016-11-02 12:38:05 +0000 | [diff] [blame] | 2640 | bool ClassLinker::FindClassInBaseDexClassLoader(ScopedObjectAccessAlreadyRunnable& soa, |
| 2641 | Thread* self, |
| 2642 | const char* descriptor, |
| 2643 | size_t hash, |
| 2644 | Handle<mirror::ClassLoader> class_loader, |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 2645 | /*out*/ ObjPtr<mirror::Class>* result) { |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 2646 | // Termination case: boot class loader. |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2647 | if (IsBootClassLoader(soa, class_loader.Get())) { |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 2648 | *result = FindClassInBootClassLoaderClassPath(self, descriptor, hash); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2649 | return true; |
| 2650 | } |
| 2651 | |
David Brazdil | 05909d8 | 2018-12-06 16:25:16 +0000 | [diff] [blame] | 2652 | if (IsPathOrDexClassLoader(soa, class_loader) || IsInMemoryDexClassLoader(soa, class_loader)) { |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 2653 | // For regular path or dex class loader the search order is: |
| 2654 | // - parent |
Nicolas Geoffray | 80a560c | 2018-10-26 13:48:51 +0100 | [diff] [blame] | 2655 | // - shared libraries |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 2656 | // - class loader dex files |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2657 | |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 2658 | // Handles as RegisterDexFile may allocate dex caches (and cause thread suspension). |
| 2659 | StackHandleScope<1> hs(self); |
| 2660 | Handle<mirror::ClassLoader> h_parent(hs.NewHandle(class_loader->GetParent())); |
| 2661 | if (!FindClassInBaseDexClassLoader(soa, self, descriptor, hash, h_parent, result)) { |
| 2662 | return false; // One of the parents is not supported. |
| 2663 | } |
| 2664 | if (*result != nullptr) { |
| 2665 | return true; // Found the class up the chain. |
| 2666 | } |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2667 | |
Nicolas Geoffray | 80a560c | 2018-10-26 13:48:51 +0100 | [diff] [blame] | 2668 | if (!FindClassInSharedLibraries(soa, self, descriptor, hash, class_loader, result)) { |
| 2669 | return false; // One of the shared library loader is not supported. |
| 2670 | } |
| 2671 | if (*result != nullptr) { |
| 2672 | return true; // Found the class in a shared library. |
| 2673 | } |
| 2674 | |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 2675 | // Search the current class loader classpath. |
| 2676 | *result = FindClassInBaseDexClassLoaderClassPath(soa, descriptor, hash, class_loader); |
Andreas Gampe | 501c3b0 | 2019-04-17 21:54:27 +0000 | [diff] [blame] | 2677 | return !soa.Self()->IsExceptionPending(); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2678 | } |
| 2679 | |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 2680 | if (IsDelegateLastClassLoader(soa, class_loader)) { |
| 2681 | // For delegate last, the search order is: |
| 2682 | // - boot class path |
Nicolas Geoffray | 80a560c | 2018-10-26 13:48:51 +0100 | [diff] [blame] | 2683 | // - shared libraries |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 2684 | // - class loader dex files |
| 2685 | // - parent |
| 2686 | *result = FindClassInBootClassLoaderClassPath(self, descriptor, hash); |
| 2687 | if (*result != nullptr) { |
| 2688 | return true; // The class is part of the boot class path. |
| 2689 | } |
Andreas Gampe | 501c3b0 | 2019-04-17 21:54:27 +0000 | [diff] [blame] | 2690 | if (self->IsExceptionPending()) { |
| 2691 | // Pending exception means there was an error other than ClassNotFound that must be returned |
| 2692 | // to the caller. |
| 2693 | return false; |
| 2694 | } |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 2695 | |
Nicolas Geoffray | 80a560c | 2018-10-26 13:48:51 +0100 | [diff] [blame] | 2696 | if (!FindClassInSharedLibraries(soa, self, descriptor, hash, class_loader, result)) { |
| 2697 | return false; // One of the shared library loader is not supported. |
| 2698 | } |
| 2699 | if (*result != nullptr) { |
| 2700 | return true; // Found the class in a shared library. |
| 2701 | } |
| 2702 | |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 2703 | *result = FindClassInBaseDexClassLoaderClassPath(soa, descriptor, hash, class_loader); |
| 2704 | if (*result != nullptr) { |
| 2705 | return true; // Found the class in the current class loader |
| 2706 | } |
Andreas Gampe | 501c3b0 | 2019-04-17 21:54:27 +0000 | [diff] [blame] | 2707 | if (self->IsExceptionPending()) { |
| 2708 | // Pending exception means there was an error other than ClassNotFound that must be returned |
| 2709 | // to the caller. |
| 2710 | return false; |
| 2711 | } |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 2712 | |
| 2713 | // Handles as RegisterDexFile may allocate dex caches (and cause thread suspension). |
| 2714 | StackHandleScope<1> hs(self); |
| 2715 | Handle<mirror::ClassLoader> h_parent(hs.NewHandle(class_loader->GetParent())); |
| 2716 | return FindClassInBaseDexClassLoader(soa, self, descriptor, hash, h_parent, result); |
| 2717 | } |
| 2718 | |
| 2719 | // Unsupported class loader. |
| 2720 | *result = nullptr; |
| 2721 | return false; |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 2722 | } |
| 2723 | |
Andreas Gampe | 501c3b0 | 2019-04-17 21:54:27 +0000 | [diff] [blame] | 2724 | namespace { |
| 2725 | |
| 2726 | // Matches exceptions caught in DexFile.defineClass. |
| 2727 | ALWAYS_INLINE bool MatchesDexFileCaughtExceptions(ObjPtr<mirror::Throwable> throwable, |
| 2728 | ClassLinker* class_linker) |
| 2729 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 2730 | return |
| 2731 | // ClassNotFoundException. |
| 2732 | throwable->InstanceOf(GetClassRoot(ClassRoot::kJavaLangClassNotFoundException, |
| 2733 | class_linker)) |
| 2734 | || |
| 2735 | // NoClassDefFoundError. TODO: Reconsider this. b/130746382. |
| 2736 | throwable->InstanceOf(Runtime::Current()->GetPreAllocatedNoClassDefFoundError()->GetClass()); |
| 2737 | } |
| 2738 | |
| 2739 | // Clear exceptions caught in DexFile.defineClass. |
| 2740 | ALWAYS_INLINE void FilterDexFileCaughtExceptions(Thread* self, ClassLinker* class_linker) |
| 2741 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 2742 | if (MatchesDexFileCaughtExceptions(self->GetException(), class_linker)) { |
| 2743 | self->ClearException(); |
| 2744 | } |
| 2745 | } |
| 2746 | |
| 2747 | } // namespace |
| 2748 | |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 2749 | // Finds the class in the boot class loader. |
| 2750 | // If the class is found the method returns the resolved class. Otherwise it returns null. |
| 2751 | ObjPtr<mirror::Class> ClassLinker::FindClassInBootClassLoaderClassPath(Thread* self, |
| 2752 | const char* descriptor, |
| 2753 | size_t hash) { |
| 2754 | ObjPtr<mirror::Class> result = nullptr; |
| 2755 | ClassPathEntry pair = FindInClassPath(descriptor, hash, boot_class_path_); |
| 2756 | if (pair.second != nullptr) { |
| 2757 | ObjPtr<mirror::Class> klass = LookupClass(self, descriptor, hash, nullptr); |
| 2758 | if (klass != nullptr) { |
| 2759 | result = EnsureResolved(self, descriptor, klass); |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2760 | } else { |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 2761 | result = DefineClass(self, |
| 2762 | descriptor, |
| 2763 | hash, |
| 2764 | ScopedNullHandle<mirror::ClassLoader>(), |
| 2765 | *pair.first, |
| 2766 | *pair.second); |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2767 | } |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 2768 | if (result == nullptr) { |
| 2769 | CHECK(self->IsExceptionPending()) << descriptor; |
Andreas Gampe | 501c3b0 | 2019-04-17 21:54:27 +0000 | [diff] [blame] | 2770 | FilterDexFileCaughtExceptions(self, this); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2771 | } |
| 2772 | } |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 2773 | return result; |
| 2774 | } |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2775 | |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 2776 | ObjPtr<mirror::Class> ClassLinker::FindClassInBaseDexClassLoaderClassPath( |
| 2777 | ScopedObjectAccessAlreadyRunnable& soa, |
| 2778 | const char* descriptor, |
| 2779 | size_t hash, |
| 2780 | Handle<mirror::ClassLoader> class_loader) { |
David Brazdil | 05909d8 | 2018-12-06 16:25:16 +0000 | [diff] [blame] | 2781 | DCHECK(IsPathOrDexClassLoader(soa, class_loader) || |
| 2782 | IsInMemoryDexClassLoader(soa, class_loader) || |
| 2783 | IsDelegateLastClassLoader(soa, class_loader)) |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 2784 | << "Unexpected class loader for descriptor " << descriptor; |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2785 | |
Andreas Gampe | b8e7c37 | 2018-02-20 18:24:55 -0800 | [diff] [blame] | 2786 | ObjPtr<mirror::Class> ret; |
| 2787 | 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] | 2788 | 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] | 2789 | if (dex_class_def != nullptr) { |
| 2790 | ObjPtr<mirror::Class> klass = DefineClass(soa.Self(), |
| 2791 | descriptor, |
| 2792 | hash, |
| 2793 | class_loader, |
| 2794 | *cp_dex_file, |
| 2795 | *dex_class_def); |
| 2796 | if (klass == nullptr) { |
| 2797 | CHECK(soa.Self()->IsExceptionPending()) << descriptor; |
Andreas Gampe | 501c3b0 | 2019-04-17 21:54:27 +0000 | [diff] [blame] | 2798 | FilterDexFileCaughtExceptions(soa.Self(), this); |
Andreas Gampe | b8e7c37 | 2018-02-20 18:24:55 -0800 | [diff] [blame] | 2799 | // 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] | 2800 | } else { |
| 2801 | DCHECK(!soa.Self()->IsExceptionPending()); |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2802 | } |
Andreas Gampe | b8e7c37 | 2018-02-20 18:24:55 -0800 | [diff] [blame] | 2803 | ret = klass; |
| 2804 | return false; // Found a Class (or error == nullptr), stop visit. |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2805 | } |
Andreas Gampe | b8e7c37 | 2018-02-20 18:24:55 -0800 | [diff] [blame] | 2806 | return true; // Continue with the next DexFile. |
| 2807 | }; |
| 2808 | |
| 2809 | VisitClassLoaderDexFiles(soa, class_loader, define_class); |
| 2810 | return ret; |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2811 | } |
| 2812 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 2813 | ObjPtr<mirror::Class> ClassLinker::FindClass(Thread* self, |
| 2814 | const char* descriptor, |
| 2815 | Handle<mirror::ClassLoader> class_loader) { |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 2816 | DCHECK_NE(*descriptor, '\0') << "descriptor is empty string"; |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 2817 | DCHECK(self != nullptr); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2818 | self->AssertNoPendingException(); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 2819 | self->PoisonObjectPointers(); // For DefineClass, CreateArrayClass, etc... |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 2820 | if (descriptor[1] == '\0') { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2821 | // only the descriptors of primitive types should be 1 character long, also avoid class lookup |
| 2822 | // for primitive classes that aren't backed by dex files. |
| 2823 | return FindPrimitiveClass(descriptor[0]); |
| 2824 | } |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2825 | const size_t hash = ComputeModifiedUtf8Hash(descriptor); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2826 | // Find the class in the loaded classes table. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2827 | ObjPtr<mirror::Class> klass = LookupClass(self, descriptor, hash, class_loader.Get()); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2828 | if (klass != nullptr) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2829 | return EnsureResolved(self, descriptor, klass); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2830 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2831 | // Class is not yet loaded. |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 2832 | if (descriptor[0] != '[' && class_loader == nullptr) { |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 2833 | // 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] | 2834 | ClassPathEntry pair = FindInClassPath(descriptor, hash, boot_class_path_); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2835 | if (pair.second != nullptr) { |
Mathieu Chartier | 9865bde | 2015-12-21 09:58:16 -0800 | [diff] [blame] | 2836 | return DefineClass(self, |
| 2837 | descriptor, |
| 2838 | hash, |
| 2839 | ScopedNullHandle<mirror::ClassLoader>(), |
| 2840 | *pair.first, |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2841 | *pair.second); |
Ian Rogers | 6355745 | 2014-06-04 16:57:15 -0700 | [diff] [blame] | 2842 | } else { |
| 2843 | // The boot class loader is searched ahead of the application class loader, failures are |
| 2844 | // expected and will be wrapped in a ClassNotFoundException. Use the pre-allocated error to |
| 2845 | // trigger the chaining with a proper stack trace. |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 2846 | ObjPtr<mirror::Throwable> pre_allocated = |
| 2847 | Runtime::Current()->GetPreAllocatedNoClassDefFoundError(); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2848 | self->SetException(pre_allocated); |
Ian Rogers | 6355745 | 2014-06-04 16:57:15 -0700 | [diff] [blame] | 2849 | return nullptr; |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 2850 | } |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 2851 | } |
| 2852 | ObjPtr<mirror::Class> result_ptr; |
| 2853 | bool descriptor_equals; |
| 2854 | if (descriptor[0] == '[') { |
| 2855 | result_ptr = CreateArrayClass(self, descriptor, hash, class_loader); |
| 2856 | DCHECK_EQ(result_ptr == nullptr, self->IsExceptionPending()); |
| 2857 | DCHECK(result_ptr == nullptr || result_ptr->DescriptorEquals(descriptor)); |
| 2858 | descriptor_equals = true; |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 2859 | } else { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 2860 | ScopedObjectAccessUnchecked soa(self); |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2861 | bool known_hierarchy = |
| 2862 | FindClassInBaseDexClassLoader(soa, self, descriptor, hash, class_loader, &result_ptr); |
| 2863 | if (result_ptr != nullptr) { |
| 2864 | // The chain was understood and we found the class. We still need to add the class to |
| 2865 | // the class table to protect from racy programs that can try and redefine the path list |
| 2866 | // which would change the Class<?> returned for subsequent evaluation of const-class. |
| 2867 | DCHECK(known_hierarchy); |
| 2868 | DCHECK(result_ptr->DescriptorEquals(descriptor)); |
| 2869 | descriptor_equals = true; |
Andreas Gampe | 501c3b0 | 2019-04-17 21:54:27 +0000 | [diff] [blame] | 2870 | } else if (!self->IsExceptionPending()) { |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2871 | // Either the chain wasn't understood or the class wasn't found. |
Andreas Gampe | 501c3b0 | 2019-04-17 21:54:27 +0000 | [diff] [blame] | 2872 | // If there is a pending exception we didn't clear, it is a not a ClassNotFoundException and |
| 2873 | // we should return it instead of silently clearing and retrying. |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2874 | // |
| 2875 | // If the chain was understood but we did not find the class, let the Java-side |
| 2876 | // rediscover all this and throw the exception with the right stack trace. Note that |
| 2877 | // the Java-side could still succeed for racy programs if another thread is actively |
| 2878 | // modifying the class loader's path list. |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2879 | |
Alex Light | 185a461 | 2018-10-04 15:54:25 -0700 | [diff] [blame] | 2880 | // 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] | 2881 | if (self->IsRuntimeThread()) { |
Calin Juravle | ccd5695 | 2016-12-15 17:57:38 +0000 | [diff] [blame] | 2882 | // Oops, we can't call into java so we can't run actual class-loader code. |
| 2883 | // This is true for e.g. for the compiler (jit or aot). |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2884 | ObjPtr<mirror::Throwable> pre_allocated = |
| 2885 | Runtime::Current()->GetPreAllocatedNoClassDefFoundError(); |
| 2886 | self->SetException(pre_allocated); |
Vladimir Marko | 2c8c6b6 | 2016-12-01 17:42:00 +0000 | [diff] [blame] | 2887 | return nullptr; |
| 2888 | } |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2889 | |
Vladimir Marko | 5fdd778 | 2017-04-20 11:26:03 +0100 | [diff] [blame] | 2890 | // Inlined DescriptorToDot(descriptor) with extra validation. |
| 2891 | // |
| 2892 | // Throw NoClassDefFoundError early rather than potentially load a class only to fail |
| 2893 | // the DescriptorEquals() check below and give a confusing error message. For example, |
| 2894 | // when native code erroneously calls JNI GetFieldId() with signature "java/lang/String" |
| 2895 | // instead of "Ljava/lang/String;", the message below using the "dot" names would be |
| 2896 | // "class loader [...] returned class java.lang.String instead of java.lang.String". |
| 2897 | size_t descriptor_length = strlen(descriptor); |
| 2898 | if (UNLIKELY(descriptor[0] != 'L') || |
| 2899 | UNLIKELY(descriptor[descriptor_length - 1] != ';') || |
| 2900 | UNLIKELY(memchr(descriptor + 1, '.', descriptor_length - 2) != nullptr)) { |
| 2901 | ThrowNoClassDefFoundError("Invalid descriptor: %s.", descriptor); |
| 2902 | return nullptr; |
| 2903 | } |
Andreas Gampe | 501c3b0 | 2019-04-17 21:54:27 +0000 | [diff] [blame] | 2904 | |
Vladimir Marko | 5fdd778 | 2017-04-20 11:26:03 +0100 | [diff] [blame] | 2905 | std::string class_name_string(descriptor + 1, descriptor_length - 2); |
| 2906 | std::replace(class_name_string.begin(), class_name_string.end(), '/', '.'); |
Andreas Gampe | 87658f3 | 2019-04-18 18:39:02 +0000 | [diff] [blame] | 2907 | if (known_hierarchy && |
| 2908 | fast_class_not_found_exceptions_ && |
| 2909 | !Runtime::Current()->IsJavaDebuggable()) { |
| 2910 | // For known hierarchy, we know that the class is going to throw an exception. If we aren't |
| 2911 | // debuggable, optimize this path by throwing directly here without going back to Java |
| 2912 | // language. This reduces how many ClassNotFoundExceptions happen. |
| 2913 | self->ThrowNewExceptionF("Ljava/lang/ClassNotFoundException;", |
| 2914 | "%s", |
| 2915 | class_name_string.c_str()); |
| 2916 | } else { |
| 2917 | ScopedLocalRef<jobject> class_loader_object( |
| 2918 | soa.Env(), soa.AddLocalReference<jobject>(class_loader.Get())); |
| 2919 | ScopedLocalRef<jobject> result(soa.Env(), nullptr); |
| 2920 | { |
| 2921 | ScopedThreadStateChange tsc(self, kNative); |
| 2922 | ScopedLocalRef<jobject> class_name_object( |
| 2923 | soa.Env(), soa.Env()->NewStringUTF(class_name_string.c_str())); |
| 2924 | if (class_name_object.get() == nullptr) { |
| 2925 | DCHECK(self->IsExceptionPending()); // OOME. |
| 2926 | return nullptr; |
| 2927 | } |
| 2928 | CHECK(class_loader_object.get() != nullptr); |
| 2929 | result.reset(soa.Env()->CallObjectMethod(class_loader_object.get(), |
| 2930 | WellKnownClasses::java_lang_ClassLoader_loadClass, |
| 2931 | class_name_object.get())); |
| 2932 | } |
| 2933 | if (result.get() == nullptr && !self->IsExceptionPending()) { |
| 2934 | // broken loader - throw NPE to be compatible with Dalvik |
| 2935 | ThrowNullPointerException(StringPrintf("ClassLoader.loadClass returned null for %s", |
| 2936 | class_name_string.c_str()).c_str()); |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2937 | return nullptr; |
| 2938 | } |
Andreas Gampe | 87658f3 | 2019-04-18 18:39:02 +0000 | [diff] [blame] | 2939 | result_ptr = soa.Decode<mirror::Class>(result.get()); |
| 2940 | // Check the name of the returned class. |
| 2941 | descriptor_equals = (result_ptr != nullptr) && result_ptr->DescriptorEquals(descriptor); |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2942 | } |
Andreas Gampe | 501c3b0 | 2019-04-17 21:54:27 +0000 | [diff] [blame] | 2943 | } else { |
| 2944 | DCHECK(!MatchesDexFileCaughtExceptions(self->GetException(), this)); |
Vladimir Marko | 2c8c6b6 | 2016-12-01 17:42:00 +0000 | [diff] [blame] | 2945 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2946 | } |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 2947 | |
| 2948 | if (self->IsExceptionPending()) { |
| 2949 | // If the ClassLoader threw or array class allocation failed, pass that exception up. |
| 2950 | // However, to comply with the RI behavior, first check if another thread succeeded. |
| 2951 | result_ptr = LookupClass(self, descriptor, hash, class_loader.Get()); |
| 2952 | if (result_ptr != nullptr && !result_ptr->IsErroneous()) { |
| 2953 | self->ClearException(); |
| 2954 | return EnsureResolved(self, descriptor, result_ptr); |
| 2955 | } |
| 2956 | return nullptr; |
| 2957 | } |
| 2958 | |
| 2959 | // Try to insert the class to the class table, checking for mismatch. |
| 2960 | ObjPtr<mirror::Class> old; |
| 2961 | { |
| 2962 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 2963 | ClassTable* const class_table = InsertClassTableForClassLoader(class_loader.Get()); |
| 2964 | old = class_table->Lookup(descriptor, hash); |
| 2965 | if (old == nullptr) { |
| 2966 | old = result_ptr; // For the comparison below, after releasing the lock. |
| 2967 | if (descriptor_equals) { |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 2968 | class_table->InsertWithHash(result_ptr, hash); |
Mathieu Chartier | 88ea61e | 2018-06-20 17:45:41 -0700 | [diff] [blame] | 2969 | WriteBarrier::ForEveryFieldWrite(class_loader.Get()); |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 2970 | } // else throw below, after releasing the lock. |
| 2971 | } |
| 2972 | } |
| 2973 | if (UNLIKELY(old != result_ptr)) { |
| 2974 | // Return `old` (even if `!descriptor_equals`) to mimic the RI behavior for parallel |
| 2975 | // capable class loaders. (All class loaders are considered parallel capable on Android.) |
Vladimir Marko | dfc0de7 | 2019-04-01 10:57:55 +0100 | [diff] [blame] | 2976 | ObjPtr<mirror::Class> loader_class = class_loader->GetClass(); |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 2977 | const char* loader_class_name = |
| 2978 | loader_class->GetDexFile().StringByTypeIdx(loader_class->GetDexTypeIndex()); |
| 2979 | LOG(WARNING) << "Initiating class loader of type " << DescriptorToDot(loader_class_name) |
| 2980 | << " is not well-behaved; it returned a different Class for racing loadClass(\"" |
| 2981 | << DescriptorToDot(descriptor) << "\")."; |
| 2982 | return EnsureResolved(self, descriptor, old); |
| 2983 | } |
| 2984 | if (UNLIKELY(!descriptor_equals)) { |
| 2985 | std::string result_storage; |
| 2986 | const char* result_name = result_ptr->GetDescriptor(&result_storage); |
| 2987 | std::string loader_storage; |
| 2988 | const char* loader_class_name = class_loader->GetClass()->GetDescriptor(&loader_storage); |
| 2989 | ThrowNoClassDefFoundError( |
| 2990 | "Initiating class loader of type %s returned class %s instead of %s.", |
| 2991 | DescriptorToDot(loader_class_name).c_str(), |
| 2992 | DescriptorToDot(result_name).c_str(), |
| 2993 | DescriptorToDot(descriptor).c_str()); |
| 2994 | return nullptr; |
| 2995 | } |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 2996 | // Success. |
| 2997 | return result_ptr; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2998 | } |
| 2999 | |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 3000 | // Helper for maintaining DefineClass counting. We need to notify callbacks when we start/end a |
| 3001 | // define-class and how many recursive DefineClasses we are at in order to allow for doing things |
| 3002 | // like pausing class definition. |
| 3003 | struct ScopedDefiningClass { |
| 3004 | public: |
| 3005 | explicit ScopedDefiningClass(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_) |
| 3006 | : self_(self), returned_(false) { |
| 3007 | Locks::mutator_lock_->AssertSharedHeld(self_); |
| 3008 | Runtime::Current()->GetRuntimeCallbacks()->BeginDefineClass(); |
| 3009 | self_->IncrDefineClassCount(); |
| 3010 | } |
| 3011 | ~ScopedDefiningClass() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 3012 | Locks::mutator_lock_->AssertSharedHeld(self_); |
| 3013 | CHECK(returned_); |
| 3014 | } |
| 3015 | |
| 3016 | ObjPtr<mirror::Class> Finish(Handle<mirror::Class> h_klass) |
| 3017 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 3018 | CHECK(!returned_); |
| 3019 | self_->DecrDefineClassCount(); |
| 3020 | Runtime::Current()->GetRuntimeCallbacks()->EndDefineClass(); |
| 3021 | Thread::PoisonObjectPointersIfDebug(); |
| 3022 | returned_ = true; |
| 3023 | return h_klass.Get(); |
| 3024 | } |
| 3025 | |
| 3026 | ObjPtr<mirror::Class> Finish(ObjPtr<mirror::Class> klass) |
| 3027 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 3028 | StackHandleScope<1> hs(self_); |
| 3029 | Handle<mirror::Class> h_klass(hs.NewHandle(klass)); |
| 3030 | return Finish(h_klass); |
| 3031 | } |
| 3032 | |
| 3033 | ObjPtr<mirror::Class> Finish(nullptr_t np ATTRIBUTE_UNUSED) |
| 3034 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 3035 | ScopedNullHandle<mirror::Class> snh; |
| 3036 | return Finish(snh); |
| 3037 | } |
| 3038 | |
| 3039 | private: |
| 3040 | Thread* self_; |
| 3041 | bool returned_; |
| 3042 | }; |
| 3043 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 3044 | ObjPtr<mirror::Class> ClassLinker::DefineClass(Thread* self, |
| 3045 | const char* descriptor, |
| 3046 | size_t hash, |
| 3047 | Handle<mirror::ClassLoader> class_loader, |
| 3048 | const DexFile& dex_file, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 3049 | const dex::ClassDef& dex_class_def) { |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 3050 | ScopedDefiningClass sdc(self); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3051 | StackHandleScope<3> hs(self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3052 | auto klass = hs.NewHandle<mirror::Class>(nullptr); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3053 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3054 | // Load the class from the dex file. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3055 | if (UNLIKELY(!init_done_)) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3056 | // finish up init of hand crafted class_roots_ |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3057 | if (strcmp(descriptor, "Ljava/lang/Object;") == 0) { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 3058 | klass.Assign(GetClassRoot<mirror::Object>(this)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3059 | } else if (strcmp(descriptor, "Ljava/lang/Class;") == 0) { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 3060 | klass.Assign(GetClassRoot<mirror::Class>(this)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3061 | } else if (strcmp(descriptor, "Ljava/lang/String;") == 0) { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 3062 | klass.Assign(GetClassRoot<mirror::String>(this)); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 3063 | } else if (strcmp(descriptor, "Ljava/lang/ref/Reference;") == 0) { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 3064 | klass.Assign(GetClassRoot<mirror::Reference>(this)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3065 | } else if (strcmp(descriptor, "Ljava/lang/DexCache;") == 0) { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 3066 | klass.Assign(GetClassRoot<mirror::DexCache>(this)); |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 3067 | } else if (strcmp(descriptor, "Ldalvik/system/ClassExt;") == 0) { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 3068 | klass.Assign(GetClassRoot<mirror::ClassExt>(this)); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3069 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3070 | } |
| 3071 | |
Vladimir Marko | b9c29f6 | 2019-03-20 14:22:51 +0000 | [diff] [blame] | 3072 | // For AOT-compilation of an app, we may use a shortened boot class path that excludes |
| 3073 | // some runtime modules. Prevent definition of classes in app class loader that could clash |
| 3074 | // with these modules as these classes could be resolved differently during execution. |
| 3075 | if (class_loader != nullptr && |
| 3076 | Runtime::Current()->IsAotCompiler() && |
Vladimir Marko | d1f7351 | 2020-04-02 10:50:35 +0100 | [diff] [blame] | 3077 | IsUpdatableBootClassPathDescriptor(descriptor)) { |
Vladimir Marko | b9c29f6 | 2019-03-20 14:22:51 +0000 | [diff] [blame] | 3078 | ObjPtr<mirror::Throwable> pre_allocated = |
| 3079 | Runtime::Current()->GetPreAllocatedNoClassDefFoundError(); |
| 3080 | self->SetException(pre_allocated); |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 3081 | return sdc.Finish(nullptr); |
Vladimir Marko | b9c29f6 | 2019-03-20 14:22:51 +0000 | [diff] [blame] | 3082 | } |
| 3083 | |
Calin Juravle | 3378768 | 2019-07-26 14:27:18 -0700 | [diff] [blame] | 3084 | // For AOT-compilation of an app, we may use only a public SDK to resolve symbols. If the SDK |
| 3085 | // checks are configured (a non null SdkChecker) and the descriptor is not in the provided |
| 3086 | // public class path then we prevent the definition of the class. |
| 3087 | // |
| 3088 | // NOTE that we only do the checks for the boot classpath APIs. Anything else, like the app |
| 3089 | // classpath is not checked. |
| 3090 | if (class_loader == nullptr && |
| 3091 | Runtime::Current()->IsAotCompiler() && |
| 3092 | DenyAccessBasedOnPublicSdk(descriptor)) { |
| 3093 | ObjPtr<mirror::Throwable> pre_allocated = |
| 3094 | Runtime::Current()->GetPreAllocatedNoClassDefFoundError(); |
| 3095 | self->SetException(pre_allocated); |
| 3096 | return sdc.Finish(nullptr); |
| 3097 | } |
| 3098 | |
Alex Light | e9f6103 | 2018-09-24 16:04:51 -0700 | [diff] [blame] | 3099 | // This is to prevent the calls to ClassLoad and ClassPrepare which can cause java/user-supplied |
| 3100 | // code to be executed. We put it up here so we can avoid all the allocations associated with |
| 3101 | // creating the class. This can happen with (eg) jit threads. |
| 3102 | if (!self->CanLoadClasses()) { |
| 3103 | // Make sure we don't try to load anything, potentially causing an infinite loop. |
| 3104 | ObjPtr<mirror::Throwable> pre_allocated = |
| 3105 | Runtime::Current()->GetPreAllocatedNoClassDefFoundError(); |
| 3106 | self->SetException(pre_allocated); |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 3107 | return sdc.Finish(nullptr); |
Alex Light | e9f6103 | 2018-09-24 16:04:51 -0700 | [diff] [blame] | 3108 | } |
| 3109 | |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3110 | if (klass == nullptr) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3111 | // Allocate a class with the status of not ready. |
| 3112 | // Interface object should get the right size here. Regular class will |
| 3113 | // figure out the right size later and be replaced with one of the right |
| 3114 | // size when the class becomes resolved. |
Chang Xing | 0c2c222 | 2017-08-04 14:36:17 -0700 | [diff] [blame] | 3115 | if (CanAllocClass()) { |
| 3116 | klass.Assign(AllocClass(self, SizeOfClassWithoutEmbeddedTables(dex_file, dex_class_def))); |
| 3117 | } else { |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 3118 | return sdc.Finish(nullptr); |
Chang Xing | 0c2c222 | 2017-08-04 14:36:17 -0700 | [diff] [blame] | 3119 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3120 | } |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3121 | if (UNLIKELY(klass == nullptr)) { |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3122 | self->AssertPendingOOMException(); |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 3123 | return sdc.Finish(nullptr); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3124 | } |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 3125 | // Get the real dex file. This will return the input if there aren't any callbacks or they do |
| 3126 | // nothing. |
| 3127 | DexFile const* new_dex_file = nullptr; |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 3128 | dex::ClassDef const* new_class_def = nullptr; |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 3129 | // TODO We should ideally figure out some way to move this after we get a lock on the klass so it |
| 3130 | // will only be called once. |
| 3131 | Runtime::Current()->GetRuntimeCallbacks()->ClassPreDefine(descriptor, |
| 3132 | klass, |
| 3133 | class_loader, |
| 3134 | dex_file, |
| 3135 | dex_class_def, |
| 3136 | &new_dex_file, |
| 3137 | &new_class_def); |
Alex Light | 440b5d9 | 2017-01-24 15:32:25 -0800 | [diff] [blame] | 3138 | // Check to see if an exception happened during runtime callbacks. Return if so. |
| 3139 | if (self->IsExceptionPending()) { |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 3140 | return sdc.Finish(nullptr); |
Alex Light | 440b5d9 | 2017-01-24 15:32:25 -0800 | [diff] [blame] | 3141 | } |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 3142 | ObjPtr<mirror::DexCache> dex_cache = RegisterDexFile(*new_dex_file, class_loader.Get()); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3143 | if (dex_cache == nullptr) { |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3144 | self->AssertPendingException(); |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 3145 | return sdc.Finish(nullptr); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3146 | } |
| 3147 | klass->SetDexCache(dex_cache); |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 3148 | SetupClass(*new_dex_file, *new_class_def, klass, class_loader.Get()); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3149 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 3150 | // Mark the string class by setting its access flag. |
| 3151 | if (UNLIKELY(!init_done_)) { |
| 3152 | if (strcmp(descriptor, "Ljava/lang/String;") == 0) { |
| 3153 | klass->SetStringClass(); |
| 3154 | } |
| 3155 | } |
| 3156 | |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 3157 | ObjectLock<mirror::Class> lock(self, klass); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3158 | klass->SetClinitThreadId(self->GetTid()); |
Mathieu Chartier | 1e4841e | 2016-12-15 14:21:04 -0800 | [diff] [blame] | 3159 | // 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] | 3160 | klass->SetIfTable(GetClassRoot<mirror::Object>(this)->GetIfTable()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3161 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3162 | // Add the newly loaded class to the loaded classes table. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3163 | ObjPtr<mirror::Class> existing = InsertClass(descriptor, klass.Get(), hash); |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 3164 | if (existing != nullptr) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3165 | // We failed to insert because we raced with another thread. Calling EnsureResolved may cause |
| 3166 | // this thread to block. |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 3167 | return sdc.Finish(EnsureResolved(self, descriptor, existing)); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3168 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3169 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3170 | // Load the fields and other things after we are inserted in the table. This is so that we don't |
| 3171 | // end up allocating unfree-able linear alloc resources and then lose the race condition. The |
| 3172 | // other reason is that the field roots are only visited from the class table. So we need to be |
| 3173 | // inserted before we allocate / fill in these fields. |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 3174 | LoadClass(self, *new_dex_file, *new_class_def, klass); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3175 | if (self->IsExceptionPending()) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 3176 | VLOG(class_linker) << self->GetException()->Dump(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3177 | // An exception occured during load, set status to erroneous while holding klass' lock in case |
| 3178 | // notification is necessary. |
| 3179 | if (!klass->IsErroneous()) { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 3180 | mirror::Class::SetStatus(klass, ClassStatus::kErrorUnresolved, self); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3181 | } |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 3182 | return sdc.Finish(nullptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3183 | } |
| 3184 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3185 | // Finish loading (if necessary) by finding parents |
| 3186 | CHECK(!klass->IsLoaded()); |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 3187 | if (!LoadSuperAndInterfaces(klass, *new_dex_file)) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3188 | // Loading failed. |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 3189 | if (!klass->IsErroneous()) { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 3190 | mirror::Class::SetStatus(klass, ClassStatus::kErrorUnresolved, self); |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 3191 | } |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 3192 | return sdc.Finish(nullptr); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3193 | } |
| 3194 | CHECK(klass->IsLoaded()); |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 3195 | |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 3196 | // At this point the class is loaded. Publish a ClassLoad event. |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 3197 | // 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] | 3198 | Runtime::Current()->GetRuntimeCallbacks()->ClassLoad(klass); |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 3199 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3200 | // Link the class (if necessary) |
| 3201 | CHECK(!klass->IsResolved()); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3202 | // TODO: Use fast jobjects? |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3203 | auto interfaces = hs.NewHandle<mirror::ObjectArray<mirror::Class>>(nullptr); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3204 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 3205 | MutableHandle<mirror::Class> h_new_class = hs.NewHandle<mirror::Class>(nullptr); |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 3206 | if (!LinkClass(self, descriptor, klass, interfaces, &h_new_class)) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3207 | // Linking failed. |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 3208 | if (!klass->IsErroneous()) { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 3209 | mirror::Class::SetStatus(klass, ClassStatus::kErrorUnresolved, self); |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 3210 | } |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 3211 | return sdc.Finish(nullptr); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3212 | } |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 3213 | self->AssertNoPendingException(); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3214 | CHECK(h_new_class != nullptr) << descriptor; |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 3215 | CHECK(h_new_class->IsResolved() && !h_new_class->IsErroneousResolved()) << descriptor; |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 3216 | |
Sebastien Hertz | a8a697f | 2015-01-15 12:28:47 +0100 | [diff] [blame] | 3217 | // Instrumentation may have updated entrypoints for all methods of all |
| 3218 | // classes. However it could not update methods of this class while we |
| 3219 | // were loading it. Now the class is resolved, we can update entrypoints |
| 3220 | // as required by instrumentation. |
| 3221 | if (Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled()) { |
| 3222 | // We must be in the kRunnable state to prevent instrumentation from |
| 3223 | // suspending all threads to update entrypoints while we are doing it |
| 3224 | // for this class. |
| 3225 | DCHECK_EQ(self->GetState(), kRunnable); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 3226 | Runtime::Current()->GetInstrumentation()->InstallStubsForClass(h_new_class.Get()); |
Sebastien Hertz | a8a697f | 2015-01-15 12:28:47 +0100 | [diff] [blame] | 3227 | } |
| 3228 | |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 3229 | /* |
| 3230 | * We send CLASS_PREPARE events to the debugger from here. The |
| 3231 | * definition of "preparation" is creating the static fields for a |
| 3232 | * class and initializing them to the standard default values, but not |
| 3233 | * executing any code (that comes later, during "initialization"). |
| 3234 | * |
| 3235 | * We did the static preparation in LinkClass. |
| 3236 | * |
| 3237 | * The class has been prepared and resolved but possibly not yet verified |
| 3238 | * at this point. |
| 3239 | */ |
Andreas Gampe | ac30fa2 | 2017-01-18 21:02:36 -0800 | [diff] [blame] | 3240 | Runtime::Current()->GetRuntimeCallbacks()->ClassPrepare(klass, h_new_class); |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 3241 | |
Tamas Berghammer | 160e6df | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 3242 | // Notify native debugger of the new class and its layout. |
| 3243 | jit::Jit::NewTypeLoadedIfUsingJit(h_new_class.Get()); |
| 3244 | |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 3245 | return sdc.Finish(h_new_class); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3246 | } |
| 3247 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3248 | uint32_t ClassLinker::SizeOfClassWithoutEmbeddedTables(const DexFile& dex_file, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 3249 | const dex::ClassDef& dex_class_def) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 3250 | size_t num_ref = 0; |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 3251 | size_t num_8 = 0; |
| 3252 | size_t num_16 = 0; |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 3253 | size_t num_32 = 0; |
| 3254 | size_t num_64 = 0; |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3255 | ClassAccessor accessor(dex_file, dex_class_def); |
| 3256 | // We allow duplicate definitions of the same field in a class_data_item |
| 3257 | // but ignore the repeated indexes here, b/21868015. |
| 3258 | uint32_t last_field_idx = dex::kDexNoIndex; |
| 3259 | for (const ClassAccessor::Field& field : accessor.GetStaticFields()) { |
| 3260 | uint32_t field_idx = field.GetIndex(); |
| 3261 | // Ordering enforced by DexFileVerifier. |
| 3262 | DCHECK(last_field_idx == dex::kDexNoIndex || last_field_idx <= field_idx); |
| 3263 | if (UNLIKELY(field_idx == last_field_idx)) { |
| 3264 | continue; |
| 3265 | } |
| 3266 | last_field_idx = field_idx; |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 3267 | const dex::FieldId& field_id = dex_file.GetFieldId(field_idx); |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3268 | const char* descriptor = dex_file.GetFieldTypeDescriptor(field_id); |
| 3269 | char c = descriptor[0]; |
| 3270 | switch (c) { |
| 3271 | case 'L': |
| 3272 | case '[': |
| 3273 | num_ref++; |
| 3274 | break; |
| 3275 | case 'J': |
| 3276 | case 'D': |
| 3277 | num_64++; |
| 3278 | break; |
| 3279 | case 'I': |
| 3280 | case 'F': |
| 3281 | num_32++; |
| 3282 | break; |
| 3283 | case 'S': |
| 3284 | case 'C': |
| 3285 | num_16++; |
| 3286 | break; |
| 3287 | case 'B': |
| 3288 | case 'Z': |
| 3289 | num_8++; |
| 3290 | break; |
| 3291 | default: |
| 3292 | LOG(FATAL) << "Unknown descriptor: " << c; |
| 3293 | UNREACHABLE(); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 3294 | } |
| 3295 | } |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3296 | return mirror::Class::ComputeClassSize(false, |
| 3297 | 0, |
| 3298 | num_8, |
| 3299 | num_16, |
| 3300 | num_32, |
| 3301 | num_64, |
| 3302 | num_ref, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3303 | image_pointer_size_); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 3304 | } |
| 3305 | |
Alex Light | fc49fec | 2018-01-16 22:28:36 +0000 | [diff] [blame] | 3306 | // Special case to get oat code without overwriting a trampoline. |
| 3307 | const void* ClassLinker::GetQuickOatCodeFor(ArtMethod* method) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3308 | CHECK(method->IsInvokable()) << method->PrettyMethod(); |
Nicolas Geoffray | a7a4759 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 3309 | if (method->IsProxyMethod()) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 3310 | return GetQuickProxyInvokeHandler(); |
Jeff Hao | 8df6cea | 2013-07-29 13:54:48 -0700 | [diff] [blame] | 3311 | } |
Nicolas Geoffray | 3238440 | 2019-07-17 20:06:44 +0100 | [diff] [blame] | 3312 | const void* code = method->GetOatMethodQuickCode(GetImagePointerSize()); |
Alex Light | fc49fec | 2018-01-16 22:28:36 +0000 | [diff] [blame] | 3313 | if (code != nullptr) { |
| 3314 | return code; |
Mathieu Chartier | 2535abe | 2015-02-17 10:38:49 -0800 | [diff] [blame] | 3315 | } |
Nicolas Geoffray | 3238440 | 2019-07-17 20:06:44 +0100 | [diff] [blame] | 3316 | |
| 3317 | jit::Jit* jit = Runtime::Current()->GetJit(); |
| 3318 | if (jit != nullptr) { |
| 3319 | code = jit->GetCodeCache()->GetSavedEntryPointOfPreCompiledMethod(method); |
| 3320 | if (code != nullptr) { |
| 3321 | return code; |
| 3322 | } |
| 3323 | } |
| 3324 | |
Alex Light | fc49fec | 2018-01-16 22:28:36 +0000 | [diff] [blame] | 3325 | if (method->IsNative()) { |
| 3326 | // No code and native? Use generic trampoline. |
| 3327 | return GetQuickGenericJniStub(); |
| 3328 | } |
Nicolas Geoffray | 0039182 | 2019-12-10 10:17:23 +0000 | [diff] [blame] | 3329 | |
Nicolas Geoffray | c39af94 | 2021-01-25 08:43:57 +0000 | [diff] [blame] | 3330 | if (interpreter::CanRuntimeUseNterp() && CanMethodUseNterp(method)) { |
Nicolas Geoffray | 0039182 | 2019-12-10 10:17:23 +0000 | [diff] [blame] | 3331 | return interpreter::GetNterpEntryPoint(); |
| 3332 | } |
| 3333 | |
Alex Light | fc49fec | 2018-01-16 22:28:36 +0000 | [diff] [blame] | 3334 | return GetQuickToInterpreterBridge(); |
TDYa127 | 8532191 | 2012-04-01 15:24:56 -0700 | [diff] [blame] | 3335 | } |
| 3336 | |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 3337 | bool ClassLinker::ShouldUseInterpreterEntrypoint(ArtMethod* method, const void* quick_code) { |
Alex Light | 2d441b1 | 2018-06-08 15:33:21 -0700 | [diff] [blame] | 3338 | ScopedAssertNoThreadSuspension sants(__FUNCTION__); |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 3339 | if (UNLIKELY(method->IsNative() || method->IsProxyMethod())) { |
| 3340 | return false; |
| 3341 | } |
| 3342 | |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 3343 | if (quick_code == nullptr) { |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 3344 | return true; |
| 3345 | } |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 3346 | |
| 3347 | Runtime* runtime = Runtime::Current(); |
| 3348 | instrumentation::Instrumentation* instr = runtime->GetInstrumentation(); |
| 3349 | if (instr->InterpretOnly()) { |
| 3350 | return true; |
| 3351 | } |
| 3352 | |
| 3353 | if (runtime->GetClassLinker()->IsQuickToInterpreterBridge(quick_code)) { |
| 3354 | // Doing this check avoids doing compiled/interpreter transitions. |
| 3355 | return true; |
| 3356 | } |
| 3357 | |
Alex Light | fc58809 | 2020-01-23 15:39:08 -0800 | [diff] [blame] | 3358 | if (Thread::Current()->IsForceInterpreter()) { |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 3359 | // Force the use of interpreter when it is required by the debugger. |
| 3360 | return true; |
| 3361 | } |
| 3362 | |
Alex Light | 8f34aba | 2017-10-09 13:46:32 -0700 | [diff] [blame] | 3363 | if (Thread::Current()->IsAsyncExceptionPending()) { |
| 3364 | // Force use of interpreter to handle async-exceptions |
| 3365 | return true; |
| 3366 | } |
| 3367 | |
Alex Light | 2d441b1 | 2018-06-08 15:33:21 -0700 | [diff] [blame] | 3368 | if (quick_code == GetQuickInstrumentationEntryPoint()) { |
| 3369 | const void* instr_target = instr->GetCodeForInvoke(method); |
| 3370 | DCHECK_NE(instr_target, GetQuickInstrumentationEntryPoint()) << method->PrettyMethod(); |
| 3371 | return ShouldUseInterpreterEntrypoint(method, instr_target); |
| 3372 | } |
| 3373 | |
Nicolas Geoffray | 433b79a | 2017-01-30 20:54:45 +0000 | [diff] [blame] | 3374 | if (runtime->IsJavaDebuggable()) { |
| 3375 | // For simplicity, we ignore precompiled code and go to the interpreter |
| 3376 | // assuming we don't already have jitted code. |
| 3377 | // We could look at the oat file where `quick_code` is being defined, |
| 3378 | // and check whether it's been compiled debuggable, but we decided to |
| 3379 | // only rely on the JIT for debuggable apps. |
Alex Light | 6b16d89 | 2016-11-11 11:21:04 -0800 | [diff] [blame] | 3380 | jit::Jit* jit = Runtime::Current()->GetJit(); |
| 3381 | return (jit == nullptr) || !jit->GetCodeCache()->ContainsPc(quick_code); |
| 3382 | } |
| 3383 | |
Nicolas Geoffray | c9de61c | 2018-11-27 17:34:31 +0000 | [diff] [blame] | 3384 | if (runtime->IsNativeDebuggable()) { |
Calin Juravle | e5de54c | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 3385 | DCHECK(runtime->UseJitCompilation() && runtime->GetJit()->JitAtFirstUse()); |
David Srbecky | f448016 | 2016-03-16 00:06:24 +0000 | [diff] [blame] | 3386 | // If we are doing native debugging, ignore application's AOT code, |
Nicolas Geoffray | 433b79a | 2017-01-30 20:54:45 +0000 | [diff] [blame] | 3387 | // since we want to JIT it (at first use) with extra stackmaps for native |
| 3388 | // debugging. We keep however all AOT code from the boot image, |
| 3389 | // since the JIT-at-first-use is blocking and would result in non-negligible |
| 3390 | // startup performance impact. |
David Srbecky | f448016 | 2016-03-16 00:06:24 +0000 | [diff] [blame] | 3391 | return !runtime->GetHeap()->IsInBootImageOatFile(quick_code); |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 3392 | } |
| 3393 | |
| 3394 | return false; |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 3395 | } |
| 3396 | |
Vladimir Marko | 86c8752 | 2020-05-11 16:55:55 +0100 | [diff] [blame] | 3397 | void ClassLinker::FixupStaticTrampolines(Thread* self, ObjPtr<mirror::Class> klass) { |
Alex Light | 2d441b1 | 2018-06-08 15:33:21 -0700 | [diff] [blame] | 3398 | ScopedAssertNoThreadSuspension sants(__FUNCTION__); |
Vladimir Marko | cce414f | 2019-10-07 08:51:33 +0100 | [diff] [blame] | 3399 | DCHECK(klass->IsVisiblyInitialized()) << klass->PrettyDescriptor(); |
Vladimir Marko | 86c8752 | 2020-05-11 16:55:55 +0100 | [diff] [blame] | 3400 | size_t num_direct_methods = klass->NumDirectMethods(); |
| 3401 | if (num_direct_methods == 0) { |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 3402 | return; // No direct methods => no static methods. |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3403 | } |
Vladimir Marko | cce414f | 2019-10-07 08:51:33 +0100 | [diff] [blame] | 3404 | if (UNLIKELY(klass->IsProxyClass())) { |
| 3405 | return; |
| 3406 | } |
Vladimir Marko | 86c8752 | 2020-05-11 16:55:55 +0100 | [diff] [blame] | 3407 | PointerSize pointer_size = image_pointer_size_; |
| 3408 | if (std::any_of(klass->GetDirectMethods(pointer_size).begin(), |
| 3409 | klass->GetDirectMethods(pointer_size).end(), |
| 3410 | [](const ArtMethod& m) { return m.IsCriticalNative(); })) { |
| 3411 | // Store registered @CriticalNative methods, if any, to JNI entrypoints. |
| 3412 | // Direct methods are a contiguous chunk of memory, so use the ordering of the map. |
| 3413 | ArtMethod* first_method = klass->GetDirectMethod(0u, pointer_size); |
| 3414 | ArtMethod* last_method = klass->GetDirectMethod(num_direct_methods - 1u, pointer_size); |
| 3415 | MutexLock lock(self, critical_native_code_with_clinit_check_lock_); |
| 3416 | auto lb = critical_native_code_with_clinit_check_.lower_bound(first_method); |
| 3417 | while (lb != critical_native_code_with_clinit_check_.end() && lb->first <= last_method) { |
| 3418 | lb->first->SetEntryPointFromJni(lb->second); |
| 3419 | lb = critical_native_code_with_clinit_check_.erase(lb); |
| 3420 | } |
| 3421 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3422 | Runtime* runtime = Runtime::Current(); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 3423 | if (!runtime->IsStarted()) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 3424 | if (runtime->IsAotCompiler() || runtime->GetHeap()->HasBootImageSpace()) { |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 3425 | return; // OAT file unavailable. |
| 3426 | } |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3427 | } |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 3428 | |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 3429 | const DexFile& dex_file = klass->GetDexFile(); |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 3430 | bool has_oat_class; |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3431 | OatFile::OatClass oat_class = OatFile::FindOatClass(dex_file, |
| 3432 | klass->GetDexClassDefIndex(), |
| 3433 | &has_oat_class); |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 3434 | // Link the code of methods skipped by LinkCode. |
Vladimir Marko | 86c8752 | 2020-05-11 16:55:55 +0100 | [diff] [blame] | 3435 | for (size_t method_index = 0; method_index < num_direct_methods; ++method_index) { |
| 3436 | ArtMethod* method = klass->GetDirectMethod(method_index, pointer_size); |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 3437 | if (!method->IsStatic()) { |
| 3438 | // Only update static methods. |
| 3439 | continue; |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3440 | } |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 3441 | const void* quick_code = nullptr; |
Nicolas Geoffray | 0039182 | 2019-12-10 10:17:23 +0000 | [diff] [blame] | 3442 | |
| 3443 | // In order: |
| 3444 | // 1) Check if we have AOT Code. |
| 3445 | // 2) Check if we have JIT Code. |
| 3446 | // 3) Check if we can use Nterp. |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 3447 | if (has_oat_class) { |
| 3448 | OatFile::OatMethod oat_method = oat_class.GetOatMethod(method_index); |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 3449 | quick_code = oat_method.GetQuickCode(); |
| 3450 | } |
Nicolas Geoffray | 0039182 | 2019-12-10 10:17:23 +0000 | [diff] [blame] | 3451 | |
Vladimir Marko | cce414f | 2019-10-07 08:51:33 +0100 | [diff] [blame] | 3452 | jit::Jit* jit = runtime->GetJit(); |
Nicolas Geoffray | 3238440 | 2019-07-17 20:06:44 +0100 | [diff] [blame] | 3453 | if (quick_code == nullptr && jit != nullptr) { |
| 3454 | quick_code = jit->GetCodeCache()->GetSavedEntryPointOfPreCompiledMethod(method); |
Nicolas Geoffray | 7989ac9 | 2019-04-10 12:42:30 +0100 | [diff] [blame] | 3455 | } |
Nicolas Geoffray | 0039182 | 2019-12-10 10:17:23 +0000 | [diff] [blame] | 3456 | |
| 3457 | if (quick_code == nullptr && |
| 3458 | interpreter::CanRuntimeUseNterp() && |
Nicolas Geoffray | c39af94 | 2021-01-25 08:43:57 +0000 | [diff] [blame] | 3459 | CanMethodUseNterp(method)) { |
Nicolas Geoffray | 0039182 | 2019-12-10 10:17:23 +0000 | [diff] [blame] | 3460 | quick_code = interpreter::GetNterpEntryPoint(); |
| 3461 | } |
| 3462 | |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 3463 | // Check whether the method is native, in which case it's generic JNI. |
Ulya Trafimovich | 5439f05 | 2020-07-29 10:03:46 +0100 | [diff] [blame] | 3464 | if (quick_code == nullptr && method->IsNative()) { |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 3465 | quick_code = GetQuickGenericJniStub(); |
| 3466 | } else if (ShouldUseInterpreterEntrypoint(method, quick_code)) { |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 3467 | // Use interpreter entry point. |
Nicolas Geoffray | 0039182 | 2019-12-10 10:17:23 +0000 | [diff] [blame] | 3468 | if (IsQuickToInterpreterBridge(method->GetEntryPointFromQuickCompiledCode())) { |
| 3469 | // If we have the trampoline or the bridge already, no need to update. |
| 3470 | // This saves in not dirtying boot image memory. |
| 3471 | continue; |
| 3472 | } |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 3473 | quick_code = GetQuickToInterpreterBridge(); |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 3474 | } |
Nicolas Geoffray | 0039182 | 2019-12-10 10:17:23 +0000 | [diff] [blame] | 3475 | CHECK(quick_code != nullptr); |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 3476 | runtime->GetInstrumentation()->UpdateMethodsCode(method, quick_code); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3477 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3478 | // Ignore virtual methods on the iterator. |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3479 | } |
| 3480 | |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3481 | // Does anything needed to make sure that the compiler will not generate a direct invoke to this |
| 3482 | // method. Should only be called on non-invokable methods. |
Nicolas Geoffray | f05f04b | 2019-10-31 11:50:41 +0000 | [diff] [blame] | 3483 | inline void EnsureThrowsInvocationError(ClassLinker* class_linker, ArtMethod* method) |
| 3484 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 3485 | DCHECK(method != nullptr); |
| 3486 | DCHECK(!method->IsInvokable()); |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3487 | method->SetEntryPointFromQuickCompiledCodePtrSize( |
| 3488 | class_linker->GetQuickToInterpreterBridgeTrampoline(), |
| 3489 | class_linker->GetImagePointerSize()); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 3490 | } |
| 3491 | |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3492 | static void LinkCode(ClassLinker* class_linker, |
| 3493 | ArtMethod* method, |
| 3494 | const OatFile::OatClass* oat_class, |
| 3495 | uint32_t class_def_method_index) REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | 2d441b1 | 2018-06-08 15:33:21 -0700 | [diff] [blame] | 3496 | ScopedAssertNoThreadSuspension sants(__FUNCTION__); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3497 | Runtime* const runtime = Runtime::Current(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 3498 | if (runtime->IsAotCompiler()) { |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 3499 | // The following code only applies to a non-compiler runtime. |
| 3500 | return; |
| 3501 | } |
Nicolas Geoffray | 5ee206f | 2019-10-08 15:09:17 +0100 | [diff] [blame] | 3502 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3503 | // Method shouldn't have already been linked. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 3504 | DCHECK(method->GetEntryPointFromQuickCompiledCode() == nullptr); |
Jeff Hao | 1674363 | 2013-05-08 10:59:04 -0700 | [diff] [blame] | 3505 | |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 3506 | if (!method->IsInvokable()) { |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3507 | EnsureThrowsInvocationError(class_linker, method); |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 3508 | return; |
| 3509 | } |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3510 | |
Nicolas Geoffray | 5ee206f | 2019-10-08 15:09:17 +0100 | [diff] [blame] | 3511 | const void* quick_code = nullptr; |
| 3512 | if (oat_class != nullptr) { |
| 3513 | // Every kind of method should at least get an invoke stub from the oat_method. |
| 3514 | // non-abstract methods also get their code pointers. |
| 3515 | const OatFile::OatMethod oat_method = oat_class->GetOatMethod(class_def_method_index); |
| 3516 | quick_code = oat_method.GetQuickCode(); |
| 3517 | } |
| 3518 | |
| 3519 | bool enter_interpreter = class_linker->ShouldUseInterpreterEntrypoint(method, quick_code); |
| 3520 | |
| 3521 | // Note: this mimics the logic in image_writer.cc that installs the resolution |
| 3522 | // stub only if we have compiled code and the method needs a class initialization |
| 3523 | // check. |
Ulya Trafimovich | 5439f05 | 2020-07-29 10:03:46 +0100 | [diff] [blame] | 3524 | if (quick_code == nullptr) { |
Nicolas Geoffray | 5ee206f | 2019-10-08 15:09:17 +0100 | [diff] [blame] | 3525 | method->SetEntryPointFromQuickCompiledCode( |
| 3526 | method->IsNative() ? GetQuickGenericJniStub() : GetQuickToInterpreterBridge()); |
| 3527 | } else if (enter_interpreter) { |
| 3528 | method->SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge()); |
Vladimir Marko | 5115a4d | 2019-10-17 14:56:47 +0100 | [diff] [blame] | 3529 | } else if (NeedsClinitCheckBeforeCall(method)) { |
| 3530 | DCHECK(!method->GetDeclaringClass()->IsVisiblyInitialized()); // Actually ClassStatus::Idx. |
| 3531 | // If we do have code but the method needs a class initialization check before calling |
| 3532 | // that code, install the resolution stub that will perform the check. |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 3533 | // It will be replaced by the proper entry point by ClassLinker::FixupStaticTrampolines |
| 3534 | // after initializing class (see ClassLinker::InitializeClass method). |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 3535 | method->SetEntryPointFromQuickCompiledCode(GetQuickResolutionStub()); |
Nicolas Geoffray | 5ee206f | 2019-10-08 15:09:17 +0100 | [diff] [blame] | 3536 | } else { |
| 3537 | method->SetEntryPointFromQuickCompiledCode(quick_code); |
Ian Rogers | 0d6de04 | 2012-02-29 08:50:26 -0800 | [diff] [blame] | 3538 | } |
jeffhao | 26c0a1a | 2012-01-17 16:28:33 -0800 | [diff] [blame] | 3539 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3540 | if (method->IsNative()) { |
Vladimir Marko | 86c8752 | 2020-05-11 16:55:55 +0100 | [diff] [blame] | 3541 | // Set up the dlsym lookup stub. Do not go through `UnregisterNative()` |
| 3542 | // as the extra processing for @CriticalNative is not needed yet. |
| 3543 | method->SetEntryPointFromJni( |
| 3544 | method->IsCriticalNative() ? GetJniDlsymLookupCriticalStub() : GetJniDlsymLookupStub()); |
Andreas Gampe | 9054683 | 2014-03-12 18:07:19 -0700 | [diff] [blame] | 3545 | |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 3546 | if (enter_interpreter || quick_code == nullptr) { |
Nicolas Geoffray | 5ee206f | 2019-10-08 15:09:17 +0100 | [diff] [blame] | 3547 | // We have a native method here without code. Then it should have the generic JNI |
| 3548 | // trampoline as entrypoint. |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 3549 | // 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] | 3550 | DCHECK(class_linker->IsQuickGenericJniStub(method->GetEntryPointFromQuickCompiledCode())); |
Andreas Gampe | 9054683 | 2014-03-12 18:07:19 -0700 | [diff] [blame] | 3551 | } |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 3552 | } |
| 3553 | } |
| 3554 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3555 | void ClassLinker::SetupClass(const DexFile& dex_file, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 3556 | const dex::ClassDef& dex_class_def, |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3557 | Handle<mirror::Class> klass, |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3558 | ObjPtr<mirror::ClassLoader> class_loader) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3559 | CHECK(klass != nullptr); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3560 | CHECK(klass->GetDexCache() != nullptr); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 3561 | CHECK_EQ(ClassStatus::kNotReady, klass->GetStatus()); |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 3562 | const char* descriptor = dex_file.GetClassDescriptor(dex_class_def); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3563 | CHECK(descriptor != nullptr); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3564 | |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 3565 | klass->SetClass(GetClassRoot<mirror::Class>(this)); |
Andreas Gampe | 5182932 | 2014-08-25 15:05:04 -0700 | [diff] [blame] | 3566 | uint32_t access_flags = dex_class_def.GetJavaAccessFlags(); |
Brian Carlstrom | 8e3fb14 | 2013-10-09 21:00:27 -0700 | [diff] [blame] | 3567 | CHECK_EQ(access_flags & ~kAccJavaFlagsMask, 0U); |
Vladimir Marko | b68bb7a | 2020-03-17 10:55:25 +0000 | [diff] [blame] | 3568 | klass->SetAccessFlagsDuringLinking(access_flags); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3569 | klass->SetClassLoader(class_loader); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3570 | DCHECK_EQ(klass->GetPrimitiveType(), Primitive::kPrimNot); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 3571 | mirror::Class::SetStatus(klass, ClassStatus::kIdx, nullptr); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3572 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 3573 | klass->SetDexClassDefIndex(dex_file.GetIndexForClassDef(dex_class_def)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3574 | klass->SetDexTypeIndex(dex_class_def.class_idx_); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3575 | } |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3576 | |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3577 | LengthPrefixedArray<ArtField>* ClassLinker::AllocArtFieldArray(Thread* self, |
| 3578 | LinearAlloc* allocator, |
| 3579 | size_t length) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3580 | if (length == 0) { |
| 3581 | return nullptr; |
| 3582 | } |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 3583 | // If the ArtField alignment changes, review all uses of LengthPrefixedArray<ArtField>. |
| 3584 | static_assert(alignof(ArtField) == 4, "ArtField alignment is expected to be 4."); |
| 3585 | size_t storage_size = LengthPrefixedArray<ArtField>::ComputeSize(length); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3586 | void* array_storage = allocator->Alloc(self, storage_size); |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 3587 | auto* ret = new(array_storage) LengthPrefixedArray<ArtField>(length); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3588 | CHECK(ret != nullptr); |
| 3589 | std::uninitialized_fill_n(&ret->At(0), length, ArtField()); |
| 3590 | return ret; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3591 | } |
| 3592 | |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3593 | LengthPrefixedArray<ArtMethod>* ClassLinker::AllocArtMethodArray(Thread* self, |
| 3594 | LinearAlloc* allocator, |
| 3595 | size_t length) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3596 | if (length == 0) { |
| 3597 | return nullptr; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3598 | } |
Vladimir Marko | 1463285 | 2015-08-17 12:07:23 +0100 | [diff] [blame] | 3599 | const size_t method_alignment = ArtMethod::Alignment(image_pointer_size_); |
| 3600 | const size_t method_size = ArtMethod::Size(image_pointer_size_); |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 3601 | const size_t storage_size = |
| 3602 | LengthPrefixedArray<ArtMethod>::ComputeSize(length, method_size, method_alignment); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3603 | void* array_storage = allocator->Alloc(self, storage_size); |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 3604 | auto* ret = new (array_storage) LengthPrefixedArray<ArtMethod>(length); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3605 | CHECK(ret != nullptr); |
| 3606 | for (size_t i = 0; i < length; ++i) { |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 3607 | new(reinterpret_cast<void*>(&ret->At(i, method_size, method_alignment))) ArtMethod; |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3608 | } |
| 3609 | return ret; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3610 | } |
| 3611 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3612 | LinearAlloc* ClassLinker::GetAllocatorForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3613 | if (class_loader == nullptr) { |
| 3614 | return Runtime::Current()->GetLinearAlloc(); |
| 3615 | } |
| 3616 | LinearAlloc* allocator = class_loader->GetAllocator(); |
| 3617 | DCHECK(allocator != nullptr); |
| 3618 | return allocator; |
| 3619 | } |
| 3620 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3621 | LinearAlloc* ClassLinker::GetOrCreateAllocatorForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 3622 | if (class_loader == nullptr) { |
| 3623 | return Runtime::Current()->GetLinearAlloc(); |
| 3624 | } |
| 3625 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
| 3626 | LinearAlloc* allocator = class_loader->GetAllocator(); |
| 3627 | if (allocator == nullptr) { |
Mathieu Chartier | 5b83050 | 2016-03-02 10:30:23 -0800 | [diff] [blame] | 3628 | RegisterClassLoader(class_loader); |
| 3629 | allocator = class_loader->GetAllocator(); |
| 3630 | CHECK(allocator != nullptr); |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 3631 | } |
| 3632 | return allocator; |
| 3633 | } |
| 3634 | |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3635 | void ClassLinker::LoadClass(Thread* self, |
| 3636 | const DexFile& dex_file, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 3637 | const dex::ClassDef& dex_class_def, |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3638 | Handle<mirror::Class> klass) { |
David Brazdil | 20c765f | 2018-10-27 21:45:15 +0000 | [diff] [blame] | 3639 | ClassAccessor accessor(dex_file, |
| 3640 | dex_class_def, |
| 3641 | /* parse_hiddenapi_class_data= */ klass->IsBootStrapClassLoaded()); |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3642 | if (!accessor.HasClassData()) { |
| 3643 | return; |
| 3644 | } |
| 3645 | Runtime* const runtime = Runtime::Current(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3646 | { |
| 3647 | // Note: We cannot have thread suspension until the field and method arrays are setup or else |
| 3648 | // Class::VisitFieldRoots may miss some fields or methods. |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 3649 | ScopedAssertNoThreadSuspension nts(__FUNCTION__); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3650 | // Load static fields. |
Vladimir Marko | 23682bf | 2015-06-24 14:28:03 +0100 | [diff] [blame] | 3651 | // We allow duplicate definitions of the same field in a class_data_item |
| 3652 | // but ignore the repeated indexes here, b/21868015. |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3653 | LinearAlloc* const allocator = GetAllocatorForClassLoader(klass->GetClassLoader()); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3654 | LengthPrefixedArray<ArtField>* sfields = AllocArtFieldArray(self, |
| 3655 | allocator, |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3656 | accessor.NumStaticFields()); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3657 | LengthPrefixedArray<ArtField>* ifields = AllocArtFieldArray(self, |
| 3658 | allocator, |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3659 | accessor.NumInstanceFields()); |
| 3660 | size_t num_sfields = 0u; |
Vladimir Marko | 23682bf | 2015-06-24 14:28:03 +0100 | [diff] [blame] | 3661 | size_t num_ifields = 0u; |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3662 | uint32_t last_static_field_idx = 0u; |
| 3663 | uint32_t last_instance_field_idx = 0u; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 3664 | |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3665 | // Methods |
| 3666 | bool has_oat_class = false; |
| 3667 | const OatFile::OatClass oat_class = (runtime->IsStarted() && !runtime->IsAotCompiler()) |
| 3668 | ? OatFile::FindOatClass(dex_file, klass->GetDexClassDefIndex(), &has_oat_class) |
| 3669 | : OatFile::OatClass::Invalid(); |
| 3670 | const OatFile::OatClass* oat_class_ptr = has_oat_class ? &oat_class : nullptr; |
| 3671 | klass->SetMethodsPtr( |
| 3672 | AllocArtMethodArray(self, allocator, accessor.NumMethods()), |
| 3673 | accessor.NumDirectMethods(), |
| 3674 | accessor.NumVirtualMethods()); |
| 3675 | size_t class_def_method_index = 0; |
| 3676 | uint32_t last_dex_method_index = dex::kDexNoIndex; |
| 3677 | size_t last_class_def_method_index = 0; |
| 3678 | |
| 3679 | // Use the visitor since the ranged based loops are bit slower from seeking. Seeking to the |
| 3680 | // methods needs to decode all of the fields. |
| 3681 | accessor.VisitFieldsAndMethods([&]( |
| 3682 | const ClassAccessor::Field& field) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 3683 | uint32_t field_idx = field.GetIndex(); |
| 3684 | DCHECK_GE(field_idx, last_static_field_idx); // Ordering enforced by DexFileVerifier. |
| 3685 | if (num_sfields == 0 || LIKELY(field_idx > last_static_field_idx)) { |
| 3686 | LoadField(field, klass, &sfields->At(num_sfields)); |
| 3687 | ++num_sfields; |
| 3688 | last_static_field_idx = field_idx; |
| 3689 | } |
| 3690 | }, [&](const ClassAccessor::Field& field) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 3691 | uint32_t field_idx = field.GetIndex(); |
| 3692 | DCHECK_GE(field_idx, last_instance_field_idx); // Ordering enforced by DexFileVerifier. |
| 3693 | if (num_ifields == 0 || LIKELY(field_idx > last_instance_field_idx)) { |
| 3694 | LoadField(field, klass, &ifields->At(num_ifields)); |
| 3695 | ++num_ifields; |
| 3696 | last_instance_field_idx = field_idx; |
| 3697 | } |
| 3698 | }, [&](const ClassAccessor::Method& method) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 3699 | ArtMethod* art_method = klass->GetDirectMethodUnchecked(class_def_method_index, |
| 3700 | image_pointer_size_); |
| 3701 | LoadMethod(dex_file, method, klass, art_method); |
| 3702 | LinkCode(this, art_method, oat_class_ptr, class_def_method_index); |
| 3703 | uint32_t it_method_index = method.GetIndex(); |
| 3704 | if (last_dex_method_index == it_method_index) { |
| 3705 | // duplicate case |
| 3706 | art_method->SetMethodIndex(last_class_def_method_index); |
| 3707 | } else { |
| 3708 | art_method->SetMethodIndex(class_def_method_index); |
| 3709 | last_dex_method_index = it_method_index; |
| 3710 | last_class_def_method_index = class_def_method_index; |
| 3711 | } |
| 3712 | ++class_def_method_index; |
| 3713 | }, [&](const ClassAccessor::Method& method) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 3714 | ArtMethod* art_method = klass->GetVirtualMethodUnchecked( |
| 3715 | class_def_method_index - accessor.NumDirectMethods(), |
| 3716 | image_pointer_size_); |
| 3717 | LoadMethod(dex_file, method, klass, art_method); |
| 3718 | LinkCode(this, art_method, oat_class_ptr, class_def_method_index); |
| 3719 | ++class_def_method_index; |
| 3720 | }); |
| 3721 | |
| 3722 | if (UNLIKELY(num_ifields + num_sfields != accessor.NumFields())) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3723 | LOG(WARNING) << "Duplicate fields in class " << klass->PrettyDescriptor() |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3724 | << " (unique static fields: " << num_sfields << "/" << accessor.NumStaticFields() |
| 3725 | << ", unique instance fields: " << num_ifields << "/" << accessor.NumInstanceFields() |
| 3726 | << ")"; |
Vladimir Marko | 81819db | 2015-11-05 15:30:12 +0000 | [diff] [blame] | 3727 | // NOTE: Not shrinking the over-allocated sfields/ifields, just setting size. |
| 3728 | if (sfields != nullptr) { |
| 3729 | sfields->SetSize(num_sfields); |
| 3730 | } |
| 3731 | if (ifields != nullptr) { |
| 3732 | ifields->SetSize(num_ifields); |
| 3733 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3734 | } |
Vladimir Marko | 81819db | 2015-11-05 15:30:12 +0000 | [diff] [blame] | 3735 | // Set the field arrays. |
| 3736 | klass->SetSFieldsPtr(sfields); |
| 3737 | DCHECK_EQ(klass->NumStaticFields(), num_sfields); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3738 | klass->SetIFieldsPtr(ifields); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3739 | DCHECK_EQ(klass->NumInstanceFields(), num_ifields); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3740 | } |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 3741 | // 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] | 3742 | WriteBarrier::ForEveryFieldWrite(klass.Get()); |
Mathieu Chartier | f3f2a7a | 2015-04-14 15:43:10 -0700 | [diff] [blame] | 3743 | self->AllowThreadSuspension(); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3744 | } |
| 3745 | |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3746 | void ClassLinker::LoadField(const ClassAccessor::Field& field, |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3747 | Handle<mirror::Class> klass, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3748 | ArtField* dst) { |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3749 | const uint32_t field_idx = field.GetIndex(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3750 | dst->SetDexFieldIndex(field_idx); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3751 | dst->SetDeclaringClass(klass.Get()); |
David Brazdil | f6a8a55 | 2018-01-15 18:10:50 +0000 | [diff] [blame] | 3752 | |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 3753 | // Get access flags from the DexFile and set hiddenapi runtime access flags. |
| 3754 | dst->SetAccessFlags(field.GetAccessFlags() | hiddenapi::CreateRuntimeFlags(field)); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3755 | } |
| 3756 | |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 3757 | void ClassLinker::LoadMethod(const DexFile& dex_file, |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3758 | const ClassAccessor::Method& method, |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3759 | Handle<mirror::Class> klass, |
| 3760 | ArtMethod* dst) { |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3761 | const uint32_t dex_method_idx = method.GetIndex(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 3762 | const dex::MethodId& method_id = dex_file.GetMethodId(dex_method_idx); |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 3763 | const char* method_name = dex_file.StringDataByIdx(method_id.name_idx_); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 3764 | |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 3765 | ScopedAssertNoThreadSuspension ants("LoadMethod"); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 3766 | dst->SetDexMethodIndex(dex_method_idx); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3767 | dst->SetDeclaringClass(klass.Get()); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3768 | |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 3769 | // Get access flags from the DexFile and set hiddenapi runtime access flags. |
| 3770 | uint32_t access_flags = method.GetAccessFlags() | hiddenapi::CreateRuntimeFlags(method); |
David Brazdil | f6a8a55 | 2018-01-15 18:10:50 +0000 | [diff] [blame] | 3771 | |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 3772 | if (UNLIKELY(strcmp("finalize", method_name) == 0)) { |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3773 | // Set finalizable flag on declaring class. |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 3774 | if (strcmp("V", dex_file.GetShorty(method_id.proto_idx_)) == 0) { |
| 3775 | // Void return type. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3776 | if (klass->GetClassLoader() != nullptr) { // All non-boot finalizer methods are flagged. |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 3777 | klass->SetFinalizable(); |
| 3778 | } else { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3779 | std::string temp; |
| 3780 | const char* klass_descriptor = klass->GetDescriptor(&temp); |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 3781 | // The Enum class declares a "final" finalize() method to prevent subclasses from |
| 3782 | // introducing a finalizer. We don't want to set the finalizable flag for Enum or its |
| 3783 | // subclasses, so we exclude it here. |
| 3784 | // We also want to avoid setting the flag on Object, where we know that finalize() is |
| 3785 | // empty. |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3786 | if (strcmp(klass_descriptor, "Ljava/lang/Object;") != 0 && |
| 3787 | strcmp(klass_descriptor, "Ljava/lang/Enum;") != 0) { |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3788 | klass->SetFinalizable(); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3789 | } |
| 3790 | } |
| 3791 | } |
| 3792 | } else if (method_name[0] == '<') { |
| 3793 | // Fix broken access flags for initializers. Bug 11157540. |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 3794 | bool is_init = (strcmp("<init>", method_name) == 0); |
| 3795 | bool is_clinit = !is_init && (strcmp("<clinit>", method_name) == 0); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3796 | if (UNLIKELY(!is_init && !is_clinit)) { |
| 3797 | LOG(WARNING) << "Unexpected '<' at start of method name " << method_name; |
| 3798 | } else { |
| 3799 | if (UNLIKELY((access_flags & kAccConstructor) == 0)) { |
| 3800 | 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] | 3801 | << klass->PrettyDescriptor() << " in dex file " << dex_file.GetLocation(); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3802 | access_flags |= kAccConstructor; |
| 3803 | } |
| 3804 | } |
| 3805 | } |
Vladimir Marko | b0a6aee | 2017-10-27 10:34:04 +0100 | [diff] [blame] | 3806 | if (UNLIKELY((access_flags & kAccNative) != 0u)) { |
| 3807 | // Check if the native method is annotated with @FastNative or @CriticalNative. |
| 3808 | access_flags |= annotations::GetNativeMethodAnnotationAccessFlags( |
| 3809 | dex_file, dst->GetClassDef(), dex_method_idx); |
| 3810 | } |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3811 | dst->SetAccessFlags(access_flags); |
David Srbecky | e36e7f2 | 2018-11-14 14:21:23 +0000 | [diff] [blame] | 3812 | // Must be done after SetAccessFlags since IsAbstract depends on it. |
| 3813 | if (klass->IsInterface() && dst->IsAbstract()) { |
| 3814 | dst->CalculateAndSetImtIndex(); |
| 3815 | } |
Nicolas Geoffray | 4717175 | 2020-08-31 15:03:20 +0100 | [diff] [blame] | 3816 | if (dst->HasCodeItem()) { |
| 3817 | DCHECK_NE(method.GetCodeItemOffset(), 0u); |
| 3818 | if (Runtime::Current()->IsAotCompiler()) { |
| 3819 | dst->SetDataPtrSize(reinterpret_cast32<void*>(method.GetCodeItemOffset()), image_pointer_size_); |
| 3820 | } else { |
Nicolas Geoffray | e1d2dce | 2020-09-21 10:06:31 +0100 | [diff] [blame] | 3821 | dst->SetCodeItem(dst->GetDexFile()->GetCodeItem(method.GetCodeItemOffset())); |
Nicolas Geoffray | 4717175 | 2020-08-31 15:03:20 +0100 | [diff] [blame] | 3822 | } |
| 3823 | } else { |
| 3824 | dst->SetDataPtrSize(nullptr, image_pointer_size_); |
| 3825 | DCHECK_EQ(method.GetCodeItemOffset(), 0u); |
| 3826 | } |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3827 | } |
| 3828 | |
Mathieu Chartier | 0a19e21 | 2019-11-27 14:35:24 -0800 | [diff] [blame] | 3829 | void ClassLinker::AppendToBootClassPath(Thread* self, const DexFile* dex_file) { |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3830 | ObjPtr<mirror::DexCache> dex_cache = AllocAndInitializeDexCache( |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 3831 | self, |
Mathieu Chartier | 0a19e21 | 2019-11-27 14:35:24 -0800 | [diff] [blame] | 3832 | *dex_file, |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3833 | Runtime::Current()->GetLinearAlloc()); |
Mathieu Chartier | 0a19e21 | 2019-11-27 14:35:24 -0800 | [diff] [blame] | 3834 | 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] | 3835 | AppendToBootClassPath(dex_file, dex_cache); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 3836 | } |
| 3837 | |
Mathieu Chartier | 0a19e21 | 2019-11-27 14:35:24 -0800 | [diff] [blame] | 3838 | void ClassLinker::AppendToBootClassPath(const DexFile* dex_file, |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3839 | ObjPtr<mirror::DexCache> dex_cache) { |
Mathieu Chartier | 0a19e21 | 2019-11-27 14:35:24 -0800 | [diff] [blame] | 3840 | CHECK(dex_file != nullptr); |
| 3841 | CHECK(dex_cache != nullptr) << dex_file->GetLocation(); |
| 3842 | boot_class_path_.push_back(dex_file); |
Andreas Gampe | be7af22 | 2017-07-25 09:57:28 -0700 | [diff] [blame] | 3843 | WriterMutexLock mu(Thread::Current(), *Locks::dex_lock_); |
Mathieu Chartier | 0a19e21 | 2019-11-27 14:35:24 -0800 | [diff] [blame] | 3844 | RegisterDexFileLocked(*dex_file, dex_cache, /* class_loader= */ nullptr); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 3845 | } |
| 3846 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 3847 | void ClassLinker::RegisterDexFileLocked(const DexFile& dex_file, |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3848 | ObjPtr<mirror::DexCache> dex_cache, |
| 3849 | ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3850 | Thread* const self = Thread::Current(); |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 3851 | Locks::dex_lock_->AssertExclusiveHeld(self); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3852 | CHECK(dex_cache != nullptr) << dex_file.GetLocation(); |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 3853 | CHECK_EQ(dex_cache->GetDexFile(), &dex_file) << dex_file.GetLocation(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 3854 | // For app images, the dex cache location may be a suffix of the dex file location since the |
| 3855 | // dex file location is an absolute path. |
Mathieu Chartier | 7617216 | 2016-01-26 14:54:06 -0800 | [diff] [blame] | 3856 | const std::string dex_cache_location = dex_cache->GetLocation()->ToModifiedUtf8(); |
| 3857 | const size_t dex_cache_length = dex_cache_location.length(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 3858 | CHECK_GT(dex_cache_length, 0u) << dex_file.GetLocation(); |
| 3859 | std::string dex_file_location = dex_file.GetLocation(); |
Nicolas Geoffray | e3e0f70 | 2019-03-12 07:02:02 +0000 | [diff] [blame] | 3860 | // The following paths checks don't work on preopt when using boot dex files, where the dex |
| 3861 | // cache location is the one on device, and the dex_file's location is the one on host. |
| 3862 | if (!(Runtime::Current()->IsAotCompiler() && class_loader == nullptr && !kIsTargetBuild)) { |
| 3863 | CHECK_GE(dex_file_location.length(), dex_cache_length) |
| 3864 | << dex_cache_location << " " << dex_file.GetLocation(); |
| 3865 | const std::string dex_file_suffix = dex_file_location.substr( |
| 3866 | dex_file_location.length() - dex_cache_length, |
| 3867 | dex_cache_length); |
| 3868 | // Example dex_cache location is SettingsProvider.apk and |
| 3869 | // dex file location is /system/priv-app/SettingsProvider/SettingsProvider.apk |
| 3870 | CHECK_EQ(dex_cache_location, dex_file_suffix); |
| 3871 | } |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 3872 | const OatFile* oat_file = |
| 3873 | (dex_file.GetOatDexFile() != nullptr) ? dex_file.GetOatDexFile()->GetOatFile() : nullptr; |
Vladimir Marko | b066d43 | 2018-01-03 13:14:37 +0000 | [diff] [blame] | 3874 | // Clean up pass to remove null dex caches; null dex caches can occur due to class unloading |
| 3875 | // and we are lazily removing null entries. Also check if we need to initialize OatFile data |
| 3876 | // (.data.bimg.rel.ro and .bss sections) needed for code execution. |
| 3877 | bool initialize_oat_file_data = (oat_file != nullptr) && oat_file->IsExecutable(); |
Ian Rogers | 55256cb | 2017-12-21 17:07:11 -0800 | [diff] [blame] | 3878 | JavaVMExt* const vm = self->GetJniEnv()->GetVm(); |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 3879 | for (auto it = dex_caches_.begin(); it != dex_caches_.end(); ) { |
| 3880 | DexCacheData data = *it; |
| 3881 | if (self->IsJWeakCleared(data.weak_root)) { |
| 3882 | vm->DeleteWeakGlobalRef(self, data.weak_root); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3883 | it = dex_caches_.erase(it); |
| 3884 | } else { |
Vladimir Marko | b066d43 | 2018-01-03 13:14:37 +0000 | [diff] [blame] | 3885 | if (initialize_oat_file_data && |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 3886 | it->dex_file->GetOatDexFile() != nullptr && |
| 3887 | it->dex_file->GetOatDexFile()->GetOatFile() == oat_file) { |
Vladimir Marko | b066d43 | 2018-01-03 13:14:37 +0000 | [diff] [blame] | 3888 | initialize_oat_file_data = false; // Already initialized. |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 3889 | } |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3890 | ++it; |
| 3891 | } |
Brian Carlstrom | 81a9087 | 2015-08-28 09:07:14 -0700 | [diff] [blame] | 3892 | } |
Vladimir Marko | b066d43 | 2018-01-03 13:14:37 +0000 | [diff] [blame] | 3893 | if (initialize_oat_file_data) { |
Vladimir Marko | 1cedb4a | 2019-02-06 14:13:28 +0000 | [diff] [blame] | 3894 | oat_file->InitializeRelocations(); |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 3895 | } |
David Brazdil | a5c3a80 | 2019-03-08 14:59:41 +0000 | [diff] [blame] | 3896 | // Let hiddenapi assign a domain to the newly registered dex file. |
| 3897 | hiddenapi::InitializeDexFileDomain(dex_file, class_loader); |
| 3898 | |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3899 | jweak dex_cache_jweak = vm->AddWeakGlobalRef(self, dex_cache); |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 3900 | DexCacheData data; |
| 3901 | data.weak_root = dex_cache_jweak; |
| 3902 | data.dex_file = dex_cache->GetDexFile(); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3903 | data.class_table = ClassTableForClassLoader(class_loader); |
David Srbecky | afc60cd | 2018-12-05 11:59:31 +0000 | [diff] [blame] | 3904 | AddNativeDebugInfoForDex(self, data.dex_file); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3905 | DCHECK(data.class_table != nullptr); |
Mathieu Chartier | 72041a0 | 2017-07-14 18:23:25 -0700 | [diff] [blame] | 3906 | // Make sure to hold the dex cache live in the class table. This case happens for the boot class |
| 3907 | // path dex caches without an image. |
| 3908 | data.class_table->InsertStrongRoot(dex_cache); |
Andreas Gampe | 8a1a0f7 | 2020-03-03 16:07:45 -0800 | [diff] [blame] | 3909 | // Make sure that the dex cache holds the classloader live. |
| 3910 | dex_cache->SetClassLoader(class_loader); |
Mathieu Chartier | 72041a0 | 2017-07-14 18:23:25 -0700 | [diff] [blame] | 3911 | if (class_loader != nullptr) { |
| 3912 | // Since we added a strong root to the class table, do the write barrier as required for |
| 3913 | // remembered sets and generational GCs. |
Mathieu Chartier | 88ea61e | 2018-06-20 17:45:41 -0700 | [diff] [blame] | 3914 | WriteBarrier::ForEveryFieldWrite(class_loader); |
Mathieu Chartier | 72041a0 | 2017-07-14 18:23:25 -0700 | [diff] [blame] | 3915 | } |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 3916 | dex_caches_.push_back(data); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 3917 | } |
| 3918 | |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 3919 | ObjPtr<mirror::DexCache> ClassLinker::DecodeDexCacheLocked(Thread* self, const DexCacheData* data) { |
| 3920 | return data != nullptr |
| 3921 | ? ObjPtr<mirror::DexCache>::DownCast(self->DecodeJObject(data->weak_root)) |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3922 | : nullptr; |
| 3923 | } |
| 3924 | |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 3925 | bool ClassLinker::IsSameClassLoader( |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3926 | ObjPtr<mirror::DexCache> dex_cache, |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 3927 | const DexCacheData* data, |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3928 | ObjPtr<mirror::ClassLoader> class_loader) { |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 3929 | CHECK(data != nullptr); |
| 3930 | DCHECK_EQ(dex_cache->GetDexFile(), data->dex_file); |
| 3931 | return data->class_table == ClassTableForClassLoader(class_loader); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3932 | } |
| 3933 | |
Alex Light | 07f0621 | 2017-06-01 14:01:43 -0700 | [diff] [blame] | 3934 | void ClassLinker::RegisterExistingDexCache(ObjPtr<mirror::DexCache> dex_cache, |
| 3935 | ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | ed4ee44 | 2018-06-05 14:23:35 -0700 | [diff] [blame] | 3936 | SCOPED_TRACE << __FUNCTION__ << " " << dex_cache->GetDexFile()->GetLocation(); |
Alex Light | 07f0621 | 2017-06-01 14:01:43 -0700 | [diff] [blame] | 3937 | Thread* self = Thread::Current(); |
| 3938 | StackHandleScope<2> hs(self); |
| 3939 | Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(dex_cache)); |
| 3940 | Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader)); |
| 3941 | const DexFile* dex_file = dex_cache->GetDexFile(); |
| 3942 | DCHECK(dex_file != nullptr) << "Attempt to register uninitialized dex_cache object!"; |
| 3943 | if (kIsDebugBuild) { |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 3944 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
| 3945 | const DexCacheData* old_data = FindDexCacheDataLocked(*dex_file); |
| 3946 | ObjPtr<mirror::DexCache> old_dex_cache = DecodeDexCacheLocked(self, old_data); |
Alex Light | 07f0621 | 2017-06-01 14:01:43 -0700 | [diff] [blame] | 3947 | DCHECK(old_dex_cache.IsNull()) << "Attempt to manually register a dex cache thats already " |
| 3948 | << "been registered on dex file " << dex_file->GetLocation(); |
| 3949 | } |
| 3950 | ClassTable* table; |
| 3951 | { |
| 3952 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 3953 | table = InsertClassTableForClassLoader(h_class_loader.Get()); |
| 3954 | } |
Ivan Maidanski | 2b69b9c | 2018-05-14 13:50:48 +0300 | [diff] [blame] | 3955 | // Avoid a deadlock between a garbage collecting thread running a checkpoint, |
| 3956 | // a thread holding the dex lock and blocking on a condition variable regarding |
| 3957 | // weak references access, and a thread blocking on the dex lock. |
Ivan Maidanski | 2b69b9c | 2018-05-14 13:50:48 +0300 | [diff] [blame] | 3958 | gc::ScopedGCCriticalSection gcs(self, gc::kGcCauseClassLinker, gc::kCollectorTypeClassLinker); |
Alex Light | 07f0621 | 2017-06-01 14:01:43 -0700 | [diff] [blame] | 3959 | WriterMutexLock mu(self, *Locks::dex_lock_); |
| 3960 | RegisterDexFileLocked(*dex_file, h_dex_cache.Get(), h_class_loader.Get()); |
| 3961 | table->InsertStrongRoot(h_dex_cache.Get()); |
| 3962 | if (h_class_loader.Get() != nullptr) { |
| 3963 | // Since we added a strong root to the class table, do the write barrier as required for |
| 3964 | // remembered sets and generational GCs. |
Mathieu Chartier | 88ea61e | 2018-06-20 17:45:41 -0700 | [diff] [blame] | 3965 | WriteBarrier::ForEveryFieldWrite(h_class_loader.Get()); |
Alex Light | 07f0621 | 2017-06-01 14:01:43 -0700 | [diff] [blame] | 3966 | } |
| 3967 | } |
| 3968 | |
Alex Light | de7f878 | 2020-02-24 10:14:22 -0800 | [diff] [blame] | 3969 | static void ThrowDexFileAlreadyRegisteredError(Thread* self, const DexFile& dex_file) |
| 3970 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 3971 | self->ThrowNewExceptionF("Ljava/lang/InternalError;", |
Alex Light | de7f878 | 2020-02-24 10:14:22 -0800 | [diff] [blame] | 3972 | "Attempt to register dex file %s with multiple class loaders", |
| 3973 | dex_file.GetLocation().c_str()); |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 3974 | } |
| 3975 | |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3976 | ObjPtr<mirror::DexCache> ClassLinker::RegisterDexFile(const DexFile& dex_file, |
| 3977 | ObjPtr<mirror::ClassLoader> class_loader) { |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 3978 | Thread* self = Thread::Current(); |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 3979 | ObjPtr<mirror::DexCache> old_dex_cache; |
| 3980 | bool registered_with_another_class_loader = false; |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 3981 | { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 3982 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 3983 | const DexCacheData* old_data = FindDexCacheDataLocked(dex_file); |
| 3984 | old_dex_cache = DecodeDexCacheLocked(self, old_data); |
| 3985 | if (old_dex_cache != nullptr) { |
| 3986 | if (IsSameClassLoader(old_dex_cache, old_data, class_loader)) { |
| 3987 | return old_dex_cache; |
| 3988 | } else { |
| 3989 | // TODO This is not very clean looking. Should maybe try to make a way to request exceptions |
| 3990 | // be thrown when it's safe to do so to simplify this. |
| 3991 | registered_with_another_class_loader = true; |
| 3992 | } |
| 3993 | } |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3994 | } |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 3995 | // We need to have released the dex_lock_ to allocate safely. |
| 3996 | if (registered_with_another_class_loader) { |
| 3997 | ThrowDexFileAlreadyRegisteredError(self, dex_file); |
| 3998 | return nullptr; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3999 | } |
Mathieu Chartier | ed4ee44 | 2018-06-05 14:23:35 -0700 | [diff] [blame] | 4000 | SCOPED_TRACE << __FUNCTION__ << " " << dex_file.GetLocation(); |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 4001 | LinearAlloc* const linear_alloc = GetOrCreateAllocatorForClassLoader(class_loader); |
| 4002 | DCHECK(linear_alloc != nullptr); |
| 4003 | ClassTable* table; |
| 4004 | { |
| 4005 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 4006 | table = InsertClassTableForClassLoader(class_loader); |
| 4007 | } |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 4008 | // Don't alloc while holding the lock, since allocation may need to |
| 4009 | // suspend all threads and another thread may need the dex_lock_ to |
| 4010 | // get to a suspend point. |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4011 | StackHandleScope<3> hs(self); |
| 4012 | Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader)); |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 4013 | Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(AllocDexCache(self, dex_file))); |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 4014 | { |
Ivan Maidanski | 2b69b9c | 2018-05-14 13:50:48 +0300 | [diff] [blame] | 4015 | // Avoid a deadlock between a garbage collecting thread running a checkpoint, |
| 4016 | // a thread holding the dex lock and blocking on a condition variable regarding |
| 4017 | // weak references access, and a thread blocking on the dex lock. |
Ivan Maidanski | 2b69b9c | 2018-05-14 13:50:48 +0300 | [diff] [blame] | 4018 | gc::ScopedGCCriticalSection gcs(self, gc::kGcCauseClassLinker, gc::kCollectorTypeClassLinker); |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 4019 | WriterMutexLock mu(self, *Locks::dex_lock_); |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 4020 | const DexCacheData* old_data = FindDexCacheDataLocked(dex_file); |
| 4021 | old_dex_cache = DecodeDexCacheLocked(self, old_data); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4022 | if (old_dex_cache == nullptr && h_dex_cache != nullptr) { |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 4023 | // Do InitializeNativeFields while holding dex lock to make sure two threads don't call it |
| 4024 | // at the same time with the same dex cache. Since the .bss is shared this can cause failing |
| 4025 | // DCHECK that the arrays are null. |
| 4026 | h_dex_cache->InitializeNativeFields(&dex_file, linear_alloc); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4027 | RegisterDexFileLocked(dex_file, h_dex_cache.Get(), h_class_loader.Get()); |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 4028 | } |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 4029 | if (old_dex_cache != nullptr) { |
| 4030 | // Another thread managed to initialize the dex cache faster, so use that DexCache. |
| 4031 | // If this thread encountered OOME, ignore it. |
| 4032 | DCHECK_EQ(h_dex_cache == nullptr, self->IsExceptionPending()); |
| 4033 | self->ClearException(); |
| 4034 | // We cannot call EnsureSameClassLoader() or allocate an exception while holding the |
| 4035 | // dex_lock_. |
| 4036 | if (IsSameClassLoader(old_dex_cache, old_data, h_class_loader.Get())) { |
| 4037 | return old_dex_cache; |
| 4038 | } else { |
| 4039 | registered_with_another_class_loader = true; |
| 4040 | } |
| 4041 | } |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4042 | } |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 4043 | if (registered_with_another_class_loader) { |
| 4044 | ThrowDexFileAlreadyRegisteredError(self, dex_file); |
| 4045 | return nullptr; |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4046 | } |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4047 | if (h_dex_cache == nullptr) { |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4048 | self->AssertPendingOOMException(); |
| 4049 | return nullptr; |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 4050 | } |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 4051 | table->InsertStrongRoot(h_dex_cache.Get()); |
Mathieu Chartier | a1467d0 | 2017-02-22 09:22:50 -0800 | [diff] [blame] | 4052 | if (h_class_loader.Get() != nullptr) { |
| 4053 | // Since we added a strong root to the class table, do the write barrier as required for |
| 4054 | // remembered sets and generational GCs. |
Mathieu Chartier | 88ea61e | 2018-06-20 17:45:41 -0700 | [diff] [blame] | 4055 | WriteBarrier::ForEveryFieldWrite(h_class_loader.Get()); |
Mathieu Chartier | a1467d0 | 2017-02-22 09:22:50 -0800 | [diff] [blame] | 4056 | } |
Nicolas Geoffray | 1d4f009 | 2020-08-07 14:01:05 +0100 | [diff] [blame] | 4057 | PaletteHooks* hooks = nullptr; |
| 4058 | VLOG(class_linker) << "Registered dex file " << dex_file.GetLocation(); |
| 4059 | if (PaletteGetHooks(&hooks) == PaletteStatus::kOkay) { |
| 4060 | hooks->NotifyDexFileLoaded(dex_file.GetLocation().c_str()); |
| 4061 | } |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 4062 | return h_dex_cache.Get(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 4063 | } |
| 4064 | |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4065 | bool ClassLinker::IsDexFileRegistered(Thread* self, const DexFile& dex_file) { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 4066 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 4067 | return DecodeDexCacheLocked(self, FindDexCacheDataLocked(dex_file)) != nullptr; |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 4068 | } |
| 4069 | |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4070 | ObjPtr<mirror::DexCache> ClassLinker::FindDexCache(Thread* self, const DexFile& dex_file) { |
| 4071 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 4072 | const DexCacheData* dex_cache_data = FindDexCacheDataLocked(dex_file); |
| 4073 | ObjPtr<mirror::DexCache> dex_cache = DecodeDexCacheLocked(self, dex_cache_data); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4074 | if (dex_cache != nullptr) { |
| 4075 | return dex_cache; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 4076 | } |
Brian Carlstrom | 81a9087 | 2015-08-28 09:07:14 -0700 | [diff] [blame] | 4077 | // Failure, dump diagnostic and abort. |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 4078 | for (const DexCacheData& data : dex_caches_) { |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 4079 | if (DecodeDexCacheLocked(self, &data) != nullptr) { |
Andreas Gampe | 37c5846 | 2017-03-27 15:14:27 -0700 | [diff] [blame] | 4080 | LOG(FATAL_WITHOUT_ABORT) << "Registered dex file " << data.dex_file->GetLocation(); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 4081 | } |
Brian Carlstrom | 81a9087 | 2015-08-28 09:07:14 -0700 | [diff] [blame] | 4082 | } |
Mathieu Chartier | 9e050df | 2017-08-09 10:05:47 -0700 | [diff] [blame] | 4083 | LOG(FATAL) << "Failed to find DexCache for DexFile " << dex_file.GetLocation() |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 4084 | << " " << &dex_file << " " << dex_cache_data->dex_file; |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 4085 | UNREACHABLE(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4086 | } |
| 4087 | |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4088 | ClassTable* ClassLinker::FindClassTable(Thread* self, ObjPtr<mirror::DexCache> dex_cache) { |
| 4089 | const DexFile* dex_file = dex_cache->GetDexFile(); |
| 4090 | DCHECK(dex_file != nullptr); |
| 4091 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
| 4092 | // Search assuming unique-ness of dex file. |
| 4093 | for (const DexCacheData& data : dex_caches_) { |
| 4094 | // Avoid decoding (and read barriers) other unrelated dex caches. |
| 4095 | if (data.dex_file == dex_file) { |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 4096 | ObjPtr<mirror::DexCache> registered_dex_cache = DecodeDexCacheLocked(self, &data); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4097 | if (registered_dex_cache != nullptr) { |
| 4098 | CHECK_EQ(registered_dex_cache, dex_cache) << dex_file->GetLocation(); |
| 4099 | return data.class_table; |
| 4100 | } |
| 4101 | } |
| 4102 | } |
| 4103 | return nullptr; |
| 4104 | } |
| 4105 | |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 4106 | const ClassLinker::DexCacheData* ClassLinker::FindDexCacheDataLocked(const DexFile& dex_file) { |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4107 | // Search assuming unique-ness of dex file. |
| 4108 | for (const DexCacheData& data : dex_caches_) { |
| 4109 | // Avoid decoding (and read barriers) other unrelated dex caches. |
| 4110 | if (data.dex_file == &dex_file) { |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 4111 | return &data; |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4112 | } |
| 4113 | } |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 4114 | return nullptr; |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4115 | } |
| 4116 | |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 4117 | void ClassLinker::CreatePrimitiveClass(Thread* self, |
| 4118 | Primitive::Type type, |
| 4119 | ClassRoot primitive_root) { |
Vladimir Marko | acb906d | 2018-05-30 10:23:49 +0100 | [diff] [blame] | 4120 | ObjPtr<mirror::Class> primitive_class = |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 4121 | AllocClass(self, mirror::Class::PrimitiveClassSize(image_pointer_size_)); |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 4122 | CHECK(primitive_class != nullptr) << "OOM for primitive class " << type; |
| 4123 | // Do not hold lock on the primitive class object, the initialization of |
| 4124 | // primitive classes is done while the process is still single threaded. |
Vladimir Marko | b68bb7a | 2020-03-17 10:55:25 +0000 | [diff] [blame] | 4125 | primitive_class->SetAccessFlagsDuringLinking( |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 4126 | kAccPublic | kAccFinal | kAccAbstract | kAccVerificationAttempted); |
| 4127 | primitive_class->SetPrimitiveType(type); |
| 4128 | primitive_class->SetIfTable(GetClassRoot<mirror::Object>(this)->GetIfTable()); |
| 4129 | // Skip EnsureSkipAccessChecksMethods(). We can skip the verified status, |
| 4130 | // the kAccVerificationAttempted flag was added above, and there are no |
| 4131 | // methods that need the kAccSkipAccessChecks flag. |
| 4132 | DCHECK_EQ(primitive_class->NumMethods(), 0u); |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 4133 | // Primitive classes are initialized during single threaded startup, so visibly initialized. |
| 4134 | primitive_class->SetStatusForPrimitiveOrArray(ClassStatus::kVisiblyInitialized); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4135 | const char* descriptor = Primitive::Descriptor(type); |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 4136 | ObjPtr<mirror::Class> existing = InsertClass(descriptor, |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 4137 | primitive_class, |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 4138 | ComputeModifiedUtf8Hash(descriptor)); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4139 | CHECK(existing == nullptr) << "InitPrimitiveClass(" << type << ") failed"; |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 4140 | SetClassRoot(primitive_root, primitive_class); |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 4141 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4142 | |
Vladimir Marko | 0261055 | 2018-06-04 14:38:00 +0100 | [diff] [blame] | 4143 | inline ObjPtr<mirror::IfTable> ClassLinker::GetArrayIfTable() { |
| 4144 | return GetClassRoot<mirror::ObjectArray<mirror::Object>>(this)->GetIfTable(); |
| 4145 | } |
| 4146 | |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 4147 | // Create an array class (i.e. the class object for the array, not the |
| 4148 | // array itself). "descriptor" looks like "[C" or "[[[[B" or |
| 4149 | // "[Ljava/lang/String;". |
| 4150 | // |
| 4151 | // If "descriptor" refers to an array of primitives, look up the |
| 4152 | // primitive type's internally-generated class object. |
| 4153 | // |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 4154 | // "class_loader" is the class loader of the class that's referring to |
| 4155 | // us. It's used to ensure that we're looking for the element type in |
| 4156 | // the right context. It does NOT become the class loader for the |
| 4157 | // array class; that always comes from the base element class. |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 4158 | // |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 4159 | // Returns null with an exception raised on failure. |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 4160 | ObjPtr<mirror::Class> ClassLinker::CreateArrayClass(Thread* self, |
| 4161 | const char* descriptor, |
| 4162 | size_t hash, |
| 4163 | Handle<mirror::ClassLoader> class_loader) { |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 4164 | // Identify the underlying component type |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4165 | CHECK_EQ('[', descriptor[0]); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4166 | StackHandleScope<2> hs(self); |
Alex Light | e9f6103 | 2018-09-24 16:04:51 -0700 | [diff] [blame] | 4167 | |
| 4168 | // This is to prevent the calls to ClassLoad and ClassPrepare which can cause java/user-supplied |
| 4169 | // code to be executed. We put it up here so we can avoid all the allocations associated with |
| 4170 | // creating the class. This can happen with (eg) jit threads. |
| 4171 | if (!self->CanLoadClasses()) { |
| 4172 | // Make sure we don't try to load anything, potentially causing an infinite loop. |
| 4173 | ObjPtr<mirror::Throwable> pre_allocated = |
| 4174 | Runtime::Current()->GetPreAllocatedNoClassDefFoundError(); |
| 4175 | self->SetException(pre_allocated); |
| 4176 | return nullptr; |
| 4177 | } |
| 4178 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4179 | MutableHandle<mirror::Class> component_type(hs.NewHandle(FindClass(self, descriptor + 1, |
| 4180 | class_loader))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4181 | if (component_type == nullptr) { |
Mathieu Chartier | c0a9ea4 | 2014-02-03 16:36:49 -0800 | [diff] [blame] | 4182 | DCHECK(self->IsExceptionPending()); |
Andreas Gampe | dc13d7d | 2014-07-23 20:18:36 -0700 | [diff] [blame] | 4183 | // We need to accept erroneous classes as component types. |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 4184 | const size_t component_hash = ComputeModifiedUtf8Hash(descriptor + 1); |
| 4185 | component_type.Assign(LookupClass(self, descriptor + 1, component_hash, class_loader.Get())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4186 | if (component_type == nullptr) { |
Andreas Gampe | dc13d7d | 2014-07-23 20:18:36 -0700 | [diff] [blame] | 4187 | DCHECK(self->IsExceptionPending()); |
| 4188 | return nullptr; |
| 4189 | } else { |
| 4190 | self->ClearException(); |
| 4191 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 4192 | } |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 4193 | if (UNLIKELY(component_type->IsPrimitiveVoid())) { |
| 4194 | ThrowNoClassDefFoundError("Attempt to create array of void primitive type"); |
| 4195 | return nullptr; |
| 4196 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 4197 | // See if the component type is already loaded. Array classes are |
| 4198 | // always associated with the class loader of their underlying |
| 4199 | // element type -- an array of Strings goes with the loader for |
| 4200 | // java/lang/String -- so we need to look for it there. (The |
| 4201 | // caller should have checked for the existence of the class |
| 4202 | // before calling here, but they did so with *their* class loader, |
| 4203 | // not the component type's loader.) |
| 4204 | // |
| 4205 | // If we find it, the caller adds "loader" to the class' initiating |
| 4206 | // loader list, which should prevent us from going through this again. |
| 4207 | // |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4208 | // This call is unnecessary if "loader" and "component_type->GetClassLoader()" |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 4209 | // are the same, because our caller (FindClass) just did the |
| 4210 | // lookup. (Even if we get this wrong we still have correct behavior, |
| 4211 | // because we effectively do this lookup again when we add the new |
| 4212 | // class to the hash table --- necessary because of possible races with |
| 4213 | // other threads.) |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4214 | if (class_loader.Get() != component_type->GetClassLoader()) { |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 4215 | ObjPtr<mirror::Class> new_class = |
| 4216 | LookupClass(self, descriptor, hash, component_type->GetClassLoader()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4217 | if (new_class != nullptr) { |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 4218 | return new_class; |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 4219 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 4220 | } |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 4221 | // Core array classes, i.e. Object[], Class[], String[] and primitive |
| 4222 | // arrays, have special initialization and they should be found above. |
| 4223 | DCHECK(!component_type->IsObjectClass() || |
| 4224 | // Guard from false positives for errors before setting superclass. |
| 4225 | component_type->IsErroneousUnresolved()); |
| 4226 | DCHECK(!component_type->IsStringClass()); |
| 4227 | DCHECK(!component_type->IsClassClass()); |
| 4228 | DCHECK(!component_type->IsPrimitive()); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 4229 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 4230 | // Fill out the fields in the Class. |
| 4231 | // |
| 4232 | // It is possible to execute some methods against arrays, because |
| 4233 | // all arrays are subclasses of java_lang_Object_, so we need to set |
| 4234 | // up a vtable. We can just point at the one in java_lang_Object_. |
| 4235 | // |
| 4236 | // Array classes are simple enough that we don't need to do a full |
| 4237 | // link step. |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 4238 | size_t array_class_size = mirror::Array::ClassSize(image_pointer_size_); |
| 4239 | auto visitor = [this, array_class_size, component_type](ObjPtr<mirror::Object> obj, |
| 4240 | size_t usable_size) |
| 4241 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | b68bb7a | 2020-03-17 10:55:25 +0000 | [diff] [blame] | 4242 | ScopedAssertNoNewTransactionRecords sanntr("CreateArrayClass"); |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 4243 | mirror::Class::InitializeClassVisitor init_class(array_class_size); |
| 4244 | init_class(obj, usable_size); |
| 4245 | ObjPtr<mirror::Class> klass = ObjPtr<mirror::Class>::DownCast(obj); |
| 4246 | klass->SetComponentType(component_type.Get()); |
| 4247 | // Do not hold lock for initialization, the fence issued after the visitor |
| 4248 | // returns ensures memory visibility together with the implicit consume |
| 4249 | // semantics (for all supported architectures) for any thread that loads |
| 4250 | // the array class reference from any memory locations afterwards. |
| 4251 | FinishArrayClassSetup(klass); |
| 4252 | }; |
| 4253 | auto new_class = hs.NewHandle<mirror::Class>( |
| 4254 | AllocClass(self, GetClassRoot<mirror::Class>(this), array_class_size, visitor)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4255 | if (new_class == nullptr) { |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 4256 | self->AssertPendingOOMException(); |
| 4257 | return nullptr; |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 4258 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 4259 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4260 | ObjPtr<mirror::Class> existing = InsertClass(descriptor, new_class.Get(), hash); |
Mathieu Chartier | c0a9ea4 | 2014-02-03 16:36:49 -0800 | [diff] [blame] | 4261 | if (existing == nullptr) { |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4262 | // We postpone ClassLoad and ClassPrepare events to this point in time to avoid |
| 4263 | // duplicate events in case of races. Array classes don't really follow dedicated |
| 4264 | // load and prepare, anyways. |
| 4265 | Runtime::Current()->GetRuntimeCallbacks()->ClassLoad(new_class); |
| 4266 | Runtime::Current()->GetRuntimeCallbacks()->ClassPrepare(new_class, new_class); |
| 4267 | |
Tamas Berghammer | 160e6df | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 4268 | jit::Jit::NewTypeLoadedIfUsingJit(new_class.Get()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4269 | return new_class.Get(); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 4270 | } |
| 4271 | // Another thread must have loaded the class after we |
| 4272 | // started but before we finished. Abandon what we've |
| 4273 | // done. |
| 4274 | // |
| 4275 | // (Yes, this happens.) |
| 4276 | |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 4277 | return existing; |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 4278 | } |
| 4279 | |
Vladimir Marko | 9186b18 | 2018-11-06 14:55:54 +0000 | [diff] [blame] | 4280 | ObjPtr<mirror::Class> ClassLinker::LookupPrimitiveClass(char type) { |
| 4281 | ClassRoot class_root; |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 4282 | switch (type) { |
Vladimir Marko | 9186b18 | 2018-11-06 14:55:54 +0000 | [diff] [blame] | 4283 | case 'B': class_root = ClassRoot::kPrimitiveByte; break; |
| 4284 | case 'C': class_root = ClassRoot::kPrimitiveChar; break; |
| 4285 | case 'D': class_root = ClassRoot::kPrimitiveDouble; break; |
| 4286 | case 'F': class_root = ClassRoot::kPrimitiveFloat; break; |
| 4287 | case 'I': class_root = ClassRoot::kPrimitiveInt; break; |
| 4288 | case 'J': class_root = ClassRoot::kPrimitiveLong; break; |
| 4289 | case 'S': class_root = ClassRoot::kPrimitiveShort; break; |
| 4290 | case 'Z': class_root = ClassRoot::kPrimitiveBoolean; break; |
| 4291 | case 'V': class_root = ClassRoot::kPrimitiveVoid; break; |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 4292 | default: |
Vladimir Marko | 9186b18 | 2018-11-06 14:55:54 +0000 | [diff] [blame] | 4293 | return nullptr; |
Carl Shapiro | 744ad05 | 2011-08-06 15:53:36 -0700 | [diff] [blame] | 4294 | } |
Vladimir Marko | 9186b18 | 2018-11-06 14:55:54 +0000 | [diff] [blame] | 4295 | return GetClassRoot(class_root, this); |
| 4296 | } |
| 4297 | |
| 4298 | ObjPtr<mirror::Class> ClassLinker::FindPrimitiveClass(char type) { |
| 4299 | ObjPtr<mirror::Class> result = LookupPrimitiveClass(type); |
| 4300 | if (UNLIKELY(result == nullptr)) { |
| 4301 | std::string printable_type(PrintableChar(type)); |
| 4302 | ThrowNoClassDefFoundError("Not a primitive type: %s", printable_type.c_str()); |
| 4303 | } |
| 4304 | return result; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4305 | } |
| 4306 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 4307 | ObjPtr<mirror::Class> ClassLinker::InsertClass(const char* descriptor, |
| 4308 | ObjPtr<mirror::Class> klass, |
| 4309 | size_t hash) { |
Alex Light | e9f6103 | 2018-09-24 16:04:51 -0700 | [diff] [blame] | 4310 | DCHECK(Thread::Current()->CanLoadClasses()); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 4311 | if (VLOG_IS_ON(class_linker)) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4312 | ObjPtr<mirror::DexCache> dex_cache = klass->GetDexCache(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 4313 | std::string source; |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4314 | if (dex_cache != nullptr) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 4315 | source += " from "; |
| 4316 | source += dex_cache->GetLocation()->ToModifiedUtf8(); |
| 4317 | } |
| 4318 | LOG(INFO) << "Loaded class " << descriptor << source; |
| 4319 | } |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 4320 | { |
| 4321 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Vladimir Marko | 0984e48 | 2019-03-27 16:41:41 +0000 | [diff] [blame] | 4322 | const ObjPtr<mirror::ClassLoader> class_loader = klass->GetClassLoader(); |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 4323 | ClassTable* const class_table = InsertClassTableForClassLoader(class_loader); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4324 | ObjPtr<mirror::Class> existing = class_table->Lookup(descriptor, hash); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4325 | if (existing != nullptr) { |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 4326 | return existing; |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 4327 | } |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 4328 | VerifyObject(klass); |
| 4329 | class_table->InsertWithHash(klass, hash); |
| 4330 | if (class_loader != nullptr) { |
| 4331 | // 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] | 4332 | WriteBarrier::ForEveryFieldWrite(class_loader); |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 4333 | } |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 4334 | if (log_new_roots_) { |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 4335 | new_class_roots_.push_back(GcRoot<mirror::Class>(klass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4336 | } |
| 4337 | } |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 4338 | if (kIsDebugBuild) { |
| 4339 | // Test that copied methods correctly can find their holder. |
| 4340 | for (ArtMethod& method : klass->GetCopiedMethods(image_pointer_size_)) { |
| 4341 | CHECK_EQ(GetHoldingClassOfCopiedMethod(&method), klass); |
| 4342 | } |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 4343 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4344 | return nullptr; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4345 | } |
| 4346 | |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 4347 | void ClassLinker::WriteBarrierForBootOatFileBssRoots(const OatFile* oat_file) { |
Mathieu Chartier | a1467d0 | 2017-02-22 09:22:50 -0800 | [diff] [blame] | 4348 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
| 4349 | DCHECK(!oat_file->GetBssGcRoots().empty()) << oat_file->GetLocation(); |
| 4350 | if (log_new_roots_ && !ContainsElement(new_bss_roots_boot_oat_files_, oat_file)) { |
| 4351 | new_bss_roots_boot_oat_files_.push_back(oat_file); |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 4352 | } |
| 4353 | } |
| 4354 | |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4355 | // TODO This should really be in mirror::Class. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4356 | void ClassLinker::UpdateClassMethods(ObjPtr<mirror::Class> klass, |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4357 | LengthPrefixedArray<ArtMethod>* new_methods) { |
| 4358 | klass->SetMethodsPtrUnchecked(new_methods, |
| 4359 | klass->NumDirectMethods(), |
| 4360 | klass->NumDeclaredVirtualMethods()); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4361 | // 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] | 4362 | WriteBarrier::ForEveryFieldWrite(klass); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4363 | } |
| 4364 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 4365 | ObjPtr<mirror::Class> ClassLinker::LookupClass(Thread* self, |
| 4366 | const char* descriptor, |
| 4367 | ObjPtr<mirror::ClassLoader> class_loader) { |
Andreas Gampe | 2ff3b97 | 2017-06-05 18:14:53 -0700 | [diff] [blame] | 4368 | return LookupClass(self, descriptor, ComputeModifiedUtf8Hash(descriptor), class_loader); |
| 4369 | } |
| 4370 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 4371 | ObjPtr<mirror::Class> ClassLinker::LookupClass(Thread* self, |
| 4372 | const char* descriptor, |
| 4373 | size_t hash, |
| 4374 | ObjPtr<mirror::ClassLoader> class_loader) { |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 4375 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 4376 | ClassTable* const class_table = ClassTableForClassLoader(class_loader); |
| 4377 | if (class_table != nullptr) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4378 | ObjPtr<mirror::Class> result = class_table->Lookup(descriptor, hash); |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 4379 | if (result != nullptr) { |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 4380 | return result; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4381 | } |
Sameer Abu Asal | 2c6de22 | 2013-05-02 17:38:59 -0700 | [diff] [blame] | 4382 | } |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 4383 | return nullptr; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4384 | } |
| 4385 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4386 | class MoveClassTableToPreZygoteVisitor : public ClassLoaderVisitor { |
| 4387 | public: |
Igor Murashkin | 2ffb703 | 2017-11-08 13:35:21 -0800 | [diff] [blame] | 4388 | MoveClassTableToPreZygoteVisitor() {} |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4389 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4390 | void Visit(ObjPtr<mirror::ClassLoader> class_loader) |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4391 | REQUIRES(Locks::classlinker_classes_lock_) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 4392 | REQUIRES_SHARED(Locks::mutator_lock_) override { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4393 | ClassTable* const class_table = class_loader->GetClassTable(); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 4394 | if (class_table != nullptr) { |
| 4395 | class_table->FreezeSnapshot(); |
| 4396 | } |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 4397 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4398 | }; |
| 4399 | |
| 4400 | void ClassLinker::MoveClassTableToPreZygote() { |
| 4401 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 4402 | boot_class_table_->FreezeSnapshot(); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4403 | MoveClassTableToPreZygoteVisitor visitor; |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 4404 | VisitClassLoaders(&visitor); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 4405 | } |
| 4406 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4407 | // Look up classes by hash and descriptor and put all matching ones in the result array. |
| 4408 | class LookupClassesVisitor : public ClassLoaderVisitor { |
| 4409 | public: |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4410 | LookupClassesVisitor(const char* descriptor, |
| 4411 | size_t hash, |
| 4412 | std::vector<ObjPtr<mirror::Class>>* result) |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4413 | : descriptor_(descriptor), |
| 4414 | hash_(hash), |
| 4415 | result_(result) {} |
| 4416 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4417 | void Visit(ObjPtr<mirror::ClassLoader> class_loader) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 4418 | REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_) override { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4419 | ClassTable* const class_table = class_loader->GetClassTable(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4420 | ObjPtr<mirror::Class> klass = class_table->Lookup(descriptor_, hash_); |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 4421 | // Add `klass` only if `class_loader` is its defining (not just initiating) class loader. |
| 4422 | if (klass != nullptr && klass->GetClassLoader() == class_loader) { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4423 | result_->push_back(klass); |
| 4424 | } |
| 4425 | } |
| 4426 | |
| 4427 | private: |
| 4428 | const char* const descriptor_; |
| 4429 | const size_t hash_; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4430 | std::vector<ObjPtr<mirror::Class>>* const result_; |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4431 | }; |
| 4432 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4433 | void ClassLinker::LookupClasses(const char* descriptor, |
| 4434 | std::vector<ObjPtr<mirror::Class>>& result) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4435 | result.clear(); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4436 | Thread* const self = Thread::Current(); |
| 4437 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 4438 | const size_t hash = ComputeModifiedUtf8Hash(descriptor); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 4439 | ObjPtr<mirror::Class> klass = boot_class_table_->Lookup(descriptor, hash); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 4440 | if (klass != nullptr) { |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 4441 | DCHECK(klass->GetClassLoader() == nullptr); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 4442 | result.push_back(klass); |
| 4443 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4444 | LookupClassesVisitor visitor(descriptor, hash, &result); |
| 4445 | VisitClassLoaders(&visitor); |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 4446 | } |
| 4447 | |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4448 | bool ClassLinker::AttemptSupertypeVerification(Thread* self, |
| 4449 | Handle<mirror::Class> klass, |
| 4450 | Handle<mirror::Class> supertype) { |
| 4451 | DCHECK(self != nullptr); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4452 | DCHECK(klass != nullptr); |
| 4453 | DCHECK(supertype != nullptr); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4454 | |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4455 | if (!supertype->IsVerified() && !supertype->IsErroneous()) { |
| 4456 | VerifyClass(self, supertype); |
| 4457 | } |
Nicolas Geoffray | 7cc3ae5 | 2017-03-07 14:33:37 +0000 | [diff] [blame] | 4458 | |
Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 4459 | if (supertype->IsVerified() |
| 4460 | || supertype->ShouldVerifyAtRuntime() |
| 4461 | || supertype->IsVerifiedNeedsAccessChecks()) { |
Nicolas Geoffray | 7cc3ae5 | 2017-03-07 14:33:37 +0000 | [diff] [blame] | 4462 | // The supertype is either verified, or we soft failed at AOT time. |
| 4463 | DCHECK(supertype->IsVerified() || Runtime::Current()->IsAotCompiler()); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4464 | return true; |
| 4465 | } |
| 4466 | // If we got this far then we have a hard failure. |
| 4467 | std::string error_msg = |
| 4468 | StringPrintf("Rejecting class %s that attempts to sub-type erroneous class %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4469 | klass->PrettyDescriptor().c_str(), |
| 4470 | supertype->PrettyDescriptor().c_str()); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4471 | LOG(WARNING) << error_msg << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8(); |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4472 | StackHandleScope<1> hs(self); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4473 | Handle<mirror::Throwable> cause(hs.NewHandle(self->GetException())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4474 | if (cause != nullptr) { |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4475 | // Set during VerifyClass call (if at all). |
| 4476 | self->ClearException(); |
| 4477 | } |
| 4478 | // Change into a verify error. |
| 4479 | ThrowVerifyError(klass.Get(), "%s", error_msg.c_str()); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4480 | if (cause != nullptr) { |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4481 | self->GetException()->SetCause(cause.Get()); |
| 4482 | } |
| 4483 | ClassReference ref(klass->GetDexCache()->GetDexFile(), klass->GetDexClassDefIndex()); |
| 4484 | if (Runtime::Current()->IsAotCompiler()) { |
| 4485 | Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref); |
| 4486 | } |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4487 | // Need to grab the lock to change status. |
| 4488 | ObjectLock<mirror::Class> super_lock(self, klass); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4489 | mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4490 | return false; |
| 4491 | } |
| 4492 | |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4493 | verifier::FailureKind ClassLinker::VerifyClass( |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame] | 4494 | Thread* self, Handle<mirror::Class> klass, verifier::HardFailLogMode log_level) { |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4495 | { |
| 4496 | // TODO: assert that the monitor on the Class is held |
| 4497 | ObjectLock<mirror::Class> lock(self, klass); |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 4498 | |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4499 | // Is somebody verifying this now? |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4500 | ClassStatus old_status = klass->GetStatus(); |
Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 4501 | while (old_status == ClassStatus::kVerifying) { |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4502 | lock.WaitIgnoringInterrupts(); |
Mathieu Chartier | 5ef7020 | 2017-06-29 10:45:10 -0700 | [diff] [blame] | 4503 | // WaitIgnoringInterrupts can still receive an interrupt and return early, in this |
| 4504 | // case we may see the same status again. b/62912904. This is why the check is |
| 4505 | // greater or equal. |
| 4506 | CHECK(klass->IsErroneous() || (klass->GetStatus() >= old_status)) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4507 | << "Class '" << klass->PrettyClass() |
| 4508 | << "' performed an illegal verification state transition from " << old_status |
| 4509 | << " to " << klass->GetStatus(); |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4510 | old_status = klass->GetStatus(); |
| 4511 | } |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 4512 | |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4513 | // The class might already be erroneous, for example at compile time if we attempted to verify |
| 4514 | // this class as a parent to another. |
| 4515 | if (klass->IsErroneous()) { |
| 4516 | ThrowEarlierClassFailure(klass.Get()); |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4517 | return verifier::FailureKind::kHardFailure; |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4518 | } |
Brian Carlstrom | 9b5ee88 | 2012-02-28 09:48:54 -0800 | [diff] [blame] | 4519 | |
Nicolas Geoffray | 7cc3ae5 | 2017-03-07 14:33:37 +0000 | [diff] [blame] | 4520 | // Don't attempt to re-verify if already verified. |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4521 | if (klass->IsVerified()) { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 4522 | EnsureSkipAccessChecksMethods(klass, image_pointer_size_); |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4523 | return verifier::FailureKind::kNoFailure; |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4524 | } |
Nicolas Geoffray | 7cc3ae5 | 2017-03-07 14:33:37 +0000 | [diff] [blame] | 4525 | |
Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 4526 | if (klass->IsVerifiedNeedsAccessChecks()) { |
| 4527 | if (!Runtime::Current()->IsAotCompiler()) { |
| 4528 | // Mark the class as having a verification attempt to avoid re-running |
| 4529 | // the verifier and avoid calling EnsureSkipAccessChecksMethods. |
| 4530 | klass->SetVerificationAttempted(); |
| 4531 | mirror::Class::SetStatus(klass, ClassStatus::kVerified, self); |
| 4532 | } |
| 4533 | return verifier::FailureKind::kAccessChecksFailure; |
| 4534 | } |
| 4535 | |
Nicolas Geoffray | 7cc3ae5 | 2017-03-07 14:33:37 +0000 | [diff] [blame] | 4536 | // For AOT, don't attempt to re-verify if we have already found we should |
| 4537 | // verify at runtime. |
Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 4538 | if (klass->ShouldVerifyAtRuntime()) { |
| 4539 | CHECK(Runtime::Current()->IsAotCompiler()); |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4540 | return verifier::FailureKind::kSoftFailure; |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4541 | } |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 4542 | |
Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 4543 | DCHECK_EQ(klass->GetStatus(), ClassStatus::kResolved); |
| 4544 | mirror::Class::SetStatus(klass, ClassStatus::kVerifying, self); |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4545 | |
| 4546 | // Skip verification if disabled. |
| 4547 | if (!Runtime::Current()->IsVerificationEnabled()) { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4548 | mirror::Class::SetStatus(klass, ClassStatus::kVerified, self); |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 4549 | EnsureSkipAccessChecksMethods(klass, image_pointer_size_); |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4550 | return verifier::FailureKind::kNoFailure; |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4551 | } |
Jeff Hao | 4a200f5 | 2014-04-01 14:58:49 -0700 | [diff] [blame] | 4552 | } |
| 4553 | |
Bharadwaj Kalandhabhatta | 271c1e1 | 2017-06-27 11:14:49 -0700 | [diff] [blame] | 4554 | VLOG(class_linker) << "Beginning verification for class: " |
| 4555 | << klass->PrettyDescriptor() |
| 4556 | << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8(); |
| 4557 | |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 4558 | // Verify super class. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4559 | StackHandleScope<2> hs(self); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4560 | MutableHandle<mirror::Class> supertype(hs.NewHandle(klass->GetSuperClass())); |
| 4561 | // 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] | 4562 | if (supertype != nullptr && !AttemptSupertypeVerification(self, klass, supertype)) { |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4563 | CHECK(self->IsExceptionPending()) << "Verification error should be pending."; |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4564 | return verifier::FailureKind::kHardFailure; |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4565 | } |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 4566 | |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4567 | // Verify all default super-interfaces. |
| 4568 | // |
| 4569 | // (1) Don't bother if the superclass has already had a soft verification failure. |
| 4570 | // |
| 4571 | // (2) Interfaces shouldn't bother to do this recursive verification because they cannot cause |
| 4572 | // recursive initialization by themselves. This is because when an interface is initialized |
| 4573 | // directly it must not initialize its superinterfaces. We are allowed to verify regardless |
| 4574 | // but choose not to for an optimization. If the interfaces is being verified due to a class |
| 4575 | // initialization (which would need all the default interfaces to be verified) the class code |
| 4576 | // will trigger the recursive verification anyway. |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4577 | if ((supertype == nullptr || supertype->IsVerified()) // See (1) |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4578 | && !klass->IsInterface()) { // See (2) |
| 4579 | int32_t iftable_count = klass->GetIfTableCount(); |
| 4580 | MutableHandle<mirror::Class> iface(hs.NewHandle<mirror::Class>(nullptr)); |
| 4581 | // Loop through all interfaces this class has defined. It doesn't matter the order. |
| 4582 | for (int32_t i = 0; i < iftable_count; i++) { |
| 4583 | iface.Assign(klass->GetIfTable()->GetInterface(i)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4584 | DCHECK(iface != nullptr); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4585 | // We only care if we have default interfaces and can skip if we are already verified... |
| 4586 | if (LIKELY(!iface->HasDefaultMethods() || iface->IsVerified())) { |
| 4587 | continue; |
| 4588 | } else if (UNLIKELY(!AttemptSupertypeVerification(self, klass, iface))) { |
| 4589 | // We had a hard failure while verifying this interface. Just return immediately. |
| 4590 | CHECK(self->IsExceptionPending()) << "Verification error should be pending."; |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4591 | return verifier::FailureKind::kHardFailure; |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4592 | } else if (UNLIKELY(!iface->IsVerified())) { |
| 4593 | // We softly failed to verify the iface. Stop checking and clean up. |
| 4594 | // Put the iface into the supertype handle so we know what caused us to fail. |
| 4595 | supertype.Assign(iface.Get()); |
| 4596 | break; |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 4597 | } |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 4598 | } |
| 4599 | } |
| 4600 | |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4601 | // At this point if verification failed, then supertype is the "first" supertype that failed |
| 4602 | // verification (without a specific order). If verification succeeded, then supertype is either |
| 4603 | // null or the original superclass of klass and is verified. |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4604 | DCHECK(supertype == nullptr || |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4605 | supertype.Get() == klass->GetSuperClass() || |
| 4606 | !supertype->IsVerified()); |
| 4607 | |
Elliott Hughes | 634eb2e | 2012-03-22 16:06:28 -0700 | [diff] [blame] | 4608 | // 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] | 4609 | const DexFile& dex_file = *klass->GetDexCache()->GetDexFile(); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4610 | ClassStatus oat_file_class_status(ClassStatus::kNotReady); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4611 | bool preverified = VerifyClassUsingOatFile(dex_file, klass.Get(), oat_file_class_status); |
Bharadwaj Kalandhabhatta | 271c1e1 | 2017-06-27 11:14:49 -0700 | [diff] [blame] | 4612 | |
| 4613 | VLOG(class_linker) << "Class preverified status for class " |
| 4614 | << klass->PrettyDescriptor() |
| 4615 | << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8() |
| 4616 | << ": " |
Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 4617 | << preverified |
| 4618 | << "( " << oat_file_class_status << ")"; |
Bharadwaj Kalandhabhatta | 271c1e1 | 2017-06-27 11:14:49 -0700 | [diff] [blame] | 4619 | |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 4620 | // If the oat file says the class had an error, re-run the verifier. That way we will get a |
| 4621 | // precise error message. To ensure a rerun, test: |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 4622 | // mirror::Class::IsErroneous(oat_file_class_status) => !preverified |
| 4623 | DCHECK(!mirror::Class::IsErroneous(oat_file_class_status) || !preverified); |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 4624 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4625 | std::string error_msg; |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4626 | verifier::FailureKind verifier_failure = verifier::FailureKind::kNoFailure; |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 4627 | if (!preverified) { |
Mathieu Chartier | 9e050df | 2017-08-09 10:05:47 -0700 | [diff] [blame] | 4628 | verifier_failure = PerformClassVerification(self, klass, log_level, &error_msg); |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 4629 | } |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4630 | |
| 4631 | // Verification is done, grab the lock again. |
| 4632 | ObjectLock<mirror::Class> lock(self, klass); |
| 4633 | |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4634 | if (preverified || verifier_failure != verifier::FailureKind::kHardFailure) { |
| 4635 | if (!preverified && verifier_failure != verifier::FailureKind::kNoFailure) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4636 | VLOG(class_linker) << "Soft verification failure in class " |
| 4637 | << klass->PrettyDescriptor() |
| 4638 | << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8() |
| 4639 | << " because: " << error_msg; |
Ian Rogers | 529781d | 2012-07-23 17:24:29 -0700 | [diff] [blame] | 4640 | } |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 4641 | self->AssertNoPendingException(); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 4642 | // Make sure all classes referenced by catch blocks are resolved. |
Alex Light | 5a55986 | 2016-01-29 12:24:48 -0800 | [diff] [blame] | 4643 | ResolveClassExceptionHandlerTypes(klass); |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4644 | if (verifier_failure == verifier::FailureKind::kNoFailure) { |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4645 | // Even though there were no verifier failures we need to respect whether the super-class and |
| 4646 | // super-default-interfaces were verified or requiring runtime reverification. |
Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 4647 | if (supertype == nullptr |
| 4648 | || supertype->IsVerified() |
| 4649 | || supertype->IsVerifiedNeedsAccessChecks()) { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4650 | mirror::Class::SetStatus(klass, ClassStatus::kVerified, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4651 | } else { |
Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 4652 | CHECK(Runtime::Current()->IsAotCompiler()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4653 | CHECK_EQ(supertype->GetStatus(), ClassStatus::kRetryVerificationAtRuntime); |
| 4654 | mirror::Class::SetStatus(klass, ClassStatus::kRetryVerificationAtRuntime, self); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4655 | // 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] | 4656 | verifier_failure = verifier::FailureKind::kSoftFailure; |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4657 | } |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 4658 | } else { |
Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 4659 | CHECK(verifier_failure == verifier::FailureKind::kSoftFailure || |
Nicolas Geoffray | d1728bf | 2021-01-12 14:02:29 +0000 | [diff] [blame] | 4660 | verifier_failure == verifier::FailureKind::kTypeChecksFailure || |
Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 4661 | verifier_failure == verifier::FailureKind::kAccessChecksFailure); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 4662 | // Soft failures at compile time should be retried at runtime. Soft |
| 4663 | // failures at runtime will be handled by slow paths in the generated |
| 4664 | // code. Set status accordingly. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 4665 | if (Runtime::Current()->IsAotCompiler()) { |
Nicolas Geoffray | d1728bf | 2021-01-12 14:02:29 +0000 | [diff] [blame] | 4666 | if (verifier_failure == verifier::FailureKind::kSoftFailure || |
| 4667 | verifier_failure == verifier::FailureKind::kTypeChecksFailure) { |
Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 4668 | mirror::Class::SetStatus(klass, ClassStatus::kRetryVerificationAtRuntime, self); |
| 4669 | } else { |
| 4670 | mirror::Class::SetStatus(klass, ClassStatus::kVerifiedNeedsAccessChecks, self); |
| 4671 | } |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 4672 | } else { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4673 | mirror::Class::SetStatus(klass, ClassStatus::kVerified, self); |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 4674 | // As this is a fake verified status, make sure the methods are _not_ marked |
| 4675 | // kAccSkipAccessChecks later. |
| 4676 | klass->SetVerificationAttempted(); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 4677 | } |
| 4678 | } |
jeffhao | 5cfd6fb | 2011-09-27 13:54:29 -0700 | [diff] [blame] | 4679 | } else { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4680 | VLOG(verifier) << "Verification failed on class " << klass->PrettyDescriptor() |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 4681 | << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8() |
| 4682 | << " because: " << error_msg; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4683 | self->AssertNoPendingException(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4684 | ThrowVerifyError(klass.Get(), "%s", error_msg.c_str()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4685 | mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self); |
jeffhao | 5cfd6fb | 2011-09-27 13:54:29 -0700 | [diff] [blame] | 4686 | } |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4687 | if (preverified || verifier_failure == verifier::FailureKind::kNoFailure) { |
Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 4688 | if (oat_file_class_status == ClassStatus::kVerifiedNeedsAccessChecks || |
| 4689 | UNLIKELY(Runtime::Current()->IsVerificationSoftFail())) { |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 4690 | // Never skip access checks if the verification soft fail is forced. |
| 4691 | // Mark the class as having a verification attempt to avoid re-running the verifier. |
| 4692 | klass->SetVerificationAttempted(); |
| 4693 | } else { |
Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 4694 | // Class is verified so we don't need to do any access check on its methods. |
| 4695 | // Let the interpreter know it by setting the kAccSkipAccessChecks flag onto each |
| 4696 | // method. |
| 4697 | // Note: we're going here during compilation and at runtime. When we set the |
| 4698 | // kAccSkipAccessChecks flag when compiling image classes, the flag is recorded |
| 4699 | // in the image and is set when loading the image. |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 4700 | EnsureSkipAccessChecksMethods(klass, image_pointer_size_); |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 4701 | } |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 4702 | } |
Nicolas Geoffray | 486dda0 | 2017-09-11 14:15:52 +0100 | [diff] [blame] | 4703 | // Done verifying. Notify the compiler about the verification status, in case the class |
| 4704 | // was verified implicitly (eg super class of a compiled class). |
| 4705 | if (Runtime::Current()->IsAotCompiler()) { |
| 4706 | Runtime::Current()->GetCompilerCallbacks()->UpdateClassState( |
| 4707 | ClassReference(&klass->GetDexFile(), klass->GetDexClassDefIndex()), klass->GetStatus()); |
| 4708 | } |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame] | 4709 | return verifier_failure; |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 4710 | } |
| 4711 | |
Mathieu Chartier | 9e050df | 2017-08-09 10:05:47 -0700 | [diff] [blame] | 4712 | verifier::FailureKind ClassLinker::PerformClassVerification(Thread* self, |
| 4713 | Handle<mirror::Class> klass, |
| 4714 | verifier::HardFailLogMode log_level, |
| 4715 | std::string* error_msg) { |
| 4716 | Runtime* const runtime = Runtime::Current(); |
Andreas Gampe | a43ba3d | 2019-03-13 15:49:20 -0700 | [diff] [blame] | 4717 | return verifier::ClassVerifier::VerifyClass(self, |
| 4718 | klass.Get(), |
| 4719 | runtime->GetCompilerCallbacks(), |
| 4720 | runtime->IsAotCompiler(), |
| 4721 | log_level, |
| 4722 | Runtime::Current()->GetTargetSdkVersion(), |
| 4723 | error_msg); |
Mathieu Chartier | 9e050df | 2017-08-09 10:05:47 -0700 | [diff] [blame] | 4724 | } |
| 4725 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 4726 | bool ClassLinker::VerifyClassUsingOatFile(const DexFile& dex_file, |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4727 | ObjPtr<mirror::Class> klass, |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4728 | ClassStatus& oat_file_class_status) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 4729 | // If we're compiling, we can only verify the class using the oat file if |
| 4730 | // 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] | 4731 | // the compilation unit (app - dependencies). We will let the compiler callback |
| 4732 | // tell us about the latter. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 4733 | if (Runtime::Current()->IsAotCompiler()) { |
Andreas Gampe | e993458 | 2018-01-19 21:23:04 -0800 | [diff] [blame] | 4734 | CompilerCallbacks* callbacks = Runtime::Current()->GetCompilerCallbacks(); |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 4735 | // We are compiling an app (not the image). |
Andreas Gampe | e993458 | 2018-01-19 21:23:04 -0800 | [diff] [blame] | 4736 | if (!callbacks->CanUseOatStatusForVerification(klass.Ptr())) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 4737 | return false; |
| 4738 | } |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4739 | } |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 4740 | |
Andreas Gampe | b40d361 | 2018-06-26 15:49:42 -0700 | [diff] [blame] | 4741 | const OatDexFile* oat_dex_file = dex_file.GetOatDexFile(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4742 | // 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] | 4743 | if (oat_dex_file == nullptr || oat_dex_file->GetOatFile() == nullptr) { |
Anwar Ghuloum | ad256bb | 2013-07-18 14:58:55 -0700 | [diff] [blame] | 4744 | return false; |
| 4745 | } |
| 4746 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 4747 | uint16_t class_def_index = klass->GetDexClassDefIndex(); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 4748 | oat_file_class_status = oat_dex_file->GetOatClass(class_def_index).GetStatus(); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4749 | if (oat_file_class_status >= ClassStatus::kVerified) { |
Mathieu Chartier | a079e3a | 2016-03-16 19:08:31 -0700 | [diff] [blame] | 4750 | return true; |
| 4751 | } |
Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 4752 | if (oat_file_class_status >= ClassStatus::kVerifiedNeedsAccessChecks) { |
| 4753 | // We return that the clas has already been verified, and the caller should |
| 4754 | // check the class status to ensure we run with access checks. |
| 4755 | return true; |
| 4756 | } |
Mathieu Chartier | a079e3a | 2016-03-16 19:08:31 -0700 | [diff] [blame] | 4757 | // If we only verified a subset of the classes at compile time, we can end up with classes that |
| 4758 | // were resolved by the verifier. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4759 | if (oat_file_class_status == ClassStatus::kResolved) { |
Mathieu Chartier | a079e3a | 2016-03-16 19:08:31 -0700 | [diff] [blame] | 4760 | return false; |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4761 | } |
Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 4762 | // We never expect a .oat file to have kRetryVerificationAtRuntime statuses. |
| 4763 | CHECK_NE(oat_file_class_status, ClassStatus::kRetryVerificationAtRuntime) |
| 4764 | << klass->PrettyClass() << " " << dex_file.GetLocation(); |
| 4765 | |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 4766 | if (mirror::Class::IsErroneous(oat_file_class_status)) { |
jeffhao | 1ac2944 | 2012-03-26 11:37:32 -0700 | [diff] [blame] | 4767 | // Compile time verification failed with a hard error. This is caused by invalid instructions |
| 4768 | // in the class. These errors are unrecoverable. |
| 4769 | return false; |
| 4770 | } |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4771 | if (oat_file_class_status == ClassStatus::kNotReady) { |
Ian Rogers | c476227 | 2012-02-01 15:55:55 -0800 | [diff] [blame] | 4772 | // Status is uninitialized if we couldn't determine the status at compile time, for example, |
| 4773 | // not loading the class. |
| 4774 | // TODO: when the verifier doesn't rely on Class-es failing to resolve/load the type hierarchy |
| 4775 | // isn't a problem and this case shouldn't occur |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4776 | return false; |
| 4777 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 4778 | std::string temp; |
Elliott Hughes | 634eb2e | 2012-03-22 16:06:28 -0700 | [diff] [blame] | 4779 | LOG(FATAL) << "Unexpected class status: " << oat_file_class_status |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4780 | << " " << dex_file.GetLocation() << " " << klass->PrettyClass() << " " |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 4781 | << klass->GetDescriptor(&temp); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 4782 | UNREACHABLE(); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4783 | } |
| 4784 | |
Alex Light | 5a55986 | 2016-01-29 12:24:48 -0800 | [diff] [blame] | 4785 | void ClassLinker::ResolveClassExceptionHandlerTypes(Handle<mirror::Class> klass) { |
Alex Light | 51a64d5 | 2015-12-17 13:55:59 -0800 | [diff] [blame] | 4786 | for (ArtMethod& method : klass->GetMethods(image_pointer_size_)) { |
Alex Light | 5a55986 | 2016-01-29 12:24:48 -0800 | [diff] [blame] | 4787 | ResolveMethodExceptionHandlerTypes(&method); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4788 | } |
| 4789 | } |
| 4790 | |
Alex Light | 5a55986 | 2016-01-29 12:24:48 -0800 | [diff] [blame] | 4791 | void ClassLinker::ResolveMethodExceptionHandlerTypes(ArtMethod* method) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4792 | // similar to DexVerifier::ScanTryCatchBlocks and dex2oat's ResolveExceptionsForMethod. |
David Sehr | 0225f8e | 2018-01-31 08:52:24 +0000 | [diff] [blame] | 4793 | CodeItemDataAccessor accessor(method->DexInstructionData()); |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 4794 | if (!accessor.HasCodeItem()) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4795 | return; // native or abstract method |
| 4796 | } |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 4797 | if (accessor.TriesSize() == 0) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4798 | return; // nothing to process |
| 4799 | } |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 4800 | const uint8_t* handlers_ptr = accessor.GetCatchHandlerData(0); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4801 | uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4802 | for (uint32_t idx = 0; idx < handlers_size; idx++) { |
| 4803 | CatchHandlerIterator iterator(handlers_ptr); |
| 4804 | for (; iterator.HasNext(); iterator.Next()) { |
| 4805 | // Ensure exception types are resolved so that they don't need resolution to be delivered, |
| 4806 | // unresolved exception types will be ignored by exception delivery |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 4807 | if (iterator.GetHandlerTypeIndex().IsValid()) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4808 | ObjPtr<mirror::Class> exception_type = ResolveType(iterator.GetHandlerTypeIndex(), method); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4809 | if (exception_type == nullptr) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4810 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 4811 | Thread::Current()->ClearException(); |
| 4812 | } |
| 4813 | } |
| 4814 | } |
| 4815 | handlers_ptr = iterator.EndDataPointer(); |
| 4816 | } |
| 4817 | } |
| 4818 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 4819 | ObjPtr<mirror::Class> ClassLinker::CreateProxyClass(ScopedObjectAccessAlreadyRunnable& soa, |
| 4820 | jstring name, |
| 4821 | jobjectArray interfaces, |
| 4822 | jobject loader, |
| 4823 | jobjectArray methods, |
| 4824 | jobjectArray throws) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4825 | Thread* self = soa.Self(); |
Alex Light | e9f6103 | 2018-09-24 16:04:51 -0700 | [diff] [blame] | 4826 | |
| 4827 | // This is to prevent the calls to ClassLoad and ClassPrepare which can cause java/user-supplied |
| 4828 | // code to be executed. We put it up here so we can avoid all the allocations associated with |
| 4829 | // creating the class. This can happen with (eg) jit-threads. |
| 4830 | if (!self->CanLoadClasses()) { |
| 4831 | // Make sure we don't try to load anything, potentially causing an infinite loop. |
| 4832 | ObjPtr<mirror::Throwable> pre_allocated = |
| 4833 | Runtime::Current()->GetPreAllocatedNoClassDefFoundError(); |
| 4834 | self->SetException(pre_allocated); |
| 4835 | return nullptr; |
| 4836 | } |
| 4837 | |
Alex Light | 133987d | 2020-03-26 19:22:12 +0000 | [diff] [blame] | 4838 | StackHandleScope<12> hs(self); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4839 | MutableHandle<mirror::Class> temp_klass(hs.NewHandle( |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 4840 | AllocClass(self, GetClassRoot<mirror::Class>(this), sizeof(mirror::Class)))); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4841 | if (temp_klass == nullptr) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4842 | CHECK(self->IsExceptionPending()); // OOME. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4843 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4844 | } |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4845 | DCHECK(temp_klass->GetClass() != nullptr); |
| 4846 | temp_klass->SetObjectSize(sizeof(mirror::Proxy)); |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 4847 | // Set the class access flags incl. VerificationAttempted, so we do not try to set the flag on |
| 4848 | // the methods. |
Vladimir Marko | b68bb7a | 2020-03-17 10:55:25 +0000 | [diff] [blame] | 4849 | temp_klass->SetAccessFlagsDuringLinking( |
| 4850 | kAccClassIsProxy | kAccPublic | kAccFinal | kAccVerificationAttempted); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4851 | temp_klass->SetClassLoader(soa.Decode<mirror::ClassLoader>(loader)); |
| 4852 | DCHECK_EQ(temp_klass->GetPrimitiveType(), Primitive::kPrimNot); |
| 4853 | temp_klass->SetName(soa.Decode<mirror::String>(name)); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 4854 | temp_klass->SetDexCache(GetClassRoot<mirror::Proxy>(this)->GetDexCache()); |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 4855 | // Object has an empty iftable, copy it for that reason. |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 4856 | temp_klass->SetIfTable(GetClassRoot<mirror::Object>(this)->GetIfTable()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4857 | mirror::Class::SetStatus(temp_klass, ClassStatus::kIdx, self); |
Vladimir Marko | 3892e62 | 2019-03-15 15:22:18 +0000 | [diff] [blame] | 4858 | std::string storage; |
| 4859 | const char* descriptor = temp_klass->GetDescriptor(&storage); |
| 4860 | const size_t hash = ComputeModifiedUtf8Hash(descriptor); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4861 | |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 4862 | // 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] | 4863 | LinearAlloc* const allocator = GetOrCreateAllocatorForClassLoader(temp_klass->GetClassLoader()); |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 4864 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4865 | // Insert the class before loading the fields as the field roots |
| 4866 | // (ArtField::declaring_class_) are only visited from the class |
| 4867 | // table. There can't be any suspend points between inserting the |
| 4868 | // class and setting the field arrays below. |
Vladimir Marko | 3892e62 | 2019-03-15 15:22:18 +0000 | [diff] [blame] | 4869 | ObjPtr<mirror::Class> existing = InsertClass(descriptor, temp_klass.Get(), hash); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4870 | CHECK(existing == nullptr); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4871 | |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 4872 | // Instance fields are inherited, but we add a couple of static fields... |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4873 | const size_t num_fields = 2; |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 4874 | LengthPrefixedArray<ArtField>* sfields = AllocArtFieldArray(self, allocator, num_fields); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4875 | temp_klass->SetSFieldsPtr(sfields); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4876 | |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 4877 | // 1. Create a static field 'interfaces' that holds the _declared_ interfaces implemented by |
| 4878 | // our proxy, so Class.getInterfaces doesn't return the flattened set. |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4879 | ArtField& interfaces_sfield = sfields->At(0); |
| 4880 | interfaces_sfield.SetDexFieldIndex(0); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4881 | interfaces_sfield.SetDeclaringClass(temp_klass.Get()); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4882 | interfaces_sfield.SetAccessFlags(kAccStatic | kAccPublic | kAccFinal); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4883 | |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 4884 | // 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] | 4885 | ArtField& throws_sfield = sfields->At(1); |
| 4886 | throws_sfield.SetDexFieldIndex(1); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4887 | throws_sfield.SetDeclaringClass(temp_klass.Get()); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4888 | throws_sfield.SetAccessFlags(kAccStatic | kAccPublic | kAccFinal); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4889 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4890 | // Proxies have 1 direct method, the constructor |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4891 | const size_t num_direct_methods = 1; |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4892 | |
Alex Light | 133987d | 2020-03-26 19:22:12 +0000 | [diff] [blame] | 4893 | // The array we get passed contains all methods, including private and static |
| 4894 | // ones that aren't proxied. We need to filter those out since only interface |
| 4895 | // methods (non-private & virtual) are actually proxied. |
| 4896 | Handle<mirror::ObjectArray<mirror::Method>> h_methods = |
| 4897 | hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::Method>>(methods)); |
Vladimir Marko | 679730e | 2018-05-25 15:06:48 +0100 | [diff] [blame] | 4898 | DCHECK_EQ(h_methods->GetClass(), GetClassRoot<mirror::ObjectArray<mirror::Method>>()) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4899 | << mirror::Class::PrettyClass(h_methods->GetClass()); |
Alex Light | 133987d | 2020-03-26 19:22:12 +0000 | [diff] [blame] | 4900 | // List of the actual virtual methods this class will have. |
| 4901 | std::vector<ArtMethod*> proxied_methods; |
| 4902 | std::vector<size_t> proxied_throws_idx; |
| 4903 | proxied_methods.reserve(h_methods->GetLength()); |
| 4904 | proxied_throws_idx.reserve(h_methods->GetLength()); |
| 4905 | // Filter out to only the non-private virtual methods. |
| 4906 | for (auto [mirror, idx] : ZipCount(h_methods.Iterate<mirror::Method>())) { |
| 4907 | ArtMethod* m = mirror->GetArtMethod(); |
| 4908 | if (!m->IsPrivate() && !m->IsStatic()) { |
| 4909 | proxied_methods.push_back(m); |
| 4910 | proxied_throws_idx.push_back(idx); |
| 4911 | } |
| 4912 | } |
| 4913 | const size_t num_virtual_methods = proxied_methods.size(); |
Alex Light | bc11509 | 2020-03-27 11:25:16 -0700 | [diff] [blame] | 4914 | // We also need to filter out the 'throws'. The 'throws' are a Class[][] that |
| 4915 | // contains an array of all the classes each function is declared to throw. |
| 4916 | // This is used to wrap unexpected exceptions in a |
| 4917 | // UndeclaredThrowableException exception. This array is in the same order as |
| 4918 | // the methods array and like the methods array must be filtered to remove any |
| 4919 | // non-proxied methods. |
Alex Light | 133987d | 2020-03-26 19:22:12 +0000 | [diff] [blame] | 4920 | const bool has_filtered_methods = |
| 4921 | static_cast<int32_t>(num_virtual_methods) != h_methods->GetLength(); |
| 4922 | MutableHandle<mirror::ObjectArray<mirror::ObjectArray<mirror::Class>>> original_proxied_throws( |
| 4923 | hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::ObjectArray<mirror::Class>>>(throws))); |
| 4924 | MutableHandle<mirror::ObjectArray<mirror::ObjectArray<mirror::Class>>> proxied_throws( |
| 4925 | hs.NewHandle<mirror::ObjectArray<mirror::ObjectArray<mirror::Class>>>( |
| 4926 | (has_filtered_methods) |
| 4927 | ? mirror::ObjectArray<mirror::ObjectArray<mirror::Class>>::Alloc( |
| 4928 | self, original_proxied_throws->GetClass(), num_virtual_methods) |
| 4929 | : original_proxied_throws.Get())); |
Alex Light | bc11509 | 2020-03-27 11:25:16 -0700 | [diff] [blame] | 4930 | if (proxied_throws.IsNull() && !original_proxied_throws.IsNull()) { |
| 4931 | self->AssertPendingOOMException(); |
| 4932 | return nullptr; |
| 4933 | } |
Alex Light | 133987d | 2020-03-26 19:22:12 +0000 | [diff] [blame] | 4934 | if (has_filtered_methods) { |
| 4935 | for (auto [orig_idx, new_idx] : ZipCount(MakeIterationRange(proxied_throws_idx))) { |
| 4936 | DCHECK_LE(new_idx, orig_idx); |
| 4937 | proxied_throws->Set(new_idx, original_proxied_throws->Get(orig_idx)); |
| 4938 | } |
| 4939 | } |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4940 | |
| 4941 | // Create the methods array. |
| 4942 | LengthPrefixedArray<ArtMethod>* proxy_class_methods = AllocArtMethodArray( |
| 4943 | self, allocator, num_direct_methods + num_virtual_methods); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4944 | // Currently AllocArtMethodArray cannot return null, but the OOM logic is left there in case we |
| 4945 | // want to throw OOM in the future. |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4946 | if (UNLIKELY(proxy_class_methods == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4947 | self->AssertPendingOOMException(); |
| 4948 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4949 | } |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4950 | temp_klass->SetMethodsPtr(proxy_class_methods, num_direct_methods, num_virtual_methods); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4951 | |
| 4952 | // Create the single direct method. |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4953 | CreateProxyConstructor(temp_klass, temp_klass->GetDirectMethodUnchecked(0, image_pointer_size_)); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4954 | |
| 4955 | // Create virtual method using specified prototypes. |
| 4956 | // TODO These should really use the iterators. |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4957 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4958 | auto* virtual_method = temp_klass->GetVirtualMethodUnchecked(i, image_pointer_size_); |
Alex Light | 133987d | 2020-03-26 19:22:12 +0000 | [diff] [blame] | 4959 | auto* prototype = proxied_methods[i]; |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4960 | CreateProxyMethod(temp_klass, prototype, virtual_method); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4961 | DCHECK(virtual_method->GetDeclaringClass() != nullptr); |
| 4962 | DCHECK(prototype->GetDeclaringClass() != nullptr); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4963 | } |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4964 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4965 | // The super class is java.lang.reflect.Proxy |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 4966 | temp_klass->SetSuperClass(GetClassRoot<mirror::Proxy>(this)); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4967 | // Now effectively in the loaded state. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4968 | mirror::Class::SetStatus(temp_klass, ClassStatus::kLoaded, self); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4969 | self->AssertNoPendingException(); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4970 | |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4971 | // At this point the class is loaded. Publish a ClassLoad event. |
| 4972 | // Note: this may be a temporary class. It is a listener's responsibility to handle this. |
| 4973 | Runtime::Current()->GetRuntimeCallbacks()->ClassLoad(temp_klass); |
| 4974 | |
| 4975 | MutableHandle<mirror::Class> klass = hs.NewHandle<mirror::Class>(nullptr); |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 4976 | { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4977 | // Must hold lock on object when resolved. |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4978 | ObjectLock<mirror::Class> resolution_lock(self, temp_klass); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4979 | // Link the fields and virtual methods, creating vtable and iftables. |
| 4980 | // The new class will replace the old one in the class table. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4981 | Handle<mirror::ObjectArray<mirror::Class>> h_interfaces( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 4982 | hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::Class>>(interfaces))); |
Vladimir Marko | 3892e62 | 2019-03-15 15:22:18 +0000 | [diff] [blame] | 4983 | if (!LinkClass(self, descriptor, temp_klass, h_interfaces, &klass)) { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4984 | mirror::Class::SetStatus(temp_klass, ClassStatus::kErrorUnresolved, self); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 4985 | return nullptr; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4986 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4987 | } |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4988 | CHECK(temp_klass->IsRetired()); |
| 4989 | CHECK_NE(temp_klass.Get(), klass.Get()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4990 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4991 | CHECK_EQ(interfaces_sfield.GetDeclaringClass(), klass.Get()); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 4992 | interfaces_sfield.SetObject<false>( |
| 4993 | klass.Get(), |
Mathieu Chartier | f8ac97f | 2016-10-05 15:56:52 -0700 | [diff] [blame] | 4994 | soa.Decode<mirror::ObjectArray<mirror::Class>>(interfaces)); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4995 | CHECK_EQ(throws_sfield.GetDeclaringClass(), klass.Get()); |
| 4996 | throws_sfield.SetObject<false>( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 4997 | klass.Get(), |
Alex Light | 133987d | 2020-03-26 19:22:12 +0000 | [diff] [blame] | 4998 | proxied_throws.Get()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4999 | |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 5000 | Runtime::Current()->GetRuntimeCallbacks()->ClassPrepare(temp_klass, klass); |
| 5001 | |
Vladimir Marko | 305c38b | 2018-02-14 11:50:07 +0000 | [diff] [blame] | 5002 | // SubtypeCheckInfo::Initialized must happen-before any new-instance for that type. |
| 5003 | // See also ClassLinker::EnsureInitialized(). |
| 5004 | if (kBitstringSubtypeCheckEnabled) { |
| 5005 | MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_); |
| 5006 | SubtypeCheck<ObjPtr<mirror::Class>>::EnsureInitialized(klass.Get()); |
| 5007 | // TODO: Avoid taking subtype_check_lock_ if SubtypeCheck for j.l.r.Proxy is already assigned. |
| 5008 | } |
| 5009 | |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 5010 | VisiblyInitializedCallback* callback = nullptr; |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5011 | { |
| 5012 | // Lock on klass is released. Lock new class object. |
| 5013 | ObjectLock<mirror::Class> initialization_lock(self, klass); |
Andreas Gampe | 5b20b35 | 2018-10-11 19:03:20 -0700 | [diff] [blame] | 5014 | EnsureSkipAccessChecksMethods(klass, image_pointer_size_); |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 5015 | // Conservatively go through the ClassStatus::kInitialized state. |
| 5016 | callback = MarkClassInitialized(self, klass); |
| 5017 | } |
| 5018 | if (callback != nullptr) { |
| 5019 | callback->MakeVisible(self); |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 5020 | } |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 5021 | |
David Srbecky | 346fd96 | 2020-07-27 16:51:00 +0100 | [diff] [blame] | 5022 | // Consistency checks. |
Elliott Hughes | 67d9200 | 2012-03-26 15:08:51 -0700 | [diff] [blame] | 5023 | if (kIsDebugBuild) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 5024 | CHECK(klass->GetIFieldsPtr() == nullptr); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5025 | CheckProxyConstructor(klass->GetDirectMethod(0, image_pointer_size_)); |
| 5026 | |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 5027 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5028 | auto* virtual_method = klass->GetVirtualMethodUnchecked(i, image_pointer_size_); |
Alex Light | 133987d | 2020-03-26 19:22:12 +0000 | [diff] [blame] | 5029 | CheckProxyMethod(virtual_method, proxied_methods[i]); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 5030 | } |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 5031 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 5032 | StackHandleScope<1> hs2(self); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 5033 | Handle<mirror::String> decoded_name = hs2.NewHandle(soa.Decode<mirror::String>(name)); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 5034 | std::string interfaces_field_name(StringPrintf("java.lang.Class[] %s.interfaces", |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 5035 | decoded_name->ToModifiedUtf8().c_str())); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5036 | CHECK_EQ(ArtField::PrettyField(klass->GetStaticField(0)), interfaces_field_name); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 5037 | |
| 5038 | std::string throws_field_name(StringPrintf("java.lang.Class[][] %s.throws", |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 5039 | decoded_name->ToModifiedUtf8().c_str())); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5040 | CHECK_EQ(ArtField::PrettyField(klass->GetStaticField(1)), throws_field_name); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 5041 | |
Narayan Kamath | 6b2dc31 | 2017-03-14 13:26:12 +0000 | [diff] [blame] | 5042 | CHECK_EQ(klass.Get()->GetProxyInterfaces(), |
Mathieu Chartier | f8ac97f | 2016-10-05 15:56:52 -0700 | [diff] [blame] | 5043 | soa.Decode<mirror::ObjectArray<mirror::Class>>(interfaces)); |
Narayan Kamath | 6b2dc31 | 2017-03-14 13:26:12 +0000 | [diff] [blame] | 5044 | CHECK_EQ(klass.Get()->GetProxyThrows(), |
Alex Light | 133987d | 2020-03-26 19:22:12 +0000 | [diff] [blame] | 5045 | proxied_throws.Get()); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 5046 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5047 | return klass.Get(); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 5048 | } |
| 5049 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5050 | void ClassLinker::CreateProxyConstructor(Handle<mirror::Class> klass, ArtMethod* out) { |
| 5051 | // Create constructor for Proxy that must initialize the method. |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 5052 | ObjPtr<mirror::Class> proxy_class = GetClassRoot<mirror::Proxy>(this); |
| 5053 | CHECK_EQ(proxy_class->NumDirectMethods(), 21u); |
Przemyslaw Szczepaniak | f11cd29 | 2016-08-17 17:46:38 +0100 | [diff] [blame] | 5054 | |
Igor Murashkin | 9d3d752 | 2017-02-27 10:39:49 -0800 | [diff] [blame] | 5055 | // Find the <init>(InvocationHandler)V method. The exact method offset varies depending |
| 5056 | // 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] | 5057 | ArtMethod* proxy_constructor = |
| 5058 | jni::DecodeArtMethod(WellKnownClasses::java_lang_reflect_Proxy_init); |
Igor Murashkin | 9d3d752 | 2017-02-27 10:39:49 -0800 | [diff] [blame] | 5059 | DCHECK(proxy_constructor != nullptr) |
| 5060 | << "Could not find <init> method in java.lang.reflect.Proxy"; |
| 5061 | |
Jeff Hao | db8a664 | 2014-08-14 17:18:52 -0700 | [diff] [blame] | 5062 | // Clone the existing constructor of Proxy (our constructor would just invoke it so steal its |
| 5063 | // code_ too) |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5064 | DCHECK(out != nullptr); |
| 5065 | out->CopyFrom(proxy_constructor, image_pointer_size_); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 5066 | // 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] | 5067 | // 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] | 5068 | // 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] | 5069 | out->SetAccessFlags((out->GetAccessFlags() & ~kAccProtected) | |
| 5070 | kAccPublic | |
| 5071 | kAccCompileDontBother); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5072 | out->SetDeclaringClass(klass.Get()); |
Vladimir Marko | d1ee20f | 2017-08-17 09:21:16 +0000 | [diff] [blame] | 5073 | |
| 5074 | // Set the original constructor method. |
| 5075 | out->SetDataPtrSize(proxy_constructor, image_pointer_size_); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 5076 | } |
| 5077 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5078 | void ClassLinker::CheckProxyConstructor(ArtMethod* constructor) const { |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 5079 | CHECK(constructor->IsConstructor()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5080 | auto* np = constructor->GetInterfaceMethodIfProxy(image_pointer_size_); |
| 5081 | CHECK_STREQ(np->GetName(), "<init>"); |
| 5082 | CHECK_STREQ(np->GetSignature().ToString().c_str(), "(Ljava/lang/reflect/InvocationHandler;)V"); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 5083 | DCHECK(constructor->IsPublic()); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 5084 | } |
| 5085 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5086 | void ClassLinker::CreateProxyMethod(Handle<mirror::Class> klass, ArtMethod* prototype, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5087 | ArtMethod* out) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 5088 | // 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] | 5089 | // as necessary |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5090 | DCHECK(out != nullptr); |
| 5091 | out->CopyFrom(prototype, image_pointer_size_); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 5092 | |
Alex Light | e9dd04f | 2016-03-16 16:09:45 -0700 | [diff] [blame] | 5093 | // Set class to be the concrete proxy class. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5094 | out->SetDeclaringClass(klass.Get()); |
Alex Light | e9dd04f | 2016-03-16 16:09:45 -0700 | [diff] [blame] | 5095 | // Clear the abstract, default and conflict flags to ensure that defaults aren't picked in |
| 5096 | // preference to the invocation handler. |
| 5097 | const uint32_t kRemoveFlags = kAccAbstract | kAccDefault | kAccDefaultConflict; |
| 5098 | // Make the method final. |
Mathieu Chartier | 201e297 | 2017-06-05 18:34:53 -0700 | [diff] [blame] | 5099 | // Mark kAccCompileDontBother so that we don't take JIT samples for the method. b/62349349 |
| 5100 | const uint32_t kAddFlags = kAccFinal | kAccCompileDontBother; |
Alex Light | e9dd04f | 2016-03-16 16:09:45 -0700 | [diff] [blame] | 5101 | out->SetAccessFlags((out->GetAccessFlags() & ~kRemoveFlags) | kAddFlags); |
| 5102 | |
Vladimir Marko | d1ee20f | 2017-08-17 09:21:16 +0000 | [diff] [blame] | 5103 | // Set the original interface method. |
| 5104 | out->SetDataPtrSize(prototype, image_pointer_size_); |
| 5105 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 5106 | // At runtime the method looks like a reference and argument saving method, clone the code |
| 5107 | // related parameters from this method. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5108 | out->SetEntryPointFromQuickCompiledCode(GetQuickProxyInvokeHandler()); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 5109 | } |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 5110 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5111 | void ClassLinker::CheckProxyMethod(ArtMethod* method, ArtMethod* prototype) const { |
David Srbecky | 346fd96 | 2020-07-27 16:51:00 +0100 | [diff] [blame] | 5112 | // Basic consistency checks. |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 5113 | CHECK(!prototype->IsFinal()); |
| 5114 | CHECK(method->IsFinal()); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5115 | CHECK(method->IsInvokable()); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 5116 | |
| 5117 | // The proxy method doesn't have its own dex cache or dex file and so it steals those of its |
| 5118 | // 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] | 5119 | CHECK_EQ(prototype->GetDexMethodIndex(), method->GetDexMethodIndex()); |
Vladimir Marko | 5c3e9d1 | 2017-08-30 16:43:54 +0100 | [diff] [blame] | 5120 | CHECK_EQ(prototype, method->GetInterfaceMethodIfProxy(image_pointer_size_)); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 5121 | } |
| 5122 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5123 | bool ClassLinker::CanWeInitializeClass(ObjPtr<mirror::Class> klass, bool can_init_statics, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5124 | bool can_init_parents) { |
Brian Carlstrom | 610e49f | 2013-11-04 17:07:22 -0800 | [diff] [blame] | 5125 | if (can_init_statics && can_init_parents) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5126 | return true; |
| 5127 | } |
| 5128 | if (!can_init_statics) { |
| 5129 | // Check if there's a class initializer. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5130 | ArtMethod* clinit = klass->FindClassInitializer(image_pointer_size_); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5131 | if (clinit != nullptr) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5132 | return false; |
| 5133 | } |
| 5134 | // Check if there are encoded static values needing initialization. |
| 5135 | if (klass->NumStaticFields() != 0) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 5136 | const dex::ClassDef* dex_class_def = klass->GetClassDef(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5137 | DCHECK(dex_class_def != nullptr); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5138 | if (dex_class_def->static_values_off_ != 0) { |
| 5139 | return false; |
| 5140 | } |
| 5141 | } |
Vladimir Marko | 889b72d | 2019-11-12 11:01:13 +0000 | [diff] [blame] | 5142 | } |
| 5143 | // If we are a class we need to initialize all interfaces with default methods when we are |
| 5144 | // initialized. Check all of them. |
| 5145 | if (!klass->IsInterface()) { |
| 5146 | size_t num_interfaces = klass->GetIfTableCount(); |
| 5147 | for (size_t i = 0; i < num_interfaces; i++) { |
| 5148 | ObjPtr<mirror::Class> iface = klass->GetIfTable()->GetInterface(i); |
| 5149 | if (iface->HasDefaultMethods() && !iface->IsInitialized()) { |
| 5150 | if (!can_init_parents || !CanWeInitializeClass(iface, can_init_statics, can_init_parents)) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5151 | return false; |
| 5152 | } |
| 5153 | } |
| 5154 | } |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5155 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5156 | if (klass->IsInterface() || !klass->HasSuperClass()) { |
| 5157 | return true; |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5158 | } |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5159 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Vladimir Marko | 889b72d | 2019-11-12 11:01:13 +0000 | [diff] [blame] | 5160 | if (super_class->IsInitialized()) { |
| 5161 | return true; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5162 | } |
Vladimir Marko | 889b72d | 2019-11-12 11:01:13 +0000 | [diff] [blame] | 5163 | return can_init_parents && CanWeInitializeClass(super_class, can_init_statics, can_init_parents); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5164 | } |
| 5165 | |
Mathieu Chartier | 2336954 | 2020-03-04 08:24:11 -0800 | [diff] [blame] | 5166 | bool ClassLinker::InitializeClass(Thread* self, |
| 5167 | Handle<mirror::Class> klass, |
| 5168 | bool can_init_statics, |
| 5169 | bool can_init_parents) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5170 | // see JLS 3rd edition, 12.4.2 "Detailed Initialization Procedure" for the locking protocol |
| 5171 | |
| 5172 | // Are we already initialized and therefore done? |
| 5173 | // Note: we differ from the JLS here as we don't do this under the lock, this is benign as |
| 5174 | // an initialized class will never change its state. |
| 5175 | if (klass->IsInitialized()) { |
| 5176 | return true; |
| 5177 | } |
| 5178 | |
| 5179 | // 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] | 5180 | if (!CanWeInitializeClass(klass.Get(), can_init_statics, can_init_parents)) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5181 | return false; |
| 5182 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5183 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5184 | self->AllowThreadSuspension(); |
Mathieu Chartier | 2336954 | 2020-03-04 08:24:11 -0800 | [diff] [blame] | 5185 | Runtime* const runtime = Runtime::Current(); |
| 5186 | const bool stats_enabled = runtime->HasStatsEnabled(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5187 | uint64_t t0; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5188 | { |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 5189 | ObjectLock<mirror::Class> lock(self, klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5190 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5191 | // Re-check under the lock in case another thread initialized ahead of us. |
| 5192 | if (klass->IsInitialized()) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5193 | return true; |
| 5194 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5195 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5196 | // 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] | 5197 | if (klass->IsErroneous()) { |
Andreas Gampe | 7b3063b | 2019-01-07 14:12:52 -0800 | [diff] [blame] | 5198 | ThrowEarlierClassFailure(klass.Get(), true, /* log= */ true); |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 5199 | VlogClassInitializationFailure(klass); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5200 | return false; |
| 5201 | } |
| 5202 | |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 5203 | CHECK(klass->IsResolved() && !klass->IsErroneousResolved()) |
| 5204 | << klass->PrettyClass() << ": state=" << klass->GetStatus(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5205 | |
| 5206 | if (!klass->IsVerified()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5207 | VerifyClass(self, klass); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5208 | if (!klass->IsVerified()) { |
| 5209 | // We failed to verify, expect either the klass to be erroneous or verification failed at |
| 5210 | // compile time. |
| 5211 | if (klass->IsErroneous()) { |
Andreas Gampe | fc49fa0 | 2016-04-21 12:21:55 -0700 | [diff] [blame] | 5212 | // The class is erroneous. This may be a verifier error, or another thread attempted |
| 5213 | // verification and/or initialization and failed. We can distinguish those cases by |
| 5214 | // whether an exception is already pending. |
| 5215 | if (self->IsExceptionPending()) { |
| 5216 | // Check that it's a VerifyError. |
| 5217 | DCHECK_EQ("java.lang.Class<java.lang.VerifyError>", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5218 | mirror::Class::PrettyClass(self->GetException()->GetClass())); |
Andreas Gampe | fc49fa0 | 2016-04-21 12:21:55 -0700 | [diff] [blame] | 5219 | } else { |
| 5220 | // Check that another thread attempted initialization. |
| 5221 | DCHECK_NE(0, klass->GetClinitThreadId()); |
| 5222 | DCHECK_NE(self->GetTid(), klass->GetClinitThreadId()); |
| 5223 | // Need to rethrow the previous failure now. |
| 5224 | ThrowEarlierClassFailure(klass.Get(), true); |
| 5225 | } |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 5226 | VlogClassInitializationFailure(klass); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5227 | } else { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 5228 | CHECK(Runtime::Current()->IsAotCompiler()); |
Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 5229 | CHECK(klass->ShouldVerifyAtRuntime() || klass->IsVerifiedNeedsAccessChecks()); |
Vladimir Marko | d79b37b | 2018-11-02 13:06:22 +0000 | [diff] [blame] | 5230 | self->AssertNoPendingException(); |
| 5231 | self->SetException(Runtime::Current()->GetPreAllocatedNoClassDefFoundError()); |
jeffhao | a9b3bf4 | 2012-06-06 17:18:39 -0700 | [diff] [blame] | 5232 | } |
Vladimir Marko | d79b37b | 2018-11-02 13:06:22 +0000 | [diff] [blame] | 5233 | self->AssertPendingException(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5234 | return false; |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 5235 | } else { |
| 5236 | self->AssertNoPendingException(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5237 | } |
Andreas Gampe | fc49fa0 | 2016-04-21 12:21:55 -0700 | [diff] [blame] | 5238 | |
| 5239 | // A separate thread could have moved us all the way to initialized. A "simple" example |
| 5240 | // involves a subclass of the current class being initialized at the same time (which |
| 5241 | // will implicitly initialize the superclass, if scheduled that way). b/28254258 |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 5242 | DCHECK(!klass->IsErroneous()) << klass->GetStatus(); |
Andreas Gampe | fc49fa0 | 2016-04-21 12:21:55 -0700 | [diff] [blame] | 5243 | if (klass->IsInitialized()) { |
| 5244 | return true; |
| 5245 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5246 | } |
| 5247 | |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5248 | // If the class is ClassStatus::kInitializing, either this thread is |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5249 | // initializing higher up the stack or another thread has beat us |
| 5250 | // to initializing and we need to wait. Either way, this |
| 5251 | // invocation of InitializeClass will not be responsible for |
| 5252 | // running <clinit> and will return. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5253 | if (klass->GetStatus() == ClassStatus::kInitializing) { |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 5254 | // Could have got an exception during verification. |
| 5255 | if (self->IsExceptionPending()) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 5256 | VlogClassInitializationFailure(klass); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 5257 | return false; |
| 5258 | } |
Elliott Hughes | 005ab2e | 2011-09-11 17:15:31 -0700 | [diff] [blame] | 5259 | // We caught somebody else in the act; was it us? |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 5260 | if (klass->GetClinitThreadId() == self->GetTid()) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5261 | // Yes. That's fine. Return so we can continue initializing. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5262 | return true; |
| 5263 | } |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5264 | // No. That's fine. Wait for another thread to finish initializing. |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5265 | return WaitForInitializeClass(klass, self, lock); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5266 | } |
| 5267 | |
Jeff Hao | e2e4034 | 2017-07-19 10:45:18 -0700 | [diff] [blame] | 5268 | // Try to get the oat class's status for this class if the oat file is present. The compiler |
| 5269 | // tries to validate superclass descriptors, and writes the result into the oat file. |
| 5270 | // Runtime correctness is guaranteed by classpath checks done on loading. If the classpath |
| 5271 | // is different at runtime than it was at compile time, the oat file is rejected. So if the |
| 5272 | // 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] | 5273 | bool has_oat_class = false; |
Jeff Hao | e2e4034 | 2017-07-19 10:45:18 -0700 | [diff] [blame] | 5274 | const OatFile::OatClass oat_class = (runtime->IsStarted() && !runtime->IsAotCompiler()) |
| 5275 | ? OatFile::FindOatClass(klass->GetDexFile(), klass->GetDexClassDefIndex(), &has_oat_class) |
| 5276 | : OatFile::OatClass::Invalid(); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5277 | if (oat_class.GetStatus() < ClassStatus::kSuperclassValidated && |
Jeff Hao | 0cb1728 | 2017-07-12 14:51:49 -0700 | [diff] [blame] | 5278 | !ValidateSuperClassDescriptors(klass)) { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5279 | mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5280 | return false; |
| 5281 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5282 | self->AllowThreadSuspension(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5283 | |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5284 | CHECK_EQ(klass->GetStatus(), ClassStatus::kVerified) << klass->PrettyClass() |
Andreas Gampe | 9510ccd | 2016-04-20 09:55:25 -0700 | [diff] [blame] | 5285 | << " self.tid=" << self->GetTid() << " clinit.tid=" << klass->GetClinitThreadId(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5286 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5287 | // From here out other threads may observe that we're initializing and so changes of state |
| 5288 | // require the a notification. |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 5289 | klass->SetClinitThreadId(self->GetTid()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5290 | mirror::Class::SetStatus(klass, ClassStatus::kInitializing, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5291 | |
Mathieu Chartier | 2336954 | 2020-03-04 08:24:11 -0800 | [diff] [blame] | 5292 | t0 = stats_enabled ? NanoTime() : 0u; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5293 | } |
| 5294 | |
Andreas Gampe | af86470 | 2019-07-23 14:05:35 -0700 | [diff] [blame] | 5295 | uint64_t t_sub = 0; |
| 5296 | |
Brian Carlstrom | 6d3f72c | 2013-08-21 18:06:34 -0700 | [diff] [blame] | 5297 | // Initialize super classes, must be done while initializing for the JLS. |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5298 | if (!klass->IsInterface() && klass->HasSuperClass()) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5299 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5300 | if (!super_class->IsInitialized()) { |
| 5301 | CHECK(!super_class->IsInterface()); |
| 5302 | CHECK(can_init_parents); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5303 | StackHandleScope<1> hs(self); |
| 5304 | Handle<mirror::Class> handle_scope_super(hs.NewHandle(super_class)); |
Mathieu Chartier | 2336954 | 2020-03-04 08:24:11 -0800 | [diff] [blame] | 5305 | uint64_t super_t0 = stats_enabled ? NanoTime() : 0u; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5306 | bool super_initialized = InitializeClass(self, handle_scope_super, can_init_statics, true); |
Mathieu Chartier | 2336954 | 2020-03-04 08:24:11 -0800 | [diff] [blame] | 5307 | uint64_t super_t1 = stats_enabled ? NanoTime() : 0u; |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5308 | if (!super_initialized) { |
| 5309 | // The super class was verified ahead of entering initializing, we should only be here if |
| 5310 | // the super class became erroneous due to initialization. |
Chang Xing | adbb91c | 2017-07-17 11:23:55 -0700 | [diff] [blame] | 5311 | // For the case of aot compiler, the super class might also be initializing but we don't |
| 5312 | // want to process circular dependencies in pre-compile. |
| 5313 | CHECK(self->IsExceptionPending()) |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 5314 | << "Super class initialization failed for " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5315 | << handle_scope_super->PrettyDescriptor() |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5316 | << " that has unexpected status " << handle_scope_super->GetStatus() |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5317 | << "\nPending exception:\n" |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 5318 | << (self->GetException() != nullptr ? self->GetException()->Dump() : ""); |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 5319 | ObjectLock<mirror::Class> lock(self, klass); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5320 | // Initialization failed because the super-class is erroneous. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5321 | mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5322 | return false; |
| 5323 | } |
Andreas Gampe | af86470 | 2019-07-23 14:05:35 -0700 | [diff] [blame] | 5324 | t_sub = super_t1 - super_t0; |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 5325 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5326 | } |
| 5327 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5328 | if (!klass->IsInterface()) { |
| 5329 | // Initialize interfaces with default methods for the JLS. |
| 5330 | size_t num_direct_interfaces = klass->NumDirectInterfaces(); |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 5331 | // Only setup the (expensive) handle scope if we actually need to. |
| 5332 | if (UNLIKELY(num_direct_interfaces > 0)) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5333 | StackHandleScope<1> hs_iface(self); |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 5334 | MutableHandle<mirror::Class> handle_scope_iface(hs_iface.NewHandle<mirror::Class>(nullptr)); |
| 5335 | for (size_t i = 0; i < num_direct_interfaces; i++) { |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 5336 | handle_scope_iface.Assign(mirror::Class::GetDirectInterface(self, klass.Get(), i)); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 5337 | CHECK(handle_scope_iface != nullptr) << klass->PrettyDescriptor() << " iface #" << i; |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 5338 | CHECK(handle_scope_iface->IsInterface()); |
| 5339 | if (handle_scope_iface->HasBeenRecursivelyInitialized()) { |
| 5340 | // We have already done this for this interface. Skip it. |
| 5341 | continue; |
| 5342 | } |
| 5343 | // We cannot just call initialize class directly because we need to ensure that ALL |
| 5344 | // interfaces with default methods are initialized. Non-default interface initialization |
| 5345 | // will not affect other non-default super-interfaces. |
Mathieu Chartier | 2336954 | 2020-03-04 08:24:11 -0800 | [diff] [blame] | 5346 | // This is not very precise, misses all walking. |
| 5347 | uint64_t inf_t0 = stats_enabled ? NanoTime() : 0u; |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 5348 | bool iface_initialized = InitializeDefaultInterfaceRecursive(self, |
| 5349 | handle_scope_iface, |
| 5350 | can_init_statics, |
| 5351 | can_init_parents); |
Mathieu Chartier | 2336954 | 2020-03-04 08:24:11 -0800 | [diff] [blame] | 5352 | uint64_t inf_t1 = stats_enabled ? NanoTime() : 0u; |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 5353 | if (!iface_initialized) { |
| 5354 | ObjectLock<mirror::Class> lock(self, klass); |
| 5355 | // Initialization failed because one of our interfaces with default methods is erroneous. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5356 | mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self); |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 5357 | return false; |
| 5358 | } |
Andreas Gampe | af86470 | 2019-07-23 14:05:35 -0700 | [diff] [blame] | 5359 | t_sub += inf_t1 - inf_t0; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5360 | } |
| 5361 | } |
| 5362 | } |
| 5363 | |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 5364 | const size_t num_static_fields = klass->NumStaticFields(); |
| 5365 | if (num_static_fields > 0) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 5366 | const dex::ClassDef* dex_class_def = klass->GetClassDef(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5367 | CHECK(dex_class_def != nullptr); |
Hiroshi Yamauchi | 67ef46a | 2014-08-21 15:59:43 -0700 | [diff] [blame] | 5368 | StackHandleScope<3> hs(self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5369 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader())); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 5370 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache())); |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 5371 | |
| 5372 | // Eagerly fill in static fields so that the we don't have to do as many expensive |
| 5373 | // Class::FindStaticField in ResolveField. |
| 5374 | for (size_t i = 0; i < num_static_fields; ++i) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 5375 | ArtField* field = klass->GetStaticField(i); |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 5376 | const uint32_t field_idx = field->GetDexFieldIndex(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 5377 | ArtField* resolved_field = dex_cache->GetResolvedField(field_idx, image_pointer_size_); |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 5378 | if (resolved_field == nullptr) { |
David Brazdil | 1ab0fa8 | 2018-05-04 11:28:03 +0100 | [diff] [blame] | 5379 | // 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] | 5380 | DCHECK(!hiddenapi::ShouldDenyAccessToMember( |
| 5381 | field, |
| 5382 | hiddenapi::AccessContext(class_loader.Get(), dex_cache.Get()), |
| 5383 | hiddenapi::AccessMethod::kNone)); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 5384 | dex_cache->SetResolvedField(field_idx, field, image_pointer_size_); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5385 | } else { |
| 5386 | DCHECK_EQ(field, resolved_field); |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 5387 | } |
| 5388 | } |
| 5389 | |
Vladimir Marko | e11dd50 | 2017-12-08 14:09:45 +0000 | [diff] [blame] | 5390 | annotations::RuntimeEncodedStaticFieldValueIterator value_it(dex_cache, |
| 5391 | class_loader, |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 5392 | this, |
| 5393 | *dex_class_def); |
Vladimir Marko | e11dd50 | 2017-12-08 14:09:45 +0000 | [diff] [blame] | 5394 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 5395 | |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 5396 | if (value_it.HasNext()) { |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 5397 | ClassAccessor accessor(dex_file, *dex_class_def); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5398 | CHECK(can_init_statics); |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 5399 | for (const ClassAccessor::Field& field : accessor.GetStaticFields()) { |
| 5400 | if (!value_it.HasNext()) { |
| 5401 | break; |
| 5402 | } |
| 5403 | ArtField* art_field = ResolveField(field.GetIndex(), |
| 5404 | dex_cache, |
| 5405 | class_loader, |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 5406 | /* is_static= */ true); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 5407 | if (Runtime::Current()->IsActiveTransaction()) { |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 5408 | value_it.ReadValueToField<true>(art_field); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 5409 | } else { |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 5410 | value_it.ReadValueToField<false>(art_field); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 5411 | } |
Mathieu Chartier | da595be | 2016-08-10 13:57:39 -0700 | [diff] [blame] | 5412 | if (self->IsExceptionPending()) { |
| 5413 | break; |
| 5414 | } |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 5415 | value_it.Next(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5416 | } |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 5417 | DCHECK(self->IsExceptionPending() || !value_it.HasNext()); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5418 | } |
| 5419 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5420 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5421 | |
Mathieu Chartier | da595be | 2016-08-10 13:57:39 -0700 | [diff] [blame] | 5422 | if (!self->IsExceptionPending()) { |
| 5423 | ArtMethod* clinit = klass->FindClassInitializer(image_pointer_size_); |
| 5424 | if (clinit != nullptr) { |
| 5425 | CHECK(can_init_statics); |
| 5426 | JValue result; |
| 5427 | clinit->Invoke(self, nullptr, 0, &result, "V"); |
| 5428 | } |
| 5429 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5430 | self->AllowThreadSuspension(); |
Mathieu Chartier | 2336954 | 2020-03-04 08:24:11 -0800 | [diff] [blame] | 5431 | uint64_t t1 = stats_enabled ? NanoTime() : 0u; |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 5432 | |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 5433 | VisiblyInitializedCallback* callback = nullptr; |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 5434 | bool success = true; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5435 | { |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 5436 | ObjectLock<mirror::Class> lock(self, klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5437 | |
| 5438 | if (self->IsExceptionPending()) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 5439 | WrapExceptionInInitializer(klass); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5440 | mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self); |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 5441 | success = false; |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 5442 | } else if (Runtime::Current()->IsTransactionAborted()) { |
| 5443 | // The exception thrown when the transaction aborted has been caught and cleared |
| 5444 | // so we need to throw it again now. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5445 | VLOG(compiler) << "Return from class initializer of " |
| 5446 | << mirror::Class::PrettyDescriptor(klass.Get()) |
Sebastien Hertz | bd9cf9f | 2015-03-03 12:16:13 +0100 | [diff] [blame] | 5447 | << " without exception while transaction was aborted: re-throw it now."; |
Mathieu Chartier | 2336954 | 2020-03-04 08:24:11 -0800 | [diff] [blame] | 5448 | runtime->ThrowTransactionAbortError(self); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5449 | mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self); |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 5450 | success = false; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5451 | } else { |
Mathieu Chartier | 2336954 | 2020-03-04 08:24:11 -0800 | [diff] [blame] | 5452 | if (stats_enabled) { |
| 5453 | RuntimeStats* global_stats = runtime->GetStats(); |
| 5454 | RuntimeStats* thread_stats = self->GetStats(); |
| 5455 | ++global_stats->class_init_count; |
| 5456 | ++thread_stats->class_init_count; |
| 5457 | global_stats->class_init_time_ns += (t1 - t0 - t_sub); |
| 5458 | thread_stats->class_init_time_ns += (t1 - t0 - t_sub); |
| 5459 | } |
Ian Rogers | e6bb3b2 | 2013-08-19 21:51:45 -0700 | [diff] [blame] | 5460 | // Set the class as initialized except if failed to initialize static fields. |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 5461 | callback = MarkClassInitialized(self, klass); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5462 | if (VLOG_IS_ON(class_linker)) { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 5463 | std::string temp; |
| 5464 | LOG(INFO) << "Initialized class " << klass->GetDescriptor(&temp) << " from " << |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 5465 | klass->GetLocation(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 5466 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5467 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5468 | } |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 5469 | if (callback != nullptr) { |
| 5470 | callback->MakeVisible(self); |
| 5471 | } |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 5472 | return success; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5473 | } |
| 5474 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5475 | // We recursively run down the tree of interfaces. We need to do this in the order they are declared |
| 5476 | // and perform the initialization only on those interfaces that contain default methods. |
| 5477 | bool ClassLinker::InitializeDefaultInterfaceRecursive(Thread* self, |
| 5478 | Handle<mirror::Class> iface, |
| 5479 | bool can_init_statics, |
| 5480 | bool can_init_parents) { |
| 5481 | CHECK(iface->IsInterface()); |
| 5482 | size_t num_direct_ifaces = iface->NumDirectInterfaces(); |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 5483 | // Only create the (expensive) handle scope if we need it. |
| 5484 | if (UNLIKELY(num_direct_ifaces > 0)) { |
| 5485 | StackHandleScope<1> hs(self); |
| 5486 | MutableHandle<mirror::Class> handle_super_iface(hs.NewHandle<mirror::Class>(nullptr)); |
| 5487 | // First we initialize all of iface's super-interfaces recursively. |
| 5488 | for (size_t i = 0; i < num_direct_ifaces; i++) { |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 5489 | ObjPtr<mirror::Class> super_iface = mirror::Class::GetDirectInterface(self, iface.Get(), i); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 5490 | CHECK(super_iface != nullptr) << iface->PrettyDescriptor() << " iface #" << i; |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 5491 | if (!super_iface->HasBeenRecursivelyInitialized()) { |
| 5492 | // Recursive step |
| 5493 | handle_super_iface.Assign(super_iface); |
| 5494 | if (!InitializeDefaultInterfaceRecursive(self, |
| 5495 | handle_super_iface, |
| 5496 | can_init_statics, |
| 5497 | can_init_parents)) { |
| 5498 | return false; |
| 5499 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5500 | } |
| 5501 | } |
| 5502 | } |
| 5503 | |
| 5504 | bool result = true; |
| 5505 | // Then we initialize 'iface' if it has default methods. We do not need to (and in fact must not) |
| 5506 | // initialize if we don't have default methods. |
| 5507 | if (iface->HasDefaultMethods()) { |
| 5508 | result = EnsureInitialized(self, iface, can_init_statics, can_init_parents); |
| 5509 | } |
| 5510 | |
| 5511 | // Mark that this interface has undergone recursive default interface initialization so we know we |
| 5512 | // can skip it on any later class initializations. We do this even if we are not a default |
| 5513 | // interface since we can still avoid the traversal. This is purely a performance optimization. |
| 5514 | if (result) { |
| 5515 | // TODO This should be done in a better way |
Andreas Gampe | 976b298 | 2018-03-02 17:54:22 -0800 | [diff] [blame] | 5516 | // Note: Use a try-lock to avoid blocking when someone else is holding the lock on this |
| 5517 | // interface. It is bad (Java) style, but not impossible. Marking the recursive |
| 5518 | // initialization is a performance optimization (to avoid another idempotent visit |
| 5519 | // for other implementing classes/interfaces), and can be revisited later. |
| 5520 | ObjectTryLock<mirror::Class> lock(self, iface); |
| 5521 | if (lock.Acquired()) { |
| 5522 | iface->SetRecursivelyInitialized(); |
| 5523 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5524 | } |
| 5525 | return result; |
| 5526 | } |
| 5527 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5528 | bool ClassLinker::WaitForInitializeClass(Handle<mirror::Class> klass, |
| 5529 | Thread* self, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5530 | ObjectLock<mirror::Class>& lock) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5531 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5532 | while (true) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 5533 | self->AssertNoPendingException(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5534 | CHECK(!klass->IsInitialized()); |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5535 | lock.WaitIgnoringInterrupts(); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5536 | |
| 5537 | // When we wake up, repeat the test for init-in-progress. If |
| 5538 | // there's an exception pending (only possible if |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 5539 | // we were not using WaitIgnoringInterrupts), bail out. |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5540 | if (self->IsExceptionPending()) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 5541 | WrapExceptionInInitializer(klass); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5542 | mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5543 | return false; |
| 5544 | } |
| 5545 | // Spurious wakeup? Go back to waiting. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5546 | if (klass->GetStatus() == ClassStatus::kInitializing) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5547 | continue; |
| 5548 | } |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5549 | if (klass->GetStatus() == ClassStatus::kVerified && |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 5550 | Runtime::Current()->IsAotCompiler()) { |
Ian Rogers | 3d1548d | 2012-09-24 14:08:03 -0700 | [diff] [blame] | 5551 | // Compile time initialization failed. |
| 5552 | return false; |
| 5553 | } |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5554 | if (klass->IsErroneous()) { |
| 5555 | // The caller wants an exception, but it was thrown in a |
| 5556 | // different thread. Synthesize one here. |
Brian Carlstrom | df14324 | 2011-10-10 18:05:34 -0700 | [diff] [blame] | 5557 | ThrowNoClassDefFoundError("<clinit> failed for class %s; see exception in other thread", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5558 | klass->PrettyDescriptor().c_str()); |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 5559 | VlogClassInitializationFailure(klass); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5560 | return false; |
| 5561 | } |
| 5562 | if (klass->IsInitialized()) { |
| 5563 | return true; |
| 5564 | } |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5565 | LOG(FATAL) << "Unexpected class status. " << klass->PrettyClass() << " is " |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 5566 | << klass->GetStatus(); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5567 | } |
Ian Rogers | 0714083 | 2014-09-30 15:43:59 -0700 | [diff] [blame] | 5568 | UNREACHABLE(); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5569 | } |
| 5570 | |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5571 | static void ThrowSignatureCheckResolveReturnTypeException(Handle<mirror::Class> klass, |
| 5572 | Handle<mirror::Class> super_klass, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5573 | ArtMethod* method, |
| 5574 | ArtMethod* m) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5575 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5576 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 5577 | DCHECK(!m->IsProxyMethod()); |
| 5578 | const DexFile* dex_file = m->GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 5579 | const dex::MethodId& method_id = dex_file->GetMethodId(m->GetDexMethodIndex()); |
| 5580 | const dex::ProtoId& proto_id = dex_file->GetMethodPrototype(method_id); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 5581 | dex::TypeIndex return_type_idx = proto_id.return_type_idx_; |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5582 | std::string return_type = dex_file->PrettyType(return_type_idx); |
| 5583 | std::string class_loader = mirror::Object::PrettyTypeOf(m->GetDeclaringClass()->GetClassLoader()); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5584 | ThrowWrappedLinkageError(klass.Get(), |
| 5585 | "While checking class %s method %s signature against %s %s: " |
| 5586 | "Failed to resolve return type %s with %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5587 | mirror::Class::PrettyDescriptor(klass.Get()).c_str(), |
| 5588 | ArtMethod::PrettyMethod(method).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5589 | super_klass->IsInterface() ? "interface" : "superclass", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5590 | mirror::Class::PrettyDescriptor(super_klass.Get()).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5591 | return_type.c_str(), class_loader.c_str()); |
| 5592 | } |
| 5593 | |
| 5594 | static void ThrowSignatureCheckResolveArgException(Handle<mirror::Class> klass, |
| 5595 | Handle<mirror::Class> super_klass, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5596 | ArtMethod* method, |
| 5597 | ArtMethod* m, |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5598 | uint32_t index, |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 5599 | dex::TypeIndex arg_type_idx) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5600 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5601 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 5602 | DCHECK(!m->IsProxyMethod()); |
| 5603 | const DexFile* dex_file = m->GetDexFile(); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5604 | std::string arg_type = dex_file->PrettyType(arg_type_idx); |
| 5605 | std::string class_loader = mirror::Object::PrettyTypeOf(m->GetDeclaringClass()->GetClassLoader()); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5606 | ThrowWrappedLinkageError(klass.Get(), |
| 5607 | "While checking class %s method %s signature against %s %s: " |
| 5608 | "Failed to resolve arg %u type %s with %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5609 | mirror::Class::PrettyDescriptor(klass.Get()).c_str(), |
| 5610 | ArtMethod::PrettyMethod(method).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5611 | super_klass->IsInterface() ? "interface" : "superclass", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5612 | mirror::Class::PrettyDescriptor(super_klass.Get()).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5613 | index, arg_type.c_str(), class_loader.c_str()); |
| 5614 | } |
| 5615 | |
| 5616 | static void ThrowSignatureMismatch(Handle<mirror::Class> klass, |
| 5617 | Handle<mirror::Class> super_klass, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5618 | ArtMethod* method, |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5619 | const std::string& error_msg) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5620 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5621 | ThrowLinkageError(klass.Get(), |
| 5622 | "Class %s method %s resolves differently in %s %s: %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5623 | mirror::Class::PrettyDescriptor(klass.Get()).c_str(), |
| 5624 | ArtMethod::PrettyMethod(method).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5625 | super_klass->IsInterface() ? "interface" : "superclass", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5626 | mirror::Class::PrettyDescriptor(super_klass.Get()).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5627 | error_msg.c_str()); |
| 5628 | } |
| 5629 | |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5630 | static bool HasSameSignatureWithDifferentClassLoaders(Thread* self, |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5631 | Handle<mirror::Class> klass, |
| 5632 | Handle<mirror::Class> super_klass, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5633 | ArtMethod* method1, |
| 5634 | ArtMethod* method2) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5635 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5636 | { |
| 5637 | StackHandleScope<1> hs(self); |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 5638 | Handle<mirror::Class> return_type(hs.NewHandle(method1->ResolveReturnType())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 5639 | if (UNLIKELY(return_type == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5640 | ThrowSignatureCheckResolveReturnTypeException(klass, super_klass, method1, method1); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5641 | return false; |
| 5642 | } |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 5643 | ObjPtr<mirror::Class> other_return_type = method2->ResolveReturnType(); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5644 | if (UNLIKELY(other_return_type == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5645 | ThrowSignatureCheckResolveReturnTypeException(klass, super_klass, method1, method2); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5646 | return false; |
| 5647 | } |
Vladimir Marko | 862f43c | 2015-02-10 18:22:57 +0000 | [diff] [blame] | 5648 | if (UNLIKELY(other_return_type != return_type.Get())) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5649 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 5650 | StringPrintf("Return types mismatch: %s(%p) vs %s(%p)", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5651 | return_type->PrettyClassAndClassLoader().c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5652 | return_type.Get(), |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5653 | other_return_type->PrettyClassAndClassLoader().c_str(), |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5654 | other_return_type.Ptr())); |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5655 | return false; |
| 5656 | } |
| 5657 | } |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 5658 | const dex::TypeList* types1 = method1->GetParameterTypeList(); |
| 5659 | const dex::TypeList* types2 = method2->GetParameterTypeList(); |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5660 | if (types1 == nullptr) { |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5661 | if (types2 != nullptr && types2->Size() != 0) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5662 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 5663 | StringPrintf("Type list mismatch with %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5664 | method2->PrettyMethod(true).c_str())); |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5665 | return false; |
| 5666 | } |
| 5667 | return true; |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5668 | } else if (UNLIKELY(types2 == nullptr)) { |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5669 | if (types1->Size() != 0) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5670 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 5671 | StringPrintf("Type list mismatch with %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5672 | method2->PrettyMethod(true).c_str())); |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5673 | return false; |
| 5674 | } |
| 5675 | return true; |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5676 | } |
| 5677 | uint32_t num_types = types1->Size(); |
| 5678 | if (UNLIKELY(num_types != types2->Size())) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5679 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 5680 | StringPrintf("Type list mismatch with %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5681 | method2->PrettyMethod(true).c_str())); |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5682 | return false; |
| 5683 | } |
| 5684 | for (uint32_t i = 0; i < num_types; ++i) { |
Vladimir Marko | 862f43c | 2015-02-10 18:22:57 +0000 | [diff] [blame] | 5685 | StackHandleScope<1> hs(self); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 5686 | dex::TypeIndex param_type_idx = types1->GetTypeItem(i).type_idx_; |
Vladimir Marko | 862f43c | 2015-02-10 18:22:57 +0000 | [diff] [blame] | 5687 | Handle<mirror::Class> param_type(hs.NewHandle( |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 5688 | method1->ResolveClassFromTypeIndex(param_type_idx))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 5689 | if (UNLIKELY(param_type == nullptr)) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5690 | ThrowSignatureCheckResolveArgException(klass, super_klass, method1, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5691 | method1, i, param_type_idx); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5692 | return false; |
| 5693 | } |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 5694 | dex::TypeIndex other_param_type_idx = types2->GetTypeItem(i).type_idx_; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5695 | ObjPtr<mirror::Class> other_param_type = |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 5696 | method2->ResolveClassFromTypeIndex(other_param_type_idx); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5697 | if (UNLIKELY(other_param_type == nullptr)) { |
| 5698 | ThrowSignatureCheckResolveArgException(klass, super_klass, method1, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5699 | method2, i, other_param_type_idx); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5700 | return false; |
| 5701 | } |
Vladimir Marko | 862f43c | 2015-02-10 18:22:57 +0000 | [diff] [blame] | 5702 | if (UNLIKELY(param_type.Get() != other_param_type)) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5703 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 5704 | StringPrintf("Parameter %u type mismatch: %s(%p) vs %s(%p)", |
| 5705 | i, |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5706 | param_type->PrettyClassAndClassLoader().c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5707 | param_type.Get(), |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5708 | other_param_type->PrettyClassAndClassLoader().c_str(), |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5709 | other_param_type.Ptr())); |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5710 | return false; |
| 5711 | } |
| 5712 | } |
| 5713 | return true; |
| 5714 | } |
| 5715 | |
| 5716 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5717 | bool ClassLinker::ValidateSuperClassDescriptors(Handle<mirror::Class> klass) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5718 | if (klass->IsInterface()) { |
| 5719 | return true; |
| 5720 | } |
Ian Rogers | 151f221 | 2014-05-06 11:27:27 -0700 | [diff] [blame] | 5721 | // Begin with the methods local to the superclass. |
Ian Rogers | ded66a0 | 2014-10-28 18:12:55 -0700 | [diff] [blame] | 5722 | Thread* self = Thread::Current(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5723 | StackHandleScope<1> hs(self); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5724 | MutableHandle<mirror::Class> super_klass(hs.NewHandle<mirror::Class>(nullptr)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5725 | if (klass->HasSuperClass() && |
| 5726 | klass->GetClassLoader() != klass->GetSuperClass()->GetClassLoader()) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5727 | super_klass.Assign(klass->GetSuperClass()); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 5728 | for (int i = klass->GetSuperClass()->GetVTableLength() - 1; i >= 0; --i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5729 | auto* m = klass->GetVTableEntry(i, image_pointer_size_); |
| 5730 | auto* super_m = klass->GetSuperClass()->GetVTableEntry(i, image_pointer_size_); |
| 5731 | if (m != super_m) { |
Vladimir Marko | 942fd31 | 2017-01-16 20:52:19 +0000 | [diff] [blame] | 5732 | if (UNLIKELY(!HasSameSignatureWithDifferentClassLoaders(self, |
| 5733 | klass, |
| 5734 | super_klass, |
| 5735 | m, |
| 5736 | super_m))) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5737 | self->AssertPendingException(); |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5738 | return false; |
| 5739 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5740 | } |
| 5741 | } |
| 5742 | } |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 5743 | for (int32_t i = 0; i < klass->GetIfTableCount(); ++i) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5744 | super_klass.Assign(klass->GetIfTable()->GetInterface(i)); |
| 5745 | if (klass->GetClassLoader() != super_klass->GetClassLoader()) { |
| 5746 | uint32_t num_methods = super_klass->NumVirtualMethods(); |
Ian Rogers | 151f221 | 2014-05-06 11:27:27 -0700 | [diff] [blame] | 5747 | for (uint32_t j = 0; j < num_methods; ++j) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5748 | auto* m = klass->GetIfTable()->GetMethodArray(i)->GetElementPtrSize<ArtMethod*>( |
| 5749 | j, image_pointer_size_); |
| 5750 | auto* super_m = super_klass->GetVirtualMethod(j, image_pointer_size_); |
| 5751 | if (m != super_m) { |
Vladimir Marko | 942fd31 | 2017-01-16 20:52:19 +0000 | [diff] [blame] | 5752 | if (UNLIKELY(!HasSameSignatureWithDifferentClassLoaders(self, |
| 5753 | klass, |
| 5754 | super_klass, |
| 5755 | m, |
| 5756 | super_m))) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5757 | self->AssertPendingException(); |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5758 | return false; |
| 5759 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5760 | } |
| 5761 | } |
| 5762 | } |
| 5763 | } |
| 5764 | return true; |
| 5765 | } |
| 5766 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5767 | bool ClassLinker::EnsureInitialized(Thread* self, |
| 5768 | Handle<mirror::Class> c, |
| 5769 | bool can_init_fields, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5770 | bool can_init_parents) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 5771 | DCHECK(c != nullptr); |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 5772 | |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 5773 | if (c->IsInitialized()) { |
Vladimir Marko | 8e11065 | 2019-07-30 10:14:41 +0100 | [diff] [blame] | 5774 | // If we've seen an initialized but not visibly initialized class |
| 5775 | // many times, request visible initialization. |
| 5776 | if (kRuntimeISA == InstructionSet::kX86 || kRuntimeISA == InstructionSet::kX86_64) { |
| 5777 | // Thanks to the x86 memory model classes skip the initialized status. |
| 5778 | DCHECK(c->IsVisiblyInitialized()); |
| 5779 | } else if (UNLIKELY(!c->IsVisiblyInitialized())) { |
| 5780 | if (self->IncrementMakeVisiblyInitializedCounter()) { |
| 5781 | MakeInitializedClassesVisiblyInitialized(self, /*wait=*/ false); |
| 5782 | } |
| 5783 | } |
Andreas Gampe | 5b20b35 | 2018-10-11 19:03:20 -0700 | [diff] [blame] | 5784 | DCHECK(c->WasVerificationAttempted()) << c->PrettyClassAndClassLoader(); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 5785 | return true; |
| 5786 | } |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 5787 | // SubtypeCheckInfo::Initialized must happen-before any new-instance for that type. |
| 5788 | // |
| 5789 | // Ensure the bitstring is initialized before any of the class initialization |
| 5790 | // logic occurs. Once a class initializer starts running, objects can |
| 5791 | // escape into the heap and use the subtype checking code. |
| 5792 | // |
| 5793 | // Note: A class whose SubtypeCheckInfo is at least Initialized means it |
| 5794 | // can be used as a source for the IsSubClass check, and that all ancestors |
| 5795 | // of the class are Assigned (can be used as a target for IsSubClass check) |
| 5796 | // or Overflowed (can be used as a source for IsSubClass check). |
Vladimir Marko | 305c38b | 2018-02-14 11:50:07 +0000 | [diff] [blame] | 5797 | if (kBitstringSubtypeCheckEnabled) { |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 5798 | MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_); |
Vladimir Marko | 38b8b25 | 2018-01-02 19:07:06 +0000 | [diff] [blame] | 5799 | SubtypeCheck<ObjPtr<mirror::Class>>::EnsureInitialized(c.Get()); |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 5800 | // TODO: Avoid taking subtype_check_lock_ if SubtypeCheck is already initialized. |
| 5801 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5802 | const bool success = InitializeClass(self, c, can_init_fields, can_init_parents); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 5803 | if (!success) { |
| 5804 | if (can_init_fields && can_init_parents) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5805 | CHECK(self->IsExceptionPending()) << c->PrettyClass(); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 5806 | } |
| 5807 | } else { |
| 5808 | self->AssertNoPendingException(); |
Ian Rogers | 595799e | 2012-01-11 17:32:51 -0800 | [diff] [blame] | 5809 | } |
| 5810 | return success; |
Elliott Hughes | f4c21c9 | 2011-08-19 17:31:31 -0700 | [diff] [blame] | 5811 | } |
| 5812 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5813 | void ClassLinker::FixupTemporaryDeclaringClass(ObjPtr<mirror::Class> temp_class, |
| 5814 | ObjPtr<mirror::Class> new_class) { |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 5815 | DCHECK_EQ(temp_class->NumInstanceFields(), 0u); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 5816 | for (ArtField& field : new_class->GetIFields()) { |
| 5817 | if (field.GetDeclaringClass() == temp_class) { |
| 5818 | field.SetDeclaringClass(new_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5819 | } |
| 5820 | } |
| 5821 | |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 5822 | DCHECK_EQ(temp_class->NumStaticFields(), 0u); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 5823 | for (ArtField& field : new_class->GetSFields()) { |
| 5824 | if (field.GetDeclaringClass() == temp_class) { |
| 5825 | field.SetDeclaringClass(new_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5826 | } |
| 5827 | } |
| 5828 | |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 5829 | DCHECK_EQ(temp_class->NumDirectMethods(), 0u); |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 5830 | DCHECK_EQ(temp_class->NumVirtualMethods(), 0u); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 5831 | for (auto& method : new_class->GetMethods(image_pointer_size_)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5832 | if (method.GetDeclaringClass() == temp_class) { |
| 5833 | method.SetDeclaringClass(new_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5834 | } |
| 5835 | } |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 5836 | |
| 5837 | // Make sure the remembered set and mod-union tables know that we updated some of the native |
| 5838 | // roots. |
Mathieu Chartier | 88ea61e | 2018-06-20 17:45:41 -0700 | [diff] [blame] | 5839 | WriteBarrier::ForEveryFieldWrite(new_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5840 | } |
| 5841 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5842 | void ClassLinker::RegisterClassLoader(ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | 5b83050 | 2016-03-02 10:30:23 -0800 | [diff] [blame] | 5843 | CHECK(class_loader->GetAllocator() == nullptr); |
| 5844 | CHECK(class_loader->GetClassTable() == nullptr); |
| 5845 | Thread* const self = Thread::Current(); |
| 5846 | ClassLoaderData data; |
Ian Rogers | 55256cb | 2017-12-21 17:07:11 -0800 | [diff] [blame] | 5847 | data.weak_root = self->GetJniEnv()->GetVm()->AddWeakGlobalRef(self, class_loader); |
Mathieu Chartier | 5b83050 | 2016-03-02 10:30:23 -0800 | [diff] [blame] | 5848 | // Create and set the class table. |
| 5849 | data.class_table = new ClassTable; |
| 5850 | class_loader->SetClassTable(data.class_table); |
| 5851 | // Create and set the linear allocator. |
| 5852 | data.allocator = Runtime::Current()->CreateLinearAlloc(); |
| 5853 | class_loader->SetAllocator(data.allocator); |
| 5854 | // Add to the list so that we know to free the data later. |
| 5855 | class_loaders_.push_back(data); |
| 5856 | } |
| 5857 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5858 | ClassTable* ClassLinker::InsertClassTableForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 5859 | if (class_loader == nullptr) { |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 5860 | return boot_class_table_.get(); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5861 | } |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 5862 | ClassTable* class_table = class_loader->GetClassTable(); |
| 5863 | if (class_table == nullptr) { |
Mathieu Chartier | 5b83050 | 2016-03-02 10:30:23 -0800 | [diff] [blame] | 5864 | RegisterClassLoader(class_loader); |
| 5865 | class_table = class_loader->GetClassTable(); |
| 5866 | DCHECK(class_table != nullptr); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 5867 | } |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5868 | return class_table; |
| 5869 | } |
| 5870 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5871 | ClassTable* ClassLinker::ClassTableForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) { |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 5872 | return class_loader == nullptr ? boot_class_table_.get() : class_loader->GetClassTable(); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5873 | } |
| 5874 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5875 | static ImTable* FindSuperImt(ObjPtr<mirror::Class> klass, PointerSize pointer_size) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5876 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5877 | while (klass->HasSuperClass()) { |
| 5878 | klass = klass->GetSuperClass(); |
| 5879 | if (klass->ShouldHaveImt()) { |
| 5880 | return klass->GetImt(pointer_size); |
| 5881 | } |
| 5882 | } |
| 5883 | return nullptr; |
| 5884 | } |
| 5885 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5886 | bool ClassLinker::LinkClass(Thread* self, |
| 5887 | const char* descriptor, |
| 5888 | Handle<mirror::Class> klass, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5889 | Handle<mirror::ObjectArray<mirror::Class>> interfaces, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5890 | MutableHandle<mirror::Class>* h_new_class_out) { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5891 | CHECK_EQ(ClassStatus::kLoaded, klass->GetStatus()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5892 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5893 | if (!LinkSuperClass(klass)) { |
| 5894 | return false; |
| 5895 | } |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5896 | ArtMethod* imt_data[ImTable::kSize]; |
| 5897 | // If there are any new conflicts compared to super class. |
| 5898 | bool new_conflict = false; |
Nicolas Geoffray | 918dcea | 2017-07-21 07:58:14 +0000 | [diff] [blame] | 5899 | std::fill_n(imt_data, arraysize(imt_data), Runtime::Current()->GetImtUnimplementedMethod()); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5900 | if (!LinkMethods(self, klass, interfaces, &new_conflict, imt_data)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5901 | return false; |
| 5902 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5903 | if (!LinkInstanceFields(self, klass)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5904 | return false; |
| 5905 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5906 | size_t class_size; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5907 | if (!LinkStaticFields(self, klass, &class_size)) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 5908 | return false; |
| 5909 | } |
| 5910 | CreateReferenceInstanceOffsets(klass); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5911 | CHECK_EQ(ClassStatus::kLoaded, klass->GetStatus()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5912 | |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5913 | ImTable* imt = nullptr; |
| 5914 | if (klass->ShouldHaveImt()) { |
| 5915 | // If there are any new conflicts compared to the super class we can not make a copy. There |
| 5916 | // can be cases where both will have a conflict method at the same slot without having the same |
| 5917 | // set of conflicts. In this case, we can not share the IMT since the conflict table slow path |
| 5918 | // will possibly create a table that is incorrect for either of the classes. |
| 5919 | // Same IMT with new_conflict does not happen very often. |
| 5920 | if (!new_conflict) { |
| 5921 | ImTable* super_imt = FindSuperImt(klass.Get(), image_pointer_size_); |
| 5922 | if (super_imt != nullptr) { |
| 5923 | bool imt_equals = true; |
| 5924 | for (size_t i = 0; i < ImTable::kSize && imt_equals; ++i) { |
| 5925 | imt_equals = imt_equals && (super_imt->Get(i, image_pointer_size_) == imt_data[i]); |
| 5926 | } |
| 5927 | if (imt_equals) { |
| 5928 | imt = super_imt; |
| 5929 | } |
| 5930 | } |
| 5931 | } |
| 5932 | if (imt == nullptr) { |
| 5933 | LinearAlloc* allocator = GetAllocatorForClassLoader(klass->GetClassLoader()); |
| 5934 | imt = reinterpret_cast<ImTable*>( |
| 5935 | allocator->Alloc(self, ImTable::SizeInBytes(image_pointer_size_))); |
| 5936 | if (imt == nullptr) { |
| 5937 | return false; |
| 5938 | } |
| 5939 | imt->Populate(imt_data, image_pointer_size_); |
| 5940 | } |
| 5941 | } |
| 5942 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5943 | if (!klass->IsTemp() || (!init_done_ && klass->GetClassSize() == class_size)) { |
| 5944 | // We don't need to retire this class as it has no embedded tables or it was created the |
| 5945 | // correct size during class linker initialization. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5946 | CHECK_EQ(klass->GetClassSize(), class_size) << klass->PrettyDescriptor(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5947 | |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5948 | if (klass->ShouldHaveEmbeddedVTable()) { |
| 5949 | klass->PopulateEmbeddedVTable(image_pointer_size_); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5950 | } |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5951 | if (klass->ShouldHaveImt()) { |
| 5952 | klass->SetImt(imt, image_pointer_size_); |
| 5953 | } |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 5954 | |
| 5955 | // Update CHA info based on whether we override methods. |
| 5956 | // Have to do this before setting the class as resolved which allows |
| 5957 | // instantiation of klass. |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 5958 | if (LIKELY(descriptor != nullptr) && cha_ != nullptr) { |
Andreas Gampe | c1ac9ee | 2017-07-24 22:35:49 -0700 | [diff] [blame] | 5959 | cha_->UpdateAfterLoadingOf(klass); |
| 5960 | } |
Nicolas Geoffray | 918dcea | 2017-07-21 07:58:14 +0000 | [diff] [blame] | 5961 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5962 | // This will notify waiters on klass that saw the not yet resolved |
| 5963 | // class in the class_table_ during EnsureResolved. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5964 | mirror::Class::SetStatus(klass, ClassStatus::kResolved, self); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 5965 | h_new_class_out->Assign(klass.Get()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5966 | } else { |
| 5967 | CHECK(!klass->IsResolved()); |
| 5968 | // Retire the temporary class and create the correctly sized resolved class. |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 5969 | StackHandleScope<1> hs(self); |
Vladimir Marko | 3068d58 | 2019-05-28 16:39:29 +0100 | [diff] [blame] | 5970 | Handle<mirror::Class> h_new_class = |
| 5971 | 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] | 5972 | // Set arrays to null since we don't want to have multiple classes with the same ArtField or |
| 5973 | // ArtMethod array pointers. If this occurs, it causes bugs in remembered sets since the GC |
| 5974 | // may not see any references to the target space and clean the card for a class if another |
| 5975 | // class had the same array pointer. |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 5976 | klass->SetMethodsPtrUnchecked(nullptr, 0, 0); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 5977 | klass->SetSFieldsPtrUnchecked(nullptr); |
| 5978 | klass->SetIFieldsPtrUnchecked(nullptr); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 5979 | if (UNLIKELY(h_new_class == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5980 | self->AssertPendingOOMException(); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5981 | mirror::Class::SetStatus(klass, ClassStatus::kErrorUnresolved, self); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5982 | return false; |
| 5983 | } |
| 5984 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 5985 | CHECK_EQ(h_new_class->GetClassSize(), class_size); |
| 5986 | ObjectLock<mirror::Class> lock(self, h_new_class); |
| 5987 | FixupTemporaryDeclaringClass(klass.Get(), h_new_class.Get()); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5988 | |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 5989 | if (LIKELY(descriptor != nullptr)) { |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 5990 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Vladimir Marko | 0984e48 | 2019-03-27 16:41:41 +0000 | [diff] [blame] | 5991 | const ObjPtr<mirror::ClassLoader> class_loader = h_new_class.Get()->GetClassLoader(); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5992 | ClassTable* const table = InsertClassTableForClassLoader(class_loader); |
Vladimir Marko | 0984e48 | 2019-03-27 16:41:41 +0000 | [diff] [blame] | 5993 | const ObjPtr<mirror::Class> existing = |
| 5994 | table->UpdateClass(descriptor, h_new_class.Get(), ComputeModifiedUtf8Hash(descriptor)); |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 5995 | if (class_loader != nullptr) { |
| 5996 | // We updated the class in the class table, perform the write barrier so that the GC knows |
| 5997 | // about the change. |
Mathieu Chartier | 88ea61e | 2018-06-20 17:45:41 -0700 | [diff] [blame] | 5998 | WriteBarrier::ForEveryFieldWrite(class_loader); |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 5999 | } |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 6000 | CHECK_EQ(existing, klass.Get()); |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 6001 | if (log_new_roots_) { |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 6002 | new_class_roots_.push_back(GcRoot<mirror::Class>(h_new_class.Get())); |
| 6003 | } |
| 6004 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 6005 | |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 6006 | // Update CHA info based on whether we override methods. |
| 6007 | // Have to do this before setting the class as resolved which allows |
| 6008 | // instantiation of klass. |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 6009 | if (LIKELY(descriptor != nullptr) && cha_ != nullptr) { |
Andreas Gampe | c1ac9ee | 2017-07-24 22:35:49 -0700 | [diff] [blame] | 6010 | cha_->UpdateAfterLoadingOf(h_new_class); |
| 6011 | } |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 6012 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 6013 | // This will notify waiters on temp class that saw the not yet resolved class in the |
| 6014 | // class_table_ during EnsureResolved. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 6015 | mirror::Class::SetStatus(klass, ClassStatus::kRetired, self); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 6016 | |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 6017 | CHECK_EQ(h_new_class->GetStatus(), ClassStatus::kResolving); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 6018 | // This will notify waiters on new_class that saw the not yet resolved |
| 6019 | // class in the class_table_ during EnsureResolved. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 6020 | mirror::Class::SetStatus(h_new_class, ClassStatus::kResolved, self); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 6021 | // Return the new class. |
| 6022 | h_new_class_out->Assign(h_new_class.Get()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 6023 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6024 | return true; |
| 6025 | } |
| 6026 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 6027 | bool ClassLinker::LoadSuperAndInterfaces(Handle<mirror::Class> klass, const DexFile& dex_file) { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 6028 | CHECK_EQ(ClassStatus::kIdx, klass->GetStatus()); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 6029 | const dex::ClassDef& class_def = dex_file.GetClassDef(klass->GetDexClassDefIndex()); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 6030 | dex::TypeIndex super_class_idx = class_def.superclass_idx_; |
| 6031 | if (super_class_idx.IsValid()) { |
Roland Levillain | 90328ac | 2016-05-18 12:25:38 +0100 | [diff] [blame] | 6032 | // Check that a class does not inherit from itself directly. |
| 6033 | // |
| 6034 | // TODO: This is a cheap check to detect the straightforward case |
| 6035 | // of a class extending itself (b/28685551), but we should do a |
| 6036 | // proper cycle detection on loaded classes, to detect all cases |
| 6037 | // of class circularity errors (b/28830038). |
| 6038 | if (super_class_idx == class_def.class_idx_) { |
| 6039 | ThrowClassCircularityError(klass.Get(), |
| 6040 | "Class %s extends itself", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6041 | klass->PrettyDescriptor().c_str()); |
Roland Levillain | 90328ac | 2016-05-18 12:25:38 +0100 | [diff] [blame] | 6042 | return false; |
| 6043 | } |
| 6044 | |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 6045 | ObjPtr<mirror::Class> super_class = ResolveType(super_class_idx, klass.Get()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 6046 | if (super_class == nullptr) { |
Brian Carlstrom | 65ca077 | 2011-09-24 16:03:08 -0700 | [diff] [blame] | 6047 | DCHECK(Thread::Current()->IsExceptionPending()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6048 | return false; |
| 6049 | } |
Ian Rogers | be125a9 | 2012-01-11 15:19:49 -0800 | [diff] [blame] | 6050 | // Verify |
| 6051 | if (!klass->CanAccess(super_class)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 6052 | ThrowIllegalAccessError(klass.Get(), "Class %s extended by class %s is inaccessible", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6053 | super_class->PrettyDescriptor().c_str(), |
| 6054 | klass->PrettyDescriptor().c_str()); |
Ian Rogers | be125a9 | 2012-01-11 15:19:49 -0800 | [diff] [blame] | 6055 | return false; |
| 6056 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 6057 | CHECK(super_class->IsResolved()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 6058 | klass->SetSuperClass(super_class); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6059 | } |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 6060 | const dex::TypeList* interfaces = dex_file.GetInterfacesList(class_def); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 6061 | if (interfaces != nullptr) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 6062 | for (size_t i = 0; i < interfaces->Size(); i++) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 6063 | dex::TypeIndex idx = interfaces->GetTypeItem(i).type_idx_; |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 6064 | ObjPtr<mirror::Class> interface = ResolveType(idx, klass.Get()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 6065 | if (interface == nullptr) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 6066 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 6067 | return false; |
| 6068 | } |
| 6069 | // Verify |
| 6070 | if (!klass->CanAccess(interface)) { |
| 6071 | // TODO: the RI seemed to ignore this in my testing. |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 6072 | ThrowIllegalAccessError(klass.Get(), |
| 6073 | "Interface %s implemented by class %s is inaccessible", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6074 | interface->PrettyDescriptor().c_str(), |
| 6075 | klass->PrettyDescriptor().c_str()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 6076 | return false; |
| 6077 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6078 | } |
| 6079 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 6080 | // Mark the class as loaded. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 6081 | mirror::Class::SetStatus(klass, ClassStatus::kLoaded, nullptr); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6082 | return true; |
| 6083 | } |
| 6084 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 6085 | bool ClassLinker::LinkSuperClass(Handle<mirror::Class> klass) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6086 | CHECK(!klass->IsPrimitive()); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6087 | ObjPtr<mirror::Class> super = klass->GetSuperClass(); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 6088 | ObjPtr<mirror::Class> object_class = GetClassRoot<mirror::Object>(this); |
| 6089 | if (klass.Get() == object_class) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 6090 | if (super != nullptr) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 6091 | ThrowClassFormatError(klass.Get(), "java.lang.Object must not have a superclass"); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6092 | return false; |
| 6093 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6094 | return true; |
| 6095 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 6096 | if (super == nullptr) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 6097 | ThrowLinkageError(klass.Get(), "No superclass defined for class %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6098 | klass->PrettyDescriptor().c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6099 | return false; |
| 6100 | } |
| 6101 | // Verify |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 6102 | if (klass->IsInterface() && super != object_class) { |
Vladimir Marko | 1fcae9f | 2017-11-28 14:14:19 +0000 | [diff] [blame] | 6103 | ThrowClassFormatError(klass.Get(), "Interfaces must have java.lang.Object as superclass"); |
| 6104 | return false; |
| 6105 | } |
Vladimir Marko | b43b2d8 | 2017-07-18 17:46:38 +0100 | [diff] [blame] | 6106 | if (super->IsFinal()) { |
| 6107 | ThrowVerifyError(klass.Get(), |
| 6108 | "Superclass %s of %s is declared final", |
| 6109 | super->PrettyDescriptor().c_str(), |
| 6110 | klass->PrettyDescriptor().c_str()); |
| 6111 | return false; |
| 6112 | } |
| 6113 | if (super->IsInterface()) { |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 6114 | ThrowIncompatibleClassChangeError(klass.Get(), |
Vladimir Marko | b43b2d8 | 2017-07-18 17:46:38 +0100 | [diff] [blame] | 6115 | "Superclass %s of %s is an interface", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6116 | super->PrettyDescriptor().c_str(), |
Vladimir Marko | b43b2d8 | 2017-07-18 17:46:38 +0100 | [diff] [blame] | 6117 | klass->PrettyDescriptor().c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6118 | return false; |
| 6119 | } |
| 6120 | if (!klass->CanAccess(super)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 6121 | ThrowIllegalAccessError(klass.Get(), "Superclass %s is inaccessible to class %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6122 | super->PrettyDescriptor().c_str(), |
| 6123 | klass->PrettyDescriptor().c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6124 | return false; |
| 6125 | } |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 6126 | |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 6127 | // Inherit kAccClassIsFinalizable from the superclass in case this |
| 6128 | // class doesn't override finalize. |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 6129 | if (super->IsFinalizable()) { |
| 6130 | klass->SetFinalizable(); |
| 6131 | } |
| 6132 | |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 6133 | // Inherit class loader flag form super class. |
| 6134 | if (super->IsClassLoaderClass()) { |
| 6135 | klass->SetClassLoaderClass(); |
| 6136 | } |
| 6137 | |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 6138 | // Inherit reference flags (if any) from the superclass. |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 6139 | uint32_t reference_flags = (super->GetClassFlags() & mirror::kClassFlagReference); |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 6140 | if (reference_flags != 0) { |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 6141 | CHECK_EQ(klass->GetClassFlags(), 0u); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 6142 | klass->SetClassFlags(klass->GetClassFlags() | reference_flags); |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 6143 | } |
Elliott Hughes | 72ee0ae | 2011-10-10 17:31:28 -0700 | [diff] [blame] | 6144 | // Disallow custom direct subclasses of java.lang.ref.Reference. |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 6145 | if (init_done_ && super == GetClassRoot<mirror::Reference>(this)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 6146 | ThrowLinkageError(klass.Get(), |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 6147 | "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] | 6148 | klass->PrettyDescriptor().c_str()); |
Elliott Hughes | 72ee0ae | 2011-10-10 17:31:28 -0700 | [diff] [blame] | 6149 | return false; |
| 6150 | } |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 6151 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 6152 | if (kIsDebugBuild) { |
| 6153 | // Ensure super classes are fully resolved prior to resolving fields.. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 6154 | while (super != nullptr) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 6155 | CHECK(super->IsResolved()); |
| 6156 | super = super->GetSuperClass(); |
| 6157 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 6158 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6159 | return true; |
| 6160 | } |
| 6161 | |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 6162 | // A wrapper class representing the result of a method translation used for linking methods and |
| 6163 | // updating superclass default methods. For each method in a classes vtable there are 4 states it |
| 6164 | // could be in: |
| 6165 | // 1) No translation is necessary. In this case there is no MethodTranslation object for it. This |
| 6166 | // is the standard case and is true when the method is not overridable by a default method, |
| 6167 | // the class defines a concrete implementation of the method, the default method implementation |
| 6168 | // remains the same, or an abstract method stayed abstract. |
| 6169 | // 2) The method must be translated to a different default method. We note this with |
| 6170 | // CreateTranslatedMethod. |
| 6171 | // 3) The method must be replaced with a conflict method. This happens when a superclass |
| 6172 | // implements an interface with a default method and this class implements an unrelated |
| 6173 | // interface that also defines that default method. We note this with CreateConflictingMethod. |
| 6174 | // 4) The method must be replaced with an abstract miranda method. This happens when a superclass |
| 6175 | // implements an interface with a default method and this class implements a subinterface of |
| 6176 | // the superclass's interface which declares the default method abstract. We note this with |
| 6177 | // CreateAbstractMethod. |
| 6178 | // |
| 6179 | // When a method translation is unnecessary (case #1), we don't put it into the |
| 6180 | // default_translation maps. So an instance of MethodTranslation must be in one of #2-#4. |
| 6181 | class ClassLinker::MethodTranslation { |
| 6182 | public: |
| 6183 | MethodTranslation() : translation_(nullptr), type_(Type::kInvalid) {} |
| 6184 | |
| 6185 | // This slot must become a default conflict method. |
| 6186 | static MethodTranslation CreateConflictingMethod() { |
| 6187 | return MethodTranslation(Type::kConflict, /*translation=*/nullptr); |
| 6188 | } |
| 6189 | |
| 6190 | // This slot must become an abstract method. |
| 6191 | static MethodTranslation CreateAbstractMethod() { |
| 6192 | return MethodTranslation(Type::kAbstract, /*translation=*/nullptr); |
| 6193 | } |
| 6194 | |
| 6195 | // Use the given method as the current value for this vtable slot during translation. |
| 6196 | static MethodTranslation CreateTranslatedMethod(ArtMethod* new_method) { |
| 6197 | return MethodTranslation(Type::kTranslation, new_method); |
| 6198 | } |
| 6199 | |
| 6200 | // Returns true if this is a method that must become a conflict method. |
| 6201 | bool IsInConflict() const { |
| 6202 | return type_ == Type::kConflict; |
| 6203 | } |
| 6204 | |
| 6205 | // Returns true if this is a method that must become an abstract method. |
| 6206 | bool IsAbstract() const { |
| 6207 | return type_ == Type::kAbstract; |
| 6208 | } |
| 6209 | |
| 6210 | // Returns true if this is a method that must become a different method. |
| 6211 | bool IsTranslation() const { |
| 6212 | return type_ == Type::kTranslation; |
| 6213 | } |
| 6214 | |
| 6215 | // Get the translated version of this method. |
| 6216 | ArtMethod* GetTranslation() const { |
| 6217 | DCHECK(IsTranslation()); |
| 6218 | DCHECK(translation_ != nullptr); |
| 6219 | return translation_; |
| 6220 | } |
| 6221 | |
| 6222 | private: |
| 6223 | enum class Type { |
| 6224 | kInvalid, |
| 6225 | kTranslation, |
| 6226 | kConflict, |
| 6227 | kAbstract, |
| 6228 | }; |
| 6229 | |
| 6230 | MethodTranslation(Type type, ArtMethod* translation) |
| 6231 | : translation_(translation), type_(type) {} |
| 6232 | |
| 6233 | ArtMethod* translation_; |
| 6234 | Type type_; |
| 6235 | }; |
| 6236 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 6237 | // Populate the class vtable and itable. Compute return type indices. |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 6238 | bool ClassLinker::LinkMethods(Thread* self, |
| 6239 | Handle<mirror::Class> klass, |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6240 | Handle<mirror::ObjectArray<mirror::Class>> interfaces, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6241 | bool* out_new_conflict, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 6242 | ArtMethod** out_imt) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 6243 | self->AllowThreadSuspension(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6244 | // A map from vtable indexes to the method they need to be updated to point to. Used because we |
| 6245 | // need to have default methods be in the virtuals array of each class but we don't set that up |
| 6246 | // until LinkInterfaceMethods. |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 6247 | constexpr size_t kBufferSize = 8; // Avoid malloc/free for a few translations. |
| 6248 | std::pair<size_t, ClassLinker::MethodTranslation> buffer[kBufferSize]; |
| 6249 | HashMap<size_t, ClassLinker::MethodTranslation> default_translations(buffer, kBufferSize); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6250 | // Link virtual methods then interface methods. |
| 6251 | // We set up the interface lookup table first because we need it to determine if we need to update |
| 6252 | // any vtable entries with new default method implementations. |
| 6253 | return SetupInterfaceLookupTable(self, klass, interfaces) |
| 6254 | && LinkVirtualMethods(self, klass, /*out*/ &default_translations) |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6255 | && LinkInterfaceMethods(self, klass, default_translations, out_new_conflict, out_imt); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6256 | } |
| 6257 | |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 6258 | // Comparator for name and signature of a method, used in finding overriding methods. Implementation |
| 6259 | // avoids the use of handles, if it didn't then rather than compare dex files we could compare dex |
| 6260 | // caches in the implementation below. |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 6261 | class MethodNameAndSignatureComparator final : public ValueObject { |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 6262 | public: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6263 | explicit MethodNameAndSignatureComparator(ArtMethod* method) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 6264 | REQUIRES_SHARED(Locks::mutator_lock_) : |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 6265 | dex_file_(method->GetDexFile()), mid_(&dex_file_->GetMethodId(method->GetDexMethodIndex())), |
| 6266 | name_(nullptr), name_len_(0) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6267 | DCHECK(!method->IsProxyMethod()) << method->PrettyMethod(); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6268 | } |
| 6269 | |
| 6270 | const char* GetName() { |
| 6271 | if (name_ == nullptr) { |
| 6272 | name_ = dex_file_->StringDataAndUtf16LengthByIdx(mid_->name_idx_, &name_len_); |
| 6273 | } |
| 6274 | return name_; |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 6275 | } |
| 6276 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6277 | bool HasSameNameAndSignature(ArtMethod* other) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 6278 | REQUIRES_SHARED(Locks::mutator_lock_) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6279 | DCHECK(!other->IsProxyMethod()) << other->PrettyMethod(); |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 6280 | const DexFile* other_dex_file = other->GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 6281 | const dex::MethodId& other_mid = other_dex_file->GetMethodId(other->GetDexMethodIndex()); |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 6282 | if (dex_file_ == other_dex_file) { |
| 6283 | return mid_->name_idx_ == other_mid.name_idx_ && mid_->proto_idx_ == other_mid.proto_idx_; |
| 6284 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6285 | GetName(); // Only used to make sure its calculated. |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 6286 | uint32_t other_name_len; |
| 6287 | const char* other_name = other_dex_file->StringDataAndUtf16LengthByIdx(other_mid.name_idx_, |
| 6288 | &other_name_len); |
| 6289 | if (name_len_ != other_name_len || strcmp(name_, other_name) != 0) { |
| 6290 | return false; |
| 6291 | } |
| 6292 | return dex_file_->GetMethodSignature(*mid_) == other_dex_file->GetMethodSignature(other_mid); |
| 6293 | } |
| 6294 | |
| 6295 | private: |
| 6296 | // Dex file for the method to compare against. |
| 6297 | const DexFile* const dex_file_; |
| 6298 | // MethodId for the method to compare against. |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 6299 | const dex::MethodId* const mid_; |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 6300 | // Lazily computed name from the dex file's strings. |
| 6301 | const char* name_; |
| 6302 | // Lazily computed name length. |
| 6303 | uint32_t name_len_; |
| 6304 | }; |
| 6305 | |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6306 | class LinkVirtualHashTable { |
| 6307 | public: |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 6308 | LinkVirtualHashTable(Handle<mirror::Class> klass, |
| 6309 | size_t hash_size, |
| 6310 | uint32_t* hash_table, |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 6311 | PointerSize image_pointer_size) |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 6312 | : klass_(klass), |
| 6313 | hash_size_(hash_size), |
| 6314 | hash_table_(hash_table), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6315 | image_pointer_size_(image_pointer_size) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6316 | std::fill(hash_table_, hash_table_ + hash_size_, invalid_index_); |
| 6317 | } |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 6318 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 6319 | void Add(uint32_t virtual_method_index) REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6320 | ArtMethod* local_method = klass_->GetVirtualMethodDuringLinking( |
| 6321 | virtual_method_index, image_pointer_size_); |
| 6322 | const char* name = local_method->GetInterfaceMethodIfProxy(image_pointer_size_)->GetName(); |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 6323 | uint32_t hash = ComputeModifiedUtf8Hash(name); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6324 | uint32_t index = hash % hash_size_; |
| 6325 | // Linear probe until we have an empty slot. |
| 6326 | while (hash_table_[index] != invalid_index_) { |
| 6327 | if (++index == hash_size_) { |
| 6328 | index = 0; |
| 6329 | } |
| 6330 | } |
| 6331 | hash_table_[index] = virtual_method_index; |
| 6332 | } |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 6333 | |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6334 | uint32_t FindAndRemove(MethodNameAndSignatureComparator* comparator) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 6335 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6336 | const char* name = comparator->GetName(); |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 6337 | uint32_t hash = ComputeModifiedUtf8Hash(name); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6338 | size_t index = hash % hash_size_; |
| 6339 | while (true) { |
| 6340 | const uint32_t value = hash_table_[index]; |
| 6341 | // Since linear probe makes continuous blocks, hitting an invalid index means we are done |
| 6342 | // the block and can safely assume not found. |
| 6343 | if (value == invalid_index_) { |
| 6344 | break; |
| 6345 | } |
| 6346 | if (value != removed_index_) { // This signifies not already overriden. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6347 | ArtMethod* virtual_method = |
| 6348 | klass_->GetVirtualMethodDuringLinking(value, image_pointer_size_); |
| 6349 | if (comparator->HasSameNameAndSignature( |
| 6350 | virtual_method->GetInterfaceMethodIfProxy(image_pointer_size_))) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6351 | hash_table_[index] = removed_index_; |
| 6352 | return value; |
| 6353 | } |
| 6354 | } |
| 6355 | if (++index == hash_size_) { |
| 6356 | index = 0; |
| 6357 | } |
| 6358 | } |
| 6359 | return GetNotFoundIndex(); |
| 6360 | } |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 6361 | |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6362 | static uint32_t GetNotFoundIndex() { |
| 6363 | return invalid_index_; |
| 6364 | } |
| 6365 | |
| 6366 | private: |
| 6367 | static const uint32_t invalid_index_; |
| 6368 | static const uint32_t removed_index_; |
| 6369 | |
| 6370 | Handle<mirror::Class> klass_; |
| 6371 | const size_t hash_size_; |
| 6372 | uint32_t* const hash_table_; |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 6373 | const PointerSize image_pointer_size_; |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6374 | }; |
| 6375 | |
| 6376 | const uint32_t LinkVirtualHashTable::invalid_index_ = std::numeric_limits<uint32_t>::max(); |
| 6377 | const uint32_t LinkVirtualHashTable::removed_index_ = std::numeric_limits<uint32_t>::max() - 1; |
| 6378 | |
Stephen Hines | 1ddd913 | 2017-02-08 01:51:18 -0800 | [diff] [blame] | 6379 | bool ClassLinker::LinkVirtualMethods( |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6380 | Thread* self, |
| 6381 | Handle<mirror::Class> klass, |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 6382 | /*out*/HashMap<size_t, ClassLinker::MethodTranslation>* default_translations) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6383 | const size_t num_virtual_methods = klass->NumVirtualMethods(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6384 | if (klass->IsInterface()) { |
| 6385 | // No vtable. |
| 6386 | if (!IsUint<16>(num_virtual_methods)) { |
| 6387 | ThrowClassFormatError(klass.Get(), "Too many methods on interface: %zu", num_virtual_methods); |
| 6388 | return false; |
| 6389 | } |
| 6390 | bool has_defaults = false; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6391 | // Assign each method an IMT index and set the default flag. |
| 6392 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
| 6393 | ArtMethod* m = klass->GetVirtualMethodDuringLinking(i, image_pointer_size_); |
| 6394 | m->SetMethodIndex(i); |
| 6395 | if (!m->IsAbstract()) { |
Vladimir Marko | 1c993cd | 2020-05-28 09:30:06 +0000 | [diff] [blame] | 6396 | // If the dex file does not support default methods, throw ClassFormatError. |
| 6397 | // This check is necessary to protect from odd cases, such as native default |
| 6398 | // methods, that the dex file verifier permits for old dex file versions. b/157170505 |
| 6399 | // FIXME: This should be `if (!m->GetDexFile()->SupportsDefaultMethods())` but we're |
| 6400 | // currently running CTS tests for default methods with dex file version 035 which |
| 6401 | // does not support default methods. So, we limit this to native methods. b/157718952 |
| 6402 | if (m->IsNative()) { |
| 6403 | DCHECK(!m->GetDexFile()->SupportsDefaultMethods()); |
| 6404 | ThrowClassFormatError(klass.Get(), |
| 6405 | "Dex file does not support default method '%s'", |
| 6406 | m->PrettyMethod().c_str()); |
| 6407 | return false; |
| 6408 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6409 | m->SetAccessFlags(m->GetAccessFlags() | kAccDefault); |
| 6410 | has_defaults = true; |
| 6411 | } |
| 6412 | } |
| 6413 | // Mark that we have default methods so that we won't need to scan the virtual_methods_ array |
| 6414 | // during initialization. This is a performance optimization. We could simply traverse the |
| 6415 | // virtual_methods_ array again during initialization. |
| 6416 | if (has_defaults) { |
| 6417 | klass->SetHasDefaultMethods(); |
| 6418 | } |
| 6419 | return true; |
| 6420 | } else if (klass->HasSuperClass()) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6421 | const size_t super_vtable_length = klass->GetSuperClass()->GetVTableLength(); |
| 6422 | const size_t max_count = num_virtual_methods + super_vtable_length; |
Vladimir Marko | 3068d58 | 2019-05-28 16:39:29 +0100 | [diff] [blame] | 6423 | StackHandleScope<3> hs(self); |
Mingyao Yang | 38eecb0 | 2014-08-13 14:51:03 -0700 | [diff] [blame] | 6424 | Handle<mirror::Class> super_class(hs.NewHandle(klass->GetSuperClass())); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6425 | MutableHandle<mirror::PointerArray> vtable; |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6426 | if (super_class->ShouldHaveEmbeddedVTable()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6427 | vtable = hs.NewHandle(AllocPointerArray(self, max_count)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 6428 | if (UNLIKELY(vtable == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6429 | self->AssertPendingOOMException(); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 6430 | return false; |
| 6431 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6432 | for (size_t i = 0; i < super_vtable_length; i++) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6433 | vtable->SetElementPtrSize( |
| 6434 | i, super_class->GetEmbeddedVTableEntry(i, image_pointer_size_), image_pointer_size_); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 6435 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6436 | // We might need to change vtable if we have new virtual methods or new interfaces (since that |
| 6437 | // might give us new default methods). If no new interfaces then we can skip the rest since |
| 6438 | // the class cannot override any of the super-class's methods. This is required for |
| 6439 | // correctness since without it we might not update overridden default method vtable entries |
| 6440 | // correctly. |
| 6441 | if (num_virtual_methods == 0 && super_class->GetIfTableCount() == klass->GetIfTableCount()) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6442 | klass->SetVTable(vtable.Get()); |
| 6443 | return true; |
| 6444 | } |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 6445 | } else { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6446 | DCHECK(super_class->IsAbstract() && !super_class->IsArrayClass()); |
Vladimir Marko | 3068d58 | 2019-05-28 16:39:29 +0100 | [diff] [blame] | 6447 | Handle<mirror::PointerArray> super_vtable = hs.NewHandle(super_class->GetVTable()); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6448 | CHECK(super_vtable != nullptr) << super_class->PrettyClass(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6449 | // We might need to change vtable if we have new virtual methods or new interfaces (since that |
| 6450 | // might give us new default methods). See comment above. |
| 6451 | if (num_virtual_methods == 0 && super_class->GetIfTableCount() == klass->GetIfTableCount()) { |
Vladimir Marko | 3068d58 | 2019-05-28 16:39:29 +0100 | [diff] [blame] | 6452 | klass->SetVTable(super_vtable.Get()); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6453 | return true; |
| 6454 | } |
Vladimir Marko | 3068d58 | 2019-05-28 16:39:29 +0100 | [diff] [blame] | 6455 | vtable = hs.NewHandle(ObjPtr<mirror::PointerArray>::DownCast( |
| 6456 | mirror::Array::CopyOf(super_vtable, self, max_count))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 6457 | if (UNLIKELY(vtable == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6458 | self->AssertPendingOOMException(); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 6459 | return false; |
| 6460 | } |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 6461 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6462 | // How the algorithm works: |
| 6463 | // 1. Populate hash table by adding num_virtual_methods from klass. The values in the hash |
| 6464 | // table are: invalid_index for unused slots, index super_vtable_length + i for a virtual |
| 6465 | // method which has not been matched to a vtable method, and j if the virtual method at the |
| 6466 | // index overrode the super virtual method at index j. |
| 6467 | // 2. Loop through super virtual methods, if they overwrite, update hash table to j |
| 6468 | // (j < super_vtable_length) to avoid redundant checks. (TODO maybe use this info for reducing |
| 6469 | // the need for the initial vtable which we later shrink back down). |
| 6470 | // 3. Add non overridden methods to the end of the vtable. |
| 6471 | static constexpr size_t kMaxStackHash = 250; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6472 | // + 1 so that even if we only have new default methods we will still be able to use this hash |
| 6473 | // table (i.e. it will never have 0 size). |
| 6474 | const size_t hash_table_size = num_virtual_methods * 3 + 1; |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6475 | uint32_t* hash_table_ptr; |
| 6476 | std::unique_ptr<uint32_t[]> hash_heap_storage; |
| 6477 | if (hash_table_size <= kMaxStackHash) { |
| 6478 | hash_table_ptr = reinterpret_cast<uint32_t*>( |
| 6479 | alloca(hash_table_size * sizeof(*hash_table_ptr))); |
| 6480 | } else { |
| 6481 | hash_heap_storage.reset(new uint32_t[hash_table_size]); |
| 6482 | hash_table_ptr = hash_heap_storage.get(); |
| 6483 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6484 | 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] | 6485 | // Add virtual methods to the hash table. |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6486 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6487 | DCHECK(klass->GetVirtualMethodDuringLinking( |
| 6488 | i, image_pointer_size_)->GetDeclaringClass() != nullptr); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6489 | hash_table.Add(i); |
| 6490 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6491 | // 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] | 6492 | // the hash table. |
| 6493 | for (size_t j = 0; j < super_vtable_length; ++j) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6494 | // 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] | 6495 | ArtMethod* super_method = vtable->GetElementPtrSize<ArtMethod*>(j, image_pointer_size_); |
Alex Light | c7a420c | 2016-10-18 14:33:18 -0700 | [diff] [blame] | 6496 | if (!klass->CanAccessMember(super_method->GetDeclaringClass(), |
| 6497 | super_method->GetAccessFlags())) { |
| 6498 | // Continue on to the next method since this one is package private and canot be overridden. |
| 6499 | // Before Android 4.1, the package-private method super_method might have been incorrectly |
| 6500 | // overridden. |
| 6501 | continue; |
| 6502 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6503 | MethodNameAndSignatureComparator super_method_name_comparator( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6504 | super_method->GetInterfaceMethodIfProxy(image_pointer_size_)); |
Alex Light | c7a420c | 2016-10-18 14:33:18 -0700 | [diff] [blame] | 6505 | // We remove the method so that subsequent lookups will be faster by making the hash-map |
| 6506 | // smaller as we go on. |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6507 | uint32_t hash_index = hash_table.FindAndRemove(&super_method_name_comparator); |
| 6508 | if (hash_index != hash_table.GetNotFoundIndex()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6509 | ArtMethod* virtual_method = klass->GetVirtualMethodDuringLinking( |
| 6510 | hash_index, image_pointer_size_); |
Alex Light | c7a420c | 2016-10-18 14:33:18 -0700 | [diff] [blame] | 6511 | if (super_method->IsFinal()) { |
| 6512 | ThrowLinkageError(klass.Get(), "Method %s overrides final method in class %s", |
| 6513 | virtual_method->PrettyMethod().c_str(), |
| 6514 | super_method->GetDeclaringClassDescriptor()); |
| 6515 | return false; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6516 | } |
Alex Light | c7a420c | 2016-10-18 14:33:18 -0700 | [diff] [blame] | 6517 | vtable->SetElementPtrSize(j, virtual_method, image_pointer_size_); |
| 6518 | virtual_method->SetMethodIndex(j); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6519 | } else if (super_method->IsOverridableByDefaultMethod()) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6520 | // We didn't directly override this method but we might through default methods... |
| 6521 | // Check for default method update. |
| 6522 | ArtMethod* default_method = nullptr; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6523 | switch (FindDefaultMethodImplementation(self, |
| 6524 | super_method, |
| 6525 | klass, |
| 6526 | /*out*/&default_method)) { |
| 6527 | case DefaultMethodSearchResult::kDefaultConflict: { |
| 6528 | // A conflict was found looking for default methods. Note this (assuming it wasn't |
| 6529 | // pre-existing) in the translations map. |
| 6530 | if (UNLIKELY(!super_method->IsDefaultConflicting())) { |
| 6531 | // Don't generate another conflict method to reduce memory use as an optimization. |
| 6532 | default_translations->insert( |
| 6533 | {j, ClassLinker::MethodTranslation::CreateConflictingMethod()}); |
| 6534 | } |
| 6535 | break; |
| 6536 | } |
| 6537 | case DefaultMethodSearchResult::kAbstractFound: { |
| 6538 | // No conflict but method is abstract. |
| 6539 | // We note that this vtable entry must be made abstract. |
| 6540 | if (UNLIKELY(!super_method->IsAbstract())) { |
| 6541 | default_translations->insert( |
| 6542 | {j, ClassLinker::MethodTranslation::CreateAbstractMethod()}); |
| 6543 | } |
| 6544 | break; |
| 6545 | } |
| 6546 | case DefaultMethodSearchResult::kDefaultFound: { |
| 6547 | if (UNLIKELY(super_method->IsDefaultConflicting() || |
| 6548 | default_method->GetDeclaringClass() != super_method->GetDeclaringClass())) { |
| 6549 | // Found a default method implementation that is new. |
| 6550 | // TODO Refactor this add default methods to virtuals here and not in |
| 6551 | // LinkInterfaceMethods maybe. |
| 6552 | // The problem is default methods might override previously present |
| 6553 | // default-method or miranda-method vtable entries from the superclass. |
| 6554 | // Unfortunately we need these to be entries in this class's virtuals. We do not |
| 6555 | // give these entries there until LinkInterfaceMethods so we pass this map around |
| 6556 | // to let it know which vtable entries need to be updated. |
| 6557 | // Make a note that vtable entry j must be updated, store what it needs to be updated |
| 6558 | // to. We will allocate a virtual method slot in LinkInterfaceMethods and fix it up |
| 6559 | // then. |
| 6560 | default_translations->insert( |
| 6561 | {j, ClassLinker::MethodTranslation::CreateTranslatedMethod(default_method)}); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6562 | VLOG(class_linker) << "Method " << super_method->PrettyMethod() |
| 6563 | << " overridden by default " |
| 6564 | << default_method->PrettyMethod() |
| 6565 | << " in " << mirror::Class::PrettyClass(klass.Get()); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6566 | } |
| 6567 | break; |
| 6568 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6569 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6570 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6571 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6572 | size_t actual_count = super_vtable_length; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6573 | // Add the non-overridden methods at the end. |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6574 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6575 | ArtMethod* local_method = klass->GetVirtualMethodDuringLinking(i, image_pointer_size_); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6576 | size_t method_idx = local_method->GetMethodIndexDuringLinking(); |
| 6577 | if (method_idx < super_vtable_length && |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6578 | local_method == vtable->GetElementPtrSize<ArtMethod*>(method_idx, image_pointer_size_)) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6579 | continue; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6580 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6581 | vtable->SetElementPtrSize(actual_count, local_method, image_pointer_size_); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6582 | local_method->SetMethodIndex(actual_count); |
| 6583 | ++actual_count; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6584 | } |
Andreas Gampe | ab1eb0d | 2015-02-13 19:23:55 -0800 | [diff] [blame] | 6585 | if (!IsUint<16>(actual_count)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 6586 | ThrowClassFormatError(klass.Get(), "Too many methods defined on class: %zd", actual_count); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6587 | return false; |
| 6588 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 6589 | // Shrink vtable if possible |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6590 | CHECK_LE(actual_count, max_count); |
| 6591 | if (actual_count < max_count) { |
Vladimir Marko | 3068d58 | 2019-05-28 16:39:29 +0100 | [diff] [blame] | 6592 | vtable.Assign(ObjPtr<mirror::PointerArray>::DownCast( |
| 6593 | mirror::Array::CopyOf(vtable, self, actual_count))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 6594 | if (UNLIKELY(vtable == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6595 | self->AssertPendingOOMException(); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 6596 | return false; |
| 6597 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6598 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 6599 | klass->SetVTable(vtable.Get()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6600 | } else { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 6601 | CHECK_EQ(klass.Get(), GetClassRoot<mirror::Object>(this)); |
Andreas Gampe | ab1eb0d | 2015-02-13 19:23:55 -0800 | [diff] [blame] | 6602 | if (!IsUint<16>(num_virtual_methods)) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6603 | ThrowClassFormatError(klass.Get(), "Too many methods: %d", |
| 6604 | static_cast<int>(num_virtual_methods)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6605 | return false; |
| 6606 | } |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 6607 | ObjPtr<mirror::PointerArray> vtable = AllocPointerArray(self, num_virtual_methods); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6608 | if (UNLIKELY(vtable == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6609 | self->AssertPendingOOMException(); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 6610 | return false; |
| 6611 | } |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 6612 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6613 | ArtMethod* virtual_method = klass->GetVirtualMethodDuringLinking(i, image_pointer_size_); |
| 6614 | vtable->SetElementPtrSize(i, virtual_method, image_pointer_size_); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 6615 | virtual_method->SetMethodIndex(i & 0xFFFF); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6616 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6617 | klass->SetVTable(vtable); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6618 | } |
| 6619 | return true; |
| 6620 | } |
| 6621 | |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6622 | // Determine if the given iface has any subinterface in the given list that declares the method |
| 6623 | // specified by 'target'. |
| 6624 | // |
| 6625 | // Arguments |
| 6626 | // - self: The thread we are running on |
| 6627 | // - target: A comparator that will match any method that overrides the method we are checking for |
| 6628 | // - iftable: The iftable we are searching for an overriding method on. |
| 6629 | // - ifstart: The index of the interface we are checking to see if anything overrides |
| 6630 | // - iface: The interface we are checking to see if anything overrides. |
| 6631 | // - image_pointer_size: |
| 6632 | // The image pointer size. |
| 6633 | // |
| 6634 | // Returns |
| 6635 | // - True: There is some method that matches the target comparator defined in an interface that |
| 6636 | // is a subtype of iface. |
| 6637 | // - False: There is no method that matches the target comparator in any interface that is a subtype |
| 6638 | // of iface. |
| 6639 | static bool ContainsOverridingMethodOf(Thread* self, |
| 6640 | MethodNameAndSignatureComparator& target, |
| 6641 | Handle<mirror::IfTable> iftable, |
| 6642 | size_t ifstart, |
| 6643 | Handle<mirror::Class> iface, |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 6644 | PointerSize image_pointer_size) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 6645 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6646 | DCHECK(self != nullptr); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 6647 | DCHECK(iface != nullptr); |
| 6648 | DCHECK(iftable != nullptr); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6649 | DCHECK_GE(ifstart, 0u); |
| 6650 | DCHECK_LT(ifstart, iftable->Count()); |
| 6651 | DCHECK_EQ(iface.Get(), iftable->GetInterface(ifstart)); |
| 6652 | DCHECK(iface->IsInterface()); |
| 6653 | |
| 6654 | size_t iftable_count = iftable->Count(); |
| 6655 | StackHandleScope<1> hs(self); |
| 6656 | MutableHandle<mirror::Class> current_iface(hs.NewHandle<mirror::Class>(nullptr)); |
| 6657 | for (size_t k = ifstart + 1; k < iftable_count; k++) { |
| 6658 | // Skip ifstart since our current interface obviously cannot override itself. |
| 6659 | current_iface.Assign(iftable->GetInterface(k)); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6660 | // Iterate through every method on this interface. The order does not matter. |
| 6661 | for (ArtMethod& current_method : current_iface->GetDeclaredVirtualMethods(image_pointer_size)) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6662 | if (UNLIKELY(target.HasSameNameAndSignature( |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6663 | current_method.GetInterfaceMethodIfProxy(image_pointer_size)))) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6664 | // Check if the i'th interface is a subtype of this one. |
| 6665 | if (iface->IsAssignableFrom(current_iface.Get())) { |
| 6666 | return true; |
| 6667 | } |
| 6668 | break; |
| 6669 | } |
| 6670 | } |
| 6671 | } |
| 6672 | return false; |
| 6673 | } |
| 6674 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6675 | // 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] | 6676 | // out_default_method and returns kDefaultFound on success. If no default method was found return |
| 6677 | // kAbstractFound and store nullptr into out_default_method. If an error occurs (such as a |
| 6678 | // default_method conflict) it will return kDefaultConflict. |
| 6679 | ClassLinker::DefaultMethodSearchResult ClassLinker::FindDefaultMethodImplementation( |
| 6680 | Thread* self, |
| 6681 | ArtMethod* target_method, |
| 6682 | Handle<mirror::Class> klass, |
| 6683 | /*out*/ArtMethod** out_default_method) const { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6684 | DCHECK(self != nullptr); |
| 6685 | DCHECK(target_method != nullptr); |
| 6686 | DCHECK(out_default_method != nullptr); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6687 | |
| 6688 | *out_default_method = nullptr; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6689 | |
| 6690 | // We organize the interface table so that, for interface I any subinterfaces J follow it in the |
| 6691 | // table. This lets us walk the table backwards when searching for default methods. The first one |
| 6692 | // we encounter is the best candidate since it is the most specific. Once we have found it we keep |
| 6693 | // track of it and then continue checking all other interfaces, since we need to throw an error if |
| 6694 | // we encounter conflicting default method implementations (one is not a subtype of the other). |
| 6695 | // |
| 6696 | // The order of unrelated interfaces does not matter and is not defined. |
| 6697 | size_t iftable_count = klass->GetIfTableCount(); |
| 6698 | if (iftable_count == 0) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6699 | // No interfaces. We have already reset out to null so just return kAbstractFound. |
| 6700 | return DefaultMethodSearchResult::kAbstractFound; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6701 | } |
| 6702 | |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6703 | StackHandleScope<3> hs(self); |
| 6704 | MutableHandle<mirror::Class> chosen_iface(hs.NewHandle<mirror::Class>(nullptr)); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6705 | MutableHandle<mirror::IfTable> iftable(hs.NewHandle(klass->GetIfTable())); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6706 | MutableHandle<mirror::Class> iface(hs.NewHandle<mirror::Class>(nullptr)); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6707 | MethodNameAndSignatureComparator target_name_comparator( |
| 6708 | target_method->GetInterfaceMethodIfProxy(image_pointer_size_)); |
| 6709 | // Iterates over the klass's iftable in reverse |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6710 | for (size_t k = iftable_count; k != 0; ) { |
| 6711 | --k; |
| 6712 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6713 | DCHECK_LT(k, iftable->Count()); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6714 | |
| 6715 | iface.Assign(iftable->GetInterface(k)); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6716 | // Iterate through every declared method on this interface. The order does not matter. |
| 6717 | for (auto& method_iter : iface->GetDeclaredVirtualMethods(image_pointer_size_)) { |
| 6718 | ArtMethod* current_method = &method_iter; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6719 | // Skip abstract methods and methods with different names. |
| 6720 | if (current_method->IsAbstract() || |
| 6721 | !target_name_comparator.HasSameNameAndSignature( |
| 6722 | current_method->GetInterfaceMethodIfProxy(image_pointer_size_))) { |
| 6723 | continue; |
Alex Light | d7c10c2 | 2016-03-31 10:03:07 -0700 | [diff] [blame] | 6724 | } else if (!current_method->IsPublic()) { |
| 6725 | // The verifier should have caught the non-public method for dex version 37. Just warn and |
| 6726 | // skip it since this is from before default-methods so we don't really need to care that it |
| 6727 | // has code. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6728 | LOG(WARNING) << "Interface method " << current_method->PrettyMethod() |
| 6729 | << " is not public! " |
Alex Light | d7c10c2 | 2016-03-31 10:03:07 -0700 | [diff] [blame] | 6730 | << "This will be a fatal error in subsequent versions of android. " |
| 6731 | << "Continuing anyway."; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6732 | } |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 6733 | if (UNLIKELY(chosen_iface != nullptr)) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6734 | // We have multiple default impls of the same method. This is a potential default conflict. |
| 6735 | // We need to check if this possibly conflicting method is either a superclass of the chosen |
| 6736 | // default implementation or is overridden by a non-default interface method. In either case |
| 6737 | // there is no conflict. |
| 6738 | if (!iface->IsAssignableFrom(chosen_iface.Get()) && |
| 6739 | !ContainsOverridingMethodOf(self, |
| 6740 | target_name_comparator, |
| 6741 | iftable, |
| 6742 | k, |
| 6743 | iface, |
| 6744 | image_pointer_size_)) { |
Nicolas Geoffray | 7f3e0db | 2016-01-28 09:29:31 +0000 | [diff] [blame] | 6745 | VLOG(class_linker) << "Conflicting default method implementations found: " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6746 | << current_method->PrettyMethod() << " and " |
| 6747 | << ArtMethod::PrettyMethod(*out_default_method) << " in class " |
| 6748 | << klass->PrettyClass() << " conflict."; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6749 | *out_default_method = nullptr; |
| 6750 | return DefaultMethodSearchResult::kDefaultConflict; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6751 | } else { |
| 6752 | break; // Continue checking at the next interface. |
| 6753 | } |
| 6754 | } else { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6755 | // chosen_iface == null |
| 6756 | if (!ContainsOverridingMethodOf(self, |
| 6757 | target_name_comparator, |
| 6758 | iftable, |
| 6759 | k, |
| 6760 | iface, |
| 6761 | image_pointer_size_)) { |
| 6762 | // Don't set this as the chosen interface if something else is overriding it (because that |
| 6763 | // other interface would be potentially chosen instead if it was default). If the other |
| 6764 | // interface was abstract then we wouldn't select this interface as chosen anyway since |
| 6765 | // the abstract method masks it. |
| 6766 | *out_default_method = current_method; |
| 6767 | chosen_iface.Assign(iface.Get()); |
| 6768 | // We should now finish traversing the graph to find if we have default methods that |
| 6769 | // conflict. |
| 6770 | } else { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6771 | VLOG(class_linker) << "A default method '" << current_method->PrettyMethod() |
| 6772 | << "' was " |
| 6773 | << "skipped because it was overridden by an abstract method in a " |
| 6774 | << "subinterface on class '" << klass->PrettyClass() << "'"; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6775 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6776 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6777 | break; |
| 6778 | } |
| 6779 | } |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6780 | if (*out_default_method != nullptr) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6781 | VLOG(class_linker) << "Default method '" << (*out_default_method)->PrettyMethod() |
| 6782 | << "' selected " |
| 6783 | << "as the implementation for '" << target_method->PrettyMethod() |
| 6784 | << "' in '" << klass->PrettyClass() << "'"; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6785 | return DefaultMethodSearchResult::kDefaultFound; |
| 6786 | } else { |
| 6787 | return DefaultMethodSearchResult::kAbstractFound; |
| 6788 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6789 | } |
| 6790 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6791 | ArtMethod* ClassLinker::AddMethodToConflictTable(ObjPtr<mirror::Class> klass, |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6792 | ArtMethod* conflict_method, |
| 6793 | ArtMethod* interface_method, |
Nicolas Geoffray | 47213e4 | 2020-12-30 15:12:00 +0000 | [diff] [blame] | 6794 | ArtMethod* method) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 6795 | ImtConflictTable* current_table = conflict_method->GetImtConflictTable(kRuntimePointerSize); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6796 | Runtime* const runtime = Runtime::Current(); |
| 6797 | LinearAlloc* linear_alloc = GetAllocatorForClassLoader(klass->GetClassLoader()); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6798 | |
| 6799 | // Create a new entry if the existing one is the shared conflict method. |
Nicolas Geoffray | 47213e4 | 2020-12-30 15:12:00 +0000 | [diff] [blame] | 6800 | ArtMethod* new_conflict_method = (conflict_method == runtime->GetImtConflictMethod()) |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6801 | ? runtime->CreateImtConflictMethod(linear_alloc) |
| 6802 | : conflict_method; |
| 6803 | |
| 6804 | // Allocate a new table. Note that we will leak this table at the next conflict, |
| 6805 | // but that's a tradeoff compared to making the table fixed size. |
| 6806 | void* data = linear_alloc->Alloc( |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6807 | Thread::Current(), ImtConflictTable::ComputeSizeWithOneMoreEntry(current_table, |
| 6808 | image_pointer_size_)); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6809 | if (data == nullptr) { |
| 6810 | LOG(ERROR) << "Failed to allocate conflict table"; |
| 6811 | return conflict_method; |
| 6812 | } |
| 6813 | ImtConflictTable* new_table = new (data) ImtConflictTable(current_table, |
| 6814 | interface_method, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6815 | method, |
| 6816 | image_pointer_size_); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6817 | |
| 6818 | // Do a fence to ensure threads see the data in the table before it is assigned |
| 6819 | // to the conflict method. |
| 6820 | // Note that there is a race in the presence of multiple threads and we may leak |
| 6821 | // memory from the LinearAlloc, but that's a tradeoff compared to using |
| 6822 | // atomic operations. |
Orion Hodson | 27b9676 | 2018-03-13 16:06:57 +0000 | [diff] [blame] | 6823 | std::atomic_thread_fence(std::memory_order_release); |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6824 | new_conflict_method->SetImtConflictTable(new_table, image_pointer_size_); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6825 | return new_conflict_method; |
| 6826 | } |
| 6827 | |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 6828 | bool ClassLinker::AllocateIfTableMethodArrays(Thread* self, |
| 6829 | Handle<mirror::Class> klass, |
| 6830 | Handle<mirror::IfTable> iftable) { |
| 6831 | DCHECK(!klass->IsInterface()); |
| 6832 | const bool has_superclass = klass->HasSuperClass(); |
| 6833 | const bool extend_super_iftable = has_superclass; |
| 6834 | const size_t ifcount = klass->GetIfTableCount(); |
| 6835 | const size_t super_ifcount = has_superclass ? klass->GetSuperClass()->GetIfTableCount() : 0U; |
| 6836 | for (size_t i = 0; i < ifcount; ++i) { |
| 6837 | size_t num_methods = iftable->GetInterface(i)->NumDeclaredVirtualMethods(); |
| 6838 | if (num_methods > 0) { |
| 6839 | const bool is_super = i < super_ifcount; |
| 6840 | // This is an interface implemented by a super-class. Therefore we can just copy the method |
| 6841 | // array from the superclass. |
| 6842 | const bool super_interface = is_super && extend_super_iftable; |
| 6843 | ObjPtr<mirror::PointerArray> method_array; |
| 6844 | if (super_interface) { |
| 6845 | ObjPtr<mirror::IfTable> if_table = klass->GetSuperClass()->GetIfTable(); |
| 6846 | DCHECK(if_table != nullptr); |
| 6847 | DCHECK(if_table->GetMethodArray(i) != nullptr); |
| 6848 | // 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] | 6849 | StackHandleScope<1u> hs(self); |
| 6850 | Handle<mirror::PointerArray> old_array = hs.NewHandle(if_table->GetMethodArray(i)); |
| 6851 | method_array = |
| 6852 | ObjPtr<mirror::PointerArray>::DownCast(mirror::Object::Clone(old_array, self)); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 6853 | } else { |
| 6854 | method_array = AllocPointerArray(self, num_methods); |
| 6855 | } |
| 6856 | if (UNLIKELY(method_array == nullptr)) { |
| 6857 | self->AssertPendingOOMException(); |
| 6858 | return false; |
| 6859 | } |
| 6860 | iftable->SetMethodArray(i, method_array); |
| 6861 | } |
| 6862 | } |
| 6863 | return true; |
| 6864 | } |
| 6865 | |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6866 | void ClassLinker::SetIMTRef(ArtMethod* unimplemented_method, |
| 6867 | ArtMethod* imt_conflict_method, |
| 6868 | ArtMethod* current_method, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6869 | /*out*/bool* new_conflict, |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6870 | /*out*/ArtMethod** imt_ref) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6871 | // Place method in imt if entry is empty, place conflict otherwise. |
| 6872 | if (*imt_ref == unimplemented_method) { |
| 6873 | *imt_ref = current_method; |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 6874 | } else if (!(*imt_ref)->IsRuntimeMethod()) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6875 | // If we are not a conflict and we have the same signature and name as the imt |
| 6876 | // entry, it must be that we overwrote a superclass vtable entry. |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 6877 | // Note that we have checked IsRuntimeMethod, as there may be multiple different |
| 6878 | // conflict methods. |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6879 | MethodNameAndSignatureComparator imt_comparator( |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6880 | (*imt_ref)->GetInterfaceMethodIfProxy(image_pointer_size_)); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6881 | if (imt_comparator.HasSameNameAndSignature( |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6882 | current_method->GetInterfaceMethodIfProxy(image_pointer_size_))) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6883 | *imt_ref = current_method; |
| 6884 | } else { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6885 | *imt_ref = imt_conflict_method; |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6886 | *new_conflict = true; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6887 | } |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 6888 | } else { |
| 6889 | // Place the default conflict method. Note that there may be an existing conflict |
| 6890 | // method in the IMT, but it could be one tailored to the super class, with a |
| 6891 | // specific ImtConflictTable. |
| 6892 | *imt_ref = imt_conflict_method; |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6893 | *new_conflict = true; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6894 | } |
| 6895 | } |
| 6896 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6897 | void ClassLinker::FillIMTAndConflictTables(ObjPtr<mirror::Class> klass) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6898 | DCHECK(klass->ShouldHaveImt()) << klass->PrettyClass(); |
| 6899 | DCHECK(!klass->IsTemp()) << klass->PrettyClass(); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6900 | ArtMethod* imt_data[ImTable::kSize]; |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6901 | Runtime* const runtime = Runtime::Current(); |
| 6902 | ArtMethod* const unimplemented_method = runtime->GetImtUnimplementedMethod(); |
| 6903 | ArtMethod* const conflict_method = runtime->GetImtConflictMethod(); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6904 | std::fill_n(imt_data, arraysize(imt_data), unimplemented_method); |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6905 | if (klass->GetIfTable() != nullptr) { |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6906 | bool new_conflict = false; |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6907 | FillIMTFromIfTable(klass->GetIfTable(), |
| 6908 | unimplemented_method, |
| 6909 | conflict_method, |
| 6910 | klass, |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 6911 | /*create_conflict_tables=*/true, |
| 6912 | /*ignore_copied_methods=*/false, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6913 | &new_conflict, |
| 6914 | &imt_data[0]); |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6915 | } |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6916 | // Compare the IMT with the super class including the conflict methods. If they are equivalent, |
| 6917 | // we can just use the same pointer. |
| 6918 | ImTable* imt = nullptr; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6919 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6920 | if (super_class != nullptr && super_class->ShouldHaveImt()) { |
| 6921 | ImTable* super_imt = super_class->GetImt(image_pointer_size_); |
| 6922 | bool same = true; |
| 6923 | for (size_t i = 0; same && i < ImTable::kSize; ++i) { |
| 6924 | ArtMethod* method = imt_data[i]; |
| 6925 | ArtMethod* super_method = super_imt->Get(i, image_pointer_size_); |
| 6926 | if (method != super_method) { |
| 6927 | bool is_conflict_table = method->IsRuntimeMethod() && |
| 6928 | method != unimplemented_method && |
| 6929 | method != conflict_method; |
| 6930 | // Verify conflict contents. |
| 6931 | bool super_conflict_table = super_method->IsRuntimeMethod() && |
| 6932 | super_method != unimplemented_method && |
| 6933 | super_method != conflict_method; |
| 6934 | if (!is_conflict_table || !super_conflict_table) { |
| 6935 | same = false; |
| 6936 | } else { |
| 6937 | ImtConflictTable* table1 = method->GetImtConflictTable(image_pointer_size_); |
| 6938 | ImtConflictTable* table2 = super_method->GetImtConflictTable(image_pointer_size_); |
| 6939 | same = same && table1->Equals(table2, image_pointer_size_); |
| 6940 | } |
| 6941 | } |
| 6942 | } |
| 6943 | if (same) { |
| 6944 | imt = super_imt; |
| 6945 | } |
| 6946 | } |
| 6947 | if (imt == nullptr) { |
| 6948 | imt = klass->GetImt(image_pointer_size_); |
| 6949 | DCHECK(imt != nullptr); |
| 6950 | imt->Populate(imt_data, image_pointer_size_); |
| 6951 | } else { |
| 6952 | klass->SetImt(imt, image_pointer_size_); |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6953 | } |
| 6954 | } |
| 6955 | |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6956 | ImtConflictTable* ClassLinker::CreateImtConflictTable(size_t count, |
| 6957 | LinearAlloc* linear_alloc, |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 6958 | PointerSize image_pointer_size) { |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6959 | void* data = linear_alloc->Alloc(Thread::Current(), |
| 6960 | ImtConflictTable::ComputeSize(count, |
| 6961 | image_pointer_size)); |
| 6962 | return (data != nullptr) ? new (data) ImtConflictTable(count, image_pointer_size) : nullptr; |
| 6963 | } |
| 6964 | |
| 6965 | ImtConflictTable* ClassLinker::CreateImtConflictTable(size_t count, LinearAlloc* linear_alloc) { |
| 6966 | return CreateImtConflictTable(count, linear_alloc, image_pointer_size_); |
| 6967 | } |
| 6968 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6969 | void ClassLinker::FillIMTFromIfTable(ObjPtr<mirror::IfTable> if_table, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6970 | ArtMethod* unimplemented_method, |
| 6971 | ArtMethod* imt_conflict_method, |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6972 | ObjPtr<mirror::Class> klass, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6973 | bool create_conflict_tables, |
| 6974 | bool ignore_copied_methods, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6975 | /*out*/bool* new_conflict, |
| 6976 | /*out*/ArtMethod** imt) { |
| 6977 | uint32_t conflict_counts[ImTable::kSize] = {}; |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6978 | for (size_t i = 0, length = if_table->Count(); i < length; ++i) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6979 | ObjPtr<mirror::Class> interface = if_table->GetInterface(i); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6980 | const size_t num_virtuals = interface->NumVirtualMethods(); |
| 6981 | const size_t method_array_count = if_table->GetMethodArrayCount(i); |
| 6982 | // Virtual methods can be larger than the if table methods if there are default methods. |
| 6983 | DCHECK_GE(num_virtuals, method_array_count); |
| 6984 | if (kIsDebugBuild) { |
| 6985 | if (klass->IsInterface()) { |
| 6986 | DCHECK_EQ(method_array_count, 0u); |
| 6987 | } else { |
| 6988 | DCHECK_EQ(interface->NumDeclaredVirtualMethods(), method_array_count); |
| 6989 | } |
| 6990 | } |
| 6991 | if (method_array_count == 0) { |
| 6992 | continue; |
| 6993 | } |
Vladimir Marko | 557fece | 2019-03-26 14:29:41 +0000 | [diff] [blame] | 6994 | ObjPtr<mirror::PointerArray> method_array = if_table->GetMethodArray(i); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6995 | for (size_t j = 0; j < method_array_count; ++j) { |
| 6996 | ArtMethod* implementation_method = |
| 6997 | method_array->GetElementPtrSize<ArtMethod*>(j, image_pointer_size_); |
| 6998 | if (ignore_copied_methods && implementation_method->IsCopied()) { |
| 6999 | continue; |
| 7000 | } |
| 7001 | DCHECK(implementation_method != nullptr); |
| 7002 | // Miranda methods cannot be used to implement an interface method, but they are safe to put |
| 7003 | // in the IMT since their entrypoint is the interface trampoline. If we put any copied methods |
| 7004 | // or interface methods in the IMT here they will not create extra conflicts since we compare |
| 7005 | // names and signatures in SetIMTRef. |
| 7006 | ArtMethod* interface_method = interface->GetVirtualMethod(j, image_pointer_size_); |
David Srbecky | e36e7f2 | 2018-11-14 14:21:23 +0000 | [diff] [blame] | 7007 | const uint32_t imt_index = interface_method->GetImtIndex(); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 7008 | |
| 7009 | // There is only any conflicts if all of the interface methods for an IMT slot don't have |
| 7010 | // the same implementation method, keep track of this to avoid creating a conflict table in |
| 7011 | // this case. |
| 7012 | |
| 7013 | // Conflict table size for each IMT slot. |
| 7014 | ++conflict_counts[imt_index]; |
| 7015 | |
| 7016 | SetIMTRef(unimplemented_method, |
| 7017 | imt_conflict_method, |
| 7018 | implementation_method, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 7019 | /*out*/new_conflict, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 7020 | /*out*/&imt[imt_index]); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 7021 | } |
| 7022 | } |
| 7023 | |
| 7024 | if (create_conflict_tables) { |
| 7025 | // Create the conflict tables. |
| 7026 | LinearAlloc* linear_alloc = GetAllocatorForClassLoader(klass->GetClassLoader()); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 7027 | for (size_t i = 0; i < ImTable::kSize; ++i) { |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 7028 | size_t conflicts = conflict_counts[i]; |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 7029 | if (imt[i] == imt_conflict_method) { |
| 7030 | ImtConflictTable* new_table = CreateImtConflictTable(conflicts, linear_alloc); |
| 7031 | if (new_table != nullptr) { |
| 7032 | ArtMethod* new_conflict_method = |
| 7033 | Runtime::Current()->CreateImtConflictMethod(linear_alloc); |
| 7034 | new_conflict_method->SetImtConflictTable(new_table, image_pointer_size_); |
| 7035 | imt[i] = new_conflict_method; |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 7036 | } else { |
| 7037 | LOG(ERROR) << "Failed to allocate conflict table"; |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 7038 | imt[i] = imt_conflict_method; |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 7039 | } |
| 7040 | } else { |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 7041 | DCHECK_NE(imt[i], imt_conflict_method); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 7042 | } |
| 7043 | } |
| 7044 | |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 7045 | for (size_t i = 0, length = if_table->Count(); i < length; ++i) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7046 | ObjPtr<mirror::Class> interface = if_table->GetInterface(i); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 7047 | const size_t method_array_count = if_table->GetMethodArrayCount(i); |
| 7048 | // Virtual methods can be larger than the if table methods if there are default methods. |
| 7049 | if (method_array_count == 0) { |
| 7050 | continue; |
| 7051 | } |
Vladimir Marko | 557fece | 2019-03-26 14:29:41 +0000 | [diff] [blame] | 7052 | ObjPtr<mirror::PointerArray> method_array = if_table->GetMethodArray(i); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 7053 | for (size_t j = 0; j < method_array_count; ++j) { |
| 7054 | ArtMethod* implementation_method = |
| 7055 | method_array->GetElementPtrSize<ArtMethod*>(j, image_pointer_size_); |
| 7056 | if (ignore_copied_methods && implementation_method->IsCopied()) { |
| 7057 | continue; |
| 7058 | } |
| 7059 | DCHECK(implementation_method != nullptr); |
| 7060 | ArtMethod* interface_method = interface->GetVirtualMethod(j, image_pointer_size_); |
David Srbecky | e36e7f2 | 2018-11-14 14:21:23 +0000 | [diff] [blame] | 7061 | const uint32_t imt_index = interface_method->GetImtIndex(); |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 7062 | if (!imt[imt_index]->IsRuntimeMethod() || |
| 7063 | imt[imt_index] == unimplemented_method || |
| 7064 | imt[imt_index] == imt_conflict_method) { |
| 7065 | continue; |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 7066 | } |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 7067 | ImtConflictTable* table = imt[imt_index]->GetImtConflictTable(image_pointer_size_); |
| 7068 | const size_t num_entries = table->NumEntries(image_pointer_size_); |
| 7069 | table->SetInterfaceMethod(num_entries, image_pointer_size_, interface_method); |
| 7070 | table->SetImplementationMethod(num_entries, image_pointer_size_, implementation_method); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 7071 | } |
| 7072 | } |
| 7073 | } |
| 7074 | } |
| 7075 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7076 | // Simple helper function that checks that no subtypes of 'val' are contained within the 'classes' |
| 7077 | // set. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7078 | static bool NotSubinterfaceOfAny( |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 7079 | const HashSet<mirror::Class*>& classes, |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7080 | ObjPtr<mirror::Class> val) |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7081 | REQUIRES(Roles::uninterruptible_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 7082 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7083 | DCHECK(val != nullptr); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7084 | for (ObjPtr<mirror::Class> c : classes) { |
| 7085 | if (val->IsAssignableFrom(c)) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7086 | return false; |
| 7087 | } |
| 7088 | } |
| 7089 | return true; |
| 7090 | } |
| 7091 | |
| 7092 | // Fills in and flattens the interface inheritance hierarchy. |
| 7093 | // |
| 7094 | // By the end of this function all interfaces in the transitive closure of to_process are added to |
| 7095 | // the iftable and every interface precedes all of its sub-interfaces in this list. |
| 7096 | // |
| 7097 | // all I, J: Interface | I <: J implies J precedes I |
| 7098 | // |
| 7099 | // (note A <: B means that A is a subtype of B) |
| 7100 | // |
| 7101 | // This returns the total number of items in the iftable. The iftable might be resized down after |
| 7102 | // this call. |
| 7103 | // |
| 7104 | // We order this backwards so that we do not need to reorder superclass interfaces when new |
| 7105 | // interfaces are added in subclass's interface tables. |
| 7106 | // |
| 7107 | // Upon entry into this function iftable is a copy of the superclass's iftable with the first |
| 7108 | // super_ifcount entries filled in with the transitive closure of the interfaces of the superclass. |
| 7109 | // The other entries are uninitialized. We will fill in the remaining entries in this function. The |
| 7110 | // iftable must be large enough to hold all interfaces without changing its size. |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 7111 | static size_t FillIfTable(Thread* self, |
| 7112 | ObjPtr<mirror::Class> klass, |
| 7113 | ObjPtr<mirror::ObjectArray<mirror::Class>> interfaces, |
| 7114 | ObjPtr<mirror::IfTable> iftable, |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7115 | size_t super_ifcount, |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 7116 | size_t num_interfaces) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 7117 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 7118 | ScopedAssertNoThreadSuspension nts(__FUNCTION__); |
| 7119 | // This is the set of all classes already in the iftable. Used to make checking |
| 7120 | // if a class has already been added quicker. |
| 7121 | constexpr size_t kBufferSize = 32; // 256 bytes on 64-bit architectures. |
| 7122 | mirror::Class* buffer[kBufferSize]; |
| 7123 | HashSet<mirror::Class*> classes_in_iftable(buffer, kBufferSize); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7124 | // The first super_ifcount elements are from the superclass. We note that they are already added. |
| 7125 | for (size_t i = 0; i < super_ifcount; i++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7126 | ObjPtr<mirror::Class> iface = iftable->GetInterface(i); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7127 | DCHECK(NotSubinterfaceOfAny(classes_in_iftable, iface)) << "Bad ordering."; |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 7128 | classes_in_iftable.insert(iface.Ptr()); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7129 | } |
| 7130 | size_t filled_ifcount = super_ifcount; |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 7131 | const bool have_interfaces = interfaces != nullptr; |
| 7132 | for (size_t i = 0; i != num_interfaces; ++i) { |
| 7133 | ObjPtr<mirror::Class> interface = have_interfaces |
| 7134 | ? interfaces->Get(i) |
| 7135 | : mirror::Class::GetDirectInterface(self, klass, i); |
| 7136 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7137 | // Let us call the first filled_ifcount elements of iftable the current-iface-list. |
| 7138 | // At this point in the loop current-iface-list has the invariant that: |
| 7139 | // for every pair of interfaces I,J within it: |
| 7140 | // if index_of(I) < index_of(J) then I is not a subtype of J |
| 7141 | |
| 7142 | // If we have already seen this element then all of its super-interfaces must already be in the |
| 7143 | // current-iface-list so we can skip adding it. |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 7144 | if (classes_in_iftable.find(interface.Ptr()) == classes_in_iftable.end()) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7145 | // We haven't seen this interface so add all of its super-interfaces onto the |
| 7146 | // current-iface-list, skipping those already on it. |
| 7147 | int32_t ifcount = interface->GetIfTableCount(); |
| 7148 | for (int32_t j = 0; j < ifcount; j++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7149 | ObjPtr<mirror::Class> super_interface = interface->GetIfTable()->GetInterface(j); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7150 | if (!ContainsElement(classes_in_iftable, super_interface)) { |
| 7151 | DCHECK(NotSubinterfaceOfAny(classes_in_iftable, super_interface)) << "Bad ordering."; |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 7152 | classes_in_iftable.insert(super_interface.Ptr()); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7153 | iftable->SetInterface(filled_ifcount, super_interface); |
| 7154 | filled_ifcount++; |
| 7155 | } |
| 7156 | } |
| 7157 | DCHECK(NotSubinterfaceOfAny(classes_in_iftable, interface)) << "Bad ordering"; |
| 7158 | // Place this interface onto the current-iface-list after all of its super-interfaces. |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 7159 | classes_in_iftable.insert(interface.Ptr()); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7160 | iftable->SetInterface(filled_ifcount, interface); |
| 7161 | filled_ifcount++; |
| 7162 | } else if (kIsDebugBuild) { |
| 7163 | // Check all super-interfaces are already in the list. |
| 7164 | int32_t ifcount = interface->GetIfTableCount(); |
| 7165 | for (int32_t j = 0; j < ifcount; j++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7166 | ObjPtr<mirror::Class> super_interface = interface->GetIfTable()->GetInterface(j); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7167 | DCHECK(ContainsElement(classes_in_iftable, super_interface)) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 7168 | << "Iftable does not contain " << mirror::Class::PrettyClass(super_interface) |
| 7169 | << ", a superinterface of " << interface->PrettyClass(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7170 | } |
| 7171 | } |
| 7172 | } |
| 7173 | if (kIsDebugBuild) { |
| 7174 | // Check that the iftable is ordered correctly. |
| 7175 | for (size_t i = 0; i < filled_ifcount; i++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7176 | ObjPtr<mirror::Class> if_a = iftable->GetInterface(i); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7177 | for (size_t j = i + 1; j < filled_ifcount; j++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7178 | ObjPtr<mirror::Class> if_b = iftable->GetInterface(j); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7179 | // !(if_a <: if_b) |
| 7180 | CHECK(!if_b->IsAssignableFrom(if_a)) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 7181 | << "Bad interface order: " << mirror::Class::PrettyClass(if_a) << " (index " << i |
| 7182 | << ") extends " |
| 7183 | << 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] | 7184 | << "interface list."; |
| 7185 | } |
| 7186 | } |
| 7187 | } |
| 7188 | return filled_ifcount; |
| 7189 | } |
| 7190 | |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 7191 | bool ClassLinker::SetupInterfaceLookupTable(Thread* self, |
| 7192 | Handle<mirror::Class> klass, |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7193 | Handle<mirror::ObjectArray<mirror::Class>> interfaces) { |
| 7194 | StackHandleScope<1> hs(self); |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 7195 | const bool has_superclass = klass->HasSuperClass(); |
| 7196 | const size_t super_ifcount = has_superclass ? klass->GetSuperClass()->GetIfTableCount() : 0U; |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 7197 | const bool have_interfaces = interfaces != nullptr; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7198 | const size_t num_interfaces = |
| 7199 | have_interfaces ? interfaces->GetLength() : klass->NumDirectInterfaces(); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 7200 | if (num_interfaces == 0) { |
| 7201 | if (super_ifcount == 0) { |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 7202 | if (LIKELY(has_superclass)) { |
| 7203 | klass->SetIfTable(klass->GetSuperClass()->GetIfTable()); |
| 7204 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 7205 | // Class implements no interfaces. |
| 7206 | DCHECK_EQ(klass->GetIfTableCount(), 0); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 7207 | return true; |
| 7208 | } |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 7209 | // Class implements same interfaces as parent, are any of these not marker interfaces? |
| 7210 | bool has_non_marker_interface = false; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7211 | ObjPtr<mirror::IfTable> super_iftable = klass->GetSuperClass()->GetIfTable(); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 7212 | for (size_t i = 0; i < super_ifcount; ++i) { |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 7213 | if (super_iftable->GetMethodArrayCount(i) > 0) { |
| 7214 | has_non_marker_interface = true; |
| 7215 | break; |
| 7216 | } |
| 7217 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 7218 | // Class just inherits marker interfaces from parent so recycle parent's iftable. |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 7219 | if (!has_non_marker_interface) { |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 7220 | klass->SetIfTable(super_iftable); |
| 7221 | return true; |
| 7222 | } |
| 7223 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 7224 | size_t ifcount = super_ifcount + num_interfaces; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7225 | // Check that every class being implemented is an interface. |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 7226 | for (size_t i = 0; i < num_interfaces; i++) { |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 7227 | ObjPtr<mirror::Class> interface = have_interfaces |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 7228 | ? interfaces->GetWithoutChecks(i) |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 7229 | : mirror::Class::GetDirectInterface(self, klass.Get(), i); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 7230 | DCHECK(interface != nullptr); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 7231 | if (UNLIKELY(!interface->IsInterface())) { |
| 7232 | std::string temp; |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 7233 | ThrowIncompatibleClassChangeError(klass.Get(), |
| 7234 | "Class %s implements non-interface class %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 7235 | klass->PrettyDescriptor().c_str(), |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 7236 | PrettyDescriptor(interface->GetDescriptor(&temp)).c_str()); |
| 7237 | return false; |
| 7238 | } |
| 7239 | ifcount += interface->GetIfTableCount(); |
| 7240 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7241 | // Create the interface function table. |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 7242 | MutableHandle<mirror::IfTable> iftable(hs.NewHandle(AllocIfTable(self, ifcount))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 7243 | if (UNLIKELY(iftable == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7244 | self->AssertPendingOOMException(); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 7245 | return false; |
| 7246 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7247 | // Fill in table with superclass's iftable. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7248 | if (super_ifcount != 0) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7249 | ObjPtr<mirror::IfTable> super_iftable = klass->GetSuperClass()->GetIfTable(); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 7250 | for (size_t i = 0; i < super_ifcount; i++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7251 | ObjPtr<mirror::Class> super_interface = super_iftable->GetInterface(i); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 7252 | iftable->SetInterface(i, super_interface); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 7253 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7254 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7255 | |
| 7256 | // Note that AllowThreadSuspension is to thread suspension as pthread_testcancel is to pthread |
| 7257 | // cancellation. That is it will suspend if one has a pending suspend request but otherwise |
| 7258 | // doesn't really do anything. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 7259 | self->AllowThreadSuspension(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7260 | |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 7261 | const size_t new_ifcount = FillIfTable( |
| 7262 | self, klass.Get(), interfaces.Get(), iftable.Get(), super_ifcount, num_interfaces); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7263 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 7264 | self->AllowThreadSuspension(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7265 | |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 7266 | // Shrink iftable in case duplicates were found |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7267 | if (new_ifcount < ifcount) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 7268 | DCHECK_NE(num_interfaces, 0U); |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 7269 | iftable.Assign(ObjPtr<mirror::IfTable>::DownCast( |
Vladimir Marko | 3068d58 | 2019-05-28 16:39:29 +0100 | [diff] [blame] | 7270 | mirror::IfTable::CopyOf(iftable, self, new_ifcount * mirror::IfTable::kMax))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 7271 | if (UNLIKELY(iftable == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7272 | self->AssertPendingOOMException(); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 7273 | return false; |
| 7274 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7275 | ifcount = new_ifcount; |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 7276 | } else { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7277 | DCHECK_EQ(new_ifcount, ifcount); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 7278 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 7279 | klass->SetIfTable(iftable.Get()); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7280 | return true; |
| 7281 | } |
| 7282 | |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 7283 | // Finds the method with a name/signature that matches cmp in the given lists of methods. The list |
| 7284 | // of methods must be unique. |
| 7285 | static ArtMethod* FindSameNameAndSignature(MethodNameAndSignatureComparator& cmp ATTRIBUTE_UNUSED) { |
| 7286 | return nullptr; |
| 7287 | } |
| 7288 | |
| 7289 | template <typename ... Types> |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7290 | static ArtMethod* FindSameNameAndSignature(MethodNameAndSignatureComparator& cmp, |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 7291 | const ScopedArenaVector<ArtMethod*>& list, |
| 7292 | const Types& ... rest) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 7293 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7294 | for (ArtMethod* method : list) { |
| 7295 | if (cmp.HasSameNameAndSignature(method)) { |
| 7296 | return method; |
| 7297 | } |
| 7298 | } |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 7299 | return FindSameNameAndSignature(cmp, rest...); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7300 | } |
| 7301 | |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 7302 | namespace { |
| 7303 | |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 7304 | // Check that all vtable entries are present in this class's virtuals or are the same as a |
| 7305 | // superclasses vtable entry. |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 7306 | void CheckClassOwnsVTableEntries(Thread* self, |
| 7307 | Handle<mirror::Class> klass, |
| 7308 | PointerSize pointer_size) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 7309 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 7310 | StackHandleScope<2> hs(self); |
| 7311 | Handle<mirror::PointerArray> check_vtable(hs.NewHandle(klass->GetVTableDuringLinking())); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7312 | ObjPtr<mirror::Class> super_temp = (klass->HasSuperClass()) ? klass->GetSuperClass() : nullptr; |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 7313 | Handle<mirror::Class> superclass(hs.NewHandle(super_temp)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 7314 | int32_t super_vtable_length = (superclass != nullptr) ? superclass->GetVTableLength() : 0; |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 7315 | for (int32_t i = 0; i < check_vtable->GetLength(); ++i) { |
| 7316 | ArtMethod* m = check_vtable->GetElementPtrSize<ArtMethod*>(i, pointer_size); |
| 7317 | CHECK(m != nullptr); |
| 7318 | |
Alex Light | a41a3078 | 2017-03-29 11:33:19 -0700 | [diff] [blame] | 7319 | if (m->GetMethodIndexDuringLinking() != i) { |
| 7320 | LOG(WARNING) << m->PrettyMethod() |
| 7321 | << " has an unexpected method index for its spot in the vtable for class" |
| 7322 | << klass->PrettyClass(); |
| 7323 | } |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 7324 | ArraySlice<ArtMethod> virtuals = klass->GetVirtualMethodsSliceUnchecked(pointer_size); |
| 7325 | auto is_same_method = [m] (const ArtMethod& meth) { |
| 7326 | return &meth == m; |
| 7327 | }; |
Alex Light | 3f98053 | 2017-03-17 15:10:32 -0700 | [diff] [blame] | 7328 | if (!((super_vtable_length > i && superclass->GetVTableEntry(i, pointer_size) == m) || |
| 7329 | std::find_if(virtuals.begin(), virtuals.end(), is_same_method) != virtuals.end())) { |
| 7330 | LOG(WARNING) << m->PrettyMethod() << " does not seem to be owned by current class " |
| 7331 | << klass->PrettyClass() << " or any of its superclasses!"; |
| 7332 | } |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 7333 | } |
| 7334 | } |
| 7335 | |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 7336 | // Check to make sure the vtable does not have duplicates. Duplicates could cause problems when a |
| 7337 | // method is overridden in a subclass. |
Andreas Gampe | a2fed08 | 2019-02-01 09:34:43 -0800 | [diff] [blame] | 7338 | template <PointerSize kPointerSize> |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 7339 | void CheckVTableHasNoDuplicates(Thread* self, Handle<mirror::Class> klass) |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 7340 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 7341 | StackHandleScope<1> hs(self); |
| 7342 | Handle<mirror::PointerArray> vtable(hs.NewHandle(klass->GetVTableDuringLinking())); |
| 7343 | int32_t num_entries = vtable->GetLength(); |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 7344 | |
| 7345 | // Observations: |
| 7346 | // * The older implementation was O(n^2) and got too expensive for apps with larger classes. |
| 7347 | // * Many classes do not override Object functions (e.g., equals/hashCode/toString). Thus, |
| 7348 | // for many classes outside of libcore a cross-dexfile check has to be run anyways. |
| 7349 | // * In the cross-dexfile case, with the O(n^2), in the best case O(n) cross checks would have |
| 7350 | // to be done. It is thus OK in a single-pass algorithm to read all data, anyways. |
| 7351 | // * The single-pass algorithm will trade memory for speed, but that is OK. |
| 7352 | |
| 7353 | CHECK_GT(num_entries, 0); |
| 7354 | |
| 7355 | auto log_fn = [&vtable, &klass](int32_t i, int32_t j) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 7356 | ArtMethod* m1 = vtable->GetElementPtrSize<ArtMethod*, kPointerSize>(i); |
| 7357 | ArtMethod* m2 = vtable->GetElementPtrSize<ArtMethod*, kPointerSize>(j); |
| 7358 | LOG(WARNING) << "vtable entries " << i << " and " << j << " are identical for " |
| 7359 | << klass->PrettyClass() << " in method " << m1->PrettyMethod() |
| 7360 | << " (0x" << std::hex << reinterpret_cast<uintptr_t>(m2) << ") and " |
| 7361 | << m2->PrettyMethod() << " (0x" << std::hex |
| 7362 | << reinterpret_cast<uintptr_t>(m2) << ")"; |
| 7363 | }; |
| 7364 | struct BaseHashType { |
| 7365 | static size_t HashCombine(size_t seed, size_t val) { |
| 7366 | return seed ^ (val + 0x9e3779b9 + (seed << 6) + (seed >> 2)); |
| 7367 | } |
| 7368 | }; |
| 7369 | |
| 7370 | // Check assuming all entries come from the same dex file. |
| 7371 | { |
| 7372 | // Find the first interesting method and its dex file. |
| 7373 | int32_t start = 0; |
| 7374 | for (; start < num_entries; ++start) { |
| 7375 | ArtMethod* vtable_entry = vtable->GetElementPtrSize<ArtMethod*, kPointerSize>(start); |
| 7376 | // Don't bother if we cannot 'see' the vtable entry (i.e. it is a package-private member |
| 7377 | // maybe). |
| 7378 | if (!klass->CanAccessMember(vtable_entry->GetDeclaringClass(), |
| 7379 | vtable_entry->GetAccessFlags())) { |
| 7380 | continue; |
| 7381 | } |
| 7382 | break; |
| 7383 | } |
| 7384 | if (start == num_entries) { |
| 7385 | return; |
| 7386 | } |
| 7387 | const DexFile* dex_file = |
| 7388 | vtable->GetElementPtrSize<ArtMethod*, kPointerSize>(start)-> |
| 7389 | GetInterfaceMethodIfProxy(kPointerSize)->GetDexFile(); |
| 7390 | |
| 7391 | // Helper function to avoid logging if we have to run the cross-file checks. |
| 7392 | auto check_fn = [&](bool log_warn) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 7393 | // Use a map to store seen entries, as the storage space is too large for a bitvector. |
| 7394 | using PairType = std::pair<uint32_t, uint16_t>; |
| 7395 | struct PairHash : BaseHashType { |
| 7396 | size_t operator()(const PairType& key) const { |
| 7397 | return BaseHashType::HashCombine(BaseHashType::HashCombine(0, key.first), key.second); |
| 7398 | } |
| 7399 | }; |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 7400 | HashMap<PairType, int32_t, DefaultMapEmptyFn<PairType, int32_t>, PairHash> seen; |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 7401 | seen.reserve(2 * num_entries); |
| 7402 | bool need_slow_path = false; |
| 7403 | bool found_dup = false; |
| 7404 | for (int i = start; i < num_entries; ++i) { |
| 7405 | // Can use Unchecked here as the start loop already ensured that the arrays are correct |
| 7406 | // wrt/ kPointerSize. |
| 7407 | ArtMethod* vtable_entry = vtable->GetElementPtrSizeUnchecked<ArtMethod*, kPointerSize>(i); |
| 7408 | if (!klass->CanAccessMember(vtable_entry->GetDeclaringClass(), |
| 7409 | vtable_entry->GetAccessFlags())) { |
| 7410 | continue; |
| 7411 | } |
| 7412 | ArtMethod* m = vtable_entry->GetInterfaceMethodIfProxy(kPointerSize); |
| 7413 | if (dex_file != m->GetDexFile()) { |
| 7414 | need_slow_path = true; |
| 7415 | break; |
| 7416 | } |
| 7417 | const dex::MethodId* m_mid = &dex_file->GetMethodId(m->GetDexMethodIndex()); |
| 7418 | PairType pair = std::make_pair(m_mid->name_idx_.index_, m_mid->proto_idx_.index_); |
| 7419 | auto it = seen.find(pair); |
| 7420 | if (it != seen.end()) { |
| 7421 | found_dup = true; |
| 7422 | if (log_warn) { |
| 7423 | log_fn(it->second, i); |
| 7424 | } |
| 7425 | } else { |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 7426 | seen.insert(std::make_pair(pair, i)); |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 7427 | } |
| 7428 | } |
| 7429 | return std::make_pair(need_slow_path, found_dup); |
| 7430 | }; |
| 7431 | std::pair<bool, bool> result = check_fn(/* log_warn= */ false); |
| 7432 | if (!result.first) { |
| 7433 | if (result.second) { |
| 7434 | check_fn(/* log_warn= */ true); |
| 7435 | } |
| 7436 | return; |
| 7437 | } |
| 7438 | } |
| 7439 | |
| 7440 | // Need to check across dex files. |
| 7441 | struct Entry { |
| 7442 | size_t cached_hash = 0; |
Vladimir Marko | aa027b8 | 2021-01-06 20:34:20 +0000 | [diff] [blame] | 7443 | uint32_t name_len = 0; |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 7444 | const char* name = nullptr; |
| 7445 | Signature signature = Signature::NoSignature(); |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 7446 | |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 7447 | Entry() = default; |
| 7448 | Entry(const Entry& other) = default; |
| 7449 | Entry& operator=(const Entry& other) = default; |
| 7450 | |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 7451 | Entry(const DexFile* dex_file, const dex::MethodId& mid) |
Vladimir Marko | aa027b8 | 2021-01-06 20:34:20 +0000 | [diff] [blame] | 7452 | : name_len(0), // Explicit to enforce ordering with -Werror,-Wreorder-ctor. |
| 7453 | // This call writes `name_len` and it is therefore necessary that the |
| 7454 | // initializer for `name_len` comes before it, otherwise the value |
| 7455 | // from the call would be overwritten by that initializer. |
| 7456 | name(dex_file->StringDataAndUtf16LengthByIdx(mid.name_idx_, &name_len)), |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 7457 | signature(dex_file->GetMethodSignature(mid)) { |
Vladimir Marko | aa027b8 | 2021-01-06 20:34:20 +0000 | [diff] [blame] | 7458 | // The `name_len` has been initialized to the UTF16 length. Calculate length in bytes. |
| 7459 | if (name[name_len] != 0) { |
| 7460 | name_len += strlen(name + name_len); |
| 7461 | } |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 7462 | } |
| 7463 | |
| 7464 | bool operator==(const Entry& other) const { |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 7465 | return name_len == other.name_len && |
| 7466 | memcmp(name, other.name, name_len) == 0 && |
| 7467 | signature == other.signature; |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 7468 | } |
| 7469 | }; |
| 7470 | struct EntryHash { |
| 7471 | size_t operator()(const Entry& key) const { |
| 7472 | return key.cached_hash; |
| 7473 | } |
| 7474 | }; |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 7475 | HashMap<Entry, int32_t, DefaultMapEmptyFn<Entry, int32_t>, EntryHash> map; |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 7476 | for (int32_t i = 0; i < num_entries; ++i) { |
| 7477 | // Can use Unchecked here as the first loop already ensured that the arrays are correct |
| 7478 | // wrt/ kPointerSize. |
| 7479 | ArtMethod* vtable_entry = vtable->GetElementPtrSizeUnchecked<ArtMethod*, kPointerSize>(i); |
| 7480 | // Don't bother if we cannot 'see' the vtable entry (i.e. it is a package-private member |
| 7481 | // maybe). |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 7482 | if (!klass->CanAccessMember(vtable_entry->GetDeclaringClass(), |
| 7483 | vtable_entry->GetAccessFlags())) { |
| 7484 | continue; |
| 7485 | } |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 7486 | ArtMethod* m = vtable_entry->GetInterfaceMethodIfProxy(kPointerSize); |
| 7487 | const DexFile* dex_file = m->GetDexFile(); |
| 7488 | const dex::MethodId& mid = dex_file->GetMethodId(m->GetDexMethodIndex()); |
| 7489 | |
| 7490 | Entry e(dex_file, mid); |
| 7491 | |
| 7492 | size_t string_hash = std::hash<std::string_view>()(std::string_view(e.name, e.name_len)); |
| 7493 | size_t sig_hash = std::hash<std::string>()(e.signature.ToString()); |
| 7494 | e.cached_hash = BaseHashType::HashCombine(BaseHashType::HashCombine(0u, string_hash), |
| 7495 | sig_hash); |
| 7496 | |
| 7497 | auto it = map.find(e); |
| 7498 | if (it != map.end()) { |
| 7499 | log_fn(it->second, i); |
| 7500 | } else { |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 7501 | map.insert(std::make_pair(e, i)); |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 7502 | } |
| 7503 | } |
| 7504 | } |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 7505 | |
| 7506 | void CheckVTableHasNoDuplicates(Thread* self, |
| 7507 | Handle<mirror::Class> klass, |
| 7508 | PointerSize pointer_size) |
Andreas Gampe | a2fed08 | 2019-02-01 09:34:43 -0800 | [diff] [blame] | 7509 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 7510 | switch (pointer_size) { |
| 7511 | case PointerSize::k64: |
| 7512 | CheckVTableHasNoDuplicates<PointerSize::k64>(self, klass); |
| 7513 | break; |
| 7514 | case PointerSize::k32: |
| 7515 | CheckVTableHasNoDuplicates<PointerSize::k32>(self, klass); |
| 7516 | break; |
| 7517 | } |
| 7518 | } |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 7519 | |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 7520 | static void CheckVTable(Thread* self, Handle<mirror::Class> klass, PointerSize pointer_size) |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 7521 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 7522 | CheckClassOwnsVTableEntries(self, klass, pointer_size); |
| 7523 | CheckVTableHasNoDuplicates(self, klass, pointer_size); |
| 7524 | } |
| 7525 | |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 7526 | } // namespace |
| 7527 | |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 7528 | void ClassLinker::FillImtFromSuperClass(Handle<mirror::Class> klass, |
| 7529 | ArtMethod* unimplemented_method, |
| 7530 | ArtMethod* imt_conflict_method, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 7531 | bool* new_conflict, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 7532 | ArtMethod** imt) { |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7533 | DCHECK(klass->HasSuperClass()); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7534 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 7535 | if (super_class->ShouldHaveImt()) { |
| 7536 | ImTable* super_imt = super_class->GetImt(image_pointer_size_); |
| 7537 | for (size_t i = 0; i < ImTable::kSize; ++i) { |
| 7538 | imt[i] = super_imt->Get(i, image_pointer_size_); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7539 | } |
| 7540 | } else { |
| 7541 | // No imt in the super class, need to reconstruct from the iftable. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7542 | ObjPtr<mirror::IfTable> if_table = super_class->GetIfTable(); |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 7543 | if (if_table->Count() != 0) { |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 7544 | // Ignore copied methods since we will handle these in LinkInterfaceMethods. |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 7545 | FillIMTFromIfTable(if_table, |
| 7546 | unimplemented_method, |
| 7547 | imt_conflict_method, |
| 7548 | klass.Get(), |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 7549 | /*create_conflict_tables=*/false, |
| 7550 | /*ignore_copied_methods=*/true, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 7551 | /*out*/new_conflict, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 7552 | /*out*/imt); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7553 | } |
| 7554 | } |
| 7555 | } |
| 7556 | |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7557 | class ClassLinker::LinkInterfaceMethodsHelper { |
| 7558 | public: |
| 7559 | LinkInterfaceMethodsHelper(ClassLinker* class_linker, |
| 7560 | Handle<mirror::Class> klass, |
| 7561 | Thread* self, |
| 7562 | Runtime* runtime) |
| 7563 | : class_linker_(class_linker), |
| 7564 | klass_(klass), |
| 7565 | method_alignment_(ArtMethod::Alignment(class_linker->GetImagePointerSize())), |
| 7566 | method_size_(ArtMethod::Size(class_linker->GetImagePointerSize())), |
| 7567 | self_(self), |
| 7568 | stack_(runtime->GetLinearAlloc()->GetArenaPool()), |
| 7569 | allocator_(&stack_), |
| 7570 | default_conflict_methods_(allocator_.Adapter()), |
| 7571 | overriding_default_conflict_methods_(allocator_.Adapter()), |
| 7572 | miranda_methods_(allocator_.Adapter()), |
| 7573 | default_methods_(allocator_.Adapter()), |
| 7574 | overriding_default_methods_(allocator_.Adapter()), |
| 7575 | move_table_(allocator_.Adapter()) { |
| 7576 | } |
| 7577 | |
| 7578 | ArtMethod* FindMethod(ArtMethod* interface_method, |
| 7579 | MethodNameAndSignatureComparator& interface_name_comparator, |
| 7580 | ArtMethod* vtable_impl) |
| 7581 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 7582 | |
| 7583 | ArtMethod* GetOrCreateMirandaMethod(ArtMethod* interface_method, |
| 7584 | MethodNameAndSignatureComparator& interface_name_comparator) |
| 7585 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 7586 | |
| 7587 | bool HasNewVirtuals() const { |
| 7588 | return !(miranda_methods_.empty() && |
| 7589 | default_methods_.empty() && |
| 7590 | overriding_default_methods_.empty() && |
| 7591 | overriding_default_conflict_methods_.empty() && |
| 7592 | default_conflict_methods_.empty()); |
| 7593 | } |
| 7594 | |
| 7595 | void ReallocMethods() REQUIRES_SHARED(Locks::mutator_lock_); |
| 7596 | |
| 7597 | ObjPtr<mirror::PointerArray> UpdateVtable( |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 7598 | const HashMap<size_t, ClassLinker::MethodTranslation>& default_translations, |
Vladimir Marko | 3068d58 | 2019-05-28 16:39:29 +0100 | [diff] [blame] | 7599 | Handle<mirror::PointerArray> old_vtable) REQUIRES_SHARED(Locks::mutator_lock_); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7600 | |
| 7601 | void UpdateIfTable(Handle<mirror::IfTable> iftable) REQUIRES_SHARED(Locks::mutator_lock_); |
| 7602 | |
| 7603 | void UpdateIMT(ArtMethod** out_imt); |
| 7604 | |
| 7605 | void CheckNoStaleMethodsInDexCache() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 7606 | if (kIsDebugBuild) { |
| 7607 | PointerSize pointer_size = class_linker_->GetImagePointerSize(); |
| 7608 | // Check that there are no stale methods are in the dex cache array. |
| 7609 | auto* resolved_methods = klass_->GetDexCache()->GetResolvedMethods(); |
| 7610 | for (size_t i = 0, count = klass_->GetDexCache()->NumResolvedMethods(); i < count; ++i) { |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 7611 | auto pair = mirror::DexCache::GetNativePairPtrSize(resolved_methods, i, pointer_size); |
| 7612 | ArtMethod* m = pair.object; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7613 | CHECK(move_table_.find(m) == move_table_.end() || |
| 7614 | // The original versions of copied methods will still be present so allow those too. |
| 7615 | // Note that if the first check passes this might fail to GetDeclaringClass(). |
| 7616 | std::find_if(m->GetDeclaringClass()->GetMethods(pointer_size).begin(), |
| 7617 | m->GetDeclaringClass()->GetMethods(pointer_size).end(), |
| 7618 | [m] (ArtMethod& meth) { |
| 7619 | return &meth == m; |
| 7620 | }) != m->GetDeclaringClass()->GetMethods(pointer_size).end()) |
| 7621 | << "Obsolete method " << m->PrettyMethod() << " is in dex cache!"; |
| 7622 | } |
| 7623 | } |
| 7624 | } |
| 7625 | |
| 7626 | void ClobberOldMethods(LengthPrefixedArray<ArtMethod>* old_methods, |
| 7627 | LengthPrefixedArray<ArtMethod>* methods) { |
| 7628 | if (kIsDebugBuild) { |
| 7629 | CHECK(methods != nullptr); |
| 7630 | // Put some random garbage in old methods to help find stale pointers. |
| 7631 | if (methods != old_methods && old_methods != nullptr) { |
| 7632 | // Need to make sure the GC is not running since it could be scanning the methods we are |
| 7633 | // about to overwrite. |
| 7634 | ScopedThreadStateChange tsc(self_, kSuspended); |
| 7635 | gc::ScopedGCCriticalSection gcs(self_, |
| 7636 | gc::kGcCauseClassLinker, |
| 7637 | gc::kCollectorTypeClassLinker); |
| 7638 | const size_t old_size = LengthPrefixedArray<ArtMethod>::ComputeSize(old_methods->size(), |
| 7639 | method_size_, |
| 7640 | method_alignment_); |
| 7641 | memset(old_methods, 0xFEu, old_size); |
| 7642 | } |
| 7643 | } |
| 7644 | } |
| 7645 | |
| 7646 | private: |
| 7647 | size_t NumberOfNewVirtuals() const { |
| 7648 | return miranda_methods_.size() + |
| 7649 | default_methods_.size() + |
| 7650 | overriding_default_conflict_methods_.size() + |
| 7651 | overriding_default_methods_.size() + |
| 7652 | default_conflict_methods_.size(); |
| 7653 | } |
| 7654 | |
| 7655 | bool FillTables() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 7656 | return !klass_->IsInterface(); |
| 7657 | } |
| 7658 | |
| 7659 | void LogNewVirtuals() const REQUIRES_SHARED(Locks::mutator_lock_) { |
| 7660 | DCHECK(!klass_->IsInterface() || (default_methods_.empty() && miranda_methods_.empty())) |
| 7661 | << "Interfaces should only have default-conflict methods appended to them."; |
| 7662 | VLOG(class_linker) << mirror::Class::PrettyClass(klass_.Get()) << ": miranda_methods=" |
| 7663 | << miranda_methods_.size() |
| 7664 | << " default_methods=" << default_methods_.size() |
| 7665 | << " overriding_default_methods=" << overriding_default_methods_.size() |
| 7666 | << " default_conflict_methods=" << default_conflict_methods_.size() |
| 7667 | << " overriding_default_conflict_methods=" |
| 7668 | << overriding_default_conflict_methods_.size(); |
| 7669 | } |
| 7670 | |
| 7671 | ClassLinker* class_linker_; |
| 7672 | Handle<mirror::Class> klass_; |
| 7673 | size_t method_alignment_; |
| 7674 | size_t method_size_; |
| 7675 | Thread* const self_; |
| 7676 | |
| 7677 | // These are allocated on the heap to begin, we then transfer to linear alloc when we re-create |
| 7678 | // the virtual methods array. |
| 7679 | // Need to use low 4GB arenas for compiler or else the pointers wont fit in 32 bit method array |
| 7680 | // during cross compilation. |
| 7681 | // Use the linear alloc pool since this one is in the low 4gb for the compiler. |
| 7682 | ArenaStack stack_; |
| 7683 | ScopedArenaAllocator allocator_; |
| 7684 | |
| 7685 | ScopedArenaVector<ArtMethod*> default_conflict_methods_; |
| 7686 | ScopedArenaVector<ArtMethod*> overriding_default_conflict_methods_; |
| 7687 | ScopedArenaVector<ArtMethod*> miranda_methods_; |
| 7688 | ScopedArenaVector<ArtMethod*> default_methods_; |
| 7689 | ScopedArenaVector<ArtMethod*> overriding_default_methods_; |
| 7690 | |
| 7691 | ScopedArenaUnorderedMap<ArtMethod*, ArtMethod*> move_table_; |
| 7692 | }; |
| 7693 | |
| 7694 | ArtMethod* ClassLinker::LinkInterfaceMethodsHelper::FindMethod( |
| 7695 | ArtMethod* interface_method, |
| 7696 | MethodNameAndSignatureComparator& interface_name_comparator, |
| 7697 | ArtMethod* vtable_impl) { |
| 7698 | ArtMethod* current_method = nullptr; |
| 7699 | switch (class_linker_->FindDefaultMethodImplementation(self_, |
| 7700 | interface_method, |
| 7701 | klass_, |
| 7702 | /*out*/¤t_method)) { |
| 7703 | case DefaultMethodSearchResult::kDefaultConflict: { |
| 7704 | // Default method conflict. |
| 7705 | DCHECK(current_method == nullptr); |
| 7706 | ArtMethod* default_conflict_method = nullptr; |
| 7707 | if (vtable_impl != nullptr && vtable_impl->IsDefaultConflicting()) { |
| 7708 | // We can reuse the method from the superclass, don't bother adding it to virtuals. |
| 7709 | default_conflict_method = vtable_impl; |
| 7710 | } else { |
| 7711 | // See if we already have a conflict method for this method. |
| 7712 | ArtMethod* preexisting_conflict = FindSameNameAndSignature( |
| 7713 | interface_name_comparator, |
| 7714 | default_conflict_methods_, |
| 7715 | overriding_default_conflict_methods_); |
| 7716 | if (LIKELY(preexisting_conflict != nullptr)) { |
| 7717 | // We already have another conflict we can reuse. |
| 7718 | default_conflict_method = preexisting_conflict; |
| 7719 | } else { |
| 7720 | // Note that we do this even if we are an interface since we need to create this and |
| 7721 | // cannot reuse another classes. |
| 7722 | // Create a new conflict method for this to use. |
| 7723 | default_conflict_method = reinterpret_cast<ArtMethod*>(allocator_.Alloc(method_size_)); |
| 7724 | new(default_conflict_method) ArtMethod(interface_method, |
| 7725 | class_linker_->GetImagePointerSize()); |
| 7726 | if (vtable_impl == nullptr) { |
| 7727 | // Save the conflict method. We need to add it to the vtable. |
| 7728 | default_conflict_methods_.push_back(default_conflict_method); |
| 7729 | } else { |
| 7730 | // Save the conflict method but it is already in the vtable. |
| 7731 | overriding_default_conflict_methods_.push_back(default_conflict_method); |
| 7732 | } |
| 7733 | } |
| 7734 | } |
| 7735 | current_method = default_conflict_method; |
| 7736 | break; |
| 7737 | } // case kDefaultConflict |
| 7738 | case DefaultMethodSearchResult::kDefaultFound: { |
| 7739 | DCHECK(current_method != nullptr); |
| 7740 | // Found a default method. |
| 7741 | if (vtable_impl != nullptr && |
| 7742 | current_method->GetDeclaringClass() == vtable_impl->GetDeclaringClass()) { |
| 7743 | // We found a default method but it was the same one we already have from our |
| 7744 | // superclass. Don't bother adding it to our vtable again. |
| 7745 | current_method = vtable_impl; |
| 7746 | } else if (LIKELY(FillTables())) { |
| 7747 | // Interfaces don't need to copy default methods since they don't have vtables. |
| 7748 | // Only record this default method if it is new to save space. |
| 7749 | // TODO It might be worthwhile to copy default methods on interfaces anyway since it |
| 7750 | // would make lookup for interface super much faster. (We would only need to scan |
| 7751 | // the iftable to find if there is a NSME or AME.) |
| 7752 | ArtMethod* old = FindSameNameAndSignature(interface_name_comparator, |
| 7753 | default_methods_, |
| 7754 | overriding_default_methods_); |
| 7755 | if (old == nullptr) { |
| 7756 | // We found a default method implementation and there were no conflicts. |
| 7757 | if (vtable_impl == nullptr) { |
| 7758 | // Save the default method. We need to add it to the vtable. |
| 7759 | default_methods_.push_back(current_method); |
| 7760 | } else { |
| 7761 | // Save the default method but it is already in the vtable. |
| 7762 | overriding_default_methods_.push_back(current_method); |
| 7763 | } |
| 7764 | } else { |
| 7765 | CHECK(old == current_method) << "Multiple default implementations selected!"; |
| 7766 | } |
| 7767 | } |
| 7768 | break; |
| 7769 | } // case kDefaultFound |
| 7770 | case DefaultMethodSearchResult::kAbstractFound: { |
| 7771 | DCHECK(current_method == nullptr); |
| 7772 | // Abstract method masks all defaults. |
| 7773 | if (vtable_impl != nullptr && |
| 7774 | vtable_impl->IsAbstract() && |
| 7775 | !vtable_impl->IsDefaultConflicting()) { |
| 7776 | // We need to make this an abstract method but the version in the vtable already is so |
| 7777 | // don't do anything. |
| 7778 | current_method = vtable_impl; |
| 7779 | } |
| 7780 | break; |
| 7781 | } // case kAbstractFound |
| 7782 | } |
| 7783 | return current_method; |
| 7784 | } |
| 7785 | |
| 7786 | ArtMethod* ClassLinker::LinkInterfaceMethodsHelper::GetOrCreateMirandaMethod( |
| 7787 | ArtMethod* interface_method, |
| 7788 | MethodNameAndSignatureComparator& interface_name_comparator) { |
| 7789 | // Find out if there is already a miranda method we can use. |
| 7790 | ArtMethod* miranda_method = FindSameNameAndSignature(interface_name_comparator, |
| 7791 | miranda_methods_); |
| 7792 | if (miranda_method == nullptr) { |
| 7793 | DCHECK(interface_method->IsAbstract()) << interface_method->PrettyMethod(); |
| 7794 | miranda_method = reinterpret_cast<ArtMethod*>(allocator_.Alloc(method_size_)); |
| 7795 | CHECK(miranda_method != nullptr); |
| 7796 | // Point the interface table at a phantom slot. |
| 7797 | new(miranda_method) ArtMethod(interface_method, class_linker_->GetImagePointerSize()); |
| 7798 | miranda_methods_.push_back(miranda_method); |
| 7799 | } |
| 7800 | return miranda_method; |
| 7801 | } |
| 7802 | |
| 7803 | void ClassLinker::LinkInterfaceMethodsHelper::ReallocMethods() { |
| 7804 | LogNewVirtuals(); |
| 7805 | |
| 7806 | const size_t old_method_count = klass_->NumMethods(); |
| 7807 | const size_t new_method_count = old_method_count + NumberOfNewVirtuals(); |
| 7808 | DCHECK_NE(old_method_count, new_method_count); |
| 7809 | |
| 7810 | // Attempt to realloc to save RAM if possible. |
| 7811 | LengthPrefixedArray<ArtMethod>* old_methods = klass_->GetMethodsPtr(); |
| 7812 | // The Realloced virtual methods aren't visible from the class roots, so there is no issue |
| 7813 | // where GCs could attempt to mark stale pointers due to memcpy. And since we overwrite the |
| 7814 | // realloced memory with out->CopyFrom, we are guaranteed to have objects in the to space since |
| 7815 | // CopyFrom has internal read barriers. |
| 7816 | // |
| 7817 | // TODO We should maybe move some of this into mirror::Class or at least into another method. |
| 7818 | const size_t old_size = LengthPrefixedArray<ArtMethod>::ComputeSize(old_method_count, |
| 7819 | method_size_, |
| 7820 | method_alignment_); |
| 7821 | const size_t new_size = LengthPrefixedArray<ArtMethod>::ComputeSize(new_method_count, |
| 7822 | method_size_, |
| 7823 | method_alignment_); |
| 7824 | const size_t old_methods_ptr_size = (old_methods != nullptr) ? old_size : 0; |
| 7825 | auto* methods = reinterpret_cast<LengthPrefixedArray<ArtMethod>*>( |
Nicolas Geoffray | 48b40cc | 2017-08-07 16:52:40 +0100 | [diff] [blame] | 7826 | class_linker_->GetAllocatorForClassLoader(klass_->GetClassLoader())->Realloc( |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7827 | self_, old_methods, old_methods_ptr_size, new_size)); |
| 7828 | CHECK(methods != nullptr); // Native allocation failure aborts. |
| 7829 | |
| 7830 | PointerSize pointer_size = class_linker_->GetImagePointerSize(); |
| 7831 | if (methods != old_methods) { |
| 7832 | // Maps from heap allocated miranda method to linear alloc miranda method. |
| 7833 | StrideIterator<ArtMethod> out = methods->begin(method_size_, method_alignment_); |
| 7834 | // Copy over the old methods. |
| 7835 | for (auto& m : klass_->GetMethods(pointer_size)) { |
| 7836 | move_table_.emplace(&m, &*out); |
| 7837 | // The CopyFrom is only necessary to not miss read barriers since Realloc won't do read |
| 7838 | // barriers when it copies. |
| 7839 | out->CopyFrom(&m, pointer_size); |
| 7840 | ++out; |
| 7841 | } |
| 7842 | } |
| 7843 | StrideIterator<ArtMethod> out(methods->begin(method_size_, method_alignment_) + old_method_count); |
| 7844 | // Copy over miranda methods before copying vtable since CopyOf may cause thread suspension and |
| 7845 | // we want the roots of the miranda methods to get visited. |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7846 | for (size_t i = 0; i < miranda_methods_.size(); ++i) { |
| 7847 | ArtMethod* mir_method = miranda_methods_[i]; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7848 | ArtMethod& new_method = *out; |
| 7849 | new_method.CopyFrom(mir_method, pointer_size); |
| 7850 | new_method.SetAccessFlags(new_method.GetAccessFlags() | kAccMiranda | kAccCopied); |
| 7851 | DCHECK_NE(new_method.GetAccessFlags() & kAccAbstract, 0u) |
| 7852 | << "Miranda method should be abstract!"; |
| 7853 | move_table_.emplace(mir_method, &new_method); |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7854 | // Update the entry in the method array, as the array will be used for future lookups, |
| 7855 | // where thread suspension is allowed. |
| 7856 | // As such, the array should not contain locally allocated ArtMethod, otherwise the GC |
| 7857 | // would not see them. |
| 7858 | miranda_methods_[i] = &new_method; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7859 | ++out; |
| 7860 | } |
| 7861 | // We need to copy the default methods into our own method table since the runtime requires that |
| 7862 | // every method on a class's vtable be in that respective class's virtual method table. |
| 7863 | // NOTE This means that two classes might have the same implementation of a method from the same |
| 7864 | // interface but will have different ArtMethod*s for them. This also means we cannot compare a |
| 7865 | // default method found on a class with one found on the declaring interface directly and must |
| 7866 | // look at the declaring class to determine if they are the same. |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7867 | for (ScopedArenaVector<ArtMethod*>* methods_vec : {&default_methods_, |
| 7868 | &overriding_default_methods_}) { |
| 7869 | for (size_t i = 0; i < methods_vec->size(); ++i) { |
| 7870 | ArtMethod* def_method = (*methods_vec)[i]; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7871 | ArtMethod& new_method = *out; |
| 7872 | new_method.CopyFrom(def_method, pointer_size); |
| 7873 | // Clear the kAccSkipAccessChecks flag if it is present. Since this class hasn't been |
| 7874 | // verified yet it shouldn't have methods that are skipping access checks. |
| 7875 | // TODO This is rather arbitrary. We should maybe support classes where only some of its |
| 7876 | // methods are skip_access_checks. |
Vladimir Marko | b0a6aee | 2017-10-27 10:34:04 +0100 | [diff] [blame] | 7877 | DCHECK_EQ(new_method.GetAccessFlags() & kAccNative, 0u); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7878 | constexpr uint32_t kSetFlags = kAccDefault | kAccCopied; |
| 7879 | constexpr uint32_t kMaskFlags = ~kAccSkipAccessChecks; |
| 7880 | new_method.SetAccessFlags((new_method.GetAccessFlags() | kSetFlags) & kMaskFlags); |
| 7881 | move_table_.emplace(def_method, &new_method); |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7882 | // Update the entry in the method array, as the array will be used for future lookups, |
| 7883 | // where thread suspension is allowed. |
| 7884 | // As such, the array should not contain locally allocated ArtMethod, otherwise the GC |
| 7885 | // would not see them. |
| 7886 | (*methods_vec)[i] = &new_method; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7887 | ++out; |
| 7888 | } |
| 7889 | } |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7890 | for (ScopedArenaVector<ArtMethod*>* methods_vec : {&default_conflict_methods_, |
| 7891 | &overriding_default_conflict_methods_}) { |
| 7892 | for (size_t i = 0; i < methods_vec->size(); ++i) { |
| 7893 | ArtMethod* conf_method = (*methods_vec)[i]; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7894 | ArtMethod& new_method = *out; |
| 7895 | new_method.CopyFrom(conf_method, pointer_size); |
| 7896 | // This is a type of default method (there are default method impls, just a conflict) so |
| 7897 | // mark this as a default, non-abstract method, since thats what it is. Also clear the |
| 7898 | // kAccSkipAccessChecks bit since this class hasn't been verified yet it shouldn't have |
| 7899 | // methods that are skipping access checks. |
Nicolas Geoffray | 7aca9d5 | 2018-09-07 11:13:33 +0100 | [diff] [blame] | 7900 | // Also clear potential kAccSingleImplementation to avoid CHA trying to inline |
| 7901 | // the default method. |
Vladimir Marko | b0a6aee | 2017-10-27 10:34:04 +0100 | [diff] [blame] | 7902 | DCHECK_EQ(new_method.GetAccessFlags() & kAccNative, 0u); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7903 | constexpr uint32_t kSetFlags = kAccDefault | kAccDefaultConflict | kAccCopied; |
Nicolas Geoffray | 7aca9d5 | 2018-09-07 11:13:33 +0100 | [diff] [blame] | 7904 | constexpr uint32_t kMaskFlags = |
| 7905 | ~(kAccAbstract | kAccSkipAccessChecks | kAccSingleImplementation); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7906 | new_method.SetAccessFlags((new_method.GetAccessFlags() | kSetFlags) & kMaskFlags); |
| 7907 | DCHECK(new_method.IsDefaultConflicting()); |
| 7908 | // The actual method might or might not be marked abstract since we just copied it from a |
| 7909 | // (possibly default) interface method. We need to set it entry point to be the bridge so |
| 7910 | // that the compiler will not invoke the implementation of whatever method we copied from. |
| 7911 | EnsureThrowsInvocationError(class_linker_, &new_method); |
| 7912 | move_table_.emplace(conf_method, &new_method); |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7913 | // Update the entry in the method array, as the array will be used for future lookups, |
| 7914 | // where thread suspension is allowed. |
| 7915 | // As such, the array should not contain locally allocated ArtMethod, otherwise the GC |
| 7916 | // would not see them. |
| 7917 | (*methods_vec)[i] = &new_method; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7918 | ++out; |
| 7919 | } |
| 7920 | } |
| 7921 | methods->SetSize(new_method_count); |
| 7922 | class_linker_->UpdateClassMethods(klass_.Get(), methods); |
| 7923 | } |
| 7924 | |
| 7925 | ObjPtr<mirror::PointerArray> ClassLinker::LinkInterfaceMethodsHelper::UpdateVtable( |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 7926 | const HashMap<size_t, ClassLinker::MethodTranslation>& default_translations, |
Vladimir Marko | 3068d58 | 2019-05-28 16:39:29 +0100 | [diff] [blame] | 7927 | Handle<mirror::PointerArray> old_vtable) { |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7928 | // Update the vtable to the new method structures. We can skip this for interfaces since they |
| 7929 | // do not have vtables. |
| 7930 | const size_t old_vtable_count = old_vtable->GetLength(); |
| 7931 | const size_t new_vtable_count = old_vtable_count + |
| 7932 | miranda_methods_.size() + |
| 7933 | default_methods_.size() + |
| 7934 | default_conflict_methods_.size(); |
| 7935 | |
Vladimir Marko | 3068d58 | 2019-05-28 16:39:29 +0100 | [diff] [blame] | 7936 | ObjPtr<mirror::PointerArray> vtable = ObjPtr<mirror::PointerArray>::DownCast( |
| 7937 | mirror::Array::CopyOf(old_vtable, self_, new_vtable_count)); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7938 | if (UNLIKELY(vtable == nullptr)) { |
| 7939 | self_->AssertPendingOOMException(); |
| 7940 | return nullptr; |
| 7941 | } |
| 7942 | |
| 7943 | size_t vtable_pos = old_vtable_count; |
| 7944 | PointerSize pointer_size = class_linker_->GetImagePointerSize(); |
| 7945 | // Update all the newly copied method's indexes so they denote their placement in the vtable. |
| 7946 | for (const ScopedArenaVector<ArtMethod*>& methods_vec : {default_methods_, |
| 7947 | default_conflict_methods_, |
| 7948 | miranda_methods_}) { |
| 7949 | // These are the functions that are not already in the vtable! |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7950 | for (ArtMethod* new_vtable_method : methods_vec) { |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7951 | // Leave the declaring class alone the method's dex_code_item_offset_ and dex_method_index_ |
| 7952 | // fields are references into the dex file the method was defined in. Since the ArtMethod |
| 7953 | // does not store that information it uses declaring_class_->dex_cache_. |
| 7954 | new_vtable_method->SetMethodIndex(0xFFFF & vtable_pos); |
| 7955 | vtable->SetElementPtrSize(vtable_pos, new_vtable_method, pointer_size); |
| 7956 | ++vtable_pos; |
| 7957 | } |
| 7958 | } |
| 7959 | DCHECK_EQ(vtable_pos, new_vtable_count); |
| 7960 | |
| 7961 | // Update old vtable methods. We use the default_translations map to figure out what each |
| 7962 | // vtable entry should be updated to, if they need to be at all. |
| 7963 | for (size_t i = 0; i < old_vtable_count; ++i) { |
| 7964 | ArtMethod* translated_method = vtable->GetElementPtrSize<ArtMethod*>(i, pointer_size); |
| 7965 | // Try and find what we need to change this method to. |
| 7966 | auto translation_it = default_translations.find(i); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7967 | if (translation_it != default_translations.end()) { |
| 7968 | if (translation_it->second.IsInConflict()) { |
| 7969 | // Find which conflict method we are to use for this method. |
| 7970 | MethodNameAndSignatureComparator old_method_comparator( |
| 7971 | translated_method->GetInterfaceMethodIfProxy(pointer_size)); |
| 7972 | // We only need to look through overriding_default_conflict_methods since this is an |
| 7973 | // overridden method we are fixing up here. |
| 7974 | ArtMethod* new_conflict_method = FindSameNameAndSignature( |
| 7975 | old_method_comparator, overriding_default_conflict_methods_); |
| 7976 | CHECK(new_conflict_method != nullptr) << "Expected a conflict method!"; |
| 7977 | translated_method = new_conflict_method; |
| 7978 | } else if (translation_it->second.IsAbstract()) { |
| 7979 | // Find which miranda method we are to use for this method. |
| 7980 | MethodNameAndSignatureComparator old_method_comparator( |
| 7981 | translated_method->GetInterfaceMethodIfProxy(pointer_size)); |
| 7982 | ArtMethod* miranda_method = FindSameNameAndSignature(old_method_comparator, |
| 7983 | miranda_methods_); |
| 7984 | DCHECK(miranda_method != nullptr); |
| 7985 | translated_method = miranda_method; |
| 7986 | } else { |
| 7987 | // Normal default method (changed from an older default or abstract interface method). |
| 7988 | DCHECK(translation_it->second.IsTranslation()); |
| 7989 | translated_method = translation_it->second.GetTranslation(); |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7990 | auto it = move_table_.find(translated_method); |
| 7991 | DCHECK(it != move_table_.end()); |
| 7992 | translated_method = it->second; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7993 | } |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7994 | } else { |
| 7995 | auto it = move_table_.find(translated_method); |
| 7996 | translated_method = (it != move_table_.end()) ? it->second : nullptr; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7997 | } |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7998 | |
| 7999 | if (translated_method != nullptr) { |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 8000 | // Make sure the new_methods index is set. |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 8001 | if (translated_method->GetMethodIndexDuringLinking() != i) { |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 8002 | if (kIsDebugBuild) { |
| 8003 | auto* methods = klass_->GetMethodsPtr(); |
| 8004 | CHECK_LE(reinterpret_cast<uintptr_t>(&*methods->begin(method_size_, method_alignment_)), |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 8005 | reinterpret_cast<uintptr_t>(translated_method)); |
| 8006 | CHECK_LT(reinterpret_cast<uintptr_t>(translated_method), |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 8007 | reinterpret_cast<uintptr_t>(&*methods->end(method_size_, method_alignment_))); |
| 8008 | } |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 8009 | translated_method->SetMethodIndex(0xFFFF & i); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 8010 | } |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 8011 | vtable->SetElementPtrSize(i, translated_method, pointer_size); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 8012 | } |
| 8013 | } |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 8014 | klass_->SetVTable(vtable); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 8015 | return vtable; |
| 8016 | } |
| 8017 | |
| 8018 | void ClassLinker::LinkInterfaceMethodsHelper::UpdateIfTable(Handle<mirror::IfTable> iftable) { |
| 8019 | PointerSize pointer_size = class_linker_->GetImagePointerSize(); |
| 8020 | const size_t ifcount = klass_->GetIfTableCount(); |
| 8021 | // Go fix up all the stale iftable pointers. |
| 8022 | for (size_t i = 0; i < ifcount; ++i) { |
| 8023 | for (size_t j = 0, count = iftable->GetMethodArrayCount(i); j < count; ++j) { |
Vladimir Marko | 557fece | 2019-03-26 14:29:41 +0000 | [diff] [blame] | 8024 | ObjPtr<mirror::PointerArray> method_array = iftable->GetMethodArray(i); |
| 8025 | ArtMethod* m = method_array->GetElementPtrSize<ArtMethod*>(j, pointer_size); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 8026 | DCHECK(m != nullptr) << klass_->PrettyClass(); |
| 8027 | auto it = move_table_.find(m); |
| 8028 | if (it != move_table_.end()) { |
| 8029 | auto* new_m = it->second; |
| 8030 | DCHECK(new_m != nullptr) << klass_->PrettyClass(); |
| 8031 | method_array->SetElementPtrSize(j, new_m, pointer_size); |
| 8032 | } |
| 8033 | } |
| 8034 | } |
| 8035 | } |
| 8036 | |
| 8037 | void ClassLinker::LinkInterfaceMethodsHelper::UpdateIMT(ArtMethod** out_imt) { |
| 8038 | // Fix up IMT next. |
| 8039 | for (size_t i = 0; i < ImTable::kSize; ++i) { |
| 8040 | auto it = move_table_.find(out_imt[i]); |
| 8041 | if (it != move_table_.end()) { |
| 8042 | out_imt[i] = it->second; |
| 8043 | } |
| 8044 | } |
| 8045 | } |
| 8046 | |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8047 | // TODO This method needs to be split up into several smaller methods. |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 8048 | bool ClassLinker::LinkInterfaceMethods( |
| 8049 | Thread* self, |
| 8050 | Handle<mirror::Class> klass, |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 8051 | const HashMap<size_t, ClassLinker::MethodTranslation>& default_translations, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 8052 | bool* out_new_conflict, |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 8053 | ArtMethod** out_imt) { |
| 8054 | StackHandleScope<3> hs(self); |
| 8055 | Runtime* const runtime = Runtime::Current(); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8056 | |
| 8057 | const bool is_interface = klass->IsInterface(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 8058 | const bool has_superclass = klass->HasSuperClass(); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8059 | const bool fill_tables = !is_interface; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 8060 | const size_t super_ifcount = has_superclass ? klass->GetSuperClass()->GetIfTableCount() : 0U; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 8061 | const size_t ifcount = klass->GetIfTableCount(); |
| 8062 | |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 8063 | Handle<mirror::IfTable> iftable(hs.NewHandle(klass->GetIfTable())); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8064 | |
| 8065 | MutableHandle<mirror::PointerArray> vtable(hs.NewHandle(klass->GetVTableDuringLinking())); |
| 8066 | ArtMethod* const unimplemented_method = runtime->GetImtUnimplementedMethod(); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 8067 | ArtMethod* const imt_conflict_method = runtime->GetImtConflictMethod(); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 8068 | // Copy the IMT from the super class if possible. |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8069 | const bool extend_super_iftable = has_superclass; |
| 8070 | if (has_superclass && fill_tables) { |
| 8071 | FillImtFromSuperClass(klass, |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8072 | unimplemented_method, |
| 8073 | imt_conflict_method, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 8074 | out_new_conflict, |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 8075 | out_imt); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 8076 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8077 | // Allocate method arrays before since we don't want miss visiting miranda method roots due to |
| 8078 | // thread suspension. |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8079 | if (fill_tables) { |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 8080 | if (!AllocateIfTableMethodArrays(self, klass, iftable)) { |
| 8081 | return false; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8082 | } |
| 8083 | } |
| 8084 | |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 8085 | LinkInterfaceMethodsHelper helper(this, klass, self, runtime); |
| 8086 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 8087 | auto* old_cause = self->StartAssertNoThreadSuspension( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8088 | "Copying ArtMethods for LinkInterfaceMethods"); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 8089 | // Going in reverse to ensure that we will hit abstract methods that override defaults before the |
| 8090 | // defaults. This means we don't need to do any trickery when creating the Miranda methods, since |
| 8091 | // they will already be null. This has the additional benefit that the declarer of a miranda |
| 8092 | // method will actually declare an abstract method. |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 8093 | for (size_t i = ifcount; i != 0u; ) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 8094 | --i; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 8095 | DCHECK_LT(i, ifcount); |
| 8096 | |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8097 | size_t num_methods = iftable->GetInterface(i)->NumDeclaredVirtualMethods(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8098 | if (num_methods > 0) { |
| 8099 | StackHandleScope<2> hs2(self); |
| 8100 | const bool is_super = i < super_ifcount; |
| 8101 | const bool super_interface = is_super && extend_super_iftable; |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8102 | // We don't actually create or fill these tables for interfaces, we just copy some methods for |
| 8103 | // conflict methods. Just set this as nullptr in those cases. |
| 8104 | Handle<mirror::PointerArray> method_array(fill_tables |
| 8105 | ? hs2.NewHandle(iftable->GetMethodArray(i)) |
| 8106 | : hs2.NewHandle<mirror::PointerArray>(nullptr)); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8107 | |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 8108 | ArraySlice<ArtMethod> input_virtual_methods; |
Mathieu Chartier | 9865bde | 2015-12-21 09:58:16 -0800 | [diff] [blame] | 8109 | ScopedNullHandle<mirror::PointerArray> null_handle; |
| 8110 | Handle<mirror::PointerArray> input_vtable_array(null_handle); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8111 | int32_t input_array_length = 0; |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 8112 | |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 8113 | // TODO Cleanup Needed: In the presence of default methods this optimization is rather dirty |
| 8114 | // and confusing. Default methods should always look through all the superclasses |
| 8115 | // because they are the last choice of an implementation. We get around this by looking |
| 8116 | // at the super-classes iftable methods (copied into method_array previously) when we are |
| 8117 | // 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] | 8118 | bool using_virtuals; |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8119 | if (super_interface || is_interface) { |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 8120 | // 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] | 8121 | // whole vtable. |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 8122 | using_virtuals = true; |
Alex Light | a467a6e | 2020-03-23 16:07:29 -0700 | [diff] [blame] | 8123 | input_virtual_methods = klass->GetDeclaredVirtualMethodsSlice(image_pointer_size_); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 8124 | input_array_length = input_virtual_methods.size(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8125 | } else { |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 8126 | // For a new interface, however, we need the whole vtable in case a new |
| 8127 | // interface method is implemented in the whole superclass. |
| 8128 | using_virtuals = false; |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8129 | DCHECK(vtable != nullptr); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8130 | input_vtable_array = vtable; |
| 8131 | input_array_length = input_vtable_array->GetLength(); |
| 8132 | } |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 8133 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 8134 | // For each method in interface |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 8135 | for (size_t j = 0; j < num_methods; ++j) { |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 8136 | auto* interface_method = iftable->GetInterface(i)->GetVirtualMethod(j, image_pointer_size_); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 8137 | MethodNameAndSignatureComparator interface_name_comparator( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8138 | interface_method->GetInterfaceMethodIfProxy(image_pointer_size_)); |
David Srbecky | e36e7f2 | 2018-11-14 14:21:23 +0000 | [diff] [blame] | 8139 | uint32_t imt_index = interface_method->GetImtIndex(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 8140 | ArtMethod** imt_ptr = &out_imt[imt_index]; |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 8141 | // For each method listed in the interface's method list, find the |
| 8142 | // matching method in our class's method list. We want to favor the |
| 8143 | // subclass over the superclass, which just requires walking |
| 8144 | // back from the end of the vtable. (This only matters if the |
| 8145 | // superclass defines a private method and this class redefines |
| 8146 | // it -- otherwise it would use the same vtable slot. In .dex files |
| 8147 | // those don't end up in the virtual method table, so it shouldn't |
| 8148 | // matter which direction we go. We walk it backward anyway.) |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 8149 | // |
| 8150 | // To find defaults we need to do the same but also go over interfaces. |
| 8151 | bool found_impl = false; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 8152 | ArtMethod* vtable_impl = nullptr; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 8153 | for (int32_t k = input_array_length - 1; k >= 0; --k) { |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 8154 | ArtMethod* vtable_method = using_virtuals ? |
| 8155 | &input_virtual_methods[k] : |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8156 | input_vtable_array->GetElementPtrSize<ArtMethod*>(k, image_pointer_size_); |
| 8157 | ArtMethod* vtable_method_for_name_comparison = |
| 8158 | vtable_method->GetInterfaceMethodIfProxy(image_pointer_size_); |
Alex Light | a467a6e | 2020-03-23 16:07:29 -0700 | [diff] [blame] | 8159 | DCHECK(!vtable_method->IsStatic()) << vtable_method->PrettyMethod(); |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 8160 | if (interface_name_comparator.HasSameNameAndSignature( |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 8161 | vtable_method_for_name_comparison)) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 8162 | if (!vtable_method->IsAbstract() && !vtable_method->IsPublic()) { |
Mathieu Chartier | 4d122c1 | 2015-06-17 14:14:36 -0700 | [diff] [blame] | 8163 | // Must do EndAssertNoThreadSuspension before throw since the throw can cause |
| 8164 | // allocations. |
| 8165 | self->EndAssertNoThreadSuspension(old_cause); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8166 | ThrowIllegalAccessError(klass.Get(), |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 8167 | "Method '%s' implementing interface method '%s' is not public", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 8168 | vtable_method->PrettyMethod().c_str(), |
| 8169 | interface_method->PrettyMethod().c_str()); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 8170 | return false; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 8171 | } else if (UNLIKELY(vtable_method->IsOverridableByDefaultMethod())) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 8172 | // We might have a newer, better, default method for this, so we just skip it. If we |
| 8173 | // are still using this we will select it again when scanning for default methods. To |
| 8174 | // obviate the need to copy the method again we will make a note that we already found |
| 8175 | // a default here. |
| 8176 | // TODO This should be much cleaner. |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 8177 | vtable_impl = vtable_method; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 8178 | break; |
| 8179 | } else { |
| 8180 | found_impl = true; |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8181 | if (LIKELY(fill_tables)) { |
| 8182 | method_array->SetElementPtrSize(j, vtable_method, image_pointer_size_); |
| 8183 | // Place method in imt if entry is empty, place conflict otherwise. |
| 8184 | SetIMTRef(unimplemented_method, |
| 8185 | imt_conflict_method, |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8186 | vtable_method, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 8187 | /*out*/out_new_conflict, |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8188 | /*out*/imt_ptr); |
| 8189 | } |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 8190 | break; |
| 8191 | } |
| 8192 | } |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 8193 | } |
| 8194 | // Continue on to the next method if we are done. |
| 8195 | if (LIKELY(found_impl)) { |
| 8196 | continue; |
| 8197 | } else if (LIKELY(super_interface)) { |
| 8198 | // Don't look for a default implementation when the super-method is implemented directly |
| 8199 | // by the class. |
| 8200 | // |
| 8201 | // See if we can use the superclasses method and skip searching everything else. |
| 8202 | // Note: !found_impl && super_interface |
| 8203 | CHECK(extend_super_iftable); |
| 8204 | // If this is a super_interface method it is possible we shouldn't override it because a |
| 8205 | // superclass could have implemented it directly. We get the method the superclass used |
| 8206 | // to implement this to know if we can override it with a default method. Doing this is |
| 8207 | // safe since we know that the super_iftable is filled in so we can simply pull it from |
| 8208 | // there. We don't bother if this is not a super-classes interface since in that case we |
| 8209 | // have scanned the entire vtable anyway and would have found it. |
| 8210 | // TODO This is rather dirty but it is faster than searching through the entire vtable |
| 8211 | // every time. |
| 8212 | ArtMethod* supers_method = |
| 8213 | method_array->GetElementPtrSize<ArtMethod*>(j, image_pointer_size_); |
| 8214 | DCHECK(supers_method != nullptr); |
| 8215 | DCHECK(interface_name_comparator.HasSameNameAndSignature(supers_method)); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8216 | if (LIKELY(!supers_method->IsOverridableByDefaultMethod())) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 8217 | // The method is not overridable by a default method (i.e. it is directly implemented |
| 8218 | // in some class). Therefore move onto the next interface method. |
| 8219 | continue; |
Alex Light | d6c2bfa | 2016-05-02 18:51:34 -0700 | [diff] [blame] | 8220 | } else { |
| 8221 | // If the super-classes method is override-able by a default method we need to keep |
| 8222 | // track of it since though it is override-able it is not guaranteed to be 'overridden'. |
| 8223 | // 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] | 8224 | // to the vtable twice, causing corruption (vtable entries having inconsistent and |
| 8225 | // illegal states, incorrect vtable size, and incorrect or inconsistent iftable entries) |
| 8226 | // in this class and any subclasses. |
Alex Light | d6c2bfa | 2016-05-02 18:51:34 -0700 | [diff] [blame] | 8227 | DCHECK(vtable_impl == nullptr || vtable_impl == supers_method) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 8228 | << "vtable_impl was " << ArtMethod::PrettyMethod(vtable_impl) |
| 8229 | << " and not 'nullptr' or " |
| 8230 | << supers_method->PrettyMethod() |
| 8231 | << " as expected. IFTable appears to be corrupt!"; |
Alex Light | d6c2bfa | 2016-05-02 18:51:34 -0700 | [diff] [blame] | 8232 | vtable_impl = supers_method; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 8233 | } |
| 8234 | } |
| 8235 | // 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] | 8236 | ArtMethod* current_method = helper.FindMethod(interface_method, |
| 8237 | interface_name_comparator, |
| 8238 | vtable_impl); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8239 | if (LIKELY(fill_tables)) { |
Alex Light | 1277108 | 2016-01-26 16:07:41 -0800 | [diff] [blame] | 8240 | if (current_method == nullptr && !super_interface) { |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8241 | // We could not find an implementation for this method and since it is a brand new |
| 8242 | // interface we searched the entire vtable (and all default methods) for an |
| 8243 | // 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] | 8244 | current_method = helper.GetOrCreateMirandaMethod(interface_method, |
| 8245 | interface_name_comparator); |
Alex Light | 1277108 | 2016-01-26 16:07:41 -0800 | [diff] [blame] | 8246 | } |
| 8247 | |
| 8248 | if (current_method != nullptr) { |
| 8249 | // We found a default method implementation. Record it in the iftable and IMT. |
| 8250 | method_array->SetElementPtrSize(j, current_method, image_pointer_size_); |
| 8251 | SetIMTRef(unimplemented_method, |
| 8252 | imt_conflict_method, |
Alex Light | 1277108 | 2016-01-26 16:07:41 -0800 | [diff] [blame] | 8253 | current_method, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 8254 | /*out*/out_new_conflict, |
Alex Light | 1277108 | 2016-01-26 16:07:41 -0800 | [diff] [blame] | 8255 | /*out*/imt_ptr); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 8256 | } |
| 8257 | } |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8258 | } // For each method in interface end. |
| 8259 | } // if (num_methods > 0) |
| 8260 | } // For each interface. |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8261 | // TODO don't extend virtuals of interface unless necessary (when is it?). |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 8262 | if (helper.HasNewVirtuals()) { |
| 8263 | LengthPrefixedArray<ArtMethod>* old_methods = kIsDebugBuild ? klass->GetMethodsPtr() : nullptr; |
| 8264 | helper.ReallocMethods(); // No return value to check. Native allocation failure aborts. |
| 8265 | LengthPrefixedArray<ArtMethod>* methods = kIsDebugBuild ? klass->GetMethodsPtr() : nullptr; |
| 8266 | |
Mathieu Chartier | d4d83b8 | 2015-06-19 20:24:45 -0700 | [diff] [blame] | 8267 | // 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] | 8268 | // suspension assert. |
| 8269 | self->EndAssertNoThreadSuspension(old_cause); |
Mathieu Chartier | d4d83b8 | 2015-06-19 20:24:45 -0700 | [diff] [blame] | 8270 | |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8271 | if (fill_tables) { |
Vladimir Marko | 3068d58 | 2019-05-28 16:39:29 +0100 | [diff] [blame] | 8272 | vtable.Assign(helper.UpdateVtable(default_translations, vtable)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8273 | if (UNLIKELY(vtable == nullptr)) { |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 8274 | // The helper has already called self->AssertPendingOOMException(); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8275 | return false; |
| 8276 | } |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 8277 | helper.UpdateIfTable(iftable); |
| 8278 | helper.UpdateIMT(out_imt); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8279 | } |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8280 | |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 8281 | helper.CheckNoStaleMethodsInDexCache(); |
| 8282 | helper.ClobberOldMethods(old_methods, methods); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8283 | } else { |
| 8284 | self->EndAssertNoThreadSuspension(old_cause); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 8285 | } |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8286 | if (kIsDebugBuild && !is_interface) { |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 8287 | CheckVTable(self, klass, image_pointer_size_); |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 8288 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 8289 | return true; |
| 8290 | } |
| 8291 | |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8292 | class ClassLinker::LinkFieldsHelper { |
| 8293 | public: |
| 8294 | static bool LinkFields(ClassLinker* class_linker, |
| 8295 | Thread* self, |
| 8296 | Handle<mirror::Class> klass, |
| 8297 | bool is_static, |
| 8298 | size_t* class_size) |
| 8299 | REQUIRES_SHARED(Locks::mutator_lock_); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 8300 | |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8301 | private: |
| 8302 | enum class FieldTypeOrder : uint16_t; |
| 8303 | class FieldGaps; |
| 8304 | |
| 8305 | struct FieldTypeOrderAndIndex { |
| 8306 | FieldTypeOrder field_type_order; |
| 8307 | uint16_t field_index; |
| 8308 | }; |
| 8309 | |
| 8310 | static FieldTypeOrder FieldTypeOrderFromFirstDescriptorCharacter(char first_char); |
| 8311 | |
| 8312 | template <size_t kSize> |
| 8313 | static MemberOffset AssignFieldOffset(ArtField* field, MemberOffset field_offset) |
| 8314 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 8315 | }; |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 8316 | |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8317 | // We use the following order of field types for assigning offsets. |
| 8318 | // Some fields can be shuffled forward to fill gaps, see `ClassLinker::LinkFields()`. |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8319 | enum class ClassLinker::LinkFieldsHelper::FieldTypeOrder : uint16_t { |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8320 | kReference = 0u, |
| 8321 | kLong, |
| 8322 | kDouble, |
| 8323 | kInt, |
| 8324 | kFloat, |
| 8325 | kChar, |
| 8326 | kShort, |
| 8327 | kBoolean, |
| 8328 | kByte, |
| 8329 | |
| 8330 | kLast64BitType = kDouble, |
| 8331 | kLast32BitType = kFloat, |
| 8332 | kLast16BitType = kShort, |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 8333 | }; |
| 8334 | |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8335 | ALWAYS_INLINE |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8336 | ClassLinker::LinkFieldsHelper::FieldTypeOrder |
| 8337 | ClassLinker::LinkFieldsHelper::FieldTypeOrderFromFirstDescriptorCharacter(char first_char) { |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8338 | switch (first_char) { |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8339 | case 'J': |
| 8340 | return FieldTypeOrder::kLong; |
| 8341 | case 'D': |
| 8342 | return FieldTypeOrder::kDouble; |
| 8343 | case 'I': |
| 8344 | return FieldTypeOrder::kInt; |
| 8345 | case 'F': |
| 8346 | return FieldTypeOrder::kFloat; |
| 8347 | case 'C': |
| 8348 | return FieldTypeOrder::kChar; |
| 8349 | case 'S': |
| 8350 | return FieldTypeOrder::kShort; |
| 8351 | case 'Z': |
| 8352 | return FieldTypeOrder::kBoolean; |
| 8353 | case 'B': |
| 8354 | return FieldTypeOrder::kByte; |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8355 | default: |
| 8356 | DCHECK(first_char == 'L' || first_char == '[') << first_char; |
| 8357 | return FieldTypeOrder::kReference; |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8358 | } |
| 8359 | } |
| 8360 | |
| 8361 | // Gaps where we can insert fields in object layout. |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8362 | class ClassLinker::LinkFieldsHelper::FieldGaps { |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8363 | public: |
| 8364 | template <uint32_t kSize> |
| 8365 | ALWAYS_INLINE MemberOffset AlignFieldOffset(MemberOffset field_offset) { |
| 8366 | static_assert(kSize == 2u || kSize == 4u || kSize == 8u); |
| 8367 | if (!IsAligned<kSize>(field_offset.Uint32Value())) { |
| 8368 | uint32_t gap_start = field_offset.Uint32Value(); |
| 8369 | field_offset = MemberOffset(RoundUp(gap_start, kSize)); |
| 8370 | AddGaps<kSize - 1u>(gap_start, field_offset.Uint32Value()); |
| 8371 | } |
| 8372 | return field_offset; |
| 8373 | } |
| 8374 | |
| 8375 | template <uint32_t kSize> |
| 8376 | bool HasGap() const { |
| 8377 | static_assert(kSize == 1u || kSize == 2u || kSize == 4u); |
| 8378 | return (kSize == 1u && gap1_offset_ != kNoOffset) || |
| 8379 | (kSize <= 2u && gap2_offset_ != kNoOffset) || |
| 8380 | gap4_offset_ != kNoOffset; |
| 8381 | } |
| 8382 | |
| 8383 | template <uint32_t kSize> |
| 8384 | MemberOffset ReleaseGap() { |
| 8385 | static_assert(kSize == 1u || kSize == 2u || kSize == 4u); |
| 8386 | uint32_t result; |
| 8387 | if (kSize == 1u && gap1_offset_ != kNoOffset) { |
| 8388 | DCHECK(gap2_offset_ == kNoOffset || gap2_offset_ > gap1_offset_); |
| 8389 | DCHECK(gap4_offset_ == kNoOffset || gap4_offset_ > gap1_offset_); |
| 8390 | result = gap1_offset_; |
| 8391 | gap1_offset_ = kNoOffset; |
| 8392 | } else if (kSize <= 2u && gap2_offset_ != kNoOffset) { |
| 8393 | DCHECK(gap4_offset_ == kNoOffset || gap4_offset_ > gap2_offset_); |
| 8394 | result = gap2_offset_; |
| 8395 | gap2_offset_ = kNoOffset; |
| 8396 | if (kSize < 2u) { |
| 8397 | AddGaps<1u>(result + kSize, result + 2u); |
| 8398 | } |
| 8399 | } else { |
| 8400 | DCHECK_NE(gap4_offset_, kNoOffset); |
| 8401 | result = gap4_offset_; |
| 8402 | gap4_offset_ = kNoOffset; |
| 8403 | if (kSize < 4u) { |
| 8404 | AddGaps<kSize | 2u>(result + kSize, result + 4u); |
| 8405 | } |
| 8406 | } |
| 8407 | return MemberOffset(result); |
| 8408 | } |
| 8409 | |
| 8410 | private: |
| 8411 | template <uint32_t kGapsToCheck> |
| 8412 | void AddGaps(uint32_t gap_start, uint32_t gap_end) { |
| 8413 | if ((kGapsToCheck & 1u) != 0u) { |
| 8414 | DCHECK_LT(gap_start, gap_end); |
| 8415 | DCHECK_ALIGNED(gap_end, 2u); |
| 8416 | if ((gap_start & 1u) != 0u) { |
| 8417 | DCHECK_EQ(gap1_offset_, kNoOffset); |
| 8418 | gap1_offset_ = gap_start; |
| 8419 | gap_start += 1u; |
| 8420 | if (kGapsToCheck == 1u || gap_start == gap_end) { |
| 8421 | DCHECK_EQ(gap_start, gap_end); |
| 8422 | return; |
| 8423 | } |
| 8424 | } |
| 8425 | } |
| 8426 | |
| 8427 | if ((kGapsToCheck & 2u) != 0u) { |
| 8428 | DCHECK_LT(gap_start, gap_end); |
| 8429 | DCHECK_ALIGNED(gap_start, 2u); |
| 8430 | DCHECK_ALIGNED(gap_end, 4u); |
| 8431 | if ((gap_start & 2u) != 0u) { |
| 8432 | DCHECK_EQ(gap2_offset_, kNoOffset); |
| 8433 | gap2_offset_ = gap_start; |
| 8434 | gap_start += 2u; |
| 8435 | if (kGapsToCheck <= 3u || gap_start == gap_end) { |
| 8436 | DCHECK_EQ(gap_start, gap_end); |
| 8437 | return; |
| 8438 | } |
| 8439 | } |
| 8440 | } |
| 8441 | |
| 8442 | if ((kGapsToCheck & 4u) != 0u) { |
| 8443 | DCHECK_LT(gap_start, gap_end); |
| 8444 | DCHECK_ALIGNED(gap_start, 4u); |
| 8445 | DCHECK_ALIGNED(gap_end, 8u); |
| 8446 | DCHECK_EQ(gap_start + 4u, gap_end); |
| 8447 | DCHECK_EQ(gap4_offset_, kNoOffset); |
| 8448 | gap4_offset_ = gap_start; |
| 8449 | return; |
| 8450 | } |
| 8451 | |
| 8452 | DCHECK(false) << "Remaining gap: " << gap_start << " to " << gap_end |
| 8453 | << " after checking " << kGapsToCheck; |
| 8454 | } |
| 8455 | |
| 8456 | static constexpr uint32_t kNoOffset = static_cast<uint32_t>(-1); |
| 8457 | |
| 8458 | uint32_t gap4_offset_ = kNoOffset; |
| 8459 | uint32_t gap2_offset_ = kNoOffset; |
| 8460 | uint32_t gap1_offset_ = kNoOffset; |
| 8461 | }; |
| 8462 | |
| 8463 | template <size_t kSize> |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8464 | ALWAYS_INLINE |
| 8465 | MemberOffset ClassLinker::LinkFieldsHelper::AssignFieldOffset(ArtField* field, |
| 8466 | MemberOffset field_offset) { |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8467 | DCHECK_ALIGNED(field_offset.Uint32Value(), kSize); |
| 8468 | DCHECK_EQ(Primitive::ComponentSize(field->GetTypeAsPrimitiveType()), kSize); |
| 8469 | field->SetOffset(field_offset); |
| 8470 | return MemberOffset(field_offset.Uint32Value() + kSize); |
| 8471 | } |
| 8472 | |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8473 | bool ClassLinker::LinkFieldsHelper::LinkFields(ClassLinker* class_linker, |
| 8474 | Thread* self, |
| 8475 | Handle<mirror::Class> klass, |
| 8476 | bool is_static, |
| 8477 | size_t* class_size) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 8478 | self->AllowThreadSuspension(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8479 | const size_t num_fields = is_static ? klass->NumStaticFields() : klass->NumInstanceFields(); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 8480 | LengthPrefixedArray<ArtField>* const fields = is_static ? klass->GetSFieldsPtr() : |
| 8481 | klass->GetIFieldsPtr(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8482 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 8483 | // Initialize field_offset |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 8484 | MemberOffset field_offset(0); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 8485 | if (is_static) { |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8486 | field_offset = klass->GetFirstReferenceStaticFieldOffsetDuringLinking( |
| 8487 | class_linker->GetImagePointerSize()); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 8488 | } else { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 8489 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 8490 | if (super_class != nullptr) { |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 8491 | CHECK(super_class->IsResolved()) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 8492 | << klass->PrettyClass() << " " << super_class->PrettyClass(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8493 | field_offset = MemberOffset(super_class->GetObjectSize()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8494 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8495 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8496 | |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 8497 | CHECK_EQ(num_fields == 0, fields == nullptr) << klass->PrettyClass(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8498 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 8499 | // we want a relatively stable order so that adding new fields |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 8500 | // minimizes disruption of C++ version such as Class and Method. |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 8501 | // |
| 8502 | // The overall sort order order is: |
| 8503 | // 1) All object reference fields, sorted alphabetically. |
| 8504 | // 2) All java long (64-bit) integer fields, sorted alphabetically. |
| 8505 | // 3) All java double (64-bit) floating point fields, sorted alphabetically. |
| 8506 | // 4) All java int (32-bit) integer fields, sorted alphabetically. |
| 8507 | // 5) All java float (32-bit) floating point fields, sorted alphabetically. |
| 8508 | // 6) All java char (16-bit) integer fields, sorted alphabetically. |
| 8509 | // 7) All java short (16-bit) integer fields, sorted alphabetically. |
| 8510 | // 8) All java boolean (8-bit) integer fields, sorted alphabetically. |
| 8511 | // 9) All java byte (8-bit) integer fields, sorted alphabetically. |
| 8512 | // |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8513 | // (References are first to increase the chance of reference visiting |
| 8514 | // being able to take a fast path using a bitmap of references at the |
| 8515 | // start of the object, see `Class::reference_instance_offsets_`.) |
| 8516 | // |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8517 | // Once the fields are sorted in this order we will attempt to fill any gaps |
| 8518 | // that might be present in the memory layout of the structure. |
| 8519 | // Note that we shall not fill gaps between the superclass fields. |
| 8520 | |
| 8521 | // Collect fields and their "type order index" (see numbered points above). |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 8522 | const char* old_no_suspend_cause = self->StartAssertNoThreadSuspension( |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8523 | "Using plain ArtField references"); |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8524 | constexpr size_t kStackBufferEntries = 64; // Avoid allocations for small number of fields. |
| 8525 | FieldTypeOrderAndIndex stack_buffer[kStackBufferEntries]; |
| 8526 | std::vector<FieldTypeOrderAndIndex> heap_buffer; |
| 8527 | ArrayRef<FieldTypeOrderAndIndex> sorted_fields; |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8528 | if (num_fields <= kStackBufferEntries) { |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8529 | sorted_fields = ArrayRef<FieldTypeOrderAndIndex>(stack_buffer, num_fields); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8530 | } else { |
| 8531 | heap_buffer.resize(num_fields); |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8532 | sorted_fields = ArrayRef<FieldTypeOrderAndIndex>(heap_buffer); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 8533 | } |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 8534 | size_t num_reference_fields = 0; |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8535 | size_t primitive_fields_start = num_fields; |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8536 | DCHECK_LE(num_fields, 1u << 16); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8537 | for (size_t i = 0; i != num_fields; ++i) { |
| 8538 | ArtField* field = &fields->At(i); |
| 8539 | const char* descriptor = field->GetTypeDescriptor(); |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8540 | FieldTypeOrder field_type_order = FieldTypeOrderFromFirstDescriptorCharacter(descriptor[0]); |
| 8541 | uint16_t field_index = dchecked_integral_cast<uint16_t>(i); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8542 | // Insert references to the start, other fields to the end. |
| 8543 | DCHECK_LT(num_reference_fields, primitive_fields_start); |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8544 | if (field_type_order == FieldTypeOrder::kReference) { |
| 8545 | sorted_fields[num_reference_fields] = { field_type_order, field_index }; |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8546 | ++num_reference_fields; |
| 8547 | } else { |
| 8548 | --primitive_fields_start; |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8549 | sorted_fields[primitive_fields_start] = { field_type_order, field_index }; |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8550 | } |
| 8551 | } |
| 8552 | DCHECK_EQ(num_reference_fields, primitive_fields_start); |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 8553 | |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8554 | // Reference fields are already sorted by field index (and dex field index). |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8555 | DCHECK(std::is_sorted( |
| 8556 | sorted_fields.begin(), |
| 8557 | sorted_fields.begin() + num_reference_fields, |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8558 | [fields](const auto& lhs, const auto& rhs) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 8559 | ArtField* lhs_field = &fields->At(lhs.field_index); |
| 8560 | ArtField* rhs_field = &fields->At(rhs.field_index); |
| 8561 | CHECK_EQ(lhs_field->GetTypeAsPrimitiveType(), Primitive::kPrimNot); |
| 8562 | CHECK_EQ(rhs_field->GetTypeAsPrimitiveType(), Primitive::kPrimNot); |
| 8563 | CHECK_EQ(lhs_field->GetDexFieldIndex() < rhs_field->GetDexFieldIndex(), |
| 8564 | lhs.field_index < rhs.field_index); |
| 8565 | return lhs_field->GetDexFieldIndex() < rhs_field->GetDexFieldIndex(); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8566 | })); |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8567 | // Primitive fields were stored in reverse order of their field index (and dex field index). |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8568 | DCHECK(std::is_sorted( |
| 8569 | sorted_fields.begin() + primitive_fields_start, |
| 8570 | sorted_fields.end(), |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8571 | [fields](const auto& lhs, const auto& rhs) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 8572 | ArtField* lhs_field = &fields->At(lhs.field_index); |
| 8573 | ArtField* rhs_field = &fields->At(rhs.field_index); |
| 8574 | CHECK_NE(lhs_field->GetTypeAsPrimitiveType(), Primitive::kPrimNot); |
| 8575 | CHECK_NE(rhs_field->GetTypeAsPrimitiveType(), Primitive::kPrimNot); |
| 8576 | CHECK_EQ(lhs_field->GetDexFieldIndex() > rhs_field->GetDexFieldIndex(), |
| 8577 | lhs.field_index > rhs.field_index); |
| 8578 | return lhs.field_index > rhs.field_index; |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8579 | })); |
| 8580 | // Sort the primitive fields by the field type order, then field index. |
| 8581 | std::sort(sorted_fields.begin() + primitive_fields_start, |
| 8582 | sorted_fields.end(), |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8583 | [](const auto& lhs, const auto& rhs) { |
| 8584 | if (lhs.field_type_order != rhs.field_type_order) { |
| 8585 | return lhs.field_type_order < rhs.field_type_order; |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8586 | } else { |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8587 | return lhs.field_index < rhs.field_index; |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8588 | } |
| 8589 | }); |
| 8590 | // Primitive fields are now sorted by field size (descending), then type, then field index. |
| 8591 | DCHECK(std::is_sorted( |
| 8592 | sorted_fields.begin() + primitive_fields_start, |
| 8593 | sorted_fields.end(), |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8594 | [fields](const auto& lhs, const auto& rhs) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 8595 | ArtField* lhs_field = &fields->At(lhs.field_index); |
| 8596 | ArtField* rhs_field = &fields->At(rhs.field_index); |
| 8597 | Primitive::Type lhs_type = lhs_field->GetTypeAsPrimitiveType(); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8598 | CHECK_NE(lhs_type, Primitive::kPrimNot); |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8599 | Primitive::Type rhs_type = rhs_field->GetTypeAsPrimitiveType(); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8600 | CHECK_NE(rhs_type, Primitive::kPrimNot); |
| 8601 | if (lhs_type != rhs_type) { |
| 8602 | size_t lhs_size = Primitive::ComponentSize(lhs_type); |
| 8603 | size_t rhs_size = Primitive::ComponentSize(rhs_type); |
| 8604 | return (lhs_size != rhs_size) ? (lhs_size > rhs_size) : (lhs_type < rhs_type); |
| 8605 | } else { |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8606 | return lhs_field->GetDexFieldIndex() < rhs_field->GetDexFieldIndex(); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8607 | } |
| 8608 | })); |
| 8609 | |
| 8610 | // Process reference fields. |
| 8611 | FieldGaps field_gaps; |
| 8612 | size_t index = 0u; |
| 8613 | if (num_reference_fields != 0u) { |
| 8614 | constexpr size_t kReferenceSize = sizeof(mirror::HeapReference<mirror::Object>); |
| 8615 | field_offset = field_gaps.AlignFieldOffset<kReferenceSize>(field_offset); |
| 8616 | for (; index != num_reference_fields; ++index) { |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8617 | ArtField* field = &fields->At(sorted_fields[index].field_index); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8618 | field_offset = AssignFieldOffset<kReferenceSize>(field, field_offset); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 8619 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 8620 | } |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8621 | // Process 64-bit fields. |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8622 | if (index != num_fields && |
| 8623 | sorted_fields[index].field_type_order <= FieldTypeOrder::kLast64BitType) { |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8624 | field_offset = field_gaps.AlignFieldOffset<8u>(field_offset); |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8625 | while (index != num_fields && |
| 8626 | sorted_fields[index].field_type_order <= FieldTypeOrder::kLast64BitType) { |
| 8627 | ArtField* field = &fields->At(sorted_fields[index].field_index); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8628 | field_offset = AssignFieldOffset<8u>(field, field_offset); |
| 8629 | ++index; |
Mathieu Chartier | 5565029 | 2020-09-14 12:21:04 -0700 | [diff] [blame] | 8630 | } |
Mathieu Chartier | 5565029 | 2020-09-14 12:21:04 -0700 | [diff] [blame] | 8631 | } |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8632 | // Process 32-bit fields. |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8633 | if (index != num_fields && |
| 8634 | sorted_fields[index].field_type_order <= FieldTypeOrder::kLast32BitType) { |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8635 | field_offset = field_gaps.AlignFieldOffset<4u>(field_offset); |
| 8636 | if (field_gaps.HasGap<4u>()) { |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8637 | ArtField* field = &fields->At(sorted_fields[index].field_index); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8638 | AssignFieldOffset<4u>(field, field_gaps.ReleaseGap<4u>()); // Ignore return value. |
| 8639 | ++index; |
| 8640 | DCHECK(!field_gaps.HasGap<4u>()); // There can be only one gap for a 32-bit field. |
| 8641 | } |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8642 | while (index != num_fields && |
| 8643 | sorted_fields[index].field_type_order <= FieldTypeOrder::kLast32BitType) { |
| 8644 | ArtField* field = &fields->At(sorted_fields[index].field_index); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8645 | field_offset = AssignFieldOffset<4u>(field, field_offset); |
| 8646 | ++index; |
| 8647 | } |
| 8648 | } |
| 8649 | // Process 16-bit fields. |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8650 | if (index != num_fields && |
| 8651 | sorted_fields[index].field_type_order <= FieldTypeOrder::kLast16BitType) { |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8652 | field_offset = field_gaps.AlignFieldOffset<2u>(field_offset); |
| 8653 | while (index != num_fields && |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8654 | sorted_fields[index].field_type_order <= FieldTypeOrder::kLast16BitType && |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8655 | field_gaps.HasGap<2u>()) { |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8656 | ArtField* field = &fields->At(sorted_fields[index].field_index); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8657 | AssignFieldOffset<2u>(field, field_gaps.ReleaseGap<2u>()); // Ignore return value. |
| 8658 | ++index; |
| 8659 | } |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8660 | while (index != num_fields && |
| 8661 | sorted_fields[index].field_type_order <= FieldTypeOrder::kLast16BitType) { |
| 8662 | ArtField* field = &fields->At(sorted_fields[index].field_index); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8663 | field_offset = AssignFieldOffset<2u>(field, field_offset); |
| 8664 | ++index; |
| 8665 | } |
| 8666 | } |
| 8667 | // Process 8-bit fields. |
| 8668 | for (; index != num_fields && field_gaps.HasGap<1u>(); ++index) { |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8669 | ArtField* field = &fields->At(sorted_fields[index].field_index); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8670 | AssignFieldOffset<1u>(field, field_gaps.ReleaseGap<1u>()); // Ignore return value. |
| 8671 | } |
| 8672 | for (; index != num_fields; ++index) { |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8673 | ArtField* field = &fields->At(sorted_fields[index].field_index); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8674 | field_offset = AssignFieldOffset<1u>(field, field_offset); |
| 8675 | } |
| 8676 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 8677 | self->EndAssertNoThreadSuspension(old_no_suspend_cause); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 8678 | |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 8679 | // We lie to the GC about the java.lang.ref.Reference.referent field, so it doesn't scan it. |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8680 | DCHECK(!class_linker->init_done_ || !klass->DescriptorEquals("Ljava/lang/ref/Reference;")); |
| 8681 | if (!is_static && |
| 8682 | UNLIKELY(!class_linker->init_done_) && |
| 8683 | klass->DescriptorEquals("Ljava/lang/ref/Reference;")) { |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 8684 | // We know there are no non-reference fields in the Reference classes, and we know |
| 8685 | // that 'referent' is alphabetically last, so this is easy... |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 8686 | CHECK_EQ(num_reference_fields, num_fields) << klass->PrettyClass(); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 8687 | CHECK_STREQ(fields->At(num_fields - 1).GetName(), "referent") |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 8688 | << klass->PrettyClass(); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 8689 | --num_reference_fields; |
| 8690 | } |
| 8691 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 8692 | size_t size = field_offset.Uint32Value(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8693 | // Update klass |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 8694 | if (is_static) { |
| 8695 | klass->SetNumReferenceStaticFields(num_reference_fields); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 8696 | *class_size = size; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 8697 | } else { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8698 | klass->SetNumReferenceInstanceFields(num_reference_fields); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 8699 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 8700 | if (num_reference_fields == 0 || super_class == nullptr) { |
| 8701 | // 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] | 8702 | // super_class is null iff the class is java.lang.Object. |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 8703 | if (super_class == nullptr || |
| 8704 | (super_class->GetClassFlags() & mirror::kClassFlagNoReferenceFields) != 0) { |
| 8705 | klass->SetClassFlags(klass->GetClassFlags() | mirror::kClassFlagNoReferenceFields); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 8706 | } |
| 8707 | } |
| 8708 | if (kIsDebugBuild) { |
| 8709 | DCHECK_EQ(super_class == nullptr, klass->DescriptorEquals("Ljava/lang/Object;")); |
| 8710 | size_t total_reference_instance_fields = 0; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 8711 | ObjPtr<mirror::Class> cur_super = klass.Get(); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 8712 | while (cur_super != nullptr) { |
| 8713 | total_reference_instance_fields += cur_super->NumReferenceInstanceFieldsDuringLinking(); |
| 8714 | cur_super = cur_super->GetSuperClass(); |
| 8715 | } |
| 8716 | if (super_class == nullptr) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 8717 | CHECK_EQ(total_reference_instance_fields, 1u) << klass->PrettyDescriptor(); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 8718 | } else { |
| 8719 | // Check that there is at least num_reference_fields other than Object.class. |
| 8720 | CHECK_GE(total_reference_instance_fields, 1u + num_reference_fields) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 8721 | << klass->PrettyClass(); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 8722 | } |
| 8723 | } |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 8724 | if (!klass->IsVariableSize()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8725 | std::string temp; |
| 8726 | DCHECK_GE(size, sizeof(mirror::Object)) << klass->GetDescriptor(&temp); |
| 8727 | size_t previous_size = klass->GetObjectSize(); |
| 8728 | if (previous_size != 0) { |
| 8729 | // Make sure that we didn't originally have an incorrect size. |
| 8730 | CHECK_EQ(previous_size, size) << klass->GetDescriptor(&temp); |
Mathieu Chartier | 79b4f38 | 2013-10-23 15:21:37 -0700 | [diff] [blame] | 8731 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8732 | klass->SetObjectSize(size); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8733 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8734 | } |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 8735 | |
| 8736 | if (kIsDebugBuild) { |
| 8737 | // Make sure that the fields array is ordered by name but all reference |
| 8738 | // offsets are at the beginning as far as alignment allows. |
| 8739 | MemberOffset start_ref_offset = is_static |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8740 | ? klass->GetFirstReferenceStaticFieldOffsetDuringLinking(class_linker->image_pointer_size_) |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 8741 | : klass->GetFirstReferenceInstanceFieldOffset(); |
| 8742 | MemberOffset end_ref_offset(start_ref_offset.Uint32Value() + |
| 8743 | num_reference_fields * |
| 8744 | sizeof(mirror::HeapReference<mirror::Object>)); |
| 8745 | MemberOffset current_ref_offset = start_ref_offset; |
| 8746 | for (size_t i = 0; i < num_fields; i++) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 8747 | ArtField* field = &fields->At(i); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8748 | VLOG(class_linker) << "LinkFields: " << (is_static ? "static" : "instance") |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 8749 | << " class=" << klass->PrettyClass() << " field=" << field->PrettyField() |
| 8750 | << " offset=" << field->GetOffsetDuringLinking(); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 8751 | if (i != 0) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 8752 | ArtField* const prev_field = &fields->At(i - 1); |
Vladimir Marko | 7a7c1db | 2014-11-17 15:13:34 +0000 | [diff] [blame] | 8753 | // NOTE: The field names can be the same. This is not possible in the Java language |
| 8754 | // 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] | 8755 | DCHECK_LE(strcmp(prev_field->GetName(), field->GetName()), 0); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 8756 | } |
| 8757 | Primitive::Type type = field->GetTypeAsPrimitiveType(); |
| 8758 | bool is_primitive = type != Primitive::kPrimNot; |
| 8759 | if (klass->DescriptorEquals("Ljava/lang/ref/Reference;") && |
| 8760 | strcmp("referent", field->GetName()) == 0) { |
| 8761 | is_primitive = true; // We lied above, so we have to expect a lie here. |
| 8762 | } |
| 8763 | MemberOffset offset = field->GetOffsetDuringLinking(); |
| 8764 | if (is_primitive) { |
| 8765 | if (offset.Uint32Value() < end_ref_offset.Uint32Value()) { |
| 8766 | // Shuffled before references. |
| 8767 | size_t type_size = Primitive::ComponentSize(type); |
| 8768 | CHECK_LT(type_size, sizeof(mirror::HeapReference<mirror::Object>)); |
| 8769 | CHECK_LT(offset.Uint32Value(), start_ref_offset.Uint32Value()); |
| 8770 | CHECK_LE(offset.Uint32Value() + type_size, start_ref_offset.Uint32Value()); |
| 8771 | CHECK(!IsAligned<sizeof(mirror::HeapReference<mirror::Object>)>(offset.Uint32Value())); |
| 8772 | } |
| 8773 | } else { |
| 8774 | CHECK_EQ(current_ref_offset.Uint32Value(), offset.Uint32Value()); |
| 8775 | current_ref_offset = MemberOffset(current_ref_offset.Uint32Value() + |
| 8776 | sizeof(mirror::HeapReference<mirror::Object>)); |
| 8777 | } |
| 8778 | } |
| 8779 | CHECK_EQ(current_ref_offset.Uint32Value(), end_ref_offset.Uint32Value()); |
| 8780 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 8781 | return true; |
| 8782 | } |
| 8783 | |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8784 | bool ClassLinker::LinkInstanceFields(Thread* self, Handle<mirror::Class> klass) { |
| 8785 | CHECK(klass != nullptr); |
| 8786 | return LinkFieldsHelper::LinkFields(this, self, klass, false, nullptr); |
| 8787 | } |
| 8788 | |
| 8789 | bool ClassLinker::LinkStaticFields(Thread* self, Handle<mirror::Class> klass, size_t* class_size) { |
| 8790 | CHECK(klass != nullptr); |
| 8791 | return LinkFieldsHelper::LinkFields(this, self, klass, true, class_size); |
| 8792 | } |
| 8793 | |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 8794 | // Set the bitmap of reference instance field offsets. |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 8795 | void ClassLinker::CreateReferenceInstanceOffsets(Handle<mirror::Class> klass) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8796 | uint32_t reference_offsets = 0; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 8797 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 8798 | // 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] | 8799 | if (super_class != nullptr) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8800 | reference_offsets = super_class->GetReferenceInstanceOffsets(); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 8801 | // Compute reference offsets unless our superclass overflowed. |
| 8802 | if (reference_offsets != mirror::Class::kClassWalkSuper) { |
| 8803 | size_t num_reference_fields = klass->NumReferenceInstanceFieldsDuringLinking(); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 8804 | if (num_reference_fields != 0u) { |
| 8805 | // All of the fields that contain object references are guaranteed be grouped in memory |
| 8806 | // starting at an appropriately aligned address after super class object data. |
| 8807 | uint32_t start_offset = RoundUp(super_class->GetObjectSize(), |
| 8808 | sizeof(mirror::HeapReference<mirror::Object>)); |
| 8809 | uint32_t start_bit = (start_offset - mirror::kObjectHeaderSize) / |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 8810 | sizeof(mirror::HeapReference<mirror::Object>); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 8811 | if (start_bit + num_reference_fields > 32) { |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 8812 | reference_offsets = mirror::Class::kClassWalkSuper; |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 8813 | } else { |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 8814 | reference_offsets |= (0xffffffffu << start_bit) & |
| 8815 | (0xffffffffu >> (32 - (start_bit + num_reference_fields))); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 8816 | } |
| 8817 | } |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 8818 | } |
| 8819 | } |
Mingyao Yang | faff0f0 | 2014-09-10 12:03:22 -0700 | [diff] [blame] | 8820 | klass->SetReferenceInstanceOffsets(reference_offsets); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 8821 | } |
| 8822 | |
Vladimir Marko | 18090d1 | 2018-06-01 16:53:12 +0100 | [diff] [blame] | 8823 | ObjPtr<mirror::String> ClassLinker::DoResolveString(dex::StringIndex string_idx, |
| 8824 | ObjPtr<mirror::DexCache> dex_cache) { |
| 8825 | StackHandleScope<1> hs(Thread::Current()); |
| 8826 | Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(dex_cache)); |
| 8827 | return DoResolveString(string_idx, h_dex_cache); |
| 8828 | } |
| 8829 | |
| 8830 | ObjPtr<mirror::String> ClassLinker::DoResolveString(dex::StringIndex string_idx, |
| 8831 | Handle<mirror::DexCache> dex_cache) { |
Vladimir Marko | a64b52d | 2017-12-08 16:27:49 +0000 | [diff] [blame] | 8832 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 8833 | uint32_t utf16_length; |
| 8834 | const char* utf8_data = dex_file.StringDataAndUtf16LengthByIdx(string_idx, &utf16_length); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 8835 | ObjPtr<mirror::String> string = intern_table_->InternStrong(utf16_length, utf8_data); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 8836 | if (string != nullptr) { |
| 8837 | dex_cache->SetResolvedString(string_idx, string); |
| 8838 | } |
Vladimir Marko | 28e012a | 2017-12-07 11:22:59 +0000 | [diff] [blame] | 8839 | return string; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 8840 | } |
| 8841 | |
Vladimir Marko | 18090d1 | 2018-06-01 16:53:12 +0100 | [diff] [blame] | 8842 | ObjPtr<mirror::String> ClassLinker::DoLookupString(dex::StringIndex string_idx, |
| 8843 | ObjPtr<mirror::DexCache> dex_cache) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8844 | DCHECK(dex_cache != nullptr); |
Vladimir Marko | a64b52d | 2017-12-08 16:27:49 +0000 | [diff] [blame] | 8845 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 8846 | uint32_t utf16_length; |
| 8847 | const char* utf8_data = dex_file.StringDataAndUtf16LengthByIdx(string_idx, &utf16_length); |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 8848 | ObjPtr<mirror::String> string = |
| 8849 | intern_table_->LookupStrong(Thread::Current(), utf16_length, utf8_data); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 8850 | if (string != nullptr) { |
| 8851 | dex_cache->SetResolvedString(string_idx, string); |
| 8852 | } |
Vladimir Marko | 28e012a | 2017-12-07 11:22:59 +0000 | [diff] [blame] | 8853 | return string; |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 8854 | } |
| 8855 | |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 8856 | ObjPtr<mirror::Class> ClassLinker::DoLookupResolvedType(dex::TypeIndex type_idx, |
Vladimir Marko | 09c5ca4 | 2018-05-31 15:15:31 +0100 | [diff] [blame] | 8857 | ObjPtr<mirror::Class> referrer) { |
| 8858 | return DoLookupResolvedType(type_idx, referrer->GetDexCache(), referrer->GetClassLoader()); |
| 8859 | } |
| 8860 | |
| 8861 | ObjPtr<mirror::Class> ClassLinker::DoLookupResolvedType(dex::TypeIndex type_idx, |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 8862 | ObjPtr<mirror::DexCache> dex_cache, |
| 8863 | ObjPtr<mirror::ClassLoader> class_loader) { |
| 8864 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
| 8865 | const char* descriptor = dex_file.StringByTypeIdx(type_idx); |
| 8866 | DCHECK_NE(*descriptor, '\0') << "descriptor is empty string"; |
| 8867 | ObjPtr<mirror::Class> type = nullptr; |
| 8868 | if (descriptor[1] == '\0') { |
| 8869 | // only the descriptors of primitive types should be 1 character long, also avoid class lookup |
| 8870 | // for primitive classes that aren't backed by dex files. |
Vladimir Marko | 9186b18 | 2018-11-06 14:55:54 +0000 | [diff] [blame] | 8871 | type = LookupPrimitiveClass(descriptor[0]); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 8872 | } else { |
| 8873 | Thread* const self = Thread::Current(); |
| 8874 | DCHECK(self != nullptr); |
| 8875 | const size_t hash = ComputeModifiedUtf8Hash(descriptor); |
| 8876 | // Find the class in the loaded classes table. |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 8877 | type = LookupClass(self, descriptor, hash, class_loader); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 8878 | } |
| 8879 | if (type != nullptr) { |
| 8880 | if (type->IsResolved()) { |
| 8881 | dex_cache->SetResolvedType(type_idx, type); |
Mathieu Chartier | b890130 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 8882 | } else { |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 8883 | type = nullptr; |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 8884 | } |
Mathieu Chartier | b890130 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 8885 | } |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 8886 | return type; |
Mathieu Chartier | b890130 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 8887 | } |
| 8888 | |
Andreas Gampe | b0625e0 | 2019-05-01 12:43:31 -0700 | [diff] [blame] | 8889 | template <typename RefType> |
| 8890 | ObjPtr<mirror::Class> ClassLinker::DoResolveType(dex::TypeIndex type_idx, RefType referrer) { |
Vladimir Marko | 09c5ca4 | 2018-05-31 15:15:31 +0100 | [diff] [blame] | 8891 | StackHandleScope<2> hs(Thread::Current()); |
| 8892 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache())); |
| 8893 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(referrer->GetClassLoader())); |
| 8894 | return DoResolveType(type_idx, dex_cache, class_loader); |
| 8895 | } |
| 8896 | |
Andreas Gampe | 4835d21 | 2018-11-21 14:55:10 -0800 | [diff] [blame] | 8897 | // Instantiate the above. |
| 8898 | template ObjPtr<mirror::Class> ClassLinker::DoResolveType(dex::TypeIndex type_idx, |
| 8899 | ArtField* referrer); |
| 8900 | template ObjPtr<mirror::Class> ClassLinker::DoResolveType(dex::TypeIndex type_idx, |
| 8901 | ArtMethod* referrer); |
| 8902 | template ObjPtr<mirror::Class> ClassLinker::DoResolveType(dex::TypeIndex type_idx, |
| 8903 | ObjPtr<mirror::Class> referrer); |
| 8904 | |
Vladimir Marko | 09c5ca4 | 2018-05-31 15:15:31 +0100 | [diff] [blame] | 8905 | ObjPtr<mirror::Class> ClassLinker::DoResolveType(dex::TypeIndex type_idx, |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 8906 | Handle<mirror::DexCache> dex_cache, |
| 8907 | Handle<mirror::ClassLoader> class_loader) { |
| 8908 | Thread* self = Thread::Current(); |
| 8909 | const char* descriptor = dex_cache->GetDexFile()->StringByTypeIdx(type_idx); |
| 8910 | ObjPtr<mirror::Class> resolved = FindClass(self, descriptor, class_loader); |
| 8911 | if (resolved != nullptr) { |
| 8912 | // TODO: we used to throw here if resolved's class loader was not the |
| 8913 | // boot class loader. This was to permit different classes with the |
| 8914 | // same name to be loaded simultaneously by different loaders |
| 8915 | dex_cache->SetResolvedType(type_idx, resolved); |
| 8916 | } else { |
| 8917 | CHECK(self->IsExceptionPending()) |
| 8918 | << "Expected pending exception for failed resolution of: " << descriptor; |
| 8919 | // Convert a ClassNotFoundException to a NoClassDefFoundError. |
| 8920 | StackHandleScope<1> hs(self); |
| 8921 | Handle<mirror::Throwable> cause(hs.NewHandle(self->GetException())); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 8922 | if (cause->InstanceOf(GetClassRoot(ClassRoot::kJavaLangClassNotFoundException, this))) { |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 8923 | DCHECK(resolved == nullptr); // No Handle needed to preserve resolved. |
| 8924 | self->ClearException(); |
| 8925 | ThrowNoClassDefFoundError("Failed resolution of: %s", descriptor); |
| 8926 | self->GetException()->SetCause(cause.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8927 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 8928 | } |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 8929 | DCHECK((resolved == nullptr) || resolved->IsResolved()) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 8930 | << resolved->PrettyDescriptor() << " " << resolved->GetStatus(); |
Vladimir Marko | 28e012a | 2017-12-07 11:22:59 +0000 | [diff] [blame] | 8931 | return resolved; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 8932 | } |
| 8933 | |
Nicolas Geoffray | ea179f4 | 2018-02-08 22:30:18 +0000 | [diff] [blame] | 8934 | ArtMethod* ClassLinker::FindResolvedMethod(ObjPtr<mirror::Class> klass, |
| 8935 | ObjPtr<mirror::DexCache> dex_cache, |
| 8936 | ObjPtr<mirror::ClassLoader> class_loader, |
| 8937 | uint32_t method_idx) { |
| 8938 | // Search for the method using dex_cache and method_idx. The Class::Find*Method() |
| 8939 | // functions can optimize the search if the dex_cache is the same as the DexCache |
| 8940 | // of the class, with fall-back to name and signature search otherwise. |
| 8941 | ArtMethod* resolved = nullptr; |
| 8942 | if (klass->IsInterface()) { |
| 8943 | resolved = klass->FindInterfaceMethod(dex_cache, method_idx, image_pointer_size_); |
| 8944 | } else { |
| 8945 | resolved = klass->FindClassMethod(dex_cache, method_idx, image_pointer_size_); |
| 8946 | } |
| 8947 | DCHECK(resolved == nullptr || resolved->GetDeclaringClassUnchecked() != nullptr); |
David Brazdil | 8ce3bfa | 2018-03-12 18:01:18 +0000 | [diff] [blame] | 8948 | if (resolved != nullptr && |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 8949 | hiddenapi::ShouldDenyAccessToMember(resolved, |
| 8950 | hiddenapi::AccessContext(class_loader, dex_cache), |
| 8951 | hiddenapi::AccessMethod::kLinking)) { |
David Brazdil | 8ce3bfa | 2018-03-12 18:01:18 +0000 | [diff] [blame] | 8952 | resolved = nullptr; |
| 8953 | } |
Nicolas Geoffray | ea179f4 | 2018-02-08 22:30:18 +0000 | [diff] [blame] | 8954 | if (resolved != nullptr) { |
| 8955 | // In case of jmvti, the dex file gets verified before being registered, so first |
| 8956 | // check if it's registered before checking class tables. |
| 8957 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Nicolas Geoffray | befa309 | 2018-02-22 14:50:01 +0000 | [diff] [blame] | 8958 | DCHECK(!IsDexFileRegistered(Thread::Current(), dex_file) || |
| 8959 | FindClassTable(Thread::Current(), dex_cache) == ClassTableForClassLoader(class_loader)) |
Nicolas Geoffray | ea179f4 | 2018-02-08 22:30:18 +0000 | [diff] [blame] | 8960 | << "DexFile referrer: " << dex_file.GetLocation() |
| 8961 | << " ClassLoader: " << DescribeLoaders(class_loader, ""); |
| 8962 | // Be a good citizen and update the dex cache to speed subsequent calls. |
| 8963 | dex_cache->SetResolvedMethod(method_idx, resolved, image_pointer_size_); |
Nicolas Geoffray | befa309 | 2018-02-22 14:50:01 +0000 | [diff] [blame] | 8964 | // Disable the following invariant check as the verifier breaks it. b/73760543 |
| 8965 | // const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx); |
| 8966 | // DCHECK(LookupResolvedType(method_id.class_idx_, dex_cache, class_loader) != nullptr) |
| 8967 | // << "Method: " << resolved->PrettyMethod() << ", " |
| 8968 | // << "Class: " << klass->PrettyClass() << " (" << klass->GetStatus() << "), " |
| 8969 | // << "DexFile referrer: " << dex_file.GetLocation(); |
Nicolas Geoffray | ea179f4 | 2018-02-08 22:30:18 +0000 | [diff] [blame] | 8970 | } |
| 8971 | return resolved; |
| 8972 | } |
| 8973 | |
David Brazdil | 4525e0b | 2018-04-05 16:57:32 +0100 | [diff] [blame] | 8974 | // Returns true if `method` is either null or hidden. |
| 8975 | // Does not print any warnings if it is hidden. |
| 8976 | static bool CheckNoSuchMethod(ArtMethod* method, |
| 8977 | ObjPtr<mirror::DexCache> dex_cache, |
| 8978 | ObjPtr<mirror::ClassLoader> class_loader) |
| 8979 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 8980 | return method == nullptr || |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 8981 | hiddenapi::ShouldDenyAccessToMember(method, |
| 8982 | hiddenapi::AccessContext(class_loader, dex_cache), |
| 8983 | hiddenapi::AccessMethod::kNone); // no warnings |
David Brazdil | 4525e0b | 2018-04-05 16:57:32 +0100 | [diff] [blame] | 8984 | } |
| 8985 | |
| 8986 | ArtMethod* ClassLinker::FindIncompatibleMethod(ObjPtr<mirror::Class> klass, |
| 8987 | ObjPtr<mirror::DexCache> dex_cache, |
| 8988 | ObjPtr<mirror::ClassLoader> class_loader, |
| 8989 | uint32_t method_idx) { |
| 8990 | if (klass->IsInterface()) { |
| 8991 | ArtMethod* method = klass->FindClassMethod(dex_cache, method_idx, image_pointer_size_); |
| 8992 | return CheckNoSuchMethod(method, dex_cache, class_loader) ? nullptr : method; |
| 8993 | } else { |
| 8994 | // If there was an interface method with the same signature, we would have |
| 8995 | // found it in the "copied" methods. Only DCHECK that the interface method |
| 8996 | // really does not exist. |
| 8997 | if (kIsDebugBuild) { |
| 8998 | ArtMethod* method = |
| 8999 | klass->FindInterfaceMethod(dex_cache, method_idx, image_pointer_size_); |
| 9000 | DCHECK(CheckNoSuchMethod(method, dex_cache, class_loader)); |
| 9001 | } |
| 9002 | return nullptr; |
| 9003 | } |
| 9004 | } |
| 9005 | |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 9006 | template <ClassLinker::ResolveMode kResolveMode> |
Vladimir Marko | 8901119 | 2017-12-11 13:45:05 +0000 | [diff] [blame] | 9007 | ArtMethod* ClassLinker::ResolveMethod(uint32_t method_idx, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 9008 | Handle<mirror::DexCache> dex_cache, |
| 9009 | Handle<mirror::ClassLoader> class_loader, |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 9010 | ArtMethod* referrer, |
| 9011 | InvokeType type) { |
Nicolas Geoffray | 605c591 | 2020-04-08 15:12:39 +0100 | [diff] [blame] | 9012 | DCHECK(!Thread::Current()->IsExceptionPending()) << Thread::Current()->GetException()->Dump(); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 9013 | DCHECK(dex_cache != nullptr); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9014 | DCHECK(referrer == nullptr || !referrer->IsProxyMethod()); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 9015 | // Check for hit in the dex cache. |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9016 | PointerSize pointer_size = image_pointer_size_; |
| 9017 | ArtMethod* resolved = dex_cache->GetResolvedMethod(method_idx, pointer_size); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 9018 | Thread::PoisonObjectPointersIfDebug(); |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 9019 | DCHECK(resolved == nullptr || !resolved->IsRuntimeMethod()); |
| 9020 | bool valid_dex_cache_method = resolved != nullptr; |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9021 | if (kResolveMode == ResolveMode::kNoChecks && valid_dex_cache_method) { |
| 9022 | // 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] | 9023 | DCHECK(resolved->GetDeclaringClassUnchecked() != nullptr) << resolved->GetDexMethodIndex(); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 9024 | return resolved; |
| 9025 | } |
Vladimir Marko | 8901119 | 2017-12-11 13:45:05 +0000 | [diff] [blame] | 9026 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9027 | const dex::MethodId& method_id = dex_file.GetMethodId(method_idx); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9028 | ObjPtr<mirror::Class> klass = nullptr; |
| 9029 | if (valid_dex_cache_method) { |
| 9030 | // We have a valid method from the DexCache but we need to perform ICCE and IAE checks. |
| 9031 | DCHECK(resolved->GetDeclaringClassUnchecked() != nullptr) << resolved->GetDexMethodIndex(); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 9032 | klass = LookupResolvedType(method_id.class_idx_, dex_cache.Get(), class_loader.Get()); |
Vladimir Marko | 6f1bd46 | 2017-12-06 17:45:03 +0000 | [diff] [blame] | 9033 | if (UNLIKELY(klass == nullptr)) { |
Nicolas Geoffray | befa309 | 2018-02-22 14:50:01 +0000 | [diff] [blame] | 9034 | // We normaly should not end up here. However the verifier currently doesn't guarantee |
| 9035 | // the invariant of having the klass in the class table. b/73760543 |
| 9036 | klass = ResolveType(method_id.class_idx_, dex_cache, class_loader); |
Nicolas Geoffray | 0761419 | 2020-04-28 17:31:31 +0100 | [diff] [blame] | 9037 | if (klass == nullptr) { |
| 9038 | // This can only happen if the current thread is not allowed to load |
| 9039 | // classes. |
| 9040 | DCHECK(!Thread::Current()->CanLoadClasses()); |
| 9041 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 9042 | return nullptr; |
| 9043 | } |
Vladimir Marko | 6f1bd46 | 2017-12-06 17:45:03 +0000 | [diff] [blame] | 9044 | } |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9045 | } else { |
| 9046 | // The method was not in the DexCache, resolve the declaring class. |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 9047 | klass = ResolveType(method_id.class_idx_, dex_cache, class_loader); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9048 | if (klass == nullptr) { |
| 9049 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 9050 | return nullptr; |
| 9051 | } |
| 9052 | } |
| 9053 | |
| 9054 | // Check if the invoke type matches the class type. |
| 9055 | if (kResolveMode == ResolveMode::kCheckICCEAndIAE && |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 9056 | CheckInvokeClassMismatch</* kThrow= */ true>( |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9057 | dex_cache.Get(), type, [klass]() { return klass; })) { |
Elliott Hughes | cc5f9a9 | 2011-09-28 19:17:29 -0700 | [diff] [blame] | 9058 | DCHECK(Thread::Current()->IsExceptionPending()); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 9059 | return nullptr; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 9060 | } |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9061 | |
| 9062 | if (!valid_dex_cache_method) { |
Nicolas Geoffray | ea179f4 | 2018-02-08 22:30:18 +0000 | [diff] [blame] | 9063 | resolved = FindResolvedMethod(klass, dex_cache.Get(), class_loader.Get(), method_idx); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 9064 | } |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9065 | |
| 9066 | // Note: We can check for IllegalAccessError only if we have a referrer. |
| 9067 | if (kResolveMode == ResolveMode::kCheckICCEAndIAE && resolved != nullptr && referrer != nullptr) { |
| 9068 | ObjPtr<mirror::Class> methods_class = resolved->GetDeclaringClass(); |
| 9069 | ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass(); |
| 9070 | if (!referring_class->CheckResolvedMethodAccess(methods_class, |
| 9071 | resolved, |
| 9072 | dex_cache.Get(), |
| 9073 | method_idx, |
| 9074 | type)) { |
| 9075 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 9076 | return nullptr; |
| 9077 | } |
| 9078 | } |
| 9079 | |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 9080 | // If we found a method, check for incompatible class changes. |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9081 | if (LIKELY(resolved != nullptr) && |
| 9082 | LIKELY(kResolveMode == ResolveMode::kNoChecks || |
| 9083 | !resolved->CheckIncompatibleClassChange(type))) { |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 9084 | return resolved; |
| 9085 | } else { |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9086 | // If we had a method, or if we can find one with another lookup type, |
| 9087 | // it's an incompatible-class-change error. |
| 9088 | if (resolved == nullptr) { |
David Brazdil | 4525e0b | 2018-04-05 16:57:32 +0100 | [diff] [blame] | 9089 | resolved = FindIncompatibleMethod(klass, dex_cache.Get(), class_loader.Get(), method_idx); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9090 | } |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 9091 | if (resolved != nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 9092 | ThrowIncompatibleClassChangeError(type, resolved->GetInvokeType(), resolved, referrer); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 9093 | } else { |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9094 | // 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] | 9095 | const char* name = dex_file.StringDataByIdx(method_id.name_idx_); |
| 9096 | const Signature signature = dex_file.GetMethodSignature(method_id); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9097 | ThrowNoSuchMethodError(type, klass, name, signature); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 9098 | } |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 9099 | Thread::Current()->AssertPendingException(); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 9100 | return nullptr; |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 9101 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 9102 | } |
| 9103 | |
Vladimir Marko | 8901119 | 2017-12-11 13:45:05 +0000 | [diff] [blame] | 9104 | ArtMethod* ClassLinker::ResolveMethodWithoutInvokeType(uint32_t method_idx, |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 9105 | Handle<mirror::DexCache> dex_cache, |
| 9106 | Handle<mirror::ClassLoader> class_loader) { |
| 9107 | ArtMethod* resolved = dex_cache->GetResolvedMethod(method_idx, image_pointer_size_); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 9108 | Thread::PoisonObjectPointersIfDebug(); |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 9109 | if (resolved != nullptr) { |
| 9110 | DCHECK(!resolved->IsRuntimeMethod()); |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 9111 | DCHECK(resolved->GetDeclaringClassUnchecked() != nullptr) << resolved->GetDexMethodIndex(); |
| 9112 | return resolved; |
| 9113 | } |
| 9114 | // Fail, get the declaring class. |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9115 | const dex::MethodId& method_id = dex_cache->GetDexFile()->GetMethodId(method_idx); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 9116 | 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] | 9117 | if (klass == nullptr) { |
| 9118 | Thread::Current()->AssertPendingException(); |
| 9119 | return nullptr; |
| 9120 | } |
| 9121 | if (klass->IsInterface()) { |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9122 | resolved = klass->FindInterfaceMethod(dex_cache.Get(), method_idx, image_pointer_size_); |
| 9123 | } else { |
| 9124 | resolved = klass->FindClassMethod(dex_cache.Get(), method_idx, image_pointer_size_); |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 9125 | } |
David Brazdil | 8ce3bfa | 2018-03-12 18:01:18 +0000 | [diff] [blame] | 9126 | if (resolved != nullptr && |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 9127 | hiddenapi::ShouldDenyAccessToMember( |
| 9128 | resolved, |
| 9129 | hiddenapi::AccessContext(class_loader.Get(), dex_cache.Get()), |
| 9130 | hiddenapi::AccessMethod::kLinking)) { |
David Brazdil | 8ce3bfa | 2018-03-12 18:01:18 +0000 | [diff] [blame] | 9131 | resolved = nullptr; |
| 9132 | } |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 9133 | return resolved; |
| 9134 | } |
| 9135 | |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 9136 | ArtField* ClassLinker::LookupResolvedField(uint32_t field_idx, |
| 9137 | ObjPtr<mirror::DexCache> dex_cache, |
| 9138 | ObjPtr<mirror::ClassLoader> class_loader, |
| 9139 | bool is_static) { |
| 9140 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9141 | const dex::FieldId& field_id = dex_file.GetFieldId(field_idx); |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 9142 | ObjPtr<mirror::Class> klass = dex_cache->GetResolvedType(field_id.class_idx_); |
| 9143 | if (klass == nullptr) { |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 9144 | klass = LookupResolvedType(field_id.class_idx_, dex_cache, class_loader); |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 9145 | } |
| 9146 | if (klass == nullptr) { |
| 9147 | // The class has not been resolved yet, so the field is also unresolved. |
| 9148 | return nullptr; |
| 9149 | } |
| 9150 | DCHECK(klass->IsResolved()); |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 9151 | |
David Brazdil | 1ab0fa8 | 2018-05-04 11:28:03 +0100 | [diff] [blame] | 9152 | return FindResolvedField(klass, dex_cache, class_loader, field_idx, is_static); |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 9153 | } |
| 9154 | |
Vladimir Marko | e11dd50 | 2017-12-08 14:09:45 +0000 | [diff] [blame] | 9155 | ArtField* ClassLinker::ResolveField(uint32_t field_idx, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 9156 | Handle<mirror::DexCache> dex_cache, |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 9157 | Handle<mirror::ClassLoader> class_loader, |
| 9158 | bool is_static) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 9159 | DCHECK(dex_cache != nullptr); |
Nicolas Geoffray | f368882 | 2020-03-25 15:04:03 +0000 | [diff] [blame] | 9160 | DCHECK(!Thread::Current()->IsExceptionPending()) << Thread::Current()->GetException()->Dump(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 9161 | ArtField* resolved = dex_cache->GetResolvedField(field_idx, image_pointer_size_); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 9162 | Thread::PoisonObjectPointersIfDebug(); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 9163 | if (resolved != nullptr) { |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 9164 | return resolved; |
| 9165 | } |
Vladimir Marko | e11dd50 | 2017-12-08 14:09:45 +0000 | [diff] [blame] | 9166 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9167 | const dex::FieldId& field_id = dex_file.GetFieldId(field_idx); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 9168 | 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] | 9169 | if (klass == nullptr) { |
Ian Rogers | 9f1ab12 | 2011-12-12 08:52:43 -0800 | [diff] [blame] | 9170 | DCHECK(Thread::Current()->IsExceptionPending()); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 9171 | return nullptr; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 9172 | } |
| 9173 | |
David Brazdil | 1ab0fa8 | 2018-05-04 11:28:03 +0100 | [diff] [blame] | 9174 | 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] | 9175 | if (resolved == nullptr) { |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 9176 | const char* name = dex_file.GetFieldName(field_id); |
| 9177 | const char* type = dex_file.GetFieldTypeDescriptor(field_id); |
David Brazdil | 8ce3bfa | 2018-03-12 18:01:18 +0000 | [diff] [blame] | 9178 | ThrowNoSuchFieldError(is_static ? "static " : "instance ", klass, type, name); |
David Brazdil | 8ce3bfa | 2018-03-12 18:01:18 +0000 | [diff] [blame] | 9179 | } |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 9180 | return resolved; |
| 9181 | } |
| 9182 | |
Vladimir Marko | e11dd50 | 2017-12-08 14:09:45 +0000 | [diff] [blame] | 9183 | ArtField* ClassLinker::ResolveFieldJLS(uint32_t field_idx, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 9184 | Handle<mirror::DexCache> dex_cache, |
| 9185 | Handle<mirror::ClassLoader> class_loader) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 9186 | DCHECK(dex_cache != nullptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 9187 | ArtField* resolved = dex_cache->GetResolvedField(field_idx, image_pointer_size_); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 9188 | Thread::PoisonObjectPointersIfDebug(); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 9189 | if (resolved != nullptr) { |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 9190 | return resolved; |
| 9191 | } |
Vladimir Marko | e11dd50 | 2017-12-08 14:09:45 +0000 | [diff] [blame] | 9192 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9193 | const dex::FieldId& field_id = dex_file.GetFieldId(field_idx); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 9194 | 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] | 9195 | if (klass == nullptr) { |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 9196 | DCHECK(Thread::Current()->IsExceptionPending()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 9197 | return nullptr; |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 9198 | } |
| 9199 | |
David Brazdil | 1ab0fa8 | 2018-05-04 11:28:03 +0100 | [diff] [blame] | 9200 | resolved = FindResolvedFieldJLS(klass, dex_cache.Get(), class_loader.Get(), field_idx); |
| 9201 | if (resolved == nullptr) { |
| 9202 | const char* name = dex_file.GetFieldName(field_id); |
| 9203 | const char* type = dex_file.GetFieldTypeDescriptor(field_id); |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 9204 | ThrowNoSuchFieldError("", klass, type, name); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 9205 | } |
| 9206 | return resolved; |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 9207 | } |
| 9208 | |
David Brazdil | 1ab0fa8 | 2018-05-04 11:28:03 +0100 | [diff] [blame] | 9209 | ArtField* ClassLinker::FindResolvedField(ObjPtr<mirror::Class> klass, |
| 9210 | ObjPtr<mirror::DexCache> dex_cache, |
| 9211 | ObjPtr<mirror::ClassLoader> class_loader, |
| 9212 | uint32_t field_idx, |
| 9213 | bool is_static) { |
| 9214 | ArtField* resolved = nullptr; |
| 9215 | Thread* self = is_static ? Thread::Current() : nullptr; |
| 9216 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
| 9217 | |
| 9218 | resolved = is_static ? mirror::Class::FindStaticField(self, klass, dex_cache, field_idx) |
| 9219 | : klass->FindInstanceField(dex_cache, field_idx); |
| 9220 | |
| 9221 | if (resolved == nullptr) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9222 | const dex::FieldId& field_id = dex_file.GetFieldId(field_idx); |
David Brazdil | 1ab0fa8 | 2018-05-04 11:28:03 +0100 | [diff] [blame] | 9223 | const char* name = dex_file.GetFieldName(field_id); |
| 9224 | const char* type = dex_file.GetFieldTypeDescriptor(field_id); |
| 9225 | resolved = is_static ? mirror::Class::FindStaticField(self, klass, name, type) |
| 9226 | : klass->FindInstanceField(name, type); |
| 9227 | } |
| 9228 | |
| 9229 | if (resolved != nullptr && |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 9230 | hiddenapi::ShouldDenyAccessToMember(resolved, |
| 9231 | hiddenapi::AccessContext(class_loader, dex_cache), |
| 9232 | hiddenapi::AccessMethod::kLinking)) { |
David Brazdil | 1ab0fa8 | 2018-05-04 11:28:03 +0100 | [diff] [blame] | 9233 | resolved = nullptr; |
| 9234 | } |
| 9235 | |
| 9236 | if (resolved != nullptr) { |
| 9237 | dex_cache->SetResolvedField(field_idx, resolved, image_pointer_size_); |
| 9238 | } |
| 9239 | |
| 9240 | return resolved; |
| 9241 | } |
| 9242 | |
| 9243 | ArtField* ClassLinker::FindResolvedFieldJLS(ObjPtr<mirror::Class> klass, |
| 9244 | ObjPtr<mirror::DexCache> dex_cache, |
| 9245 | ObjPtr<mirror::ClassLoader> class_loader, |
| 9246 | uint32_t field_idx) { |
| 9247 | ArtField* resolved = nullptr; |
| 9248 | Thread* self = Thread::Current(); |
| 9249 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9250 | const dex::FieldId& field_id = dex_file.GetFieldId(field_idx); |
David Brazdil | 1ab0fa8 | 2018-05-04 11:28:03 +0100 | [diff] [blame] | 9251 | |
| 9252 | const char* name = dex_file.GetFieldName(field_id); |
| 9253 | const char* type = dex_file.GetFieldTypeDescriptor(field_id); |
| 9254 | resolved = mirror::Class::FindField(self, klass, name, type); |
| 9255 | |
| 9256 | if (resolved != nullptr && |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 9257 | hiddenapi::ShouldDenyAccessToMember(resolved, |
| 9258 | hiddenapi::AccessContext(class_loader, dex_cache), |
| 9259 | hiddenapi::AccessMethod::kLinking)) { |
David Brazdil | 1ab0fa8 | 2018-05-04 11:28:03 +0100 | [diff] [blame] | 9260 | resolved = nullptr; |
| 9261 | } |
| 9262 | |
| 9263 | if (resolved != nullptr) { |
| 9264 | dex_cache->SetResolvedField(field_idx, resolved, image_pointer_size_); |
| 9265 | } |
| 9266 | |
| 9267 | return resolved; |
| 9268 | } |
| 9269 | |
Vladimir Marko | af94020 | 2017-12-08 15:01:18 +0000 | [diff] [blame] | 9270 | ObjPtr<mirror::MethodType> ClassLinker::ResolveMethodType( |
| 9271 | Thread* self, |
Orion Hodson | 06d10a7 | 2018-05-14 08:53:38 +0100 | [diff] [blame] | 9272 | dex::ProtoIndex proto_idx, |
Vladimir Marko | af94020 | 2017-12-08 15:01:18 +0000 | [diff] [blame] | 9273 | Handle<mirror::DexCache> dex_cache, |
| 9274 | Handle<mirror::ClassLoader> class_loader) { |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9275 | DCHECK(Runtime::Current()->IsMethodHandlesEnabled()); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 9276 | DCHECK(dex_cache != nullptr); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9277 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 9278 | ObjPtr<mirror::MethodType> resolved = dex_cache->GetResolvedMethodType(proto_idx); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9279 | if (resolved != nullptr) { |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 9280 | return resolved; |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9281 | } |
| 9282 | |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9283 | StackHandleScope<4> hs(self); |
| 9284 | |
| 9285 | // First resolve the return type. |
Vladimir Marko | af94020 | 2017-12-08 15:01:18 +0000 | [diff] [blame] | 9286 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9287 | const dex::ProtoId& proto_id = dex_file.GetProtoId(proto_idx); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9288 | Handle<mirror::Class> return_type(hs.NewHandle( |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 9289 | ResolveType(proto_id.return_type_idx_, dex_cache, class_loader))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 9290 | if (return_type == nullptr) { |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9291 | DCHECK(self->IsExceptionPending()); |
| 9292 | return nullptr; |
| 9293 | } |
| 9294 | |
| 9295 | // Then resolve the argument types. |
| 9296 | // |
| 9297 | // TODO: Is there a better way to figure out the number of method arguments |
| 9298 | // other than by looking at the shorty ? |
| 9299 | const size_t num_method_args = strlen(dex_file.StringDataByIdx(proto_id.shorty_idx_)) - 1; |
| 9300 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 9301 | ObjPtr<mirror::Class> array_of_class = GetClassRoot<mirror::ObjectArray<mirror::Class>>(this); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9302 | Handle<mirror::ObjectArray<mirror::Class>> method_params(hs.NewHandle( |
| 9303 | mirror::ObjectArray<mirror::Class>::Alloc(self, array_of_class, num_method_args))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 9304 | if (method_params == nullptr) { |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9305 | DCHECK(self->IsExceptionPending()); |
| 9306 | return nullptr; |
| 9307 | } |
| 9308 | |
| 9309 | DexFileParameterIterator it(dex_file, proto_id); |
| 9310 | int32_t i = 0; |
| 9311 | MutableHandle<mirror::Class> param_class = hs.NewHandle<mirror::Class>(nullptr); |
| 9312 | for (; it.HasNext(); it.Next()) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 9313 | const dex::TypeIndex type_idx = it.GetTypeIdx(); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 9314 | param_class.Assign(ResolveType(type_idx, dex_cache, class_loader)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 9315 | if (param_class == nullptr) { |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9316 | DCHECK(self->IsExceptionPending()); |
| 9317 | return nullptr; |
| 9318 | } |
| 9319 | |
| 9320 | method_params->Set(i++, param_class.Get()); |
| 9321 | } |
| 9322 | |
| 9323 | DCHECK(!it.HasNext()); |
| 9324 | |
| 9325 | Handle<mirror::MethodType> type = hs.NewHandle( |
| 9326 | mirror::MethodType::Create(self, return_type, method_params)); |
| 9327 | dex_cache->SetResolvedMethodType(proto_idx, type.Get()); |
| 9328 | |
| 9329 | return type.Get(); |
| 9330 | } |
| 9331 | |
Vladimir Marko | af94020 | 2017-12-08 15:01:18 +0000 | [diff] [blame] | 9332 | ObjPtr<mirror::MethodType> ClassLinker::ResolveMethodType(Thread* self, |
Orion Hodson | 06d10a7 | 2018-05-14 08:53:38 +0100 | [diff] [blame] | 9333 | dex::ProtoIndex proto_idx, |
Vladimir Marko | af94020 | 2017-12-08 15:01:18 +0000 | [diff] [blame] | 9334 | ArtMethod* referrer) { |
Orion Hodson | 2e59994 | 2017-09-22 16:17:41 +0100 | [diff] [blame] | 9335 | StackHandleScope<2> hs(self); |
Orion Hodson | 2e59994 | 2017-09-22 16:17:41 +0100 | [diff] [blame] | 9336 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache())); |
| 9337 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(referrer->GetClassLoader())); |
Vladimir Marko | af94020 | 2017-12-08 15:01:18 +0000 | [diff] [blame] | 9338 | return ResolveMethodType(self, proto_idx, dex_cache, class_loader); |
Orion Hodson | 2e59994 | 2017-09-22 16:17:41 +0100 | [diff] [blame] | 9339 | } |
| 9340 | |
Vladimir Marko | 5aead70 | 2019-03-27 11:00:36 +0000 | [diff] [blame] | 9341 | ObjPtr<mirror::MethodHandle> ClassLinker::ResolveMethodHandleForField( |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9342 | Thread* self, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9343 | const dex::MethodHandleItem& method_handle, |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9344 | ArtMethod* referrer) { |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9345 | DexFile::MethodHandleType handle_type = |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9346 | static_cast<DexFile::MethodHandleType>(method_handle.method_handle_type_); |
| 9347 | mirror::MethodHandle::Kind kind; |
Orion Hodson | fd7b2c2 | 2018-03-15 15:38:38 +0000 | [diff] [blame] | 9348 | bool is_put; |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9349 | bool is_static; |
| 9350 | int32_t num_params; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9351 | switch (handle_type) { |
| 9352 | case DexFile::MethodHandleType::kStaticPut: { |
Orion Hodson | 82b351f | 2017-07-05 14:34:25 +0100 | [diff] [blame] | 9353 | kind = mirror::MethodHandle::Kind::kStaticPut; |
Orion Hodson | fd7b2c2 | 2018-03-15 15:38:38 +0000 | [diff] [blame] | 9354 | is_put = true; |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9355 | is_static = true; |
| 9356 | num_params = 1; |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 9357 | break; |
| 9358 | } |
| 9359 | case DexFile::MethodHandleType::kStaticGet: { |
Orion Hodson | 82b351f | 2017-07-05 14:34:25 +0100 | [diff] [blame] | 9360 | kind = mirror::MethodHandle::Kind::kStaticGet; |
Orion Hodson | fd7b2c2 | 2018-03-15 15:38:38 +0000 | [diff] [blame] | 9361 | is_put = false; |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9362 | is_static = true; |
| 9363 | num_params = 0; |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 9364 | break; |
| 9365 | } |
| 9366 | case DexFile::MethodHandleType::kInstancePut: { |
Orion Hodson | 82b351f | 2017-07-05 14:34:25 +0100 | [diff] [blame] | 9367 | kind = mirror::MethodHandle::Kind::kInstancePut; |
Orion Hodson | fd7b2c2 | 2018-03-15 15:38:38 +0000 | [diff] [blame] | 9368 | is_put = true; |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9369 | is_static = false; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9370 | num_params = 2; |
| 9371 | break; |
| 9372 | } |
| 9373 | case DexFile::MethodHandleType::kInstanceGet: { |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9374 | kind = mirror::MethodHandle::Kind::kInstanceGet; |
Orion Hodson | fd7b2c2 | 2018-03-15 15:38:38 +0000 | [diff] [blame] | 9375 | is_put = false; |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9376 | is_static = false; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9377 | num_params = 1; |
| 9378 | break; |
| 9379 | } |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9380 | case DexFile::MethodHandleType::kInvokeStatic: |
Orion Hodson | 82b351f | 2017-07-05 14:34:25 +0100 | [diff] [blame] | 9381 | case DexFile::MethodHandleType::kInvokeInstance: |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9382 | case DexFile::MethodHandleType::kInvokeConstructor: |
Orion Hodson | 82b351f | 2017-07-05 14:34:25 +0100 | [diff] [blame] | 9383 | case DexFile::MethodHandleType::kInvokeDirect: |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9384 | case DexFile::MethodHandleType::kInvokeInterface: |
| 9385 | UNREACHABLE(); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9386 | } |
| 9387 | |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9388 | ArtField* target_field = |
| 9389 | ResolveField(method_handle.field_or_method_idx_, referrer, is_static); |
| 9390 | if (LIKELY(target_field != nullptr)) { |
| 9391 | ObjPtr<mirror::Class> target_class = target_field->GetDeclaringClass(); |
| 9392 | ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass(); |
| 9393 | if (UNLIKELY(!referring_class->CanAccessMember(target_class, target_field->GetAccessFlags()))) { |
| 9394 | ThrowIllegalAccessErrorField(referring_class, target_field); |
| 9395 | return nullptr; |
| 9396 | } |
Orion Hodson | fd7b2c2 | 2018-03-15 15:38:38 +0000 | [diff] [blame] | 9397 | if (UNLIKELY(is_put && target_field->IsFinal())) { |
| 9398 | ThrowIllegalAccessErrorField(referring_class, target_field); |
| 9399 | return nullptr; |
| 9400 | } |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9401 | } else { |
| 9402 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 9403 | return nullptr; |
| 9404 | } |
| 9405 | |
| 9406 | StackHandleScope<4> hs(self); |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 9407 | ObjPtr<mirror::Class> array_of_class = GetClassRoot<mirror::ObjectArray<mirror::Class>>(this); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9408 | Handle<mirror::ObjectArray<mirror::Class>> method_params(hs.NewHandle( |
| 9409 | mirror::ObjectArray<mirror::Class>::Alloc(self, array_of_class, num_params))); |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 9410 | if (UNLIKELY(method_params == nullptr)) { |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9411 | DCHECK(self->IsExceptionPending()); |
| 9412 | return nullptr; |
| 9413 | } |
| 9414 | |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9415 | Handle<mirror::Class> constructor_class; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9416 | Handle<mirror::Class> return_type; |
| 9417 | switch (handle_type) { |
| 9418 | case DexFile::MethodHandleType::kStaticPut: { |
Vladimir Marko | 4098a7a | 2017-11-06 16:00:51 +0000 | [diff] [blame] | 9419 | method_params->Set(0, target_field->ResolveType()); |
Vladimir Marko | 9186b18 | 2018-11-06 14:55:54 +0000 | [diff] [blame] | 9420 | return_type = hs.NewHandle(GetClassRoot(ClassRoot::kPrimitiveVoid, this)); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9421 | break; |
| 9422 | } |
| 9423 | case DexFile::MethodHandleType::kStaticGet: { |
Vladimir Marko | 4098a7a | 2017-11-06 16:00:51 +0000 | [diff] [blame] | 9424 | return_type = hs.NewHandle(target_field->ResolveType()); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9425 | break; |
| 9426 | } |
| 9427 | case DexFile::MethodHandleType::kInstancePut: { |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 9428 | method_params->Set(0, target_field->GetDeclaringClass()); |
Vladimir Marko | 4098a7a | 2017-11-06 16:00:51 +0000 | [diff] [blame] | 9429 | method_params->Set(1, target_field->ResolveType()); |
Vladimir Marko | 9186b18 | 2018-11-06 14:55:54 +0000 | [diff] [blame] | 9430 | return_type = hs.NewHandle(GetClassRoot(ClassRoot::kPrimitiveVoid, this)); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9431 | break; |
| 9432 | } |
| 9433 | case DexFile::MethodHandleType::kInstanceGet: { |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 9434 | method_params->Set(0, target_field->GetDeclaringClass()); |
Vladimir Marko | 4098a7a | 2017-11-06 16:00:51 +0000 | [diff] [blame] | 9435 | return_type = hs.NewHandle(target_field->ResolveType()); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9436 | break; |
| 9437 | } |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9438 | case DexFile::MethodHandleType::kInvokeStatic: |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 9439 | case DexFile::MethodHandleType::kInvokeInstance: |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9440 | case DexFile::MethodHandleType::kInvokeConstructor: |
| 9441 | case DexFile::MethodHandleType::kInvokeDirect: |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 9442 | case DexFile::MethodHandleType::kInvokeInterface: |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9443 | UNREACHABLE(); |
| 9444 | } |
| 9445 | |
| 9446 | for (int32_t i = 0; i < num_params; ++i) { |
| 9447 | if (UNLIKELY(method_params->Get(i) == nullptr)) { |
| 9448 | DCHECK(self->IsExceptionPending()); |
| 9449 | return nullptr; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9450 | } |
| 9451 | } |
| 9452 | |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9453 | if (UNLIKELY(return_type.IsNull())) { |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9454 | DCHECK(self->IsExceptionPending()); |
| 9455 | return nullptr; |
| 9456 | } |
| 9457 | |
| 9458 | Handle<mirror::MethodType> |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9459 | method_type(hs.NewHandle(mirror::MethodType::Create(self, return_type, method_params))); |
| 9460 | if (UNLIKELY(method_type.IsNull())) { |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9461 | DCHECK(self->IsExceptionPending()); |
| 9462 | return nullptr; |
| 9463 | } |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 9464 | |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9465 | uintptr_t target = reinterpret_cast<uintptr_t>(target_field); |
| 9466 | return mirror::MethodHandleImpl::Create(self, target, kind, method_type); |
| 9467 | } |
| 9468 | |
Vladimir Marko | 5aead70 | 2019-03-27 11:00:36 +0000 | [diff] [blame] | 9469 | ObjPtr<mirror::MethodHandle> ClassLinker::ResolveMethodHandleForMethod( |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9470 | Thread* self, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9471 | const dex::MethodHandleItem& method_handle, |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9472 | ArtMethod* referrer) { |
| 9473 | DexFile::MethodHandleType handle_type = |
| 9474 | static_cast<DexFile::MethodHandleType>(method_handle.method_handle_type_); |
| 9475 | mirror::MethodHandle::Kind kind; |
| 9476 | uint32_t receiver_count = 0; |
| 9477 | ArtMethod* target_method = nullptr; |
| 9478 | switch (handle_type) { |
| 9479 | case DexFile::MethodHandleType::kStaticPut: |
| 9480 | case DexFile::MethodHandleType::kStaticGet: |
| 9481 | case DexFile::MethodHandleType::kInstancePut: |
| 9482 | case DexFile::MethodHandleType::kInstanceGet: |
| 9483 | UNREACHABLE(); |
| 9484 | case DexFile::MethodHandleType::kInvokeStatic: { |
| 9485 | kind = mirror::MethodHandle::Kind::kInvokeStatic; |
| 9486 | receiver_count = 0; |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9487 | target_method = ResolveMethod<ResolveMode::kNoChecks>(self, |
| 9488 | method_handle.field_or_method_idx_, |
| 9489 | referrer, |
| 9490 | InvokeType::kStatic); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9491 | break; |
| 9492 | } |
| 9493 | case DexFile::MethodHandleType::kInvokeInstance: { |
| 9494 | kind = mirror::MethodHandle::Kind::kInvokeVirtual; |
| 9495 | receiver_count = 1; |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9496 | target_method = ResolveMethod<ResolveMode::kNoChecks>(self, |
| 9497 | method_handle.field_or_method_idx_, |
| 9498 | referrer, |
| 9499 | InvokeType::kVirtual); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9500 | break; |
| 9501 | } |
| 9502 | case DexFile::MethodHandleType::kInvokeConstructor: { |
| 9503 | // Constructors are currently implemented as a transform. They |
| 9504 | // are special cased later in this method. |
| 9505 | kind = mirror::MethodHandle::Kind::kInvokeTransform; |
| 9506 | receiver_count = 0; |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9507 | target_method = ResolveMethod<ResolveMode::kNoChecks>(self, |
| 9508 | method_handle.field_or_method_idx_, |
| 9509 | referrer, |
| 9510 | InvokeType::kDirect); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9511 | break; |
| 9512 | } |
| 9513 | case DexFile::MethodHandleType::kInvokeDirect: { |
| 9514 | kind = mirror::MethodHandle::Kind::kInvokeDirect; |
| 9515 | receiver_count = 1; |
| 9516 | StackHandleScope<2> hs(self); |
| 9517 | // A constant method handle with type kInvokeDirect can refer to |
| 9518 | // a method that is private or to a method in a super class. To |
| 9519 | // disambiguate the two options, we resolve the method ignoring |
| 9520 | // the invocation type to determine if the method is private. We |
| 9521 | // then resolve again specifying the intended invocation type to |
| 9522 | // force the appropriate checks. |
Vladimir Marko | 8901119 | 2017-12-11 13:45:05 +0000 | [diff] [blame] | 9523 | target_method = ResolveMethodWithoutInvokeType(method_handle.field_or_method_idx_, |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9524 | hs.NewHandle(referrer->GetDexCache()), |
| 9525 | hs.NewHandle(referrer->GetClassLoader())); |
| 9526 | if (UNLIKELY(target_method == nullptr)) { |
| 9527 | break; |
| 9528 | } |
| 9529 | |
| 9530 | if (target_method->IsPrivate()) { |
| 9531 | kind = mirror::MethodHandle::Kind::kInvokeDirect; |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9532 | target_method = ResolveMethod<ResolveMode::kNoChecks>(self, |
| 9533 | method_handle.field_or_method_idx_, |
| 9534 | referrer, |
| 9535 | InvokeType::kDirect); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9536 | } else { |
| 9537 | kind = mirror::MethodHandle::Kind::kInvokeSuper; |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9538 | target_method = ResolveMethod<ResolveMode::kNoChecks>(self, |
| 9539 | method_handle.field_or_method_idx_, |
| 9540 | referrer, |
| 9541 | InvokeType::kSuper); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9542 | if (UNLIKELY(target_method == nullptr)) { |
| 9543 | break; |
| 9544 | } |
| 9545 | // Find the method specified in the parent in referring class |
| 9546 | // so invoke-super invokes the method in the parent of the |
| 9547 | // referrer. |
| 9548 | target_method = |
| 9549 | referrer->GetDeclaringClass()->FindVirtualMethodForVirtual(target_method, |
| 9550 | kRuntimePointerSize); |
| 9551 | } |
| 9552 | break; |
| 9553 | } |
| 9554 | case DexFile::MethodHandleType::kInvokeInterface: { |
| 9555 | kind = mirror::MethodHandle::Kind::kInvokeInterface; |
| 9556 | receiver_count = 1; |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9557 | target_method = ResolveMethod<ResolveMode::kNoChecks>(self, |
| 9558 | method_handle.field_or_method_idx_, |
| 9559 | referrer, |
| 9560 | InvokeType::kInterface); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9561 | break; |
| 9562 | } |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 9563 | } |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9564 | |
| 9565 | if (UNLIKELY(target_method == nullptr)) { |
| 9566 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 9567 | return nullptr; |
| 9568 | } |
| 9569 | |
| 9570 | ObjPtr<mirror::Class> target_class = target_method->GetDeclaringClass(); |
| 9571 | ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass(); |
| 9572 | uint32_t access_flags = target_method->GetAccessFlags(); |
| 9573 | if (UNLIKELY(!referring_class->CanAccessMember(target_class, access_flags))) { |
| 9574 | ThrowIllegalAccessErrorMethod(referring_class, target_method); |
| 9575 | return nullptr; |
| 9576 | } |
| 9577 | |
| 9578 | // Calculate the number of parameters from the method shorty. We add the |
| 9579 | // receiver count (0 or 1) and deduct one for the return value. |
| 9580 | uint32_t shorty_length; |
| 9581 | target_method->GetShorty(&shorty_length); |
| 9582 | int32_t num_params = static_cast<int32_t>(shorty_length + receiver_count - 1); |
| 9583 | |
Orion Hodson | ecd5856 | 2018-09-24 11:27:33 +0100 | [diff] [blame] | 9584 | StackHandleScope<5> hs(self); |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 9585 | ObjPtr<mirror::Class> array_of_class = GetClassRoot<mirror::ObjectArray<mirror::Class>>(this); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9586 | Handle<mirror::ObjectArray<mirror::Class>> method_params(hs.NewHandle( |
| 9587 | mirror::ObjectArray<mirror::Class>::Alloc(self, array_of_class, num_params))); |
| 9588 | if (method_params.Get() == nullptr) { |
| 9589 | DCHECK(self->IsExceptionPending()); |
| 9590 | return nullptr; |
| 9591 | } |
| 9592 | |
Orion Hodson | ecd5856 | 2018-09-24 11:27:33 +0100 | [diff] [blame] | 9593 | const DexFile* dex_file = referrer->GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9594 | 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] | 9595 | int32_t index = 0; |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9596 | if (receiver_count != 0) { |
Orion Hodson | ecd5856 | 2018-09-24 11:27:33 +0100 | [diff] [blame] | 9597 | // Insert receiver. Use the class identified in the method handle rather than the declaring |
| 9598 | // class of the resolved method which may be super class or default interface method |
| 9599 | // (b/115964401). |
| 9600 | ObjPtr<mirror::Class> receiver_class = LookupResolvedType(method_id.class_idx_, referrer); |
| 9601 | // receiver_class should have been resolved when resolving the target method. |
| 9602 | DCHECK(receiver_class != nullptr); |
| 9603 | method_params->Set(index++, receiver_class); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9604 | } |
Orion Hodson | ecd5856 | 2018-09-24 11:27:33 +0100 | [diff] [blame] | 9605 | |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9606 | const dex::ProtoId& proto_id = dex_file->GetProtoId(method_id.proto_idx_); |
Orion Hodson | ecd5856 | 2018-09-24 11:27:33 +0100 | [diff] [blame] | 9607 | DexFileParameterIterator it(*dex_file, proto_id); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9608 | while (it.HasNext()) { |
Orion Hodson | da1cdd0 | 2018-01-31 18:08:28 +0000 | [diff] [blame] | 9609 | DCHECK_LT(index, num_params); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9610 | const dex::TypeIndex type_idx = it.GetTypeIdx(); |
Orion Hodson | ecd5856 | 2018-09-24 11:27:33 +0100 | [diff] [blame] | 9611 | ObjPtr<mirror::Class> klass = ResolveType(type_idx, referrer); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9612 | if (nullptr == klass) { |
| 9613 | DCHECK(self->IsExceptionPending()); |
| 9614 | return nullptr; |
| 9615 | } |
| 9616 | method_params->Set(index++, klass); |
| 9617 | it.Next(); |
| 9618 | } |
| 9619 | |
Orion Hodson | ecd5856 | 2018-09-24 11:27:33 +0100 | [diff] [blame] | 9620 | Handle<mirror::Class> return_type = |
| 9621 | hs.NewHandle(ResolveType(proto_id.return_type_idx_, referrer)); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9622 | if (UNLIKELY(return_type.IsNull())) { |
| 9623 | DCHECK(self->IsExceptionPending()); |
| 9624 | return nullptr; |
| 9625 | } |
| 9626 | |
| 9627 | Handle<mirror::MethodType> |
| 9628 | method_type(hs.NewHandle(mirror::MethodType::Create(self, return_type, method_params))); |
| 9629 | if (UNLIKELY(method_type.IsNull())) { |
| 9630 | DCHECK(self->IsExceptionPending()); |
| 9631 | return nullptr; |
| 9632 | } |
| 9633 | |
| 9634 | if (UNLIKELY(handle_type == DexFile::MethodHandleType::kInvokeConstructor)) { |
| 9635 | Handle<mirror::Class> constructor_class = hs.NewHandle(target_method->GetDeclaringClass()); |
| 9636 | Handle<mirror::MethodHandlesLookup> lookup = |
| 9637 | hs.NewHandle(mirror::MethodHandlesLookup::GetDefault(self)); |
| 9638 | return lookup->FindConstructor(self, constructor_class, method_type); |
| 9639 | } |
| 9640 | |
| 9641 | uintptr_t target = reinterpret_cast<uintptr_t>(target_method); |
| 9642 | return mirror::MethodHandleImpl::Create(self, target, kind, method_type); |
| 9643 | } |
| 9644 | |
Vladimir Marko | af94020 | 2017-12-08 15:01:18 +0000 | [diff] [blame] | 9645 | ObjPtr<mirror::MethodHandle> ClassLinker::ResolveMethodHandle(Thread* self, |
| 9646 | uint32_t method_handle_idx, |
| 9647 | ArtMethod* referrer) |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9648 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9649 | const DexFile* const dex_file = referrer->GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9650 | const dex::MethodHandleItem& method_handle = dex_file->GetMethodHandle(method_handle_idx); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9651 | switch (static_cast<DexFile::MethodHandleType>(method_handle.method_handle_type_)) { |
| 9652 | case DexFile::MethodHandleType::kStaticPut: |
| 9653 | case DexFile::MethodHandleType::kStaticGet: |
| 9654 | case DexFile::MethodHandleType::kInstancePut: |
| 9655 | case DexFile::MethodHandleType::kInstanceGet: |
| 9656 | return ResolveMethodHandleForField(self, method_handle, referrer); |
| 9657 | case DexFile::MethodHandleType::kInvokeStatic: |
| 9658 | case DexFile::MethodHandleType::kInvokeInstance: |
| 9659 | case DexFile::MethodHandleType::kInvokeConstructor: |
| 9660 | case DexFile::MethodHandleType::kInvokeDirect: |
| 9661 | case DexFile::MethodHandleType::kInvokeInterface: |
Orion Hodson | da1cdd0 | 2018-01-31 18:08:28 +0000 | [diff] [blame] | 9662 | return ResolveMethodHandleForMethod(self, method_handle, referrer); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9663 | } |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9664 | } |
| 9665 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 9666 | bool ClassLinker::IsQuickResolutionStub(const void* entry_point) const { |
| 9667 | return (entry_point == GetQuickResolutionStub()) || |
| 9668 | (quick_resolution_trampoline_ == entry_point); |
| 9669 | } |
| 9670 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 9671 | bool ClassLinker::IsQuickToInterpreterBridge(const void* entry_point) const { |
| 9672 | return (entry_point == GetQuickToInterpreterBridge()) || |
| 9673 | (quick_to_interpreter_bridge_trampoline_ == entry_point); |
| 9674 | } |
| 9675 | |
| 9676 | bool ClassLinker::IsQuickGenericJniStub(const void* entry_point) const { |
| 9677 | return (entry_point == GetQuickGenericJniStub()) || |
| 9678 | (quick_generic_jni_trampoline_ == entry_point); |
| 9679 | } |
| 9680 | |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 9681 | bool ClassLinker::IsJniDlsymLookupStub(const void* entry_point) const { |
Vladimir Marko | 7dac864 | 2019-11-06 17:09:30 +0000 | [diff] [blame] | 9682 | return entry_point == GetJniDlsymLookupStub() || |
| 9683 | (jni_dlsym_lookup_trampoline_ == entry_point); |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 9684 | } |
| 9685 | |
Vladimir Marko | fa458ac | 2020-02-12 14:08:07 +0000 | [diff] [blame] | 9686 | bool ClassLinker::IsJniDlsymLookupCriticalStub(const void* entry_point) const { |
| 9687 | return entry_point == GetJniDlsymLookupCriticalStub() || |
| 9688 | (jni_dlsym_lookup_critical_trampoline_ == entry_point); |
| 9689 | } |
| 9690 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 9691 | const void* ClassLinker::GetRuntimeQuickGenericJniStub() const { |
| 9692 | return GetQuickGenericJniStub(); |
| 9693 | } |
| 9694 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 9695 | void ClassLinker::SetEntryPointsToInterpreter(ArtMethod* method) const { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 9696 | if (!method->IsNative()) { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 9697 | method->SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge()); |
| 9698 | } else { |
Goran Jakovljevic | c16268f | 2017-07-27 10:03:32 +0200 | [diff] [blame] | 9699 | method->SetEntryPointFromQuickCompiledCode(GetQuickGenericJniStub()); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 9700 | } |
| 9701 | } |
| 9702 | |
Alex Light | db01a09 | 2017-04-03 15:39:55 -0700 | [diff] [blame] | 9703 | void ClassLinker::SetEntryPointsForObsoleteMethod(ArtMethod* method) const { |
| 9704 | DCHECK(method->IsObsolete()); |
| 9705 | // We cannot mess with the entrypoints of native methods because they are used to determine how |
| 9706 | // large the method's quick stack frame is. Without this information we cannot walk the stacks. |
| 9707 | if (!method->IsNative()) { |
| 9708 | method->SetEntryPointFromQuickCompiledCode(GetInvokeObsoleteMethodStub()); |
| 9709 | } |
| 9710 | } |
| 9711 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 9712 | void ClassLinker::DumpForSigQuit(std::ostream& os) { |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 9713 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 9714 | ReaderMutexLock mu(soa.Self(), *Locks::classlinker_classes_lock_); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 9715 | os << "Zygote loaded classes=" << NumZygoteClasses() << " post zygote classes=" |
| 9716 | << NumNonZygoteClasses() << "\n"; |
Nicolas Geoffray | a90c922 | 2018-09-07 13:19:19 +0100 | [diff] [blame] | 9717 | ReaderMutexLock mu2(soa.Self(), *Locks::dex_lock_); |
| 9718 | os << "Dumping registered class loaders\n"; |
| 9719 | size_t class_loader_index = 0; |
| 9720 | for (const ClassLoaderData& class_loader : class_loaders_) { |
| 9721 | ObjPtr<mirror::ClassLoader> loader = |
| 9722 | ObjPtr<mirror::ClassLoader>::DownCast(soa.Self()->DecodeJObject(class_loader.weak_root)); |
| 9723 | if (loader != nullptr) { |
| 9724 | os << "#" << class_loader_index++ << " " << loader->GetClass()->PrettyDescriptor() << ": ["; |
| 9725 | bool saw_one_dex_file = false; |
| 9726 | for (const DexCacheData& dex_cache : dex_caches_) { |
| 9727 | if (dex_cache.IsValid() && dex_cache.class_table == class_loader.class_table) { |
| 9728 | if (saw_one_dex_file) { |
| 9729 | os << ":"; |
| 9730 | } |
| 9731 | saw_one_dex_file = true; |
| 9732 | os << dex_cache.dex_file->GetLocation(); |
| 9733 | } |
| 9734 | } |
| 9735 | os << "]"; |
| 9736 | bool found_parent = false; |
| 9737 | if (loader->GetParent() != nullptr) { |
| 9738 | size_t parent_index = 0; |
| 9739 | for (const ClassLoaderData& class_loader2 : class_loaders_) { |
| 9740 | ObjPtr<mirror::ClassLoader> loader2 = ObjPtr<mirror::ClassLoader>::DownCast( |
| 9741 | soa.Self()->DecodeJObject(class_loader2.weak_root)); |
| 9742 | if (loader2 == loader->GetParent()) { |
| 9743 | os << ", parent #" << parent_index; |
| 9744 | found_parent = true; |
| 9745 | break; |
| 9746 | } |
| 9747 | parent_index++; |
| 9748 | } |
| 9749 | if (!found_parent) { |
| 9750 | os << ", unregistered parent of type " |
| 9751 | << loader->GetParent()->GetClass()->PrettyDescriptor(); |
| 9752 | } |
| 9753 | } else { |
| 9754 | os << ", no parent"; |
| 9755 | } |
| 9756 | os << "\n"; |
| 9757 | } |
| 9758 | } |
| 9759 | os << "Done dumping class loaders\n"; |
Andreas Gampe | 9b7f8b5 | 2019-06-07 08:59:29 -0700 | [diff] [blame] | 9760 | Runtime* runtime = Runtime::Current(); |
| 9761 | os << "Classes initialized: " << runtime->GetStat(KIND_GLOBAL_CLASS_INIT_COUNT) << " in " |
| 9762 | << PrettyDuration(runtime->GetStat(KIND_GLOBAL_CLASS_INIT_TIME)) << "\n"; |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 9763 | } |
| 9764 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 9765 | class CountClassesVisitor : public ClassLoaderVisitor { |
| 9766 | public: |
| 9767 | CountClassesVisitor() : num_zygote_classes(0), num_non_zygote_classes(0) {} |
| 9768 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 9769 | void Visit(ObjPtr<mirror::ClassLoader> class_loader) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 9770 | REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_) override { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 9771 | ClassTable* const class_table = class_loader->GetClassTable(); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 9772 | if (class_table != nullptr) { |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 9773 | num_zygote_classes += class_table->NumZygoteClasses(class_loader); |
| 9774 | num_non_zygote_classes += class_table->NumNonZygoteClasses(class_loader); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 9775 | } |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 9776 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 9777 | |
| 9778 | size_t num_zygote_classes; |
| 9779 | size_t num_non_zygote_classes; |
| 9780 | }; |
| 9781 | |
| 9782 | size_t ClassLinker::NumZygoteClasses() const { |
| 9783 | CountClassesVisitor visitor; |
| 9784 | VisitClassLoaders(&visitor); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 9785 | return visitor.num_zygote_classes + boot_class_table_->NumZygoteClasses(nullptr); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 9786 | } |
| 9787 | |
| 9788 | size_t ClassLinker::NumNonZygoteClasses() const { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 9789 | CountClassesVisitor visitor; |
| 9790 | VisitClassLoaders(&visitor); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 9791 | return visitor.num_non_zygote_classes + boot_class_table_->NumNonZygoteClasses(nullptr); |
Elliott Hughes | cac6cc7 | 2011-11-03 20:31:21 -0700 | [diff] [blame] | 9792 | } |
| 9793 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 9794 | size_t ClassLinker::NumLoadedClasses() { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 9795 | ReaderMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 9796 | // 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] | 9797 | return NumNonZygoteClasses(); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 9798 | } |
| 9799 | |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 9800 | pid_t ClassLinker::GetClassesLockOwner() { |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 9801 | return Locks::classlinker_classes_lock_->GetExclusiveOwnerTid(); |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 9802 | } |
| 9803 | |
| 9804 | pid_t ClassLinker::GetDexLockOwner() { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 9805 | return Locks::dex_lock_->GetExclusiveOwnerTid(); |
Brian Carlstrom | 24a3c2e | 2011-10-17 18:07:52 -0700 | [diff] [blame] | 9806 | } |
| 9807 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 9808 | void ClassLinker::SetClassRoot(ClassRoot class_root, ObjPtr<mirror::Class> klass) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 9809 | DCHECK(!init_done_); |
| 9810 | |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 9811 | DCHECK(klass != nullptr); |
| 9812 | DCHECK(klass->GetClassLoader() == nullptr); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 9813 | |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 9814 | mirror::ObjectArray<mirror::Class>* class_roots = class_roots_.Read(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 9815 | DCHECK(class_roots != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 9816 | DCHECK_LT(static_cast<uint32_t>(class_root), static_cast<uint32_t>(ClassRoot::kMax)); |
| 9817 | int32_t index = static_cast<int32_t>(class_root); |
| 9818 | DCHECK(class_roots->Get(index) == nullptr); |
| 9819 | class_roots->Set<false>(index, klass); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 9820 | } |
| 9821 | |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 9822 | ObjPtr<mirror::ClassLoader> ClassLinker::CreateWellKnownClassLoader( |
| 9823 | Thread* self, |
| 9824 | const std::vector<const DexFile*>& dex_files, |
| 9825 | Handle<mirror::Class> loader_class, |
Nicolas Geoffray | e167273 | 2018-11-30 01:09:49 +0000 | [diff] [blame] | 9826 | Handle<mirror::ClassLoader> parent_loader, |
| 9827 | Handle<mirror::ObjectArray<mirror::ClassLoader>> shared_libraries) { |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 9828 | |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 9829 | StackHandleScope<5> hs(self); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9830 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 9831 | ArtField* dex_elements_field = |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 9832 | jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList_dexElements); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9833 | |
Vladimir Marko | 4098a7a | 2017-11-06 16:00:51 +0000 | [diff] [blame] | 9834 | Handle<mirror::Class> dex_elements_class(hs.NewHandle(dex_elements_field->ResolveType())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 9835 | DCHECK(dex_elements_class != nullptr); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9836 | DCHECK(dex_elements_class->IsArrayClass()); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 9837 | Handle<mirror::ObjectArray<mirror::Object>> h_dex_elements(hs.NewHandle( |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 9838 | mirror::ObjectArray<mirror::Object>::Alloc(self, |
| 9839 | dex_elements_class.Get(), |
| 9840 | dex_files.size()))); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9841 | Handle<mirror::Class> h_dex_element_class = |
| 9842 | hs.NewHandle(dex_elements_class->GetComponentType()); |
| 9843 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 9844 | ArtField* element_file_field = |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 9845 | jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 9846 | DCHECK_EQ(h_dex_element_class.Get(), element_file_field->GetDeclaringClass()); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9847 | |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 9848 | ArtField* cookie_field = jni::DecodeArtField(WellKnownClasses::dalvik_system_DexFile_cookie); |
Vladimir Marko | 208f670 | 2017-12-08 12:00:50 +0000 | [diff] [blame] | 9849 | DCHECK_EQ(cookie_field->GetDeclaringClass(), element_file_field->LookupResolvedType()); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9850 | |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 9851 | ArtField* file_name_field = jni::DecodeArtField(WellKnownClasses::dalvik_system_DexFile_fileName); |
Vladimir Marko | 208f670 | 2017-12-08 12:00:50 +0000 | [diff] [blame] | 9852 | DCHECK_EQ(file_name_field->GetDeclaringClass(), element_file_field->LookupResolvedType()); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 9853 | |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9854 | // Fill the elements array. |
| 9855 | int32_t index = 0; |
| 9856 | for (const DexFile* dex_file : dex_files) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 9857 | StackHandleScope<4> hs2(self); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9858 | |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 9859 | // CreateWellKnownClassLoader is only used by gtests and compiler. |
| 9860 | // 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] | 9861 | Handle<mirror::LongArray> h_long_array = hs2.NewHandle(mirror::LongArray::Alloc( |
| 9862 | self, |
| 9863 | kDexFileIndexStart + 1)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 9864 | DCHECK(h_long_array != nullptr); |
Vladimir Marko | 78baed5 | 2018-10-11 10:44:58 +0100 | [diff] [blame] | 9865 | h_long_array->Set(kDexFileIndexStart, reinterpret_cast64<int64_t>(dex_file)); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9866 | |
Mathieu Chartier | 3738e98 | 2017-05-12 16:07:28 -0700 | [diff] [blame] | 9867 | // Note that this creates a finalizable dalvik.system.DexFile object and a corresponding |
| 9868 | // FinalizerReference which will never get cleaned up without a started runtime. |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9869 | Handle<mirror::Object> h_dex_file = hs2.NewHandle( |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 9870 | cookie_field->GetDeclaringClass()->AllocObject(self)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 9871 | DCHECK(h_dex_file != nullptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 9872 | cookie_field->SetObject<false>(h_dex_file.Get(), h_long_array.Get()); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9873 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 9874 | Handle<mirror::String> h_file_name = hs2.NewHandle( |
| 9875 | mirror::String::AllocFromModifiedUtf8(self, dex_file->GetLocation().c_str())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 9876 | DCHECK(h_file_name != nullptr); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 9877 | file_name_field->SetObject<false>(h_dex_file.Get(), h_file_name.Get()); |
| 9878 | |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9879 | 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] | 9880 | DCHECK(h_element != nullptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 9881 | element_file_field->SetObject<false>(h_element.Get(), h_dex_file.Get()); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9882 | |
| 9883 | h_dex_elements->Set(index, h_element.Get()); |
| 9884 | index++; |
| 9885 | } |
| 9886 | DCHECK_EQ(index, h_dex_elements->GetLength()); |
| 9887 | |
| 9888 | // Create DexPathList. |
| 9889 | Handle<mirror::Object> h_dex_path_list = hs.NewHandle( |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 9890 | dex_elements_field->GetDeclaringClass()->AllocObject(self)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 9891 | DCHECK(h_dex_path_list != nullptr); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9892 | // Set elements. |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 9893 | 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] | 9894 | // Create an empty List for the "nativeLibraryDirectories," required for native tests. |
| 9895 | // Note: this code is uncommon(oatdump)/testing-only, so don't add further WellKnownClasses |
| 9896 | // elements. |
| 9897 | { |
| 9898 | ArtField* native_lib_dirs = dex_elements_field->GetDeclaringClass()-> |
| 9899 | FindDeclaredInstanceField("nativeLibraryDirectories", "Ljava/util/List;"); |
| 9900 | DCHECK(native_lib_dirs != nullptr); |
| 9901 | ObjPtr<mirror::Class> list_class = FindSystemClass(self, "Ljava/util/ArrayList;"); |
| 9902 | DCHECK(list_class != nullptr); |
| 9903 | { |
| 9904 | StackHandleScope<1> h_list_scope(self); |
| 9905 | Handle<mirror::Class> h_list_class(h_list_scope.NewHandle<mirror::Class>(list_class)); |
| 9906 | bool list_init = EnsureInitialized(self, h_list_class, true, true); |
| 9907 | DCHECK(list_init); |
| 9908 | list_class = h_list_class.Get(); |
| 9909 | } |
| 9910 | ObjPtr<mirror::Object> list_object = list_class->AllocObject(self); |
| 9911 | // Note: we leave the object uninitialized. This must never leak into any non-testing code, but |
| 9912 | // is fine for testing. While it violates a Java-code invariant (the elementData field is |
| 9913 | // normally never null), as long as one does not try to add elements, this will still |
| 9914 | // work. |
| 9915 | native_lib_dirs->SetObject<false>(h_dex_path_list.Get(), list_object); |
| 9916 | } |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9917 | |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 9918 | // Create the class loader.. |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 9919 | Handle<mirror::ClassLoader> h_class_loader = hs.NewHandle<mirror::ClassLoader>( |
| 9920 | ObjPtr<mirror::ClassLoader>::DownCast(loader_class->AllocObject(self))); |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 9921 | DCHECK(h_class_loader != nullptr); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9922 | // Set DexPathList. |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 9923 | ArtField* path_list_field = |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 9924 | jni::DecodeArtField(WellKnownClasses::dalvik_system_BaseDexClassLoader_pathList); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 9925 | DCHECK(path_list_field != nullptr); |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 9926 | 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] | 9927 | |
| 9928 | // Make a pretend boot-classpath. |
| 9929 | // TODO: Should we scan the image? |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 9930 | ArtField* const parent_field = |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 9931 | mirror::Class::FindField(self, |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 9932 | h_class_loader->GetClass(), |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 9933 | "parent", |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 9934 | "Ljava/lang/ClassLoader;"); |
Roland Levillain | f39c9eb | 2015-05-26 15:02:07 +0100 | [diff] [blame] | 9935 | DCHECK(parent_field != nullptr); |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 9936 | if (parent_loader.Get() == nullptr) { |
| 9937 | ScopedObjectAccessUnchecked soa(self); |
| 9938 | ObjPtr<mirror::Object> boot_loader(soa.Decode<mirror::Class>( |
| 9939 | WellKnownClasses::java_lang_BootClassLoader)->AllocObject(self)); |
| 9940 | parent_field->SetObject<false>(h_class_loader.Get(), boot_loader); |
| 9941 | } else { |
| 9942 | parent_field->SetObject<false>(h_class_loader.Get(), parent_loader.Get()); |
| 9943 | } |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 9944 | |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 9945 | ArtField* shared_libraries_field = |
| 9946 | jni::DecodeArtField(WellKnownClasses::dalvik_system_BaseDexClassLoader_sharedLibraryLoaders); |
| 9947 | DCHECK(shared_libraries_field != nullptr); |
| 9948 | shared_libraries_field->SetObject<false>(h_class_loader.Get(), shared_libraries.Get()); |
| 9949 | |
| 9950 | return h_class_loader.Get(); |
| 9951 | } |
| 9952 | |
| 9953 | jobject ClassLinker::CreateWellKnownClassLoader(Thread* self, |
| 9954 | const std::vector<const DexFile*>& dex_files, |
| 9955 | jclass loader_class, |
Nicolas Geoffray | e167273 | 2018-11-30 01:09:49 +0000 | [diff] [blame] | 9956 | jobject parent_loader, |
| 9957 | jobject shared_libraries) { |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 9958 | CHECK(self->GetJniEnv()->IsSameObject(loader_class, |
| 9959 | WellKnownClasses::dalvik_system_PathClassLoader) || |
| 9960 | self->GetJniEnv()->IsSameObject(loader_class, |
David Brazdil | 1a9ac53 | 2019-03-05 11:57:13 +0000 | [diff] [blame] | 9961 | WellKnownClasses::dalvik_system_DelegateLastClassLoader) || |
| 9962 | self->GetJniEnv()->IsSameObject(loader_class, |
| 9963 | WellKnownClasses::dalvik_system_InMemoryDexClassLoader)); |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 9964 | |
| 9965 | // SOAAlreadyRunnable is protected, and we need something to add a global reference. |
| 9966 | // We could move the jobject to the callers, but all call-sites do this... |
| 9967 | ScopedObjectAccessUnchecked soa(self); |
| 9968 | |
| 9969 | // 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] | 9970 | StackHandleScope<4> hs(self); |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 9971 | |
| 9972 | Handle<mirror::Class> h_loader_class = |
| 9973 | hs.NewHandle<mirror::Class>(soa.Decode<mirror::Class>(loader_class)); |
Nicolas Geoffray | e167273 | 2018-11-30 01:09:49 +0000 | [diff] [blame] | 9974 | Handle<mirror::ClassLoader> h_parent = |
| 9975 | hs.NewHandle<mirror::ClassLoader>(soa.Decode<mirror::ClassLoader>(parent_loader)); |
| 9976 | Handle<mirror::ObjectArray<mirror::ClassLoader>> h_shared_libraries = |
| 9977 | hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::ClassLoader>>(shared_libraries)); |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 9978 | |
| 9979 | ObjPtr<mirror::ClassLoader> loader = CreateWellKnownClassLoader( |
| 9980 | self, |
| 9981 | dex_files, |
| 9982 | h_loader_class, |
Nicolas Geoffray | e167273 | 2018-11-30 01:09:49 +0000 | [diff] [blame] | 9983 | h_parent, |
| 9984 | h_shared_libraries); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9985 | |
| 9986 | // Make it a global ref and return. |
| 9987 | ScopedLocalRef<jobject> local_ref( |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 9988 | soa.Env(), soa.Env()->AddLocalReference<jobject>(loader)); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9989 | return soa.Env()->NewGlobalRef(local_ref.get()); |
| 9990 | } |
| 9991 | |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 9992 | jobject ClassLinker::CreatePathClassLoader(Thread* self, |
| 9993 | const std::vector<const DexFile*>& dex_files) { |
| 9994 | return CreateWellKnownClassLoader(self, |
| 9995 | dex_files, |
| 9996 | WellKnownClasses::dalvik_system_PathClassLoader, |
| 9997 | nullptr); |
| 9998 | } |
| 9999 | |
Andreas Gampe | 8ac7595 | 2015-06-02 21:01:45 -0700 | [diff] [blame] | 10000 | void ClassLinker::DropFindArrayClassCache() { |
| 10001 | std::fill_n(find_array_class_cache_, kFindArrayCacheSize, GcRoot<mirror::Class>(nullptr)); |
| 10002 | find_array_class_cache_next_victim_ = 0; |
| 10003 | } |
| 10004 | |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 10005 | void ClassLinker::VisitClassLoaders(ClassLoaderVisitor* visitor) const { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 10006 | Thread* const self = Thread::Current(); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 10007 | for (const ClassLoaderData& data : class_loaders_) { |
Mathieu Chartier | 4843bd5 | 2015-10-01 17:08:44 -0700 | [diff] [blame] | 10008 | // 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] | 10009 | ObjPtr<mirror::ClassLoader> class_loader = ObjPtr<mirror::ClassLoader>::DownCast( |
| 10010 | self->DecodeJObject(data.weak_root)); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 10011 | if (class_loader != nullptr) { |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 10012 | visitor->Visit(class_loader); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 10013 | } |
| 10014 | } |
| 10015 | } |
| 10016 | |
Alexey Grebenkin | 252a4e4 | 2018-04-02 18:18:01 +0300 | [diff] [blame] | 10017 | void ClassLinker::VisitAllocators(AllocatorVisitor* visitor) const { |
| 10018 | for (const ClassLoaderData& data : class_loaders_) { |
| 10019 | LinearAlloc* alloc = data.allocator; |
| 10020 | if (alloc != nullptr && !visitor->Visit(alloc)) { |
| 10021 | break; |
| 10022 | } |
| 10023 | } |
| 10024 | } |
| 10025 | |
Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 10026 | void ClassLinker::InsertDexFileInToClassLoader(ObjPtr<mirror::Object> dex_file, |
| 10027 | ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 10028 | DCHECK(dex_file != nullptr); |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 10029 | Thread* const self = Thread::Current(); |
| 10030 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 10031 | ClassTable* const table = ClassTableForClassLoader(class_loader); |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 10032 | DCHECK(table != nullptr); |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 10033 | if (table->InsertStrongRoot(dex_file) && class_loader != nullptr) { |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 10034 | // It was not already inserted, perform the write barrier to let the GC know the class loader's |
| 10035 | // class table was modified. |
Mathieu Chartier | 88ea61e | 2018-06-20 17:45:41 -0700 | [diff] [blame] | 10036 | WriteBarrier::ForEveryFieldWrite(class_loader); |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 10037 | } |
| 10038 | } |
| 10039 | |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 10040 | void ClassLinker::CleanupClassLoaders() { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 10041 | Thread* const self = Thread::Current(); |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 10042 | std::vector<ClassLoaderData> to_delete; |
| 10043 | // Do the delete outside the lock to avoid lock violation in jit code cache. |
| 10044 | { |
| 10045 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 10046 | for (auto it = class_loaders_.begin(); it != class_loaders_.end(); ) { |
| 10047 | const ClassLoaderData& data = *it; |
| 10048 | // 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] | 10049 | ObjPtr<mirror::ClassLoader> class_loader = |
| 10050 | ObjPtr<mirror::ClassLoader>::DownCast(self->DecodeJObject(data.weak_root)); |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 10051 | if (class_loader != nullptr) { |
| 10052 | ++it; |
| 10053 | } else { |
| 10054 | VLOG(class_linker) << "Freeing class loader"; |
| 10055 | to_delete.push_back(data); |
| 10056 | it = class_loaders_.erase(it); |
| 10057 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 10058 | } |
| 10059 | } |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 10060 | for (ClassLoaderData& data : to_delete) { |
Alexey Grebenkin | be4c2bd | 2018-02-01 19:09:59 +0300 | [diff] [blame] | 10061 | // CHA unloading analysis and SingleImplementaion cleanups are required. |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 10062 | DeleteClassLoader(self, data, /*cleanup_cha=*/ true); |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 10063 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 10064 | } |
| 10065 | |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 10066 | class ClassLinker::FindVirtualMethodHolderVisitor : public ClassVisitor { |
| 10067 | public: |
| 10068 | FindVirtualMethodHolderVisitor(const ArtMethod* method, PointerSize pointer_size) |
| 10069 | : method_(method), |
| 10070 | pointer_size_(pointer_size) {} |
| 10071 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 10072 | bool operator()(ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_) override { |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 10073 | if (klass->GetVirtualMethodsSliceUnchecked(pointer_size_).Contains(method_)) { |
| 10074 | holder_ = klass; |
| 10075 | } |
| 10076 | // Return false to stop searching if holder_ is not null. |
| 10077 | return holder_ == nullptr; |
| 10078 | } |
| 10079 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 10080 | ObjPtr<mirror::Class> holder_ = nullptr; |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 10081 | const ArtMethod* const method_; |
| 10082 | const PointerSize pointer_size_; |
| 10083 | }; |
| 10084 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 10085 | ObjPtr<mirror::Class> ClassLinker::GetHoldingClassOfCopiedMethod(ArtMethod* method) { |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 10086 | ScopedTrace trace(__FUNCTION__); // Since this function is slow, have a trace to notify people. |
| 10087 | CHECK(method->IsCopied()); |
| 10088 | FindVirtualMethodHolderVisitor visitor(method, image_pointer_size_); |
| 10089 | VisitClasses(&visitor); |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 10090 | return visitor.holder_; |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 10091 | } |
| 10092 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 10093 | ObjPtr<mirror::IfTable> ClassLinker::AllocIfTable(Thread* self, size_t ifcount) { |
| 10094 | return ObjPtr<mirror::IfTable>::DownCast(ObjPtr<mirror::ObjectArray<mirror::Object>>( |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 10095 | mirror::IfTable::Alloc(self, |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 10096 | GetClassRoot<mirror::ObjectArray<mirror::Object>>(this), |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 10097 | ifcount * mirror::IfTable::kMax))); |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 10098 | } |
| 10099 | |
Vladimir Marko | d1f7351 | 2020-04-02 10:50:35 +0100 | [diff] [blame] | 10100 | bool ClassLinker::IsUpdatableBootClassPathDescriptor(const char* descriptor ATTRIBUTE_UNUSED) { |
| 10101 | // Should not be called on ClassLinker, only on AotClassLinker that overrides this. |
| 10102 | LOG(FATAL) << "UNREACHABLE"; |
| 10103 | UNREACHABLE(); |
| 10104 | } |
| 10105 | |
Calin Juravle | 3378768 | 2019-07-26 14:27:18 -0700 | [diff] [blame] | 10106 | bool ClassLinker::DenyAccessBasedOnPublicSdk(ArtMethod* art_method ATTRIBUTE_UNUSED) const |
| 10107 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 10108 | // Should not be called on ClassLinker, only on AotClassLinker that overrides this. |
| 10109 | LOG(FATAL) << "UNREACHABLE"; |
| 10110 | UNREACHABLE(); |
| 10111 | } |
| 10112 | |
| 10113 | bool ClassLinker::DenyAccessBasedOnPublicSdk(ArtField* art_field ATTRIBUTE_UNUSED) const |
| 10114 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 10115 | // Should not be called on ClassLinker, only on AotClassLinker that overrides this. |
| 10116 | LOG(FATAL) << "UNREACHABLE"; |
| 10117 | UNREACHABLE(); |
| 10118 | } |
| 10119 | |
| 10120 | bool ClassLinker::DenyAccessBasedOnPublicSdk(const char* type_descriptor ATTRIBUTE_UNUSED) const { |
| 10121 | // Should not be called on ClassLinker, only on AotClassLinker that overrides this. |
| 10122 | LOG(FATAL) << "UNREACHABLE"; |
| 10123 | UNREACHABLE(); |
| 10124 | } |
| 10125 | |
Calin Juravle | 2c2724c | 2021-01-14 19:54:23 -0800 | [diff] [blame] | 10126 | void ClassLinker::SetEnablePublicSdkChecks(bool enabled ATTRIBUTE_UNUSED) { |
| 10127 | // Should not be called on ClassLinker, only on AotClassLinker that overrides this. |
| 10128 | LOG(FATAL) << "UNREACHABLE"; |
| 10129 | UNREACHABLE(); |
| 10130 | } |
| 10131 | |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 10132 | // Instantiate ClassLinker::ResolveMethod. |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 10133 | template ArtMethod* ClassLinker::ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>( |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 10134 | uint32_t method_idx, |
| 10135 | Handle<mirror::DexCache> dex_cache, |
| 10136 | Handle<mirror::ClassLoader> class_loader, |
| 10137 | ArtMethod* referrer, |
| 10138 | InvokeType type); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 10139 | template ArtMethod* ClassLinker::ResolveMethod<ClassLinker::ResolveMode::kNoChecks>( |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 10140 | uint32_t method_idx, |
| 10141 | Handle<mirror::DexCache> dex_cache, |
| 10142 | Handle<mirror::ClassLoader> class_loader, |
| 10143 | ArtMethod* referrer, |
| 10144 | InvokeType type); |
| 10145 | |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 10146 | // Instantiate ClassLinker::AllocClass. |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 10147 | template ObjPtr<mirror::Class> ClassLinker::AllocClass</* kMovable= */ true>( |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 10148 | Thread* self, |
| 10149 | ObjPtr<mirror::Class> java_lang_Class, |
| 10150 | uint32_t class_size); |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 10151 | template ObjPtr<mirror::Class> ClassLinker::AllocClass</* kMovable= */ false>( |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 10152 | Thread* self, |
| 10153 | ObjPtr<mirror::Class> java_lang_Class, |
| 10154 | uint32_t class_size); |
| 10155 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 10156 | } // namespace art |