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" |
Nicolas Geoffray | ef0b9c4 | 2022-07-04 16:24:06 +0000 | [diff] [blame] | 40 | #include "base/arena_bit_vector.h" |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 41 | #include "base/casts.h" |
Andreas Gampe | 19f5416 | 2019-05-14 16:16:28 -0700 | [diff] [blame] | 42 | #include "base/file_utils.h" |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 43 | #include "base/hash_map.h" |
| 44 | #include "base/hash_set.h" |
David Sehr | 67bf42e | 2018-02-26 16:43:04 -0800 | [diff] [blame] | 45 | #include "base/leb128.h" |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 46 | #include "base/logging.h" |
Lokesh Gidra | 606bd94 | 2022-05-23 19:00:09 +0000 | [diff] [blame] | 47 | #include "base/mem_map_arena_pool.h" |
Eric Holk | 74584e6 | 2021-02-18 14:39:17 -0800 | [diff] [blame] | 48 | #include "base/metrics/metrics.h" |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 49 | #include "base/mutex-inl.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 50 | #include "base/os.h" |
| 51 | #include "base/quasi_atomic.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 52 | #include "base/scoped_arena_containers.h" |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 53 | #include "base/scoped_flock.h" |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 54 | #include "base/stl_util.h" |
Vladimir Marko | b9c29f6 | 2019-03-20 14:22:51 +0000 | [diff] [blame] | 55 | #include "base/string_view_cpp20.h" |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 56 | #include "base/systrace.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 57 | #include "base/time_utils.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 58 | #include "base/unix_file/fd_file.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 59 | #include "base/utils.h" |
Andreas Gampe | b9aec2c | 2015-04-23 22:23:47 -0700 | [diff] [blame] | 60 | #include "base/value_object.h" |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 61 | #include "cha.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 62 | #include "class_linker-inl.h" |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 63 | #include "class_loader_utils.h" |
Vladimir Marko | 5868ada | 2020-05-12 11:50:34 +0100 | [diff] [blame] | 64 | #include "class_root-inl.h" |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 65 | #include "class_table-inl.h" |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 66 | #include "compiler_callbacks.h" |
Vladimir Marko | 606adb3 | 2018-04-05 14:49:24 +0100 | [diff] [blame] | 67 | #include "debug_print.h" |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 68 | #include "debugger.h" |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 69 | #include "dex/class_accessor-inl.h" |
David Sehr | b2ec9f5 | 2018-02-21 13:20:31 -0800 | [diff] [blame] | 70 | #include "dex/descriptors_names.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 71 | #include "dex/dex_file-inl.h" |
| 72 | #include "dex/dex_file_exception_helpers.h" |
| 73 | #include "dex/dex_file_loader.h" |
Andreas Gampe | ad1aa63 | 2019-01-02 10:30:54 -0800 | [diff] [blame] | 74 | #include "dex/signature-inl.h" |
David Sehr | 0225f8e | 2018-01-31 08:52:24 +0000 | [diff] [blame] | 75 | #include "dex/utf.h" |
Vladimir Marko | 5115a4d | 2019-10-17 14:56:47 +0100 | [diff] [blame] | 76 | #include "entrypoints/entrypoint_utils-inl.h" |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 77 | #include "entrypoints/runtime_asm_entrypoints.h" |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 78 | #include "experimental_flags.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 79 | #include "gc/accounting/card_table-inl.h" |
Mathieu Chartier | 03c1dd9 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 80 | #include "gc/accounting/heap_bitmap-inl.h" |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 81 | #include "gc/accounting/space_bitmap-inl.h" |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 82 | #include "gc/heap-visit-objects-inl.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 83 | #include "gc/heap.h" |
Mathieu Chartier | 1b1e31f | 2016-05-19 10:13:04 -0700 | [diff] [blame] | 84 | #include "gc/scoped_gc_critical_section.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 85 | #include "gc/space/image_space.h" |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 86 | #include "gc/space/space-inl.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 87 | #include "gc_root-inl.h" |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 88 | #include "handle_scope-inl.h" |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 89 | #include "hidden_api.h" |
Mathieu Chartier | 4a26f17 | 2016-01-26 14:26:18 -0800 | [diff] [blame] | 90 | #include "image-inl.h" |
Andreas Gampe | 75a7db6 | 2016-09-26 12:04:26 -0700 | [diff] [blame] | 91 | #include "imt_conflict_table.h" |
| 92 | #include "imtable-inl.h" |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 93 | #include "intern_table-inl.h" |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 94 | #include "interpreter/interpreter.h" |
Nicolas Geoffray | 0315efa | 2020-06-26 11:42:39 +0100 | [diff] [blame] | 95 | #include "interpreter/mterp/nterp.h" |
David Srbecky | fb3de3d | 2018-01-29 16:11:49 +0000 | [diff] [blame] | 96 | #include "jit/debugger_interface.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 97 | #include "jit/jit.h" |
| 98 | #include "jit/jit_code_cache.h" |
Vladimir Marko | a3ad0cd | 2018-05-04 10:06:38 +0100 | [diff] [blame] | 99 | #include "jni/java_vm_ext.h" |
| 100 | #include "jni/jni_internal.h" |
Lokesh Gidra | 606bd94 | 2022-05-23 19:00:09 +0000 | [diff] [blame] | 101 | #include "linear_alloc-inl.h" |
Andreas Gampe | 8e0f043 | 2018-10-24 13:38:03 -0700 | [diff] [blame] | 102 | #include "mirror/array-alloc-inl.h" |
| 103 | #include "mirror/array-inl.h" |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 104 | #include "mirror/call_site.h" |
Andreas Gampe | 70f5fd0 | 2018-10-24 19:58:37 -0700 | [diff] [blame] | 105 | #include "mirror/class-alloc-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 106 | #include "mirror/class-inl.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 107 | #include "mirror/class.h" |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 108 | #include "mirror/class_ext.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 109 | #include "mirror/class_loader.h" |
Ian Rogers | 39ebcb8 | 2013-05-30 16:57:23 -0700 | [diff] [blame] | 110 | #include "mirror/dex_cache-inl.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 111 | #include "mirror/dex_cache.h" |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 112 | #include "mirror/emulated_stack_frame.h" |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 113 | #include "mirror/field.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 114 | #include "mirror/iftable-inl.h" |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 115 | #include "mirror/method.h" |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 116 | #include "mirror/method_handle_impl.h" |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 117 | #include "mirror/method_handles_lookup.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 118 | #include "mirror/method_type.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 119 | #include "mirror/object-inl.h" |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 120 | #include "mirror/object-refvisitor-inl.h" |
Alex Light | a9bbc08 | 2019-11-14 14:51:41 -0800 | [diff] [blame] | 121 | #include "mirror/object.h" |
Andreas Gampe | 52ecb65 | 2018-10-24 15:18:21 -0700 | [diff] [blame] | 122 | #include "mirror/object_array-alloc-inl.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 123 | #include "mirror/object_array-inl.h" |
Alex Light | 133987d | 2020-03-26 19:22:12 +0000 | [diff] [blame] | 124 | #include "mirror/object_array.h" |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 125 | #include "mirror/object_reference.h" |
| 126 | #include "mirror/object_reference-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 127 | #include "mirror/proxy.h" |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 128 | #include "mirror/reference-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 129 | #include "mirror/stack_trace_element.h" |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 130 | #include "mirror/string-inl.h" |
Andreas Gampe | 501c3b0 | 2019-04-17 21:54:27 +0000 | [diff] [blame] | 131 | #include "mirror/throwable.h" |
Orion Hodson | 005ac51 | 2017-10-24 15:43:43 +0100 | [diff] [blame] | 132 | #include "mirror/var_handle.h" |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 133 | #include "native/dalvik_system_DexFile.h" |
Andreas Gampe | 373a9b5 | 2017-10-18 09:01:57 -0700 | [diff] [blame] | 134 | #include "nativehelper/scoped_local_ref.h" |
Nicolas Geoffray | c39af94 | 2021-01-25 08:43:57 +0000 | [diff] [blame] | 135 | #include "nterp_helpers.h" |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 136 | #include "oat.h" |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 137 | #include "oat_file-inl.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 138 | #include "oat_file.h" |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 139 | #include "oat_file_assistant.h" |
| 140 | #include "oat_file_manager.h" |
| 141 | #include "object_lock.h" |
David Sehr | 82d046e | 2018-04-23 08:14:19 -0700 | [diff] [blame] | 142 | #include "profile/profile_compilation_info.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 143 | #include "runtime.h" |
Andreas Gampe | ac30fa2 | 2017-01-18 21:02:36 -0800 | [diff] [blame] | 144 | #include "runtime_callbacks.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 145 | #include "scoped_thread_state_change-inl.h" |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 146 | #include "thread-inl.h" |
Alex Light | 133987d | 2020-03-26 19:22:12 +0000 | [diff] [blame] | 147 | #include "thread.h" |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 148 | #include "thread_list.h" |
Mathieu Chartier | 7778b88 | 2015-10-05 16:41:10 -0700 | [diff] [blame] | 149 | #include "trace.h" |
Vladimir Marko | b68bb7a | 2020-03-17 10:55:25 +0000 | [diff] [blame] | 150 | #include "transaction.h" |
Nicolas Geoffray | 6df4511 | 2021-02-07 21:51:58 +0000 | [diff] [blame] | 151 | #include "vdex_file.h" |
Andreas Gampe | a43ba3d | 2019-03-13 15:49:20 -0700 | [diff] [blame] | 152 | #include "verifier/class_verifier.h" |
Nicolas Geoffray | 8078996 | 2021-04-30 16:50:39 +0100 | [diff] [blame] | 153 | #include "verifier/verifier_deps.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 154 | #include "well_known_classes.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 155 | |
| 156 | namespace art { |
| 157 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 158 | using android::base::StringPrintf; |
| 159 | |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 160 | static constexpr bool kCheckImageObjects = kIsDebugBuild; |
Mathieu Chartier | 8790c7f | 2016-03-31 15:05:45 -0700 | [diff] [blame] | 161 | static constexpr bool kVerifyArtMethodDeclaringClasses = kIsDebugBuild; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 162 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 163 | static void ThrowNoClassDefFoundError(const char* fmt, ...) |
| 164 | __attribute__((__format__(__printf__, 1, 2))) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 165 | REQUIRES_SHARED(Locks::mutator_lock_); |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 166 | static void ThrowNoClassDefFoundError(const char* fmt, ...) { |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 167 | va_list args; |
| 168 | va_start(args, fmt); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 169 | Thread* self = Thread::Current(); |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 170 | self->ThrowNewExceptionV("Ljava/lang/NoClassDefFoundError;", fmt, args); |
Ian Rogers | cab0101 | 2012-01-10 17:35:46 -0800 | [diff] [blame] | 171 | va_end(args); |
| 172 | } |
| 173 | |
Nicolas Geoffray | 4dc6589 | 2021-07-05 17:43:35 +0100 | [diff] [blame] | 174 | static ObjPtr<mirror::Object> GetErroneousStateError(ObjPtr<mirror::Class> c) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 175 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 176 | ObjPtr<mirror::ClassExt> ext(c->GetExtData()); |
| 177 | if (ext == nullptr) { |
| 178 | return nullptr; |
| 179 | } else { |
Nicolas Geoffray | 4dc6589 | 2021-07-05 17:43:35 +0100 | [diff] [blame] | 180 | return ext->GetErroneousStateError(); |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
Nicolas Geoffray | 4dc6589 | 2021-07-05 17:43:35 +0100 | [diff] [blame] | 184 | static bool IsVerifyError(ObjPtr<mirror::Object> obj) |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 185 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Nicolas Geoffray | 4dc6589 | 2021-07-05 17:43:35 +0100 | [diff] [blame] | 186 | // This is slow, but we only use it for rethrowing an error, and for DCHECK. |
| 187 | return obj->GetClass()->DescriptorEquals("Ljava/lang/VerifyError;"); |
| 188 | } |
| 189 | |
| 190 | // Helper for ThrowEarlierClassFailure. Throws the stored error. |
| 191 | static void HandleEarlierErroneousStateError(Thread* self, |
| 192 | ClassLinker* class_linker, |
| 193 | ObjPtr<mirror::Class> c) |
| 194 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 195 | ObjPtr<mirror::Object> obj = GetErroneousStateError(c); |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 196 | DCHECK(obj != nullptr); |
| 197 | self->AssertNoPendingException(); |
Nicolas Geoffray | 4dc6589 | 2021-07-05 17:43:35 +0100 | [diff] [blame] | 198 | DCHECK(!obj->IsClass()); |
| 199 | ObjPtr<mirror::Class> throwable_class = GetClassRoot<mirror::Throwable>(class_linker); |
| 200 | ObjPtr<mirror::Class> error_class = obj->GetClass(); |
| 201 | CHECK(throwable_class->IsAssignableFrom(error_class)); |
| 202 | self->SetException(obj->AsThrowable()); |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 203 | self->AssertPendingException(); |
| 204 | } |
| 205 | |
Nicolas Geoffray | 66934ef | 2021-07-07 14:56:23 +0100 | [diff] [blame] | 206 | static void UpdateClassAfterVerification(Handle<mirror::Class> klass, |
| 207 | PointerSize pointer_size, |
| 208 | verifier::FailureKind failure_kind) |
Andreas Gampe | 5b20b35 | 2018-10-11 19:03:20 -0700 | [diff] [blame] | 209 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Nicolas Geoffray | 0039182 | 2019-12-10 10:17:23 +0000 | [diff] [blame] | 210 | Runtime* runtime = Runtime::Current(); |
| 211 | ClassLinker* class_linker = runtime->GetClassLinker(); |
Nicolas Geoffray | 2990b88 | 2021-11-30 15:48:49 +0000 | [diff] [blame] | 212 | if (klass->IsVerified() && (failure_kind == verifier::FailureKind::kNoFailure)) { |
Andreas Gampe | 5b20b35 | 2018-10-11 19:03:20 -0700 | [diff] [blame] | 213 | klass->SetSkipAccessChecksFlagOnAllMethods(pointer_size); |
Nicolas Geoffray | 66934ef | 2021-07-07 14:56:23 +0100 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | // Now that the class has passed verification, try to set nterp entrypoints |
| 217 | // to methods that currently use the switch interpreter. |
| 218 | if (interpreter::CanRuntimeUseNterp()) { |
| 219 | for (ArtMethod& m : klass->GetMethods(pointer_size)) { |
Nicolas Geoffray | c8a694d | 2022-01-17 17:12:38 +0000 | [diff] [blame] | 220 | if (class_linker->IsQuickToInterpreterBridge(m.GetEntryPointFromQuickCompiledCode())) { |
| 221 | runtime->GetInstrumentation()->InitializeMethodsCode(&m, /*aot_code=*/nullptr); |
| 222 | } |
Nicolas Geoffray | 0039182 | 2019-12-10 10:17:23 +0000 | [diff] [blame] | 223 | } |
Andreas Gampe | 5b20b35 | 2018-10-11 19:03:20 -0700 | [diff] [blame] | 224 | } |
| 225 | } |
| 226 | |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 227 | // Callback responsible for making a batch of classes visibly initialized |
| 228 | // after all threads have called it from a checkpoint, ensuring visibility. |
| 229 | class ClassLinker::VisiblyInitializedCallback final |
| 230 | : public Closure, public IntrusiveForwardListNode<VisiblyInitializedCallback> { |
| 231 | public: |
| 232 | explicit VisiblyInitializedCallback(ClassLinker* class_linker) |
| 233 | : class_linker_(class_linker), |
| 234 | num_classes_(0u), |
| 235 | thread_visibility_counter_(0), |
| 236 | barriers_() { |
| 237 | std::fill_n(classes_, kMaxClasses, nullptr); |
| 238 | } |
| 239 | |
| 240 | bool IsEmpty() const { |
| 241 | DCHECK_LE(num_classes_, kMaxClasses); |
| 242 | return num_classes_ == 0u; |
| 243 | } |
| 244 | |
| 245 | bool IsFull() const { |
| 246 | DCHECK_LE(num_classes_, kMaxClasses); |
| 247 | return num_classes_ == kMaxClasses; |
| 248 | } |
| 249 | |
| 250 | void AddClass(Thread* self, ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 251 | DCHECK_EQ(klass->GetStatus(), ClassStatus::kInitialized); |
| 252 | DCHECK(!IsFull()); |
| 253 | classes_[num_classes_] = self->GetJniEnv()->GetVm()->AddWeakGlobalRef(self, klass); |
| 254 | ++num_classes_; |
| 255 | } |
| 256 | |
| 257 | void AddBarrier(Barrier* barrier) { |
| 258 | barriers_.push_front(barrier); |
| 259 | } |
| 260 | |
| 261 | std::forward_list<Barrier*> GetAndClearBarriers() { |
| 262 | std::forward_list<Barrier*> result; |
| 263 | result.swap(barriers_); |
| 264 | result.reverse(); // Return barriers in insertion order. |
| 265 | return result; |
| 266 | } |
| 267 | |
| 268 | void MakeVisible(Thread* self) { |
| 269 | DCHECK_EQ(thread_visibility_counter_.load(std::memory_order_relaxed), 0); |
| 270 | size_t count = Runtime::Current()->GetThreadList()->RunCheckpoint(this); |
| 271 | AdjustThreadVisibilityCounter(self, count); |
| 272 | } |
| 273 | |
| 274 | void Run(Thread* self) override { |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 275 | AdjustThreadVisibilityCounter(self, -1); |
| 276 | } |
| 277 | |
| 278 | private: |
| 279 | void AdjustThreadVisibilityCounter(Thread* self, ssize_t adjustment) { |
| 280 | ssize_t old = thread_visibility_counter_.fetch_add(adjustment, std::memory_order_relaxed); |
| 281 | if (old + adjustment == 0) { |
| 282 | // All threads passed the checkpoint. Mark classes as visibly initialized. |
| 283 | { |
| 284 | ScopedObjectAccess soa(self); |
| 285 | StackHandleScope<1u> hs(self); |
| 286 | MutableHandle<mirror::Class> klass = hs.NewHandle<mirror::Class>(nullptr); |
| 287 | JavaVMExt* vm = self->GetJniEnv()->GetVm(); |
| 288 | for (size_t i = 0, num = num_classes_; i != num; ++i) { |
| 289 | klass.Assign(ObjPtr<mirror::Class>::DownCast(self->DecodeJObject(classes_[i]))); |
| 290 | vm->DeleteWeakGlobalRef(self, classes_[i]); |
| 291 | if (klass != nullptr) { |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 292 | mirror::Class::SetStatus(klass, ClassStatus::kVisiblyInitialized, self); |
Vladimir Marko | 86c8752 | 2020-05-11 16:55:55 +0100 | [diff] [blame] | 293 | class_linker_->FixupStaticTrampolines(self, klass.Get()); |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | num_classes_ = 0u; |
| 297 | } |
| 298 | class_linker_->VisiblyInitializedCallbackDone(self, this); |
| 299 | } |
| 300 | } |
| 301 | |
Vladimir Marko | 0474095 | 2023-01-04 14:06:38 +0000 | [diff] [blame] | 302 | // Making classes initialized in bigger batches helps with app startup for |
| 303 | // apps that initialize a lot of classes by running fewer checkpoints. |
| 304 | // (On the other hand, bigger batches make class initialization checks more |
| 305 | // likely to take a slow path but that is mitigated by making partially |
| 306 | // filled buffers visibly initialized if we take the slow path many times. |
| 307 | // See `Thread::kMakeVisiblyInitializedCounterTriggerCount`.) |
| 308 | static constexpr size_t kMaxClasses = 48; |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 309 | |
| 310 | ClassLinker* const class_linker_; |
| 311 | size_t num_classes_; |
| 312 | jweak classes_[kMaxClasses]; |
| 313 | |
| 314 | // The thread visibility counter starts at 0 and it is incremented by the number of |
| 315 | // threads that need to run this callback (by the thread that request the callback |
| 316 | // to be run) and decremented once for each `Run()` execution. When it reaches 0, |
| 317 | // whether after the increment or after a decrement, we know that `Run()` was executed |
| 318 | // for all threads and therefore we can mark the classes as visibly initialized. |
| 319 | std::atomic<ssize_t> thread_visibility_counter_; |
| 320 | |
| 321 | // List of barries to `Pass()` for threads that wait for the callback to complete. |
| 322 | std::forward_list<Barrier*> barriers_; |
| 323 | }; |
| 324 | |
Hans Boehm | 0f82bae | 2022-10-17 17:21:45 -0700 | [diff] [blame] | 325 | void ClassLinker::MakeInitializedClassesVisiblyInitialized(Thread* self, bool wait) { |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 326 | if (kRuntimeISA == InstructionSet::kX86 || kRuntimeISA == InstructionSet::kX86_64) { |
| 327 | return; // Nothing to do. Thanks to the x86 memory model classes skip the initialized status. |
| 328 | } |
| 329 | std::optional<Barrier> maybe_barrier; // Avoid constructing the Barrier for `wait == false`. |
| 330 | if (wait) { |
Hans Boehm | 0f82bae | 2022-10-17 17:21:45 -0700 | [diff] [blame] | 331 | Locks::mutator_lock_->AssertNotHeld(self); |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 332 | maybe_barrier.emplace(0); |
| 333 | } |
| 334 | int wait_count = 0; |
| 335 | VisiblyInitializedCallback* callback = nullptr; |
| 336 | { |
| 337 | MutexLock lock(self, visibly_initialized_callback_lock_); |
| 338 | if (visibly_initialized_callback_ != nullptr && !visibly_initialized_callback_->IsEmpty()) { |
| 339 | callback = visibly_initialized_callback_.release(); |
| 340 | running_visibly_initialized_callbacks_.push_front(*callback); |
| 341 | } |
| 342 | if (wait) { |
| 343 | DCHECK(maybe_barrier.has_value()); |
| 344 | Barrier* barrier = std::addressof(*maybe_barrier); |
| 345 | for (VisiblyInitializedCallback& cb : running_visibly_initialized_callbacks_) { |
| 346 | cb.AddBarrier(barrier); |
| 347 | ++wait_count; |
| 348 | } |
| 349 | } |
| 350 | } |
| 351 | if (callback != nullptr) { |
| 352 | callback->MakeVisible(self); |
| 353 | } |
| 354 | if (wait_count != 0) { |
| 355 | DCHECK(maybe_barrier.has_value()); |
| 356 | maybe_barrier->Increment(self, wait_count); |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | void ClassLinker::VisiblyInitializedCallbackDone(Thread* self, |
| 361 | VisiblyInitializedCallback* callback) { |
| 362 | MutexLock lock(self, visibly_initialized_callback_lock_); |
| 363 | // Pass the barriers if requested. |
| 364 | for (Barrier* barrier : callback->GetAndClearBarriers()) { |
| 365 | barrier->Pass(self); |
| 366 | } |
| 367 | // Remove the callback from the list of running callbacks. |
| 368 | auto before = running_visibly_initialized_callbacks_.before_begin(); |
| 369 | auto it = running_visibly_initialized_callbacks_.begin(); |
| 370 | DCHECK(it != running_visibly_initialized_callbacks_.end()); |
| 371 | while (std::addressof(*it) != callback) { |
| 372 | before = it; |
| 373 | ++it; |
| 374 | DCHECK(it != running_visibly_initialized_callbacks_.end()); |
| 375 | } |
| 376 | running_visibly_initialized_callbacks_.erase_after(before); |
| 377 | // Reuse or destroy the callback object. |
| 378 | if (visibly_initialized_callback_ == nullptr) { |
| 379 | visibly_initialized_callback_.reset(callback); |
| 380 | } else { |
| 381 | delete callback; |
| 382 | } |
| 383 | } |
| 384 | |
Alex Light | fb11957 | 2019-09-18 15:04:53 -0700 | [diff] [blame] | 385 | void ClassLinker::ForceClassInitialized(Thread* self, Handle<mirror::Class> klass) { |
| 386 | ClassLinker::VisiblyInitializedCallback* cb = MarkClassInitialized(self, klass); |
| 387 | if (cb != nullptr) { |
| 388 | cb->MakeVisible(self); |
| 389 | } |
| 390 | ScopedThreadSuspension sts(self, ThreadState::kSuspended); |
| 391 | MakeInitializedClassesVisiblyInitialized(self, /*wait=*/true); |
| 392 | } |
| 393 | |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 394 | ClassLinker::VisiblyInitializedCallback* ClassLinker::MarkClassInitialized( |
| 395 | Thread* self, Handle<mirror::Class> klass) { |
| 396 | if (kRuntimeISA == InstructionSet::kX86 || kRuntimeISA == InstructionSet::kX86_64) { |
| 397 | // Thanks to the x86 memory model, we do not need any memory fences and |
| 398 | // we can immediately mark the class as visibly initialized. |
| 399 | mirror::Class::SetStatus(klass, ClassStatus::kVisiblyInitialized, self); |
Vladimir Marko | 86c8752 | 2020-05-11 16:55:55 +0100 | [diff] [blame] | 400 | FixupStaticTrampolines(self, klass.Get()); |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 401 | return nullptr; |
| 402 | } |
| 403 | if (Runtime::Current()->IsActiveTransaction()) { |
| 404 | // Transactions are single-threaded, so we can mark the class as visibly intialized. |
| 405 | // (Otherwise we'd need to track the callback's entry in the transaction for rollback.) |
| 406 | mirror::Class::SetStatus(klass, ClassStatus::kVisiblyInitialized, self); |
Vladimir Marko | 86c8752 | 2020-05-11 16:55:55 +0100 | [diff] [blame] | 407 | FixupStaticTrampolines(self, klass.Get()); |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 408 | return nullptr; |
| 409 | } |
| 410 | mirror::Class::SetStatus(klass, ClassStatus::kInitialized, self); |
| 411 | MutexLock lock(self, visibly_initialized_callback_lock_); |
| 412 | if (visibly_initialized_callback_ == nullptr) { |
| 413 | visibly_initialized_callback_.reset(new VisiblyInitializedCallback(this)); |
| 414 | } |
| 415 | DCHECK(!visibly_initialized_callback_->IsFull()); |
| 416 | visibly_initialized_callback_->AddClass(self, klass.Get()); |
| 417 | |
| 418 | if (visibly_initialized_callback_->IsFull()) { |
| 419 | VisiblyInitializedCallback* callback = visibly_initialized_callback_.release(); |
| 420 | running_visibly_initialized_callbacks_.push_front(*callback); |
| 421 | return callback; |
| 422 | } else { |
| 423 | return nullptr; |
| 424 | } |
| 425 | } |
| 426 | |
Vladimir Marko | 86c8752 | 2020-05-11 16:55:55 +0100 | [diff] [blame] | 427 | const void* ClassLinker::RegisterNative( |
| 428 | Thread* self, ArtMethod* method, const void* native_method) { |
| 429 | CHECK(method->IsNative()) << method->PrettyMethod(); |
| 430 | CHECK(native_method != nullptr) << method->PrettyMethod(); |
| 431 | void* new_native_method = nullptr; |
| 432 | Runtime* runtime = Runtime::Current(); |
| 433 | runtime->GetRuntimeCallbacks()->RegisterNativeMethod(method, |
| 434 | native_method, |
| 435 | /*out*/&new_native_method); |
| 436 | if (method->IsCriticalNative()) { |
| 437 | MutexLock lock(self, critical_native_code_with_clinit_check_lock_); |
| 438 | // Remove old registered method if any. |
| 439 | auto it = critical_native_code_with_clinit_check_.find(method); |
| 440 | if (it != critical_native_code_with_clinit_check_.end()) { |
| 441 | critical_native_code_with_clinit_check_.erase(it); |
| 442 | } |
| 443 | // To ensure correct memory visibility, we need the class to be visibly |
| 444 | // initialized before we can set the JNI entrypoint. |
| 445 | if (method->GetDeclaringClass()->IsVisiblyInitialized()) { |
| 446 | method->SetEntryPointFromJni(new_native_method); |
| 447 | } else { |
| 448 | critical_native_code_with_clinit_check_.emplace(method, new_native_method); |
| 449 | } |
| 450 | } else { |
| 451 | method->SetEntryPointFromJni(new_native_method); |
| 452 | } |
| 453 | return new_native_method; |
| 454 | } |
| 455 | |
| 456 | void ClassLinker::UnregisterNative(Thread* self, ArtMethod* method) { |
| 457 | CHECK(method->IsNative()) << method->PrettyMethod(); |
| 458 | // Restore stub to lookup native pointer via dlsym. |
| 459 | if (method->IsCriticalNative()) { |
| 460 | MutexLock lock(self, critical_native_code_with_clinit_check_lock_); |
| 461 | auto it = critical_native_code_with_clinit_check_.find(method); |
| 462 | if (it != critical_native_code_with_clinit_check_.end()) { |
| 463 | critical_native_code_with_clinit_check_.erase(it); |
| 464 | } |
| 465 | method->SetEntryPointFromJni(GetJniDlsymLookupCriticalStub()); |
| 466 | } else { |
| 467 | method->SetEntryPointFromJni(GetJniDlsymLookupStub()); |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | const void* ClassLinker::GetRegisteredNative(Thread* self, ArtMethod* method) { |
| 472 | if (method->IsCriticalNative()) { |
| 473 | MutexLock lock(self, critical_native_code_with_clinit_check_lock_); |
| 474 | auto it = critical_native_code_with_clinit_check_.find(method); |
| 475 | if (it != critical_native_code_with_clinit_check_.end()) { |
| 476 | return it->second; |
| 477 | } |
| 478 | const void* native_code = method->GetEntryPointFromJni(); |
| 479 | return IsJniDlsymLookupCriticalStub(native_code) ? nullptr : native_code; |
| 480 | } else { |
| 481 | const void* native_code = method->GetEntryPointFromJni(); |
| 482 | return IsJniDlsymLookupStub(native_code) ? nullptr : native_code; |
| 483 | } |
| 484 | } |
| 485 | |
Andreas Gampe | 7b3063b | 2019-01-07 14:12:52 -0800 | [diff] [blame] | 486 | void ClassLinker::ThrowEarlierClassFailure(ObjPtr<mirror::Class> c, |
| 487 | bool wrap_in_no_class_def, |
| 488 | bool log) { |
Elliott Hughes | 5c59994 | 2012-06-13 16:45:05 -0700 | [diff] [blame] | 489 | // The class failed to initialize on a previous attempt, so we want to throw |
| 490 | // a NoClassDefFoundError (v2 2.17.5). The exception to this rule is if we |
| 491 | // failed in verification, in which case v2 5.4.1 says we need to re-throw |
| 492 | // the previous error. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 493 | Runtime* const runtime = Runtime::Current(); |
| 494 | if (!runtime->IsAotCompiler()) { // Give info if this occurs at runtime. |
Andreas Gampe | 3d6b470 | 2015-09-21 08:35:52 -0700 | [diff] [blame] | 495 | std::string extra; |
Nicolas Geoffray | 4dc6589 | 2021-07-05 17:43:35 +0100 | [diff] [blame] | 496 | ObjPtr<mirror::Object> verify_error = GetErroneousStateError(c); |
Vladimir Marko | bb206de | 2019-03-28 10:30:32 +0000 | [diff] [blame] | 497 | if (verify_error != nullptr) { |
Nicolas Geoffray | 4dc6589 | 2021-07-05 17:43:35 +0100 | [diff] [blame] | 498 | DCHECK(!verify_error->IsClass()); |
| 499 | extra = verify_error->AsThrowable()->Dump(); |
Andreas Gampe | 3d6b470 | 2015-09-21 08:35:52 -0700 | [diff] [blame] | 500 | } |
Andreas Gampe | 7b3063b | 2019-01-07 14:12:52 -0800 | [diff] [blame] | 501 | if (log) { |
| 502 | LOG(INFO) << "Rejecting re-init on previously-failed class " << c->PrettyClass() |
| 503 | << ": " << extra; |
| 504 | } |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 505 | } |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 506 | |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 507 | CHECK(c->IsErroneous()) << c->PrettyClass() << " " << c->GetStatus(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 508 | Thread* self = Thread::Current(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 509 | if (runtime->IsAotCompiler()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 510 | // At compile time, accurate errors and NCDFE are disabled to speed compilation. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 511 | ObjPtr<mirror::Throwable> pre_allocated = runtime->GetPreAllocatedNoClassDefFoundError(); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 512 | self->SetException(pre_allocated); |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 513 | } else { |
Nicolas Geoffray | 4dc6589 | 2021-07-05 17:43:35 +0100 | [diff] [blame] | 514 | ObjPtr<mirror::Object> erroneous_state_error = GetErroneousStateError(c); |
| 515 | if (erroneous_state_error != nullptr) { |
Andreas Gampe | cb08695 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 516 | // Rethrow stored error. |
Nicolas Geoffray | 4dc6589 | 2021-07-05 17:43:35 +0100 | [diff] [blame] | 517 | HandleEarlierErroneousStateError(self, this, c); |
Andreas Gampe | cb08695 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 518 | } |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 519 | // TODO This might be wrong if we hit an OOME while allocating the ClassExt. In that case we |
| 520 | // might have meant to go down the earlier if statement with the original error but it got |
| 521 | // swallowed by the OOM so we end up here. |
Nicolas Geoffray | 4dc6589 | 2021-07-05 17:43:35 +0100 | [diff] [blame] | 522 | if (erroneous_state_error == nullptr || |
| 523 | (wrap_in_no_class_def && !IsVerifyError(erroneous_state_error))) { |
Andreas Gampe | cb08695 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 524 | // If there isn't a recorded earlier error, or this is a repeat throw from initialization, |
| 525 | // the top-level exception must be a NoClassDefFoundError. The potentially already pending |
| 526 | // exception will be a cause. |
| 527 | self->ThrowNewWrappedException("Ljava/lang/NoClassDefFoundError;", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 528 | c->PrettyDescriptor().c_str()); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 529 | } |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 530 | } |
| 531 | } |
| 532 | |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 533 | static void VlogClassInitializationFailure(Handle<mirror::Class> klass) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 534 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 535 | if (VLOG_IS_ON(class_linker)) { |
| 536 | std::string temp; |
| 537 | LOG(INFO) << "Failed to initialize class " << klass->GetDescriptor(&temp) << " from " |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 538 | << klass->GetLocation() << "\n" << Thread::Current()->GetException()->Dump(); |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 539 | } |
| 540 | } |
| 541 | |
| 542 | static void WrapExceptionInInitializer(Handle<mirror::Class> klass) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 543 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 544 | Thread* self = Thread::Current(); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 545 | |
Vladimir Marko | 7b97aeb | 2022-12-01 16:17:47 +0000 | [diff] [blame] | 546 | ObjPtr<mirror::Throwable> cause = self->GetException(); |
| 547 | CHECK(cause != nullptr); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 548 | |
David Srbecky | 346fd96 | 2020-07-27 16:51:00 +0100 | [diff] [blame] | 549 | // Boot classpath classes should not fail initialization. This is a consistency debug check. |
| 550 | // 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] | 551 | if (klass->GetClassLoader() == nullptr && !Runtime::Current()->IsAotCompiler()) { |
Andreas Gampe | 22f71d2 | 2016-11-21 10:10:08 -0800 | [diff] [blame] | 552 | std::string tmp; |
Alex Light | 5047d9f | 2018-03-09 15:44:31 -0800 | [diff] [blame] | 553 | // We want to LOG(FATAL) on debug builds since this really shouldn't be happening but we need to |
| 554 | // make sure to only do it if we don't have AsyncExceptions being thrown around since those |
| 555 | // could have caused the error. |
| 556 | bool known_impossible = kIsDebugBuild && !Runtime::Current()->AreAsyncExceptionsThrown(); |
| 557 | LOG(known_impossible ? FATAL : WARNING) << klass->GetDescriptor(&tmp) |
| 558 | << " failed initialization: " |
| 559 | << self->GetException()->Dump(); |
Andreas Gampe | 22f71d2 | 2016-11-21 10:10:08 -0800 | [diff] [blame] | 560 | } |
| 561 | |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 562 | // We only wrap non-Error exceptions; an Error can just be used as-is. |
Vladimir Marko | 7b97aeb | 2022-12-01 16:17:47 +0000 | [diff] [blame] | 563 | if (!cause->IsError()) { |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 564 | self->ThrowNewWrappedException("Ljava/lang/ExceptionInInitializerError;", nullptr); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 565 | } |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 566 | VlogClassInitializationFailure(klass); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 567 | } |
| 568 | |
Andreas Gampe | 87658f3 | 2019-04-18 18:39:02 +0000 | [diff] [blame] | 569 | ClassLinker::ClassLinker(InternTable* intern_table, bool fast_class_not_found_exceptions) |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 570 | : boot_class_table_(new ClassTable()), |
| 571 | failed_dex_cache_class_lookups_(0), |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 572 | class_roots_(nullptr), |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 573 | find_array_class_cache_next_victim_(0), |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 574 | init_done_(false), |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 575 | log_new_roots_(false), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 576 | intern_table_(intern_table), |
Andreas Gampe | 87658f3 | 2019-04-18 18:39:02 +0000 | [diff] [blame] | 577 | fast_class_not_found_exceptions_(fast_class_not_found_exceptions), |
Vladimir Marko | 7dac864 | 2019-11-06 17:09:30 +0000 | [diff] [blame] | 578 | jni_dlsym_lookup_trampoline_(nullptr), |
Vladimir Marko | fa458ac | 2020-02-12 14:08:07 +0000 | [diff] [blame] | 579 | jni_dlsym_lookup_critical_trampoline_(nullptr), |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 580 | quick_resolution_trampoline_(nullptr), |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 581 | quick_imt_conflict_trampoline_(nullptr), |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 582 | quick_generic_jni_trampoline_(nullptr), |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 583 | quick_to_interpreter_bridge_trampoline_(nullptr), |
Nicolas Geoffray | c39af94 | 2021-01-25 08:43:57 +0000 | [diff] [blame] | 584 | nterp_trampoline_(nullptr), |
Andreas Gampe | c1ac9ee | 2017-07-24 22:35:49 -0700 | [diff] [blame] | 585 | image_pointer_size_(kRuntimePointerSize), |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 586 | visibly_initialized_callback_lock_("visibly initialized callback lock"), |
| 587 | visibly_initialized_callback_(nullptr), |
Vladimir Marko | 86c8752 | 2020-05-11 16:55:55 +0100 | [diff] [blame] | 588 | critical_native_code_with_clinit_check_lock_("critical native code with clinit check lock"), |
| 589 | critical_native_code_with_clinit_check_(), |
Andreas Gampe | 7dface3 | 2017-07-25 21:32:59 -0700 | [diff] [blame] | 590 | cha_(Runtime::Current()->IsAotCompiler() ? nullptr : new ClassHierarchyAnalysis()) { |
| 591 | // For CHA disabled during Aot, see b/34193647. |
| 592 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 593 | CHECK(intern_table_ != nullptr); |
Andreas Gampe | 8ac7595 | 2015-06-02 21:01:45 -0700 | [diff] [blame] | 594 | static_assert(kFindArrayCacheSize == arraysize(find_array_class_cache_), |
| 595 | "Array cache size wrong."); |
| 596 | std::fill_n(find_array_class_cache_, kFindArrayCacheSize, GcRoot<mirror::Class>(nullptr)); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 597 | } |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 598 | |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 599 | void ClassLinker::CheckSystemClass(Thread* self, Handle<mirror::Class> c1, const char* descriptor) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 600 | ObjPtr<mirror::Class> c2 = FindSystemClass(self, descriptor); |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 601 | if (c2 == nullptr) { |
| 602 | LOG(FATAL) << "Could not find class " << descriptor; |
| 603 | UNREACHABLE(); |
| 604 | } |
| 605 | if (c1.Get() != c2) { |
| 606 | std::ostringstream os1, os2; |
| 607 | c1->DumpClass(os1, mirror::Class::kDumpClassFullDetail); |
| 608 | c2->DumpClass(os2, mirror::Class::kDumpClassFullDetail); |
| 609 | LOG(FATAL) << "InitWithoutImage: Class mismatch for " << descriptor |
| 610 | << ". This is most likely the result of a broken build. Make sure that " |
| 611 | << "libcore and art projects match.\n\n" |
| 612 | << os1.str() << "\n\n" << os2.str(); |
| 613 | UNREACHABLE(); |
| 614 | } |
| 615 | } |
| 616 | |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 617 | ObjPtr<mirror::IfTable> AllocIfTable(Thread* self, |
| 618 | size_t ifcount, |
| 619 | ObjPtr<mirror::Class> iftable_class) |
| 620 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 621 | DCHECK(iftable_class->IsArrayClass()); |
| 622 | DCHECK(iftable_class->GetComponentType()->IsObjectClass()); |
| 623 | return ObjPtr<mirror::IfTable>::DownCast(ObjPtr<mirror::ObjectArray<mirror::Object>>( |
| 624 | mirror::IfTable::Alloc(self, iftable_class, ifcount * mirror::IfTable::kMax))); |
| 625 | } |
| 626 | |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 627 | bool ClassLinker::InitWithoutImage(std::vector<std::unique_ptr<const DexFile>> boot_class_path, |
| 628 | std::string* error_msg) { |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 629 | VLOG(startup) << "ClassLinker::Init"; |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 630 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 631 | Thread* const self = Thread::Current(); |
| 632 | Runtime* const runtime = Runtime::Current(); |
| 633 | gc::Heap* const heap = runtime->GetHeap(); |
| 634 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 635 | CHECK(!heap->HasBootImageSpace()) << "Runtime has image. We should use it."; |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 636 | CHECK(!init_done_); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 637 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 638 | // Use the pointer size from the runtime since we are probably creating the image. |
| 639 | image_pointer_size_ = InstructionSetPointerSize(runtime->GetInstructionSet()); |
| 640 | |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 641 | // java_lang_Class comes first, it's needed for AllocClass |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 642 | // 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] | 643 | heap->IncrementDisableMovingGC(self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 644 | StackHandleScope<64> hs(self); // 64 is picked arbitrarily. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 645 | auto class_class_size = mirror::Class::ClassClassSize(image_pointer_size_); |
Mathieu Chartier | d7a7f2f | 2018-09-07 11:57:18 -0700 | [diff] [blame] | 646 | // Allocate the object as non-movable so that there are no cases where Object::IsClass returns |
| 647 | // the incorrect result when comparing to-space vs from-space. |
Vladimir Marko | d7e9bbf | 2019-03-28 13:18:57 +0000 | [diff] [blame] | 648 | Handle<mirror::Class> java_lang_Class(hs.NewHandle(ObjPtr<mirror::Class>::DownCast( |
Vladimir Marko | 991cd5c | 2019-05-30 14:23:39 +0100 | [diff] [blame] | 649 | heap->AllocNonMovableObject(self, nullptr, class_class_size, VoidFunctor())))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 650 | CHECK(java_lang_Class != nullptr); |
Vladimir Marko | 317892b | 2018-05-31 11:11:32 +0100 | [diff] [blame] | 651 | java_lang_Class->SetClassFlags(mirror::kClassFlagClass); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 652 | java_lang_Class->SetClass(java_lang_Class.Get()); |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 653 | if (kUseBakerReadBarrier) { |
| 654 | java_lang_Class->AssertReadBarrierState(); |
Hiroshi Yamauchi | 9d04a20 | 2014-01-31 13:35:49 -0800 | [diff] [blame] | 655 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 656 | java_lang_Class->SetClassSize(class_class_size); |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 657 | java_lang_Class->SetPrimitiveType(Primitive::kPrimNot); |
Mathieu Chartier | 1d27b34 | 2014-01-28 12:51:09 -0800 | [diff] [blame] | 658 | heap->DecrementDisableMovingGC(self); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 659 | // AllocClass(ObjPtr<mirror::Class>) can now be used |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 660 | |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 661 | // Class[] is used for reflection support. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 662 | 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] | 663 | Handle<mirror::Class> class_array_class(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 664 | AllocClass(self, java_lang_Class.Get(), class_array_class_size))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 665 | class_array_class->SetComponentType(java_lang_Class.Get()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 666 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 667 | // 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] | 668 | Handle<mirror::Class> java_lang_Object(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 669 | AllocClass(self, java_lang_Class.Get(), mirror::Object::ClassSize(image_pointer_size_)))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 670 | CHECK(java_lang_Object != nullptr); |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 671 | // backfill Object as the super class of Class. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 672 | java_lang_Class->SetSuperClass(java_lang_Object.Get()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 673 | mirror::Class::SetStatus(java_lang_Object, ClassStatus::kLoaded, self); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 674 | |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 675 | java_lang_Object->SetObjectSize(sizeof(mirror::Object)); |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 676 | // Allocate in non-movable so that it's possible to check if a JNI weak global ref has been |
| 677 | // cleared without triggering the read barrier and unintentionally mark the sentinel alive. |
Vladimir Marko | 991cd5c | 2019-05-30 14:23:39 +0100 | [diff] [blame] | 678 | runtime->SetSentinel(heap->AllocNonMovableObject(self, |
| 679 | java_lang_Object.Get(), |
| 680 | java_lang_Object->GetObjectSize(), |
| 681 | VoidFunctor())); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 682 | |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 683 | // Initialize the SubtypeCheck bitstring for java.lang.Object and java.lang.Class. |
Vladimir Marko | 305c38b | 2018-02-14 11:50:07 +0000 | [diff] [blame] | 684 | if (kBitstringSubtypeCheckEnabled) { |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 685 | // It might seem the lock here is unnecessary, however all the SubtypeCheck |
| 686 | // functions are annotated to require locks all the way down. |
| 687 | // |
| 688 | // We take the lock here to avoid using NO_THREAD_SAFETY_ANALYSIS. |
| 689 | MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_); |
Vladimir Marko | 38b8b25 | 2018-01-02 19:07:06 +0000 | [diff] [blame] | 690 | SubtypeCheck<ObjPtr<mirror::Class>>::EnsureInitialized(java_lang_Object.Get()); |
| 691 | SubtypeCheck<ObjPtr<mirror::Class>>::EnsureInitialized(java_lang_Class.Get()); |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 692 | } |
| 693 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 694 | // Object[] next to hold class roots. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 695 | Handle<mirror::Class> object_array_class(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 696 | AllocClass(self, java_lang_Class.Get(), |
| 697 | mirror::ObjectArray<mirror::Object>::ClassSize(image_pointer_size_)))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 698 | object_array_class->SetComponentType(java_lang_Object.Get()); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 699 | |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 700 | // Setup java.lang.String. |
| 701 | // |
| 702 | // We make this class non-movable for the unlikely case where it were to be |
| 703 | // moved by a sticky-bit (minor) collection when using the Generational |
| 704 | // Concurrent Copying (CC) collector, potentially creating a stale reference |
| 705 | // in the `klass_` field of one of its instances allocated in the Large-Object |
| 706 | // Space (LOS) -- see the comment about the dirty card scanning logic in |
| 707 | // art::gc::collector::ConcurrentCopying::MarkingPhase. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 708 | Handle<mirror::Class> java_lang_String(hs.NewHandle( |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 709 | AllocClass</* kMovable= */ false>( |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 710 | self, java_lang_Class.Get(), mirror::String::ClassSize(image_pointer_size_)))); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 711 | java_lang_String->SetStringClass(); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 712 | mirror::Class::SetStatus(java_lang_String, ClassStatus::kResolved, self); |
Jesse Wilson | 1415074 | 2011-07-29 19:04:44 -0400 | [diff] [blame] | 713 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 714 | // Setup java.lang.ref.Reference. |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 715 | Handle<mirror::Class> java_lang_ref_Reference(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 716 | AllocClass(self, java_lang_Class.Get(), mirror::Reference::ClassSize(image_pointer_size_)))); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 717 | java_lang_ref_Reference->SetObjectSize(mirror::Reference::InstanceSize()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 718 | mirror::Class::SetStatus(java_lang_ref_Reference, ClassStatus::kResolved, self); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 719 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 720 | // 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] | 721 | class_roots_ = GcRoot<mirror::ObjectArray<mirror::Class>>( |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 722 | mirror::ObjectArray<mirror::Class>::Alloc(self, |
| 723 | object_array_class.Get(), |
| 724 | static_cast<int32_t>(ClassRoot::kMax))); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 725 | CHECK(!class_roots_.IsNull()); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 726 | SetClassRoot(ClassRoot::kJavaLangClass, java_lang_Class.Get()); |
| 727 | SetClassRoot(ClassRoot::kJavaLangObject, java_lang_Object.Get()); |
| 728 | SetClassRoot(ClassRoot::kClassArrayClass, class_array_class.Get()); |
| 729 | SetClassRoot(ClassRoot::kObjectArrayClass, object_array_class.Get()); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 730 | SetClassRoot(ClassRoot::kJavaLangString, java_lang_String.Get()); |
| 731 | SetClassRoot(ClassRoot::kJavaLangRefReference, java_lang_ref_Reference.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 732 | |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 733 | // Fill in the empty iftable. Needs to be done after the kObjectArrayClass root is set. |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 734 | java_lang_Object->SetIfTable(AllocIfTable(self, 0, object_array_class.Get())); |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 735 | |
Vladimir Marko | 0261055 | 2018-06-04 14:38:00 +0100 | [diff] [blame] | 736 | // Create array interface entries to populate once we can load system classes. |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 737 | object_array_class->SetIfTable(AllocIfTable(self, 2, object_array_class.Get())); |
Vladimir Marko | 0261055 | 2018-06-04 14:38:00 +0100 | [diff] [blame] | 738 | DCHECK_EQ(GetArrayIfTable(), object_array_class->GetIfTable()); |
| 739 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 740 | // Setup the primitive type classes. |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 741 | CreatePrimitiveClass(self, Primitive::kPrimBoolean, ClassRoot::kPrimitiveBoolean); |
| 742 | CreatePrimitiveClass(self, Primitive::kPrimByte, ClassRoot::kPrimitiveByte); |
| 743 | CreatePrimitiveClass(self, Primitive::kPrimChar, ClassRoot::kPrimitiveChar); |
| 744 | CreatePrimitiveClass(self, Primitive::kPrimShort, ClassRoot::kPrimitiveShort); |
| 745 | CreatePrimitiveClass(self, Primitive::kPrimInt, ClassRoot::kPrimitiveInt); |
| 746 | CreatePrimitiveClass(self, Primitive::kPrimLong, ClassRoot::kPrimitiveLong); |
| 747 | CreatePrimitiveClass(self, Primitive::kPrimFloat, ClassRoot::kPrimitiveFloat); |
| 748 | CreatePrimitiveClass(self, Primitive::kPrimDouble, ClassRoot::kPrimitiveDouble); |
| 749 | CreatePrimitiveClass(self, Primitive::kPrimVoid, ClassRoot::kPrimitiveVoid); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 750 | |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 751 | // Allocate the primitive array classes. We need only the native pointer |
| 752 | // array at this point (int[] or long[], depending on architecture) but |
| 753 | // we shall perform the same setup steps for all primitive array classes. |
| 754 | AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveBoolean, ClassRoot::kBooleanArrayClass); |
| 755 | AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveByte, ClassRoot::kByteArrayClass); |
| 756 | AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveChar, ClassRoot::kCharArrayClass); |
| 757 | AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveShort, ClassRoot::kShortArrayClass); |
| 758 | AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveInt, ClassRoot::kIntArrayClass); |
| 759 | AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveLong, ClassRoot::kLongArrayClass); |
| 760 | AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveFloat, ClassRoot::kFloatArrayClass); |
| 761 | AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveDouble, ClassRoot::kDoubleArrayClass); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 762 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 763 | // now that these are registered, we can use AllocClass() and AllocObjectArray |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 764 | |
Ian Rogers | 52813c9 | 2012-10-11 11:50:38 -0700 | [diff] [blame] | 765 | // Set up DexCache. This cannot be done later since AppendToBootClassPath calls AllocDexCache. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 766 | Handle<mirror::Class> java_lang_DexCache(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 767 | AllocClass(self, java_lang_Class.Get(), mirror::DexCache::ClassSize(image_pointer_size_)))); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 768 | SetClassRoot(ClassRoot::kJavaLangDexCache, java_lang_DexCache.Get()); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 769 | java_lang_DexCache->SetDexCacheClass(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 770 | java_lang_DexCache->SetObjectSize(mirror::DexCache::InstanceSize()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 771 | mirror::Class::SetStatus(java_lang_DexCache, ClassStatus::kResolved, self); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 772 | |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 773 | |
| 774 | // Setup dalvik.system.ClassExt |
| 775 | Handle<mirror::Class> dalvik_system_ClassExt(hs.NewHandle( |
| 776 | AllocClass(self, java_lang_Class.Get(), mirror::ClassExt::ClassSize(image_pointer_size_)))); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 777 | SetClassRoot(ClassRoot::kDalvikSystemClassExt, dalvik_system_ClassExt.Get()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 778 | mirror::Class::SetStatus(dalvik_system_ClassExt, ClassStatus::kResolved, self); |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 779 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 780 | // Set up array classes for string, field, method |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 781 | Handle<mirror::Class> object_array_string(hs.NewHandle( |
| 782 | AllocClass(self, java_lang_Class.Get(), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 783 | mirror::ObjectArray<mirror::String>::ClassSize(image_pointer_size_)))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 784 | object_array_string->SetComponentType(java_lang_String.Get()); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 785 | SetClassRoot(ClassRoot::kJavaLangStringArrayClass, object_array_string.Get()); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 786 | |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 787 | LinearAlloc* linear_alloc = runtime->GetLinearAlloc(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 788 | // Create runtime resolution and imt conflict methods. |
| 789 | runtime->SetResolutionMethod(runtime->CreateResolutionMethod()); |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 790 | runtime->SetImtConflictMethod(runtime->CreateImtConflictMethod(linear_alloc)); |
| 791 | runtime->SetImtUnimplementedMethod(runtime->CreateImtConflictMethod(linear_alloc)); |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 792 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 793 | // Setup boot_class_path_ and register class_path now that we can use AllocObjectArray to create |
| 794 | // DexCache instances. Needs to be after String, Field, Method arrays since AllocDexCache uses |
| 795 | // these roots. |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 796 | if (boot_class_path.empty()) { |
| 797 | *error_msg = "Boot classpath is empty."; |
| 798 | return false; |
| 799 | } |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 800 | for (auto& dex_file : boot_class_path) { |
Mathieu Chartier | 0a19e21 | 2019-11-27 14:35:24 -0800 | [diff] [blame] | 801 | if (dex_file == nullptr) { |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 802 | *error_msg = "Null dex file."; |
| 803 | return false; |
| 804 | } |
Mathieu Chartier | 0a19e21 | 2019-11-27 14:35:24 -0800 | [diff] [blame] | 805 | AppendToBootClassPath(self, dex_file.get()); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 806 | boot_dex_files_.push_back(std::move(dex_file)); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 807 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 808 | |
| 809 | // now we can use FindSystemClass |
| 810 | |
Dmitry Petrochenko | f0972a4 | 2014-05-16 17:43:39 +0700 | [diff] [blame] | 811 | // Set up GenericJNI entrypoint. That is mainly a hack for common_compiler_test.h so that |
| 812 | // we do not need friend classes or a publicly exposed setter. |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 813 | quick_generic_jni_trampoline_ = GetQuickGenericJniStub(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 814 | if (!runtime->IsAotCompiler()) { |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 815 | // 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] | 816 | jni_dlsym_lookup_trampoline_ = GetJniDlsymLookupStub(); |
Vladimir Marko | fa458ac | 2020-02-12 14:08:07 +0000 | [diff] [blame] | 817 | jni_dlsym_lookup_critical_trampoline_ = GetJniDlsymLookupCriticalStub(); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 818 | quick_resolution_trampoline_ = GetQuickResolutionStub(); |
| 819 | quick_imt_conflict_trampoline_ = GetQuickImtConflictStub(); |
Vladimir Marko | 7dac864 | 2019-11-06 17:09:30 +0000 | [diff] [blame] | 820 | quick_generic_jni_trampoline_ = GetQuickGenericJniStub(); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 821 | quick_to_interpreter_bridge_trampoline_ = GetQuickToInterpreterBridge(); |
Nicolas Geoffray | c39af94 | 2021-01-25 08:43:57 +0000 | [diff] [blame] | 822 | nterp_trampoline_ = interpreter::GetNterpEntryPoint(); |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 823 | } |
Dmitry Petrochenko | f0972a4 | 2014-05-16 17:43:39 +0700 | [diff] [blame] | 824 | |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 825 | // 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] | 826 | mirror::Class::SetStatus(java_lang_Object, ClassStatus::kNotReady, self); |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 827 | CheckSystemClass(self, java_lang_Object, "Ljava/lang/Object;"); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 828 | CHECK_EQ(java_lang_Object->GetObjectSize(), mirror::Object::InstanceSize()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 829 | mirror::Class::SetStatus(java_lang_String, ClassStatus::kNotReady, self); |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 830 | CheckSystemClass(self, java_lang_String, "Ljava/lang/String;"); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 831 | mirror::Class::SetStatus(java_lang_DexCache, ClassStatus::kNotReady, self); |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 832 | CheckSystemClass(self, java_lang_DexCache, "Ljava/lang/DexCache;"); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 833 | CHECK_EQ(java_lang_DexCache->GetObjectSize(), mirror::DexCache::InstanceSize()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 834 | mirror::Class::SetStatus(dalvik_system_ClassExt, ClassStatus::kNotReady, self); |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 835 | CheckSystemClass(self, dalvik_system_ClassExt, "Ldalvik/system/ClassExt;"); |
| 836 | CHECK_EQ(dalvik_system_ClassExt->GetObjectSize(), mirror::ClassExt::InstanceSize()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 837 | |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 838 | // Run Class through FindSystemClass. This initializes the dex_cache_ fields and register it |
| 839 | // in class_table_. |
| 840 | CheckSystemClass(self, java_lang_Class, "Ljava/lang/Class;"); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 841 | |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 842 | // Setup core array classes, i.e. Object[], String[] and Class[] and primitive |
| 843 | // arrays - can't be done until Object has a vtable and component classes are loaded. |
| 844 | FinishCoreArrayClassSetup(ClassRoot::kObjectArrayClass); |
| 845 | FinishCoreArrayClassSetup(ClassRoot::kClassArrayClass); |
| 846 | FinishCoreArrayClassSetup(ClassRoot::kJavaLangStringArrayClass); |
| 847 | FinishCoreArrayClassSetup(ClassRoot::kBooleanArrayClass); |
| 848 | FinishCoreArrayClassSetup(ClassRoot::kByteArrayClass); |
| 849 | FinishCoreArrayClassSetup(ClassRoot::kCharArrayClass); |
| 850 | FinishCoreArrayClassSetup(ClassRoot::kShortArrayClass); |
| 851 | FinishCoreArrayClassSetup(ClassRoot::kIntArrayClass); |
| 852 | FinishCoreArrayClassSetup(ClassRoot::kLongArrayClass); |
| 853 | FinishCoreArrayClassSetup(ClassRoot::kFloatArrayClass); |
| 854 | FinishCoreArrayClassSetup(ClassRoot::kDoubleArrayClass); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 855 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 856 | // Setup the single, global copy of "iftable". |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 857 | auto java_lang_Cloneable = hs.NewHandle(FindSystemClass(self, "Ljava/lang/Cloneable;")); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 858 | CHECK(java_lang_Cloneable != nullptr); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 859 | auto java_io_Serializable = hs.NewHandle(FindSystemClass(self, "Ljava/io/Serializable;")); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 860 | CHECK(java_io_Serializable != nullptr); |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 861 | // We assume that Cloneable/Serializable don't have superinterfaces -- normally we'd have to |
| 862 | // crawl up and explicitly list all of the supers as well. |
Vladimir Marko | 0261055 | 2018-06-04 14:38:00 +0100 | [diff] [blame] | 863 | object_array_class->GetIfTable()->SetInterface(0, java_lang_Cloneable.Get()); |
| 864 | object_array_class->GetIfTable()->SetInterface(1, java_io_Serializable.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 865 | |
Vladimir Marko | b10668c | 2021-06-10 09:52:53 +0100 | [diff] [blame] | 866 | // Check Class[] and Object[]'s interfaces. |
| 867 | CHECK_EQ(java_lang_Cloneable.Get(), class_array_class->GetDirectInterface(0)); |
| 868 | CHECK_EQ(java_io_Serializable.Get(), class_array_class->GetDirectInterface(1)); |
| 869 | CHECK_EQ(java_lang_Cloneable.Get(), object_array_class->GetDirectInterface(0)); |
| 870 | CHECK_EQ(java_io_Serializable.Get(), object_array_class->GetDirectInterface(1)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 871 | |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 872 | CHECK_EQ(object_array_string.Get(), |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 873 | FindSystemClass(self, GetClassRootDescriptor(ClassRoot::kJavaLangStringArrayClass))); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 874 | |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 875 | // 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] | 876 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 877 | // Create java.lang.reflect.Proxy root. |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 878 | SetClassRoot(ClassRoot::kJavaLangReflectProxy, |
| 879 | FindSystemClass(self, "Ljava/lang/reflect/Proxy;")); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 880 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 881 | // Create java.lang.reflect.Field.class root. |
Vladimir Marko | acb906d | 2018-05-30 10:23:49 +0100 | [diff] [blame] | 882 | ObjPtr<mirror::Class> class_root = FindSystemClass(self, "Ljava/lang/reflect/Field;"); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 883 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 884 | SetClassRoot(ClassRoot::kJavaLangReflectField, class_root); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 885 | |
| 886 | // Create java.lang.reflect.Field array root. |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 887 | class_root = FindSystemClass(self, "[Ljava/lang/reflect/Field;"); |
| 888 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 889 | SetClassRoot(ClassRoot::kJavaLangReflectFieldArrayClass, class_root); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 890 | |
| 891 | // Create java.lang.reflect.Constructor.class root and array root. |
| 892 | class_root = FindSystemClass(self, "Ljava/lang/reflect/Constructor;"); |
| 893 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 894 | SetClassRoot(ClassRoot::kJavaLangReflectConstructor, class_root); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 895 | class_root = FindSystemClass(self, "[Ljava/lang/reflect/Constructor;"); |
| 896 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 897 | SetClassRoot(ClassRoot::kJavaLangReflectConstructorArrayClass, class_root); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 898 | |
| 899 | // Create java.lang.reflect.Method.class root and array root. |
| 900 | class_root = FindSystemClass(self, "Ljava/lang/reflect/Method;"); |
| 901 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 902 | SetClassRoot(ClassRoot::kJavaLangReflectMethod, class_root); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 903 | class_root = FindSystemClass(self, "[Ljava/lang/reflect/Method;"); |
| 904 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 905 | SetClassRoot(ClassRoot::kJavaLangReflectMethodArrayClass, class_root); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 906 | |
Orion Hodson | 005ac51 | 2017-10-24 15:43:43 +0100 | [diff] [blame] | 907 | // Create java.lang.invoke.CallSite.class root |
| 908 | class_root = FindSystemClass(self, "Ljava/lang/invoke/CallSite;"); |
| 909 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 910 | SetClassRoot(ClassRoot::kJavaLangInvokeCallSite, class_root); |
Orion Hodson | 005ac51 | 2017-10-24 15:43:43 +0100 | [diff] [blame] | 911 | |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 912 | // Create java.lang.invoke.MethodType.class root |
| 913 | class_root = FindSystemClass(self, "Ljava/lang/invoke/MethodType;"); |
| 914 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 915 | SetClassRoot(ClassRoot::kJavaLangInvokeMethodType, class_root); |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 916 | |
| 917 | // Create java.lang.invoke.MethodHandleImpl.class root |
| 918 | class_root = FindSystemClass(self, "Ljava/lang/invoke/MethodHandleImpl;"); |
| 919 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 920 | SetClassRoot(ClassRoot::kJavaLangInvokeMethodHandleImpl, class_root); |
Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 921 | SetClassRoot(ClassRoot::kJavaLangInvokeMethodHandle, class_root->GetSuperClass()); |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 922 | |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 923 | // Create java.lang.invoke.MethodHandles.Lookup.class root |
| 924 | class_root = FindSystemClass(self, "Ljava/lang/invoke/MethodHandles$Lookup;"); |
| 925 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 926 | SetClassRoot(ClassRoot::kJavaLangInvokeMethodHandlesLookup, class_root); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 927 | |
Orion Hodson | 005ac51 | 2017-10-24 15:43:43 +0100 | [diff] [blame] | 928 | // Create java.lang.invoke.VarHandle.class root |
| 929 | class_root = FindSystemClass(self, "Ljava/lang/invoke/VarHandle;"); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 930 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 931 | SetClassRoot(ClassRoot::kJavaLangInvokeVarHandle, class_root); |
Orion Hodson | 005ac51 | 2017-10-24 15:43:43 +0100 | [diff] [blame] | 932 | |
| 933 | // Create java.lang.invoke.FieldVarHandle.class root |
| 934 | class_root = FindSystemClass(self, "Ljava/lang/invoke/FieldVarHandle;"); |
| 935 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 936 | SetClassRoot(ClassRoot::kJavaLangInvokeFieldVarHandle, class_root); |
Orion Hodson | 005ac51 | 2017-10-24 15:43:43 +0100 | [diff] [blame] | 937 | |
Orion Hodson | dd41196 | 2021-06-25 08:55:22 +0100 | [diff] [blame] | 938 | // Create java.lang.invoke.StaticFieldVarHandle.class root |
| 939 | class_root = FindSystemClass(self, "Ljava/lang/invoke/StaticFieldVarHandle;"); |
| 940 | CHECK(class_root != nullptr); |
| 941 | SetClassRoot(ClassRoot::kJavaLangInvokeStaticFieldVarHandle, class_root); |
| 942 | |
Orion Hodson | 005ac51 | 2017-10-24 15:43:43 +0100 | [diff] [blame] | 943 | // Create java.lang.invoke.ArrayElementVarHandle.class root |
| 944 | class_root = FindSystemClass(self, "Ljava/lang/invoke/ArrayElementVarHandle;"); |
| 945 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 946 | SetClassRoot(ClassRoot::kJavaLangInvokeArrayElementVarHandle, class_root); |
Orion Hodson | 005ac51 | 2017-10-24 15:43:43 +0100 | [diff] [blame] | 947 | |
| 948 | // Create java.lang.invoke.ByteArrayViewVarHandle.class root |
| 949 | class_root = FindSystemClass(self, "Ljava/lang/invoke/ByteArrayViewVarHandle;"); |
| 950 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 951 | SetClassRoot(ClassRoot::kJavaLangInvokeByteArrayViewVarHandle, class_root); |
Orion Hodson | 005ac51 | 2017-10-24 15:43:43 +0100 | [diff] [blame] | 952 | |
| 953 | // Create java.lang.invoke.ByteBufferViewVarHandle.class root |
| 954 | class_root = FindSystemClass(self, "Ljava/lang/invoke/ByteBufferViewVarHandle;"); |
| 955 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 956 | SetClassRoot(ClassRoot::kJavaLangInvokeByteBufferViewVarHandle, class_root); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 957 | |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 958 | class_root = FindSystemClass(self, "Ldalvik/system/EmulatedStackFrame;"); |
| 959 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 960 | SetClassRoot(ClassRoot::kDalvikSystemEmulatedStackFrame, class_root); |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 961 | |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 962 | // 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] | 963 | // finish initializing Reference class |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 964 | mirror::Class::SetStatus(java_lang_ref_Reference, ClassStatus::kNotReady, self); |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 965 | CheckSystemClass(self, java_lang_ref_Reference, "Ljava/lang/ref/Reference;"); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 966 | CHECK_EQ(java_lang_ref_Reference->GetObjectSize(), mirror::Reference::InstanceSize()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 967 | CHECK_EQ(java_lang_ref_Reference->GetClassSize(), |
| 968 | mirror::Reference::ClassSize(image_pointer_size_)); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 969 | class_root = FindSystemClass(self, "Ljava/lang/ref/FinalizerReference;"); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 970 | CHECK_EQ(class_root->GetClassFlags(), mirror::kClassFlagNormal); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 971 | class_root->SetClassFlags(class_root->GetClassFlags() | mirror::kClassFlagFinalizerReference); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 972 | class_root = FindSystemClass(self, "Ljava/lang/ref/PhantomReference;"); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 973 | CHECK_EQ(class_root->GetClassFlags(), mirror::kClassFlagNormal); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 974 | class_root->SetClassFlags(class_root->GetClassFlags() | mirror::kClassFlagPhantomReference); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 975 | class_root = FindSystemClass(self, "Ljava/lang/ref/SoftReference;"); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 976 | CHECK_EQ(class_root->GetClassFlags(), mirror::kClassFlagNormal); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 977 | class_root->SetClassFlags(class_root->GetClassFlags() | mirror::kClassFlagSoftReference); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 978 | class_root = FindSystemClass(self, "Ljava/lang/ref/WeakReference;"); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 979 | CHECK_EQ(class_root->GetClassFlags(), mirror::kClassFlagNormal); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 980 | class_root->SetClassFlags(class_root->GetClassFlags() | mirror::kClassFlagWeakReference); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 981 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 982 | // Setup the ClassLoader, verifying the object_size_. |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 983 | class_root = FindSystemClass(self, "Ljava/lang/ClassLoader;"); |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 984 | class_root->SetClassLoaderClass(); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 985 | CHECK_EQ(class_root->GetObjectSize(), mirror::ClassLoader::InstanceSize()); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 986 | SetClassRoot(ClassRoot::kJavaLangClassLoader, class_root); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 987 | |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 988 | // Set up java.lang.Throwable, java.lang.ClassNotFoundException, and |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 989 | // java.lang.StackTraceElement as a convenience. |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 990 | SetClassRoot(ClassRoot::kJavaLangThrowable, FindSystemClass(self, "Ljava/lang/Throwable;")); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 991 | SetClassRoot(ClassRoot::kJavaLangClassNotFoundException, |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 992 | FindSystemClass(self, "Ljava/lang/ClassNotFoundException;")); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 993 | SetClassRoot(ClassRoot::kJavaLangStackTraceElement, |
| 994 | FindSystemClass(self, "Ljava/lang/StackTraceElement;")); |
| 995 | SetClassRoot(ClassRoot::kJavaLangStackTraceElementArrayClass, |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 996 | FindSystemClass(self, "[Ljava/lang/StackTraceElement;")); |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 997 | SetClassRoot(ClassRoot::kJavaLangClassLoaderArrayClass, |
| 998 | FindSystemClass(self, "[Ljava/lang/ClassLoader;")); |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 999 | |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 1000 | // Create conflict tables that depend on the class linker. |
| 1001 | runtime->FixupConflictTables(); |
| 1002 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1003 | FinishInit(self); |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 1004 | |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 1005 | VLOG(startup) << "ClassLinker::InitFromCompiler exiting"; |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 1006 | |
| 1007 | return true; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1008 | } |
| 1009 | |
Andreas Gampe | 9abc31e | 2018-05-17 11:47:09 -0700 | [diff] [blame] | 1010 | static void CreateStringInitBindings(Thread* self, ClassLinker* class_linker) |
| 1011 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1012 | // Find String.<init> -> StringFactory bindings. |
| 1013 | ObjPtr<mirror::Class> string_factory_class = |
| 1014 | class_linker->FindSystemClass(self, "Ljava/lang/StringFactory;"); |
| 1015 | CHECK(string_factory_class != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 1016 | ObjPtr<mirror::Class> string_class = GetClassRoot<mirror::String>(class_linker); |
Andreas Gampe | 9abc31e | 2018-05-17 11:47:09 -0700 | [diff] [blame] | 1017 | WellKnownClasses::InitStringInit(string_class, string_factory_class); |
| 1018 | // Update the primordial thread. |
| 1019 | self->InitStringEntryPoints(); |
| 1020 | } |
| 1021 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1022 | void ClassLinker::FinishInit(Thread* self) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1023 | VLOG(startup) << "ClassLinker::FinishInit entering"; |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 1024 | |
Andreas Gampe | 9abc31e | 2018-05-17 11:47:09 -0700 | [diff] [blame] | 1025 | CreateStringInitBindings(self, this); |
| 1026 | |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 1027 | // Let the heap know some key offsets into java.lang.ref instances |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 1028 | // Note: we hard code the field indexes here rather than using FindInstanceField |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 1029 | // as the types of the field can't be resolved prior to the runtime being |
| 1030 | // fully initialized |
Andreas Gampe | 7b2450e | 2018-06-19 10:45:54 -0700 | [diff] [blame] | 1031 | StackHandleScope<3> hs(self); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 1032 | Handle<mirror::Class> java_lang_ref_Reference = |
| 1033 | hs.NewHandle(GetClassRoot<mirror::Reference>(this)); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1034 | Handle<mirror::Class> java_lang_ref_FinalizerReference = |
| 1035 | hs.NewHandle(FindSystemClass(self, "Ljava/lang/ref/FinalizerReference;")); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1036 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1037 | ArtField* pendingNext = java_lang_ref_Reference->GetInstanceField(0); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1038 | CHECK_STREQ(pendingNext->GetName(), "pendingNext"); |
| 1039 | CHECK_STREQ(pendingNext->GetTypeDescriptor(), "Ljava/lang/ref/Reference;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 1040 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1041 | ArtField* queue = java_lang_ref_Reference->GetInstanceField(1); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1042 | CHECK_STREQ(queue->GetName(), "queue"); |
| 1043 | CHECK_STREQ(queue->GetTypeDescriptor(), "Ljava/lang/ref/ReferenceQueue;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 1044 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1045 | ArtField* queueNext = java_lang_ref_Reference->GetInstanceField(2); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1046 | CHECK_STREQ(queueNext->GetName(), "queueNext"); |
| 1047 | CHECK_STREQ(queueNext->GetTypeDescriptor(), "Ljava/lang/ref/Reference;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 1048 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1049 | ArtField* referent = java_lang_ref_Reference->GetInstanceField(3); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1050 | CHECK_STREQ(referent->GetName(), "referent"); |
| 1051 | CHECK_STREQ(referent->GetTypeDescriptor(), "Ljava/lang/Object;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 1052 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1053 | ArtField* zombie = java_lang_ref_FinalizerReference->GetInstanceField(2); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1054 | CHECK_STREQ(zombie->GetName(), "zombie"); |
| 1055 | CHECK_STREQ(zombie->GetTypeDescriptor(), "Ljava/lang/Object;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 1056 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1057 | // ensure all class_roots_ are initialized |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 1058 | 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] | 1059 | ClassRoot class_root = static_cast<ClassRoot>(i); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1060 | ObjPtr<mirror::Class> klass = GetClassRoot(class_root); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1061 | CHECK(klass != nullptr); |
| 1062 | DCHECK(klass->IsArrayClass() || klass->IsPrimitive() || klass->GetDexCache() != nullptr); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1063 | // note SetClassRoot does additional validation. |
| 1064 | // if possible add new checks there to catch errors early |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 1065 | } |
| 1066 | |
Vladimir Marko | 0261055 | 2018-06-04 14:38:00 +0100 | [diff] [blame] | 1067 | CHECK(GetArrayIfTable() != nullptr); |
Elliott Hughes | 92f14b2 | 2011-10-06 12:29:54 -0700 | [diff] [blame] | 1068 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 1069 | // disable the slow paths in FindClass and CreatePrimitiveClass now |
| 1070 | // that Object, Class, and Object[] are setup |
| 1071 | init_done_ = true; |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 1072 | |
Andreas Gampe | 7b2450e | 2018-06-19 10:45:54 -0700 | [diff] [blame] | 1073 | // Under sanitization, the small carve-out to handle stack overflow might not be enough to |
| 1074 | // initialize the StackOverflowError class (as it might require running the verifier). Instead, |
| 1075 | // ensure that the class will be initialized. |
| 1076 | if (kMemoryToolIsAvailable && !Runtime::Current()->IsAotCompiler()) { |
Andreas Gampe | e0bbab9 | 2019-07-25 12:28:22 -0700 | [diff] [blame] | 1077 | verifier::ClassVerifier::Init(this); // Need to prepare the verifier. |
Andreas Gampe | 7b2450e | 2018-06-19 10:45:54 -0700 | [diff] [blame] | 1078 | |
| 1079 | ObjPtr<mirror::Class> soe_klass = FindSystemClass(self, "Ljava/lang/StackOverflowError;"); |
| 1080 | if (soe_klass == nullptr || !EnsureInitialized(self, hs.NewHandle(soe_klass), true, true)) { |
| 1081 | // Strange, but don't crash. |
| 1082 | LOG(WARNING) << "Could not prepare StackOverflowError."; |
| 1083 | self->ClearException(); |
| 1084 | } |
| 1085 | } |
| 1086 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1087 | VLOG(startup) << "ClassLinker::FinishInit exiting"; |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 1088 | } |
| 1089 | |
Vladimir Marko | ad82439 | 2022-10-20 10:57:35 +0000 | [diff] [blame] | 1090 | static void EnsureRootInitialized(ClassLinker* class_linker, |
| 1091 | Thread* self, |
| 1092 | ObjPtr<mirror::Class> klass) |
| 1093 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1094 | if (!klass->IsVisiblyInitialized()) { |
| 1095 | DCHECK(!klass->IsArrayClass()); |
| 1096 | DCHECK(!klass->IsPrimitive()); |
| 1097 | StackHandleScope<1> hs(self); |
| 1098 | Handle<mirror::Class> h_class(hs.NewHandle(klass)); |
| 1099 | if (!class_linker->EnsureInitialized( |
| 1100 | self, h_class, /*can_init_fields=*/ true, /*can_init_parents=*/ true)) { |
| 1101 | LOG(FATAL) << "Exception when initializing " << h_class->PrettyClass() |
| 1102 | << ": " << self->GetException()->Dump(); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 1103 | } |
| 1104 | } |
| 1105 | } |
| 1106 | |
Vladimir Marko | ad82439 | 2022-10-20 10:57:35 +0000 | [diff] [blame] | 1107 | void ClassLinker::RunEarlyRootClinits(Thread* self) { |
| 1108 | StackHandleScope<1u> hs(self); |
| 1109 | Handle<mirror::ObjectArray<mirror::Class>> class_roots = hs.NewHandle(GetClassRoots()); |
| 1110 | EnsureRootInitialized(this, self, GetClassRoot<mirror::Class>(class_roots.Get())); |
| 1111 | EnsureRootInitialized(this, self, GetClassRoot<mirror::String>(class_roots.Get())); |
Vladimir Marko | addc2d1 | 2022-11-29 08:47:02 +0000 | [diff] [blame] | 1112 | // `Field` class is needed for register_java_net_InetAddress in libcore, b/28153851. |
Vladimir Marko | ad82439 | 2022-10-20 10:57:35 +0000 | [diff] [blame] | 1113 | EnsureRootInitialized(this, self, GetClassRoot<mirror::Field>(class_roots.Get())); |
Vladimir Marko | addc2d1 | 2022-11-29 08:47:02 +0000 | [diff] [blame] | 1114 | |
| 1115 | WellKnownClasses::Init(self->GetJniEnv()); |
| 1116 | |
| 1117 | // `FinalizerReference` class is needed for initialization of `java.net.InetAddress`. |
| 1118 | // (Indirectly by constructing a `ObjectStreamField` which uses a `StringBuilder` |
| 1119 | // and, when resizing, initializes the `System` class for `System.arraycopy()` |
| 1120 | // and `System.<clinit> creates a finalizable object.) |
| 1121 | EnsureRootInitialized( |
| 1122 | this, self, WellKnownClasses::java_lang_ref_FinalizerReference_add->GetDeclaringClass()); |
Vladimir Marko | ad82439 | 2022-10-20 10:57:35 +0000 | [diff] [blame] | 1123 | } |
| 1124 | |
| 1125 | void ClassLinker::RunRootClinits(Thread* self) { |
| 1126 | StackHandleScope<1u> hs(self); |
| 1127 | Handle<mirror::ObjectArray<mirror::Class>> class_roots = hs.NewHandle(GetClassRoots()); |
| 1128 | for (size_t i = 0; i < static_cast<size_t>(ClassRoot::kMax); ++i) { |
| 1129 | EnsureRootInitialized(this, self, GetClassRoot(ClassRoot(i), class_roots.Get())); |
| 1130 | } |
| 1131 | |
| 1132 | // Make sure certain well-known classes are initialized. Note that well-known |
| 1133 | // classes are always in the boot image, so this code is primarily intended |
| 1134 | // for running without boot image but may be needed for boot image if the |
| 1135 | // AOT-initialization fails due to introduction of new code to `<clinit>`. |
| 1136 | ArtMethod* static_methods_of_classes_to_initialize[] = { |
| 1137 | // Initialize primitive boxing classes (avoid check at runtime). |
| 1138 | WellKnownClasses::java_lang_Boolean_valueOf, |
| 1139 | WellKnownClasses::java_lang_Byte_valueOf, |
| 1140 | WellKnownClasses::java_lang_Character_valueOf, |
| 1141 | WellKnownClasses::java_lang_Double_valueOf, |
| 1142 | WellKnownClasses::java_lang_Float_valueOf, |
| 1143 | WellKnownClasses::java_lang_Integer_valueOf, |
| 1144 | WellKnownClasses::java_lang_Long_valueOf, |
| 1145 | WellKnownClasses::java_lang_Short_valueOf, |
Vladimir Marko | 7b97aeb | 2022-12-01 16:17:47 +0000 | [diff] [blame] | 1146 | // Initialize `StackOverflowError`. |
| 1147 | WellKnownClasses::java_lang_StackOverflowError_init, |
Vladimir Marko | b6f965d | 2022-11-23 14:24:45 +0000 | [diff] [blame] | 1148 | // Ensure class loader classes are initialized (avoid check at runtime). |
Vladimir Marko | addc2d1 | 2022-11-29 08:47:02 +0000 | [diff] [blame] | 1149 | // Superclass `ClassLoader` is a class root and already initialized above. |
| 1150 | // Superclass `BaseDexClassLoader` is initialized implicitly. |
Vladimir Marko | b6f965d | 2022-11-23 14:24:45 +0000 | [diff] [blame] | 1151 | WellKnownClasses::dalvik_system_DelegateLastClassLoader_init, |
| 1152 | WellKnownClasses::dalvik_system_DexClassLoader_init, |
| 1153 | WellKnownClasses::dalvik_system_InMemoryDexClassLoader_init, |
| 1154 | WellKnownClasses::dalvik_system_PathClassLoader_init, |
| 1155 | WellKnownClasses::java_lang_BootClassLoader_init, |
Vladimir Marko | addc2d1 | 2022-11-29 08:47:02 +0000 | [diff] [blame] | 1156 | // Ensure `Daemons` class is initialized (avoid check at runtime). |
| 1157 | WellKnownClasses::java_lang_Daemons_start, |
Vladimir Marko | f7bd1fa | 2022-11-18 10:55:05 +0000 | [diff] [blame] | 1158 | // Ensure `Thread` and `ThreadGroup` classes are initialized (avoid check at runtime). |
| 1159 | WellKnownClasses::java_lang_Thread_init, |
| 1160 | WellKnownClasses::java_lang_ThreadGroup_add, |
Vladimir Marko | addc2d1 | 2022-11-29 08:47:02 +0000 | [diff] [blame] | 1161 | // Ensure reference classes are initialized (avoid check at runtime). |
| 1162 | // The `FinalizerReference` class was initialized in `RunEarlyRootClinits()`. |
| 1163 | WellKnownClasses::java_lang_ref_ReferenceQueue_add, |
| 1164 | // Ensure `InvocationTargetException` class is initialized (avoid check at runtime). |
| 1165 | WellKnownClasses::java_lang_reflect_InvocationTargetException_init, |
| 1166 | // Ensure `Parameter` class is initialized (avoid check at runtime). |
| 1167 | WellKnownClasses::java_lang_reflect_Parameter_init, |
Vladimir Marko | 316fe63 | 2022-11-22 11:00:59 +0000 | [diff] [blame] | 1168 | // Ensure `MethodHandles` class is initialized (avoid check at runtime). |
| 1169 | WellKnownClasses::java_lang_invoke_MethodHandles_lookup, |
Vladimir Marko | f961359 | 2022-11-07 08:10:25 +0000 | [diff] [blame] | 1170 | // Ensure `DirectByteBuffer` class is initialized (avoid check at runtime). |
| 1171 | WellKnownClasses::java_nio_DirectByteBuffer_init, |
Vladimir Marko | 41de450 | 2019-05-21 10:00:15 +0100 | [diff] [blame] | 1172 | // Ensure `FloatingDecimal` class is initialized (avoid check at runtime). |
| 1173 | WellKnownClasses::jdk_internal_math_FloatingDecimal_getBinaryToASCIIConverter_D, |
Vladimir Marko | f776c17 | 2022-11-03 17:29:49 +0000 | [diff] [blame] | 1174 | // Ensure reflection annotation classes are initialized (avoid check at runtime). |
| 1175 | WellKnownClasses::libcore_reflect_AnnotationFactory_createAnnotation, |
| 1176 | WellKnownClasses::libcore_reflect_AnnotationMember_init, |
Vladimir Marko | a3749b2 | 2022-11-02 13:42:54 +0000 | [diff] [blame] | 1177 | // We're suppressing exceptions from `DdmServer` and we do not want to repeatedly |
| 1178 | // suppress class initialization error (say, due to OOM), so initialize it early. |
| 1179 | WellKnownClasses::org_apache_harmony_dalvik_ddmc_DdmServer_dispatch, |
Vladimir Marko | ad82439 | 2022-10-20 10:57:35 +0000 | [diff] [blame] | 1180 | }; |
| 1181 | for (ArtMethod* method : static_methods_of_classes_to_initialize) { |
| 1182 | EnsureRootInitialized(this, self, method->GetDeclaringClass()); |
| 1183 | } |
Nicolas Geoffray | caf21e3 | 2022-10-24 06:39:59 +0000 | [diff] [blame] | 1184 | ArtField* static_fields_of_classes_to_initialize[] = { |
Vladimir Marko | b6f965d | 2022-11-23 14:24:45 +0000 | [diff] [blame] | 1185 | // Ensure classes used by class loaders are initialized (avoid check at runtime). |
| 1186 | WellKnownClasses::dalvik_system_DexFile_cookie, |
| 1187 | WellKnownClasses::dalvik_system_DexPathList_dexElements, |
| 1188 | WellKnownClasses::dalvik_system_DexPathList__Element_dexFile, |
Vladimir Marko | f961359 | 2022-11-07 08:10:25 +0000 | [diff] [blame] | 1189 | // Ensure `VMRuntime` is initialized (avoid check at runtime). |
| 1190 | WellKnownClasses::dalvik_system_VMRuntime_nonSdkApiUsageConsumer, |
Nicolas Geoffray | caf21e3 | 2022-10-24 06:39:59 +0000 | [diff] [blame] | 1191 | // Initialize empty arrays needed by `StackOverflowError`. |
| 1192 | WellKnownClasses::java_util_Collections_EMPTY_LIST, |
| 1193 | WellKnownClasses::libcore_util_EmptyArray_STACK_TRACE_ELEMENT, |
| 1194 | }; |
| 1195 | for (ArtField* field : static_fields_of_classes_to_initialize) { |
| 1196 | EnsureRootInitialized(this, self, field->GetDeclaringClass()); |
| 1197 | } |
Vladimir Marko | ad82439 | 2022-10-20 10:57:35 +0000 | [diff] [blame] | 1198 | } |
| 1199 | |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 1200 | ALWAYS_INLINE |
| 1201 | static uint32_t ComputeMethodHash(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1202 | DCHECK(!method->IsRuntimeMethod()); |
| 1203 | DCHECK(!method->IsProxyMethod()); |
| 1204 | DCHECK(!method->IsObsolete()); |
| 1205 | // Do not use `ArtMethod::GetNameView()` to avoid unnecessary runtime/proxy/obsolete method |
| 1206 | // checks. It is safe to avoid the read barrier here, see `ArtMethod::GetDexFile()`. |
| 1207 | const DexFile& dex_file = method->GetDeclaringClass<kWithoutReadBarrier>()->GetDexFile(); |
| 1208 | const dex::MethodId& method_id = dex_file.GetMethodId(method->GetDexMethodIndex()); |
| 1209 | std::string_view name = dex_file.GetMethodNameView(method_id); |
| 1210 | return ComputeModifiedUtf8Hash(name); |
| 1211 | } |
| 1212 | |
Vladimir Marko | bed84ef | 2022-01-21 13:57:14 +0000 | [diff] [blame] | 1213 | ALWAYS_INLINE |
| 1214 | static bool MethodSignatureEquals(ArtMethod* lhs, ArtMethod* rhs) |
| 1215 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1216 | DCHECK(!lhs->IsRuntimeMethod()); |
| 1217 | DCHECK(!lhs->IsProxyMethod()); |
| 1218 | DCHECK(!lhs->IsObsolete()); |
| 1219 | DCHECK(!rhs->IsRuntimeMethod()); |
| 1220 | DCHECK(!rhs->IsProxyMethod()); |
| 1221 | DCHECK(!rhs->IsObsolete()); |
| 1222 | // Do not use `ArtMethod::GetDexFile()` to avoid unnecessary obsolete method checks. |
| 1223 | // It is safe to avoid the read barrier here, see `ArtMethod::GetDexFile()`. |
| 1224 | const DexFile& lhs_dex_file = lhs->GetDeclaringClass<kWithoutReadBarrier>()->GetDexFile(); |
| 1225 | const DexFile& rhs_dex_file = rhs->GetDeclaringClass<kWithoutReadBarrier>()->GetDexFile(); |
| 1226 | const dex::MethodId& lhs_mid = lhs_dex_file.GetMethodId(lhs->GetDexMethodIndex()); |
| 1227 | const dex::MethodId& rhs_mid = rhs_dex_file.GetMethodId(rhs->GetDexMethodIndex()); |
| 1228 | if (&lhs_dex_file == &rhs_dex_file) { |
| 1229 | return lhs_mid.name_idx_ == rhs_mid.name_idx_ && |
| 1230 | lhs_mid.proto_idx_ == rhs_mid.proto_idx_; |
| 1231 | } else { |
| 1232 | return |
| 1233 | lhs_dex_file.GetMethodNameView(lhs_mid) == rhs_dex_file.GetMethodNameView(rhs_mid) && |
| 1234 | lhs_dex_file.GetMethodSignature(lhs_mid) == rhs_dex_file.GetMethodSignature(rhs_mid); |
| 1235 | } |
| 1236 | } |
| 1237 | |
Vladimir Marko | 4335474 | 2021-02-03 15:37:01 +0000 | [diff] [blame] | 1238 | static void InitializeObjectVirtualMethodHashes(ObjPtr<mirror::Class> java_lang_Object, |
| 1239 | PointerSize pointer_size, |
| 1240 | /*out*/ ArrayRef<uint32_t> virtual_method_hashes) |
| 1241 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1242 | ArraySlice<ArtMethod> virtual_methods = java_lang_Object->GetVirtualMethods(pointer_size); |
| 1243 | DCHECK_EQ(virtual_method_hashes.size(), virtual_methods.size()); |
| 1244 | for (size_t i = 0; i != virtual_method_hashes.size(); ++i) { |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 1245 | virtual_method_hashes[i] = ComputeMethodHash(&virtual_methods[i]); |
Vladimir Marko | 4335474 | 2021-02-03 15:37:01 +0000 | [diff] [blame] | 1246 | } |
| 1247 | } |
| 1248 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1249 | struct TrampolineCheckData { |
| 1250 | const void* quick_resolution_trampoline; |
| 1251 | const void* quick_imt_conflict_trampoline; |
| 1252 | const void* quick_generic_jni_trampoline; |
| 1253 | const void* quick_to_interpreter_bridge_trampoline; |
Nicolas Geoffray | c39af94 | 2021-01-25 08:43:57 +0000 | [diff] [blame] | 1254 | const void* nterp_trampoline; |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1255 | PointerSize pointer_size; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1256 | ArtMethod* m; |
| 1257 | bool error; |
| 1258 | }; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1259 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1260 | bool ClassLinker::InitFromBootImage(std::string* error_msg) { |
| 1261 | VLOG(startup) << __FUNCTION__ << " entering"; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1262 | CHECK(!init_done_); |
| 1263 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 1264 | Runtime* const runtime = Runtime::Current(); |
| 1265 | Thread* const self = Thread::Current(); |
| 1266 | gc::Heap* const heap = runtime->GetHeap(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1267 | std::vector<gc::space::ImageSpace*> spaces = heap->GetBootImageSpaces(); |
| 1268 | CHECK(!spaces.empty()); |
Vladimir Marko | 024d69f | 2019-06-13 10:52:32 +0100 | [diff] [blame] | 1269 | const ImageHeader& image_header = spaces[0]->GetImageHeader(); |
| 1270 | uint32_t pointer_size_unchecked = image_header.GetPointerSizeUnchecked(); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1271 | if (!ValidPointerSize(pointer_size_unchecked)) { |
| 1272 | *error_msg = StringPrintf("Invalid image pointer size: %u", pointer_size_unchecked); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1273 | return false; |
| 1274 | } |
Vladimir Marko | 3364d18 | 2019-03-13 13:55:01 +0000 | [diff] [blame] | 1275 | image_pointer_size_ = image_header.GetPointerSize(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1276 | if (!runtime->IsAotCompiler()) { |
| 1277 | // Only the Aot compiler supports having an image with a different pointer size than the |
| 1278 | // runtime. This happens on the host for compiling 32 bit tests since we use a 64 bit libart |
| 1279 | // 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] | 1280 | if (image_pointer_size_ != kRuntimePointerSize) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1281 | *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] | 1282 | static_cast<size_t>(image_pointer_size_), |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1283 | sizeof(void*)); |
| 1284 | return false; |
| 1285 | } |
| 1286 | } |
Vladimir Marko | 3364d18 | 2019-03-13 13:55:01 +0000 | [diff] [blame] | 1287 | DCHECK(!runtime->HasResolutionMethod()); |
| 1288 | runtime->SetResolutionMethod(image_header.GetImageMethod(ImageHeader::kResolutionMethod)); |
| 1289 | runtime->SetImtConflictMethod(image_header.GetImageMethod(ImageHeader::kImtConflictMethod)); |
| 1290 | runtime->SetImtUnimplementedMethod( |
| 1291 | image_header.GetImageMethod(ImageHeader::kImtUnimplementedMethod)); |
| 1292 | runtime->SetCalleeSaveMethod( |
| 1293 | image_header.GetImageMethod(ImageHeader::kSaveAllCalleeSavesMethod), |
| 1294 | CalleeSaveType::kSaveAllCalleeSaves); |
| 1295 | runtime->SetCalleeSaveMethod( |
| 1296 | image_header.GetImageMethod(ImageHeader::kSaveRefsOnlyMethod), |
| 1297 | CalleeSaveType::kSaveRefsOnly); |
| 1298 | runtime->SetCalleeSaveMethod( |
| 1299 | image_header.GetImageMethod(ImageHeader::kSaveRefsAndArgsMethod), |
| 1300 | CalleeSaveType::kSaveRefsAndArgs); |
| 1301 | runtime->SetCalleeSaveMethod( |
| 1302 | image_header.GetImageMethod(ImageHeader::kSaveEverythingMethod), |
| 1303 | CalleeSaveType::kSaveEverything); |
| 1304 | runtime->SetCalleeSaveMethod( |
| 1305 | image_header.GetImageMethod(ImageHeader::kSaveEverythingMethodForClinit), |
| 1306 | CalleeSaveType::kSaveEverythingForClinit); |
| 1307 | runtime->SetCalleeSaveMethod( |
| 1308 | image_header.GetImageMethod(ImageHeader::kSaveEverythingMethodForSuspendCheck), |
| 1309 | CalleeSaveType::kSaveEverythingForSuspendCheck); |
| 1310 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1311 | std::vector<const OatFile*> oat_files = |
| 1312 | runtime->GetOatFileManager().RegisterImageOatFiles(spaces); |
| 1313 | DCHECK(!oat_files.empty()); |
| 1314 | const OatHeader& default_oat_header = oat_files[0]->GetOatHeader(); |
Vladimir Marko | 7dac864 | 2019-11-06 17:09:30 +0000 | [diff] [blame] | 1315 | jni_dlsym_lookup_trampoline_ = default_oat_header.GetJniDlsymLookupTrampoline(); |
Vladimir Marko | fa458ac | 2020-02-12 14:08:07 +0000 | [diff] [blame] | 1316 | jni_dlsym_lookup_critical_trampoline_ = default_oat_header.GetJniDlsymLookupCriticalTrampoline(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1317 | quick_resolution_trampoline_ = default_oat_header.GetQuickResolutionTrampoline(); |
| 1318 | quick_imt_conflict_trampoline_ = default_oat_header.GetQuickImtConflictTrampoline(); |
| 1319 | quick_generic_jni_trampoline_ = default_oat_header.GetQuickGenericJniTrampoline(); |
| 1320 | quick_to_interpreter_bridge_trampoline_ = default_oat_header.GetQuickToInterpreterBridge(); |
Nicolas Geoffray | c39af94 | 2021-01-25 08:43:57 +0000 | [diff] [blame] | 1321 | nterp_trampoline_ = default_oat_header.GetNterpTrampoline(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1322 | if (kIsDebugBuild) { |
| 1323 | // Check that the other images use the same trampoline. |
| 1324 | for (size_t i = 1; i < oat_files.size(); ++i) { |
| 1325 | const OatHeader& ith_oat_header = oat_files[i]->GetOatHeader(); |
Vladimir Marko | 7dac864 | 2019-11-06 17:09:30 +0000 | [diff] [blame] | 1326 | const void* ith_jni_dlsym_lookup_trampoline_ = |
| 1327 | ith_oat_header.GetJniDlsymLookupTrampoline(); |
Vladimir Marko | fa458ac | 2020-02-12 14:08:07 +0000 | [diff] [blame] | 1328 | const void* ith_jni_dlsym_lookup_critical_trampoline_ = |
| 1329 | ith_oat_header.GetJniDlsymLookupCriticalTrampoline(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1330 | const void* ith_quick_resolution_trampoline = |
| 1331 | ith_oat_header.GetQuickResolutionTrampoline(); |
| 1332 | const void* ith_quick_imt_conflict_trampoline = |
| 1333 | ith_oat_header.GetQuickImtConflictTrampoline(); |
| 1334 | const void* ith_quick_generic_jni_trampoline = |
| 1335 | ith_oat_header.GetQuickGenericJniTrampoline(); |
| 1336 | const void* ith_quick_to_interpreter_bridge_trampoline = |
| 1337 | ith_oat_header.GetQuickToInterpreterBridge(); |
Nicolas Geoffray | c39af94 | 2021-01-25 08:43:57 +0000 | [diff] [blame] | 1338 | const void* ith_nterp_trampoline = |
| 1339 | ith_oat_header.GetNterpTrampoline(); |
Vladimir Marko | 7dac864 | 2019-11-06 17:09:30 +0000 | [diff] [blame] | 1340 | if (ith_jni_dlsym_lookup_trampoline_ != jni_dlsym_lookup_trampoline_ || |
Vladimir Marko | fa458ac | 2020-02-12 14:08:07 +0000 | [diff] [blame] | 1341 | ith_jni_dlsym_lookup_critical_trampoline_ != jni_dlsym_lookup_critical_trampoline_ || |
Vladimir Marko | 7dac864 | 2019-11-06 17:09:30 +0000 | [diff] [blame] | 1342 | ith_quick_resolution_trampoline != quick_resolution_trampoline_ || |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1343 | ith_quick_imt_conflict_trampoline != quick_imt_conflict_trampoline_ || |
| 1344 | ith_quick_generic_jni_trampoline != quick_generic_jni_trampoline_ || |
Nicolas Geoffray | c39af94 | 2021-01-25 08:43:57 +0000 | [diff] [blame] | 1345 | ith_quick_to_interpreter_bridge_trampoline != quick_to_interpreter_bridge_trampoline_ || |
| 1346 | ith_nterp_trampoline != nterp_trampoline_) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1347 | // Make sure that all methods in this image do not contain those trampolines as |
| 1348 | // entrypoints. Otherwise the class-linker won't be able to work with a single set. |
| 1349 | TrampolineCheckData data; |
| 1350 | data.error = false; |
| 1351 | data.pointer_size = GetImagePointerSize(); |
| 1352 | data.quick_resolution_trampoline = ith_quick_resolution_trampoline; |
| 1353 | data.quick_imt_conflict_trampoline = ith_quick_imt_conflict_trampoline; |
| 1354 | data.quick_generic_jni_trampoline = ith_quick_generic_jni_trampoline; |
| 1355 | data.quick_to_interpreter_bridge_trampoline = ith_quick_to_interpreter_bridge_trampoline; |
Nicolas Geoffray | c39af94 | 2021-01-25 08:43:57 +0000 | [diff] [blame] | 1356 | data.nterp_trampoline = ith_nterp_trampoline; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1357 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Andreas Gampe | 0c18338 | 2017-07-13 22:26:24 -0700 | [diff] [blame] | 1358 | auto visitor = [&](mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1359 | if (obj->IsClass()) { |
| 1360 | ObjPtr<mirror::Class> klass = obj->AsClass(); |
| 1361 | for (ArtMethod& m : klass->GetMethods(data.pointer_size)) { |
| 1362 | const void* entrypoint = |
| 1363 | m.GetEntryPointFromQuickCompiledCodePtrSize(data.pointer_size); |
| 1364 | if (entrypoint == data.quick_resolution_trampoline || |
| 1365 | entrypoint == data.quick_imt_conflict_trampoline || |
| 1366 | entrypoint == data.quick_generic_jni_trampoline || |
| 1367 | entrypoint == data.quick_to_interpreter_bridge_trampoline) { |
| 1368 | data.m = &m; |
| 1369 | data.error = true; |
| 1370 | return; |
| 1371 | } |
| 1372 | } |
| 1373 | } |
| 1374 | }; |
| 1375 | spaces[i]->GetLiveBitmap()->Walk(visitor); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1376 | if (data.error) { |
| 1377 | ArtMethod* m = data.m; |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1378 | LOG(ERROR) << "Found a broken ArtMethod: " << ArtMethod::PrettyMethod(m); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1379 | *error_msg = "Found an ArtMethod with a bad entrypoint"; |
| 1380 | return false; |
| 1381 | } |
| 1382 | } |
| 1383 | } |
| 1384 | } |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 1385 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1386 | class_roots_ = GcRoot<mirror::ObjectArray<mirror::Class>>( |
Vladimir Marko | d7e9bbf | 2019-03-28 13:18:57 +0000 | [diff] [blame] | 1387 | ObjPtr<mirror::ObjectArray<mirror::Class>>::DownCast( |
Vladimir Marko | 024d69f | 2019-06-13 10:52:32 +0100 | [diff] [blame] | 1388 | image_header.GetImageRoot(ImageHeader::kClassRoots))); |
Vladimir Marko | f75613c | 2018-06-05 12:51:04 +0100 | [diff] [blame] | 1389 | DCHECK_EQ(GetClassRoot<mirror::Class>(this)->GetClassFlags(), mirror::kClassFlagClass); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1390 | |
Vladimir Marko | 024d69f | 2019-06-13 10:52:32 +0100 | [diff] [blame] | 1391 | DCHECK_EQ(GetClassRoot<mirror::Object>(this)->GetObjectSize(), sizeof(mirror::Object)); |
| 1392 | ObjPtr<mirror::ObjectArray<mirror::Object>> boot_image_live_objects = |
| 1393 | ObjPtr<mirror::ObjectArray<mirror::Object>>::DownCast( |
| 1394 | image_header.GetImageRoot(ImageHeader::kBootImageLiveObjects)); |
| 1395 | runtime->SetSentinel(boot_image_live_objects->Get(ImageHeader::kClearedJniWeakSentinel)); |
| 1396 | DCHECK(runtime->GetSentinel().Read()->GetClass() == GetClassRoot<mirror::Object>(this)); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 1397 | |
Vladimir Marko | d190851 | 2018-11-22 14:57:28 +0000 | [diff] [blame] | 1398 | for (size_t i = 0u, size = spaces.size(); i != size; ++i) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1399 | // Boot class loader, use a null handle. |
| 1400 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
Vladimir Marko | d190851 | 2018-11-22 14:57:28 +0000 | [diff] [blame] | 1401 | if (!AddImageSpace(spaces[i], |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1402 | ScopedNullHandle<mirror::ClassLoader>(), |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1403 | /*out*/&dex_files, |
| 1404 | error_msg)) { |
| 1405 | return false; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1406 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1407 | // Append opened dex files at the end. |
| 1408 | boot_dex_files_.insert(boot_dex_files_.end(), |
| 1409 | std::make_move_iterator(dex_files.begin()), |
| 1410 | std::make_move_iterator(dex_files.end())); |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 1411 | } |
Mathieu Chartier | be8303d | 2017-08-17 17:39:39 -0700 | [diff] [blame] | 1412 | for (const std::unique_ptr<const DexFile>& dex_file : boot_dex_files_) { |
Jared Duke | 95bb995 | 2021-08-11 15:07:25 -0700 | [diff] [blame] | 1413 | OatDexFile::MadviseDexFileAtLoad(*dex_file); |
Mathieu Chartier | be8303d | 2017-08-17 17:39:39 -0700 | [diff] [blame] | 1414 | } |
Vladimir Marko | 4335474 | 2021-02-03 15:37:01 +0000 | [diff] [blame] | 1415 | InitializeObjectVirtualMethodHashes(GetClassRoot<mirror::Object>(this), |
| 1416 | image_pointer_size_, |
| 1417 | ArrayRef<uint32_t>(object_virtual_method_hashes_)); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1418 | FinishInit(self); |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 1419 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1420 | VLOG(startup) << __FUNCTION__ << " exiting"; |
| 1421 | return true; |
| 1422 | } |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 1423 | |
Vladimir Marko | 4433c43 | 2018-12-04 14:57:47 +0000 | [diff] [blame] | 1424 | void ClassLinker::AddExtraBootDexFiles( |
| 1425 | Thread* self, |
| 1426 | std::vector<std::unique_ptr<const DexFile>>&& additional_dex_files) { |
| 1427 | for (std::unique_ptr<const DexFile>& dex_file : additional_dex_files) { |
Mathieu Chartier | 0a19e21 | 2019-11-27 14:35:24 -0800 | [diff] [blame] | 1428 | AppendToBootClassPath(self, dex_file.get()); |
Orion Hodson | 771708f | 2021-01-06 15:45:16 +0000 | [diff] [blame] | 1429 | if (kIsDebugBuild) { |
| 1430 | for (const auto& boot_dex_file : boot_dex_files_) { |
| 1431 | DCHECK_NE(boot_dex_file->GetLocation(), dex_file->GetLocation()); |
| 1432 | } |
| 1433 | } |
Vladimir Marko | 4433c43 | 2018-12-04 14:57:47 +0000 | [diff] [blame] | 1434 | boot_dex_files_.push_back(std::move(dex_file)); |
| 1435 | } |
| 1436 | } |
| 1437 | |
Nicolas Geoffray | 5d3a23d | 2022-12-08 14:51:25 +0000 | [diff] [blame] | 1438 | bool ClassLinker::IsBootClassLoader(ObjPtr<mirror::Object> class_loader) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1439 | return class_loader == nullptr || |
Vladimir Marko | b6f965d | 2022-11-23 14:24:45 +0000 | [diff] [blame] | 1440 | WellKnownClasses::java_lang_BootClassLoader == class_loader->GetClass(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1441 | } |
| 1442 | |
Alexey Grebenkin | be4c2bd | 2018-02-01 19:09:59 +0300 | [diff] [blame] | 1443 | class CHAOnDeleteUpdateClassVisitor { |
| 1444 | public: |
| 1445 | explicit CHAOnDeleteUpdateClassVisitor(LinearAlloc* alloc) |
| 1446 | : allocator_(alloc), cha_(Runtime::Current()->GetClassLinker()->GetClassHierarchyAnalysis()), |
| 1447 | pointer_size_(Runtime::Current()->GetClassLinker()->GetImagePointerSize()), |
| 1448 | self_(Thread::Current()) {} |
| 1449 | |
| 1450 | bool operator()(ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1451 | // This class is going to be unloaded. Tell CHA about it. |
| 1452 | cha_->ResetSingleImplementationInHierarchy(klass, allocator_, pointer_size_); |
| 1453 | return true; |
| 1454 | } |
| 1455 | private: |
| 1456 | const LinearAlloc* allocator_; |
| 1457 | const ClassHierarchyAnalysis* cha_; |
| 1458 | const PointerSize pointer_size_; |
| 1459 | const Thread* self_; |
| 1460 | }; |
| 1461 | |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1462 | /* |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1463 | * A class used to ensure that all references to strings interned in an AppImage have been |
| 1464 | * 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] | 1465 | */ |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1466 | class CountInternedStringReferencesVisitor { |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1467 | public: |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1468 | CountInternedStringReferencesVisitor(const gc::space::ImageSpace& space, |
| 1469 | const InternTable::UnorderedSet& image_interns) |
| 1470 | : space_(space), |
| 1471 | image_interns_(image_interns), |
| 1472 | count_(0u) {} |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1473 | |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1474 | void TestObject(ObjPtr<mirror::Object> referred_obj) const |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1475 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1476 | if (referred_obj != nullptr && |
| 1477 | space_.HasAddress(referred_obj.Ptr()) && |
| 1478 | referred_obj->IsString()) { |
| 1479 | ObjPtr<mirror::String> referred_str = referred_obj->AsString(); |
Vladimir Marko | 365c020 | 2022-03-22 09:53:31 +0000 | [diff] [blame] | 1480 | uint32_t hash = static_cast<uint32_t>(referred_str->GetStoredHashCode()); |
| 1481 | // All image strings have the hash code calculated, even if they are not interned. |
| 1482 | DCHECK_EQ(hash, static_cast<uint32_t>(referred_str->ComputeHashCode())); |
| 1483 | auto it = image_interns_.FindWithHash(GcRoot<mirror::String>(referred_str), hash); |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1484 | if (it != image_interns_.end() && it->Read() == referred_str) { |
| 1485 | ++count_; |
Chris Wailes | fbeef46 | 2018-10-19 14:16:35 -0700 | [diff] [blame] | 1486 | } |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1487 | } |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1488 | } |
| 1489 | |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1490 | void VisitRootIfNonNull( |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1491 | mirror::CompressedReference<mirror::Object>* root) const |
| 1492 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1493 | if (!root->IsNull()) { |
| 1494 | VisitRoot(root); |
| 1495 | } |
| 1496 | } |
| 1497 | |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1498 | void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1499 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1500 | TestObject(root->AsMirrorPtr()); |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1501 | } |
| 1502 | |
| 1503 | // Visit Class Fields |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1504 | void operator()(ObjPtr<mirror::Object> obj, |
| 1505 | MemberOffset offset, |
| 1506 | bool is_static ATTRIBUTE_UNUSED) const |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1507 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1508 | // References within image or across images don't need a read barrier. |
| 1509 | ObjPtr<mirror::Object> referred_obj = |
| 1510 | obj->GetFieldObject<mirror::Object, kVerifyNone, kWithoutReadBarrier>(offset); |
| 1511 | TestObject(referred_obj); |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1512 | } |
| 1513 | |
| 1514 | void operator()(ObjPtr<mirror::Class> klass ATTRIBUTE_UNUSED, |
| 1515 | ObjPtr<mirror::Reference> ref) const |
| 1516 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_) { |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1517 | operator()(ref, mirror::Reference::ReferentOffset(), /*is_static=*/ false); |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1518 | } |
| 1519 | |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1520 | size_t GetCount() const { |
| 1521 | return count_; |
| 1522 | } |
| 1523 | |
| 1524 | private: |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1525 | const gc::space::ImageSpace& space_; |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1526 | const InternTable::UnorderedSet& image_interns_; |
| 1527 | mutable size_t count_; // Modified from the `const` callbacks. |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1528 | }; |
| 1529 | |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1530 | /* |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1531 | * This function counts references to strings interned in the AppImage. |
| 1532 | * 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] | 1533 | */ |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1534 | size_t CountInternedStringReferences(gc::space::ImageSpace& space, |
| 1535 | const InternTable::UnorderedSet& image_interns) |
| 1536 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1537 | const gc::accounting::ContinuousSpaceBitmap* bitmap = space.GetMarkBitmap(); |
| 1538 | const ImageHeader& image_header = space.GetImageHeader(); |
| 1539 | const uint8_t* target_base = space.GetMemMap()->Begin(); |
| 1540 | const ImageSection& objects_section = image_header.GetObjectsSection(); |
Chris Wailes | fbeef46 | 2018-10-19 14:16:35 -0700 | [diff] [blame] | 1541 | |
| 1542 | auto objects_begin = reinterpret_cast<uintptr_t>(target_base + objects_section.Offset()); |
| 1543 | auto objects_end = reinterpret_cast<uintptr_t>(target_base + objects_section.End()); |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1544 | |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1545 | CountInternedStringReferencesVisitor visitor(space, image_interns); |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1546 | bitmap->VisitMarkedRange(objects_begin, |
| 1547 | objects_end, |
| 1548 | [&space, &visitor](mirror::Object* obj) |
| 1549 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1550 | if (space.HasAddress(obj)) { |
| 1551 | if (obj->IsDexCache()) { |
Chris Wailes | fbeef46 | 2018-10-19 14:16:35 -0700 | [diff] [blame] | 1552 | obj->VisitReferences</* kVisitNativeRoots= */ true, |
| 1553 | kVerifyNone, |
| 1554 | kWithoutReadBarrier>(visitor, visitor); |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1555 | } else { |
| 1556 | // Don't visit native roots for non-dex-cache as they can't contain |
| 1557 | // native references to strings. This is verified during compilation |
| 1558 | // by ImageWriter::VerifyNativeGCRootInvariants. |
Chris Wailes | fbeef46 | 2018-10-19 14:16:35 -0700 | [diff] [blame] | 1559 | obj->VisitReferences</* kVisitNativeRoots= */ false, |
| 1560 | kVerifyNone, |
| 1561 | kWithoutReadBarrier>(visitor, visitor); |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1562 | } |
| 1563 | } |
| 1564 | }); |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1565 | return visitor.GetCount(); |
| 1566 | } |
| 1567 | |
| 1568 | template <typename Visitor> |
| 1569 | static void VisitInternedStringReferences( |
| 1570 | gc::space::ImageSpace* space, |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1571 | const Visitor& visitor) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1572 | const uint8_t* target_base = space->Begin(); |
| 1573 | const ImageSection& sro_section = |
| 1574 | space->GetImageHeader().GetImageStringReferenceOffsetsSection(); |
| 1575 | const size_t num_string_offsets = sro_section.Size() / sizeof(AppImageReferenceOffsetInfo); |
| 1576 | |
| 1577 | VLOG(image) |
| 1578 | << "ClassLinker:AppImage:InternStrings:imageStringReferenceOffsetCount = " |
| 1579 | << num_string_offsets; |
| 1580 | |
| 1581 | const auto* sro_base = |
| 1582 | reinterpret_cast<const AppImageReferenceOffsetInfo*>(target_base + sro_section.Offset()); |
| 1583 | |
| 1584 | for (size_t offset_index = 0; offset_index < num_string_offsets; ++offset_index) { |
| 1585 | uint32_t base_offset = sro_base[offset_index].first; |
| 1586 | |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 1587 | uint32_t raw_member_offset = sro_base[offset_index].second; |
| 1588 | DCHECK_ALIGNED(base_offset, 2); |
| 1589 | DCHECK_ALIGNED(raw_member_offset, 2); |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1590 | |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 1591 | ObjPtr<mirror::Object> obj_ptr = |
| 1592 | reinterpret_cast<mirror::Object*>(space->Begin() + base_offset); |
| 1593 | MemberOffset member_offset(raw_member_offset); |
| 1594 | ObjPtr<mirror::String> referred_string = |
| 1595 | obj_ptr->GetFieldObject<mirror::String, |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1596 | kVerifyNone, |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 1597 | kWithoutReadBarrier, |
| 1598 | /* kIsVolatile= */ false>(member_offset); |
| 1599 | DCHECK(referred_string != nullptr); |
| 1600 | |
| 1601 | ObjPtr<mirror::String> visited = visitor(referred_string); |
| 1602 | if (visited != referred_string) { |
| 1603 | obj_ptr->SetFieldObject</* kTransactionActive= */ false, |
| 1604 | /* kCheckTransaction= */ false, |
| 1605 | kVerifyNone, |
| 1606 | /* kIsVolatile= */ false>(member_offset, visited); |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1607 | } |
| 1608 | } |
| 1609 | } |
| 1610 | |
| 1611 | static void VerifyInternedStringReferences(gc::space::ImageSpace* space) |
| 1612 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1613 | InternTable::UnorderedSet image_interns; |
| 1614 | const ImageSection& section = space->GetImageHeader().GetInternedStringsSection(); |
| 1615 | if (section.Size() > 0) { |
| 1616 | size_t read_count; |
| 1617 | const uint8_t* data = space->Begin() + section.Offset(); |
| 1618 | InternTable::UnorderedSet image_set(data, /*make_copy_of_data=*/ false, &read_count); |
| 1619 | image_set.swap(image_interns); |
| 1620 | } |
| 1621 | size_t num_recorded_refs = 0u; |
| 1622 | VisitInternedStringReferences( |
| 1623 | space, |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1624 | [&image_interns, &num_recorded_refs](ObjPtr<mirror::String> str) |
| 1625 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1626 | auto it = image_interns.find(GcRoot<mirror::String>(str)); |
| 1627 | CHECK(it != image_interns.end()); |
| 1628 | CHECK(it->Read() == str); |
| 1629 | ++num_recorded_refs; |
| 1630 | return str; |
| 1631 | }); |
| 1632 | size_t num_found_refs = CountInternedStringReferences(*space, image_interns); |
| 1633 | CHECK_EQ(num_recorded_refs, num_found_refs); |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1634 | } |
| 1635 | |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1636 | // new_class_set is the set of classes that were read from the class table section in the image. |
| 1637 | // If there was no class table section, it is null. |
| 1638 | // 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] | 1639 | class AppImageLoadingHelper { |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1640 | public: |
Vladimir Marko | 0f3c700 | 2017-09-07 14:15:56 +0100 | [diff] [blame] | 1641 | static void Update( |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1642 | ClassLinker* class_linker, |
| 1643 | gc::space::ImageSpace* space, |
| 1644 | Handle<mirror::ClassLoader> class_loader, |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 1645 | Handle<mirror::ObjectArray<mirror::DexCache>> dex_caches) |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1646 | REQUIRES(!Locks::dex_lock_) |
| 1647 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 1648 | |
Chris Wailes | fbeef46 | 2018-10-19 14:16:35 -0700 | [diff] [blame] | 1649 | static void HandleAppImageStrings(gc::space::ImageSpace* space) |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 1650 | REQUIRES_SHARED(Locks::mutator_lock_); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1651 | }; |
| 1652 | |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 1653 | void AppImageLoadingHelper::Update( |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1654 | ClassLinker* class_linker, |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1655 | gc::space::ImageSpace* space, |
| 1656 | Handle<mirror::ClassLoader> class_loader, |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 1657 | Handle<mirror::ObjectArray<mirror::DexCache>> dex_caches) |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1658 | REQUIRES(!Locks::dex_lock_) |
| 1659 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Chris Wailes | 2386636 | 2018-08-22 16:16:58 -0700 | [diff] [blame] | 1660 | ScopedTrace app_image_timing("AppImage:Updating"); |
| 1661 | |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1662 | if (kIsDebugBuild && ClassLinker::kAppImageMayContainStrings) { |
| 1663 | // In debug build, verify the string references before applying |
| 1664 | // the Runtime::LoadAppImageStartupCache() option. |
| 1665 | VerifyInternedStringReferences(space); |
| 1666 | } |
| 1667 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1668 | Thread* const self = Thread::Current(); |
Mathieu Chartier | a88abfa | 2019-02-04 11:08:29 -0800 | [diff] [blame] | 1669 | Runtime* const runtime = Runtime::Current(); |
| 1670 | gc::Heap* const heap = runtime->GetHeap(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1671 | const ImageHeader& header = space->GetImageHeader(); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1672 | { |
Vladimir Marko | 0f3c700 | 2017-09-07 14:15:56 +0100 | [diff] [blame] | 1673 | // Register dex caches with the class loader. |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1674 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Alex Light | a9bbc08 | 2019-11-14 14:51:41 -0800 | [diff] [blame] | 1675 | for (auto dex_cache : dex_caches.Iterate<mirror::DexCache>()) { |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1676 | const DexFile* const dex_file = dex_cache->GetDexFile(); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1677 | { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 1678 | WriterMutexLock mu2(self, *Locks::dex_lock_); |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 1679 | CHECK(class_linker->FindDexCacheDataLocked(*dex_file) == nullptr); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1680 | class_linker->RegisterDexFileLocked(*dex_file, dex_cache, class_loader.Get()); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1681 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1682 | } |
Mathieu Chartier | a0b9521 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 1683 | } |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1684 | |
Mathieu Chartier | 0933cc5 | 2018-03-23 14:25:08 -0700 | [diff] [blame] | 1685 | if (ClassLinker::kAppImageMayContainStrings) { |
Chris Wailes | fbeef46 | 2018-10-19 14:16:35 -0700 | [diff] [blame] | 1686 | HandleAppImageStrings(space); |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1687 | } |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1688 | |
Mathieu Chartier | a0b9521 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 1689 | if (kVerifyArtMethodDeclaringClasses) { |
Chris Wailes | 2386636 | 2018-08-22 16:16:58 -0700 | [diff] [blame] | 1690 | ScopedTrace timing("AppImage:VerifyDeclaringClasses"); |
Mathieu Chartier | a0b9521 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 1691 | ReaderMutexLock rmu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 9d5956a | 2019-03-22 11:29:08 -0700 | [diff] [blame] | 1692 | gc::accounting::HeapBitmap* live_bitmap = heap->GetLiveBitmap(); |
| 1693 | header.VisitPackedArtMethods([&](ArtMethod& method) |
| 1694 | REQUIRES_SHARED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) { |
| 1695 | ObjPtr<mirror::Class> klass = method.GetDeclaringClassUnchecked(); |
| 1696 | if (klass != nullptr) { |
| 1697 | CHECK(live_bitmap->Test(klass.Ptr())) << "Image method has unmarked declaring class"; |
| 1698 | } |
| 1699 | }, space->Begin(), kRuntimePointerSize); |
Mathieu Chartier | 03c1dd9 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 1700 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1701 | } |
| 1702 | |
Chris Wailes | fbeef46 | 2018-10-19 14:16:35 -0700 | [diff] [blame] | 1703 | void AppImageLoadingHelper::HandleAppImageStrings(gc::space::ImageSpace* space) { |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 1704 | // Iterate over the string reference offsets stored in the image and intern |
| 1705 | // the strings they point to. |
| 1706 | ScopedTrace timing("AppImage:InternString"); |
| 1707 | |
Mathieu Chartier | a88abfa | 2019-02-04 11:08:29 -0800 | [diff] [blame] | 1708 | Runtime* const runtime = Runtime::Current(); |
| 1709 | InternTable* const intern_table = runtime->GetInternTable(); |
| 1710 | |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 1711 | // Add the intern table, removing any conflicts. For conflicts, store the new address in a map |
| 1712 | // for faster lookup. |
| 1713 | // TODO: Optimize with a bitmap or bloom filter |
| 1714 | SafeMap<mirror::String*, mirror::String*> intern_remap; |
Mathieu Chartier | a88abfa | 2019-02-04 11:08:29 -0800 | [diff] [blame] | 1715 | auto func = [&](InternTable::UnorderedSet& interns) |
Mathieu Chartier | 41c0808 | 2018-10-31 11:50:26 -0700 | [diff] [blame] | 1716 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 1717 | REQUIRES(Locks::intern_table_lock_) { |
Mathieu Chartier | 8fc7558 | 2018-11-01 14:21:33 -0700 | [diff] [blame] | 1718 | const size_t non_boot_image_strings = intern_table->CountInterns( |
| 1719 | /*visit_boot_images=*/false, |
| 1720 | /*visit_non_boot_images=*/true); |
Chris Wailes | fbeef46 | 2018-10-19 14:16:35 -0700 | [diff] [blame] | 1721 | VLOG(image) << "AppImage:stringsInInternTableSize = " << interns.size(); |
Mathieu Chartier | 8fc7558 | 2018-11-01 14:21:33 -0700 | [diff] [blame] | 1722 | VLOG(image) << "AppImage:nonBootImageInternStrings = " << non_boot_image_strings; |
| 1723 | // Visit the smaller of the two sets to compute the intersection. |
| 1724 | if (interns.size() < non_boot_image_strings) { |
| 1725 | for (auto it = interns.begin(); it != interns.end(); ) { |
| 1726 | ObjPtr<mirror::String> string = it->Read(); |
| 1727 | ObjPtr<mirror::String> existing = intern_table->LookupWeakLocked(string); |
| 1728 | if (existing == nullptr) { |
| 1729 | existing = intern_table->LookupStrongLocked(string); |
| 1730 | } |
| 1731 | if (existing != nullptr) { |
| 1732 | intern_remap.Put(string.Ptr(), existing.Ptr()); |
| 1733 | it = interns.erase(it); |
| 1734 | } else { |
| 1735 | ++it; |
| 1736 | } |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 1737 | } |
Mathieu Chartier | 8fc7558 | 2018-11-01 14:21:33 -0700 | [diff] [blame] | 1738 | } else { |
| 1739 | intern_table->VisitInterns([&](const GcRoot<mirror::String>& root) |
| 1740 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 1741 | REQUIRES(Locks::intern_table_lock_) { |
| 1742 | auto it = interns.find(root); |
| 1743 | if (it != interns.end()) { |
| 1744 | ObjPtr<mirror::String> existing = root.Read(); |
| 1745 | intern_remap.Put(it->Read(), existing.Ptr()); |
| 1746 | it = interns.erase(it); |
| 1747 | } |
| 1748 | }, /*visit_boot_images=*/false, /*visit_non_boot_images=*/true); |
| 1749 | } |
David Srbecky | 346fd96 | 2020-07-27 16:51:00 +0100 | [diff] [blame] | 1750 | // Consistency check to ensure correctness. |
Mathieu Chartier | 8fc7558 | 2018-11-01 14:21:33 -0700 | [diff] [blame] | 1751 | if (kIsDebugBuild) { |
| 1752 | for (GcRoot<mirror::String>& root : interns) { |
| 1753 | ObjPtr<mirror::String> string = root.Read(); |
| 1754 | CHECK(intern_table->LookupWeakLocked(string) == nullptr) << string->ToModifiedUtf8(); |
| 1755 | CHECK(intern_table->LookupStrongLocked(string) == nullptr) << string->ToModifiedUtf8(); |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 1756 | } |
| 1757 | } |
Mathieu Chartier | a88abfa | 2019-02-04 11:08:29 -0800 | [diff] [blame] | 1758 | }; |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1759 | intern_table->AddImageStringsToTable(space, func); |
| 1760 | if (!intern_remap.empty()) { |
Mathieu Chartier | a88abfa | 2019-02-04 11:08:29 -0800 | [diff] [blame] | 1761 | VLOG(image) << "AppImage:conflictingInternStrings = " << intern_remap.size(); |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1762 | VisitInternedStringReferences( |
| 1763 | space, |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1764 | [&intern_remap](ObjPtr<mirror::String> str) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1765 | auto it = intern_remap.find(str.Ptr()); |
| 1766 | if (it != intern_remap.end()) { |
| 1767 | return ObjPtr<mirror::String>(it->second); |
| 1768 | } |
| 1769 | return str; |
| 1770 | }); |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 1771 | } |
| 1772 | } |
| 1773 | |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1774 | static std::unique_ptr<const DexFile> OpenOatDexFile(const OatFile* oat_file, |
| 1775 | const char* location, |
| 1776 | std::string* error_msg) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1777 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1778 | DCHECK(error_msg != nullptr); |
| 1779 | std::unique_ptr<const DexFile> dex_file; |
Andreas Gampe | b40d361 | 2018-06-26 15:49:42 -0700 | [diff] [blame] | 1780 | const OatDexFile* oat_dex_file = oat_file->GetOatDexFile(location, nullptr, error_msg); |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1781 | if (oat_dex_file == nullptr) { |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1782 | return std::unique_ptr<const DexFile>(); |
| 1783 | } |
| 1784 | std::string inner_error_msg; |
| 1785 | dex_file = oat_dex_file->OpenDexFile(&inner_error_msg); |
| 1786 | if (dex_file == nullptr) { |
| 1787 | *error_msg = StringPrintf("Failed to open dex file %s from within oat file %s error '%s'", |
| 1788 | location, |
| 1789 | oat_file->GetLocation().c_str(), |
| 1790 | inner_error_msg.c_str()); |
| 1791 | return std::unique_ptr<const DexFile>(); |
| 1792 | } |
| 1793 | |
| 1794 | if (dex_file->GetLocationChecksum() != oat_dex_file->GetDexFileLocationChecksum()) { |
| 1795 | *error_msg = StringPrintf("Checksums do not match for %s: %x vs %x", |
| 1796 | location, |
| 1797 | dex_file->GetLocationChecksum(), |
| 1798 | oat_dex_file->GetDexFileLocationChecksum()); |
| 1799 | return std::unique_ptr<const DexFile>(); |
| 1800 | } |
| 1801 | return dex_file; |
| 1802 | } |
| 1803 | |
| 1804 | bool ClassLinker::OpenImageDexFiles(gc::space::ImageSpace* space, |
| 1805 | std::vector<std::unique_ptr<const DexFile>>* out_dex_files, |
| 1806 | std::string* error_msg) { |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 1807 | ScopedAssertNoThreadSuspension nts(__FUNCTION__); |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1808 | const ImageHeader& header = space->GetImageHeader(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1809 | ObjPtr<mirror::Object> dex_caches_object = header.GetImageRoot(ImageHeader::kDexCaches); |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1810 | DCHECK(dex_caches_object != nullptr); |
Vladimir Marko | 4617d58 | 2019-03-28 13:48:31 +0000 | [diff] [blame] | 1811 | ObjPtr<mirror::ObjectArray<mirror::DexCache>> dex_caches = |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1812 | dex_caches_object->AsObjectArray<mirror::DexCache>(); |
| 1813 | const OatFile* oat_file = space->GetOatFile(); |
Alex Light | a9bbc08 | 2019-11-14 14:51:41 -0800 | [diff] [blame] | 1814 | for (auto dex_cache : dex_caches->Iterate()) { |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1815 | std::string dex_file_location(dex_cache->GetLocation()->ToModifiedUtf8()); |
| 1816 | std::unique_ptr<const DexFile> dex_file = OpenOatDexFile(oat_file, |
| 1817 | dex_file_location.c_str(), |
| 1818 | error_msg); |
| 1819 | if (dex_file == nullptr) { |
| 1820 | return false; |
| 1821 | } |
| 1822 | dex_cache->SetDexFile(dex_file.get()); |
| 1823 | out_dex_files->push_back(std::move(dex_file)); |
| 1824 | } |
| 1825 | return true; |
| 1826 | } |
| 1827 | |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1828 | // Helper class for ArtMethod checks when adding an image. Keeps all required functionality |
| 1829 | // together and caches some intermediate results. |
Vladimir Marko | 76ba256 | 2022-10-12 11:27:58 +0000 | [diff] [blame] | 1830 | template <PointerSize kPointerSize> |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 1831 | class ImageChecker final { |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1832 | public: |
Vladimir Marko | 76ba256 | 2022-10-12 11:27:58 +0000 | [diff] [blame] | 1833 | static void CheckObjects(gc::Heap* heap, gc::space::ImageSpace* space) |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1834 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | 76ba256 | 2022-10-12 11:27:58 +0000 | [diff] [blame] | 1835 | // There can be no GC during boot image initialization, so we do not need read barriers. |
| 1836 | ScopedDebugDisallowReadBarriers sddrb(Thread::Current()); |
| 1837 | |
| 1838 | CHECK_EQ(kPointerSize, space->GetImageHeader().GetPointerSize()); |
| 1839 | const ImageSection& objects_section = space->GetImageHeader().GetObjectsSection(); |
| 1840 | uintptr_t space_begin = reinterpret_cast<uintptr_t>(space->Begin()); |
| 1841 | uintptr_t objects_begin = space_begin + objects_section.Offset(); |
| 1842 | uintptr_t objects_end = objects_begin + objects_section.Size(); |
| 1843 | ImageChecker ic(heap); |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 1844 | auto visitor = [&](mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1845 | DCHECK(obj != nullptr); |
Vladimir Marko | 76ba256 | 2022-10-12 11:27:58 +0000 | [diff] [blame] | 1846 | mirror::Class* obj_klass = obj->GetClass<kDefaultVerifyFlags, kWithoutReadBarrier>(); |
| 1847 | CHECK(obj_klass != nullptr) << "Null class in object " << obj; |
| 1848 | mirror::Class* class_class = obj_klass->GetClass<kDefaultVerifyFlags, kWithoutReadBarrier>(); |
| 1849 | CHECK(class_class != nullptr) << "Null class class " << obj; |
| 1850 | if (obj_klass == class_class) { |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 1851 | auto klass = obj->AsClass(); |
| 1852 | for (ArtField& field : klass->GetIFields()) { |
Vladimir Marko | 76ba256 | 2022-10-12 11:27:58 +0000 | [diff] [blame] | 1853 | CHECK_EQ(field.GetDeclaringClass<kWithoutReadBarrier>(), klass); |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 1854 | } |
| 1855 | for (ArtField& field : klass->GetSFields()) { |
Vladimir Marko | 76ba256 | 2022-10-12 11:27:58 +0000 | [diff] [blame] | 1856 | CHECK_EQ(field.GetDeclaringClass<kWithoutReadBarrier>(), klass); |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 1857 | } |
Vladimir Marko | 76ba256 | 2022-10-12 11:27:58 +0000 | [diff] [blame] | 1858 | for (ArtMethod& m : klass->GetMethods(kPointerSize)) { |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 1859 | ic.CheckArtMethod(&m, klass); |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 1860 | } |
Vladimir Marko | 76ba256 | 2022-10-12 11:27:58 +0000 | [diff] [blame] | 1861 | ObjPtr<mirror::PointerArray> vtable = |
| 1862 | klass->GetVTable<kDefaultVerifyFlags, kWithoutReadBarrier>(); |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 1863 | if (vtable != nullptr) { |
Vladimir Marko | 76ba256 | 2022-10-12 11:27:58 +0000 | [diff] [blame] | 1864 | ic.CheckArtMethodPointerArray(vtable); |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 1865 | } |
| 1866 | if (klass->ShouldHaveImt()) { |
Vladimir Marko | 76ba256 | 2022-10-12 11:27:58 +0000 | [diff] [blame] | 1867 | ImTable* imt = klass->GetImt(kPointerSize); |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 1868 | for (size_t i = 0; i < ImTable::kSize; ++i) { |
Vladimir Marko | 76ba256 | 2022-10-12 11:27:58 +0000 | [diff] [blame] | 1869 | ic.CheckArtMethod(imt->Get(i, kPointerSize), /*expected_class=*/ nullptr); |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 1870 | } |
| 1871 | } |
| 1872 | if (klass->ShouldHaveEmbeddedVTable()) { |
| 1873 | for (int32_t i = 0; i < klass->GetEmbeddedVTableLength(); ++i) { |
Vladimir Marko | 76ba256 | 2022-10-12 11:27:58 +0000 | [diff] [blame] | 1874 | ic.CheckArtMethod(klass->GetEmbeddedVTableEntry(i, kPointerSize), |
| 1875 | /*expected_class=*/ nullptr); |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 1876 | } |
| 1877 | } |
Vladimir Marko | 76ba256 | 2022-10-12 11:27:58 +0000 | [diff] [blame] | 1878 | ObjPtr<mirror::IfTable> iftable = |
| 1879 | klass->GetIfTable<kDefaultVerifyFlags, kWithoutReadBarrier>(); |
| 1880 | int32_t iftable_count = (iftable != nullptr) ? iftable->Count() : 0; |
| 1881 | for (int32_t i = 0; i < iftable_count; ++i) { |
| 1882 | ObjPtr<mirror::PointerArray> method_array = |
| 1883 | iftable->GetMethodArrayOrNull<kDefaultVerifyFlags, kWithoutReadBarrier>(i); |
| 1884 | if (method_array != nullptr) { |
| 1885 | ic.CheckArtMethodPointerArray(method_array); |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 1886 | } |
| 1887 | } |
| 1888 | } |
| 1889 | }; |
Vladimir Marko | 76ba256 | 2022-10-12 11:27:58 +0000 | [diff] [blame] | 1890 | space->GetLiveBitmap()->VisitMarkedRange(objects_begin, objects_end, visitor); |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1891 | } |
| 1892 | |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1893 | private: |
Vladimir Marko | 76ba256 | 2022-10-12 11:27:58 +0000 | [diff] [blame] | 1894 | explicit ImageChecker(gc::Heap* heap) { |
| 1895 | ArrayRef<gc::space::ImageSpace* const> spaces(heap->GetBootImageSpaces()); |
| 1896 | space_begin_.reserve(spaces.size()); |
| 1897 | for (gc::space::ImageSpace* space : spaces) { |
| 1898 | CHECK_EQ(static_cast<const void*>(space->Begin()), &space->GetImageHeader()); |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1899 | space_begin_.push_back(space->Begin()); |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1900 | } |
| 1901 | } |
| 1902 | |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 1903 | void CheckArtMethod(ArtMethod* m, ObjPtr<mirror::Class> expected_class) |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1904 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | 76ba256 | 2022-10-12 11:27:58 +0000 | [diff] [blame] | 1905 | ObjPtr<mirror::Class> declaring_class = m->GetDeclaringClassUnchecked<kWithoutReadBarrier>(); |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1906 | if (m->IsRuntimeMethod()) { |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1907 | CHECK(declaring_class == nullptr) << declaring_class << " " << m->PrettyMethod(); |
| 1908 | } else if (m->IsCopied()) { |
Vladimir Marko | 76ba256 | 2022-10-12 11:27:58 +0000 | [diff] [blame] | 1909 | CHECK(declaring_class != nullptr) << m->PrettyMethod(); |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1910 | } else if (expected_class != nullptr) { |
Vladimir Marko | 76ba256 | 2022-10-12 11:27:58 +0000 | [diff] [blame] | 1911 | CHECK_EQ(declaring_class, expected_class) << m->PrettyMethod(); |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1912 | } |
Vladimir Marko | 76ba256 | 2022-10-12 11:27:58 +0000 | [diff] [blame] | 1913 | bool contains = false; |
| 1914 | for (const uint8_t* begin : space_begin_) { |
| 1915 | const size_t offset = reinterpret_cast<uint8_t*>(m) - begin; |
| 1916 | const ImageHeader* header = reinterpret_cast<const ImageHeader*>(begin); |
| 1917 | if (header->GetMethodsSection().Contains(offset) || |
| 1918 | header->GetRuntimeMethodsSection().Contains(offset)) { |
| 1919 | contains = true; |
| 1920 | break; |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1921 | } |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1922 | } |
Vladimir Marko | 76ba256 | 2022-10-12 11:27:58 +0000 | [diff] [blame] | 1923 | CHECK(contains) << m << " not found"; |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1924 | } |
| 1925 | |
Vladimir Marko | 76ba256 | 2022-10-12 11:27:58 +0000 | [diff] [blame] | 1926 | void CheckArtMethodPointerArray(ObjPtr<mirror::PointerArray> arr) |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1927 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1928 | CHECK(arr != nullptr); |
| 1929 | for (int32_t j = 0; j < arr->GetLength(); ++j) { |
Vladimir Marko | 76ba256 | 2022-10-12 11:27:58 +0000 | [diff] [blame] | 1930 | auto* method = arr->GetElementPtrSize<ArtMethod*>(j, kPointerSize); |
| 1931 | CHECK(method != nullptr); |
| 1932 | CheckArtMethod(method, /*expected_class=*/ nullptr); |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1933 | } |
| 1934 | } |
| 1935 | |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1936 | std::vector<const uint8_t*> space_begin_; |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1937 | }; |
| 1938 | |
Andreas Gampe | be7af22 | 2017-07-25 09:57:28 -0700 | [diff] [blame] | 1939 | static void VerifyAppImage(const ImageHeader& header, |
| 1940 | const Handle<mirror::ClassLoader>& class_loader, |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 1941 | ClassTable* class_table, |
| 1942 | gc::space::ImageSpace* space) |
Andreas Gampe | be7af22 | 2017-07-25 09:57:28 -0700 | [diff] [blame] | 1943 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 9d5956a | 2019-03-22 11:29:08 -0700 | [diff] [blame] | 1944 | header.VisitPackedArtMethods([&](ArtMethod& method) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1945 | ObjPtr<mirror::Class> klass = method.GetDeclaringClass(); |
| 1946 | if (klass != nullptr && !Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(klass)) { |
| 1947 | CHECK_EQ(class_table->LookupByDescriptor(klass), klass) |
| 1948 | << mirror::Class::PrettyClass(klass); |
| 1949 | } |
| 1950 | }, space->Begin(), kRuntimePointerSize); |
Andreas Gampe | be7af22 | 2017-07-25 09:57:28 -0700 | [diff] [blame] | 1951 | { |
| 1952 | // Verify that all direct interfaces of classes in the class table are also resolved. |
| 1953 | std::vector<ObjPtr<mirror::Class>> classes; |
| 1954 | auto verify_direct_interfaces_in_table = [&](ObjPtr<mirror::Class> klass) |
| 1955 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1956 | if (!klass->IsPrimitive() && klass->GetClassLoader() == class_loader.Get()) { |
| 1957 | classes.push_back(klass); |
| 1958 | } |
| 1959 | return true; |
| 1960 | }; |
| 1961 | class_table->Visit(verify_direct_interfaces_in_table); |
Andreas Gampe | be7af22 | 2017-07-25 09:57:28 -0700 | [diff] [blame] | 1962 | for (ObjPtr<mirror::Class> klass : classes) { |
| 1963 | for (uint32_t i = 0, num = klass->NumDirectInterfaces(); i != num; ++i) { |
Vladimir Marko | b10668c | 2021-06-10 09:52:53 +0100 | [diff] [blame] | 1964 | CHECK(klass->GetDirectInterface(i) != nullptr) |
Andreas Gampe | be7af22 | 2017-07-25 09:57:28 -0700 | [diff] [blame] | 1965 | << klass->PrettyDescriptor() << " iface #" << i; |
| 1966 | } |
| 1967 | } |
| 1968 | } |
Andreas Gampe | be7af22 | 2017-07-25 09:57:28 -0700 | [diff] [blame] | 1969 | } |
| 1970 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1971 | bool ClassLinker::AddImageSpace( |
| 1972 | gc::space::ImageSpace* space, |
| 1973 | Handle<mirror::ClassLoader> class_loader, |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1974 | std::vector<std::unique_ptr<const DexFile>>* out_dex_files, |
| 1975 | std::string* error_msg) { |
| 1976 | DCHECK(out_dex_files != nullptr); |
| 1977 | DCHECK(error_msg != nullptr); |
| 1978 | const uint64_t start_time = NanoTime(); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 1979 | const bool app_image = class_loader != nullptr; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1980 | const ImageHeader& header = space->GetImageHeader(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1981 | ObjPtr<mirror::Object> dex_caches_object = header.GetImageRoot(ImageHeader::kDexCaches); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1982 | DCHECK(dex_caches_object != nullptr); |
| 1983 | Runtime* const runtime = Runtime::Current(); |
| 1984 | gc::Heap* const heap = runtime->GetHeap(); |
| 1985 | Thread* const self = Thread::Current(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1986 | // Check that the image is what we are expecting. |
| 1987 | if (image_pointer_size_ != space->GetImageHeader().GetPointerSize()) { |
| 1988 | *error_msg = StringPrintf("Application image pointer size does not match runtime: %zu vs %zu", |
| 1989 | static_cast<size_t>(space->GetImageHeader().GetPointerSize()), |
| 1990 | image_pointer_size_); |
| 1991 | return false; |
| 1992 | } |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1993 | size_t expected_image_roots = ImageHeader::NumberOfImageRoots(app_image); |
| 1994 | if (static_cast<size_t>(header.GetImageRoots()->GetLength()) != expected_image_roots) { |
| 1995 | *error_msg = StringPrintf("Expected %zu image roots but got %d", |
| 1996 | expected_image_roots, |
| 1997 | header.GetImageRoots()->GetLength()); |
| 1998 | return false; |
| 1999 | } |
| 2000 | StackHandleScope<3> hs(self); |
| 2001 | Handle<mirror::ObjectArray<mirror::DexCache>> dex_caches( |
| 2002 | hs.NewHandle(dex_caches_object->AsObjectArray<mirror::DexCache>())); |
| 2003 | Handle<mirror::ObjectArray<mirror::Class>> class_roots(hs.NewHandle( |
| 2004 | header.GetImageRoot(ImageHeader::kClassRoots)->AsObjectArray<mirror::Class>())); |
Nicolas Geoffray | 5d3a23d | 2022-12-08 14:51:25 +0000 | [diff] [blame] | 2005 | MutableHandle<mirror::Object> special_root(hs.NewHandle( |
| 2006 | app_image ? header.GetImageRoot(ImageHeader::kSpecialRoots) : nullptr)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 2007 | DCHECK(class_roots != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 2008 | if (class_roots->GetLength() != static_cast<int32_t>(ClassRoot::kMax)) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2009 | *error_msg = StringPrintf("Expected %d class roots but got %d", |
| 2010 | class_roots->GetLength(), |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 2011 | static_cast<int32_t>(ClassRoot::kMax)); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2012 | return false; |
| 2013 | } |
| 2014 | // 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] | 2015 | ObjPtr<mirror::ObjectArray<mirror::Class>> existing_class_roots = GetClassRoots(); |
| 2016 | for (size_t i = 0; i < static_cast<size_t>(ClassRoot::kMax); i++) { |
| 2017 | 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] | 2018 | *error_msg = "App image class roots must have pointer equality with runtime ones."; |
| 2019 | return false; |
| 2020 | } |
| 2021 | } |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 2022 | const OatFile* oat_file = space->GetOatFile(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2023 | if (oat_file->GetOatHeader().GetDexFileCount() != |
| 2024 | static_cast<uint32_t>(dex_caches->GetLength())) { |
| 2025 | *error_msg = "Dex cache count and dex file count mismatch while trying to initialize from " |
| 2026 | "image"; |
| 2027 | return false; |
| 2028 | } |
| 2029 | |
Alex Light | a9bbc08 | 2019-11-14 14:51:41 -0800 | [diff] [blame] | 2030 | for (auto dex_cache : dex_caches.Iterate<mirror::DexCache>()) { |
David Brazdil | 3e8aae0 | 2019-03-26 18:48:02 +0000 | [diff] [blame] | 2031 | std::string dex_file_location = dex_cache->GetLocation()->ToModifiedUtf8(); |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 2032 | std::unique_ptr<const DexFile> dex_file = OpenOatDexFile(oat_file, |
| 2033 | dex_file_location.c_str(), |
| 2034 | error_msg); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2035 | if (dex_file == nullptr) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2036 | return false; |
| 2037 | } |
| 2038 | |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 2039 | { |
David Srbecky | 33df0e3 | 2021-09-30 14:36:32 +0000 | [diff] [blame] | 2040 | // Native fields are all null. Initialize them. |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 2041 | WriterMutexLock mu(self, *Locks::dex_lock_); |
David Srbecky | 33df0e3 | 2021-09-30 14:36:32 +0000 | [diff] [blame] | 2042 | dex_cache->Initialize(dex_file.get(), class_loader.Get()); |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 2043 | } |
| 2044 | if (!app_image) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2045 | // Register dex files, keep track of existing ones that are conflicts. |
Mathieu Chartier | 0a19e21 | 2019-11-27 14:35:24 -0800 | [diff] [blame] | 2046 | AppendToBootClassPath(dex_file.get(), dex_cache); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2047 | } |
| 2048 | out_dex_files->push_back(std::move(dex_file)); |
| 2049 | } |
| 2050 | |
| 2051 | if (app_image) { |
Vladimir Marko | 4e0b5d7 | 2022-11-09 10:58:15 +0000 | [diff] [blame] | 2052 | ScopedAssertNoThreadSuspension sants("Checking app image"); |
Nicolas Geoffray | 5d3a23d | 2022-12-08 14:51:25 +0000 | [diff] [blame] | 2053 | if (special_root == nullptr) { |
| 2054 | *error_msg = "Unexpected null special root in app image"; |
| 2055 | return false; |
| 2056 | } else if (special_root->IsIntArray()) { |
| 2057 | size_t count = special_root->AsIntArray()->GetLength(); |
| 2058 | if (oat_file->GetVdexFile()->GetNumberOfDexFiles() != count) { |
Vladimir Marko | d80bbba | 2022-12-20 16:25:58 +0000 | [diff] [blame] | 2059 | *error_msg = "Checksums count does not match"; |
Nicolas Geoffray | 5d3a23d | 2022-12-08 14:51:25 +0000 | [diff] [blame] | 2060 | return false; |
| 2061 | } |
| 2062 | static_assert(sizeof(VdexFile::VdexChecksum) == sizeof(int32_t)); |
| 2063 | const VdexFile::VdexChecksum* art_checksums = |
| 2064 | reinterpret_cast<VdexFile::VdexChecksum*>(special_root->AsIntArray()->GetData()); |
| 2065 | const VdexFile::VdexChecksum* vdex_checksums = |
| 2066 | oat_file->GetVdexFile()->GetDexChecksumsArray(); |
| 2067 | if (memcmp(art_checksums, vdex_checksums, sizeof(VdexFile::VdexChecksum) * count) != 0) { |
Vladimir Marko | d80bbba | 2022-12-20 16:25:58 +0000 | [diff] [blame] | 2068 | *error_msg = "Image and vdex checksums did not match"; |
Nicolas Geoffray | 5d3a23d | 2022-12-08 14:51:25 +0000 | [diff] [blame] | 2069 | return false; |
| 2070 | } |
| 2071 | } else if (IsBootClassLoader(special_root.Get())) { |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 2072 | *error_msg = "Unexpected BootClassLoader in app image"; |
| 2073 | return false; |
Nicolas Geoffray | 5d3a23d | 2022-12-08 14:51:25 +0000 | [diff] [blame] | 2074 | } else if (!special_root->IsClassLoader()) { |
| 2075 | *error_msg = "Unexpected special root in app image"; |
| 2076 | return false; |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 2077 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2078 | } |
| 2079 | |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 2080 | if (kCheckImageObjects) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2081 | if (!app_image) { |
Vladimir Marko | 76ba256 | 2022-10-12 11:27:58 +0000 | [diff] [blame] | 2082 | if (image_pointer_size_ == PointerSize::k64) { |
| 2083 | ImageChecker<PointerSize::k64>::CheckObjects(heap, space); |
| 2084 | } else { |
| 2085 | ImageChecker<PointerSize::k32>::CheckObjects(heap, space); |
| 2086 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2087 | } |
| 2088 | } |
| 2089 | |
| 2090 | // Set entry point to interpreter if in InterpretOnly mode. |
| 2091 | if (!runtime->IsAotCompiler() && runtime->GetInstrumentation()->InterpretOnly()) { |
Mathieu Chartier | 9d5956a | 2019-03-22 11:29:08 -0700 | [diff] [blame] | 2092 | // Set image methods' entry point to interpreter. |
| 2093 | header.VisitPackedArtMethods([&](ArtMethod& method) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 2094 | if (!method.IsRuntimeMethod()) { |
| 2095 | DCHECK(method.GetDeclaringClass() != nullptr); |
Ulya Trafimovich | 5439f05 | 2020-07-29 10:03:46 +0100 | [diff] [blame] | 2096 | if (!method.IsNative() && !method.IsResolutionMethod()) { |
| 2097 | method.SetEntryPointFromQuickCompiledCodePtrSize(GetQuickToInterpreterBridge(), |
| 2098 | image_pointer_size_); |
Mathieu Chartier | 9d5956a | 2019-03-22 11:29:08 -0700 | [diff] [blame] | 2099 | } |
| 2100 | } |
| 2101 | }, space->Begin(), image_pointer_size_); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2102 | } |
| 2103 | |
Nicolas Geoffray | 4717175 | 2020-08-31 15:03:20 +0100 | [diff] [blame] | 2104 | if (!runtime->IsAotCompiler()) { |
Nicolas Geoffray | 8bf5afd | 2022-04-27 14:11:23 +0100 | [diff] [blame] | 2105 | // If we are profiling the boot classpath, disable the shared memory for |
| 2106 | // boot image method optimization. We need to disable it before doing |
| 2107 | // ResetCounter below, as counters of shared memory method always hold the |
| 2108 | // "hot" value. |
| 2109 | if (runtime->GetJITOptions()->GetProfileSaverOptions().GetProfileBootClassPath()) { |
| 2110 | header.VisitPackedArtMethods([&](ArtMethod& method) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 2111 | method.ClearMemorySharedMethod(); |
| 2112 | }, space->Begin(), image_pointer_size_); |
| 2113 | } |
| 2114 | |
Nicolas Geoffray | bd728b0 | 2021-01-27 13:21:35 +0000 | [diff] [blame] | 2115 | ScopedTrace trace("AppImage:UpdateCodeItemAndNterp"); |
Nicolas Geoffray | 4717175 | 2020-08-31 15:03:20 +0100 | [diff] [blame] | 2116 | bool can_use_nterp = interpreter::CanRuntimeUseNterp(); |
Nicolas Geoffray | 58f916c | 2021-11-15 14:02:07 +0000 | [diff] [blame] | 2117 | uint16_t hotness_threshold = runtime->GetJITOptions()->GetWarmupThreshold(); |
Nicolas Geoffray | 7e2c963 | 2020-01-09 13:41:10 +0000 | [diff] [blame] | 2118 | header.VisitPackedArtMethods([&](ArtMethod& method) REQUIRES_SHARED(Locks::mutator_lock_) { |
Nicolas Geoffray | 4717175 | 2020-08-31 15:03:20 +0100 | [diff] [blame] | 2119 | // In the image, the `data` pointer field of the ArtMethod contains the code |
| 2120 | // item offset. Change this to the actual pointer to the code item. |
| 2121 | if (method.HasCodeItem()) { |
| 2122 | const dex::CodeItem* code_item = method.GetDexFile()->GetCodeItem( |
| 2123 | reinterpret_cast32<uint32_t>(method.GetDataPtrSize(image_pointer_size_))); |
zhaoxuyang | 7156ea2 | 2022-01-10 13:58:11 +0800 | [diff] [blame] | 2124 | method.SetCodeItem(code_item, method.GetDexFile()->IsCompactDexFile()); |
Nicolas Geoffray | 61673dc | 2021-11-06 13:58:31 +0000 | [diff] [blame] | 2125 | // The hotness counter may have changed since we compiled the image, so |
| 2126 | // reset it with the runtime value. |
Nicolas Geoffray | 58f916c | 2021-11-15 14:02:07 +0000 | [diff] [blame] | 2127 | method.ResetCounter(hotness_threshold); |
Nicolas Geoffray | 4717175 | 2020-08-31 15:03:20 +0100 | [diff] [blame] | 2128 | } |
Nicolas Geoffray | c39af94 | 2021-01-25 08:43:57 +0000 | [diff] [blame] | 2129 | if (method.GetEntryPointFromQuickCompiledCode() == nterp_trampoline_) { |
| 2130 | if (can_use_nterp) { |
Nicolas Geoffray | c8a694d | 2022-01-17 17:12:38 +0000 | [diff] [blame] | 2131 | // Set image methods' entry point that point to the nterp trampoline to the |
| 2132 | // nterp entry point. This allows taking the fast path when doing a |
| 2133 | // nterp->nterp call. |
Vladimir Marko | a20ec9b | 2022-12-12 09:12:21 +0000 | [diff] [blame] | 2134 | DCHECK(!method.StillNeedsClinitCheck()); |
Nicolas Geoffray | c39af94 | 2021-01-25 08:43:57 +0000 | [diff] [blame] | 2135 | method.SetEntryPointFromQuickCompiledCode(interpreter::GetNterpEntryPoint()); |
| 2136 | } else { |
| 2137 | method.SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge()); |
| 2138 | } |
Nicolas Geoffray | 4717175 | 2020-08-31 15:03:20 +0100 | [diff] [blame] | 2139 | } |
Nicolas Geoffray | 7e2c963 | 2020-01-09 13:41:10 +0000 | [diff] [blame] | 2140 | }, space->Begin(), image_pointer_size_); |
| 2141 | } |
| 2142 | |
Nicolas Geoffray | 8c41a0b | 2020-02-06 16:52:11 +0000 | [diff] [blame] | 2143 | if (runtime->IsVerificationSoftFail()) { |
| 2144 | header.VisitPackedArtMethods([&](ArtMethod& method) REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | ff8f8c4 | 2022-09-19 17:18:24 +0000 | [diff] [blame] | 2145 | if (method.IsManagedAndInvokable()) { |
Nicolas Geoffray | 8c41a0b | 2020-02-06 16:52:11 +0000 | [diff] [blame] | 2146 | method.ClearSkipAccessChecks(); |
| 2147 | } |
| 2148 | }, space->Begin(), image_pointer_size_); |
| 2149 | } |
| 2150 | |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 2151 | ClassTable* class_table = nullptr; |
| 2152 | { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2153 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 2154 | class_table = InsertClassTableForClassLoader(class_loader.Get()); |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 2155 | } |
| 2156 | // If we have a class table section, read it and use it for verification in |
| 2157 | // UpdateAppImageClassLoadersAndDexCaches. |
| 2158 | ClassTable::ClassSet temp_set; |
Vladimir Marko | 0f3c700 | 2017-09-07 14:15:56 +0100 | [diff] [blame] | 2159 | const ImageSection& class_table_section = header.GetClassTableSection(); |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 2160 | const bool added_class_table = class_table_section.Size() > 0u; |
| 2161 | if (added_class_table) { |
| 2162 | const uint64_t start_time2 = NanoTime(); |
| 2163 | size_t read_count = 0; |
| 2164 | temp_set = ClassTable::ClassSet(space->Begin() + class_table_section.Offset(), |
| 2165 | /*make copy*/false, |
| 2166 | &read_count); |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 2167 | VLOG(image) << "Adding class table classes took " << PrettyDuration(NanoTime() - start_time2); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2168 | } |
| 2169 | if (app_image) { |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 2170 | AppImageLoadingHelper::Update(this, space, class_loader, dex_caches); |
Mathieu Chartier | 456b492 | 2018-11-06 10:35:48 -0800 | [diff] [blame] | 2171 | |
| 2172 | { |
| 2173 | ScopedTrace trace("AppImage:UpdateClassLoaders"); |
| 2174 | // Update class loader and resolved strings. If added_class_table is false, the resolved |
| 2175 | // strings were forwarded UpdateAppImageClassLoadersAndDexCaches. |
Mathieu Chartier | bc1e0fa | 2018-11-14 16:18:18 -0800 | [diff] [blame] | 2176 | ObjPtr<mirror::ClassLoader> loader(class_loader.Get()); |
Mathieu Chartier | 456b492 | 2018-11-06 10:35:48 -0800 | [diff] [blame] | 2177 | for (const ClassTable::TableSlot& root : temp_set) { |
Mathieu Chartier | bc1e0fa | 2018-11-14 16:18:18 -0800 | [diff] [blame] | 2178 | // Note: We probably don't need the read barrier unless we copy the app image objects into |
| 2179 | // the region space. |
| 2180 | ObjPtr<mirror::Class> klass(root.Read()); |
| 2181 | // Do not update class loader for boot image classes where the app image |
| 2182 | // class loader is only the initiating loader but not the defining loader. |
| 2183 | // Avoid read barrier since we are comparing against null. |
| 2184 | if (klass->GetClassLoader<kDefaultVerifyFlags, kWithoutReadBarrier>() != nullptr) { |
Vladimir Marko | b68bb7a | 2020-03-17 10:55:25 +0000 | [diff] [blame] | 2185 | klass->SetClassLoader(loader); |
Mathieu Chartier | bc1e0fa | 2018-11-14 16:18:18 -0800 | [diff] [blame] | 2186 | } |
Mathieu Chartier | 456b492 | 2018-11-06 10:35:48 -0800 | [diff] [blame] | 2187 | } |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 2188 | } |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 2189 | |
Vladimir Marko | 305c38b | 2018-02-14 11:50:07 +0000 | [diff] [blame] | 2190 | if (kBitstringSubtypeCheckEnabled) { |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 2191 | // Every class in the app image has initially SubtypeCheckInfo in the |
| 2192 | // Uninitialized state. |
| 2193 | // |
| 2194 | // The SubtypeCheck invariants imply that a SubtypeCheckInfo is at least Initialized |
| 2195 | // after class initialization is complete. The app image ClassStatus as-is |
| 2196 | // are almost all ClassStatus::Initialized, and being in the |
| 2197 | // SubtypeCheckInfo::kUninitialized state is violating that invariant. |
| 2198 | // |
| 2199 | // Force every app image class's SubtypeCheck to be at least kIninitialized. |
| 2200 | // |
| 2201 | // See also ImageWriter::FixupClass. |
Chris Wailes | 2386636 | 2018-08-22 16:16:58 -0700 | [diff] [blame] | 2202 | ScopedTrace trace("AppImage:RecacluateSubtypeCheckBitstrings"); |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 2203 | MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_); |
| 2204 | for (const ClassTable::TableSlot& root : temp_set) { |
Vladimir Marko | 38b8b25 | 2018-01-02 19:07:06 +0000 | [diff] [blame] | 2205 | SubtypeCheck<ObjPtr<mirror::Class>>::EnsureInitialized(root.Read()); |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 2206 | } |
| 2207 | } |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 2208 | } |
| 2209 | if (!oat_file->GetBssGcRoots().empty()) { |
| 2210 | // Insert oat file to class table for visiting .bss GC roots. |
| 2211 | class_table->InsertOatFile(oat_file); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2212 | } |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 2213 | |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 2214 | if (added_class_table) { |
| 2215 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 2216 | class_table->AddClassSet(std::move(temp_set)); |
| 2217 | } |
Andreas Gampe | be7af22 | 2017-07-25 09:57:28 -0700 | [diff] [blame] | 2218 | |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 2219 | if (kIsDebugBuild && app_image) { |
| 2220 | // This verification needs to happen after the classes have been added to the class loader. |
| 2221 | // Since it ensures classes are in the class table. |
Chris Wailes | 2386636 | 2018-08-22 16:16:58 -0700 | [diff] [blame] | 2222 | ScopedTrace trace("AppImage:Verify"); |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 2223 | VerifyAppImage(header, class_loader, class_table, space); |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 2224 | } |
Andreas Gampe | be7af22 | 2017-07-25 09:57:28 -0700 | [diff] [blame] | 2225 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2226 | VLOG(class_linker) << "Adding image space took " << PrettyDuration(NanoTime() - start_time); |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 2227 | return true; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 2228 | } |
| 2229 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 2230 | void ClassLinker::VisitClassRoots(RootVisitor* visitor, VisitRootFlags flags) { |
Mathieu Chartier | 7778b88 | 2015-10-05 16:41:10 -0700 | [diff] [blame] | 2231 | // Acquire tracing_enabled before locking class linker lock to prevent lock order violation. Since |
| 2232 | // enabling tracing requires the mutator lock, there are no race conditions here. |
| 2233 | const bool tracing_enabled = Trace::IsTracingEnabled(); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2234 | Thread* const self = Thread::Current(); |
| 2235 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Lokesh Gidra | ca5ed9f | 2022-04-20 01:39:28 +0000 | [diff] [blame] | 2236 | if (gUseReadBarrier) { |
Vladimir Marko | 9b03cb4 | 2017-02-16 16:37:03 +0000 | [diff] [blame] | 2237 | // We do not track new roots for CC. |
| 2238 | DCHECK_EQ(0, flags & (kVisitRootFlagNewRoots | |
| 2239 | kVisitRootFlagClearRootLog | |
| 2240 | kVisitRootFlagStartLoggingNewRoots | |
| 2241 | kVisitRootFlagStopLoggingNewRoots)); |
| 2242 | } |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2243 | if ((flags & kVisitRootFlagAllRoots) != 0) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2244 | // Argument for how root visiting deals with ArtField and ArtMethod roots. |
| 2245 | // There is 3 GC cases to handle: |
| 2246 | // Non moving concurrent: |
| 2247 | // 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] | 2248 | // live by the class and class roots. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2249 | // |
| 2250 | // Moving non-concurrent: |
| 2251 | // This case needs to call visit VisitNativeRoots in case the classes or dex cache arrays move. |
| 2252 | // To prevent missing roots, this case needs to ensure that there is no |
| 2253 | // suspend points between the point which we allocate ArtMethod arrays and place them in a |
| 2254 | // class which is in the class table. |
| 2255 | // |
| 2256 | // Moving concurrent: |
| 2257 | // Need to make sure to not copy ArtMethods without doing read barriers since the roots are |
| 2258 | // 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] | 2259 | // |
| 2260 | // Use an unbuffered visitor since the class table uses a temporary GcRoot for holding decoded |
| 2261 | // ClassTable::TableSlot. The buffered root visiting would access a stale stack location for |
| 2262 | // these objects. |
| 2263 | UnbufferedRootVisitor root_visitor(visitor, RootInfo(kRootStickyClass)); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 2264 | boot_class_table_->VisitRoots(root_visitor); |
Mathieu Chartier | 7778b88 | 2015-10-05 16:41:10 -0700 | [diff] [blame] | 2265 | // 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] | 2266 | if ((flags & kVisitRootFlagClassLoader) != 0 || tracing_enabled) { |
Lokesh Gidra | f3c44b4 | 2022-10-05 10:43:49 -0700 | [diff] [blame] | 2267 | gc::Heap* const heap = Runtime::Current()->GetHeap(); |
| 2268 | // Don't visit class-loaders if compacting with userfaultfd GC as these |
| 2269 | // weaks are updated using Runtime::SweepSystemWeaks() and the GC doesn't |
| 2270 | // tolerate double updates. |
| 2271 | if (!gUseUserfaultfd |
| 2272 | || !heap->MarkCompactCollector()->IsCompacting(self)) { |
| 2273 | for (const ClassLoaderData& data : class_loaders_) { |
| 2274 | GcRoot<mirror::Object> root(GcRoot<mirror::Object>(self->DecodeJObject(data.weak_root))); |
| 2275 | root.VisitRoot(visitor, RootInfo(kRootVMInternal)); |
| 2276 | } |
| 2277 | } else { |
| 2278 | DCHECK_EQ(heap->CurrentCollectorType(), gc::CollectorType::kCollectorTypeCMC); |
Mathieu Chartier | 7778b88 | 2015-10-05 16:41:10 -0700 | [diff] [blame] | 2279 | } |
| 2280 | } |
Lokesh Gidra | ca5ed9f | 2022-04-20 01:39:28 +0000 | [diff] [blame] | 2281 | } else if (!gUseReadBarrier && (flags & kVisitRootFlagNewRoots) != 0) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 2282 | for (auto& root : new_class_roots_) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2283 | ObjPtr<mirror::Class> old_ref = root.Read<kWithoutReadBarrier>(); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 2284 | root.VisitRoot(visitor, RootInfo(kRootStickyClass)); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2285 | ObjPtr<mirror::Class> new_ref = root.Read<kWithoutReadBarrier>(); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 2286 | // Concurrent moving GC marked new roots through the to-space invariant. |
| 2287 | CHECK_EQ(new_ref, old_ref); |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2288 | } |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 2289 | for (const OatFile* oat_file : new_bss_roots_boot_oat_files_) { |
| 2290 | for (GcRoot<mirror::Object>& root : oat_file->GetBssGcRoots()) { |
| 2291 | ObjPtr<mirror::Object> old_ref = root.Read<kWithoutReadBarrier>(); |
| 2292 | if (old_ref != nullptr) { |
| 2293 | DCHECK(old_ref->IsClass()); |
| 2294 | root.VisitRoot(visitor, RootInfo(kRootStickyClass)); |
| 2295 | ObjPtr<mirror::Object> new_ref = root.Read<kWithoutReadBarrier>(); |
| 2296 | // Concurrent moving GC marked new roots through the to-space invariant. |
| 2297 | CHECK_EQ(new_ref, old_ref); |
| 2298 | } |
| 2299 | } |
| 2300 | } |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2301 | } |
Lokesh Gidra | ca5ed9f | 2022-04-20 01:39:28 +0000 | [diff] [blame] | 2302 | if (!gUseReadBarrier && (flags & kVisitRootFlagClearRootLog) != 0) { |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2303 | new_class_roots_.clear(); |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 2304 | new_bss_roots_boot_oat_files_.clear(); |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2305 | } |
Lokesh Gidra | ca5ed9f | 2022-04-20 01:39:28 +0000 | [diff] [blame] | 2306 | if (!gUseReadBarrier && (flags & kVisitRootFlagStartLoggingNewRoots) != 0) { |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 2307 | log_new_roots_ = true; |
Lokesh Gidra | ca5ed9f | 2022-04-20 01:39:28 +0000 | [diff] [blame] | 2308 | } else if (!gUseReadBarrier && (flags & kVisitRootFlagStopLoggingNewRoots) != 0) { |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 2309 | log_new_roots_ = false; |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2310 | } |
| 2311 | // We deliberately ignore the class roots in the image since we |
| 2312 | // handle image roots by using the MS/CMS rescanning of dirty cards. |
| 2313 | } |
| 2314 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 2315 | // Keep in sync with InitCallback. Anything we visit, we need to |
| 2316 | // reinit references to when reinitializing a ClassLinker from a |
| 2317 | // mapped image. |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 2318 | void ClassLinker::VisitRoots(RootVisitor* visitor, VisitRootFlags flags) { |
Mathieu Chartier | 3100080 | 2015-06-14 14:14:37 -0700 | [diff] [blame] | 2319 | class_roots_.VisitRootIfNonNull(visitor, RootInfo(kRootVMInternal)); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 2320 | VisitClassRoots(visitor, flags); |
Mathieu Chartier | 6cfc2c0 | 2015-10-12 15:06:16 -0700 | [diff] [blame] | 2321 | // Instead of visiting the find_array_class_cache_ drop it so that it doesn't prevent class |
| 2322 | // unloading if we are marking roots. |
| 2323 | DropFindArrayClassCache(); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 2324 | } |
| 2325 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2326 | class VisitClassLoaderClassesVisitor : public ClassLoaderVisitor { |
| 2327 | public: |
| 2328 | explicit VisitClassLoaderClassesVisitor(ClassVisitor* visitor) |
| 2329 | : visitor_(visitor), |
| 2330 | done_(false) {} |
| 2331 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2332 | void Visit(ObjPtr<mirror::ClassLoader> class_loader) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 2333 | REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_) override { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2334 | ClassTable* const class_table = class_loader->GetClassTable(); |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2335 | if (!done_ && class_table != nullptr) { |
| 2336 | DefiningClassLoaderFilterVisitor visitor(class_loader, visitor_); |
| 2337 | if (!class_table->Visit(visitor)) { |
| 2338 | // If the visitor ClassTable returns false it means that we don't need to continue. |
| 2339 | done_ = true; |
| 2340 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2341 | } |
| 2342 | } |
| 2343 | |
| 2344 | private: |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2345 | // Class visitor that limits the class visits from a ClassTable to the classes with |
| 2346 | // the provided defining class loader. This filter is used to avoid multiple visits |
| 2347 | // of the same class which can be recorded for multiple initiating class loaders. |
| 2348 | class DefiningClassLoaderFilterVisitor : public ClassVisitor { |
| 2349 | public: |
| 2350 | DefiningClassLoaderFilterVisitor(ObjPtr<mirror::ClassLoader> defining_class_loader, |
| 2351 | ClassVisitor* visitor) |
| 2352 | : defining_class_loader_(defining_class_loader), visitor_(visitor) { } |
| 2353 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 2354 | bool operator()(ObjPtr<mirror::Class> klass) override REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2355 | if (klass->GetClassLoader() != defining_class_loader_) { |
| 2356 | return true; |
| 2357 | } |
| 2358 | return (*visitor_)(klass); |
| 2359 | } |
| 2360 | |
Vladimir Marko | 0984e48 | 2019-03-27 16:41:41 +0000 | [diff] [blame] | 2361 | const ObjPtr<mirror::ClassLoader> defining_class_loader_; |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2362 | ClassVisitor* const visitor_; |
| 2363 | }; |
| 2364 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2365 | ClassVisitor* const visitor_; |
| 2366 | // If done is true then we don't need to do any more visiting. |
| 2367 | bool done_; |
| 2368 | }; |
| 2369 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2370 | void ClassLinker::VisitClassesInternal(ClassVisitor* visitor) { |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 2371 | if (boot_class_table_->Visit(*visitor)) { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2372 | VisitClassLoaderClassesVisitor loader_visitor(visitor); |
| 2373 | VisitClassLoaders(&loader_visitor); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 2374 | } |
| 2375 | } |
| 2376 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2377 | void ClassLinker::VisitClasses(ClassVisitor* visitor) { |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 2378 | Thread* const self = Thread::Current(); |
| 2379 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 2380 | // Not safe to have thread suspension when we are holding a lock. |
| 2381 | if (self != nullptr) { |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 2382 | ScopedAssertNoThreadSuspension nts(__FUNCTION__); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2383 | VisitClassesInternal(visitor); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 2384 | } else { |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2385 | VisitClassesInternal(visitor); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2386 | } |
| 2387 | } |
| 2388 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2389 | class GetClassesInToVector : public ClassVisitor { |
| 2390 | public: |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 2391 | bool operator()(ObjPtr<mirror::Class> klass) override { |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2392 | classes_.push_back(klass); |
| 2393 | return true; |
| 2394 | } |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2395 | std::vector<ObjPtr<mirror::Class>> classes_; |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2396 | }; |
| 2397 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2398 | class GetClassInToObjectArray : public ClassVisitor { |
| 2399 | public: |
| 2400 | explicit GetClassInToObjectArray(mirror::ObjectArray<mirror::Class>* arr) |
| 2401 | : arr_(arr), index_(0) {} |
| 2402 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 2403 | bool operator()(ObjPtr<mirror::Class> klass) override REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2404 | ++index_; |
| 2405 | if (index_ <= arr_->GetLength()) { |
| 2406 | arr_->Set(index_ - 1, klass); |
| 2407 | return true; |
| 2408 | } |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2409 | return false; |
| 2410 | } |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2411 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2412 | bool Succeeded() const REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2413 | return index_ <= arr_->GetLength(); |
| 2414 | } |
| 2415 | |
| 2416 | private: |
| 2417 | mirror::ObjectArray<mirror::Class>* const arr_; |
| 2418 | int32_t index_; |
| 2419 | }; |
| 2420 | |
| 2421 | void ClassLinker::VisitClassesWithoutClassesLock(ClassVisitor* visitor) { |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2422 | // TODO: it may be possible to avoid secondary storage if we iterate over dex caches. The problem |
| 2423 | // is avoiding duplicates. |
| 2424 | if (!kMovingClasses) { |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 2425 | ScopedAssertNoThreadSuspension nts(__FUNCTION__); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2426 | GetClassesInToVector accumulator; |
| 2427 | VisitClasses(&accumulator); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2428 | for (ObjPtr<mirror::Class> klass : accumulator.classes_) { |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 2429 | if (!visitor->operator()(klass)) { |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2430 | return; |
| 2431 | } |
| 2432 | } |
| 2433 | } else { |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 2434 | Thread* const self = Thread::Current(); |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2435 | StackHandleScope<1> hs(self); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2436 | auto classes = hs.NewHandle<mirror::ObjectArray<mirror::Class>>(nullptr); |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2437 | // We size the array assuming classes won't be added to the class table during the visit. |
| 2438 | // If this assumption fails we iterate again. |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2439 | while (true) { |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2440 | size_t class_table_size; |
| 2441 | { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2442 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2443 | // Add 100 in case new classes get loaded when we are filling in the object array. |
| 2444 | class_table_size = NumZygoteClasses() + NumNonZygoteClasses() + 100; |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2445 | } |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 2446 | ObjPtr<mirror::Class> array_of_class = GetClassRoot<mirror::ObjectArray<mirror::Class>>(this); |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2447 | classes.Assign( |
| 2448 | mirror::ObjectArray<mirror::Class>::Alloc(self, array_of_class, class_table_size)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 2449 | CHECK(classes != nullptr); // OOME. |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2450 | GetClassInToObjectArray accumulator(classes.Get()); |
| 2451 | VisitClasses(&accumulator); |
| 2452 | if (accumulator.Succeeded()) { |
| 2453 | break; |
| 2454 | } |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2455 | } |
| 2456 | for (int32_t i = 0; i < classes->GetLength(); ++i) { |
| 2457 | // If the class table shrank during creation of the clases array we expect null elements. If |
| 2458 | // the class table grew then the loop repeats. If classes are created after the loop has |
| 2459 | // finished then we don't visit. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2460 | ObjPtr<mirror::Class> klass = classes->Get(i); |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 2461 | if (klass != nullptr && !visitor->operator()(klass)) { |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2462 | return; |
| 2463 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2464 | } |
| 2465 | } |
| 2466 | } |
| 2467 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 2468 | ClassLinker::~ClassLinker() { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2469 | Thread* const self = Thread::Current(); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 2470 | for (const ClassLoaderData& data : class_loaders_) { |
Alexey Grebenkin | be4c2bd | 2018-02-01 19:09:59 +0300 | [diff] [blame] | 2471 | // CHA unloading analysis is not needed. No negative consequences are expected because |
| 2472 | // all the classloaders are deleted at the same time. |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 2473 | DeleteClassLoader(self, data, /*cleanup_cha=*/ false); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 2474 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2475 | class_loaders_.clear(); |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 2476 | while (!running_visibly_initialized_callbacks_.empty()) { |
| 2477 | std::unique_ptr<VisiblyInitializedCallback> callback( |
| 2478 | std::addressof(running_visibly_initialized_callbacks_.front())); |
| 2479 | running_visibly_initialized_callbacks_.pop_front(); |
| 2480 | } |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 2481 | } |
| 2482 | |
Alexey Grebenkin | be4c2bd | 2018-02-01 19:09:59 +0300 | [diff] [blame] | 2483 | void ClassLinker::DeleteClassLoader(Thread* self, const ClassLoaderData& data, bool cleanup_cha) { |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 2484 | Runtime* const runtime = Runtime::Current(); |
| 2485 | JavaVMExt* const vm = runtime->GetJavaVM(); |
| 2486 | vm->DeleteWeakGlobalRef(self, data.weak_root); |
Calin Juravle | e5de54c | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 2487 | // 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] | 2488 | if (runtime->GetJit() != nullptr) { |
| 2489 | jit::JitCodeCache* code_cache = runtime->GetJit()->GetCodeCache(); |
| 2490 | if (code_cache != nullptr) { |
Mathieu Chartier | cf79cf5 | 2017-07-21 11:17:57 -0700 | [diff] [blame] | 2491 | // For the JIT case, RemoveMethodsIn removes the CHA dependencies. |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 2492 | code_cache->RemoveMethodsIn(self, *data.allocator); |
| 2493 | } |
Andreas Gampe | c1ac9ee | 2017-07-24 22:35:49 -0700 | [diff] [blame] | 2494 | } else if (cha_ != nullptr) { |
Mathieu Chartier | cf79cf5 | 2017-07-21 11:17:57 -0700 | [diff] [blame] | 2495 | // If we don't have a JIT, we need to manually remove the CHA dependencies manually. |
Vladimir Marko | 8e94a6f | 2022-12-13 16:46:22 +0000 | [diff] [blame] | 2496 | cha_->RemoveDependenciesForLinearAlloc(self, data.allocator); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 2497 | } |
Alexey Grebenkin | be4c2bd | 2018-02-01 19:09:59 +0300 | [diff] [blame] | 2498 | // Cleanup references to single implementation ArtMethods that will be deleted. |
| 2499 | if (cleanup_cha) { |
| 2500 | CHAOnDeleteUpdateClassVisitor visitor(data.allocator); |
Vladimir Marko | 5f958f6 | 2022-02-08 12:01:07 +0000 | [diff] [blame] | 2501 | data.class_table->Visit<kWithoutReadBarrier>(visitor); |
Alexey Grebenkin | be4c2bd | 2018-02-01 19:09:59 +0300 | [diff] [blame] | 2502 | } |
Vladimir Marko | 86c8752 | 2020-05-11 16:55:55 +0100 | [diff] [blame] | 2503 | { |
| 2504 | MutexLock lock(self, critical_native_code_with_clinit_check_lock_); |
| 2505 | auto end = critical_native_code_with_clinit_check_.end(); |
| 2506 | for (auto it = critical_native_code_with_clinit_check_.begin(); it != end; ) { |
| 2507 | if (data.allocator->ContainsUnsafe(it->first)) { |
| 2508 | it = critical_native_code_with_clinit_check_.erase(it); |
| 2509 | } else { |
| 2510 | ++it; |
| 2511 | } |
| 2512 | } |
| 2513 | } |
Alexey Grebenkin | be4c2bd | 2018-02-01 19:09:59 +0300 | [diff] [blame] | 2514 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 2515 | delete data.allocator; |
| 2516 | delete data.class_table; |
| 2517 | } |
| 2518 | |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 2519 | ObjPtr<mirror::PointerArray> ClassLinker::AllocPointerArray(Thread* self, size_t length) { |
| 2520 | return ObjPtr<mirror::PointerArray>::DownCast( |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 2521 | image_pointer_size_ == PointerSize::k64 |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 2522 | ? ObjPtr<mirror::Array>(mirror::LongArray::Alloc(self, length)) |
| 2523 | : ObjPtr<mirror::Array>(mirror::IntArray::Alloc(self, length))); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2524 | } |
| 2525 | |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 2526 | ObjPtr<mirror::DexCache> ClassLinker::AllocDexCache(Thread* self, const DexFile& dex_file) { |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2527 | StackHandleScope<1> hs(self); |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 2528 | auto dex_cache(hs.NewHandle(ObjPtr<mirror::DexCache>::DownCast( |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 2529 | GetClassRoot<mirror::DexCache>(this)->AllocObject(self)))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 2530 | if (dex_cache == nullptr) { |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2531 | self->AssertPendingOOMException(); |
| 2532 | return nullptr; |
| 2533 | } |
Vladimir Marko | 31c3daa | 2019-06-13 12:18:37 +0100 | [diff] [blame] | 2534 | // Use InternWeak() so that the location String can be collected when the ClassLoader |
| 2535 | // with this DexCache is collected. |
| 2536 | ObjPtr<mirror::String> location = intern_table_->InternWeak(dex_file.GetLocation().c_str()); |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2537 | if (location == nullptr) { |
| 2538 | self->AssertPendingOOMException(); |
| 2539 | return nullptr; |
| 2540 | } |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 2541 | dex_cache->SetLocation(location); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2542 | return dex_cache.Get(); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 2543 | } |
| 2544 | |
David Srbecky | 33df0e3 | 2021-09-30 14:36:32 +0000 | [diff] [blame] | 2545 | ObjPtr<mirror::DexCache> ClassLinker::AllocAndInitializeDexCache( |
| 2546 | Thread* self, const DexFile& dex_file, ObjPtr<mirror::ClassLoader> class_loader) { |
| 2547 | StackHandleScope<1> hs(self); |
| 2548 | Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader)); |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 2549 | ObjPtr<mirror::DexCache> dex_cache = AllocDexCache(self, dex_file); |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2550 | if (dex_cache != nullptr) { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 2551 | WriterMutexLock mu(self, *Locks::dex_lock_); |
David Srbecky | 33df0e3 | 2021-09-30 14:36:32 +0000 | [diff] [blame] | 2552 | dex_cache->Initialize(&dex_file, h_class_loader.Get()); |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2553 | } |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 2554 | return dex_cache; |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2555 | } |
| 2556 | |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 2557 | template <bool kMovable, typename PreFenceVisitor> |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 2558 | ObjPtr<mirror::Class> ClassLinker::AllocClass(Thread* self, |
| 2559 | ObjPtr<mirror::Class> java_lang_Class, |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 2560 | uint32_t class_size, |
| 2561 | const PreFenceVisitor& pre_fence_visitor) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2562 | DCHECK_GE(class_size, sizeof(mirror::Class)); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 2563 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 2564 | ObjPtr<mirror::Object> k = (kMovingClasses && kMovable) ? |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 2565 | heap->AllocObject(self, java_lang_Class, class_size, pre_fence_visitor) : |
| 2566 | heap->AllocNonMovableObject(self, java_lang_Class, class_size, pre_fence_visitor); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 2567 | if (UNLIKELY(k == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2568 | self->AssertPendingOOMException(); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 2569 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2570 | } |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 2571 | return k->AsClass(); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 2572 | } |
| 2573 | |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 2574 | template <bool kMovable> |
| 2575 | ObjPtr<mirror::Class> ClassLinker::AllocClass(Thread* self, |
| 2576 | ObjPtr<mirror::Class> java_lang_Class, |
| 2577 | uint32_t class_size) { |
| 2578 | mirror::Class::InitializeClassVisitor visitor(class_size); |
| 2579 | return AllocClass<kMovable>(self, java_lang_Class, class_size, visitor); |
| 2580 | } |
| 2581 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 2582 | ObjPtr<mirror::Class> ClassLinker::AllocClass(Thread* self, uint32_t class_size) { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 2583 | return AllocClass(self, GetClassRoot<mirror::Class>(this), class_size); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 2584 | } |
| 2585 | |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 2586 | void ClassLinker::AllocPrimitiveArrayClass(Thread* self, |
| 2587 | ClassRoot primitive_root, |
| 2588 | ClassRoot array_root) { |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 2589 | // We make this class non-movable for the unlikely case where it were to be |
| 2590 | // moved by a sticky-bit (minor) collection when using the Generational |
| 2591 | // Concurrent Copying (CC) collector, potentially creating a stale reference |
| 2592 | // in the `klass_` field of one of its instances allocated in the Large-Object |
| 2593 | // Space (LOS) -- see the comment about the dirty card scanning logic in |
| 2594 | // art::gc::collector::ConcurrentCopying::MarkingPhase. |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 2595 | ObjPtr<mirror::Class> array_class = AllocClass</* kMovable= */ false>( |
| 2596 | self, GetClassRoot<mirror::Class>(this), mirror::Array::ClassSize(image_pointer_size_)); |
| 2597 | ObjPtr<mirror::Class> component_type = GetClassRoot(primitive_root, this); |
| 2598 | DCHECK(component_type->IsPrimitive()); |
| 2599 | array_class->SetComponentType(component_type); |
| 2600 | SetClassRoot(array_root, array_class); |
| 2601 | } |
| 2602 | |
| 2603 | void ClassLinker::FinishArrayClassSetup(ObjPtr<mirror::Class> array_class) { |
| 2604 | ObjPtr<mirror::Class> java_lang_Object = GetClassRoot<mirror::Object>(this); |
| 2605 | array_class->SetSuperClass(java_lang_Object); |
| 2606 | array_class->SetVTable(java_lang_Object->GetVTable()); |
| 2607 | array_class->SetPrimitiveType(Primitive::kPrimNot); |
| 2608 | ObjPtr<mirror::Class> component_type = array_class->GetComponentType(); |
| 2609 | array_class->SetClassFlags(component_type->IsPrimitive() |
| 2610 | ? mirror::kClassFlagNoReferenceFields |
| 2611 | : mirror::kClassFlagObjectArray); |
| 2612 | array_class->SetClassLoader(component_type->GetClassLoader()); |
| 2613 | array_class->SetStatusForPrimitiveOrArray(ClassStatus::kLoaded); |
| 2614 | array_class->PopulateEmbeddedVTable(image_pointer_size_); |
| 2615 | ImTable* object_imt = java_lang_Object->GetImt(image_pointer_size_); |
| 2616 | array_class->SetImt(object_imt, image_pointer_size_); |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 2617 | DCHECK_EQ(array_class->NumMethods(), 0u); |
| 2618 | |
| 2619 | // don't need to set new_class->SetObjectSize(..) |
| 2620 | // because Object::SizeOf delegates to Array::SizeOf |
| 2621 | |
| 2622 | // All arrays have java/lang/Cloneable and java/io/Serializable as |
| 2623 | // interfaces. We need to set that up here, so that stuff like |
| 2624 | // "instanceof" works right. |
| 2625 | |
| 2626 | // Use the single, global copies of "interfaces" and "iftable" |
| 2627 | // (remember not to free them for arrays). |
| 2628 | { |
| 2629 | ObjPtr<mirror::IfTable> array_iftable = GetArrayIfTable(); |
| 2630 | CHECK(array_iftable != nullptr); |
| 2631 | array_class->SetIfTable(array_iftable); |
| 2632 | } |
| 2633 | |
| 2634 | // Inherit access flags from the component type. |
| 2635 | int access_flags = component_type->GetAccessFlags(); |
| 2636 | // Lose any implementation detail flags; in particular, arrays aren't finalizable. |
| 2637 | access_flags &= kAccJavaFlagsMask; |
| 2638 | // Arrays can't be used as a superclass or interface, so we want to add "abstract final" |
| 2639 | // and remove "interface". |
| 2640 | access_flags |= kAccAbstract | kAccFinal; |
| 2641 | access_flags &= ~kAccInterface; |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 2642 | |
Vladimir Marko | b68bb7a | 2020-03-17 10:55:25 +0000 | [diff] [blame] | 2643 | array_class->SetAccessFlagsDuringLinking(access_flags); |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 2644 | |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 2645 | // Array classes are fully initialized either during single threaded startup, |
| 2646 | // or from a pre-fence visitor, so visibly initialized. |
| 2647 | array_class->SetStatusForPrimitiveOrArray(ClassStatus::kVisiblyInitialized); |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 2648 | } |
| 2649 | |
| 2650 | void ClassLinker::FinishCoreArrayClassSetup(ClassRoot array_root) { |
| 2651 | // Do not hold lock on the array class object, the initialization of |
| 2652 | // core array classes is done while the process is still single threaded. |
| 2653 | ObjPtr<mirror::Class> array_class = GetClassRoot(array_root, this); |
| 2654 | FinishArrayClassSetup(array_class); |
| 2655 | |
| 2656 | std::string temp; |
| 2657 | const char* descriptor = array_class->GetDescriptor(&temp); |
| 2658 | size_t hash = ComputeModifiedUtf8Hash(descriptor); |
| 2659 | ObjPtr<mirror::Class> existing = InsertClass(descriptor, array_class, hash); |
| 2660 | CHECK(existing == nullptr); |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 2661 | } |
| 2662 | |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 2663 | ObjPtr<mirror::ObjectArray<mirror::StackTraceElement>> ClassLinker::AllocStackTraceElementArray( |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 2664 | Thread* self, |
| 2665 | size_t length) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2666 | return mirror::ObjectArray<mirror::StackTraceElement>::Alloc( |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 2667 | self, GetClassRoot<mirror::ObjectArray<mirror::StackTraceElement>>(this), length); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 2668 | } |
| 2669 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 2670 | ObjPtr<mirror::Class> ClassLinker::EnsureResolved(Thread* self, |
| 2671 | const char* descriptor, |
| 2672 | ObjPtr<mirror::Class> klass) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2673 | DCHECK(klass != nullptr); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2674 | if (kIsDebugBuild) { |
| 2675 | StackHandleScope<1> hs(self); |
| 2676 | HandleWrapperObjPtr<mirror::Class> h = hs.NewHandleWrapper(&klass); |
| 2677 | Thread::PoisonObjectPointersIfDebug(); |
| 2678 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2679 | |
| 2680 | // For temporary classes we must wait for them to be retired. |
| 2681 | if (init_done_ && klass->IsTemp()) { |
| 2682 | CHECK(!klass->IsResolved()); |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2683 | if (klass->IsErroneousUnresolved()) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2684 | ThrowEarlierClassFailure(klass); |
| 2685 | return nullptr; |
| 2686 | } |
| 2687 | StackHandleScope<1> hs(self); |
| 2688 | Handle<mirror::Class> h_class(hs.NewHandle(klass)); |
| 2689 | ObjectLock<mirror::Class> lock(self, h_class); |
| 2690 | // Loop and wait for the resolving thread to retire this class. |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2691 | while (!h_class->IsRetired() && !h_class->IsErroneousUnresolved()) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2692 | lock.WaitIgnoringInterrupts(); |
| 2693 | } |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2694 | if (h_class->IsErroneousUnresolved()) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2695 | ThrowEarlierClassFailure(h_class.Get()); |
| 2696 | return nullptr; |
| 2697 | } |
| 2698 | CHECK(h_class->IsRetired()); |
| 2699 | // Get the updated class from class table. |
Andreas Gampe | 34ee684 | 2014-12-02 15:43:52 -0800 | [diff] [blame] | 2700 | klass = LookupClass(self, descriptor, h_class.Get()->GetClassLoader()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2701 | } |
| 2702 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2703 | // Wait for the class if it has not already been linked. |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 2704 | size_t index = 0; |
| 2705 | // Maximum number of yield iterations until we start sleeping. |
| 2706 | static const size_t kNumYieldIterations = 1000; |
| 2707 | // How long each sleep is in us. |
| 2708 | static const size_t kSleepDurationUS = 1000; // 1 ms. |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2709 | while (!klass->IsResolved() && !klass->IsErroneousUnresolved()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2710 | StackHandleScope<1> hs(self); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2711 | HandleWrapperObjPtr<mirror::Class> h_class(hs.NewHandleWrapper(&klass)); |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 2712 | { |
| 2713 | ObjectTryLock<mirror::Class> lock(self, h_class); |
| 2714 | // Can not use a monitor wait here since it may block when returning and deadlock if another |
| 2715 | // thread has locked klass. |
| 2716 | if (lock.Acquired()) { |
| 2717 | // Check for circular dependencies between classes, the lock is required for SetStatus. |
| 2718 | if (!h_class->IsResolved() && h_class->GetClinitThreadId() == self->GetTid()) { |
| 2719 | ThrowClassCircularityError(h_class.Get()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 2720 | mirror::Class::SetStatus(h_class, ClassStatus::kErrorUnresolved, self); |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 2721 | return nullptr; |
| 2722 | } |
| 2723 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2724 | } |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 2725 | { |
| 2726 | // Handle wrapper deals with klass moving. |
Vladimir Marko | ddf4fd3 | 2021-11-22 16:31:57 +0000 | [diff] [blame] | 2727 | ScopedThreadSuspension sts(self, ThreadState::kSuspended); |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 2728 | if (index < kNumYieldIterations) { |
| 2729 | sched_yield(); |
| 2730 | } else { |
| 2731 | usleep(kSleepDurationUS); |
| 2732 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2733 | } |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 2734 | ++index; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2735 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2736 | |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2737 | if (klass->IsErroneousUnresolved()) { |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 2738 | ThrowEarlierClassFailure(klass); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 2739 | return nullptr; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2740 | } |
| 2741 | // Return the loaded class. No exceptions should be pending. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 2742 | CHECK(klass->IsResolved()) << klass->PrettyClass(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2743 | self->AssertNoPendingException(); |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 2744 | return klass; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2745 | } |
| 2746 | |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 2747 | using ClassPathEntry = std::pair<const DexFile*, const dex::ClassDef*>; |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2748 | |
| 2749 | // Search a collection of DexFiles for a descriptor |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2750 | ClassPathEntry FindInClassPath(const char* descriptor, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 2751 | size_t hash, const std::vector<const DexFile*>& class_path) { |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2752 | for (const DexFile* dex_file : class_path) { |
Mathieu Chartier | 0a19e21 | 2019-11-27 14:35:24 -0800 | [diff] [blame] | 2753 | DCHECK(dex_file != nullptr); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 2754 | const dex::ClassDef* dex_class_def = OatDexFile::FindClassDef(*dex_file, descriptor, hash); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2755 | if (dex_class_def != nullptr) { |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2756 | return ClassPathEntry(dex_file, dex_class_def); |
| 2757 | } |
| 2758 | } |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2759 | return ClassPathEntry(nullptr, nullptr); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2760 | } |
| 2761 | |
Nicolas Geoffray | e8445e5 | 2021-09-23 14:10:05 +0100 | [diff] [blame] | 2762 | // Helper macro to make sure each class loader lookup call handles the case the |
| 2763 | // class loader is not recognized, or the lookup threw an exception. |
| 2764 | #define RETURN_IF_UNRECOGNIZED_OR_FOUND_OR_EXCEPTION(call_, result_, thread_) \ |
| 2765 | do { \ |
| 2766 | auto local_call = call_; \ |
| 2767 | if (!local_call) { \ |
| 2768 | return false; \ |
| 2769 | } \ |
| 2770 | auto local_result = result_; \ |
| 2771 | if (local_result != nullptr) { \ |
| 2772 | return true; \ |
| 2773 | } \ |
| 2774 | auto local_thread = thread_; \ |
| 2775 | if (local_thread->IsExceptionPending()) { \ |
| 2776 | /* Pending exception means there was an error other than */ \ |
| 2777 | /* ClassNotFound that must be returned to the caller. */ \ |
Nicolas Geoffray | c9d5949 | 2022-11-17 15:03:09 +0000 | [diff] [blame] | 2778 | return false; \ |
Nicolas Geoffray | e8445e5 | 2021-09-23 14:10:05 +0100 | [diff] [blame] | 2779 | } \ |
| 2780 | } while (0) |
| 2781 | |
Vladimir Marko | 4e0b5d7 | 2022-11-09 10:58:15 +0000 | [diff] [blame] | 2782 | bool ClassLinker::FindClassInSharedLibraries(Thread* self, |
Nicolas Geoffray | 80a560c | 2018-10-26 13:48:51 +0100 | [diff] [blame] | 2783 | const char* descriptor, |
| 2784 | size_t hash, |
| 2785 | Handle<mirror::ClassLoader> class_loader, |
| 2786 | /*out*/ ObjPtr<mirror::Class>* result) { |
VladimÃr Marko | 5be5ce7 | 2022-10-18 09:49:00 +0000 | [diff] [blame] | 2787 | ArtField* field = WellKnownClasses::dalvik_system_BaseDexClassLoader_sharedLibraryLoaders; |
Vladimir Marko | 4e0b5d7 | 2022-11-09 10:58:15 +0000 | [diff] [blame] | 2788 | return FindClassInSharedLibrariesHelper(self, descriptor, hash, class_loader, field, result); |
Brad Stenning | 9c924e8 | 2021-10-11 19:09:00 -0700 | [diff] [blame] | 2789 | } |
| 2790 | |
Vladimir Marko | 4e0b5d7 | 2022-11-09 10:58:15 +0000 | [diff] [blame] | 2791 | bool ClassLinker::FindClassInSharedLibrariesHelper(Thread* self, |
Brad Stenning | 9c924e8 | 2021-10-11 19:09:00 -0700 | [diff] [blame] | 2792 | const char* descriptor, |
| 2793 | size_t hash, |
| 2794 | Handle<mirror::ClassLoader> class_loader, |
| 2795 | ArtField* field, |
| 2796 | /*out*/ ObjPtr<mirror::Class>* result) { |
Nicolas Geoffray | 80a560c | 2018-10-26 13:48:51 +0100 | [diff] [blame] | 2797 | ObjPtr<mirror::Object> raw_shared_libraries = field->GetObject(class_loader.Get()); |
| 2798 | if (raw_shared_libraries == nullptr) { |
| 2799 | return true; |
| 2800 | } |
| 2801 | |
| 2802 | StackHandleScope<2> hs(self); |
| 2803 | Handle<mirror::ObjectArray<mirror::ClassLoader>> shared_libraries( |
| 2804 | hs.NewHandle(raw_shared_libraries->AsObjectArray<mirror::ClassLoader>())); |
| 2805 | MutableHandle<mirror::ClassLoader> temp_loader = hs.NewHandle<mirror::ClassLoader>(nullptr); |
Alex Light | a9bbc08 | 2019-11-14 14:51:41 -0800 | [diff] [blame] | 2806 | for (auto loader : shared_libraries.Iterate<mirror::ClassLoader>()) { |
| 2807 | temp_loader.Assign(loader); |
Nicolas Geoffray | e8445e5 | 2021-09-23 14:10:05 +0100 | [diff] [blame] | 2808 | RETURN_IF_UNRECOGNIZED_OR_FOUND_OR_EXCEPTION( |
Vladimir Marko | 4e0b5d7 | 2022-11-09 10:58:15 +0000 | [diff] [blame] | 2809 | FindClassInBaseDexClassLoader(self, descriptor, hash, temp_loader, result), |
Nicolas Geoffray | e8445e5 | 2021-09-23 14:10:05 +0100 | [diff] [blame] | 2810 | *result, |
| 2811 | self); |
Nicolas Geoffray | 80a560c | 2018-10-26 13:48:51 +0100 | [diff] [blame] | 2812 | } |
| 2813 | return true; |
| 2814 | } |
| 2815 | |
Vladimir Marko | 4e0b5d7 | 2022-11-09 10:58:15 +0000 | [diff] [blame] | 2816 | bool ClassLinker::FindClassInSharedLibrariesAfter(Thread* self, |
Brad Stenning | 9c924e8 | 2021-10-11 19:09:00 -0700 | [diff] [blame] | 2817 | const char* descriptor, |
| 2818 | size_t hash, |
| 2819 | Handle<mirror::ClassLoader> class_loader, |
| 2820 | /*out*/ ObjPtr<mirror::Class>* result) { |
VladimÃr Marko | 5be5ce7 | 2022-10-18 09:49:00 +0000 | [diff] [blame] | 2821 | ArtField* field = WellKnownClasses::dalvik_system_BaseDexClassLoader_sharedLibraryLoadersAfter; |
Vladimir Marko | 4e0b5d7 | 2022-11-09 10:58:15 +0000 | [diff] [blame] | 2822 | return FindClassInSharedLibrariesHelper(self, descriptor, hash, class_loader, field, result); |
Brad Stenning | 9c924e8 | 2021-10-11 19:09:00 -0700 | [diff] [blame] | 2823 | } |
| 2824 | |
Vladimir Marko | 4e0b5d7 | 2022-11-09 10:58:15 +0000 | [diff] [blame] | 2825 | bool ClassLinker::FindClassInBaseDexClassLoader(Thread* self, |
Nicolas Geoffray | 7d8d8ff | 2016-11-02 12:38:05 +0000 | [diff] [blame] | 2826 | const char* descriptor, |
| 2827 | size_t hash, |
| 2828 | Handle<mirror::ClassLoader> class_loader, |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 2829 | /*out*/ ObjPtr<mirror::Class>* result) { |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 2830 | // Termination case: boot class loader. |
Nicolas Geoffray | c9d5949 | 2022-11-17 15:03:09 +0000 | [diff] [blame] | 2831 | if (IsBootClassLoader(class_loader.Get())) { |
Nicolas Geoffray | e8445e5 | 2021-09-23 14:10:05 +0100 | [diff] [blame] | 2832 | RETURN_IF_UNRECOGNIZED_OR_FOUND_OR_EXCEPTION( |
| 2833 | FindClassInBootClassLoaderClassPath(self, descriptor, hash, result), *result, self); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2834 | return true; |
| 2835 | } |
| 2836 | |
Vladimir Marko | 4e0b5d7 | 2022-11-09 10:58:15 +0000 | [diff] [blame] | 2837 | if (IsPathOrDexClassLoader(class_loader) || IsInMemoryDexClassLoader(class_loader)) { |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 2838 | // For regular path or dex class loader the search order is: |
| 2839 | // - parent |
Nicolas Geoffray | 80a560c | 2018-10-26 13:48:51 +0100 | [diff] [blame] | 2840 | // - shared libraries |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 2841 | // - class loader dex files |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2842 | |
Nicolas Geoffray | e8445e5 | 2021-09-23 14:10:05 +0100 | [diff] [blame] | 2843 | // Create a handle as RegisterDexFile may allocate dex caches (and cause thread suspension). |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 2844 | StackHandleScope<1> hs(self); |
| 2845 | Handle<mirror::ClassLoader> h_parent(hs.NewHandle(class_loader->GetParent())); |
Nicolas Geoffray | e8445e5 | 2021-09-23 14:10:05 +0100 | [diff] [blame] | 2846 | RETURN_IF_UNRECOGNIZED_OR_FOUND_OR_EXCEPTION( |
Vladimir Marko | 4e0b5d7 | 2022-11-09 10:58:15 +0000 | [diff] [blame] | 2847 | FindClassInBaseDexClassLoader(self, descriptor, hash, h_parent, result), |
Nicolas Geoffray | e8445e5 | 2021-09-23 14:10:05 +0100 | [diff] [blame] | 2848 | *result, |
| 2849 | self); |
| 2850 | RETURN_IF_UNRECOGNIZED_OR_FOUND_OR_EXCEPTION( |
Vladimir Marko | 4e0b5d7 | 2022-11-09 10:58:15 +0000 | [diff] [blame] | 2851 | FindClassInSharedLibraries(self, descriptor, hash, class_loader, result), |
Nicolas Geoffray | e8445e5 | 2021-09-23 14:10:05 +0100 | [diff] [blame] | 2852 | *result, |
| 2853 | self); |
| 2854 | RETURN_IF_UNRECOGNIZED_OR_FOUND_OR_EXCEPTION( |
Vladimir Marko | 4e0b5d7 | 2022-11-09 10:58:15 +0000 | [diff] [blame] | 2855 | FindClassInBaseDexClassLoaderClassPath(self, descriptor, hash, class_loader, result), |
Nicolas Geoffray | e8445e5 | 2021-09-23 14:10:05 +0100 | [diff] [blame] | 2856 | *result, |
| 2857 | self); |
Brad Stenning | 9c924e8 | 2021-10-11 19:09:00 -0700 | [diff] [blame] | 2858 | RETURN_IF_UNRECOGNIZED_OR_FOUND_OR_EXCEPTION( |
Vladimir Marko | 4e0b5d7 | 2022-11-09 10:58:15 +0000 | [diff] [blame] | 2859 | FindClassInSharedLibrariesAfter(self, descriptor, hash, class_loader, result), |
Brad Stenning | 9c924e8 | 2021-10-11 19:09:00 -0700 | [diff] [blame] | 2860 | *result, |
| 2861 | self); |
Nicolas Geoffray | e8445e5 | 2021-09-23 14:10:05 +0100 | [diff] [blame] | 2862 | // We did not find a class, but the class loader chain was recognized, so we |
| 2863 | // return true. |
| 2864 | return true; |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2865 | } |
| 2866 | |
Vladimir Marko | 4e0b5d7 | 2022-11-09 10:58:15 +0000 | [diff] [blame] | 2867 | if (IsDelegateLastClassLoader(class_loader)) { |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 2868 | // For delegate last, the search order is: |
| 2869 | // - boot class path |
Nicolas Geoffray | 80a560c | 2018-10-26 13:48:51 +0100 | [diff] [blame] | 2870 | // - shared libraries |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 2871 | // - class loader dex files |
| 2872 | // - parent |
Nicolas Geoffray | e8445e5 | 2021-09-23 14:10:05 +0100 | [diff] [blame] | 2873 | RETURN_IF_UNRECOGNIZED_OR_FOUND_OR_EXCEPTION( |
Nicolas Geoffray | c9d5949 | 2022-11-17 15:03:09 +0000 | [diff] [blame] | 2874 | FindClassInBootClassLoaderClassPath(self, descriptor, hash, result), *result, self); |
Nicolas Geoffray | e8445e5 | 2021-09-23 14:10:05 +0100 | [diff] [blame] | 2875 | RETURN_IF_UNRECOGNIZED_OR_FOUND_OR_EXCEPTION( |
Vladimir Marko | 4e0b5d7 | 2022-11-09 10:58:15 +0000 | [diff] [blame] | 2876 | FindClassInSharedLibraries(self, descriptor, hash, class_loader, result), |
Nicolas Geoffray | e8445e5 | 2021-09-23 14:10:05 +0100 | [diff] [blame] | 2877 | *result, |
| 2878 | self); |
| 2879 | RETURN_IF_UNRECOGNIZED_OR_FOUND_OR_EXCEPTION( |
Vladimir Marko | 4e0b5d7 | 2022-11-09 10:58:15 +0000 | [diff] [blame] | 2880 | FindClassInBaseDexClassLoaderClassPath(self, descriptor, hash, class_loader, result), |
Nicolas Geoffray | e8445e5 | 2021-09-23 14:10:05 +0100 | [diff] [blame] | 2881 | *result, |
| 2882 | self); |
Brad Stenning | 9c924e8 | 2021-10-11 19:09:00 -0700 | [diff] [blame] | 2883 | RETURN_IF_UNRECOGNIZED_OR_FOUND_OR_EXCEPTION( |
Vladimir Marko | 4e0b5d7 | 2022-11-09 10:58:15 +0000 | [diff] [blame] | 2884 | FindClassInSharedLibrariesAfter(self, descriptor, hash, class_loader, result), |
Brad Stenning | 9c924e8 | 2021-10-11 19:09:00 -0700 | [diff] [blame] | 2885 | *result, |
| 2886 | self); |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 2887 | |
Nicolas Geoffray | e8445e5 | 2021-09-23 14:10:05 +0100 | [diff] [blame] | 2888 | // Create a handle as RegisterDexFile may allocate dex caches (and cause thread suspension). |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 2889 | StackHandleScope<1> hs(self); |
| 2890 | Handle<mirror::ClassLoader> h_parent(hs.NewHandle(class_loader->GetParent())); |
Nicolas Geoffray | e8445e5 | 2021-09-23 14:10:05 +0100 | [diff] [blame] | 2891 | RETURN_IF_UNRECOGNIZED_OR_FOUND_OR_EXCEPTION( |
Vladimir Marko | 4e0b5d7 | 2022-11-09 10:58:15 +0000 | [diff] [blame] | 2892 | FindClassInBaseDexClassLoader(self, descriptor, hash, h_parent, result), |
Nicolas Geoffray | e8445e5 | 2021-09-23 14:10:05 +0100 | [diff] [blame] | 2893 | *result, |
| 2894 | self); |
| 2895 | // We did not find a class, but the class loader chain was recognized, so we |
| 2896 | // return true. |
| 2897 | return true; |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 2898 | } |
| 2899 | |
| 2900 | // Unsupported class loader. |
| 2901 | *result = nullptr; |
| 2902 | return false; |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 2903 | } |
| 2904 | |
Nicolas Geoffray | e8445e5 | 2021-09-23 14:10:05 +0100 | [diff] [blame] | 2905 | #undef RETURN_IF_UNRECOGNIZED_OR_FOUND_OR_EXCEPTION |
| 2906 | |
Nicolas Geoffray | c9d5949 | 2022-11-17 15:03:09 +0000 | [diff] [blame] | 2907 | namespace { |
| 2908 | |
| 2909 | // Matches exceptions caught in DexFile.defineClass. |
| 2910 | ALWAYS_INLINE bool MatchesDexFileCaughtExceptions(ObjPtr<mirror::Throwable> throwable, |
| 2911 | ClassLinker* class_linker) |
| 2912 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 2913 | return |
| 2914 | // ClassNotFoundException. |
| 2915 | throwable->InstanceOf(GetClassRoot(ClassRoot::kJavaLangClassNotFoundException, |
| 2916 | class_linker)) |
| 2917 | || |
| 2918 | // NoClassDefFoundError. TODO: Reconsider this. b/130746382. |
| 2919 | throwable->InstanceOf(Runtime::Current()->GetPreAllocatedNoClassDefFoundError()->GetClass()); |
| 2920 | } |
| 2921 | |
| 2922 | // Clear exceptions caught in DexFile.defineClass. |
| 2923 | ALWAYS_INLINE void FilterDexFileCaughtExceptions(Thread* self, ClassLinker* class_linker) |
| 2924 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 2925 | if (MatchesDexFileCaughtExceptions(self->GetException(), class_linker)) { |
| 2926 | self->ClearException(); |
| 2927 | } |
| 2928 | } |
| 2929 | |
| 2930 | } // namespace |
| 2931 | |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 2932 | // Finds the class in the boot class loader. |
| 2933 | // If the class is found the method returns the resolved class. Otherwise it returns null. |
Nicolas Geoffray | e8445e5 | 2021-09-23 14:10:05 +0100 | [diff] [blame] | 2934 | bool ClassLinker::FindClassInBootClassLoaderClassPath(Thread* self, |
| 2935 | const char* descriptor, |
| 2936 | size_t hash, |
| 2937 | /*out*/ ObjPtr<mirror::Class>* result) { |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 2938 | ClassPathEntry pair = FindInClassPath(descriptor, hash, boot_class_path_); |
| 2939 | if (pair.second != nullptr) { |
Nicolas Geoffray | c9d5949 | 2022-11-17 15:03:09 +0000 | [diff] [blame] | 2940 | ObjPtr<mirror::Class> klass = LookupClass(self, descriptor, hash, nullptr); |
| 2941 | if (klass != nullptr) { |
| 2942 | *result = EnsureResolved(self, descriptor, klass); |
| 2943 | } else { |
| 2944 | *result = DefineClass(self, |
| 2945 | descriptor, |
| 2946 | hash, |
| 2947 | ScopedNullHandle<mirror::ClassLoader>(), |
| 2948 | *pair.first, |
| 2949 | *pair.second); |
| 2950 | } |
| 2951 | if (*result == nullptr) { |
| 2952 | CHECK(self->IsExceptionPending()) << descriptor; |
| 2953 | FilterDexFileCaughtExceptions(self, this); |
| 2954 | } |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2955 | } |
Nicolas Geoffray | e8445e5 | 2021-09-23 14:10:05 +0100 | [diff] [blame] | 2956 | // The boot classloader is always a known lookup. |
| 2957 | return true; |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 2958 | } |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2959 | |
Nicolas Geoffray | e8445e5 | 2021-09-23 14:10:05 +0100 | [diff] [blame] | 2960 | bool ClassLinker::FindClassInBaseDexClassLoaderClassPath( |
Vladimir Marko | 4e0b5d7 | 2022-11-09 10:58:15 +0000 | [diff] [blame] | 2961 | Thread* self, |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 2962 | const char* descriptor, |
| 2963 | size_t hash, |
Nicolas Geoffray | e8445e5 | 2021-09-23 14:10:05 +0100 | [diff] [blame] | 2964 | Handle<mirror::ClassLoader> class_loader, |
| 2965 | /*out*/ ObjPtr<mirror::Class>* result) { |
Vladimir Marko | 4e0b5d7 | 2022-11-09 10:58:15 +0000 | [diff] [blame] | 2966 | DCHECK(IsPathOrDexClassLoader(class_loader) || |
| 2967 | IsInMemoryDexClassLoader(class_loader) || |
| 2968 | IsDelegateLastClassLoader(class_loader)) |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 2969 | << "Unexpected class loader for descriptor " << descriptor; |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2970 | |
Vladimir Marko | 68c0758 | 2021-04-19 16:01:15 +0000 | [diff] [blame] | 2971 | const DexFile* dex_file = nullptr; |
| 2972 | const dex::ClassDef* class_def = nullptr; |
Andreas Gampe | b8e7c37 | 2018-02-20 18:24:55 -0800 | [diff] [blame] | 2973 | ObjPtr<mirror::Class> ret; |
Vladimir Marko | 68c0758 | 2021-04-19 16:01:15 +0000 | [diff] [blame] | 2974 | auto find_class_def = [&](const DexFile* cp_dex_file) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 2975 | const dex::ClassDef* cp_class_def = OatDexFile::FindClassDef(*cp_dex_file, descriptor, hash); |
| 2976 | if (cp_class_def != nullptr) { |
| 2977 | dex_file = cp_dex_file; |
| 2978 | class_def = cp_class_def; |
| 2979 | return false; // Found a class definition, stop visit. |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2980 | } |
Andreas Gampe | b8e7c37 | 2018-02-20 18:24:55 -0800 | [diff] [blame] | 2981 | return true; // Continue with the next DexFile. |
| 2982 | }; |
Vladimir Marko | 4e0b5d7 | 2022-11-09 10:58:15 +0000 | [diff] [blame] | 2983 | VisitClassLoaderDexFiles(self, class_loader, find_class_def); |
Andreas Gampe | b8e7c37 | 2018-02-20 18:24:55 -0800 | [diff] [blame] | 2984 | |
Vladimir Marko | 68c0758 | 2021-04-19 16:01:15 +0000 | [diff] [blame] | 2985 | if (class_def != nullptr) { |
Vladimir Marko | 4e0b5d7 | 2022-11-09 10:58:15 +0000 | [diff] [blame] | 2986 | *result = DefineClass(self, descriptor, hash, class_loader, *dex_file, *class_def); |
Nicolas Geoffray | c9d5949 | 2022-11-17 15:03:09 +0000 | [diff] [blame] | 2987 | if (UNLIKELY(*result == nullptr)) { |
| 2988 | CHECK(self->IsExceptionPending()) << descriptor; |
| 2989 | FilterDexFileCaughtExceptions(self, this); |
| 2990 | } else { |
| 2991 | DCHECK(!self->IsExceptionPending()); |
| 2992 | } |
Vladimir Marko | 68c0758 | 2021-04-19 16:01:15 +0000 | [diff] [blame] | 2993 | } |
Nicolas Geoffray | e8445e5 | 2021-09-23 14:10:05 +0100 | [diff] [blame] | 2994 | // A BaseDexClassLoader is always a known lookup. |
| 2995 | return true; |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2996 | } |
| 2997 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 2998 | ObjPtr<mirror::Class> ClassLinker::FindClass(Thread* self, |
| 2999 | const char* descriptor, |
| 3000 | Handle<mirror::ClassLoader> class_loader) { |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 3001 | DCHECK_NE(*descriptor, '\0') << "descriptor is empty string"; |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 3002 | DCHECK(self != nullptr); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 3003 | self->AssertNoPendingException(); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 3004 | self->PoisonObjectPointers(); // For DefineClass, CreateArrayClass, etc... |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 3005 | if (descriptor[1] == '\0') { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3006 | // only the descriptors of primitive types should be 1 character long, also avoid class lookup |
| 3007 | // for primitive classes that aren't backed by dex files. |
| 3008 | return FindPrimitiveClass(descriptor[0]); |
| 3009 | } |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 3010 | const size_t hash = ComputeModifiedUtf8Hash(descriptor); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3011 | // Find the class in the loaded classes table. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3012 | ObjPtr<mirror::Class> klass = LookupClass(self, descriptor, hash, class_loader.Get()); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 3013 | if (klass != nullptr) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3014 | return EnsureResolved(self, descriptor, klass); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3015 | } |
Nicolas Geoffray | c9d5949 | 2022-11-17 15:03:09 +0000 | [diff] [blame] | 3016 | // Class is not yet loaded. |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3017 | if (descriptor[0] != '[' && class_loader == nullptr) { |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 3018 | // 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] | 3019 | ClassPathEntry pair = FindInClassPath(descriptor, hash, boot_class_path_); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 3020 | if (pair.second != nullptr) { |
Mathieu Chartier | 9865bde | 2015-12-21 09:58:16 -0800 | [diff] [blame] | 3021 | return DefineClass(self, |
| 3022 | descriptor, |
| 3023 | hash, |
| 3024 | ScopedNullHandle<mirror::ClassLoader>(), |
| 3025 | *pair.first, |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 3026 | *pair.second); |
Ian Rogers | 6355745 | 2014-06-04 16:57:15 -0700 | [diff] [blame] | 3027 | } else { |
| 3028 | // The boot class loader is searched ahead of the application class loader, failures are |
| 3029 | // expected and will be wrapped in a ClassNotFoundException. Use the pre-allocated error to |
| 3030 | // trigger the chaining with a proper stack trace. |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 3031 | ObjPtr<mirror::Throwable> pre_allocated = |
| 3032 | Runtime::Current()->GetPreAllocatedNoClassDefFoundError(); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 3033 | self->SetException(pre_allocated); |
Ian Rogers | 6355745 | 2014-06-04 16:57:15 -0700 | [diff] [blame] | 3034 | return nullptr; |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 3035 | } |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 3036 | } |
| 3037 | ObjPtr<mirror::Class> result_ptr; |
Nicolas Geoffray | c9d5949 | 2022-11-17 15:03:09 +0000 | [diff] [blame] | 3038 | bool descriptor_equals; |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 3039 | if (descriptor[0] == '[') { |
| 3040 | result_ptr = CreateArrayClass(self, descriptor, hash, class_loader); |
Nicolas Geoffray | c9d5949 | 2022-11-17 15:03:09 +0000 | [diff] [blame] | 3041 | DCHECK_EQ(result_ptr == nullptr, self->IsExceptionPending()); |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 3042 | DCHECK(result_ptr == nullptr || result_ptr->DescriptorEquals(descriptor)); |
| 3043 | descriptor_equals = true; |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 3044 | } else { |
Nicolas Geoffray | c9d5949 | 2022-11-17 15:03:09 +0000 | [diff] [blame] | 3045 | ScopedObjectAccessUnchecked soa(self); |
| 3046 | bool known_hierarchy = |
| 3047 | FindClassInBaseDexClassLoader(self, descriptor, hash, class_loader, &result_ptr); |
| 3048 | if (result_ptr != nullptr) { |
| 3049 | // The chain was understood and we found the class. We still need to add the class to |
| 3050 | // the class table to protect from racy programs that can try and redefine the path list |
| 3051 | // which would change the Class<?> returned for subsequent evaluation of const-class. |
| 3052 | DCHECK(known_hierarchy); |
| 3053 | DCHECK(result_ptr->DescriptorEquals(descriptor)); |
| 3054 | descriptor_equals = true; |
| 3055 | } else if (!self->IsExceptionPending()) { |
| 3056 | // Either the chain wasn't understood or the class wasn't found. |
| 3057 | // If there is a pending exception we didn't clear, it is a not a ClassNotFoundException and |
| 3058 | // we should return it instead of silently clearing and retrying. |
| 3059 | // |
| 3060 | // If the chain was understood but we did not find the class, let the Java-side |
| 3061 | // rediscover all this and throw the exception with the right stack trace. Note that |
| 3062 | // the Java-side could still succeed for racy programs if another thread is actively |
| 3063 | // modifying the class loader's path list. |
| 3064 | |
| 3065 | // The runtime is not allowed to call into java from a runtime-thread so just abort. |
| 3066 | if (self->IsRuntimeThread()) { |
| 3067 | // Oops, we can't call into java so we can't run actual class-loader code. |
| 3068 | // This is true for e.g. for the compiler (jit or aot). |
| 3069 | ObjPtr<mirror::Throwable> pre_allocated = |
| 3070 | Runtime::Current()->GetPreAllocatedNoClassDefFoundError(); |
| 3071 | self->SetException(pre_allocated); |
| 3072 | return nullptr; |
| 3073 | } |
| 3074 | |
| 3075 | // Inlined DescriptorToDot(descriptor) with extra validation. |
| 3076 | // |
| 3077 | // Throw NoClassDefFoundError early rather than potentially load a class only to fail |
| 3078 | // the DescriptorEquals() check below and give a confusing error message. For example, |
| 3079 | // when native code erroneously calls JNI GetFieldId() with signature "java/lang/String" |
| 3080 | // instead of "Ljava/lang/String;", the message below using the "dot" names would be |
| 3081 | // "class loader [...] returned class java.lang.String instead of java.lang.String". |
| 3082 | size_t descriptor_length = strlen(descriptor); |
| 3083 | if (UNLIKELY(descriptor[0] != 'L') || |
| 3084 | UNLIKELY(descriptor[descriptor_length - 1] != ';') || |
| 3085 | UNLIKELY(memchr(descriptor + 1, '.', descriptor_length - 2) != nullptr)) { |
| 3086 | ThrowNoClassDefFoundError("Invalid descriptor: %s.", descriptor); |
| 3087 | return nullptr; |
| 3088 | } |
| 3089 | |
| 3090 | std::string class_name_string(descriptor + 1, descriptor_length - 2); |
| 3091 | std::replace(class_name_string.begin(), class_name_string.end(), '/', '.'); |
| 3092 | if (known_hierarchy && |
| 3093 | fast_class_not_found_exceptions_ && |
| 3094 | !Runtime::Current()->IsJavaDebuggable()) { |
| 3095 | // For known hierarchy, we know that the class is going to throw an exception. If we aren't |
| 3096 | // debuggable, optimize this path by throwing directly here without going back to Java |
| 3097 | // language. This reduces how many ClassNotFoundExceptions happen. |
| 3098 | self->ThrowNewExceptionF("Ljava/lang/ClassNotFoundException;", |
| 3099 | "%s", |
| 3100 | class_name_string.c_str()); |
| 3101 | } else { |
Vladimir Marko | b6f965d | 2022-11-23 14:24:45 +0000 | [diff] [blame] | 3102 | StackHandleScope<1u> hs(self); |
| 3103 | Handle<mirror::String> class_name_object = hs.NewHandle( |
| 3104 | mirror::String::AllocFromModifiedUtf8(self, class_name_string.c_str())); |
| 3105 | if (class_name_object == nullptr) { |
| 3106 | DCHECK(self->IsExceptionPending()); // OOME. |
| 3107 | return nullptr; |
Nicolas Geoffray | c9d5949 | 2022-11-17 15:03:09 +0000 | [diff] [blame] | 3108 | } |
Vladimir Marko | b6f965d | 2022-11-23 14:24:45 +0000 | [diff] [blame] | 3109 | DCHECK(class_loader != nullptr); |
| 3110 | result_ptr = ObjPtr<mirror::Class>::DownCast( |
| 3111 | WellKnownClasses::java_lang_ClassLoader_loadClass->InvokeVirtual<'L', 'L'>( |
| 3112 | self, class_loader.Get(), class_name_object.Get())); |
| 3113 | if (result_ptr == nullptr && !self->IsExceptionPending()) { |
Nicolas Geoffray | c9d5949 | 2022-11-17 15:03:09 +0000 | [diff] [blame] | 3114 | // broken loader - throw NPE to be compatible with Dalvik |
| 3115 | ThrowNullPointerException(StringPrintf("ClassLoader.loadClass returned null for %s", |
| 3116 | class_name_string.c_str()).c_str()); |
| 3117 | return nullptr; |
| 3118 | } |
Nicolas Geoffray | c9d5949 | 2022-11-17 15:03:09 +0000 | [diff] [blame] | 3119 | // Check the name of the returned class. |
| 3120 | descriptor_equals = (result_ptr != nullptr) && result_ptr->DescriptorEquals(descriptor); |
| 3121 | } |
| 3122 | } else { |
| 3123 | DCHECK(!MatchesDexFileCaughtExceptions(self->GetException(), this)); |
| 3124 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3125 | } |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 3126 | |
| 3127 | if (self->IsExceptionPending()) { |
| 3128 | // If the ClassLoader threw or array class allocation failed, pass that exception up. |
| 3129 | // However, to comply with the RI behavior, first check if another thread succeeded. |
| 3130 | result_ptr = LookupClass(self, descriptor, hash, class_loader.Get()); |
| 3131 | if (result_ptr != nullptr && !result_ptr->IsErroneous()) { |
| 3132 | self->ClearException(); |
| 3133 | return EnsureResolved(self, descriptor, result_ptr); |
| 3134 | } |
| 3135 | return nullptr; |
| 3136 | } |
| 3137 | |
| 3138 | // Try to insert the class to the class table, checking for mismatch. |
| 3139 | ObjPtr<mirror::Class> old; |
| 3140 | { |
| 3141 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 3142 | ClassTable* const class_table = InsertClassTableForClassLoader(class_loader.Get()); |
| 3143 | old = class_table->Lookup(descriptor, hash); |
| 3144 | if (old == nullptr) { |
| 3145 | old = result_ptr; // For the comparison below, after releasing the lock. |
| 3146 | if (descriptor_equals) { |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 3147 | class_table->InsertWithHash(result_ptr, hash); |
Mathieu Chartier | 88ea61e | 2018-06-20 17:45:41 -0700 | [diff] [blame] | 3148 | WriteBarrier::ForEveryFieldWrite(class_loader.Get()); |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 3149 | } // else throw below, after releasing the lock. |
| 3150 | } |
| 3151 | } |
| 3152 | if (UNLIKELY(old != result_ptr)) { |
| 3153 | // Return `old` (even if `!descriptor_equals`) to mimic the RI behavior for parallel |
| 3154 | // capable class loaders. (All class loaders are considered parallel capable on Android.) |
Vladimir Marko | dfc0de7 | 2019-04-01 10:57:55 +0100 | [diff] [blame] | 3155 | ObjPtr<mirror::Class> loader_class = class_loader->GetClass(); |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 3156 | const char* loader_class_name = |
| 3157 | loader_class->GetDexFile().StringByTypeIdx(loader_class->GetDexTypeIndex()); |
| 3158 | LOG(WARNING) << "Initiating class loader of type " << DescriptorToDot(loader_class_name) |
| 3159 | << " is not well-behaved; it returned a different Class for racing loadClass(\"" |
| 3160 | << DescriptorToDot(descriptor) << "\")."; |
| 3161 | return EnsureResolved(self, descriptor, old); |
| 3162 | } |
| 3163 | if (UNLIKELY(!descriptor_equals)) { |
| 3164 | std::string result_storage; |
| 3165 | const char* result_name = result_ptr->GetDescriptor(&result_storage); |
| 3166 | std::string loader_storage; |
| 3167 | const char* loader_class_name = class_loader->GetClass()->GetDescriptor(&loader_storage); |
| 3168 | ThrowNoClassDefFoundError( |
| 3169 | "Initiating class loader of type %s returned class %s instead of %s.", |
| 3170 | DescriptorToDot(loader_class_name).c_str(), |
| 3171 | DescriptorToDot(result_name).c_str(), |
| 3172 | DescriptorToDot(descriptor).c_str()); |
| 3173 | return nullptr; |
| 3174 | } |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 3175 | // Success. |
| 3176 | return result_ptr; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3177 | } |
| 3178 | |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 3179 | // Helper for maintaining DefineClass counting. We need to notify callbacks when we start/end a |
| 3180 | // define-class and how many recursive DefineClasses we are at in order to allow for doing things |
| 3181 | // like pausing class definition. |
| 3182 | struct ScopedDefiningClass { |
| 3183 | public: |
| 3184 | explicit ScopedDefiningClass(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_) |
| 3185 | : self_(self), returned_(false) { |
| 3186 | Locks::mutator_lock_->AssertSharedHeld(self_); |
| 3187 | Runtime::Current()->GetRuntimeCallbacks()->BeginDefineClass(); |
| 3188 | self_->IncrDefineClassCount(); |
| 3189 | } |
| 3190 | ~ScopedDefiningClass() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 3191 | Locks::mutator_lock_->AssertSharedHeld(self_); |
| 3192 | CHECK(returned_); |
| 3193 | } |
| 3194 | |
| 3195 | ObjPtr<mirror::Class> Finish(Handle<mirror::Class> h_klass) |
| 3196 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 3197 | CHECK(!returned_); |
| 3198 | self_->DecrDefineClassCount(); |
| 3199 | Runtime::Current()->GetRuntimeCallbacks()->EndDefineClass(); |
| 3200 | Thread::PoisonObjectPointersIfDebug(); |
| 3201 | returned_ = true; |
| 3202 | return h_klass.Get(); |
| 3203 | } |
| 3204 | |
| 3205 | ObjPtr<mirror::Class> Finish(ObjPtr<mirror::Class> klass) |
| 3206 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 3207 | StackHandleScope<1> hs(self_); |
| 3208 | Handle<mirror::Class> h_klass(hs.NewHandle(klass)); |
| 3209 | return Finish(h_klass); |
| 3210 | } |
| 3211 | |
| 3212 | ObjPtr<mirror::Class> Finish(nullptr_t np ATTRIBUTE_UNUSED) |
| 3213 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 3214 | ScopedNullHandle<mirror::Class> snh; |
| 3215 | return Finish(snh); |
| 3216 | } |
| 3217 | |
| 3218 | private: |
| 3219 | Thread* self_; |
| 3220 | bool returned_; |
| 3221 | }; |
| 3222 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 3223 | ObjPtr<mirror::Class> ClassLinker::DefineClass(Thread* self, |
| 3224 | const char* descriptor, |
| 3225 | size_t hash, |
| 3226 | Handle<mirror::ClassLoader> class_loader, |
| 3227 | const DexFile& dex_file, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 3228 | const dex::ClassDef& dex_class_def) { |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 3229 | ScopedDefiningClass sdc(self); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3230 | StackHandleScope<3> hs(self); |
Eric Holk | 74584e6 | 2021-02-18 14:39:17 -0800 | [diff] [blame] | 3231 | metrics::AutoTimer timer{GetMetrics()->ClassLoadingTotalTime()}; |
Stefano Cianciulli | f6f5ada | 2022-10-07 14:27:34 +0000 | [diff] [blame] | 3232 | metrics::AutoTimer timeDelta{GetMetrics()->ClassLoadingTotalTimeDelta()}; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3233 | auto klass = hs.NewHandle<mirror::Class>(nullptr); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3234 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3235 | // Load the class from the dex file. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3236 | if (UNLIKELY(!init_done_)) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3237 | // finish up init of hand crafted class_roots_ |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3238 | if (strcmp(descriptor, "Ljava/lang/Object;") == 0) { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 3239 | klass.Assign(GetClassRoot<mirror::Object>(this)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3240 | } else if (strcmp(descriptor, "Ljava/lang/Class;") == 0) { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 3241 | klass.Assign(GetClassRoot<mirror::Class>(this)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3242 | } else if (strcmp(descriptor, "Ljava/lang/String;") == 0) { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 3243 | klass.Assign(GetClassRoot<mirror::String>(this)); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 3244 | } else if (strcmp(descriptor, "Ljava/lang/ref/Reference;") == 0) { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 3245 | klass.Assign(GetClassRoot<mirror::Reference>(this)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3246 | } else if (strcmp(descriptor, "Ljava/lang/DexCache;") == 0) { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 3247 | klass.Assign(GetClassRoot<mirror::DexCache>(this)); |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 3248 | } else if (strcmp(descriptor, "Ldalvik/system/ClassExt;") == 0) { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 3249 | klass.Assign(GetClassRoot<mirror::ClassExt>(this)); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3250 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3251 | } |
| 3252 | |
Calin Juravle | 3378768 | 2019-07-26 14:27:18 -0700 | [diff] [blame] | 3253 | // For AOT-compilation of an app, we may use only a public SDK to resolve symbols. If the SDK |
| 3254 | // checks are configured (a non null SdkChecker) and the descriptor is not in the provided |
| 3255 | // public class path then we prevent the definition of the class. |
| 3256 | // |
| 3257 | // NOTE that we only do the checks for the boot classpath APIs. Anything else, like the app |
| 3258 | // classpath is not checked. |
| 3259 | if (class_loader == nullptr && |
| 3260 | Runtime::Current()->IsAotCompiler() && |
| 3261 | DenyAccessBasedOnPublicSdk(descriptor)) { |
| 3262 | ObjPtr<mirror::Throwable> pre_allocated = |
| 3263 | Runtime::Current()->GetPreAllocatedNoClassDefFoundError(); |
| 3264 | self->SetException(pre_allocated); |
| 3265 | return sdc.Finish(nullptr); |
| 3266 | } |
| 3267 | |
Alex Light | e9f6103 | 2018-09-24 16:04:51 -0700 | [diff] [blame] | 3268 | // This is to prevent the calls to ClassLoad and ClassPrepare which can cause java/user-supplied |
| 3269 | // code to be executed. We put it up here so we can avoid all the allocations associated with |
| 3270 | // creating the class. This can happen with (eg) jit threads. |
| 3271 | if (!self->CanLoadClasses()) { |
| 3272 | // Make sure we don't try to load anything, potentially causing an infinite loop. |
| 3273 | ObjPtr<mirror::Throwable> pre_allocated = |
| 3274 | Runtime::Current()->GetPreAllocatedNoClassDefFoundError(); |
| 3275 | self->SetException(pre_allocated); |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 3276 | return sdc.Finish(nullptr); |
Alex Light | e9f6103 | 2018-09-24 16:04:51 -0700 | [diff] [blame] | 3277 | } |
| 3278 | |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3279 | if (klass == nullptr) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3280 | // Allocate a class with the status of not ready. |
| 3281 | // Interface object should get the right size here. Regular class will |
| 3282 | // figure out the right size later and be replaced with one of the right |
| 3283 | // size when the class becomes resolved. |
Chang Xing | 0c2c222 | 2017-08-04 14:36:17 -0700 | [diff] [blame] | 3284 | if (CanAllocClass()) { |
| 3285 | klass.Assign(AllocClass(self, SizeOfClassWithoutEmbeddedTables(dex_file, dex_class_def))); |
| 3286 | } else { |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 3287 | return sdc.Finish(nullptr); |
Chang Xing | 0c2c222 | 2017-08-04 14:36:17 -0700 | [diff] [blame] | 3288 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3289 | } |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3290 | if (UNLIKELY(klass == nullptr)) { |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3291 | self->AssertPendingOOMException(); |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 3292 | return sdc.Finish(nullptr); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3293 | } |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 3294 | // Get the real dex file. This will return the input if there aren't any callbacks or they do |
| 3295 | // nothing. |
| 3296 | DexFile const* new_dex_file = nullptr; |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 3297 | dex::ClassDef const* new_class_def = nullptr; |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 3298 | // TODO We should ideally figure out some way to move this after we get a lock on the klass so it |
| 3299 | // will only be called once. |
| 3300 | Runtime::Current()->GetRuntimeCallbacks()->ClassPreDefine(descriptor, |
| 3301 | klass, |
| 3302 | class_loader, |
| 3303 | dex_file, |
| 3304 | dex_class_def, |
| 3305 | &new_dex_file, |
| 3306 | &new_class_def); |
Alex Light | 440b5d9 | 2017-01-24 15:32:25 -0800 | [diff] [blame] | 3307 | // Check to see if an exception happened during runtime callbacks. Return if so. |
| 3308 | if (self->IsExceptionPending()) { |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 3309 | return sdc.Finish(nullptr); |
Alex Light | 440b5d9 | 2017-01-24 15:32:25 -0800 | [diff] [blame] | 3310 | } |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 3311 | ObjPtr<mirror::DexCache> dex_cache = RegisterDexFile(*new_dex_file, class_loader.Get()); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3312 | if (dex_cache == nullptr) { |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3313 | self->AssertPendingException(); |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 3314 | return sdc.Finish(nullptr); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3315 | } |
| 3316 | klass->SetDexCache(dex_cache); |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 3317 | SetupClass(*new_dex_file, *new_class_def, klass, class_loader.Get()); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3318 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 3319 | // Mark the string class by setting its access flag. |
| 3320 | if (UNLIKELY(!init_done_)) { |
| 3321 | if (strcmp(descriptor, "Ljava/lang/String;") == 0) { |
| 3322 | klass->SetStringClass(); |
| 3323 | } |
| 3324 | } |
| 3325 | |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 3326 | ObjectLock<mirror::Class> lock(self, klass); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3327 | klass->SetClinitThreadId(self->GetTid()); |
Mathieu Chartier | 1e4841e | 2016-12-15 14:21:04 -0800 | [diff] [blame] | 3328 | // 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] | 3329 | klass->SetIfTable(GetClassRoot<mirror::Object>(this)->GetIfTable()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3330 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3331 | // Add the newly loaded class to the loaded classes table. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3332 | ObjPtr<mirror::Class> existing = InsertClass(descriptor, klass.Get(), hash); |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 3333 | if (existing != nullptr) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3334 | // We failed to insert because we raced with another thread. Calling EnsureResolved may cause |
| 3335 | // this thread to block. |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 3336 | return sdc.Finish(EnsureResolved(self, descriptor, existing)); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3337 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3338 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3339 | // Load the fields and other things after we are inserted in the table. This is so that we don't |
| 3340 | // end up allocating unfree-able linear alloc resources and then lose the race condition. The |
| 3341 | // other reason is that the field roots are only visited from the class table. So we need to be |
| 3342 | // inserted before we allocate / fill in these fields. |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 3343 | LoadClass(self, *new_dex_file, *new_class_def, klass); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3344 | if (self->IsExceptionPending()) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 3345 | VLOG(class_linker) << self->GetException()->Dump(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3346 | // An exception occured during load, set status to erroneous while holding klass' lock in case |
| 3347 | // notification is necessary. |
| 3348 | if (!klass->IsErroneous()) { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 3349 | mirror::Class::SetStatus(klass, ClassStatus::kErrorUnresolved, self); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3350 | } |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 3351 | return sdc.Finish(nullptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3352 | } |
| 3353 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3354 | // Finish loading (if necessary) by finding parents |
| 3355 | CHECK(!klass->IsLoaded()); |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 3356 | if (!LoadSuperAndInterfaces(klass, *new_dex_file)) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3357 | // Loading failed. |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 3358 | if (!klass->IsErroneous()) { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 3359 | mirror::Class::SetStatus(klass, ClassStatus::kErrorUnresolved, self); |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 3360 | } |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 3361 | return sdc.Finish(nullptr); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3362 | } |
| 3363 | CHECK(klass->IsLoaded()); |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 3364 | |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 3365 | // At this point the class is loaded. Publish a ClassLoad event. |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 3366 | // 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] | 3367 | Runtime::Current()->GetRuntimeCallbacks()->ClassLoad(klass); |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 3368 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3369 | // Link the class (if necessary) |
| 3370 | CHECK(!klass->IsResolved()); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3371 | // TODO: Use fast jobjects? |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3372 | auto interfaces = hs.NewHandle<mirror::ObjectArray<mirror::Class>>(nullptr); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3373 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 3374 | MutableHandle<mirror::Class> h_new_class = hs.NewHandle<mirror::Class>(nullptr); |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 3375 | if (!LinkClass(self, descriptor, klass, interfaces, &h_new_class)) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3376 | // Linking failed. |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 3377 | if (!klass->IsErroneous()) { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 3378 | mirror::Class::SetStatus(klass, ClassStatus::kErrorUnresolved, self); |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 3379 | } |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 3380 | return sdc.Finish(nullptr); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3381 | } |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 3382 | self->AssertNoPendingException(); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3383 | CHECK(h_new_class != nullptr) << descriptor; |
Vladimir Marko | f9e82e5 | 2021-10-06 08:34:40 +0100 | [diff] [blame] | 3384 | CHECK(h_new_class->IsResolved()) << descriptor << " " << h_new_class->GetStatus(); |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 3385 | |
Sebastien Hertz | a8a697f | 2015-01-15 12:28:47 +0100 | [diff] [blame] | 3386 | // Instrumentation may have updated entrypoints for all methods of all |
| 3387 | // classes. However it could not update methods of this class while we |
| 3388 | // were loading it. Now the class is resolved, we can update entrypoints |
| 3389 | // as required by instrumentation. |
Mythri Alle | e635155 | 2023-01-12 14:48:45 +0000 | [diff] [blame] | 3390 | if (Runtime::Current()->GetInstrumentation()->EntryExitStubsInstalled()) { |
Sebastien Hertz | a8a697f | 2015-01-15 12:28:47 +0100 | [diff] [blame] | 3391 | // We must be in the kRunnable state to prevent instrumentation from |
| 3392 | // suspending all threads to update entrypoints while we are doing it |
| 3393 | // for this class. |
Vladimir Marko | ddf4fd3 | 2021-11-22 16:31:57 +0000 | [diff] [blame] | 3394 | DCHECK_EQ(self->GetState(), ThreadState::kRunnable); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 3395 | Runtime::Current()->GetInstrumentation()->InstallStubsForClass(h_new_class.Get()); |
Sebastien Hertz | a8a697f | 2015-01-15 12:28:47 +0100 | [diff] [blame] | 3396 | } |
| 3397 | |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 3398 | /* |
| 3399 | * We send CLASS_PREPARE events to the debugger from here. The |
| 3400 | * definition of "preparation" is creating the static fields for a |
| 3401 | * class and initializing them to the standard default values, but not |
| 3402 | * executing any code (that comes later, during "initialization"). |
| 3403 | * |
| 3404 | * We did the static preparation in LinkClass. |
| 3405 | * |
| 3406 | * The class has been prepared and resolved but possibly not yet verified |
| 3407 | * at this point. |
| 3408 | */ |
Andreas Gampe | ac30fa2 | 2017-01-18 21:02:36 -0800 | [diff] [blame] | 3409 | Runtime::Current()->GetRuntimeCallbacks()->ClassPrepare(klass, h_new_class); |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 3410 | |
Tamas Berghammer | 160e6df | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 3411 | // Notify native debugger of the new class and its layout. |
| 3412 | jit::Jit::NewTypeLoadedIfUsingJit(h_new_class.Get()); |
| 3413 | |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 3414 | return sdc.Finish(h_new_class); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3415 | } |
| 3416 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3417 | uint32_t ClassLinker::SizeOfClassWithoutEmbeddedTables(const DexFile& dex_file, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 3418 | const dex::ClassDef& dex_class_def) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 3419 | size_t num_ref = 0; |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 3420 | size_t num_8 = 0; |
| 3421 | size_t num_16 = 0; |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 3422 | size_t num_32 = 0; |
| 3423 | size_t num_64 = 0; |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3424 | ClassAccessor accessor(dex_file, dex_class_def); |
| 3425 | // We allow duplicate definitions of the same field in a class_data_item |
| 3426 | // but ignore the repeated indexes here, b/21868015. |
| 3427 | uint32_t last_field_idx = dex::kDexNoIndex; |
| 3428 | for (const ClassAccessor::Field& field : accessor.GetStaticFields()) { |
| 3429 | uint32_t field_idx = field.GetIndex(); |
| 3430 | // Ordering enforced by DexFileVerifier. |
| 3431 | DCHECK(last_field_idx == dex::kDexNoIndex || last_field_idx <= field_idx); |
| 3432 | if (UNLIKELY(field_idx == last_field_idx)) { |
| 3433 | continue; |
| 3434 | } |
| 3435 | last_field_idx = field_idx; |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 3436 | const dex::FieldId& field_id = dex_file.GetFieldId(field_idx); |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3437 | const char* descriptor = dex_file.GetFieldTypeDescriptor(field_id); |
| 3438 | char c = descriptor[0]; |
| 3439 | switch (c) { |
| 3440 | case 'L': |
| 3441 | case '[': |
| 3442 | num_ref++; |
| 3443 | break; |
| 3444 | case 'J': |
| 3445 | case 'D': |
| 3446 | num_64++; |
| 3447 | break; |
| 3448 | case 'I': |
| 3449 | case 'F': |
| 3450 | num_32++; |
| 3451 | break; |
| 3452 | case 'S': |
| 3453 | case 'C': |
| 3454 | num_16++; |
| 3455 | break; |
| 3456 | case 'B': |
| 3457 | case 'Z': |
| 3458 | num_8++; |
| 3459 | break; |
| 3460 | default: |
| 3461 | LOG(FATAL) << "Unknown descriptor: " << c; |
| 3462 | UNREACHABLE(); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 3463 | } |
| 3464 | } |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3465 | return mirror::Class::ComputeClassSize(false, |
| 3466 | 0, |
| 3467 | num_8, |
| 3468 | num_16, |
| 3469 | num_32, |
| 3470 | num_64, |
| 3471 | num_ref, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3472 | image_pointer_size_); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 3473 | } |
| 3474 | |
Vladimir Marko | 86c8752 | 2020-05-11 16:55:55 +0100 | [diff] [blame] | 3475 | void ClassLinker::FixupStaticTrampolines(Thread* self, ObjPtr<mirror::Class> klass) { |
Alex Light | 2d441b1 | 2018-06-08 15:33:21 -0700 | [diff] [blame] | 3476 | ScopedAssertNoThreadSuspension sants(__FUNCTION__); |
Vladimir Marko | cce414f | 2019-10-07 08:51:33 +0100 | [diff] [blame] | 3477 | DCHECK(klass->IsVisiblyInitialized()) << klass->PrettyDescriptor(); |
Vladimir Marko | 86c8752 | 2020-05-11 16:55:55 +0100 | [diff] [blame] | 3478 | size_t num_direct_methods = klass->NumDirectMethods(); |
| 3479 | if (num_direct_methods == 0) { |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 3480 | return; // No direct methods => no static methods. |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3481 | } |
Vladimir Marko | cce414f | 2019-10-07 08:51:33 +0100 | [diff] [blame] | 3482 | if (UNLIKELY(klass->IsProxyClass())) { |
| 3483 | return; |
| 3484 | } |
Vladimir Marko | 86c8752 | 2020-05-11 16:55:55 +0100 | [diff] [blame] | 3485 | PointerSize pointer_size = image_pointer_size_; |
| 3486 | if (std::any_of(klass->GetDirectMethods(pointer_size).begin(), |
| 3487 | klass->GetDirectMethods(pointer_size).end(), |
| 3488 | [](const ArtMethod& m) { return m.IsCriticalNative(); })) { |
| 3489 | // Store registered @CriticalNative methods, if any, to JNI entrypoints. |
| 3490 | // Direct methods are a contiguous chunk of memory, so use the ordering of the map. |
| 3491 | ArtMethod* first_method = klass->GetDirectMethod(0u, pointer_size); |
| 3492 | ArtMethod* last_method = klass->GetDirectMethod(num_direct_methods - 1u, pointer_size); |
| 3493 | MutexLock lock(self, critical_native_code_with_clinit_check_lock_); |
| 3494 | auto lb = critical_native_code_with_clinit_check_.lower_bound(first_method); |
| 3495 | while (lb != critical_native_code_with_clinit_check_.end() && lb->first <= last_method) { |
| 3496 | lb->first->SetEntryPointFromJni(lb->second); |
| 3497 | lb = critical_native_code_with_clinit_check_.erase(lb); |
| 3498 | } |
| 3499 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3500 | Runtime* runtime = Runtime::Current(); |
Nicolas Geoffray | 854af03 | 2021-12-21 08:32:42 +0000 | [diff] [blame] | 3501 | if (runtime->IsAotCompiler()) { |
| 3502 | // We should not update entrypoints when running the transactional |
| 3503 | // interpreter. |
| 3504 | return; |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3505 | } |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 3506 | |
Nicolas Geoffray | c8a694d | 2022-01-17 17:12:38 +0000 | [diff] [blame] | 3507 | instrumentation::Instrumentation* instrumentation = runtime->GetInstrumentation(); |
Vladimir Marko | 86c8752 | 2020-05-11 16:55:55 +0100 | [diff] [blame] | 3508 | for (size_t method_index = 0; method_index < num_direct_methods; ++method_index) { |
| 3509 | ArtMethod* method = klass->GetDirectMethod(method_index, pointer_size); |
Vladimir Marko | a20ec9b | 2022-12-12 09:12:21 +0000 | [diff] [blame] | 3510 | if (method->NeedsClinitCheckBeforeCall()) { |
| 3511 | instrumentation->UpdateMethodsCode(method, instrumentation->GetCodeForInvoke(method)); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3512 | } |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3513 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3514 | // Ignore virtual methods on the iterator. |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3515 | } |
| 3516 | |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3517 | // Does anything needed to make sure that the compiler will not generate a direct invoke to this |
| 3518 | // method. Should only be called on non-invokable methods. |
Nicolas Geoffray | f05f04b | 2019-10-31 11:50:41 +0000 | [diff] [blame] | 3519 | inline void EnsureThrowsInvocationError(ClassLinker* class_linker, ArtMethod* method) |
| 3520 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 3521 | DCHECK(method != nullptr); |
| 3522 | DCHECK(!method->IsInvokable()); |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3523 | method->SetEntryPointFromQuickCompiledCodePtrSize( |
| 3524 | class_linker->GetQuickToInterpreterBridgeTrampoline(), |
| 3525 | class_linker->GetImagePointerSize()); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 3526 | } |
| 3527 | |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3528 | static void LinkCode(ClassLinker* class_linker, |
| 3529 | ArtMethod* method, |
| 3530 | const OatFile::OatClass* oat_class, |
| 3531 | uint32_t class_def_method_index) REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | 2d441b1 | 2018-06-08 15:33:21 -0700 | [diff] [blame] | 3532 | ScopedAssertNoThreadSuspension sants(__FUNCTION__); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3533 | Runtime* const runtime = Runtime::Current(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 3534 | if (runtime->IsAotCompiler()) { |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 3535 | // The following code only applies to a non-compiler runtime. |
| 3536 | return; |
| 3537 | } |
Nicolas Geoffray | 5ee206f | 2019-10-08 15:09:17 +0100 | [diff] [blame] | 3538 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3539 | // Method shouldn't have already been linked. |
Lokesh Gidra | 485a714 | 2022-10-12 10:25:23 -0700 | [diff] [blame] | 3540 | DCHECK_EQ(method->GetEntryPointFromQuickCompiledCode(), nullptr); |
Nicolas Geoffray | 854af03 | 2021-12-21 08:32:42 +0000 | [diff] [blame] | 3541 | DCHECK(!method->GetDeclaringClass()->IsVisiblyInitialized()); // Actually ClassStatus::Idx. |
Jeff Hao | 1674363 | 2013-05-08 10:59:04 -0700 | [diff] [blame] | 3542 | |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 3543 | if (!method->IsInvokable()) { |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3544 | EnsureThrowsInvocationError(class_linker, method); |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 3545 | return; |
| 3546 | } |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3547 | |
Nicolas Geoffray | 5ee206f | 2019-10-08 15:09:17 +0100 | [diff] [blame] | 3548 | const void* quick_code = nullptr; |
| 3549 | if (oat_class != nullptr) { |
| 3550 | // Every kind of method should at least get an invoke stub from the oat_method. |
| 3551 | // non-abstract methods also get their code pointers. |
| 3552 | const OatFile::OatMethod oat_method = oat_class->GetOatMethod(class_def_method_index); |
| 3553 | quick_code = oat_method.GetQuickCode(); |
| 3554 | } |
Nicolas Geoffray | 854af03 | 2021-12-21 08:32:42 +0000 | [diff] [blame] | 3555 | runtime->GetInstrumentation()->InitializeMethodsCode(method, quick_code); |
jeffhao | 26c0a1a | 2012-01-17 16:28:33 -0800 | [diff] [blame] | 3556 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3557 | if (method->IsNative()) { |
Vladimir Marko | 86c8752 | 2020-05-11 16:55:55 +0100 | [diff] [blame] | 3558 | // Set up the dlsym lookup stub. Do not go through `UnregisterNative()` |
| 3559 | // as the extra processing for @CriticalNative is not needed yet. |
| 3560 | method->SetEntryPointFromJni( |
| 3561 | method->IsCriticalNative() ? GetJniDlsymLookupCriticalStub() : GetJniDlsymLookupStub()); |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 3562 | } |
| 3563 | } |
| 3564 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3565 | void ClassLinker::SetupClass(const DexFile& dex_file, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 3566 | const dex::ClassDef& dex_class_def, |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3567 | Handle<mirror::Class> klass, |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3568 | ObjPtr<mirror::ClassLoader> class_loader) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3569 | CHECK(klass != nullptr); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3570 | CHECK(klass->GetDexCache() != nullptr); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 3571 | CHECK_EQ(ClassStatus::kNotReady, klass->GetStatus()); |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 3572 | const char* descriptor = dex_file.GetClassDescriptor(dex_class_def); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3573 | CHECK(descriptor != nullptr); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3574 | |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 3575 | klass->SetClass(GetClassRoot<mirror::Class>(this)); |
Andreas Gampe | 5182932 | 2014-08-25 15:05:04 -0700 | [diff] [blame] | 3576 | uint32_t access_flags = dex_class_def.GetJavaAccessFlags(); |
Brian Carlstrom | 8e3fb14 | 2013-10-09 21:00:27 -0700 | [diff] [blame] | 3577 | CHECK_EQ(access_flags & ~kAccJavaFlagsMask, 0U); |
Vladimir Marko | b68bb7a | 2020-03-17 10:55:25 +0000 | [diff] [blame] | 3578 | klass->SetAccessFlagsDuringLinking(access_flags); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3579 | klass->SetClassLoader(class_loader); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3580 | DCHECK_EQ(klass->GetPrimitiveType(), Primitive::kPrimNot); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 3581 | mirror::Class::SetStatus(klass, ClassStatus::kIdx, nullptr); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3582 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 3583 | klass->SetDexClassDefIndex(dex_file.GetIndexForClassDef(dex_class_def)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3584 | klass->SetDexTypeIndex(dex_class_def.class_idx_); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3585 | } |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3586 | |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3587 | LengthPrefixedArray<ArtField>* ClassLinker::AllocArtFieldArray(Thread* self, |
| 3588 | LinearAlloc* allocator, |
| 3589 | size_t length) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3590 | if (length == 0) { |
| 3591 | return nullptr; |
| 3592 | } |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 3593 | // If the ArtField alignment changes, review all uses of LengthPrefixedArray<ArtField>. |
| 3594 | static_assert(alignof(ArtField) == 4, "ArtField alignment is expected to be 4."); |
| 3595 | size_t storage_size = LengthPrefixedArray<ArtField>::ComputeSize(length); |
Lokesh Gidra | 606bd94 | 2022-05-23 19:00:09 +0000 | [diff] [blame] | 3596 | void* array_storage = allocator->Alloc(self, storage_size, LinearAllocKind::kArtFieldArray); |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 3597 | auto* ret = new(array_storage) LengthPrefixedArray<ArtField>(length); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3598 | CHECK(ret != nullptr); |
| 3599 | std::uninitialized_fill_n(&ret->At(0), length, ArtField()); |
| 3600 | return ret; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3601 | } |
| 3602 | |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3603 | LengthPrefixedArray<ArtMethod>* ClassLinker::AllocArtMethodArray(Thread* self, |
| 3604 | LinearAlloc* allocator, |
| 3605 | size_t length) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3606 | if (length == 0) { |
| 3607 | return nullptr; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3608 | } |
Vladimir Marko | 1463285 | 2015-08-17 12:07:23 +0100 | [diff] [blame] | 3609 | const size_t method_alignment = ArtMethod::Alignment(image_pointer_size_); |
| 3610 | const size_t method_size = ArtMethod::Size(image_pointer_size_); |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 3611 | const size_t storage_size = |
| 3612 | LengthPrefixedArray<ArtMethod>::ComputeSize(length, method_size, method_alignment); |
Lokesh Gidra | 606bd94 | 2022-05-23 19:00:09 +0000 | [diff] [blame] | 3613 | void* array_storage = allocator->Alloc(self, storage_size, LinearAllocKind::kArtMethodArray); |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 3614 | auto* ret = new (array_storage) LengthPrefixedArray<ArtMethod>(length); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3615 | CHECK(ret != nullptr); |
| 3616 | for (size_t i = 0; i < length; ++i) { |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 3617 | new(reinterpret_cast<void*>(&ret->At(i, method_size, method_alignment))) ArtMethod; |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3618 | } |
| 3619 | return ret; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3620 | } |
| 3621 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3622 | LinearAlloc* ClassLinker::GetAllocatorForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3623 | if (class_loader == nullptr) { |
| 3624 | return Runtime::Current()->GetLinearAlloc(); |
| 3625 | } |
| 3626 | LinearAlloc* allocator = class_loader->GetAllocator(); |
| 3627 | DCHECK(allocator != nullptr); |
| 3628 | return allocator; |
| 3629 | } |
| 3630 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3631 | LinearAlloc* ClassLinker::GetOrCreateAllocatorForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 3632 | if (class_loader == nullptr) { |
| 3633 | return Runtime::Current()->GetLinearAlloc(); |
| 3634 | } |
| 3635 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
| 3636 | LinearAlloc* allocator = class_loader->GetAllocator(); |
| 3637 | if (allocator == nullptr) { |
Mathieu Chartier | 5b83050 | 2016-03-02 10:30:23 -0800 | [diff] [blame] | 3638 | RegisterClassLoader(class_loader); |
| 3639 | allocator = class_loader->GetAllocator(); |
| 3640 | CHECK(allocator != nullptr); |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 3641 | } |
| 3642 | return allocator; |
| 3643 | } |
| 3644 | |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3645 | void ClassLinker::LoadClass(Thread* self, |
| 3646 | const DexFile& dex_file, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 3647 | const dex::ClassDef& dex_class_def, |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3648 | Handle<mirror::Class> klass) { |
David Brazdil | 20c765f | 2018-10-27 21:45:15 +0000 | [diff] [blame] | 3649 | ClassAccessor accessor(dex_file, |
| 3650 | dex_class_def, |
| 3651 | /* parse_hiddenapi_class_data= */ klass->IsBootStrapClassLoaded()); |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3652 | if (!accessor.HasClassData()) { |
| 3653 | return; |
| 3654 | } |
| 3655 | Runtime* const runtime = Runtime::Current(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3656 | { |
| 3657 | // Note: We cannot have thread suspension until the field and method arrays are setup or else |
| 3658 | // Class::VisitFieldRoots may miss some fields or methods. |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 3659 | ScopedAssertNoThreadSuspension nts(__FUNCTION__); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3660 | // Load static fields. |
Vladimir Marko | 23682bf | 2015-06-24 14:28:03 +0100 | [diff] [blame] | 3661 | // We allow duplicate definitions of the same field in a class_data_item |
| 3662 | // but ignore the repeated indexes here, b/21868015. |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3663 | LinearAlloc* const allocator = GetAllocatorForClassLoader(klass->GetClassLoader()); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3664 | LengthPrefixedArray<ArtField>* sfields = AllocArtFieldArray(self, |
| 3665 | allocator, |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3666 | accessor.NumStaticFields()); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3667 | LengthPrefixedArray<ArtField>* ifields = AllocArtFieldArray(self, |
| 3668 | allocator, |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3669 | accessor.NumInstanceFields()); |
| 3670 | size_t num_sfields = 0u; |
Vladimir Marko | 23682bf | 2015-06-24 14:28:03 +0100 | [diff] [blame] | 3671 | size_t num_ifields = 0u; |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3672 | uint32_t last_static_field_idx = 0u; |
| 3673 | uint32_t last_instance_field_idx = 0u; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 3674 | |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3675 | // Methods |
| 3676 | bool has_oat_class = false; |
| 3677 | const OatFile::OatClass oat_class = (runtime->IsStarted() && !runtime->IsAotCompiler()) |
| 3678 | ? OatFile::FindOatClass(dex_file, klass->GetDexClassDefIndex(), &has_oat_class) |
| 3679 | : OatFile::OatClass::Invalid(); |
| 3680 | const OatFile::OatClass* oat_class_ptr = has_oat_class ? &oat_class : nullptr; |
| 3681 | klass->SetMethodsPtr( |
| 3682 | AllocArtMethodArray(self, allocator, accessor.NumMethods()), |
| 3683 | accessor.NumDirectMethods(), |
| 3684 | accessor.NumVirtualMethods()); |
| 3685 | size_t class_def_method_index = 0; |
| 3686 | uint32_t last_dex_method_index = dex::kDexNoIndex; |
| 3687 | size_t last_class_def_method_index = 0; |
| 3688 | |
Nicolas Geoffray | 58f916c | 2021-11-15 14:02:07 +0000 | [diff] [blame] | 3689 | uint16_t hotness_threshold = runtime->GetJITOptions()->GetWarmupThreshold(); |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3690 | // Use the visitor since the ranged based loops are bit slower from seeking. Seeking to the |
| 3691 | // methods needs to decode all of the fields. |
| 3692 | accessor.VisitFieldsAndMethods([&]( |
| 3693 | const ClassAccessor::Field& field) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 3694 | uint32_t field_idx = field.GetIndex(); |
| 3695 | DCHECK_GE(field_idx, last_static_field_idx); // Ordering enforced by DexFileVerifier. |
| 3696 | if (num_sfields == 0 || LIKELY(field_idx > last_static_field_idx)) { |
| 3697 | LoadField(field, klass, &sfields->At(num_sfields)); |
| 3698 | ++num_sfields; |
| 3699 | last_static_field_idx = field_idx; |
| 3700 | } |
| 3701 | }, [&](const ClassAccessor::Field& field) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 3702 | uint32_t field_idx = field.GetIndex(); |
| 3703 | DCHECK_GE(field_idx, last_instance_field_idx); // Ordering enforced by DexFileVerifier. |
| 3704 | if (num_ifields == 0 || LIKELY(field_idx > last_instance_field_idx)) { |
| 3705 | LoadField(field, klass, &ifields->At(num_ifields)); |
| 3706 | ++num_ifields; |
| 3707 | last_instance_field_idx = field_idx; |
| 3708 | } |
| 3709 | }, [&](const ClassAccessor::Method& method) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 3710 | ArtMethod* art_method = klass->GetDirectMethodUnchecked(class_def_method_index, |
| 3711 | image_pointer_size_); |
Vladimir Marko | 05f1a5b | 2022-03-09 14:20:15 +0000 | [diff] [blame] | 3712 | LoadMethod(dex_file, method, klass.Get(), art_method); |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3713 | LinkCode(this, art_method, oat_class_ptr, class_def_method_index); |
| 3714 | uint32_t it_method_index = method.GetIndex(); |
| 3715 | if (last_dex_method_index == it_method_index) { |
| 3716 | // duplicate case |
| 3717 | art_method->SetMethodIndex(last_class_def_method_index); |
| 3718 | } else { |
| 3719 | art_method->SetMethodIndex(class_def_method_index); |
| 3720 | last_dex_method_index = it_method_index; |
| 3721 | last_class_def_method_index = class_def_method_index; |
| 3722 | } |
Nicolas Geoffray | 58f916c | 2021-11-15 14:02:07 +0000 | [diff] [blame] | 3723 | art_method->ResetCounter(hotness_threshold); |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3724 | ++class_def_method_index; |
| 3725 | }, [&](const ClassAccessor::Method& method) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 3726 | ArtMethod* art_method = klass->GetVirtualMethodUnchecked( |
| 3727 | class_def_method_index - accessor.NumDirectMethods(), |
| 3728 | image_pointer_size_); |
Nicolas Geoffray | 58f916c | 2021-11-15 14:02:07 +0000 | [diff] [blame] | 3729 | art_method->ResetCounter(hotness_threshold); |
Vladimir Marko | 05f1a5b | 2022-03-09 14:20:15 +0000 | [diff] [blame] | 3730 | LoadMethod(dex_file, method, klass.Get(), art_method); |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3731 | LinkCode(this, art_method, oat_class_ptr, class_def_method_index); |
| 3732 | ++class_def_method_index; |
| 3733 | }); |
| 3734 | |
| 3735 | if (UNLIKELY(num_ifields + num_sfields != accessor.NumFields())) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3736 | LOG(WARNING) << "Duplicate fields in class " << klass->PrettyDescriptor() |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3737 | << " (unique static fields: " << num_sfields << "/" << accessor.NumStaticFields() |
| 3738 | << ", unique instance fields: " << num_ifields << "/" << accessor.NumInstanceFields() |
| 3739 | << ")"; |
Vladimir Marko | 81819db | 2015-11-05 15:30:12 +0000 | [diff] [blame] | 3740 | // NOTE: Not shrinking the over-allocated sfields/ifields, just setting size. |
| 3741 | if (sfields != nullptr) { |
| 3742 | sfields->SetSize(num_sfields); |
| 3743 | } |
| 3744 | if (ifields != nullptr) { |
| 3745 | ifields->SetSize(num_ifields); |
| 3746 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3747 | } |
Vladimir Marko | 81819db | 2015-11-05 15:30:12 +0000 | [diff] [blame] | 3748 | // Set the field arrays. |
| 3749 | klass->SetSFieldsPtr(sfields); |
| 3750 | DCHECK_EQ(klass->NumStaticFields(), num_sfields); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3751 | klass->SetIFieldsPtr(ifields); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3752 | DCHECK_EQ(klass->NumInstanceFields(), num_ifields); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3753 | } |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 3754 | // 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] | 3755 | WriteBarrier::ForEveryFieldWrite(klass.Get()); |
Mathieu Chartier | f3f2a7a | 2015-04-14 15:43:10 -0700 | [diff] [blame] | 3756 | self->AllowThreadSuspension(); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3757 | } |
| 3758 | |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3759 | void ClassLinker::LoadField(const ClassAccessor::Field& field, |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3760 | Handle<mirror::Class> klass, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3761 | ArtField* dst) { |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3762 | const uint32_t field_idx = field.GetIndex(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3763 | dst->SetDexFieldIndex(field_idx); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3764 | dst->SetDeclaringClass(klass.Get()); |
David Brazdil | f6a8a55 | 2018-01-15 18:10:50 +0000 | [diff] [blame] | 3765 | |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 3766 | // Get access flags from the DexFile and set hiddenapi runtime access flags. |
| 3767 | dst->SetAccessFlags(field.GetAccessFlags() | hiddenapi::CreateRuntimeFlags(field)); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3768 | } |
| 3769 | |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 3770 | void ClassLinker::LoadMethod(const DexFile& dex_file, |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3771 | const ClassAccessor::Method& method, |
Vladimir Marko | 05f1a5b | 2022-03-09 14:20:15 +0000 | [diff] [blame] | 3772 | ObjPtr<mirror::Class> klass, |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3773 | ArtMethod* dst) { |
Vladimir Marko | 05f1a5b | 2022-03-09 14:20:15 +0000 | [diff] [blame] | 3774 | ScopedAssertNoThreadSuspension sants(__FUNCTION__); |
| 3775 | |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3776 | const uint32_t dex_method_idx = method.GetIndex(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 3777 | const dex::MethodId& method_id = dex_file.GetMethodId(dex_method_idx); |
Vladimir Marko | 05f1a5b | 2022-03-09 14:20:15 +0000 | [diff] [blame] | 3778 | uint32_t name_utf16_length; |
| 3779 | const char* method_name = dex_file.StringDataAndUtf16LengthByIdx(method_id.name_idx_, |
| 3780 | &name_utf16_length); |
| 3781 | std::string_view shorty = dex_file.GetShortyView(dex_file.GetProtoId(method_id.proto_idx_)); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 3782 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 3783 | dst->SetDexMethodIndex(dex_method_idx); |
Vladimir Marko | 05f1a5b | 2022-03-09 14:20:15 +0000 | [diff] [blame] | 3784 | dst->SetDeclaringClass(klass); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3785 | |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 3786 | // Get access flags from the DexFile and set hiddenapi runtime access flags. |
| 3787 | uint32_t access_flags = method.GetAccessFlags() | hiddenapi::CreateRuntimeFlags(method); |
David Brazdil | f6a8a55 | 2018-01-15 18:10:50 +0000 | [diff] [blame] | 3788 | |
Vladimir Marko | 05f1a5b | 2022-03-09 14:20:15 +0000 | [diff] [blame] | 3789 | auto has_ascii_name = [method_name, name_utf16_length](const char* ascii_name, |
| 3790 | size_t length) ALWAYS_INLINE { |
| 3791 | DCHECK_EQ(strlen(ascii_name), length); |
| 3792 | return length == name_utf16_length && |
| 3793 | method_name[length] == 0 && // Is `method_name` an ASCII string? |
| 3794 | memcmp(ascii_name, method_name, length) == 0; |
| 3795 | }; |
| 3796 | if (UNLIKELY(has_ascii_name("finalize", sizeof("finalize") - 1u))) { |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3797 | // Set finalizable flag on declaring class. |
Vladimir Marko | 05f1a5b | 2022-03-09 14:20:15 +0000 | [diff] [blame] | 3798 | if (shorty == "V") { |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 3799 | // Void return type. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3800 | if (klass->GetClassLoader() != nullptr) { // All non-boot finalizer methods are flagged. |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 3801 | klass->SetFinalizable(); |
| 3802 | } else { |
Vladimir Marko | 05f1a5b | 2022-03-09 14:20:15 +0000 | [diff] [blame] | 3803 | std::string_view klass_descriptor = |
| 3804 | dex_file.GetTypeDescriptorView(dex_file.GetTypeId(klass->GetDexTypeIndex())); |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 3805 | // The Enum class declares a "final" finalize() method to prevent subclasses from |
| 3806 | // introducing a finalizer. We don't want to set the finalizable flag for Enum or its |
| 3807 | // subclasses, so we exclude it here. |
| 3808 | // We also want to avoid setting the flag on Object, where we know that finalize() is |
| 3809 | // empty. |
Vladimir Marko | 05f1a5b | 2022-03-09 14:20:15 +0000 | [diff] [blame] | 3810 | if (klass_descriptor != "Ljava/lang/Object;" && |
| 3811 | klass_descriptor != "Ljava/lang/Enum;") { |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3812 | klass->SetFinalizable(); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3813 | } |
| 3814 | } |
| 3815 | } |
| 3816 | } else if (method_name[0] == '<') { |
| 3817 | // Fix broken access flags for initializers. Bug 11157540. |
Vladimir Marko | 05f1a5b | 2022-03-09 14:20:15 +0000 | [diff] [blame] | 3818 | bool is_init = has_ascii_name("<init>", sizeof("<init>") - 1u); |
| 3819 | bool is_clinit = has_ascii_name("<clinit>", sizeof("<clinit>") - 1u); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3820 | if (UNLIKELY(!is_init && !is_clinit)) { |
| 3821 | LOG(WARNING) << "Unexpected '<' at start of method name " << method_name; |
| 3822 | } else { |
| 3823 | if (UNLIKELY((access_flags & kAccConstructor) == 0)) { |
| 3824 | 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] | 3825 | << klass->PrettyDescriptor() << " in dex file " << dex_file.GetLocation(); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3826 | access_flags |= kAccConstructor; |
| 3827 | } |
| 3828 | } |
| 3829 | } |
Vladimir Marko | e815aec | 2022-03-29 15:58:40 +0000 | [diff] [blame] | 3830 | |
| 3831 | // Check for nterp invoke fast-path based on shorty. |
| 3832 | bool all_parameters_are_reference = true; |
| 3833 | bool all_parameters_are_reference_or_int = true; |
| 3834 | for (size_t i = 1; i < shorty.length(); ++i) { |
| 3835 | if (shorty[i] != 'L') { |
| 3836 | all_parameters_are_reference = false; |
| 3837 | if (shorty[i] == 'F' || shorty[i] == 'D' || shorty[i] == 'J') { |
| 3838 | all_parameters_are_reference_or_int = false; |
| 3839 | break; |
| 3840 | } |
| 3841 | } |
| 3842 | } |
| 3843 | if (all_parameters_are_reference_or_int && shorty[0] != 'F' && shorty[0] != 'D') { |
| 3844 | access_flags |= kAccNterpInvokeFastPathFlag; |
| 3845 | } |
| 3846 | |
Vladimir Marko | b0a6aee | 2017-10-27 10:34:04 +0100 | [diff] [blame] | 3847 | if (UNLIKELY((access_flags & kAccNative) != 0u)) { |
| 3848 | // Check if the native method is annotated with @FastNative or @CriticalNative. |
Vladimir Marko | 05f1a5b | 2022-03-09 14:20:15 +0000 | [diff] [blame] | 3849 | const dex::ClassDef& class_def = dex_file.GetClassDef(klass->GetDexClassDefIndex()); |
| 3850 | access_flags |= |
| 3851 | annotations::GetNativeMethodAnnotationAccessFlags(dex_file, class_def, dex_method_idx); |
| 3852 | dst->SetAccessFlags(access_flags); |
| 3853 | DCHECK(!dst->IsAbstract()); |
| 3854 | DCHECK(!dst->HasCodeItem()); |
Nicolas Geoffray | 4717175 | 2020-08-31 15:03:20 +0100 | [diff] [blame] | 3855 | DCHECK_EQ(method.GetCodeItemOffset(), 0u); |
Vladimir Marko | 05f1a5b | 2022-03-09 14:20:15 +0000 | [diff] [blame] | 3856 | dst->SetDataPtrSize(nullptr, image_pointer_size_); // JNI stub/trampoline not linked yet. |
| 3857 | } else if ((access_flags & kAccAbstract) != 0u) { |
| 3858 | dst->SetAccessFlags(access_flags); |
| 3859 | // Must be done after SetAccessFlags since IsAbstract depends on it. |
| 3860 | DCHECK(dst->IsAbstract()); |
| 3861 | if (klass->IsInterface()) { |
| 3862 | dst->CalculateAndSetImtIndex(); |
| 3863 | } |
| 3864 | DCHECK(!dst->HasCodeItem()); |
| 3865 | DCHECK_EQ(method.GetCodeItemOffset(), 0u); |
| 3866 | dst->SetDataPtrSize(nullptr, image_pointer_size_); // Single implementation not set yet. |
| 3867 | } else { |
Vladimir Marko | e815aec | 2022-03-29 15:58:40 +0000 | [diff] [blame] | 3868 | // Check for nterp entry fast-path based on shorty. |
| 3869 | if (all_parameters_are_reference) { |
| 3870 | access_flags |= kAccNterpEntryPointFastPathFlag; |
| 3871 | } |
Vladimir Marko | 05f1a5b | 2022-03-09 14:20:15 +0000 | [diff] [blame] | 3872 | const dex::ClassDef& class_def = dex_file.GetClassDef(klass->GetDexClassDefIndex()); |
| 3873 | if (annotations::MethodIsNeverCompile(dex_file, class_def, dex_method_idx)) { |
| 3874 | access_flags |= kAccCompileDontBother; |
| 3875 | } |
| 3876 | dst->SetAccessFlags(access_flags); |
| 3877 | DCHECK(!dst->IsAbstract()); |
| 3878 | DCHECK(dst->HasCodeItem()); |
| 3879 | uint32_t code_item_offset = method.GetCodeItemOffset(); |
| 3880 | DCHECK_NE(code_item_offset, 0u); |
| 3881 | if (Runtime::Current()->IsAotCompiler()) { |
| 3882 | dst->SetDataPtrSize(reinterpret_cast32<void*>(code_item_offset), image_pointer_size_); |
| 3883 | } else { |
| 3884 | dst->SetCodeItem(dex_file.GetCodeItem(code_item_offset), dex_file.IsCompactDexFile()); |
| 3885 | } |
Nicolas Geoffray | 43c9cd7 | 2021-03-10 15:09:19 +0000 | [diff] [blame] | 3886 | } |
Nicolas Geoffray | f9ae8e3 | 2022-02-15 22:54:11 +0000 | [diff] [blame] | 3887 | |
Nicolas Geoffray | 8bf5afd | 2022-04-27 14:11:23 +0100 | [diff] [blame] | 3888 | if (Runtime::Current()->IsZygote() && |
| 3889 | !Runtime::Current()->GetJITOptions()->GetProfileSaverOptions().GetProfileBootClassPath()) { |
Nicolas Geoffray | f9ae8e3 | 2022-02-15 22:54:11 +0000 | [diff] [blame] | 3890 | dst->SetMemorySharedMethod(); |
| 3891 | } |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3892 | } |
| 3893 | |
Mathieu Chartier | 0a19e21 | 2019-11-27 14:35:24 -0800 | [diff] [blame] | 3894 | void ClassLinker::AppendToBootClassPath(Thread* self, const DexFile* dex_file) { |
David Srbecky | 33df0e3 | 2021-09-30 14:36:32 +0000 | [diff] [blame] | 3895 | ObjPtr<mirror::DexCache> dex_cache = |
| 3896 | AllocAndInitializeDexCache(self, *dex_file, /* class_loader= */ nullptr); |
Mathieu Chartier | 0a19e21 | 2019-11-27 14:35:24 -0800 | [diff] [blame] | 3897 | 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] | 3898 | AppendToBootClassPath(dex_file, dex_cache); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 3899 | } |
| 3900 | |
Mathieu Chartier | 0a19e21 | 2019-11-27 14:35:24 -0800 | [diff] [blame] | 3901 | void ClassLinker::AppendToBootClassPath(const DexFile* dex_file, |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3902 | ObjPtr<mirror::DexCache> dex_cache) { |
Mathieu Chartier | 0a19e21 | 2019-11-27 14:35:24 -0800 | [diff] [blame] | 3903 | CHECK(dex_file != nullptr); |
| 3904 | CHECK(dex_cache != nullptr) << dex_file->GetLocation(); |
Nicolas Geoffray | 7913cf3 | 2022-02-16 09:28:21 +0000 | [diff] [blame] | 3905 | CHECK_EQ(dex_cache->GetDexFile(), dex_file) << dex_file->GetLocation(); |
Mathieu Chartier | 0a19e21 | 2019-11-27 14:35:24 -0800 | [diff] [blame] | 3906 | boot_class_path_.push_back(dex_file); |
Andreas Gampe | be7af22 | 2017-07-25 09:57:28 -0700 | [diff] [blame] | 3907 | WriterMutexLock mu(Thread::Current(), *Locks::dex_lock_); |
Mathieu Chartier | 0a19e21 | 2019-11-27 14:35:24 -0800 | [diff] [blame] | 3908 | RegisterDexFileLocked(*dex_file, dex_cache, /* class_loader= */ nullptr); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 3909 | } |
| 3910 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 3911 | void ClassLinker::RegisterDexFileLocked(const DexFile& dex_file, |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3912 | ObjPtr<mirror::DexCache> dex_cache, |
| 3913 | ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3914 | Thread* const self = Thread::Current(); |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 3915 | Locks::dex_lock_->AssertExclusiveHeld(self); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3916 | CHECK(dex_cache != nullptr) << dex_file.GetLocation(); |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 3917 | CHECK_EQ(dex_cache->GetDexFile(), &dex_file) << dex_file.GetLocation(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 3918 | // For app images, the dex cache location may be a suffix of the dex file location since the |
| 3919 | // dex file location is an absolute path. |
Mathieu Chartier | 7617216 | 2016-01-26 14:54:06 -0800 | [diff] [blame] | 3920 | const std::string dex_cache_location = dex_cache->GetLocation()->ToModifiedUtf8(); |
| 3921 | const size_t dex_cache_length = dex_cache_location.length(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 3922 | CHECK_GT(dex_cache_length, 0u) << dex_file.GetLocation(); |
| 3923 | std::string dex_file_location = dex_file.GetLocation(); |
Nicolas Geoffray | e3e0f70 | 2019-03-12 07:02:02 +0000 | [diff] [blame] | 3924 | // The following paths checks don't work on preopt when using boot dex files, where the dex |
| 3925 | // cache location is the one on device, and the dex_file's location is the one on host. |
VladimÃr Marko | 1deea77 | 2022-11-30 16:17:07 +0000 | [diff] [blame] | 3926 | Runtime* runtime = Runtime::Current(); |
| 3927 | if (!(runtime->IsAotCompiler() && class_loader == nullptr && !kIsTargetBuild)) { |
Nicolas Geoffray | e3e0f70 | 2019-03-12 07:02:02 +0000 | [diff] [blame] | 3928 | CHECK_GE(dex_file_location.length(), dex_cache_length) |
| 3929 | << dex_cache_location << " " << dex_file.GetLocation(); |
| 3930 | const std::string dex_file_suffix = dex_file_location.substr( |
| 3931 | dex_file_location.length() - dex_cache_length, |
| 3932 | dex_cache_length); |
| 3933 | // Example dex_cache location is SettingsProvider.apk and |
| 3934 | // dex file location is /system/priv-app/SettingsProvider/SettingsProvider.apk |
| 3935 | CHECK_EQ(dex_cache_location, dex_file_suffix); |
| 3936 | } |
Vladimir Marko | 68457df | 2022-10-26 07:18:57 +0000 | [diff] [blame] | 3937 | |
| 3938 | // Check if we need to initialize OatFile data (.data.bimg.rel.ro and .bss |
VladimÃr Marko | 1deea77 | 2022-11-30 16:17:07 +0000 | [diff] [blame] | 3939 | // sections) needed for code execution and register the oat code range. |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 3940 | const OatFile* oat_file = |
| 3941 | (dex_file.GetOatDexFile() != nullptr) ? dex_file.GetOatDexFile()->GetOatFile() : nullptr; |
Vladimir Marko | b066d43 | 2018-01-03 13:14:37 +0000 | [diff] [blame] | 3942 | bool initialize_oat_file_data = (oat_file != nullptr) && oat_file->IsExecutable(); |
Vladimir Marko | 68457df | 2022-10-26 07:18:57 +0000 | [diff] [blame] | 3943 | if (initialize_oat_file_data) { |
| 3944 | for (const auto& entry : dex_caches_) { |
| 3945 | if (!self->IsJWeakCleared(entry.second.weak_root) && |
| 3946 | entry.first->GetOatDexFile() != nullptr && |
| 3947 | entry.first->GetOatDexFile()->GetOatFile() == oat_file) { |
Vladimir Marko | b066d43 | 2018-01-03 13:14:37 +0000 | [diff] [blame] | 3948 | initialize_oat_file_data = false; // Already initialized. |
Vladimir Marko | 68457df | 2022-10-26 07:18:57 +0000 | [diff] [blame] | 3949 | break; |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 3950 | } |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3951 | } |
Brian Carlstrom | 81a9087 | 2015-08-28 09:07:14 -0700 | [diff] [blame] | 3952 | } |
Vladimir Marko | b066d43 | 2018-01-03 13:14:37 +0000 | [diff] [blame] | 3953 | if (initialize_oat_file_data) { |
Vladimir Marko | 1cedb4a | 2019-02-06 14:13:28 +0000 | [diff] [blame] | 3954 | oat_file->InitializeRelocations(); |
VladimÃr Marko | 1deea77 | 2022-11-30 16:17:07 +0000 | [diff] [blame] | 3955 | // Notify the fault handler about the new executable code range if needed. |
| 3956 | size_t exec_offset = oat_file->GetOatHeader().GetExecutableOffset(); |
| 3957 | DCHECK_LE(exec_offset, oat_file->Size()); |
| 3958 | size_t exec_size = oat_file->Size() - exec_offset; |
| 3959 | if (exec_size != 0u) { |
| 3960 | runtime->AddGeneratedCodeRange(oat_file->Begin() + exec_offset, exec_size); |
| 3961 | } |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 3962 | } |
Vladimir Marko | 68457df | 2022-10-26 07:18:57 +0000 | [diff] [blame] | 3963 | |
David Brazdil | a5c3a80 | 2019-03-08 14:59:41 +0000 | [diff] [blame] | 3964 | // Let hiddenapi assign a domain to the newly registered dex file. |
| 3965 | hiddenapi::InitializeDexFileDomain(dex_file, class_loader); |
| 3966 | |
Vladimir Marko | 68457df | 2022-10-26 07:18:57 +0000 | [diff] [blame] | 3967 | jweak dex_cache_jweak = self->GetJniEnv()->GetVm()->AddWeakGlobalRef(self, dex_cache); |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 3968 | DexCacheData data; |
| 3969 | data.weak_root = dex_cache_jweak; |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3970 | data.class_table = ClassTableForClassLoader(class_loader); |
David Srbecky | 6fbcc29 | 2021-02-23 01:05:32 +0000 | [diff] [blame] | 3971 | AddNativeDebugInfoForDex(self, &dex_file); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3972 | DCHECK(data.class_table != nullptr); |
Mathieu Chartier | 72041a0 | 2017-07-14 18:23:25 -0700 | [diff] [blame] | 3973 | // Make sure to hold the dex cache live in the class table. This case happens for the boot class |
| 3974 | // path dex caches without an image. |
| 3975 | data.class_table->InsertStrongRoot(dex_cache); |
Andreas Gampe | 8a1a0f7 | 2020-03-03 16:07:45 -0800 | [diff] [blame] | 3976 | // Make sure that the dex cache holds the classloader live. |
| 3977 | dex_cache->SetClassLoader(class_loader); |
Mathieu Chartier | 72041a0 | 2017-07-14 18:23:25 -0700 | [diff] [blame] | 3978 | if (class_loader != nullptr) { |
| 3979 | // Since we added a strong root to the class table, do the write barrier as required for |
| 3980 | // remembered sets and generational GCs. |
Mathieu Chartier | 88ea61e | 2018-06-20 17:45:41 -0700 | [diff] [blame] | 3981 | WriteBarrier::ForEveryFieldWrite(class_loader); |
Mathieu Chartier | 72041a0 | 2017-07-14 18:23:25 -0700 | [diff] [blame] | 3982 | } |
David Srbecky | 6fbcc29 | 2021-02-23 01:05:32 +0000 | [diff] [blame] | 3983 | bool inserted = dex_caches_.emplace(&dex_file, std::move(data)).second; |
| 3984 | CHECK(inserted); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 3985 | } |
| 3986 | |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 3987 | ObjPtr<mirror::DexCache> ClassLinker::DecodeDexCacheLocked(Thread* self, const DexCacheData* data) { |
| 3988 | return data != nullptr |
| 3989 | ? ObjPtr<mirror::DexCache>::DownCast(self->DecodeJObject(data->weak_root)) |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3990 | : nullptr; |
| 3991 | } |
| 3992 | |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 3993 | bool ClassLinker::IsSameClassLoader( |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3994 | ObjPtr<mirror::DexCache> dex_cache, |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 3995 | const DexCacheData* data, |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3996 | ObjPtr<mirror::ClassLoader> class_loader) { |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 3997 | CHECK(data != nullptr); |
David Srbecky | 6fbcc29 | 2021-02-23 01:05:32 +0000 | [diff] [blame] | 3998 | DCHECK_EQ(FindDexCacheDataLocked(*dex_cache->GetDexFile()), data); |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 3999 | return data->class_table == ClassTableForClassLoader(class_loader); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4000 | } |
| 4001 | |
Alex Light | 07f0621 | 2017-06-01 14:01:43 -0700 | [diff] [blame] | 4002 | void ClassLinker::RegisterExistingDexCache(ObjPtr<mirror::DexCache> dex_cache, |
| 4003 | ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | ed4ee44 | 2018-06-05 14:23:35 -0700 | [diff] [blame] | 4004 | SCOPED_TRACE << __FUNCTION__ << " " << dex_cache->GetDexFile()->GetLocation(); |
Alex Light | 07f0621 | 2017-06-01 14:01:43 -0700 | [diff] [blame] | 4005 | Thread* self = Thread::Current(); |
| 4006 | StackHandleScope<2> hs(self); |
| 4007 | Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(dex_cache)); |
| 4008 | Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader)); |
| 4009 | const DexFile* dex_file = dex_cache->GetDexFile(); |
| 4010 | DCHECK(dex_file != nullptr) << "Attempt to register uninitialized dex_cache object!"; |
| 4011 | if (kIsDebugBuild) { |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 4012 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
| 4013 | const DexCacheData* old_data = FindDexCacheDataLocked(*dex_file); |
| 4014 | ObjPtr<mirror::DexCache> old_dex_cache = DecodeDexCacheLocked(self, old_data); |
Alex Light | 07f0621 | 2017-06-01 14:01:43 -0700 | [diff] [blame] | 4015 | DCHECK(old_dex_cache.IsNull()) << "Attempt to manually register a dex cache thats already " |
| 4016 | << "been registered on dex file " << dex_file->GetLocation(); |
| 4017 | } |
| 4018 | ClassTable* table; |
| 4019 | { |
| 4020 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 4021 | table = InsertClassTableForClassLoader(h_class_loader.Get()); |
| 4022 | } |
Ivan Maidanski | 2b69b9c | 2018-05-14 13:50:48 +0300 | [diff] [blame] | 4023 | // Avoid a deadlock between a garbage collecting thread running a checkpoint, |
| 4024 | // a thread holding the dex lock and blocking on a condition variable regarding |
| 4025 | // weak references access, and a thread blocking on the dex lock. |
Ivan Maidanski | 2b69b9c | 2018-05-14 13:50:48 +0300 | [diff] [blame] | 4026 | gc::ScopedGCCriticalSection gcs(self, gc::kGcCauseClassLinker, gc::kCollectorTypeClassLinker); |
Alex Light | 07f0621 | 2017-06-01 14:01:43 -0700 | [diff] [blame] | 4027 | WriterMutexLock mu(self, *Locks::dex_lock_); |
| 4028 | RegisterDexFileLocked(*dex_file, h_dex_cache.Get(), h_class_loader.Get()); |
| 4029 | table->InsertStrongRoot(h_dex_cache.Get()); |
| 4030 | if (h_class_loader.Get() != nullptr) { |
| 4031 | // Since we added a strong root to the class table, do the write barrier as required for |
| 4032 | // remembered sets and generational GCs. |
Mathieu Chartier | 88ea61e | 2018-06-20 17:45:41 -0700 | [diff] [blame] | 4033 | WriteBarrier::ForEveryFieldWrite(h_class_loader.Get()); |
Alex Light | 07f0621 | 2017-06-01 14:01:43 -0700 | [diff] [blame] | 4034 | } |
| 4035 | } |
| 4036 | |
Alex Light | de7f878 | 2020-02-24 10:14:22 -0800 | [diff] [blame] | 4037 | static void ThrowDexFileAlreadyRegisteredError(Thread* self, const DexFile& dex_file) |
| 4038 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 4039 | self->ThrowNewExceptionF("Ljava/lang/InternalError;", |
Alex Light | de7f878 | 2020-02-24 10:14:22 -0800 | [diff] [blame] | 4040 | "Attempt to register dex file %s with multiple class loaders", |
| 4041 | dex_file.GetLocation().c_str()); |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 4042 | } |
| 4043 | |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4044 | ObjPtr<mirror::DexCache> ClassLinker::RegisterDexFile(const DexFile& dex_file, |
| 4045 | ObjPtr<mirror::ClassLoader> class_loader) { |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 4046 | Thread* self = Thread::Current(); |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 4047 | ObjPtr<mirror::DexCache> old_dex_cache; |
| 4048 | bool registered_with_another_class_loader = false; |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 4049 | { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 4050 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 4051 | const DexCacheData* old_data = FindDexCacheDataLocked(dex_file); |
| 4052 | old_dex_cache = DecodeDexCacheLocked(self, old_data); |
| 4053 | if (old_dex_cache != nullptr) { |
| 4054 | if (IsSameClassLoader(old_dex_cache, old_data, class_loader)) { |
| 4055 | return old_dex_cache; |
| 4056 | } else { |
| 4057 | // TODO This is not very clean looking. Should maybe try to make a way to request exceptions |
| 4058 | // be thrown when it's safe to do so to simplify this. |
| 4059 | registered_with_another_class_loader = true; |
| 4060 | } |
| 4061 | } |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4062 | } |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 4063 | // We need to have released the dex_lock_ to allocate safely. |
| 4064 | if (registered_with_another_class_loader) { |
| 4065 | ThrowDexFileAlreadyRegisteredError(self, dex_file); |
| 4066 | return nullptr; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 4067 | } |
Mathieu Chartier | ed4ee44 | 2018-06-05 14:23:35 -0700 | [diff] [blame] | 4068 | SCOPED_TRACE << __FUNCTION__ << " " << dex_file.GetLocation(); |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 4069 | LinearAlloc* const linear_alloc = GetOrCreateAllocatorForClassLoader(class_loader); |
| 4070 | DCHECK(linear_alloc != nullptr); |
| 4071 | ClassTable* table; |
| 4072 | { |
| 4073 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 4074 | table = InsertClassTableForClassLoader(class_loader); |
| 4075 | } |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 4076 | // Don't alloc while holding the lock, since allocation may need to |
| 4077 | // suspend all threads and another thread may need the dex_lock_ to |
| 4078 | // get to a suspend point. |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4079 | StackHandleScope<3> hs(self); |
| 4080 | Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader)); |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 4081 | Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(AllocDexCache(self, dex_file))); |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 4082 | { |
Ivan Maidanski | 2b69b9c | 2018-05-14 13:50:48 +0300 | [diff] [blame] | 4083 | // Avoid a deadlock between a garbage collecting thread running a checkpoint, |
| 4084 | // a thread holding the dex lock and blocking on a condition variable regarding |
| 4085 | // weak references access, and a thread blocking on the dex lock. |
Ivan Maidanski | 2b69b9c | 2018-05-14 13:50:48 +0300 | [diff] [blame] | 4086 | gc::ScopedGCCriticalSection gcs(self, gc::kGcCauseClassLinker, gc::kCollectorTypeClassLinker); |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 4087 | WriterMutexLock mu(self, *Locks::dex_lock_); |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 4088 | const DexCacheData* old_data = FindDexCacheDataLocked(dex_file); |
| 4089 | old_dex_cache = DecodeDexCacheLocked(self, old_data); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4090 | if (old_dex_cache == nullptr && h_dex_cache != nullptr) { |
David Srbecky | 33df0e3 | 2021-09-30 14:36:32 +0000 | [diff] [blame] | 4091 | // Do Initialize while holding dex lock to make sure two threads don't call it |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 4092 | // at the same time with the same dex cache. Since the .bss is shared this can cause failing |
| 4093 | // DCHECK that the arrays are null. |
David Srbecky | 33df0e3 | 2021-09-30 14:36:32 +0000 | [diff] [blame] | 4094 | h_dex_cache->Initialize(&dex_file, h_class_loader.Get()); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4095 | RegisterDexFileLocked(dex_file, h_dex_cache.Get(), h_class_loader.Get()); |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 4096 | } |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 4097 | if (old_dex_cache != nullptr) { |
| 4098 | // Another thread managed to initialize the dex cache faster, so use that DexCache. |
| 4099 | // If this thread encountered OOME, ignore it. |
| 4100 | DCHECK_EQ(h_dex_cache == nullptr, self->IsExceptionPending()); |
| 4101 | self->ClearException(); |
| 4102 | // We cannot call EnsureSameClassLoader() or allocate an exception while holding the |
| 4103 | // dex_lock_. |
| 4104 | if (IsSameClassLoader(old_dex_cache, old_data, h_class_loader.Get())) { |
| 4105 | return old_dex_cache; |
| 4106 | } else { |
| 4107 | registered_with_another_class_loader = true; |
| 4108 | } |
| 4109 | } |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4110 | } |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 4111 | if (registered_with_another_class_loader) { |
| 4112 | ThrowDexFileAlreadyRegisteredError(self, dex_file); |
| 4113 | return nullptr; |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4114 | } |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4115 | if (h_dex_cache == nullptr) { |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4116 | self->AssertPendingOOMException(); |
| 4117 | return nullptr; |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 4118 | } |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 4119 | table->InsertStrongRoot(h_dex_cache.Get()); |
Mathieu Chartier | a1467d0 | 2017-02-22 09:22:50 -0800 | [diff] [blame] | 4120 | if (h_class_loader.Get() != nullptr) { |
| 4121 | // Since we added a strong root to the class table, do the write barrier as required for |
| 4122 | // remembered sets and generational GCs. |
Mathieu Chartier | 88ea61e | 2018-06-20 17:45:41 -0700 | [diff] [blame] | 4123 | WriteBarrier::ForEveryFieldWrite(h_class_loader.Get()); |
Mathieu Chartier | a1467d0 | 2017-02-22 09:22:50 -0800 | [diff] [blame] | 4124 | } |
Nicolas Geoffray | 1d4f009 | 2020-08-07 14:01:05 +0100 | [diff] [blame] | 4125 | VLOG(class_linker) << "Registered dex file " << dex_file.GetLocation(); |
Nicolas Geoffray | 4f6bb44 | 2021-06-02 18:05:51 +0100 | [diff] [blame] | 4126 | PaletteNotifyDexFileLoaded(dex_file.GetLocation().c_str()); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 4127 | return h_dex_cache.Get(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 4128 | } |
| 4129 | |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4130 | bool ClassLinker::IsDexFileRegistered(Thread* self, const DexFile& dex_file) { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 4131 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 4132 | return DecodeDexCacheLocked(self, FindDexCacheDataLocked(dex_file)) != nullptr; |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 4133 | } |
| 4134 | |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4135 | ObjPtr<mirror::DexCache> ClassLinker::FindDexCache(Thread* self, const DexFile& dex_file) { |
| 4136 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 4137 | const DexCacheData* dex_cache_data = FindDexCacheDataLocked(dex_file); |
| 4138 | ObjPtr<mirror::DexCache> dex_cache = DecodeDexCacheLocked(self, dex_cache_data); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4139 | if (dex_cache != nullptr) { |
| 4140 | return dex_cache; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 4141 | } |
Brian Carlstrom | 81a9087 | 2015-08-28 09:07:14 -0700 | [diff] [blame] | 4142 | // Failure, dump diagnostic and abort. |
David Srbecky | 6fbcc29 | 2021-02-23 01:05:32 +0000 | [diff] [blame] | 4143 | for (const auto& entry : dex_caches_) { |
| 4144 | const DexCacheData& data = entry.second; |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 4145 | if (DecodeDexCacheLocked(self, &data) != nullptr) { |
David Srbecky | 6fbcc29 | 2021-02-23 01:05:32 +0000 | [diff] [blame] | 4146 | LOG(FATAL_WITHOUT_ABORT) << "Registered dex file " << entry.first->GetLocation(); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 4147 | } |
Brian Carlstrom | 81a9087 | 2015-08-28 09:07:14 -0700 | [diff] [blame] | 4148 | } |
Mathieu Chartier | 9e050df | 2017-08-09 10:05:47 -0700 | [diff] [blame] | 4149 | LOG(FATAL) << "Failed to find DexCache for DexFile " << dex_file.GetLocation() |
David Srbecky | 6fbcc29 | 2021-02-23 01:05:32 +0000 | [diff] [blame] | 4150 | << " " << &dex_file; |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 4151 | UNREACHABLE(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4152 | } |
| 4153 | |
Santiago Aboy Solanes | 4b8ea5d | 2021-11-19 10:14:54 +0000 | [diff] [blame] | 4154 | ObjPtr<mirror::DexCache> ClassLinker::FindDexCache(Thread* self, const OatDexFile& oat_dex_file) { |
Santiago Aboy Solanes | 970ba21 | 2021-10-21 10:52:47 +0100 | [diff] [blame] | 4155 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
| 4156 | const DexCacheData* dex_cache_data = FindDexCacheDataLocked(oat_dex_file); |
| 4157 | ObjPtr<mirror::DexCache> dex_cache = DecodeDexCacheLocked(self, dex_cache_data); |
| 4158 | if (dex_cache != nullptr) { |
| 4159 | return dex_cache; |
| 4160 | } |
| 4161 | // Failure, dump diagnostic and abort. |
| 4162 | for (const auto& entry : dex_caches_) { |
| 4163 | const DexCacheData& data = entry.second; |
| 4164 | if (DecodeDexCacheLocked(self, &data) != nullptr) { |
| 4165 | LOG(FATAL_WITHOUT_ABORT) << "Registered dex file " << entry.first->GetLocation(); |
| 4166 | } |
| 4167 | } |
Santiago Aboy Solanes | 4b8ea5d | 2021-11-19 10:14:54 +0000 | [diff] [blame] | 4168 | LOG(FATAL) << "Failed to find DexCache for OatDexFile " << oat_dex_file.GetDexFileLocation() |
Santiago Aboy Solanes | 970ba21 | 2021-10-21 10:52:47 +0100 | [diff] [blame] | 4169 | << " " << &oat_dex_file; |
| 4170 | UNREACHABLE(); |
| 4171 | } |
| 4172 | |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4173 | ClassTable* ClassLinker::FindClassTable(Thread* self, ObjPtr<mirror::DexCache> dex_cache) { |
| 4174 | const DexFile* dex_file = dex_cache->GetDexFile(); |
| 4175 | DCHECK(dex_file != nullptr); |
| 4176 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
David Srbecky | 6fbcc29 | 2021-02-23 01:05:32 +0000 | [diff] [blame] | 4177 | auto it = dex_caches_.find(dex_file); |
| 4178 | if (it != dex_caches_.end()) { |
| 4179 | const DexCacheData& data = it->second; |
| 4180 | ObjPtr<mirror::DexCache> registered_dex_cache = DecodeDexCacheLocked(self, &data); |
| 4181 | if (registered_dex_cache != nullptr) { |
| 4182 | CHECK_EQ(registered_dex_cache, dex_cache) << dex_file->GetLocation(); |
| 4183 | return data.class_table; |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4184 | } |
| 4185 | } |
| 4186 | return nullptr; |
| 4187 | } |
| 4188 | |
Santiago Aboy Solanes | 970ba21 | 2021-10-21 10:52:47 +0100 | [diff] [blame] | 4189 | const ClassLinker::DexCacheData* ClassLinker::FindDexCacheDataLocked( |
Santiago Aboy Solanes | 4b8ea5d | 2021-11-19 10:14:54 +0000 | [diff] [blame] | 4190 | const OatDexFile& oat_dex_file) { |
| 4191 | auto it = std::find_if(dex_caches_.begin(), dex_caches_.end(), [&](const auto& entry) { |
| 4192 | return entry.first->GetOatDexFile() == &oat_dex_file; |
Santiago Aboy Solanes | 970ba21 | 2021-10-21 10:52:47 +0100 | [diff] [blame] | 4193 | }); |
| 4194 | return it != dex_caches_.end() ? &it->second : nullptr; |
| 4195 | } |
| 4196 | |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 4197 | const ClassLinker::DexCacheData* ClassLinker::FindDexCacheDataLocked(const DexFile& dex_file) { |
David Srbecky | 6fbcc29 | 2021-02-23 01:05:32 +0000 | [diff] [blame] | 4198 | auto it = dex_caches_.find(&dex_file); |
| 4199 | return it != dex_caches_.end() ? &it->second : nullptr; |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4200 | } |
| 4201 | |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 4202 | void ClassLinker::CreatePrimitiveClass(Thread* self, |
| 4203 | Primitive::Type type, |
| 4204 | ClassRoot primitive_root) { |
Vladimir Marko | acb906d | 2018-05-30 10:23:49 +0100 | [diff] [blame] | 4205 | ObjPtr<mirror::Class> primitive_class = |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 4206 | AllocClass(self, mirror::Class::PrimitiveClassSize(image_pointer_size_)); |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 4207 | CHECK(primitive_class != nullptr) << "OOM for primitive class " << type; |
| 4208 | // Do not hold lock on the primitive class object, the initialization of |
| 4209 | // primitive classes is done while the process is still single threaded. |
Nicolas Geoffray | 66934ef | 2021-07-07 14:56:23 +0100 | [diff] [blame] | 4210 | primitive_class->SetAccessFlagsDuringLinking(kAccPublic | kAccFinal | kAccAbstract); |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 4211 | primitive_class->SetPrimitiveType(type); |
| 4212 | primitive_class->SetIfTable(GetClassRoot<mirror::Object>(this)->GetIfTable()); |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 4213 | DCHECK_EQ(primitive_class->NumMethods(), 0u); |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 4214 | // Primitive classes are initialized during single threaded startup, so visibly initialized. |
| 4215 | primitive_class->SetStatusForPrimitiveOrArray(ClassStatus::kVisiblyInitialized); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4216 | const char* descriptor = Primitive::Descriptor(type); |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 4217 | ObjPtr<mirror::Class> existing = InsertClass(descriptor, |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 4218 | primitive_class, |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 4219 | ComputeModifiedUtf8Hash(descriptor)); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4220 | CHECK(existing == nullptr) << "InitPrimitiveClass(" << type << ") failed"; |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 4221 | SetClassRoot(primitive_root, primitive_class); |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 4222 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4223 | |
Vladimir Marko | 0261055 | 2018-06-04 14:38:00 +0100 | [diff] [blame] | 4224 | inline ObjPtr<mirror::IfTable> ClassLinker::GetArrayIfTable() { |
| 4225 | return GetClassRoot<mirror::ObjectArray<mirror::Object>>(this)->GetIfTable(); |
| 4226 | } |
| 4227 | |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 4228 | // Create an array class (i.e. the class object for the array, not the |
| 4229 | // array itself). "descriptor" looks like "[C" or "[[[[B" or |
| 4230 | // "[Ljava/lang/String;". |
| 4231 | // |
| 4232 | // If "descriptor" refers to an array of primitives, look up the |
| 4233 | // primitive type's internally-generated class object. |
| 4234 | // |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 4235 | // "class_loader" is the class loader of the class that's referring to |
| 4236 | // us. It's used to ensure that we're looking for the element type in |
| 4237 | // the right context. It does NOT become the class loader for the |
| 4238 | // array class; that always comes from the base element class. |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 4239 | // |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 4240 | // Returns null with an exception raised on failure. |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 4241 | ObjPtr<mirror::Class> ClassLinker::CreateArrayClass(Thread* self, |
| 4242 | const char* descriptor, |
| 4243 | size_t hash, |
| 4244 | Handle<mirror::ClassLoader> class_loader) { |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 4245 | // Identify the underlying component type |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4246 | CHECK_EQ('[', descriptor[0]); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4247 | StackHandleScope<2> hs(self); |
Alex Light | e9f6103 | 2018-09-24 16:04:51 -0700 | [diff] [blame] | 4248 | |
| 4249 | // This is to prevent the calls to ClassLoad and ClassPrepare which can cause java/user-supplied |
| 4250 | // code to be executed. We put it up here so we can avoid all the allocations associated with |
| 4251 | // creating the class. This can happen with (eg) jit threads. |
| 4252 | if (!self->CanLoadClasses()) { |
| 4253 | // Make sure we don't try to load anything, potentially causing an infinite loop. |
| 4254 | ObjPtr<mirror::Throwable> pre_allocated = |
| 4255 | Runtime::Current()->GetPreAllocatedNoClassDefFoundError(); |
| 4256 | self->SetException(pre_allocated); |
| 4257 | return nullptr; |
| 4258 | } |
| 4259 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4260 | MutableHandle<mirror::Class> component_type(hs.NewHandle(FindClass(self, descriptor + 1, |
| 4261 | class_loader))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4262 | if (component_type == nullptr) { |
Mathieu Chartier | c0a9ea4 | 2014-02-03 16:36:49 -0800 | [diff] [blame] | 4263 | DCHECK(self->IsExceptionPending()); |
Nicolas Geoffray | 74b230a | 2022-08-01 16:20:06 +0100 | [diff] [blame] | 4264 | // We need to accept erroneous classes as component types. Under AOT, we |
| 4265 | // don't accept them as we cannot encode the erroneous class in an image. |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 4266 | const size_t component_hash = ComputeModifiedUtf8Hash(descriptor + 1); |
| 4267 | component_type.Assign(LookupClass(self, descriptor + 1, component_hash, class_loader.Get())); |
Nicolas Geoffray | 74b230a | 2022-08-01 16:20:06 +0100 | [diff] [blame] | 4268 | if (component_type == nullptr || Runtime::Current()->IsAotCompiler()) { |
Andreas Gampe | dc13d7d | 2014-07-23 20:18:36 -0700 | [diff] [blame] | 4269 | DCHECK(self->IsExceptionPending()); |
| 4270 | return nullptr; |
| 4271 | } else { |
| 4272 | self->ClearException(); |
| 4273 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 4274 | } |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 4275 | if (UNLIKELY(component_type->IsPrimitiveVoid())) { |
| 4276 | ThrowNoClassDefFoundError("Attempt to create array of void primitive type"); |
| 4277 | return nullptr; |
| 4278 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 4279 | // See if the component type is already loaded. Array classes are |
| 4280 | // always associated with the class loader of their underlying |
| 4281 | // element type -- an array of Strings goes with the loader for |
| 4282 | // java/lang/String -- so we need to look for it there. (The |
| 4283 | // caller should have checked for the existence of the class |
| 4284 | // before calling here, but they did so with *their* class loader, |
| 4285 | // not the component type's loader.) |
| 4286 | // |
| 4287 | // If we find it, the caller adds "loader" to the class' initiating |
| 4288 | // loader list, which should prevent us from going through this again. |
| 4289 | // |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4290 | // This call is unnecessary if "loader" and "component_type->GetClassLoader()" |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 4291 | // are the same, because our caller (FindClass) just did the |
| 4292 | // lookup. (Even if we get this wrong we still have correct behavior, |
| 4293 | // because we effectively do this lookup again when we add the new |
| 4294 | // class to the hash table --- necessary because of possible races with |
| 4295 | // other threads.) |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4296 | if (class_loader.Get() != component_type->GetClassLoader()) { |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 4297 | ObjPtr<mirror::Class> new_class = |
| 4298 | LookupClass(self, descriptor, hash, component_type->GetClassLoader()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4299 | if (new_class != nullptr) { |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 4300 | return new_class; |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 4301 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 4302 | } |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 4303 | // Core array classes, i.e. Object[], Class[], String[] and primitive |
| 4304 | // arrays, have special initialization and they should be found above. |
Santiago Aboy Solanes | 6cdabe1 | 2022-02-18 15:27:43 +0000 | [diff] [blame] | 4305 | DCHECK_IMPLIES(component_type->IsObjectClass(), |
| 4306 | // Guard from false positives for errors before setting superclass. |
| 4307 | component_type->IsErroneousUnresolved()); |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 4308 | DCHECK(!component_type->IsStringClass()); |
| 4309 | DCHECK(!component_type->IsClassClass()); |
| 4310 | DCHECK(!component_type->IsPrimitive()); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 4311 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 4312 | // Fill out the fields in the Class. |
| 4313 | // |
| 4314 | // It is possible to execute some methods against arrays, because |
| 4315 | // all arrays are subclasses of java_lang_Object_, so we need to set |
| 4316 | // up a vtable. We can just point at the one in java_lang_Object_. |
| 4317 | // |
| 4318 | // Array classes are simple enough that we don't need to do a full |
| 4319 | // link step. |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 4320 | size_t array_class_size = mirror::Array::ClassSize(image_pointer_size_); |
| 4321 | auto visitor = [this, array_class_size, component_type](ObjPtr<mirror::Object> obj, |
| 4322 | size_t usable_size) |
| 4323 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | b68bb7a | 2020-03-17 10:55:25 +0000 | [diff] [blame] | 4324 | ScopedAssertNoNewTransactionRecords sanntr("CreateArrayClass"); |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 4325 | mirror::Class::InitializeClassVisitor init_class(array_class_size); |
| 4326 | init_class(obj, usable_size); |
| 4327 | ObjPtr<mirror::Class> klass = ObjPtr<mirror::Class>::DownCast(obj); |
| 4328 | klass->SetComponentType(component_type.Get()); |
| 4329 | // Do not hold lock for initialization, the fence issued after the visitor |
| 4330 | // returns ensures memory visibility together with the implicit consume |
| 4331 | // semantics (for all supported architectures) for any thread that loads |
| 4332 | // the array class reference from any memory locations afterwards. |
| 4333 | FinishArrayClassSetup(klass); |
| 4334 | }; |
| 4335 | auto new_class = hs.NewHandle<mirror::Class>( |
| 4336 | AllocClass(self, GetClassRoot<mirror::Class>(this), array_class_size, visitor)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4337 | if (new_class == nullptr) { |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 4338 | self->AssertPendingOOMException(); |
| 4339 | return nullptr; |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 4340 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 4341 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4342 | ObjPtr<mirror::Class> existing = InsertClass(descriptor, new_class.Get(), hash); |
Mathieu Chartier | c0a9ea4 | 2014-02-03 16:36:49 -0800 | [diff] [blame] | 4343 | if (existing == nullptr) { |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4344 | // We postpone ClassLoad and ClassPrepare events to this point in time to avoid |
| 4345 | // duplicate events in case of races. Array classes don't really follow dedicated |
| 4346 | // load and prepare, anyways. |
| 4347 | Runtime::Current()->GetRuntimeCallbacks()->ClassLoad(new_class); |
| 4348 | Runtime::Current()->GetRuntimeCallbacks()->ClassPrepare(new_class, new_class); |
| 4349 | |
Tamas Berghammer | 160e6df | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 4350 | jit::Jit::NewTypeLoadedIfUsingJit(new_class.Get()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4351 | return new_class.Get(); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 4352 | } |
| 4353 | // Another thread must have loaded the class after we |
| 4354 | // started but before we finished. Abandon what we've |
| 4355 | // done. |
| 4356 | // |
| 4357 | // (Yes, this happens.) |
| 4358 | |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 4359 | return existing; |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 4360 | } |
| 4361 | |
Vladimir Marko | 9186b18 | 2018-11-06 14:55:54 +0000 | [diff] [blame] | 4362 | ObjPtr<mirror::Class> ClassLinker::LookupPrimitiveClass(char type) { |
| 4363 | ClassRoot class_root; |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 4364 | switch (type) { |
Vladimir Marko | 9186b18 | 2018-11-06 14:55:54 +0000 | [diff] [blame] | 4365 | case 'B': class_root = ClassRoot::kPrimitiveByte; break; |
| 4366 | case 'C': class_root = ClassRoot::kPrimitiveChar; break; |
| 4367 | case 'D': class_root = ClassRoot::kPrimitiveDouble; break; |
| 4368 | case 'F': class_root = ClassRoot::kPrimitiveFloat; break; |
| 4369 | case 'I': class_root = ClassRoot::kPrimitiveInt; break; |
| 4370 | case 'J': class_root = ClassRoot::kPrimitiveLong; break; |
| 4371 | case 'S': class_root = ClassRoot::kPrimitiveShort; break; |
| 4372 | case 'Z': class_root = ClassRoot::kPrimitiveBoolean; break; |
| 4373 | case 'V': class_root = ClassRoot::kPrimitiveVoid; break; |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 4374 | default: |
Vladimir Marko | 9186b18 | 2018-11-06 14:55:54 +0000 | [diff] [blame] | 4375 | return nullptr; |
Carl Shapiro | 744ad05 | 2011-08-06 15:53:36 -0700 | [diff] [blame] | 4376 | } |
Vladimir Marko | 9186b18 | 2018-11-06 14:55:54 +0000 | [diff] [blame] | 4377 | return GetClassRoot(class_root, this); |
| 4378 | } |
| 4379 | |
| 4380 | ObjPtr<mirror::Class> ClassLinker::FindPrimitiveClass(char type) { |
| 4381 | ObjPtr<mirror::Class> result = LookupPrimitiveClass(type); |
| 4382 | if (UNLIKELY(result == nullptr)) { |
| 4383 | std::string printable_type(PrintableChar(type)); |
| 4384 | ThrowNoClassDefFoundError("Not a primitive type: %s", printable_type.c_str()); |
| 4385 | } |
| 4386 | return result; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4387 | } |
| 4388 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 4389 | ObjPtr<mirror::Class> ClassLinker::InsertClass(const char* descriptor, |
| 4390 | ObjPtr<mirror::Class> klass, |
| 4391 | size_t hash) { |
Alex Light | e9f6103 | 2018-09-24 16:04:51 -0700 | [diff] [blame] | 4392 | DCHECK(Thread::Current()->CanLoadClasses()); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 4393 | if (VLOG_IS_ON(class_linker)) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4394 | ObjPtr<mirror::DexCache> dex_cache = klass->GetDexCache(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 4395 | std::string source; |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4396 | if (dex_cache != nullptr) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 4397 | source += " from "; |
| 4398 | source += dex_cache->GetLocation()->ToModifiedUtf8(); |
| 4399 | } |
| 4400 | LOG(INFO) << "Loaded class " << descriptor << source; |
| 4401 | } |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 4402 | { |
| 4403 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Vladimir Marko | 0984e48 | 2019-03-27 16:41:41 +0000 | [diff] [blame] | 4404 | const ObjPtr<mirror::ClassLoader> class_loader = klass->GetClassLoader(); |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 4405 | ClassTable* const class_table = InsertClassTableForClassLoader(class_loader); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4406 | ObjPtr<mirror::Class> existing = class_table->Lookup(descriptor, hash); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4407 | if (existing != nullptr) { |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 4408 | return existing; |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 4409 | } |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 4410 | VerifyObject(klass); |
| 4411 | class_table->InsertWithHash(klass, hash); |
| 4412 | if (class_loader != nullptr) { |
| 4413 | // 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] | 4414 | WriteBarrier::ForEveryFieldWrite(class_loader); |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 4415 | } |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 4416 | if (log_new_roots_) { |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 4417 | new_class_roots_.push_back(GcRoot<mirror::Class>(klass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4418 | } |
| 4419 | } |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 4420 | if (kIsDebugBuild) { |
| 4421 | // Test that copied methods correctly can find their holder. |
| 4422 | for (ArtMethod& method : klass->GetCopiedMethods(image_pointer_size_)) { |
| 4423 | CHECK_EQ(GetHoldingClassOfCopiedMethod(&method), klass); |
| 4424 | } |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 4425 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4426 | return nullptr; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4427 | } |
| 4428 | |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 4429 | void ClassLinker::WriteBarrierForBootOatFileBssRoots(const OatFile* oat_file) { |
Mathieu Chartier | a1467d0 | 2017-02-22 09:22:50 -0800 | [diff] [blame] | 4430 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
| 4431 | DCHECK(!oat_file->GetBssGcRoots().empty()) << oat_file->GetLocation(); |
| 4432 | if (log_new_roots_ && !ContainsElement(new_bss_roots_boot_oat_files_, oat_file)) { |
| 4433 | new_bss_roots_boot_oat_files_.push_back(oat_file); |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 4434 | } |
| 4435 | } |
| 4436 | |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4437 | // TODO This should really be in mirror::Class. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4438 | void ClassLinker::UpdateClassMethods(ObjPtr<mirror::Class> klass, |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4439 | LengthPrefixedArray<ArtMethod>* new_methods) { |
| 4440 | klass->SetMethodsPtrUnchecked(new_methods, |
| 4441 | klass->NumDirectMethods(), |
| 4442 | klass->NumDeclaredVirtualMethods()); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4443 | // 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] | 4444 | WriteBarrier::ForEveryFieldWrite(klass); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4445 | } |
| 4446 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 4447 | ObjPtr<mirror::Class> ClassLinker::LookupClass(Thread* self, |
| 4448 | const char* descriptor, |
| 4449 | ObjPtr<mirror::ClassLoader> class_loader) { |
Andreas Gampe | 2ff3b97 | 2017-06-05 18:14:53 -0700 | [diff] [blame] | 4450 | return LookupClass(self, descriptor, ComputeModifiedUtf8Hash(descriptor), class_loader); |
| 4451 | } |
| 4452 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 4453 | ObjPtr<mirror::Class> ClassLinker::LookupClass(Thread* self, |
| 4454 | const char* descriptor, |
| 4455 | size_t hash, |
| 4456 | ObjPtr<mirror::ClassLoader> class_loader) { |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 4457 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 4458 | ClassTable* const class_table = ClassTableForClassLoader(class_loader); |
| 4459 | if (class_table != nullptr) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4460 | ObjPtr<mirror::Class> result = class_table->Lookup(descriptor, hash); |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 4461 | if (result != nullptr) { |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 4462 | return result; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4463 | } |
Sameer Abu Asal | 2c6de22 | 2013-05-02 17:38:59 -0700 | [diff] [blame] | 4464 | } |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 4465 | return nullptr; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4466 | } |
| 4467 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4468 | class MoveClassTableToPreZygoteVisitor : public ClassLoaderVisitor { |
| 4469 | public: |
Igor Murashkin | 2ffb703 | 2017-11-08 13:35:21 -0800 | [diff] [blame] | 4470 | MoveClassTableToPreZygoteVisitor() {} |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4471 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4472 | void Visit(ObjPtr<mirror::ClassLoader> class_loader) |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4473 | REQUIRES(Locks::classlinker_classes_lock_) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 4474 | REQUIRES_SHARED(Locks::mutator_lock_) override { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4475 | ClassTable* const class_table = class_loader->GetClassTable(); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 4476 | if (class_table != nullptr) { |
| 4477 | class_table->FreezeSnapshot(); |
| 4478 | } |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 4479 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4480 | }; |
| 4481 | |
| 4482 | void ClassLinker::MoveClassTableToPreZygote() { |
| 4483 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 4484 | boot_class_table_->FreezeSnapshot(); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4485 | MoveClassTableToPreZygoteVisitor visitor; |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 4486 | VisitClassLoaders(&visitor); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 4487 | } |
| 4488 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4489 | // Look up classes by hash and descriptor and put all matching ones in the result array. |
| 4490 | class LookupClassesVisitor : public ClassLoaderVisitor { |
| 4491 | public: |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4492 | LookupClassesVisitor(const char* descriptor, |
| 4493 | size_t hash, |
| 4494 | std::vector<ObjPtr<mirror::Class>>* result) |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4495 | : descriptor_(descriptor), |
| 4496 | hash_(hash), |
| 4497 | result_(result) {} |
| 4498 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4499 | void Visit(ObjPtr<mirror::ClassLoader> class_loader) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 4500 | REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_) override { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4501 | ClassTable* const class_table = class_loader->GetClassTable(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4502 | ObjPtr<mirror::Class> klass = class_table->Lookup(descriptor_, hash_); |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 4503 | // Add `klass` only if `class_loader` is its defining (not just initiating) class loader. |
| 4504 | if (klass != nullptr && klass->GetClassLoader() == class_loader) { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4505 | result_->push_back(klass); |
| 4506 | } |
| 4507 | } |
| 4508 | |
| 4509 | private: |
| 4510 | const char* const descriptor_; |
| 4511 | const size_t hash_; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4512 | std::vector<ObjPtr<mirror::Class>>* const result_; |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4513 | }; |
| 4514 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4515 | void ClassLinker::LookupClasses(const char* descriptor, |
| 4516 | std::vector<ObjPtr<mirror::Class>>& result) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4517 | result.clear(); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4518 | Thread* const self = Thread::Current(); |
| 4519 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 4520 | const size_t hash = ComputeModifiedUtf8Hash(descriptor); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 4521 | ObjPtr<mirror::Class> klass = boot_class_table_->Lookup(descriptor, hash); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 4522 | if (klass != nullptr) { |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 4523 | DCHECK(klass->GetClassLoader() == nullptr); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 4524 | result.push_back(klass); |
| 4525 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4526 | LookupClassesVisitor visitor(descriptor, hash, &result); |
| 4527 | VisitClassLoaders(&visitor); |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 4528 | } |
| 4529 | |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4530 | bool ClassLinker::AttemptSupertypeVerification(Thread* self, |
Nicolas Geoffray | 5b0b2e1 | 2021-03-19 14:48:40 +0000 | [diff] [blame] | 4531 | verifier::VerifierDeps* verifier_deps, |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4532 | Handle<mirror::Class> klass, |
| 4533 | Handle<mirror::Class> supertype) { |
| 4534 | DCHECK(self != nullptr); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4535 | DCHECK(klass != nullptr); |
| 4536 | DCHECK(supertype != nullptr); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4537 | |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4538 | if (!supertype->IsVerified() && !supertype->IsErroneous()) { |
Nicolas Geoffray | 5b0b2e1 | 2021-03-19 14:48:40 +0000 | [diff] [blame] | 4539 | VerifyClass(self, verifier_deps, supertype); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4540 | } |
Nicolas Geoffray | 7cc3ae5 | 2017-03-07 14:33:37 +0000 | [diff] [blame] | 4541 | |
Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 4542 | if (supertype->IsVerified() |
| 4543 | || supertype->ShouldVerifyAtRuntime() |
| 4544 | || supertype->IsVerifiedNeedsAccessChecks()) { |
Nicolas Geoffray | 7cc3ae5 | 2017-03-07 14:33:37 +0000 | [diff] [blame] | 4545 | // The supertype is either verified, or we soft failed at AOT time. |
| 4546 | DCHECK(supertype->IsVerified() || Runtime::Current()->IsAotCompiler()); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4547 | return true; |
| 4548 | } |
| 4549 | // If we got this far then we have a hard failure. |
| 4550 | std::string error_msg = |
| 4551 | StringPrintf("Rejecting class %s that attempts to sub-type erroneous class %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4552 | klass->PrettyDescriptor().c_str(), |
| 4553 | supertype->PrettyDescriptor().c_str()); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4554 | LOG(WARNING) << error_msg << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8(); |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4555 | StackHandleScope<1> hs(self); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4556 | Handle<mirror::Throwable> cause(hs.NewHandle(self->GetException())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4557 | if (cause != nullptr) { |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4558 | // Set during VerifyClass call (if at all). |
| 4559 | self->ClearException(); |
| 4560 | } |
| 4561 | // Change into a verify error. |
| 4562 | ThrowVerifyError(klass.Get(), "%s", error_msg.c_str()); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4563 | if (cause != nullptr) { |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4564 | self->GetException()->SetCause(cause.Get()); |
| 4565 | } |
| 4566 | ClassReference ref(klass->GetDexCache()->GetDexFile(), klass->GetDexClassDefIndex()); |
| 4567 | if (Runtime::Current()->IsAotCompiler()) { |
| 4568 | Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref); |
| 4569 | } |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4570 | // Need to grab the lock to change status. |
| 4571 | ObjectLock<mirror::Class> super_lock(self, klass); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4572 | mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4573 | return false; |
| 4574 | } |
| 4575 | |
Nicolas Geoffray | 5b0b2e1 | 2021-03-19 14:48:40 +0000 | [diff] [blame] | 4576 | verifier::FailureKind ClassLinker::VerifyClass(Thread* self, |
| 4577 | verifier::VerifierDeps* verifier_deps, |
| 4578 | Handle<mirror::Class> klass, |
| 4579 | verifier::HardFailLogMode log_level) { |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4580 | { |
| 4581 | // TODO: assert that the monitor on the Class is held |
| 4582 | ObjectLock<mirror::Class> lock(self, klass); |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 4583 | |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4584 | // Is somebody verifying this now? |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4585 | ClassStatus old_status = klass->GetStatus(); |
Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 4586 | while (old_status == ClassStatus::kVerifying) { |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4587 | lock.WaitIgnoringInterrupts(); |
Mathieu Chartier | 5ef7020 | 2017-06-29 10:45:10 -0700 | [diff] [blame] | 4588 | // WaitIgnoringInterrupts can still receive an interrupt and return early, in this |
| 4589 | // case we may see the same status again. b/62912904. This is why the check is |
| 4590 | // greater or equal. |
| 4591 | CHECK(klass->IsErroneous() || (klass->GetStatus() >= old_status)) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4592 | << "Class '" << klass->PrettyClass() |
| 4593 | << "' performed an illegal verification state transition from " << old_status |
| 4594 | << " to " << klass->GetStatus(); |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4595 | old_status = klass->GetStatus(); |
| 4596 | } |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 4597 | |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4598 | // The class might already be erroneous, for example at compile time if we attempted to verify |
| 4599 | // this class as a parent to another. |
| 4600 | if (klass->IsErroneous()) { |
| 4601 | ThrowEarlierClassFailure(klass.Get()); |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4602 | return verifier::FailureKind::kHardFailure; |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4603 | } |
Brian Carlstrom | 9b5ee88 | 2012-02-28 09:48:54 -0800 | [diff] [blame] | 4604 | |
Nicolas Geoffray | 7cc3ae5 | 2017-03-07 14:33:37 +0000 | [diff] [blame] | 4605 | // Don't attempt to re-verify if already verified. |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4606 | if (klass->IsVerified()) { |
Nicolas Geoffray | 8078996 | 2021-04-30 16:50:39 +0100 | [diff] [blame] | 4607 | if (verifier_deps != nullptr && |
| 4608 | verifier_deps->ContainsDexFile(klass->GetDexFile()) && |
| 4609 | !verifier_deps->HasRecordedVerifiedStatus(klass->GetDexFile(), *klass->GetClassDef()) && |
| 4610 | !Runtime::Current()->IsAotCompiler()) { |
| 4611 | // If the klass is verified, but `verifier_deps` did not record it, this |
| 4612 | // means we are running background verification of a secondary dex file. |
| 4613 | // Re-run the verifier to populate `verifier_deps`. |
| 4614 | // No need to run the verification when running on the AOT Compiler, as |
| 4615 | // the driver handles those multithreaded cases already. |
| 4616 | std::string error_msg; |
| 4617 | verifier::FailureKind failure = |
| 4618 | PerformClassVerification(self, verifier_deps, klass, log_level, &error_msg); |
| 4619 | // We could have soft failures, so just check that we don't have a hard |
| 4620 | // failure. |
| 4621 | DCHECK_NE(failure, verifier::FailureKind::kHardFailure) << error_msg; |
| 4622 | } |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4623 | return verifier::FailureKind::kNoFailure; |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4624 | } |
Nicolas Geoffray | 7cc3ae5 | 2017-03-07 14:33:37 +0000 | [diff] [blame] | 4625 | |
Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 4626 | if (klass->IsVerifiedNeedsAccessChecks()) { |
| 4627 | if (!Runtime::Current()->IsAotCompiler()) { |
| 4628 | // Mark the class as having a verification attempt to avoid re-running |
Nicolas Geoffray | 66934ef | 2021-07-07 14:56:23 +0100 | [diff] [blame] | 4629 | // the verifier. |
Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 4630 | mirror::Class::SetStatus(klass, ClassStatus::kVerified, self); |
| 4631 | } |
| 4632 | return verifier::FailureKind::kAccessChecksFailure; |
| 4633 | } |
| 4634 | |
Nicolas Geoffray | 7cc3ae5 | 2017-03-07 14:33:37 +0000 | [diff] [blame] | 4635 | // For AOT, don't attempt to re-verify if we have already found we should |
| 4636 | // verify at runtime. |
Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 4637 | if (klass->ShouldVerifyAtRuntime()) { |
| 4638 | CHECK(Runtime::Current()->IsAotCompiler()); |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4639 | return verifier::FailureKind::kSoftFailure; |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4640 | } |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 4641 | |
Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 4642 | DCHECK_EQ(klass->GetStatus(), ClassStatus::kResolved); |
| 4643 | mirror::Class::SetStatus(klass, ClassStatus::kVerifying, self); |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4644 | |
| 4645 | // Skip verification if disabled. |
| 4646 | if (!Runtime::Current()->IsVerificationEnabled()) { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4647 | mirror::Class::SetStatus(klass, ClassStatus::kVerified, self); |
Nicolas Geoffray | 66934ef | 2021-07-07 14:56:23 +0100 | [diff] [blame] | 4648 | UpdateClassAfterVerification(klass, image_pointer_size_, verifier::FailureKind::kNoFailure); |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4649 | return verifier::FailureKind::kNoFailure; |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4650 | } |
Jeff Hao | 4a200f5 | 2014-04-01 14:58:49 -0700 | [diff] [blame] | 4651 | } |
| 4652 | |
Bharadwaj Kalandhabhatta | 271c1e1 | 2017-06-27 11:14:49 -0700 | [diff] [blame] | 4653 | VLOG(class_linker) << "Beginning verification for class: " |
| 4654 | << klass->PrettyDescriptor() |
| 4655 | << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8(); |
| 4656 | |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 4657 | // Verify super class. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4658 | StackHandleScope<2> hs(self); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4659 | MutableHandle<mirror::Class> supertype(hs.NewHandle(klass->GetSuperClass())); |
| 4660 | // If we have a superclass and we get a hard verification failure we can return immediately. |
Nicolas Geoffray | 5b0b2e1 | 2021-03-19 14:48:40 +0000 | [diff] [blame] | 4661 | if (supertype != nullptr && |
| 4662 | !AttemptSupertypeVerification(self, verifier_deps, klass, supertype)) { |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4663 | CHECK(self->IsExceptionPending()) << "Verification error should be pending."; |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4664 | return verifier::FailureKind::kHardFailure; |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4665 | } |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 4666 | |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4667 | // Verify all default super-interfaces. |
| 4668 | // |
| 4669 | // (1) Don't bother if the superclass has already had a soft verification failure. |
| 4670 | // |
| 4671 | // (2) Interfaces shouldn't bother to do this recursive verification because they cannot cause |
| 4672 | // recursive initialization by themselves. This is because when an interface is initialized |
| 4673 | // directly it must not initialize its superinterfaces. We are allowed to verify regardless |
| 4674 | // but choose not to for an optimization. If the interfaces is being verified due to a class |
| 4675 | // initialization (which would need all the default interfaces to be verified) the class code |
| 4676 | // will trigger the recursive verification anyway. |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4677 | if ((supertype == nullptr || supertype->IsVerified()) // See (1) |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4678 | && !klass->IsInterface()) { // See (2) |
| 4679 | int32_t iftable_count = klass->GetIfTableCount(); |
| 4680 | MutableHandle<mirror::Class> iface(hs.NewHandle<mirror::Class>(nullptr)); |
| 4681 | // Loop through all interfaces this class has defined. It doesn't matter the order. |
| 4682 | for (int32_t i = 0; i < iftable_count; i++) { |
| 4683 | iface.Assign(klass->GetIfTable()->GetInterface(i)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4684 | DCHECK(iface != nullptr); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4685 | // We only care if we have default interfaces and can skip if we are already verified... |
| 4686 | if (LIKELY(!iface->HasDefaultMethods() || iface->IsVerified())) { |
| 4687 | continue; |
Nicolas Geoffray | 5b0b2e1 | 2021-03-19 14:48:40 +0000 | [diff] [blame] | 4688 | } else if (UNLIKELY(!AttemptSupertypeVerification(self, verifier_deps, klass, iface))) { |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4689 | // We had a hard failure while verifying this interface. Just return immediately. |
| 4690 | CHECK(self->IsExceptionPending()) << "Verification error should be pending."; |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4691 | return verifier::FailureKind::kHardFailure; |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4692 | } else if (UNLIKELY(!iface->IsVerified())) { |
| 4693 | // We softly failed to verify the iface. Stop checking and clean up. |
| 4694 | // Put the iface into the supertype handle so we know what caused us to fail. |
| 4695 | supertype.Assign(iface.Get()); |
| 4696 | break; |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 4697 | } |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 4698 | } |
| 4699 | } |
| 4700 | |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4701 | // At this point if verification failed, then supertype is the "first" supertype that failed |
| 4702 | // verification (without a specific order). If verification succeeded, then supertype is either |
| 4703 | // null or the original superclass of klass and is verified. |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4704 | DCHECK(supertype == nullptr || |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4705 | supertype.Get() == klass->GetSuperClass() || |
| 4706 | !supertype->IsVerified()); |
| 4707 | |
Elliott Hughes | 634eb2e | 2012-03-22 16:06:28 -0700 | [diff] [blame] | 4708 | // 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] | 4709 | const DexFile& dex_file = *klass->GetDexCache()->GetDexFile(); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4710 | ClassStatus oat_file_class_status(ClassStatus::kNotReady); |
Nicolas Geoffray | 6df4511 | 2021-02-07 21:51:58 +0000 | [diff] [blame] | 4711 | bool preverified = VerifyClassUsingOatFile(self, dex_file, klass, oat_file_class_status); |
Bharadwaj Kalandhabhatta | 271c1e1 | 2017-06-27 11:14:49 -0700 | [diff] [blame] | 4712 | |
| 4713 | VLOG(class_linker) << "Class preverified status for class " |
| 4714 | << klass->PrettyDescriptor() |
| 4715 | << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8() |
| 4716 | << ": " |
Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 4717 | << preverified |
| 4718 | << "( " << oat_file_class_status << ")"; |
Bharadwaj Kalandhabhatta | 271c1e1 | 2017-06-27 11:14:49 -0700 | [diff] [blame] | 4719 | |
Nicolas Geoffray | 44dc8a3 | 2021-06-21 15:23:49 +0100 | [diff] [blame] | 4720 | // If the oat file says the class had an error, re-run the verifier. That way we will either: |
| 4721 | // 1) Be successful at runtime, or |
| 4722 | // 2) Get a precise error message. |
Santiago Aboy Solanes | 6cdabe1 | 2022-02-18 15:27:43 +0000 | [diff] [blame] | 4723 | DCHECK_IMPLIES(mirror::Class::IsErroneous(oat_file_class_status), !preverified); |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 4724 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4725 | std::string error_msg; |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4726 | verifier::FailureKind verifier_failure = verifier::FailureKind::kNoFailure; |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 4727 | if (!preverified) { |
Nicolas Geoffray | 5b0b2e1 | 2021-03-19 14:48:40 +0000 | [diff] [blame] | 4728 | verifier_failure = PerformClassVerification(self, verifier_deps, klass, log_level, &error_msg); |
Nicolas Geoffray | 66934ef | 2021-07-07 14:56:23 +0100 | [diff] [blame] | 4729 | } else if (oat_file_class_status == ClassStatus::kVerifiedNeedsAccessChecks) { |
| 4730 | verifier_failure = verifier::FailureKind::kAccessChecksFailure; |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 4731 | } |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4732 | |
| 4733 | // Verification is done, grab the lock again. |
| 4734 | ObjectLock<mirror::Class> lock(self, klass); |
Nicolas Geoffray | 66934ef | 2021-07-07 14:56:23 +0100 | [diff] [blame] | 4735 | self->AssertNoPendingException(); |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4736 | |
Nicolas Geoffray | 66934ef | 2021-07-07 14:56:23 +0100 | [diff] [blame] | 4737 | if (verifier_failure == verifier::FailureKind::kHardFailure) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4738 | VLOG(verifier) << "Verification failed on class " << klass->PrettyDescriptor() |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 4739 | << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8() |
| 4740 | << " because: " << error_msg; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4741 | ThrowVerifyError(klass.Get(), "%s", error_msg.c_str()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4742 | mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self); |
Nicolas Geoffray | 66934ef | 2021-07-07 14:56:23 +0100 | [diff] [blame] | 4743 | return verifier_failure; |
jeffhao | 5cfd6fb | 2011-09-27 13:54:29 -0700 | [diff] [blame] | 4744 | } |
Nicolas Geoffray | 66934ef | 2021-07-07 14:56:23 +0100 | [diff] [blame] | 4745 | |
| 4746 | // Make sure all classes referenced by catch blocks are resolved. |
| 4747 | ResolveClassExceptionHandlerTypes(klass); |
| 4748 | |
Nicolas Geoffray | 486dda0 | 2017-09-11 14:15:52 +0100 | [diff] [blame] | 4749 | if (Runtime::Current()->IsAotCompiler()) { |
Nicolas Geoffray | 66934ef | 2021-07-07 14:56:23 +0100 | [diff] [blame] | 4750 | if (supertype != nullptr && supertype->ShouldVerifyAtRuntime()) { |
| 4751 | // Regardless of our own verification result, we need to verify the class |
| 4752 | // at runtime if the super class is not verified. This is required in case |
| 4753 | // we generate an app/boot image. |
Nicolas Geoffray | 66934ef | 2021-07-07 14:56:23 +0100 | [diff] [blame] | 4754 | mirror::Class::SetStatus(klass, ClassStatus::kRetryVerificationAtRuntime, self); |
| 4755 | } else if (verifier_failure == verifier::FailureKind::kNoFailure) { |
| 4756 | mirror::Class::SetStatus(klass, ClassStatus::kVerified, self); |
| 4757 | } else if (verifier_failure == verifier::FailureKind::kSoftFailure || |
| 4758 | verifier_failure == verifier::FailureKind::kTypeChecksFailure) { |
| 4759 | mirror::Class::SetStatus(klass, ClassStatus::kRetryVerificationAtRuntime, self); |
| 4760 | } else { |
| 4761 | mirror::Class::SetStatus(klass, ClassStatus::kVerifiedNeedsAccessChecks, self); |
| 4762 | } |
| 4763 | // Notify the compiler about the verification status, in case the class |
| 4764 | // was verified implicitly (eg super class of a compiled class). When the |
| 4765 | // compiler unloads dex file after compilation, we still want to keep |
| 4766 | // verification states. |
Nicolas Geoffray | 486dda0 | 2017-09-11 14:15:52 +0100 | [diff] [blame] | 4767 | Runtime::Current()->GetCompilerCallbacks()->UpdateClassState( |
| 4768 | ClassReference(&klass->GetDexFile(), klass->GetDexClassDefIndex()), klass->GetStatus()); |
Nicolas Geoffray | 66934ef | 2021-07-07 14:56:23 +0100 | [diff] [blame] | 4769 | } else { |
| 4770 | mirror::Class::SetStatus(klass, ClassStatus::kVerified, self); |
Nicolas Geoffray | 486dda0 | 2017-09-11 14:15:52 +0100 | [diff] [blame] | 4771 | } |
Nicolas Geoffray | 66934ef | 2021-07-07 14:56:23 +0100 | [diff] [blame] | 4772 | |
| 4773 | UpdateClassAfterVerification(klass, image_pointer_size_, verifier_failure); |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame] | 4774 | return verifier_failure; |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 4775 | } |
| 4776 | |
Mathieu Chartier | 9e050df | 2017-08-09 10:05:47 -0700 | [diff] [blame] | 4777 | verifier::FailureKind ClassLinker::PerformClassVerification(Thread* self, |
Nicolas Geoffray | 5b0b2e1 | 2021-03-19 14:48:40 +0000 | [diff] [blame] | 4778 | verifier::VerifierDeps* verifier_deps, |
Mathieu Chartier | 9e050df | 2017-08-09 10:05:47 -0700 | [diff] [blame] | 4779 | Handle<mirror::Class> klass, |
| 4780 | verifier::HardFailLogMode log_level, |
| 4781 | std::string* error_msg) { |
| 4782 | Runtime* const runtime = Runtime::Current(); |
Nicolas Geoffray | 7744b69 | 2021-07-06 16:19:32 +0100 | [diff] [blame] | 4783 | StackHandleScope<2> hs(self); |
| 4784 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache())); |
| 4785 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader())); |
Andreas Gampe | a43ba3d | 2019-03-13 15:49:20 -0700 | [diff] [blame] | 4786 | return verifier::ClassVerifier::VerifyClass(self, |
Nicolas Geoffray | 5b0b2e1 | 2021-03-19 14:48:40 +0000 | [diff] [blame] | 4787 | verifier_deps, |
Nicolas Geoffray | 7744b69 | 2021-07-06 16:19:32 +0100 | [diff] [blame] | 4788 | dex_cache->GetDexFile(), |
| 4789 | klass, |
| 4790 | dex_cache, |
| 4791 | class_loader, |
| 4792 | *klass->GetClassDef(), |
Andreas Gampe | a43ba3d | 2019-03-13 15:49:20 -0700 | [diff] [blame] | 4793 | runtime->GetCompilerCallbacks(), |
Andreas Gampe | a43ba3d | 2019-03-13 15:49:20 -0700 | [diff] [blame] | 4794 | log_level, |
| 4795 | Runtime::Current()->GetTargetSdkVersion(), |
| 4796 | error_msg); |
Mathieu Chartier | 9e050df | 2017-08-09 10:05:47 -0700 | [diff] [blame] | 4797 | } |
| 4798 | |
Nicolas Geoffray | 6df4511 | 2021-02-07 21:51:58 +0000 | [diff] [blame] | 4799 | bool ClassLinker::VerifyClassUsingOatFile(Thread* self, |
| 4800 | const DexFile& dex_file, |
| 4801 | Handle<mirror::Class> klass, |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4802 | ClassStatus& oat_file_class_status) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 4803 | // If we're compiling, we can only verify the class using the oat file if |
| 4804 | // 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] | 4805 | // the compilation unit (app - dependencies). We will let the compiler callback |
| 4806 | // tell us about the latter. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 4807 | if (Runtime::Current()->IsAotCompiler()) { |
Andreas Gampe | e993458 | 2018-01-19 21:23:04 -0800 | [diff] [blame] | 4808 | CompilerCallbacks* callbacks = Runtime::Current()->GetCompilerCallbacks(); |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 4809 | // We are compiling an app (not the image). |
Nicolas Geoffray | 6df4511 | 2021-02-07 21:51:58 +0000 | [diff] [blame] | 4810 | if (!callbacks->CanUseOatStatusForVerification(klass.Get())) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 4811 | return false; |
| 4812 | } |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4813 | } |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 4814 | |
Andreas Gampe | b40d361 | 2018-06-26 15:49:42 -0700 | [diff] [blame] | 4815 | const OatDexFile* oat_dex_file = dex_file.GetOatDexFile(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4816 | // 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] | 4817 | if (oat_dex_file == nullptr || oat_dex_file->GetOatFile() == nullptr) { |
Anwar Ghuloum | ad256bb | 2013-07-18 14:58:55 -0700 | [diff] [blame] | 4818 | return false; |
| 4819 | } |
| 4820 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 4821 | uint16_t class_def_index = klass->GetDexClassDefIndex(); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 4822 | oat_file_class_status = oat_dex_file->GetOatClass(class_def_index).GetStatus(); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4823 | if (oat_file_class_status >= ClassStatus::kVerified) { |
Mathieu Chartier | a079e3a | 2016-03-16 19:08:31 -0700 | [diff] [blame] | 4824 | return true; |
| 4825 | } |
Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 4826 | if (oat_file_class_status >= ClassStatus::kVerifiedNeedsAccessChecks) { |
| 4827 | // We return that the clas has already been verified, and the caller should |
| 4828 | // check the class status to ensure we run with access checks. |
| 4829 | return true; |
| 4830 | } |
Nicolas Geoffray | 6df4511 | 2021-02-07 21:51:58 +0000 | [diff] [blame] | 4831 | |
| 4832 | // Check the class status with the vdex file. |
| 4833 | const OatFile* oat_file = oat_dex_file->GetOatFile(); |
| 4834 | if (oat_file != nullptr) { |
Nicolas Geoffray | 44dc8a3 | 2021-06-21 15:23:49 +0100 | [diff] [blame] | 4835 | ClassStatus vdex_status = oat_file->GetVdexFile()->ComputeClassStatus(self, klass); |
| 4836 | if (vdex_status >= ClassStatus::kVerifiedNeedsAccessChecks) { |
Nicolas Geoffray | 327cfcf | 2021-10-12 14:13:25 +0100 | [diff] [blame] | 4837 | VLOG(verifier) << "Vdex verification success for " << klass->PrettyClass(); |
Nicolas Geoffray | 44dc8a3 | 2021-06-21 15:23:49 +0100 | [diff] [blame] | 4838 | oat_file_class_status = vdex_status; |
Nicolas Geoffray | 6df4511 | 2021-02-07 21:51:58 +0000 | [diff] [blame] | 4839 | return true; |
| 4840 | } |
| 4841 | } |
| 4842 | |
Mathieu Chartier | a079e3a | 2016-03-16 19:08:31 -0700 | [diff] [blame] | 4843 | // If we only verified a subset of the classes at compile time, we can end up with classes that |
| 4844 | // were resolved by the verifier. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4845 | if (oat_file_class_status == ClassStatus::kResolved) { |
Mathieu Chartier | a079e3a | 2016-03-16 19:08:31 -0700 | [diff] [blame] | 4846 | return false; |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4847 | } |
Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 4848 | // We never expect a .oat file to have kRetryVerificationAtRuntime statuses. |
| 4849 | CHECK_NE(oat_file_class_status, ClassStatus::kRetryVerificationAtRuntime) |
| 4850 | << klass->PrettyClass() << " " << dex_file.GetLocation(); |
| 4851 | |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 4852 | if (mirror::Class::IsErroneous(oat_file_class_status)) { |
Nicolas Geoffray | 44dc8a3 | 2021-06-21 15:23:49 +0100 | [diff] [blame] | 4853 | // Compile time verification failed with a hard error. We'll re-run |
| 4854 | // verification, which might be successful at runtime. |
jeffhao | 1ac2944 | 2012-03-26 11:37:32 -0700 | [diff] [blame] | 4855 | return false; |
| 4856 | } |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4857 | if (oat_file_class_status == ClassStatus::kNotReady) { |
Ian Rogers | c476227 | 2012-02-01 15:55:55 -0800 | [diff] [blame] | 4858 | // Status is uninitialized if we couldn't determine the status at compile time, for example, |
| 4859 | // not loading the class. |
| 4860 | // TODO: when the verifier doesn't rely on Class-es failing to resolve/load the type hierarchy |
| 4861 | // isn't a problem and this case shouldn't occur |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4862 | return false; |
| 4863 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 4864 | std::string temp; |
Elliott Hughes | 634eb2e | 2012-03-22 16:06:28 -0700 | [diff] [blame] | 4865 | LOG(FATAL) << "Unexpected class status: " << oat_file_class_status |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4866 | << " " << dex_file.GetLocation() << " " << klass->PrettyClass() << " " |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 4867 | << klass->GetDescriptor(&temp); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 4868 | UNREACHABLE(); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4869 | } |
| 4870 | |
Alex Light | 5a55986 | 2016-01-29 12:24:48 -0800 | [diff] [blame] | 4871 | void ClassLinker::ResolveClassExceptionHandlerTypes(Handle<mirror::Class> klass) { |
Alex Light | 51a64d5 | 2015-12-17 13:55:59 -0800 | [diff] [blame] | 4872 | for (ArtMethod& method : klass->GetMethods(image_pointer_size_)) { |
Alex Light | 5a55986 | 2016-01-29 12:24:48 -0800 | [diff] [blame] | 4873 | ResolveMethodExceptionHandlerTypes(&method); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4874 | } |
| 4875 | } |
| 4876 | |
Alex Light | 5a55986 | 2016-01-29 12:24:48 -0800 | [diff] [blame] | 4877 | void ClassLinker::ResolveMethodExceptionHandlerTypes(ArtMethod* method) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4878 | // similar to DexVerifier::ScanTryCatchBlocks and dex2oat's ResolveExceptionsForMethod. |
David Sehr | 0225f8e | 2018-01-31 08:52:24 +0000 | [diff] [blame] | 4879 | CodeItemDataAccessor accessor(method->DexInstructionData()); |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 4880 | if (!accessor.HasCodeItem()) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4881 | return; // native or abstract method |
| 4882 | } |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 4883 | if (accessor.TriesSize() == 0) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4884 | return; // nothing to process |
| 4885 | } |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 4886 | const uint8_t* handlers_ptr = accessor.GetCatchHandlerData(0); |
Nicolas Geoffray | ade0ca6 | 2022-10-10 13:35:11 +0100 | [diff] [blame] | 4887 | CHECK(method->GetDexFile()->IsInDataSection(handlers_ptr)) |
| 4888 | << method->PrettyMethod() |
| 4889 | << "@" << method->GetDexFile()->GetLocation() |
| 4890 | << "@" << reinterpret_cast<const void*>(handlers_ptr) |
| 4891 | << " is_compact_dex=" << method->GetDexFile()->IsCompactDexFile(); |
| 4892 | |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4893 | uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4894 | for (uint32_t idx = 0; idx < handlers_size; idx++) { |
| 4895 | CatchHandlerIterator iterator(handlers_ptr); |
| 4896 | for (; iterator.HasNext(); iterator.Next()) { |
| 4897 | // Ensure exception types are resolved so that they don't need resolution to be delivered, |
| 4898 | // unresolved exception types will be ignored by exception delivery |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 4899 | if (iterator.GetHandlerTypeIndex().IsValid()) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4900 | ObjPtr<mirror::Class> exception_type = ResolveType(iterator.GetHandlerTypeIndex(), method); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4901 | if (exception_type == nullptr) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4902 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 4903 | Thread::Current()->ClearException(); |
| 4904 | } |
| 4905 | } |
| 4906 | } |
| 4907 | handlers_ptr = iterator.EndDataPointer(); |
| 4908 | } |
| 4909 | } |
| 4910 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 4911 | ObjPtr<mirror::Class> ClassLinker::CreateProxyClass(ScopedObjectAccessAlreadyRunnable& soa, |
| 4912 | jstring name, |
| 4913 | jobjectArray interfaces, |
| 4914 | jobject loader, |
| 4915 | jobjectArray methods, |
| 4916 | jobjectArray throws) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4917 | Thread* self = soa.Self(); |
Alex Light | e9f6103 | 2018-09-24 16:04:51 -0700 | [diff] [blame] | 4918 | |
| 4919 | // This is to prevent the calls to ClassLoad and ClassPrepare which can cause java/user-supplied |
| 4920 | // code to be executed. We put it up here so we can avoid all the allocations associated with |
| 4921 | // creating the class. This can happen with (eg) jit-threads. |
| 4922 | if (!self->CanLoadClasses()) { |
| 4923 | // Make sure we don't try to load anything, potentially causing an infinite loop. |
| 4924 | ObjPtr<mirror::Throwable> pre_allocated = |
| 4925 | Runtime::Current()->GetPreAllocatedNoClassDefFoundError(); |
| 4926 | self->SetException(pre_allocated); |
| 4927 | return nullptr; |
| 4928 | } |
| 4929 | |
Nicolas Geoffray | c9d5949 | 2022-11-17 15:03:09 +0000 | [diff] [blame] | 4930 | StackHandleScope<12> hs(self); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4931 | MutableHandle<mirror::Class> temp_klass(hs.NewHandle( |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 4932 | AllocClass(self, GetClassRoot<mirror::Class>(this), sizeof(mirror::Class)))); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4933 | if (temp_klass == nullptr) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4934 | CHECK(self->IsExceptionPending()); // OOME. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4935 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4936 | } |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4937 | DCHECK(temp_klass->GetClass() != nullptr); |
| 4938 | temp_klass->SetObjectSize(sizeof(mirror::Proxy)); |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 4939 | // Set the class access flags incl. VerificationAttempted, so we do not try to set the flag on |
| 4940 | // the methods. |
Nicolas Geoffray | 66934ef | 2021-07-07 14:56:23 +0100 | [diff] [blame] | 4941 | temp_klass->SetAccessFlagsDuringLinking(kAccClassIsProxy | kAccPublic | kAccFinal); |
Nicolas Geoffray | c9d5949 | 2022-11-17 15:03:09 +0000 | [diff] [blame] | 4942 | temp_klass->SetClassLoader(soa.Decode<mirror::ClassLoader>(loader)); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4943 | DCHECK_EQ(temp_klass->GetPrimitiveType(), Primitive::kPrimNot); |
| 4944 | temp_klass->SetName(soa.Decode<mirror::String>(name)); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 4945 | temp_klass->SetDexCache(GetClassRoot<mirror::Proxy>(this)->GetDexCache()); |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 4946 | // Object has an empty iftable, copy it for that reason. |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 4947 | temp_klass->SetIfTable(GetClassRoot<mirror::Object>(this)->GetIfTable()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4948 | mirror::Class::SetStatus(temp_klass, ClassStatus::kIdx, self); |
Vladimir Marko | 3892e62 | 2019-03-15 15:22:18 +0000 | [diff] [blame] | 4949 | std::string storage; |
| 4950 | const char* descriptor = temp_klass->GetDescriptor(&storage); |
| 4951 | const size_t hash = ComputeModifiedUtf8Hash(descriptor); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4952 | |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 4953 | // 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] | 4954 | LinearAlloc* const allocator = GetOrCreateAllocatorForClassLoader(temp_klass->GetClassLoader()); |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 4955 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4956 | // Insert the class before loading the fields as the field roots |
| 4957 | // (ArtField::declaring_class_) are only visited from the class |
| 4958 | // table. There can't be any suspend points between inserting the |
| 4959 | // class and setting the field arrays below. |
Vladimir Marko | 3892e62 | 2019-03-15 15:22:18 +0000 | [diff] [blame] | 4960 | ObjPtr<mirror::Class> existing = InsertClass(descriptor, temp_klass.Get(), hash); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4961 | CHECK(existing == nullptr); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4962 | |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 4963 | // Instance fields are inherited, but we add a couple of static fields... |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4964 | const size_t num_fields = 2; |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 4965 | LengthPrefixedArray<ArtField>* sfields = AllocArtFieldArray(self, allocator, num_fields); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4966 | temp_klass->SetSFieldsPtr(sfields); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4967 | |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 4968 | // 1. Create a static field 'interfaces' that holds the _declared_ interfaces implemented by |
| 4969 | // our proxy, so Class.getInterfaces doesn't return the flattened set. |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4970 | ArtField& interfaces_sfield = sfields->At(0); |
| 4971 | interfaces_sfield.SetDexFieldIndex(0); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4972 | interfaces_sfield.SetDeclaringClass(temp_klass.Get()); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4973 | interfaces_sfield.SetAccessFlags(kAccStatic | kAccPublic | kAccFinal); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4974 | |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 4975 | // 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] | 4976 | ArtField& throws_sfield = sfields->At(1); |
| 4977 | throws_sfield.SetDexFieldIndex(1); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4978 | throws_sfield.SetDeclaringClass(temp_klass.Get()); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4979 | throws_sfield.SetAccessFlags(kAccStatic | kAccPublic | kAccFinal); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4980 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4981 | // Proxies have 1 direct method, the constructor |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4982 | const size_t num_direct_methods = 1; |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4983 | |
Alex Light | 133987d | 2020-03-26 19:22:12 +0000 | [diff] [blame] | 4984 | // The array we get passed contains all methods, including private and static |
| 4985 | // ones that aren't proxied. We need to filter those out since only interface |
| 4986 | // methods (non-private & virtual) are actually proxied. |
| 4987 | Handle<mirror::ObjectArray<mirror::Method>> h_methods = |
| 4988 | hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::Method>>(methods)); |
Vladimir Marko | 679730e | 2018-05-25 15:06:48 +0100 | [diff] [blame] | 4989 | DCHECK_EQ(h_methods->GetClass(), GetClassRoot<mirror::ObjectArray<mirror::Method>>()) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4990 | << mirror::Class::PrettyClass(h_methods->GetClass()); |
Alex Light | 133987d | 2020-03-26 19:22:12 +0000 | [diff] [blame] | 4991 | // List of the actual virtual methods this class will have. |
| 4992 | std::vector<ArtMethod*> proxied_methods; |
| 4993 | std::vector<size_t> proxied_throws_idx; |
| 4994 | proxied_methods.reserve(h_methods->GetLength()); |
| 4995 | proxied_throws_idx.reserve(h_methods->GetLength()); |
| 4996 | // Filter out to only the non-private virtual methods. |
| 4997 | for (auto [mirror, idx] : ZipCount(h_methods.Iterate<mirror::Method>())) { |
| 4998 | ArtMethod* m = mirror->GetArtMethod(); |
| 4999 | if (!m->IsPrivate() && !m->IsStatic()) { |
| 5000 | proxied_methods.push_back(m); |
| 5001 | proxied_throws_idx.push_back(idx); |
| 5002 | } |
| 5003 | } |
| 5004 | const size_t num_virtual_methods = proxied_methods.size(); |
Alex Light | bc11509 | 2020-03-27 11:25:16 -0700 | [diff] [blame] | 5005 | // We also need to filter out the 'throws'. The 'throws' are a Class[][] that |
| 5006 | // contains an array of all the classes each function is declared to throw. |
| 5007 | // This is used to wrap unexpected exceptions in a |
| 5008 | // UndeclaredThrowableException exception. This array is in the same order as |
| 5009 | // the methods array and like the methods array must be filtered to remove any |
| 5010 | // non-proxied methods. |
Alex Light | 133987d | 2020-03-26 19:22:12 +0000 | [diff] [blame] | 5011 | const bool has_filtered_methods = |
| 5012 | static_cast<int32_t>(num_virtual_methods) != h_methods->GetLength(); |
| 5013 | MutableHandle<mirror::ObjectArray<mirror::ObjectArray<mirror::Class>>> original_proxied_throws( |
| 5014 | hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::ObjectArray<mirror::Class>>>(throws))); |
| 5015 | MutableHandle<mirror::ObjectArray<mirror::ObjectArray<mirror::Class>>> proxied_throws( |
| 5016 | hs.NewHandle<mirror::ObjectArray<mirror::ObjectArray<mirror::Class>>>( |
| 5017 | (has_filtered_methods) |
| 5018 | ? mirror::ObjectArray<mirror::ObjectArray<mirror::Class>>::Alloc( |
| 5019 | self, original_proxied_throws->GetClass(), num_virtual_methods) |
| 5020 | : original_proxied_throws.Get())); |
Alex Light | bc11509 | 2020-03-27 11:25:16 -0700 | [diff] [blame] | 5021 | if (proxied_throws.IsNull() && !original_proxied_throws.IsNull()) { |
| 5022 | self->AssertPendingOOMException(); |
| 5023 | return nullptr; |
| 5024 | } |
Alex Light | 133987d | 2020-03-26 19:22:12 +0000 | [diff] [blame] | 5025 | if (has_filtered_methods) { |
| 5026 | for (auto [orig_idx, new_idx] : ZipCount(MakeIterationRange(proxied_throws_idx))) { |
| 5027 | DCHECK_LE(new_idx, orig_idx); |
| 5028 | proxied_throws->Set(new_idx, original_proxied_throws->Get(orig_idx)); |
| 5029 | } |
| 5030 | } |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 5031 | |
| 5032 | // Create the methods array. |
| 5033 | LengthPrefixedArray<ArtMethod>* proxy_class_methods = AllocArtMethodArray( |
| 5034 | self, allocator, num_direct_methods + num_virtual_methods); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5035 | // Currently AllocArtMethodArray cannot return null, but the OOM logic is left there in case we |
| 5036 | // want to throw OOM in the future. |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 5037 | if (UNLIKELY(proxy_class_methods == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5038 | self->AssertPendingOOMException(); |
| 5039 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 5040 | } |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 5041 | temp_klass->SetMethodsPtr(proxy_class_methods, num_direct_methods, num_virtual_methods); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 5042 | |
| 5043 | // Create the single direct method. |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 5044 | CreateProxyConstructor(temp_klass, temp_klass->GetDirectMethodUnchecked(0, image_pointer_size_)); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 5045 | |
| 5046 | // Create virtual method using specified prototypes. |
| 5047 | // TODO These should really use the iterators. |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 5048 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 5049 | auto* virtual_method = temp_klass->GetVirtualMethodUnchecked(i, image_pointer_size_); |
Alex Light | 133987d | 2020-03-26 19:22:12 +0000 | [diff] [blame] | 5050 | auto* prototype = proxied_methods[i]; |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 5051 | CreateProxyMethod(temp_klass, prototype, virtual_method); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5052 | DCHECK(virtual_method->GetDeclaringClass() != nullptr); |
| 5053 | DCHECK(prototype->GetDeclaringClass() != nullptr); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 5054 | } |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 5055 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 5056 | // The super class is java.lang.reflect.Proxy |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 5057 | temp_klass->SetSuperClass(GetClassRoot<mirror::Proxy>(this)); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 5058 | // Now effectively in the loaded state. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5059 | mirror::Class::SetStatus(temp_klass, ClassStatus::kLoaded, self); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 5060 | self->AssertNoPendingException(); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 5061 | |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 5062 | // At this point the class is loaded. Publish a ClassLoad event. |
| 5063 | // Note: this may be a temporary class. It is a listener's responsibility to handle this. |
| 5064 | Runtime::Current()->GetRuntimeCallbacks()->ClassLoad(temp_klass); |
| 5065 | |
| 5066 | MutableHandle<mirror::Class> klass = hs.NewHandle<mirror::Class>(nullptr); |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 5067 | { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5068 | // Must hold lock on object when resolved. |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 5069 | ObjectLock<mirror::Class> resolution_lock(self, temp_klass); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 5070 | // Link the fields and virtual methods, creating vtable and iftables. |
| 5071 | // The new class will replace the old one in the class table. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5072 | Handle<mirror::ObjectArray<mirror::Class>> h_interfaces( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 5073 | hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::Class>>(interfaces))); |
Vladimir Marko | 3892e62 | 2019-03-15 15:22:18 +0000 | [diff] [blame] | 5074 | if (!LinkClass(self, descriptor, temp_klass, h_interfaces, &klass)) { |
Vladimir Marko | a4d28dd | 2021-06-30 11:28:06 +0100 | [diff] [blame] | 5075 | if (!temp_klass->IsErroneous()) { |
| 5076 | mirror::Class::SetStatus(temp_klass, ClassStatus::kErrorUnresolved, self); |
| 5077 | } |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 5078 | return nullptr; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 5079 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5080 | } |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 5081 | CHECK(temp_klass->IsRetired()); |
| 5082 | CHECK_NE(temp_klass.Get(), klass.Get()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5083 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 5084 | CHECK_EQ(interfaces_sfield.GetDeclaringClass(), klass.Get()); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 5085 | interfaces_sfield.SetObject<false>( |
| 5086 | klass.Get(), |
Mathieu Chartier | f8ac97f | 2016-10-05 15:56:52 -0700 | [diff] [blame] | 5087 | soa.Decode<mirror::ObjectArray<mirror::Class>>(interfaces)); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 5088 | CHECK_EQ(throws_sfield.GetDeclaringClass(), klass.Get()); |
| 5089 | throws_sfield.SetObject<false>( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 5090 | klass.Get(), |
Alex Light | 133987d | 2020-03-26 19:22:12 +0000 | [diff] [blame] | 5091 | proxied_throws.Get()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5092 | |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 5093 | Runtime::Current()->GetRuntimeCallbacks()->ClassPrepare(temp_klass, klass); |
| 5094 | |
Vladimir Marko | 305c38b | 2018-02-14 11:50:07 +0000 | [diff] [blame] | 5095 | // SubtypeCheckInfo::Initialized must happen-before any new-instance for that type. |
| 5096 | // See also ClassLinker::EnsureInitialized(). |
| 5097 | if (kBitstringSubtypeCheckEnabled) { |
| 5098 | MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_); |
| 5099 | SubtypeCheck<ObjPtr<mirror::Class>>::EnsureInitialized(klass.Get()); |
| 5100 | // TODO: Avoid taking subtype_check_lock_ if SubtypeCheck for j.l.r.Proxy is already assigned. |
| 5101 | } |
| 5102 | |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 5103 | VisiblyInitializedCallback* callback = nullptr; |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5104 | { |
| 5105 | // Lock on klass is released. Lock new class object. |
| 5106 | ObjectLock<mirror::Class> initialization_lock(self, klass); |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 5107 | // Conservatively go through the ClassStatus::kInitialized state. |
| 5108 | callback = MarkClassInitialized(self, klass); |
| 5109 | } |
| 5110 | if (callback != nullptr) { |
| 5111 | callback->MakeVisible(self); |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 5112 | } |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 5113 | |
David Srbecky | 346fd96 | 2020-07-27 16:51:00 +0100 | [diff] [blame] | 5114 | // Consistency checks. |
Elliott Hughes | 67d9200 | 2012-03-26 15:08:51 -0700 | [diff] [blame] | 5115 | if (kIsDebugBuild) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 5116 | CHECK(klass->GetIFieldsPtr() == nullptr); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5117 | CheckProxyConstructor(klass->GetDirectMethod(0, image_pointer_size_)); |
| 5118 | |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 5119 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5120 | auto* virtual_method = klass->GetVirtualMethodUnchecked(i, image_pointer_size_); |
Alex Light | 133987d | 2020-03-26 19:22:12 +0000 | [diff] [blame] | 5121 | CheckProxyMethod(virtual_method, proxied_methods[i]); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 5122 | } |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 5123 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 5124 | StackHandleScope<1> hs2(self); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 5125 | Handle<mirror::String> decoded_name = hs2.NewHandle(soa.Decode<mirror::String>(name)); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 5126 | std::string interfaces_field_name(StringPrintf("java.lang.Class[] %s.interfaces", |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 5127 | decoded_name->ToModifiedUtf8().c_str())); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5128 | CHECK_EQ(ArtField::PrettyField(klass->GetStaticField(0)), interfaces_field_name); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 5129 | |
| 5130 | std::string throws_field_name(StringPrintf("java.lang.Class[][] %s.throws", |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 5131 | decoded_name->ToModifiedUtf8().c_str())); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5132 | CHECK_EQ(ArtField::PrettyField(klass->GetStaticField(1)), throws_field_name); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 5133 | |
Narayan Kamath | 6b2dc31 | 2017-03-14 13:26:12 +0000 | [diff] [blame] | 5134 | CHECK_EQ(klass.Get()->GetProxyInterfaces(), |
Mathieu Chartier | f8ac97f | 2016-10-05 15:56:52 -0700 | [diff] [blame] | 5135 | soa.Decode<mirror::ObjectArray<mirror::Class>>(interfaces)); |
Narayan Kamath | 6b2dc31 | 2017-03-14 13:26:12 +0000 | [diff] [blame] | 5136 | CHECK_EQ(klass.Get()->GetProxyThrows(), |
Alex Light | 133987d | 2020-03-26 19:22:12 +0000 | [diff] [blame] | 5137 | proxied_throws.Get()); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 5138 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5139 | return klass.Get(); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 5140 | } |
| 5141 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5142 | void ClassLinker::CreateProxyConstructor(Handle<mirror::Class> klass, ArtMethod* out) { |
| 5143 | // Create constructor for Proxy that must initialize the method. |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 5144 | ObjPtr<mirror::Class> proxy_class = GetClassRoot<mirror::Proxy>(this); |
| 5145 | CHECK_EQ(proxy_class->NumDirectMethods(), 21u); |
Przemyslaw Szczepaniak | f11cd29 | 2016-08-17 17:46:38 +0100 | [diff] [blame] | 5146 | |
Igor Murashkin | 9d3d752 | 2017-02-27 10:39:49 -0800 | [diff] [blame] | 5147 | // Find the <init>(InvocationHandler)V method. The exact method offset varies depending |
| 5148 | // on which front-end compiler was used to build the libcore DEX files. |
Vladimir Marko | addc2d1 | 2022-11-29 08:47:02 +0000 | [diff] [blame] | 5149 | ArtMethod* proxy_constructor = WellKnownClasses::java_lang_reflect_Proxy_init; |
Igor Murashkin | 9d3d752 | 2017-02-27 10:39:49 -0800 | [diff] [blame] | 5150 | DCHECK(proxy_constructor != nullptr) |
| 5151 | << "Could not find <init> method in java.lang.reflect.Proxy"; |
| 5152 | |
Jeff Hao | db8a664 | 2014-08-14 17:18:52 -0700 | [diff] [blame] | 5153 | // Clone the existing constructor of Proxy (our constructor would just invoke it so steal its |
| 5154 | // code_ too) |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5155 | DCHECK(out != nullptr); |
| 5156 | out->CopyFrom(proxy_constructor, image_pointer_size_); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 5157 | // 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] | 5158 | // 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] | 5159 | // 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] | 5160 | out->SetAccessFlags((out->GetAccessFlags() & ~kAccProtected) | |
| 5161 | kAccPublic | |
| 5162 | kAccCompileDontBother); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5163 | out->SetDeclaringClass(klass.Get()); |
Vladimir Marko | d1ee20f | 2017-08-17 09:21:16 +0000 | [diff] [blame] | 5164 | |
| 5165 | // Set the original constructor method. |
| 5166 | out->SetDataPtrSize(proxy_constructor, image_pointer_size_); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 5167 | } |
| 5168 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5169 | void ClassLinker::CheckProxyConstructor(ArtMethod* constructor) const { |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 5170 | CHECK(constructor->IsConstructor()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5171 | auto* np = constructor->GetInterfaceMethodIfProxy(image_pointer_size_); |
| 5172 | CHECK_STREQ(np->GetName(), "<init>"); |
| 5173 | CHECK_STREQ(np->GetSignature().ToString().c_str(), "(Ljava/lang/reflect/InvocationHandler;)V"); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 5174 | DCHECK(constructor->IsPublic()); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 5175 | } |
| 5176 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5177 | void ClassLinker::CreateProxyMethod(Handle<mirror::Class> klass, ArtMethod* prototype, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5178 | ArtMethod* out) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 5179 | // 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] | 5180 | // as necessary |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5181 | DCHECK(out != nullptr); |
| 5182 | out->CopyFrom(prototype, image_pointer_size_); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 5183 | |
Alex Light | e9dd04f | 2016-03-16 16:09:45 -0700 | [diff] [blame] | 5184 | // Set class to be the concrete proxy class. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5185 | out->SetDeclaringClass(klass.Get()); |
Vladimir Marko | de0d0de | 2021-03-18 14:12:35 +0000 | [diff] [blame] | 5186 | // Clear the abstract and default flags to ensure that defaults aren't picked in |
Alex Light | e9dd04f | 2016-03-16 16:09:45 -0700 | [diff] [blame] | 5187 | // preference to the invocation handler. |
Vladimir Marko | de0d0de | 2021-03-18 14:12:35 +0000 | [diff] [blame] | 5188 | const uint32_t kRemoveFlags = kAccAbstract | kAccDefault; |
Alex Light | e9dd04f | 2016-03-16 16:09:45 -0700 | [diff] [blame] | 5189 | // Make the method final. |
Mathieu Chartier | 201e297 | 2017-06-05 18:34:53 -0700 | [diff] [blame] | 5190 | // Mark kAccCompileDontBother so that we don't take JIT samples for the method. b/62349349 |
| 5191 | const uint32_t kAddFlags = kAccFinal | kAccCompileDontBother; |
Alex Light | e9dd04f | 2016-03-16 16:09:45 -0700 | [diff] [blame] | 5192 | out->SetAccessFlags((out->GetAccessFlags() & ~kRemoveFlags) | kAddFlags); |
| 5193 | |
Vladimir Marko | d1ee20f | 2017-08-17 09:21:16 +0000 | [diff] [blame] | 5194 | // Set the original interface method. |
| 5195 | out->SetDataPtrSize(prototype, image_pointer_size_); |
| 5196 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 5197 | // At runtime the method looks like a reference and argument saving method, clone the code |
| 5198 | // related parameters from this method. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5199 | out->SetEntryPointFromQuickCompiledCode(GetQuickProxyInvokeHandler()); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 5200 | } |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 5201 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5202 | void ClassLinker::CheckProxyMethod(ArtMethod* method, ArtMethod* prototype) const { |
David Srbecky | 346fd96 | 2020-07-27 16:51:00 +0100 | [diff] [blame] | 5203 | // Basic consistency checks. |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 5204 | CHECK(!prototype->IsFinal()); |
| 5205 | CHECK(method->IsFinal()); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5206 | CHECK(method->IsInvokable()); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 5207 | |
| 5208 | // The proxy method doesn't have its own dex cache or dex file and so it steals those of its |
| 5209 | // 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] | 5210 | CHECK_EQ(prototype->GetDexMethodIndex(), method->GetDexMethodIndex()); |
Vladimir Marko | 5c3e9d1 | 2017-08-30 16:43:54 +0100 | [diff] [blame] | 5211 | CHECK_EQ(prototype, method->GetInterfaceMethodIfProxy(image_pointer_size_)); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 5212 | } |
| 5213 | |
Nicolas Geoffray | 4a10dcc | 2022-08-12 19:18:43 +0100 | [diff] [blame] | 5214 | bool ClassLinker::CanWeInitializeClass(ObjPtr<mirror::Class> klass, |
| 5215 | bool can_init_statics, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5216 | bool can_init_parents) { |
Brian Carlstrom | 610e49f | 2013-11-04 17:07:22 -0800 | [diff] [blame] | 5217 | if (can_init_statics && can_init_parents) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5218 | return true; |
| 5219 | } |
Nicolas Geoffray | 4a10dcc | 2022-08-12 19:18:43 +0100 | [diff] [blame] | 5220 | DCHECK(Runtime::Current()->IsAotCompiler()); |
| 5221 | |
| 5222 | // We currently don't support initializing at AOT time classes that need access |
| 5223 | // checks. |
| 5224 | if (klass->IsVerifiedNeedsAccessChecks()) { |
| 5225 | return false; |
| 5226 | } |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5227 | if (!can_init_statics) { |
| 5228 | // Check if there's a class initializer. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5229 | ArtMethod* clinit = klass->FindClassInitializer(image_pointer_size_); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5230 | if (clinit != nullptr) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5231 | return false; |
| 5232 | } |
| 5233 | // Check if there are encoded static values needing initialization. |
| 5234 | if (klass->NumStaticFields() != 0) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 5235 | const dex::ClassDef* dex_class_def = klass->GetClassDef(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5236 | DCHECK(dex_class_def != nullptr); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5237 | if (dex_class_def->static_values_off_ != 0) { |
| 5238 | return false; |
| 5239 | } |
| 5240 | } |
Vladimir Marko | 889b72d | 2019-11-12 11:01:13 +0000 | [diff] [blame] | 5241 | } |
| 5242 | // If we are a class we need to initialize all interfaces with default methods when we are |
| 5243 | // initialized. Check all of them. |
| 5244 | if (!klass->IsInterface()) { |
| 5245 | size_t num_interfaces = klass->GetIfTableCount(); |
| 5246 | for (size_t i = 0; i < num_interfaces; i++) { |
| 5247 | ObjPtr<mirror::Class> iface = klass->GetIfTable()->GetInterface(i); |
| 5248 | if (iface->HasDefaultMethods() && !iface->IsInitialized()) { |
| 5249 | if (!can_init_parents || !CanWeInitializeClass(iface, can_init_statics, can_init_parents)) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5250 | return false; |
| 5251 | } |
| 5252 | } |
| 5253 | } |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5254 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5255 | if (klass->IsInterface() || !klass->HasSuperClass()) { |
| 5256 | return true; |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5257 | } |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5258 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Vladimir Marko | 889b72d | 2019-11-12 11:01:13 +0000 | [diff] [blame] | 5259 | if (super_class->IsInitialized()) { |
| 5260 | return true; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5261 | } |
Vladimir Marko | 889b72d | 2019-11-12 11:01:13 +0000 | [diff] [blame] | 5262 | 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] | 5263 | } |
| 5264 | |
Mathieu Chartier | 2336954 | 2020-03-04 08:24:11 -0800 | [diff] [blame] | 5265 | bool ClassLinker::InitializeClass(Thread* self, |
| 5266 | Handle<mirror::Class> klass, |
| 5267 | bool can_init_statics, |
| 5268 | bool can_init_parents) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5269 | // see JLS 3rd edition, 12.4.2 "Detailed Initialization Procedure" for the locking protocol |
| 5270 | |
| 5271 | // Are we already initialized and therefore done? |
| 5272 | // Note: we differ from the JLS here as we don't do this under the lock, this is benign as |
| 5273 | // an initialized class will never change its state. |
| 5274 | if (klass->IsInitialized()) { |
| 5275 | return true; |
| 5276 | } |
| 5277 | |
| 5278 | // 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] | 5279 | if (!CanWeInitializeClass(klass.Get(), can_init_statics, can_init_parents)) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5280 | return false; |
| 5281 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5282 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5283 | self->AllowThreadSuspension(); |
Mathieu Chartier | 2336954 | 2020-03-04 08:24:11 -0800 | [diff] [blame] | 5284 | Runtime* const runtime = Runtime::Current(); |
| 5285 | const bool stats_enabled = runtime->HasStatsEnabled(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5286 | uint64_t t0; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5287 | { |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 5288 | ObjectLock<mirror::Class> lock(self, klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5289 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5290 | // Re-check under the lock in case another thread initialized ahead of us. |
| 5291 | if (klass->IsInitialized()) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5292 | return true; |
| 5293 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5294 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5295 | // 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] | 5296 | if (klass->IsErroneous()) { |
Andreas Gampe | 7b3063b | 2019-01-07 14:12:52 -0800 | [diff] [blame] | 5297 | ThrowEarlierClassFailure(klass.Get(), true, /* log= */ true); |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 5298 | VlogClassInitializationFailure(klass); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5299 | return false; |
| 5300 | } |
| 5301 | |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 5302 | CHECK(klass->IsResolved() && !klass->IsErroneousResolved()) |
| 5303 | << klass->PrettyClass() << ": state=" << klass->GetStatus(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5304 | |
| 5305 | if (!klass->IsVerified()) { |
Nicolas Geoffray | 5b0b2e1 | 2021-03-19 14:48:40 +0000 | [diff] [blame] | 5306 | VerifyClass(self, /*verifier_deps= */ nullptr, klass); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5307 | if (!klass->IsVerified()) { |
| 5308 | // We failed to verify, expect either the klass to be erroneous or verification failed at |
| 5309 | // compile time. |
| 5310 | if (klass->IsErroneous()) { |
Andreas Gampe | fc49fa0 | 2016-04-21 12:21:55 -0700 | [diff] [blame] | 5311 | // The class is erroneous. This may be a verifier error, or another thread attempted |
| 5312 | // verification and/or initialization and failed. We can distinguish those cases by |
| 5313 | // whether an exception is already pending. |
| 5314 | if (self->IsExceptionPending()) { |
| 5315 | // Check that it's a VerifyError. |
Nicolas Geoffray | 4dc6589 | 2021-07-05 17:43:35 +0100 | [diff] [blame] | 5316 | DCHECK(IsVerifyError(self->GetException())); |
Andreas Gampe | fc49fa0 | 2016-04-21 12:21:55 -0700 | [diff] [blame] | 5317 | } else { |
| 5318 | // Check that another thread attempted initialization. |
| 5319 | DCHECK_NE(0, klass->GetClinitThreadId()); |
| 5320 | DCHECK_NE(self->GetTid(), klass->GetClinitThreadId()); |
| 5321 | // Need to rethrow the previous failure now. |
| 5322 | ThrowEarlierClassFailure(klass.Get(), true); |
| 5323 | } |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 5324 | VlogClassInitializationFailure(klass); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5325 | } else { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 5326 | CHECK(Runtime::Current()->IsAotCompiler()); |
Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 5327 | CHECK(klass->ShouldVerifyAtRuntime() || klass->IsVerifiedNeedsAccessChecks()); |
Vladimir Marko | d79b37b | 2018-11-02 13:06:22 +0000 | [diff] [blame] | 5328 | self->AssertNoPendingException(); |
| 5329 | self->SetException(Runtime::Current()->GetPreAllocatedNoClassDefFoundError()); |
jeffhao | a9b3bf4 | 2012-06-06 17:18:39 -0700 | [diff] [blame] | 5330 | } |
Vladimir Marko | d79b37b | 2018-11-02 13:06:22 +0000 | [diff] [blame] | 5331 | self->AssertPendingException(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5332 | return false; |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 5333 | } else { |
| 5334 | self->AssertNoPendingException(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5335 | } |
Andreas Gampe | fc49fa0 | 2016-04-21 12:21:55 -0700 | [diff] [blame] | 5336 | |
| 5337 | // A separate thread could have moved us all the way to initialized. A "simple" example |
| 5338 | // involves a subclass of the current class being initialized at the same time (which |
| 5339 | // will implicitly initialize the superclass, if scheduled that way). b/28254258 |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 5340 | DCHECK(!klass->IsErroneous()) << klass->GetStatus(); |
Andreas Gampe | fc49fa0 | 2016-04-21 12:21:55 -0700 | [diff] [blame] | 5341 | if (klass->IsInitialized()) { |
| 5342 | return true; |
| 5343 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5344 | } |
| 5345 | |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5346 | // If the class is ClassStatus::kInitializing, either this thread is |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5347 | // initializing higher up the stack or another thread has beat us |
| 5348 | // to initializing and we need to wait. Either way, this |
| 5349 | // invocation of InitializeClass will not be responsible for |
| 5350 | // running <clinit> and will return. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5351 | if (klass->GetStatus() == ClassStatus::kInitializing) { |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 5352 | // Could have got an exception during verification. |
| 5353 | if (self->IsExceptionPending()) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 5354 | VlogClassInitializationFailure(klass); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 5355 | return false; |
| 5356 | } |
Elliott Hughes | 005ab2e | 2011-09-11 17:15:31 -0700 | [diff] [blame] | 5357 | // We caught somebody else in the act; was it us? |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 5358 | if (klass->GetClinitThreadId() == self->GetTid()) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5359 | // Yes. That's fine. Return so we can continue initializing. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5360 | return true; |
| 5361 | } |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5362 | // No. That's fine. Wait for another thread to finish initializing. |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5363 | return WaitForInitializeClass(klass, self, lock); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5364 | } |
| 5365 | |
Jeff Hao | e2e4034 | 2017-07-19 10:45:18 -0700 | [diff] [blame] | 5366 | // Try to get the oat class's status for this class if the oat file is present. The compiler |
| 5367 | // tries to validate superclass descriptors, and writes the result into the oat file. |
| 5368 | // Runtime correctness is guaranteed by classpath checks done on loading. If the classpath |
| 5369 | // is different at runtime than it was at compile time, the oat file is rejected. So if the |
| 5370 | // 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] | 5371 | bool has_oat_class = false; |
Jeff Hao | e2e4034 | 2017-07-19 10:45:18 -0700 | [diff] [blame] | 5372 | const OatFile::OatClass oat_class = (runtime->IsStarted() && !runtime->IsAotCompiler()) |
| 5373 | ? OatFile::FindOatClass(klass->GetDexFile(), klass->GetDexClassDefIndex(), &has_oat_class) |
| 5374 | : OatFile::OatClass::Invalid(); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5375 | if (oat_class.GetStatus() < ClassStatus::kSuperclassValidated && |
Jeff Hao | 0cb1728 | 2017-07-12 14:51:49 -0700 | [diff] [blame] | 5376 | !ValidateSuperClassDescriptors(klass)) { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5377 | mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5378 | return false; |
| 5379 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5380 | self->AllowThreadSuspension(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5381 | |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5382 | CHECK_EQ(klass->GetStatus(), ClassStatus::kVerified) << klass->PrettyClass() |
Andreas Gampe | 9510ccd | 2016-04-20 09:55:25 -0700 | [diff] [blame] | 5383 | << " self.tid=" << self->GetTid() << " clinit.tid=" << klass->GetClinitThreadId(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5384 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5385 | // From here out other threads may observe that we're initializing and so changes of state |
| 5386 | // require the a notification. |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 5387 | klass->SetClinitThreadId(self->GetTid()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5388 | mirror::Class::SetStatus(klass, ClassStatus::kInitializing, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5389 | |
Mathieu Chartier | 2336954 | 2020-03-04 08:24:11 -0800 | [diff] [blame] | 5390 | t0 = stats_enabled ? NanoTime() : 0u; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5391 | } |
| 5392 | |
Andreas Gampe | af86470 | 2019-07-23 14:05:35 -0700 | [diff] [blame] | 5393 | uint64_t t_sub = 0; |
| 5394 | |
Brian Carlstrom | 6d3f72c | 2013-08-21 18:06:34 -0700 | [diff] [blame] | 5395 | // Initialize super classes, must be done while initializing for the JLS. |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5396 | if (!klass->IsInterface() && klass->HasSuperClass()) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5397 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5398 | if (!super_class->IsInitialized()) { |
| 5399 | CHECK(!super_class->IsInterface()); |
| 5400 | CHECK(can_init_parents); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5401 | StackHandleScope<1> hs(self); |
| 5402 | Handle<mirror::Class> handle_scope_super(hs.NewHandle(super_class)); |
Mathieu Chartier | 2336954 | 2020-03-04 08:24:11 -0800 | [diff] [blame] | 5403 | uint64_t super_t0 = stats_enabled ? NanoTime() : 0u; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5404 | bool super_initialized = InitializeClass(self, handle_scope_super, can_init_statics, true); |
Mathieu Chartier | 2336954 | 2020-03-04 08:24:11 -0800 | [diff] [blame] | 5405 | uint64_t super_t1 = stats_enabled ? NanoTime() : 0u; |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5406 | if (!super_initialized) { |
| 5407 | // The super class was verified ahead of entering initializing, we should only be here if |
| 5408 | // the super class became erroneous due to initialization. |
Chang Xing | adbb91c | 2017-07-17 11:23:55 -0700 | [diff] [blame] | 5409 | // For the case of aot compiler, the super class might also be initializing but we don't |
| 5410 | // want to process circular dependencies in pre-compile. |
| 5411 | CHECK(self->IsExceptionPending()) |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 5412 | << "Super class initialization failed for " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5413 | << handle_scope_super->PrettyDescriptor() |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5414 | << " that has unexpected status " << handle_scope_super->GetStatus() |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5415 | << "\nPending exception:\n" |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 5416 | << (self->GetException() != nullptr ? self->GetException()->Dump() : ""); |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 5417 | ObjectLock<mirror::Class> lock(self, klass); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5418 | // Initialization failed because the super-class is erroneous. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5419 | mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5420 | return false; |
| 5421 | } |
Andreas Gampe | af86470 | 2019-07-23 14:05:35 -0700 | [diff] [blame] | 5422 | t_sub = super_t1 - super_t0; |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 5423 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5424 | } |
| 5425 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5426 | if (!klass->IsInterface()) { |
| 5427 | // Initialize interfaces with default methods for the JLS. |
| 5428 | size_t num_direct_interfaces = klass->NumDirectInterfaces(); |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 5429 | // Only setup the (expensive) handle scope if we actually need to. |
| 5430 | if (UNLIKELY(num_direct_interfaces > 0)) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5431 | StackHandleScope<1> hs_iface(self); |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 5432 | MutableHandle<mirror::Class> handle_scope_iface(hs_iface.NewHandle<mirror::Class>(nullptr)); |
| 5433 | for (size_t i = 0; i < num_direct_interfaces; i++) { |
Vladimir Marko | b10668c | 2021-06-10 09:52:53 +0100 | [diff] [blame] | 5434 | handle_scope_iface.Assign(klass->GetDirectInterface(i)); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 5435 | CHECK(handle_scope_iface != nullptr) << klass->PrettyDescriptor() << " iface #" << i; |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 5436 | CHECK(handle_scope_iface->IsInterface()); |
| 5437 | if (handle_scope_iface->HasBeenRecursivelyInitialized()) { |
| 5438 | // We have already done this for this interface. Skip it. |
| 5439 | continue; |
| 5440 | } |
| 5441 | // We cannot just call initialize class directly because we need to ensure that ALL |
| 5442 | // interfaces with default methods are initialized. Non-default interface initialization |
| 5443 | // will not affect other non-default super-interfaces. |
Mathieu Chartier | 2336954 | 2020-03-04 08:24:11 -0800 | [diff] [blame] | 5444 | // This is not very precise, misses all walking. |
| 5445 | uint64_t inf_t0 = stats_enabled ? NanoTime() : 0u; |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 5446 | bool iface_initialized = InitializeDefaultInterfaceRecursive(self, |
| 5447 | handle_scope_iface, |
| 5448 | can_init_statics, |
| 5449 | can_init_parents); |
Mathieu Chartier | 2336954 | 2020-03-04 08:24:11 -0800 | [diff] [blame] | 5450 | uint64_t inf_t1 = stats_enabled ? NanoTime() : 0u; |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 5451 | if (!iface_initialized) { |
| 5452 | ObjectLock<mirror::Class> lock(self, klass); |
| 5453 | // Initialization failed because one of our interfaces with default methods is erroneous. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5454 | mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self); |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 5455 | return false; |
| 5456 | } |
Andreas Gampe | af86470 | 2019-07-23 14:05:35 -0700 | [diff] [blame] | 5457 | t_sub += inf_t1 - inf_t0; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5458 | } |
| 5459 | } |
| 5460 | } |
| 5461 | |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 5462 | const size_t num_static_fields = klass->NumStaticFields(); |
| 5463 | if (num_static_fields > 0) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 5464 | const dex::ClassDef* dex_class_def = klass->GetClassDef(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5465 | CHECK(dex_class_def != nullptr); |
Hiroshi Yamauchi | 67ef46a | 2014-08-21 15:59:43 -0700 | [diff] [blame] | 5466 | StackHandleScope<3> hs(self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5467 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader())); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 5468 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache())); |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 5469 | |
| 5470 | // Eagerly fill in static fields so that the we don't have to do as many expensive |
| 5471 | // Class::FindStaticField in ResolveField. |
| 5472 | for (size_t i = 0; i < num_static_fields; ++i) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 5473 | ArtField* field = klass->GetStaticField(i); |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 5474 | const uint32_t field_idx = field->GetDexFieldIndex(); |
David Srbecky | 5de5efe | 2021-02-15 21:23:00 +0000 | [diff] [blame] | 5475 | ArtField* resolved_field = dex_cache->GetResolvedField(field_idx); |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 5476 | if (resolved_field == nullptr) { |
David Brazdil | 1ab0fa8 | 2018-05-04 11:28:03 +0100 | [diff] [blame] | 5477 | // 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] | 5478 | DCHECK(!hiddenapi::ShouldDenyAccessToMember( |
| 5479 | field, |
| 5480 | hiddenapi::AccessContext(class_loader.Get(), dex_cache.Get()), |
| 5481 | hiddenapi::AccessMethod::kNone)); |
David Srbecky | 5de5efe | 2021-02-15 21:23:00 +0000 | [diff] [blame] | 5482 | dex_cache->SetResolvedField(field_idx, field); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5483 | } else { |
| 5484 | DCHECK_EQ(field, resolved_field); |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 5485 | } |
| 5486 | } |
| 5487 | |
Vladimir Marko | e11dd50 | 2017-12-08 14:09:45 +0000 | [diff] [blame] | 5488 | annotations::RuntimeEncodedStaticFieldValueIterator value_it(dex_cache, |
| 5489 | class_loader, |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 5490 | this, |
| 5491 | *dex_class_def); |
Vladimir Marko | e11dd50 | 2017-12-08 14:09:45 +0000 | [diff] [blame] | 5492 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 5493 | |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 5494 | if (value_it.HasNext()) { |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 5495 | ClassAccessor accessor(dex_file, *dex_class_def); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5496 | CHECK(can_init_statics); |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 5497 | for (const ClassAccessor::Field& field : accessor.GetStaticFields()) { |
| 5498 | if (!value_it.HasNext()) { |
| 5499 | break; |
| 5500 | } |
| 5501 | ArtField* art_field = ResolveField(field.GetIndex(), |
| 5502 | dex_cache, |
| 5503 | class_loader, |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 5504 | /* is_static= */ true); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 5505 | if (Runtime::Current()->IsActiveTransaction()) { |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 5506 | value_it.ReadValueToField<true>(art_field); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 5507 | } else { |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 5508 | value_it.ReadValueToField<false>(art_field); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 5509 | } |
Mathieu Chartier | da595be | 2016-08-10 13:57:39 -0700 | [diff] [blame] | 5510 | if (self->IsExceptionPending()) { |
| 5511 | break; |
| 5512 | } |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 5513 | value_it.Next(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5514 | } |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 5515 | DCHECK(self->IsExceptionPending() || !value_it.HasNext()); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5516 | } |
| 5517 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5518 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5519 | |
Mathieu Chartier | da595be | 2016-08-10 13:57:39 -0700 | [diff] [blame] | 5520 | if (!self->IsExceptionPending()) { |
| 5521 | ArtMethod* clinit = klass->FindClassInitializer(image_pointer_size_); |
| 5522 | if (clinit != nullptr) { |
| 5523 | CHECK(can_init_statics); |
| 5524 | JValue result; |
| 5525 | clinit->Invoke(self, nullptr, 0, &result, "V"); |
| 5526 | } |
| 5527 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5528 | self->AllowThreadSuspension(); |
Mathieu Chartier | 2336954 | 2020-03-04 08:24:11 -0800 | [diff] [blame] | 5529 | uint64_t t1 = stats_enabled ? NanoTime() : 0u; |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 5530 | |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 5531 | VisiblyInitializedCallback* callback = nullptr; |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 5532 | bool success = true; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5533 | { |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 5534 | ObjectLock<mirror::Class> lock(self, klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5535 | |
| 5536 | if (self->IsExceptionPending()) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 5537 | WrapExceptionInInitializer(klass); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5538 | mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self); |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 5539 | success = false; |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 5540 | } else if (Runtime::Current()->IsTransactionAborted()) { |
| 5541 | // The exception thrown when the transaction aborted has been caught and cleared |
| 5542 | // so we need to throw it again now. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5543 | VLOG(compiler) << "Return from class initializer of " |
| 5544 | << mirror::Class::PrettyDescriptor(klass.Get()) |
Sebastien Hertz | bd9cf9f | 2015-03-03 12:16:13 +0100 | [diff] [blame] | 5545 | << " without exception while transaction was aborted: re-throw it now."; |
Mathieu Chartier | 2336954 | 2020-03-04 08:24:11 -0800 | [diff] [blame] | 5546 | runtime->ThrowTransactionAbortError(self); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5547 | mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self); |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 5548 | success = false; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5549 | } else { |
Mathieu Chartier | 2336954 | 2020-03-04 08:24:11 -0800 | [diff] [blame] | 5550 | if (stats_enabled) { |
| 5551 | RuntimeStats* global_stats = runtime->GetStats(); |
| 5552 | RuntimeStats* thread_stats = self->GetStats(); |
| 5553 | ++global_stats->class_init_count; |
| 5554 | ++thread_stats->class_init_count; |
| 5555 | global_stats->class_init_time_ns += (t1 - t0 - t_sub); |
| 5556 | thread_stats->class_init_time_ns += (t1 - t0 - t_sub); |
| 5557 | } |
Ian Rogers | e6bb3b2 | 2013-08-19 21:51:45 -0700 | [diff] [blame] | 5558 | // Set the class as initialized except if failed to initialize static fields. |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 5559 | callback = MarkClassInitialized(self, klass); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5560 | if (VLOG_IS_ON(class_linker)) { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 5561 | std::string temp; |
| 5562 | LOG(INFO) << "Initialized class " << klass->GetDescriptor(&temp) << " from " << |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 5563 | klass->GetLocation(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 5564 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5565 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5566 | } |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 5567 | if (callback != nullptr) { |
| 5568 | callback->MakeVisible(self); |
| 5569 | } |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 5570 | return success; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5571 | } |
| 5572 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5573 | // We recursively run down the tree of interfaces. We need to do this in the order they are declared |
| 5574 | // and perform the initialization only on those interfaces that contain default methods. |
| 5575 | bool ClassLinker::InitializeDefaultInterfaceRecursive(Thread* self, |
| 5576 | Handle<mirror::Class> iface, |
| 5577 | bool can_init_statics, |
| 5578 | bool can_init_parents) { |
| 5579 | CHECK(iface->IsInterface()); |
| 5580 | size_t num_direct_ifaces = iface->NumDirectInterfaces(); |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 5581 | // Only create the (expensive) handle scope if we need it. |
| 5582 | if (UNLIKELY(num_direct_ifaces > 0)) { |
| 5583 | StackHandleScope<1> hs(self); |
| 5584 | MutableHandle<mirror::Class> handle_super_iface(hs.NewHandle<mirror::Class>(nullptr)); |
| 5585 | // First we initialize all of iface's super-interfaces recursively. |
| 5586 | for (size_t i = 0; i < num_direct_ifaces; i++) { |
Vladimir Marko | b10668c | 2021-06-10 09:52:53 +0100 | [diff] [blame] | 5587 | ObjPtr<mirror::Class> super_iface = iface->GetDirectInterface(i); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 5588 | CHECK(super_iface != nullptr) << iface->PrettyDescriptor() << " iface #" << i; |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 5589 | if (!super_iface->HasBeenRecursivelyInitialized()) { |
| 5590 | // Recursive step |
| 5591 | handle_super_iface.Assign(super_iface); |
| 5592 | if (!InitializeDefaultInterfaceRecursive(self, |
| 5593 | handle_super_iface, |
| 5594 | can_init_statics, |
| 5595 | can_init_parents)) { |
| 5596 | return false; |
| 5597 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5598 | } |
| 5599 | } |
| 5600 | } |
| 5601 | |
| 5602 | bool result = true; |
| 5603 | // Then we initialize 'iface' if it has default methods. We do not need to (and in fact must not) |
| 5604 | // initialize if we don't have default methods. |
| 5605 | if (iface->HasDefaultMethods()) { |
| 5606 | result = EnsureInitialized(self, iface, can_init_statics, can_init_parents); |
| 5607 | } |
| 5608 | |
| 5609 | // Mark that this interface has undergone recursive default interface initialization so we know we |
| 5610 | // can skip it on any later class initializations. We do this even if we are not a default |
| 5611 | // interface since we can still avoid the traversal. This is purely a performance optimization. |
| 5612 | if (result) { |
| 5613 | // TODO This should be done in a better way |
Andreas Gampe | 976b298 | 2018-03-02 17:54:22 -0800 | [diff] [blame] | 5614 | // Note: Use a try-lock to avoid blocking when someone else is holding the lock on this |
| 5615 | // interface. It is bad (Java) style, but not impossible. Marking the recursive |
| 5616 | // initialization is a performance optimization (to avoid another idempotent visit |
| 5617 | // for other implementing classes/interfaces), and can be revisited later. |
| 5618 | ObjectTryLock<mirror::Class> lock(self, iface); |
| 5619 | if (lock.Acquired()) { |
| 5620 | iface->SetRecursivelyInitialized(); |
| 5621 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5622 | } |
| 5623 | return result; |
| 5624 | } |
| 5625 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5626 | bool ClassLinker::WaitForInitializeClass(Handle<mirror::Class> klass, |
| 5627 | Thread* self, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5628 | ObjectLock<mirror::Class>& lock) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5629 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5630 | while (true) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 5631 | self->AssertNoPendingException(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5632 | CHECK(!klass->IsInitialized()); |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5633 | lock.WaitIgnoringInterrupts(); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5634 | |
| 5635 | // When we wake up, repeat the test for init-in-progress. If |
| 5636 | // there's an exception pending (only possible if |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 5637 | // we were not using WaitIgnoringInterrupts), bail out. |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5638 | if (self->IsExceptionPending()) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 5639 | WrapExceptionInInitializer(klass); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5640 | mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5641 | return false; |
| 5642 | } |
| 5643 | // Spurious wakeup? Go back to waiting. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5644 | if (klass->GetStatus() == ClassStatus::kInitializing) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5645 | continue; |
| 5646 | } |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5647 | if (klass->GetStatus() == ClassStatus::kVerified && |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 5648 | Runtime::Current()->IsAotCompiler()) { |
Ian Rogers | 3d1548d | 2012-09-24 14:08:03 -0700 | [diff] [blame] | 5649 | // Compile time initialization failed. |
| 5650 | return false; |
| 5651 | } |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5652 | if (klass->IsErroneous()) { |
| 5653 | // The caller wants an exception, but it was thrown in a |
| 5654 | // different thread. Synthesize one here. |
Brian Carlstrom | df14324 | 2011-10-10 18:05:34 -0700 | [diff] [blame] | 5655 | ThrowNoClassDefFoundError("<clinit> failed for class %s; see exception in other thread", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5656 | klass->PrettyDescriptor().c_str()); |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 5657 | VlogClassInitializationFailure(klass); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5658 | return false; |
| 5659 | } |
| 5660 | if (klass->IsInitialized()) { |
| 5661 | return true; |
| 5662 | } |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5663 | LOG(FATAL) << "Unexpected class status. " << klass->PrettyClass() << " is " |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 5664 | << klass->GetStatus(); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5665 | } |
Ian Rogers | 0714083 | 2014-09-30 15:43:59 -0700 | [diff] [blame] | 5666 | UNREACHABLE(); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5667 | } |
| 5668 | |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5669 | static void ThrowSignatureCheckResolveReturnTypeException(Handle<mirror::Class> klass, |
| 5670 | Handle<mirror::Class> super_klass, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5671 | ArtMethod* method, |
| 5672 | ArtMethod* m) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5673 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5674 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 5675 | DCHECK(!m->IsProxyMethod()); |
| 5676 | const DexFile* dex_file = m->GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 5677 | const dex::MethodId& method_id = dex_file->GetMethodId(m->GetDexMethodIndex()); |
| 5678 | const dex::ProtoId& proto_id = dex_file->GetMethodPrototype(method_id); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 5679 | dex::TypeIndex return_type_idx = proto_id.return_type_idx_; |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5680 | std::string return_type = dex_file->PrettyType(return_type_idx); |
| 5681 | std::string class_loader = mirror::Object::PrettyTypeOf(m->GetDeclaringClass()->GetClassLoader()); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5682 | ThrowWrappedLinkageError(klass.Get(), |
| 5683 | "While checking class %s method %s signature against %s %s: " |
| 5684 | "Failed to resolve return type %s with %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5685 | mirror::Class::PrettyDescriptor(klass.Get()).c_str(), |
| 5686 | ArtMethod::PrettyMethod(method).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5687 | super_klass->IsInterface() ? "interface" : "superclass", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5688 | mirror::Class::PrettyDescriptor(super_klass.Get()).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5689 | return_type.c_str(), class_loader.c_str()); |
| 5690 | } |
| 5691 | |
| 5692 | static void ThrowSignatureCheckResolveArgException(Handle<mirror::Class> klass, |
| 5693 | Handle<mirror::Class> super_klass, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5694 | ArtMethod* method, |
| 5695 | ArtMethod* m, |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5696 | uint32_t index, |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 5697 | dex::TypeIndex arg_type_idx) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5698 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5699 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 5700 | DCHECK(!m->IsProxyMethod()); |
| 5701 | const DexFile* dex_file = m->GetDexFile(); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5702 | std::string arg_type = dex_file->PrettyType(arg_type_idx); |
| 5703 | std::string class_loader = mirror::Object::PrettyTypeOf(m->GetDeclaringClass()->GetClassLoader()); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5704 | ThrowWrappedLinkageError(klass.Get(), |
| 5705 | "While checking class %s method %s signature against %s %s: " |
| 5706 | "Failed to resolve arg %u type %s with %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5707 | mirror::Class::PrettyDescriptor(klass.Get()).c_str(), |
| 5708 | ArtMethod::PrettyMethod(method).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5709 | super_klass->IsInterface() ? "interface" : "superclass", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5710 | mirror::Class::PrettyDescriptor(super_klass.Get()).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5711 | index, arg_type.c_str(), class_loader.c_str()); |
| 5712 | } |
| 5713 | |
| 5714 | static void ThrowSignatureMismatch(Handle<mirror::Class> klass, |
| 5715 | Handle<mirror::Class> super_klass, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5716 | ArtMethod* method, |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5717 | const std::string& error_msg) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5718 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5719 | ThrowLinkageError(klass.Get(), |
| 5720 | "Class %s method %s resolves differently in %s %s: %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5721 | mirror::Class::PrettyDescriptor(klass.Get()).c_str(), |
| 5722 | ArtMethod::PrettyMethod(method).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5723 | super_klass->IsInterface() ? "interface" : "superclass", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5724 | mirror::Class::PrettyDescriptor(super_klass.Get()).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5725 | error_msg.c_str()); |
| 5726 | } |
| 5727 | |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5728 | static bool HasSameSignatureWithDifferentClassLoaders(Thread* self, |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5729 | Handle<mirror::Class> klass, |
| 5730 | Handle<mirror::Class> super_klass, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5731 | ArtMethod* method1, |
| 5732 | ArtMethod* method2) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5733 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5734 | { |
| 5735 | StackHandleScope<1> hs(self); |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 5736 | Handle<mirror::Class> return_type(hs.NewHandle(method1->ResolveReturnType())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 5737 | if (UNLIKELY(return_type == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5738 | ThrowSignatureCheckResolveReturnTypeException(klass, super_klass, method1, method1); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5739 | return false; |
| 5740 | } |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 5741 | ObjPtr<mirror::Class> other_return_type = method2->ResolveReturnType(); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5742 | if (UNLIKELY(other_return_type == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5743 | ThrowSignatureCheckResolveReturnTypeException(klass, super_klass, method1, method2); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5744 | return false; |
| 5745 | } |
Vladimir Marko | 862f43c | 2015-02-10 18:22:57 +0000 | [diff] [blame] | 5746 | if (UNLIKELY(other_return_type != return_type.Get())) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5747 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 5748 | StringPrintf("Return types mismatch: %s(%p) vs %s(%p)", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5749 | return_type->PrettyClassAndClassLoader().c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5750 | return_type.Get(), |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5751 | other_return_type->PrettyClassAndClassLoader().c_str(), |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5752 | other_return_type.Ptr())); |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5753 | return false; |
| 5754 | } |
| 5755 | } |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 5756 | const dex::TypeList* types1 = method1->GetParameterTypeList(); |
| 5757 | const dex::TypeList* types2 = method2->GetParameterTypeList(); |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5758 | if (types1 == nullptr) { |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5759 | if (types2 != nullptr && types2->Size() != 0) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5760 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 5761 | StringPrintf("Type list mismatch with %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5762 | method2->PrettyMethod(true).c_str())); |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5763 | return false; |
| 5764 | } |
| 5765 | return true; |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5766 | } else if (UNLIKELY(types2 == nullptr)) { |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5767 | if (types1->Size() != 0) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5768 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 5769 | StringPrintf("Type list mismatch with %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5770 | method2->PrettyMethod(true).c_str())); |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5771 | return false; |
| 5772 | } |
| 5773 | return true; |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5774 | } |
| 5775 | uint32_t num_types = types1->Size(); |
| 5776 | if (UNLIKELY(num_types != types2->Size())) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5777 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 5778 | StringPrintf("Type list mismatch with %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5779 | method2->PrettyMethod(true).c_str())); |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5780 | return false; |
| 5781 | } |
| 5782 | for (uint32_t i = 0; i < num_types; ++i) { |
Vladimir Marko | 862f43c | 2015-02-10 18:22:57 +0000 | [diff] [blame] | 5783 | StackHandleScope<1> hs(self); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 5784 | dex::TypeIndex param_type_idx = types1->GetTypeItem(i).type_idx_; |
Vladimir Marko | 862f43c | 2015-02-10 18:22:57 +0000 | [diff] [blame] | 5785 | Handle<mirror::Class> param_type(hs.NewHandle( |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 5786 | method1->ResolveClassFromTypeIndex(param_type_idx))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 5787 | if (UNLIKELY(param_type == nullptr)) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5788 | ThrowSignatureCheckResolveArgException(klass, super_klass, method1, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5789 | method1, i, param_type_idx); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5790 | return false; |
| 5791 | } |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 5792 | dex::TypeIndex other_param_type_idx = types2->GetTypeItem(i).type_idx_; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5793 | ObjPtr<mirror::Class> other_param_type = |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 5794 | method2->ResolveClassFromTypeIndex(other_param_type_idx); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5795 | if (UNLIKELY(other_param_type == nullptr)) { |
| 5796 | ThrowSignatureCheckResolveArgException(klass, super_klass, method1, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5797 | method2, i, other_param_type_idx); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5798 | return false; |
| 5799 | } |
Vladimir Marko | 862f43c | 2015-02-10 18:22:57 +0000 | [diff] [blame] | 5800 | if (UNLIKELY(param_type.Get() != other_param_type)) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5801 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 5802 | StringPrintf("Parameter %u type mismatch: %s(%p) vs %s(%p)", |
| 5803 | i, |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5804 | param_type->PrettyClassAndClassLoader().c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5805 | param_type.Get(), |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5806 | other_param_type->PrettyClassAndClassLoader().c_str(), |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5807 | other_param_type.Ptr())); |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5808 | return false; |
| 5809 | } |
| 5810 | } |
| 5811 | return true; |
| 5812 | } |
| 5813 | |
| 5814 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5815 | bool ClassLinker::ValidateSuperClassDescriptors(Handle<mirror::Class> klass) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5816 | if (klass->IsInterface()) { |
| 5817 | return true; |
| 5818 | } |
Ian Rogers | 151f221 | 2014-05-06 11:27:27 -0700 | [diff] [blame] | 5819 | // Begin with the methods local to the superclass. |
Ian Rogers | ded66a0 | 2014-10-28 18:12:55 -0700 | [diff] [blame] | 5820 | Thread* self = Thread::Current(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5821 | StackHandleScope<1> hs(self); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5822 | MutableHandle<mirror::Class> super_klass(hs.NewHandle<mirror::Class>(nullptr)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5823 | if (klass->HasSuperClass() && |
| 5824 | klass->GetClassLoader() != klass->GetSuperClass()->GetClassLoader()) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5825 | super_klass.Assign(klass->GetSuperClass()); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 5826 | for (int i = klass->GetSuperClass()->GetVTableLength() - 1; i >= 0; --i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5827 | auto* m = klass->GetVTableEntry(i, image_pointer_size_); |
| 5828 | auto* super_m = klass->GetSuperClass()->GetVTableEntry(i, image_pointer_size_); |
| 5829 | if (m != super_m) { |
Vladimir Marko | 942fd31 | 2017-01-16 20:52:19 +0000 | [diff] [blame] | 5830 | if (UNLIKELY(!HasSameSignatureWithDifferentClassLoaders(self, |
| 5831 | klass, |
| 5832 | super_klass, |
| 5833 | m, |
| 5834 | super_m))) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5835 | self->AssertPendingException(); |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5836 | return false; |
| 5837 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5838 | } |
| 5839 | } |
| 5840 | } |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 5841 | for (int32_t i = 0; i < klass->GetIfTableCount(); ++i) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5842 | super_klass.Assign(klass->GetIfTable()->GetInterface(i)); |
| 5843 | if (klass->GetClassLoader() != super_klass->GetClassLoader()) { |
| 5844 | uint32_t num_methods = super_klass->NumVirtualMethods(); |
Ian Rogers | 151f221 | 2014-05-06 11:27:27 -0700 | [diff] [blame] | 5845 | for (uint32_t j = 0; j < num_methods; ++j) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5846 | auto* m = klass->GetIfTable()->GetMethodArray(i)->GetElementPtrSize<ArtMethod*>( |
| 5847 | j, image_pointer_size_); |
| 5848 | auto* super_m = super_klass->GetVirtualMethod(j, image_pointer_size_); |
| 5849 | if (m != super_m) { |
Vladimir Marko | 942fd31 | 2017-01-16 20:52:19 +0000 | [diff] [blame] | 5850 | if (UNLIKELY(!HasSameSignatureWithDifferentClassLoaders(self, |
| 5851 | klass, |
| 5852 | super_klass, |
| 5853 | m, |
| 5854 | super_m))) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5855 | self->AssertPendingException(); |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5856 | return false; |
| 5857 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5858 | } |
| 5859 | } |
| 5860 | } |
| 5861 | } |
| 5862 | return true; |
| 5863 | } |
| 5864 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5865 | bool ClassLinker::EnsureInitialized(Thread* self, |
| 5866 | Handle<mirror::Class> c, |
| 5867 | bool can_init_fields, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5868 | bool can_init_parents) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 5869 | DCHECK(c != nullptr); |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 5870 | |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 5871 | if (c->IsInitialized()) { |
Vladimir Marko | 8e11065 | 2019-07-30 10:14:41 +0100 | [diff] [blame] | 5872 | // If we've seen an initialized but not visibly initialized class |
| 5873 | // many times, request visible initialization. |
| 5874 | if (kRuntimeISA == InstructionSet::kX86 || kRuntimeISA == InstructionSet::kX86_64) { |
| 5875 | // Thanks to the x86 memory model classes skip the initialized status. |
| 5876 | DCHECK(c->IsVisiblyInitialized()); |
| 5877 | } else if (UNLIKELY(!c->IsVisiblyInitialized())) { |
| 5878 | if (self->IncrementMakeVisiblyInitializedCounter()) { |
| 5879 | MakeInitializedClassesVisiblyInitialized(self, /*wait=*/ false); |
| 5880 | } |
| 5881 | } |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 5882 | return true; |
| 5883 | } |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 5884 | // SubtypeCheckInfo::Initialized must happen-before any new-instance for that type. |
| 5885 | // |
| 5886 | // Ensure the bitstring is initialized before any of the class initialization |
| 5887 | // logic occurs. Once a class initializer starts running, objects can |
| 5888 | // escape into the heap and use the subtype checking code. |
| 5889 | // |
| 5890 | // Note: A class whose SubtypeCheckInfo is at least Initialized means it |
| 5891 | // can be used as a source for the IsSubClass check, and that all ancestors |
| 5892 | // of the class are Assigned (can be used as a target for IsSubClass check) |
| 5893 | // or Overflowed (can be used as a source for IsSubClass check). |
Vladimir Marko | 305c38b | 2018-02-14 11:50:07 +0000 | [diff] [blame] | 5894 | if (kBitstringSubtypeCheckEnabled) { |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 5895 | MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_); |
Vladimir Marko | 38b8b25 | 2018-01-02 19:07:06 +0000 | [diff] [blame] | 5896 | SubtypeCheck<ObjPtr<mirror::Class>>::EnsureInitialized(c.Get()); |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 5897 | // TODO: Avoid taking subtype_check_lock_ if SubtypeCheck is already initialized. |
| 5898 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5899 | const bool success = InitializeClass(self, c, can_init_fields, can_init_parents); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 5900 | if (!success) { |
| 5901 | if (can_init_fields && can_init_parents) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5902 | CHECK(self->IsExceptionPending()) << c->PrettyClass(); |
Vladimir Marko | ac57691 | 2021-03-31 11:16:22 +0100 | [diff] [blame] | 5903 | } else { |
| 5904 | // There may or may not be an exception pending. If there is, clear it. |
| 5905 | // We propagate the exception only if we can initialize fields and parents. |
| 5906 | self->ClearException(); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 5907 | } |
| 5908 | } else { |
| 5909 | self->AssertNoPendingException(); |
Ian Rogers | 595799e | 2012-01-11 17:32:51 -0800 | [diff] [blame] | 5910 | } |
| 5911 | return success; |
Elliott Hughes | f4c21c9 | 2011-08-19 17:31:31 -0700 | [diff] [blame] | 5912 | } |
| 5913 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5914 | void ClassLinker::FixupTemporaryDeclaringClass(ObjPtr<mirror::Class> temp_class, |
| 5915 | ObjPtr<mirror::Class> new_class) { |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 5916 | DCHECK_EQ(temp_class->NumInstanceFields(), 0u); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 5917 | for (ArtField& field : new_class->GetIFields()) { |
| 5918 | if (field.GetDeclaringClass() == temp_class) { |
| 5919 | field.SetDeclaringClass(new_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5920 | } |
| 5921 | } |
| 5922 | |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 5923 | DCHECK_EQ(temp_class->NumStaticFields(), 0u); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 5924 | for (ArtField& field : new_class->GetSFields()) { |
| 5925 | if (field.GetDeclaringClass() == temp_class) { |
| 5926 | field.SetDeclaringClass(new_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5927 | } |
| 5928 | } |
| 5929 | |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 5930 | DCHECK_EQ(temp_class->NumDirectMethods(), 0u); |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 5931 | DCHECK_EQ(temp_class->NumVirtualMethods(), 0u); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 5932 | for (auto& method : new_class->GetMethods(image_pointer_size_)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5933 | if (method.GetDeclaringClass() == temp_class) { |
| 5934 | method.SetDeclaringClass(new_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5935 | } |
| 5936 | } |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 5937 | |
| 5938 | // Make sure the remembered set and mod-union tables know that we updated some of the native |
| 5939 | // roots. |
Mathieu Chartier | 88ea61e | 2018-06-20 17:45:41 -0700 | [diff] [blame] | 5940 | WriteBarrier::ForEveryFieldWrite(new_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5941 | } |
| 5942 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5943 | void ClassLinker::RegisterClassLoader(ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | 5b83050 | 2016-03-02 10:30:23 -0800 | [diff] [blame] | 5944 | CHECK(class_loader->GetAllocator() == nullptr); |
| 5945 | CHECK(class_loader->GetClassTable() == nullptr); |
| 5946 | Thread* const self = Thread::Current(); |
| 5947 | ClassLoaderData data; |
Ian Rogers | 55256cb | 2017-12-21 17:07:11 -0800 | [diff] [blame] | 5948 | data.weak_root = self->GetJniEnv()->GetVm()->AddWeakGlobalRef(self, class_loader); |
Mathieu Chartier | 5b83050 | 2016-03-02 10:30:23 -0800 | [diff] [blame] | 5949 | // Create and set the class table. |
| 5950 | data.class_table = new ClassTable; |
| 5951 | class_loader->SetClassTable(data.class_table); |
| 5952 | // Create and set the linear allocator. |
| 5953 | data.allocator = Runtime::Current()->CreateLinearAlloc(); |
| 5954 | class_loader->SetAllocator(data.allocator); |
| 5955 | // Add to the list so that we know to free the data later. |
| 5956 | class_loaders_.push_back(data); |
| 5957 | } |
| 5958 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5959 | ClassTable* ClassLinker::InsertClassTableForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 5960 | if (class_loader == nullptr) { |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 5961 | return boot_class_table_.get(); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5962 | } |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 5963 | ClassTable* class_table = class_loader->GetClassTable(); |
| 5964 | if (class_table == nullptr) { |
Mathieu Chartier | 5b83050 | 2016-03-02 10:30:23 -0800 | [diff] [blame] | 5965 | RegisterClassLoader(class_loader); |
| 5966 | class_table = class_loader->GetClassTable(); |
| 5967 | DCHECK(class_table != nullptr); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 5968 | } |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5969 | return class_table; |
| 5970 | } |
| 5971 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5972 | ClassTable* ClassLinker::ClassTableForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) { |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 5973 | return class_loader == nullptr ? boot_class_table_.get() : class_loader->GetClassTable(); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5974 | } |
| 5975 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5976 | static ImTable* FindSuperImt(ObjPtr<mirror::Class> klass, PointerSize pointer_size) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5977 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5978 | while (klass->HasSuperClass()) { |
| 5979 | klass = klass->GetSuperClass(); |
| 5980 | if (klass->ShouldHaveImt()) { |
| 5981 | return klass->GetImt(pointer_size); |
| 5982 | } |
| 5983 | } |
| 5984 | return nullptr; |
| 5985 | } |
| 5986 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5987 | bool ClassLinker::LinkClass(Thread* self, |
| 5988 | const char* descriptor, |
| 5989 | Handle<mirror::Class> klass, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5990 | Handle<mirror::ObjectArray<mirror::Class>> interfaces, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5991 | MutableHandle<mirror::Class>* h_new_class_out) { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5992 | CHECK_EQ(ClassStatus::kLoaded, klass->GetStatus()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5993 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5994 | if (!LinkSuperClass(klass)) { |
| 5995 | return false; |
| 5996 | } |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5997 | ArtMethod* imt_data[ImTable::kSize]; |
| 5998 | // If there are any new conflicts compared to super class. |
| 5999 | bool new_conflict = false; |
Nicolas Geoffray | 918dcea | 2017-07-21 07:58:14 +0000 | [diff] [blame] | 6000 | std::fill_n(imt_data, arraysize(imt_data), Runtime::Current()->GetImtUnimplementedMethod()); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6001 | if (!LinkMethods(self, klass, interfaces, &new_conflict, imt_data)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6002 | return false; |
| 6003 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 6004 | if (!LinkInstanceFields(self, klass)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6005 | return false; |
| 6006 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 6007 | size_t class_size; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 6008 | if (!LinkStaticFields(self, klass, &class_size)) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 6009 | return false; |
| 6010 | } |
| 6011 | CreateReferenceInstanceOffsets(klass); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 6012 | CHECK_EQ(ClassStatus::kLoaded, klass->GetStatus()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 6013 | |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6014 | ImTable* imt = nullptr; |
| 6015 | if (klass->ShouldHaveImt()) { |
| 6016 | // If there are any new conflicts compared to the super class we can not make a copy. There |
| 6017 | // can be cases where both will have a conflict method at the same slot without having the same |
| 6018 | // set of conflicts. In this case, we can not share the IMT since the conflict table slow path |
| 6019 | // will possibly create a table that is incorrect for either of the classes. |
| 6020 | // Same IMT with new_conflict does not happen very often. |
| 6021 | if (!new_conflict) { |
| 6022 | ImTable* super_imt = FindSuperImt(klass.Get(), image_pointer_size_); |
| 6023 | if (super_imt != nullptr) { |
| 6024 | bool imt_equals = true; |
| 6025 | for (size_t i = 0; i < ImTable::kSize && imt_equals; ++i) { |
| 6026 | imt_equals = imt_equals && (super_imt->Get(i, image_pointer_size_) == imt_data[i]); |
| 6027 | } |
| 6028 | if (imt_equals) { |
| 6029 | imt = super_imt; |
| 6030 | } |
| 6031 | } |
| 6032 | } |
| 6033 | if (imt == nullptr) { |
| 6034 | LinearAlloc* allocator = GetAllocatorForClassLoader(klass->GetClassLoader()); |
| 6035 | imt = reinterpret_cast<ImTable*>( |
Lokesh Gidra | 606bd94 | 2022-05-23 19:00:09 +0000 | [diff] [blame] | 6036 | allocator->Alloc(self, |
| 6037 | ImTable::SizeInBytes(image_pointer_size_), |
| 6038 | LinearAllocKind::kNoGCRoots)); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6039 | if (imt == nullptr) { |
| 6040 | return false; |
| 6041 | } |
| 6042 | imt->Populate(imt_data, image_pointer_size_); |
| 6043 | } |
| 6044 | } |
| 6045 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 6046 | if (!klass->IsTemp() || (!init_done_ && klass->GetClassSize() == class_size)) { |
| 6047 | // We don't need to retire this class as it has no embedded tables or it was created the |
| 6048 | // correct size during class linker initialization. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6049 | CHECK_EQ(klass->GetClassSize(), class_size) << klass->PrettyDescriptor(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 6050 | |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6051 | if (klass->ShouldHaveEmbeddedVTable()) { |
| 6052 | klass->PopulateEmbeddedVTable(image_pointer_size_); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 6053 | } |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6054 | if (klass->ShouldHaveImt()) { |
| 6055 | klass->SetImt(imt, image_pointer_size_); |
| 6056 | } |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 6057 | |
| 6058 | // Update CHA info based on whether we override methods. |
| 6059 | // Have to do this before setting the class as resolved which allows |
| 6060 | // instantiation of klass. |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 6061 | if (LIKELY(descriptor != nullptr) && cha_ != nullptr) { |
Andreas Gampe | c1ac9ee | 2017-07-24 22:35:49 -0700 | [diff] [blame] | 6062 | cha_->UpdateAfterLoadingOf(klass); |
| 6063 | } |
Nicolas Geoffray | 918dcea | 2017-07-21 07:58:14 +0000 | [diff] [blame] | 6064 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 6065 | // This will notify waiters on klass that saw the not yet resolved |
| 6066 | // class in the class_table_ during EnsureResolved. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 6067 | mirror::Class::SetStatus(klass, ClassStatus::kResolved, self); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 6068 | h_new_class_out->Assign(klass.Get()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 6069 | } else { |
| 6070 | CHECK(!klass->IsResolved()); |
| 6071 | // Retire the temporary class and create the correctly sized resolved class. |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 6072 | StackHandleScope<1> hs(self); |
Vladimir Marko | 3068d58 | 2019-05-28 16:39:29 +0100 | [diff] [blame] | 6073 | Handle<mirror::Class> h_new_class = |
| 6074 | 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] | 6075 | // Set arrays to null since we don't want to have multiple classes with the same ArtField or |
| 6076 | // ArtMethod array pointers. If this occurs, it causes bugs in remembered sets since the GC |
| 6077 | // may not see any references to the target space and clean the card for a class if another |
| 6078 | // class had the same array pointer. |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6079 | klass->SetMethodsPtrUnchecked(nullptr, 0, 0); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 6080 | klass->SetSFieldsPtrUnchecked(nullptr); |
| 6081 | klass->SetIFieldsPtrUnchecked(nullptr); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 6082 | if (UNLIKELY(h_new_class == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6083 | self->AssertPendingOOMException(); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 6084 | mirror::Class::SetStatus(klass, ClassStatus::kErrorUnresolved, self); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 6085 | return false; |
| 6086 | } |
| 6087 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 6088 | CHECK_EQ(h_new_class->GetClassSize(), class_size); |
| 6089 | ObjectLock<mirror::Class> lock(self, h_new_class); |
| 6090 | FixupTemporaryDeclaringClass(klass.Get(), h_new_class.Get()); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 6091 | |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 6092 | if (LIKELY(descriptor != nullptr)) { |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 6093 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Vladimir Marko | 0984e48 | 2019-03-27 16:41:41 +0000 | [diff] [blame] | 6094 | const ObjPtr<mirror::ClassLoader> class_loader = h_new_class.Get()->GetClassLoader(); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 6095 | ClassTable* const table = InsertClassTableForClassLoader(class_loader); |
Vladimir Marko | 0984e48 | 2019-03-27 16:41:41 +0000 | [diff] [blame] | 6096 | const ObjPtr<mirror::Class> existing = |
| 6097 | table->UpdateClass(descriptor, h_new_class.Get(), ComputeModifiedUtf8Hash(descriptor)); |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 6098 | if (class_loader != nullptr) { |
| 6099 | // We updated the class in the class table, perform the write barrier so that the GC knows |
| 6100 | // about the change. |
Mathieu Chartier | 88ea61e | 2018-06-20 17:45:41 -0700 | [diff] [blame] | 6101 | WriteBarrier::ForEveryFieldWrite(class_loader); |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 6102 | } |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 6103 | CHECK_EQ(existing, klass.Get()); |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 6104 | if (log_new_roots_) { |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 6105 | new_class_roots_.push_back(GcRoot<mirror::Class>(h_new_class.Get())); |
| 6106 | } |
| 6107 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 6108 | |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 6109 | // Update CHA info based on whether we override methods. |
| 6110 | // Have to do this before setting the class as resolved which allows |
| 6111 | // instantiation of klass. |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 6112 | if (LIKELY(descriptor != nullptr) && cha_ != nullptr) { |
Andreas Gampe | c1ac9ee | 2017-07-24 22:35:49 -0700 | [diff] [blame] | 6113 | cha_->UpdateAfterLoadingOf(h_new_class); |
| 6114 | } |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 6115 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 6116 | // This will notify waiters on temp class that saw the not yet resolved class in the |
| 6117 | // class_table_ during EnsureResolved. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 6118 | mirror::Class::SetStatus(klass, ClassStatus::kRetired, self); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 6119 | |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 6120 | CHECK_EQ(h_new_class->GetStatus(), ClassStatus::kResolving); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 6121 | // This will notify waiters on new_class that saw the not yet resolved |
| 6122 | // class in the class_table_ during EnsureResolved. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 6123 | mirror::Class::SetStatus(h_new_class, ClassStatus::kResolved, self); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 6124 | // Return the new class. |
| 6125 | h_new_class_out->Assign(h_new_class.Get()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 6126 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6127 | return true; |
| 6128 | } |
| 6129 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 6130 | bool ClassLinker::LoadSuperAndInterfaces(Handle<mirror::Class> klass, const DexFile& dex_file) { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 6131 | CHECK_EQ(ClassStatus::kIdx, klass->GetStatus()); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 6132 | const dex::ClassDef& class_def = dex_file.GetClassDef(klass->GetDexClassDefIndex()); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 6133 | dex::TypeIndex super_class_idx = class_def.superclass_idx_; |
| 6134 | if (super_class_idx.IsValid()) { |
Roland Levillain | 90328ac | 2016-05-18 12:25:38 +0100 | [diff] [blame] | 6135 | // Check that a class does not inherit from itself directly. |
| 6136 | // |
| 6137 | // TODO: This is a cheap check to detect the straightforward case |
| 6138 | // of a class extending itself (b/28685551), but we should do a |
| 6139 | // proper cycle detection on loaded classes, to detect all cases |
| 6140 | // of class circularity errors (b/28830038). |
| 6141 | if (super_class_idx == class_def.class_idx_) { |
| 6142 | ThrowClassCircularityError(klass.Get(), |
| 6143 | "Class %s extends itself", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6144 | klass->PrettyDescriptor().c_str()); |
Roland Levillain | 90328ac | 2016-05-18 12:25:38 +0100 | [diff] [blame] | 6145 | return false; |
| 6146 | } |
| 6147 | |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 6148 | ObjPtr<mirror::Class> super_class = ResolveType(super_class_idx, klass.Get()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 6149 | if (super_class == nullptr) { |
Brian Carlstrom | 65ca077 | 2011-09-24 16:03:08 -0700 | [diff] [blame] | 6150 | DCHECK(Thread::Current()->IsExceptionPending()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6151 | return false; |
| 6152 | } |
Ian Rogers | be125a9 | 2012-01-11 15:19:49 -0800 | [diff] [blame] | 6153 | // Verify |
| 6154 | if (!klass->CanAccess(super_class)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 6155 | ThrowIllegalAccessError(klass.Get(), "Class %s extended by class %s is inaccessible", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6156 | super_class->PrettyDescriptor().c_str(), |
| 6157 | klass->PrettyDescriptor().c_str()); |
Ian Rogers | be125a9 | 2012-01-11 15:19:49 -0800 | [diff] [blame] | 6158 | return false; |
| 6159 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 6160 | CHECK(super_class->IsResolved()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 6161 | klass->SetSuperClass(super_class); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6162 | } |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 6163 | const dex::TypeList* interfaces = dex_file.GetInterfacesList(class_def); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 6164 | if (interfaces != nullptr) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 6165 | for (size_t i = 0; i < interfaces->Size(); i++) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 6166 | dex::TypeIndex idx = interfaces->GetTypeItem(i).type_idx_; |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 6167 | ObjPtr<mirror::Class> interface = ResolveType(idx, klass.Get()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 6168 | if (interface == nullptr) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 6169 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 6170 | return false; |
| 6171 | } |
| 6172 | // Verify |
| 6173 | if (!klass->CanAccess(interface)) { |
| 6174 | // TODO: the RI seemed to ignore this in my testing. |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 6175 | ThrowIllegalAccessError(klass.Get(), |
| 6176 | "Interface %s implemented by class %s is inaccessible", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6177 | interface->PrettyDescriptor().c_str(), |
| 6178 | klass->PrettyDescriptor().c_str()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 6179 | return false; |
| 6180 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6181 | } |
| 6182 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 6183 | // Mark the class as loaded. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 6184 | mirror::Class::SetStatus(klass, ClassStatus::kLoaded, nullptr); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6185 | return true; |
| 6186 | } |
| 6187 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 6188 | bool ClassLinker::LinkSuperClass(Handle<mirror::Class> klass) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6189 | CHECK(!klass->IsPrimitive()); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6190 | ObjPtr<mirror::Class> super = klass->GetSuperClass(); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 6191 | ObjPtr<mirror::Class> object_class = GetClassRoot<mirror::Object>(this); |
| 6192 | if (klass.Get() == object_class) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 6193 | if (super != nullptr) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 6194 | ThrowClassFormatError(klass.Get(), "java.lang.Object must not have a superclass"); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6195 | return false; |
| 6196 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6197 | return true; |
| 6198 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 6199 | if (super == nullptr) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 6200 | ThrowLinkageError(klass.Get(), "No superclass defined for class %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6201 | klass->PrettyDescriptor().c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6202 | return false; |
| 6203 | } |
| 6204 | // Verify |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 6205 | if (klass->IsInterface() && super != object_class) { |
Vladimir Marko | 1fcae9f | 2017-11-28 14:14:19 +0000 | [diff] [blame] | 6206 | ThrowClassFormatError(klass.Get(), "Interfaces must have java.lang.Object as superclass"); |
| 6207 | return false; |
| 6208 | } |
Vladimir Marko | b43b2d8 | 2017-07-18 17:46:38 +0100 | [diff] [blame] | 6209 | if (super->IsFinal()) { |
| 6210 | ThrowVerifyError(klass.Get(), |
| 6211 | "Superclass %s of %s is declared final", |
| 6212 | super->PrettyDescriptor().c_str(), |
| 6213 | klass->PrettyDescriptor().c_str()); |
| 6214 | return false; |
| 6215 | } |
| 6216 | if (super->IsInterface()) { |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 6217 | ThrowIncompatibleClassChangeError(klass.Get(), |
Vladimir Marko | b43b2d8 | 2017-07-18 17:46:38 +0100 | [diff] [blame] | 6218 | "Superclass %s of %s is an interface", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6219 | super->PrettyDescriptor().c_str(), |
Vladimir Marko | b43b2d8 | 2017-07-18 17:46:38 +0100 | [diff] [blame] | 6220 | klass->PrettyDescriptor().c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6221 | return false; |
| 6222 | } |
| 6223 | if (!klass->CanAccess(super)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 6224 | ThrowIllegalAccessError(klass.Get(), "Superclass %s is inaccessible to class %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6225 | super->PrettyDescriptor().c_str(), |
| 6226 | klass->PrettyDescriptor().c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6227 | return false; |
| 6228 | } |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 6229 | |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 6230 | // Inherit kAccClassIsFinalizable from the superclass in case this |
| 6231 | // class doesn't override finalize. |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 6232 | if (super->IsFinalizable()) { |
| 6233 | klass->SetFinalizable(); |
| 6234 | } |
| 6235 | |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 6236 | // Inherit class loader flag form super class. |
| 6237 | if (super->IsClassLoaderClass()) { |
| 6238 | klass->SetClassLoaderClass(); |
| 6239 | } |
| 6240 | |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 6241 | // Inherit reference flags (if any) from the superclass. |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 6242 | uint32_t reference_flags = (super->GetClassFlags() & mirror::kClassFlagReference); |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 6243 | if (reference_flags != 0) { |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 6244 | CHECK_EQ(klass->GetClassFlags(), 0u); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 6245 | klass->SetClassFlags(klass->GetClassFlags() | reference_flags); |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 6246 | } |
Elliott Hughes | 72ee0ae | 2011-10-10 17:31:28 -0700 | [diff] [blame] | 6247 | // Disallow custom direct subclasses of java.lang.ref.Reference. |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 6248 | if (init_done_ && super == GetClassRoot<mirror::Reference>(this)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 6249 | ThrowLinkageError(klass.Get(), |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 6250 | "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] | 6251 | klass->PrettyDescriptor().c_str()); |
Elliott Hughes | 72ee0ae | 2011-10-10 17:31:28 -0700 | [diff] [blame] | 6252 | return false; |
| 6253 | } |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 6254 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 6255 | if (kIsDebugBuild) { |
| 6256 | // Ensure super classes are fully resolved prior to resolving fields.. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 6257 | while (super != nullptr) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 6258 | CHECK(super->IsResolved()); |
| 6259 | super = super->GetSuperClass(); |
| 6260 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 6261 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6262 | return true; |
| 6263 | } |
| 6264 | |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 6265 | // Comparator for name and signature of a method, used in finding overriding methods. Implementation |
| 6266 | // avoids the use of handles, if it didn't then rather than compare dex files we could compare dex |
| 6267 | // caches in the implementation below. |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 6268 | class MethodNameAndSignatureComparator final : public ValueObject { |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 6269 | public: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6270 | explicit MethodNameAndSignatureComparator(ArtMethod* method) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 6271 | REQUIRES_SHARED(Locks::mutator_lock_) : |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 6272 | dex_file_(method->GetDexFile()), mid_(&dex_file_->GetMethodId(method->GetDexMethodIndex())), |
Vladimir Marko | b4bd92f | 2021-07-05 12:18:26 +0100 | [diff] [blame] | 6273 | name_view_() { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6274 | DCHECK(!method->IsProxyMethod()) << method->PrettyMethod(); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6275 | } |
| 6276 | |
Vladimir Marko | b4bd92f | 2021-07-05 12:18:26 +0100 | [diff] [blame] | 6277 | ALWAYS_INLINE std::string_view GetNameView() { |
| 6278 | if (name_view_.empty()) { |
| 6279 | name_view_ = dex_file_->StringViewByIdx(mid_->name_idx_); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6280 | } |
Vladimir Marko | b4bd92f | 2021-07-05 12:18:26 +0100 | [diff] [blame] | 6281 | return name_view_; |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 6282 | } |
| 6283 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6284 | bool HasSameNameAndSignature(ArtMethod* other) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 6285 | REQUIRES_SHARED(Locks::mutator_lock_) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6286 | DCHECK(!other->IsProxyMethod()) << other->PrettyMethod(); |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 6287 | const DexFile* other_dex_file = other->GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 6288 | const dex::MethodId& other_mid = other_dex_file->GetMethodId(other->GetDexMethodIndex()); |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 6289 | if (dex_file_ == other_dex_file) { |
| 6290 | return mid_->name_idx_ == other_mid.name_idx_ && mid_->proto_idx_ == other_mid.proto_idx_; |
| 6291 | } |
Vladimir Marko | b4bd92f | 2021-07-05 12:18:26 +0100 | [diff] [blame] | 6292 | return GetNameView() == other_dex_file->StringViewByIdx(other_mid.name_idx_) && |
| 6293 | dex_file_->GetMethodSignature(*mid_) == other_dex_file->GetMethodSignature(other_mid); |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 6294 | } |
| 6295 | |
| 6296 | private: |
| 6297 | // Dex file for the method to compare against. |
| 6298 | const DexFile* const dex_file_; |
| 6299 | // MethodId for the method to compare against. |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 6300 | const dex::MethodId* const mid_; |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 6301 | // Lazily computed name from the dex file's strings. |
Vladimir Marko | b4bd92f | 2021-07-05 12:18:26 +0100 | [diff] [blame] | 6302 | std::string_view name_view_; |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 6303 | }; |
| 6304 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6305 | ArtMethod* ClassLinker::AddMethodToConflictTable(ObjPtr<mirror::Class> klass, |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6306 | ArtMethod* conflict_method, |
| 6307 | ArtMethod* interface_method, |
Nicolas Geoffray | 47213e4 | 2020-12-30 15:12:00 +0000 | [diff] [blame] | 6308 | ArtMethod* method) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 6309 | ImtConflictTable* current_table = conflict_method->GetImtConflictTable(kRuntimePointerSize); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6310 | Runtime* const runtime = Runtime::Current(); |
| 6311 | LinearAlloc* linear_alloc = GetAllocatorForClassLoader(klass->GetClassLoader()); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6312 | |
| 6313 | // 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] | 6314 | ArtMethod* new_conflict_method = (conflict_method == runtime->GetImtConflictMethod()) |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6315 | ? runtime->CreateImtConflictMethod(linear_alloc) |
| 6316 | : conflict_method; |
| 6317 | |
| 6318 | // Allocate a new table. Note that we will leak this table at the next conflict, |
| 6319 | // but that's a tradeoff compared to making the table fixed size. |
| 6320 | void* data = linear_alloc->Alloc( |
Lokesh Gidra | 606bd94 | 2022-05-23 19:00:09 +0000 | [diff] [blame] | 6321 | Thread::Current(), |
| 6322 | ImtConflictTable::ComputeSizeWithOneMoreEntry(current_table, image_pointer_size_), |
| 6323 | LinearAllocKind::kNoGCRoots); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6324 | if (data == nullptr) { |
| 6325 | LOG(ERROR) << "Failed to allocate conflict table"; |
| 6326 | return conflict_method; |
| 6327 | } |
| 6328 | ImtConflictTable* new_table = new (data) ImtConflictTable(current_table, |
| 6329 | interface_method, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6330 | method, |
| 6331 | image_pointer_size_); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6332 | |
| 6333 | // Do a fence to ensure threads see the data in the table before it is assigned |
| 6334 | // to the conflict method. |
| 6335 | // Note that there is a race in the presence of multiple threads and we may leak |
| 6336 | // memory from the LinearAlloc, but that's a tradeoff compared to using |
| 6337 | // atomic operations. |
Orion Hodson | 27b9676 | 2018-03-13 16:06:57 +0000 | [diff] [blame] | 6338 | std::atomic_thread_fence(std::memory_order_release); |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6339 | new_conflict_method->SetImtConflictTable(new_table, image_pointer_size_); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6340 | return new_conflict_method; |
| 6341 | } |
| 6342 | |
| 6343 | void ClassLinker::SetIMTRef(ArtMethod* unimplemented_method, |
| 6344 | ArtMethod* imt_conflict_method, |
| 6345 | ArtMethod* current_method, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6346 | /*out*/bool* new_conflict, |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6347 | /*out*/ArtMethod** imt_ref) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6348 | // Place method in imt if entry is empty, place conflict otherwise. |
| 6349 | if (*imt_ref == unimplemented_method) { |
| 6350 | *imt_ref = current_method; |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 6351 | } else if (!(*imt_ref)->IsRuntimeMethod()) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6352 | // If we are not a conflict and we have the same signature and name as the imt |
| 6353 | // entry, it must be that we overwrote a superclass vtable entry. |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 6354 | // Note that we have checked IsRuntimeMethod, as there may be multiple different |
| 6355 | // conflict methods. |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6356 | MethodNameAndSignatureComparator imt_comparator( |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6357 | (*imt_ref)->GetInterfaceMethodIfProxy(image_pointer_size_)); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6358 | if (imt_comparator.HasSameNameAndSignature( |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6359 | current_method->GetInterfaceMethodIfProxy(image_pointer_size_))) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6360 | *imt_ref = current_method; |
| 6361 | } else { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6362 | *imt_ref = imt_conflict_method; |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6363 | *new_conflict = true; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6364 | } |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 6365 | } else { |
| 6366 | // Place the default conflict method. Note that there may be an existing conflict |
| 6367 | // method in the IMT, but it could be one tailored to the super class, with a |
| 6368 | // specific ImtConflictTable. |
| 6369 | *imt_ref = imt_conflict_method; |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6370 | *new_conflict = true; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6371 | } |
| 6372 | } |
| 6373 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6374 | void ClassLinker::FillIMTAndConflictTables(ObjPtr<mirror::Class> klass) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6375 | DCHECK(klass->ShouldHaveImt()) << klass->PrettyClass(); |
| 6376 | DCHECK(!klass->IsTemp()) << klass->PrettyClass(); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6377 | ArtMethod* imt_data[ImTable::kSize]; |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6378 | Runtime* const runtime = Runtime::Current(); |
| 6379 | ArtMethod* const unimplemented_method = runtime->GetImtUnimplementedMethod(); |
| 6380 | ArtMethod* const conflict_method = runtime->GetImtConflictMethod(); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6381 | std::fill_n(imt_data, arraysize(imt_data), unimplemented_method); |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6382 | if (klass->GetIfTable() != nullptr) { |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6383 | bool new_conflict = false; |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6384 | FillIMTFromIfTable(klass->GetIfTable(), |
| 6385 | unimplemented_method, |
| 6386 | conflict_method, |
| 6387 | klass, |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 6388 | /*create_conflict_tables=*/true, |
| 6389 | /*ignore_copied_methods=*/false, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6390 | &new_conflict, |
| 6391 | &imt_data[0]); |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6392 | } |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6393 | // Compare the IMT with the super class including the conflict methods. If they are equivalent, |
| 6394 | // we can just use the same pointer. |
| 6395 | ImTable* imt = nullptr; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6396 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6397 | if (super_class != nullptr && super_class->ShouldHaveImt()) { |
| 6398 | ImTable* super_imt = super_class->GetImt(image_pointer_size_); |
| 6399 | bool same = true; |
| 6400 | for (size_t i = 0; same && i < ImTable::kSize; ++i) { |
| 6401 | ArtMethod* method = imt_data[i]; |
| 6402 | ArtMethod* super_method = super_imt->Get(i, image_pointer_size_); |
| 6403 | if (method != super_method) { |
| 6404 | bool is_conflict_table = method->IsRuntimeMethod() && |
| 6405 | method != unimplemented_method && |
| 6406 | method != conflict_method; |
| 6407 | // Verify conflict contents. |
| 6408 | bool super_conflict_table = super_method->IsRuntimeMethod() && |
| 6409 | super_method != unimplemented_method && |
| 6410 | super_method != conflict_method; |
| 6411 | if (!is_conflict_table || !super_conflict_table) { |
| 6412 | same = false; |
| 6413 | } else { |
| 6414 | ImtConflictTable* table1 = method->GetImtConflictTable(image_pointer_size_); |
| 6415 | ImtConflictTable* table2 = super_method->GetImtConflictTable(image_pointer_size_); |
| 6416 | same = same && table1->Equals(table2, image_pointer_size_); |
| 6417 | } |
| 6418 | } |
| 6419 | } |
| 6420 | if (same) { |
| 6421 | imt = super_imt; |
| 6422 | } |
| 6423 | } |
| 6424 | if (imt == nullptr) { |
| 6425 | imt = klass->GetImt(image_pointer_size_); |
| 6426 | DCHECK(imt != nullptr); |
| 6427 | imt->Populate(imt_data, image_pointer_size_); |
| 6428 | } else { |
| 6429 | klass->SetImt(imt, image_pointer_size_); |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6430 | } |
| 6431 | } |
| 6432 | |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6433 | ImtConflictTable* ClassLinker::CreateImtConflictTable(size_t count, |
| 6434 | LinearAlloc* linear_alloc, |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 6435 | PointerSize image_pointer_size) { |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6436 | void* data = linear_alloc->Alloc(Thread::Current(), |
Lokesh Gidra | 606bd94 | 2022-05-23 19:00:09 +0000 | [diff] [blame] | 6437 | ImtConflictTable::ComputeSize(count, image_pointer_size), |
| 6438 | LinearAllocKind::kNoGCRoots); |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6439 | return (data != nullptr) ? new (data) ImtConflictTable(count, image_pointer_size) : nullptr; |
| 6440 | } |
| 6441 | |
| 6442 | ImtConflictTable* ClassLinker::CreateImtConflictTable(size_t count, LinearAlloc* linear_alloc) { |
| 6443 | return CreateImtConflictTable(count, linear_alloc, image_pointer_size_); |
| 6444 | } |
| 6445 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6446 | void ClassLinker::FillIMTFromIfTable(ObjPtr<mirror::IfTable> if_table, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6447 | ArtMethod* unimplemented_method, |
| 6448 | ArtMethod* imt_conflict_method, |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6449 | ObjPtr<mirror::Class> klass, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6450 | bool create_conflict_tables, |
| 6451 | bool ignore_copied_methods, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6452 | /*out*/bool* new_conflict, |
| 6453 | /*out*/ArtMethod** imt) { |
| 6454 | uint32_t conflict_counts[ImTable::kSize] = {}; |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6455 | for (size_t i = 0, length = if_table->Count(); i < length; ++i) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6456 | ObjPtr<mirror::Class> interface = if_table->GetInterface(i); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6457 | const size_t num_virtuals = interface->NumVirtualMethods(); |
| 6458 | const size_t method_array_count = if_table->GetMethodArrayCount(i); |
| 6459 | // Virtual methods can be larger than the if table methods if there are default methods. |
| 6460 | DCHECK_GE(num_virtuals, method_array_count); |
| 6461 | if (kIsDebugBuild) { |
| 6462 | if (klass->IsInterface()) { |
| 6463 | DCHECK_EQ(method_array_count, 0u); |
| 6464 | } else { |
| 6465 | DCHECK_EQ(interface->NumDeclaredVirtualMethods(), method_array_count); |
| 6466 | } |
| 6467 | } |
| 6468 | if (method_array_count == 0) { |
| 6469 | continue; |
| 6470 | } |
Vladimir Marko | 557fece | 2019-03-26 14:29:41 +0000 | [diff] [blame] | 6471 | ObjPtr<mirror::PointerArray> method_array = if_table->GetMethodArray(i); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6472 | for (size_t j = 0; j < method_array_count; ++j) { |
| 6473 | ArtMethod* implementation_method = |
| 6474 | method_array->GetElementPtrSize<ArtMethod*>(j, image_pointer_size_); |
| 6475 | if (ignore_copied_methods && implementation_method->IsCopied()) { |
| 6476 | continue; |
| 6477 | } |
| 6478 | DCHECK(implementation_method != nullptr); |
| 6479 | // Miranda methods cannot be used to implement an interface method, but they are safe to put |
| 6480 | // in the IMT since their entrypoint is the interface trampoline. If we put any copied methods |
| 6481 | // or interface methods in the IMT here they will not create extra conflicts since we compare |
| 6482 | // names and signatures in SetIMTRef. |
| 6483 | ArtMethod* interface_method = interface->GetVirtualMethod(j, image_pointer_size_); |
David Srbecky | e36e7f2 | 2018-11-14 14:21:23 +0000 | [diff] [blame] | 6484 | const uint32_t imt_index = interface_method->GetImtIndex(); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6485 | |
| 6486 | // There is only any conflicts if all of the interface methods for an IMT slot don't have |
| 6487 | // the same implementation method, keep track of this to avoid creating a conflict table in |
| 6488 | // this case. |
| 6489 | |
| 6490 | // Conflict table size for each IMT slot. |
| 6491 | ++conflict_counts[imt_index]; |
| 6492 | |
| 6493 | SetIMTRef(unimplemented_method, |
| 6494 | imt_conflict_method, |
| 6495 | implementation_method, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6496 | /*out*/new_conflict, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6497 | /*out*/&imt[imt_index]); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6498 | } |
| 6499 | } |
| 6500 | |
| 6501 | if (create_conflict_tables) { |
| 6502 | // Create the conflict tables. |
| 6503 | LinearAlloc* linear_alloc = GetAllocatorForClassLoader(klass->GetClassLoader()); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6504 | for (size_t i = 0; i < ImTable::kSize; ++i) { |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6505 | size_t conflicts = conflict_counts[i]; |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6506 | if (imt[i] == imt_conflict_method) { |
| 6507 | ImtConflictTable* new_table = CreateImtConflictTable(conflicts, linear_alloc); |
| 6508 | if (new_table != nullptr) { |
| 6509 | ArtMethod* new_conflict_method = |
| 6510 | Runtime::Current()->CreateImtConflictMethod(linear_alloc); |
| 6511 | new_conflict_method->SetImtConflictTable(new_table, image_pointer_size_); |
| 6512 | imt[i] = new_conflict_method; |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6513 | } else { |
| 6514 | LOG(ERROR) << "Failed to allocate conflict table"; |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6515 | imt[i] = imt_conflict_method; |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6516 | } |
| 6517 | } else { |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6518 | DCHECK_NE(imt[i], imt_conflict_method); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6519 | } |
| 6520 | } |
| 6521 | |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6522 | for (size_t i = 0, length = if_table->Count(); i < length; ++i) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6523 | ObjPtr<mirror::Class> interface = if_table->GetInterface(i); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6524 | const size_t method_array_count = if_table->GetMethodArrayCount(i); |
| 6525 | // Virtual methods can be larger than the if table methods if there are default methods. |
| 6526 | if (method_array_count == 0) { |
| 6527 | continue; |
| 6528 | } |
Vladimir Marko | 557fece | 2019-03-26 14:29:41 +0000 | [diff] [blame] | 6529 | ObjPtr<mirror::PointerArray> method_array = if_table->GetMethodArray(i); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6530 | for (size_t j = 0; j < method_array_count; ++j) { |
| 6531 | ArtMethod* implementation_method = |
| 6532 | method_array->GetElementPtrSize<ArtMethod*>(j, image_pointer_size_); |
| 6533 | if (ignore_copied_methods && implementation_method->IsCopied()) { |
| 6534 | continue; |
| 6535 | } |
| 6536 | DCHECK(implementation_method != nullptr); |
| 6537 | ArtMethod* interface_method = interface->GetVirtualMethod(j, image_pointer_size_); |
David Srbecky | e36e7f2 | 2018-11-14 14:21:23 +0000 | [diff] [blame] | 6538 | const uint32_t imt_index = interface_method->GetImtIndex(); |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6539 | if (!imt[imt_index]->IsRuntimeMethod() || |
| 6540 | imt[imt_index] == unimplemented_method || |
| 6541 | imt[imt_index] == imt_conflict_method) { |
| 6542 | continue; |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6543 | } |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6544 | ImtConflictTable* table = imt[imt_index]->GetImtConflictTable(image_pointer_size_); |
| 6545 | const size_t num_entries = table->NumEntries(image_pointer_size_); |
| 6546 | table->SetInterfaceMethod(num_entries, image_pointer_size_, interface_method); |
| 6547 | table->SetImplementationMethod(num_entries, image_pointer_size_, implementation_method); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6548 | } |
| 6549 | } |
| 6550 | } |
| 6551 | } |
| 6552 | |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6553 | namespace { |
| 6554 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6555 | // Simple helper function that checks that no subtypes of 'val' are contained within the 'classes' |
| 6556 | // set. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6557 | static bool NotSubinterfaceOfAny( |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6558 | const ScopedArenaHashSet<mirror::Class*>& classes, |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6559 | ObjPtr<mirror::Class> val) |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6560 | REQUIRES(Roles::uninterruptible_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 6561 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6562 | DCHECK(val != nullptr); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6563 | for (ObjPtr<mirror::Class> c : classes) { |
| 6564 | if (val->IsAssignableFrom(c)) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6565 | return false; |
| 6566 | } |
| 6567 | } |
| 6568 | return true; |
| 6569 | } |
| 6570 | |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6571 | // We record new interfaces by the index of the direct interface and the index in the |
| 6572 | // direct interface's `IfTable`, or `dex::kDexNoIndex` if it's the direct interface itself. |
| 6573 | struct NewInterfaceReference { |
| 6574 | uint32_t direct_interface_index; |
| 6575 | uint32_t direct_interface_iftable_index; |
| 6576 | }; |
| 6577 | |
| 6578 | class ProxyInterfacesAccessor { |
| 6579 | public: |
| 6580 | explicit ProxyInterfacesAccessor(Handle<mirror::ObjectArray<mirror::Class>> interfaces) |
| 6581 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 6582 | : interfaces_(interfaces) {} |
| 6583 | |
| 6584 | size_t GetLength() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 6585 | return interfaces_->GetLength(); |
| 6586 | } |
| 6587 | |
| 6588 | ObjPtr<mirror::Class> GetInterface(size_t index) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 6589 | DCHECK_LT(index, GetLength()); |
| 6590 | return interfaces_->GetWithoutChecks(index); |
| 6591 | } |
| 6592 | |
| 6593 | private: |
| 6594 | Handle<mirror::ObjectArray<mirror::Class>> interfaces_; |
| 6595 | }; |
| 6596 | |
| 6597 | class NonProxyInterfacesAccessor { |
| 6598 | public: |
| 6599 | NonProxyInterfacesAccessor(ClassLinker* class_linker, Handle<mirror::Class> klass) |
| 6600 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 6601 | : interfaces_(klass->GetInterfaceTypeList()), |
| 6602 | class_linker_(class_linker), |
| 6603 | klass_(klass) { |
| 6604 | DCHECK(!klass->IsProxyClass()); |
| 6605 | } |
| 6606 | |
| 6607 | size_t GetLength() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 6608 | return (interfaces_ != nullptr) ? interfaces_->Size() : 0u; |
| 6609 | } |
| 6610 | |
| 6611 | ObjPtr<mirror::Class> GetInterface(size_t index) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 6612 | DCHECK_LT(index, GetLength()); |
| 6613 | dex::TypeIndex type_index = interfaces_->GetTypeItem(index).type_idx_; |
| 6614 | return class_linker_->LookupResolvedType(type_index, klass_.Get()); |
| 6615 | } |
| 6616 | |
| 6617 | private: |
| 6618 | const dex::TypeList* interfaces_; |
| 6619 | ClassLinker* class_linker_; |
| 6620 | Handle<mirror::Class> klass_; |
| 6621 | }; |
| 6622 | |
| 6623 | // Finds new interfaces to add to the interface table in addition to superclass interfaces. |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6624 | // |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6625 | // Interfaces in the interface table must satisfy the following constraint: |
| 6626 | // all I, J: Interface | I <: J implies J precedes I |
| 6627 | // (note A <: B means that A is a subtype of B). We order this backwards so that we do not need |
| 6628 | // to reorder superclass interfaces when new interfaces are added in subclass's interface tables. |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6629 | // |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6630 | // This function returns a list of references for all interfaces in the transitive |
| 6631 | // closure of the direct interfaces that are not in the superclass interfaces. |
| 6632 | // The entries in the list are ordered to satisfy the interface table ordering |
| 6633 | // constraint and therefore the interface table formed by appending them to the |
| 6634 | // superclass interface table shall also satisfy that constraint. |
| 6635 | template <typename InterfaceAccessor> |
| 6636 | ALWAYS_INLINE |
| 6637 | static ArrayRef<const NewInterfaceReference> FindNewIfTableInterfaces( |
| 6638 | ObjPtr<mirror::IfTable> super_iftable, |
| 6639 | size_t super_ifcount, |
| 6640 | ScopedArenaAllocator* allocator, |
| 6641 | InterfaceAccessor&& interfaces, |
| 6642 | ArrayRef<NewInterfaceReference> initial_storage, |
| 6643 | /*out*/ScopedArenaVector<NewInterfaceReference>* supplemental_storage) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 6644 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 6645 | ScopedAssertNoThreadSuspension nts(__FUNCTION__); |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6646 | |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 6647 | // This is the set of all classes already in the iftable. Used to make checking |
| 6648 | // if a class has already been added quicker. |
| 6649 | constexpr size_t kBufferSize = 32; // 256 bytes on 64-bit architectures. |
| 6650 | mirror::Class* buffer[kBufferSize]; |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6651 | ScopedArenaHashSet<mirror::Class*> classes_in_iftable(buffer, kBufferSize, allocator->Adapter()); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6652 | // The first super_ifcount elements are from the superclass. We note that they are already added. |
| 6653 | for (size_t i = 0; i < super_ifcount; i++) { |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6654 | ObjPtr<mirror::Class> iface = super_iftable->GetInterface(i); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6655 | DCHECK(NotSubinterfaceOfAny(classes_in_iftable, iface)) << "Bad ordering."; |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6656 | classes_in_iftable.Put(iface.Ptr()); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6657 | } |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6658 | |
| 6659 | ArrayRef<NewInterfaceReference> current_storage = initial_storage; |
| 6660 | DCHECK_NE(current_storage.size(), 0u); |
| 6661 | size_t num_new_interfaces = 0u; |
| 6662 | auto insert_reference = [&](uint32_t direct_interface_index, |
| 6663 | uint32_t direct_interface_iface_index) { |
| 6664 | if (UNLIKELY(num_new_interfaces == current_storage.size())) { |
| 6665 | bool copy = current_storage.data() != supplemental_storage->data(); |
| 6666 | supplemental_storage->resize(2u * num_new_interfaces); |
| 6667 | if (copy) { |
| 6668 | std::copy_n(current_storage.data(), num_new_interfaces, supplemental_storage->data()); |
| 6669 | } |
| 6670 | current_storage = ArrayRef<NewInterfaceReference>(*supplemental_storage); |
| 6671 | } |
| 6672 | current_storage[num_new_interfaces] = {direct_interface_index, direct_interface_iface_index}; |
| 6673 | ++num_new_interfaces; |
| 6674 | }; |
| 6675 | |
| 6676 | for (size_t i = 0, num_interfaces = interfaces.GetLength(); i != num_interfaces; ++i) { |
| 6677 | ObjPtr<mirror::Class> interface = interfaces.GetInterface(i); |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 6678 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6679 | // Let us call the first filled_ifcount elements of iftable the current-iface-list. |
| 6680 | // At this point in the loop current-iface-list has the invariant that: |
| 6681 | // for every pair of interfaces I,J within it: |
| 6682 | // if index_of(I) < index_of(J) then I is not a subtype of J |
| 6683 | |
| 6684 | // If we have already seen this element then all of its super-interfaces must already be in the |
| 6685 | // current-iface-list so we can skip adding it. |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 6686 | if (classes_in_iftable.find(interface.Ptr()) == classes_in_iftable.end()) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6687 | // We haven't seen this interface so add all of its super-interfaces onto the |
| 6688 | // current-iface-list, skipping those already on it. |
| 6689 | int32_t ifcount = interface->GetIfTableCount(); |
| 6690 | for (int32_t j = 0; j < ifcount; j++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6691 | ObjPtr<mirror::Class> super_interface = interface->GetIfTable()->GetInterface(j); |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6692 | if (classes_in_iftable.find(super_interface.Ptr()) == classes_in_iftable.end()) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6693 | DCHECK(NotSubinterfaceOfAny(classes_in_iftable, super_interface)) << "Bad ordering."; |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6694 | classes_in_iftable.Put(super_interface.Ptr()); |
| 6695 | insert_reference(i, j); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6696 | } |
| 6697 | } |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6698 | // Add this interface reference after all of its super-interfaces. |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6699 | DCHECK(NotSubinterfaceOfAny(classes_in_iftable, interface)) << "Bad ordering"; |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6700 | classes_in_iftable.Put(interface.Ptr()); |
| 6701 | insert_reference(i, dex::kDexNoIndex); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6702 | } else if (kIsDebugBuild) { |
| 6703 | // Check all super-interfaces are already in the list. |
| 6704 | int32_t ifcount = interface->GetIfTableCount(); |
| 6705 | for (int32_t j = 0; j < ifcount; j++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6706 | ObjPtr<mirror::Class> super_interface = interface->GetIfTable()->GetInterface(j); |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6707 | DCHECK(classes_in_iftable.find(super_interface.Ptr()) != classes_in_iftable.end()) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6708 | << "Iftable does not contain " << mirror::Class::PrettyClass(super_interface) |
| 6709 | << ", a superinterface of " << interface->PrettyClass(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6710 | } |
| 6711 | } |
| 6712 | } |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6713 | return ArrayRef<const NewInterfaceReference>(current_storage.data(), num_new_interfaces); |
| 6714 | } |
| 6715 | |
| 6716 | template <typename InterfaceAccessor> |
| 6717 | static ObjPtr<mirror::IfTable> SetupInterfaceLookupTable( |
| 6718 | Thread* self, |
| 6719 | Handle<mirror::Class> klass, |
| 6720 | ScopedArenaAllocator* allocator, |
| 6721 | InterfaceAccessor&& interfaces) |
| 6722 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 6723 | DCHECK(klass->HasSuperClass()); |
| 6724 | ObjPtr<mirror::IfTable> super_iftable = klass->GetSuperClass()->GetIfTable(); |
Vladimir Marko | 0441d20 | 2022-02-18 13:55:15 +0000 | [diff] [blame] | 6725 | DCHECK(super_iftable != nullptr); |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6726 | const size_t num_interfaces = interfaces.GetLength(); |
| 6727 | |
Vladimir Marko | 0441d20 | 2022-02-18 13:55:15 +0000 | [diff] [blame] | 6728 | // If there are no new interfaces, return the interface table from superclass. |
| 6729 | // If any implementation methods are overridden, we shall copy the table and |
| 6730 | // the method arrays that contain any differences (copy-on-write). |
| 6731 | if (num_interfaces == 0) { |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6732 | return super_iftable; |
| 6733 | } |
| 6734 | |
| 6735 | // Check that every class being implemented is an interface. |
| 6736 | for (size_t i = 0; i != num_interfaces; ++i) { |
| 6737 | ObjPtr<mirror::Class> interface = interfaces.GetInterface(i); |
| 6738 | DCHECK(interface != nullptr); |
| 6739 | if (UNLIKELY(!interface->IsInterface())) { |
| 6740 | ThrowIncompatibleClassChangeError(klass.Get(), |
| 6741 | "Class %s implements non-interface class %s", |
| 6742 | klass->PrettyDescriptor().c_str(), |
| 6743 | interface->PrettyDescriptor().c_str()); |
| 6744 | return nullptr; |
| 6745 | } |
| 6746 | } |
| 6747 | |
| 6748 | static constexpr size_t kMaxStackReferences = 16; |
| 6749 | NewInterfaceReference initial_storage[kMaxStackReferences]; |
| 6750 | ScopedArenaVector<NewInterfaceReference> supplemental_storage(allocator->Adapter()); |
Vladimir Marko | 0441d20 | 2022-02-18 13:55:15 +0000 | [diff] [blame] | 6751 | const size_t super_ifcount = super_iftable->Count(); |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6752 | ArrayRef<const NewInterfaceReference> new_interface_references = |
| 6753 | FindNewIfTableInterfaces( |
| 6754 | super_iftable, |
| 6755 | super_ifcount, |
| 6756 | allocator, |
| 6757 | interfaces, |
| 6758 | ArrayRef<NewInterfaceReference>(initial_storage), |
| 6759 | &supplemental_storage); |
| 6760 | |
Vladimir Marko | 0441d20 | 2022-02-18 13:55:15 +0000 | [diff] [blame] | 6761 | // If all declared interfaces were already present in superclass interface table, |
| 6762 | // return the interface table from superclass. See above. |
| 6763 | if (UNLIKELY(new_interface_references.empty())) { |
| 6764 | return super_iftable; |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6765 | } |
| 6766 | |
| 6767 | // Create the interface table. |
| 6768 | size_t ifcount = super_ifcount + new_interface_references.size(); |
| 6769 | ObjPtr<mirror::IfTable> iftable = AllocIfTable(self, ifcount, super_iftable->GetClass()); |
| 6770 | if (UNLIKELY(iftable == nullptr)) { |
| 6771 | self->AssertPendingOOMException(); |
| 6772 | return nullptr; |
| 6773 | } |
| 6774 | // Fill in table with superclass's iftable. |
| 6775 | if (super_ifcount != 0) { |
| 6776 | // Reload `super_iftable` as it may have been clobbered by the allocation. |
| 6777 | super_iftable = klass->GetSuperClass()->GetIfTable(); |
Vladimir Marko | 0441d20 | 2022-02-18 13:55:15 +0000 | [diff] [blame] | 6778 | for (size_t i = 0; i != super_ifcount; i++) { |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6779 | ObjPtr<mirror::Class> super_interface = super_iftable->GetInterface(i); |
Vladimir Marko | 0441d20 | 2022-02-18 13:55:15 +0000 | [diff] [blame] | 6780 | DCHECK(super_interface != nullptr); |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6781 | iftable->SetInterface(i, super_interface); |
Vladimir Marko | 0441d20 | 2022-02-18 13:55:15 +0000 | [diff] [blame] | 6782 | ObjPtr<mirror::PointerArray> method_array = super_iftable->GetMethodArrayOrNull(i); |
| 6783 | if (method_array != nullptr) { |
| 6784 | iftable->SetMethodArray(i, method_array); |
| 6785 | } |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6786 | } |
| 6787 | } |
| 6788 | // Fill in the table with additional interfaces. |
| 6789 | size_t current_index = super_ifcount; |
| 6790 | for (NewInterfaceReference ref : new_interface_references) { |
| 6791 | ObjPtr<mirror::Class> direct_interface = interfaces.GetInterface(ref.direct_interface_index); |
| 6792 | ObjPtr<mirror::Class> new_interface = (ref.direct_interface_iftable_index != dex::kDexNoIndex) |
| 6793 | ? direct_interface->GetIfTable()->GetInterface(ref.direct_interface_iftable_index) |
| 6794 | : direct_interface; |
| 6795 | iftable->SetInterface(current_index, new_interface); |
| 6796 | ++current_index; |
| 6797 | } |
| 6798 | DCHECK_EQ(current_index, ifcount); |
| 6799 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6800 | if (kIsDebugBuild) { |
| 6801 | // Check that the iftable is ordered correctly. |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6802 | for (size_t i = 0; i < ifcount; i++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6803 | ObjPtr<mirror::Class> if_a = iftable->GetInterface(i); |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6804 | for (size_t j = i + 1; j < ifcount; j++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6805 | ObjPtr<mirror::Class> if_b = iftable->GetInterface(j); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6806 | // !(if_a <: if_b) |
| 6807 | CHECK(!if_b->IsAssignableFrom(if_a)) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6808 | << "Bad interface order: " << mirror::Class::PrettyClass(if_a) << " (index " << i |
| 6809 | << ") extends " |
| 6810 | << 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] | 6811 | << "interface list."; |
| 6812 | } |
| 6813 | } |
| 6814 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6815 | |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6816 | return iftable; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6817 | } |
| 6818 | |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6819 | // Check that all vtable entries are present in this class's virtuals or are the same as a |
| 6820 | // superclasses vtable entry. |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 6821 | void CheckClassOwnsVTableEntries(Thread* self, |
| 6822 | Handle<mirror::Class> klass, |
| 6823 | PointerSize pointer_size) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 6824 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6825 | StackHandleScope<2> hs(self); |
| 6826 | Handle<mirror::PointerArray> check_vtable(hs.NewHandle(klass->GetVTableDuringLinking())); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6827 | ObjPtr<mirror::Class> super_temp = (klass->HasSuperClass()) ? klass->GetSuperClass() : nullptr; |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6828 | Handle<mirror::Class> superclass(hs.NewHandle(super_temp)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 6829 | int32_t super_vtable_length = (superclass != nullptr) ? superclass->GetVTableLength() : 0; |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6830 | for (int32_t i = 0; i < check_vtable->GetLength(); ++i) { |
| 6831 | ArtMethod* m = check_vtable->GetElementPtrSize<ArtMethod*>(i, pointer_size); |
| 6832 | CHECK(m != nullptr); |
| 6833 | |
Alex Light | a41a3078 | 2017-03-29 11:33:19 -0700 | [diff] [blame] | 6834 | if (m->GetMethodIndexDuringLinking() != i) { |
| 6835 | LOG(WARNING) << m->PrettyMethod() |
| 6836 | << " has an unexpected method index for its spot in the vtable for class" |
| 6837 | << klass->PrettyClass(); |
| 6838 | } |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6839 | ArraySlice<ArtMethod> virtuals = klass->GetVirtualMethodsSliceUnchecked(pointer_size); |
| 6840 | auto is_same_method = [m] (const ArtMethod& meth) { |
| 6841 | return &meth == m; |
| 6842 | }; |
Alex Light | 3f98053 | 2017-03-17 15:10:32 -0700 | [diff] [blame] | 6843 | if (!((super_vtable_length > i && superclass->GetVTableEntry(i, pointer_size) == m) || |
| 6844 | std::find_if(virtuals.begin(), virtuals.end(), is_same_method) != virtuals.end())) { |
| 6845 | LOG(WARNING) << m->PrettyMethod() << " does not seem to be owned by current class " |
| 6846 | << klass->PrettyClass() << " or any of its superclasses!"; |
| 6847 | } |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6848 | } |
| 6849 | } |
| 6850 | |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6851 | // Check to make sure the vtable does not have duplicates. Duplicates could cause problems when a |
| 6852 | // method is overridden in a subclass. |
Andreas Gampe | a2fed08 | 2019-02-01 09:34:43 -0800 | [diff] [blame] | 6853 | template <PointerSize kPointerSize> |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 6854 | void CheckVTableHasNoDuplicates(Thread* self, Handle<mirror::Class> klass) |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6855 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 6856 | StackHandleScope<1> hs(self); |
| 6857 | Handle<mirror::PointerArray> vtable(hs.NewHandle(klass->GetVTableDuringLinking())); |
| 6858 | int32_t num_entries = vtable->GetLength(); |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 6859 | |
| 6860 | // Observations: |
| 6861 | // * The older implementation was O(n^2) and got too expensive for apps with larger classes. |
| 6862 | // * Many classes do not override Object functions (e.g., equals/hashCode/toString). Thus, |
| 6863 | // for many classes outside of libcore a cross-dexfile check has to be run anyways. |
| 6864 | // * In the cross-dexfile case, with the O(n^2), in the best case O(n) cross checks would have |
| 6865 | // to be done. It is thus OK in a single-pass algorithm to read all data, anyways. |
| 6866 | // * The single-pass algorithm will trade memory for speed, but that is OK. |
| 6867 | |
| 6868 | CHECK_GT(num_entries, 0); |
| 6869 | |
| 6870 | auto log_fn = [&vtable, &klass](int32_t i, int32_t j) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 6871 | ArtMethod* m1 = vtable->GetElementPtrSize<ArtMethod*, kPointerSize>(i); |
| 6872 | ArtMethod* m2 = vtable->GetElementPtrSize<ArtMethod*, kPointerSize>(j); |
| 6873 | LOG(WARNING) << "vtable entries " << i << " and " << j << " are identical for " |
| 6874 | << klass->PrettyClass() << " in method " << m1->PrettyMethod() |
| 6875 | << " (0x" << std::hex << reinterpret_cast<uintptr_t>(m2) << ") and " |
| 6876 | << m2->PrettyMethod() << " (0x" << std::hex |
| 6877 | << reinterpret_cast<uintptr_t>(m2) << ")"; |
| 6878 | }; |
| 6879 | struct BaseHashType { |
| 6880 | static size_t HashCombine(size_t seed, size_t val) { |
| 6881 | return seed ^ (val + 0x9e3779b9 + (seed << 6) + (seed >> 2)); |
| 6882 | } |
| 6883 | }; |
| 6884 | |
| 6885 | // Check assuming all entries come from the same dex file. |
| 6886 | { |
| 6887 | // Find the first interesting method and its dex file. |
| 6888 | int32_t start = 0; |
| 6889 | for (; start < num_entries; ++start) { |
| 6890 | ArtMethod* vtable_entry = vtable->GetElementPtrSize<ArtMethod*, kPointerSize>(start); |
| 6891 | // Don't bother if we cannot 'see' the vtable entry (i.e. it is a package-private member |
| 6892 | // maybe). |
| 6893 | if (!klass->CanAccessMember(vtable_entry->GetDeclaringClass(), |
| 6894 | vtable_entry->GetAccessFlags())) { |
| 6895 | continue; |
| 6896 | } |
| 6897 | break; |
| 6898 | } |
| 6899 | if (start == num_entries) { |
| 6900 | return; |
| 6901 | } |
| 6902 | const DexFile* dex_file = |
| 6903 | vtable->GetElementPtrSize<ArtMethod*, kPointerSize>(start)-> |
| 6904 | GetInterfaceMethodIfProxy(kPointerSize)->GetDexFile(); |
| 6905 | |
| 6906 | // Helper function to avoid logging if we have to run the cross-file checks. |
| 6907 | auto check_fn = [&](bool log_warn) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 6908 | // Use a map to store seen entries, as the storage space is too large for a bitvector. |
| 6909 | using PairType = std::pair<uint32_t, uint16_t>; |
| 6910 | struct PairHash : BaseHashType { |
| 6911 | size_t operator()(const PairType& key) const { |
| 6912 | return BaseHashType::HashCombine(BaseHashType::HashCombine(0, key.first), key.second); |
| 6913 | } |
| 6914 | }; |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 6915 | HashMap<PairType, int32_t, DefaultMapEmptyFn<PairType, int32_t>, PairHash> seen; |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 6916 | seen.reserve(2 * num_entries); |
| 6917 | bool need_slow_path = false; |
| 6918 | bool found_dup = false; |
| 6919 | for (int i = start; i < num_entries; ++i) { |
| 6920 | // Can use Unchecked here as the start loop already ensured that the arrays are correct |
| 6921 | // wrt/ kPointerSize. |
| 6922 | ArtMethod* vtable_entry = vtable->GetElementPtrSizeUnchecked<ArtMethod*, kPointerSize>(i); |
| 6923 | if (!klass->CanAccessMember(vtable_entry->GetDeclaringClass(), |
| 6924 | vtable_entry->GetAccessFlags())) { |
| 6925 | continue; |
| 6926 | } |
| 6927 | ArtMethod* m = vtable_entry->GetInterfaceMethodIfProxy(kPointerSize); |
| 6928 | if (dex_file != m->GetDexFile()) { |
| 6929 | need_slow_path = true; |
| 6930 | break; |
| 6931 | } |
| 6932 | const dex::MethodId* m_mid = &dex_file->GetMethodId(m->GetDexMethodIndex()); |
| 6933 | PairType pair = std::make_pair(m_mid->name_idx_.index_, m_mid->proto_idx_.index_); |
| 6934 | auto it = seen.find(pair); |
| 6935 | if (it != seen.end()) { |
| 6936 | found_dup = true; |
| 6937 | if (log_warn) { |
| 6938 | log_fn(it->second, i); |
| 6939 | } |
| 6940 | } else { |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 6941 | seen.insert(std::make_pair(pair, i)); |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 6942 | } |
| 6943 | } |
| 6944 | return std::make_pair(need_slow_path, found_dup); |
| 6945 | }; |
| 6946 | std::pair<bool, bool> result = check_fn(/* log_warn= */ false); |
| 6947 | if (!result.first) { |
| 6948 | if (result.second) { |
| 6949 | check_fn(/* log_warn= */ true); |
| 6950 | } |
| 6951 | return; |
| 6952 | } |
| 6953 | } |
| 6954 | |
| 6955 | // Need to check across dex files. |
| 6956 | struct Entry { |
| 6957 | size_t cached_hash = 0; |
Vladimir Marko | aa027b8 | 2021-01-06 20:34:20 +0000 | [diff] [blame] | 6958 | uint32_t name_len = 0; |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 6959 | const char* name = nullptr; |
| 6960 | Signature signature = Signature::NoSignature(); |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 6961 | |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 6962 | Entry() = default; |
| 6963 | Entry(const Entry& other) = default; |
| 6964 | Entry& operator=(const Entry& other) = default; |
| 6965 | |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 6966 | Entry(const DexFile* dex_file, const dex::MethodId& mid) |
Vladimir Marko | aa027b8 | 2021-01-06 20:34:20 +0000 | [diff] [blame] | 6967 | : name_len(0), // Explicit to enforce ordering with -Werror,-Wreorder-ctor. |
| 6968 | // This call writes `name_len` and it is therefore necessary that the |
| 6969 | // initializer for `name_len` comes before it, otherwise the value |
| 6970 | // from the call would be overwritten by that initializer. |
| 6971 | name(dex_file->StringDataAndUtf16LengthByIdx(mid.name_idx_, &name_len)), |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 6972 | signature(dex_file->GetMethodSignature(mid)) { |
Vladimir Marko | aa027b8 | 2021-01-06 20:34:20 +0000 | [diff] [blame] | 6973 | // The `name_len` has been initialized to the UTF16 length. Calculate length in bytes. |
| 6974 | if (name[name_len] != 0) { |
| 6975 | name_len += strlen(name + name_len); |
| 6976 | } |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 6977 | } |
| 6978 | |
| 6979 | bool operator==(const Entry& other) const { |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 6980 | return name_len == other.name_len && |
| 6981 | memcmp(name, other.name, name_len) == 0 && |
| 6982 | signature == other.signature; |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 6983 | } |
| 6984 | }; |
| 6985 | struct EntryHash { |
| 6986 | size_t operator()(const Entry& key) const { |
| 6987 | return key.cached_hash; |
| 6988 | } |
| 6989 | }; |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 6990 | HashMap<Entry, int32_t, DefaultMapEmptyFn<Entry, int32_t>, EntryHash> map; |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 6991 | for (int32_t i = 0; i < num_entries; ++i) { |
| 6992 | // Can use Unchecked here as the first loop already ensured that the arrays are correct |
| 6993 | // wrt/ kPointerSize. |
| 6994 | ArtMethod* vtable_entry = vtable->GetElementPtrSizeUnchecked<ArtMethod*, kPointerSize>(i); |
| 6995 | // Don't bother if we cannot 'see' the vtable entry (i.e. it is a package-private member |
| 6996 | // maybe). |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6997 | if (!klass->CanAccessMember(vtable_entry->GetDeclaringClass(), |
| 6998 | vtable_entry->GetAccessFlags())) { |
| 6999 | continue; |
| 7000 | } |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 7001 | ArtMethod* m = vtable_entry->GetInterfaceMethodIfProxy(kPointerSize); |
| 7002 | const DexFile* dex_file = m->GetDexFile(); |
| 7003 | const dex::MethodId& mid = dex_file->GetMethodId(m->GetDexMethodIndex()); |
| 7004 | |
| 7005 | Entry e(dex_file, mid); |
| 7006 | |
| 7007 | size_t string_hash = std::hash<std::string_view>()(std::string_view(e.name, e.name_len)); |
| 7008 | size_t sig_hash = std::hash<std::string>()(e.signature.ToString()); |
| 7009 | e.cached_hash = BaseHashType::HashCombine(BaseHashType::HashCombine(0u, string_hash), |
| 7010 | sig_hash); |
| 7011 | |
| 7012 | auto it = map.find(e); |
| 7013 | if (it != map.end()) { |
| 7014 | log_fn(it->second, i); |
| 7015 | } else { |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 7016 | map.insert(std::make_pair(e, i)); |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 7017 | } |
| 7018 | } |
| 7019 | } |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 7020 | |
| 7021 | void CheckVTableHasNoDuplicates(Thread* self, |
| 7022 | Handle<mirror::Class> klass, |
| 7023 | PointerSize pointer_size) |
Andreas Gampe | a2fed08 | 2019-02-01 09:34:43 -0800 | [diff] [blame] | 7024 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 7025 | switch (pointer_size) { |
| 7026 | case PointerSize::k64: |
| 7027 | CheckVTableHasNoDuplicates<PointerSize::k64>(self, klass); |
| 7028 | break; |
| 7029 | case PointerSize::k32: |
| 7030 | CheckVTableHasNoDuplicates<PointerSize::k32>(self, klass); |
| 7031 | break; |
| 7032 | } |
| 7033 | } |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 7034 | |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 7035 | static void CheckVTable(Thread* self, Handle<mirror::Class> klass, PointerSize pointer_size) |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 7036 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 7037 | CheckClassOwnsVTableEntries(self, klass, pointer_size); |
| 7038 | CheckVTableHasNoDuplicates(self, klass, pointer_size); |
| 7039 | } |
| 7040 | |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 7041 | } // namespace |
| 7042 | |
Vladimir Marko | b91402f | 2021-12-21 15:55:06 +0000 | [diff] [blame] | 7043 | template <PointerSize kPointerSize> |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 7044 | class ClassLinker::LinkMethodsHelper { |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7045 | public: |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 7046 | LinkMethodsHelper(ClassLinker* class_linker, |
| 7047 | Handle<mirror::Class> klass, |
| 7048 | Thread* self, |
| 7049 | Runtime* runtime) |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7050 | : class_linker_(class_linker), |
| 7051 | klass_(klass), |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7052 | self_(self), |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 7053 | runtime_(runtime), |
Lokesh Gidra | 606bd94 | 2022-05-23 19:00:09 +0000 | [diff] [blame] | 7054 | stack_(runtime->GetArenaPool()), |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7055 | allocator_(&stack_), |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 7056 | copied_method_records_(copied_method_records_initial_buffer_, |
| 7057 | kCopiedMethodRecordInitialBufferSize, |
| 7058 | allocator_.Adapter()), |
| 7059 | num_new_copied_methods_(0u) { |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7060 | } |
| 7061 | |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 7062 | // Links the virtual and interface methods for the given class. |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 7063 | // |
| 7064 | // Arguments: |
| 7065 | // * self - The current thread. |
| 7066 | // * klass - class, whose vtable will be filled in. |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 7067 | // * interfaces - implemented interfaces for a proxy class, otherwise null. |
| 7068 | // * out_new_conflict - whether there is a new conflict compared to the superclass. |
| 7069 | // * out_imt - interface method table to fill. |
| 7070 | bool LinkMethods( |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 7071 | Thread* self, |
| 7072 | Handle<mirror::Class> klass, |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 7073 | Handle<mirror::ObjectArray<mirror::Class>> interfaces, |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 7074 | bool* out_new_conflict, |
| 7075 | ArtMethod** out_imt) |
| 7076 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 7077 | |
| 7078 | private: |
Vladimir Marko | 0441d20 | 2022-02-18 13:55:15 +0000 | [diff] [blame] | 7079 | // Allocate a pointer array. |
| 7080 | static ObjPtr<mirror::PointerArray> AllocPointerArray(Thread* self, size_t length) |
| 7081 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 7082 | |
| 7083 | // Allocate method arrays for interfaces. |
| 7084 | bool AllocateIfTableMethodArrays(Thread* self, |
| 7085 | Handle<mirror::Class> klass, |
| 7086 | Handle<mirror::IfTable> iftable) |
| 7087 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 7088 | |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7089 | // Assign vtable indexes to declared virtual methods for a non-interface class other |
| 7090 | // than `java.lang.Object`. Returns the number of vtable entries on success, 0 on failure. |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 7091 | // This function also assigns vtable indexes for interface methods in new interfaces |
| 7092 | // and records data for copied methods which shall be referenced by the vtable. |
Vladimir Marko | bed84ef | 2022-01-21 13:57:14 +0000 | [diff] [blame] | 7093 | size_t AssignVTableIndexes(ObjPtr<mirror::Class> klass, |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7094 | ObjPtr<mirror::Class> super_class, |
Vladimir Marko | 5171813 | 2022-02-07 16:31:08 +0000 | [diff] [blame] | 7095 | bool is_super_abstract, |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 7096 | size_t num_virtual_methods, |
| 7097 | ObjPtr<mirror::IfTable> iftable) |
| 7098 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 7099 | |
| 7100 | bool FindCopiedMethodsForInterface(ObjPtr<mirror::Class> klass, |
| 7101 | size_t num_virtual_methods, |
| 7102 | ObjPtr<mirror::IfTable> iftable) |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7103 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 7104 | |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 7105 | bool LinkJavaLangObjectMethods(Thread* self, Handle<mirror::Class> klass) |
Vladimir Marko | b91402f | 2021-12-21 15:55:06 +0000 | [diff] [blame] | 7106 | REQUIRES_SHARED(Locks::mutator_lock_) COLD_ATTR; |
| 7107 | |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 7108 | void ReallocMethods(ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_); |
Vladimir Marko | 0441d20 | 2022-02-18 13:55:15 +0000 | [diff] [blame] | 7109 | bool FinalizeIfTable(Handle<mirror::Class> klass, |
| 7110 | MutableHandle<mirror::IfTable> iftable, |
| 7111 | Handle<mirror::PointerArray> vtable, |
Vladimir Marko | 5171813 | 2022-02-07 16:31:08 +0000 | [diff] [blame] | 7112 | bool is_klass_abstract, |
| 7113 | bool is_super_abstract, |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 7114 | bool* out_new_conflict, |
| 7115 | ArtMethod** out_imt) |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 7116 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 7117 | |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7118 | void ClobberOldMethods(LengthPrefixedArray<ArtMethod>* old_methods, |
| 7119 | LengthPrefixedArray<ArtMethod>* methods) { |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 7120 | if (kIsDebugBuild && old_methods != nullptr) { |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7121 | CHECK(methods != nullptr); |
| 7122 | // Put some random garbage in old methods to help find stale pointers. |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 7123 | if (methods != old_methods) { |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7124 | // Need to make sure the GC is not running since it could be scanning the methods we are |
| 7125 | // about to overwrite. |
Vladimir Marko | ddf4fd3 | 2021-11-22 16:31:57 +0000 | [diff] [blame] | 7126 | ScopedThreadStateChange tsc(self_, ThreadState::kSuspended); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7127 | gc::ScopedGCCriticalSection gcs(self_, |
| 7128 | gc::kGcCauseClassLinker, |
| 7129 | gc::kCollectorTypeClassLinker); |
| 7130 | const size_t old_size = LengthPrefixedArray<ArtMethod>::ComputeSize(old_methods->size(), |
Vladimir Marko | b91402f | 2021-12-21 15:55:06 +0000 | [diff] [blame] | 7131 | kMethodSize, |
| 7132 | kMethodAlignment); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7133 | memset(old_methods, 0xFEu, old_size); |
Lokesh Gidra | 606bd94 | 2022-05-23 19:00:09 +0000 | [diff] [blame] | 7134 | // Set size to 0 to avoid visiting declaring classes. |
| 7135 | if (gUseUserfaultfd) { |
| 7136 | old_methods->SetSize(0); |
| 7137 | } |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7138 | } |
| 7139 | } |
| 7140 | } |
| 7141 | |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 7142 | NO_INLINE |
| 7143 | void LogNewVirtuals(LengthPrefixedArray<ArtMethod>* methods) const |
| 7144 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 7145 | ObjPtr<mirror::Class> klass = klass_.Get(); |
| 7146 | size_t num_new_copied_methods = num_new_copied_methods_; |
| 7147 | size_t old_method_count = methods->size() - num_new_copied_methods; |
| 7148 | size_t super_vtable_length = klass->GetSuperClass()->GetVTableLength(); |
| 7149 | size_t num_miranda_methods = 0u; |
| 7150 | size_t num_overriding_default_methods = 0u; |
| 7151 | size_t num_default_methods = 0u; |
| 7152 | size_t num_overriding_default_conflict_methods = 0u; |
| 7153 | size_t num_default_conflict_methods = 0u; |
| 7154 | for (size_t i = 0; i != num_new_copied_methods; ++i) { |
| 7155 | ArtMethod& m = methods->At(old_method_count + i, kMethodSize, kMethodAlignment); |
| 7156 | if (m.IsDefault()) { |
| 7157 | if (m.GetMethodIndexDuringLinking() < super_vtable_length) { |
| 7158 | ++num_overriding_default_methods; |
| 7159 | } else { |
| 7160 | ++num_default_methods; |
| 7161 | } |
| 7162 | } else if (m.IsDefaultConflicting()) { |
| 7163 | if (m.GetMethodIndexDuringLinking() < super_vtable_length) { |
| 7164 | ++num_overriding_default_conflict_methods; |
| 7165 | } else { |
| 7166 | ++num_default_conflict_methods; |
| 7167 | } |
| 7168 | } else { |
| 7169 | DCHECK(m.IsMiranda()); |
| 7170 | ++num_miranda_methods; |
| 7171 | } |
Vladimir Marko | 5cfb7bb | 2022-01-28 11:12:17 +0000 | [diff] [blame] | 7172 | } |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 7173 | VLOG(class_linker) << klass->PrettyClass() << ": miranda_methods=" << num_miranda_methods |
| 7174 | << " default_methods=" << num_default_methods |
| 7175 | << " overriding_default_methods=" << num_overriding_default_methods |
| 7176 | << " default_conflict_methods=" << num_default_conflict_methods |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7177 | << " overriding_default_conflict_methods=" |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 7178 | << num_overriding_default_conflict_methods; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7179 | } |
| 7180 | |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7181 | class MethodIndexEmptyFn { |
| 7182 | public: |
| 7183 | void MakeEmpty(uint32_t& item) const { |
| 7184 | item = dex::kDexNoIndex; |
| 7185 | } |
| 7186 | bool IsEmpty(const uint32_t& item) const { |
| 7187 | return item == dex::kDexNoIndex; |
| 7188 | } |
| 7189 | }; |
| 7190 | |
Vladimir Marko | 7ddae99 | 2022-01-18 14:27:20 +0000 | [diff] [blame] | 7191 | class VTableIndexCheckerDebug { |
| 7192 | protected: |
| 7193 | explicit VTableIndexCheckerDebug(size_t vtable_length) |
| 7194 | : vtable_length_(vtable_length) {} |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7195 | |
Vladimir Marko | 7ddae99 | 2022-01-18 14:27:20 +0000 | [diff] [blame] | 7196 | void CheckIndex(uint32_t index) const { |
| 7197 | CHECK_LT(index, vtable_length_); |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7198 | } |
| 7199 | |
| 7200 | private: |
Vladimir Marko | 7ddae99 | 2022-01-18 14:27:20 +0000 | [diff] [blame] | 7201 | uint32_t vtable_length_; |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7202 | }; |
| 7203 | |
Vladimir Marko | 7ddae99 | 2022-01-18 14:27:20 +0000 | [diff] [blame] | 7204 | class VTableIndexCheckerRelease { |
| 7205 | protected: |
| 7206 | explicit VTableIndexCheckerRelease(size_t vtable_length ATTRIBUTE_UNUSED) {} |
| 7207 | void CheckIndex(uint32_t index ATTRIBUTE_UNUSED) const {} |
| 7208 | }; |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7209 | |
Vladimir Marko | 7ddae99 | 2022-01-18 14:27:20 +0000 | [diff] [blame] | 7210 | using VTableIndexChecker = |
| 7211 | std::conditional_t<kIsDebugBuild, VTableIndexCheckerDebug, VTableIndexCheckerRelease>; |
| 7212 | |
| 7213 | class VTableAccessor : private VTableIndexChecker { |
| 7214 | public: |
| 7215 | VTableAccessor(uint8_t* raw_vtable, size_t vtable_length) |
| 7216 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 7217 | : VTableIndexChecker(vtable_length), |
| 7218 | raw_vtable_(raw_vtable) {} |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7219 | |
| 7220 | ArtMethod* GetVTableEntry(uint32_t index) const REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | 7ddae99 | 2022-01-18 14:27:20 +0000 | [diff] [blame] | 7221 | this->CheckIndex(index); |
| 7222 | uint8_t* entry = raw_vtable_ + static_cast<size_t>(kPointerSize) * index; |
| 7223 | if (kPointerSize == PointerSize::k64) { |
| 7224 | return reinterpret_cast64<ArtMethod*>(*reinterpret_cast<uint64_t*>(entry)); |
| 7225 | } else { |
| 7226 | return reinterpret_cast32<ArtMethod*>(*reinterpret_cast<uint32_t*>(entry)); |
| 7227 | } |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7228 | } |
| 7229 | |
| 7230 | private: |
Vladimir Marko | 7ddae99 | 2022-01-18 14:27:20 +0000 | [diff] [blame] | 7231 | uint8_t* raw_vtable_; |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7232 | }; |
| 7233 | |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7234 | class VTableSignatureHash { |
| 7235 | public: |
Vladimir Marko | 7ddae99 | 2022-01-18 14:27:20 +0000 | [diff] [blame] | 7236 | explicit VTableSignatureHash(VTableAccessor accessor) |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7237 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 7238 | : accessor_(accessor) {} |
| 7239 | |
| 7240 | // NO_THREAD_SAFETY_ANALYSIS: This is called from unannotated `HashSet<>` functions. |
| 7241 | size_t operator()(ArtMethod* method) const NO_THREAD_SAFETY_ANALYSIS { |
| 7242 | return ComputeMethodHash(method); |
| 7243 | } |
| 7244 | |
| 7245 | // NO_THREAD_SAFETY_ANALYSIS: This is called from unannotated `HashSet<>` functions. |
| 7246 | size_t operator()(uint32_t index) const NO_THREAD_SAFETY_ANALYSIS { |
| 7247 | return ComputeMethodHash(accessor_.GetVTableEntry(index)); |
| 7248 | } |
| 7249 | |
| 7250 | private: |
Vladimir Marko | 7ddae99 | 2022-01-18 14:27:20 +0000 | [diff] [blame] | 7251 | VTableAccessor accessor_; |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7252 | }; |
| 7253 | |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7254 | class VTableSignatureEqual { |
| 7255 | public: |
Vladimir Marko | 7ddae99 | 2022-01-18 14:27:20 +0000 | [diff] [blame] | 7256 | explicit VTableSignatureEqual(VTableAccessor accessor) |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7257 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 7258 | : accessor_(accessor) {} |
| 7259 | |
| 7260 | // NO_THREAD_SAFETY_ANALYSIS: This is called from unannotated `HashSet<>` functions. |
| 7261 | bool operator()(uint32_t lhs_index, ArtMethod* rhs) const NO_THREAD_SAFETY_ANALYSIS { |
Vladimir Marko | bed84ef | 2022-01-21 13:57:14 +0000 | [diff] [blame] | 7262 | return MethodSignatureEquals(accessor_.GetVTableEntry(lhs_index), rhs); |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7263 | } |
| 7264 | |
| 7265 | // NO_THREAD_SAFETY_ANALYSIS: This is called from unannotated `HashSet<>` functions. |
| 7266 | bool operator()(uint32_t lhs_index, uint32_t rhs_index) const NO_THREAD_SAFETY_ANALYSIS { |
| 7267 | return (*this)(lhs_index, accessor_.GetVTableEntry(rhs_index)); |
| 7268 | } |
| 7269 | |
| 7270 | private: |
Vladimir Marko | 7ddae99 | 2022-01-18 14:27:20 +0000 | [diff] [blame] | 7271 | VTableAccessor accessor_; |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7272 | }; |
| 7273 | |
Vladimir Marko | 7ddae99 | 2022-01-18 14:27:20 +0000 | [diff] [blame] | 7274 | using VTableSignatureSet = |
| 7275 | ScopedArenaHashSet<uint32_t, MethodIndexEmptyFn, VTableSignatureHash, VTableSignatureEqual>; |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7276 | |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 7277 | class DeclaredVirtualSignatureHash { |
| 7278 | public: |
| 7279 | explicit DeclaredVirtualSignatureHash(ObjPtr<mirror::Class> klass) |
| 7280 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 7281 | : klass_(klass) {} |
| 7282 | |
| 7283 | // NO_THREAD_SAFETY_ANALYSIS: This is called from unannotated `HashSet<>` functions. |
| 7284 | size_t operator()(ArtMethod* method) const NO_THREAD_SAFETY_ANALYSIS { |
| 7285 | return ComputeMethodHash(method); |
| 7286 | } |
| 7287 | |
| 7288 | // NO_THREAD_SAFETY_ANALYSIS: This is called from unannotated `HashSet<>` functions. |
| 7289 | size_t operator()(uint32_t index) const NO_THREAD_SAFETY_ANALYSIS { |
| 7290 | DCHECK_LT(index, klass_->NumDeclaredVirtualMethods()); |
| 7291 | ArtMethod* method = klass_->GetVirtualMethodDuringLinking(index, kPointerSize); |
| 7292 | return ComputeMethodHash(method->GetInterfaceMethodIfProxy(kPointerSize)); |
| 7293 | } |
| 7294 | |
| 7295 | private: |
| 7296 | ObjPtr<mirror::Class> klass_; |
| 7297 | }; |
| 7298 | |
| 7299 | class DeclaredVirtualSignatureEqual { |
| 7300 | public: |
| 7301 | explicit DeclaredVirtualSignatureEqual(ObjPtr<mirror::Class> klass) |
| 7302 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 7303 | : klass_(klass) {} |
| 7304 | |
| 7305 | // NO_THREAD_SAFETY_ANALYSIS: This is called from unannotated `HashSet<>` functions. |
| 7306 | bool operator()(uint32_t lhs_index, ArtMethod* rhs) const NO_THREAD_SAFETY_ANALYSIS { |
| 7307 | DCHECK_LT(lhs_index, klass_->NumDeclaredVirtualMethods()); |
| 7308 | ArtMethod* lhs = klass_->GetVirtualMethodDuringLinking(lhs_index, kPointerSize); |
| 7309 | return MethodSignatureEquals(lhs->GetInterfaceMethodIfProxy(kPointerSize), rhs); |
| 7310 | } |
| 7311 | |
| 7312 | // NO_THREAD_SAFETY_ANALYSIS: This is called from unannotated `HashSet<>` functions. |
| 7313 | bool operator()(uint32_t lhs_index, uint32_t rhs_index) const NO_THREAD_SAFETY_ANALYSIS { |
| 7314 | DCHECK_LT(lhs_index, klass_->NumDeclaredVirtualMethods()); |
| 7315 | DCHECK_LT(rhs_index, klass_->NumDeclaredVirtualMethods()); |
| 7316 | return lhs_index == rhs_index; |
| 7317 | } |
| 7318 | |
| 7319 | private: |
| 7320 | ObjPtr<mirror::Class> klass_; |
| 7321 | }; |
| 7322 | |
| 7323 | using DeclaredVirtualSignatureSet = ScopedArenaHashSet<uint32_t, |
| 7324 | MethodIndexEmptyFn, |
| 7325 | DeclaredVirtualSignatureHash, |
| 7326 | DeclaredVirtualSignatureEqual>; |
| 7327 | |
| 7328 | // Helper class to keep records for determining the correct copied method to create. |
| 7329 | class CopiedMethodRecord { |
| 7330 | public: |
| 7331 | enum class State : uint32_t { |
| 7332 | // Note: The `*Single` values are used when we know that there is only one interface |
| 7333 | // method with the given signature that's not masked; that method is the main method. |
| 7334 | // We use this knowledge for faster masking check, otherwise we need to search for |
| 7335 | // a masking method through methods of all interfaces that could potentially mask it. |
| 7336 | kAbstractSingle, |
| 7337 | kDefaultSingle, |
| 7338 | kAbstract, |
| 7339 | kDefault, |
| 7340 | kDefaultConflict, |
| 7341 | kUseSuperMethod, |
| 7342 | }; |
| 7343 | |
| 7344 | CopiedMethodRecord() |
| 7345 | : main_method_(nullptr), |
| 7346 | method_index_(0u), |
| 7347 | state_(State::kAbstractSingle) {} |
| 7348 | |
| 7349 | CopiedMethodRecord(ArtMethod* main_method, size_t vtable_index) |
| 7350 | : main_method_(main_method), |
| 7351 | method_index_(vtable_index), |
| 7352 | state_(State::kAbstractSingle) {} |
| 7353 | |
| 7354 | // Set main method. The new main method must be more specific implementation. |
| 7355 | void SetMainMethod(ArtMethod* main_method) { |
| 7356 | DCHECK(main_method_ != nullptr); |
| 7357 | main_method_ = main_method; |
| 7358 | } |
| 7359 | |
| 7360 | // The main method is the first encountered default method if any, |
| 7361 | // otherwise the first encountered abstract method. |
| 7362 | ArtMethod* GetMainMethod() const { |
| 7363 | return main_method_; |
| 7364 | } |
| 7365 | |
| 7366 | void SetMethodIndex(size_t method_index) { |
| 7367 | DCHECK_NE(method_index, dex::kDexNoIndex); |
| 7368 | method_index_ = method_index; |
| 7369 | } |
| 7370 | |
| 7371 | size_t GetMethodIndex() const { |
| 7372 | DCHECK_NE(method_index_, dex::kDexNoIndex); |
| 7373 | return method_index_; |
| 7374 | } |
| 7375 | |
| 7376 | void SetState(State state) { |
| 7377 | state_ = state; |
| 7378 | } |
| 7379 | |
| 7380 | State GetState() const { |
| 7381 | return state_; |
| 7382 | } |
| 7383 | |
| 7384 | ALWAYS_INLINE |
| 7385 | void UpdateStateForInterface(ObjPtr<mirror::Class> iface, |
| 7386 | ArtMethod* interface_method, |
| 7387 | ObjPtr<mirror::IfTable> iftable, |
| 7388 | size_t ifcount, |
| 7389 | size_t index) |
| 7390 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 7391 | DCHECK_EQ(ifcount, iftable->Count()); |
| 7392 | DCHECK_LT(index, ifcount); |
| 7393 | DCHECK(iface == interface_method->GetDeclaringClass()); |
| 7394 | DCHECK(iface == iftable->GetInterface(index)); |
| 7395 | DCHECK(interface_method->IsDefault()); |
| 7396 | if (GetState() != State::kDefaultConflict) { |
| 7397 | DCHECK(GetState() == State::kDefault); |
| 7398 | // We do not record all overriding methods, so we need to walk over all |
| 7399 | // interfaces that could mask the `interface_method`. |
| 7400 | if (ContainsOverridingMethodOf(iftable, index + 1, ifcount, iface, interface_method)) { |
| 7401 | return; // Found an overriding method that masks `interface_method`. |
| 7402 | } |
| 7403 | // We have a new default method that's not masked by any other method. |
| 7404 | SetState(State::kDefaultConflict); |
| 7405 | } |
| 7406 | } |
| 7407 | |
| 7408 | ALWAYS_INLINE |
| 7409 | void UpdateState(ObjPtr<mirror::Class> iface, |
| 7410 | ArtMethod* interface_method, |
| 7411 | size_t vtable_index, |
| 7412 | ObjPtr<mirror::IfTable> iftable, |
| 7413 | size_t ifcount, |
| 7414 | size_t index) |
| 7415 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 7416 | DCHECK_EQ(ifcount, iftable->Count()); |
| 7417 | DCHECK_LT(index, ifcount); |
| 7418 | if (kIsDebugBuild) { |
| 7419 | if (interface_method->IsCopied()) { |
| 7420 | // Called from `FinalizeState()` for a default method from superclass. |
| 7421 | // The `index` points to the last interface inherited from the superclass |
| 7422 | // as we need to search only the new interfaces for masking methods. |
| 7423 | DCHECK(interface_method->IsDefault()); |
| 7424 | } else { |
| 7425 | DCHECK(iface == interface_method->GetDeclaringClass()); |
| 7426 | DCHECK(iface == iftable->GetInterface(index)); |
| 7427 | } |
| 7428 | } |
| 7429 | DCHECK_EQ(vtable_index, method_index_); |
| 7430 | auto slow_is_masked = [=]() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 7431 | return ContainsImplementingMethod(iftable, index + 1, ifcount, iface, vtable_index); |
| 7432 | }; |
| 7433 | UpdateStateImpl(iface, interface_method, slow_is_masked); |
| 7434 | } |
| 7435 | |
| 7436 | ALWAYS_INLINE |
| 7437 | void FinalizeState(ArtMethod* super_method, |
| 7438 | size_t vtable_index, |
| 7439 | ObjPtr<mirror::IfTable> iftable, |
| 7440 | size_t ifcount, |
| 7441 | ObjPtr<mirror::IfTable> super_iftable, |
| 7442 | size_t super_ifcount) |
| 7443 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 7444 | DCHECK(super_method->IsCopied()); |
| 7445 | DCHECK_EQ(vtable_index, method_index_); |
| 7446 | DCHECK_EQ(vtable_index, super_method->GetMethodIndex()); |
| 7447 | DCHECK_NE(super_ifcount, 0u); |
| 7448 | if (super_method->IsDefault()) { |
| 7449 | if (UNLIKELY(super_method->IsDefaultConflicting())) { |
| 7450 | // Some of the default methods that contributed to the conflict in the superclass |
| 7451 | // may be masked by new interfaces. Walk over all the interfaces and update state |
| 7452 | // as long as the current state is not `kDefaultConflict`. |
| 7453 | size_t i = super_ifcount; |
| 7454 | while (GetState() != State::kDefaultConflict && i != 0u) { |
| 7455 | --i; |
| 7456 | ObjPtr<mirror::Class> iface = iftable->GetInterface(i); |
| 7457 | DCHECK(iface == super_iftable->GetInterface(i)); |
| 7458 | auto [found, index] = |
| 7459 | MethodArrayContains(super_iftable->GetMethodArrayOrNull(i), super_method); |
| 7460 | if (found) { |
| 7461 | ArtMethod* interface_method = iface->GetVirtualMethod(index, kPointerSize); |
| 7462 | auto slow_is_masked = [=]() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 7463 | // Note: The `iftable` has method arrays in range [super_ifcount, ifcount) filled |
| 7464 | // with vtable indexes but the range [0, super_ifcount) is empty, so we need to |
| 7465 | // use the `super_iftable` filled with implementation methods for that range. |
| 7466 | return ContainsImplementingMethod( |
| 7467 | super_iftable, i + 1u, super_ifcount, iface, super_method) || |
| 7468 | ContainsImplementingMethod( |
| 7469 | iftable, super_ifcount, ifcount, iface, vtable_index); |
| 7470 | }; |
| 7471 | UpdateStateImpl(iface, interface_method, slow_is_masked); |
| 7472 | } |
| 7473 | } |
| 7474 | if (GetState() == State::kDefaultConflict) { |
| 7475 | SetState(State::kUseSuperMethod); |
| 7476 | } |
| 7477 | } else { |
| 7478 | // There was exactly one default method in superclass interfaces that was |
| 7479 | // not masked by subinterfaces. Use `UpdateState()` to process it and pass |
| 7480 | // `super_ifcount - 1` as index for checking if it's been masked by new interfaces. |
| 7481 | ObjPtr<mirror::Class> iface = super_method->GetDeclaringClass(); |
| 7482 | UpdateState( |
| 7483 | iface, super_method, vtable_index, iftable, ifcount, /*index=*/ super_ifcount - 1u); |
| 7484 | if (GetMainMethod() == super_method) { |
| 7485 | DCHECK(GetState() == State::kDefault) << enum_cast<uint32_t>(GetState()); |
| 7486 | SetState(State::kUseSuperMethod); |
| 7487 | } |
| 7488 | } |
| 7489 | } else { |
| 7490 | DCHECK(super_method->IsMiranda()); |
| 7491 | // Any default methods with this signature in superclass interfaces have been |
| 7492 | // masked by subinterfaces. Check if we can reuse the miranda method. |
| 7493 | if (GetState() == State::kAbstractSingle || GetState() == State::kAbstract) { |
| 7494 | SetState(State::kUseSuperMethod); |
| 7495 | } |
| 7496 | } |
| 7497 | } |
| 7498 | |
| 7499 | private: |
| 7500 | template <typename Predicate> |
| 7501 | ALWAYS_INLINE |
| 7502 | void UpdateStateImpl(ObjPtr<mirror::Class> iface, |
| 7503 | ArtMethod* interface_method, |
| 7504 | Predicate&& slow_is_masked) |
| 7505 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 7506 | bool have_default = false; |
| 7507 | switch (GetState()) { |
| 7508 | case State::kDefaultSingle: |
| 7509 | have_default = true; |
| 7510 | FALLTHROUGH_INTENDED; |
| 7511 | case State::kAbstractSingle: |
| 7512 | if (GetMainMethod()->GetDeclaringClass()->Implements(iface)) { |
| 7513 | return; // The main method masks the `interface_method`. |
| 7514 | } |
| 7515 | if (!interface_method->IsDefault()) { |
| 7516 | SetState(have_default ? State::kDefault : State::kAbstract); |
| 7517 | return; |
| 7518 | } |
| 7519 | break; |
| 7520 | case State::kDefault: |
| 7521 | have_default = true; |
| 7522 | FALLTHROUGH_INTENDED; |
| 7523 | case State::kAbstract: |
| 7524 | if (!interface_method->IsDefault()) { |
| 7525 | return; // Keep the same state. We do not need to check for masking. |
| 7526 | } |
| 7527 | // We do not record all overriding methods, so we need to walk over all |
| 7528 | // interfaces that could mask the `interface_method`. The provided |
| 7529 | // predicate `slow_is_masked()` does that. |
| 7530 | if (slow_is_masked()) { |
| 7531 | return; // Found an overriding method that masks `interface_method`. |
| 7532 | } |
| 7533 | break; |
| 7534 | case State::kDefaultConflict: |
| 7535 | return; // The state cannot change anymore. |
| 7536 | default: |
| 7537 | LOG(FATAL) << "Unexpected state: " << enum_cast<uint32_t>(GetState()); |
| 7538 | UNREACHABLE(); |
| 7539 | } |
| 7540 | // We have a new default method that's not masked by any other method. |
| 7541 | DCHECK(interface_method->IsDefault()); |
| 7542 | if (have_default) { |
| 7543 | SetState(State::kDefaultConflict); |
| 7544 | } else { |
| 7545 | SetMainMethod(interface_method); |
| 7546 | SetState(State::kDefault); |
| 7547 | } |
| 7548 | } |
| 7549 | |
| 7550 | // Determine if the given `iftable` contains in the given range a subinterface of `iface` |
| 7551 | // that declares a method with the same name and signature as 'interface_method'. |
| 7552 | // |
| 7553 | // Arguments |
| 7554 | // - iftable: The iftable we are searching for an overriding method. |
| 7555 | // - begin: The start of the range to search. |
| 7556 | // - end: The end of the range to search. |
| 7557 | // - iface: The interface we are checking to see if anything overrides. |
| 7558 | // - interface_method: |
| 7559 | // The interface method providing a name and signature we're searching for. |
| 7560 | // |
| 7561 | // Returns whether an overriding method was found in any subinterface of `iface`. |
| 7562 | static bool ContainsOverridingMethodOf(ObjPtr<mirror::IfTable> iftable, |
| 7563 | size_t begin, |
| 7564 | size_t end, |
| 7565 | ObjPtr<mirror::Class> iface, |
| 7566 | ArtMethod* interface_method) |
| 7567 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 7568 | for (size_t i = begin; i != end; ++i) { |
| 7569 | ObjPtr<mirror::Class> current_iface = iftable->GetInterface(i); |
| 7570 | for (ArtMethod& current_method : current_iface->GetDeclaredVirtualMethods(kPointerSize)) { |
| 7571 | if (MethodSignatureEquals(¤t_method, interface_method)) { |
| 7572 | // Check if the i'th interface is a subtype of this one. |
| 7573 | if (current_iface->Implements(iface)) { |
| 7574 | return true; |
| 7575 | } |
| 7576 | break; |
| 7577 | } |
| 7578 | } |
| 7579 | } |
| 7580 | return false; |
| 7581 | } |
| 7582 | |
| 7583 | // Determine if the given `iftable` contains in the given range a subinterface of `iface` |
| 7584 | // that declares a method implemented by 'target'. This is an optimized version of |
| 7585 | // `ContainsOverridingMethodOf()` that searches implementation method arrays instead |
| 7586 | // of comparing signatures for declared interface methods. |
| 7587 | // |
| 7588 | // Arguments |
| 7589 | // - iftable: The iftable we are searching for an overriding method. |
| 7590 | // - begin: The start of the range to search. |
| 7591 | // - end: The end of the range to search. |
| 7592 | // - iface: The interface we are checking to see if anything overrides. |
| 7593 | // - target: The implementation method we're searching for. |
| 7594 | // Note that the new `iftable` is filled with vtable indexes for new interfaces, |
| 7595 | // so this needs to be the vtable index if we're searching that range. |
| 7596 | // |
| 7597 | // Returns whether the `target` was found in a method array for any subinterface of `iface`. |
| 7598 | template <typename TargetType> |
| 7599 | static bool ContainsImplementingMethod(ObjPtr<mirror::IfTable> iftable, |
| 7600 | size_t begin, |
| 7601 | size_t end, |
| 7602 | ObjPtr<mirror::Class> iface, |
| 7603 | TargetType target) |
| 7604 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 7605 | for (size_t i = begin; i != end; ++i) { |
| 7606 | if (MethodArrayContains(iftable->GetMethodArrayOrNull(i), target).first && |
| 7607 | iftable->GetInterface(i)->Implements(iface)) { |
| 7608 | return true; |
| 7609 | } |
| 7610 | } |
| 7611 | return false; |
| 7612 | } |
| 7613 | |
| 7614 | template <typename TargetType> |
| 7615 | static std::pair<bool, size_t> MethodArrayContains(ObjPtr<mirror::PointerArray> method_array, |
| 7616 | TargetType target) |
| 7617 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 7618 | size_t num_methods = (method_array != nullptr) ? method_array->GetLength() : 0u; |
| 7619 | for (size_t j = 0; j != num_methods; ++j) { |
| 7620 | if (method_array->GetElementPtrSize<TargetType, kPointerSize>(j) == target) { |
| 7621 | return {true, j}; |
| 7622 | } |
| 7623 | } |
| 7624 | return {false, 0}; |
| 7625 | } |
| 7626 | |
| 7627 | ArtMethod* main_method_; |
| 7628 | uint32_t method_index_; |
| 7629 | State state_; |
| 7630 | }; |
| 7631 | |
| 7632 | class CopiedMethodRecordEmptyFn { |
| 7633 | public: |
| 7634 | void MakeEmpty(CopiedMethodRecord& item) const { |
| 7635 | item = CopiedMethodRecord(); |
| 7636 | } |
| 7637 | bool IsEmpty(const CopiedMethodRecord& item) const { |
| 7638 | return item.GetMainMethod() == nullptr; |
| 7639 | } |
| 7640 | }; |
| 7641 | |
| 7642 | class CopiedMethodRecordHash { |
| 7643 | public: |
| 7644 | // NO_THREAD_SAFETY_ANALYSIS: This is called from unannotated `HashSet<>` functions. |
| 7645 | size_t operator()(ArtMethod* method) const NO_THREAD_SAFETY_ANALYSIS { |
| 7646 | DCHECK(method != nullptr); |
| 7647 | return ComputeMethodHash(method); |
| 7648 | } |
| 7649 | |
| 7650 | // NO_THREAD_SAFETY_ANALYSIS: This is called from unannotated `HashSet<>` functions. |
| 7651 | size_t operator()(const CopiedMethodRecord& record) const NO_THREAD_SAFETY_ANALYSIS { |
| 7652 | return (*this)(record.GetMainMethod()); |
| 7653 | } |
| 7654 | }; |
| 7655 | |
| 7656 | class CopiedMethodRecordEqual { |
| 7657 | public: |
| 7658 | // NO_THREAD_SAFETY_ANALYSIS: This is called from unannotated `HashSet<>` functions. |
| 7659 | bool operator()(const CopiedMethodRecord& lhs_record, |
| 7660 | ArtMethod* rhs) const NO_THREAD_SAFETY_ANALYSIS { |
| 7661 | ArtMethod* lhs = lhs_record.GetMainMethod(); |
| 7662 | DCHECK(lhs != nullptr); |
| 7663 | DCHECK(rhs != nullptr); |
| 7664 | return MethodSignatureEquals(lhs, rhs); |
| 7665 | } |
| 7666 | |
| 7667 | // NO_THREAD_SAFETY_ANALYSIS: This is called from unannotated `HashSet<>` functions. |
| 7668 | bool operator()(const CopiedMethodRecord& lhs_record, |
| 7669 | const CopiedMethodRecord& rhs_record) const NO_THREAD_SAFETY_ANALYSIS { |
| 7670 | return (*this)(lhs_record, rhs_record.GetMainMethod()); |
| 7671 | } |
| 7672 | }; |
| 7673 | |
| 7674 | using CopiedMethodRecordSet = ScopedArenaHashSet<CopiedMethodRecord, |
| 7675 | CopiedMethodRecordEmptyFn, |
| 7676 | CopiedMethodRecordHash, |
| 7677 | CopiedMethodRecordEqual>; |
| 7678 | |
Vladimir Marko | b91402f | 2021-12-21 15:55:06 +0000 | [diff] [blame] | 7679 | static constexpr size_t kMethodAlignment = ArtMethod::Alignment(kPointerSize); |
| 7680 | static constexpr size_t kMethodSize = ArtMethod::Size(kPointerSize); |
| 7681 | |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7682 | ClassLinker* class_linker_; |
| 7683 | Handle<mirror::Class> klass_; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7684 | Thread* const self_; |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 7685 | Runtime* const runtime_; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7686 | |
| 7687 | // These are allocated on the heap to begin, we then transfer to linear alloc when we re-create |
| 7688 | // the virtual methods array. |
| 7689 | // Need to use low 4GB arenas for compiler or else the pointers wont fit in 32 bit method array |
| 7690 | // during cross compilation. |
| 7691 | // Use the linear alloc pool since this one is in the low 4gb for the compiler. |
| 7692 | ArenaStack stack_; |
| 7693 | ScopedArenaAllocator allocator_; |
| 7694 | |
Vladimir Marko | d5d11d9 | 2021-02-02 16:24:25 +0000 | [diff] [blame] | 7695 | // If there are multiple methods with the same signature in the superclass vtable |
| 7696 | // (which can happen with a new virtual method having the same signature as an |
| 7697 | // inaccessible package-private method from another package in the superclass), |
| 7698 | // we keep singly-linked lists in this single array that maps vtable index to the |
| 7699 | // next vtable index in the list, `dex::kDexNoIndex` denotes the end of a list. |
| 7700 | ArrayRef<uint32_t> same_signature_vtable_lists_; |
| 7701 | |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 7702 | // Avoid large allocation for a few copied method records. |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 7703 | // Keep the initial buffer on the stack to avoid arena allocations |
| 7704 | // if there are no special cases (the first arena allocation is costly). |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 7705 | static constexpr size_t kCopiedMethodRecordInitialBufferSize = 16u; |
| 7706 | CopiedMethodRecord copied_method_records_initial_buffer_[kCopiedMethodRecordInitialBufferSize]; |
| 7707 | CopiedMethodRecordSet copied_method_records_; |
| 7708 | size_t num_new_copied_methods_; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7709 | }; |
| 7710 | |
Vladimir Marko | b91402f | 2021-12-21 15:55:06 +0000 | [diff] [blame] | 7711 | template <PointerSize kPointerSize> |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 7712 | NO_INLINE |
| 7713 | void ClassLinker::LinkMethodsHelper<kPointerSize>::ReallocMethods(ObjPtr<mirror::Class> klass) { |
| 7714 | // There should be no thread suspension in this function, |
| 7715 | // native allocations do not cause thread suspension. |
| 7716 | ScopedAssertNoThreadSuspension sants(__FUNCTION__); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7717 | |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 7718 | size_t num_new_copied_methods = num_new_copied_methods_; |
| 7719 | DCHECK_NE(num_new_copied_methods, 0u); |
| 7720 | const size_t old_method_count = klass->NumMethods(); |
| 7721 | const size_t new_method_count = old_method_count + num_new_copied_methods; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7722 | |
| 7723 | // Attempt to realloc to save RAM if possible. |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 7724 | LengthPrefixedArray<ArtMethod>* old_methods = klass->GetMethodsPtr(); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7725 | // The Realloced virtual methods aren't visible from the class roots, so there is no issue |
| 7726 | // where GCs could attempt to mark stale pointers due to memcpy. And since we overwrite the |
| 7727 | // realloced memory with out->CopyFrom, we are guaranteed to have objects in the to space since |
| 7728 | // CopyFrom has internal read barriers. |
| 7729 | // |
| 7730 | // TODO We should maybe move some of this into mirror::Class or at least into another method. |
| 7731 | const size_t old_size = LengthPrefixedArray<ArtMethod>::ComputeSize(old_method_count, |
Vladimir Marko | b91402f | 2021-12-21 15:55:06 +0000 | [diff] [blame] | 7732 | kMethodSize, |
| 7733 | kMethodAlignment); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7734 | const size_t new_size = LengthPrefixedArray<ArtMethod>::ComputeSize(new_method_count, |
Vladimir Marko | b91402f | 2021-12-21 15:55:06 +0000 | [diff] [blame] | 7735 | kMethodSize, |
| 7736 | kMethodAlignment); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7737 | const size_t old_methods_ptr_size = (old_methods != nullptr) ? old_size : 0; |
| 7738 | auto* methods = reinterpret_cast<LengthPrefixedArray<ArtMethod>*>( |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 7739 | class_linker_->GetAllocatorForClassLoader(klass->GetClassLoader())->Realloc( |
Lokesh Gidra | 606bd94 | 2022-05-23 19:00:09 +0000 | [diff] [blame] | 7740 | self_, old_methods, old_methods_ptr_size, new_size, LinearAllocKind::kArtMethodArray)); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7741 | CHECK(methods != nullptr); // Native allocation failure aborts. |
| 7742 | |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7743 | if (methods != old_methods) { |
Lokesh Gidra | 606bd94 | 2022-05-23 19:00:09 +0000 | [diff] [blame] | 7744 | if (gUseReadBarrier) { |
| 7745 | StrideIterator<ArtMethod> out = methods->begin(kMethodSize, kMethodAlignment); |
| 7746 | // Copy over the old methods. The `ArtMethod::CopyFrom()` is only necessary to not miss |
| 7747 | // read barriers since `LinearAlloc::Realloc()` won't do read barriers when it copies. |
| 7748 | for (auto& m : klass->GetMethods(kPointerSize)) { |
| 7749 | out->CopyFrom(&m, kPointerSize); |
| 7750 | ++out; |
| 7751 | } |
| 7752 | } else if (gUseUserfaultfd) { |
| 7753 | // Clear the declaring class of the old dangling method array so that GC doesn't |
| 7754 | // try to update them, which could cause crashes in userfaultfd GC due to |
| 7755 | // checks in post-compact address computation. |
| 7756 | for (auto& m : klass->GetMethods(kPointerSize)) { |
| 7757 | m.SetDeclaringClass(nullptr); |
| 7758 | } |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7759 | } |
| 7760 | } |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 7761 | |
| 7762 | // Collect and sort copied method records by the vtable index. This places overriding |
| 7763 | // copied methods first, sorted by the vtable index already assigned in the superclass, |
| 7764 | // followed by copied methods with new signatures in the order in which we encountered |
| 7765 | // them when going over virtual methods of new interfaces. |
| 7766 | // This order is deterministic but implementation-defined. |
| 7767 | // |
| 7768 | // Avoid arena allocation for a few records (the first arena allocation is costly). |
| 7769 | constexpr size_t kSortedRecordsBufferSize = 16; |
| 7770 | CopiedMethodRecord* sorted_records_buffer[kSortedRecordsBufferSize]; |
| 7771 | CopiedMethodRecord** sorted_records = (num_new_copied_methods <= kSortedRecordsBufferSize) |
| 7772 | ? sorted_records_buffer |
| 7773 | : allocator_.AllocArray<CopiedMethodRecord*>(num_new_copied_methods); |
| 7774 | size_t filled_sorted_records = 0u; |
| 7775 | for (CopiedMethodRecord& record : copied_method_records_) { |
| 7776 | if (record.GetState() != CopiedMethodRecord::State::kUseSuperMethod) { |
| 7777 | DCHECK_LT(filled_sorted_records, num_new_copied_methods); |
| 7778 | sorted_records[filled_sorted_records] = &record; |
| 7779 | ++filled_sorted_records; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7780 | } |
| 7781 | } |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 7782 | DCHECK_EQ(filled_sorted_records, num_new_copied_methods); |
| 7783 | std::sort(sorted_records, |
| 7784 | sorted_records + num_new_copied_methods, |
| 7785 | [](const CopiedMethodRecord* lhs, const CopiedMethodRecord* rhs) { |
| 7786 | return lhs->GetMethodIndex() < rhs->GetMethodIndex(); |
| 7787 | }); |
| 7788 | |
| 7789 | if (klass->IsInterface()) { |
| 7790 | // Some records may have been pruned. Update method indexes in collected records. |
| 7791 | size_t interface_method_index = klass->NumDeclaredVirtualMethods(); |
| 7792 | for (size_t i = 0; i != num_new_copied_methods; ++i) { |
| 7793 | CopiedMethodRecord* record = sorted_records[i]; |
| 7794 | DCHECK_LE(interface_method_index, record->GetMethodIndex()); |
| 7795 | record->SetMethodIndex(interface_method_index); |
| 7796 | ++interface_method_index; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7797 | } |
| 7798 | } |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 7799 | |
| 7800 | // Add copied methods. |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7801 | methods->SetSize(new_method_count); |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 7802 | for (size_t i = 0; i != num_new_copied_methods; ++i) { |
| 7803 | const CopiedMethodRecord* record = sorted_records[i]; |
| 7804 | ArtMethod* interface_method = record->GetMainMethod(); |
| 7805 | DCHECK(!interface_method->IsCopied()); |
| 7806 | ArtMethod& new_method = methods->At(old_method_count + i, kMethodSize, kMethodAlignment); |
| 7807 | new_method.CopyFrom(interface_method, kPointerSize); |
| 7808 | new_method.SetMethodIndex(dchecked_integral_cast<uint16_t>(record->GetMethodIndex())); |
| 7809 | switch (record->GetState()) { |
| 7810 | case CopiedMethodRecord::State::kAbstractSingle: |
| 7811 | case CopiedMethodRecord::State::kAbstract: { |
| 7812 | DCHECK(!klass->IsInterface()); // We do not create miranda methods for interfaces. |
| 7813 | uint32_t access_flags = new_method.GetAccessFlags(); |
| 7814 | DCHECK_EQ(access_flags & (kAccAbstract | kAccIntrinsic | kAccDefault), kAccAbstract) |
| 7815 | << "Miranda method should be abstract but not intrinsic or default!"; |
| 7816 | new_method.SetAccessFlags(access_flags | kAccCopied); |
| 7817 | break; |
| 7818 | } |
| 7819 | case CopiedMethodRecord::State::kDefaultSingle: |
| 7820 | case CopiedMethodRecord::State::kDefault: { |
| 7821 | DCHECK(!klass->IsInterface()); // We do not copy default methods for interfaces. |
| 7822 | // Clear the kAccSkipAccessChecks flag if it is present. Since this class hasn't been |
| 7823 | // verified yet it shouldn't have methods that are skipping access checks. |
| 7824 | // TODO This is rather arbitrary. We should maybe support classes where only some of its |
| 7825 | // methods are skip_access_checks. |
| 7826 | DCHECK_EQ(new_method.GetAccessFlags() & kAccNative, 0u); |
| 7827 | constexpr uint32_t kSetFlags = kAccDefault | kAccCopied; |
| 7828 | constexpr uint32_t kMaskFlags = ~kAccSkipAccessChecks; |
| 7829 | new_method.SetAccessFlags((new_method.GetAccessFlags() | kSetFlags) & kMaskFlags); |
| 7830 | break; |
| 7831 | } |
| 7832 | case CopiedMethodRecord::State::kDefaultConflict: { |
| 7833 | // This is a type of default method (there are default method impls, just a conflict) |
| 7834 | // so mark this as a default. We use the `kAccAbstract` flag to distinguish it from |
| 7835 | // invokable copied default method without using a separate access flag but the default |
| 7836 | // conflicting method is technically not abstract and ArtMethod::IsAbstract() shall |
| 7837 | // return false. Also clear the kAccSkipAccessChecks bit since this class hasn't been |
| 7838 | // verified yet it shouldn't have methods that are skipping access checks. Also clear |
| 7839 | // potential kAccSingleImplementation to avoid CHA trying to inline the default method. |
| 7840 | uint32_t access_flags = new_method.GetAccessFlags(); |
| 7841 | DCHECK_EQ(access_flags & (kAccNative | kAccIntrinsic), 0u); |
| 7842 | constexpr uint32_t kSetFlags = kAccDefault | kAccAbstract | kAccCopied; |
| 7843 | constexpr uint32_t kMaskFlags = ~(kAccSkipAccessChecks | kAccSingleImplementation); |
| 7844 | new_method.SetAccessFlags((access_flags | kSetFlags) & kMaskFlags); |
| 7845 | DCHECK(new_method.IsDefaultConflicting()); |
| 7846 | DCHECK(!new_method.IsAbstract()); |
| 7847 | // The actual method might or might not be marked abstract since we just copied it from |
| 7848 | // a (possibly default) interface method. We need to set its entry point to be the bridge |
| 7849 | // so that the compiler will not invoke the implementation of whatever method we copied |
| 7850 | // from. |
| 7851 | EnsureThrowsInvocationError(class_linker_, &new_method); |
| 7852 | break; |
| 7853 | } |
| 7854 | default: |
| 7855 | LOG(FATAL) << "Unexpected state: " << enum_cast<uint32_t>(record->GetState()); |
| 7856 | UNREACHABLE(); |
| 7857 | } |
| 7858 | } |
| 7859 | |
| 7860 | if (VLOG_IS_ON(class_linker)) { |
| 7861 | LogNewVirtuals(methods); |
| 7862 | } |
| 7863 | |
| 7864 | class_linker_->UpdateClassMethods(klass, methods); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7865 | } |
| 7866 | |
Vladimir Marko | b91402f | 2021-12-21 15:55:06 +0000 | [diff] [blame] | 7867 | template <PointerSize kPointerSize> |
Vladimir Marko | 0441d20 | 2022-02-18 13:55:15 +0000 | [diff] [blame] | 7868 | bool ClassLinker::LinkMethodsHelper<kPointerSize>::FinalizeIfTable( |
| 7869 | Handle<mirror::Class> klass, |
| 7870 | MutableHandle<mirror::IfTable> iftable, |
| 7871 | Handle<mirror::PointerArray> vtable, |
Vladimir Marko | 5171813 | 2022-02-07 16:31:08 +0000 | [diff] [blame] | 7872 | bool is_klass_abstract, |
| 7873 | bool is_super_abstract, |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 7874 | bool* out_new_conflict, |
| 7875 | ArtMethod** out_imt) { |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 7876 | size_t ifcount = iftable->Count(); |
Vladimir Marko | 0441d20 | 2022-02-18 13:55:15 +0000 | [diff] [blame] | 7877 | // We do not need a read barrier here as the length is constant, both from-space and |
| 7878 | // to-space `IfTable`s shall yield the same result. See also `Class::GetIfTableCount()`. |
| 7879 | size_t super_ifcount = |
| 7880 | klass->GetSuperClass<kDefaultVerifyFlags, kWithoutReadBarrier>()->GetIfTableCount(); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7881 | |
Vladimir Marko | 0441d20 | 2022-02-18 13:55:15 +0000 | [diff] [blame] | 7882 | ClassLinker* class_linker = nullptr; |
Vladimir Marko | 5171813 | 2022-02-07 16:31:08 +0000 | [diff] [blame] | 7883 | ArtMethod* unimplemented_method = nullptr; |
| 7884 | ArtMethod* imt_conflict_method = nullptr; |
| 7885 | uintptr_t imt_methods_begin = 0u; |
| 7886 | size_t imt_methods_size = 0u; |
| 7887 | DCHECK_EQ(klass->ShouldHaveImt(), !is_klass_abstract); |
| 7888 | DCHECK_EQ(klass->GetSuperClass()->ShouldHaveImt(), !is_super_abstract); |
| 7889 | if (!is_klass_abstract) { |
Vladimir Marko | 0441d20 | 2022-02-18 13:55:15 +0000 | [diff] [blame] | 7890 | class_linker = class_linker_; |
Vladimir Marko | 5171813 | 2022-02-07 16:31:08 +0000 | [diff] [blame] | 7891 | unimplemented_method = runtime_->GetImtUnimplementedMethod(); |
| 7892 | imt_conflict_method = runtime_->GetImtConflictMethod(); |
| 7893 | if (is_super_abstract) { |
| 7894 | // There was no IMT in superclass to copy to `out_imt[]`, so we need |
| 7895 | // to fill it with all implementation methods from superclass. |
| 7896 | DCHECK_EQ(imt_methods_begin, 0u); |
| 7897 | imt_methods_size = std::numeric_limits<size_t>::max(); // No method at the last byte. |
| 7898 | } else { |
| 7899 | // If the superclass has IMT, we have already copied it to `out_imt[]` and |
| 7900 | // we do not need to call `SetIMTRef()` for interfaces from superclass when |
| 7901 | // the implementation method is already in the superclass, only for new methods. |
| 7902 | // For simplicity, use the entire method array including direct methods. |
| 7903 | LengthPrefixedArray<ArtMethod>* const new_methods = klass->GetMethodsPtr(); |
| 7904 | if (new_methods != nullptr) { |
| 7905 | DCHECK_NE(new_methods->size(), 0u); |
| 7906 | imt_methods_begin = reinterpret_cast<uintptr_t>(&new_methods->At(0)); |
| 7907 | imt_methods_size = new_methods->size() * kMethodSize; |
| 7908 | } |
| 7909 | } |
| 7910 | } |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7911 | |
Vladimir Marko | 0441d20 | 2022-02-18 13:55:15 +0000 | [diff] [blame] | 7912 | auto update_imt = [=](ObjPtr<mirror::Class> iface, size_t j, ArtMethod* implementation) |
| 7913 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 7914 | // Place method in imt if entry is empty, place conflict otherwise. |
| 7915 | ArtMethod** imt_ptr = &out_imt[iface->GetVirtualMethod(j, kPointerSize)->GetImtIndex()]; |
| 7916 | class_linker->SetIMTRef(unimplemented_method, |
| 7917 | imt_conflict_method, |
| 7918 | implementation, |
| 7919 | /*out*/out_new_conflict, |
| 7920 | /*out*/imt_ptr); |
| 7921 | }; |
| 7922 | |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 7923 | // For interfaces inherited from superclass, the new method arrays are empty, |
| 7924 | // so use vtable indexes from implementation methods from the superclass method array. |
| 7925 | for (size_t i = 0; i != super_ifcount; ++i) { |
| 7926 | ObjPtr<mirror::PointerArray> method_array = iftable->GetMethodArrayOrNull(i); |
Vladimir Marko | 0441d20 | 2022-02-18 13:55:15 +0000 | [diff] [blame] | 7927 | DCHECK(method_array == klass->GetSuperClass()->GetIfTable()->GetMethodArrayOrNull(i)); |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 7928 | if (method_array == nullptr) { |
| 7929 | continue; |
| 7930 | } |
| 7931 | size_t num_methods = method_array->GetLength(); |
| 7932 | ObjPtr<mirror::Class> iface = iftable->GetInterface(i); |
Vladimir Marko | 0441d20 | 2022-02-18 13:55:15 +0000 | [diff] [blame] | 7933 | size_t j = 0; |
| 7934 | // First loop has method array shared with the super class. |
| 7935 | for (; j != num_methods; ++j) { |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 7936 | ArtMethod* super_implementation = |
Vladimir Marko | 0441d20 | 2022-02-18 13:55:15 +0000 | [diff] [blame] | 7937 | method_array->GetElementPtrSize<ArtMethod*, kPointerSize>(j); |
| 7938 | size_t vtable_index = super_implementation->GetMethodIndex(); |
| 7939 | ArtMethod* implementation = |
| 7940 | vtable->GetElementPtrSize<ArtMethod*, kPointerSize>(vtable_index); |
| 7941 | // Check if we need to update IMT with this method, see above. |
| 7942 | if (reinterpret_cast<uintptr_t>(implementation) - imt_methods_begin < imt_methods_size) { |
| 7943 | update_imt(iface, j, implementation); |
| 7944 | } |
| 7945 | if (implementation != super_implementation) { |
| 7946 | // Copy-on-write and move to the next loop. |
| 7947 | Thread* self = self_; |
| 7948 | StackHandleScope<2u> hs(self); |
| 7949 | Handle<mirror::PointerArray> old_method_array = hs.NewHandle(method_array); |
| 7950 | HandleWrapperObjPtr<mirror::Class> h_iface = hs.NewHandleWrapper(&iface); |
| 7951 | if (ifcount == super_ifcount && iftable.Get() == klass->GetSuperClass()->GetIfTable()) { |
| 7952 | ObjPtr<mirror::IfTable> new_iftable = ObjPtr<mirror::IfTable>::DownCast( |
| 7953 | mirror::ObjectArray<mirror::Object>::CopyOf( |
| 7954 | iftable, self, ifcount * mirror::IfTable::kMax)); |
| 7955 | if (new_iftable == nullptr) { |
| 7956 | return false; |
| 7957 | } |
| 7958 | iftable.Assign(new_iftable); |
| 7959 | } |
| 7960 | method_array = ObjPtr<mirror::PointerArray>::DownCast( |
| 7961 | mirror::Array::CopyOf(old_method_array, self, num_methods)); |
| 7962 | if (method_array == nullptr) { |
| 7963 | return false; |
| 7964 | } |
| 7965 | iftable->SetMethodArray(i, method_array); |
| 7966 | method_array->SetElementPtrSize(j, implementation, kPointerSize); |
| 7967 | ++j; |
| 7968 | break; |
| 7969 | } |
| 7970 | } |
| 7971 | // Second loop (if non-empty) has method array different from the superclass. |
| 7972 | for (; j != num_methods; ++j) { |
| 7973 | ArtMethod* super_implementation = |
| 7974 | method_array->GetElementPtrSize<ArtMethod*, kPointerSize>(j); |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 7975 | size_t vtable_index = super_implementation->GetMethodIndex(); |
| 7976 | ArtMethod* implementation = |
| 7977 | vtable->GetElementPtrSize<ArtMethod*, kPointerSize>(vtable_index); |
| 7978 | method_array->SetElementPtrSize(j, implementation, kPointerSize); |
Vladimir Marko | 5171813 | 2022-02-07 16:31:08 +0000 | [diff] [blame] | 7979 | // Check if we need to update IMT with this method, see above. |
| 7980 | if (reinterpret_cast<uintptr_t>(implementation) - imt_methods_begin < imt_methods_size) { |
Vladimir Marko | 0441d20 | 2022-02-18 13:55:15 +0000 | [diff] [blame] | 7981 | update_imt(iface, j, implementation); |
Vladimir Marko | 5171813 | 2022-02-07 16:31:08 +0000 | [diff] [blame] | 7982 | } |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7983 | } |
| 7984 | } |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7985 | |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 7986 | // New interface method arrays contain vtable indexes. Translate them to methods. |
Vladimir Marko | 5171813 | 2022-02-07 16:31:08 +0000 | [diff] [blame] | 7987 | DCHECK_EQ(klass->ShouldHaveImt(), !is_klass_abstract); |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 7988 | for (size_t i = super_ifcount; i != ifcount; ++i) { |
| 7989 | ObjPtr<mirror::PointerArray> method_array = iftable->GetMethodArrayOrNull(i); |
| 7990 | if (method_array == nullptr) { |
| 7991 | continue; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7992 | } |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 7993 | size_t num_methods = method_array->GetLength(); |
| 7994 | ObjPtr<mirror::Class> iface = iftable->GetInterface(i); |
| 7995 | for (size_t j = 0; j != num_methods; ++j) { |
| 7996 | size_t vtable_index = method_array->GetElementPtrSize<size_t, kPointerSize>(j); |
| 7997 | ArtMethod* implementation = |
| 7998 | vtable->GetElementPtrSize<ArtMethod*, kPointerSize>(vtable_index); |
| 7999 | method_array->SetElementPtrSize(j, implementation, kPointerSize); |
Vladimir Marko | 5171813 | 2022-02-07 16:31:08 +0000 | [diff] [blame] | 8000 | if (!is_klass_abstract) { |
Vladimir Marko | 0441d20 | 2022-02-18 13:55:15 +0000 | [diff] [blame] | 8001 | update_imt(iface, j, implementation); |
Vladimir Marko | 5171813 | 2022-02-07 16:31:08 +0000 | [diff] [blame] | 8002 | } |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 8003 | } |
| 8004 | } |
Vladimir Marko | 0441d20 | 2022-02-18 13:55:15 +0000 | [diff] [blame] | 8005 | |
| 8006 | return true; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 8007 | } |
| 8008 | |
Vladimir Marko | b91402f | 2021-12-21 15:55:06 +0000 | [diff] [blame] | 8009 | template <PointerSize kPointerSize> |
Vladimir Marko | 0441d20 | 2022-02-18 13:55:15 +0000 | [diff] [blame] | 8010 | ObjPtr<mirror::PointerArray> ClassLinker::LinkMethodsHelper<kPointerSize>::AllocPointerArray( |
| 8011 | Thread* self, size_t length) { |
| 8012 | using PointerArrayType = std::conditional_t< |
| 8013 | kPointerSize == PointerSize::k64, mirror::LongArray, mirror::IntArray>; |
| 8014 | ObjPtr<mirror::Array> array = PointerArrayType::Alloc(self, length); |
| 8015 | return ObjPtr<mirror::PointerArray>::DownCast(array); |
| 8016 | } |
| 8017 | |
| 8018 | template <PointerSize kPointerSize> |
| 8019 | bool ClassLinker::LinkMethodsHelper<kPointerSize>::AllocateIfTableMethodArrays( |
| 8020 | Thread* self, |
| 8021 | Handle<mirror::Class> klass, |
| 8022 | Handle<mirror::IfTable> iftable) { |
| 8023 | DCHECK(!klass->IsInterface()); |
| 8024 | DCHECK(klass_->HasSuperClass()); |
| 8025 | const size_t ifcount = iftable->Count(); |
| 8026 | // We do not need a read barrier here as the length is constant, both from-space and |
| 8027 | // to-space `IfTable`s shall yield the same result. See also `Class::GetIfTableCount()`. |
| 8028 | size_t super_ifcount = |
| 8029 | klass->GetSuperClass<kDefaultVerifyFlags, kWithoutReadBarrier>()->GetIfTableCount(); |
| 8030 | if (ifcount == super_ifcount) { |
| 8031 | DCHECK(iftable.Get() == klass_->GetSuperClass()->GetIfTable()); |
| 8032 | return true; |
| 8033 | } |
| 8034 | |
| 8035 | if (kIsDebugBuild) { |
| 8036 | // The method array references for superclass interfaces have been copied. |
| 8037 | // We shall allocate new arrays if needed (copy-on-write) in `FinalizeIfTable()`. |
| 8038 | ObjPtr<mirror::IfTable> super_iftable = klass_->GetSuperClass()->GetIfTable(); |
| 8039 | for (size_t i = 0; i != super_ifcount; ++i) { |
| 8040 | CHECK(iftable->GetInterface(i) == super_iftable->GetInterface(i)); |
| 8041 | CHECK(iftable->GetMethodArrayOrNull(i) == super_iftable->GetMethodArrayOrNull(i)); |
| 8042 | } |
| 8043 | } |
| 8044 | |
| 8045 | for (size_t i = super_ifcount; i < ifcount; ++i) { |
| 8046 | size_t num_methods = iftable->GetInterface(i)->NumDeclaredVirtualMethods(); |
| 8047 | if (num_methods > 0) { |
| 8048 | ObjPtr<mirror::PointerArray> method_array = AllocPointerArray(self, num_methods); |
| 8049 | if (UNLIKELY(method_array == nullptr)) { |
| 8050 | self->AssertPendingOOMException(); |
| 8051 | return false; |
| 8052 | } |
| 8053 | iftable->SetMethodArray(i, method_array); |
| 8054 | } |
| 8055 | } |
| 8056 | return true; |
| 8057 | } |
| 8058 | |
| 8059 | template <PointerSize kPointerSize> |
Vladimir Marko | bed84ef | 2022-01-21 13:57:14 +0000 | [diff] [blame] | 8060 | size_t ClassLinker::LinkMethodsHelper<kPointerSize>::AssignVTableIndexes( |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 8061 | ObjPtr<mirror::Class> klass, |
| 8062 | ObjPtr<mirror::Class> super_class, |
Vladimir Marko | 5171813 | 2022-02-07 16:31:08 +0000 | [diff] [blame] | 8063 | bool is_super_abstract, |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 8064 | size_t num_virtual_methods, |
| 8065 | ObjPtr<mirror::IfTable> iftable) { |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 8066 | DCHECK(!klass->IsInterface()); |
| 8067 | DCHECK(klass->HasSuperClass()); |
| 8068 | DCHECK(klass->GetSuperClass() == super_class); |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 8069 | |
| 8070 | // There should be no thread suspension unless we want to throw an exception. |
Vladimir Marko | 7ddae99 | 2022-01-18 14:27:20 +0000 | [diff] [blame] | 8071 | // (We are using `ObjPtr<>` and raw vtable pointers that are invalidated by thread suspension.) |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 8072 | std::optional<ScopedAssertNoThreadSuspension> sants(__FUNCTION__); |
| 8073 | |
| 8074 | // Prepare a hash table with virtual methods from the superclass. |
| 8075 | // For the unlikely cases that there are multiple methods with the same signature |
| 8076 | // but different vtable indexes, keep an array with indexes of the previous |
| 8077 | // methods with the same signature (walked as singly-linked lists). |
Vladimir Marko | 7ddae99 | 2022-01-18 14:27:20 +0000 | [diff] [blame] | 8078 | uint8_t* raw_super_vtable; |
| 8079 | size_t super_vtable_length; |
Vladimir Marko | 5171813 | 2022-02-07 16:31:08 +0000 | [diff] [blame] | 8080 | if (is_super_abstract) { |
| 8081 | DCHECK(!super_class->ShouldHaveEmbeddedVTable()); |
Vladimir Marko | 7ddae99 | 2022-01-18 14:27:20 +0000 | [diff] [blame] | 8082 | ObjPtr<mirror::PointerArray> super_vtable = super_class->GetVTableDuringLinking(); |
| 8083 | DCHECK(super_vtable != nullptr); |
| 8084 | raw_super_vtable = reinterpret_cast<uint8_t*>(super_vtable.Ptr()) + |
| 8085 | mirror::Array::DataOffset(static_cast<size_t>(kPointerSize)).Uint32Value(); |
| 8086 | super_vtable_length = super_vtable->GetLength(); |
Vladimir Marko | 5171813 | 2022-02-07 16:31:08 +0000 | [diff] [blame] | 8087 | } else { |
| 8088 | DCHECK(super_class->ShouldHaveEmbeddedVTable()); |
| 8089 | raw_super_vtable = reinterpret_cast<uint8_t*>(super_class.Ptr()) + |
| 8090 | mirror::Class::EmbeddedVTableOffset(kPointerSize).Uint32Value(); |
| 8091 | super_vtable_length = super_class->GetEmbeddedVTableLength(); |
Vladimir Marko | 7ddae99 | 2022-01-18 14:27:20 +0000 | [diff] [blame] | 8092 | } |
| 8093 | VTableAccessor super_vtable_accessor(raw_super_vtable, super_vtable_length); |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 8094 | static constexpr double kMinLoadFactor = 0.3; |
| 8095 | static constexpr double kMaxLoadFactor = 0.5; |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 8096 | static constexpr size_t kMaxStackBuferSize = 256; |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 8097 | const size_t declared_virtuals_buffer_size = num_virtual_methods * 3; |
Nicolas Geoffray | ef0b9c4 | 2022-07-04 16:24:06 +0000 | [diff] [blame] | 8098 | const size_t super_vtable_buffer_size = super_vtable_length * 3; |
| 8099 | const size_t bit_vector_size = BitVector::BitsToWords(num_virtual_methods); |
| 8100 | const size_t total_size = |
| 8101 | declared_virtuals_buffer_size + super_vtable_buffer_size + bit_vector_size; |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 8102 | |
Nicolas Geoffray | ef0b9c4 | 2022-07-04 16:24:06 +0000 | [diff] [blame] | 8103 | uint32_t* declared_virtuals_buffer_ptr = (total_size <= kMaxStackBuferSize) |
| 8104 | ? reinterpret_cast<uint32_t*>(alloca(total_size * sizeof(uint32_t))) |
| 8105 | : allocator_.AllocArray<uint32_t>(total_size); |
| 8106 | uint32_t* bit_vector_buffer_ptr = declared_virtuals_buffer_ptr + declared_virtuals_buffer_size; |
| 8107 | |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 8108 | DeclaredVirtualSignatureSet declared_virtual_signatures( |
| 8109 | kMinLoadFactor, |
| 8110 | kMaxLoadFactor, |
| 8111 | DeclaredVirtualSignatureHash(klass), |
| 8112 | DeclaredVirtualSignatureEqual(klass), |
| 8113 | declared_virtuals_buffer_ptr, |
| 8114 | declared_virtuals_buffer_size, |
| 8115 | allocator_.Adapter()); |
Nicolas Geoffray | ef0b9c4 | 2022-07-04 16:24:06 +0000 | [diff] [blame] | 8116 | |
| 8117 | ArrayRef<uint32_t> same_signature_vtable_lists; |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 8118 | const bool is_proxy_class = klass->IsProxyClass(); |
| 8119 | size_t vtable_length = super_vtable_length; |
Nicolas Geoffray | ef0b9c4 | 2022-07-04 16:24:06 +0000 | [diff] [blame] | 8120 | |
| 8121 | // Record which declared methods are overriding a super method. |
| 8122 | BitVector initialized_methods(/* expandable= */ false, |
| 8123 | Allocator::GetNoopAllocator(), |
| 8124 | bit_vector_size, |
| 8125 | bit_vector_buffer_ptr); |
| 8126 | |
| 8127 | // Note: our sets hash on the method name, and therefore we pay a high |
| 8128 | // performance price when a class has many overloads. |
| 8129 | // |
| 8130 | // We populate a set of declared signatures instead of signatures from the |
| 8131 | // super vtable (which is only lazy populated in case of interface overriding, |
| 8132 | // see below). This makes sure that we pay the performance price only on that |
| 8133 | // class, and not on its subclasses (except in the case of interface overriding, see below). |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 8134 | for (size_t i = 0; i != num_virtual_methods; ++i) { |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 8135 | ArtMethod* virtual_method = klass->GetVirtualMethodDuringLinking(i, kPointerSize); |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 8136 | DCHECK(!virtual_method->IsStatic()) << virtual_method->PrettyMethod(); |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 8137 | ArtMethod* signature_method = UNLIKELY(is_proxy_class) |
| 8138 | ? virtual_method->GetInterfaceMethodForProxyUnchecked(kPointerSize) |
| 8139 | : virtual_method; |
| 8140 | size_t hash = ComputeMethodHash(signature_method); |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 8141 | declared_virtual_signatures.PutWithHash(i, hash); |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 8142 | } |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 8143 | |
Nicolas Geoffray | ef0b9c4 | 2022-07-04 16:24:06 +0000 | [diff] [blame] | 8144 | // Loop through each super vtable method and see if they are overridden by a method we added to |
| 8145 | // the hash table. |
| 8146 | for (size_t j = 0; j < super_vtable_length; ++j) { |
| 8147 | // Search the hash table to see if we are overridden by any method. |
| 8148 | ArtMethod* super_method = super_vtable_accessor.GetVTableEntry(j); |
| 8149 | if (!klass->CanAccessMember(super_method->GetDeclaringClass(), |
| 8150 | super_method->GetAccessFlags())) { |
| 8151 | // Continue on to the next method since this one is package private and cannot be overridden. |
| 8152 | // Before Android 4.1, the package-private method super_method might have been incorrectly |
| 8153 | // overridden. |
| 8154 | continue; |
| 8155 | } |
| 8156 | size_t hash = (j < mirror::Object::kVTableLength) |
| 8157 | ? class_linker_->object_virtual_method_hashes_[j] |
| 8158 | : ComputeMethodHash(super_method); |
| 8159 | auto it = declared_virtual_signatures.FindWithHash(super_method, hash); |
| 8160 | if (it == declared_virtual_signatures.end()) { |
| 8161 | continue; |
| 8162 | } |
| 8163 | ArtMethod* virtual_method = klass->GetVirtualMethodDuringLinking(*it, kPointerSize); |
| 8164 | if (super_method->IsFinal()) { |
| 8165 | sants.reset(); |
| 8166 | ThrowLinkageError(klass, "Method %s overrides final method in class %s", |
| 8167 | virtual_method->PrettyMethod().c_str(), |
| 8168 | super_method->GetDeclaringClassDescriptor()); |
| 8169 | return 0u; |
| 8170 | } |
| 8171 | if (initialized_methods.IsBitSet(*it)) { |
| 8172 | // The method is overriding more than one method. |
| 8173 | // We record that information in a linked list to later set the method in the vtable |
| 8174 | // locations that are not the method index. |
| 8175 | if (same_signature_vtable_lists.empty()) { |
| 8176 | same_signature_vtable_lists = ArrayRef<uint32_t>( |
| 8177 | allocator_.AllocArray<uint32_t>(super_vtable_length), super_vtable_length); |
| 8178 | std::fill_n(same_signature_vtable_lists.data(), super_vtable_length, dex::kDexNoIndex); |
| 8179 | same_signature_vtable_lists_ = same_signature_vtable_lists; |
| 8180 | } |
| 8181 | same_signature_vtable_lists[j] = virtual_method->GetMethodIndexDuringLinking(); |
| 8182 | } else { |
| 8183 | initialized_methods.SetBit(*it); |
| 8184 | } |
| 8185 | |
| 8186 | // We arbitrarily set to the largest index. This is also expected when |
| 8187 | // iterating over the `same_signature_vtable_lists_`. |
| 8188 | virtual_method->SetMethodIndex(j); |
| 8189 | } |
| 8190 | |
| 8191 | // Add the non-overridden methods at the end. |
| 8192 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
| 8193 | if (!initialized_methods.IsBitSet(i)) { |
| 8194 | ArtMethod* local_method = klass->GetVirtualMethodDuringLinking(i, kPointerSize); |
| 8195 | local_method->SetMethodIndex(vtable_length); |
| 8196 | vtable_length++; |
| 8197 | } |
| 8198 | } |
| 8199 | |
| 8200 | // A lazily constructed super vtable set, which we only populate in the less |
| 8201 | // common sittuation of a superclass implementing a method declared in an |
| 8202 | // interface this class inherits. |
| 8203 | // We still try to allocate the set on the stack as using the arena will have |
| 8204 | // a larger cost. |
| 8205 | uint32_t* super_vtable_buffer_ptr = bit_vector_buffer_ptr + bit_vector_size; |
| 8206 | VTableSignatureSet super_vtable_signatures( |
| 8207 | kMinLoadFactor, |
| 8208 | kMaxLoadFactor, |
| 8209 | VTableSignatureHash(super_vtable_accessor), |
| 8210 | VTableSignatureEqual(super_vtable_accessor), |
| 8211 | super_vtable_buffer_ptr, |
| 8212 | super_vtable_buffer_size, |
| 8213 | allocator_.Adapter()); |
| 8214 | |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 8215 | // Assign vtable indexes for interface methods in new interfaces and store them |
| 8216 | // in implementation method arrays. These shall be replaced by actual method |
| 8217 | // pointers later. We do not need to do this for superclass interfaces as we can |
| 8218 | // get these vtable indexes from implementation methods in superclass iftable. |
| 8219 | // Record data for copied methods which shall be referenced by the vtable. |
| 8220 | const size_t ifcount = iftable->Count(); |
| 8221 | ObjPtr<mirror::IfTable> super_iftable = super_class->GetIfTable(); |
| 8222 | const size_t super_ifcount = super_iftable->Count(); |
| 8223 | for (size_t i = ifcount; i != super_ifcount; ) { |
| 8224 | --i; |
| 8225 | DCHECK_LT(i, ifcount); |
| 8226 | ObjPtr<mirror::Class> iface = iftable->GetInterface(i); |
| 8227 | ObjPtr<mirror::PointerArray> method_array = iftable->GetMethodArrayOrNull(i); |
| 8228 | size_t num_methods = (method_array != nullptr) ? method_array->GetLength() : 0u; |
| 8229 | for (size_t j = 0; j != num_methods; ++j) { |
| 8230 | ArtMethod* interface_method = iface->GetVirtualMethod(j, kPointerSize); |
| 8231 | size_t hash = ComputeMethodHash(interface_method); |
| 8232 | ArtMethod* vtable_method = nullptr; |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 8233 | auto it1 = declared_virtual_signatures.FindWithHash(interface_method, hash); |
| 8234 | if (it1 != declared_virtual_signatures.end()) { |
Nicolas Geoffray | ef0b9c4 | 2022-07-04 16:24:06 +0000 | [diff] [blame] | 8235 | ArtMethod* found_method = klass->GetVirtualMethodDuringLinking(*it1, kPointerSize); |
| 8236 | // For interface overriding, we only look at public methods. |
| 8237 | if (found_method->IsPublic()) { |
| 8238 | vtable_method = found_method; |
| 8239 | } |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 8240 | } else { |
Nicolas Geoffray | ef0b9c4 | 2022-07-04 16:24:06 +0000 | [diff] [blame] | 8241 | // This situation should be rare (a superclass implements a method |
| 8242 | // declared in an interface this class is inheriting). Only in this case |
| 8243 | // do we lazily populate the super_vtable_signatures. |
| 8244 | if (super_vtable_signatures.empty()) { |
| 8245 | for (size_t k = 0; k < super_vtable_length; ++k) { |
| 8246 | ArtMethod* super_method = super_vtable_accessor.GetVTableEntry(k); |
| 8247 | if (!super_method->IsPublic()) { |
| 8248 | // For interface overriding, we only look at public methods. |
| 8249 | continue; |
| 8250 | } |
| 8251 | size_t super_hash = (k < mirror::Object::kVTableLength) |
| 8252 | ? class_linker_->object_virtual_method_hashes_[k] |
| 8253 | : ComputeMethodHash(super_method); |
| 8254 | auto [it, inserted] = super_vtable_signatures.InsertWithHash(k, super_hash); |
| 8255 | DCHECK(inserted || super_vtable_accessor.GetVTableEntry(*it) == super_method); |
| 8256 | } |
| 8257 | } |
Nicolas Geoffray | 868e576 | 2022-06-08 15:00:06 +0100 | [diff] [blame] | 8258 | auto it2 = super_vtable_signatures.FindWithHash(interface_method, hash); |
| 8259 | if (it2 != super_vtable_signatures.end()) { |
| 8260 | vtable_method = super_vtable_accessor.GetVTableEntry(*it2); |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 8261 | } |
| 8262 | } |
Nicolas Geoffray | d88c149 | 2022-07-06 10:01:58 +0100 | [diff] [blame] | 8263 | |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 8264 | uint32_t vtable_index = vtable_length; |
Nicolas Geoffray | ef0b9c4 | 2022-07-04 16:24:06 +0000 | [diff] [blame] | 8265 | if (vtable_method != nullptr) { |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 8266 | vtable_index = vtable_method->GetMethodIndexDuringLinking(); |
| 8267 | if (!vtable_method->IsOverridableByDefaultMethod()) { |
| 8268 | method_array->SetElementPtrSize(j, vtable_index, kPointerSize); |
| 8269 | continue; |
| 8270 | } |
| 8271 | } |
| 8272 | |
| 8273 | auto [it, inserted] = copied_method_records_.InsertWithHash( |
| 8274 | CopiedMethodRecord(interface_method, vtable_index), hash); |
Nicolas Geoffray | ef0b9c4 | 2022-07-04 16:24:06 +0000 | [diff] [blame] | 8275 | if (vtable_method != nullptr) { |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 8276 | DCHECK_EQ(vtable_index, it->GetMethodIndex()); |
| 8277 | } else if (inserted) { |
| 8278 | DCHECK_EQ(vtable_index, it->GetMethodIndex()); |
| 8279 | DCHECK_EQ(vtable_index, vtable_length); |
| 8280 | ++vtable_length; |
| 8281 | } else { |
| 8282 | vtable_index = it->GetMethodIndex(); |
| 8283 | } |
| 8284 | method_array->SetElementPtrSize(j, it->GetMethodIndex(), kPointerSize); |
| 8285 | if (inserted) { |
| 8286 | it->SetState(interface_method->IsAbstract() ? CopiedMethodRecord::State::kAbstractSingle |
| 8287 | : CopiedMethodRecord::State::kDefaultSingle); |
| 8288 | } else { |
| 8289 | it->UpdateState(iface, interface_method, vtable_index, iftable, ifcount, i); |
| 8290 | } |
| 8291 | } |
| 8292 | } |
| 8293 | // Finalize copied method records and check if we can reuse some methods from superclass vtable. |
| 8294 | size_t num_new_copied_methods = copied_method_records_.size(); |
| 8295 | for (CopiedMethodRecord& record : copied_method_records_) { |
| 8296 | uint32_t vtable_index = record.GetMethodIndex(); |
| 8297 | if (vtable_index < super_vtable_length) { |
| 8298 | ArtMethod* super_method = super_vtable_accessor.GetVTableEntry(record.GetMethodIndex()); |
| 8299 | DCHECK(super_method->IsOverridableByDefaultMethod()); |
| 8300 | record.FinalizeState( |
| 8301 | super_method, vtable_index, iftable, ifcount, super_iftable, super_ifcount); |
| 8302 | if (record.GetState() == CopiedMethodRecord::State::kUseSuperMethod) { |
| 8303 | --num_new_copied_methods; |
| 8304 | } |
| 8305 | } |
| 8306 | } |
| 8307 | num_new_copied_methods_ = num_new_copied_methods; |
| 8308 | |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 8309 | if (UNLIKELY(!IsUint<16>(vtable_length))) { |
| 8310 | sants.reset(); |
| 8311 | ThrowClassFormatError(klass, "Too many methods defined on class: %zd", vtable_length); |
| 8312 | return 0u; |
| 8313 | } |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 8314 | |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 8315 | return vtable_length; |
| 8316 | } |
| 8317 | |
| 8318 | template <PointerSize kPointerSize> |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 8319 | bool ClassLinker::LinkMethodsHelper<kPointerSize>::FindCopiedMethodsForInterface( |
| 8320 | ObjPtr<mirror::Class> klass, |
| 8321 | size_t num_virtual_methods, |
| 8322 | ObjPtr<mirror::IfTable> iftable) { |
| 8323 | DCHECK(klass->IsInterface()); |
| 8324 | DCHECK(klass->HasSuperClass()); |
| 8325 | DCHECK(klass->GetSuperClass()->IsObjectClass()); |
| 8326 | DCHECK_EQ(klass->GetSuperClass()->GetIfTableCount(), 0); |
| 8327 | |
| 8328 | // There should be no thread suspension unless we want to throw an exception. |
| 8329 | // (We are using `ObjPtr<>`s that are invalidated by thread suspension.) |
| 8330 | std::optional<ScopedAssertNoThreadSuspension> sants(__FUNCTION__); |
| 8331 | |
| 8332 | // Prepare a `HashSet<>` with the declared virtual methods. These mask any methods |
| 8333 | // from superinterfaces, so we can filter out matching superinterface methods. |
| 8334 | static constexpr double kMinLoadFactor = 0.3; |
| 8335 | static constexpr double kMaxLoadFactor = 0.5; |
| 8336 | static constexpr size_t kMaxStackBuferSize = 256; |
| 8337 | const size_t declared_virtuals_buffer_size = num_virtual_methods * 3; |
| 8338 | uint32_t* declared_virtuals_buffer_ptr = (declared_virtuals_buffer_size <= kMaxStackBuferSize) |
| 8339 | ? reinterpret_cast<uint32_t*>(alloca(declared_virtuals_buffer_size * sizeof(uint32_t))) |
| 8340 | : allocator_.AllocArray<uint32_t>(declared_virtuals_buffer_size); |
| 8341 | DeclaredVirtualSignatureSet declared_virtual_signatures( |
| 8342 | kMinLoadFactor, |
| 8343 | kMaxLoadFactor, |
| 8344 | DeclaredVirtualSignatureHash(klass), |
| 8345 | DeclaredVirtualSignatureEqual(klass), |
| 8346 | declared_virtuals_buffer_ptr, |
| 8347 | declared_virtuals_buffer_size, |
| 8348 | allocator_.Adapter()); |
| 8349 | for (size_t i = 0; i != num_virtual_methods; ++i) { |
| 8350 | ArtMethod* virtual_method = klass->GetVirtualMethodDuringLinking(i, kPointerSize); |
| 8351 | DCHECK(!virtual_method->IsStatic()) << virtual_method->PrettyMethod(); |
| 8352 | size_t hash = ComputeMethodHash(virtual_method); |
| 8353 | declared_virtual_signatures.PutWithHash(i, hash); |
| 8354 | } |
| 8355 | |
| 8356 | // We do not create miranda methods for interface classes, so we do not need to track |
| 8357 | // non-default (abstract) interface methods. The downside is that we cannot use the |
| 8358 | // optimized code paths with `CopiedMethodRecord::State::kDefaultSingle` and since |
| 8359 | // we do not fill method arrays for interfaces, the method search actually has to |
| 8360 | // compare signatures instead of searching for the implementing method. |
| 8361 | const size_t ifcount = iftable->Count(); |
| 8362 | size_t new_method_index = num_virtual_methods; |
| 8363 | for (size_t i = ifcount; i != 0u; ) { |
| 8364 | --i; |
| 8365 | DCHECK_LT(i, ifcount); |
| 8366 | ObjPtr<mirror::Class> iface = iftable->GetInterface(i); |
| 8367 | if (!iface->HasDefaultMethods()) { |
| 8368 | continue; // No default methods to process. |
| 8369 | } |
| 8370 | size_t num_methods = iface->NumDeclaredVirtualMethods(); |
| 8371 | for (size_t j = 0; j != num_methods; ++j) { |
| 8372 | ArtMethod* interface_method = iface->GetVirtualMethod(j, kPointerSize); |
| 8373 | if (!interface_method->IsDefault()) { |
| 8374 | continue; // Do not process this non-default method. |
| 8375 | } |
| 8376 | size_t hash = ComputeMethodHash(interface_method); |
| 8377 | auto it1 = declared_virtual_signatures.FindWithHash(interface_method, hash); |
| 8378 | if (it1 != declared_virtual_signatures.end()) { |
| 8379 | ArtMethod* virtual_method = klass->GetVirtualMethodDuringLinking(*it1, kPointerSize); |
| 8380 | if (!virtual_method->IsAbstract() && !virtual_method->IsPublic()) { |
| 8381 | sants.reset(); |
| 8382 | ThrowIllegalAccessErrorForImplementingMethod(klass, virtual_method, interface_method); |
| 8383 | return false; |
| 8384 | } |
| 8385 | continue; // This default method is masked by a method declared in this interface. |
| 8386 | } |
| 8387 | |
| 8388 | CopiedMethodRecord new_record(interface_method, new_method_index); |
| 8389 | auto it = copied_method_records_.FindWithHash(new_record, hash); |
| 8390 | if (it == copied_method_records_.end()) { |
| 8391 | // Pretend that there is another default method and try to update the state. |
| 8392 | // If the `interface_method` is not masked, the state shall change to |
| 8393 | // `kDefaultConflict`; if it is masked, the state remains `kDefault`. |
| 8394 | new_record.SetState(CopiedMethodRecord::State::kDefault); |
| 8395 | new_record.UpdateStateForInterface(iface, interface_method, iftable, ifcount, i); |
| 8396 | if (new_record.GetState() == CopiedMethodRecord::State::kDefaultConflict) { |
| 8397 | // Insert the new record with the state `kDefault`. |
| 8398 | new_record.SetState(CopiedMethodRecord::State::kDefault); |
| 8399 | copied_method_records_.PutWithHash(new_record, hash); |
| 8400 | DCHECK_EQ(new_method_index, new_record.GetMethodIndex()); |
| 8401 | ++new_method_index; |
| 8402 | } |
| 8403 | } else { |
| 8404 | it->UpdateStateForInterface(iface, interface_method, iftable, ifcount, i); |
| 8405 | } |
| 8406 | } |
| 8407 | } |
| 8408 | |
| 8409 | // Prune records without conflict. (Method indexes are updated in `ReallocMethods()`.) |
| 8410 | // We do not copy normal default methods to subinterfaces, instead we find the |
| 8411 | // default method with `Class::FindVirtualMethodForInterfaceSuper()` when needed. |
| 8412 | size_t num_new_copied_methods = copied_method_records_.size(); |
| 8413 | for (CopiedMethodRecord& record : copied_method_records_) { |
| 8414 | if (record.GetState() != CopiedMethodRecord::State::kDefaultConflict) { |
| 8415 | DCHECK(record.GetState() == CopiedMethodRecord::State::kDefault); |
| 8416 | record.SetState(CopiedMethodRecord::State::kUseSuperMethod); |
| 8417 | --num_new_copied_methods; |
| 8418 | } |
| 8419 | } |
| 8420 | num_new_copied_methods_ = num_new_copied_methods; |
| 8421 | |
| 8422 | return true; |
| 8423 | } |
| 8424 | |
| 8425 | |
| 8426 | template <PointerSize kPointerSize> |
Vladimir Marko | b91402f | 2021-12-21 15:55:06 +0000 | [diff] [blame] | 8427 | FLATTEN |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 8428 | bool ClassLinker::LinkMethodsHelper<kPointerSize>::LinkMethods( |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 8429 | Thread* self, |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 8430 | Handle<mirror::Class> klass, |
| 8431 | Handle<mirror::ObjectArray<mirror::Class>> interfaces, |
| 8432 | bool* out_new_conflict, |
| 8433 | ArtMethod** out_imt) { |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 8434 | const size_t num_virtual_methods = klass->NumVirtualMethods(); |
| 8435 | if (klass->IsInterface()) { |
| 8436 | // No vtable. |
| 8437 | if (!IsUint<16>(num_virtual_methods)) { |
| 8438 | ThrowClassFormatError(klass.Get(), "Too many methods on interface: %zu", num_virtual_methods); |
| 8439 | return false; |
| 8440 | } |
Vladimir Marko | ff8f8c4 | 2022-09-19 17:18:24 +0000 | [diff] [blame] | 8441 | // Assign each method an interface table index and set the default flag. |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 8442 | bool has_defaults = false; |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 8443 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Vladimir Marko | b91402f | 2021-12-21 15:55:06 +0000 | [diff] [blame] | 8444 | ArtMethod* m = klass->GetVirtualMethodDuringLinking(i, kPointerSize); |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 8445 | m->SetMethodIndex(i); |
Vladimir Marko | ff8f8c4 | 2022-09-19 17:18:24 +0000 | [diff] [blame] | 8446 | uint32_t access_flags = m->GetAccessFlags(); |
| 8447 | DCHECK(!ArtMethod::IsDefault(access_flags)); |
| 8448 | DCHECK_EQ(!ArtMethod::IsAbstract(access_flags), ArtMethod::IsInvokable(access_flags)); |
| 8449 | if (ArtMethod::IsInvokable(access_flags)) { |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 8450 | // If the dex file does not support default methods, throw ClassFormatError. |
| 8451 | // This check is necessary to protect from odd cases, such as native default |
| 8452 | // methods, that the dex file verifier permits for old dex file versions. b/157170505 |
| 8453 | // FIXME: This should be `if (!m->GetDexFile()->SupportsDefaultMethods())` but we're |
| 8454 | // currently running CTS tests for default methods with dex file version 035 which |
| 8455 | // does not support default methods. So, we limit this to native methods. b/157718952 |
Vladimir Marko | ff8f8c4 | 2022-09-19 17:18:24 +0000 | [diff] [blame] | 8456 | if (ArtMethod::IsNative(access_flags)) { |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 8457 | DCHECK(!m->GetDexFile()->SupportsDefaultMethods()); |
| 8458 | ThrowClassFormatError(klass.Get(), |
| 8459 | "Dex file does not support default method '%s'", |
| 8460 | m->PrettyMethod().c_str()); |
| 8461 | return false; |
| 8462 | } |
Vladimir Marko | ff8f8c4 | 2022-09-19 17:18:24 +0000 | [diff] [blame] | 8463 | if (!ArtMethod::IsPublic(access_flags)) { |
Vladimir Marko | fd0aec4 | 2022-01-27 14:27:24 +0000 | [diff] [blame] | 8464 | // The verifier should have caught the non-public method for dex version 37. |
| 8465 | // Just warn and skip it since this is from before default-methods so we don't |
| 8466 | // really need to care that it has code. |
| 8467 | LOG(WARNING) << "Default interface method " << m->PrettyMethod() << " is not public! " |
| 8468 | << "This will be a fatal error in subsequent versions of android. " |
| 8469 | << "Continuing anyway."; |
| 8470 | } |
Vladimir Marko | ff8f8c4 | 2022-09-19 17:18:24 +0000 | [diff] [blame] | 8471 | m->SetAccessFlags(access_flags | kAccDefault); |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 8472 | has_defaults = true; |
| 8473 | } |
| 8474 | } |
| 8475 | // Mark that we have default methods so that we won't need to scan the virtual_methods_ array |
| 8476 | // during initialization. This is a performance optimization. We could simply traverse the |
| 8477 | // virtual_methods_ array again during initialization. |
| 8478 | if (has_defaults) { |
| 8479 | klass->SetHasDefaultMethods(); |
| 8480 | } |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 8481 | ObjPtr<mirror::IfTable> iftable = SetupInterfaceLookupTable( |
| 8482 | self, klass, &allocator_, NonProxyInterfacesAccessor(class_linker_, klass)); |
| 8483 | if (UNLIKELY(iftable == nullptr)) { |
| 8484 | self->AssertPendingException(); |
| 8485 | return false; |
| 8486 | } |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 8487 | size_t ifcount = iftable->Count(); |
| 8488 | bool have_super_with_defaults = false; |
| 8489 | for (size_t i = 0; i != ifcount; ++i) { |
| 8490 | if (iftable->GetInterface(i)->HasDefaultMethods()) { |
| 8491 | have_super_with_defaults = true; |
| 8492 | break; |
| 8493 | } |
| 8494 | } |
| 8495 | LengthPrefixedArray<ArtMethod>* old_methods = kIsDebugBuild ? klass->GetMethodsPtr() : nullptr; |
| 8496 | if (have_super_with_defaults) { |
| 8497 | if (!FindCopiedMethodsForInterface(klass.Get(), num_virtual_methods, iftable)) { |
| 8498 | self->AssertPendingException(); |
| 8499 | return false; |
| 8500 | } |
| 8501 | if (num_new_copied_methods_ != 0u) { |
| 8502 | // Re-check the number of methods. |
| 8503 | size_t final_num_virtual_methods = num_virtual_methods + num_new_copied_methods_; |
| 8504 | if (!IsUint<16>(final_num_virtual_methods)) { |
| 8505 | ThrowClassFormatError( |
| 8506 | klass.Get(), "Too many methods on interface: %zu", final_num_virtual_methods); |
| 8507 | return false; |
| 8508 | } |
| 8509 | ReallocMethods(klass.Get()); |
| 8510 | } |
| 8511 | } |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 8512 | klass->SetIfTable(iftable); |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 8513 | if (kIsDebugBuild) { |
| 8514 | // May cause thread suspension, so do this after we're done with `ObjPtr<> iftable`. |
| 8515 | ClobberOldMethods(old_methods, klass->GetMethodsPtr()); |
| 8516 | } |
| 8517 | return true; |
Vladimir Marko | b91402f | 2021-12-21 15:55:06 +0000 | [diff] [blame] | 8518 | } else if (LIKELY(klass->HasSuperClass())) { |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 8519 | // We set up the interface lookup table now because we need it to determine if we need |
| 8520 | // to update any vtable entries with new default method implementations. |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 8521 | StackHandleScope<3> hs(self); |
Vladimir Marko | 0441d20 | 2022-02-18 13:55:15 +0000 | [diff] [blame] | 8522 | MutableHandle<mirror::IfTable> iftable = hs.NewHandle(UNLIKELY(klass->IsProxyClass()) |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 8523 | ? SetupInterfaceLookupTable(self, klass, &allocator_, ProxyInterfacesAccessor(interfaces)) |
| 8524 | : SetupInterfaceLookupTable( |
| 8525 | self, klass, &allocator_, NonProxyInterfacesAccessor(class_linker_, klass))); |
| 8526 | if (UNLIKELY(iftable == nullptr)) { |
| 8527 | self->AssertPendingException(); |
| 8528 | return false; |
| 8529 | } |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 8530 | |
Vladimir Marko | 5171813 | 2022-02-07 16:31:08 +0000 | [diff] [blame] | 8531 | // Copy the IMT from superclass if present and needed. Update with new methods later. |
Vladimir Marko | 0441d20 | 2022-02-18 13:55:15 +0000 | [diff] [blame] | 8532 | Handle<mirror::Class> super_class = hs.NewHandle(klass->GetSuperClass()); |
Vladimir Marko | 5171813 | 2022-02-07 16:31:08 +0000 | [diff] [blame] | 8533 | bool is_klass_abstract = klass->IsAbstract(); |
| 8534 | bool is_super_abstract = super_class->IsAbstract(); |
| 8535 | DCHECK_EQ(klass->ShouldHaveImt(), !is_klass_abstract); |
| 8536 | DCHECK_EQ(super_class->ShouldHaveImt(), !is_super_abstract); |
| 8537 | if (!is_klass_abstract && !is_super_abstract) { |
| 8538 | ImTable* super_imt = super_class->GetImt(kPointerSize); |
| 8539 | for (size_t i = 0; i < ImTable::kSize; ++i) { |
| 8540 | out_imt[i] = super_imt->Get(i, kPointerSize); |
| 8541 | } |
| 8542 | } |
| 8543 | |
| 8544 | // If there are no new virtual methods and no new interfaces, we can simply reuse |
| 8545 | // the vtable from superclass. We may need to make a copy if it's embedded. |
Vladimir Marko | 0441d20 | 2022-02-18 13:55:15 +0000 | [diff] [blame] | 8546 | const size_t super_vtable_length = super_class->GetVTableLength(); |
Vladimir Marko | 0f71b19 | 2022-02-02 17:20:12 +0000 | [diff] [blame] | 8547 | if (num_virtual_methods == 0 && iftable.Get() == super_class->GetIfTable()) { |
Vladimir Marko | 5171813 | 2022-02-07 16:31:08 +0000 | [diff] [blame] | 8548 | DCHECK_EQ(is_super_abstract, !super_class->ShouldHaveEmbeddedVTable()); |
| 8549 | if (is_super_abstract) { |
| 8550 | DCHECK(super_class->IsAbstract() && !super_class->IsArrayClass()); |
| 8551 | ObjPtr<mirror::PointerArray> super_vtable = super_class->GetVTable(); |
| 8552 | CHECK(super_vtable != nullptr) << super_class->PrettyClass(); |
| 8553 | klass->SetVTable(super_vtable); |
| 8554 | // No IMT in the super class, we need to reconstruct it from the iftable. |
| 8555 | if (!is_klass_abstract && iftable->Count() != 0) { |
| 8556 | class_linker_->FillIMTFromIfTable(iftable.Get(), |
| 8557 | runtime_->GetImtUnimplementedMethod(), |
| 8558 | runtime_->GetImtConflictMethod(), |
| 8559 | klass.Get(), |
| 8560 | /*create_conflict_tables=*/false, |
| 8561 | /*ignore_copied_methods=*/false, |
| 8562 | out_new_conflict, |
| 8563 | out_imt); |
| 8564 | } |
| 8565 | } else { |
Vladimir Marko | 0441d20 | 2022-02-18 13:55:15 +0000 | [diff] [blame] | 8566 | ObjPtr<mirror::PointerArray> vtable = AllocPointerArray(self, super_vtable_length); |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 8567 | if (UNLIKELY(vtable == nullptr)) { |
| 8568 | self->AssertPendingOOMException(); |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 8569 | return false; |
| 8570 | } |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 8571 | for (size_t i = 0; i < super_vtable_length; i++) { |
| 8572 | vtable->SetElementPtrSize( |
| 8573 | i, super_class->GetEmbeddedVTableEntry(i, kPointerSize), kPointerSize); |
| 8574 | } |
| 8575 | klass->SetVTable(vtable); |
Vladimir Marko | 5171813 | 2022-02-07 16:31:08 +0000 | [diff] [blame] | 8576 | // The IMT was already copied from superclass if `klass` is not abstract. |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 8577 | } |
Vladimir Marko | bed84ef | 2022-01-21 13:57:14 +0000 | [diff] [blame] | 8578 | klass->SetIfTable(iftable.Get()); |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 8579 | return true; |
| 8580 | } |
| 8581 | |
Vladimir Marko | bed84ef | 2022-01-21 13:57:14 +0000 | [diff] [blame] | 8582 | // Allocate method arrays, so that we can link interface methods without thread suspension, |
| 8583 | // otherwise GC could miss visiting newly allocated copied methods. |
| 8584 | // TODO: Do not allocate copied methods during linking, store only records about what |
| 8585 | // we need to allocate and allocate it at the end. Start with superclass iftable and |
| 8586 | // perform copy-on-write when needed to facilitate maximum memory sharing. |
Vladimir Marko | 0441d20 | 2022-02-18 13:55:15 +0000 | [diff] [blame] | 8587 | if (!AllocateIfTableMethodArrays(self, klass, iftable)) { |
| 8588 | self->AssertPendingOOMException(); |
Vladimir Marko | bed84ef | 2022-01-21 13:57:14 +0000 | [diff] [blame] | 8589 | return false; |
| 8590 | } |
| 8591 | |
Vladimir Marko | 5171813 | 2022-02-07 16:31:08 +0000 | [diff] [blame] | 8592 | size_t final_vtable_size = AssignVTableIndexes( |
| 8593 | klass.Get(), super_class.Get(), is_super_abstract, num_virtual_methods, iftable.Get()); |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 8594 | if (final_vtable_size == 0u) { |
| 8595 | self->AssertPendingException(); |
| 8596 | return false; |
| 8597 | } |
| 8598 | DCHECK(IsUint<16>(final_vtable_size)); |
| 8599 | |
| 8600 | // Allocate the new vtable. |
Vladimir Marko | 0441d20 | 2022-02-18 13:55:15 +0000 | [diff] [blame] | 8601 | Handle<mirror::PointerArray> vtable = hs.NewHandle(AllocPointerArray(self, final_vtable_size)); |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 8602 | if (UNLIKELY(vtable == nullptr)) { |
| 8603 | self->AssertPendingOOMException(); |
| 8604 | return false; |
| 8605 | } |
| 8606 | |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 8607 | LengthPrefixedArray<ArtMethod>* old_methods = kIsDebugBuild ? klass->GetMethodsPtr() : nullptr; |
| 8608 | if (num_new_copied_methods_ != 0u) { |
| 8609 | ReallocMethods(klass.Get()); |
| 8610 | } |
| 8611 | |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 8612 | // Store new virtual methods in the new vtable. |
Vladimir Marko | d5d11d9 | 2021-02-02 16:24:25 +0000 | [diff] [blame] | 8613 | ArrayRef<uint32_t> same_signature_vtable_lists = same_signature_vtable_lists_; |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 8614 | for (ArtMethod& virtual_method : klass->GetVirtualMethodsSliceUnchecked(kPointerSize)) { |
Vladimir Marko | d5d11d9 | 2021-02-02 16:24:25 +0000 | [diff] [blame] | 8615 | uint32_t vtable_index = virtual_method.GetMethodIndexDuringLinking(); |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 8616 | vtable->SetElementPtrSize(vtable_index, &virtual_method, kPointerSize); |
Vladimir Marko | d5d11d9 | 2021-02-02 16:24:25 +0000 | [diff] [blame] | 8617 | if (UNLIKELY(vtable_index < same_signature_vtable_lists.size())) { |
Nicolas Geoffray | ef0b9c4 | 2022-07-04 16:24:06 +0000 | [diff] [blame] | 8618 | // We may override more than one method according to JLS, see b/211854716. |
Vladimir Marko | d5d11d9 | 2021-02-02 16:24:25 +0000 | [diff] [blame] | 8619 | while (same_signature_vtable_lists[vtable_index] != dex::kDexNoIndex) { |
| 8620 | DCHECK_LT(same_signature_vtable_lists[vtable_index], vtable_index); |
| 8621 | vtable_index = same_signature_vtable_lists[vtable_index]; |
Nicolas Geoffray | ef0b9c4 | 2022-07-04 16:24:06 +0000 | [diff] [blame] | 8622 | vtable->SetElementPtrSize(vtable_index, &virtual_method, kPointerSize); |
| 8623 | if (kIsDebugBuild) { |
| 8624 | ArtMethod* current_method = super_class->GetVTableEntry(vtable_index, kPointerSize); |
| 8625 | DCHECK(klass->CanAccessMember(current_method->GetDeclaringClass(), |
| 8626 | current_method->GetAccessFlags())); |
Vladimir Marko | d5d11d9 | 2021-02-02 16:24:25 +0000 | [diff] [blame] | 8627 | DCHECK(!current_method->IsFinal()); |
Vladimir Marko | d5d11d9 | 2021-02-02 16:24:25 +0000 | [diff] [blame] | 8628 | } |
| 8629 | } |
| 8630 | } |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 8631 | } |
| 8632 | |
| 8633 | // For non-overridden vtable slots, copy a method from `super_class`. |
| 8634 | for (size_t j = 0; j != super_vtable_length; ++j) { |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 8635 | if (vtable->GetElementPtrSize<ArtMethod*, kPointerSize>(j) == nullptr) { |
| 8636 | ArtMethod* super_method = super_class->GetVTableEntry(j, kPointerSize); |
| 8637 | vtable->SetElementPtrSize(j, super_method, kPointerSize); |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 8638 | } |
| 8639 | } |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 8640 | |
Vladimir Marko | 5171813 | 2022-02-07 16:31:08 +0000 | [diff] [blame] | 8641 | // Update the `iftable` (and IMT) with finalized virtual methods. |
Vladimir Marko | 0441d20 | 2022-02-18 13:55:15 +0000 | [diff] [blame] | 8642 | if (!FinalizeIfTable(klass, |
| 8643 | iftable, |
| 8644 | vtable, |
| 8645 | is_klass_abstract, |
| 8646 | is_super_abstract, |
| 8647 | out_new_conflict, |
| 8648 | out_imt)) { |
| 8649 | self->AssertPendingOOMException(); |
| 8650 | return false; |
| 8651 | } |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 8652 | |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 8653 | klass->SetVTable(vtable.Get()); |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 8654 | klass->SetIfTable(iftable.Get()); |
| 8655 | if (kIsDebugBuild) { |
| 8656 | CheckVTable(self, klass, kPointerSize); |
| 8657 | ClobberOldMethods(old_methods, klass->GetMethodsPtr()); |
| 8658 | } |
| 8659 | return true; |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 8660 | } else { |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 8661 | return LinkJavaLangObjectMethods(self, klass); |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 8662 | } |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 8663 | } |
| 8664 | |
Vladimir Marko | b91402f | 2021-12-21 15:55:06 +0000 | [diff] [blame] | 8665 | template <PointerSize kPointerSize> |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 8666 | bool ClassLinker::LinkMethodsHelper<kPointerSize>::LinkJavaLangObjectMethods( |
Vladimir Marko | b91402f | 2021-12-21 15:55:06 +0000 | [diff] [blame] | 8667 | Thread* self, |
| 8668 | Handle<mirror::Class> klass) { |
| 8669 | DCHECK_EQ(klass.Get(), GetClassRoot<mirror::Object>(class_linker_)); |
| 8670 | DCHECK_EQ(klass->NumVirtualMethods(), mirror::Object::kVTableLength); |
| 8671 | static_assert(IsUint<16>(mirror::Object::kVTableLength)); |
Vladimir Marko | 0441d20 | 2022-02-18 13:55:15 +0000 | [diff] [blame] | 8672 | ObjPtr<mirror::PointerArray> vtable = AllocPointerArray(self, mirror::Object::kVTableLength); |
Vladimir Marko | b91402f | 2021-12-21 15:55:06 +0000 | [diff] [blame] | 8673 | if (UNLIKELY(vtable == nullptr)) { |
| 8674 | self->AssertPendingOOMException(); |
| 8675 | return false; |
| 8676 | } |
| 8677 | for (size_t i = 0; i < mirror::Object::kVTableLength; ++i) { |
| 8678 | ArtMethod* virtual_method = klass->GetVirtualMethodDuringLinking(i, kPointerSize); |
| 8679 | vtable->SetElementPtrSize(i, virtual_method, kPointerSize); |
| 8680 | virtual_method->SetMethodIndex(i); |
| 8681 | } |
| 8682 | klass->SetVTable(vtable); |
| 8683 | InitializeObjectVirtualMethodHashes( |
| 8684 | klass.Get(), |
| 8685 | kPointerSize, |
| 8686 | ArrayRef<uint32_t>(class_linker_->object_virtual_method_hashes_)); |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 8687 | // The interface table is already allocated but there are no interface methods to link. |
| 8688 | DCHECK(klass->GetIfTable() != nullptr); |
| 8689 | DCHECK_EQ(klass->GetIfTableCount(), 0); |
Vladimir Marko | b91402f | 2021-12-21 15:55:06 +0000 | [diff] [blame] | 8690 | return true; |
| 8691 | } |
| 8692 | |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 8693 | // Populate the class vtable and itable. Compute return type indices. |
| 8694 | bool ClassLinker::LinkMethods(Thread* self, |
| 8695 | Handle<mirror::Class> klass, |
| 8696 | Handle<mirror::ObjectArray<mirror::Class>> interfaces, |
| 8697 | bool* out_new_conflict, |
| 8698 | ArtMethod** out_imt) { |
| 8699 | self->AllowThreadSuspension(); |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 8700 | // Link virtual methods then interface methods. |
| 8701 | Runtime* const runtime = Runtime::Current(); |
Vladimir Marko | b91402f | 2021-12-21 15:55:06 +0000 | [diff] [blame] | 8702 | if (LIKELY(GetImagePointerSize() == kRuntimePointerSize)) { |
| 8703 | LinkMethodsHelper<kRuntimePointerSize> helper(this, klass, self, runtime); |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 8704 | return helper.LinkMethods(self, klass, interfaces, out_new_conflict, out_imt); |
Vladimir Marko | b91402f | 2021-12-21 15:55:06 +0000 | [diff] [blame] | 8705 | } else { |
| 8706 | constexpr PointerSize kOtherPointerSize = |
| 8707 | (kRuntimePointerSize == PointerSize::k64) ? PointerSize::k32 : PointerSize::k64; |
| 8708 | LinkMethodsHelper<kOtherPointerSize> helper(this, klass, self, runtime); |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 8709 | return helper.LinkMethods(self, klass, interfaces, out_new_conflict, out_imt); |
Vladimir Marko | b91402f | 2021-12-21 15:55:06 +0000 | [diff] [blame] | 8710 | } |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 8711 | } |
| 8712 | |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8713 | class ClassLinker::LinkFieldsHelper { |
| 8714 | public: |
| 8715 | static bool LinkFields(ClassLinker* class_linker, |
| 8716 | Thread* self, |
| 8717 | Handle<mirror::Class> klass, |
| 8718 | bool is_static, |
| 8719 | size_t* class_size) |
| 8720 | REQUIRES_SHARED(Locks::mutator_lock_); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 8721 | |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8722 | private: |
| 8723 | enum class FieldTypeOrder : uint16_t; |
| 8724 | class FieldGaps; |
| 8725 | |
| 8726 | struct FieldTypeOrderAndIndex { |
| 8727 | FieldTypeOrder field_type_order; |
| 8728 | uint16_t field_index; |
| 8729 | }; |
| 8730 | |
| 8731 | static FieldTypeOrder FieldTypeOrderFromFirstDescriptorCharacter(char first_char); |
| 8732 | |
| 8733 | template <size_t kSize> |
| 8734 | static MemberOffset AssignFieldOffset(ArtField* field, MemberOffset field_offset) |
| 8735 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 8736 | }; |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 8737 | |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8738 | // We use the following order of field types for assigning offsets. |
Roland Levillain | 87553ec | 2022-07-15 17:49:30 +0100 | [diff] [blame] | 8739 | // Some fields can be shuffled forward to fill gaps, see |
| 8740 | // `ClassLinker::LinkFieldsHelper::LinkFields()`. |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8741 | enum class ClassLinker::LinkFieldsHelper::FieldTypeOrder : uint16_t { |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8742 | kReference = 0u, |
| 8743 | kLong, |
| 8744 | kDouble, |
| 8745 | kInt, |
| 8746 | kFloat, |
| 8747 | kChar, |
| 8748 | kShort, |
| 8749 | kBoolean, |
| 8750 | kByte, |
| 8751 | |
| 8752 | kLast64BitType = kDouble, |
| 8753 | kLast32BitType = kFloat, |
| 8754 | kLast16BitType = kShort, |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 8755 | }; |
| 8756 | |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8757 | ALWAYS_INLINE |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8758 | ClassLinker::LinkFieldsHelper::FieldTypeOrder |
| 8759 | ClassLinker::LinkFieldsHelper::FieldTypeOrderFromFirstDescriptorCharacter(char first_char) { |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8760 | switch (first_char) { |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8761 | case 'J': |
| 8762 | return FieldTypeOrder::kLong; |
| 8763 | case 'D': |
| 8764 | return FieldTypeOrder::kDouble; |
| 8765 | case 'I': |
| 8766 | return FieldTypeOrder::kInt; |
| 8767 | case 'F': |
| 8768 | return FieldTypeOrder::kFloat; |
| 8769 | case 'C': |
| 8770 | return FieldTypeOrder::kChar; |
| 8771 | case 'S': |
| 8772 | return FieldTypeOrder::kShort; |
| 8773 | case 'Z': |
| 8774 | return FieldTypeOrder::kBoolean; |
| 8775 | case 'B': |
| 8776 | return FieldTypeOrder::kByte; |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8777 | default: |
| 8778 | DCHECK(first_char == 'L' || first_char == '[') << first_char; |
| 8779 | return FieldTypeOrder::kReference; |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8780 | } |
| 8781 | } |
| 8782 | |
| 8783 | // Gaps where we can insert fields in object layout. |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8784 | class ClassLinker::LinkFieldsHelper::FieldGaps { |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8785 | public: |
| 8786 | template <uint32_t kSize> |
| 8787 | ALWAYS_INLINE MemberOffset AlignFieldOffset(MemberOffset field_offset) { |
| 8788 | static_assert(kSize == 2u || kSize == 4u || kSize == 8u); |
| 8789 | if (!IsAligned<kSize>(field_offset.Uint32Value())) { |
| 8790 | uint32_t gap_start = field_offset.Uint32Value(); |
| 8791 | field_offset = MemberOffset(RoundUp(gap_start, kSize)); |
| 8792 | AddGaps<kSize - 1u>(gap_start, field_offset.Uint32Value()); |
| 8793 | } |
| 8794 | return field_offset; |
| 8795 | } |
| 8796 | |
| 8797 | template <uint32_t kSize> |
| 8798 | bool HasGap() const { |
| 8799 | static_assert(kSize == 1u || kSize == 2u || kSize == 4u); |
| 8800 | return (kSize == 1u && gap1_offset_ != kNoOffset) || |
| 8801 | (kSize <= 2u && gap2_offset_ != kNoOffset) || |
| 8802 | gap4_offset_ != kNoOffset; |
| 8803 | } |
| 8804 | |
| 8805 | template <uint32_t kSize> |
| 8806 | MemberOffset ReleaseGap() { |
| 8807 | static_assert(kSize == 1u || kSize == 2u || kSize == 4u); |
| 8808 | uint32_t result; |
| 8809 | if (kSize == 1u && gap1_offset_ != kNoOffset) { |
| 8810 | DCHECK(gap2_offset_ == kNoOffset || gap2_offset_ > gap1_offset_); |
| 8811 | DCHECK(gap4_offset_ == kNoOffset || gap4_offset_ > gap1_offset_); |
| 8812 | result = gap1_offset_; |
| 8813 | gap1_offset_ = kNoOffset; |
| 8814 | } else if (kSize <= 2u && gap2_offset_ != kNoOffset) { |
| 8815 | DCHECK(gap4_offset_ == kNoOffset || gap4_offset_ > gap2_offset_); |
| 8816 | result = gap2_offset_; |
| 8817 | gap2_offset_ = kNoOffset; |
| 8818 | if (kSize < 2u) { |
| 8819 | AddGaps<1u>(result + kSize, result + 2u); |
| 8820 | } |
| 8821 | } else { |
| 8822 | DCHECK_NE(gap4_offset_, kNoOffset); |
| 8823 | result = gap4_offset_; |
| 8824 | gap4_offset_ = kNoOffset; |
| 8825 | if (kSize < 4u) { |
| 8826 | AddGaps<kSize | 2u>(result + kSize, result + 4u); |
| 8827 | } |
| 8828 | } |
| 8829 | return MemberOffset(result); |
| 8830 | } |
| 8831 | |
| 8832 | private: |
| 8833 | template <uint32_t kGapsToCheck> |
| 8834 | void AddGaps(uint32_t gap_start, uint32_t gap_end) { |
| 8835 | if ((kGapsToCheck & 1u) != 0u) { |
| 8836 | DCHECK_LT(gap_start, gap_end); |
| 8837 | DCHECK_ALIGNED(gap_end, 2u); |
| 8838 | if ((gap_start & 1u) != 0u) { |
| 8839 | DCHECK_EQ(gap1_offset_, kNoOffset); |
| 8840 | gap1_offset_ = gap_start; |
| 8841 | gap_start += 1u; |
| 8842 | if (kGapsToCheck == 1u || gap_start == gap_end) { |
| 8843 | DCHECK_EQ(gap_start, gap_end); |
| 8844 | return; |
| 8845 | } |
| 8846 | } |
| 8847 | } |
| 8848 | |
| 8849 | if ((kGapsToCheck & 2u) != 0u) { |
| 8850 | DCHECK_LT(gap_start, gap_end); |
| 8851 | DCHECK_ALIGNED(gap_start, 2u); |
| 8852 | DCHECK_ALIGNED(gap_end, 4u); |
| 8853 | if ((gap_start & 2u) != 0u) { |
| 8854 | DCHECK_EQ(gap2_offset_, kNoOffset); |
| 8855 | gap2_offset_ = gap_start; |
| 8856 | gap_start += 2u; |
| 8857 | if (kGapsToCheck <= 3u || gap_start == gap_end) { |
| 8858 | DCHECK_EQ(gap_start, gap_end); |
| 8859 | return; |
| 8860 | } |
| 8861 | } |
| 8862 | } |
| 8863 | |
| 8864 | if ((kGapsToCheck & 4u) != 0u) { |
| 8865 | DCHECK_LT(gap_start, gap_end); |
| 8866 | DCHECK_ALIGNED(gap_start, 4u); |
| 8867 | DCHECK_ALIGNED(gap_end, 8u); |
| 8868 | DCHECK_EQ(gap_start + 4u, gap_end); |
| 8869 | DCHECK_EQ(gap4_offset_, kNoOffset); |
| 8870 | gap4_offset_ = gap_start; |
| 8871 | return; |
| 8872 | } |
| 8873 | |
| 8874 | DCHECK(false) << "Remaining gap: " << gap_start << " to " << gap_end |
| 8875 | << " after checking " << kGapsToCheck; |
| 8876 | } |
| 8877 | |
| 8878 | static constexpr uint32_t kNoOffset = static_cast<uint32_t>(-1); |
| 8879 | |
| 8880 | uint32_t gap4_offset_ = kNoOffset; |
| 8881 | uint32_t gap2_offset_ = kNoOffset; |
| 8882 | uint32_t gap1_offset_ = kNoOffset; |
| 8883 | }; |
| 8884 | |
| 8885 | template <size_t kSize> |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8886 | ALWAYS_INLINE |
| 8887 | MemberOffset ClassLinker::LinkFieldsHelper::AssignFieldOffset(ArtField* field, |
| 8888 | MemberOffset field_offset) { |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8889 | DCHECK_ALIGNED(field_offset.Uint32Value(), kSize); |
| 8890 | DCHECK_EQ(Primitive::ComponentSize(field->GetTypeAsPrimitiveType()), kSize); |
| 8891 | field->SetOffset(field_offset); |
| 8892 | return MemberOffset(field_offset.Uint32Value() + kSize); |
| 8893 | } |
| 8894 | |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8895 | bool ClassLinker::LinkFieldsHelper::LinkFields(ClassLinker* class_linker, |
| 8896 | Thread* self, |
| 8897 | Handle<mirror::Class> klass, |
| 8898 | bool is_static, |
| 8899 | size_t* class_size) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 8900 | self->AllowThreadSuspension(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8901 | const size_t num_fields = is_static ? klass->NumStaticFields() : klass->NumInstanceFields(); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 8902 | LengthPrefixedArray<ArtField>* const fields = is_static ? klass->GetSFieldsPtr() : |
| 8903 | klass->GetIFieldsPtr(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8904 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 8905 | // Initialize field_offset |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 8906 | MemberOffset field_offset(0); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 8907 | if (is_static) { |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8908 | field_offset = klass->GetFirstReferenceStaticFieldOffsetDuringLinking( |
| 8909 | class_linker->GetImagePointerSize()); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 8910 | } else { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 8911 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 8912 | if (super_class != nullptr) { |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 8913 | CHECK(super_class->IsResolved()) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 8914 | << klass->PrettyClass() << " " << super_class->PrettyClass(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8915 | field_offset = MemberOffset(super_class->GetObjectSize()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8916 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8917 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8918 | |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 8919 | CHECK_EQ(num_fields == 0, fields == nullptr) << klass->PrettyClass(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8920 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 8921 | // we want a relatively stable order so that adding new fields |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 8922 | // minimizes disruption of C++ version such as Class and Method. |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 8923 | // |
| 8924 | // The overall sort order order is: |
| 8925 | // 1) All object reference fields, sorted alphabetically. |
| 8926 | // 2) All java long (64-bit) integer fields, sorted alphabetically. |
| 8927 | // 3) All java double (64-bit) floating point fields, sorted alphabetically. |
| 8928 | // 4) All java int (32-bit) integer fields, sorted alphabetically. |
| 8929 | // 5) All java float (32-bit) floating point fields, sorted alphabetically. |
| 8930 | // 6) All java char (16-bit) integer fields, sorted alphabetically. |
| 8931 | // 7) All java short (16-bit) integer fields, sorted alphabetically. |
| 8932 | // 8) All java boolean (8-bit) integer fields, sorted alphabetically. |
| 8933 | // 9) All java byte (8-bit) integer fields, sorted alphabetically. |
| 8934 | // |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8935 | // (References are first to increase the chance of reference visiting |
| 8936 | // being able to take a fast path using a bitmap of references at the |
| 8937 | // start of the object, see `Class::reference_instance_offsets_`.) |
| 8938 | // |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8939 | // Once the fields are sorted in this order we will attempt to fill any gaps |
| 8940 | // that might be present in the memory layout of the structure. |
| 8941 | // Note that we shall not fill gaps between the superclass fields. |
| 8942 | |
| 8943 | // Collect fields and their "type order index" (see numbered points above). |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 8944 | const char* old_no_suspend_cause = self->StartAssertNoThreadSuspension( |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8945 | "Using plain ArtField references"); |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8946 | constexpr size_t kStackBufferEntries = 64; // Avoid allocations for small number of fields. |
| 8947 | FieldTypeOrderAndIndex stack_buffer[kStackBufferEntries]; |
| 8948 | std::vector<FieldTypeOrderAndIndex> heap_buffer; |
| 8949 | ArrayRef<FieldTypeOrderAndIndex> sorted_fields; |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8950 | if (num_fields <= kStackBufferEntries) { |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8951 | sorted_fields = ArrayRef<FieldTypeOrderAndIndex>(stack_buffer, num_fields); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8952 | } else { |
| 8953 | heap_buffer.resize(num_fields); |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8954 | sorted_fields = ArrayRef<FieldTypeOrderAndIndex>(heap_buffer); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 8955 | } |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 8956 | size_t num_reference_fields = 0; |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8957 | size_t primitive_fields_start = num_fields; |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8958 | DCHECK_LE(num_fields, 1u << 16); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8959 | for (size_t i = 0; i != num_fields; ++i) { |
| 8960 | ArtField* field = &fields->At(i); |
| 8961 | const char* descriptor = field->GetTypeDescriptor(); |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8962 | FieldTypeOrder field_type_order = FieldTypeOrderFromFirstDescriptorCharacter(descriptor[0]); |
| 8963 | uint16_t field_index = dchecked_integral_cast<uint16_t>(i); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8964 | // Insert references to the start, other fields to the end. |
| 8965 | DCHECK_LT(num_reference_fields, primitive_fields_start); |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8966 | if (field_type_order == FieldTypeOrder::kReference) { |
| 8967 | sorted_fields[num_reference_fields] = { field_type_order, field_index }; |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8968 | ++num_reference_fields; |
| 8969 | } else { |
| 8970 | --primitive_fields_start; |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8971 | sorted_fields[primitive_fields_start] = { field_type_order, field_index }; |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8972 | } |
| 8973 | } |
| 8974 | DCHECK_EQ(num_reference_fields, primitive_fields_start); |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 8975 | |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8976 | // Reference fields are already sorted by field index (and dex field index). |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8977 | DCHECK(std::is_sorted( |
| 8978 | sorted_fields.begin(), |
| 8979 | sorted_fields.begin() + num_reference_fields, |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8980 | [fields](const auto& lhs, const auto& rhs) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 8981 | ArtField* lhs_field = &fields->At(lhs.field_index); |
| 8982 | ArtField* rhs_field = &fields->At(rhs.field_index); |
| 8983 | CHECK_EQ(lhs_field->GetTypeAsPrimitiveType(), Primitive::kPrimNot); |
| 8984 | CHECK_EQ(rhs_field->GetTypeAsPrimitiveType(), Primitive::kPrimNot); |
| 8985 | CHECK_EQ(lhs_field->GetDexFieldIndex() < rhs_field->GetDexFieldIndex(), |
| 8986 | lhs.field_index < rhs.field_index); |
| 8987 | return lhs_field->GetDexFieldIndex() < rhs_field->GetDexFieldIndex(); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8988 | })); |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8989 | // 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] | 8990 | DCHECK(std::is_sorted( |
| 8991 | sorted_fields.begin() + primitive_fields_start, |
| 8992 | sorted_fields.end(), |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8993 | [fields](const auto& lhs, const auto& rhs) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 8994 | ArtField* lhs_field = &fields->At(lhs.field_index); |
| 8995 | ArtField* rhs_field = &fields->At(rhs.field_index); |
| 8996 | CHECK_NE(lhs_field->GetTypeAsPrimitiveType(), Primitive::kPrimNot); |
| 8997 | CHECK_NE(rhs_field->GetTypeAsPrimitiveType(), Primitive::kPrimNot); |
| 8998 | CHECK_EQ(lhs_field->GetDexFieldIndex() > rhs_field->GetDexFieldIndex(), |
| 8999 | lhs.field_index > rhs.field_index); |
| 9000 | return lhs.field_index > rhs.field_index; |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 9001 | })); |
| 9002 | // Sort the primitive fields by the field type order, then field index. |
| 9003 | std::sort(sorted_fields.begin() + primitive_fields_start, |
| 9004 | sorted_fields.end(), |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 9005 | [](const auto& lhs, const auto& rhs) { |
| 9006 | if (lhs.field_type_order != rhs.field_type_order) { |
| 9007 | return lhs.field_type_order < rhs.field_type_order; |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 9008 | } else { |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 9009 | return lhs.field_index < rhs.field_index; |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 9010 | } |
| 9011 | }); |
| 9012 | // Primitive fields are now sorted by field size (descending), then type, then field index. |
| 9013 | DCHECK(std::is_sorted( |
| 9014 | sorted_fields.begin() + primitive_fields_start, |
| 9015 | sorted_fields.end(), |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 9016 | [fields](const auto& lhs, const auto& rhs) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 9017 | ArtField* lhs_field = &fields->At(lhs.field_index); |
| 9018 | ArtField* rhs_field = &fields->At(rhs.field_index); |
| 9019 | Primitive::Type lhs_type = lhs_field->GetTypeAsPrimitiveType(); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 9020 | CHECK_NE(lhs_type, Primitive::kPrimNot); |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 9021 | Primitive::Type rhs_type = rhs_field->GetTypeAsPrimitiveType(); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 9022 | CHECK_NE(rhs_type, Primitive::kPrimNot); |
| 9023 | if (lhs_type != rhs_type) { |
| 9024 | size_t lhs_size = Primitive::ComponentSize(lhs_type); |
| 9025 | size_t rhs_size = Primitive::ComponentSize(rhs_type); |
| 9026 | return (lhs_size != rhs_size) ? (lhs_size > rhs_size) : (lhs_type < rhs_type); |
| 9027 | } else { |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 9028 | return lhs_field->GetDexFieldIndex() < rhs_field->GetDexFieldIndex(); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 9029 | } |
| 9030 | })); |
| 9031 | |
| 9032 | // Process reference fields. |
| 9033 | FieldGaps field_gaps; |
| 9034 | size_t index = 0u; |
| 9035 | if (num_reference_fields != 0u) { |
| 9036 | constexpr size_t kReferenceSize = sizeof(mirror::HeapReference<mirror::Object>); |
| 9037 | field_offset = field_gaps.AlignFieldOffset<kReferenceSize>(field_offset); |
| 9038 | for (; index != num_reference_fields; ++index) { |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 9039 | ArtField* field = &fields->At(sorted_fields[index].field_index); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 9040 | field_offset = AssignFieldOffset<kReferenceSize>(field, field_offset); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 9041 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 9042 | } |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 9043 | // Process 64-bit fields. |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 9044 | if (index != num_fields && |
| 9045 | sorted_fields[index].field_type_order <= FieldTypeOrder::kLast64BitType) { |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 9046 | field_offset = field_gaps.AlignFieldOffset<8u>(field_offset); |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 9047 | while (index != num_fields && |
| 9048 | sorted_fields[index].field_type_order <= FieldTypeOrder::kLast64BitType) { |
| 9049 | ArtField* field = &fields->At(sorted_fields[index].field_index); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 9050 | field_offset = AssignFieldOffset<8u>(field, field_offset); |
| 9051 | ++index; |
Mathieu Chartier | 5565029 | 2020-09-14 12:21:04 -0700 | [diff] [blame] | 9052 | } |
Mathieu Chartier | 5565029 | 2020-09-14 12:21:04 -0700 | [diff] [blame] | 9053 | } |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 9054 | // Process 32-bit fields. |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 9055 | if (index != num_fields && |
| 9056 | sorted_fields[index].field_type_order <= FieldTypeOrder::kLast32BitType) { |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 9057 | field_offset = field_gaps.AlignFieldOffset<4u>(field_offset); |
| 9058 | if (field_gaps.HasGap<4u>()) { |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 9059 | ArtField* field = &fields->At(sorted_fields[index].field_index); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 9060 | AssignFieldOffset<4u>(field, field_gaps.ReleaseGap<4u>()); // Ignore return value. |
| 9061 | ++index; |
| 9062 | DCHECK(!field_gaps.HasGap<4u>()); // There can be only one gap for a 32-bit field. |
| 9063 | } |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 9064 | while (index != num_fields && |
| 9065 | sorted_fields[index].field_type_order <= FieldTypeOrder::kLast32BitType) { |
| 9066 | ArtField* field = &fields->At(sorted_fields[index].field_index); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 9067 | field_offset = AssignFieldOffset<4u>(field, field_offset); |
| 9068 | ++index; |
| 9069 | } |
| 9070 | } |
| 9071 | // Process 16-bit fields. |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 9072 | if (index != num_fields && |
| 9073 | sorted_fields[index].field_type_order <= FieldTypeOrder::kLast16BitType) { |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 9074 | field_offset = field_gaps.AlignFieldOffset<2u>(field_offset); |
| 9075 | while (index != num_fields && |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 9076 | sorted_fields[index].field_type_order <= FieldTypeOrder::kLast16BitType && |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 9077 | field_gaps.HasGap<2u>()) { |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 9078 | ArtField* field = &fields->At(sorted_fields[index].field_index); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 9079 | AssignFieldOffset<2u>(field, field_gaps.ReleaseGap<2u>()); // Ignore return value. |
| 9080 | ++index; |
| 9081 | } |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 9082 | while (index != num_fields && |
| 9083 | sorted_fields[index].field_type_order <= FieldTypeOrder::kLast16BitType) { |
| 9084 | ArtField* field = &fields->At(sorted_fields[index].field_index); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 9085 | field_offset = AssignFieldOffset<2u>(field, field_offset); |
| 9086 | ++index; |
| 9087 | } |
| 9088 | } |
| 9089 | // Process 8-bit fields. |
| 9090 | for (; index != num_fields && field_gaps.HasGap<1u>(); ++index) { |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 9091 | ArtField* field = &fields->At(sorted_fields[index].field_index); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 9092 | AssignFieldOffset<1u>(field, field_gaps.ReleaseGap<1u>()); // Ignore return value. |
| 9093 | } |
| 9094 | for (; index != num_fields; ++index) { |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 9095 | ArtField* field = &fields->At(sorted_fields[index].field_index); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 9096 | field_offset = AssignFieldOffset<1u>(field, field_offset); |
| 9097 | } |
| 9098 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 9099 | self->EndAssertNoThreadSuspension(old_no_suspend_cause); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 9100 | |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 9101 | // We lie to the GC about the java.lang.ref.Reference.referent field, so it doesn't scan it. |
Santiago Aboy Solanes | 6cdabe1 | 2022-02-18 15:27:43 +0000 | [diff] [blame] | 9102 | DCHECK_IMPLIES(class_linker->init_done_, !klass->DescriptorEquals("Ljava/lang/ref/Reference;")); |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 9103 | if (!is_static && |
| 9104 | UNLIKELY(!class_linker->init_done_) && |
| 9105 | klass->DescriptorEquals("Ljava/lang/ref/Reference;")) { |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 9106 | // We know there are no non-reference fields in the Reference classes, and we know |
| 9107 | // that 'referent' is alphabetically last, so this is easy... |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 9108 | CHECK_EQ(num_reference_fields, num_fields) << klass->PrettyClass(); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 9109 | CHECK_STREQ(fields->At(num_fields - 1).GetName(), "referent") |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 9110 | << klass->PrettyClass(); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 9111 | --num_reference_fields; |
| 9112 | } |
| 9113 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 9114 | size_t size = field_offset.Uint32Value(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 9115 | // Update klass |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 9116 | if (is_static) { |
| 9117 | klass->SetNumReferenceStaticFields(num_reference_fields); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 9118 | *class_size = size; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 9119 | } else { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 9120 | klass->SetNumReferenceInstanceFields(num_reference_fields); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 9121 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 9122 | if (num_reference_fields == 0 || super_class == nullptr) { |
| 9123 | // 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] | 9124 | // super_class is null iff the class is java.lang.Object. |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 9125 | if (super_class == nullptr || |
| 9126 | (super_class->GetClassFlags() & mirror::kClassFlagNoReferenceFields) != 0) { |
| 9127 | klass->SetClassFlags(klass->GetClassFlags() | mirror::kClassFlagNoReferenceFields); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 9128 | } |
| 9129 | } |
| 9130 | if (kIsDebugBuild) { |
| 9131 | DCHECK_EQ(super_class == nullptr, klass->DescriptorEquals("Ljava/lang/Object;")); |
| 9132 | size_t total_reference_instance_fields = 0; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 9133 | ObjPtr<mirror::Class> cur_super = klass.Get(); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 9134 | while (cur_super != nullptr) { |
| 9135 | total_reference_instance_fields += cur_super->NumReferenceInstanceFieldsDuringLinking(); |
| 9136 | cur_super = cur_super->GetSuperClass(); |
| 9137 | } |
| 9138 | if (super_class == nullptr) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 9139 | CHECK_EQ(total_reference_instance_fields, 1u) << klass->PrettyDescriptor(); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 9140 | } else { |
| 9141 | // Check that there is at least num_reference_fields other than Object.class. |
| 9142 | CHECK_GE(total_reference_instance_fields, 1u + num_reference_fields) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 9143 | << klass->PrettyClass(); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 9144 | } |
| 9145 | } |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 9146 | if (!klass->IsVariableSize()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 9147 | std::string temp; |
| 9148 | DCHECK_GE(size, sizeof(mirror::Object)) << klass->GetDescriptor(&temp); |
| 9149 | size_t previous_size = klass->GetObjectSize(); |
| 9150 | if (previous_size != 0) { |
| 9151 | // Make sure that we didn't originally have an incorrect size. |
| 9152 | CHECK_EQ(previous_size, size) << klass->GetDescriptor(&temp); |
Mathieu Chartier | 79b4f38 | 2013-10-23 15:21:37 -0700 | [diff] [blame] | 9153 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 9154 | klass->SetObjectSize(size); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 9155 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 9156 | } |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 9157 | |
| 9158 | if (kIsDebugBuild) { |
| 9159 | // Make sure that the fields array is ordered by name but all reference |
| 9160 | // offsets are at the beginning as far as alignment allows. |
| 9161 | MemberOffset start_ref_offset = is_static |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 9162 | ? klass->GetFirstReferenceStaticFieldOffsetDuringLinking(class_linker->image_pointer_size_) |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 9163 | : klass->GetFirstReferenceInstanceFieldOffset(); |
| 9164 | MemberOffset end_ref_offset(start_ref_offset.Uint32Value() + |
| 9165 | num_reference_fields * |
| 9166 | sizeof(mirror::HeapReference<mirror::Object>)); |
| 9167 | MemberOffset current_ref_offset = start_ref_offset; |
| 9168 | for (size_t i = 0; i < num_fields; i++) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 9169 | ArtField* field = &fields->At(i); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 9170 | VLOG(class_linker) << "LinkFields: " << (is_static ? "static" : "instance") |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 9171 | << " class=" << klass->PrettyClass() << " field=" << field->PrettyField() |
| 9172 | << " offset=" << field->GetOffsetDuringLinking(); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 9173 | if (i != 0) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 9174 | ArtField* const prev_field = &fields->At(i - 1); |
Vladimir Marko | 7a7c1db | 2014-11-17 15:13:34 +0000 | [diff] [blame] | 9175 | // NOTE: The field names can be the same. This is not possible in the Java language |
| 9176 | // 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] | 9177 | DCHECK_LE(strcmp(prev_field->GetName(), field->GetName()), 0); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 9178 | } |
| 9179 | Primitive::Type type = field->GetTypeAsPrimitiveType(); |
| 9180 | bool is_primitive = type != Primitive::kPrimNot; |
| 9181 | if (klass->DescriptorEquals("Ljava/lang/ref/Reference;") && |
| 9182 | strcmp("referent", field->GetName()) == 0) { |
| 9183 | is_primitive = true; // We lied above, so we have to expect a lie here. |
| 9184 | } |
| 9185 | MemberOffset offset = field->GetOffsetDuringLinking(); |
| 9186 | if (is_primitive) { |
| 9187 | if (offset.Uint32Value() < end_ref_offset.Uint32Value()) { |
| 9188 | // Shuffled before references. |
| 9189 | size_t type_size = Primitive::ComponentSize(type); |
| 9190 | CHECK_LT(type_size, sizeof(mirror::HeapReference<mirror::Object>)); |
| 9191 | CHECK_LT(offset.Uint32Value(), start_ref_offset.Uint32Value()); |
| 9192 | CHECK_LE(offset.Uint32Value() + type_size, start_ref_offset.Uint32Value()); |
| 9193 | CHECK(!IsAligned<sizeof(mirror::HeapReference<mirror::Object>)>(offset.Uint32Value())); |
| 9194 | } |
| 9195 | } else { |
| 9196 | CHECK_EQ(current_ref_offset.Uint32Value(), offset.Uint32Value()); |
| 9197 | current_ref_offset = MemberOffset(current_ref_offset.Uint32Value() + |
| 9198 | sizeof(mirror::HeapReference<mirror::Object>)); |
| 9199 | } |
| 9200 | } |
| 9201 | CHECK_EQ(current_ref_offset.Uint32Value(), end_ref_offset.Uint32Value()); |
| 9202 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 9203 | return true; |
| 9204 | } |
| 9205 | |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 9206 | bool ClassLinker::LinkInstanceFields(Thread* self, Handle<mirror::Class> klass) { |
| 9207 | CHECK(klass != nullptr); |
| 9208 | return LinkFieldsHelper::LinkFields(this, self, klass, false, nullptr); |
| 9209 | } |
| 9210 | |
| 9211 | bool ClassLinker::LinkStaticFields(Thread* self, Handle<mirror::Class> klass, size_t* class_size) { |
| 9212 | CHECK(klass != nullptr); |
| 9213 | return LinkFieldsHelper::LinkFields(this, self, klass, true, class_size); |
| 9214 | } |
| 9215 | |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 9216 | // Set the bitmap of reference instance field offsets. |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 9217 | void ClassLinker::CreateReferenceInstanceOffsets(Handle<mirror::Class> klass) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 9218 | uint32_t reference_offsets = 0; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 9219 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 9220 | // 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] | 9221 | if (super_class != nullptr) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 9222 | reference_offsets = super_class->GetReferenceInstanceOffsets(); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 9223 | // Compute reference offsets unless our superclass overflowed. |
| 9224 | if (reference_offsets != mirror::Class::kClassWalkSuper) { |
| 9225 | size_t num_reference_fields = klass->NumReferenceInstanceFieldsDuringLinking(); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 9226 | if (num_reference_fields != 0u) { |
| 9227 | // All of the fields that contain object references are guaranteed be grouped in memory |
| 9228 | // starting at an appropriately aligned address after super class object data. |
| 9229 | uint32_t start_offset = RoundUp(super_class->GetObjectSize(), |
| 9230 | sizeof(mirror::HeapReference<mirror::Object>)); |
| 9231 | uint32_t start_bit = (start_offset - mirror::kObjectHeaderSize) / |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 9232 | sizeof(mirror::HeapReference<mirror::Object>); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 9233 | if (start_bit + num_reference_fields > 32) { |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 9234 | reference_offsets = mirror::Class::kClassWalkSuper; |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 9235 | } else { |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 9236 | reference_offsets |= (0xffffffffu << start_bit) & |
| 9237 | (0xffffffffu >> (32 - (start_bit + num_reference_fields))); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 9238 | } |
| 9239 | } |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 9240 | } |
| 9241 | } |
Mingyao Yang | faff0f0 | 2014-09-10 12:03:22 -0700 | [diff] [blame] | 9242 | klass->SetReferenceInstanceOffsets(reference_offsets); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 9243 | } |
| 9244 | |
Vladimir Marko | 18090d1 | 2018-06-01 16:53:12 +0100 | [diff] [blame] | 9245 | ObjPtr<mirror::String> ClassLinker::DoResolveString(dex::StringIndex string_idx, |
| 9246 | ObjPtr<mirror::DexCache> dex_cache) { |
| 9247 | StackHandleScope<1> hs(Thread::Current()); |
| 9248 | Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(dex_cache)); |
| 9249 | return DoResolveString(string_idx, h_dex_cache); |
| 9250 | } |
| 9251 | |
| 9252 | ObjPtr<mirror::String> ClassLinker::DoResolveString(dex::StringIndex string_idx, |
| 9253 | Handle<mirror::DexCache> dex_cache) { |
Vladimir Marko | a64b52d | 2017-12-08 16:27:49 +0000 | [diff] [blame] | 9254 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 9255 | uint32_t utf16_length; |
| 9256 | const char* utf8_data = dex_file.StringDataAndUtf16LengthByIdx(string_idx, &utf16_length); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 9257 | ObjPtr<mirror::String> string = intern_table_->InternStrong(utf16_length, utf8_data); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 9258 | if (string != nullptr) { |
| 9259 | dex_cache->SetResolvedString(string_idx, string); |
| 9260 | } |
Vladimir Marko | 28e012a | 2017-12-07 11:22:59 +0000 | [diff] [blame] | 9261 | return string; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 9262 | } |
| 9263 | |
Vladimir Marko | 18090d1 | 2018-06-01 16:53:12 +0100 | [diff] [blame] | 9264 | ObjPtr<mirror::String> ClassLinker::DoLookupString(dex::StringIndex string_idx, |
| 9265 | ObjPtr<mirror::DexCache> dex_cache) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 9266 | DCHECK(dex_cache != nullptr); |
Vladimir Marko | a64b52d | 2017-12-08 16:27:49 +0000 | [diff] [blame] | 9267 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 9268 | uint32_t utf16_length; |
| 9269 | const char* utf8_data = dex_file.StringDataAndUtf16LengthByIdx(string_idx, &utf16_length); |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 9270 | ObjPtr<mirror::String> string = |
| 9271 | intern_table_->LookupStrong(Thread::Current(), utf16_length, utf8_data); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 9272 | if (string != nullptr) { |
| 9273 | dex_cache->SetResolvedString(string_idx, string); |
| 9274 | } |
Vladimir Marko | 28e012a | 2017-12-07 11:22:59 +0000 | [diff] [blame] | 9275 | return string; |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 9276 | } |
| 9277 | |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 9278 | ObjPtr<mirror::Class> ClassLinker::DoLookupResolvedType(dex::TypeIndex type_idx, |
Vladimir Marko | 09c5ca4 | 2018-05-31 15:15:31 +0100 | [diff] [blame] | 9279 | ObjPtr<mirror::Class> referrer) { |
| 9280 | return DoLookupResolvedType(type_idx, referrer->GetDexCache(), referrer->GetClassLoader()); |
| 9281 | } |
| 9282 | |
| 9283 | ObjPtr<mirror::Class> ClassLinker::DoLookupResolvedType(dex::TypeIndex type_idx, |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 9284 | ObjPtr<mirror::DexCache> dex_cache, |
| 9285 | ObjPtr<mirror::ClassLoader> class_loader) { |
Nicolas Geoffray | ab91eef | 2021-09-14 09:48:51 +0100 | [diff] [blame] | 9286 | DCHECK(dex_cache->GetClassLoader() == class_loader); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 9287 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
| 9288 | const char* descriptor = dex_file.StringByTypeIdx(type_idx); |
Vladimir Marko | c63d967 | 2021-03-31 15:50:39 +0100 | [diff] [blame] | 9289 | ObjPtr<mirror::Class> type = LookupResolvedType(descriptor, class_loader); |
| 9290 | if (type != nullptr) { |
| 9291 | DCHECK(type->IsResolved()); |
| 9292 | dex_cache->SetResolvedType(type_idx, type); |
| 9293 | } |
| 9294 | return type; |
| 9295 | } |
| 9296 | |
| 9297 | ObjPtr<mirror::Class> ClassLinker::LookupResolvedType(const char* descriptor, |
| 9298 | ObjPtr<mirror::ClassLoader> class_loader) { |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 9299 | DCHECK_NE(*descriptor, '\0') << "descriptor is empty string"; |
| 9300 | ObjPtr<mirror::Class> type = nullptr; |
| 9301 | if (descriptor[1] == '\0') { |
| 9302 | // only the descriptors of primitive types should be 1 character long, also avoid class lookup |
| 9303 | // for primitive classes that aren't backed by dex files. |
Vladimir Marko | 9186b18 | 2018-11-06 14:55:54 +0000 | [diff] [blame] | 9304 | type = LookupPrimitiveClass(descriptor[0]); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 9305 | } else { |
| 9306 | Thread* const self = Thread::Current(); |
| 9307 | DCHECK(self != nullptr); |
| 9308 | const size_t hash = ComputeModifiedUtf8Hash(descriptor); |
| 9309 | // Find the class in the loaded classes table. |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 9310 | type = LookupClass(self, descriptor, hash, class_loader); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 9311 | } |
Vladimir Marko | c63d967 | 2021-03-31 15:50:39 +0100 | [diff] [blame] | 9312 | return (type != nullptr && type->IsResolved()) ? type : nullptr; |
Mathieu Chartier | b890130 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 9313 | } |
| 9314 | |
Andreas Gampe | b0625e0 | 2019-05-01 12:43:31 -0700 | [diff] [blame] | 9315 | template <typename RefType> |
| 9316 | ObjPtr<mirror::Class> ClassLinker::DoResolveType(dex::TypeIndex type_idx, RefType referrer) { |
Vladimir Marko | 09c5ca4 | 2018-05-31 15:15:31 +0100 | [diff] [blame] | 9317 | StackHandleScope<2> hs(Thread::Current()); |
| 9318 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache())); |
| 9319 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(referrer->GetClassLoader())); |
| 9320 | return DoResolveType(type_idx, dex_cache, class_loader); |
| 9321 | } |
| 9322 | |
Andreas Gampe | 4835d21 | 2018-11-21 14:55:10 -0800 | [diff] [blame] | 9323 | // Instantiate the above. |
| 9324 | template ObjPtr<mirror::Class> ClassLinker::DoResolveType(dex::TypeIndex type_idx, |
| 9325 | ArtField* referrer); |
| 9326 | template ObjPtr<mirror::Class> ClassLinker::DoResolveType(dex::TypeIndex type_idx, |
| 9327 | ArtMethod* referrer); |
| 9328 | template ObjPtr<mirror::Class> ClassLinker::DoResolveType(dex::TypeIndex type_idx, |
| 9329 | ObjPtr<mirror::Class> referrer); |
| 9330 | |
Vladimir Marko | 09c5ca4 | 2018-05-31 15:15:31 +0100 | [diff] [blame] | 9331 | ObjPtr<mirror::Class> ClassLinker::DoResolveType(dex::TypeIndex type_idx, |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 9332 | Handle<mirror::DexCache> dex_cache, |
| 9333 | Handle<mirror::ClassLoader> class_loader) { |
Nicolas Geoffray | ab91eef | 2021-09-14 09:48:51 +0100 | [diff] [blame] | 9334 | DCHECK(dex_cache->GetClassLoader() == class_loader.Get()); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 9335 | Thread* self = Thread::Current(); |
| 9336 | const char* descriptor = dex_cache->GetDexFile()->StringByTypeIdx(type_idx); |
| 9337 | ObjPtr<mirror::Class> resolved = FindClass(self, descriptor, class_loader); |
| 9338 | if (resolved != nullptr) { |
| 9339 | // TODO: we used to throw here if resolved's class loader was not the |
| 9340 | // boot class loader. This was to permit different classes with the |
| 9341 | // same name to be loaded simultaneously by different loaders |
| 9342 | dex_cache->SetResolvedType(type_idx, resolved); |
| 9343 | } else { |
| 9344 | CHECK(self->IsExceptionPending()) |
| 9345 | << "Expected pending exception for failed resolution of: " << descriptor; |
| 9346 | // Convert a ClassNotFoundException to a NoClassDefFoundError. |
| 9347 | StackHandleScope<1> hs(self); |
| 9348 | Handle<mirror::Throwable> cause(hs.NewHandle(self->GetException())); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 9349 | if (cause->InstanceOf(GetClassRoot(ClassRoot::kJavaLangClassNotFoundException, this))) { |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 9350 | DCHECK(resolved == nullptr); // No Handle needed to preserve resolved. |
| 9351 | self->ClearException(); |
| 9352 | ThrowNoClassDefFoundError("Failed resolution of: %s", descriptor); |
| 9353 | self->GetException()->SetCause(cause.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 9354 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 9355 | } |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 9356 | DCHECK((resolved == nullptr) || resolved->IsResolved()) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 9357 | << resolved->PrettyDescriptor() << " " << resolved->GetStatus(); |
Vladimir Marko | 28e012a | 2017-12-07 11:22:59 +0000 | [diff] [blame] | 9358 | return resolved; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 9359 | } |
| 9360 | |
Nicolas Geoffray | ea179f4 | 2018-02-08 22:30:18 +0000 | [diff] [blame] | 9361 | ArtMethod* ClassLinker::FindResolvedMethod(ObjPtr<mirror::Class> klass, |
| 9362 | ObjPtr<mirror::DexCache> dex_cache, |
| 9363 | ObjPtr<mirror::ClassLoader> class_loader, |
| 9364 | uint32_t method_idx) { |
Nicolas Geoffray | ab91eef | 2021-09-14 09:48:51 +0100 | [diff] [blame] | 9365 | DCHECK(dex_cache->GetClassLoader() == class_loader); |
Nicolas Geoffray | ea179f4 | 2018-02-08 22:30:18 +0000 | [diff] [blame] | 9366 | // Search for the method using dex_cache and method_idx. The Class::Find*Method() |
| 9367 | // functions can optimize the search if the dex_cache is the same as the DexCache |
| 9368 | // of the class, with fall-back to name and signature search otherwise. |
| 9369 | ArtMethod* resolved = nullptr; |
| 9370 | if (klass->IsInterface()) { |
| 9371 | resolved = klass->FindInterfaceMethod(dex_cache, method_idx, image_pointer_size_); |
| 9372 | } else { |
| 9373 | resolved = klass->FindClassMethod(dex_cache, method_idx, image_pointer_size_); |
| 9374 | } |
| 9375 | DCHECK(resolved == nullptr || resolved->GetDeclaringClassUnchecked() != nullptr); |
David Brazdil | 8ce3bfa | 2018-03-12 18:01:18 +0000 | [diff] [blame] | 9376 | if (resolved != nullptr && |
Nicolas Geoffray | fedff51 | 2021-02-07 21:36:33 +0000 | [diff] [blame] | 9377 | // We pass AccessMethod::kNone instead of kLinking to not warn yet on the |
| 9378 | // access, as we'll be looking if the method can be accessed through an |
| 9379 | // interface. |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 9380 | hiddenapi::ShouldDenyAccessToMember(resolved, |
| 9381 | hiddenapi::AccessContext(class_loader, dex_cache), |
Nicolas Geoffray | fedff51 | 2021-02-07 21:36:33 +0000 | [diff] [blame] | 9382 | hiddenapi::AccessMethod::kNone)) { |
| 9383 | // The resolved method that we have found cannot be accessed due to |
| 9384 | // hiddenapi (typically it is declared up the hierarchy and is not an SDK |
| 9385 | // method). Try to find an interface method from the implemented interfaces which is |
Nicolas Geoffray | af61f50 | 2021-03-31 16:03:50 +0000 | [diff] [blame] | 9386 | // part of the SDK. |
| 9387 | ArtMethod* itf_method = klass->FindAccessibleInterfaceMethod(resolved, image_pointer_size_); |
Nicolas Geoffray | fedff51 | 2021-02-07 21:36:33 +0000 | [diff] [blame] | 9388 | if (itf_method == nullptr) { |
| 9389 | // No interface method. Call ShouldDenyAccessToMember again but this time |
| 9390 | // with AccessMethod::kLinking to ensure that an appropriate warning is |
| 9391 | // logged. |
| 9392 | hiddenapi::ShouldDenyAccessToMember(resolved, |
| 9393 | hiddenapi::AccessContext(class_loader, dex_cache), |
| 9394 | hiddenapi::AccessMethod::kLinking); |
| 9395 | resolved = nullptr; |
| 9396 | } else { |
| 9397 | // We found an interface method that is accessible, continue with the resolved method. |
| 9398 | } |
David Brazdil | 8ce3bfa | 2018-03-12 18:01:18 +0000 | [diff] [blame] | 9399 | } |
Nicolas Geoffray | ea179f4 | 2018-02-08 22:30:18 +0000 | [diff] [blame] | 9400 | if (resolved != nullptr) { |
| 9401 | // In case of jmvti, the dex file gets verified before being registered, so first |
| 9402 | // check if it's registered before checking class tables. |
| 9403 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Santiago Aboy Solanes | 6cdabe1 | 2022-02-18 15:27:43 +0000 | [diff] [blame] | 9404 | DCHECK_IMPLIES( |
| 9405 | IsDexFileRegistered(Thread::Current(), dex_file), |
| 9406 | FindClassTable(Thread::Current(), dex_cache) == ClassTableForClassLoader(class_loader)) |
Nicolas Geoffray | ea179f4 | 2018-02-08 22:30:18 +0000 | [diff] [blame] | 9407 | << "DexFile referrer: " << dex_file.GetLocation() |
| 9408 | << " ClassLoader: " << DescribeLoaders(class_loader, ""); |
| 9409 | // Be a good citizen and update the dex cache to speed subsequent calls. |
David Srbecky | 5de5efe | 2021-02-15 21:23:00 +0000 | [diff] [blame] | 9410 | dex_cache->SetResolvedMethod(method_idx, resolved); |
Nicolas Geoffray | befa309 | 2018-02-22 14:50:01 +0000 | [diff] [blame] | 9411 | // Disable the following invariant check as the verifier breaks it. b/73760543 |
| 9412 | // const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx); |
| 9413 | // DCHECK(LookupResolvedType(method_id.class_idx_, dex_cache, class_loader) != nullptr) |
| 9414 | // << "Method: " << resolved->PrettyMethod() << ", " |
| 9415 | // << "Class: " << klass->PrettyClass() << " (" << klass->GetStatus() << "), " |
| 9416 | // << "DexFile referrer: " << dex_file.GetLocation(); |
Nicolas Geoffray | ea179f4 | 2018-02-08 22:30:18 +0000 | [diff] [blame] | 9417 | } |
| 9418 | return resolved; |
| 9419 | } |
| 9420 | |
David Brazdil | 4525e0b | 2018-04-05 16:57:32 +0100 | [diff] [blame] | 9421 | // Returns true if `method` is either null or hidden. |
| 9422 | // Does not print any warnings if it is hidden. |
| 9423 | static bool CheckNoSuchMethod(ArtMethod* method, |
| 9424 | ObjPtr<mirror::DexCache> dex_cache, |
| 9425 | ObjPtr<mirror::ClassLoader> class_loader) |
| 9426 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Nicolas Geoffray | c07f488 | 2021-09-13 09:20:33 +0100 | [diff] [blame] | 9427 | DCHECK(dex_cache->GetClassLoader().Ptr() == class_loader.Ptr()); |
David Brazdil | 4525e0b | 2018-04-05 16:57:32 +0100 | [diff] [blame] | 9428 | return method == nullptr || |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 9429 | hiddenapi::ShouldDenyAccessToMember(method, |
| 9430 | hiddenapi::AccessContext(class_loader, dex_cache), |
| 9431 | hiddenapi::AccessMethod::kNone); // no warnings |
David Brazdil | 4525e0b | 2018-04-05 16:57:32 +0100 | [diff] [blame] | 9432 | } |
| 9433 | |
| 9434 | ArtMethod* ClassLinker::FindIncompatibleMethod(ObjPtr<mirror::Class> klass, |
| 9435 | ObjPtr<mirror::DexCache> dex_cache, |
| 9436 | ObjPtr<mirror::ClassLoader> class_loader, |
| 9437 | uint32_t method_idx) { |
Nicolas Geoffray | ab91eef | 2021-09-14 09:48:51 +0100 | [diff] [blame] | 9438 | DCHECK(dex_cache->GetClassLoader() == class_loader); |
David Brazdil | 4525e0b | 2018-04-05 16:57:32 +0100 | [diff] [blame] | 9439 | if (klass->IsInterface()) { |
| 9440 | ArtMethod* method = klass->FindClassMethod(dex_cache, method_idx, image_pointer_size_); |
| 9441 | return CheckNoSuchMethod(method, dex_cache, class_loader) ? nullptr : method; |
| 9442 | } else { |
| 9443 | // If there was an interface method with the same signature, we would have |
| 9444 | // found it in the "copied" methods. Only DCHECK that the interface method |
| 9445 | // really does not exist. |
| 9446 | if (kIsDebugBuild) { |
| 9447 | ArtMethod* method = |
| 9448 | klass->FindInterfaceMethod(dex_cache, method_idx, image_pointer_size_); |
Nicolas Geoffray | 2464218 | 2022-02-14 14:40:51 +0000 | [diff] [blame] | 9449 | CHECK(CheckNoSuchMethod(method, dex_cache, class_loader) || |
| 9450 | (klass->FindAccessibleInterfaceMethod(method, image_pointer_size_) == nullptr)); |
David Brazdil | 4525e0b | 2018-04-05 16:57:32 +0100 | [diff] [blame] | 9451 | } |
| 9452 | return nullptr; |
| 9453 | } |
| 9454 | } |
| 9455 | |
Vladimir Marko | 8901119 | 2017-12-11 13:45:05 +0000 | [diff] [blame] | 9456 | ArtMethod* ClassLinker::ResolveMethodWithoutInvokeType(uint32_t method_idx, |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 9457 | Handle<mirror::DexCache> dex_cache, |
| 9458 | Handle<mirror::ClassLoader> class_loader) { |
Nicolas Geoffray | ab91eef | 2021-09-14 09:48:51 +0100 | [diff] [blame] | 9459 | DCHECK(dex_cache->GetClassLoader() == class_loader.Get()); |
David Srbecky | 5de5efe | 2021-02-15 21:23:00 +0000 | [diff] [blame] | 9460 | ArtMethod* resolved = dex_cache->GetResolvedMethod(method_idx); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 9461 | Thread::PoisonObjectPointersIfDebug(); |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 9462 | if (resolved != nullptr) { |
| 9463 | DCHECK(!resolved->IsRuntimeMethod()); |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 9464 | DCHECK(resolved->GetDeclaringClassUnchecked() != nullptr) << resolved->GetDexMethodIndex(); |
| 9465 | return resolved; |
| 9466 | } |
| 9467 | // Fail, get the declaring class. |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9468 | const dex::MethodId& method_id = dex_cache->GetDexFile()->GetMethodId(method_idx); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 9469 | 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] | 9470 | if (klass == nullptr) { |
| 9471 | Thread::Current()->AssertPendingException(); |
| 9472 | return nullptr; |
| 9473 | } |
Nicolas Geoffray | ae12f96 | 2023-01-06 12:06:13 +0000 | [diff] [blame] | 9474 | return FindResolvedMethod(klass, dex_cache.Get(), class_loader.Get(), method_idx); |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 9475 | } |
| 9476 | |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 9477 | ArtField* ClassLinker::LookupResolvedField(uint32_t field_idx, |
| 9478 | ObjPtr<mirror::DexCache> dex_cache, |
| 9479 | ObjPtr<mirror::ClassLoader> class_loader, |
| 9480 | bool is_static) { |
Nicolas Geoffray | c07f488 | 2021-09-13 09:20:33 +0100 | [diff] [blame] | 9481 | DCHECK(dex_cache->GetClassLoader().Ptr() == class_loader.Ptr()); |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 9482 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9483 | const dex::FieldId& field_id = dex_file.GetFieldId(field_idx); |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 9484 | ObjPtr<mirror::Class> klass = dex_cache->GetResolvedType(field_id.class_idx_); |
| 9485 | if (klass == nullptr) { |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 9486 | klass = LookupResolvedType(field_id.class_idx_, dex_cache, class_loader); |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 9487 | } |
| 9488 | if (klass == nullptr) { |
| 9489 | // The class has not been resolved yet, so the field is also unresolved. |
| 9490 | return nullptr; |
| 9491 | } |
| 9492 | DCHECK(klass->IsResolved()); |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 9493 | |
David Brazdil | 1ab0fa8 | 2018-05-04 11:28:03 +0100 | [diff] [blame] | 9494 | return FindResolvedField(klass, dex_cache, class_loader, field_idx, is_static); |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 9495 | } |
| 9496 | |
Vladimir Marko | e11dd50 | 2017-12-08 14:09:45 +0000 | [diff] [blame] | 9497 | ArtField* ClassLinker::ResolveFieldJLS(uint32_t field_idx, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 9498 | Handle<mirror::DexCache> dex_cache, |
| 9499 | Handle<mirror::ClassLoader> class_loader) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 9500 | DCHECK(dex_cache != nullptr); |
Nicolas Geoffray | ab91eef | 2021-09-14 09:48:51 +0100 | [diff] [blame] | 9501 | DCHECK(dex_cache->GetClassLoader() == class_loader.Get()); |
David Srbecky | 5de5efe | 2021-02-15 21:23:00 +0000 | [diff] [blame] | 9502 | ArtField* resolved = dex_cache->GetResolvedField(field_idx); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 9503 | Thread::PoisonObjectPointersIfDebug(); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 9504 | if (resolved != nullptr) { |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 9505 | return resolved; |
| 9506 | } |
Vladimir Marko | e11dd50 | 2017-12-08 14:09:45 +0000 | [diff] [blame] | 9507 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9508 | const dex::FieldId& field_id = dex_file.GetFieldId(field_idx); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 9509 | 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] | 9510 | if (klass == nullptr) { |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 9511 | DCHECK(Thread::Current()->IsExceptionPending()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 9512 | return nullptr; |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 9513 | } |
| 9514 | |
David Brazdil | 1ab0fa8 | 2018-05-04 11:28:03 +0100 | [diff] [blame] | 9515 | resolved = FindResolvedFieldJLS(klass, dex_cache.Get(), class_loader.Get(), field_idx); |
| 9516 | if (resolved == nullptr) { |
| 9517 | const char* name = dex_file.GetFieldName(field_id); |
| 9518 | const char* type = dex_file.GetFieldTypeDescriptor(field_id); |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 9519 | ThrowNoSuchFieldError("", klass, type, name); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 9520 | } |
| 9521 | return resolved; |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 9522 | } |
| 9523 | |
David Brazdil | 1ab0fa8 | 2018-05-04 11:28:03 +0100 | [diff] [blame] | 9524 | ArtField* ClassLinker::FindResolvedField(ObjPtr<mirror::Class> klass, |
| 9525 | ObjPtr<mirror::DexCache> dex_cache, |
| 9526 | ObjPtr<mirror::ClassLoader> class_loader, |
| 9527 | uint32_t field_idx, |
| 9528 | bool is_static) { |
Nicolas Geoffray | ab91eef | 2021-09-14 09:48:51 +0100 | [diff] [blame] | 9529 | DCHECK(dex_cache->GetClassLoader() == class_loader); |
Vladimir Marko | b10668c | 2021-06-10 09:52:53 +0100 | [diff] [blame] | 9530 | ArtField* resolved = is_static ? klass->FindStaticField(dex_cache, field_idx) |
| 9531 | : klass->FindInstanceField(dex_cache, field_idx); |
David Brazdil | 1ab0fa8 | 2018-05-04 11:28:03 +0100 | [diff] [blame] | 9532 | if (resolved != nullptr && |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 9533 | hiddenapi::ShouldDenyAccessToMember(resolved, |
| 9534 | hiddenapi::AccessContext(class_loader, dex_cache), |
| 9535 | hiddenapi::AccessMethod::kLinking)) { |
David Brazdil | 1ab0fa8 | 2018-05-04 11:28:03 +0100 | [diff] [blame] | 9536 | resolved = nullptr; |
| 9537 | } |
| 9538 | |
| 9539 | if (resolved != nullptr) { |
David Srbecky | 5de5efe | 2021-02-15 21:23:00 +0000 | [diff] [blame] | 9540 | dex_cache->SetResolvedField(field_idx, resolved); |
David Brazdil | 1ab0fa8 | 2018-05-04 11:28:03 +0100 | [diff] [blame] | 9541 | } |
| 9542 | |
| 9543 | return resolved; |
| 9544 | } |
| 9545 | |
| 9546 | ArtField* ClassLinker::FindResolvedFieldJLS(ObjPtr<mirror::Class> klass, |
| 9547 | ObjPtr<mirror::DexCache> dex_cache, |
| 9548 | ObjPtr<mirror::ClassLoader> class_loader, |
| 9549 | uint32_t field_idx) { |
Nicolas Geoffray | c07f488 | 2021-09-13 09:20:33 +0100 | [diff] [blame] | 9550 | DCHECK(dex_cache->GetClassLoader().Ptr() == class_loader.Ptr()); |
Vladimir Marko | b10668c | 2021-06-10 09:52:53 +0100 | [diff] [blame] | 9551 | ArtField* resolved = klass->FindField(dex_cache, field_idx); |
David Brazdil | 1ab0fa8 | 2018-05-04 11:28:03 +0100 | [diff] [blame] | 9552 | |
| 9553 | if (resolved != nullptr && |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 9554 | hiddenapi::ShouldDenyAccessToMember(resolved, |
| 9555 | hiddenapi::AccessContext(class_loader, dex_cache), |
| 9556 | hiddenapi::AccessMethod::kLinking)) { |
David Brazdil | 1ab0fa8 | 2018-05-04 11:28:03 +0100 | [diff] [blame] | 9557 | resolved = nullptr; |
| 9558 | } |
| 9559 | |
| 9560 | if (resolved != nullptr) { |
David Srbecky | 5de5efe | 2021-02-15 21:23:00 +0000 | [diff] [blame] | 9561 | dex_cache->SetResolvedField(field_idx, resolved); |
David Brazdil | 1ab0fa8 | 2018-05-04 11:28:03 +0100 | [diff] [blame] | 9562 | } |
| 9563 | |
| 9564 | return resolved; |
| 9565 | } |
| 9566 | |
Vladimir Marko | af94020 | 2017-12-08 15:01:18 +0000 | [diff] [blame] | 9567 | ObjPtr<mirror::MethodType> ClassLinker::ResolveMethodType( |
| 9568 | Thread* self, |
Orion Hodson | 06d10a7 | 2018-05-14 08:53:38 +0100 | [diff] [blame] | 9569 | dex::ProtoIndex proto_idx, |
Vladimir Marko | af94020 | 2017-12-08 15:01:18 +0000 | [diff] [blame] | 9570 | Handle<mirror::DexCache> dex_cache, |
| 9571 | Handle<mirror::ClassLoader> class_loader) { |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9572 | DCHECK(Runtime::Current()->IsMethodHandlesEnabled()); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 9573 | DCHECK(dex_cache != nullptr); |
Nicolas Geoffray | ab91eef | 2021-09-14 09:48:51 +0100 | [diff] [blame] | 9574 | DCHECK(dex_cache->GetClassLoader() == class_loader.Get()); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9575 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 9576 | ObjPtr<mirror::MethodType> resolved = dex_cache->GetResolvedMethodType(proto_idx); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9577 | if (resolved != nullptr) { |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 9578 | return resolved; |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9579 | } |
| 9580 | |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9581 | StackHandleScope<4> hs(self); |
| 9582 | |
| 9583 | // First resolve the return type. |
Vladimir Marko | af94020 | 2017-12-08 15:01:18 +0000 | [diff] [blame] | 9584 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9585 | const dex::ProtoId& proto_id = dex_file.GetProtoId(proto_idx); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9586 | Handle<mirror::Class> return_type(hs.NewHandle( |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 9587 | ResolveType(proto_id.return_type_idx_, dex_cache, class_loader))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 9588 | if (return_type == nullptr) { |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9589 | DCHECK(self->IsExceptionPending()); |
| 9590 | return nullptr; |
| 9591 | } |
| 9592 | |
| 9593 | // Then resolve the argument types. |
| 9594 | // |
| 9595 | // TODO: Is there a better way to figure out the number of method arguments |
| 9596 | // other than by looking at the shorty ? |
| 9597 | const size_t num_method_args = strlen(dex_file.StringDataByIdx(proto_id.shorty_idx_)) - 1; |
| 9598 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 9599 | ObjPtr<mirror::Class> array_of_class = GetClassRoot<mirror::ObjectArray<mirror::Class>>(this); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9600 | Handle<mirror::ObjectArray<mirror::Class>> method_params(hs.NewHandle( |
| 9601 | mirror::ObjectArray<mirror::Class>::Alloc(self, array_of_class, num_method_args))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 9602 | if (method_params == nullptr) { |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9603 | DCHECK(self->IsExceptionPending()); |
| 9604 | return nullptr; |
| 9605 | } |
| 9606 | |
| 9607 | DexFileParameterIterator it(dex_file, proto_id); |
| 9608 | int32_t i = 0; |
| 9609 | MutableHandle<mirror::Class> param_class = hs.NewHandle<mirror::Class>(nullptr); |
| 9610 | for (; it.HasNext(); it.Next()) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 9611 | const dex::TypeIndex type_idx = it.GetTypeIdx(); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 9612 | param_class.Assign(ResolveType(type_idx, dex_cache, class_loader)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 9613 | if (param_class == nullptr) { |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9614 | DCHECK(self->IsExceptionPending()); |
| 9615 | return nullptr; |
| 9616 | } |
| 9617 | |
| 9618 | method_params->Set(i++, param_class.Get()); |
| 9619 | } |
| 9620 | |
| 9621 | DCHECK(!it.HasNext()); |
| 9622 | |
| 9623 | Handle<mirror::MethodType> type = hs.NewHandle( |
| 9624 | mirror::MethodType::Create(self, return_type, method_params)); |
Nicolas Geoffray | 4aebd58 | 2021-07-23 15:27:31 +0100 | [diff] [blame] | 9625 | if (type != nullptr) { |
Orion Hodson | 9fa3b7c | 2022-10-31 18:03:40 +0000 | [diff] [blame] | 9626 | // Ensure all stores for the newly created MethodType are visible, before we attempt to place |
| 9627 | // it in the DexCache (b/224733324). |
| 9628 | std::atomic_thread_fence(std::memory_order_release); |
Nicolas Geoffray | 4aebd58 | 2021-07-23 15:27:31 +0100 | [diff] [blame] | 9629 | dex_cache->SetResolvedMethodType(proto_idx, type.Get()); |
| 9630 | } |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9631 | |
| 9632 | return type.Get(); |
| 9633 | } |
| 9634 | |
Vladimir Marko | af94020 | 2017-12-08 15:01:18 +0000 | [diff] [blame] | 9635 | ObjPtr<mirror::MethodType> ClassLinker::ResolveMethodType(Thread* self, |
Orion Hodson | 06d10a7 | 2018-05-14 08:53:38 +0100 | [diff] [blame] | 9636 | dex::ProtoIndex proto_idx, |
Vladimir Marko | af94020 | 2017-12-08 15:01:18 +0000 | [diff] [blame] | 9637 | ArtMethod* referrer) { |
Orion Hodson | 2e59994 | 2017-09-22 16:17:41 +0100 | [diff] [blame] | 9638 | StackHandleScope<2> hs(self); |
Orion Hodson | 2e59994 | 2017-09-22 16:17:41 +0100 | [diff] [blame] | 9639 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache())); |
| 9640 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(referrer->GetClassLoader())); |
Vladimir Marko | af94020 | 2017-12-08 15:01:18 +0000 | [diff] [blame] | 9641 | return ResolveMethodType(self, proto_idx, dex_cache, class_loader); |
Orion Hodson | 2e59994 | 2017-09-22 16:17:41 +0100 | [diff] [blame] | 9642 | } |
| 9643 | |
Vladimir Marko | 5aead70 | 2019-03-27 11:00:36 +0000 | [diff] [blame] | 9644 | ObjPtr<mirror::MethodHandle> ClassLinker::ResolveMethodHandleForField( |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9645 | Thread* self, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9646 | const dex::MethodHandleItem& method_handle, |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9647 | ArtMethod* referrer) { |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9648 | DexFile::MethodHandleType handle_type = |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9649 | static_cast<DexFile::MethodHandleType>(method_handle.method_handle_type_); |
| 9650 | mirror::MethodHandle::Kind kind; |
Orion Hodson | fd7b2c2 | 2018-03-15 15:38:38 +0000 | [diff] [blame] | 9651 | bool is_put; |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9652 | bool is_static; |
| 9653 | int32_t num_params; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9654 | switch (handle_type) { |
| 9655 | case DexFile::MethodHandleType::kStaticPut: { |
Orion Hodson | 82b351f | 2017-07-05 14:34:25 +0100 | [diff] [blame] | 9656 | kind = mirror::MethodHandle::Kind::kStaticPut; |
Orion Hodson | fd7b2c2 | 2018-03-15 15:38:38 +0000 | [diff] [blame] | 9657 | is_put = true; |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9658 | is_static = true; |
| 9659 | num_params = 1; |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 9660 | break; |
| 9661 | } |
| 9662 | case DexFile::MethodHandleType::kStaticGet: { |
Orion Hodson | 82b351f | 2017-07-05 14:34:25 +0100 | [diff] [blame] | 9663 | kind = mirror::MethodHandle::Kind::kStaticGet; |
Orion Hodson | fd7b2c2 | 2018-03-15 15:38:38 +0000 | [diff] [blame] | 9664 | is_put = false; |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9665 | is_static = true; |
| 9666 | num_params = 0; |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 9667 | break; |
| 9668 | } |
| 9669 | case DexFile::MethodHandleType::kInstancePut: { |
Orion Hodson | 82b351f | 2017-07-05 14:34:25 +0100 | [diff] [blame] | 9670 | kind = mirror::MethodHandle::Kind::kInstancePut; |
Orion Hodson | fd7b2c2 | 2018-03-15 15:38:38 +0000 | [diff] [blame] | 9671 | is_put = true; |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9672 | is_static = false; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9673 | num_params = 2; |
| 9674 | break; |
| 9675 | } |
| 9676 | case DexFile::MethodHandleType::kInstanceGet: { |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9677 | kind = mirror::MethodHandle::Kind::kInstanceGet; |
Orion Hodson | fd7b2c2 | 2018-03-15 15:38:38 +0000 | [diff] [blame] | 9678 | is_put = false; |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9679 | is_static = false; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9680 | num_params = 1; |
| 9681 | break; |
| 9682 | } |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9683 | case DexFile::MethodHandleType::kInvokeStatic: |
Orion Hodson | 82b351f | 2017-07-05 14:34:25 +0100 | [diff] [blame] | 9684 | case DexFile::MethodHandleType::kInvokeInstance: |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9685 | case DexFile::MethodHandleType::kInvokeConstructor: |
Orion Hodson | 82b351f | 2017-07-05 14:34:25 +0100 | [diff] [blame] | 9686 | case DexFile::MethodHandleType::kInvokeDirect: |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9687 | case DexFile::MethodHandleType::kInvokeInterface: |
| 9688 | UNREACHABLE(); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9689 | } |
| 9690 | |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9691 | ArtField* target_field = |
| 9692 | ResolveField(method_handle.field_or_method_idx_, referrer, is_static); |
| 9693 | if (LIKELY(target_field != nullptr)) { |
| 9694 | ObjPtr<mirror::Class> target_class = target_field->GetDeclaringClass(); |
| 9695 | ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass(); |
| 9696 | if (UNLIKELY(!referring_class->CanAccessMember(target_class, target_field->GetAccessFlags()))) { |
| 9697 | ThrowIllegalAccessErrorField(referring_class, target_field); |
| 9698 | return nullptr; |
| 9699 | } |
Orion Hodson | fd7b2c2 | 2018-03-15 15:38:38 +0000 | [diff] [blame] | 9700 | if (UNLIKELY(is_put && target_field->IsFinal())) { |
| 9701 | ThrowIllegalAccessErrorField(referring_class, target_field); |
| 9702 | return nullptr; |
| 9703 | } |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9704 | } else { |
| 9705 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 9706 | return nullptr; |
| 9707 | } |
| 9708 | |
| 9709 | StackHandleScope<4> hs(self); |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 9710 | ObjPtr<mirror::Class> array_of_class = GetClassRoot<mirror::ObjectArray<mirror::Class>>(this); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9711 | Handle<mirror::ObjectArray<mirror::Class>> method_params(hs.NewHandle( |
| 9712 | mirror::ObjectArray<mirror::Class>::Alloc(self, array_of_class, num_params))); |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 9713 | if (UNLIKELY(method_params == nullptr)) { |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9714 | DCHECK(self->IsExceptionPending()); |
| 9715 | return nullptr; |
| 9716 | } |
| 9717 | |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9718 | Handle<mirror::Class> constructor_class; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9719 | Handle<mirror::Class> return_type; |
| 9720 | switch (handle_type) { |
| 9721 | case DexFile::MethodHandleType::kStaticPut: { |
Vladimir Marko | 4098a7a | 2017-11-06 16:00:51 +0000 | [diff] [blame] | 9722 | method_params->Set(0, target_field->ResolveType()); |
Vladimir Marko | 9186b18 | 2018-11-06 14:55:54 +0000 | [diff] [blame] | 9723 | return_type = hs.NewHandle(GetClassRoot(ClassRoot::kPrimitiveVoid, this)); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9724 | break; |
| 9725 | } |
| 9726 | case DexFile::MethodHandleType::kStaticGet: { |
Vladimir Marko | 4098a7a | 2017-11-06 16:00:51 +0000 | [diff] [blame] | 9727 | return_type = hs.NewHandle(target_field->ResolveType()); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9728 | break; |
| 9729 | } |
| 9730 | case DexFile::MethodHandleType::kInstancePut: { |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 9731 | method_params->Set(0, target_field->GetDeclaringClass()); |
Vladimir Marko | 4098a7a | 2017-11-06 16:00:51 +0000 | [diff] [blame] | 9732 | method_params->Set(1, target_field->ResolveType()); |
Vladimir Marko | 9186b18 | 2018-11-06 14:55:54 +0000 | [diff] [blame] | 9733 | return_type = hs.NewHandle(GetClassRoot(ClassRoot::kPrimitiveVoid, this)); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9734 | break; |
| 9735 | } |
| 9736 | case DexFile::MethodHandleType::kInstanceGet: { |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 9737 | method_params->Set(0, target_field->GetDeclaringClass()); |
Vladimir Marko | 4098a7a | 2017-11-06 16:00:51 +0000 | [diff] [blame] | 9738 | return_type = hs.NewHandle(target_field->ResolveType()); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9739 | break; |
| 9740 | } |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9741 | case DexFile::MethodHandleType::kInvokeStatic: |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 9742 | case DexFile::MethodHandleType::kInvokeInstance: |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9743 | case DexFile::MethodHandleType::kInvokeConstructor: |
| 9744 | case DexFile::MethodHandleType::kInvokeDirect: |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 9745 | case DexFile::MethodHandleType::kInvokeInterface: |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9746 | UNREACHABLE(); |
| 9747 | } |
| 9748 | |
| 9749 | for (int32_t i = 0; i < num_params; ++i) { |
| 9750 | if (UNLIKELY(method_params->Get(i) == nullptr)) { |
| 9751 | DCHECK(self->IsExceptionPending()); |
| 9752 | return nullptr; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9753 | } |
| 9754 | } |
| 9755 | |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9756 | if (UNLIKELY(return_type.IsNull())) { |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9757 | DCHECK(self->IsExceptionPending()); |
| 9758 | return nullptr; |
| 9759 | } |
| 9760 | |
| 9761 | Handle<mirror::MethodType> |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9762 | method_type(hs.NewHandle(mirror::MethodType::Create(self, return_type, method_params))); |
| 9763 | if (UNLIKELY(method_type.IsNull())) { |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9764 | DCHECK(self->IsExceptionPending()); |
| 9765 | return nullptr; |
| 9766 | } |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 9767 | |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9768 | uintptr_t target = reinterpret_cast<uintptr_t>(target_field); |
| 9769 | return mirror::MethodHandleImpl::Create(self, target, kind, method_type); |
| 9770 | } |
| 9771 | |
Vladimir Marko | 5aead70 | 2019-03-27 11:00:36 +0000 | [diff] [blame] | 9772 | ObjPtr<mirror::MethodHandle> ClassLinker::ResolveMethodHandleForMethod( |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9773 | Thread* self, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9774 | const dex::MethodHandleItem& method_handle, |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9775 | ArtMethod* referrer) { |
| 9776 | DexFile::MethodHandleType handle_type = |
| 9777 | static_cast<DexFile::MethodHandleType>(method_handle.method_handle_type_); |
| 9778 | mirror::MethodHandle::Kind kind; |
| 9779 | uint32_t receiver_count = 0; |
| 9780 | ArtMethod* target_method = nullptr; |
| 9781 | switch (handle_type) { |
| 9782 | case DexFile::MethodHandleType::kStaticPut: |
| 9783 | case DexFile::MethodHandleType::kStaticGet: |
| 9784 | case DexFile::MethodHandleType::kInstancePut: |
| 9785 | case DexFile::MethodHandleType::kInstanceGet: |
| 9786 | UNREACHABLE(); |
| 9787 | case DexFile::MethodHandleType::kInvokeStatic: { |
| 9788 | kind = mirror::MethodHandle::Kind::kInvokeStatic; |
| 9789 | receiver_count = 0; |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9790 | target_method = ResolveMethod<ResolveMode::kNoChecks>(self, |
| 9791 | method_handle.field_or_method_idx_, |
| 9792 | referrer, |
| 9793 | InvokeType::kStatic); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9794 | break; |
| 9795 | } |
| 9796 | case DexFile::MethodHandleType::kInvokeInstance: { |
| 9797 | kind = mirror::MethodHandle::Kind::kInvokeVirtual; |
| 9798 | receiver_count = 1; |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9799 | target_method = ResolveMethod<ResolveMode::kNoChecks>(self, |
| 9800 | method_handle.field_or_method_idx_, |
| 9801 | referrer, |
| 9802 | InvokeType::kVirtual); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9803 | break; |
| 9804 | } |
| 9805 | case DexFile::MethodHandleType::kInvokeConstructor: { |
| 9806 | // Constructors are currently implemented as a transform. They |
| 9807 | // are special cased later in this method. |
| 9808 | kind = mirror::MethodHandle::Kind::kInvokeTransform; |
| 9809 | receiver_count = 0; |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9810 | target_method = ResolveMethod<ResolveMode::kNoChecks>(self, |
| 9811 | method_handle.field_or_method_idx_, |
| 9812 | referrer, |
| 9813 | InvokeType::kDirect); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9814 | break; |
| 9815 | } |
| 9816 | case DexFile::MethodHandleType::kInvokeDirect: { |
| 9817 | kind = mirror::MethodHandle::Kind::kInvokeDirect; |
| 9818 | receiver_count = 1; |
| 9819 | StackHandleScope<2> hs(self); |
| 9820 | // A constant method handle with type kInvokeDirect can refer to |
| 9821 | // a method that is private or to a method in a super class. To |
| 9822 | // disambiguate the two options, we resolve the method ignoring |
| 9823 | // the invocation type to determine if the method is private. We |
| 9824 | // then resolve again specifying the intended invocation type to |
| 9825 | // force the appropriate checks. |
Vladimir Marko | 8901119 | 2017-12-11 13:45:05 +0000 | [diff] [blame] | 9826 | target_method = ResolveMethodWithoutInvokeType(method_handle.field_or_method_idx_, |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9827 | hs.NewHandle(referrer->GetDexCache()), |
| 9828 | hs.NewHandle(referrer->GetClassLoader())); |
| 9829 | if (UNLIKELY(target_method == nullptr)) { |
| 9830 | break; |
| 9831 | } |
| 9832 | |
| 9833 | if (target_method->IsPrivate()) { |
| 9834 | kind = mirror::MethodHandle::Kind::kInvokeDirect; |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9835 | target_method = ResolveMethod<ResolveMode::kNoChecks>(self, |
| 9836 | method_handle.field_or_method_idx_, |
| 9837 | referrer, |
| 9838 | InvokeType::kDirect); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9839 | } else { |
| 9840 | kind = mirror::MethodHandle::Kind::kInvokeSuper; |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9841 | target_method = ResolveMethod<ResolveMode::kNoChecks>(self, |
| 9842 | method_handle.field_or_method_idx_, |
| 9843 | referrer, |
| 9844 | InvokeType::kSuper); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9845 | if (UNLIKELY(target_method == nullptr)) { |
| 9846 | break; |
| 9847 | } |
| 9848 | // Find the method specified in the parent in referring class |
| 9849 | // so invoke-super invokes the method in the parent of the |
| 9850 | // referrer. |
| 9851 | target_method = |
| 9852 | referrer->GetDeclaringClass()->FindVirtualMethodForVirtual(target_method, |
| 9853 | kRuntimePointerSize); |
| 9854 | } |
| 9855 | break; |
| 9856 | } |
| 9857 | case DexFile::MethodHandleType::kInvokeInterface: { |
| 9858 | kind = mirror::MethodHandle::Kind::kInvokeInterface; |
| 9859 | receiver_count = 1; |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9860 | target_method = ResolveMethod<ResolveMode::kNoChecks>(self, |
| 9861 | method_handle.field_or_method_idx_, |
| 9862 | referrer, |
| 9863 | InvokeType::kInterface); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9864 | break; |
| 9865 | } |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 9866 | } |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9867 | |
| 9868 | if (UNLIKELY(target_method == nullptr)) { |
| 9869 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 9870 | return nullptr; |
| 9871 | } |
| 9872 | |
| 9873 | ObjPtr<mirror::Class> target_class = target_method->GetDeclaringClass(); |
| 9874 | ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass(); |
| 9875 | uint32_t access_flags = target_method->GetAccessFlags(); |
| 9876 | if (UNLIKELY(!referring_class->CanAccessMember(target_class, access_flags))) { |
| 9877 | ThrowIllegalAccessErrorMethod(referring_class, target_method); |
| 9878 | return nullptr; |
| 9879 | } |
| 9880 | |
| 9881 | // Calculate the number of parameters from the method shorty. We add the |
| 9882 | // receiver count (0 or 1) and deduct one for the return value. |
| 9883 | uint32_t shorty_length; |
| 9884 | target_method->GetShorty(&shorty_length); |
| 9885 | int32_t num_params = static_cast<int32_t>(shorty_length + receiver_count - 1); |
| 9886 | |
Orion Hodson | ecd5856 | 2018-09-24 11:27:33 +0100 | [diff] [blame] | 9887 | StackHandleScope<5> hs(self); |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 9888 | ObjPtr<mirror::Class> array_of_class = GetClassRoot<mirror::ObjectArray<mirror::Class>>(this); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9889 | Handle<mirror::ObjectArray<mirror::Class>> method_params(hs.NewHandle( |
| 9890 | mirror::ObjectArray<mirror::Class>::Alloc(self, array_of_class, num_params))); |
| 9891 | if (method_params.Get() == nullptr) { |
| 9892 | DCHECK(self->IsExceptionPending()); |
| 9893 | return nullptr; |
| 9894 | } |
| 9895 | |
Orion Hodson | ecd5856 | 2018-09-24 11:27:33 +0100 | [diff] [blame] | 9896 | const DexFile* dex_file = referrer->GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9897 | 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] | 9898 | int32_t index = 0; |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9899 | if (receiver_count != 0) { |
Orion Hodson | ecd5856 | 2018-09-24 11:27:33 +0100 | [diff] [blame] | 9900 | // Insert receiver. Use the class identified in the method handle rather than the declaring |
| 9901 | // class of the resolved method which may be super class or default interface method |
| 9902 | // (b/115964401). |
| 9903 | ObjPtr<mirror::Class> receiver_class = LookupResolvedType(method_id.class_idx_, referrer); |
| 9904 | // receiver_class should have been resolved when resolving the target method. |
| 9905 | DCHECK(receiver_class != nullptr); |
| 9906 | method_params->Set(index++, receiver_class); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9907 | } |
Orion Hodson | ecd5856 | 2018-09-24 11:27:33 +0100 | [diff] [blame] | 9908 | |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9909 | const dex::ProtoId& proto_id = dex_file->GetProtoId(method_id.proto_idx_); |
Orion Hodson | ecd5856 | 2018-09-24 11:27:33 +0100 | [diff] [blame] | 9910 | DexFileParameterIterator it(*dex_file, proto_id); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9911 | while (it.HasNext()) { |
Orion Hodson | da1cdd0 | 2018-01-31 18:08:28 +0000 | [diff] [blame] | 9912 | DCHECK_LT(index, num_params); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9913 | const dex::TypeIndex type_idx = it.GetTypeIdx(); |
Orion Hodson | ecd5856 | 2018-09-24 11:27:33 +0100 | [diff] [blame] | 9914 | ObjPtr<mirror::Class> klass = ResolveType(type_idx, referrer); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9915 | if (nullptr == klass) { |
| 9916 | DCHECK(self->IsExceptionPending()); |
| 9917 | return nullptr; |
| 9918 | } |
| 9919 | method_params->Set(index++, klass); |
| 9920 | it.Next(); |
| 9921 | } |
| 9922 | |
Orion Hodson | ecd5856 | 2018-09-24 11:27:33 +0100 | [diff] [blame] | 9923 | Handle<mirror::Class> return_type = |
| 9924 | hs.NewHandle(ResolveType(proto_id.return_type_idx_, referrer)); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9925 | if (UNLIKELY(return_type.IsNull())) { |
| 9926 | DCHECK(self->IsExceptionPending()); |
| 9927 | return nullptr; |
| 9928 | } |
| 9929 | |
| 9930 | Handle<mirror::MethodType> |
| 9931 | method_type(hs.NewHandle(mirror::MethodType::Create(self, return_type, method_params))); |
| 9932 | if (UNLIKELY(method_type.IsNull())) { |
| 9933 | DCHECK(self->IsExceptionPending()); |
| 9934 | return nullptr; |
| 9935 | } |
| 9936 | |
| 9937 | if (UNLIKELY(handle_type == DexFile::MethodHandleType::kInvokeConstructor)) { |
| 9938 | Handle<mirror::Class> constructor_class = hs.NewHandle(target_method->GetDeclaringClass()); |
| 9939 | Handle<mirror::MethodHandlesLookup> lookup = |
| 9940 | hs.NewHandle(mirror::MethodHandlesLookup::GetDefault(self)); |
| 9941 | return lookup->FindConstructor(self, constructor_class, method_type); |
| 9942 | } |
| 9943 | |
| 9944 | uintptr_t target = reinterpret_cast<uintptr_t>(target_method); |
| 9945 | return mirror::MethodHandleImpl::Create(self, target, kind, method_type); |
| 9946 | } |
| 9947 | |
Vladimir Marko | af94020 | 2017-12-08 15:01:18 +0000 | [diff] [blame] | 9948 | ObjPtr<mirror::MethodHandle> ClassLinker::ResolveMethodHandle(Thread* self, |
| 9949 | uint32_t method_handle_idx, |
| 9950 | ArtMethod* referrer) |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9951 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9952 | const DexFile* const dex_file = referrer->GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9953 | const dex::MethodHandleItem& method_handle = dex_file->GetMethodHandle(method_handle_idx); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9954 | switch (static_cast<DexFile::MethodHandleType>(method_handle.method_handle_type_)) { |
| 9955 | case DexFile::MethodHandleType::kStaticPut: |
| 9956 | case DexFile::MethodHandleType::kStaticGet: |
| 9957 | case DexFile::MethodHandleType::kInstancePut: |
| 9958 | case DexFile::MethodHandleType::kInstanceGet: |
| 9959 | return ResolveMethodHandleForField(self, method_handle, referrer); |
| 9960 | case DexFile::MethodHandleType::kInvokeStatic: |
| 9961 | case DexFile::MethodHandleType::kInvokeInstance: |
| 9962 | case DexFile::MethodHandleType::kInvokeConstructor: |
| 9963 | case DexFile::MethodHandleType::kInvokeDirect: |
| 9964 | case DexFile::MethodHandleType::kInvokeInterface: |
Orion Hodson | da1cdd0 | 2018-01-31 18:08:28 +0000 | [diff] [blame] | 9965 | return ResolveMethodHandleForMethod(self, method_handle, referrer); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9966 | } |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9967 | } |
| 9968 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 9969 | bool ClassLinker::IsQuickResolutionStub(const void* entry_point) const { |
| 9970 | return (entry_point == GetQuickResolutionStub()) || |
| 9971 | (quick_resolution_trampoline_ == entry_point); |
| 9972 | } |
| 9973 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 9974 | bool ClassLinker::IsQuickToInterpreterBridge(const void* entry_point) const { |
| 9975 | return (entry_point == GetQuickToInterpreterBridge()) || |
| 9976 | (quick_to_interpreter_bridge_trampoline_ == entry_point); |
| 9977 | } |
| 9978 | |
| 9979 | bool ClassLinker::IsQuickGenericJniStub(const void* entry_point) const { |
| 9980 | return (entry_point == GetQuickGenericJniStub()) || |
| 9981 | (quick_generic_jni_trampoline_ == entry_point); |
| 9982 | } |
| 9983 | |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 9984 | bool ClassLinker::IsJniDlsymLookupStub(const void* entry_point) const { |
Vladimir Marko | 7dac864 | 2019-11-06 17:09:30 +0000 | [diff] [blame] | 9985 | return entry_point == GetJniDlsymLookupStub() || |
| 9986 | (jni_dlsym_lookup_trampoline_ == entry_point); |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 9987 | } |
| 9988 | |
Vladimir Marko | fa458ac | 2020-02-12 14:08:07 +0000 | [diff] [blame] | 9989 | bool ClassLinker::IsJniDlsymLookupCriticalStub(const void* entry_point) const { |
| 9990 | return entry_point == GetJniDlsymLookupCriticalStub() || |
| 9991 | (jni_dlsym_lookup_critical_trampoline_ == entry_point); |
| 9992 | } |
| 9993 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 9994 | const void* ClassLinker::GetRuntimeQuickGenericJniStub() const { |
| 9995 | return GetQuickGenericJniStub(); |
| 9996 | } |
| 9997 | |
Alex Light | db01a09 | 2017-04-03 15:39:55 -0700 | [diff] [blame] | 9998 | void ClassLinker::SetEntryPointsForObsoleteMethod(ArtMethod* method) const { |
| 9999 | DCHECK(method->IsObsolete()); |
| 10000 | // We cannot mess with the entrypoints of native methods because they are used to determine how |
| 10001 | // large the method's quick stack frame is. Without this information we cannot walk the stacks. |
| 10002 | if (!method->IsNative()) { |
| 10003 | method->SetEntryPointFromQuickCompiledCode(GetInvokeObsoleteMethodStub()); |
| 10004 | } |
| 10005 | } |
| 10006 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 10007 | void ClassLinker::DumpForSigQuit(std::ostream& os) { |
Hans Boehm | fb3ad72 | 2021-08-16 16:53:17 +0000 | [diff] [blame] | 10008 | ScopedObjectAccess soa(Thread::Current()); |
| 10009 | ReaderMutexLock mu(soa.Self(), *Locks::classlinker_classes_lock_); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 10010 | os << "Zygote loaded classes=" << NumZygoteClasses() << " post zygote classes=" |
| 10011 | << NumNonZygoteClasses() << "\n"; |
Hans Boehm | fb3ad72 | 2021-08-16 16:53:17 +0000 | [diff] [blame] | 10012 | ReaderMutexLock mu2(soa.Self(), *Locks::dex_lock_); |
Nicolas Geoffray | a90c922 | 2018-09-07 13:19:19 +0100 | [diff] [blame] | 10013 | os << "Dumping registered class loaders\n"; |
| 10014 | size_t class_loader_index = 0; |
| 10015 | for (const ClassLoaderData& class_loader : class_loaders_) { |
| 10016 | ObjPtr<mirror::ClassLoader> loader = |
Hans Boehm | fb3ad72 | 2021-08-16 16:53:17 +0000 | [diff] [blame] | 10017 | ObjPtr<mirror::ClassLoader>::DownCast(soa.Self()->DecodeJObject(class_loader.weak_root)); |
Nicolas Geoffray | a90c922 | 2018-09-07 13:19:19 +0100 | [diff] [blame] | 10018 | if (loader != nullptr) { |
| 10019 | os << "#" << class_loader_index++ << " " << loader->GetClass()->PrettyDescriptor() << ": ["; |
| 10020 | bool saw_one_dex_file = false; |
David Srbecky | 6fbcc29 | 2021-02-23 01:05:32 +0000 | [diff] [blame] | 10021 | for (const auto& entry : dex_caches_) { |
| 10022 | const DexCacheData& dex_cache = entry.second; |
| 10023 | if (dex_cache.class_table == class_loader.class_table) { |
Nicolas Geoffray | a90c922 | 2018-09-07 13:19:19 +0100 | [diff] [blame] | 10024 | if (saw_one_dex_file) { |
| 10025 | os << ":"; |
| 10026 | } |
| 10027 | saw_one_dex_file = true; |
David Srbecky | 6fbcc29 | 2021-02-23 01:05:32 +0000 | [diff] [blame] | 10028 | os << entry.first->GetLocation(); |
Nicolas Geoffray | a90c922 | 2018-09-07 13:19:19 +0100 | [diff] [blame] | 10029 | } |
| 10030 | } |
| 10031 | os << "]"; |
| 10032 | bool found_parent = false; |
| 10033 | if (loader->GetParent() != nullptr) { |
| 10034 | size_t parent_index = 0; |
| 10035 | for (const ClassLoaderData& class_loader2 : class_loaders_) { |
| 10036 | ObjPtr<mirror::ClassLoader> loader2 = ObjPtr<mirror::ClassLoader>::DownCast( |
Hans Boehm | fb3ad72 | 2021-08-16 16:53:17 +0000 | [diff] [blame] | 10037 | soa.Self()->DecodeJObject(class_loader2.weak_root)); |
Nicolas Geoffray | a90c922 | 2018-09-07 13:19:19 +0100 | [diff] [blame] | 10038 | if (loader2 == loader->GetParent()) { |
| 10039 | os << ", parent #" << parent_index; |
| 10040 | found_parent = true; |
| 10041 | break; |
| 10042 | } |
| 10043 | parent_index++; |
| 10044 | } |
| 10045 | if (!found_parent) { |
| 10046 | os << ", unregistered parent of type " |
| 10047 | << loader->GetParent()->GetClass()->PrettyDescriptor(); |
| 10048 | } |
| 10049 | } else { |
| 10050 | os << ", no parent"; |
| 10051 | } |
| 10052 | os << "\n"; |
| 10053 | } |
| 10054 | } |
| 10055 | os << "Done dumping class loaders\n"; |
Andreas Gampe | 9b7f8b5 | 2019-06-07 08:59:29 -0700 | [diff] [blame] | 10056 | Runtime* runtime = Runtime::Current(); |
| 10057 | os << "Classes initialized: " << runtime->GetStat(KIND_GLOBAL_CLASS_INIT_COUNT) << " in " |
| 10058 | << PrettyDuration(runtime->GetStat(KIND_GLOBAL_CLASS_INIT_TIME)) << "\n"; |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 10059 | } |
| 10060 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 10061 | class CountClassesVisitor : public ClassLoaderVisitor { |
| 10062 | public: |
| 10063 | CountClassesVisitor() : num_zygote_classes(0), num_non_zygote_classes(0) {} |
| 10064 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 10065 | void Visit(ObjPtr<mirror::ClassLoader> class_loader) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 10066 | REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_) override { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 10067 | ClassTable* const class_table = class_loader->GetClassTable(); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 10068 | if (class_table != nullptr) { |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 10069 | num_zygote_classes += class_table->NumZygoteClasses(class_loader); |
| 10070 | num_non_zygote_classes += class_table->NumNonZygoteClasses(class_loader); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 10071 | } |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 10072 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 10073 | |
| 10074 | size_t num_zygote_classes; |
| 10075 | size_t num_non_zygote_classes; |
| 10076 | }; |
| 10077 | |
| 10078 | size_t ClassLinker::NumZygoteClasses() const { |
| 10079 | CountClassesVisitor visitor; |
| 10080 | VisitClassLoaders(&visitor); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 10081 | return visitor.num_zygote_classes + boot_class_table_->NumZygoteClasses(nullptr); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 10082 | } |
| 10083 | |
| 10084 | size_t ClassLinker::NumNonZygoteClasses() const { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 10085 | CountClassesVisitor visitor; |
| 10086 | VisitClassLoaders(&visitor); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 10087 | return visitor.num_non_zygote_classes + boot_class_table_->NumNonZygoteClasses(nullptr); |
Elliott Hughes | cac6cc7 | 2011-11-03 20:31:21 -0700 | [diff] [blame] | 10088 | } |
| 10089 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 10090 | size_t ClassLinker::NumLoadedClasses() { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 10091 | ReaderMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 10092 | // 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] | 10093 | return NumNonZygoteClasses(); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 10094 | } |
| 10095 | |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 10096 | pid_t ClassLinker::GetClassesLockOwner() { |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 10097 | return Locks::classlinker_classes_lock_->GetExclusiveOwnerTid(); |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 10098 | } |
| 10099 | |
| 10100 | pid_t ClassLinker::GetDexLockOwner() { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 10101 | return Locks::dex_lock_->GetExclusiveOwnerTid(); |
Brian Carlstrom | 24a3c2e | 2011-10-17 18:07:52 -0700 | [diff] [blame] | 10102 | } |
| 10103 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 10104 | void ClassLinker::SetClassRoot(ClassRoot class_root, ObjPtr<mirror::Class> klass) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 10105 | DCHECK(!init_done_); |
| 10106 | |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 10107 | DCHECK(klass != nullptr); |
| 10108 | DCHECK(klass->GetClassLoader() == nullptr); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 10109 | |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 10110 | mirror::ObjectArray<mirror::Class>* class_roots = class_roots_.Read(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 10111 | DCHECK(class_roots != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 10112 | DCHECK_LT(static_cast<uint32_t>(class_root), static_cast<uint32_t>(ClassRoot::kMax)); |
| 10113 | int32_t index = static_cast<int32_t>(class_root); |
| 10114 | DCHECK(class_roots->Get(index) == nullptr); |
| 10115 | class_roots->Set<false>(index, klass); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 10116 | } |
| 10117 | |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 10118 | ObjPtr<mirror::ClassLoader> ClassLinker::CreateWellKnownClassLoader( |
| 10119 | Thread* self, |
| 10120 | const std::vector<const DexFile*>& dex_files, |
| 10121 | Handle<mirror::Class> loader_class, |
Nicolas Geoffray | e167273 | 2018-11-30 01:09:49 +0000 | [diff] [blame] | 10122 | Handle<mirror::ClassLoader> parent_loader, |
Brad Stenning | 9c924e8 | 2021-10-11 19:09:00 -0700 | [diff] [blame] | 10123 | Handle<mirror::ObjectArray<mirror::ClassLoader>> shared_libraries, |
| 10124 | Handle<mirror::ObjectArray<mirror::ClassLoader>> shared_libraries_after) { |
Vladimir Marko | b6f965d | 2022-11-23 14:24:45 +0000 | [diff] [blame] | 10125 | CHECK(loader_class.Get() == WellKnownClasses::dalvik_system_PathClassLoader || |
| 10126 | loader_class.Get() == WellKnownClasses::dalvik_system_DelegateLastClassLoader || |
| 10127 | loader_class.Get() == WellKnownClasses::dalvik_system_InMemoryDexClassLoader); |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 10128 | |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 10129 | StackHandleScope<5> hs(self); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 10130 | |
VladimÃr Marko | 5be5ce7 | 2022-10-18 09:49:00 +0000 | [diff] [blame] | 10131 | ArtField* dex_elements_field = WellKnownClasses::dalvik_system_DexPathList_dexElements; |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 10132 | |
Vladimir Marko | 4098a7a | 2017-11-06 16:00:51 +0000 | [diff] [blame] | 10133 | Handle<mirror::Class> dex_elements_class(hs.NewHandle(dex_elements_field->ResolveType())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 10134 | DCHECK(dex_elements_class != nullptr); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 10135 | DCHECK(dex_elements_class->IsArrayClass()); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 10136 | Handle<mirror::ObjectArray<mirror::Object>> h_dex_elements(hs.NewHandle( |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 10137 | mirror::ObjectArray<mirror::Object>::Alloc(self, |
| 10138 | dex_elements_class.Get(), |
| 10139 | dex_files.size()))); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 10140 | Handle<mirror::Class> h_dex_element_class = |
| 10141 | hs.NewHandle(dex_elements_class->GetComponentType()); |
| 10142 | |
VladimÃr Marko | 5be5ce7 | 2022-10-18 09:49:00 +0000 | [diff] [blame] | 10143 | ArtField* element_file_field = WellKnownClasses::dalvik_system_DexPathList__Element_dexFile; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 10144 | DCHECK_EQ(h_dex_element_class.Get(), element_file_field->GetDeclaringClass()); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 10145 | |
VladimÃr Marko | 5be5ce7 | 2022-10-18 09:49:00 +0000 | [diff] [blame] | 10146 | ArtField* cookie_field = WellKnownClasses::dalvik_system_DexFile_cookie; |
Vladimir Marko | 208f670 | 2017-12-08 12:00:50 +0000 | [diff] [blame] | 10147 | DCHECK_EQ(cookie_field->GetDeclaringClass(), element_file_field->LookupResolvedType()); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 10148 | |
VladimÃr Marko | 5be5ce7 | 2022-10-18 09:49:00 +0000 | [diff] [blame] | 10149 | ArtField* file_name_field = WellKnownClasses::dalvik_system_DexFile_fileName; |
Vladimir Marko | 208f670 | 2017-12-08 12:00:50 +0000 | [diff] [blame] | 10150 | DCHECK_EQ(file_name_field->GetDeclaringClass(), element_file_field->LookupResolvedType()); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 10151 | |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 10152 | // Fill the elements array. |
| 10153 | int32_t index = 0; |
| 10154 | for (const DexFile* dex_file : dex_files) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 10155 | StackHandleScope<4> hs2(self); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 10156 | |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 10157 | // CreateWellKnownClassLoader is only used by gtests and compiler. |
| 10158 | // 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] | 10159 | Handle<mirror::LongArray> h_long_array = hs2.NewHandle(mirror::LongArray::Alloc( |
| 10160 | self, |
| 10161 | kDexFileIndexStart + 1)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 10162 | DCHECK(h_long_array != nullptr); |
Vladimir Marko | 78baed5 | 2018-10-11 10:44:58 +0100 | [diff] [blame] | 10163 | h_long_array->Set(kDexFileIndexStart, reinterpret_cast64<int64_t>(dex_file)); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 10164 | |
Mathieu Chartier | 3738e98 | 2017-05-12 16:07:28 -0700 | [diff] [blame] | 10165 | // Note that this creates a finalizable dalvik.system.DexFile object and a corresponding |
| 10166 | // FinalizerReference which will never get cleaned up without a started runtime. |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 10167 | Handle<mirror::Object> h_dex_file = hs2.NewHandle( |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 10168 | cookie_field->GetDeclaringClass()->AllocObject(self)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 10169 | DCHECK(h_dex_file != nullptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 10170 | cookie_field->SetObject<false>(h_dex_file.Get(), h_long_array.Get()); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 10171 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 10172 | Handle<mirror::String> h_file_name = hs2.NewHandle( |
| 10173 | mirror::String::AllocFromModifiedUtf8(self, dex_file->GetLocation().c_str())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 10174 | DCHECK(h_file_name != nullptr); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 10175 | file_name_field->SetObject<false>(h_dex_file.Get(), h_file_name.Get()); |
| 10176 | |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 10177 | 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] | 10178 | DCHECK(h_element != nullptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 10179 | element_file_field->SetObject<false>(h_element.Get(), h_dex_file.Get()); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 10180 | |
| 10181 | h_dex_elements->Set(index, h_element.Get()); |
| 10182 | index++; |
| 10183 | } |
| 10184 | DCHECK_EQ(index, h_dex_elements->GetLength()); |
| 10185 | |
| 10186 | // Create DexPathList. |
| 10187 | Handle<mirror::Object> h_dex_path_list = hs.NewHandle( |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 10188 | dex_elements_field->GetDeclaringClass()->AllocObject(self)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 10189 | DCHECK(h_dex_path_list != nullptr); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 10190 | // Set elements. |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 10191 | 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] | 10192 | // Create an empty List for the "nativeLibraryDirectories," required for native tests. |
| 10193 | // Note: this code is uncommon(oatdump)/testing-only, so don't add further WellKnownClasses |
| 10194 | // elements. |
| 10195 | { |
| 10196 | ArtField* native_lib_dirs = dex_elements_field->GetDeclaringClass()-> |
| 10197 | FindDeclaredInstanceField("nativeLibraryDirectories", "Ljava/util/List;"); |
| 10198 | DCHECK(native_lib_dirs != nullptr); |
| 10199 | ObjPtr<mirror::Class> list_class = FindSystemClass(self, "Ljava/util/ArrayList;"); |
| 10200 | DCHECK(list_class != nullptr); |
| 10201 | { |
| 10202 | StackHandleScope<1> h_list_scope(self); |
| 10203 | Handle<mirror::Class> h_list_class(h_list_scope.NewHandle<mirror::Class>(list_class)); |
| 10204 | bool list_init = EnsureInitialized(self, h_list_class, true, true); |
| 10205 | DCHECK(list_init); |
| 10206 | list_class = h_list_class.Get(); |
| 10207 | } |
| 10208 | ObjPtr<mirror::Object> list_object = list_class->AllocObject(self); |
| 10209 | // Note: we leave the object uninitialized. This must never leak into any non-testing code, but |
| 10210 | // is fine for testing. While it violates a Java-code invariant (the elementData field is |
| 10211 | // normally never null), as long as one does not try to add elements, this will still |
| 10212 | // work. |
| 10213 | native_lib_dirs->SetObject<false>(h_dex_path_list.Get(), list_object); |
| 10214 | } |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 10215 | |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 10216 | // Create the class loader.. |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 10217 | Handle<mirror::ClassLoader> h_class_loader = hs.NewHandle<mirror::ClassLoader>( |
| 10218 | ObjPtr<mirror::ClassLoader>::DownCast(loader_class->AllocObject(self))); |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 10219 | DCHECK(h_class_loader != nullptr); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 10220 | // Set DexPathList. |
VladimÃr Marko | 5be5ce7 | 2022-10-18 09:49:00 +0000 | [diff] [blame] | 10221 | ArtField* path_list_field = WellKnownClasses::dalvik_system_BaseDexClassLoader_pathList; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 10222 | DCHECK(path_list_field != nullptr); |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 10223 | 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] | 10224 | |
| 10225 | // Make a pretend boot-classpath. |
| 10226 | // TODO: Should we scan the image? |
VladimÃr Marko | 5be5ce7 | 2022-10-18 09:49:00 +0000 | [diff] [blame] | 10227 | ArtField* const parent_field = WellKnownClasses::java_lang_ClassLoader_parent; |
Roland Levillain | f39c9eb | 2015-05-26 15:02:07 +0100 | [diff] [blame] | 10228 | DCHECK(parent_field != nullptr); |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 10229 | if (parent_loader.Get() == nullptr) { |
Vladimir Marko | b6f965d | 2022-11-23 14:24:45 +0000 | [diff] [blame] | 10230 | ObjPtr<mirror::Object> boot_loader( |
| 10231 | WellKnownClasses::java_lang_BootClassLoader->AllocObject(self)); |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 10232 | parent_field->SetObject<false>(h_class_loader.Get(), boot_loader); |
| 10233 | } else { |
| 10234 | parent_field->SetObject<false>(h_class_loader.Get(), parent_loader.Get()); |
| 10235 | } |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 10236 | |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 10237 | ArtField* shared_libraries_field = |
VladimÃr Marko | 5be5ce7 | 2022-10-18 09:49:00 +0000 | [diff] [blame] | 10238 | WellKnownClasses::dalvik_system_BaseDexClassLoader_sharedLibraryLoaders; |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 10239 | DCHECK(shared_libraries_field != nullptr); |
| 10240 | shared_libraries_field->SetObject<false>(h_class_loader.Get(), shared_libraries.Get()); |
| 10241 | |
Brad Stenning | 9c924e8 | 2021-10-11 19:09:00 -0700 | [diff] [blame] | 10242 | ArtField* shared_libraries_after_field = |
VladimÃr Marko | 5be5ce7 | 2022-10-18 09:49:00 +0000 | [diff] [blame] | 10243 | WellKnownClasses::dalvik_system_BaseDexClassLoader_sharedLibraryLoadersAfter; |
Brad Stenning | 9c924e8 | 2021-10-11 19:09:00 -0700 | [diff] [blame] | 10244 | DCHECK(shared_libraries_after_field != nullptr); |
| 10245 | shared_libraries_after_field->SetObject<false>(h_class_loader.Get(), |
| 10246 | shared_libraries_after.Get()); |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 10247 | return h_class_loader.Get(); |
| 10248 | } |
| 10249 | |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 10250 | jobject ClassLinker::CreatePathClassLoader(Thread* self, |
| 10251 | const std::vector<const DexFile*>& dex_files) { |
Vladimir Marko | b6f965d | 2022-11-23 14:24:45 +0000 | [diff] [blame] | 10252 | StackHandleScope<3u> hs(self); |
| 10253 | Handle<mirror::Class> d_s_pcl = |
| 10254 | hs.NewHandle(WellKnownClasses::dalvik_system_PathClassLoader.Get()); |
| 10255 | auto null_parent = hs.NewHandle<mirror::ClassLoader>(nullptr); |
| 10256 | auto null_libs = hs.NewHandle<mirror::ObjectArray<mirror::ClassLoader>>(nullptr); |
| 10257 | ObjPtr<mirror::ClassLoader> class_loader = |
| 10258 | CreateWellKnownClassLoader(self, dex_files, d_s_pcl, null_parent, null_libs, null_libs); |
| 10259 | return Runtime::Current()->GetJavaVM()->AddGlobalRef(self, class_loader); |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 10260 | } |
| 10261 | |
Andreas Gampe | 8ac7595 | 2015-06-02 21:01:45 -0700 | [diff] [blame] | 10262 | void ClassLinker::DropFindArrayClassCache() { |
| 10263 | std::fill_n(find_array_class_cache_, kFindArrayCacheSize, GcRoot<mirror::Class>(nullptr)); |
| 10264 | find_array_class_cache_next_victim_ = 0; |
| 10265 | } |
| 10266 | |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 10267 | void ClassLinker::VisitClassLoaders(ClassLoaderVisitor* visitor) const { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 10268 | Thread* const self = Thread::Current(); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 10269 | for (const ClassLoaderData& data : class_loaders_) { |
Mathieu Chartier | 4843bd5 | 2015-10-01 17:08:44 -0700 | [diff] [blame] | 10270 | // 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] | 10271 | ObjPtr<mirror::ClassLoader> class_loader = ObjPtr<mirror::ClassLoader>::DownCast( |
| 10272 | self->DecodeJObject(data.weak_root)); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 10273 | if (class_loader != nullptr) { |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 10274 | visitor->Visit(class_loader); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 10275 | } |
| 10276 | } |
| 10277 | } |
| 10278 | |
Lokesh Gidra | b7607c2 | 2022-01-28 12:30:31 -0800 | [diff] [blame] | 10279 | void ClassLinker::VisitDexCaches(DexCacheVisitor* visitor) const { |
| 10280 | Thread* const self = Thread::Current(); |
| 10281 | for (const auto& it : dex_caches_) { |
| 10282 | // Need to use DecodeJObject so that we get null for cleared JNI weak globals. |
| 10283 | ObjPtr<mirror::DexCache> dex_cache = ObjPtr<mirror::DexCache>::DownCast( |
| 10284 | self->DecodeJObject(it.second.weak_root)); |
| 10285 | if (dex_cache != nullptr) { |
| 10286 | visitor->Visit(dex_cache); |
| 10287 | } |
| 10288 | } |
| 10289 | } |
| 10290 | |
Alexey Grebenkin | 252a4e4 | 2018-04-02 18:18:01 +0300 | [diff] [blame] | 10291 | void ClassLinker::VisitAllocators(AllocatorVisitor* visitor) const { |
| 10292 | for (const ClassLoaderData& data : class_loaders_) { |
| 10293 | LinearAlloc* alloc = data.allocator; |
| 10294 | if (alloc != nullptr && !visitor->Visit(alloc)) { |
| 10295 | break; |
| 10296 | } |
| 10297 | } |
| 10298 | } |
| 10299 | |
Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 10300 | void ClassLinker::InsertDexFileInToClassLoader(ObjPtr<mirror::Object> dex_file, |
| 10301 | ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 10302 | DCHECK(dex_file != nullptr); |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 10303 | Thread* const self = Thread::Current(); |
| 10304 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 10305 | ClassTable* const table = ClassTableForClassLoader(class_loader); |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 10306 | DCHECK(table != nullptr); |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 10307 | if (table->InsertStrongRoot(dex_file) && class_loader != nullptr) { |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 10308 | // It was not already inserted, perform the write barrier to let the GC know the class loader's |
| 10309 | // class table was modified. |
Mathieu Chartier | 88ea61e | 2018-06-20 17:45:41 -0700 | [diff] [blame] | 10310 | WriteBarrier::ForEveryFieldWrite(class_loader); |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 10311 | } |
| 10312 | } |
| 10313 | |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 10314 | void ClassLinker::CleanupClassLoaders() { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 10315 | Thread* const self = Thread::Current(); |
Vladimir Marko | 8e94a6f | 2022-12-13 16:46:22 +0000 | [diff] [blame] | 10316 | std::list<ClassLoaderData> to_delete; |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 10317 | // Do the delete outside the lock to avoid lock violation in jit code cache. |
| 10318 | { |
| 10319 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 10320 | for (auto it = class_loaders_.begin(); it != class_loaders_.end(); ) { |
Vladimir Marko | 8e94a6f | 2022-12-13 16:46:22 +0000 | [diff] [blame] | 10321 | auto this_it = it; |
| 10322 | ++it; |
| 10323 | const ClassLoaderData& data = *this_it; |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 10324 | // 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] | 10325 | ObjPtr<mirror::ClassLoader> class_loader = |
| 10326 | ObjPtr<mirror::ClassLoader>::DownCast(self->DecodeJObject(data.weak_root)); |
Vladimir Marko | 8e94a6f | 2022-12-13 16:46:22 +0000 | [diff] [blame] | 10327 | if (class_loader == nullptr) { |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 10328 | VLOG(class_linker) << "Freeing class loader"; |
Vladimir Marko | 8e94a6f | 2022-12-13 16:46:22 +0000 | [diff] [blame] | 10329 | to_delete.splice(to_delete.end(), class_loaders_, this_it); |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 10330 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 10331 | } |
| 10332 | } |
VladimÃr Marko | 1deea77 | 2022-11-30 16:17:07 +0000 | [diff] [blame] | 10333 | std::set<const OatFile*> unregistered_oat_files; |
Vladimir Marko | 68457df | 2022-10-26 07:18:57 +0000 | [diff] [blame] | 10334 | if (!to_delete.empty()) { |
| 10335 | JavaVMExt* vm = self->GetJniEnv()->GetVm(); |
| 10336 | WriterMutexLock mu(self, *Locks::dex_lock_); |
| 10337 | for (auto it = dex_caches_.begin(), end = dex_caches_.end(); it != end; ) { |
VladimÃr Marko | 1deea77 | 2022-11-30 16:17:07 +0000 | [diff] [blame] | 10338 | const DexFile* dex_file = it->first; |
Vladimir Marko | 68457df | 2022-10-26 07:18:57 +0000 | [diff] [blame] | 10339 | const DexCacheData& data = it->second; |
| 10340 | if (self->DecodeJObject(data.weak_root) == nullptr) { |
| 10341 | DCHECK(to_delete.end() != std::find_if( |
| 10342 | to_delete.begin(), |
| 10343 | to_delete.end(), |
| 10344 | [&](const ClassLoaderData& cld) { return cld.class_table == data.class_table; })); |
VladimÃr Marko | 1deea77 | 2022-11-30 16:17:07 +0000 | [diff] [blame] | 10345 | if (dex_file->GetOatDexFile() != nullptr && |
| 10346 | dex_file->GetOatDexFile()->GetOatFile() != nullptr && |
| 10347 | dex_file->GetOatDexFile()->GetOatFile()->IsExecutable()) { |
| 10348 | unregistered_oat_files.insert(dex_file->GetOatDexFile()->GetOatFile()); |
| 10349 | } |
Vladimir Marko | 68457df | 2022-10-26 07:18:57 +0000 | [diff] [blame] | 10350 | vm->DeleteWeakGlobalRef(self, data.weak_root); |
| 10351 | it = dex_caches_.erase(it); |
| 10352 | } else { |
| 10353 | ++it; |
| 10354 | } |
| 10355 | } |
| 10356 | } |
VladimÃr Marko | 1deea77 | 2022-11-30 16:17:07 +0000 | [diff] [blame] | 10357 | { |
| 10358 | ScopedDebugDisallowReadBarriers sddrb(self); |
| 10359 | for (ClassLoaderData& data : to_delete) { |
| 10360 | // CHA unloading analysis and SingleImplementaion cleanups are required. |
| 10361 | DeleteClassLoader(self, data, /*cleanup_cha=*/ true); |
| 10362 | } |
| 10363 | } |
| 10364 | if (!unregistered_oat_files.empty()) { |
| 10365 | for (const OatFile* oat_file : unregistered_oat_files) { |
| 10366 | // Notify the fault handler about removal of the executable code range if needed. |
| 10367 | DCHECK(oat_file->IsExecutable()); |
| 10368 | size_t exec_offset = oat_file->GetOatHeader().GetExecutableOffset(); |
| 10369 | DCHECK_LE(exec_offset, oat_file->Size()); |
| 10370 | size_t exec_size = oat_file->Size() - exec_offset; |
| 10371 | if (exec_size != 0u) { |
| 10372 | Runtime::Current()->RemoveGeneratedCodeRange(oat_file->Begin() + exec_offset, exec_size); |
| 10373 | } |
| 10374 | } |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 10375 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 10376 | } |
| 10377 | |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 10378 | class ClassLinker::FindVirtualMethodHolderVisitor : public ClassVisitor { |
| 10379 | public: |
| 10380 | FindVirtualMethodHolderVisitor(const ArtMethod* method, PointerSize pointer_size) |
| 10381 | : method_(method), |
| 10382 | pointer_size_(pointer_size) {} |
| 10383 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 10384 | bool operator()(ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_) override { |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 10385 | if (klass->GetVirtualMethodsSliceUnchecked(pointer_size_).Contains(method_)) { |
| 10386 | holder_ = klass; |
| 10387 | } |
| 10388 | // Return false to stop searching if holder_ is not null. |
| 10389 | return holder_ == nullptr; |
| 10390 | } |
| 10391 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 10392 | ObjPtr<mirror::Class> holder_ = nullptr; |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 10393 | const ArtMethod* const method_; |
| 10394 | const PointerSize pointer_size_; |
| 10395 | }; |
| 10396 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 10397 | ObjPtr<mirror::Class> ClassLinker::GetHoldingClassOfCopiedMethod(ArtMethod* method) { |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 10398 | ScopedTrace trace(__FUNCTION__); // Since this function is slow, have a trace to notify people. |
| 10399 | CHECK(method->IsCopied()); |
| 10400 | FindVirtualMethodHolderVisitor visitor(method, image_pointer_size_); |
| 10401 | VisitClasses(&visitor); |
Vladimir Marko | d80bbba | 2022-12-20 16:25:58 +0000 | [diff] [blame] | 10402 | DCHECK(visitor.holder_ != nullptr); |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 10403 | return visitor.holder_; |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 10404 | } |
| 10405 | |
Vladimir Marko | d80bbba | 2022-12-20 16:25:58 +0000 | [diff] [blame] | 10406 | ObjPtr<mirror::ClassLoader> ClassLinker::GetHoldingClassLoaderOfCopiedMethod(Thread* self, |
| 10407 | ArtMethod* method) { |
| 10408 | // Note: `GetHoldingClassOfCopiedMethod(method)` is a lot more expensive than finding |
| 10409 | // the class loader, so we're using it only to verify the result in debug mode. |
| 10410 | CHECK(method->IsCopied()); |
| 10411 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
| 10412 | // Check if the copied method is in the boot class path. |
| 10413 | if (heap->IsBootImageAddress(method) || GetAllocatorForClassLoader(nullptr)->Contains(method)) { |
| 10414 | DCHECK(GetHoldingClassOfCopiedMethod(method)->GetClassLoader() == nullptr); |
| 10415 | return nullptr; |
| 10416 | } |
| 10417 | // Check if the copied method is in an app image. |
| 10418 | // Note: Continuous spaces contain boot image spaces and app image spaces. |
| 10419 | // However, they are sorted by address, so boot images are not trivial to skip. |
| 10420 | ArrayRef<gc::space::ContinuousSpace* const> spaces(heap->GetContinuousSpaces()); |
| 10421 | DCHECK_GE(spaces.size(), heap->GetBootImageSpaces().size()); |
| 10422 | for (gc::space::ContinuousSpace* space : spaces) { |
| 10423 | if (space->IsImageSpace()) { |
| 10424 | gc::space::ImageSpace* image_space = space->AsImageSpace(); |
| 10425 | size_t offset = reinterpret_cast<const uint8_t*>(method) - image_space->Begin(); |
| 10426 | const ImageSection& methods_section = image_space->GetImageHeader().GetMethodsSection(); |
| 10427 | if (offset - methods_section.Offset() < methods_section.Size()) { |
| 10428 | // Grab the class loader from the first non-BCP class in the app image class table. |
| 10429 | // Note: If we allow classes from arbitrary parent or library class loaders in app |
| 10430 | // images, this shall need to be updated to actually search for the exact class. |
| 10431 | const ImageSection& class_table_section = |
| 10432 | image_space->GetImageHeader().GetClassTableSection(); |
| 10433 | CHECK_NE(class_table_section.Size(), 0u); |
| 10434 | const uint8_t* ptr = image_space->Begin() + class_table_section.Offset(); |
| 10435 | size_t read_count = 0; |
| 10436 | ClassTable::ClassSet class_set(ptr, /*make_copy_of_data=*/ false, &read_count); |
| 10437 | CHECK(!class_set.empty()); |
| 10438 | auto it = class_set.begin(); |
| 10439 | // No read barrier needed for references to non-movable image classes. |
| 10440 | while ((*it).Read<kWithoutReadBarrier>()->IsBootStrapClassLoaded()) { |
| 10441 | ++it; |
| 10442 | CHECK(it != class_set.end()); |
| 10443 | } |
| 10444 | ObjPtr<mirror::ClassLoader> class_loader = |
| 10445 | (*it).Read<kWithoutReadBarrier>()->GetClassLoader(); |
| 10446 | DCHECK(GetHoldingClassOfCopiedMethod(method)->GetClassLoader() == class_loader); |
| 10447 | return class_loader; |
| 10448 | } |
| 10449 | } |
| 10450 | } |
| 10451 | // Otherwise, the method must be in one of the `LinearAlloc` memory areas. |
| 10452 | jweak result = nullptr; |
| 10453 | { |
| 10454 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 10455 | for (const ClassLoaderData& data : class_loaders_) { |
| 10456 | if (data.allocator->Contains(method)) { |
| 10457 | result = data.weak_root; |
| 10458 | break; |
| 10459 | } |
| 10460 | } |
| 10461 | } |
| 10462 | CHECK(result != nullptr) << "Did not find allocator holding the copied method: " << method |
| 10463 | << " " << method->PrettyMethod(); |
| 10464 | // The `method` is alive, so the class loader must also be alive. |
| 10465 | return ObjPtr<mirror::ClassLoader>::DownCast( |
| 10466 | Runtime::Current()->GetJavaVM()->DecodeWeakGlobalAsStrong(result)); |
| 10467 | } |
| 10468 | |
Calin Juravle | 3378768 | 2019-07-26 14:27:18 -0700 | [diff] [blame] | 10469 | bool ClassLinker::DenyAccessBasedOnPublicSdk(ArtMethod* art_method ATTRIBUTE_UNUSED) const |
| 10470 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 10471 | // Should not be called on ClassLinker, only on AotClassLinker that overrides this. |
| 10472 | LOG(FATAL) << "UNREACHABLE"; |
| 10473 | UNREACHABLE(); |
| 10474 | } |
| 10475 | |
| 10476 | bool ClassLinker::DenyAccessBasedOnPublicSdk(ArtField* art_field ATTRIBUTE_UNUSED) const |
| 10477 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 10478 | // Should not be called on ClassLinker, only on AotClassLinker that overrides this. |
| 10479 | LOG(FATAL) << "UNREACHABLE"; |
| 10480 | UNREACHABLE(); |
| 10481 | } |
| 10482 | |
| 10483 | bool ClassLinker::DenyAccessBasedOnPublicSdk(const char* type_descriptor ATTRIBUTE_UNUSED) const { |
| 10484 | // Should not be called on ClassLinker, only on AotClassLinker that overrides this. |
| 10485 | LOG(FATAL) << "UNREACHABLE"; |
| 10486 | UNREACHABLE(); |
| 10487 | } |
| 10488 | |
Calin Juravle | 2c2724c | 2021-01-14 19:54:23 -0800 | [diff] [blame] | 10489 | void ClassLinker::SetEnablePublicSdkChecks(bool enabled ATTRIBUTE_UNUSED) { |
| 10490 | // Should not be called on ClassLinker, only on AotClassLinker that overrides this. |
| 10491 | LOG(FATAL) << "UNREACHABLE"; |
| 10492 | UNREACHABLE(); |
| 10493 | } |
| 10494 | |
Guo Li | 1ee0290 | 2022-07-13 15:57:31 +0800 | [diff] [blame] | 10495 | void ClassLinker::RemoveDexFromCaches(const DexFile& dex_file) { |
| 10496 | ReaderMutexLock mu(Thread::Current(), *Locks::dex_lock_); |
| 10497 | |
| 10498 | auto it = dex_caches_.find(&dex_file); |
| 10499 | if (it != dex_caches_.end()) { |
| 10500 | dex_caches_.erase(it); |
| 10501 | } |
| 10502 | } |
| 10503 | |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 10504 | // Instantiate ClassLinker::AllocClass. |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 10505 | template ObjPtr<mirror::Class> ClassLinker::AllocClass</* kMovable= */ true>( |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 10506 | Thread* self, |
| 10507 | ObjPtr<mirror::Class> java_lang_Class, |
| 10508 | uint32_t class_size); |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 10509 | template ObjPtr<mirror::Class> ClassLinker::AllocClass</* kMovable= */ false>( |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 10510 | Thread* self, |
| 10511 | ObjPtr<mirror::Class> java_lang_Class, |
| 10512 | uint32_t class_size); |
| 10513 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 10514 | } // namespace art |