Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 16 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 17 | #include "class_linker.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 18 | |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 19 | #include <unistd.h> |
| 20 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 21 | #include <algorithm> |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 22 | #include <deque> |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 23 | #include <forward_list> |
Ian Rogers | cf7f191 | 2014-10-22 22:06:39 -0700 | [diff] [blame] | 24 | #include <iostream> |
Vladimir Marko | 2130053 | 2017-01-24 18:06:55 +0000 | [diff] [blame] | 25 | #include <map> |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 26 | #include <memory> |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 27 | #include <queue> |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 28 | #include <string> |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 29 | #include <string_view> |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 30 | #include <tuple> |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 31 | #include <utility> |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 32 | #include <vector> |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 33 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 34 | #include "android-base/stringprintf.h" |
| 35 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 36 | #include "art_field-inl.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 37 | #include "art_method-inl.h" |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 38 | #include "barrier.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 39 | #include "base/arena_allocator.h" |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 40 | #include "base/casts.h" |
Andreas Gampe | 19f5416 | 2019-05-14 16:16:28 -0700 | [diff] [blame] | 41 | #include "base/file_utils.h" |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 42 | #include "base/hash_map.h" |
| 43 | #include "base/hash_set.h" |
David Sehr | 67bf42e | 2018-02-26 16:43:04 -0800 | [diff] [blame] | 44 | #include "base/leb128.h" |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 45 | #include "base/logging.h" |
Eric Holk | 74584e6 | 2021-02-18 14:39:17 -0800 | [diff] [blame] | 46 | #include "base/metrics/metrics.h" |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 47 | #include "base/mutex-inl.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 48 | #include "base/os.h" |
| 49 | #include "base/quasi_atomic.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 50 | #include "base/scoped_arena_containers.h" |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 51 | #include "base/scoped_flock.h" |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 52 | #include "base/stl_util.h" |
Vladimir Marko | b9c29f6 | 2019-03-20 14:22:51 +0000 | [diff] [blame] | 53 | #include "base/string_view_cpp20.h" |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 54 | #include "base/systrace.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 55 | #include "base/time_utils.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 56 | #include "base/unix_file/fd_file.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 57 | #include "base/utils.h" |
Andreas Gampe | b9aec2c | 2015-04-23 22:23:47 -0700 | [diff] [blame] | 58 | #include "base/value_object.h" |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 59 | #include "cha.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 60 | #include "class_linker-inl.h" |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 61 | #include "class_loader_utils.h" |
Vladimir Marko | 5868ada | 2020-05-12 11:50:34 +0100 | [diff] [blame] | 62 | #include "class_root-inl.h" |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 63 | #include "class_table-inl.h" |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 64 | #include "compiler_callbacks.h" |
Vladimir Marko | 606adb3 | 2018-04-05 14:49:24 +0100 | [diff] [blame] | 65 | #include "debug_print.h" |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 66 | #include "debugger.h" |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 67 | #include "dex/class_accessor-inl.h" |
David Sehr | b2ec9f5 | 2018-02-21 13:20:31 -0800 | [diff] [blame] | 68 | #include "dex/descriptors_names.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 69 | #include "dex/dex_file-inl.h" |
| 70 | #include "dex/dex_file_exception_helpers.h" |
| 71 | #include "dex/dex_file_loader.h" |
Andreas Gampe | ad1aa63 | 2019-01-02 10:30:54 -0800 | [diff] [blame] | 72 | #include "dex/signature-inl.h" |
David Sehr | 0225f8e | 2018-01-31 08:52:24 +0000 | [diff] [blame] | 73 | #include "dex/utf.h" |
Vladimir Marko | 5115a4d | 2019-10-17 14:56:47 +0100 | [diff] [blame] | 74 | #include "entrypoints/entrypoint_utils-inl.h" |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 75 | #include "entrypoints/runtime_asm_entrypoints.h" |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 76 | #include "experimental_flags.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 77 | #include "gc/accounting/card_table-inl.h" |
Mathieu Chartier | 03c1dd9 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 78 | #include "gc/accounting/heap_bitmap-inl.h" |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 79 | #include "gc/accounting/space_bitmap-inl.h" |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 80 | #include "gc/heap-visit-objects-inl.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 81 | #include "gc/heap.h" |
Mathieu Chartier | 1b1e31f | 2016-05-19 10:13:04 -0700 | [diff] [blame] | 82 | #include "gc/scoped_gc_critical_section.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 83 | #include "gc/space/image_space.h" |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 84 | #include "gc/space/space-inl.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 85 | #include "gc_root-inl.h" |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 86 | #include "handle_scope-inl.h" |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 87 | #include "hidden_api.h" |
Mathieu Chartier | 4a26f17 | 2016-01-26 14:26:18 -0800 | [diff] [blame] | 88 | #include "image-inl.h" |
Andreas Gampe | 75a7db6 | 2016-09-26 12:04:26 -0700 | [diff] [blame] | 89 | #include "imt_conflict_table.h" |
| 90 | #include "imtable-inl.h" |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 91 | #include "intern_table-inl.h" |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 92 | #include "interpreter/interpreter.h" |
Nicolas Geoffray | 0315efa | 2020-06-26 11:42:39 +0100 | [diff] [blame] | 93 | #include "interpreter/mterp/nterp.h" |
David Srbecky | fb3de3d | 2018-01-29 16:11:49 +0000 | [diff] [blame] | 94 | #include "jit/debugger_interface.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 95 | #include "jit/jit.h" |
| 96 | #include "jit/jit_code_cache.h" |
Vladimir Marko | a3ad0cd | 2018-05-04 10:06:38 +0100 | [diff] [blame] | 97 | #include "jni/java_vm_ext.h" |
| 98 | #include "jni/jni_internal.h" |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 99 | #include "linear_alloc.h" |
Andreas Gampe | 8e0f043 | 2018-10-24 13:38:03 -0700 | [diff] [blame] | 100 | #include "mirror/array-alloc-inl.h" |
| 101 | #include "mirror/array-inl.h" |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 102 | #include "mirror/call_site.h" |
Andreas Gampe | 70f5fd0 | 2018-10-24 19:58:37 -0700 | [diff] [blame] | 103 | #include "mirror/class-alloc-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 104 | #include "mirror/class-inl.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 105 | #include "mirror/class.h" |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 106 | #include "mirror/class_ext.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 107 | #include "mirror/class_loader.h" |
Ian Rogers | 39ebcb8 | 2013-05-30 16:57:23 -0700 | [diff] [blame] | 108 | #include "mirror/dex_cache-inl.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 109 | #include "mirror/dex_cache.h" |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 110 | #include "mirror/emulated_stack_frame.h" |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 111 | #include "mirror/field.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 112 | #include "mirror/iftable-inl.h" |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 113 | #include "mirror/method.h" |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 114 | #include "mirror/method_handle_impl.h" |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 115 | #include "mirror/method_handles_lookup.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 116 | #include "mirror/method_type.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 117 | #include "mirror/object-inl.h" |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 118 | #include "mirror/object-refvisitor-inl.h" |
Alex Light | a9bbc08 | 2019-11-14 14:51:41 -0800 | [diff] [blame] | 119 | #include "mirror/object.h" |
Andreas Gampe | 52ecb65 | 2018-10-24 15:18:21 -0700 | [diff] [blame] | 120 | #include "mirror/object_array-alloc-inl.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 121 | #include "mirror/object_array-inl.h" |
Alex Light | 133987d | 2020-03-26 19:22:12 +0000 | [diff] [blame] | 122 | #include "mirror/object_array.h" |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 123 | #include "mirror/object_reference.h" |
| 124 | #include "mirror/object_reference-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 125 | #include "mirror/proxy.h" |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 126 | #include "mirror/reference-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 127 | #include "mirror/stack_trace_element.h" |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 128 | #include "mirror/string-inl.h" |
Andreas Gampe | 501c3b0 | 2019-04-17 21:54:27 +0000 | [diff] [blame] | 129 | #include "mirror/throwable.h" |
Orion Hodson | 005ac51 | 2017-10-24 15:43:43 +0100 | [diff] [blame] | 130 | #include "mirror/var_handle.h" |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 131 | #include "native/dalvik_system_DexFile.h" |
Andreas Gampe | 373a9b5 | 2017-10-18 09:01:57 -0700 | [diff] [blame] | 132 | #include "nativehelper/scoped_local_ref.h" |
Nicolas Geoffray | c39af94 | 2021-01-25 08:43:57 +0000 | [diff] [blame] | 133 | #include "nterp_helpers.h" |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 134 | #include "oat.h" |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 135 | #include "oat_file-inl.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 136 | #include "oat_file.h" |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 137 | #include "oat_file_assistant.h" |
| 138 | #include "oat_file_manager.h" |
| 139 | #include "object_lock.h" |
David Sehr | 82d046e | 2018-04-23 08:14:19 -0700 | [diff] [blame] | 140 | #include "profile/profile_compilation_info.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 141 | #include "runtime.h" |
Andreas Gampe | ac30fa2 | 2017-01-18 21:02:36 -0800 | [diff] [blame] | 142 | #include "runtime_callbacks.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 143 | #include "scoped_thread_state_change-inl.h" |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 144 | #include "thread-inl.h" |
Alex Light | 133987d | 2020-03-26 19:22:12 +0000 | [diff] [blame] | 145 | #include "thread.h" |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 146 | #include "thread_list.h" |
Mathieu Chartier | 7778b88 | 2015-10-05 16:41:10 -0700 | [diff] [blame] | 147 | #include "trace.h" |
Vladimir Marko | b68bb7a | 2020-03-17 10:55:25 +0000 | [diff] [blame] | 148 | #include "transaction.h" |
Nicolas Geoffray | 6df4511 | 2021-02-07 21:51:58 +0000 | [diff] [blame] | 149 | #include "vdex_file.h" |
Andreas Gampe | a43ba3d | 2019-03-13 15:49:20 -0700 | [diff] [blame] | 150 | #include "verifier/class_verifier.h" |
Nicolas Geoffray | 8078996 | 2021-04-30 16:50:39 +0100 | [diff] [blame] | 151 | #include "verifier/verifier_deps.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 152 | #include "well_known_classes.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 153 | |
| 154 | namespace art { |
| 155 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 156 | using android::base::StringPrintf; |
| 157 | |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 158 | static constexpr bool kCheckImageObjects = kIsDebugBuild; |
Mathieu Chartier | 8790c7f | 2016-03-31 15:05:45 -0700 | [diff] [blame] | 159 | static constexpr bool kVerifyArtMethodDeclaringClasses = kIsDebugBuild; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 160 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 161 | static void ThrowNoClassDefFoundError(const char* fmt, ...) |
| 162 | __attribute__((__format__(__printf__, 1, 2))) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 163 | REQUIRES_SHARED(Locks::mutator_lock_); |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 164 | static void ThrowNoClassDefFoundError(const char* fmt, ...) { |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 165 | va_list args; |
| 166 | va_start(args, fmt); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 167 | Thread* self = Thread::Current(); |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 168 | self->ThrowNewExceptionV("Ljava/lang/NoClassDefFoundError;", fmt, args); |
Ian Rogers | cab0101 | 2012-01-10 17:35:46 -0800 | [diff] [blame] | 169 | va_end(args); |
| 170 | } |
| 171 | |
Nicolas Geoffray | 4dc6589 | 2021-07-05 17:43:35 +0100 | [diff] [blame] | 172 | static ObjPtr<mirror::Object> GetErroneousStateError(ObjPtr<mirror::Class> c) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 173 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 174 | ObjPtr<mirror::ClassExt> ext(c->GetExtData()); |
| 175 | if (ext == nullptr) { |
| 176 | return nullptr; |
| 177 | } else { |
Nicolas Geoffray | 4dc6589 | 2021-07-05 17:43:35 +0100 | [diff] [blame] | 178 | return ext->GetErroneousStateError(); |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | |
Nicolas Geoffray | 4dc6589 | 2021-07-05 17:43:35 +0100 | [diff] [blame] | 182 | static bool IsVerifyError(ObjPtr<mirror::Object> obj) |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 183 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Nicolas Geoffray | 4dc6589 | 2021-07-05 17:43:35 +0100 | [diff] [blame] | 184 | // This is slow, but we only use it for rethrowing an error, and for DCHECK. |
| 185 | return obj->GetClass()->DescriptorEquals("Ljava/lang/VerifyError;"); |
| 186 | } |
| 187 | |
| 188 | // Helper for ThrowEarlierClassFailure. Throws the stored error. |
| 189 | static void HandleEarlierErroneousStateError(Thread* self, |
| 190 | ClassLinker* class_linker, |
| 191 | ObjPtr<mirror::Class> c) |
| 192 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 193 | ObjPtr<mirror::Object> obj = GetErroneousStateError(c); |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 194 | DCHECK(obj != nullptr); |
| 195 | self->AssertNoPendingException(); |
Nicolas Geoffray | 4dc6589 | 2021-07-05 17:43:35 +0100 | [diff] [blame] | 196 | DCHECK(!obj->IsClass()); |
| 197 | ObjPtr<mirror::Class> throwable_class = GetClassRoot<mirror::Throwable>(class_linker); |
| 198 | ObjPtr<mirror::Class> error_class = obj->GetClass(); |
| 199 | CHECK(throwable_class->IsAssignableFrom(error_class)); |
| 200 | self->SetException(obj->AsThrowable()); |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 201 | self->AssertPendingException(); |
| 202 | } |
| 203 | |
Nicolas Geoffray | 66934ef | 2021-07-07 14:56:23 +0100 | [diff] [blame] | 204 | static void UpdateClassAfterVerification(Handle<mirror::Class> klass, |
| 205 | PointerSize pointer_size, |
| 206 | verifier::FailureKind failure_kind) |
Andreas Gampe | 5b20b35 | 2018-10-11 19:03:20 -0700 | [diff] [blame] | 207 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Nicolas Geoffray | 0039182 | 2019-12-10 10:17:23 +0000 | [diff] [blame] | 208 | Runtime* runtime = Runtime::Current(); |
| 209 | ClassLinker* class_linker = runtime->GetClassLinker(); |
Nicolas Geoffray | 2990b88 | 2021-11-30 15:48:49 +0000 | [diff] [blame] | 210 | if (klass->IsVerified() && (failure_kind == verifier::FailureKind::kNoFailure)) { |
Andreas Gampe | 5b20b35 | 2018-10-11 19:03:20 -0700 | [diff] [blame] | 211 | klass->SetSkipAccessChecksFlagOnAllMethods(pointer_size); |
Nicolas Geoffray | 66934ef | 2021-07-07 14:56:23 +0100 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | // Now that the class has passed verification, try to set nterp entrypoints |
| 215 | // to methods that currently use the switch interpreter. |
| 216 | if (interpreter::CanRuntimeUseNterp()) { |
| 217 | for (ArtMethod& m : klass->GetMethods(pointer_size)) { |
Nicolas Geoffray | c8a694d | 2022-01-17 17:12:38 +0000 | [diff] [blame] | 218 | if (class_linker->IsQuickToInterpreterBridge(m.GetEntryPointFromQuickCompiledCode())) { |
| 219 | runtime->GetInstrumentation()->InitializeMethodsCode(&m, /*aot_code=*/nullptr); |
| 220 | } |
Nicolas Geoffray | 0039182 | 2019-12-10 10:17:23 +0000 | [diff] [blame] | 221 | } |
Andreas Gampe | 5b20b35 | 2018-10-11 19:03:20 -0700 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 225 | // Callback responsible for making a batch of classes visibly initialized |
| 226 | // after all threads have called it from a checkpoint, ensuring visibility. |
| 227 | class ClassLinker::VisiblyInitializedCallback final |
| 228 | : public Closure, public IntrusiveForwardListNode<VisiblyInitializedCallback> { |
| 229 | public: |
| 230 | explicit VisiblyInitializedCallback(ClassLinker* class_linker) |
| 231 | : class_linker_(class_linker), |
| 232 | num_classes_(0u), |
| 233 | thread_visibility_counter_(0), |
| 234 | barriers_() { |
| 235 | std::fill_n(classes_, kMaxClasses, nullptr); |
| 236 | } |
| 237 | |
| 238 | bool IsEmpty() const { |
| 239 | DCHECK_LE(num_classes_, kMaxClasses); |
| 240 | return num_classes_ == 0u; |
| 241 | } |
| 242 | |
| 243 | bool IsFull() const { |
| 244 | DCHECK_LE(num_classes_, kMaxClasses); |
| 245 | return num_classes_ == kMaxClasses; |
| 246 | } |
| 247 | |
| 248 | void AddClass(Thread* self, ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 249 | DCHECK_EQ(klass->GetStatus(), ClassStatus::kInitialized); |
| 250 | DCHECK(!IsFull()); |
| 251 | classes_[num_classes_] = self->GetJniEnv()->GetVm()->AddWeakGlobalRef(self, klass); |
| 252 | ++num_classes_; |
| 253 | } |
| 254 | |
| 255 | void AddBarrier(Barrier* barrier) { |
| 256 | barriers_.push_front(barrier); |
| 257 | } |
| 258 | |
| 259 | std::forward_list<Barrier*> GetAndClearBarriers() { |
| 260 | std::forward_list<Barrier*> result; |
| 261 | result.swap(barriers_); |
| 262 | result.reverse(); // Return barriers in insertion order. |
| 263 | return result; |
| 264 | } |
| 265 | |
| 266 | void MakeVisible(Thread* self) { |
| 267 | DCHECK_EQ(thread_visibility_counter_.load(std::memory_order_relaxed), 0); |
| 268 | size_t count = Runtime::Current()->GetThreadList()->RunCheckpoint(this); |
| 269 | AdjustThreadVisibilityCounter(self, count); |
| 270 | } |
| 271 | |
| 272 | void Run(Thread* self) override { |
| 273 | self->ClearMakeVisiblyInitializedCounter(); |
| 274 | AdjustThreadVisibilityCounter(self, -1); |
| 275 | } |
| 276 | |
| 277 | private: |
| 278 | void AdjustThreadVisibilityCounter(Thread* self, ssize_t adjustment) { |
| 279 | ssize_t old = thread_visibility_counter_.fetch_add(adjustment, std::memory_order_relaxed); |
| 280 | if (old + adjustment == 0) { |
| 281 | // All threads passed the checkpoint. Mark classes as visibly initialized. |
| 282 | { |
| 283 | ScopedObjectAccess soa(self); |
| 284 | StackHandleScope<1u> hs(self); |
| 285 | MutableHandle<mirror::Class> klass = hs.NewHandle<mirror::Class>(nullptr); |
| 286 | JavaVMExt* vm = self->GetJniEnv()->GetVm(); |
| 287 | for (size_t i = 0, num = num_classes_; i != num; ++i) { |
| 288 | klass.Assign(ObjPtr<mirror::Class>::DownCast(self->DecodeJObject(classes_[i]))); |
| 289 | vm->DeleteWeakGlobalRef(self, classes_[i]); |
| 290 | if (klass != nullptr) { |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 291 | mirror::Class::SetStatus(klass, ClassStatus::kVisiblyInitialized, self); |
Vladimir Marko | 86c8752 | 2020-05-11 16:55:55 +0100 | [diff] [blame] | 292 | class_linker_->FixupStaticTrampolines(self, klass.Get()); |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | num_classes_ = 0u; |
| 296 | } |
| 297 | class_linker_->VisiblyInitializedCallbackDone(self, this); |
| 298 | } |
| 299 | } |
| 300 | |
Vladimir Marko | 9f18fbc | 2019-07-31 15:06:12 +0100 | [diff] [blame] | 301 | static constexpr size_t kMaxClasses = 16; |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 302 | |
| 303 | ClassLinker* const class_linker_; |
| 304 | size_t num_classes_; |
| 305 | jweak classes_[kMaxClasses]; |
| 306 | |
| 307 | // The thread visibility counter starts at 0 and it is incremented by the number of |
| 308 | // threads that need to run this callback (by the thread that request the callback |
| 309 | // to be run) and decremented once for each `Run()` execution. When it reaches 0, |
| 310 | // whether after the increment or after a decrement, we know that `Run()` was executed |
| 311 | // for all threads and therefore we can mark the classes as visibly initialized. |
| 312 | std::atomic<ssize_t> thread_visibility_counter_; |
| 313 | |
| 314 | // List of barries to `Pass()` for threads that wait for the callback to complete. |
| 315 | std::forward_list<Barrier*> barriers_; |
| 316 | }; |
| 317 | |
| 318 | void ClassLinker::MakeInitializedClassesVisiblyInitialized(Thread* self, bool wait) { |
| 319 | if (kRuntimeISA == InstructionSet::kX86 || kRuntimeISA == InstructionSet::kX86_64) { |
| 320 | return; // Nothing to do. Thanks to the x86 memory model classes skip the initialized status. |
| 321 | } |
| 322 | std::optional<Barrier> maybe_barrier; // Avoid constructing the Barrier for `wait == false`. |
| 323 | if (wait) { |
| 324 | maybe_barrier.emplace(0); |
| 325 | } |
| 326 | int wait_count = 0; |
| 327 | VisiblyInitializedCallback* callback = nullptr; |
| 328 | { |
| 329 | MutexLock lock(self, visibly_initialized_callback_lock_); |
| 330 | if (visibly_initialized_callback_ != nullptr && !visibly_initialized_callback_->IsEmpty()) { |
| 331 | callback = visibly_initialized_callback_.release(); |
| 332 | running_visibly_initialized_callbacks_.push_front(*callback); |
| 333 | } |
| 334 | if (wait) { |
| 335 | DCHECK(maybe_barrier.has_value()); |
| 336 | Barrier* barrier = std::addressof(*maybe_barrier); |
| 337 | for (VisiblyInitializedCallback& cb : running_visibly_initialized_callbacks_) { |
| 338 | cb.AddBarrier(barrier); |
| 339 | ++wait_count; |
| 340 | } |
| 341 | } |
| 342 | } |
| 343 | if (callback != nullptr) { |
| 344 | callback->MakeVisible(self); |
| 345 | } |
| 346 | if (wait_count != 0) { |
| 347 | DCHECK(maybe_barrier.has_value()); |
| 348 | maybe_barrier->Increment(self, wait_count); |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | void ClassLinker::VisiblyInitializedCallbackDone(Thread* self, |
| 353 | VisiblyInitializedCallback* callback) { |
| 354 | MutexLock lock(self, visibly_initialized_callback_lock_); |
| 355 | // Pass the barriers if requested. |
| 356 | for (Barrier* barrier : callback->GetAndClearBarriers()) { |
| 357 | barrier->Pass(self); |
| 358 | } |
| 359 | // Remove the callback from the list of running callbacks. |
| 360 | auto before = running_visibly_initialized_callbacks_.before_begin(); |
| 361 | auto it = running_visibly_initialized_callbacks_.begin(); |
| 362 | DCHECK(it != running_visibly_initialized_callbacks_.end()); |
| 363 | while (std::addressof(*it) != callback) { |
| 364 | before = it; |
| 365 | ++it; |
| 366 | DCHECK(it != running_visibly_initialized_callbacks_.end()); |
| 367 | } |
| 368 | running_visibly_initialized_callbacks_.erase_after(before); |
| 369 | // Reuse or destroy the callback object. |
| 370 | if (visibly_initialized_callback_ == nullptr) { |
| 371 | visibly_initialized_callback_.reset(callback); |
| 372 | } else { |
| 373 | delete callback; |
| 374 | } |
| 375 | } |
| 376 | |
Alex Light | fb11957 | 2019-09-18 15:04:53 -0700 | [diff] [blame] | 377 | void ClassLinker::ForceClassInitialized(Thread* self, Handle<mirror::Class> klass) { |
| 378 | ClassLinker::VisiblyInitializedCallback* cb = MarkClassInitialized(self, klass); |
| 379 | if (cb != nullptr) { |
| 380 | cb->MakeVisible(self); |
| 381 | } |
| 382 | ScopedThreadSuspension sts(self, ThreadState::kSuspended); |
| 383 | MakeInitializedClassesVisiblyInitialized(self, /*wait=*/true); |
| 384 | } |
| 385 | |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 386 | ClassLinker::VisiblyInitializedCallback* ClassLinker::MarkClassInitialized( |
| 387 | Thread* self, Handle<mirror::Class> klass) { |
| 388 | if (kRuntimeISA == InstructionSet::kX86 || kRuntimeISA == InstructionSet::kX86_64) { |
| 389 | // Thanks to the x86 memory model, we do not need any memory fences and |
| 390 | // we can immediately mark the class as visibly initialized. |
| 391 | mirror::Class::SetStatus(klass, ClassStatus::kVisiblyInitialized, self); |
Vladimir Marko | 86c8752 | 2020-05-11 16:55:55 +0100 | [diff] [blame] | 392 | FixupStaticTrampolines(self, klass.Get()); |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 393 | return nullptr; |
| 394 | } |
| 395 | if (Runtime::Current()->IsActiveTransaction()) { |
| 396 | // Transactions are single-threaded, so we can mark the class as visibly intialized. |
| 397 | // (Otherwise we'd need to track the callback's entry in the transaction for rollback.) |
| 398 | mirror::Class::SetStatus(klass, ClassStatus::kVisiblyInitialized, self); |
Vladimir Marko | 86c8752 | 2020-05-11 16:55:55 +0100 | [diff] [blame] | 399 | FixupStaticTrampolines(self, klass.Get()); |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 400 | return nullptr; |
| 401 | } |
| 402 | mirror::Class::SetStatus(klass, ClassStatus::kInitialized, self); |
| 403 | MutexLock lock(self, visibly_initialized_callback_lock_); |
| 404 | if (visibly_initialized_callback_ == nullptr) { |
| 405 | visibly_initialized_callback_.reset(new VisiblyInitializedCallback(this)); |
| 406 | } |
| 407 | DCHECK(!visibly_initialized_callback_->IsFull()); |
| 408 | visibly_initialized_callback_->AddClass(self, klass.Get()); |
| 409 | |
| 410 | if (visibly_initialized_callback_->IsFull()) { |
| 411 | VisiblyInitializedCallback* callback = visibly_initialized_callback_.release(); |
| 412 | running_visibly_initialized_callbacks_.push_front(*callback); |
| 413 | return callback; |
| 414 | } else { |
| 415 | return nullptr; |
| 416 | } |
| 417 | } |
| 418 | |
Vladimir Marko | 86c8752 | 2020-05-11 16:55:55 +0100 | [diff] [blame] | 419 | const void* ClassLinker::RegisterNative( |
| 420 | Thread* self, ArtMethod* method, const void* native_method) { |
| 421 | CHECK(method->IsNative()) << method->PrettyMethod(); |
| 422 | CHECK(native_method != nullptr) << method->PrettyMethod(); |
| 423 | void* new_native_method = nullptr; |
| 424 | Runtime* runtime = Runtime::Current(); |
| 425 | runtime->GetRuntimeCallbacks()->RegisterNativeMethod(method, |
| 426 | native_method, |
| 427 | /*out*/&new_native_method); |
| 428 | if (method->IsCriticalNative()) { |
| 429 | MutexLock lock(self, critical_native_code_with_clinit_check_lock_); |
| 430 | // Remove old registered method if any. |
| 431 | auto it = critical_native_code_with_clinit_check_.find(method); |
| 432 | if (it != critical_native_code_with_clinit_check_.end()) { |
| 433 | critical_native_code_with_clinit_check_.erase(it); |
| 434 | } |
| 435 | // To ensure correct memory visibility, we need the class to be visibly |
| 436 | // initialized before we can set the JNI entrypoint. |
| 437 | if (method->GetDeclaringClass()->IsVisiblyInitialized()) { |
| 438 | method->SetEntryPointFromJni(new_native_method); |
| 439 | } else { |
| 440 | critical_native_code_with_clinit_check_.emplace(method, new_native_method); |
| 441 | } |
| 442 | } else { |
| 443 | method->SetEntryPointFromJni(new_native_method); |
| 444 | } |
| 445 | return new_native_method; |
| 446 | } |
| 447 | |
| 448 | void ClassLinker::UnregisterNative(Thread* self, ArtMethod* method) { |
| 449 | CHECK(method->IsNative()) << method->PrettyMethod(); |
| 450 | // Restore stub to lookup native pointer via dlsym. |
| 451 | if (method->IsCriticalNative()) { |
| 452 | MutexLock lock(self, critical_native_code_with_clinit_check_lock_); |
| 453 | auto it = critical_native_code_with_clinit_check_.find(method); |
| 454 | if (it != critical_native_code_with_clinit_check_.end()) { |
| 455 | critical_native_code_with_clinit_check_.erase(it); |
| 456 | } |
| 457 | method->SetEntryPointFromJni(GetJniDlsymLookupCriticalStub()); |
| 458 | } else { |
| 459 | method->SetEntryPointFromJni(GetJniDlsymLookupStub()); |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | const void* ClassLinker::GetRegisteredNative(Thread* self, ArtMethod* method) { |
| 464 | if (method->IsCriticalNative()) { |
| 465 | MutexLock lock(self, critical_native_code_with_clinit_check_lock_); |
| 466 | auto it = critical_native_code_with_clinit_check_.find(method); |
| 467 | if (it != critical_native_code_with_clinit_check_.end()) { |
| 468 | return it->second; |
| 469 | } |
| 470 | const void* native_code = method->GetEntryPointFromJni(); |
| 471 | return IsJniDlsymLookupCriticalStub(native_code) ? nullptr : native_code; |
| 472 | } else { |
| 473 | const void* native_code = method->GetEntryPointFromJni(); |
| 474 | return IsJniDlsymLookupStub(native_code) ? nullptr : native_code; |
| 475 | } |
| 476 | } |
| 477 | |
Andreas Gampe | 7b3063b | 2019-01-07 14:12:52 -0800 | [diff] [blame] | 478 | void ClassLinker::ThrowEarlierClassFailure(ObjPtr<mirror::Class> c, |
| 479 | bool wrap_in_no_class_def, |
| 480 | bool log) { |
Elliott Hughes | 5c59994 | 2012-06-13 16:45:05 -0700 | [diff] [blame] | 481 | // The class failed to initialize on a previous attempt, so we want to throw |
| 482 | // a NoClassDefFoundError (v2 2.17.5). The exception to this rule is if we |
| 483 | // failed in verification, in which case v2 5.4.1 says we need to re-throw |
| 484 | // the previous error. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 485 | Runtime* const runtime = Runtime::Current(); |
| 486 | if (!runtime->IsAotCompiler()) { // Give info if this occurs at runtime. |
Andreas Gampe | 3d6b470 | 2015-09-21 08:35:52 -0700 | [diff] [blame] | 487 | std::string extra; |
Nicolas Geoffray | 4dc6589 | 2021-07-05 17:43:35 +0100 | [diff] [blame] | 488 | ObjPtr<mirror::Object> verify_error = GetErroneousStateError(c); |
Vladimir Marko | bb206de | 2019-03-28 10:30:32 +0000 | [diff] [blame] | 489 | if (verify_error != nullptr) { |
Nicolas Geoffray | 4dc6589 | 2021-07-05 17:43:35 +0100 | [diff] [blame] | 490 | DCHECK(!verify_error->IsClass()); |
| 491 | extra = verify_error->AsThrowable()->Dump(); |
Andreas Gampe | 3d6b470 | 2015-09-21 08:35:52 -0700 | [diff] [blame] | 492 | } |
Andreas Gampe | 7b3063b | 2019-01-07 14:12:52 -0800 | [diff] [blame] | 493 | if (log) { |
| 494 | LOG(INFO) << "Rejecting re-init on previously-failed class " << c->PrettyClass() |
| 495 | << ": " << extra; |
| 496 | } |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 497 | } |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 498 | |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 499 | CHECK(c->IsErroneous()) << c->PrettyClass() << " " << c->GetStatus(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 500 | Thread* self = Thread::Current(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 501 | if (runtime->IsAotCompiler()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 502 | // At compile time, accurate errors and NCDFE are disabled to speed compilation. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 503 | ObjPtr<mirror::Throwable> pre_allocated = runtime->GetPreAllocatedNoClassDefFoundError(); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 504 | self->SetException(pre_allocated); |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 505 | } else { |
Nicolas Geoffray | 4dc6589 | 2021-07-05 17:43:35 +0100 | [diff] [blame] | 506 | ObjPtr<mirror::Object> erroneous_state_error = GetErroneousStateError(c); |
| 507 | if (erroneous_state_error != nullptr) { |
Andreas Gampe | cb08695 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 508 | // Rethrow stored error. |
Nicolas Geoffray | 4dc6589 | 2021-07-05 17:43:35 +0100 | [diff] [blame] | 509 | HandleEarlierErroneousStateError(self, this, c); |
Andreas Gampe | cb08695 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 510 | } |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 511 | // TODO This might be wrong if we hit an OOME while allocating the ClassExt. In that case we |
| 512 | // might have meant to go down the earlier if statement with the original error but it got |
| 513 | // swallowed by the OOM so we end up here. |
Nicolas Geoffray | 4dc6589 | 2021-07-05 17:43:35 +0100 | [diff] [blame] | 514 | if (erroneous_state_error == nullptr || |
| 515 | (wrap_in_no_class_def && !IsVerifyError(erroneous_state_error))) { |
Andreas Gampe | cb08695 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 516 | // If there isn't a recorded earlier error, or this is a repeat throw from initialization, |
| 517 | // the top-level exception must be a NoClassDefFoundError. The potentially already pending |
| 518 | // exception will be a cause. |
| 519 | self->ThrowNewWrappedException("Ljava/lang/NoClassDefFoundError;", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 520 | c->PrettyDescriptor().c_str()); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 521 | } |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 522 | } |
| 523 | } |
| 524 | |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 525 | static void VlogClassInitializationFailure(Handle<mirror::Class> klass) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 526 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 527 | if (VLOG_IS_ON(class_linker)) { |
| 528 | std::string temp; |
| 529 | LOG(INFO) << "Failed to initialize class " << klass->GetDescriptor(&temp) << " from " |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 530 | << klass->GetLocation() << "\n" << Thread::Current()->GetException()->Dump(); |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 531 | } |
| 532 | } |
| 533 | |
| 534 | static void WrapExceptionInInitializer(Handle<mirror::Class> klass) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 535 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 536 | Thread* self = Thread::Current(); |
| 537 | JNIEnv* env = self->GetJniEnv(); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 538 | |
| 539 | ScopedLocalRef<jthrowable> cause(env, env->ExceptionOccurred()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 540 | CHECK(cause.get() != nullptr); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 541 | |
David Srbecky | 346fd96 | 2020-07-27 16:51:00 +0100 | [diff] [blame] | 542 | // Boot classpath classes should not fail initialization. This is a consistency debug check. |
| 543 | // 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] | 544 | if (klass->GetClassLoader() == nullptr && !Runtime::Current()->IsAotCompiler()) { |
Andreas Gampe | 22f71d2 | 2016-11-21 10:10:08 -0800 | [diff] [blame] | 545 | std::string tmp; |
Alex Light | 5047d9f | 2018-03-09 15:44:31 -0800 | [diff] [blame] | 546 | // We want to LOG(FATAL) on debug builds since this really shouldn't be happening but we need to |
| 547 | // make sure to only do it if we don't have AsyncExceptions being thrown around since those |
| 548 | // could have caused the error. |
| 549 | bool known_impossible = kIsDebugBuild && !Runtime::Current()->AreAsyncExceptionsThrown(); |
| 550 | LOG(known_impossible ? FATAL : WARNING) << klass->GetDescriptor(&tmp) |
| 551 | << " failed initialization: " |
| 552 | << self->GetException()->Dump(); |
Andreas Gampe | 22f71d2 | 2016-11-21 10:10:08 -0800 | [diff] [blame] | 553 | } |
| 554 | |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 555 | env->ExceptionClear(); |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 556 | bool is_error = env->IsInstanceOf(cause.get(), WellKnownClasses::java_lang_Error); |
| 557 | env->Throw(cause.get()); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 558 | |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 559 | // We only wrap non-Error exceptions; an Error can just be used as-is. |
| 560 | if (!is_error) { |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 561 | self->ThrowNewWrappedException("Ljava/lang/ExceptionInInitializerError;", nullptr); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 562 | } |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 563 | VlogClassInitializationFailure(klass); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 564 | } |
| 565 | |
Andreas Gampe | 87658f3 | 2019-04-18 18:39:02 +0000 | [diff] [blame] | 566 | ClassLinker::ClassLinker(InternTable* intern_table, bool fast_class_not_found_exceptions) |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 567 | : boot_class_table_(new ClassTable()), |
| 568 | failed_dex_cache_class_lookups_(0), |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 569 | class_roots_(nullptr), |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 570 | find_array_class_cache_next_victim_(0), |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 571 | init_done_(false), |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 572 | log_new_roots_(false), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 573 | intern_table_(intern_table), |
Andreas Gampe | 87658f3 | 2019-04-18 18:39:02 +0000 | [diff] [blame] | 574 | fast_class_not_found_exceptions_(fast_class_not_found_exceptions), |
Vladimir Marko | 7dac864 | 2019-11-06 17:09:30 +0000 | [diff] [blame] | 575 | jni_dlsym_lookup_trampoline_(nullptr), |
Vladimir Marko | fa458ac | 2020-02-12 14:08:07 +0000 | [diff] [blame] | 576 | jni_dlsym_lookup_critical_trampoline_(nullptr), |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 577 | quick_resolution_trampoline_(nullptr), |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 578 | quick_imt_conflict_trampoline_(nullptr), |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 579 | quick_generic_jni_trampoline_(nullptr), |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 580 | quick_to_interpreter_bridge_trampoline_(nullptr), |
Nicolas Geoffray | c39af94 | 2021-01-25 08:43:57 +0000 | [diff] [blame] | 581 | nterp_trampoline_(nullptr), |
Andreas Gampe | c1ac9ee | 2017-07-24 22:35:49 -0700 | [diff] [blame] | 582 | image_pointer_size_(kRuntimePointerSize), |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 583 | visibly_initialized_callback_lock_("visibly initialized callback lock"), |
| 584 | visibly_initialized_callback_(nullptr), |
Vladimir Marko | 86c8752 | 2020-05-11 16:55:55 +0100 | [diff] [blame] | 585 | critical_native_code_with_clinit_check_lock_("critical native code with clinit check lock"), |
| 586 | critical_native_code_with_clinit_check_(), |
Andreas Gampe | 7dface3 | 2017-07-25 21:32:59 -0700 | [diff] [blame] | 587 | cha_(Runtime::Current()->IsAotCompiler() ? nullptr : new ClassHierarchyAnalysis()) { |
| 588 | // For CHA disabled during Aot, see b/34193647. |
| 589 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 590 | CHECK(intern_table_ != nullptr); |
Andreas Gampe | 8ac7595 | 2015-06-02 21:01:45 -0700 | [diff] [blame] | 591 | static_assert(kFindArrayCacheSize == arraysize(find_array_class_cache_), |
| 592 | "Array cache size wrong."); |
| 593 | std::fill_n(find_array_class_cache_, kFindArrayCacheSize, GcRoot<mirror::Class>(nullptr)); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 594 | } |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 595 | |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 596 | void ClassLinker::CheckSystemClass(Thread* self, Handle<mirror::Class> c1, const char* descriptor) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 597 | ObjPtr<mirror::Class> c2 = FindSystemClass(self, descriptor); |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 598 | if (c2 == nullptr) { |
| 599 | LOG(FATAL) << "Could not find class " << descriptor; |
| 600 | UNREACHABLE(); |
| 601 | } |
| 602 | if (c1.Get() != c2) { |
| 603 | std::ostringstream os1, os2; |
| 604 | c1->DumpClass(os1, mirror::Class::kDumpClassFullDetail); |
| 605 | c2->DumpClass(os2, mirror::Class::kDumpClassFullDetail); |
| 606 | LOG(FATAL) << "InitWithoutImage: Class mismatch for " << descriptor |
| 607 | << ". This is most likely the result of a broken build. Make sure that " |
| 608 | << "libcore and art projects match.\n\n" |
| 609 | << os1.str() << "\n\n" << os2.str(); |
| 610 | UNREACHABLE(); |
| 611 | } |
| 612 | } |
| 613 | |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 614 | ObjPtr<mirror::IfTable> AllocIfTable(Thread* self, |
| 615 | size_t ifcount, |
| 616 | ObjPtr<mirror::Class> iftable_class) |
| 617 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 618 | DCHECK(iftable_class->IsArrayClass()); |
| 619 | DCHECK(iftable_class->GetComponentType()->IsObjectClass()); |
| 620 | return ObjPtr<mirror::IfTable>::DownCast(ObjPtr<mirror::ObjectArray<mirror::Object>>( |
| 621 | mirror::IfTable::Alloc(self, iftable_class, ifcount * mirror::IfTable::kMax))); |
| 622 | } |
| 623 | |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 624 | bool ClassLinker::InitWithoutImage(std::vector<std::unique_ptr<const DexFile>> boot_class_path, |
| 625 | std::string* error_msg) { |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 626 | VLOG(startup) << "ClassLinker::Init"; |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 627 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 628 | Thread* const self = Thread::Current(); |
| 629 | Runtime* const runtime = Runtime::Current(); |
| 630 | gc::Heap* const heap = runtime->GetHeap(); |
| 631 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 632 | CHECK(!heap->HasBootImageSpace()) << "Runtime has image. We should use it."; |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 633 | CHECK(!init_done_); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 634 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 635 | // Use the pointer size from the runtime since we are probably creating the image. |
| 636 | image_pointer_size_ = InstructionSetPointerSize(runtime->GetInstructionSet()); |
| 637 | |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 638 | // java_lang_Class comes first, it's needed for AllocClass |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 639 | // 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] | 640 | heap->IncrementDisableMovingGC(self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 641 | StackHandleScope<64> hs(self); // 64 is picked arbitrarily. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 642 | auto class_class_size = mirror::Class::ClassClassSize(image_pointer_size_); |
Mathieu Chartier | d7a7f2f | 2018-09-07 11:57:18 -0700 | [diff] [blame] | 643 | // Allocate the object as non-movable so that there are no cases where Object::IsClass returns |
| 644 | // the incorrect result when comparing to-space vs from-space. |
Vladimir Marko | d7e9bbf | 2019-03-28 13:18:57 +0000 | [diff] [blame] | 645 | Handle<mirror::Class> java_lang_Class(hs.NewHandle(ObjPtr<mirror::Class>::DownCast( |
Vladimir Marko | 991cd5c | 2019-05-30 14:23:39 +0100 | [diff] [blame] | 646 | heap->AllocNonMovableObject(self, nullptr, class_class_size, VoidFunctor())))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 647 | CHECK(java_lang_Class != nullptr); |
Vladimir Marko | 317892b | 2018-05-31 11:11:32 +0100 | [diff] [blame] | 648 | java_lang_Class->SetClassFlags(mirror::kClassFlagClass); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 649 | java_lang_Class->SetClass(java_lang_Class.Get()); |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 650 | if (kUseBakerReadBarrier) { |
| 651 | java_lang_Class->AssertReadBarrierState(); |
Hiroshi Yamauchi | 9d04a20 | 2014-01-31 13:35:49 -0800 | [diff] [blame] | 652 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 653 | java_lang_Class->SetClassSize(class_class_size); |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 654 | java_lang_Class->SetPrimitiveType(Primitive::kPrimNot); |
Mathieu Chartier | 1d27b34 | 2014-01-28 12:51:09 -0800 | [diff] [blame] | 655 | heap->DecrementDisableMovingGC(self); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 656 | // AllocClass(ObjPtr<mirror::Class>) can now be used |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 657 | |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 658 | // Class[] is used for reflection support. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 659 | 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] | 660 | Handle<mirror::Class> class_array_class(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 661 | AllocClass(self, java_lang_Class.Get(), class_array_class_size))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 662 | class_array_class->SetComponentType(java_lang_Class.Get()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 663 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 664 | // 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] | 665 | Handle<mirror::Class> java_lang_Object(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 666 | AllocClass(self, java_lang_Class.Get(), mirror::Object::ClassSize(image_pointer_size_)))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 667 | CHECK(java_lang_Object != nullptr); |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 668 | // backfill Object as the super class of Class. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 669 | java_lang_Class->SetSuperClass(java_lang_Object.Get()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 670 | mirror::Class::SetStatus(java_lang_Object, ClassStatus::kLoaded, self); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 671 | |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 672 | java_lang_Object->SetObjectSize(sizeof(mirror::Object)); |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 673 | // Allocate in non-movable so that it's possible to check if a JNI weak global ref has been |
| 674 | // cleared without triggering the read barrier and unintentionally mark the sentinel alive. |
Vladimir Marko | 991cd5c | 2019-05-30 14:23:39 +0100 | [diff] [blame] | 675 | runtime->SetSentinel(heap->AllocNonMovableObject(self, |
| 676 | java_lang_Object.Get(), |
| 677 | java_lang_Object->GetObjectSize(), |
| 678 | VoidFunctor())); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 679 | |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 680 | // Initialize the SubtypeCheck bitstring for java.lang.Object and java.lang.Class. |
Vladimir Marko | 305c38b | 2018-02-14 11:50:07 +0000 | [diff] [blame] | 681 | if (kBitstringSubtypeCheckEnabled) { |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 682 | // It might seem the lock here is unnecessary, however all the SubtypeCheck |
| 683 | // functions are annotated to require locks all the way down. |
| 684 | // |
| 685 | // We take the lock here to avoid using NO_THREAD_SAFETY_ANALYSIS. |
| 686 | MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_); |
Vladimir Marko | 38b8b25 | 2018-01-02 19:07:06 +0000 | [diff] [blame] | 687 | SubtypeCheck<ObjPtr<mirror::Class>>::EnsureInitialized(java_lang_Object.Get()); |
| 688 | SubtypeCheck<ObjPtr<mirror::Class>>::EnsureInitialized(java_lang_Class.Get()); |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 689 | } |
| 690 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 691 | // Object[] next to hold class roots. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 692 | Handle<mirror::Class> object_array_class(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 693 | AllocClass(self, java_lang_Class.Get(), |
| 694 | mirror::ObjectArray<mirror::Object>::ClassSize(image_pointer_size_)))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 695 | object_array_class->SetComponentType(java_lang_Object.Get()); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 696 | |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 697 | // Setup java.lang.String. |
| 698 | // |
| 699 | // We make this class non-movable for the unlikely case where it were to be |
| 700 | // moved by a sticky-bit (minor) collection when using the Generational |
| 701 | // Concurrent Copying (CC) collector, potentially creating a stale reference |
| 702 | // in the `klass_` field of one of its instances allocated in the Large-Object |
| 703 | // Space (LOS) -- see the comment about the dirty card scanning logic in |
| 704 | // art::gc::collector::ConcurrentCopying::MarkingPhase. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 705 | Handle<mirror::Class> java_lang_String(hs.NewHandle( |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 706 | AllocClass</* kMovable= */ false>( |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 707 | self, java_lang_Class.Get(), mirror::String::ClassSize(image_pointer_size_)))); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 708 | java_lang_String->SetStringClass(); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 709 | mirror::Class::SetStatus(java_lang_String, ClassStatus::kResolved, self); |
Jesse Wilson | 1415074 | 2011-07-29 19:04:44 -0400 | [diff] [blame] | 710 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 711 | // Setup java.lang.ref.Reference. |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 712 | Handle<mirror::Class> java_lang_ref_Reference(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 713 | AllocClass(self, java_lang_Class.Get(), mirror::Reference::ClassSize(image_pointer_size_)))); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 714 | java_lang_ref_Reference->SetObjectSize(mirror::Reference::InstanceSize()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 715 | mirror::Class::SetStatus(java_lang_ref_Reference, ClassStatus::kResolved, self); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 716 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 717 | // 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] | 718 | class_roots_ = GcRoot<mirror::ObjectArray<mirror::Class>>( |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 719 | mirror::ObjectArray<mirror::Class>::Alloc(self, |
| 720 | object_array_class.Get(), |
| 721 | static_cast<int32_t>(ClassRoot::kMax))); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 722 | CHECK(!class_roots_.IsNull()); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 723 | SetClassRoot(ClassRoot::kJavaLangClass, java_lang_Class.Get()); |
| 724 | SetClassRoot(ClassRoot::kJavaLangObject, java_lang_Object.Get()); |
| 725 | SetClassRoot(ClassRoot::kClassArrayClass, class_array_class.Get()); |
| 726 | SetClassRoot(ClassRoot::kObjectArrayClass, object_array_class.Get()); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 727 | SetClassRoot(ClassRoot::kJavaLangString, java_lang_String.Get()); |
| 728 | SetClassRoot(ClassRoot::kJavaLangRefReference, java_lang_ref_Reference.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 729 | |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 730 | // 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] | 731 | java_lang_Object->SetIfTable(AllocIfTable(self, 0, object_array_class.Get())); |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 732 | |
Vladimir Marko | 0261055 | 2018-06-04 14:38:00 +0100 | [diff] [blame] | 733 | // Create array interface entries to populate once we can load system classes. |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 734 | object_array_class->SetIfTable(AllocIfTable(self, 2, object_array_class.Get())); |
Vladimir Marko | 0261055 | 2018-06-04 14:38:00 +0100 | [diff] [blame] | 735 | DCHECK_EQ(GetArrayIfTable(), object_array_class->GetIfTable()); |
| 736 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 737 | // Setup the primitive type classes. |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 738 | CreatePrimitiveClass(self, Primitive::kPrimBoolean, ClassRoot::kPrimitiveBoolean); |
| 739 | CreatePrimitiveClass(self, Primitive::kPrimByte, ClassRoot::kPrimitiveByte); |
| 740 | CreatePrimitiveClass(self, Primitive::kPrimChar, ClassRoot::kPrimitiveChar); |
| 741 | CreatePrimitiveClass(self, Primitive::kPrimShort, ClassRoot::kPrimitiveShort); |
| 742 | CreatePrimitiveClass(self, Primitive::kPrimInt, ClassRoot::kPrimitiveInt); |
| 743 | CreatePrimitiveClass(self, Primitive::kPrimLong, ClassRoot::kPrimitiveLong); |
| 744 | CreatePrimitiveClass(self, Primitive::kPrimFloat, ClassRoot::kPrimitiveFloat); |
| 745 | CreatePrimitiveClass(self, Primitive::kPrimDouble, ClassRoot::kPrimitiveDouble); |
| 746 | CreatePrimitiveClass(self, Primitive::kPrimVoid, ClassRoot::kPrimitiveVoid); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 747 | |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 748 | // Allocate the primitive array classes. We need only the native pointer |
| 749 | // array at this point (int[] or long[], depending on architecture) but |
| 750 | // we shall perform the same setup steps for all primitive array classes. |
| 751 | AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveBoolean, ClassRoot::kBooleanArrayClass); |
| 752 | AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveByte, ClassRoot::kByteArrayClass); |
| 753 | AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveChar, ClassRoot::kCharArrayClass); |
| 754 | AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveShort, ClassRoot::kShortArrayClass); |
| 755 | AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveInt, ClassRoot::kIntArrayClass); |
| 756 | AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveLong, ClassRoot::kLongArrayClass); |
| 757 | AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveFloat, ClassRoot::kFloatArrayClass); |
| 758 | AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveDouble, ClassRoot::kDoubleArrayClass); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 759 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 760 | // now that these are registered, we can use AllocClass() and AllocObjectArray |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 761 | |
Ian Rogers | 52813c9 | 2012-10-11 11:50:38 -0700 | [diff] [blame] | 762 | // Set up DexCache. This cannot be done later since AppendToBootClassPath calls AllocDexCache. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 763 | Handle<mirror::Class> java_lang_DexCache(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 764 | AllocClass(self, java_lang_Class.Get(), mirror::DexCache::ClassSize(image_pointer_size_)))); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 765 | SetClassRoot(ClassRoot::kJavaLangDexCache, java_lang_DexCache.Get()); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 766 | java_lang_DexCache->SetDexCacheClass(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 767 | java_lang_DexCache->SetObjectSize(mirror::DexCache::InstanceSize()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 768 | mirror::Class::SetStatus(java_lang_DexCache, ClassStatus::kResolved, self); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 769 | |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 770 | |
| 771 | // Setup dalvik.system.ClassExt |
| 772 | Handle<mirror::Class> dalvik_system_ClassExt(hs.NewHandle( |
| 773 | AllocClass(self, java_lang_Class.Get(), mirror::ClassExt::ClassSize(image_pointer_size_)))); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 774 | SetClassRoot(ClassRoot::kDalvikSystemClassExt, dalvik_system_ClassExt.Get()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 775 | mirror::Class::SetStatus(dalvik_system_ClassExt, ClassStatus::kResolved, self); |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 776 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 777 | // Set up array classes for string, field, method |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 778 | Handle<mirror::Class> object_array_string(hs.NewHandle( |
| 779 | AllocClass(self, java_lang_Class.Get(), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 780 | mirror::ObjectArray<mirror::String>::ClassSize(image_pointer_size_)))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 781 | object_array_string->SetComponentType(java_lang_String.Get()); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 782 | SetClassRoot(ClassRoot::kJavaLangStringArrayClass, object_array_string.Get()); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 783 | |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 784 | LinearAlloc* linear_alloc = runtime->GetLinearAlloc(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 785 | // Create runtime resolution and imt conflict methods. |
| 786 | runtime->SetResolutionMethod(runtime->CreateResolutionMethod()); |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 787 | runtime->SetImtConflictMethod(runtime->CreateImtConflictMethod(linear_alloc)); |
| 788 | runtime->SetImtUnimplementedMethod(runtime->CreateImtConflictMethod(linear_alloc)); |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 789 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 790 | // Setup boot_class_path_ and register class_path now that we can use AllocObjectArray to create |
| 791 | // DexCache instances. Needs to be after String, Field, Method arrays since AllocDexCache uses |
| 792 | // these roots. |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 793 | if (boot_class_path.empty()) { |
| 794 | *error_msg = "Boot classpath is empty."; |
| 795 | return false; |
| 796 | } |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 797 | for (auto& dex_file : boot_class_path) { |
Mathieu Chartier | 0a19e21 | 2019-11-27 14:35:24 -0800 | [diff] [blame] | 798 | if (dex_file == nullptr) { |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 799 | *error_msg = "Null dex file."; |
| 800 | return false; |
| 801 | } |
Mathieu Chartier | 0a19e21 | 2019-11-27 14:35:24 -0800 | [diff] [blame] | 802 | AppendToBootClassPath(self, dex_file.get()); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 803 | boot_dex_files_.push_back(std::move(dex_file)); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 804 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 805 | |
| 806 | // now we can use FindSystemClass |
| 807 | |
Dmitry Petrochenko | f0972a4 | 2014-05-16 17:43:39 +0700 | [diff] [blame] | 808 | // Set up GenericJNI entrypoint. That is mainly a hack for common_compiler_test.h so that |
| 809 | // we do not need friend classes or a publicly exposed setter. |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 810 | quick_generic_jni_trampoline_ = GetQuickGenericJniStub(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 811 | if (!runtime->IsAotCompiler()) { |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 812 | // 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] | 813 | jni_dlsym_lookup_trampoline_ = GetJniDlsymLookupStub(); |
Vladimir Marko | fa458ac | 2020-02-12 14:08:07 +0000 | [diff] [blame] | 814 | jni_dlsym_lookup_critical_trampoline_ = GetJniDlsymLookupCriticalStub(); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 815 | quick_resolution_trampoline_ = GetQuickResolutionStub(); |
| 816 | quick_imt_conflict_trampoline_ = GetQuickImtConflictStub(); |
Vladimir Marko | 7dac864 | 2019-11-06 17:09:30 +0000 | [diff] [blame] | 817 | quick_generic_jni_trampoline_ = GetQuickGenericJniStub(); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 818 | quick_to_interpreter_bridge_trampoline_ = GetQuickToInterpreterBridge(); |
Nicolas Geoffray | c39af94 | 2021-01-25 08:43:57 +0000 | [diff] [blame] | 819 | nterp_trampoline_ = interpreter::GetNterpEntryPoint(); |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 820 | } |
Dmitry Petrochenko | f0972a4 | 2014-05-16 17:43:39 +0700 | [diff] [blame] | 821 | |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 822 | // 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] | 823 | mirror::Class::SetStatus(java_lang_Object, ClassStatus::kNotReady, self); |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 824 | CheckSystemClass(self, java_lang_Object, "Ljava/lang/Object;"); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 825 | CHECK_EQ(java_lang_Object->GetObjectSize(), mirror::Object::InstanceSize()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 826 | mirror::Class::SetStatus(java_lang_String, ClassStatus::kNotReady, self); |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 827 | CheckSystemClass(self, java_lang_String, "Ljava/lang/String;"); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 828 | mirror::Class::SetStatus(java_lang_DexCache, ClassStatus::kNotReady, self); |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 829 | CheckSystemClass(self, java_lang_DexCache, "Ljava/lang/DexCache;"); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 830 | CHECK_EQ(java_lang_DexCache->GetObjectSize(), mirror::DexCache::InstanceSize()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 831 | mirror::Class::SetStatus(dalvik_system_ClassExt, ClassStatus::kNotReady, self); |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 832 | CheckSystemClass(self, dalvik_system_ClassExt, "Ldalvik/system/ClassExt;"); |
| 833 | CHECK_EQ(dalvik_system_ClassExt->GetObjectSize(), mirror::ClassExt::InstanceSize()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 834 | |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 835 | // Run Class through FindSystemClass. This initializes the dex_cache_ fields and register it |
| 836 | // in class_table_. |
| 837 | CheckSystemClass(self, java_lang_Class, "Ljava/lang/Class;"); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 838 | |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 839 | // Setup core array classes, i.e. Object[], String[] and Class[] and primitive |
| 840 | // arrays - can't be done until Object has a vtable and component classes are loaded. |
| 841 | FinishCoreArrayClassSetup(ClassRoot::kObjectArrayClass); |
| 842 | FinishCoreArrayClassSetup(ClassRoot::kClassArrayClass); |
| 843 | FinishCoreArrayClassSetup(ClassRoot::kJavaLangStringArrayClass); |
| 844 | FinishCoreArrayClassSetup(ClassRoot::kBooleanArrayClass); |
| 845 | FinishCoreArrayClassSetup(ClassRoot::kByteArrayClass); |
| 846 | FinishCoreArrayClassSetup(ClassRoot::kCharArrayClass); |
| 847 | FinishCoreArrayClassSetup(ClassRoot::kShortArrayClass); |
| 848 | FinishCoreArrayClassSetup(ClassRoot::kIntArrayClass); |
| 849 | FinishCoreArrayClassSetup(ClassRoot::kLongArrayClass); |
| 850 | FinishCoreArrayClassSetup(ClassRoot::kFloatArrayClass); |
| 851 | FinishCoreArrayClassSetup(ClassRoot::kDoubleArrayClass); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 852 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 853 | // Setup the single, global copy of "iftable". |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 854 | auto java_lang_Cloneable = hs.NewHandle(FindSystemClass(self, "Ljava/lang/Cloneable;")); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 855 | CHECK(java_lang_Cloneable != nullptr); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 856 | auto java_io_Serializable = hs.NewHandle(FindSystemClass(self, "Ljava/io/Serializable;")); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 857 | CHECK(java_io_Serializable != nullptr); |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 858 | // We assume that Cloneable/Serializable don't have superinterfaces -- normally we'd have to |
| 859 | // crawl up and explicitly list all of the supers as well. |
Vladimir Marko | 0261055 | 2018-06-04 14:38:00 +0100 | [diff] [blame] | 860 | object_array_class->GetIfTable()->SetInterface(0, java_lang_Cloneable.Get()); |
| 861 | object_array_class->GetIfTable()->SetInterface(1, java_io_Serializable.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 862 | |
Vladimir Marko | b10668c | 2021-06-10 09:52:53 +0100 | [diff] [blame] | 863 | // Check Class[] and Object[]'s interfaces. |
| 864 | CHECK_EQ(java_lang_Cloneable.Get(), class_array_class->GetDirectInterface(0)); |
| 865 | CHECK_EQ(java_io_Serializable.Get(), class_array_class->GetDirectInterface(1)); |
| 866 | CHECK_EQ(java_lang_Cloneable.Get(), object_array_class->GetDirectInterface(0)); |
| 867 | CHECK_EQ(java_io_Serializable.Get(), object_array_class->GetDirectInterface(1)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 868 | |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 869 | CHECK_EQ(object_array_string.Get(), |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 870 | FindSystemClass(self, GetClassRootDescriptor(ClassRoot::kJavaLangStringArrayClass))); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 871 | |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 872 | // 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] | 873 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 874 | // Create java.lang.reflect.Proxy root. |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 875 | SetClassRoot(ClassRoot::kJavaLangReflectProxy, |
| 876 | FindSystemClass(self, "Ljava/lang/reflect/Proxy;")); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 877 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 878 | // Create java.lang.reflect.Field.class root. |
Vladimir Marko | acb906d | 2018-05-30 10:23:49 +0100 | [diff] [blame] | 879 | ObjPtr<mirror::Class> class_root = FindSystemClass(self, "Ljava/lang/reflect/Field;"); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 880 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 881 | SetClassRoot(ClassRoot::kJavaLangReflectField, class_root); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 882 | |
| 883 | // Create java.lang.reflect.Field array root. |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 884 | class_root = FindSystemClass(self, "[Ljava/lang/reflect/Field;"); |
| 885 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 886 | SetClassRoot(ClassRoot::kJavaLangReflectFieldArrayClass, class_root); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 887 | |
| 888 | // Create java.lang.reflect.Constructor.class root and array root. |
| 889 | class_root = FindSystemClass(self, "Ljava/lang/reflect/Constructor;"); |
| 890 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 891 | SetClassRoot(ClassRoot::kJavaLangReflectConstructor, class_root); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 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::kJavaLangReflectConstructorArrayClass, class_root); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 895 | |
| 896 | // Create java.lang.reflect.Method.class root and array root. |
| 897 | class_root = FindSystemClass(self, "Ljava/lang/reflect/Method;"); |
| 898 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 899 | SetClassRoot(ClassRoot::kJavaLangReflectMethod, class_root); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 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::kJavaLangReflectMethodArrayClass, class_root); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 903 | |
Orion Hodson | 005ac51 | 2017-10-24 15:43:43 +0100 | [diff] [blame] | 904 | // Create java.lang.invoke.CallSite.class root |
| 905 | class_root = FindSystemClass(self, "Ljava/lang/invoke/CallSite;"); |
| 906 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 907 | SetClassRoot(ClassRoot::kJavaLangInvokeCallSite, class_root); |
Orion Hodson | 005ac51 | 2017-10-24 15:43:43 +0100 | [diff] [blame] | 908 | |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 909 | // Create java.lang.invoke.MethodType.class root |
| 910 | class_root = FindSystemClass(self, "Ljava/lang/invoke/MethodType;"); |
| 911 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 912 | SetClassRoot(ClassRoot::kJavaLangInvokeMethodType, class_root); |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 913 | |
| 914 | // Create java.lang.invoke.MethodHandleImpl.class root |
| 915 | class_root = FindSystemClass(self, "Ljava/lang/invoke/MethodHandleImpl;"); |
| 916 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 917 | SetClassRoot(ClassRoot::kJavaLangInvokeMethodHandleImpl, class_root); |
Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 918 | SetClassRoot(ClassRoot::kJavaLangInvokeMethodHandle, class_root->GetSuperClass()); |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 919 | |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 920 | // Create java.lang.invoke.MethodHandles.Lookup.class root |
| 921 | class_root = FindSystemClass(self, "Ljava/lang/invoke/MethodHandles$Lookup;"); |
| 922 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 923 | SetClassRoot(ClassRoot::kJavaLangInvokeMethodHandlesLookup, class_root); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 924 | |
Orion Hodson | 005ac51 | 2017-10-24 15:43:43 +0100 | [diff] [blame] | 925 | // Create java.lang.invoke.VarHandle.class root |
| 926 | class_root = FindSystemClass(self, "Ljava/lang/invoke/VarHandle;"); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 927 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 928 | SetClassRoot(ClassRoot::kJavaLangInvokeVarHandle, class_root); |
Orion Hodson | 005ac51 | 2017-10-24 15:43:43 +0100 | [diff] [blame] | 929 | |
| 930 | // Create java.lang.invoke.FieldVarHandle.class root |
| 931 | class_root = FindSystemClass(self, "Ljava/lang/invoke/FieldVarHandle;"); |
| 932 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 933 | SetClassRoot(ClassRoot::kJavaLangInvokeFieldVarHandle, class_root); |
Orion Hodson | 005ac51 | 2017-10-24 15:43:43 +0100 | [diff] [blame] | 934 | |
Orion Hodson | dd41196 | 2021-06-25 08:55:22 +0100 | [diff] [blame] | 935 | // Create java.lang.invoke.StaticFieldVarHandle.class root |
| 936 | class_root = FindSystemClass(self, "Ljava/lang/invoke/StaticFieldVarHandle;"); |
| 937 | CHECK(class_root != nullptr); |
| 938 | SetClassRoot(ClassRoot::kJavaLangInvokeStaticFieldVarHandle, class_root); |
| 939 | |
Orion Hodson | 005ac51 | 2017-10-24 15:43:43 +0100 | [diff] [blame] | 940 | // Create java.lang.invoke.ArrayElementVarHandle.class root |
| 941 | class_root = FindSystemClass(self, "Ljava/lang/invoke/ArrayElementVarHandle;"); |
| 942 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 943 | SetClassRoot(ClassRoot::kJavaLangInvokeArrayElementVarHandle, class_root); |
Orion Hodson | 005ac51 | 2017-10-24 15:43:43 +0100 | [diff] [blame] | 944 | |
| 945 | // Create java.lang.invoke.ByteArrayViewVarHandle.class root |
| 946 | class_root = FindSystemClass(self, "Ljava/lang/invoke/ByteArrayViewVarHandle;"); |
| 947 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 948 | SetClassRoot(ClassRoot::kJavaLangInvokeByteArrayViewVarHandle, class_root); |
Orion Hodson | 005ac51 | 2017-10-24 15:43:43 +0100 | [diff] [blame] | 949 | |
| 950 | // Create java.lang.invoke.ByteBufferViewVarHandle.class root |
| 951 | class_root = FindSystemClass(self, "Ljava/lang/invoke/ByteBufferViewVarHandle;"); |
| 952 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 953 | SetClassRoot(ClassRoot::kJavaLangInvokeByteBufferViewVarHandle, class_root); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 954 | |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 955 | class_root = FindSystemClass(self, "Ldalvik/system/EmulatedStackFrame;"); |
| 956 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 957 | SetClassRoot(ClassRoot::kDalvikSystemEmulatedStackFrame, class_root); |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 958 | |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 959 | // 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] | 960 | // finish initializing Reference class |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 961 | mirror::Class::SetStatus(java_lang_ref_Reference, ClassStatus::kNotReady, self); |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 962 | CheckSystemClass(self, java_lang_ref_Reference, "Ljava/lang/ref/Reference;"); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 963 | CHECK_EQ(java_lang_ref_Reference->GetObjectSize(), mirror::Reference::InstanceSize()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 964 | CHECK_EQ(java_lang_ref_Reference->GetClassSize(), |
| 965 | mirror::Reference::ClassSize(image_pointer_size_)); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 966 | class_root = FindSystemClass(self, "Ljava/lang/ref/FinalizerReference;"); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 967 | CHECK_EQ(class_root->GetClassFlags(), mirror::kClassFlagNormal); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 968 | class_root->SetClassFlags(class_root->GetClassFlags() | mirror::kClassFlagFinalizerReference); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 969 | class_root = FindSystemClass(self, "Ljava/lang/ref/PhantomReference;"); |
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::kClassFlagPhantomReference); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 972 | class_root = FindSystemClass(self, "Ljava/lang/ref/SoftReference;"); |
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::kClassFlagSoftReference); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 975 | class_root = FindSystemClass(self, "Ljava/lang/ref/WeakReference;"); |
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::kClassFlagWeakReference); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 978 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 979 | // Setup the ClassLoader, verifying the object_size_. |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 980 | class_root = FindSystemClass(self, "Ljava/lang/ClassLoader;"); |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 981 | class_root->SetClassLoaderClass(); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 982 | CHECK_EQ(class_root->GetObjectSize(), mirror::ClassLoader::InstanceSize()); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 983 | SetClassRoot(ClassRoot::kJavaLangClassLoader, class_root); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 984 | |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 985 | // Set up java.lang.Throwable, java.lang.ClassNotFoundException, and |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 986 | // java.lang.StackTraceElement as a convenience. |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 987 | SetClassRoot(ClassRoot::kJavaLangThrowable, FindSystemClass(self, "Ljava/lang/Throwable;")); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 988 | SetClassRoot(ClassRoot::kJavaLangClassNotFoundException, |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 989 | FindSystemClass(self, "Ljava/lang/ClassNotFoundException;")); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 990 | SetClassRoot(ClassRoot::kJavaLangStackTraceElement, |
| 991 | FindSystemClass(self, "Ljava/lang/StackTraceElement;")); |
| 992 | SetClassRoot(ClassRoot::kJavaLangStackTraceElementArrayClass, |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 993 | FindSystemClass(self, "[Ljava/lang/StackTraceElement;")); |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 994 | SetClassRoot(ClassRoot::kJavaLangClassLoaderArrayClass, |
| 995 | FindSystemClass(self, "[Ljava/lang/ClassLoader;")); |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 996 | |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 997 | // Create conflict tables that depend on the class linker. |
| 998 | runtime->FixupConflictTables(); |
| 999 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1000 | FinishInit(self); |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 1001 | |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 1002 | VLOG(startup) << "ClassLinker::InitFromCompiler exiting"; |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 1003 | |
| 1004 | return true; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1005 | } |
| 1006 | |
Andreas Gampe | 9abc31e | 2018-05-17 11:47:09 -0700 | [diff] [blame] | 1007 | static void CreateStringInitBindings(Thread* self, ClassLinker* class_linker) |
| 1008 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1009 | // Find String.<init> -> StringFactory bindings. |
| 1010 | ObjPtr<mirror::Class> string_factory_class = |
| 1011 | class_linker->FindSystemClass(self, "Ljava/lang/StringFactory;"); |
| 1012 | CHECK(string_factory_class != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 1013 | ObjPtr<mirror::Class> string_class = GetClassRoot<mirror::String>(class_linker); |
Andreas Gampe | 9abc31e | 2018-05-17 11:47:09 -0700 | [diff] [blame] | 1014 | WellKnownClasses::InitStringInit(string_class, string_factory_class); |
| 1015 | // Update the primordial thread. |
| 1016 | self->InitStringEntryPoints(); |
| 1017 | } |
| 1018 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1019 | void ClassLinker::FinishInit(Thread* self) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1020 | VLOG(startup) << "ClassLinker::FinishInit entering"; |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 1021 | |
Andreas Gampe | 9abc31e | 2018-05-17 11:47:09 -0700 | [diff] [blame] | 1022 | CreateStringInitBindings(self, this); |
| 1023 | |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 1024 | // Let the heap know some key offsets into java.lang.ref instances |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 1025 | // Note: we hard code the field indexes here rather than using FindInstanceField |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 1026 | // as the types of the field can't be resolved prior to the runtime being |
| 1027 | // fully initialized |
Andreas Gampe | 7b2450e | 2018-06-19 10:45:54 -0700 | [diff] [blame] | 1028 | StackHandleScope<3> hs(self); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 1029 | Handle<mirror::Class> java_lang_ref_Reference = |
| 1030 | hs.NewHandle(GetClassRoot<mirror::Reference>(this)); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1031 | Handle<mirror::Class> java_lang_ref_FinalizerReference = |
| 1032 | hs.NewHandle(FindSystemClass(self, "Ljava/lang/ref/FinalizerReference;")); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1033 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1034 | ArtField* pendingNext = java_lang_ref_Reference->GetInstanceField(0); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1035 | CHECK_STREQ(pendingNext->GetName(), "pendingNext"); |
| 1036 | CHECK_STREQ(pendingNext->GetTypeDescriptor(), "Ljava/lang/ref/Reference;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 1037 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1038 | ArtField* queue = java_lang_ref_Reference->GetInstanceField(1); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1039 | CHECK_STREQ(queue->GetName(), "queue"); |
| 1040 | CHECK_STREQ(queue->GetTypeDescriptor(), "Ljava/lang/ref/ReferenceQueue;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 1041 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1042 | ArtField* queueNext = java_lang_ref_Reference->GetInstanceField(2); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1043 | CHECK_STREQ(queueNext->GetName(), "queueNext"); |
| 1044 | CHECK_STREQ(queueNext->GetTypeDescriptor(), "Ljava/lang/ref/Reference;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 1045 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1046 | ArtField* referent = java_lang_ref_Reference->GetInstanceField(3); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1047 | CHECK_STREQ(referent->GetName(), "referent"); |
| 1048 | CHECK_STREQ(referent->GetTypeDescriptor(), "Ljava/lang/Object;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 1049 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1050 | ArtField* zombie = java_lang_ref_FinalizerReference->GetInstanceField(2); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1051 | CHECK_STREQ(zombie->GetName(), "zombie"); |
| 1052 | CHECK_STREQ(zombie->GetTypeDescriptor(), "Ljava/lang/Object;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 1053 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1054 | // ensure all class_roots_ are initialized |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 1055 | 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] | 1056 | ClassRoot class_root = static_cast<ClassRoot>(i); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1057 | ObjPtr<mirror::Class> klass = GetClassRoot(class_root); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1058 | CHECK(klass != nullptr); |
| 1059 | DCHECK(klass->IsArrayClass() || klass->IsPrimitive() || klass->GetDexCache() != nullptr); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1060 | // note SetClassRoot does additional validation. |
| 1061 | // if possible add new checks there to catch errors early |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 1062 | } |
| 1063 | |
Vladimir Marko | 0261055 | 2018-06-04 14:38:00 +0100 | [diff] [blame] | 1064 | CHECK(GetArrayIfTable() != nullptr); |
Elliott Hughes | 92f14b2 | 2011-10-06 12:29:54 -0700 | [diff] [blame] | 1065 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 1066 | // disable the slow paths in FindClass and CreatePrimitiveClass now |
| 1067 | // that Object, Class, and Object[] are setup |
| 1068 | init_done_ = true; |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 1069 | |
Andreas Gampe | 7b2450e | 2018-06-19 10:45:54 -0700 | [diff] [blame] | 1070 | // Under sanitization, the small carve-out to handle stack overflow might not be enough to |
| 1071 | // initialize the StackOverflowError class (as it might require running the verifier). Instead, |
| 1072 | // ensure that the class will be initialized. |
| 1073 | if (kMemoryToolIsAvailable && !Runtime::Current()->IsAotCompiler()) { |
Andreas Gampe | e0bbab9 | 2019-07-25 12:28:22 -0700 | [diff] [blame] | 1074 | verifier::ClassVerifier::Init(this); // Need to prepare the verifier. |
Andreas Gampe | 7b2450e | 2018-06-19 10:45:54 -0700 | [diff] [blame] | 1075 | |
| 1076 | ObjPtr<mirror::Class> soe_klass = FindSystemClass(self, "Ljava/lang/StackOverflowError;"); |
| 1077 | if (soe_klass == nullptr || !EnsureInitialized(self, hs.NewHandle(soe_klass), true, true)) { |
| 1078 | // Strange, but don't crash. |
| 1079 | LOG(WARNING) << "Could not prepare StackOverflowError."; |
| 1080 | self->ClearException(); |
| 1081 | } |
| 1082 | } |
| 1083 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1084 | VLOG(startup) << "ClassLinker::FinishInit exiting"; |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 1085 | } |
| 1086 | |
Vladimir Marko | dcfcce4 | 2018-06-27 10:00:28 +0000 | [diff] [blame] | 1087 | void ClassLinker::RunRootClinits(Thread* self) { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 1088 | for (size_t i = 0; i < static_cast<size_t>(ClassRoot::kMax); ++i) { |
| 1089 | ObjPtr<mirror::Class> c = GetClassRoot(ClassRoot(i), this); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 1090 | if (!c->IsArrayClass() && !c->IsPrimitive()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1091 | StackHandleScope<1> hs(self); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 1092 | Handle<mirror::Class> h_class(hs.NewHandle(c)); |
David Srbecky | 08110ef | 2020-05-20 19:33:43 +0100 | [diff] [blame] | 1093 | if (!EnsureInitialized(self, h_class, true, true)) { |
| 1094 | LOG(FATAL) << "Exception when initializing " << h_class->PrettyClass() |
| 1095 | << ": " << self->GetException()->Dump(); |
| 1096 | } |
Vladimir Marko | dcfcce4 | 2018-06-27 10:00:28 +0000 | [diff] [blame] | 1097 | } else { |
| 1098 | DCHECK(c->IsInitialized()); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 1099 | } |
| 1100 | } |
| 1101 | } |
| 1102 | |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 1103 | ALWAYS_INLINE |
| 1104 | static uint32_t ComputeMethodHash(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1105 | DCHECK(!method->IsRuntimeMethod()); |
| 1106 | DCHECK(!method->IsProxyMethod()); |
| 1107 | DCHECK(!method->IsObsolete()); |
| 1108 | // Do not use `ArtMethod::GetNameView()` to avoid unnecessary runtime/proxy/obsolete method |
| 1109 | // checks. It is safe to avoid the read barrier here, see `ArtMethod::GetDexFile()`. |
| 1110 | const DexFile& dex_file = method->GetDeclaringClass<kWithoutReadBarrier>()->GetDexFile(); |
| 1111 | const dex::MethodId& method_id = dex_file.GetMethodId(method->GetDexMethodIndex()); |
| 1112 | std::string_view name = dex_file.GetMethodNameView(method_id); |
| 1113 | return ComputeModifiedUtf8Hash(name); |
| 1114 | } |
| 1115 | |
Vladimir Marko | bed84ef | 2022-01-21 13:57:14 +0000 | [diff] [blame] | 1116 | ALWAYS_INLINE |
| 1117 | static bool MethodSignatureEquals(ArtMethod* lhs, ArtMethod* rhs) |
| 1118 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1119 | DCHECK(!lhs->IsRuntimeMethod()); |
| 1120 | DCHECK(!lhs->IsProxyMethod()); |
| 1121 | DCHECK(!lhs->IsObsolete()); |
| 1122 | DCHECK(!rhs->IsRuntimeMethod()); |
| 1123 | DCHECK(!rhs->IsProxyMethod()); |
| 1124 | DCHECK(!rhs->IsObsolete()); |
| 1125 | // Do not use `ArtMethod::GetDexFile()` to avoid unnecessary obsolete method checks. |
| 1126 | // It is safe to avoid the read barrier here, see `ArtMethod::GetDexFile()`. |
| 1127 | const DexFile& lhs_dex_file = lhs->GetDeclaringClass<kWithoutReadBarrier>()->GetDexFile(); |
| 1128 | const DexFile& rhs_dex_file = rhs->GetDeclaringClass<kWithoutReadBarrier>()->GetDexFile(); |
| 1129 | const dex::MethodId& lhs_mid = lhs_dex_file.GetMethodId(lhs->GetDexMethodIndex()); |
| 1130 | const dex::MethodId& rhs_mid = rhs_dex_file.GetMethodId(rhs->GetDexMethodIndex()); |
| 1131 | if (&lhs_dex_file == &rhs_dex_file) { |
| 1132 | return lhs_mid.name_idx_ == rhs_mid.name_idx_ && |
| 1133 | lhs_mid.proto_idx_ == rhs_mid.proto_idx_; |
| 1134 | } else { |
| 1135 | return |
| 1136 | lhs_dex_file.GetMethodNameView(lhs_mid) == rhs_dex_file.GetMethodNameView(rhs_mid) && |
| 1137 | lhs_dex_file.GetMethodSignature(lhs_mid) == rhs_dex_file.GetMethodSignature(rhs_mid); |
| 1138 | } |
| 1139 | } |
| 1140 | |
Vladimir Marko | 4335474 | 2021-02-03 15:37:01 +0000 | [diff] [blame] | 1141 | static void InitializeObjectVirtualMethodHashes(ObjPtr<mirror::Class> java_lang_Object, |
| 1142 | PointerSize pointer_size, |
| 1143 | /*out*/ ArrayRef<uint32_t> virtual_method_hashes) |
| 1144 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1145 | ArraySlice<ArtMethod> virtual_methods = java_lang_Object->GetVirtualMethods(pointer_size); |
| 1146 | DCHECK_EQ(virtual_method_hashes.size(), virtual_methods.size()); |
| 1147 | for (size_t i = 0; i != virtual_method_hashes.size(); ++i) { |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 1148 | virtual_method_hashes[i] = ComputeMethodHash(&virtual_methods[i]); |
Vladimir Marko | 4335474 | 2021-02-03 15:37:01 +0000 | [diff] [blame] | 1149 | } |
| 1150 | } |
| 1151 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1152 | struct TrampolineCheckData { |
| 1153 | const void* quick_resolution_trampoline; |
| 1154 | const void* quick_imt_conflict_trampoline; |
| 1155 | const void* quick_generic_jni_trampoline; |
| 1156 | const void* quick_to_interpreter_bridge_trampoline; |
Nicolas Geoffray | c39af94 | 2021-01-25 08:43:57 +0000 | [diff] [blame] | 1157 | const void* nterp_trampoline; |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1158 | PointerSize pointer_size; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1159 | ArtMethod* m; |
| 1160 | bool error; |
| 1161 | }; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1162 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1163 | bool ClassLinker::InitFromBootImage(std::string* error_msg) { |
| 1164 | VLOG(startup) << __FUNCTION__ << " entering"; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1165 | CHECK(!init_done_); |
| 1166 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 1167 | Runtime* const runtime = Runtime::Current(); |
| 1168 | Thread* const self = Thread::Current(); |
| 1169 | gc::Heap* const heap = runtime->GetHeap(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1170 | std::vector<gc::space::ImageSpace*> spaces = heap->GetBootImageSpaces(); |
| 1171 | CHECK(!spaces.empty()); |
Vladimir Marko | 024d69f | 2019-06-13 10:52:32 +0100 | [diff] [blame] | 1172 | const ImageHeader& image_header = spaces[0]->GetImageHeader(); |
| 1173 | uint32_t pointer_size_unchecked = image_header.GetPointerSizeUnchecked(); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1174 | if (!ValidPointerSize(pointer_size_unchecked)) { |
| 1175 | *error_msg = StringPrintf("Invalid image pointer size: %u", pointer_size_unchecked); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1176 | return false; |
| 1177 | } |
Vladimir Marko | 3364d18 | 2019-03-13 13:55:01 +0000 | [diff] [blame] | 1178 | image_pointer_size_ = image_header.GetPointerSize(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1179 | if (!runtime->IsAotCompiler()) { |
| 1180 | // Only the Aot compiler supports having an image with a different pointer size than the |
| 1181 | // runtime. This happens on the host for compiling 32 bit tests since we use a 64 bit libart |
| 1182 | // 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] | 1183 | if (image_pointer_size_ != kRuntimePointerSize) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1184 | *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] | 1185 | static_cast<size_t>(image_pointer_size_), |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1186 | sizeof(void*)); |
| 1187 | return false; |
| 1188 | } |
| 1189 | } |
Vladimir Marko | 3364d18 | 2019-03-13 13:55:01 +0000 | [diff] [blame] | 1190 | DCHECK(!runtime->HasResolutionMethod()); |
| 1191 | runtime->SetResolutionMethod(image_header.GetImageMethod(ImageHeader::kResolutionMethod)); |
| 1192 | runtime->SetImtConflictMethod(image_header.GetImageMethod(ImageHeader::kImtConflictMethod)); |
| 1193 | runtime->SetImtUnimplementedMethod( |
| 1194 | image_header.GetImageMethod(ImageHeader::kImtUnimplementedMethod)); |
| 1195 | runtime->SetCalleeSaveMethod( |
| 1196 | image_header.GetImageMethod(ImageHeader::kSaveAllCalleeSavesMethod), |
| 1197 | CalleeSaveType::kSaveAllCalleeSaves); |
| 1198 | runtime->SetCalleeSaveMethod( |
| 1199 | image_header.GetImageMethod(ImageHeader::kSaveRefsOnlyMethod), |
| 1200 | CalleeSaveType::kSaveRefsOnly); |
| 1201 | runtime->SetCalleeSaveMethod( |
| 1202 | image_header.GetImageMethod(ImageHeader::kSaveRefsAndArgsMethod), |
| 1203 | CalleeSaveType::kSaveRefsAndArgs); |
| 1204 | runtime->SetCalleeSaveMethod( |
| 1205 | image_header.GetImageMethod(ImageHeader::kSaveEverythingMethod), |
| 1206 | CalleeSaveType::kSaveEverything); |
| 1207 | runtime->SetCalleeSaveMethod( |
| 1208 | image_header.GetImageMethod(ImageHeader::kSaveEverythingMethodForClinit), |
| 1209 | CalleeSaveType::kSaveEverythingForClinit); |
| 1210 | runtime->SetCalleeSaveMethod( |
| 1211 | image_header.GetImageMethod(ImageHeader::kSaveEverythingMethodForSuspendCheck), |
| 1212 | CalleeSaveType::kSaveEverythingForSuspendCheck); |
| 1213 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1214 | std::vector<const OatFile*> oat_files = |
| 1215 | runtime->GetOatFileManager().RegisterImageOatFiles(spaces); |
| 1216 | DCHECK(!oat_files.empty()); |
| 1217 | const OatHeader& default_oat_header = oat_files[0]->GetOatHeader(); |
Vladimir Marko | 7dac864 | 2019-11-06 17:09:30 +0000 | [diff] [blame] | 1218 | jni_dlsym_lookup_trampoline_ = default_oat_header.GetJniDlsymLookupTrampoline(); |
Vladimir Marko | fa458ac | 2020-02-12 14:08:07 +0000 | [diff] [blame] | 1219 | jni_dlsym_lookup_critical_trampoline_ = default_oat_header.GetJniDlsymLookupCriticalTrampoline(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1220 | quick_resolution_trampoline_ = default_oat_header.GetQuickResolutionTrampoline(); |
| 1221 | quick_imt_conflict_trampoline_ = default_oat_header.GetQuickImtConflictTrampoline(); |
| 1222 | quick_generic_jni_trampoline_ = default_oat_header.GetQuickGenericJniTrampoline(); |
| 1223 | quick_to_interpreter_bridge_trampoline_ = default_oat_header.GetQuickToInterpreterBridge(); |
Nicolas Geoffray | c39af94 | 2021-01-25 08:43:57 +0000 | [diff] [blame] | 1224 | nterp_trampoline_ = default_oat_header.GetNterpTrampoline(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1225 | if (kIsDebugBuild) { |
| 1226 | // Check that the other images use the same trampoline. |
| 1227 | for (size_t i = 1; i < oat_files.size(); ++i) { |
| 1228 | const OatHeader& ith_oat_header = oat_files[i]->GetOatHeader(); |
Vladimir Marko | 7dac864 | 2019-11-06 17:09:30 +0000 | [diff] [blame] | 1229 | const void* ith_jni_dlsym_lookup_trampoline_ = |
| 1230 | ith_oat_header.GetJniDlsymLookupTrampoline(); |
Vladimir Marko | fa458ac | 2020-02-12 14:08:07 +0000 | [diff] [blame] | 1231 | const void* ith_jni_dlsym_lookup_critical_trampoline_ = |
| 1232 | ith_oat_header.GetJniDlsymLookupCriticalTrampoline(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1233 | const void* ith_quick_resolution_trampoline = |
| 1234 | ith_oat_header.GetQuickResolutionTrampoline(); |
| 1235 | const void* ith_quick_imt_conflict_trampoline = |
| 1236 | ith_oat_header.GetQuickImtConflictTrampoline(); |
| 1237 | const void* ith_quick_generic_jni_trampoline = |
| 1238 | ith_oat_header.GetQuickGenericJniTrampoline(); |
| 1239 | const void* ith_quick_to_interpreter_bridge_trampoline = |
| 1240 | ith_oat_header.GetQuickToInterpreterBridge(); |
Nicolas Geoffray | c39af94 | 2021-01-25 08:43:57 +0000 | [diff] [blame] | 1241 | const void* ith_nterp_trampoline = |
| 1242 | ith_oat_header.GetNterpTrampoline(); |
Vladimir Marko | 7dac864 | 2019-11-06 17:09:30 +0000 | [diff] [blame] | 1243 | if (ith_jni_dlsym_lookup_trampoline_ != jni_dlsym_lookup_trampoline_ || |
Vladimir Marko | fa458ac | 2020-02-12 14:08:07 +0000 | [diff] [blame] | 1244 | ith_jni_dlsym_lookup_critical_trampoline_ != jni_dlsym_lookup_critical_trampoline_ || |
Vladimir Marko | 7dac864 | 2019-11-06 17:09:30 +0000 | [diff] [blame] | 1245 | ith_quick_resolution_trampoline != quick_resolution_trampoline_ || |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1246 | ith_quick_imt_conflict_trampoline != quick_imt_conflict_trampoline_ || |
| 1247 | ith_quick_generic_jni_trampoline != quick_generic_jni_trampoline_ || |
Nicolas Geoffray | c39af94 | 2021-01-25 08:43:57 +0000 | [diff] [blame] | 1248 | ith_quick_to_interpreter_bridge_trampoline != quick_to_interpreter_bridge_trampoline_ || |
| 1249 | ith_nterp_trampoline != nterp_trampoline_) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1250 | // Make sure that all methods in this image do not contain those trampolines as |
| 1251 | // entrypoints. Otherwise the class-linker won't be able to work with a single set. |
| 1252 | TrampolineCheckData data; |
| 1253 | data.error = false; |
| 1254 | data.pointer_size = GetImagePointerSize(); |
| 1255 | data.quick_resolution_trampoline = ith_quick_resolution_trampoline; |
| 1256 | data.quick_imt_conflict_trampoline = ith_quick_imt_conflict_trampoline; |
| 1257 | data.quick_generic_jni_trampoline = ith_quick_generic_jni_trampoline; |
| 1258 | data.quick_to_interpreter_bridge_trampoline = ith_quick_to_interpreter_bridge_trampoline; |
Nicolas Geoffray | c39af94 | 2021-01-25 08:43:57 +0000 | [diff] [blame] | 1259 | data.nterp_trampoline = ith_nterp_trampoline; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1260 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Andreas Gampe | 0c18338 | 2017-07-13 22:26:24 -0700 | [diff] [blame] | 1261 | auto visitor = [&](mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1262 | if (obj->IsClass()) { |
| 1263 | ObjPtr<mirror::Class> klass = obj->AsClass(); |
| 1264 | for (ArtMethod& m : klass->GetMethods(data.pointer_size)) { |
| 1265 | const void* entrypoint = |
| 1266 | m.GetEntryPointFromQuickCompiledCodePtrSize(data.pointer_size); |
| 1267 | if (entrypoint == data.quick_resolution_trampoline || |
| 1268 | entrypoint == data.quick_imt_conflict_trampoline || |
| 1269 | entrypoint == data.quick_generic_jni_trampoline || |
| 1270 | entrypoint == data.quick_to_interpreter_bridge_trampoline) { |
| 1271 | data.m = &m; |
| 1272 | data.error = true; |
| 1273 | return; |
| 1274 | } |
| 1275 | } |
| 1276 | } |
| 1277 | }; |
| 1278 | spaces[i]->GetLiveBitmap()->Walk(visitor); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1279 | if (data.error) { |
| 1280 | ArtMethod* m = data.m; |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1281 | LOG(ERROR) << "Found a broken ArtMethod: " << ArtMethod::PrettyMethod(m); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1282 | *error_msg = "Found an ArtMethod with a bad entrypoint"; |
| 1283 | return false; |
| 1284 | } |
| 1285 | } |
| 1286 | } |
| 1287 | } |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 1288 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1289 | class_roots_ = GcRoot<mirror::ObjectArray<mirror::Class>>( |
Vladimir Marko | d7e9bbf | 2019-03-28 13:18:57 +0000 | [diff] [blame] | 1290 | ObjPtr<mirror::ObjectArray<mirror::Class>>::DownCast( |
Vladimir Marko | 024d69f | 2019-06-13 10:52:32 +0100 | [diff] [blame] | 1291 | image_header.GetImageRoot(ImageHeader::kClassRoots))); |
Vladimir Marko | f75613c | 2018-06-05 12:51:04 +0100 | [diff] [blame] | 1292 | DCHECK_EQ(GetClassRoot<mirror::Class>(this)->GetClassFlags(), mirror::kClassFlagClass); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1293 | |
Vladimir Marko | 024d69f | 2019-06-13 10:52:32 +0100 | [diff] [blame] | 1294 | DCHECK_EQ(GetClassRoot<mirror::Object>(this)->GetObjectSize(), sizeof(mirror::Object)); |
| 1295 | ObjPtr<mirror::ObjectArray<mirror::Object>> boot_image_live_objects = |
| 1296 | ObjPtr<mirror::ObjectArray<mirror::Object>>::DownCast( |
| 1297 | image_header.GetImageRoot(ImageHeader::kBootImageLiveObjects)); |
| 1298 | runtime->SetSentinel(boot_image_live_objects->Get(ImageHeader::kClearedJniWeakSentinel)); |
| 1299 | DCHECK(runtime->GetSentinel().Read()->GetClass() == GetClassRoot<mirror::Object>(this)); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 1300 | |
Vladimir Marko | d190851 | 2018-11-22 14:57:28 +0000 | [diff] [blame] | 1301 | for (size_t i = 0u, size = spaces.size(); i != size; ++i) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1302 | // Boot class loader, use a null handle. |
| 1303 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
Vladimir Marko | d190851 | 2018-11-22 14:57:28 +0000 | [diff] [blame] | 1304 | if (!AddImageSpace(spaces[i], |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1305 | ScopedNullHandle<mirror::ClassLoader>(), |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1306 | /*out*/&dex_files, |
| 1307 | error_msg)) { |
| 1308 | return false; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1309 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1310 | // Append opened dex files at the end. |
| 1311 | boot_dex_files_.insert(boot_dex_files_.end(), |
| 1312 | std::make_move_iterator(dex_files.begin()), |
| 1313 | std::make_move_iterator(dex_files.end())); |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 1314 | } |
Mathieu Chartier | be8303d | 2017-08-17 17:39:39 -0700 | [diff] [blame] | 1315 | for (const std::unique_ptr<const DexFile>& dex_file : boot_dex_files_) { |
Jared Duke | 95bb995 | 2021-08-11 15:07:25 -0700 | [diff] [blame] | 1316 | OatDexFile::MadviseDexFileAtLoad(*dex_file); |
Mathieu Chartier | be8303d | 2017-08-17 17:39:39 -0700 | [diff] [blame] | 1317 | } |
Vladimir Marko | 4335474 | 2021-02-03 15:37:01 +0000 | [diff] [blame] | 1318 | InitializeObjectVirtualMethodHashes(GetClassRoot<mirror::Object>(this), |
| 1319 | image_pointer_size_, |
| 1320 | ArrayRef<uint32_t>(object_virtual_method_hashes_)); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1321 | FinishInit(self); |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 1322 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1323 | VLOG(startup) << __FUNCTION__ << " exiting"; |
| 1324 | return true; |
| 1325 | } |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 1326 | |
Vladimir Marko | 4433c43 | 2018-12-04 14:57:47 +0000 | [diff] [blame] | 1327 | void ClassLinker::AddExtraBootDexFiles( |
| 1328 | Thread* self, |
| 1329 | std::vector<std::unique_ptr<const DexFile>>&& additional_dex_files) { |
| 1330 | for (std::unique_ptr<const DexFile>& dex_file : additional_dex_files) { |
Mathieu Chartier | 0a19e21 | 2019-11-27 14:35:24 -0800 | [diff] [blame] | 1331 | AppendToBootClassPath(self, dex_file.get()); |
Orion Hodson | 771708f | 2021-01-06 15:45:16 +0000 | [diff] [blame] | 1332 | if (kIsDebugBuild) { |
| 1333 | for (const auto& boot_dex_file : boot_dex_files_) { |
| 1334 | DCHECK_NE(boot_dex_file->GetLocation(), dex_file->GetLocation()); |
| 1335 | } |
| 1336 | } |
Vladimir Marko | 4433c43 | 2018-12-04 14:57:47 +0000 | [diff] [blame] | 1337 | boot_dex_files_.push_back(std::move(dex_file)); |
| 1338 | } |
| 1339 | } |
| 1340 | |
Jeff Hao | 5872d7c | 2016-04-27 11:07:41 -0700 | [diff] [blame] | 1341 | bool ClassLinker::IsBootClassLoader(ScopedObjectAccessAlreadyRunnable& soa, |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1342 | ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1343 | return class_loader == nullptr || |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 1344 | soa.Decode<mirror::Class>(WellKnownClasses::java_lang_BootClassLoader) == |
| 1345 | class_loader->GetClass(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1346 | } |
| 1347 | |
Alexey Grebenkin | be4c2bd | 2018-02-01 19:09:59 +0300 | [diff] [blame] | 1348 | class CHAOnDeleteUpdateClassVisitor { |
| 1349 | public: |
| 1350 | explicit CHAOnDeleteUpdateClassVisitor(LinearAlloc* alloc) |
| 1351 | : allocator_(alloc), cha_(Runtime::Current()->GetClassLinker()->GetClassHierarchyAnalysis()), |
| 1352 | pointer_size_(Runtime::Current()->GetClassLinker()->GetImagePointerSize()), |
| 1353 | self_(Thread::Current()) {} |
| 1354 | |
| 1355 | bool operator()(ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1356 | // This class is going to be unloaded. Tell CHA about it. |
| 1357 | cha_->ResetSingleImplementationInHierarchy(klass, allocator_, pointer_size_); |
| 1358 | return true; |
| 1359 | } |
| 1360 | private: |
| 1361 | const LinearAlloc* allocator_; |
| 1362 | const ClassHierarchyAnalysis* cha_; |
| 1363 | const PointerSize pointer_size_; |
| 1364 | const Thread* self_; |
| 1365 | }; |
| 1366 | |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1367 | /* |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1368 | * A class used to ensure that all references to strings interned in an AppImage have been |
| 1369 | * 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] | 1370 | */ |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1371 | class CountInternedStringReferencesVisitor { |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1372 | public: |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1373 | CountInternedStringReferencesVisitor(const gc::space::ImageSpace& space, |
| 1374 | const InternTable::UnorderedSet& image_interns) |
| 1375 | : space_(space), |
| 1376 | image_interns_(image_interns), |
| 1377 | count_(0u) {} |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1378 | |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1379 | void TestObject(ObjPtr<mirror::Object> referred_obj) const |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1380 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1381 | if (referred_obj != nullptr && |
| 1382 | space_.HasAddress(referred_obj.Ptr()) && |
| 1383 | referred_obj->IsString()) { |
| 1384 | ObjPtr<mirror::String> referred_str = referred_obj->AsString(); |
Vladimir Marko | 365c020 | 2022-03-22 09:53:31 +0000 | [diff] [blame] | 1385 | uint32_t hash = static_cast<uint32_t>(referred_str->GetStoredHashCode()); |
| 1386 | // All image strings have the hash code calculated, even if they are not interned. |
| 1387 | DCHECK_EQ(hash, static_cast<uint32_t>(referred_str->ComputeHashCode())); |
| 1388 | auto it = image_interns_.FindWithHash(GcRoot<mirror::String>(referred_str), hash); |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1389 | if (it != image_interns_.end() && it->Read() == referred_str) { |
| 1390 | ++count_; |
Chris Wailes | fbeef46 | 2018-10-19 14:16:35 -0700 | [diff] [blame] | 1391 | } |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1392 | } |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1393 | } |
| 1394 | |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1395 | void VisitRootIfNonNull( |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1396 | mirror::CompressedReference<mirror::Object>* root) const |
| 1397 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1398 | if (!root->IsNull()) { |
| 1399 | VisitRoot(root); |
| 1400 | } |
| 1401 | } |
| 1402 | |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1403 | void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1404 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1405 | TestObject(root->AsMirrorPtr()); |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1406 | } |
| 1407 | |
| 1408 | // Visit Class Fields |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1409 | void operator()(ObjPtr<mirror::Object> obj, |
| 1410 | MemberOffset offset, |
| 1411 | bool is_static ATTRIBUTE_UNUSED) const |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1412 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1413 | // References within image or across images don't need a read barrier. |
| 1414 | ObjPtr<mirror::Object> referred_obj = |
| 1415 | obj->GetFieldObject<mirror::Object, kVerifyNone, kWithoutReadBarrier>(offset); |
| 1416 | TestObject(referred_obj); |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1417 | } |
| 1418 | |
| 1419 | void operator()(ObjPtr<mirror::Class> klass ATTRIBUTE_UNUSED, |
| 1420 | ObjPtr<mirror::Reference> ref) const |
| 1421 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_) { |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1422 | operator()(ref, mirror::Reference::ReferentOffset(), /*is_static=*/ false); |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1423 | } |
| 1424 | |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1425 | size_t GetCount() const { |
| 1426 | return count_; |
| 1427 | } |
| 1428 | |
| 1429 | private: |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1430 | const gc::space::ImageSpace& space_; |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1431 | const InternTable::UnorderedSet& image_interns_; |
| 1432 | mutable size_t count_; // Modified from the `const` callbacks. |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1433 | }; |
| 1434 | |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1435 | /* |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1436 | * This function counts references to strings interned in the AppImage. |
| 1437 | * 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] | 1438 | */ |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1439 | size_t CountInternedStringReferences(gc::space::ImageSpace& space, |
| 1440 | const InternTable::UnorderedSet& image_interns) |
| 1441 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1442 | const gc::accounting::ContinuousSpaceBitmap* bitmap = space.GetMarkBitmap(); |
| 1443 | const ImageHeader& image_header = space.GetImageHeader(); |
| 1444 | const uint8_t* target_base = space.GetMemMap()->Begin(); |
| 1445 | const ImageSection& objects_section = image_header.GetObjectsSection(); |
Chris Wailes | fbeef46 | 2018-10-19 14:16:35 -0700 | [diff] [blame] | 1446 | |
| 1447 | auto objects_begin = reinterpret_cast<uintptr_t>(target_base + objects_section.Offset()); |
| 1448 | auto objects_end = reinterpret_cast<uintptr_t>(target_base + objects_section.End()); |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1449 | |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1450 | CountInternedStringReferencesVisitor visitor(space, image_interns); |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1451 | bitmap->VisitMarkedRange(objects_begin, |
| 1452 | objects_end, |
| 1453 | [&space, &visitor](mirror::Object* obj) |
| 1454 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1455 | if (space.HasAddress(obj)) { |
| 1456 | if (obj->IsDexCache()) { |
Chris Wailes | fbeef46 | 2018-10-19 14:16:35 -0700 | [diff] [blame] | 1457 | obj->VisitReferences</* kVisitNativeRoots= */ true, |
| 1458 | kVerifyNone, |
| 1459 | kWithoutReadBarrier>(visitor, visitor); |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1460 | } else { |
| 1461 | // Don't visit native roots for non-dex-cache as they can't contain |
| 1462 | // native references to strings. This is verified during compilation |
| 1463 | // by ImageWriter::VerifyNativeGCRootInvariants. |
Chris Wailes | fbeef46 | 2018-10-19 14:16:35 -0700 | [diff] [blame] | 1464 | obj->VisitReferences</* kVisitNativeRoots= */ false, |
| 1465 | kVerifyNone, |
| 1466 | kWithoutReadBarrier>(visitor, visitor); |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1467 | } |
| 1468 | } |
| 1469 | }); |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1470 | return visitor.GetCount(); |
| 1471 | } |
| 1472 | |
| 1473 | template <typename Visitor> |
| 1474 | static void VisitInternedStringReferences( |
| 1475 | gc::space::ImageSpace* space, |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1476 | const Visitor& visitor) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1477 | const uint8_t* target_base = space->Begin(); |
| 1478 | const ImageSection& sro_section = |
| 1479 | space->GetImageHeader().GetImageStringReferenceOffsetsSection(); |
| 1480 | const size_t num_string_offsets = sro_section.Size() / sizeof(AppImageReferenceOffsetInfo); |
| 1481 | |
| 1482 | VLOG(image) |
| 1483 | << "ClassLinker:AppImage:InternStrings:imageStringReferenceOffsetCount = " |
| 1484 | << num_string_offsets; |
| 1485 | |
| 1486 | const auto* sro_base = |
| 1487 | reinterpret_cast<const AppImageReferenceOffsetInfo*>(target_base + sro_section.Offset()); |
| 1488 | |
| 1489 | for (size_t offset_index = 0; offset_index < num_string_offsets; ++offset_index) { |
| 1490 | uint32_t base_offset = sro_base[offset_index].first; |
| 1491 | |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 1492 | uint32_t raw_member_offset = sro_base[offset_index].second; |
| 1493 | DCHECK_ALIGNED(base_offset, 2); |
| 1494 | DCHECK_ALIGNED(raw_member_offset, 2); |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1495 | |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 1496 | ObjPtr<mirror::Object> obj_ptr = |
| 1497 | reinterpret_cast<mirror::Object*>(space->Begin() + base_offset); |
| 1498 | MemberOffset member_offset(raw_member_offset); |
| 1499 | ObjPtr<mirror::String> referred_string = |
| 1500 | obj_ptr->GetFieldObject<mirror::String, |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1501 | kVerifyNone, |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 1502 | kWithoutReadBarrier, |
| 1503 | /* kIsVolatile= */ false>(member_offset); |
| 1504 | DCHECK(referred_string != nullptr); |
| 1505 | |
| 1506 | ObjPtr<mirror::String> visited = visitor(referred_string); |
| 1507 | if (visited != referred_string) { |
| 1508 | obj_ptr->SetFieldObject</* kTransactionActive= */ false, |
| 1509 | /* kCheckTransaction= */ false, |
| 1510 | kVerifyNone, |
| 1511 | /* kIsVolatile= */ false>(member_offset, visited); |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1512 | } |
| 1513 | } |
| 1514 | } |
| 1515 | |
| 1516 | static void VerifyInternedStringReferences(gc::space::ImageSpace* space) |
| 1517 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1518 | InternTable::UnorderedSet image_interns; |
| 1519 | const ImageSection& section = space->GetImageHeader().GetInternedStringsSection(); |
| 1520 | if (section.Size() > 0) { |
| 1521 | size_t read_count; |
| 1522 | const uint8_t* data = space->Begin() + section.Offset(); |
| 1523 | InternTable::UnorderedSet image_set(data, /*make_copy_of_data=*/ false, &read_count); |
| 1524 | image_set.swap(image_interns); |
| 1525 | } |
| 1526 | size_t num_recorded_refs = 0u; |
| 1527 | VisitInternedStringReferences( |
| 1528 | space, |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1529 | [&image_interns, &num_recorded_refs](ObjPtr<mirror::String> str) |
| 1530 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1531 | auto it = image_interns.find(GcRoot<mirror::String>(str)); |
| 1532 | CHECK(it != image_interns.end()); |
| 1533 | CHECK(it->Read() == str); |
| 1534 | ++num_recorded_refs; |
| 1535 | return str; |
| 1536 | }); |
| 1537 | size_t num_found_refs = CountInternedStringReferences(*space, image_interns); |
| 1538 | CHECK_EQ(num_recorded_refs, num_found_refs); |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1539 | } |
| 1540 | |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1541 | // new_class_set is the set of classes that were read from the class table section in the image. |
| 1542 | // If there was no class table section, it is null. |
| 1543 | // 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] | 1544 | class AppImageLoadingHelper { |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1545 | public: |
Vladimir Marko | 0f3c700 | 2017-09-07 14:15:56 +0100 | [diff] [blame] | 1546 | static void Update( |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1547 | ClassLinker* class_linker, |
| 1548 | gc::space::ImageSpace* space, |
| 1549 | Handle<mirror::ClassLoader> class_loader, |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 1550 | Handle<mirror::ObjectArray<mirror::DexCache>> dex_caches) |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1551 | REQUIRES(!Locks::dex_lock_) |
| 1552 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 1553 | |
Chris Wailes | fbeef46 | 2018-10-19 14:16:35 -0700 | [diff] [blame] | 1554 | static void HandleAppImageStrings(gc::space::ImageSpace* space) |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 1555 | REQUIRES_SHARED(Locks::mutator_lock_); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1556 | }; |
| 1557 | |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 1558 | void AppImageLoadingHelper::Update( |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1559 | ClassLinker* class_linker, |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1560 | gc::space::ImageSpace* space, |
| 1561 | Handle<mirror::ClassLoader> class_loader, |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 1562 | Handle<mirror::ObjectArray<mirror::DexCache>> dex_caches) |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1563 | REQUIRES(!Locks::dex_lock_) |
| 1564 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Chris Wailes | 2386636 | 2018-08-22 16:16:58 -0700 | [diff] [blame] | 1565 | ScopedTrace app_image_timing("AppImage:Updating"); |
| 1566 | |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1567 | if (kIsDebugBuild && ClassLinker::kAppImageMayContainStrings) { |
| 1568 | // In debug build, verify the string references before applying |
| 1569 | // the Runtime::LoadAppImageStartupCache() option. |
| 1570 | VerifyInternedStringReferences(space); |
| 1571 | } |
| 1572 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1573 | Thread* const self = Thread::Current(); |
Mathieu Chartier | a88abfa | 2019-02-04 11:08:29 -0800 | [diff] [blame] | 1574 | Runtime* const runtime = Runtime::Current(); |
| 1575 | gc::Heap* const heap = runtime->GetHeap(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1576 | const ImageHeader& header = space->GetImageHeader(); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1577 | { |
Vladimir Marko | 0f3c700 | 2017-09-07 14:15:56 +0100 | [diff] [blame] | 1578 | // Register dex caches with the class loader. |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1579 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Alex Light | a9bbc08 | 2019-11-14 14:51:41 -0800 | [diff] [blame] | 1580 | for (auto dex_cache : dex_caches.Iterate<mirror::DexCache>()) { |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1581 | const DexFile* const dex_file = dex_cache->GetDexFile(); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1582 | { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 1583 | WriterMutexLock mu2(self, *Locks::dex_lock_); |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 1584 | CHECK(class_linker->FindDexCacheDataLocked(*dex_file) == nullptr); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1585 | class_linker->RegisterDexFileLocked(*dex_file, dex_cache, class_loader.Get()); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1586 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1587 | } |
Mathieu Chartier | a0b9521 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 1588 | } |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1589 | |
Mathieu Chartier | 0933cc5 | 2018-03-23 14:25:08 -0700 | [diff] [blame] | 1590 | if (ClassLinker::kAppImageMayContainStrings) { |
Chris Wailes | fbeef46 | 2018-10-19 14:16:35 -0700 | [diff] [blame] | 1591 | HandleAppImageStrings(space); |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1592 | } |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1593 | |
Mathieu Chartier | a0b9521 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 1594 | if (kVerifyArtMethodDeclaringClasses) { |
Chris Wailes | 2386636 | 2018-08-22 16:16:58 -0700 | [diff] [blame] | 1595 | ScopedTrace timing("AppImage:VerifyDeclaringClasses"); |
Mathieu Chartier | a0b9521 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 1596 | ReaderMutexLock rmu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 9d5956a | 2019-03-22 11:29:08 -0700 | [diff] [blame] | 1597 | gc::accounting::HeapBitmap* live_bitmap = heap->GetLiveBitmap(); |
| 1598 | header.VisitPackedArtMethods([&](ArtMethod& method) |
| 1599 | REQUIRES_SHARED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) { |
| 1600 | ObjPtr<mirror::Class> klass = method.GetDeclaringClassUnchecked(); |
| 1601 | if (klass != nullptr) { |
| 1602 | CHECK(live_bitmap->Test(klass.Ptr())) << "Image method has unmarked declaring class"; |
| 1603 | } |
| 1604 | }, space->Begin(), kRuntimePointerSize); |
Mathieu Chartier | 03c1dd9 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 1605 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1606 | } |
| 1607 | |
Chris Wailes | fbeef46 | 2018-10-19 14:16:35 -0700 | [diff] [blame] | 1608 | void AppImageLoadingHelper::HandleAppImageStrings(gc::space::ImageSpace* space) { |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 1609 | // Iterate over the string reference offsets stored in the image and intern |
| 1610 | // the strings they point to. |
| 1611 | ScopedTrace timing("AppImage:InternString"); |
| 1612 | |
Mathieu Chartier | a88abfa | 2019-02-04 11:08:29 -0800 | [diff] [blame] | 1613 | Runtime* const runtime = Runtime::Current(); |
| 1614 | InternTable* const intern_table = runtime->GetInternTable(); |
| 1615 | |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 1616 | // Add the intern table, removing any conflicts. For conflicts, store the new address in a map |
| 1617 | // for faster lookup. |
| 1618 | // TODO: Optimize with a bitmap or bloom filter |
| 1619 | SafeMap<mirror::String*, mirror::String*> intern_remap; |
Mathieu Chartier | a88abfa | 2019-02-04 11:08:29 -0800 | [diff] [blame] | 1620 | auto func = [&](InternTable::UnorderedSet& interns) |
Mathieu Chartier | 41c0808 | 2018-10-31 11:50:26 -0700 | [diff] [blame] | 1621 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 1622 | REQUIRES(Locks::intern_table_lock_) { |
Mathieu Chartier | 8fc7558 | 2018-11-01 14:21:33 -0700 | [diff] [blame] | 1623 | const size_t non_boot_image_strings = intern_table->CountInterns( |
| 1624 | /*visit_boot_images=*/false, |
| 1625 | /*visit_non_boot_images=*/true); |
Chris Wailes | fbeef46 | 2018-10-19 14:16:35 -0700 | [diff] [blame] | 1626 | VLOG(image) << "AppImage:stringsInInternTableSize = " << interns.size(); |
Mathieu Chartier | 8fc7558 | 2018-11-01 14:21:33 -0700 | [diff] [blame] | 1627 | VLOG(image) << "AppImage:nonBootImageInternStrings = " << non_boot_image_strings; |
| 1628 | // Visit the smaller of the two sets to compute the intersection. |
| 1629 | if (interns.size() < non_boot_image_strings) { |
| 1630 | for (auto it = interns.begin(); it != interns.end(); ) { |
| 1631 | ObjPtr<mirror::String> string = it->Read(); |
| 1632 | ObjPtr<mirror::String> existing = intern_table->LookupWeakLocked(string); |
| 1633 | if (existing == nullptr) { |
| 1634 | existing = intern_table->LookupStrongLocked(string); |
| 1635 | } |
| 1636 | if (existing != nullptr) { |
| 1637 | intern_remap.Put(string.Ptr(), existing.Ptr()); |
| 1638 | it = interns.erase(it); |
| 1639 | } else { |
| 1640 | ++it; |
| 1641 | } |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 1642 | } |
Mathieu Chartier | 8fc7558 | 2018-11-01 14:21:33 -0700 | [diff] [blame] | 1643 | } else { |
| 1644 | intern_table->VisitInterns([&](const GcRoot<mirror::String>& root) |
| 1645 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 1646 | REQUIRES(Locks::intern_table_lock_) { |
| 1647 | auto it = interns.find(root); |
| 1648 | if (it != interns.end()) { |
| 1649 | ObjPtr<mirror::String> existing = root.Read(); |
| 1650 | intern_remap.Put(it->Read(), existing.Ptr()); |
| 1651 | it = interns.erase(it); |
| 1652 | } |
| 1653 | }, /*visit_boot_images=*/false, /*visit_non_boot_images=*/true); |
| 1654 | } |
David Srbecky | 346fd96 | 2020-07-27 16:51:00 +0100 | [diff] [blame] | 1655 | // Consistency check to ensure correctness. |
Mathieu Chartier | 8fc7558 | 2018-11-01 14:21:33 -0700 | [diff] [blame] | 1656 | if (kIsDebugBuild) { |
| 1657 | for (GcRoot<mirror::String>& root : interns) { |
| 1658 | ObjPtr<mirror::String> string = root.Read(); |
| 1659 | CHECK(intern_table->LookupWeakLocked(string) == nullptr) << string->ToModifiedUtf8(); |
| 1660 | CHECK(intern_table->LookupStrongLocked(string) == nullptr) << string->ToModifiedUtf8(); |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 1661 | } |
| 1662 | } |
Mathieu Chartier | a88abfa | 2019-02-04 11:08:29 -0800 | [diff] [blame] | 1663 | }; |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1664 | intern_table->AddImageStringsToTable(space, func); |
| 1665 | if (!intern_remap.empty()) { |
Mathieu Chartier | a88abfa | 2019-02-04 11:08:29 -0800 | [diff] [blame] | 1666 | VLOG(image) << "AppImage:conflictingInternStrings = " << intern_remap.size(); |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1667 | VisitInternedStringReferences( |
| 1668 | space, |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1669 | [&intern_remap](ObjPtr<mirror::String> str) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1670 | auto it = intern_remap.find(str.Ptr()); |
| 1671 | if (it != intern_remap.end()) { |
| 1672 | return ObjPtr<mirror::String>(it->second); |
| 1673 | } |
| 1674 | return str; |
| 1675 | }); |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 1676 | } |
| 1677 | } |
| 1678 | |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1679 | static std::unique_ptr<const DexFile> OpenOatDexFile(const OatFile* oat_file, |
| 1680 | const char* location, |
| 1681 | std::string* error_msg) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1682 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1683 | DCHECK(error_msg != nullptr); |
| 1684 | std::unique_ptr<const DexFile> dex_file; |
Andreas Gampe | b40d361 | 2018-06-26 15:49:42 -0700 | [diff] [blame] | 1685 | const OatDexFile* oat_dex_file = oat_file->GetOatDexFile(location, nullptr, error_msg); |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1686 | if (oat_dex_file == nullptr) { |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1687 | return std::unique_ptr<const DexFile>(); |
| 1688 | } |
| 1689 | std::string inner_error_msg; |
| 1690 | dex_file = oat_dex_file->OpenDexFile(&inner_error_msg); |
| 1691 | if (dex_file == nullptr) { |
| 1692 | *error_msg = StringPrintf("Failed to open dex file %s from within oat file %s error '%s'", |
| 1693 | location, |
| 1694 | oat_file->GetLocation().c_str(), |
| 1695 | inner_error_msg.c_str()); |
| 1696 | return std::unique_ptr<const DexFile>(); |
| 1697 | } |
| 1698 | |
| 1699 | if (dex_file->GetLocationChecksum() != oat_dex_file->GetDexFileLocationChecksum()) { |
| 1700 | *error_msg = StringPrintf("Checksums do not match for %s: %x vs %x", |
| 1701 | location, |
| 1702 | dex_file->GetLocationChecksum(), |
| 1703 | oat_dex_file->GetDexFileLocationChecksum()); |
| 1704 | return std::unique_ptr<const DexFile>(); |
| 1705 | } |
| 1706 | return dex_file; |
| 1707 | } |
| 1708 | |
| 1709 | bool ClassLinker::OpenImageDexFiles(gc::space::ImageSpace* space, |
| 1710 | std::vector<std::unique_ptr<const DexFile>>* out_dex_files, |
| 1711 | std::string* error_msg) { |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 1712 | ScopedAssertNoThreadSuspension nts(__FUNCTION__); |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1713 | const ImageHeader& header = space->GetImageHeader(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1714 | ObjPtr<mirror::Object> dex_caches_object = header.GetImageRoot(ImageHeader::kDexCaches); |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1715 | DCHECK(dex_caches_object != nullptr); |
Vladimir Marko | 4617d58 | 2019-03-28 13:48:31 +0000 | [diff] [blame] | 1716 | ObjPtr<mirror::ObjectArray<mirror::DexCache>> dex_caches = |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1717 | dex_caches_object->AsObjectArray<mirror::DexCache>(); |
| 1718 | const OatFile* oat_file = space->GetOatFile(); |
Alex Light | a9bbc08 | 2019-11-14 14:51:41 -0800 | [diff] [blame] | 1719 | for (auto dex_cache : dex_caches->Iterate()) { |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1720 | std::string dex_file_location(dex_cache->GetLocation()->ToModifiedUtf8()); |
| 1721 | std::unique_ptr<const DexFile> dex_file = OpenOatDexFile(oat_file, |
| 1722 | dex_file_location.c_str(), |
| 1723 | error_msg); |
| 1724 | if (dex_file == nullptr) { |
| 1725 | return false; |
| 1726 | } |
| 1727 | dex_cache->SetDexFile(dex_file.get()); |
| 1728 | out_dex_files->push_back(std::move(dex_file)); |
| 1729 | } |
| 1730 | return true; |
| 1731 | } |
| 1732 | |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1733 | // Helper class for ArtMethod checks when adding an image. Keeps all required functionality |
| 1734 | // together and caches some intermediate results. |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 1735 | class ImageChecker final { |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1736 | public: |
| 1737 | static void CheckObjects(gc::Heap* heap, ClassLinker* class_linker) |
| 1738 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 1739 | ImageChecker ic(heap, class_linker); |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 1740 | auto visitor = [&](mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1741 | DCHECK(obj != nullptr); |
| 1742 | CHECK(obj->GetClass() != nullptr) << "Null class in object " << obj; |
| 1743 | CHECK(obj->GetClass()->GetClass() != nullptr) << "Null class class " << obj; |
| 1744 | if (obj->IsClass()) { |
| 1745 | auto klass = obj->AsClass(); |
| 1746 | for (ArtField& field : klass->GetIFields()) { |
| 1747 | CHECK_EQ(field.GetDeclaringClass(), klass); |
| 1748 | } |
| 1749 | for (ArtField& field : klass->GetSFields()) { |
| 1750 | CHECK_EQ(field.GetDeclaringClass(), klass); |
| 1751 | } |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 1752 | const PointerSize pointer_size = ic.pointer_size_; |
Vladimir Marko | c524e9e | 2019-03-26 10:54:50 +0000 | [diff] [blame] | 1753 | for (ArtMethod& m : klass->GetMethods(pointer_size)) { |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 1754 | ic.CheckArtMethod(&m, klass); |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 1755 | } |
Vladimir Marko | c524e9e | 2019-03-26 10:54:50 +0000 | [diff] [blame] | 1756 | ObjPtr<mirror::PointerArray> vtable = klass->GetVTable(); |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 1757 | if (vtable != nullptr) { |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 1758 | ic.CheckArtMethodPointerArray(vtable, nullptr); |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 1759 | } |
| 1760 | if (klass->ShouldHaveImt()) { |
| 1761 | ImTable* imt = klass->GetImt(pointer_size); |
| 1762 | for (size_t i = 0; i < ImTable::kSize; ++i) { |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 1763 | ic.CheckArtMethod(imt->Get(i, pointer_size), nullptr); |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 1764 | } |
| 1765 | } |
| 1766 | if (klass->ShouldHaveEmbeddedVTable()) { |
| 1767 | for (int32_t i = 0; i < klass->GetEmbeddedVTableLength(); ++i) { |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 1768 | ic.CheckArtMethod(klass->GetEmbeddedVTableEntry(i, pointer_size), nullptr); |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 1769 | } |
| 1770 | } |
Vladimir Marko | c524e9e | 2019-03-26 10:54:50 +0000 | [diff] [blame] | 1771 | ObjPtr<mirror::IfTable> iftable = klass->GetIfTable(); |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 1772 | for (int32_t i = 0; i < klass->GetIfTableCount(); ++i) { |
| 1773 | if (iftable->GetMethodArrayCount(i) > 0) { |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 1774 | ic.CheckArtMethodPointerArray(iftable->GetMethodArray(i), nullptr); |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 1775 | } |
| 1776 | } |
| 1777 | } |
| 1778 | }; |
| 1779 | heap->VisitObjects(visitor); |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1780 | } |
| 1781 | |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1782 | private: |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 1783 | ImageChecker(gc::Heap* heap, ClassLinker* class_linker) |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1784 | : spaces_(heap->GetBootImageSpaces()), |
| 1785 | pointer_size_(class_linker->GetImagePointerSize()) { |
| 1786 | space_begin_.reserve(spaces_.size()); |
| 1787 | method_sections_.reserve(spaces_.size()); |
| 1788 | runtime_method_sections_.reserve(spaces_.size()); |
| 1789 | for (gc::space::ImageSpace* space : spaces_) { |
| 1790 | space_begin_.push_back(space->Begin()); |
| 1791 | auto& header = space->GetImageHeader(); |
| 1792 | method_sections_.push_back(&header.GetMethodsSection()); |
| 1793 | runtime_method_sections_.push_back(&header.GetRuntimeMethodsSection()); |
| 1794 | } |
| 1795 | } |
| 1796 | |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 1797 | void CheckArtMethod(ArtMethod* m, ObjPtr<mirror::Class> expected_class) |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1798 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1799 | if (m->IsRuntimeMethod()) { |
| 1800 | ObjPtr<mirror::Class> declaring_class = m->GetDeclaringClassUnchecked(); |
| 1801 | CHECK(declaring_class == nullptr) << declaring_class << " " << m->PrettyMethod(); |
| 1802 | } else if (m->IsCopied()) { |
| 1803 | CHECK(m->GetDeclaringClass() != nullptr) << m->PrettyMethod(); |
| 1804 | } else if (expected_class != nullptr) { |
| 1805 | CHECK_EQ(m->GetDeclaringClassUnchecked(), expected_class) << m->PrettyMethod(); |
| 1806 | } |
| 1807 | if (!spaces_.empty()) { |
| 1808 | bool contains = false; |
| 1809 | for (size_t i = 0; !contains && i != space_begin_.size(); ++i) { |
| 1810 | const size_t offset = reinterpret_cast<uint8_t*>(m) - space_begin_[i]; |
| 1811 | contains = method_sections_[i]->Contains(offset) || |
| 1812 | runtime_method_sections_[i]->Contains(offset); |
| 1813 | } |
| 1814 | CHECK(contains) << m << " not found"; |
| 1815 | } |
| 1816 | } |
| 1817 | |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 1818 | void CheckArtMethodPointerArray(ObjPtr<mirror::PointerArray> arr, |
| 1819 | ObjPtr<mirror::Class> expected_class) |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1820 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1821 | CHECK(arr != nullptr); |
| 1822 | for (int32_t j = 0; j < arr->GetLength(); ++j) { |
| 1823 | auto* method = arr->GetElementPtrSize<ArtMethod*>(j, pointer_size_); |
| 1824 | // expected_class == null means we are a dex cache. |
| 1825 | if (expected_class != nullptr) { |
| 1826 | CHECK(method != nullptr); |
| 1827 | } |
| 1828 | if (method != nullptr) { |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 1829 | CheckArtMethod(method, expected_class); |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1830 | } |
| 1831 | } |
| 1832 | } |
| 1833 | |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1834 | const std::vector<gc::space::ImageSpace*>& spaces_; |
| 1835 | const PointerSize pointer_size_; |
| 1836 | |
| 1837 | // Cached sections from the spaces. |
| 1838 | std::vector<const uint8_t*> space_begin_; |
| 1839 | std::vector<const ImageSection*> method_sections_; |
| 1840 | std::vector<const ImageSection*> runtime_method_sections_; |
| 1841 | }; |
| 1842 | |
Andreas Gampe | be7af22 | 2017-07-25 09:57:28 -0700 | [diff] [blame] | 1843 | static void VerifyAppImage(const ImageHeader& header, |
| 1844 | const Handle<mirror::ClassLoader>& class_loader, |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 1845 | ClassTable* class_table, |
| 1846 | gc::space::ImageSpace* space) |
Andreas Gampe | be7af22 | 2017-07-25 09:57:28 -0700 | [diff] [blame] | 1847 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 9d5956a | 2019-03-22 11:29:08 -0700 | [diff] [blame] | 1848 | header.VisitPackedArtMethods([&](ArtMethod& method) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1849 | ObjPtr<mirror::Class> klass = method.GetDeclaringClass(); |
| 1850 | if (klass != nullptr && !Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(klass)) { |
| 1851 | CHECK_EQ(class_table->LookupByDescriptor(klass), klass) |
| 1852 | << mirror::Class::PrettyClass(klass); |
| 1853 | } |
| 1854 | }, space->Begin(), kRuntimePointerSize); |
Andreas Gampe | be7af22 | 2017-07-25 09:57:28 -0700 | [diff] [blame] | 1855 | { |
| 1856 | // Verify that all direct interfaces of classes in the class table are also resolved. |
| 1857 | std::vector<ObjPtr<mirror::Class>> classes; |
| 1858 | auto verify_direct_interfaces_in_table = [&](ObjPtr<mirror::Class> klass) |
| 1859 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1860 | if (!klass->IsPrimitive() && klass->GetClassLoader() == class_loader.Get()) { |
| 1861 | classes.push_back(klass); |
| 1862 | } |
| 1863 | return true; |
| 1864 | }; |
| 1865 | class_table->Visit(verify_direct_interfaces_in_table); |
Andreas Gampe | be7af22 | 2017-07-25 09:57:28 -0700 | [diff] [blame] | 1866 | for (ObjPtr<mirror::Class> klass : classes) { |
| 1867 | for (uint32_t i = 0, num = klass->NumDirectInterfaces(); i != num; ++i) { |
Vladimir Marko | b10668c | 2021-06-10 09:52:53 +0100 | [diff] [blame] | 1868 | CHECK(klass->GetDirectInterface(i) != nullptr) |
Andreas Gampe | be7af22 | 2017-07-25 09:57:28 -0700 | [diff] [blame] | 1869 | << klass->PrettyDescriptor() << " iface #" << i; |
| 1870 | } |
| 1871 | } |
| 1872 | } |
Andreas Gampe | be7af22 | 2017-07-25 09:57:28 -0700 | [diff] [blame] | 1873 | } |
| 1874 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1875 | bool ClassLinker::AddImageSpace( |
| 1876 | gc::space::ImageSpace* space, |
| 1877 | Handle<mirror::ClassLoader> class_loader, |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1878 | std::vector<std::unique_ptr<const DexFile>>* out_dex_files, |
| 1879 | std::string* error_msg) { |
| 1880 | DCHECK(out_dex_files != nullptr); |
| 1881 | DCHECK(error_msg != nullptr); |
| 1882 | const uint64_t start_time = NanoTime(); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 1883 | const bool app_image = class_loader != nullptr; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1884 | const ImageHeader& header = space->GetImageHeader(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1885 | ObjPtr<mirror::Object> dex_caches_object = header.GetImageRoot(ImageHeader::kDexCaches); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1886 | DCHECK(dex_caches_object != nullptr); |
| 1887 | Runtime* const runtime = Runtime::Current(); |
| 1888 | gc::Heap* const heap = runtime->GetHeap(); |
| 1889 | Thread* const self = Thread::Current(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1890 | // Check that the image is what we are expecting. |
| 1891 | if (image_pointer_size_ != space->GetImageHeader().GetPointerSize()) { |
| 1892 | *error_msg = StringPrintf("Application image pointer size does not match runtime: %zu vs %zu", |
| 1893 | static_cast<size_t>(space->GetImageHeader().GetPointerSize()), |
| 1894 | image_pointer_size_); |
| 1895 | return false; |
| 1896 | } |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1897 | size_t expected_image_roots = ImageHeader::NumberOfImageRoots(app_image); |
| 1898 | if (static_cast<size_t>(header.GetImageRoots()->GetLength()) != expected_image_roots) { |
| 1899 | *error_msg = StringPrintf("Expected %zu image roots but got %d", |
| 1900 | expected_image_roots, |
| 1901 | header.GetImageRoots()->GetLength()); |
| 1902 | return false; |
| 1903 | } |
| 1904 | StackHandleScope<3> hs(self); |
| 1905 | Handle<mirror::ObjectArray<mirror::DexCache>> dex_caches( |
| 1906 | hs.NewHandle(dex_caches_object->AsObjectArray<mirror::DexCache>())); |
| 1907 | Handle<mirror::ObjectArray<mirror::Class>> class_roots(hs.NewHandle( |
| 1908 | header.GetImageRoot(ImageHeader::kClassRoots)->AsObjectArray<mirror::Class>())); |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1909 | MutableHandle<mirror::ClassLoader> image_class_loader(hs.NewHandle( |
Vladimir Marko | f75613c | 2018-06-05 12:51:04 +0100 | [diff] [blame] | 1910 | app_image ? header.GetImageRoot(ImageHeader::kAppImageClassLoader)->AsClassLoader() |
| 1911 | : nullptr)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 1912 | DCHECK(class_roots != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 1913 | if (class_roots->GetLength() != static_cast<int32_t>(ClassRoot::kMax)) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1914 | *error_msg = StringPrintf("Expected %d class roots but got %d", |
| 1915 | class_roots->GetLength(), |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 1916 | static_cast<int32_t>(ClassRoot::kMax)); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1917 | return false; |
| 1918 | } |
| 1919 | // 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] | 1920 | ObjPtr<mirror::ObjectArray<mirror::Class>> existing_class_roots = GetClassRoots(); |
| 1921 | for (size_t i = 0; i < static_cast<size_t>(ClassRoot::kMax); i++) { |
| 1922 | 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] | 1923 | *error_msg = "App image class roots must have pointer equality with runtime ones."; |
| 1924 | return false; |
| 1925 | } |
| 1926 | } |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1927 | const OatFile* oat_file = space->GetOatFile(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1928 | if (oat_file->GetOatHeader().GetDexFileCount() != |
| 1929 | static_cast<uint32_t>(dex_caches->GetLength())) { |
| 1930 | *error_msg = "Dex cache count and dex file count mismatch while trying to initialize from " |
| 1931 | "image"; |
| 1932 | return false; |
| 1933 | } |
| 1934 | |
Alex Light | a9bbc08 | 2019-11-14 14:51:41 -0800 | [diff] [blame] | 1935 | for (auto dex_cache : dex_caches.Iterate<mirror::DexCache>()) { |
David Brazdil | 3e8aae0 | 2019-03-26 18:48:02 +0000 | [diff] [blame] | 1936 | std::string dex_file_location = dex_cache->GetLocation()->ToModifiedUtf8(); |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1937 | std::unique_ptr<const DexFile> dex_file = OpenOatDexFile(oat_file, |
| 1938 | dex_file_location.c_str(), |
| 1939 | error_msg); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1940 | if (dex_file == nullptr) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1941 | return false; |
| 1942 | } |
| 1943 | |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 1944 | { |
David Srbecky | 33df0e3 | 2021-09-30 14:36:32 +0000 | [diff] [blame] | 1945 | // Native fields are all null. Initialize them. |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 1946 | WriterMutexLock mu(self, *Locks::dex_lock_); |
David Srbecky | 33df0e3 | 2021-09-30 14:36:32 +0000 | [diff] [blame] | 1947 | dex_cache->Initialize(dex_file.get(), class_loader.Get()); |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 1948 | } |
| 1949 | if (!app_image) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1950 | // Register dex files, keep track of existing ones that are conflicts. |
Mathieu Chartier | 0a19e21 | 2019-11-27 14:35:24 -0800 | [diff] [blame] | 1951 | AppendToBootClassPath(dex_file.get(), dex_cache); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1952 | } |
| 1953 | out_dex_files->push_back(std::move(dex_file)); |
| 1954 | } |
| 1955 | |
| 1956 | if (app_image) { |
| 1957 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Nicolas Geoffray | f0d3002 | 2018-11-20 17:45:38 +0000 | [diff] [blame] | 1958 | ScopedAssertNoThreadSuspension sants("Checking app image", soa.Self()); |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1959 | if (IsBootClassLoader(soa, image_class_loader.Get())) { |
| 1960 | *error_msg = "Unexpected BootClassLoader in app image"; |
| 1961 | return false; |
| 1962 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1963 | } |
| 1964 | |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 1965 | if (kCheckImageObjects) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1966 | if (!app_image) { |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 1967 | ImageChecker::CheckObjects(heap, this); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1968 | } |
| 1969 | } |
| 1970 | |
| 1971 | // Set entry point to interpreter if in InterpretOnly mode. |
| 1972 | if (!runtime->IsAotCompiler() && runtime->GetInstrumentation()->InterpretOnly()) { |
Mathieu Chartier | 9d5956a | 2019-03-22 11:29:08 -0700 | [diff] [blame] | 1973 | // Set image methods' entry point to interpreter. |
| 1974 | header.VisitPackedArtMethods([&](ArtMethod& method) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1975 | if (!method.IsRuntimeMethod()) { |
| 1976 | DCHECK(method.GetDeclaringClass() != nullptr); |
Ulya Trafimovich | 5439f05 | 2020-07-29 10:03:46 +0100 | [diff] [blame] | 1977 | if (!method.IsNative() && !method.IsResolutionMethod()) { |
| 1978 | method.SetEntryPointFromQuickCompiledCodePtrSize(GetQuickToInterpreterBridge(), |
| 1979 | image_pointer_size_); |
Mathieu Chartier | 9d5956a | 2019-03-22 11:29:08 -0700 | [diff] [blame] | 1980 | } |
| 1981 | } |
| 1982 | }, space->Begin(), image_pointer_size_); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1983 | } |
| 1984 | |
Nicolas Geoffray | 4717175 | 2020-08-31 15:03:20 +0100 | [diff] [blame] | 1985 | if (!runtime->IsAotCompiler()) { |
Nicolas Geoffray | 0ca39d1 | 2022-04-27 14:11:23 +0100 | [diff] [blame] | 1986 | // If we are profiling the boot classpath, disable the shared memory for |
| 1987 | // boot image method optimization. We need to disable it before doing |
| 1988 | // ResetCounter below, as counters of shared memory method always hold the |
| 1989 | // "hot" value. |
| 1990 | if (runtime->GetJITOptions()->GetProfileSaverOptions().GetProfileBootClassPath()) { |
| 1991 | header.VisitPackedArtMethods([&](ArtMethod& method) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1992 | method.ClearMemorySharedMethod(); |
| 1993 | }, space->Begin(), image_pointer_size_); |
| 1994 | } |
| 1995 | |
Nicolas Geoffray | bd728b0 | 2021-01-27 13:21:35 +0000 | [diff] [blame] | 1996 | ScopedTrace trace("AppImage:UpdateCodeItemAndNterp"); |
Nicolas Geoffray | 4717175 | 2020-08-31 15:03:20 +0100 | [diff] [blame] | 1997 | bool can_use_nterp = interpreter::CanRuntimeUseNterp(); |
Nicolas Geoffray | 58f916c | 2021-11-15 14:02:07 +0000 | [diff] [blame] | 1998 | uint16_t hotness_threshold = runtime->GetJITOptions()->GetWarmupThreshold(); |
Nicolas Geoffray | 7e2c963 | 2020-01-09 13:41:10 +0000 | [diff] [blame] | 1999 | header.VisitPackedArtMethods([&](ArtMethod& method) REQUIRES_SHARED(Locks::mutator_lock_) { |
Nicolas Geoffray | 4717175 | 2020-08-31 15:03:20 +0100 | [diff] [blame] | 2000 | // In the image, the `data` pointer field of the ArtMethod contains the code |
| 2001 | // item offset. Change this to the actual pointer to the code item. |
| 2002 | if (method.HasCodeItem()) { |
| 2003 | const dex::CodeItem* code_item = method.GetDexFile()->GetCodeItem( |
| 2004 | reinterpret_cast32<uint32_t>(method.GetDataPtrSize(image_pointer_size_))); |
zhaoxuyang | 7156ea2 | 2022-01-10 13:58:11 +0800 | [diff] [blame] | 2005 | method.SetCodeItem(code_item, method.GetDexFile()->IsCompactDexFile()); |
Nicolas Geoffray | 61673dc | 2021-11-06 13:58:31 +0000 | [diff] [blame] | 2006 | // The hotness counter may have changed since we compiled the image, so |
| 2007 | // reset it with the runtime value. |
Nicolas Geoffray | 58f916c | 2021-11-15 14:02:07 +0000 | [diff] [blame] | 2008 | method.ResetCounter(hotness_threshold); |
Nicolas Geoffray | 4717175 | 2020-08-31 15:03:20 +0100 | [diff] [blame] | 2009 | } |
Nicolas Geoffray | c39af94 | 2021-01-25 08:43:57 +0000 | [diff] [blame] | 2010 | if (method.GetEntryPointFromQuickCompiledCode() == nterp_trampoline_) { |
| 2011 | if (can_use_nterp) { |
Nicolas Geoffray | c8a694d | 2022-01-17 17:12:38 +0000 | [diff] [blame] | 2012 | // Set image methods' entry point that point to the nterp trampoline to the |
| 2013 | // nterp entry point. This allows taking the fast path when doing a |
| 2014 | // nterp->nterp call. |
Santiago Aboy Solanes | 6cdabe1 | 2022-02-18 15:27:43 +0000 | [diff] [blame] | 2015 | DCHECK_IMPLIES(NeedsClinitCheckBeforeCall(&method), |
| 2016 | method.GetDeclaringClass()->IsVisiblyInitialized()); |
Nicolas Geoffray | c39af94 | 2021-01-25 08:43:57 +0000 | [diff] [blame] | 2017 | method.SetEntryPointFromQuickCompiledCode(interpreter::GetNterpEntryPoint()); |
| 2018 | } else { |
| 2019 | method.SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge()); |
| 2020 | } |
Nicolas Geoffray | 4717175 | 2020-08-31 15:03:20 +0100 | [diff] [blame] | 2021 | } |
Nicolas Geoffray | 7e2c963 | 2020-01-09 13:41:10 +0000 | [diff] [blame] | 2022 | }, space->Begin(), image_pointer_size_); |
| 2023 | } |
| 2024 | |
Nicolas Geoffray | 8c41a0b | 2020-02-06 16:52:11 +0000 | [diff] [blame] | 2025 | if (runtime->IsVerificationSoftFail()) { |
| 2026 | header.VisitPackedArtMethods([&](ArtMethod& method) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 2027 | if (!method.IsNative() && method.IsInvokable()) { |
| 2028 | method.ClearSkipAccessChecks(); |
| 2029 | } |
| 2030 | }, space->Begin(), image_pointer_size_); |
| 2031 | } |
| 2032 | |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 2033 | ClassTable* class_table = nullptr; |
| 2034 | { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2035 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 2036 | class_table = InsertClassTableForClassLoader(class_loader.Get()); |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 2037 | } |
| 2038 | // If we have a class table section, read it and use it for verification in |
| 2039 | // UpdateAppImageClassLoadersAndDexCaches. |
| 2040 | ClassTable::ClassSet temp_set; |
Vladimir Marko | 0f3c700 | 2017-09-07 14:15:56 +0100 | [diff] [blame] | 2041 | const ImageSection& class_table_section = header.GetClassTableSection(); |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 2042 | const bool added_class_table = class_table_section.Size() > 0u; |
| 2043 | if (added_class_table) { |
| 2044 | const uint64_t start_time2 = NanoTime(); |
| 2045 | size_t read_count = 0; |
| 2046 | temp_set = ClassTable::ClassSet(space->Begin() + class_table_section.Offset(), |
| 2047 | /*make copy*/false, |
| 2048 | &read_count); |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 2049 | VLOG(image) << "Adding class table classes took " << PrettyDuration(NanoTime() - start_time2); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2050 | } |
| 2051 | if (app_image) { |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 2052 | AppImageLoadingHelper::Update(this, space, class_loader, dex_caches); |
Mathieu Chartier | 456b492 | 2018-11-06 10:35:48 -0800 | [diff] [blame] | 2053 | |
| 2054 | { |
| 2055 | ScopedTrace trace("AppImage:UpdateClassLoaders"); |
| 2056 | // Update class loader and resolved strings. If added_class_table is false, the resolved |
| 2057 | // strings were forwarded UpdateAppImageClassLoadersAndDexCaches. |
Mathieu Chartier | bc1e0fa | 2018-11-14 16:18:18 -0800 | [diff] [blame] | 2058 | ObjPtr<mirror::ClassLoader> loader(class_loader.Get()); |
Mathieu Chartier | 456b492 | 2018-11-06 10:35:48 -0800 | [diff] [blame] | 2059 | for (const ClassTable::TableSlot& root : temp_set) { |
Mathieu Chartier | bc1e0fa | 2018-11-14 16:18:18 -0800 | [diff] [blame] | 2060 | // Note: We probably don't need the read barrier unless we copy the app image objects into |
| 2061 | // the region space. |
| 2062 | ObjPtr<mirror::Class> klass(root.Read()); |
| 2063 | // Do not update class loader for boot image classes where the app image |
| 2064 | // class loader is only the initiating loader but not the defining loader. |
| 2065 | // Avoid read barrier since we are comparing against null. |
| 2066 | if (klass->GetClassLoader<kDefaultVerifyFlags, kWithoutReadBarrier>() != nullptr) { |
Vladimir Marko | b68bb7a | 2020-03-17 10:55:25 +0000 | [diff] [blame] | 2067 | klass->SetClassLoader(loader); |
Mathieu Chartier | bc1e0fa | 2018-11-14 16:18:18 -0800 | [diff] [blame] | 2068 | } |
Mathieu Chartier | 456b492 | 2018-11-06 10:35:48 -0800 | [diff] [blame] | 2069 | } |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 2070 | } |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 2071 | |
Vladimir Marko | 305c38b | 2018-02-14 11:50:07 +0000 | [diff] [blame] | 2072 | if (kBitstringSubtypeCheckEnabled) { |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 2073 | // Every class in the app image has initially SubtypeCheckInfo in the |
| 2074 | // Uninitialized state. |
| 2075 | // |
| 2076 | // The SubtypeCheck invariants imply that a SubtypeCheckInfo is at least Initialized |
| 2077 | // after class initialization is complete. The app image ClassStatus as-is |
| 2078 | // are almost all ClassStatus::Initialized, and being in the |
| 2079 | // SubtypeCheckInfo::kUninitialized state is violating that invariant. |
| 2080 | // |
| 2081 | // Force every app image class's SubtypeCheck to be at least kIninitialized. |
| 2082 | // |
| 2083 | // See also ImageWriter::FixupClass. |
Chris Wailes | 2386636 | 2018-08-22 16:16:58 -0700 | [diff] [blame] | 2084 | ScopedTrace trace("AppImage:RecacluateSubtypeCheckBitstrings"); |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 2085 | MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_); |
| 2086 | for (const ClassTable::TableSlot& root : temp_set) { |
Vladimir Marko | 38b8b25 | 2018-01-02 19:07:06 +0000 | [diff] [blame] | 2087 | SubtypeCheck<ObjPtr<mirror::Class>>::EnsureInitialized(root.Read()); |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 2088 | } |
| 2089 | } |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 2090 | } |
| 2091 | if (!oat_file->GetBssGcRoots().empty()) { |
| 2092 | // Insert oat file to class table for visiting .bss GC roots. |
| 2093 | class_table->InsertOatFile(oat_file); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2094 | } |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 2095 | |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 2096 | if (added_class_table) { |
| 2097 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 2098 | class_table->AddClassSet(std::move(temp_set)); |
| 2099 | } |
Andreas Gampe | be7af22 | 2017-07-25 09:57:28 -0700 | [diff] [blame] | 2100 | |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 2101 | if (kIsDebugBuild && app_image) { |
| 2102 | // This verification needs to happen after the classes have been added to the class loader. |
| 2103 | // Since it ensures classes are in the class table. |
Chris Wailes | 2386636 | 2018-08-22 16:16:58 -0700 | [diff] [blame] | 2104 | ScopedTrace trace("AppImage:Verify"); |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 2105 | VerifyAppImage(header, class_loader, class_table, space); |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 2106 | } |
Andreas Gampe | be7af22 | 2017-07-25 09:57:28 -0700 | [diff] [blame] | 2107 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2108 | VLOG(class_linker) << "Adding image space took " << PrettyDuration(NanoTime() - start_time); |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 2109 | return true; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 2110 | } |
| 2111 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 2112 | void ClassLinker::VisitClassRoots(RootVisitor* visitor, VisitRootFlags flags) { |
Mathieu Chartier | 7778b88 | 2015-10-05 16:41:10 -0700 | [diff] [blame] | 2113 | // Acquire tracing_enabled before locking class linker lock to prevent lock order violation. Since |
| 2114 | // enabling tracing requires the mutator lock, there are no race conditions here. |
| 2115 | const bool tracing_enabled = Trace::IsTracingEnabled(); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2116 | Thread* const self = Thread::Current(); |
| 2117 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Vladimir Marko | 9b03cb4 | 2017-02-16 16:37:03 +0000 | [diff] [blame] | 2118 | if (kUseReadBarrier) { |
| 2119 | // We do not track new roots for CC. |
| 2120 | DCHECK_EQ(0, flags & (kVisitRootFlagNewRoots | |
| 2121 | kVisitRootFlagClearRootLog | |
| 2122 | kVisitRootFlagStartLoggingNewRoots | |
| 2123 | kVisitRootFlagStopLoggingNewRoots)); |
| 2124 | } |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2125 | if ((flags & kVisitRootFlagAllRoots) != 0) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2126 | // Argument for how root visiting deals with ArtField and ArtMethod roots. |
| 2127 | // There is 3 GC cases to handle: |
| 2128 | // Non moving concurrent: |
| 2129 | // 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] | 2130 | // live by the class and class roots. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2131 | // |
| 2132 | // Moving non-concurrent: |
| 2133 | // This case needs to call visit VisitNativeRoots in case the classes or dex cache arrays move. |
| 2134 | // To prevent missing roots, this case needs to ensure that there is no |
| 2135 | // suspend points between the point which we allocate ArtMethod arrays and place them in a |
| 2136 | // class which is in the class table. |
| 2137 | // |
| 2138 | // Moving concurrent: |
| 2139 | // Need to make sure to not copy ArtMethods without doing read barriers since the roots are |
| 2140 | // 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] | 2141 | // |
| 2142 | // Use an unbuffered visitor since the class table uses a temporary GcRoot for holding decoded |
| 2143 | // ClassTable::TableSlot. The buffered root visiting would access a stale stack location for |
| 2144 | // these objects. |
| 2145 | UnbufferedRootVisitor root_visitor(visitor, RootInfo(kRootStickyClass)); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 2146 | boot_class_table_->VisitRoots(root_visitor); |
Mathieu Chartier | 7778b88 | 2015-10-05 16:41:10 -0700 | [diff] [blame] | 2147 | // 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] | 2148 | if ((flags & kVisitRootFlagClassLoader) != 0 || tracing_enabled) { |
Mathieu Chartier | 7778b88 | 2015-10-05 16:41:10 -0700 | [diff] [blame] | 2149 | for (const ClassLoaderData& data : class_loaders_) { |
| 2150 | GcRoot<mirror::Object> root(GcRoot<mirror::Object>(self->DecodeJObject(data.weak_root))); |
| 2151 | root.VisitRoot(visitor, RootInfo(kRootVMInternal)); |
| 2152 | } |
| 2153 | } |
Vladimir Marko | 9b03cb4 | 2017-02-16 16:37:03 +0000 | [diff] [blame] | 2154 | } else if (!kUseReadBarrier && (flags & kVisitRootFlagNewRoots) != 0) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 2155 | for (auto& root : new_class_roots_) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2156 | ObjPtr<mirror::Class> old_ref = root.Read<kWithoutReadBarrier>(); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 2157 | root.VisitRoot(visitor, RootInfo(kRootStickyClass)); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2158 | ObjPtr<mirror::Class> new_ref = root.Read<kWithoutReadBarrier>(); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 2159 | // Concurrent moving GC marked new roots through the to-space invariant. |
| 2160 | CHECK_EQ(new_ref, old_ref); |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2161 | } |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 2162 | for (const OatFile* oat_file : new_bss_roots_boot_oat_files_) { |
| 2163 | for (GcRoot<mirror::Object>& root : oat_file->GetBssGcRoots()) { |
| 2164 | ObjPtr<mirror::Object> old_ref = root.Read<kWithoutReadBarrier>(); |
| 2165 | if (old_ref != nullptr) { |
| 2166 | DCHECK(old_ref->IsClass()); |
| 2167 | root.VisitRoot(visitor, RootInfo(kRootStickyClass)); |
| 2168 | ObjPtr<mirror::Object> new_ref = root.Read<kWithoutReadBarrier>(); |
| 2169 | // Concurrent moving GC marked new roots through the to-space invariant. |
| 2170 | CHECK_EQ(new_ref, old_ref); |
| 2171 | } |
| 2172 | } |
| 2173 | } |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2174 | } |
Vladimir Marko | 9b03cb4 | 2017-02-16 16:37:03 +0000 | [diff] [blame] | 2175 | if (!kUseReadBarrier && (flags & kVisitRootFlagClearRootLog) != 0) { |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2176 | new_class_roots_.clear(); |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 2177 | new_bss_roots_boot_oat_files_.clear(); |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2178 | } |
Vladimir Marko | 9b03cb4 | 2017-02-16 16:37:03 +0000 | [diff] [blame] | 2179 | if (!kUseReadBarrier && (flags & kVisitRootFlagStartLoggingNewRoots) != 0) { |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 2180 | log_new_roots_ = true; |
Vladimir Marko | 9b03cb4 | 2017-02-16 16:37:03 +0000 | [diff] [blame] | 2181 | } else if (!kUseReadBarrier && (flags & kVisitRootFlagStopLoggingNewRoots) != 0) { |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 2182 | log_new_roots_ = false; |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2183 | } |
| 2184 | // We deliberately ignore the class roots in the image since we |
| 2185 | // handle image roots by using the MS/CMS rescanning of dirty cards. |
| 2186 | } |
| 2187 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 2188 | // Keep in sync with InitCallback. Anything we visit, we need to |
| 2189 | // reinit references to when reinitializing a ClassLinker from a |
| 2190 | // mapped image. |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 2191 | void ClassLinker::VisitRoots(RootVisitor* visitor, VisitRootFlags flags) { |
Mathieu Chartier | 3100080 | 2015-06-14 14:14:37 -0700 | [diff] [blame] | 2192 | class_roots_.VisitRootIfNonNull(visitor, RootInfo(kRootVMInternal)); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 2193 | VisitClassRoots(visitor, flags); |
Mathieu Chartier | 6cfc2c0 | 2015-10-12 15:06:16 -0700 | [diff] [blame] | 2194 | // Instead of visiting the find_array_class_cache_ drop it so that it doesn't prevent class |
| 2195 | // unloading if we are marking roots. |
| 2196 | DropFindArrayClassCache(); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 2197 | } |
| 2198 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2199 | class VisitClassLoaderClassesVisitor : public ClassLoaderVisitor { |
| 2200 | public: |
| 2201 | explicit VisitClassLoaderClassesVisitor(ClassVisitor* visitor) |
| 2202 | : visitor_(visitor), |
| 2203 | done_(false) {} |
| 2204 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2205 | void Visit(ObjPtr<mirror::ClassLoader> class_loader) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 2206 | REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_) override { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2207 | ClassTable* const class_table = class_loader->GetClassTable(); |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2208 | if (!done_ && class_table != nullptr) { |
| 2209 | DefiningClassLoaderFilterVisitor visitor(class_loader, visitor_); |
| 2210 | if (!class_table->Visit(visitor)) { |
| 2211 | // If the visitor ClassTable returns false it means that we don't need to continue. |
| 2212 | done_ = true; |
| 2213 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2214 | } |
| 2215 | } |
| 2216 | |
| 2217 | private: |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2218 | // Class visitor that limits the class visits from a ClassTable to the classes with |
| 2219 | // the provided defining class loader. This filter is used to avoid multiple visits |
| 2220 | // of the same class which can be recorded for multiple initiating class loaders. |
| 2221 | class DefiningClassLoaderFilterVisitor : public ClassVisitor { |
| 2222 | public: |
| 2223 | DefiningClassLoaderFilterVisitor(ObjPtr<mirror::ClassLoader> defining_class_loader, |
| 2224 | ClassVisitor* visitor) |
| 2225 | : defining_class_loader_(defining_class_loader), visitor_(visitor) { } |
| 2226 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 2227 | bool operator()(ObjPtr<mirror::Class> klass) override REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2228 | if (klass->GetClassLoader() != defining_class_loader_) { |
| 2229 | return true; |
| 2230 | } |
| 2231 | return (*visitor_)(klass); |
| 2232 | } |
| 2233 | |
Vladimir Marko | 0984e48 | 2019-03-27 16:41:41 +0000 | [diff] [blame] | 2234 | const ObjPtr<mirror::ClassLoader> defining_class_loader_; |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2235 | ClassVisitor* const visitor_; |
| 2236 | }; |
| 2237 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2238 | ClassVisitor* const visitor_; |
| 2239 | // If done is true then we don't need to do any more visiting. |
| 2240 | bool done_; |
| 2241 | }; |
| 2242 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2243 | void ClassLinker::VisitClassesInternal(ClassVisitor* visitor) { |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 2244 | if (boot_class_table_->Visit(*visitor)) { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2245 | VisitClassLoaderClassesVisitor loader_visitor(visitor); |
| 2246 | VisitClassLoaders(&loader_visitor); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 2247 | } |
| 2248 | } |
| 2249 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2250 | void ClassLinker::VisitClasses(ClassVisitor* visitor) { |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 2251 | Thread* const self = Thread::Current(); |
| 2252 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 2253 | // Not safe to have thread suspension when we are holding a lock. |
| 2254 | if (self != nullptr) { |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 2255 | ScopedAssertNoThreadSuspension nts(__FUNCTION__); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2256 | VisitClassesInternal(visitor); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 2257 | } else { |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2258 | VisitClassesInternal(visitor); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2259 | } |
| 2260 | } |
| 2261 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2262 | class GetClassesInToVector : public ClassVisitor { |
| 2263 | public: |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 2264 | bool operator()(ObjPtr<mirror::Class> klass) override { |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2265 | classes_.push_back(klass); |
| 2266 | return true; |
| 2267 | } |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2268 | std::vector<ObjPtr<mirror::Class>> classes_; |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2269 | }; |
| 2270 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2271 | class GetClassInToObjectArray : public ClassVisitor { |
| 2272 | public: |
| 2273 | explicit GetClassInToObjectArray(mirror::ObjectArray<mirror::Class>* arr) |
| 2274 | : arr_(arr), index_(0) {} |
| 2275 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 2276 | bool operator()(ObjPtr<mirror::Class> klass) override REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2277 | ++index_; |
| 2278 | if (index_ <= arr_->GetLength()) { |
| 2279 | arr_->Set(index_ - 1, klass); |
| 2280 | return true; |
| 2281 | } |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2282 | return false; |
| 2283 | } |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2284 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2285 | bool Succeeded() const REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2286 | return index_ <= arr_->GetLength(); |
| 2287 | } |
| 2288 | |
| 2289 | private: |
| 2290 | mirror::ObjectArray<mirror::Class>* const arr_; |
| 2291 | int32_t index_; |
| 2292 | }; |
| 2293 | |
| 2294 | void ClassLinker::VisitClassesWithoutClassesLock(ClassVisitor* visitor) { |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2295 | // TODO: it may be possible to avoid secondary storage if we iterate over dex caches. The problem |
| 2296 | // is avoiding duplicates. |
| 2297 | if (!kMovingClasses) { |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 2298 | ScopedAssertNoThreadSuspension nts(__FUNCTION__); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2299 | GetClassesInToVector accumulator; |
| 2300 | VisitClasses(&accumulator); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2301 | for (ObjPtr<mirror::Class> klass : accumulator.classes_) { |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 2302 | if (!visitor->operator()(klass)) { |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2303 | return; |
| 2304 | } |
| 2305 | } |
| 2306 | } else { |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 2307 | Thread* const self = Thread::Current(); |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2308 | StackHandleScope<1> hs(self); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2309 | auto classes = hs.NewHandle<mirror::ObjectArray<mirror::Class>>(nullptr); |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2310 | // We size the array assuming classes won't be added to the class table during the visit. |
| 2311 | // If this assumption fails we iterate again. |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2312 | while (true) { |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2313 | size_t class_table_size; |
| 2314 | { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2315 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2316 | // Add 100 in case new classes get loaded when we are filling in the object array. |
| 2317 | class_table_size = NumZygoteClasses() + NumNonZygoteClasses() + 100; |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2318 | } |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 2319 | ObjPtr<mirror::Class> array_of_class = GetClassRoot<mirror::ObjectArray<mirror::Class>>(this); |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2320 | classes.Assign( |
| 2321 | mirror::ObjectArray<mirror::Class>::Alloc(self, array_of_class, class_table_size)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 2322 | CHECK(classes != nullptr); // OOME. |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2323 | GetClassInToObjectArray accumulator(classes.Get()); |
| 2324 | VisitClasses(&accumulator); |
| 2325 | if (accumulator.Succeeded()) { |
| 2326 | break; |
| 2327 | } |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2328 | } |
| 2329 | for (int32_t i = 0; i < classes->GetLength(); ++i) { |
| 2330 | // If the class table shrank during creation of the clases array we expect null elements. If |
| 2331 | // the class table grew then the loop repeats. If classes are created after the loop has |
| 2332 | // finished then we don't visit. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2333 | ObjPtr<mirror::Class> klass = classes->Get(i); |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 2334 | if (klass != nullptr && !visitor->operator()(klass)) { |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2335 | return; |
| 2336 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2337 | } |
| 2338 | } |
| 2339 | } |
| 2340 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 2341 | ClassLinker::~ClassLinker() { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2342 | Thread* const self = Thread::Current(); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 2343 | for (const ClassLoaderData& data : class_loaders_) { |
Alexey Grebenkin | be4c2bd | 2018-02-01 19:09:59 +0300 | [diff] [blame] | 2344 | // CHA unloading analysis is not needed. No negative consequences are expected because |
| 2345 | // all the classloaders are deleted at the same time. |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 2346 | DeleteClassLoader(self, data, /*cleanup_cha=*/ false); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 2347 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2348 | class_loaders_.clear(); |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 2349 | while (!running_visibly_initialized_callbacks_.empty()) { |
| 2350 | std::unique_ptr<VisiblyInitializedCallback> callback( |
| 2351 | std::addressof(running_visibly_initialized_callbacks_.front())); |
| 2352 | running_visibly_initialized_callbacks_.pop_front(); |
| 2353 | } |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 2354 | } |
| 2355 | |
Alexey Grebenkin | be4c2bd | 2018-02-01 19:09:59 +0300 | [diff] [blame] | 2356 | void ClassLinker::DeleteClassLoader(Thread* self, const ClassLoaderData& data, bool cleanup_cha) { |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 2357 | Runtime* const runtime = Runtime::Current(); |
| 2358 | JavaVMExt* const vm = runtime->GetJavaVM(); |
| 2359 | vm->DeleteWeakGlobalRef(self, data.weak_root); |
Calin Juravle | e5de54c | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 2360 | // 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] | 2361 | if (runtime->GetJit() != nullptr) { |
| 2362 | jit::JitCodeCache* code_cache = runtime->GetJit()->GetCodeCache(); |
| 2363 | if (code_cache != nullptr) { |
Mathieu Chartier | cf79cf5 | 2017-07-21 11:17:57 -0700 | [diff] [blame] | 2364 | // For the JIT case, RemoveMethodsIn removes the CHA dependencies. |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 2365 | code_cache->RemoveMethodsIn(self, *data.allocator); |
| 2366 | } |
Andreas Gampe | c1ac9ee | 2017-07-24 22:35:49 -0700 | [diff] [blame] | 2367 | } else if (cha_ != nullptr) { |
Mathieu Chartier | cf79cf5 | 2017-07-21 11:17:57 -0700 | [diff] [blame] | 2368 | // If we don't have a JIT, we need to manually remove the CHA dependencies manually. |
Andreas Gampe | c1ac9ee | 2017-07-24 22:35:49 -0700 | [diff] [blame] | 2369 | cha_->RemoveDependenciesForLinearAlloc(data.allocator); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 2370 | } |
Alexey Grebenkin | be4c2bd | 2018-02-01 19:09:59 +0300 | [diff] [blame] | 2371 | // Cleanup references to single implementation ArtMethods that will be deleted. |
| 2372 | if (cleanup_cha) { |
| 2373 | CHAOnDeleteUpdateClassVisitor visitor(data.allocator); |
Vladimir Marko | 5f958f6 | 2022-02-08 12:01:07 +0000 | [diff] [blame] | 2374 | data.class_table->Visit<kWithoutReadBarrier>(visitor); |
Alexey Grebenkin | be4c2bd | 2018-02-01 19:09:59 +0300 | [diff] [blame] | 2375 | } |
Vladimir Marko | 86c8752 | 2020-05-11 16:55:55 +0100 | [diff] [blame] | 2376 | { |
| 2377 | MutexLock lock(self, critical_native_code_with_clinit_check_lock_); |
| 2378 | auto end = critical_native_code_with_clinit_check_.end(); |
| 2379 | for (auto it = critical_native_code_with_clinit_check_.begin(); it != end; ) { |
| 2380 | if (data.allocator->ContainsUnsafe(it->first)) { |
| 2381 | it = critical_native_code_with_clinit_check_.erase(it); |
| 2382 | } else { |
| 2383 | ++it; |
| 2384 | } |
| 2385 | } |
| 2386 | } |
Alexey Grebenkin | be4c2bd | 2018-02-01 19:09:59 +0300 | [diff] [blame] | 2387 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 2388 | delete data.allocator; |
| 2389 | delete data.class_table; |
| 2390 | } |
| 2391 | |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 2392 | ObjPtr<mirror::PointerArray> ClassLinker::AllocPointerArray(Thread* self, size_t length) { |
| 2393 | return ObjPtr<mirror::PointerArray>::DownCast( |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 2394 | image_pointer_size_ == PointerSize::k64 |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 2395 | ? ObjPtr<mirror::Array>(mirror::LongArray::Alloc(self, length)) |
| 2396 | : ObjPtr<mirror::Array>(mirror::IntArray::Alloc(self, length))); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2397 | } |
| 2398 | |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 2399 | ObjPtr<mirror::DexCache> ClassLinker::AllocDexCache(Thread* self, const DexFile& dex_file) { |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2400 | StackHandleScope<1> hs(self); |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 2401 | auto dex_cache(hs.NewHandle(ObjPtr<mirror::DexCache>::DownCast( |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 2402 | GetClassRoot<mirror::DexCache>(this)->AllocObject(self)))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 2403 | if (dex_cache == nullptr) { |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2404 | self->AssertPendingOOMException(); |
| 2405 | return nullptr; |
| 2406 | } |
Vladimir Marko | 31c3daa | 2019-06-13 12:18:37 +0100 | [diff] [blame] | 2407 | // Use InternWeak() so that the location String can be collected when the ClassLoader |
| 2408 | // with this DexCache is collected. |
| 2409 | ObjPtr<mirror::String> location = intern_table_->InternWeak(dex_file.GetLocation().c_str()); |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2410 | if (location == nullptr) { |
| 2411 | self->AssertPendingOOMException(); |
| 2412 | return nullptr; |
| 2413 | } |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 2414 | dex_cache->SetLocation(location); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2415 | return dex_cache.Get(); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 2416 | } |
| 2417 | |
David Srbecky | 33df0e3 | 2021-09-30 14:36:32 +0000 | [diff] [blame] | 2418 | ObjPtr<mirror::DexCache> ClassLinker::AllocAndInitializeDexCache( |
| 2419 | Thread* self, const DexFile& dex_file, ObjPtr<mirror::ClassLoader> class_loader) { |
| 2420 | StackHandleScope<1> hs(self); |
| 2421 | Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader)); |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 2422 | ObjPtr<mirror::DexCache> dex_cache = AllocDexCache(self, dex_file); |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2423 | if (dex_cache != nullptr) { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 2424 | WriterMutexLock mu(self, *Locks::dex_lock_); |
David Srbecky | 33df0e3 | 2021-09-30 14:36:32 +0000 | [diff] [blame] | 2425 | dex_cache->Initialize(&dex_file, h_class_loader.Get()); |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2426 | } |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 2427 | return dex_cache; |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2428 | } |
| 2429 | |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 2430 | template <bool kMovable, typename PreFenceVisitor> |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 2431 | ObjPtr<mirror::Class> ClassLinker::AllocClass(Thread* self, |
| 2432 | ObjPtr<mirror::Class> java_lang_Class, |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 2433 | uint32_t class_size, |
| 2434 | const PreFenceVisitor& pre_fence_visitor) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2435 | DCHECK_GE(class_size, sizeof(mirror::Class)); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 2436 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 2437 | ObjPtr<mirror::Object> k = (kMovingClasses && kMovable) ? |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 2438 | heap->AllocObject(self, java_lang_Class, class_size, pre_fence_visitor) : |
| 2439 | heap->AllocNonMovableObject(self, java_lang_Class, class_size, pre_fence_visitor); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 2440 | if (UNLIKELY(k == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2441 | self->AssertPendingOOMException(); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 2442 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2443 | } |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 2444 | return k->AsClass(); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 2445 | } |
| 2446 | |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 2447 | template <bool kMovable> |
| 2448 | ObjPtr<mirror::Class> ClassLinker::AllocClass(Thread* self, |
| 2449 | ObjPtr<mirror::Class> java_lang_Class, |
| 2450 | uint32_t class_size) { |
| 2451 | mirror::Class::InitializeClassVisitor visitor(class_size); |
| 2452 | return AllocClass<kMovable>(self, java_lang_Class, class_size, visitor); |
| 2453 | } |
| 2454 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 2455 | ObjPtr<mirror::Class> ClassLinker::AllocClass(Thread* self, uint32_t class_size) { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 2456 | return AllocClass(self, GetClassRoot<mirror::Class>(this), class_size); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 2457 | } |
| 2458 | |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 2459 | void ClassLinker::AllocPrimitiveArrayClass(Thread* self, |
| 2460 | ClassRoot primitive_root, |
| 2461 | ClassRoot array_root) { |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 2462 | // We make this class non-movable for the unlikely case where it were to be |
| 2463 | // moved by a sticky-bit (minor) collection when using the Generational |
| 2464 | // Concurrent Copying (CC) collector, potentially creating a stale reference |
| 2465 | // in the `klass_` field of one of its instances allocated in the Large-Object |
| 2466 | // Space (LOS) -- see the comment about the dirty card scanning logic in |
| 2467 | // art::gc::collector::ConcurrentCopying::MarkingPhase. |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 2468 | ObjPtr<mirror::Class> array_class = AllocClass</* kMovable= */ false>( |
| 2469 | self, GetClassRoot<mirror::Class>(this), mirror::Array::ClassSize(image_pointer_size_)); |
| 2470 | ObjPtr<mirror::Class> component_type = GetClassRoot(primitive_root, this); |
| 2471 | DCHECK(component_type->IsPrimitive()); |
| 2472 | array_class->SetComponentType(component_type); |
| 2473 | SetClassRoot(array_root, array_class); |
| 2474 | } |
| 2475 | |
| 2476 | void ClassLinker::FinishArrayClassSetup(ObjPtr<mirror::Class> array_class) { |
| 2477 | ObjPtr<mirror::Class> java_lang_Object = GetClassRoot<mirror::Object>(this); |
| 2478 | array_class->SetSuperClass(java_lang_Object); |
| 2479 | array_class->SetVTable(java_lang_Object->GetVTable()); |
| 2480 | array_class->SetPrimitiveType(Primitive::kPrimNot); |
| 2481 | ObjPtr<mirror::Class> component_type = array_class->GetComponentType(); |
| 2482 | array_class->SetClassFlags(component_type->IsPrimitive() |
| 2483 | ? mirror::kClassFlagNoReferenceFields |
| 2484 | : mirror::kClassFlagObjectArray); |
| 2485 | array_class->SetClassLoader(component_type->GetClassLoader()); |
| 2486 | array_class->SetStatusForPrimitiveOrArray(ClassStatus::kLoaded); |
| 2487 | array_class->PopulateEmbeddedVTable(image_pointer_size_); |
| 2488 | ImTable* object_imt = java_lang_Object->GetImt(image_pointer_size_); |
| 2489 | array_class->SetImt(object_imt, image_pointer_size_); |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 2490 | DCHECK_EQ(array_class->NumMethods(), 0u); |
| 2491 | |
| 2492 | // don't need to set new_class->SetObjectSize(..) |
| 2493 | // because Object::SizeOf delegates to Array::SizeOf |
| 2494 | |
| 2495 | // All arrays have java/lang/Cloneable and java/io/Serializable as |
| 2496 | // interfaces. We need to set that up here, so that stuff like |
| 2497 | // "instanceof" works right. |
| 2498 | |
| 2499 | // Use the single, global copies of "interfaces" and "iftable" |
| 2500 | // (remember not to free them for arrays). |
| 2501 | { |
| 2502 | ObjPtr<mirror::IfTable> array_iftable = GetArrayIfTable(); |
| 2503 | CHECK(array_iftable != nullptr); |
| 2504 | array_class->SetIfTable(array_iftable); |
| 2505 | } |
| 2506 | |
| 2507 | // Inherit access flags from the component type. |
| 2508 | int access_flags = component_type->GetAccessFlags(); |
| 2509 | // Lose any implementation detail flags; in particular, arrays aren't finalizable. |
| 2510 | access_flags &= kAccJavaFlagsMask; |
| 2511 | // Arrays can't be used as a superclass or interface, so we want to add "abstract final" |
| 2512 | // and remove "interface". |
| 2513 | access_flags |= kAccAbstract | kAccFinal; |
| 2514 | access_flags &= ~kAccInterface; |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 2515 | |
Vladimir Marko | b68bb7a | 2020-03-17 10:55:25 +0000 | [diff] [blame] | 2516 | array_class->SetAccessFlagsDuringLinking(access_flags); |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 2517 | |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 2518 | // Array classes are fully initialized either during single threaded startup, |
| 2519 | // or from a pre-fence visitor, so visibly initialized. |
| 2520 | array_class->SetStatusForPrimitiveOrArray(ClassStatus::kVisiblyInitialized); |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 2521 | } |
| 2522 | |
| 2523 | void ClassLinker::FinishCoreArrayClassSetup(ClassRoot array_root) { |
| 2524 | // Do not hold lock on the array class object, the initialization of |
| 2525 | // core array classes is done while the process is still single threaded. |
| 2526 | ObjPtr<mirror::Class> array_class = GetClassRoot(array_root, this); |
| 2527 | FinishArrayClassSetup(array_class); |
| 2528 | |
| 2529 | std::string temp; |
| 2530 | const char* descriptor = array_class->GetDescriptor(&temp); |
| 2531 | size_t hash = ComputeModifiedUtf8Hash(descriptor); |
| 2532 | ObjPtr<mirror::Class> existing = InsertClass(descriptor, array_class, hash); |
| 2533 | CHECK(existing == nullptr); |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 2534 | } |
| 2535 | |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 2536 | ObjPtr<mirror::ObjectArray<mirror::StackTraceElement>> ClassLinker::AllocStackTraceElementArray( |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 2537 | Thread* self, |
| 2538 | size_t length) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2539 | return mirror::ObjectArray<mirror::StackTraceElement>::Alloc( |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 2540 | self, GetClassRoot<mirror::ObjectArray<mirror::StackTraceElement>>(this), length); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 2541 | } |
| 2542 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 2543 | ObjPtr<mirror::Class> ClassLinker::EnsureResolved(Thread* self, |
| 2544 | const char* descriptor, |
| 2545 | ObjPtr<mirror::Class> klass) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2546 | DCHECK(klass != nullptr); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2547 | if (kIsDebugBuild) { |
| 2548 | StackHandleScope<1> hs(self); |
| 2549 | HandleWrapperObjPtr<mirror::Class> h = hs.NewHandleWrapper(&klass); |
| 2550 | Thread::PoisonObjectPointersIfDebug(); |
| 2551 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2552 | |
| 2553 | // For temporary classes we must wait for them to be retired. |
| 2554 | if (init_done_ && klass->IsTemp()) { |
| 2555 | CHECK(!klass->IsResolved()); |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2556 | if (klass->IsErroneousUnresolved()) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2557 | ThrowEarlierClassFailure(klass); |
| 2558 | return nullptr; |
| 2559 | } |
| 2560 | StackHandleScope<1> hs(self); |
| 2561 | Handle<mirror::Class> h_class(hs.NewHandle(klass)); |
| 2562 | ObjectLock<mirror::Class> lock(self, h_class); |
| 2563 | // Loop and wait for the resolving thread to retire this class. |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2564 | while (!h_class->IsRetired() && !h_class->IsErroneousUnresolved()) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2565 | lock.WaitIgnoringInterrupts(); |
| 2566 | } |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2567 | if (h_class->IsErroneousUnresolved()) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2568 | ThrowEarlierClassFailure(h_class.Get()); |
| 2569 | return nullptr; |
| 2570 | } |
| 2571 | CHECK(h_class->IsRetired()); |
| 2572 | // Get the updated class from class table. |
Andreas Gampe | 34ee684 | 2014-12-02 15:43:52 -0800 | [diff] [blame] | 2573 | klass = LookupClass(self, descriptor, h_class.Get()->GetClassLoader()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2574 | } |
| 2575 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2576 | // Wait for the class if it has not already been linked. |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 2577 | size_t index = 0; |
| 2578 | // Maximum number of yield iterations until we start sleeping. |
| 2579 | static const size_t kNumYieldIterations = 1000; |
| 2580 | // How long each sleep is in us. |
| 2581 | static const size_t kSleepDurationUS = 1000; // 1 ms. |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2582 | while (!klass->IsResolved() && !klass->IsErroneousUnresolved()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2583 | StackHandleScope<1> hs(self); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2584 | HandleWrapperObjPtr<mirror::Class> h_class(hs.NewHandleWrapper(&klass)); |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 2585 | { |
| 2586 | ObjectTryLock<mirror::Class> lock(self, h_class); |
| 2587 | // Can not use a monitor wait here since it may block when returning and deadlock if another |
| 2588 | // thread has locked klass. |
| 2589 | if (lock.Acquired()) { |
| 2590 | // Check for circular dependencies between classes, the lock is required for SetStatus. |
| 2591 | if (!h_class->IsResolved() && h_class->GetClinitThreadId() == self->GetTid()) { |
| 2592 | ThrowClassCircularityError(h_class.Get()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 2593 | mirror::Class::SetStatus(h_class, ClassStatus::kErrorUnresolved, self); |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 2594 | return nullptr; |
| 2595 | } |
| 2596 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2597 | } |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 2598 | { |
| 2599 | // Handle wrapper deals with klass moving. |
Vladimir Marko | ddf4fd3 | 2021-11-22 16:31:57 +0000 | [diff] [blame] | 2600 | ScopedThreadSuspension sts(self, ThreadState::kSuspended); |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 2601 | if (index < kNumYieldIterations) { |
| 2602 | sched_yield(); |
| 2603 | } else { |
| 2604 | usleep(kSleepDurationUS); |
| 2605 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2606 | } |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 2607 | ++index; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2608 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2609 | |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2610 | if (klass->IsErroneousUnresolved()) { |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 2611 | ThrowEarlierClassFailure(klass); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 2612 | return nullptr; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2613 | } |
| 2614 | // Return the loaded class. No exceptions should be pending. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 2615 | CHECK(klass->IsResolved()) << klass->PrettyClass(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2616 | self->AssertNoPendingException(); |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 2617 | return klass; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2618 | } |
| 2619 | |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 2620 | using ClassPathEntry = std::pair<const DexFile*, const dex::ClassDef*>; |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2621 | |
| 2622 | // Search a collection of DexFiles for a descriptor |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2623 | ClassPathEntry FindInClassPath(const char* descriptor, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 2624 | size_t hash, const std::vector<const DexFile*>& class_path) { |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2625 | for (const DexFile* dex_file : class_path) { |
Mathieu Chartier | 0a19e21 | 2019-11-27 14:35:24 -0800 | [diff] [blame] | 2626 | DCHECK(dex_file != nullptr); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 2627 | const dex::ClassDef* dex_class_def = OatDexFile::FindClassDef(*dex_file, descriptor, hash); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2628 | if (dex_class_def != nullptr) { |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2629 | return ClassPathEntry(dex_file, dex_class_def); |
| 2630 | } |
| 2631 | } |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2632 | return ClassPathEntry(nullptr, nullptr); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2633 | } |
| 2634 | |
Nicolas Geoffray | e8445e5 | 2021-09-23 14:10:05 +0100 | [diff] [blame] | 2635 | // Helper macro to make sure each class loader lookup call handles the case the |
| 2636 | // class loader is not recognized, or the lookup threw an exception. |
| 2637 | #define RETURN_IF_UNRECOGNIZED_OR_FOUND_OR_EXCEPTION(call_, result_, thread_) \ |
| 2638 | do { \ |
| 2639 | auto local_call = call_; \ |
| 2640 | if (!local_call) { \ |
| 2641 | return false; \ |
| 2642 | } \ |
| 2643 | auto local_result = result_; \ |
| 2644 | if (local_result != nullptr) { \ |
| 2645 | return true; \ |
| 2646 | } \ |
| 2647 | auto local_thread = thread_; \ |
| 2648 | if (local_thread->IsExceptionPending()) { \ |
| 2649 | /* Pending exception means there was an error other than */ \ |
| 2650 | /* ClassNotFound that must be returned to the caller. */ \ |
| 2651 | return false; \ |
| 2652 | } \ |
| 2653 | } while (0) |
| 2654 | |
Nicolas Geoffray | 80a560c | 2018-10-26 13:48:51 +0100 | [diff] [blame] | 2655 | bool ClassLinker::FindClassInSharedLibraries(ScopedObjectAccessAlreadyRunnable& soa, |
| 2656 | Thread* self, |
| 2657 | const char* descriptor, |
| 2658 | size_t hash, |
| 2659 | Handle<mirror::ClassLoader> class_loader, |
| 2660 | /*out*/ ObjPtr<mirror::Class>* result) { |
| 2661 | ArtField* field = |
| 2662 | jni::DecodeArtField(WellKnownClasses::dalvik_system_BaseDexClassLoader_sharedLibraryLoaders); |
Brad Stenning | 9c924e8 | 2021-10-11 19:09:00 -0700 | [diff] [blame] | 2663 | return FindClassInSharedLibrariesHelper(soa, self, descriptor, hash, class_loader, field, result); |
| 2664 | } |
| 2665 | |
| 2666 | bool ClassLinker::FindClassInSharedLibrariesHelper(ScopedObjectAccessAlreadyRunnable& soa, |
| 2667 | Thread* self, |
| 2668 | const char* descriptor, |
| 2669 | size_t hash, |
| 2670 | Handle<mirror::ClassLoader> class_loader, |
| 2671 | ArtField* field, |
| 2672 | /*out*/ ObjPtr<mirror::Class>* result) { |
Nicolas Geoffray | 80a560c | 2018-10-26 13:48:51 +0100 | [diff] [blame] | 2673 | ObjPtr<mirror::Object> raw_shared_libraries = field->GetObject(class_loader.Get()); |
| 2674 | if (raw_shared_libraries == nullptr) { |
| 2675 | return true; |
| 2676 | } |
| 2677 | |
| 2678 | StackHandleScope<2> hs(self); |
| 2679 | Handle<mirror::ObjectArray<mirror::ClassLoader>> shared_libraries( |
| 2680 | hs.NewHandle(raw_shared_libraries->AsObjectArray<mirror::ClassLoader>())); |
| 2681 | MutableHandle<mirror::ClassLoader> temp_loader = hs.NewHandle<mirror::ClassLoader>(nullptr); |
Alex Light | a9bbc08 | 2019-11-14 14:51:41 -0800 | [diff] [blame] | 2682 | for (auto loader : shared_libraries.Iterate<mirror::ClassLoader>()) { |
| 2683 | temp_loader.Assign(loader); |
Nicolas Geoffray | e8445e5 | 2021-09-23 14:10:05 +0100 | [diff] [blame] | 2684 | RETURN_IF_UNRECOGNIZED_OR_FOUND_OR_EXCEPTION( |
| 2685 | FindClassInBaseDexClassLoader(soa, self, descriptor, hash, temp_loader, result), |
| 2686 | *result, |
| 2687 | self); |
Nicolas Geoffray | 80a560c | 2018-10-26 13:48:51 +0100 | [diff] [blame] | 2688 | } |
| 2689 | return true; |
| 2690 | } |
| 2691 | |
Brad Stenning | 9c924e8 | 2021-10-11 19:09:00 -0700 | [diff] [blame] | 2692 | bool ClassLinker::FindClassInSharedLibrariesAfter(ScopedObjectAccessAlreadyRunnable& soa, |
| 2693 | Thread* self, |
| 2694 | const char* descriptor, |
| 2695 | size_t hash, |
| 2696 | Handle<mirror::ClassLoader> class_loader, |
| 2697 | /*out*/ ObjPtr<mirror::Class>* result) { |
| 2698 | ArtField* field = jni::DecodeArtField( |
| 2699 | WellKnownClasses::dalvik_system_BaseDexClassLoader_sharedLibraryLoadersAfter); |
| 2700 | return FindClassInSharedLibrariesHelper(soa, self, descriptor, hash, class_loader, field, result); |
| 2701 | } |
| 2702 | |
Nicolas Geoffray | 7d8d8ff | 2016-11-02 12:38:05 +0000 | [diff] [blame] | 2703 | bool ClassLinker::FindClassInBaseDexClassLoader(ScopedObjectAccessAlreadyRunnable& soa, |
| 2704 | Thread* self, |
| 2705 | const char* descriptor, |
| 2706 | size_t hash, |
| 2707 | Handle<mirror::ClassLoader> class_loader, |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 2708 | /*out*/ ObjPtr<mirror::Class>* result) { |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 2709 | // Termination case: boot class loader. |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2710 | if (IsBootClassLoader(soa, class_loader.Get())) { |
Nicolas Geoffray | e8445e5 | 2021-09-23 14:10:05 +0100 | [diff] [blame] | 2711 | RETURN_IF_UNRECOGNIZED_OR_FOUND_OR_EXCEPTION( |
| 2712 | FindClassInBootClassLoaderClassPath(self, descriptor, hash, result), *result, self); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2713 | return true; |
| 2714 | } |
| 2715 | |
David Brazdil | 05909d8 | 2018-12-06 16:25:16 +0000 | [diff] [blame] | 2716 | if (IsPathOrDexClassLoader(soa, class_loader) || IsInMemoryDexClassLoader(soa, class_loader)) { |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 2717 | // For regular path or dex class loader the search order is: |
| 2718 | // - parent |
Nicolas Geoffray | 80a560c | 2018-10-26 13:48:51 +0100 | [diff] [blame] | 2719 | // - shared libraries |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 2720 | // - class loader dex files |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2721 | |
Nicolas Geoffray | e8445e5 | 2021-09-23 14:10:05 +0100 | [diff] [blame] | 2722 | // 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] | 2723 | StackHandleScope<1> hs(self); |
| 2724 | Handle<mirror::ClassLoader> h_parent(hs.NewHandle(class_loader->GetParent())); |
Nicolas Geoffray | e8445e5 | 2021-09-23 14:10:05 +0100 | [diff] [blame] | 2725 | RETURN_IF_UNRECOGNIZED_OR_FOUND_OR_EXCEPTION( |
| 2726 | FindClassInBaseDexClassLoader(soa, self, descriptor, hash, h_parent, result), |
| 2727 | *result, |
| 2728 | self); |
| 2729 | RETURN_IF_UNRECOGNIZED_OR_FOUND_OR_EXCEPTION( |
| 2730 | FindClassInSharedLibraries(soa, self, descriptor, hash, class_loader, result), |
| 2731 | *result, |
| 2732 | self); |
| 2733 | RETURN_IF_UNRECOGNIZED_OR_FOUND_OR_EXCEPTION( |
| 2734 | FindClassInBaseDexClassLoaderClassPath(soa, descriptor, hash, class_loader, result), |
| 2735 | *result, |
| 2736 | self); |
Brad Stenning | 9c924e8 | 2021-10-11 19:09:00 -0700 | [diff] [blame] | 2737 | RETURN_IF_UNRECOGNIZED_OR_FOUND_OR_EXCEPTION( |
| 2738 | FindClassInSharedLibrariesAfter(soa, self, descriptor, hash, class_loader, result), |
| 2739 | *result, |
| 2740 | self); |
Nicolas Geoffray | e8445e5 | 2021-09-23 14:10:05 +0100 | [diff] [blame] | 2741 | // We did not find a class, but the class loader chain was recognized, so we |
| 2742 | // return true. |
| 2743 | return true; |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2744 | } |
| 2745 | |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 2746 | if (IsDelegateLastClassLoader(soa, class_loader)) { |
| 2747 | // For delegate last, the search order is: |
| 2748 | // - boot class path |
Nicolas Geoffray | 80a560c | 2018-10-26 13:48:51 +0100 | [diff] [blame] | 2749 | // - shared libraries |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 2750 | // - class loader dex files |
| 2751 | // - parent |
Nicolas Geoffray | e8445e5 | 2021-09-23 14:10:05 +0100 | [diff] [blame] | 2752 | RETURN_IF_UNRECOGNIZED_OR_FOUND_OR_EXCEPTION( |
| 2753 | FindClassInBootClassLoaderClassPath(self, descriptor, hash, result), *result, self); |
| 2754 | RETURN_IF_UNRECOGNIZED_OR_FOUND_OR_EXCEPTION( |
| 2755 | FindClassInSharedLibraries(soa, self, descriptor, hash, class_loader, result), |
| 2756 | *result, |
| 2757 | self); |
| 2758 | RETURN_IF_UNRECOGNIZED_OR_FOUND_OR_EXCEPTION( |
| 2759 | FindClassInBaseDexClassLoaderClassPath(soa, descriptor, hash, class_loader, result), |
| 2760 | *result, |
| 2761 | self); |
Brad Stenning | 9c924e8 | 2021-10-11 19:09:00 -0700 | [diff] [blame] | 2762 | RETURN_IF_UNRECOGNIZED_OR_FOUND_OR_EXCEPTION( |
| 2763 | FindClassInSharedLibrariesAfter(soa, self, descriptor, hash, class_loader, result), |
| 2764 | *result, |
| 2765 | self); |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 2766 | |
Nicolas Geoffray | e8445e5 | 2021-09-23 14:10:05 +0100 | [diff] [blame] | 2767 | // 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] | 2768 | StackHandleScope<1> hs(self); |
| 2769 | Handle<mirror::ClassLoader> h_parent(hs.NewHandle(class_loader->GetParent())); |
Nicolas Geoffray | e8445e5 | 2021-09-23 14:10:05 +0100 | [diff] [blame] | 2770 | RETURN_IF_UNRECOGNIZED_OR_FOUND_OR_EXCEPTION( |
| 2771 | FindClassInBaseDexClassLoader(soa, self, descriptor, hash, h_parent, result), |
| 2772 | *result, |
| 2773 | self); |
| 2774 | // We did not find a class, but the class loader chain was recognized, so we |
| 2775 | // return true. |
| 2776 | return true; |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 2777 | } |
| 2778 | |
| 2779 | // Unsupported class loader. |
| 2780 | *result = nullptr; |
| 2781 | return false; |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 2782 | } |
| 2783 | |
Nicolas Geoffray | e8445e5 | 2021-09-23 14:10:05 +0100 | [diff] [blame] | 2784 | #undef RETURN_IF_UNRECOGNIZED_OR_FOUND_OR_EXCEPTION |
| 2785 | |
Andreas Gampe | 501c3b0 | 2019-04-17 21:54:27 +0000 | [diff] [blame] | 2786 | namespace { |
| 2787 | |
| 2788 | // Matches exceptions caught in DexFile.defineClass. |
| 2789 | ALWAYS_INLINE bool MatchesDexFileCaughtExceptions(ObjPtr<mirror::Throwable> throwable, |
| 2790 | ClassLinker* class_linker) |
| 2791 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 2792 | return |
| 2793 | // ClassNotFoundException. |
| 2794 | throwable->InstanceOf(GetClassRoot(ClassRoot::kJavaLangClassNotFoundException, |
| 2795 | class_linker)) |
| 2796 | || |
| 2797 | // NoClassDefFoundError. TODO: Reconsider this. b/130746382. |
| 2798 | throwable->InstanceOf(Runtime::Current()->GetPreAllocatedNoClassDefFoundError()->GetClass()); |
| 2799 | } |
| 2800 | |
| 2801 | // Clear exceptions caught in DexFile.defineClass. |
| 2802 | ALWAYS_INLINE void FilterDexFileCaughtExceptions(Thread* self, ClassLinker* class_linker) |
| 2803 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 2804 | if (MatchesDexFileCaughtExceptions(self->GetException(), class_linker)) { |
| 2805 | self->ClearException(); |
| 2806 | } |
| 2807 | } |
| 2808 | |
| 2809 | } // namespace |
| 2810 | |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 2811 | // Finds the class in the boot class loader. |
| 2812 | // 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] | 2813 | bool ClassLinker::FindClassInBootClassLoaderClassPath(Thread* self, |
| 2814 | const char* descriptor, |
| 2815 | size_t hash, |
| 2816 | /*out*/ ObjPtr<mirror::Class>* result) { |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 2817 | ClassPathEntry pair = FindInClassPath(descriptor, hash, boot_class_path_); |
| 2818 | if (pair.second != nullptr) { |
| 2819 | ObjPtr<mirror::Class> klass = LookupClass(self, descriptor, hash, nullptr); |
| 2820 | if (klass != nullptr) { |
Nicolas Geoffray | e8445e5 | 2021-09-23 14:10:05 +0100 | [diff] [blame] | 2821 | *result = EnsureResolved(self, descriptor, klass); |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2822 | } else { |
Nicolas Geoffray | e8445e5 | 2021-09-23 14:10:05 +0100 | [diff] [blame] | 2823 | *result = DefineClass(self, |
| 2824 | descriptor, |
| 2825 | hash, |
| 2826 | ScopedNullHandle<mirror::ClassLoader>(), |
| 2827 | *pair.first, |
| 2828 | *pair.second); |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2829 | } |
Nicolas Geoffray | e8445e5 | 2021-09-23 14:10:05 +0100 | [diff] [blame] | 2830 | if (*result == nullptr) { |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 2831 | CHECK(self->IsExceptionPending()) << descriptor; |
Andreas Gampe | 501c3b0 | 2019-04-17 21:54:27 +0000 | [diff] [blame] | 2832 | FilterDexFileCaughtExceptions(self, this); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2833 | } |
| 2834 | } |
Nicolas Geoffray | e8445e5 | 2021-09-23 14:10:05 +0100 | [diff] [blame] | 2835 | // The boot classloader is always a known lookup. |
| 2836 | return true; |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 2837 | } |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2838 | |
Nicolas Geoffray | e8445e5 | 2021-09-23 14:10:05 +0100 | [diff] [blame] | 2839 | bool ClassLinker::FindClassInBaseDexClassLoaderClassPath( |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 2840 | ScopedObjectAccessAlreadyRunnable& soa, |
| 2841 | const char* descriptor, |
| 2842 | size_t hash, |
Nicolas Geoffray | e8445e5 | 2021-09-23 14:10:05 +0100 | [diff] [blame] | 2843 | Handle<mirror::ClassLoader> class_loader, |
| 2844 | /*out*/ ObjPtr<mirror::Class>* result) { |
David Brazdil | 05909d8 | 2018-12-06 16:25:16 +0000 | [diff] [blame] | 2845 | DCHECK(IsPathOrDexClassLoader(soa, class_loader) || |
| 2846 | IsInMemoryDexClassLoader(soa, class_loader) || |
| 2847 | IsDelegateLastClassLoader(soa, class_loader)) |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 2848 | << "Unexpected class loader for descriptor " << descriptor; |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2849 | |
Vladimir Marko | 68c0758 | 2021-04-19 16:01:15 +0000 | [diff] [blame] | 2850 | const DexFile* dex_file = nullptr; |
| 2851 | const dex::ClassDef* class_def = nullptr; |
Andreas Gampe | b8e7c37 | 2018-02-20 18:24:55 -0800 | [diff] [blame] | 2852 | ObjPtr<mirror::Class> ret; |
Vladimir Marko | 68c0758 | 2021-04-19 16:01:15 +0000 | [diff] [blame] | 2853 | auto find_class_def = [&](const DexFile* cp_dex_file) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 2854 | const dex::ClassDef* cp_class_def = OatDexFile::FindClassDef(*cp_dex_file, descriptor, hash); |
| 2855 | if (cp_class_def != nullptr) { |
| 2856 | dex_file = cp_dex_file; |
| 2857 | class_def = cp_class_def; |
| 2858 | return false; // Found a class definition, stop visit. |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2859 | } |
Andreas Gampe | b8e7c37 | 2018-02-20 18:24:55 -0800 | [diff] [blame] | 2860 | return true; // Continue with the next DexFile. |
| 2861 | }; |
Vladimir Marko | 68c0758 | 2021-04-19 16:01:15 +0000 | [diff] [blame] | 2862 | VisitClassLoaderDexFiles(soa, class_loader, find_class_def); |
Andreas Gampe | b8e7c37 | 2018-02-20 18:24:55 -0800 | [diff] [blame] | 2863 | |
Vladimir Marko | 68c0758 | 2021-04-19 16:01:15 +0000 | [diff] [blame] | 2864 | if (class_def != nullptr) { |
Nicolas Geoffray | e8445e5 | 2021-09-23 14:10:05 +0100 | [diff] [blame] | 2865 | *result = DefineClass(soa.Self(), descriptor, hash, class_loader, *dex_file, *class_def); |
| 2866 | if (UNLIKELY(*result == nullptr)) { |
Vladimir Marko | 68c0758 | 2021-04-19 16:01:15 +0000 | [diff] [blame] | 2867 | CHECK(soa.Self()->IsExceptionPending()) << descriptor; |
| 2868 | FilterDexFileCaughtExceptions(soa.Self(), this); |
| 2869 | } else { |
| 2870 | DCHECK(!soa.Self()->IsExceptionPending()); |
| 2871 | } |
| 2872 | } |
Nicolas Geoffray | e8445e5 | 2021-09-23 14:10:05 +0100 | [diff] [blame] | 2873 | // A BaseDexClassLoader is always a known lookup. |
| 2874 | return true; |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2875 | } |
| 2876 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 2877 | ObjPtr<mirror::Class> ClassLinker::FindClass(Thread* self, |
| 2878 | const char* descriptor, |
| 2879 | Handle<mirror::ClassLoader> class_loader) { |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 2880 | DCHECK_NE(*descriptor, '\0') << "descriptor is empty string"; |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 2881 | DCHECK(self != nullptr); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2882 | self->AssertNoPendingException(); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 2883 | self->PoisonObjectPointers(); // For DefineClass, CreateArrayClass, etc... |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 2884 | if (descriptor[1] == '\0') { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2885 | // only the descriptors of primitive types should be 1 character long, also avoid class lookup |
| 2886 | // for primitive classes that aren't backed by dex files. |
| 2887 | return FindPrimitiveClass(descriptor[0]); |
| 2888 | } |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2889 | const size_t hash = ComputeModifiedUtf8Hash(descriptor); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2890 | // Find the class in the loaded classes table. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2891 | ObjPtr<mirror::Class> klass = LookupClass(self, descriptor, hash, class_loader.Get()); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2892 | if (klass != nullptr) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2893 | return EnsureResolved(self, descriptor, klass); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2894 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2895 | // Class is not yet loaded. |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 2896 | if (descriptor[0] != '[' && class_loader == nullptr) { |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 2897 | // 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] | 2898 | ClassPathEntry pair = FindInClassPath(descriptor, hash, boot_class_path_); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2899 | if (pair.second != nullptr) { |
Mathieu Chartier | 9865bde | 2015-12-21 09:58:16 -0800 | [diff] [blame] | 2900 | return DefineClass(self, |
| 2901 | descriptor, |
| 2902 | hash, |
| 2903 | ScopedNullHandle<mirror::ClassLoader>(), |
| 2904 | *pair.first, |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2905 | *pair.second); |
Ian Rogers | 6355745 | 2014-06-04 16:57:15 -0700 | [diff] [blame] | 2906 | } else { |
| 2907 | // The boot class loader is searched ahead of the application class loader, failures are |
| 2908 | // expected and will be wrapped in a ClassNotFoundException. Use the pre-allocated error to |
| 2909 | // trigger the chaining with a proper stack trace. |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 2910 | ObjPtr<mirror::Throwable> pre_allocated = |
| 2911 | Runtime::Current()->GetPreAllocatedNoClassDefFoundError(); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2912 | self->SetException(pre_allocated); |
Ian Rogers | 6355745 | 2014-06-04 16:57:15 -0700 | [diff] [blame] | 2913 | return nullptr; |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 2914 | } |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 2915 | } |
| 2916 | ObjPtr<mirror::Class> result_ptr; |
| 2917 | bool descriptor_equals; |
| 2918 | if (descriptor[0] == '[') { |
| 2919 | result_ptr = CreateArrayClass(self, descriptor, hash, class_loader); |
| 2920 | DCHECK_EQ(result_ptr == nullptr, self->IsExceptionPending()); |
| 2921 | DCHECK(result_ptr == nullptr || result_ptr->DescriptorEquals(descriptor)); |
| 2922 | descriptor_equals = true; |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 2923 | } else { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 2924 | ScopedObjectAccessUnchecked soa(self); |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2925 | bool known_hierarchy = |
| 2926 | FindClassInBaseDexClassLoader(soa, self, descriptor, hash, class_loader, &result_ptr); |
| 2927 | if (result_ptr != nullptr) { |
| 2928 | // The chain was understood and we found the class. We still need to add the class to |
| 2929 | // the class table to protect from racy programs that can try and redefine the path list |
| 2930 | // which would change the Class<?> returned for subsequent evaluation of const-class. |
| 2931 | DCHECK(known_hierarchy); |
| 2932 | DCHECK(result_ptr->DescriptorEquals(descriptor)); |
| 2933 | descriptor_equals = true; |
Andreas Gampe | 501c3b0 | 2019-04-17 21:54:27 +0000 | [diff] [blame] | 2934 | } else if (!self->IsExceptionPending()) { |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2935 | // Either the chain wasn't understood or the class wasn't found. |
Andreas Gampe | 501c3b0 | 2019-04-17 21:54:27 +0000 | [diff] [blame] | 2936 | // If there is a pending exception we didn't clear, it is a not a ClassNotFoundException and |
| 2937 | // we should return it instead of silently clearing and retrying. |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2938 | // |
| 2939 | // If the chain was understood but we did not find the class, let the Java-side |
| 2940 | // rediscover all this and throw the exception with the right stack trace. Note that |
| 2941 | // the Java-side could still succeed for racy programs if another thread is actively |
| 2942 | // modifying the class loader's path list. |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2943 | |
Alex Light | 185a461 | 2018-10-04 15:54:25 -0700 | [diff] [blame] | 2944 | // The runtime is not allowed to call into java from a runtime-thread so just abort. |
Alex Light | e9f6103 | 2018-09-24 16:04:51 -0700 | [diff] [blame] | 2945 | if (self->IsRuntimeThread()) { |
Calin Juravle | ccd5695 | 2016-12-15 17:57:38 +0000 | [diff] [blame] | 2946 | // Oops, we can't call into java so we can't run actual class-loader code. |
| 2947 | // This is true for e.g. for the compiler (jit or aot). |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2948 | ObjPtr<mirror::Throwable> pre_allocated = |
| 2949 | Runtime::Current()->GetPreAllocatedNoClassDefFoundError(); |
| 2950 | self->SetException(pre_allocated); |
Vladimir Marko | 2c8c6b6 | 2016-12-01 17:42:00 +0000 | [diff] [blame] | 2951 | return nullptr; |
| 2952 | } |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2953 | |
Vladimir Marko | 5fdd778 | 2017-04-20 11:26:03 +0100 | [diff] [blame] | 2954 | // Inlined DescriptorToDot(descriptor) with extra validation. |
| 2955 | // |
| 2956 | // Throw NoClassDefFoundError early rather than potentially load a class only to fail |
| 2957 | // the DescriptorEquals() check below and give a confusing error message. For example, |
| 2958 | // when native code erroneously calls JNI GetFieldId() with signature "java/lang/String" |
| 2959 | // instead of "Ljava/lang/String;", the message below using the "dot" names would be |
| 2960 | // "class loader [...] returned class java.lang.String instead of java.lang.String". |
| 2961 | size_t descriptor_length = strlen(descriptor); |
| 2962 | if (UNLIKELY(descriptor[0] != 'L') || |
| 2963 | UNLIKELY(descriptor[descriptor_length - 1] != ';') || |
| 2964 | UNLIKELY(memchr(descriptor + 1, '.', descriptor_length - 2) != nullptr)) { |
| 2965 | ThrowNoClassDefFoundError("Invalid descriptor: %s.", descriptor); |
| 2966 | return nullptr; |
| 2967 | } |
Andreas Gampe | 501c3b0 | 2019-04-17 21:54:27 +0000 | [diff] [blame] | 2968 | |
Vladimir Marko | 5fdd778 | 2017-04-20 11:26:03 +0100 | [diff] [blame] | 2969 | std::string class_name_string(descriptor + 1, descriptor_length - 2); |
| 2970 | std::replace(class_name_string.begin(), class_name_string.end(), '/', '.'); |
Andreas Gampe | 87658f3 | 2019-04-18 18:39:02 +0000 | [diff] [blame] | 2971 | if (known_hierarchy && |
| 2972 | fast_class_not_found_exceptions_ && |
| 2973 | !Runtime::Current()->IsJavaDebuggable()) { |
| 2974 | // For known hierarchy, we know that the class is going to throw an exception. If we aren't |
| 2975 | // debuggable, optimize this path by throwing directly here without going back to Java |
| 2976 | // language. This reduces how many ClassNotFoundExceptions happen. |
| 2977 | self->ThrowNewExceptionF("Ljava/lang/ClassNotFoundException;", |
| 2978 | "%s", |
| 2979 | class_name_string.c_str()); |
| 2980 | } else { |
| 2981 | ScopedLocalRef<jobject> class_loader_object( |
| 2982 | soa.Env(), soa.AddLocalReference<jobject>(class_loader.Get())); |
| 2983 | ScopedLocalRef<jobject> result(soa.Env(), nullptr); |
| 2984 | { |
Vladimir Marko | ddf4fd3 | 2021-11-22 16:31:57 +0000 | [diff] [blame] | 2985 | ScopedThreadStateChange tsc(self, ThreadState::kNative); |
Andreas Gampe | 87658f3 | 2019-04-18 18:39:02 +0000 | [diff] [blame] | 2986 | ScopedLocalRef<jobject> class_name_object( |
| 2987 | soa.Env(), soa.Env()->NewStringUTF(class_name_string.c_str())); |
| 2988 | if (class_name_object.get() == nullptr) { |
| 2989 | DCHECK(self->IsExceptionPending()); // OOME. |
| 2990 | return nullptr; |
| 2991 | } |
| 2992 | CHECK(class_loader_object.get() != nullptr); |
| 2993 | result.reset(soa.Env()->CallObjectMethod(class_loader_object.get(), |
| 2994 | WellKnownClasses::java_lang_ClassLoader_loadClass, |
| 2995 | class_name_object.get())); |
| 2996 | } |
| 2997 | if (result.get() == nullptr && !self->IsExceptionPending()) { |
| 2998 | // broken loader - throw NPE to be compatible with Dalvik |
| 2999 | ThrowNullPointerException(StringPrintf("ClassLoader.loadClass returned null for %s", |
| 3000 | class_name_string.c_str()).c_str()); |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 3001 | return nullptr; |
| 3002 | } |
Andreas Gampe | 87658f3 | 2019-04-18 18:39:02 +0000 | [diff] [blame] | 3003 | result_ptr = soa.Decode<mirror::Class>(result.get()); |
| 3004 | // Check the name of the returned class. |
| 3005 | descriptor_equals = (result_ptr != nullptr) && result_ptr->DescriptorEquals(descriptor); |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 3006 | } |
Andreas Gampe | 501c3b0 | 2019-04-17 21:54:27 +0000 | [diff] [blame] | 3007 | } else { |
| 3008 | DCHECK(!MatchesDexFileCaughtExceptions(self->GetException(), this)); |
Vladimir Marko | 2c8c6b6 | 2016-12-01 17:42:00 +0000 | [diff] [blame] | 3009 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3010 | } |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 3011 | |
| 3012 | if (self->IsExceptionPending()) { |
| 3013 | // If the ClassLoader threw or array class allocation failed, pass that exception up. |
| 3014 | // However, to comply with the RI behavior, first check if another thread succeeded. |
| 3015 | result_ptr = LookupClass(self, descriptor, hash, class_loader.Get()); |
| 3016 | if (result_ptr != nullptr && !result_ptr->IsErroneous()) { |
| 3017 | self->ClearException(); |
| 3018 | return EnsureResolved(self, descriptor, result_ptr); |
| 3019 | } |
| 3020 | return nullptr; |
| 3021 | } |
| 3022 | |
| 3023 | // Try to insert the class to the class table, checking for mismatch. |
| 3024 | ObjPtr<mirror::Class> old; |
| 3025 | { |
| 3026 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 3027 | ClassTable* const class_table = InsertClassTableForClassLoader(class_loader.Get()); |
| 3028 | old = class_table->Lookup(descriptor, hash); |
| 3029 | if (old == nullptr) { |
| 3030 | old = result_ptr; // For the comparison below, after releasing the lock. |
| 3031 | if (descriptor_equals) { |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 3032 | class_table->InsertWithHash(result_ptr, hash); |
Mathieu Chartier | 88ea61e | 2018-06-20 17:45:41 -0700 | [diff] [blame] | 3033 | WriteBarrier::ForEveryFieldWrite(class_loader.Get()); |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 3034 | } // else throw below, after releasing the lock. |
| 3035 | } |
| 3036 | } |
| 3037 | if (UNLIKELY(old != result_ptr)) { |
| 3038 | // Return `old` (even if `!descriptor_equals`) to mimic the RI behavior for parallel |
| 3039 | // capable class loaders. (All class loaders are considered parallel capable on Android.) |
Vladimir Marko | dfc0de7 | 2019-04-01 10:57:55 +0100 | [diff] [blame] | 3040 | ObjPtr<mirror::Class> loader_class = class_loader->GetClass(); |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 3041 | const char* loader_class_name = |
| 3042 | loader_class->GetDexFile().StringByTypeIdx(loader_class->GetDexTypeIndex()); |
| 3043 | LOG(WARNING) << "Initiating class loader of type " << DescriptorToDot(loader_class_name) |
| 3044 | << " is not well-behaved; it returned a different Class for racing loadClass(\"" |
| 3045 | << DescriptorToDot(descriptor) << "\")."; |
| 3046 | return EnsureResolved(self, descriptor, old); |
| 3047 | } |
| 3048 | if (UNLIKELY(!descriptor_equals)) { |
| 3049 | std::string result_storage; |
| 3050 | const char* result_name = result_ptr->GetDescriptor(&result_storage); |
| 3051 | std::string loader_storage; |
| 3052 | const char* loader_class_name = class_loader->GetClass()->GetDescriptor(&loader_storage); |
| 3053 | ThrowNoClassDefFoundError( |
| 3054 | "Initiating class loader of type %s returned class %s instead of %s.", |
| 3055 | DescriptorToDot(loader_class_name).c_str(), |
| 3056 | DescriptorToDot(result_name).c_str(), |
| 3057 | DescriptorToDot(descriptor).c_str()); |
| 3058 | return nullptr; |
| 3059 | } |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 3060 | // Success. |
| 3061 | return result_ptr; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3062 | } |
| 3063 | |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 3064 | // Helper for maintaining DefineClass counting. We need to notify callbacks when we start/end a |
| 3065 | // define-class and how many recursive DefineClasses we are at in order to allow for doing things |
| 3066 | // like pausing class definition. |
| 3067 | struct ScopedDefiningClass { |
| 3068 | public: |
| 3069 | explicit ScopedDefiningClass(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_) |
| 3070 | : self_(self), returned_(false) { |
| 3071 | Locks::mutator_lock_->AssertSharedHeld(self_); |
| 3072 | Runtime::Current()->GetRuntimeCallbacks()->BeginDefineClass(); |
| 3073 | self_->IncrDefineClassCount(); |
| 3074 | } |
| 3075 | ~ScopedDefiningClass() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 3076 | Locks::mutator_lock_->AssertSharedHeld(self_); |
| 3077 | CHECK(returned_); |
| 3078 | } |
| 3079 | |
| 3080 | ObjPtr<mirror::Class> Finish(Handle<mirror::Class> h_klass) |
| 3081 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 3082 | CHECK(!returned_); |
| 3083 | self_->DecrDefineClassCount(); |
| 3084 | Runtime::Current()->GetRuntimeCallbacks()->EndDefineClass(); |
| 3085 | Thread::PoisonObjectPointersIfDebug(); |
| 3086 | returned_ = true; |
| 3087 | return h_klass.Get(); |
| 3088 | } |
| 3089 | |
| 3090 | ObjPtr<mirror::Class> Finish(ObjPtr<mirror::Class> klass) |
| 3091 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 3092 | StackHandleScope<1> hs(self_); |
| 3093 | Handle<mirror::Class> h_klass(hs.NewHandle(klass)); |
| 3094 | return Finish(h_klass); |
| 3095 | } |
| 3096 | |
| 3097 | ObjPtr<mirror::Class> Finish(nullptr_t np ATTRIBUTE_UNUSED) |
| 3098 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 3099 | ScopedNullHandle<mirror::Class> snh; |
| 3100 | return Finish(snh); |
| 3101 | } |
| 3102 | |
| 3103 | private: |
| 3104 | Thread* self_; |
| 3105 | bool returned_; |
| 3106 | }; |
| 3107 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 3108 | ObjPtr<mirror::Class> ClassLinker::DefineClass(Thread* self, |
| 3109 | const char* descriptor, |
| 3110 | size_t hash, |
| 3111 | Handle<mirror::ClassLoader> class_loader, |
| 3112 | const DexFile& dex_file, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 3113 | const dex::ClassDef& dex_class_def) { |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 3114 | ScopedDefiningClass sdc(self); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3115 | StackHandleScope<3> hs(self); |
Eric Holk | 74584e6 | 2021-02-18 14:39:17 -0800 | [diff] [blame] | 3116 | metrics::AutoTimer timer{GetMetrics()->ClassLoadingTotalTime()}; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3117 | auto klass = hs.NewHandle<mirror::Class>(nullptr); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3118 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3119 | // Load the class from the dex file. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3120 | if (UNLIKELY(!init_done_)) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3121 | // finish up init of hand crafted class_roots_ |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3122 | if (strcmp(descriptor, "Ljava/lang/Object;") == 0) { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 3123 | klass.Assign(GetClassRoot<mirror::Object>(this)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3124 | } else if (strcmp(descriptor, "Ljava/lang/Class;") == 0) { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 3125 | klass.Assign(GetClassRoot<mirror::Class>(this)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3126 | } else if (strcmp(descriptor, "Ljava/lang/String;") == 0) { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 3127 | klass.Assign(GetClassRoot<mirror::String>(this)); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 3128 | } else if (strcmp(descriptor, "Ljava/lang/ref/Reference;") == 0) { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 3129 | klass.Assign(GetClassRoot<mirror::Reference>(this)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3130 | } else if (strcmp(descriptor, "Ljava/lang/DexCache;") == 0) { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 3131 | klass.Assign(GetClassRoot<mirror::DexCache>(this)); |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 3132 | } else if (strcmp(descriptor, "Ldalvik/system/ClassExt;") == 0) { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 3133 | klass.Assign(GetClassRoot<mirror::ClassExt>(this)); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3134 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3135 | } |
| 3136 | |
Calin Juravle | 3378768 | 2019-07-26 14:27:18 -0700 | [diff] [blame] | 3137 | // For AOT-compilation of an app, we may use only a public SDK to resolve symbols. If the SDK |
| 3138 | // checks are configured (a non null SdkChecker) and the descriptor is not in the provided |
| 3139 | // public class path then we prevent the definition of the class. |
| 3140 | // |
| 3141 | // NOTE that we only do the checks for the boot classpath APIs. Anything else, like the app |
| 3142 | // classpath is not checked. |
| 3143 | if (class_loader == nullptr && |
| 3144 | Runtime::Current()->IsAotCompiler() && |
| 3145 | DenyAccessBasedOnPublicSdk(descriptor)) { |
| 3146 | ObjPtr<mirror::Throwable> pre_allocated = |
| 3147 | Runtime::Current()->GetPreAllocatedNoClassDefFoundError(); |
| 3148 | self->SetException(pre_allocated); |
| 3149 | return sdc.Finish(nullptr); |
| 3150 | } |
| 3151 | |
Alex Light | e9f6103 | 2018-09-24 16:04:51 -0700 | [diff] [blame] | 3152 | // This is to prevent the calls to ClassLoad and ClassPrepare which can cause java/user-supplied |
| 3153 | // code to be executed. We put it up here so we can avoid all the allocations associated with |
| 3154 | // creating the class. This can happen with (eg) jit threads. |
| 3155 | if (!self->CanLoadClasses()) { |
| 3156 | // Make sure we don't try to load anything, potentially causing an infinite loop. |
| 3157 | ObjPtr<mirror::Throwable> pre_allocated = |
| 3158 | Runtime::Current()->GetPreAllocatedNoClassDefFoundError(); |
| 3159 | self->SetException(pre_allocated); |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 3160 | return sdc.Finish(nullptr); |
Alex Light | e9f6103 | 2018-09-24 16:04:51 -0700 | [diff] [blame] | 3161 | } |
| 3162 | |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3163 | if (klass == nullptr) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3164 | // Allocate a class with the status of not ready. |
| 3165 | // Interface object should get the right size here. Regular class will |
| 3166 | // figure out the right size later and be replaced with one of the right |
| 3167 | // size when the class becomes resolved. |
Chang Xing | 0c2c222 | 2017-08-04 14:36:17 -0700 | [diff] [blame] | 3168 | if (CanAllocClass()) { |
| 3169 | klass.Assign(AllocClass(self, SizeOfClassWithoutEmbeddedTables(dex_file, dex_class_def))); |
| 3170 | } else { |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 3171 | return sdc.Finish(nullptr); |
Chang Xing | 0c2c222 | 2017-08-04 14:36:17 -0700 | [diff] [blame] | 3172 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3173 | } |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3174 | if (UNLIKELY(klass == nullptr)) { |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3175 | self->AssertPendingOOMException(); |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 3176 | return sdc.Finish(nullptr); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3177 | } |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 3178 | // Get the real dex file. This will return the input if there aren't any callbacks or they do |
| 3179 | // nothing. |
| 3180 | DexFile const* new_dex_file = nullptr; |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 3181 | dex::ClassDef const* new_class_def = nullptr; |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 3182 | // TODO We should ideally figure out some way to move this after we get a lock on the klass so it |
| 3183 | // will only be called once. |
| 3184 | Runtime::Current()->GetRuntimeCallbacks()->ClassPreDefine(descriptor, |
| 3185 | klass, |
| 3186 | class_loader, |
| 3187 | dex_file, |
| 3188 | dex_class_def, |
| 3189 | &new_dex_file, |
| 3190 | &new_class_def); |
Alex Light | 440b5d9 | 2017-01-24 15:32:25 -0800 | [diff] [blame] | 3191 | // Check to see if an exception happened during runtime callbacks. Return if so. |
| 3192 | if (self->IsExceptionPending()) { |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 3193 | return sdc.Finish(nullptr); |
Alex Light | 440b5d9 | 2017-01-24 15:32:25 -0800 | [diff] [blame] | 3194 | } |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 3195 | ObjPtr<mirror::DexCache> dex_cache = RegisterDexFile(*new_dex_file, class_loader.Get()); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3196 | if (dex_cache == nullptr) { |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3197 | self->AssertPendingException(); |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 3198 | return sdc.Finish(nullptr); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3199 | } |
| 3200 | klass->SetDexCache(dex_cache); |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 3201 | SetupClass(*new_dex_file, *new_class_def, klass, class_loader.Get()); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3202 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 3203 | // Mark the string class by setting its access flag. |
| 3204 | if (UNLIKELY(!init_done_)) { |
| 3205 | if (strcmp(descriptor, "Ljava/lang/String;") == 0) { |
| 3206 | klass->SetStringClass(); |
| 3207 | } |
| 3208 | } |
| 3209 | |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 3210 | ObjectLock<mirror::Class> lock(self, klass); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3211 | klass->SetClinitThreadId(self->GetTid()); |
Mathieu Chartier | 1e4841e | 2016-12-15 14:21:04 -0800 | [diff] [blame] | 3212 | // 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] | 3213 | klass->SetIfTable(GetClassRoot<mirror::Object>(this)->GetIfTable()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3214 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3215 | // Add the newly loaded class to the loaded classes table. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3216 | ObjPtr<mirror::Class> existing = InsertClass(descriptor, klass.Get(), hash); |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 3217 | if (existing != nullptr) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3218 | // We failed to insert because we raced with another thread. Calling EnsureResolved may cause |
| 3219 | // this thread to block. |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 3220 | return sdc.Finish(EnsureResolved(self, descriptor, existing)); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3221 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3222 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3223 | // Load the fields and other things after we are inserted in the table. This is so that we don't |
| 3224 | // end up allocating unfree-able linear alloc resources and then lose the race condition. The |
| 3225 | // other reason is that the field roots are only visited from the class table. So we need to be |
| 3226 | // inserted before we allocate / fill in these fields. |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 3227 | LoadClass(self, *new_dex_file, *new_class_def, klass); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3228 | if (self->IsExceptionPending()) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 3229 | VLOG(class_linker) << self->GetException()->Dump(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3230 | // An exception occured during load, set status to erroneous while holding klass' lock in case |
| 3231 | // notification is necessary. |
| 3232 | if (!klass->IsErroneous()) { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 3233 | mirror::Class::SetStatus(klass, ClassStatus::kErrorUnresolved, self); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3234 | } |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 3235 | return sdc.Finish(nullptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3236 | } |
| 3237 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3238 | // Finish loading (if necessary) by finding parents |
| 3239 | CHECK(!klass->IsLoaded()); |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 3240 | if (!LoadSuperAndInterfaces(klass, *new_dex_file)) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3241 | // Loading failed. |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 3242 | if (!klass->IsErroneous()) { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 3243 | mirror::Class::SetStatus(klass, ClassStatus::kErrorUnresolved, self); |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 3244 | } |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 3245 | return sdc.Finish(nullptr); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3246 | } |
| 3247 | CHECK(klass->IsLoaded()); |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 3248 | |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 3249 | // At this point the class is loaded. Publish a ClassLoad event. |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 3250 | // 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] | 3251 | Runtime::Current()->GetRuntimeCallbacks()->ClassLoad(klass); |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 3252 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3253 | // Link the class (if necessary) |
| 3254 | CHECK(!klass->IsResolved()); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3255 | // TODO: Use fast jobjects? |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3256 | auto interfaces = hs.NewHandle<mirror::ObjectArray<mirror::Class>>(nullptr); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3257 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 3258 | MutableHandle<mirror::Class> h_new_class = hs.NewHandle<mirror::Class>(nullptr); |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 3259 | if (!LinkClass(self, descriptor, klass, interfaces, &h_new_class)) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3260 | // Linking failed. |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 3261 | if (!klass->IsErroneous()) { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 3262 | mirror::Class::SetStatus(klass, ClassStatus::kErrorUnresolved, self); |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 3263 | } |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 3264 | return sdc.Finish(nullptr); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3265 | } |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 3266 | self->AssertNoPendingException(); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3267 | CHECK(h_new_class != nullptr) << descriptor; |
Vladimir Marko | f9e82e5 | 2021-10-06 08:34:40 +0100 | [diff] [blame] | 3268 | CHECK(h_new_class->IsResolved()) << descriptor << " " << h_new_class->GetStatus(); |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 3269 | |
Sebastien Hertz | a8a697f | 2015-01-15 12:28:47 +0100 | [diff] [blame] | 3270 | // Instrumentation may have updated entrypoints for all methods of all |
| 3271 | // classes. However it could not update methods of this class while we |
| 3272 | // were loading it. Now the class is resolved, we can update entrypoints |
| 3273 | // as required by instrumentation. |
| 3274 | if (Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled()) { |
| 3275 | // We must be in the kRunnable state to prevent instrumentation from |
| 3276 | // suspending all threads to update entrypoints while we are doing it |
| 3277 | // for this class. |
Vladimir Marko | ddf4fd3 | 2021-11-22 16:31:57 +0000 | [diff] [blame] | 3278 | DCHECK_EQ(self->GetState(), ThreadState::kRunnable); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 3279 | Runtime::Current()->GetInstrumentation()->InstallStubsForClass(h_new_class.Get()); |
Sebastien Hertz | a8a697f | 2015-01-15 12:28:47 +0100 | [diff] [blame] | 3280 | } |
| 3281 | |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 3282 | /* |
| 3283 | * We send CLASS_PREPARE events to the debugger from here. The |
| 3284 | * definition of "preparation" is creating the static fields for a |
| 3285 | * class and initializing them to the standard default values, but not |
| 3286 | * executing any code (that comes later, during "initialization"). |
| 3287 | * |
| 3288 | * We did the static preparation in LinkClass. |
| 3289 | * |
| 3290 | * The class has been prepared and resolved but possibly not yet verified |
| 3291 | * at this point. |
| 3292 | */ |
Andreas Gampe | ac30fa2 | 2017-01-18 21:02:36 -0800 | [diff] [blame] | 3293 | Runtime::Current()->GetRuntimeCallbacks()->ClassPrepare(klass, h_new_class); |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 3294 | |
Tamas Berghammer | 160e6df | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 3295 | // Notify native debugger of the new class and its layout. |
| 3296 | jit::Jit::NewTypeLoadedIfUsingJit(h_new_class.Get()); |
| 3297 | |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 3298 | return sdc.Finish(h_new_class); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3299 | } |
| 3300 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3301 | uint32_t ClassLinker::SizeOfClassWithoutEmbeddedTables(const DexFile& dex_file, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 3302 | const dex::ClassDef& dex_class_def) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 3303 | size_t num_ref = 0; |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 3304 | size_t num_8 = 0; |
| 3305 | size_t num_16 = 0; |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 3306 | size_t num_32 = 0; |
| 3307 | size_t num_64 = 0; |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3308 | ClassAccessor accessor(dex_file, dex_class_def); |
| 3309 | // We allow duplicate definitions of the same field in a class_data_item |
| 3310 | // but ignore the repeated indexes here, b/21868015. |
| 3311 | uint32_t last_field_idx = dex::kDexNoIndex; |
| 3312 | for (const ClassAccessor::Field& field : accessor.GetStaticFields()) { |
| 3313 | uint32_t field_idx = field.GetIndex(); |
| 3314 | // Ordering enforced by DexFileVerifier. |
| 3315 | DCHECK(last_field_idx == dex::kDexNoIndex || last_field_idx <= field_idx); |
| 3316 | if (UNLIKELY(field_idx == last_field_idx)) { |
| 3317 | continue; |
| 3318 | } |
| 3319 | last_field_idx = field_idx; |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 3320 | const dex::FieldId& field_id = dex_file.GetFieldId(field_idx); |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3321 | const char* descriptor = dex_file.GetFieldTypeDescriptor(field_id); |
| 3322 | char c = descriptor[0]; |
| 3323 | switch (c) { |
| 3324 | case 'L': |
| 3325 | case '[': |
| 3326 | num_ref++; |
| 3327 | break; |
| 3328 | case 'J': |
| 3329 | case 'D': |
| 3330 | num_64++; |
| 3331 | break; |
| 3332 | case 'I': |
| 3333 | case 'F': |
| 3334 | num_32++; |
| 3335 | break; |
| 3336 | case 'S': |
| 3337 | case 'C': |
| 3338 | num_16++; |
| 3339 | break; |
| 3340 | case 'B': |
| 3341 | case 'Z': |
| 3342 | num_8++; |
| 3343 | break; |
| 3344 | default: |
| 3345 | LOG(FATAL) << "Unknown descriptor: " << c; |
| 3346 | UNREACHABLE(); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 3347 | } |
| 3348 | } |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3349 | return mirror::Class::ComputeClassSize(false, |
| 3350 | 0, |
| 3351 | num_8, |
| 3352 | num_16, |
| 3353 | num_32, |
| 3354 | num_64, |
| 3355 | num_ref, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3356 | image_pointer_size_); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 3357 | } |
| 3358 | |
Vladimir Marko | 86c8752 | 2020-05-11 16:55:55 +0100 | [diff] [blame] | 3359 | void ClassLinker::FixupStaticTrampolines(Thread* self, ObjPtr<mirror::Class> klass) { |
Alex Light | 2d441b1 | 2018-06-08 15:33:21 -0700 | [diff] [blame] | 3360 | ScopedAssertNoThreadSuspension sants(__FUNCTION__); |
Vladimir Marko | cce414f | 2019-10-07 08:51:33 +0100 | [diff] [blame] | 3361 | DCHECK(klass->IsVisiblyInitialized()) << klass->PrettyDescriptor(); |
Vladimir Marko | 86c8752 | 2020-05-11 16:55:55 +0100 | [diff] [blame] | 3362 | size_t num_direct_methods = klass->NumDirectMethods(); |
| 3363 | if (num_direct_methods == 0) { |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 3364 | return; // No direct methods => no static methods. |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3365 | } |
Vladimir Marko | cce414f | 2019-10-07 08:51:33 +0100 | [diff] [blame] | 3366 | if (UNLIKELY(klass->IsProxyClass())) { |
| 3367 | return; |
| 3368 | } |
Vladimir Marko | 86c8752 | 2020-05-11 16:55:55 +0100 | [diff] [blame] | 3369 | PointerSize pointer_size = image_pointer_size_; |
| 3370 | if (std::any_of(klass->GetDirectMethods(pointer_size).begin(), |
| 3371 | klass->GetDirectMethods(pointer_size).end(), |
| 3372 | [](const ArtMethod& m) { return m.IsCriticalNative(); })) { |
| 3373 | // Store registered @CriticalNative methods, if any, to JNI entrypoints. |
| 3374 | // Direct methods are a contiguous chunk of memory, so use the ordering of the map. |
| 3375 | ArtMethod* first_method = klass->GetDirectMethod(0u, pointer_size); |
| 3376 | ArtMethod* last_method = klass->GetDirectMethod(num_direct_methods - 1u, pointer_size); |
| 3377 | MutexLock lock(self, critical_native_code_with_clinit_check_lock_); |
| 3378 | auto lb = critical_native_code_with_clinit_check_.lower_bound(first_method); |
| 3379 | while (lb != critical_native_code_with_clinit_check_.end() && lb->first <= last_method) { |
| 3380 | lb->first->SetEntryPointFromJni(lb->second); |
| 3381 | lb = critical_native_code_with_clinit_check_.erase(lb); |
| 3382 | } |
| 3383 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3384 | Runtime* runtime = Runtime::Current(); |
Nicolas Geoffray | 854af03 | 2021-12-21 08:32:42 +0000 | [diff] [blame] | 3385 | if (runtime->IsAotCompiler()) { |
| 3386 | // We should not update entrypoints when running the transactional |
| 3387 | // interpreter. |
| 3388 | return; |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3389 | } |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 3390 | |
Nicolas Geoffray | c8a694d | 2022-01-17 17:12:38 +0000 | [diff] [blame] | 3391 | instrumentation::Instrumentation* instrumentation = runtime->GetInstrumentation(); |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 3392 | // Link the code of methods skipped by LinkCode. |
Vladimir Marko | 86c8752 | 2020-05-11 16:55:55 +0100 | [diff] [blame] | 3393 | for (size_t method_index = 0; method_index < num_direct_methods; ++method_index) { |
| 3394 | ArtMethod* method = klass->GetDirectMethod(method_index, pointer_size); |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 3395 | if (!method->IsStatic()) { |
| 3396 | // Only update static methods. |
| 3397 | continue; |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3398 | } |
Nicolas Geoffray | 854af03 | 2021-12-21 08:32:42 +0000 | [diff] [blame] | 3399 | instrumentation->UpdateMethodsCode(method, instrumentation->GetCodeForInvoke(method)); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3400 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3401 | // Ignore virtual methods on the iterator. |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3402 | } |
| 3403 | |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3404 | // Does anything needed to make sure that the compiler will not generate a direct invoke to this |
| 3405 | // method. Should only be called on non-invokable methods. |
Nicolas Geoffray | f05f04b | 2019-10-31 11:50:41 +0000 | [diff] [blame] | 3406 | inline void EnsureThrowsInvocationError(ClassLinker* class_linker, ArtMethod* method) |
| 3407 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 3408 | DCHECK(method != nullptr); |
| 3409 | DCHECK(!method->IsInvokable()); |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3410 | method->SetEntryPointFromQuickCompiledCodePtrSize( |
| 3411 | class_linker->GetQuickToInterpreterBridgeTrampoline(), |
| 3412 | class_linker->GetImagePointerSize()); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 3413 | } |
| 3414 | |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3415 | static void LinkCode(ClassLinker* class_linker, |
| 3416 | ArtMethod* method, |
| 3417 | const OatFile::OatClass* oat_class, |
| 3418 | uint32_t class_def_method_index) REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | 2d441b1 | 2018-06-08 15:33:21 -0700 | [diff] [blame] | 3419 | ScopedAssertNoThreadSuspension sants(__FUNCTION__); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3420 | Runtime* const runtime = Runtime::Current(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 3421 | if (runtime->IsAotCompiler()) { |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 3422 | // The following code only applies to a non-compiler runtime. |
| 3423 | return; |
| 3424 | } |
Nicolas Geoffray | 5ee206f | 2019-10-08 15:09:17 +0100 | [diff] [blame] | 3425 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3426 | // Method shouldn't have already been linked. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 3427 | DCHECK(method->GetEntryPointFromQuickCompiledCode() == nullptr); |
Nicolas Geoffray | 854af03 | 2021-12-21 08:32:42 +0000 | [diff] [blame] | 3428 | DCHECK(!method->GetDeclaringClass()->IsVisiblyInitialized()); // Actually ClassStatus::Idx. |
Jeff Hao | 1674363 | 2013-05-08 10:59:04 -0700 | [diff] [blame] | 3429 | |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 3430 | if (!method->IsInvokable()) { |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3431 | EnsureThrowsInvocationError(class_linker, method); |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 3432 | return; |
| 3433 | } |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3434 | |
Nicolas Geoffray | 5ee206f | 2019-10-08 15:09:17 +0100 | [diff] [blame] | 3435 | const void* quick_code = nullptr; |
| 3436 | if (oat_class != nullptr) { |
| 3437 | // Every kind of method should at least get an invoke stub from the oat_method. |
| 3438 | // non-abstract methods also get their code pointers. |
| 3439 | const OatFile::OatMethod oat_method = oat_class->GetOatMethod(class_def_method_index); |
| 3440 | quick_code = oat_method.GetQuickCode(); |
| 3441 | } |
Nicolas Geoffray | 854af03 | 2021-12-21 08:32:42 +0000 | [diff] [blame] | 3442 | runtime->GetInstrumentation()->InitializeMethodsCode(method, quick_code); |
jeffhao | 26c0a1a | 2012-01-17 16:28:33 -0800 | [diff] [blame] | 3443 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3444 | if (method->IsNative()) { |
Vladimir Marko | 86c8752 | 2020-05-11 16:55:55 +0100 | [diff] [blame] | 3445 | // Set up the dlsym lookup stub. Do not go through `UnregisterNative()` |
| 3446 | // as the extra processing for @CriticalNative is not needed yet. |
| 3447 | method->SetEntryPointFromJni( |
| 3448 | method->IsCriticalNative() ? GetJniDlsymLookupCriticalStub() : GetJniDlsymLookupStub()); |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 3449 | } |
| 3450 | } |
| 3451 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3452 | void ClassLinker::SetupClass(const DexFile& dex_file, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 3453 | const dex::ClassDef& dex_class_def, |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3454 | Handle<mirror::Class> klass, |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3455 | ObjPtr<mirror::ClassLoader> class_loader) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3456 | CHECK(klass != nullptr); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3457 | CHECK(klass->GetDexCache() != nullptr); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 3458 | CHECK_EQ(ClassStatus::kNotReady, klass->GetStatus()); |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 3459 | const char* descriptor = dex_file.GetClassDescriptor(dex_class_def); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3460 | CHECK(descriptor != nullptr); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3461 | |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 3462 | klass->SetClass(GetClassRoot<mirror::Class>(this)); |
Andreas Gampe | 5182932 | 2014-08-25 15:05:04 -0700 | [diff] [blame] | 3463 | uint32_t access_flags = dex_class_def.GetJavaAccessFlags(); |
Brian Carlstrom | 8e3fb14 | 2013-10-09 21:00:27 -0700 | [diff] [blame] | 3464 | CHECK_EQ(access_flags & ~kAccJavaFlagsMask, 0U); |
Vladimir Marko | b68bb7a | 2020-03-17 10:55:25 +0000 | [diff] [blame] | 3465 | klass->SetAccessFlagsDuringLinking(access_flags); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3466 | klass->SetClassLoader(class_loader); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3467 | DCHECK_EQ(klass->GetPrimitiveType(), Primitive::kPrimNot); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 3468 | mirror::Class::SetStatus(klass, ClassStatus::kIdx, nullptr); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3469 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 3470 | klass->SetDexClassDefIndex(dex_file.GetIndexForClassDef(dex_class_def)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3471 | klass->SetDexTypeIndex(dex_class_def.class_idx_); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3472 | } |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3473 | |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3474 | LengthPrefixedArray<ArtField>* ClassLinker::AllocArtFieldArray(Thread* self, |
| 3475 | LinearAlloc* allocator, |
| 3476 | size_t length) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3477 | if (length == 0) { |
| 3478 | return nullptr; |
| 3479 | } |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 3480 | // If the ArtField alignment changes, review all uses of LengthPrefixedArray<ArtField>. |
| 3481 | static_assert(alignof(ArtField) == 4, "ArtField alignment is expected to be 4."); |
| 3482 | size_t storage_size = LengthPrefixedArray<ArtField>::ComputeSize(length); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3483 | void* array_storage = allocator->Alloc(self, storage_size); |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 3484 | auto* ret = new(array_storage) LengthPrefixedArray<ArtField>(length); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3485 | CHECK(ret != nullptr); |
| 3486 | std::uninitialized_fill_n(&ret->At(0), length, ArtField()); |
| 3487 | return ret; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3488 | } |
| 3489 | |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3490 | LengthPrefixedArray<ArtMethod>* ClassLinker::AllocArtMethodArray(Thread* self, |
| 3491 | LinearAlloc* allocator, |
| 3492 | size_t length) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3493 | if (length == 0) { |
| 3494 | return nullptr; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3495 | } |
Vladimir Marko | 1463285 | 2015-08-17 12:07:23 +0100 | [diff] [blame] | 3496 | const size_t method_alignment = ArtMethod::Alignment(image_pointer_size_); |
| 3497 | const size_t method_size = ArtMethod::Size(image_pointer_size_); |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 3498 | const size_t storage_size = |
| 3499 | LengthPrefixedArray<ArtMethod>::ComputeSize(length, method_size, method_alignment); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3500 | void* array_storage = allocator->Alloc(self, storage_size); |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 3501 | auto* ret = new (array_storage) LengthPrefixedArray<ArtMethod>(length); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3502 | CHECK(ret != nullptr); |
| 3503 | for (size_t i = 0; i < length; ++i) { |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 3504 | new(reinterpret_cast<void*>(&ret->At(i, method_size, method_alignment))) ArtMethod; |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3505 | } |
| 3506 | return ret; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3507 | } |
| 3508 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3509 | LinearAlloc* ClassLinker::GetAllocatorForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3510 | if (class_loader == nullptr) { |
| 3511 | return Runtime::Current()->GetLinearAlloc(); |
| 3512 | } |
| 3513 | LinearAlloc* allocator = class_loader->GetAllocator(); |
| 3514 | DCHECK(allocator != nullptr); |
| 3515 | return allocator; |
| 3516 | } |
| 3517 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3518 | LinearAlloc* ClassLinker::GetOrCreateAllocatorForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 3519 | if (class_loader == nullptr) { |
| 3520 | return Runtime::Current()->GetLinearAlloc(); |
| 3521 | } |
| 3522 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
| 3523 | LinearAlloc* allocator = class_loader->GetAllocator(); |
| 3524 | if (allocator == nullptr) { |
Mathieu Chartier | 5b83050 | 2016-03-02 10:30:23 -0800 | [diff] [blame] | 3525 | RegisterClassLoader(class_loader); |
| 3526 | allocator = class_loader->GetAllocator(); |
| 3527 | CHECK(allocator != nullptr); |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 3528 | } |
| 3529 | return allocator; |
| 3530 | } |
| 3531 | |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3532 | void ClassLinker::LoadClass(Thread* self, |
| 3533 | const DexFile& dex_file, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 3534 | const dex::ClassDef& dex_class_def, |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3535 | Handle<mirror::Class> klass) { |
David Brazdil | 20c765f | 2018-10-27 21:45:15 +0000 | [diff] [blame] | 3536 | ClassAccessor accessor(dex_file, |
| 3537 | dex_class_def, |
| 3538 | /* parse_hiddenapi_class_data= */ klass->IsBootStrapClassLoaded()); |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3539 | if (!accessor.HasClassData()) { |
| 3540 | return; |
| 3541 | } |
| 3542 | Runtime* const runtime = Runtime::Current(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3543 | { |
| 3544 | // Note: We cannot have thread suspension until the field and method arrays are setup or else |
| 3545 | // Class::VisitFieldRoots may miss some fields or methods. |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 3546 | ScopedAssertNoThreadSuspension nts(__FUNCTION__); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3547 | // Load static fields. |
Vladimir Marko | 23682bf | 2015-06-24 14:28:03 +0100 | [diff] [blame] | 3548 | // We allow duplicate definitions of the same field in a class_data_item |
| 3549 | // but ignore the repeated indexes here, b/21868015. |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3550 | LinearAlloc* const allocator = GetAllocatorForClassLoader(klass->GetClassLoader()); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3551 | LengthPrefixedArray<ArtField>* sfields = AllocArtFieldArray(self, |
| 3552 | allocator, |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3553 | accessor.NumStaticFields()); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3554 | LengthPrefixedArray<ArtField>* ifields = AllocArtFieldArray(self, |
| 3555 | allocator, |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3556 | accessor.NumInstanceFields()); |
| 3557 | size_t num_sfields = 0u; |
Vladimir Marko | 23682bf | 2015-06-24 14:28:03 +0100 | [diff] [blame] | 3558 | size_t num_ifields = 0u; |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3559 | uint32_t last_static_field_idx = 0u; |
| 3560 | uint32_t last_instance_field_idx = 0u; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 3561 | |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3562 | // Methods |
| 3563 | bool has_oat_class = false; |
| 3564 | const OatFile::OatClass oat_class = (runtime->IsStarted() && !runtime->IsAotCompiler()) |
| 3565 | ? OatFile::FindOatClass(dex_file, klass->GetDexClassDefIndex(), &has_oat_class) |
| 3566 | : OatFile::OatClass::Invalid(); |
| 3567 | const OatFile::OatClass* oat_class_ptr = has_oat_class ? &oat_class : nullptr; |
| 3568 | klass->SetMethodsPtr( |
| 3569 | AllocArtMethodArray(self, allocator, accessor.NumMethods()), |
| 3570 | accessor.NumDirectMethods(), |
| 3571 | accessor.NumVirtualMethods()); |
| 3572 | size_t class_def_method_index = 0; |
| 3573 | uint32_t last_dex_method_index = dex::kDexNoIndex; |
| 3574 | size_t last_class_def_method_index = 0; |
| 3575 | |
Nicolas Geoffray | 58f916c | 2021-11-15 14:02:07 +0000 | [diff] [blame] | 3576 | uint16_t hotness_threshold = runtime->GetJITOptions()->GetWarmupThreshold(); |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3577 | // Use the visitor since the ranged based loops are bit slower from seeking. Seeking to the |
| 3578 | // methods needs to decode all of the fields. |
| 3579 | accessor.VisitFieldsAndMethods([&]( |
| 3580 | const ClassAccessor::Field& field) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 3581 | uint32_t field_idx = field.GetIndex(); |
| 3582 | DCHECK_GE(field_idx, last_static_field_idx); // Ordering enforced by DexFileVerifier. |
| 3583 | if (num_sfields == 0 || LIKELY(field_idx > last_static_field_idx)) { |
| 3584 | LoadField(field, klass, &sfields->At(num_sfields)); |
| 3585 | ++num_sfields; |
| 3586 | last_static_field_idx = field_idx; |
| 3587 | } |
| 3588 | }, [&](const ClassAccessor::Field& field) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 3589 | uint32_t field_idx = field.GetIndex(); |
| 3590 | DCHECK_GE(field_idx, last_instance_field_idx); // Ordering enforced by DexFileVerifier. |
| 3591 | if (num_ifields == 0 || LIKELY(field_idx > last_instance_field_idx)) { |
| 3592 | LoadField(field, klass, &ifields->At(num_ifields)); |
| 3593 | ++num_ifields; |
| 3594 | last_instance_field_idx = field_idx; |
| 3595 | } |
| 3596 | }, [&](const ClassAccessor::Method& method) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 3597 | ArtMethod* art_method = klass->GetDirectMethodUnchecked(class_def_method_index, |
| 3598 | image_pointer_size_); |
Vladimir Marko | 05f1a5b | 2022-03-09 14:20:15 +0000 | [diff] [blame] | 3599 | LoadMethod(dex_file, method, klass.Get(), art_method); |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3600 | LinkCode(this, art_method, oat_class_ptr, class_def_method_index); |
| 3601 | uint32_t it_method_index = method.GetIndex(); |
| 3602 | if (last_dex_method_index == it_method_index) { |
| 3603 | // duplicate case |
| 3604 | art_method->SetMethodIndex(last_class_def_method_index); |
| 3605 | } else { |
| 3606 | art_method->SetMethodIndex(class_def_method_index); |
| 3607 | last_dex_method_index = it_method_index; |
| 3608 | last_class_def_method_index = class_def_method_index; |
| 3609 | } |
Nicolas Geoffray | 58f916c | 2021-11-15 14:02:07 +0000 | [diff] [blame] | 3610 | art_method->ResetCounter(hotness_threshold); |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3611 | ++class_def_method_index; |
| 3612 | }, [&](const ClassAccessor::Method& method) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 3613 | ArtMethod* art_method = klass->GetVirtualMethodUnchecked( |
| 3614 | class_def_method_index - accessor.NumDirectMethods(), |
| 3615 | image_pointer_size_); |
Nicolas Geoffray | 58f916c | 2021-11-15 14:02:07 +0000 | [diff] [blame] | 3616 | art_method->ResetCounter(hotness_threshold); |
Vladimir Marko | 05f1a5b | 2022-03-09 14:20:15 +0000 | [diff] [blame] | 3617 | LoadMethod(dex_file, method, klass.Get(), art_method); |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3618 | LinkCode(this, art_method, oat_class_ptr, class_def_method_index); |
| 3619 | ++class_def_method_index; |
| 3620 | }); |
| 3621 | |
| 3622 | if (UNLIKELY(num_ifields + num_sfields != accessor.NumFields())) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3623 | LOG(WARNING) << "Duplicate fields in class " << klass->PrettyDescriptor() |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3624 | << " (unique static fields: " << num_sfields << "/" << accessor.NumStaticFields() |
| 3625 | << ", unique instance fields: " << num_ifields << "/" << accessor.NumInstanceFields() |
| 3626 | << ")"; |
Vladimir Marko | 81819db | 2015-11-05 15:30:12 +0000 | [diff] [blame] | 3627 | // NOTE: Not shrinking the over-allocated sfields/ifields, just setting size. |
| 3628 | if (sfields != nullptr) { |
| 3629 | sfields->SetSize(num_sfields); |
| 3630 | } |
| 3631 | if (ifields != nullptr) { |
| 3632 | ifields->SetSize(num_ifields); |
| 3633 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3634 | } |
Vladimir Marko | 81819db | 2015-11-05 15:30:12 +0000 | [diff] [blame] | 3635 | // Set the field arrays. |
| 3636 | klass->SetSFieldsPtr(sfields); |
| 3637 | DCHECK_EQ(klass->NumStaticFields(), num_sfields); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3638 | klass->SetIFieldsPtr(ifields); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3639 | DCHECK_EQ(klass->NumInstanceFields(), num_ifields); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3640 | } |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 3641 | // 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] | 3642 | WriteBarrier::ForEveryFieldWrite(klass.Get()); |
Mathieu Chartier | f3f2a7a | 2015-04-14 15:43:10 -0700 | [diff] [blame] | 3643 | self->AllowThreadSuspension(); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3644 | } |
| 3645 | |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3646 | void ClassLinker::LoadField(const ClassAccessor::Field& field, |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3647 | Handle<mirror::Class> klass, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3648 | ArtField* dst) { |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3649 | const uint32_t field_idx = field.GetIndex(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3650 | dst->SetDexFieldIndex(field_idx); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3651 | dst->SetDeclaringClass(klass.Get()); |
David Brazdil | f6a8a55 | 2018-01-15 18:10:50 +0000 | [diff] [blame] | 3652 | |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 3653 | // Get access flags from the DexFile and set hiddenapi runtime access flags. |
| 3654 | dst->SetAccessFlags(field.GetAccessFlags() | hiddenapi::CreateRuntimeFlags(field)); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3655 | } |
| 3656 | |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 3657 | void ClassLinker::LoadMethod(const DexFile& dex_file, |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3658 | const ClassAccessor::Method& method, |
Vladimir Marko | 05f1a5b | 2022-03-09 14:20:15 +0000 | [diff] [blame] | 3659 | ObjPtr<mirror::Class> klass, |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3660 | ArtMethod* dst) { |
Vladimir Marko | 05f1a5b | 2022-03-09 14:20:15 +0000 | [diff] [blame] | 3661 | ScopedAssertNoThreadSuspension sants(__FUNCTION__); |
| 3662 | |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3663 | const uint32_t dex_method_idx = method.GetIndex(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 3664 | const dex::MethodId& method_id = dex_file.GetMethodId(dex_method_idx); |
Vladimir Marko | 05f1a5b | 2022-03-09 14:20:15 +0000 | [diff] [blame] | 3665 | uint32_t name_utf16_length; |
| 3666 | const char* method_name = dex_file.StringDataAndUtf16LengthByIdx(method_id.name_idx_, |
| 3667 | &name_utf16_length); |
| 3668 | 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] | 3669 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 3670 | dst->SetDexMethodIndex(dex_method_idx); |
Vladimir Marko | 05f1a5b | 2022-03-09 14:20:15 +0000 | [diff] [blame] | 3671 | dst->SetDeclaringClass(klass); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3672 | |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 3673 | // Get access flags from the DexFile and set hiddenapi runtime access flags. |
| 3674 | uint32_t access_flags = method.GetAccessFlags() | hiddenapi::CreateRuntimeFlags(method); |
David Brazdil | f6a8a55 | 2018-01-15 18:10:50 +0000 | [diff] [blame] | 3675 | |
Vladimir Marko | 05f1a5b | 2022-03-09 14:20:15 +0000 | [diff] [blame] | 3676 | auto has_ascii_name = [method_name, name_utf16_length](const char* ascii_name, |
| 3677 | size_t length) ALWAYS_INLINE { |
| 3678 | DCHECK_EQ(strlen(ascii_name), length); |
| 3679 | return length == name_utf16_length && |
| 3680 | method_name[length] == 0 && // Is `method_name` an ASCII string? |
| 3681 | memcmp(ascii_name, method_name, length) == 0; |
| 3682 | }; |
| 3683 | if (UNLIKELY(has_ascii_name("finalize", sizeof("finalize") - 1u))) { |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3684 | // Set finalizable flag on declaring class. |
Vladimir Marko | 05f1a5b | 2022-03-09 14:20:15 +0000 | [diff] [blame] | 3685 | if (shorty == "V") { |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 3686 | // Void return type. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3687 | if (klass->GetClassLoader() != nullptr) { // All non-boot finalizer methods are flagged. |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 3688 | klass->SetFinalizable(); |
| 3689 | } else { |
Vladimir Marko | 05f1a5b | 2022-03-09 14:20:15 +0000 | [diff] [blame] | 3690 | std::string_view klass_descriptor = |
| 3691 | dex_file.GetTypeDescriptorView(dex_file.GetTypeId(klass->GetDexTypeIndex())); |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 3692 | // The Enum class declares a "final" finalize() method to prevent subclasses from |
| 3693 | // introducing a finalizer. We don't want to set the finalizable flag for Enum or its |
| 3694 | // subclasses, so we exclude it here. |
| 3695 | // We also want to avoid setting the flag on Object, where we know that finalize() is |
| 3696 | // empty. |
Vladimir Marko | 05f1a5b | 2022-03-09 14:20:15 +0000 | [diff] [blame] | 3697 | if (klass_descriptor != "Ljava/lang/Object;" && |
| 3698 | klass_descriptor != "Ljava/lang/Enum;") { |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3699 | klass->SetFinalizable(); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3700 | } |
| 3701 | } |
| 3702 | } |
| 3703 | } else if (method_name[0] == '<') { |
| 3704 | // Fix broken access flags for initializers. Bug 11157540. |
Vladimir Marko | 05f1a5b | 2022-03-09 14:20:15 +0000 | [diff] [blame] | 3705 | bool is_init = has_ascii_name("<init>", sizeof("<init>") - 1u); |
| 3706 | bool is_clinit = has_ascii_name("<clinit>", sizeof("<clinit>") - 1u); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3707 | if (UNLIKELY(!is_init && !is_clinit)) { |
| 3708 | LOG(WARNING) << "Unexpected '<' at start of method name " << method_name; |
| 3709 | } else { |
| 3710 | if (UNLIKELY((access_flags & kAccConstructor) == 0)) { |
| 3711 | 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] | 3712 | << klass->PrettyDescriptor() << " in dex file " << dex_file.GetLocation(); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3713 | access_flags |= kAccConstructor; |
| 3714 | } |
| 3715 | } |
| 3716 | } |
Vladimir Marko | e815aec | 2022-03-29 15:58:40 +0000 | [diff] [blame] | 3717 | |
| 3718 | // Check for nterp invoke fast-path based on shorty. |
| 3719 | bool all_parameters_are_reference = true; |
| 3720 | bool all_parameters_are_reference_or_int = true; |
| 3721 | for (size_t i = 1; i < shorty.length(); ++i) { |
| 3722 | if (shorty[i] != 'L') { |
| 3723 | all_parameters_are_reference = false; |
| 3724 | if (shorty[i] == 'F' || shorty[i] == 'D' || shorty[i] == 'J') { |
| 3725 | all_parameters_are_reference_or_int = false; |
| 3726 | break; |
| 3727 | } |
| 3728 | } |
| 3729 | } |
| 3730 | if (all_parameters_are_reference_or_int && shorty[0] != 'F' && shorty[0] != 'D') { |
| 3731 | access_flags |= kAccNterpInvokeFastPathFlag; |
| 3732 | } |
| 3733 | |
Vladimir Marko | b0a6aee | 2017-10-27 10:34:04 +0100 | [diff] [blame] | 3734 | if (UNLIKELY((access_flags & kAccNative) != 0u)) { |
| 3735 | // Check if the native method is annotated with @FastNative or @CriticalNative. |
Vladimir Marko | 05f1a5b | 2022-03-09 14:20:15 +0000 | [diff] [blame] | 3736 | const dex::ClassDef& class_def = dex_file.GetClassDef(klass->GetDexClassDefIndex()); |
| 3737 | access_flags |= |
| 3738 | annotations::GetNativeMethodAnnotationAccessFlags(dex_file, class_def, dex_method_idx); |
| 3739 | dst->SetAccessFlags(access_flags); |
| 3740 | DCHECK(!dst->IsAbstract()); |
| 3741 | DCHECK(!dst->HasCodeItem()); |
Nicolas Geoffray | 4717175 | 2020-08-31 15:03:20 +0100 | [diff] [blame] | 3742 | DCHECK_EQ(method.GetCodeItemOffset(), 0u); |
Vladimir Marko | 05f1a5b | 2022-03-09 14:20:15 +0000 | [diff] [blame] | 3743 | dst->SetDataPtrSize(nullptr, image_pointer_size_); // JNI stub/trampoline not linked yet. |
| 3744 | } else if ((access_flags & kAccAbstract) != 0u) { |
| 3745 | dst->SetAccessFlags(access_flags); |
| 3746 | // Must be done after SetAccessFlags since IsAbstract depends on it. |
| 3747 | DCHECK(dst->IsAbstract()); |
| 3748 | if (klass->IsInterface()) { |
| 3749 | dst->CalculateAndSetImtIndex(); |
| 3750 | } |
| 3751 | DCHECK(!dst->HasCodeItem()); |
| 3752 | DCHECK_EQ(method.GetCodeItemOffset(), 0u); |
| 3753 | dst->SetDataPtrSize(nullptr, image_pointer_size_); // Single implementation not set yet. |
| 3754 | } else { |
Vladimir Marko | e815aec | 2022-03-29 15:58:40 +0000 | [diff] [blame] | 3755 | // Check for nterp entry fast-path based on shorty. |
| 3756 | if (all_parameters_are_reference) { |
| 3757 | access_flags |= kAccNterpEntryPointFastPathFlag; |
| 3758 | } |
Vladimir Marko | 05f1a5b | 2022-03-09 14:20:15 +0000 | [diff] [blame] | 3759 | const dex::ClassDef& class_def = dex_file.GetClassDef(klass->GetDexClassDefIndex()); |
| 3760 | if (annotations::MethodIsNeverCompile(dex_file, class_def, dex_method_idx)) { |
| 3761 | access_flags |= kAccCompileDontBother; |
| 3762 | } |
| 3763 | dst->SetAccessFlags(access_flags); |
| 3764 | DCHECK(!dst->IsAbstract()); |
| 3765 | DCHECK(dst->HasCodeItem()); |
| 3766 | uint32_t code_item_offset = method.GetCodeItemOffset(); |
| 3767 | DCHECK_NE(code_item_offset, 0u); |
| 3768 | if (Runtime::Current()->IsAotCompiler()) { |
| 3769 | dst->SetDataPtrSize(reinterpret_cast32<void*>(code_item_offset), image_pointer_size_); |
| 3770 | } else { |
| 3771 | dst->SetCodeItem(dex_file.GetCodeItem(code_item_offset), dex_file.IsCompactDexFile()); |
| 3772 | } |
Nicolas Geoffray | 43c9cd7 | 2021-03-10 15:09:19 +0000 | [diff] [blame] | 3773 | } |
Nicolas Geoffray | f9ae8e3 | 2022-02-15 22:54:11 +0000 | [diff] [blame] | 3774 | |
Nicolas Geoffray | 0ca39d1 | 2022-04-27 14:11:23 +0100 | [diff] [blame] | 3775 | if (Runtime::Current()->IsZygote() && |
| 3776 | !Runtime::Current()->GetJITOptions()->GetProfileSaverOptions().GetProfileBootClassPath()) { |
Nicolas Geoffray | f9ae8e3 | 2022-02-15 22:54:11 +0000 | [diff] [blame] | 3777 | dst->SetMemorySharedMethod(); |
| 3778 | } |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3779 | } |
| 3780 | |
Mathieu Chartier | 0a19e21 | 2019-11-27 14:35:24 -0800 | [diff] [blame] | 3781 | void ClassLinker::AppendToBootClassPath(Thread* self, const DexFile* dex_file) { |
David Srbecky | 33df0e3 | 2021-09-30 14:36:32 +0000 | [diff] [blame] | 3782 | ObjPtr<mirror::DexCache> dex_cache = |
| 3783 | AllocAndInitializeDexCache(self, *dex_file, /* class_loader= */ nullptr); |
Mathieu Chartier | 0a19e21 | 2019-11-27 14:35:24 -0800 | [diff] [blame] | 3784 | 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] | 3785 | AppendToBootClassPath(dex_file, dex_cache); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 3786 | } |
| 3787 | |
Mathieu Chartier | 0a19e21 | 2019-11-27 14:35:24 -0800 | [diff] [blame] | 3788 | void ClassLinker::AppendToBootClassPath(const DexFile* dex_file, |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3789 | ObjPtr<mirror::DexCache> dex_cache) { |
Mathieu Chartier | 0a19e21 | 2019-11-27 14:35:24 -0800 | [diff] [blame] | 3790 | CHECK(dex_file != nullptr); |
| 3791 | CHECK(dex_cache != nullptr) << dex_file->GetLocation(); |
Nicolas Geoffray | 7913cf3 | 2022-02-16 09:28:21 +0000 | [diff] [blame] | 3792 | CHECK_EQ(dex_cache->GetDexFile(), dex_file) << dex_file->GetLocation(); |
Mathieu Chartier | 0a19e21 | 2019-11-27 14:35:24 -0800 | [diff] [blame] | 3793 | boot_class_path_.push_back(dex_file); |
Andreas Gampe | be7af22 | 2017-07-25 09:57:28 -0700 | [diff] [blame] | 3794 | WriterMutexLock mu(Thread::Current(), *Locks::dex_lock_); |
Mathieu Chartier | 0a19e21 | 2019-11-27 14:35:24 -0800 | [diff] [blame] | 3795 | RegisterDexFileLocked(*dex_file, dex_cache, /* class_loader= */ nullptr); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 3796 | } |
| 3797 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 3798 | void ClassLinker::RegisterDexFileLocked(const DexFile& dex_file, |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3799 | ObjPtr<mirror::DexCache> dex_cache, |
| 3800 | ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3801 | Thread* const self = Thread::Current(); |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 3802 | Locks::dex_lock_->AssertExclusiveHeld(self); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3803 | CHECK(dex_cache != nullptr) << dex_file.GetLocation(); |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 3804 | CHECK_EQ(dex_cache->GetDexFile(), &dex_file) << dex_file.GetLocation(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 3805 | // For app images, the dex cache location may be a suffix of the dex file location since the |
| 3806 | // dex file location is an absolute path. |
Mathieu Chartier | 7617216 | 2016-01-26 14:54:06 -0800 | [diff] [blame] | 3807 | const std::string dex_cache_location = dex_cache->GetLocation()->ToModifiedUtf8(); |
| 3808 | const size_t dex_cache_length = dex_cache_location.length(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 3809 | CHECK_GT(dex_cache_length, 0u) << dex_file.GetLocation(); |
| 3810 | std::string dex_file_location = dex_file.GetLocation(); |
Nicolas Geoffray | e3e0f70 | 2019-03-12 07:02:02 +0000 | [diff] [blame] | 3811 | // The following paths checks don't work on preopt when using boot dex files, where the dex |
| 3812 | // cache location is the one on device, and the dex_file's location is the one on host. |
| 3813 | if (!(Runtime::Current()->IsAotCompiler() && class_loader == nullptr && !kIsTargetBuild)) { |
| 3814 | CHECK_GE(dex_file_location.length(), dex_cache_length) |
| 3815 | << dex_cache_location << " " << dex_file.GetLocation(); |
| 3816 | const std::string dex_file_suffix = dex_file_location.substr( |
| 3817 | dex_file_location.length() - dex_cache_length, |
| 3818 | dex_cache_length); |
| 3819 | // Example dex_cache location is SettingsProvider.apk and |
| 3820 | // dex file location is /system/priv-app/SettingsProvider/SettingsProvider.apk |
| 3821 | CHECK_EQ(dex_cache_location, dex_file_suffix); |
| 3822 | } |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 3823 | const OatFile* oat_file = |
| 3824 | (dex_file.GetOatDexFile() != nullptr) ? dex_file.GetOatDexFile()->GetOatFile() : nullptr; |
Vladimir Marko | b066d43 | 2018-01-03 13:14:37 +0000 | [diff] [blame] | 3825 | // Clean up pass to remove null dex caches; null dex caches can occur due to class unloading |
| 3826 | // and we are lazily removing null entries. Also check if we need to initialize OatFile data |
| 3827 | // (.data.bimg.rel.ro and .bss sections) needed for code execution. |
| 3828 | bool initialize_oat_file_data = (oat_file != nullptr) && oat_file->IsExecutable(); |
Ian Rogers | 55256cb | 2017-12-21 17:07:11 -0800 | [diff] [blame] | 3829 | JavaVMExt* const vm = self->GetJniEnv()->GetVm(); |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 3830 | for (auto it = dex_caches_.begin(); it != dex_caches_.end(); ) { |
David Srbecky | 6fbcc29 | 2021-02-23 01:05:32 +0000 | [diff] [blame] | 3831 | const DexCacheData& data = it->second; |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 3832 | if (self->IsJWeakCleared(data.weak_root)) { |
| 3833 | vm->DeleteWeakGlobalRef(self, data.weak_root); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3834 | it = dex_caches_.erase(it); |
| 3835 | } else { |
Vladimir Marko | b066d43 | 2018-01-03 13:14:37 +0000 | [diff] [blame] | 3836 | if (initialize_oat_file_data && |
David Srbecky | 6fbcc29 | 2021-02-23 01:05:32 +0000 | [diff] [blame] | 3837 | it->first->GetOatDexFile() != nullptr && |
| 3838 | it->first->GetOatDexFile()->GetOatFile() == oat_file) { |
Vladimir Marko | b066d43 | 2018-01-03 13:14:37 +0000 | [diff] [blame] | 3839 | initialize_oat_file_data = false; // Already initialized. |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 3840 | } |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3841 | ++it; |
| 3842 | } |
Brian Carlstrom | 81a9087 | 2015-08-28 09:07:14 -0700 | [diff] [blame] | 3843 | } |
Vladimir Marko | b066d43 | 2018-01-03 13:14:37 +0000 | [diff] [blame] | 3844 | if (initialize_oat_file_data) { |
Vladimir Marko | 1cedb4a | 2019-02-06 14:13:28 +0000 | [diff] [blame] | 3845 | oat_file->InitializeRelocations(); |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 3846 | } |
David Brazdil | a5c3a80 | 2019-03-08 14:59:41 +0000 | [diff] [blame] | 3847 | // Let hiddenapi assign a domain to the newly registered dex file. |
| 3848 | hiddenapi::InitializeDexFileDomain(dex_file, class_loader); |
| 3849 | |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3850 | jweak dex_cache_jweak = vm->AddWeakGlobalRef(self, dex_cache); |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 3851 | DexCacheData data; |
| 3852 | data.weak_root = dex_cache_jweak; |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3853 | data.class_table = ClassTableForClassLoader(class_loader); |
David Srbecky | 6fbcc29 | 2021-02-23 01:05:32 +0000 | [diff] [blame] | 3854 | AddNativeDebugInfoForDex(self, &dex_file); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3855 | DCHECK(data.class_table != nullptr); |
Mathieu Chartier | 72041a0 | 2017-07-14 18:23:25 -0700 | [diff] [blame] | 3856 | // Make sure to hold the dex cache live in the class table. This case happens for the boot class |
| 3857 | // path dex caches without an image. |
| 3858 | data.class_table->InsertStrongRoot(dex_cache); |
Andreas Gampe | 8a1a0f7 | 2020-03-03 16:07:45 -0800 | [diff] [blame] | 3859 | // Make sure that the dex cache holds the classloader live. |
| 3860 | dex_cache->SetClassLoader(class_loader); |
Mathieu Chartier | 72041a0 | 2017-07-14 18:23:25 -0700 | [diff] [blame] | 3861 | if (class_loader != nullptr) { |
| 3862 | // Since we added a strong root to the class table, do the write barrier as required for |
| 3863 | // remembered sets and generational GCs. |
Mathieu Chartier | 88ea61e | 2018-06-20 17:45:41 -0700 | [diff] [blame] | 3864 | WriteBarrier::ForEveryFieldWrite(class_loader); |
Mathieu Chartier | 72041a0 | 2017-07-14 18:23:25 -0700 | [diff] [blame] | 3865 | } |
David Srbecky | 6fbcc29 | 2021-02-23 01:05:32 +0000 | [diff] [blame] | 3866 | bool inserted = dex_caches_.emplace(&dex_file, std::move(data)).second; |
| 3867 | CHECK(inserted); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 3868 | } |
| 3869 | |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 3870 | ObjPtr<mirror::DexCache> ClassLinker::DecodeDexCacheLocked(Thread* self, const DexCacheData* data) { |
| 3871 | return data != nullptr |
| 3872 | ? ObjPtr<mirror::DexCache>::DownCast(self->DecodeJObject(data->weak_root)) |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3873 | : nullptr; |
| 3874 | } |
| 3875 | |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 3876 | bool ClassLinker::IsSameClassLoader( |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3877 | ObjPtr<mirror::DexCache> dex_cache, |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 3878 | const DexCacheData* data, |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3879 | ObjPtr<mirror::ClassLoader> class_loader) { |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 3880 | CHECK(data != nullptr); |
David Srbecky | 6fbcc29 | 2021-02-23 01:05:32 +0000 | [diff] [blame] | 3881 | DCHECK_EQ(FindDexCacheDataLocked(*dex_cache->GetDexFile()), data); |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 3882 | return data->class_table == ClassTableForClassLoader(class_loader); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3883 | } |
| 3884 | |
Alex Light | 07f0621 | 2017-06-01 14:01:43 -0700 | [diff] [blame] | 3885 | void ClassLinker::RegisterExistingDexCache(ObjPtr<mirror::DexCache> dex_cache, |
| 3886 | ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | ed4ee44 | 2018-06-05 14:23:35 -0700 | [diff] [blame] | 3887 | SCOPED_TRACE << __FUNCTION__ << " " << dex_cache->GetDexFile()->GetLocation(); |
Alex Light | 07f0621 | 2017-06-01 14:01:43 -0700 | [diff] [blame] | 3888 | Thread* self = Thread::Current(); |
| 3889 | StackHandleScope<2> hs(self); |
| 3890 | Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(dex_cache)); |
| 3891 | Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader)); |
| 3892 | const DexFile* dex_file = dex_cache->GetDexFile(); |
| 3893 | DCHECK(dex_file != nullptr) << "Attempt to register uninitialized dex_cache object!"; |
| 3894 | if (kIsDebugBuild) { |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 3895 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
| 3896 | const DexCacheData* old_data = FindDexCacheDataLocked(*dex_file); |
| 3897 | ObjPtr<mirror::DexCache> old_dex_cache = DecodeDexCacheLocked(self, old_data); |
Alex Light | 07f0621 | 2017-06-01 14:01:43 -0700 | [diff] [blame] | 3898 | DCHECK(old_dex_cache.IsNull()) << "Attempt to manually register a dex cache thats already " |
| 3899 | << "been registered on dex file " << dex_file->GetLocation(); |
| 3900 | } |
| 3901 | ClassTable* table; |
| 3902 | { |
| 3903 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 3904 | table = InsertClassTableForClassLoader(h_class_loader.Get()); |
| 3905 | } |
Ivan Maidanski | 2b69b9c | 2018-05-14 13:50:48 +0300 | [diff] [blame] | 3906 | // Avoid a deadlock between a garbage collecting thread running a checkpoint, |
| 3907 | // a thread holding the dex lock and blocking on a condition variable regarding |
| 3908 | // weak references access, and a thread blocking on the dex lock. |
Ivan Maidanski | 2b69b9c | 2018-05-14 13:50:48 +0300 | [diff] [blame] | 3909 | gc::ScopedGCCriticalSection gcs(self, gc::kGcCauseClassLinker, gc::kCollectorTypeClassLinker); |
Alex Light | 07f0621 | 2017-06-01 14:01:43 -0700 | [diff] [blame] | 3910 | WriterMutexLock mu(self, *Locks::dex_lock_); |
| 3911 | RegisterDexFileLocked(*dex_file, h_dex_cache.Get(), h_class_loader.Get()); |
| 3912 | table->InsertStrongRoot(h_dex_cache.Get()); |
| 3913 | if (h_class_loader.Get() != nullptr) { |
| 3914 | // Since we added a strong root to the class table, do the write barrier as required for |
| 3915 | // remembered sets and generational GCs. |
Mathieu Chartier | 88ea61e | 2018-06-20 17:45:41 -0700 | [diff] [blame] | 3916 | WriteBarrier::ForEveryFieldWrite(h_class_loader.Get()); |
Alex Light | 07f0621 | 2017-06-01 14:01:43 -0700 | [diff] [blame] | 3917 | } |
| 3918 | } |
| 3919 | |
Alex Light | de7f878 | 2020-02-24 10:14:22 -0800 | [diff] [blame] | 3920 | static void ThrowDexFileAlreadyRegisteredError(Thread* self, const DexFile& dex_file) |
| 3921 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 3922 | self->ThrowNewExceptionF("Ljava/lang/InternalError;", |
Alex Light | de7f878 | 2020-02-24 10:14:22 -0800 | [diff] [blame] | 3923 | "Attempt to register dex file %s with multiple class loaders", |
| 3924 | dex_file.GetLocation().c_str()); |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 3925 | } |
| 3926 | |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3927 | ObjPtr<mirror::DexCache> ClassLinker::RegisterDexFile(const DexFile& dex_file, |
| 3928 | ObjPtr<mirror::ClassLoader> class_loader) { |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 3929 | Thread* self = Thread::Current(); |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 3930 | ObjPtr<mirror::DexCache> old_dex_cache; |
| 3931 | bool registered_with_another_class_loader = false; |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 3932 | { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 3933 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 3934 | const DexCacheData* old_data = FindDexCacheDataLocked(dex_file); |
| 3935 | old_dex_cache = DecodeDexCacheLocked(self, old_data); |
| 3936 | if (old_dex_cache != nullptr) { |
| 3937 | if (IsSameClassLoader(old_dex_cache, old_data, class_loader)) { |
| 3938 | return old_dex_cache; |
| 3939 | } else { |
| 3940 | // TODO This is not very clean looking. Should maybe try to make a way to request exceptions |
| 3941 | // be thrown when it's safe to do so to simplify this. |
| 3942 | registered_with_another_class_loader = true; |
| 3943 | } |
| 3944 | } |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3945 | } |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 3946 | // We need to have released the dex_lock_ to allocate safely. |
| 3947 | if (registered_with_another_class_loader) { |
| 3948 | ThrowDexFileAlreadyRegisteredError(self, dex_file); |
| 3949 | return nullptr; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3950 | } |
Mathieu Chartier | ed4ee44 | 2018-06-05 14:23:35 -0700 | [diff] [blame] | 3951 | SCOPED_TRACE << __FUNCTION__ << " " << dex_file.GetLocation(); |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 3952 | LinearAlloc* const linear_alloc = GetOrCreateAllocatorForClassLoader(class_loader); |
| 3953 | DCHECK(linear_alloc != nullptr); |
| 3954 | ClassTable* table; |
| 3955 | { |
| 3956 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 3957 | table = InsertClassTableForClassLoader(class_loader); |
| 3958 | } |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 3959 | // Don't alloc while holding the lock, since allocation may need to |
| 3960 | // suspend all threads and another thread may need the dex_lock_ to |
| 3961 | // get to a suspend point. |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3962 | StackHandleScope<3> hs(self); |
| 3963 | Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader)); |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 3964 | Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(AllocDexCache(self, dex_file))); |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 3965 | { |
Ivan Maidanski | 2b69b9c | 2018-05-14 13:50:48 +0300 | [diff] [blame] | 3966 | // Avoid a deadlock between a garbage collecting thread running a checkpoint, |
| 3967 | // a thread holding the dex lock and blocking on a condition variable regarding |
| 3968 | // weak references access, and a thread blocking on the dex lock. |
Ivan Maidanski | 2b69b9c | 2018-05-14 13:50:48 +0300 | [diff] [blame] | 3969 | gc::ScopedGCCriticalSection gcs(self, gc::kGcCauseClassLinker, gc::kCollectorTypeClassLinker); |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 3970 | WriterMutexLock mu(self, *Locks::dex_lock_); |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 3971 | const DexCacheData* old_data = FindDexCacheDataLocked(dex_file); |
| 3972 | old_dex_cache = DecodeDexCacheLocked(self, old_data); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3973 | if (old_dex_cache == nullptr && h_dex_cache != nullptr) { |
David Srbecky | 33df0e3 | 2021-09-30 14:36:32 +0000 | [diff] [blame] | 3974 | // 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] | 3975 | // at the same time with the same dex cache. Since the .bss is shared this can cause failing |
| 3976 | // DCHECK that the arrays are null. |
David Srbecky | 33df0e3 | 2021-09-30 14:36:32 +0000 | [diff] [blame] | 3977 | h_dex_cache->Initialize(&dex_file, h_class_loader.Get()); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3978 | RegisterDexFileLocked(dex_file, h_dex_cache.Get(), h_class_loader.Get()); |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 3979 | } |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 3980 | if (old_dex_cache != nullptr) { |
| 3981 | // Another thread managed to initialize the dex cache faster, so use that DexCache. |
| 3982 | // If this thread encountered OOME, ignore it. |
| 3983 | DCHECK_EQ(h_dex_cache == nullptr, self->IsExceptionPending()); |
| 3984 | self->ClearException(); |
| 3985 | // We cannot call EnsureSameClassLoader() or allocate an exception while holding the |
| 3986 | // dex_lock_. |
| 3987 | if (IsSameClassLoader(old_dex_cache, old_data, h_class_loader.Get())) { |
| 3988 | return old_dex_cache; |
| 3989 | } else { |
| 3990 | registered_with_another_class_loader = true; |
| 3991 | } |
| 3992 | } |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3993 | } |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 3994 | if (registered_with_another_class_loader) { |
| 3995 | ThrowDexFileAlreadyRegisteredError(self, dex_file); |
| 3996 | return nullptr; |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3997 | } |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3998 | if (h_dex_cache == nullptr) { |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3999 | self->AssertPendingOOMException(); |
| 4000 | return nullptr; |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 4001 | } |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 4002 | table->InsertStrongRoot(h_dex_cache.Get()); |
Mathieu Chartier | a1467d0 | 2017-02-22 09:22:50 -0800 | [diff] [blame] | 4003 | if (h_class_loader.Get() != nullptr) { |
| 4004 | // Since we added a strong root to the class table, do the write barrier as required for |
| 4005 | // remembered sets and generational GCs. |
Mathieu Chartier | 88ea61e | 2018-06-20 17:45:41 -0700 | [diff] [blame] | 4006 | WriteBarrier::ForEveryFieldWrite(h_class_loader.Get()); |
Mathieu Chartier | a1467d0 | 2017-02-22 09:22:50 -0800 | [diff] [blame] | 4007 | } |
Nicolas Geoffray | 1d4f009 | 2020-08-07 14:01:05 +0100 | [diff] [blame] | 4008 | VLOG(class_linker) << "Registered dex file " << dex_file.GetLocation(); |
Nicolas Geoffray | 4f6bb44 | 2021-06-02 18:05:51 +0100 | [diff] [blame] | 4009 | PaletteNotifyDexFileLoaded(dex_file.GetLocation().c_str()); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 4010 | return h_dex_cache.Get(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 4011 | } |
| 4012 | |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4013 | bool ClassLinker::IsDexFileRegistered(Thread* self, const DexFile& dex_file) { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 4014 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 4015 | return DecodeDexCacheLocked(self, FindDexCacheDataLocked(dex_file)) != nullptr; |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 4016 | } |
| 4017 | |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4018 | ObjPtr<mirror::DexCache> ClassLinker::FindDexCache(Thread* self, const DexFile& dex_file) { |
| 4019 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 4020 | const DexCacheData* dex_cache_data = FindDexCacheDataLocked(dex_file); |
| 4021 | ObjPtr<mirror::DexCache> dex_cache = DecodeDexCacheLocked(self, dex_cache_data); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4022 | if (dex_cache != nullptr) { |
| 4023 | return dex_cache; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 4024 | } |
Brian Carlstrom | 81a9087 | 2015-08-28 09:07:14 -0700 | [diff] [blame] | 4025 | // Failure, dump diagnostic and abort. |
David Srbecky | 6fbcc29 | 2021-02-23 01:05:32 +0000 | [diff] [blame] | 4026 | for (const auto& entry : dex_caches_) { |
| 4027 | const DexCacheData& data = entry.second; |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 4028 | if (DecodeDexCacheLocked(self, &data) != nullptr) { |
David Srbecky | 6fbcc29 | 2021-02-23 01:05:32 +0000 | [diff] [blame] | 4029 | LOG(FATAL_WITHOUT_ABORT) << "Registered dex file " << entry.first->GetLocation(); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 4030 | } |
Brian Carlstrom | 81a9087 | 2015-08-28 09:07:14 -0700 | [diff] [blame] | 4031 | } |
Mathieu Chartier | 9e050df | 2017-08-09 10:05:47 -0700 | [diff] [blame] | 4032 | LOG(FATAL) << "Failed to find DexCache for DexFile " << dex_file.GetLocation() |
David Srbecky | 6fbcc29 | 2021-02-23 01:05:32 +0000 | [diff] [blame] | 4033 | << " " << &dex_file; |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 4034 | UNREACHABLE(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4035 | } |
| 4036 | |
Santiago Aboy Solanes | 4b8ea5d | 2021-11-19 10:14:54 +0000 | [diff] [blame] | 4037 | 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] | 4038 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
| 4039 | const DexCacheData* dex_cache_data = FindDexCacheDataLocked(oat_dex_file); |
| 4040 | ObjPtr<mirror::DexCache> dex_cache = DecodeDexCacheLocked(self, dex_cache_data); |
| 4041 | if (dex_cache != nullptr) { |
| 4042 | return dex_cache; |
| 4043 | } |
| 4044 | // Failure, dump diagnostic and abort. |
| 4045 | for (const auto& entry : dex_caches_) { |
| 4046 | const DexCacheData& data = entry.second; |
| 4047 | if (DecodeDexCacheLocked(self, &data) != nullptr) { |
| 4048 | LOG(FATAL_WITHOUT_ABORT) << "Registered dex file " << entry.first->GetLocation(); |
| 4049 | } |
| 4050 | } |
Santiago Aboy Solanes | 4b8ea5d | 2021-11-19 10:14:54 +0000 | [diff] [blame] | 4051 | 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] | 4052 | << " " << &oat_dex_file; |
| 4053 | UNREACHABLE(); |
| 4054 | } |
| 4055 | |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4056 | ClassTable* ClassLinker::FindClassTable(Thread* self, ObjPtr<mirror::DexCache> dex_cache) { |
| 4057 | const DexFile* dex_file = dex_cache->GetDexFile(); |
| 4058 | DCHECK(dex_file != nullptr); |
| 4059 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
David Srbecky | 6fbcc29 | 2021-02-23 01:05:32 +0000 | [diff] [blame] | 4060 | auto it = dex_caches_.find(dex_file); |
| 4061 | if (it != dex_caches_.end()) { |
| 4062 | const DexCacheData& data = it->second; |
| 4063 | ObjPtr<mirror::DexCache> registered_dex_cache = DecodeDexCacheLocked(self, &data); |
| 4064 | if (registered_dex_cache != nullptr) { |
| 4065 | CHECK_EQ(registered_dex_cache, dex_cache) << dex_file->GetLocation(); |
| 4066 | return data.class_table; |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4067 | } |
| 4068 | } |
| 4069 | return nullptr; |
| 4070 | } |
| 4071 | |
Santiago Aboy Solanes | 970ba21 | 2021-10-21 10:52:47 +0100 | [diff] [blame] | 4072 | const ClassLinker::DexCacheData* ClassLinker::FindDexCacheDataLocked( |
Santiago Aboy Solanes | 4b8ea5d | 2021-11-19 10:14:54 +0000 | [diff] [blame] | 4073 | const OatDexFile& oat_dex_file) { |
| 4074 | auto it = std::find_if(dex_caches_.begin(), dex_caches_.end(), [&](const auto& entry) { |
| 4075 | return entry.first->GetOatDexFile() == &oat_dex_file; |
Santiago Aboy Solanes | 970ba21 | 2021-10-21 10:52:47 +0100 | [diff] [blame] | 4076 | }); |
| 4077 | return it != dex_caches_.end() ? &it->second : nullptr; |
| 4078 | } |
| 4079 | |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 4080 | const ClassLinker::DexCacheData* ClassLinker::FindDexCacheDataLocked(const DexFile& dex_file) { |
David Srbecky | 6fbcc29 | 2021-02-23 01:05:32 +0000 | [diff] [blame] | 4081 | auto it = dex_caches_.find(&dex_file); |
| 4082 | return it != dex_caches_.end() ? &it->second : nullptr; |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4083 | } |
| 4084 | |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 4085 | void ClassLinker::CreatePrimitiveClass(Thread* self, |
| 4086 | Primitive::Type type, |
| 4087 | ClassRoot primitive_root) { |
Vladimir Marko | acb906d | 2018-05-30 10:23:49 +0100 | [diff] [blame] | 4088 | ObjPtr<mirror::Class> primitive_class = |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 4089 | AllocClass(self, mirror::Class::PrimitiveClassSize(image_pointer_size_)); |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 4090 | CHECK(primitive_class != nullptr) << "OOM for primitive class " << type; |
| 4091 | // Do not hold lock on the primitive class object, the initialization of |
| 4092 | // primitive classes is done while the process is still single threaded. |
Nicolas Geoffray | 66934ef | 2021-07-07 14:56:23 +0100 | [diff] [blame] | 4093 | primitive_class->SetAccessFlagsDuringLinking(kAccPublic | kAccFinal | kAccAbstract); |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 4094 | primitive_class->SetPrimitiveType(type); |
| 4095 | primitive_class->SetIfTable(GetClassRoot<mirror::Object>(this)->GetIfTable()); |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 4096 | DCHECK_EQ(primitive_class->NumMethods(), 0u); |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 4097 | // Primitive classes are initialized during single threaded startup, so visibly initialized. |
| 4098 | primitive_class->SetStatusForPrimitiveOrArray(ClassStatus::kVisiblyInitialized); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4099 | const char* descriptor = Primitive::Descriptor(type); |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 4100 | ObjPtr<mirror::Class> existing = InsertClass(descriptor, |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 4101 | primitive_class, |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 4102 | ComputeModifiedUtf8Hash(descriptor)); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4103 | CHECK(existing == nullptr) << "InitPrimitiveClass(" << type << ") failed"; |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 4104 | SetClassRoot(primitive_root, primitive_class); |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 4105 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4106 | |
Vladimir Marko | 0261055 | 2018-06-04 14:38:00 +0100 | [diff] [blame] | 4107 | inline ObjPtr<mirror::IfTable> ClassLinker::GetArrayIfTable() { |
| 4108 | return GetClassRoot<mirror::ObjectArray<mirror::Object>>(this)->GetIfTable(); |
| 4109 | } |
| 4110 | |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 4111 | // Create an array class (i.e. the class object for the array, not the |
| 4112 | // array itself). "descriptor" looks like "[C" or "[[[[B" or |
| 4113 | // "[Ljava/lang/String;". |
| 4114 | // |
| 4115 | // If "descriptor" refers to an array of primitives, look up the |
| 4116 | // primitive type's internally-generated class object. |
| 4117 | // |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 4118 | // "class_loader" is the class loader of the class that's referring to |
| 4119 | // us. It's used to ensure that we're looking for the element type in |
| 4120 | // the right context. It does NOT become the class loader for the |
| 4121 | // array class; that always comes from the base element class. |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 4122 | // |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 4123 | // Returns null with an exception raised on failure. |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 4124 | ObjPtr<mirror::Class> ClassLinker::CreateArrayClass(Thread* self, |
| 4125 | const char* descriptor, |
| 4126 | size_t hash, |
| 4127 | Handle<mirror::ClassLoader> class_loader) { |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 4128 | // Identify the underlying component type |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4129 | CHECK_EQ('[', descriptor[0]); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4130 | StackHandleScope<2> hs(self); |
Alex Light | e9f6103 | 2018-09-24 16:04:51 -0700 | [diff] [blame] | 4131 | |
| 4132 | // This is to prevent the calls to ClassLoad and ClassPrepare which can cause java/user-supplied |
| 4133 | // code to be executed. We put it up here so we can avoid all the allocations associated with |
| 4134 | // creating the class. This can happen with (eg) jit threads. |
| 4135 | if (!self->CanLoadClasses()) { |
| 4136 | // Make sure we don't try to load anything, potentially causing an infinite loop. |
| 4137 | ObjPtr<mirror::Throwable> pre_allocated = |
| 4138 | Runtime::Current()->GetPreAllocatedNoClassDefFoundError(); |
| 4139 | self->SetException(pre_allocated); |
| 4140 | return nullptr; |
| 4141 | } |
| 4142 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4143 | MutableHandle<mirror::Class> component_type(hs.NewHandle(FindClass(self, descriptor + 1, |
| 4144 | class_loader))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4145 | if (component_type == nullptr) { |
Mathieu Chartier | c0a9ea4 | 2014-02-03 16:36:49 -0800 | [diff] [blame] | 4146 | DCHECK(self->IsExceptionPending()); |
Andreas Gampe | dc13d7d | 2014-07-23 20:18:36 -0700 | [diff] [blame] | 4147 | // We need to accept erroneous classes as component types. |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 4148 | const size_t component_hash = ComputeModifiedUtf8Hash(descriptor + 1); |
| 4149 | component_type.Assign(LookupClass(self, descriptor + 1, component_hash, class_loader.Get())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4150 | if (component_type == nullptr) { |
Andreas Gampe | dc13d7d | 2014-07-23 20:18:36 -0700 | [diff] [blame] | 4151 | DCHECK(self->IsExceptionPending()); |
| 4152 | return nullptr; |
| 4153 | } else { |
| 4154 | self->ClearException(); |
| 4155 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 4156 | } |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 4157 | if (UNLIKELY(component_type->IsPrimitiveVoid())) { |
| 4158 | ThrowNoClassDefFoundError("Attempt to create array of void primitive type"); |
| 4159 | return nullptr; |
| 4160 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 4161 | // See if the component type is already loaded. Array classes are |
| 4162 | // always associated with the class loader of their underlying |
| 4163 | // element type -- an array of Strings goes with the loader for |
| 4164 | // java/lang/String -- so we need to look for it there. (The |
| 4165 | // caller should have checked for the existence of the class |
| 4166 | // before calling here, but they did so with *their* class loader, |
| 4167 | // not the component type's loader.) |
| 4168 | // |
| 4169 | // If we find it, the caller adds "loader" to the class' initiating |
| 4170 | // loader list, which should prevent us from going through this again. |
| 4171 | // |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4172 | // This call is unnecessary if "loader" and "component_type->GetClassLoader()" |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 4173 | // are the same, because our caller (FindClass) just did the |
| 4174 | // lookup. (Even if we get this wrong we still have correct behavior, |
| 4175 | // because we effectively do this lookup again when we add the new |
| 4176 | // class to the hash table --- necessary because of possible races with |
| 4177 | // other threads.) |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4178 | if (class_loader.Get() != component_type->GetClassLoader()) { |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 4179 | ObjPtr<mirror::Class> new_class = |
| 4180 | LookupClass(self, descriptor, hash, component_type->GetClassLoader()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4181 | if (new_class != nullptr) { |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 4182 | return new_class; |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 4183 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 4184 | } |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 4185 | // Core array classes, i.e. Object[], Class[], String[] and primitive |
| 4186 | // arrays, have special initialization and they should be found above. |
Santiago Aboy Solanes | 6cdabe1 | 2022-02-18 15:27:43 +0000 | [diff] [blame] | 4187 | DCHECK_IMPLIES(component_type->IsObjectClass(), |
| 4188 | // Guard from false positives for errors before setting superclass. |
| 4189 | component_type->IsErroneousUnresolved()); |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 4190 | DCHECK(!component_type->IsStringClass()); |
| 4191 | DCHECK(!component_type->IsClassClass()); |
| 4192 | DCHECK(!component_type->IsPrimitive()); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 4193 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 4194 | // Fill out the fields in the Class. |
| 4195 | // |
| 4196 | // It is possible to execute some methods against arrays, because |
| 4197 | // all arrays are subclasses of java_lang_Object_, so we need to set |
| 4198 | // up a vtable. We can just point at the one in java_lang_Object_. |
| 4199 | // |
| 4200 | // Array classes are simple enough that we don't need to do a full |
| 4201 | // link step. |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 4202 | size_t array_class_size = mirror::Array::ClassSize(image_pointer_size_); |
| 4203 | auto visitor = [this, array_class_size, component_type](ObjPtr<mirror::Object> obj, |
| 4204 | size_t usable_size) |
| 4205 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | b68bb7a | 2020-03-17 10:55:25 +0000 | [diff] [blame] | 4206 | ScopedAssertNoNewTransactionRecords sanntr("CreateArrayClass"); |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 4207 | mirror::Class::InitializeClassVisitor init_class(array_class_size); |
| 4208 | init_class(obj, usable_size); |
| 4209 | ObjPtr<mirror::Class> klass = ObjPtr<mirror::Class>::DownCast(obj); |
| 4210 | klass->SetComponentType(component_type.Get()); |
| 4211 | // Do not hold lock for initialization, the fence issued after the visitor |
| 4212 | // returns ensures memory visibility together with the implicit consume |
| 4213 | // semantics (for all supported architectures) for any thread that loads |
| 4214 | // the array class reference from any memory locations afterwards. |
| 4215 | FinishArrayClassSetup(klass); |
| 4216 | }; |
| 4217 | auto new_class = hs.NewHandle<mirror::Class>( |
| 4218 | AllocClass(self, GetClassRoot<mirror::Class>(this), array_class_size, visitor)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4219 | if (new_class == nullptr) { |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 4220 | self->AssertPendingOOMException(); |
| 4221 | return nullptr; |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 4222 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 4223 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4224 | ObjPtr<mirror::Class> existing = InsertClass(descriptor, new_class.Get(), hash); |
Mathieu Chartier | c0a9ea4 | 2014-02-03 16:36:49 -0800 | [diff] [blame] | 4225 | if (existing == nullptr) { |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4226 | // We postpone ClassLoad and ClassPrepare events to this point in time to avoid |
| 4227 | // duplicate events in case of races. Array classes don't really follow dedicated |
| 4228 | // load and prepare, anyways. |
| 4229 | Runtime::Current()->GetRuntimeCallbacks()->ClassLoad(new_class); |
| 4230 | Runtime::Current()->GetRuntimeCallbacks()->ClassPrepare(new_class, new_class); |
| 4231 | |
Tamas Berghammer | 160e6df | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 4232 | jit::Jit::NewTypeLoadedIfUsingJit(new_class.Get()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4233 | return new_class.Get(); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 4234 | } |
| 4235 | // Another thread must have loaded the class after we |
| 4236 | // started but before we finished. Abandon what we've |
| 4237 | // done. |
| 4238 | // |
| 4239 | // (Yes, this happens.) |
| 4240 | |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 4241 | return existing; |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 4242 | } |
| 4243 | |
Vladimir Marko | 9186b18 | 2018-11-06 14:55:54 +0000 | [diff] [blame] | 4244 | ObjPtr<mirror::Class> ClassLinker::LookupPrimitiveClass(char type) { |
| 4245 | ClassRoot class_root; |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 4246 | switch (type) { |
Vladimir Marko | 9186b18 | 2018-11-06 14:55:54 +0000 | [diff] [blame] | 4247 | case 'B': class_root = ClassRoot::kPrimitiveByte; break; |
| 4248 | case 'C': class_root = ClassRoot::kPrimitiveChar; break; |
| 4249 | case 'D': class_root = ClassRoot::kPrimitiveDouble; break; |
| 4250 | case 'F': class_root = ClassRoot::kPrimitiveFloat; break; |
| 4251 | case 'I': class_root = ClassRoot::kPrimitiveInt; break; |
| 4252 | case 'J': class_root = ClassRoot::kPrimitiveLong; break; |
| 4253 | case 'S': class_root = ClassRoot::kPrimitiveShort; break; |
| 4254 | case 'Z': class_root = ClassRoot::kPrimitiveBoolean; break; |
| 4255 | case 'V': class_root = ClassRoot::kPrimitiveVoid; break; |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 4256 | default: |
Vladimir Marko | 9186b18 | 2018-11-06 14:55:54 +0000 | [diff] [blame] | 4257 | return nullptr; |
Carl Shapiro | 744ad05 | 2011-08-06 15:53:36 -0700 | [diff] [blame] | 4258 | } |
Vladimir Marko | 9186b18 | 2018-11-06 14:55:54 +0000 | [diff] [blame] | 4259 | return GetClassRoot(class_root, this); |
| 4260 | } |
| 4261 | |
| 4262 | ObjPtr<mirror::Class> ClassLinker::FindPrimitiveClass(char type) { |
| 4263 | ObjPtr<mirror::Class> result = LookupPrimitiveClass(type); |
| 4264 | if (UNLIKELY(result == nullptr)) { |
| 4265 | std::string printable_type(PrintableChar(type)); |
| 4266 | ThrowNoClassDefFoundError("Not a primitive type: %s", printable_type.c_str()); |
| 4267 | } |
| 4268 | return result; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4269 | } |
| 4270 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 4271 | ObjPtr<mirror::Class> ClassLinker::InsertClass(const char* descriptor, |
| 4272 | ObjPtr<mirror::Class> klass, |
| 4273 | size_t hash) { |
Alex Light | e9f6103 | 2018-09-24 16:04:51 -0700 | [diff] [blame] | 4274 | DCHECK(Thread::Current()->CanLoadClasses()); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 4275 | if (VLOG_IS_ON(class_linker)) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4276 | ObjPtr<mirror::DexCache> dex_cache = klass->GetDexCache(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 4277 | std::string source; |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4278 | if (dex_cache != nullptr) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 4279 | source += " from "; |
| 4280 | source += dex_cache->GetLocation()->ToModifiedUtf8(); |
| 4281 | } |
| 4282 | LOG(INFO) << "Loaded class " << descriptor << source; |
| 4283 | } |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 4284 | { |
| 4285 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Vladimir Marko | 0984e48 | 2019-03-27 16:41:41 +0000 | [diff] [blame] | 4286 | const ObjPtr<mirror::ClassLoader> class_loader = klass->GetClassLoader(); |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 4287 | ClassTable* const class_table = InsertClassTableForClassLoader(class_loader); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4288 | ObjPtr<mirror::Class> existing = class_table->Lookup(descriptor, hash); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4289 | if (existing != nullptr) { |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 4290 | return existing; |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 4291 | } |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 4292 | VerifyObject(klass); |
| 4293 | class_table->InsertWithHash(klass, hash); |
| 4294 | if (class_loader != nullptr) { |
| 4295 | // 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] | 4296 | WriteBarrier::ForEveryFieldWrite(class_loader); |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 4297 | } |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 4298 | if (log_new_roots_) { |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 4299 | new_class_roots_.push_back(GcRoot<mirror::Class>(klass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4300 | } |
| 4301 | } |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 4302 | if (kIsDebugBuild) { |
| 4303 | // Test that copied methods correctly can find their holder. |
| 4304 | for (ArtMethod& method : klass->GetCopiedMethods(image_pointer_size_)) { |
| 4305 | CHECK_EQ(GetHoldingClassOfCopiedMethod(&method), klass); |
| 4306 | } |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 4307 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4308 | return nullptr; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4309 | } |
| 4310 | |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 4311 | void ClassLinker::WriteBarrierForBootOatFileBssRoots(const OatFile* oat_file) { |
Mathieu Chartier | a1467d0 | 2017-02-22 09:22:50 -0800 | [diff] [blame] | 4312 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
| 4313 | DCHECK(!oat_file->GetBssGcRoots().empty()) << oat_file->GetLocation(); |
| 4314 | if (log_new_roots_ && !ContainsElement(new_bss_roots_boot_oat_files_, oat_file)) { |
| 4315 | new_bss_roots_boot_oat_files_.push_back(oat_file); |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 4316 | } |
| 4317 | } |
| 4318 | |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4319 | // TODO This should really be in mirror::Class. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4320 | void ClassLinker::UpdateClassMethods(ObjPtr<mirror::Class> klass, |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4321 | LengthPrefixedArray<ArtMethod>* new_methods) { |
| 4322 | klass->SetMethodsPtrUnchecked(new_methods, |
| 4323 | klass->NumDirectMethods(), |
| 4324 | klass->NumDeclaredVirtualMethods()); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4325 | // 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] | 4326 | WriteBarrier::ForEveryFieldWrite(klass); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4327 | } |
| 4328 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 4329 | ObjPtr<mirror::Class> ClassLinker::LookupClass(Thread* self, |
| 4330 | const char* descriptor, |
| 4331 | ObjPtr<mirror::ClassLoader> class_loader) { |
Andreas Gampe | 2ff3b97 | 2017-06-05 18:14:53 -0700 | [diff] [blame] | 4332 | return LookupClass(self, descriptor, ComputeModifiedUtf8Hash(descriptor), class_loader); |
| 4333 | } |
| 4334 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 4335 | ObjPtr<mirror::Class> ClassLinker::LookupClass(Thread* self, |
| 4336 | const char* descriptor, |
| 4337 | size_t hash, |
| 4338 | ObjPtr<mirror::ClassLoader> class_loader) { |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 4339 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 4340 | ClassTable* const class_table = ClassTableForClassLoader(class_loader); |
| 4341 | if (class_table != nullptr) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4342 | ObjPtr<mirror::Class> result = class_table->Lookup(descriptor, hash); |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 4343 | if (result != nullptr) { |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 4344 | return result; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4345 | } |
Sameer Abu Asal | 2c6de22 | 2013-05-02 17:38:59 -0700 | [diff] [blame] | 4346 | } |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 4347 | return nullptr; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4348 | } |
| 4349 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4350 | class MoveClassTableToPreZygoteVisitor : public ClassLoaderVisitor { |
| 4351 | public: |
Igor Murashkin | 2ffb703 | 2017-11-08 13:35:21 -0800 | [diff] [blame] | 4352 | MoveClassTableToPreZygoteVisitor() {} |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4353 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4354 | void Visit(ObjPtr<mirror::ClassLoader> class_loader) |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4355 | REQUIRES(Locks::classlinker_classes_lock_) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 4356 | REQUIRES_SHARED(Locks::mutator_lock_) override { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4357 | ClassTable* const class_table = class_loader->GetClassTable(); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 4358 | if (class_table != nullptr) { |
| 4359 | class_table->FreezeSnapshot(); |
| 4360 | } |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 4361 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4362 | }; |
| 4363 | |
| 4364 | void ClassLinker::MoveClassTableToPreZygote() { |
| 4365 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 4366 | boot_class_table_->FreezeSnapshot(); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4367 | MoveClassTableToPreZygoteVisitor visitor; |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 4368 | VisitClassLoaders(&visitor); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 4369 | } |
| 4370 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4371 | // Look up classes by hash and descriptor and put all matching ones in the result array. |
| 4372 | class LookupClassesVisitor : public ClassLoaderVisitor { |
| 4373 | public: |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4374 | LookupClassesVisitor(const char* descriptor, |
| 4375 | size_t hash, |
| 4376 | std::vector<ObjPtr<mirror::Class>>* result) |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4377 | : descriptor_(descriptor), |
| 4378 | hash_(hash), |
| 4379 | result_(result) {} |
| 4380 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4381 | void Visit(ObjPtr<mirror::ClassLoader> class_loader) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 4382 | REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_) override { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4383 | ClassTable* const class_table = class_loader->GetClassTable(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4384 | ObjPtr<mirror::Class> klass = class_table->Lookup(descriptor_, hash_); |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 4385 | // Add `klass` only if `class_loader` is its defining (not just initiating) class loader. |
| 4386 | if (klass != nullptr && klass->GetClassLoader() == class_loader) { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4387 | result_->push_back(klass); |
| 4388 | } |
| 4389 | } |
| 4390 | |
| 4391 | private: |
| 4392 | const char* const descriptor_; |
| 4393 | const size_t hash_; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4394 | std::vector<ObjPtr<mirror::Class>>* const result_; |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4395 | }; |
| 4396 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4397 | void ClassLinker::LookupClasses(const char* descriptor, |
| 4398 | std::vector<ObjPtr<mirror::Class>>& result) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4399 | result.clear(); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4400 | Thread* const self = Thread::Current(); |
| 4401 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 4402 | const size_t hash = ComputeModifiedUtf8Hash(descriptor); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 4403 | ObjPtr<mirror::Class> klass = boot_class_table_->Lookup(descriptor, hash); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 4404 | if (klass != nullptr) { |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 4405 | DCHECK(klass->GetClassLoader() == nullptr); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 4406 | result.push_back(klass); |
| 4407 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4408 | LookupClassesVisitor visitor(descriptor, hash, &result); |
| 4409 | VisitClassLoaders(&visitor); |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 4410 | } |
| 4411 | |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4412 | bool ClassLinker::AttemptSupertypeVerification(Thread* self, |
Nicolas Geoffray | 5b0b2e1 | 2021-03-19 14:48:40 +0000 | [diff] [blame] | 4413 | verifier::VerifierDeps* verifier_deps, |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4414 | Handle<mirror::Class> klass, |
| 4415 | Handle<mirror::Class> supertype) { |
| 4416 | DCHECK(self != nullptr); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4417 | DCHECK(klass != nullptr); |
| 4418 | DCHECK(supertype != nullptr); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4419 | |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4420 | if (!supertype->IsVerified() && !supertype->IsErroneous()) { |
Nicolas Geoffray | 5b0b2e1 | 2021-03-19 14:48:40 +0000 | [diff] [blame] | 4421 | VerifyClass(self, verifier_deps, supertype); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4422 | } |
Nicolas Geoffray | 7cc3ae5 | 2017-03-07 14:33:37 +0000 | [diff] [blame] | 4423 | |
Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 4424 | if (supertype->IsVerified() |
| 4425 | || supertype->ShouldVerifyAtRuntime() |
| 4426 | || supertype->IsVerifiedNeedsAccessChecks()) { |
Nicolas Geoffray | 7cc3ae5 | 2017-03-07 14:33:37 +0000 | [diff] [blame] | 4427 | // The supertype is either verified, or we soft failed at AOT time. |
| 4428 | DCHECK(supertype->IsVerified() || Runtime::Current()->IsAotCompiler()); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4429 | return true; |
| 4430 | } |
| 4431 | // If we got this far then we have a hard failure. |
| 4432 | std::string error_msg = |
| 4433 | StringPrintf("Rejecting class %s that attempts to sub-type erroneous class %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4434 | klass->PrettyDescriptor().c_str(), |
| 4435 | supertype->PrettyDescriptor().c_str()); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4436 | LOG(WARNING) << error_msg << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8(); |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4437 | StackHandleScope<1> hs(self); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4438 | Handle<mirror::Throwable> cause(hs.NewHandle(self->GetException())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4439 | if (cause != nullptr) { |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4440 | // Set during VerifyClass call (if at all). |
| 4441 | self->ClearException(); |
| 4442 | } |
| 4443 | // Change into a verify error. |
| 4444 | ThrowVerifyError(klass.Get(), "%s", error_msg.c_str()); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4445 | if (cause != nullptr) { |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4446 | self->GetException()->SetCause(cause.Get()); |
| 4447 | } |
| 4448 | ClassReference ref(klass->GetDexCache()->GetDexFile(), klass->GetDexClassDefIndex()); |
| 4449 | if (Runtime::Current()->IsAotCompiler()) { |
| 4450 | Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref); |
| 4451 | } |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4452 | // Need to grab the lock to change status. |
| 4453 | ObjectLock<mirror::Class> super_lock(self, klass); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4454 | mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4455 | return false; |
| 4456 | } |
| 4457 | |
Nicolas Geoffray | 5b0b2e1 | 2021-03-19 14:48:40 +0000 | [diff] [blame] | 4458 | verifier::FailureKind ClassLinker::VerifyClass(Thread* self, |
| 4459 | verifier::VerifierDeps* verifier_deps, |
| 4460 | Handle<mirror::Class> klass, |
| 4461 | verifier::HardFailLogMode log_level) { |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4462 | { |
| 4463 | // TODO: assert that the monitor on the Class is held |
| 4464 | ObjectLock<mirror::Class> lock(self, klass); |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 4465 | |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4466 | // Is somebody verifying this now? |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4467 | ClassStatus old_status = klass->GetStatus(); |
Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 4468 | while (old_status == ClassStatus::kVerifying) { |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4469 | lock.WaitIgnoringInterrupts(); |
Mathieu Chartier | 5ef7020 | 2017-06-29 10:45:10 -0700 | [diff] [blame] | 4470 | // WaitIgnoringInterrupts can still receive an interrupt and return early, in this |
| 4471 | // case we may see the same status again. b/62912904. This is why the check is |
| 4472 | // greater or equal. |
| 4473 | CHECK(klass->IsErroneous() || (klass->GetStatus() >= old_status)) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4474 | << "Class '" << klass->PrettyClass() |
| 4475 | << "' performed an illegal verification state transition from " << old_status |
| 4476 | << " to " << klass->GetStatus(); |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4477 | old_status = klass->GetStatus(); |
| 4478 | } |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 4479 | |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4480 | // The class might already be erroneous, for example at compile time if we attempted to verify |
| 4481 | // this class as a parent to another. |
| 4482 | if (klass->IsErroneous()) { |
| 4483 | ThrowEarlierClassFailure(klass.Get()); |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4484 | return verifier::FailureKind::kHardFailure; |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4485 | } |
Brian Carlstrom | 9b5ee88 | 2012-02-28 09:48:54 -0800 | [diff] [blame] | 4486 | |
Nicolas Geoffray | 7cc3ae5 | 2017-03-07 14:33:37 +0000 | [diff] [blame] | 4487 | // Don't attempt to re-verify if already verified. |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4488 | if (klass->IsVerified()) { |
Nicolas Geoffray | 8078996 | 2021-04-30 16:50:39 +0100 | [diff] [blame] | 4489 | if (verifier_deps != nullptr && |
| 4490 | verifier_deps->ContainsDexFile(klass->GetDexFile()) && |
| 4491 | !verifier_deps->HasRecordedVerifiedStatus(klass->GetDexFile(), *klass->GetClassDef()) && |
| 4492 | !Runtime::Current()->IsAotCompiler()) { |
| 4493 | // If the klass is verified, but `verifier_deps` did not record it, this |
| 4494 | // means we are running background verification of a secondary dex file. |
| 4495 | // Re-run the verifier to populate `verifier_deps`. |
| 4496 | // No need to run the verification when running on the AOT Compiler, as |
| 4497 | // the driver handles those multithreaded cases already. |
| 4498 | std::string error_msg; |
| 4499 | verifier::FailureKind failure = |
| 4500 | PerformClassVerification(self, verifier_deps, klass, log_level, &error_msg); |
| 4501 | // We could have soft failures, so just check that we don't have a hard |
| 4502 | // failure. |
| 4503 | DCHECK_NE(failure, verifier::FailureKind::kHardFailure) << error_msg; |
| 4504 | } |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4505 | return verifier::FailureKind::kNoFailure; |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4506 | } |
Nicolas Geoffray | 7cc3ae5 | 2017-03-07 14:33:37 +0000 | [diff] [blame] | 4507 | |
Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 4508 | if (klass->IsVerifiedNeedsAccessChecks()) { |
| 4509 | if (!Runtime::Current()->IsAotCompiler()) { |
| 4510 | // Mark the class as having a verification attempt to avoid re-running |
Nicolas Geoffray | 66934ef | 2021-07-07 14:56:23 +0100 | [diff] [blame] | 4511 | // the verifier. |
Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 4512 | mirror::Class::SetStatus(klass, ClassStatus::kVerified, self); |
| 4513 | } |
| 4514 | return verifier::FailureKind::kAccessChecksFailure; |
| 4515 | } |
| 4516 | |
Nicolas Geoffray | 7cc3ae5 | 2017-03-07 14:33:37 +0000 | [diff] [blame] | 4517 | // For AOT, don't attempt to re-verify if we have already found we should |
| 4518 | // verify at runtime. |
Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 4519 | if (klass->ShouldVerifyAtRuntime()) { |
| 4520 | CHECK(Runtime::Current()->IsAotCompiler()); |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4521 | return verifier::FailureKind::kSoftFailure; |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4522 | } |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 4523 | |
Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 4524 | DCHECK_EQ(klass->GetStatus(), ClassStatus::kResolved); |
| 4525 | mirror::Class::SetStatus(klass, ClassStatus::kVerifying, self); |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4526 | |
| 4527 | // Skip verification if disabled. |
| 4528 | if (!Runtime::Current()->IsVerificationEnabled()) { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4529 | mirror::Class::SetStatus(klass, ClassStatus::kVerified, self); |
Nicolas Geoffray | 66934ef | 2021-07-07 14:56:23 +0100 | [diff] [blame] | 4530 | UpdateClassAfterVerification(klass, image_pointer_size_, verifier::FailureKind::kNoFailure); |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4531 | return verifier::FailureKind::kNoFailure; |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4532 | } |
Jeff Hao | 4a200f5 | 2014-04-01 14:58:49 -0700 | [diff] [blame] | 4533 | } |
| 4534 | |
Bharadwaj Kalandhabhatta | 271c1e1 | 2017-06-27 11:14:49 -0700 | [diff] [blame] | 4535 | VLOG(class_linker) << "Beginning verification for class: " |
| 4536 | << klass->PrettyDescriptor() |
| 4537 | << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8(); |
| 4538 | |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 4539 | // Verify super class. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4540 | StackHandleScope<2> hs(self); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4541 | MutableHandle<mirror::Class> supertype(hs.NewHandle(klass->GetSuperClass())); |
| 4542 | // 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] | 4543 | if (supertype != nullptr && |
| 4544 | !AttemptSupertypeVerification(self, verifier_deps, klass, supertype)) { |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4545 | CHECK(self->IsExceptionPending()) << "Verification error should be pending."; |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4546 | return verifier::FailureKind::kHardFailure; |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4547 | } |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 4548 | |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4549 | // Verify all default super-interfaces. |
| 4550 | // |
| 4551 | // (1) Don't bother if the superclass has already had a soft verification failure. |
| 4552 | // |
| 4553 | // (2) Interfaces shouldn't bother to do this recursive verification because they cannot cause |
| 4554 | // recursive initialization by themselves. This is because when an interface is initialized |
| 4555 | // directly it must not initialize its superinterfaces. We are allowed to verify regardless |
| 4556 | // but choose not to for an optimization. If the interfaces is being verified due to a class |
| 4557 | // initialization (which would need all the default interfaces to be verified) the class code |
| 4558 | // will trigger the recursive verification anyway. |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4559 | if ((supertype == nullptr || supertype->IsVerified()) // See (1) |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4560 | && !klass->IsInterface()) { // See (2) |
| 4561 | int32_t iftable_count = klass->GetIfTableCount(); |
| 4562 | MutableHandle<mirror::Class> iface(hs.NewHandle<mirror::Class>(nullptr)); |
| 4563 | // Loop through all interfaces this class has defined. It doesn't matter the order. |
| 4564 | for (int32_t i = 0; i < iftable_count; i++) { |
| 4565 | iface.Assign(klass->GetIfTable()->GetInterface(i)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4566 | DCHECK(iface != nullptr); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4567 | // We only care if we have default interfaces and can skip if we are already verified... |
| 4568 | if (LIKELY(!iface->HasDefaultMethods() || iface->IsVerified())) { |
| 4569 | continue; |
Nicolas Geoffray | 5b0b2e1 | 2021-03-19 14:48:40 +0000 | [diff] [blame] | 4570 | } else if (UNLIKELY(!AttemptSupertypeVerification(self, verifier_deps, klass, iface))) { |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4571 | // We had a hard failure while verifying this interface. Just return immediately. |
| 4572 | CHECK(self->IsExceptionPending()) << "Verification error should be pending."; |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4573 | return verifier::FailureKind::kHardFailure; |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4574 | } else if (UNLIKELY(!iface->IsVerified())) { |
| 4575 | // We softly failed to verify the iface. Stop checking and clean up. |
| 4576 | // Put the iface into the supertype handle so we know what caused us to fail. |
| 4577 | supertype.Assign(iface.Get()); |
| 4578 | break; |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 4579 | } |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 4580 | } |
| 4581 | } |
| 4582 | |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4583 | // At this point if verification failed, then supertype is the "first" supertype that failed |
| 4584 | // verification (without a specific order). If verification succeeded, then supertype is either |
| 4585 | // null or the original superclass of klass and is verified. |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4586 | DCHECK(supertype == nullptr || |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4587 | supertype.Get() == klass->GetSuperClass() || |
| 4588 | !supertype->IsVerified()); |
| 4589 | |
Elliott Hughes | 634eb2e | 2012-03-22 16:06:28 -0700 | [diff] [blame] | 4590 | // 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] | 4591 | const DexFile& dex_file = *klass->GetDexCache()->GetDexFile(); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4592 | ClassStatus oat_file_class_status(ClassStatus::kNotReady); |
Nicolas Geoffray | 6df4511 | 2021-02-07 21:51:58 +0000 | [diff] [blame] | 4593 | bool preverified = VerifyClassUsingOatFile(self, dex_file, klass, oat_file_class_status); |
Bharadwaj Kalandhabhatta | 271c1e1 | 2017-06-27 11:14:49 -0700 | [diff] [blame] | 4594 | |
| 4595 | VLOG(class_linker) << "Class preverified status for class " |
| 4596 | << klass->PrettyDescriptor() |
| 4597 | << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8() |
| 4598 | << ": " |
Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 4599 | << preverified |
| 4600 | << "( " << oat_file_class_status << ")"; |
Bharadwaj Kalandhabhatta | 271c1e1 | 2017-06-27 11:14:49 -0700 | [diff] [blame] | 4601 | |
Nicolas Geoffray | 44dc8a3 | 2021-06-21 15:23:49 +0100 | [diff] [blame] | 4602 | // If the oat file says the class had an error, re-run the verifier. That way we will either: |
| 4603 | // 1) Be successful at runtime, or |
| 4604 | // 2) Get a precise error message. |
Santiago Aboy Solanes | 6cdabe1 | 2022-02-18 15:27:43 +0000 | [diff] [blame] | 4605 | DCHECK_IMPLIES(mirror::Class::IsErroneous(oat_file_class_status), !preverified); |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 4606 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4607 | std::string error_msg; |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4608 | verifier::FailureKind verifier_failure = verifier::FailureKind::kNoFailure; |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 4609 | if (!preverified) { |
Nicolas Geoffray | 5b0b2e1 | 2021-03-19 14:48:40 +0000 | [diff] [blame] | 4610 | verifier_failure = PerformClassVerification(self, verifier_deps, klass, log_level, &error_msg); |
Nicolas Geoffray | 66934ef | 2021-07-07 14:56:23 +0100 | [diff] [blame] | 4611 | } else if (oat_file_class_status == ClassStatus::kVerifiedNeedsAccessChecks) { |
| 4612 | verifier_failure = verifier::FailureKind::kAccessChecksFailure; |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 4613 | } |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4614 | |
| 4615 | // Verification is done, grab the lock again. |
| 4616 | ObjectLock<mirror::Class> lock(self, klass); |
Nicolas Geoffray | 66934ef | 2021-07-07 14:56:23 +0100 | [diff] [blame] | 4617 | self->AssertNoPendingException(); |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4618 | |
Nicolas Geoffray | 66934ef | 2021-07-07 14:56:23 +0100 | [diff] [blame] | 4619 | if (verifier_failure == verifier::FailureKind::kHardFailure) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4620 | VLOG(verifier) << "Verification failed on class " << klass->PrettyDescriptor() |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 4621 | << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8() |
| 4622 | << " because: " << error_msg; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4623 | ThrowVerifyError(klass.Get(), "%s", error_msg.c_str()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4624 | mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self); |
Nicolas Geoffray | 66934ef | 2021-07-07 14:56:23 +0100 | [diff] [blame] | 4625 | return verifier_failure; |
jeffhao | 5cfd6fb | 2011-09-27 13:54:29 -0700 | [diff] [blame] | 4626 | } |
Nicolas Geoffray | 66934ef | 2021-07-07 14:56:23 +0100 | [diff] [blame] | 4627 | |
| 4628 | // Make sure all classes referenced by catch blocks are resolved. |
| 4629 | ResolveClassExceptionHandlerTypes(klass); |
| 4630 | |
Nicolas Geoffray | 486dda0 | 2017-09-11 14:15:52 +0100 | [diff] [blame] | 4631 | if (Runtime::Current()->IsAotCompiler()) { |
Nicolas Geoffray | 66934ef | 2021-07-07 14:56:23 +0100 | [diff] [blame] | 4632 | if (supertype != nullptr && supertype->ShouldVerifyAtRuntime()) { |
| 4633 | // Regardless of our own verification result, we need to verify the class |
| 4634 | // at runtime if the super class is not verified. This is required in case |
| 4635 | // we generate an app/boot image. |
Nicolas Geoffray | 66934ef | 2021-07-07 14:56:23 +0100 | [diff] [blame] | 4636 | mirror::Class::SetStatus(klass, ClassStatus::kRetryVerificationAtRuntime, self); |
| 4637 | } else if (verifier_failure == verifier::FailureKind::kNoFailure) { |
| 4638 | mirror::Class::SetStatus(klass, ClassStatus::kVerified, self); |
| 4639 | } else if (verifier_failure == verifier::FailureKind::kSoftFailure || |
| 4640 | verifier_failure == verifier::FailureKind::kTypeChecksFailure) { |
| 4641 | mirror::Class::SetStatus(klass, ClassStatus::kRetryVerificationAtRuntime, self); |
| 4642 | } else { |
| 4643 | mirror::Class::SetStatus(klass, ClassStatus::kVerifiedNeedsAccessChecks, self); |
| 4644 | } |
| 4645 | // Notify the compiler about the verification status, in case the class |
| 4646 | // was verified implicitly (eg super class of a compiled class). When the |
| 4647 | // compiler unloads dex file after compilation, we still want to keep |
| 4648 | // verification states. |
Nicolas Geoffray | 486dda0 | 2017-09-11 14:15:52 +0100 | [diff] [blame] | 4649 | Runtime::Current()->GetCompilerCallbacks()->UpdateClassState( |
| 4650 | ClassReference(&klass->GetDexFile(), klass->GetDexClassDefIndex()), klass->GetStatus()); |
Nicolas Geoffray | 66934ef | 2021-07-07 14:56:23 +0100 | [diff] [blame] | 4651 | } else { |
| 4652 | mirror::Class::SetStatus(klass, ClassStatus::kVerified, self); |
Nicolas Geoffray | 486dda0 | 2017-09-11 14:15:52 +0100 | [diff] [blame] | 4653 | } |
Nicolas Geoffray | 66934ef | 2021-07-07 14:56:23 +0100 | [diff] [blame] | 4654 | |
| 4655 | UpdateClassAfterVerification(klass, image_pointer_size_, verifier_failure); |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame] | 4656 | return verifier_failure; |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 4657 | } |
| 4658 | |
Mathieu Chartier | 9e050df | 2017-08-09 10:05:47 -0700 | [diff] [blame] | 4659 | verifier::FailureKind ClassLinker::PerformClassVerification(Thread* self, |
Nicolas Geoffray | 5b0b2e1 | 2021-03-19 14:48:40 +0000 | [diff] [blame] | 4660 | verifier::VerifierDeps* verifier_deps, |
Mathieu Chartier | 9e050df | 2017-08-09 10:05:47 -0700 | [diff] [blame] | 4661 | Handle<mirror::Class> klass, |
| 4662 | verifier::HardFailLogMode log_level, |
| 4663 | std::string* error_msg) { |
| 4664 | Runtime* const runtime = Runtime::Current(); |
Nicolas Geoffray | 7744b69 | 2021-07-06 16:19:32 +0100 | [diff] [blame] | 4665 | StackHandleScope<2> hs(self); |
| 4666 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache())); |
| 4667 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader())); |
Andreas Gampe | a43ba3d | 2019-03-13 15:49:20 -0700 | [diff] [blame] | 4668 | return verifier::ClassVerifier::VerifyClass(self, |
Nicolas Geoffray | 5b0b2e1 | 2021-03-19 14:48:40 +0000 | [diff] [blame] | 4669 | verifier_deps, |
Nicolas Geoffray | 7744b69 | 2021-07-06 16:19:32 +0100 | [diff] [blame] | 4670 | dex_cache->GetDexFile(), |
| 4671 | klass, |
| 4672 | dex_cache, |
| 4673 | class_loader, |
| 4674 | *klass->GetClassDef(), |
Andreas Gampe | a43ba3d | 2019-03-13 15:49:20 -0700 | [diff] [blame] | 4675 | runtime->GetCompilerCallbacks(), |
Andreas Gampe | a43ba3d | 2019-03-13 15:49:20 -0700 | [diff] [blame] | 4676 | log_level, |
| 4677 | Runtime::Current()->GetTargetSdkVersion(), |
| 4678 | error_msg); |
Mathieu Chartier | 9e050df | 2017-08-09 10:05:47 -0700 | [diff] [blame] | 4679 | } |
| 4680 | |
Nicolas Geoffray | 6df4511 | 2021-02-07 21:51:58 +0000 | [diff] [blame] | 4681 | bool ClassLinker::VerifyClassUsingOatFile(Thread* self, |
| 4682 | const DexFile& dex_file, |
| 4683 | Handle<mirror::Class> klass, |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4684 | ClassStatus& oat_file_class_status) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 4685 | // If we're compiling, we can only verify the class using the oat file if |
| 4686 | // 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] | 4687 | // the compilation unit (app - dependencies). We will let the compiler callback |
| 4688 | // tell us about the latter. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 4689 | if (Runtime::Current()->IsAotCompiler()) { |
Andreas Gampe | e993458 | 2018-01-19 21:23:04 -0800 | [diff] [blame] | 4690 | CompilerCallbacks* callbacks = Runtime::Current()->GetCompilerCallbacks(); |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 4691 | // We are compiling an app (not the image). |
Nicolas Geoffray | 6df4511 | 2021-02-07 21:51:58 +0000 | [diff] [blame] | 4692 | if (!callbacks->CanUseOatStatusForVerification(klass.Get())) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 4693 | return false; |
| 4694 | } |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4695 | } |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 4696 | |
Andreas Gampe | b40d361 | 2018-06-26 15:49:42 -0700 | [diff] [blame] | 4697 | const OatDexFile* oat_dex_file = dex_file.GetOatDexFile(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4698 | // 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] | 4699 | if (oat_dex_file == nullptr || oat_dex_file->GetOatFile() == nullptr) { |
Anwar Ghuloum | ad256bb | 2013-07-18 14:58:55 -0700 | [diff] [blame] | 4700 | return false; |
| 4701 | } |
| 4702 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 4703 | uint16_t class_def_index = klass->GetDexClassDefIndex(); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 4704 | oat_file_class_status = oat_dex_file->GetOatClass(class_def_index).GetStatus(); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4705 | if (oat_file_class_status >= ClassStatus::kVerified) { |
Mathieu Chartier | a079e3a | 2016-03-16 19:08:31 -0700 | [diff] [blame] | 4706 | return true; |
| 4707 | } |
Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 4708 | if (oat_file_class_status >= ClassStatus::kVerifiedNeedsAccessChecks) { |
| 4709 | // We return that the clas has already been verified, and the caller should |
| 4710 | // check the class status to ensure we run with access checks. |
| 4711 | return true; |
| 4712 | } |
Nicolas Geoffray | 6df4511 | 2021-02-07 21:51:58 +0000 | [diff] [blame] | 4713 | |
| 4714 | // Check the class status with the vdex file. |
| 4715 | const OatFile* oat_file = oat_dex_file->GetOatFile(); |
| 4716 | if (oat_file != nullptr) { |
Nicolas Geoffray | 44dc8a3 | 2021-06-21 15:23:49 +0100 | [diff] [blame] | 4717 | ClassStatus vdex_status = oat_file->GetVdexFile()->ComputeClassStatus(self, klass); |
| 4718 | if (vdex_status >= ClassStatus::kVerifiedNeedsAccessChecks) { |
Nicolas Geoffray | 327cfcf | 2021-10-12 14:13:25 +0100 | [diff] [blame] | 4719 | VLOG(verifier) << "Vdex verification success for " << klass->PrettyClass(); |
Nicolas Geoffray | 44dc8a3 | 2021-06-21 15:23:49 +0100 | [diff] [blame] | 4720 | oat_file_class_status = vdex_status; |
Nicolas Geoffray | 6df4511 | 2021-02-07 21:51:58 +0000 | [diff] [blame] | 4721 | return true; |
| 4722 | } |
| 4723 | } |
| 4724 | |
Mathieu Chartier | a079e3a | 2016-03-16 19:08:31 -0700 | [diff] [blame] | 4725 | // If we only verified a subset of the classes at compile time, we can end up with classes that |
| 4726 | // were resolved by the verifier. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4727 | if (oat_file_class_status == ClassStatus::kResolved) { |
Mathieu Chartier | a079e3a | 2016-03-16 19:08:31 -0700 | [diff] [blame] | 4728 | return false; |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4729 | } |
Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 4730 | // We never expect a .oat file to have kRetryVerificationAtRuntime statuses. |
| 4731 | CHECK_NE(oat_file_class_status, ClassStatus::kRetryVerificationAtRuntime) |
| 4732 | << klass->PrettyClass() << " " << dex_file.GetLocation(); |
| 4733 | |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 4734 | if (mirror::Class::IsErroneous(oat_file_class_status)) { |
Nicolas Geoffray | 44dc8a3 | 2021-06-21 15:23:49 +0100 | [diff] [blame] | 4735 | // Compile time verification failed with a hard error. We'll re-run |
| 4736 | // verification, which might be successful at runtime. |
jeffhao | 1ac2944 | 2012-03-26 11:37:32 -0700 | [diff] [blame] | 4737 | return false; |
| 4738 | } |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4739 | if (oat_file_class_status == ClassStatus::kNotReady) { |
Ian Rogers | c476227 | 2012-02-01 15:55:55 -0800 | [diff] [blame] | 4740 | // Status is uninitialized if we couldn't determine the status at compile time, for example, |
| 4741 | // not loading the class. |
| 4742 | // TODO: when the verifier doesn't rely on Class-es failing to resolve/load the type hierarchy |
| 4743 | // isn't a problem and this case shouldn't occur |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4744 | return false; |
| 4745 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 4746 | std::string temp; |
Elliott Hughes | 634eb2e | 2012-03-22 16:06:28 -0700 | [diff] [blame] | 4747 | LOG(FATAL) << "Unexpected class status: " << oat_file_class_status |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4748 | << " " << dex_file.GetLocation() << " " << klass->PrettyClass() << " " |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 4749 | << klass->GetDescriptor(&temp); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 4750 | UNREACHABLE(); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4751 | } |
| 4752 | |
Alex Light | 5a55986 | 2016-01-29 12:24:48 -0800 | [diff] [blame] | 4753 | void ClassLinker::ResolveClassExceptionHandlerTypes(Handle<mirror::Class> klass) { |
Alex Light | 51a64d5 | 2015-12-17 13:55:59 -0800 | [diff] [blame] | 4754 | for (ArtMethod& method : klass->GetMethods(image_pointer_size_)) { |
Alex Light | 5a55986 | 2016-01-29 12:24:48 -0800 | [diff] [blame] | 4755 | ResolveMethodExceptionHandlerTypes(&method); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4756 | } |
| 4757 | } |
| 4758 | |
Alex Light | 5a55986 | 2016-01-29 12:24:48 -0800 | [diff] [blame] | 4759 | void ClassLinker::ResolveMethodExceptionHandlerTypes(ArtMethod* method) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4760 | // similar to DexVerifier::ScanTryCatchBlocks and dex2oat's ResolveExceptionsForMethod. |
David Sehr | 0225f8e | 2018-01-31 08:52:24 +0000 | [diff] [blame] | 4761 | CodeItemDataAccessor accessor(method->DexInstructionData()); |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 4762 | if (!accessor.HasCodeItem()) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4763 | return; // native or abstract method |
| 4764 | } |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 4765 | if (accessor.TriesSize() == 0) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4766 | return; // nothing to process |
| 4767 | } |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 4768 | const uint8_t* handlers_ptr = accessor.GetCatchHandlerData(0); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4769 | uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4770 | for (uint32_t idx = 0; idx < handlers_size; idx++) { |
| 4771 | CatchHandlerIterator iterator(handlers_ptr); |
| 4772 | for (; iterator.HasNext(); iterator.Next()) { |
| 4773 | // Ensure exception types are resolved so that they don't need resolution to be delivered, |
| 4774 | // unresolved exception types will be ignored by exception delivery |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 4775 | if (iterator.GetHandlerTypeIndex().IsValid()) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4776 | ObjPtr<mirror::Class> exception_type = ResolveType(iterator.GetHandlerTypeIndex(), method); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4777 | if (exception_type == nullptr) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4778 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 4779 | Thread::Current()->ClearException(); |
| 4780 | } |
| 4781 | } |
| 4782 | } |
| 4783 | handlers_ptr = iterator.EndDataPointer(); |
| 4784 | } |
| 4785 | } |
| 4786 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 4787 | ObjPtr<mirror::Class> ClassLinker::CreateProxyClass(ScopedObjectAccessAlreadyRunnable& soa, |
| 4788 | jstring name, |
| 4789 | jobjectArray interfaces, |
| 4790 | jobject loader, |
| 4791 | jobjectArray methods, |
| 4792 | jobjectArray throws) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4793 | Thread* self = soa.Self(); |
Alex Light | e9f6103 | 2018-09-24 16:04:51 -0700 | [diff] [blame] | 4794 | |
| 4795 | // This is to prevent the calls to ClassLoad and ClassPrepare which can cause java/user-supplied |
| 4796 | // code to be executed. We put it up here so we can avoid all the allocations associated with |
| 4797 | // creating the class. This can happen with (eg) jit-threads. |
| 4798 | if (!self->CanLoadClasses()) { |
| 4799 | // Make sure we don't try to load anything, potentially causing an infinite loop. |
| 4800 | ObjPtr<mirror::Throwable> pre_allocated = |
| 4801 | Runtime::Current()->GetPreAllocatedNoClassDefFoundError(); |
| 4802 | self->SetException(pre_allocated); |
| 4803 | return nullptr; |
| 4804 | } |
| 4805 | |
Alex Light | 133987d | 2020-03-26 19:22:12 +0000 | [diff] [blame] | 4806 | StackHandleScope<12> hs(self); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4807 | MutableHandle<mirror::Class> temp_klass(hs.NewHandle( |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 4808 | AllocClass(self, GetClassRoot<mirror::Class>(this), sizeof(mirror::Class)))); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4809 | if (temp_klass == nullptr) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4810 | CHECK(self->IsExceptionPending()); // OOME. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4811 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4812 | } |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4813 | DCHECK(temp_klass->GetClass() != nullptr); |
| 4814 | temp_klass->SetObjectSize(sizeof(mirror::Proxy)); |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 4815 | // Set the class access flags incl. VerificationAttempted, so we do not try to set the flag on |
| 4816 | // the methods. |
Nicolas Geoffray | 66934ef | 2021-07-07 14:56:23 +0100 | [diff] [blame] | 4817 | temp_klass->SetAccessFlagsDuringLinking(kAccClassIsProxy | kAccPublic | kAccFinal); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4818 | temp_klass->SetClassLoader(soa.Decode<mirror::ClassLoader>(loader)); |
| 4819 | DCHECK_EQ(temp_klass->GetPrimitiveType(), Primitive::kPrimNot); |
| 4820 | temp_klass->SetName(soa.Decode<mirror::String>(name)); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 4821 | temp_klass->SetDexCache(GetClassRoot<mirror::Proxy>(this)->GetDexCache()); |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 4822 | // Object has an empty iftable, copy it for that reason. |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 4823 | temp_klass->SetIfTable(GetClassRoot<mirror::Object>(this)->GetIfTable()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4824 | mirror::Class::SetStatus(temp_klass, ClassStatus::kIdx, self); |
Vladimir Marko | 3892e62 | 2019-03-15 15:22:18 +0000 | [diff] [blame] | 4825 | std::string storage; |
| 4826 | const char* descriptor = temp_klass->GetDescriptor(&storage); |
| 4827 | const size_t hash = ComputeModifiedUtf8Hash(descriptor); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4828 | |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 4829 | // 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] | 4830 | LinearAlloc* const allocator = GetOrCreateAllocatorForClassLoader(temp_klass->GetClassLoader()); |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 4831 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4832 | // Insert the class before loading the fields as the field roots |
| 4833 | // (ArtField::declaring_class_) are only visited from the class |
| 4834 | // table. There can't be any suspend points between inserting the |
| 4835 | // class and setting the field arrays below. |
Vladimir Marko | 3892e62 | 2019-03-15 15:22:18 +0000 | [diff] [blame] | 4836 | ObjPtr<mirror::Class> existing = InsertClass(descriptor, temp_klass.Get(), hash); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4837 | CHECK(existing == nullptr); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4838 | |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 4839 | // Instance fields are inherited, but we add a couple of static fields... |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4840 | const size_t num_fields = 2; |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 4841 | LengthPrefixedArray<ArtField>* sfields = AllocArtFieldArray(self, allocator, num_fields); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4842 | temp_klass->SetSFieldsPtr(sfields); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4843 | |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 4844 | // 1. Create a static field 'interfaces' that holds the _declared_ interfaces implemented by |
| 4845 | // our proxy, so Class.getInterfaces doesn't return the flattened set. |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4846 | ArtField& interfaces_sfield = sfields->At(0); |
| 4847 | interfaces_sfield.SetDexFieldIndex(0); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4848 | interfaces_sfield.SetDeclaringClass(temp_klass.Get()); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4849 | interfaces_sfield.SetAccessFlags(kAccStatic | kAccPublic | kAccFinal); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4850 | |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 4851 | // 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] | 4852 | ArtField& throws_sfield = sfields->At(1); |
| 4853 | throws_sfield.SetDexFieldIndex(1); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4854 | throws_sfield.SetDeclaringClass(temp_klass.Get()); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4855 | throws_sfield.SetAccessFlags(kAccStatic | kAccPublic | kAccFinal); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4856 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4857 | // Proxies have 1 direct method, the constructor |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4858 | const size_t num_direct_methods = 1; |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4859 | |
Alex Light | 133987d | 2020-03-26 19:22:12 +0000 | [diff] [blame] | 4860 | // The array we get passed contains all methods, including private and static |
| 4861 | // ones that aren't proxied. We need to filter those out since only interface |
| 4862 | // methods (non-private & virtual) are actually proxied. |
| 4863 | Handle<mirror::ObjectArray<mirror::Method>> h_methods = |
| 4864 | hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::Method>>(methods)); |
Vladimir Marko | 679730e | 2018-05-25 15:06:48 +0100 | [diff] [blame] | 4865 | DCHECK_EQ(h_methods->GetClass(), GetClassRoot<mirror::ObjectArray<mirror::Method>>()) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4866 | << mirror::Class::PrettyClass(h_methods->GetClass()); |
Alex Light | 133987d | 2020-03-26 19:22:12 +0000 | [diff] [blame] | 4867 | // List of the actual virtual methods this class will have. |
| 4868 | std::vector<ArtMethod*> proxied_methods; |
| 4869 | std::vector<size_t> proxied_throws_idx; |
| 4870 | proxied_methods.reserve(h_methods->GetLength()); |
| 4871 | proxied_throws_idx.reserve(h_methods->GetLength()); |
| 4872 | // Filter out to only the non-private virtual methods. |
| 4873 | for (auto [mirror, idx] : ZipCount(h_methods.Iterate<mirror::Method>())) { |
| 4874 | ArtMethod* m = mirror->GetArtMethod(); |
| 4875 | if (!m->IsPrivate() && !m->IsStatic()) { |
| 4876 | proxied_methods.push_back(m); |
| 4877 | proxied_throws_idx.push_back(idx); |
| 4878 | } |
| 4879 | } |
| 4880 | const size_t num_virtual_methods = proxied_methods.size(); |
Alex Light | bc11509 | 2020-03-27 11:25:16 -0700 | [diff] [blame] | 4881 | // We also need to filter out the 'throws'. The 'throws' are a Class[][] that |
| 4882 | // contains an array of all the classes each function is declared to throw. |
| 4883 | // This is used to wrap unexpected exceptions in a |
| 4884 | // UndeclaredThrowableException exception. This array is in the same order as |
| 4885 | // the methods array and like the methods array must be filtered to remove any |
| 4886 | // non-proxied methods. |
Alex Light | 133987d | 2020-03-26 19:22:12 +0000 | [diff] [blame] | 4887 | const bool has_filtered_methods = |
| 4888 | static_cast<int32_t>(num_virtual_methods) != h_methods->GetLength(); |
| 4889 | MutableHandle<mirror::ObjectArray<mirror::ObjectArray<mirror::Class>>> original_proxied_throws( |
| 4890 | hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::ObjectArray<mirror::Class>>>(throws))); |
| 4891 | MutableHandle<mirror::ObjectArray<mirror::ObjectArray<mirror::Class>>> proxied_throws( |
| 4892 | hs.NewHandle<mirror::ObjectArray<mirror::ObjectArray<mirror::Class>>>( |
| 4893 | (has_filtered_methods) |
| 4894 | ? mirror::ObjectArray<mirror::ObjectArray<mirror::Class>>::Alloc( |
| 4895 | self, original_proxied_throws->GetClass(), num_virtual_methods) |
| 4896 | : original_proxied_throws.Get())); |
Alex Light | bc11509 | 2020-03-27 11:25:16 -0700 | [diff] [blame] | 4897 | if (proxied_throws.IsNull() && !original_proxied_throws.IsNull()) { |
| 4898 | self->AssertPendingOOMException(); |
| 4899 | return nullptr; |
| 4900 | } |
Alex Light | 133987d | 2020-03-26 19:22:12 +0000 | [diff] [blame] | 4901 | if (has_filtered_methods) { |
| 4902 | for (auto [orig_idx, new_idx] : ZipCount(MakeIterationRange(proxied_throws_idx))) { |
| 4903 | DCHECK_LE(new_idx, orig_idx); |
| 4904 | proxied_throws->Set(new_idx, original_proxied_throws->Get(orig_idx)); |
| 4905 | } |
| 4906 | } |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4907 | |
| 4908 | // Create the methods array. |
| 4909 | LengthPrefixedArray<ArtMethod>* proxy_class_methods = AllocArtMethodArray( |
| 4910 | self, allocator, num_direct_methods + num_virtual_methods); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4911 | // Currently AllocArtMethodArray cannot return null, but the OOM logic is left there in case we |
| 4912 | // want to throw OOM in the future. |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4913 | if (UNLIKELY(proxy_class_methods == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4914 | self->AssertPendingOOMException(); |
| 4915 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4916 | } |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4917 | temp_klass->SetMethodsPtr(proxy_class_methods, num_direct_methods, num_virtual_methods); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4918 | |
| 4919 | // Create the single direct method. |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4920 | CreateProxyConstructor(temp_klass, temp_klass->GetDirectMethodUnchecked(0, image_pointer_size_)); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4921 | |
| 4922 | // Create virtual method using specified prototypes. |
| 4923 | // TODO These should really use the iterators. |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4924 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4925 | auto* virtual_method = temp_klass->GetVirtualMethodUnchecked(i, image_pointer_size_); |
Alex Light | 133987d | 2020-03-26 19:22:12 +0000 | [diff] [blame] | 4926 | auto* prototype = proxied_methods[i]; |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4927 | CreateProxyMethod(temp_klass, prototype, virtual_method); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4928 | DCHECK(virtual_method->GetDeclaringClass() != nullptr); |
| 4929 | DCHECK(prototype->GetDeclaringClass() != nullptr); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4930 | } |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4931 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4932 | // The super class is java.lang.reflect.Proxy |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 4933 | temp_klass->SetSuperClass(GetClassRoot<mirror::Proxy>(this)); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4934 | // Now effectively in the loaded state. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4935 | mirror::Class::SetStatus(temp_klass, ClassStatus::kLoaded, self); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4936 | self->AssertNoPendingException(); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4937 | |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4938 | // At this point the class is loaded. Publish a ClassLoad event. |
| 4939 | // Note: this may be a temporary class. It is a listener's responsibility to handle this. |
| 4940 | Runtime::Current()->GetRuntimeCallbacks()->ClassLoad(temp_klass); |
| 4941 | |
| 4942 | MutableHandle<mirror::Class> klass = hs.NewHandle<mirror::Class>(nullptr); |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 4943 | { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4944 | // Must hold lock on object when resolved. |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4945 | ObjectLock<mirror::Class> resolution_lock(self, temp_klass); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4946 | // Link the fields and virtual methods, creating vtable and iftables. |
| 4947 | // The new class will replace the old one in the class table. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4948 | Handle<mirror::ObjectArray<mirror::Class>> h_interfaces( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 4949 | hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::Class>>(interfaces))); |
Vladimir Marko | 3892e62 | 2019-03-15 15:22:18 +0000 | [diff] [blame] | 4950 | if (!LinkClass(self, descriptor, temp_klass, h_interfaces, &klass)) { |
Vladimir Marko | a4d28dd | 2021-06-30 11:28:06 +0100 | [diff] [blame] | 4951 | if (!temp_klass->IsErroneous()) { |
| 4952 | mirror::Class::SetStatus(temp_klass, ClassStatus::kErrorUnresolved, self); |
| 4953 | } |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 4954 | return nullptr; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4955 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4956 | } |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4957 | CHECK(temp_klass->IsRetired()); |
| 4958 | CHECK_NE(temp_klass.Get(), klass.Get()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4959 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4960 | CHECK_EQ(interfaces_sfield.GetDeclaringClass(), klass.Get()); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 4961 | interfaces_sfield.SetObject<false>( |
| 4962 | klass.Get(), |
Mathieu Chartier | f8ac97f | 2016-10-05 15:56:52 -0700 | [diff] [blame] | 4963 | soa.Decode<mirror::ObjectArray<mirror::Class>>(interfaces)); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4964 | CHECK_EQ(throws_sfield.GetDeclaringClass(), klass.Get()); |
| 4965 | throws_sfield.SetObject<false>( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 4966 | klass.Get(), |
Alex Light | 133987d | 2020-03-26 19:22:12 +0000 | [diff] [blame] | 4967 | proxied_throws.Get()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4968 | |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4969 | Runtime::Current()->GetRuntimeCallbacks()->ClassPrepare(temp_klass, klass); |
| 4970 | |
Vladimir Marko | 305c38b | 2018-02-14 11:50:07 +0000 | [diff] [blame] | 4971 | // SubtypeCheckInfo::Initialized must happen-before any new-instance for that type. |
| 4972 | // See also ClassLinker::EnsureInitialized(). |
| 4973 | if (kBitstringSubtypeCheckEnabled) { |
| 4974 | MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_); |
| 4975 | SubtypeCheck<ObjPtr<mirror::Class>>::EnsureInitialized(klass.Get()); |
| 4976 | // TODO: Avoid taking subtype_check_lock_ if SubtypeCheck for j.l.r.Proxy is already assigned. |
| 4977 | } |
| 4978 | |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 4979 | VisiblyInitializedCallback* callback = nullptr; |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4980 | { |
| 4981 | // Lock on klass is released. Lock new class object. |
| 4982 | ObjectLock<mirror::Class> initialization_lock(self, klass); |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 4983 | // Conservatively go through the ClassStatus::kInitialized state. |
| 4984 | callback = MarkClassInitialized(self, klass); |
| 4985 | } |
| 4986 | if (callback != nullptr) { |
| 4987 | callback->MakeVisible(self); |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 4988 | } |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4989 | |
David Srbecky | 346fd96 | 2020-07-27 16:51:00 +0100 | [diff] [blame] | 4990 | // Consistency checks. |
Elliott Hughes | 67d9200 | 2012-03-26 15:08:51 -0700 | [diff] [blame] | 4991 | if (kIsDebugBuild) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4992 | CHECK(klass->GetIFieldsPtr() == nullptr); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4993 | CheckProxyConstructor(klass->GetDirectMethod(0, image_pointer_size_)); |
| 4994 | |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4995 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4996 | auto* virtual_method = klass->GetVirtualMethodUnchecked(i, image_pointer_size_); |
Alex Light | 133987d | 2020-03-26 19:22:12 +0000 | [diff] [blame] | 4997 | CheckProxyMethod(virtual_method, proxied_methods[i]); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4998 | } |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 4999 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 5000 | StackHandleScope<1> hs2(self); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 5001 | Handle<mirror::String> decoded_name = hs2.NewHandle(soa.Decode<mirror::String>(name)); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 5002 | std::string interfaces_field_name(StringPrintf("java.lang.Class[] %s.interfaces", |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 5003 | decoded_name->ToModifiedUtf8().c_str())); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5004 | CHECK_EQ(ArtField::PrettyField(klass->GetStaticField(0)), interfaces_field_name); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 5005 | |
| 5006 | std::string throws_field_name(StringPrintf("java.lang.Class[][] %s.throws", |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 5007 | decoded_name->ToModifiedUtf8().c_str())); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5008 | CHECK_EQ(ArtField::PrettyField(klass->GetStaticField(1)), throws_field_name); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 5009 | |
Narayan Kamath | 6b2dc31 | 2017-03-14 13:26:12 +0000 | [diff] [blame] | 5010 | CHECK_EQ(klass.Get()->GetProxyInterfaces(), |
Mathieu Chartier | f8ac97f | 2016-10-05 15:56:52 -0700 | [diff] [blame] | 5011 | soa.Decode<mirror::ObjectArray<mirror::Class>>(interfaces)); |
Narayan Kamath | 6b2dc31 | 2017-03-14 13:26:12 +0000 | [diff] [blame] | 5012 | CHECK_EQ(klass.Get()->GetProxyThrows(), |
Alex Light | 133987d | 2020-03-26 19:22:12 +0000 | [diff] [blame] | 5013 | proxied_throws.Get()); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 5014 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5015 | return klass.Get(); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 5016 | } |
| 5017 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5018 | void ClassLinker::CreateProxyConstructor(Handle<mirror::Class> klass, ArtMethod* out) { |
| 5019 | // Create constructor for Proxy that must initialize the method. |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 5020 | ObjPtr<mirror::Class> proxy_class = GetClassRoot<mirror::Proxy>(this); |
| 5021 | CHECK_EQ(proxy_class->NumDirectMethods(), 21u); |
Przemyslaw Szczepaniak | f11cd29 | 2016-08-17 17:46:38 +0100 | [diff] [blame] | 5022 | |
Igor Murashkin | 9d3d752 | 2017-02-27 10:39:49 -0800 | [diff] [blame] | 5023 | // Find the <init>(InvocationHandler)V method. The exact method offset varies depending |
| 5024 | // on which front-end compiler was used to build the libcore DEX files. |
Alex Light | 6cae5ea | 2018-06-07 17:07:02 -0700 | [diff] [blame] | 5025 | ArtMethod* proxy_constructor = |
| 5026 | jni::DecodeArtMethod(WellKnownClasses::java_lang_reflect_Proxy_init); |
Igor Murashkin | 9d3d752 | 2017-02-27 10:39:49 -0800 | [diff] [blame] | 5027 | DCHECK(proxy_constructor != nullptr) |
| 5028 | << "Could not find <init> method in java.lang.reflect.Proxy"; |
| 5029 | |
Jeff Hao | db8a664 | 2014-08-14 17:18:52 -0700 | [diff] [blame] | 5030 | // Clone the existing constructor of Proxy (our constructor would just invoke it so steal its |
| 5031 | // code_ too) |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5032 | DCHECK(out != nullptr); |
| 5033 | out->CopyFrom(proxy_constructor, image_pointer_size_); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 5034 | // 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] | 5035 | // 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] | 5036 | // 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] | 5037 | out->SetAccessFlags((out->GetAccessFlags() & ~kAccProtected) | |
| 5038 | kAccPublic | |
| 5039 | kAccCompileDontBother); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5040 | out->SetDeclaringClass(klass.Get()); |
Vladimir Marko | d1ee20f | 2017-08-17 09:21:16 +0000 | [diff] [blame] | 5041 | |
| 5042 | // Set the original constructor method. |
| 5043 | out->SetDataPtrSize(proxy_constructor, image_pointer_size_); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 5044 | } |
| 5045 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5046 | void ClassLinker::CheckProxyConstructor(ArtMethod* constructor) const { |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 5047 | CHECK(constructor->IsConstructor()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5048 | auto* np = constructor->GetInterfaceMethodIfProxy(image_pointer_size_); |
| 5049 | CHECK_STREQ(np->GetName(), "<init>"); |
| 5050 | CHECK_STREQ(np->GetSignature().ToString().c_str(), "(Ljava/lang/reflect/InvocationHandler;)V"); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 5051 | DCHECK(constructor->IsPublic()); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 5052 | } |
| 5053 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5054 | void ClassLinker::CreateProxyMethod(Handle<mirror::Class> klass, ArtMethod* prototype, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5055 | ArtMethod* out) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 5056 | // 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] | 5057 | // as necessary |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5058 | DCHECK(out != nullptr); |
| 5059 | out->CopyFrom(prototype, image_pointer_size_); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 5060 | |
Alex Light | e9dd04f | 2016-03-16 16:09:45 -0700 | [diff] [blame] | 5061 | // Set class to be the concrete proxy class. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5062 | out->SetDeclaringClass(klass.Get()); |
Vladimir Marko | de0d0de | 2021-03-18 14:12:35 +0000 | [diff] [blame] | 5063 | // 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] | 5064 | // preference to the invocation handler. |
Vladimir Marko | de0d0de | 2021-03-18 14:12:35 +0000 | [diff] [blame] | 5065 | const uint32_t kRemoveFlags = kAccAbstract | kAccDefault; |
Alex Light | e9dd04f | 2016-03-16 16:09:45 -0700 | [diff] [blame] | 5066 | // Make the method final. |
Mathieu Chartier | 201e297 | 2017-06-05 18:34:53 -0700 | [diff] [blame] | 5067 | // Mark kAccCompileDontBother so that we don't take JIT samples for the method. b/62349349 |
| 5068 | const uint32_t kAddFlags = kAccFinal | kAccCompileDontBother; |
Alex Light | e9dd04f | 2016-03-16 16:09:45 -0700 | [diff] [blame] | 5069 | out->SetAccessFlags((out->GetAccessFlags() & ~kRemoveFlags) | kAddFlags); |
| 5070 | |
Vladimir Marko | d1ee20f | 2017-08-17 09:21:16 +0000 | [diff] [blame] | 5071 | // Set the original interface method. |
| 5072 | out->SetDataPtrSize(prototype, image_pointer_size_); |
| 5073 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 5074 | // At runtime the method looks like a reference and argument saving method, clone the code |
| 5075 | // related parameters from this method. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5076 | out->SetEntryPointFromQuickCompiledCode(GetQuickProxyInvokeHandler()); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 5077 | } |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 5078 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5079 | void ClassLinker::CheckProxyMethod(ArtMethod* method, ArtMethod* prototype) const { |
David Srbecky | 346fd96 | 2020-07-27 16:51:00 +0100 | [diff] [blame] | 5080 | // Basic consistency checks. |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 5081 | CHECK(!prototype->IsFinal()); |
| 5082 | CHECK(method->IsFinal()); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5083 | CHECK(method->IsInvokable()); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 5084 | |
| 5085 | // The proxy method doesn't have its own dex cache or dex file and so it steals those of its |
| 5086 | // 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] | 5087 | CHECK_EQ(prototype->GetDexMethodIndex(), method->GetDexMethodIndex()); |
Vladimir Marko | 5c3e9d1 | 2017-08-30 16:43:54 +0100 | [diff] [blame] | 5088 | CHECK_EQ(prototype, method->GetInterfaceMethodIfProxy(image_pointer_size_)); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 5089 | } |
| 5090 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5091 | bool ClassLinker::CanWeInitializeClass(ObjPtr<mirror::Class> klass, bool can_init_statics, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5092 | bool can_init_parents) { |
Brian Carlstrom | 610e49f | 2013-11-04 17:07:22 -0800 | [diff] [blame] | 5093 | if (can_init_statics && can_init_parents) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5094 | return true; |
| 5095 | } |
| 5096 | if (!can_init_statics) { |
| 5097 | // Check if there's a class initializer. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5098 | ArtMethod* clinit = klass->FindClassInitializer(image_pointer_size_); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5099 | if (clinit != nullptr) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5100 | return false; |
| 5101 | } |
| 5102 | // Check if there are encoded static values needing initialization. |
| 5103 | if (klass->NumStaticFields() != 0) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 5104 | const dex::ClassDef* dex_class_def = klass->GetClassDef(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5105 | DCHECK(dex_class_def != nullptr); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5106 | if (dex_class_def->static_values_off_ != 0) { |
| 5107 | return false; |
| 5108 | } |
| 5109 | } |
Vladimir Marko | 889b72d | 2019-11-12 11:01:13 +0000 | [diff] [blame] | 5110 | } |
| 5111 | // If we are a class we need to initialize all interfaces with default methods when we are |
| 5112 | // initialized. Check all of them. |
| 5113 | if (!klass->IsInterface()) { |
| 5114 | size_t num_interfaces = klass->GetIfTableCount(); |
| 5115 | for (size_t i = 0; i < num_interfaces; i++) { |
| 5116 | ObjPtr<mirror::Class> iface = klass->GetIfTable()->GetInterface(i); |
| 5117 | if (iface->HasDefaultMethods() && !iface->IsInitialized()) { |
| 5118 | if (!can_init_parents || !CanWeInitializeClass(iface, can_init_statics, can_init_parents)) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5119 | return false; |
| 5120 | } |
| 5121 | } |
| 5122 | } |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5123 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5124 | if (klass->IsInterface() || !klass->HasSuperClass()) { |
| 5125 | return true; |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5126 | } |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5127 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Vladimir Marko | 889b72d | 2019-11-12 11:01:13 +0000 | [diff] [blame] | 5128 | if (super_class->IsInitialized()) { |
| 5129 | return true; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5130 | } |
Vladimir Marko | 889b72d | 2019-11-12 11:01:13 +0000 | [diff] [blame] | 5131 | 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] | 5132 | } |
| 5133 | |
Mathieu Chartier | 2336954 | 2020-03-04 08:24:11 -0800 | [diff] [blame] | 5134 | bool ClassLinker::InitializeClass(Thread* self, |
| 5135 | Handle<mirror::Class> klass, |
| 5136 | bool can_init_statics, |
| 5137 | bool can_init_parents) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5138 | // see JLS 3rd edition, 12.4.2 "Detailed Initialization Procedure" for the locking protocol |
| 5139 | |
| 5140 | // Are we already initialized and therefore done? |
| 5141 | // Note: we differ from the JLS here as we don't do this under the lock, this is benign as |
| 5142 | // an initialized class will never change its state. |
| 5143 | if (klass->IsInitialized()) { |
| 5144 | return true; |
| 5145 | } |
| 5146 | |
| 5147 | // 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] | 5148 | if (!CanWeInitializeClass(klass.Get(), can_init_statics, can_init_parents)) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5149 | return false; |
| 5150 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5151 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5152 | self->AllowThreadSuspension(); |
Mathieu Chartier | 2336954 | 2020-03-04 08:24:11 -0800 | [diff] [blame] | 5153 | Runtime* const runtime = Runtime::Current(); |
| 5154 | const bool stats_enabled = runtime->HasStatsEnabled(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5155 | uint64_t t0; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5156 | { |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 5157 | ObjectLock<mirror::Class> lock(self, klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5158 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5159 | // Re-check under the lock in case another thread initialized ahead of us. |
| 5160 | if (klass->IsInitialized()) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5161 | return true; |
| 5162 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5163 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5164 | // 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] | 5165 | if (klass->IsErroneous()) { |
Andreas Gampe | 7b3063b | 2019-01-07 14:12:52 -0800 | [diff] [blame] | 5166 | ThrowEarlierClassFailure(klass.Get(), true, /* log= */ true); |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 5167 | VlogClassInitializationFailure(klass); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5168 | return false; |
| 5169 | } |
| 5170 | |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 5171 | CHECK(klass->IsResolved() && !klass->IsErroneousResolved()) |
| 5172 | << klass->PrettyClass() << ": state=" << klass->GetStatus(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5173 | |
| 5174 | if (!klass->IsVerified()) { |
Nicolas Geoffray | 5b0b2e1 | 2021-03-19 14:48:40 +0000 | [diff] [blame] | 5175 | VerifyClass(self, /*verifier_deps= */ nullptr, klass); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5176 | if (!klass->IsVerified()) { |
| 5177 | // We failed to verify, expect either the klass to be erroneous or verification failed at |
| 5178 | // compile time. |
| 5179 | if (klass->IsErroneous()) { |
Andreas Gampe | fc49fa0 | 2016-04-21 12:21:55 -0700 | [diff] [blame] | 5180 | // The class is erroneous. This may be a verifier error, or another thread attempted |
| 5181 | // verification and/or initialization and failed. We can distinguish those cases by |
| 5182 | // whether an exception is already pending. |
| 5183 | if (self->IsExceptionPending()) { |
| 5184 | // Check that it's a VerifyError. |
Nicolas Geoffray | 4dc6589 | 2021-07-05 17:43:35 +0100 | [diff] [blame] | 5185 | DCHECK(IsVerifyError(self->GetException())); |
Andreas Gampe | fc49fa0 | 2016-04-21 12:21:55 -0700 | [diff] [blame] | 5186 | } else { |
| 5187 | // Check that another thread attempted initialization. |
| 5188 | DCHECK_NE(0, klass->GetClinitThreadId()); |
| 5189 | DCHECK_NE(self->GetTid(), klass->GetClinitThreadId()); |
| 5190 | // Need to rethrow the previous failure now. |
| 5191 | ThrowEarlierClassFailure(klass.Get(), true); |
| 5192 | } |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 5193 | VlogClassInitializationFailure(klass); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5194 | } else { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 5195 | CHECK(Runtime::Current()->IsAotCompiler()); |
Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 5196 | CHECK(klass->ShouldVerifyAtRuntime() || klass->IsVerifiedNeedsAccessChecks()); |
Vladimir Marko | d79b37b | 2018-11-02 13:06:22 +0000 | [diff] [blame] | 5197 | self->AssertNoPendingException(); |
| 5198 | self->SetException(Runtime::Current()->GetPreAllocatedNoClassDefFoundError()); |
jeffhao | a9b3bf4 | 2012-06-06 17:18:39 -0700 | [diff] [blame] | 5199 | } |
Vladimir Marko | d79b37b | 2018-11-02 13:06:22 +0000 | [diff] [blame] | 5200 | self->AssertPendingException(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5201 | return false; |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 5202 | } else { |
| 5203 | self->AssertNoPendingException(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5204 | } |
Andreas Gampe | fc49fa0 | 2016-04-21 12:21:55 -0700 | [diff] [blame] | 5205 | |
| 5206 | // A separate thread could have moved us all the way to initialized. A "simple" example |
| 5207 | // involves a subclass of the current class being initialized at the same time (which |
| 5208 | // will implicitly initialize the superclass, if scheduled that way). b/28254258 |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 5209 | DCHECK(!klass->IsErroneous()) << klass->GetStatus(); |
Andreas Gampe | fc49fa0 | 2016-04-21 12:21:55 -0700 | [diff] [blame] | 5210 | if (klass->IsInitialized()) { |
| 5211 | return true; |
| 5212 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5213 | } |
| 5214 | |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5215 | // If the class is ClassStatus::kInitializing, either this thread is |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5216 | // initializing higher up the stack or another thread has beat us |
| 5217 | // to initializing and we need to wait. Either way, this |
| 5218 | // invocation of InitializeClass will not be responsible for |
| 5219 | // running <clinit> and will return. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5220 | if (klass->GetStatus() == ClassStatus::kInitializing) { |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 5221 | // Could have got an exception during verification. |
| 5222 | if (self->IsExceptionPending()) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 5223 | VlogClassInitializationFailure(klass); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 5224 | return false; |
| 5225 | } |
Elliott Hughes | 005ab2e | 2011-09-11 17:15:31 -0700 | [diff] [blame] | 5226 | // We caught somebody else in the act; was it us? |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 5227 | if (klass->GetClinitThreadId() == self->GetTid()) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5228 | // Yes. That's fine. Return so we can continue initializing. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5229 | return true; |
| 5230 | } |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5231 | // No. That's fine. Wait for another thread to finish initializing. |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5232 | return WaitForInitializeClass(klass, self, lock); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5233 | } |
| 5234 | |
Jeff Hao | e2e4034 | 2017-07-19 10:45:18 -0700 | [diff] [blame] | 5235 | // Try to get the oat class's status for this class if the oat file is present. The compiler |
| 5236 | // tries to validate superclass descriptors, and writes the result into the oat file. |
| 5237 | // Runtime correctness is guaranteed by classpath checks done on loading. If the classpath |
| 5238 | // is different at runtime than it was at compile time, the oat file is rejected. So if the |
| 5239 | // 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] | 5240 | bool has_oat_class = false; |
Jeff Hao | e2e4034 | 2017-07-19 10:45:18 -0700 | [diff] [blame] | 5241 | const OatFile::OatClass oat_class = (runtime->IsStarted() && !runtime->IsAotCompiler()) |
| 5242 | ? OatFile::FindOatClass(klass->GetDexFile(), klass->GetDexClassDefIndex(), &has_oat_class) |
| 5243 | : OatFile::OatClass::Invalid(); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5244 | if (oat_class.GetStatus() < ClassStatus::kSuperclassValidated && |
Jeff Hao | 0cb1728 | 2017-07-12 14:51:49 -0700 | [diff] [blame] | 5245 | !ValidateSuperClassDescriptors(klass)) { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5246 | mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5247 | return false; |
| 5248 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5249 | self->AllowThreadSuspension(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5250 | |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5251 | CHECK_EQ(klass->GetStatus(), ClassStatus::kVerified) << klass->PrettyClass() |
Andreas Gampe | 9510ccd | 2016-04-20 09:55:25 -0700 | [diff] [blame] | 5252 | << " self.tid=" << self->GetTid() << " clinit.tid=" << klass->GetClinitThreadId(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5253 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5254 | // From here out other threads may observe that we're initializing and so changes of state |
| 5255 | // require the a notification. |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 5256 | klass->SetClinitThreadId(self->GetTid()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5257 | mirror::Class::SetStatus(klass, ClassStatus::kInitializing, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5258 | |
Mathieu Chartier | 2336954 | 2020-03-04 08:24:11 -0800 | [diff] [blame] | 5259 | t0 = stats_enabled ? NanoTime() : 0u; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5260 | } |
| 5261 | |
Andreas Gampe | af86470 | 2019-07-23 14:05:35 -0700 | [diff] [blame] | 5262 | uint64_t t_sub = 0; |
| 5263 | |
Brian Carlstrom | 6d3f72c | 2013-08-21 18:06:34 -0700 | [diff] [blame] | 5264 | // Initialize super classes, must be done while initializing for the JLS. |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5265 | if (!klass->IsInterface() && klass->HasSuperClass()) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5266 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5267 | if (!super_class->IsInitialized()) { |
| 5268 | CHECK(!super_class->IsInterface()); |
| 5269 | CHECK(can_init_parents); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5270 | StackHandleScope<1> hs(self); |
| 5271 | Handle<mirror::Class> handle_scope_super(hs.NewHandle(super_class)); |
Mathieu Chartier | 2336954 | 2020-03-04 08:24:11 -0800 | [diff] [blame] | 5272 | uint64_t super_t0 = stats_enabled ? NanoTime() : 0u; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5273 | bool super_initialized = InitializeClass(self, handle_scope_super, can_init_statics, true); |
Mathieu Chartier | 2336954 | 2020-03-04 08:24:11 -0800 | [diff] [blame] | 5274 | uint64_t super_t1 = stats_enabled ? NanoTime() : 0u; |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5275 | if (!super_initialized) { |
| 5276 | // The super class was verified ahead of entering initializing, we should only be here if |
| 5277 | // the super class became erroneous due to initialization. |
Chang Xing | adbb91c | 2017-07-17 11:23:55 -0700 | [diff] [blame] | 5278 | // For the case of aot compiler, the super class might also be initializing but we don't |
| 5279 | // want to process circular dependencies in pre-compile. |
| 5280 | CHECK(self->IsExceptionPending()) |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 5281 | << "Super class initialization failed for " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5282 | << handle_scope_super->PrettyDescriptor() |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5283 | << " that has unexpected status " << handle_scope_super->GetStatus() |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5284 | << "\nPending exception:\n" |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 5285 | << (self->GetException() != nullptr ? self->GetException()->Dump() : ""); |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 5286 | ObjectLock<mirror::Class> lock(self, klass); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5287 | // Initialization failed because the super-class is erroneous. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5288 | mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5289 | return false; |
| 5290 | } |
Andreas Gampe | af86470 | 2019-07-23 14:05:35 -0700 | [diff] [blame] | 5291 | t_sub = super_t1 - super_t0; |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 5292 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5293 | } |
| 5294 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5295 | if (!klass->IsInterface()) { |
| 5296 | // Initialize interfaces with default methods for the JLS. |
| 5297 | size_t num_direct_interfaces = klass->NumDirectInterfaces(); |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 5298 | // Only setup the (expensive) handle scope if we actually need to. |
| 5299 | if (UNLIKELY(num_direct_interfaces > 0)) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5300 | StackHandleScope<1> hs_iface(self); |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 5301 | MutableHandle<mirror::Class> handle_scope_iface(hs_iface.NewHandle<mirror::Class>(nullptr)); |
| 5302 | for (size_t i = 0; i < num_direct_interfaces; i++) { |
Vladimir Marko | b10668c | 2021-06-10 09:52:53 +0100 | [diff] [blame] | 5303 | handle_scope_iface.Assign(klass->GetDirectInterface(i)); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 5304 | CHECK(handle_scope_iface != nullptr) << klass->PrettyDescriptor() << " iface #" << i; |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 5305 | CHECK(handle_scope_iface->IsInterface()); |
| 5306 | if (handle_scope_iface->HasBeenRecursivelyInitialized()) { |
| 5307 | // We have already done this for this interface. Skip it. |
| 5308 | continue; |
| 5309 | } |
| 5310 | // We cannot just call initialize class directly because we need to ensure that ALL |
| 5311 | // interfaces with default methods are initialized. Non-default interface initialization |
| 5312 | // will not affect other non-default super-interfaces. |
Mathieu Chartier | 2336954 | 2020-03-04 08:24:11 -0800 | [diff] [blame] | 5313 | // This is not very precise, misses all walking. |
| 5314 | uint64_t inf_t0 = stats_enabled ? NanoTime() : 0u; |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 5315 | bool iface_initialized = InitializeDefaultInterfaceRecursive(self, |
| 5316 | handle_scope_iface, |
| 5317 | can_init_statics, |
| 5318 | can_init_parents); |
Mathieu Chartier | 2336954 | 2020-03-04 08:24:11 -0800 | [diff] [blame] | 5319 | uint64_t inf_t1 = stats_enabled ? NanoTime() : 0u; |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 5320 | if (!iface_initialized) { |
| 5321 | ObjectLock<mirror::Class> lock(self, klass); |
| 5322 | // Initialization failed because one of our interfaces with default methods is erroneous. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5323 | mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self); |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 5324 | return false; |
| 5325 | } |
Andreas Gampe | af86470 | 2019-07-23 14:05:35 -0700 | [diff] [blame] | 5326 | t_sub += inf_t1 - inf_t0; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5327 | } |
| 5328 | } |
| 5329 | } |
| 5330 | |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 5331 | const size_t num_static_fields = klass->NumStaticFields(); |
| 5332 | if (num_static_fields > 0) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 5333 | const dex::ClassDef* dex_class_def = klass->GetClassDef(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5334 | CHECK(dex_class_def != nullptr); |
Hiroshi Yamauchi | 67ef46a | 2014-08-21 15:59:43 -0700 | [diff] [blame] | 5335 | StackHandleScope<3> hs(self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5336 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader())); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 5337 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache())); |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 5338 | |
| 5339 | // Eagerly fill in static fields so that the we don't have to do as many expensive |
| 5340 | // Class::FindStaticField in ResolveField. |
| 5341 | for (size_t i = 0; i < num_static_fields; ++i) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 5342 | ArtField* field = klass->GetStaticField(i); |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 5343 | const uint32_t field_idx = field->GetDexFieldIndex(); |
David Srbecky | 5de5efe | 2021-02-15 21:23:00 +0000 | [diff] [blame] | 5344 | ArtField* resolved_field = dex_cache->GetResolvedField(field_idx); |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 5345 | if (resolved_field == nullptr) { |
David Brazdil | 1ab0fa8 | 2018-05-04 11:28:03 +0100 | [diff] [blame] | 5346 | // 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] | 5347 | DCHECK(!hiddenapi::ShouldDenyAccessToMember( |
| 5348 | field, |
| 5349 | hiddenapi::AccessContext(class_loader.Get(), dex_cache.Get()), |
| 5350 | hiddenapi::AccessMethod::kNone)); |
David Srbecky | 5de5efe | 2021-02-15 21:23:00 +0000 | [diff] [blame] | 5351 | dex_cache->SetResolvedField(field_idx, field); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5352 | } else { |
| 5353 | DCHECK_EQ(field, resolved_field); |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 5354 | } |
| 5355 | } |
| 5356 | |
Vladimir Marko | e11dd50 | 2017-12-08 14:09:45 +0000 | [diff] [blame] | 5357 | annotations::RuntimeEncodedStaticFieldValueIterator value_it(dex_cache, |
| 5358 | class_loader, |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 5359 | this, |
| 5360 | *dex_class_def); |
Vladimir Marko | e11dd50 | 2017-12-08 14:09:45 +0000 | [diff] [blame] | 5361 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 5362 | |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 5363 | if (value_it.HasNext()) { |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 5364 | ClassAccessor accessor(dex_file, *dex_class_def); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5365 | CHECK(can_init_statics); |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 5366 | for (const ClassAccessor::Field& field : accessor.GetStaticFields()) { |
| 5367 | if (!value_it.HasNext()) { |
| 5368 | break; |
| 5369 | } |
| 5370 | ArtField* art_field = ResolveField(field.GetIndex(), |
| 5371 | dex_cache, |
| 5372 | class_loader, |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 5373 | /* is_static= */ true); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 5374 | if (Runtime::Current()->IsActiveTransaction()) { |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 5375 | value_it.ReadValueToField<true>(art_field); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 5376 | } else { |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 5377 | value_it.ReadValueToField<false>(art_field); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 5378 | } |
Mathieu Chartier | da595be | 2016-08-10 13:57:39 -0700 | [diff] [blame] | 5379 | if (self->IsExceptionPending()) { |
| 5380 | break; |
| 5381 | } |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 5382 | value_it.Next(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5383 | } |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 5384 | DCHECK(self->IsExceptionPending() || !value_it.HasNext()); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5385 | } |
| 5386 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5387 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5388 | |
Mathieu Chartier | da595be | 2016-08-10 13:57:39 -0700 | [diff] [blame] | 5389 | if (!self->IsExceptionPending()) { |
| 5390 | ArtMethod* clinit = klass->FindClassInitializer(image_pointer_size_); |
| 5391 | if (clinit != nullptr) { |
| 5392 | CHECK(can_init_statics); |
| 5393 | JValue result; |
| 5394 | clinit->Invoke(self, nullptr, 0, &result, "V"); |
| 5395 | } |
| 5396 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5397 | self->AllowThreadSuspension(); |
Mathieu Chartier | 2336954 | 2020-03-04 08:24:11 -0800 | [diff] [blame] | 5398 | uint64_t t1 = stats_enabled ? NanoTime() : 0u; |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 5399 | |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 5400 | VisiblyInitializedCallback* callback = nullptr; |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 5401 | bool success = true; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5402 | { |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 5403 | ObjectLock<mirror::Class> lock(self, klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5404 | |
| 5405 | if (self->IsExceptionPending()) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 5406 | WrapExceptionInInitializer(klass); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5407 | mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self); |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 5408 | success = false; |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 5409 | } else if (Runtime::Current()->IsTransactionAborted()) { |
| 5410 | // The exception thrown when the transaction aborted has been caught and cleared |
| 5411 | // so we need to throw it again now. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5412 | VLOG(compiler) << "Return from class initializer of " |
| 5413 | << mirror::Class::PrettyDescriptor(klass.Get()) |
Sebastien Hertz | bd9cf9f | 2015-03-03 12:16:13 +0100 | [diff] [blame] | 5414 | << " without exception while transaction was aborted: re-throw it now."; |
Mathieu Chartier | 2336954 | 2020-03-04 08:24:11 -0800 | [diff] [blame] | 5415 | runtime->ThrowTransactionAbortError(self); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5416 | mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self); |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 5417 | success = false; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5418 | } else { |
Mathieu Chartier | 2336954 | 2020-03-04 08:24:11 -0800 | [diff] [blame] | 5419 | if (stats_enabled) { |
| 5420 | RuntimeStats* global_stats = runtime->GetStats(); |
| 5421 | RuntimeStats* thread_stats = self->GetStats(); |
| 5422 | ++global_stats->class_init_count; |
| 5423 | ++thread_stats->class_init_count; |
| 5424 | global_stats->class_init_time_ns += (t1 - t0 - t_sub); |
| 5425 | thread_stats->class_init_time_ns += (t1 - t0 - t_sub); |
| 5426 | } |
Ian Rogers | e6bb3b2 | 2013-08-19 21:51:45 -0700 | [diff] [blame] | 5427 | // Set the class as initialized except if failed to initialize static fields. |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 5428 | callback = MarkClassInitialized(self, klass); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5429 | if (VLOG_IS_ON(class_linker)) { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 5430 | std::string temp; |
| 5431 | LOG(INFO) << "Initialized class " << klass->GetDescriptor(&temp) << " from " << |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 5432 | klass->GetLocation(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 5433 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5434 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5435 | } |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 5436 | if (callback != nullptr) { |
| 5437 | callback->MakeVisible(self); |
| 5438 | } |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 5439 | return success; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5440 | } |
| 5441 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5442 | // We recursively run down the tree of interfaces. We need to do this in the order they are declared |
| 5443 | // and perform the initialization only on those interfaces that contain default methods. |
| 5444 | bool ClassLinker::InitializeDefaultInterfaceRecursive(Thread* self, |
| 5445 | Handle<mirror::Class> iface, |
| 5446 | bool can_init_statics, |
| 5447 | bool can_init_parents) { |
| 5448 | CHECK(iface->IsInterface()); |
| 5449 | size_t num_direct_ifaces = iface->NumDirectInterfaces(); |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 5450 | // Only create the (expensive) handle scope if we need it. |
| 5451 | if (UNLIKELY(num_direct_ifaces > 0)) { |
| 5452 | StackHandleScope<1> hs(self); |
| 5453 | MutableHandle<mirror::Class> handle_super_iface(hs.NewHandle<mirror::Class>(nullptr)); |
| 5454 | // First we initialize all of iface's super-interfaces recursively. |
| 5455 | for (size_t i = 0; i < num_direct_ifaces; i++) { |
Vladimir Marko | b10668c | 2021-06-10 09:52:53 +0100 | [diff] [blame] | 5456 | ObjPtr<mirror::Class> super_iface = iface->GetDirectInterface(i); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 5457 | CHECK(super_iface != nullptr) << iface->PrettyDescriptor() << " iface #" << i; |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 5458 | if (!super_iface->HasBeenRecursivelyInitialized()) { |
| 5459 | // Recursive step |
| 5460 | handle_super_iface.Assign(super_iface); |
| 5461 | if (!InitializeDefaultInterfaceRecursive(self, |
| 5462 | handle_super_iface, |
| 5463 | can_init_statics, |
| 5464 | can_init_parents)) { |
| 5465 | return false; |
| 5466 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5467 | } |
| 5468 | } |
| 5469 | } |
| 5470 | |
| 5471 | bool result = true; |
| 5472 | // Then we initialize 'iface' if it has default methods. We do not need to (and in fact must not) |
| 5473 | // initialize if we don't have default methods. |
| 5474 | if (iface->HasDefaultMethods()) { |
| 5475 | result = EnsureInitialized(self, iface, can_init_statics, can_init_parents); |
| 5476 | } |
| 5477 | |
| 5478 | // Mark that this interface has undergone recursive default interface initialization so we know we |
| 5479 | // can skip it on any later class initializations. We do this even if we are not a default |
| 5480 | // interface since we can still avoid the traversal. This is purely a performance optimization. |
| 5481 | if (result) { |
| 5482 | // TODO This should be done in a better way |
Andreas Gampe | 976b298 | 2018-03-02 17:54:22 -0800 | [diff] [blame] | 5483 | // Note: Use a try-lock to avoid blocking when someone else is holding the lock on this |
| 5484 | // interface. It is bad (Java) style, but not impossible. Marking the recursive |
| 5485 | // initialization is a performance optimization (to avoid another idempotent visit |
| 5486 | // for other implementing classes/interfaces), and can be revisited later. |
| 5487 | ObjectTryLock<mirror::Class> lock(self, iface); |
| 5488 | if (lock.Acquired()) { |
| 5489 | iface->SetRecursivelyInitialized(); |
| 5490 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5491 | } |
| 5492 | return result; |
| 5493 | } |
| 5494 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5495 | bool ClassLinker::WaitForInitializeClass(Handle<mirror::Class> klass, |
| 5496 | Thread* self, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5497 | ObjectLock<mirror::Class>& lock) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5498 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5499 | while (true) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 5500 | self->AssertNoPendingException(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5501 | CHECK(!klass->IsInitialized()); |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5502 | lock.WaitIgnoringInterrupts(); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5503 | |
| 5504 | // When we wake up, repeat the test for init-in-progress. If |
| 5505 | // there's an exception pending (only possible if |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 5506 | // we were not using WaitIgnoringInterrupts), bail out. |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5507 | if (self->IsExceptionPending()) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 5508 | WrapExceptionInInitializer(klass); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5509 | mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5510 | return false; |
| 5511 | } |
| 5512 | // Spurious wakeup? Go back to waiting. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5513 | if (klass->GetStatus() == ClassStatus::kInitializing) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5514 | continue; |
| 5515 | } |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5516 | if (klass->GetStatus() == ClassStatus::kVerified && |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 5517 | Runtime::Current()->IsAotCompiler()) { |
Ian Rogers | 3d1548d | 2012-09-24 14:08:03 -0700 | [diff] [blame] | 5518 | // Compile time initialization failed. |
| 5519 | return false; |
| 5520 | } |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5521 | if (klass->IsErroneous()) { |
| 5522 | // The caller wants an exception, but it was thrown in a |
| 5523 | // different thread. Synthesize one here. |
Brian Carlstrom | df14324 | 2011-10-10 18:05:34 -0700 | [diff] [blame] | 5524 | ThrowNoClassDefFoundError("<clinit> failed for class %s; see exception in other thread", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5525 | klass->PrettyDescriptor().c_str()); |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 5526 | VlogClassInitializationFailure(klass); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5527 | return false; |
| 5528 | } |
| 5529 | if (klass->IsInitialized()) { |
| 5530 | return true; |
| 5531 | } |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5532 | LOG(FATAL) << "Unexpected class status. " << klass->PrettyClass() << " is " |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 5533 | << klass->GetStatus(); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5534 | } |
Ian Rogers | 0714083 | 2014-09-30 15:43:59 -0700 | [diff] [blame] | 5535 | UNREACHABLE(); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5536 | } |
| 5537 | |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5538 | static void ThrowSignatureCheckResolveReturnTypeException(Handle<mirror::Class> klass, |
| 5539 | Handle<mirror::Class> super_klass, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5540 | ArtMethod* method, |
| 5541 | ArtMethod* m) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5542 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5543 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 5544 | DCHECK(!m->IsProxyMethod()); |
| 5545 | const DexFile* dex_file = m->GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 5546 | const dex::MethodId& method_id = dex_file->GetMethodId(m->GetDexMethodIndex()); |
| 5547 | const dex::ProtoId& proto_id = dex_file->GetMethodPrototype(method_id); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 5548 | dex::TypeIndex return_type_idx = proto_id.return_type_idx_; |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5549 | std::string return_type = dex_file->PrettyType(return_type_idx); |
| 5550 | std::string class_loader = mirror::Object::PrettyTypeOf(m->GetDeclaringClass()->GetClassLoader()); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5551 | ThrowWrappedLinkageError(klass.Get(), |
| 5552 | "While checking class %s method %s signature against %s %s: " |
| 5553 | "Failed to resolve return type %s with %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5554 | mirror::Class::PrettyDescriptor(klass.Get()).c_str(), |
| 5555 | ArtMethod::PrettyMethod(method).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5556 | super_klass->IsInterface() ? "interface" : "superclass", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5557 | mirror::Class::PrettyDescriptor(super_klass.Get()).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5558 | return_type.c_str(), class_loader.c_str()); |
| 5559 | } |
| 5560 | |
| 5561 | static void ThrowSignatureCheckResolveArgException(Handle<mirror::Class> klass, |
| 5562 | Handle<mirror::Class> super_klass, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5563 | ArtMethod* method, |
| 5564 | ArtMethod* m, |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5565 | uint32_t index, |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 5566 | dex::TypeIndex arg_type_idx) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5567 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5568 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 5569 | DCHECK(!m->IsProxyMethod()); |
| 5570 | const DexFile* dex_file = m->GetDexFile(); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5571 | std::string arg_type = dex_file->PrettyType(arg_type_idx); |
| 5572 | std::string class_loader = mirror::Object::PrettyTypeOf(m->GetDeclaringClass()->GetClassLoader()); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5573 | ThrowWrappedLinkageError(klass.Get(), |
| 5574 | "While checking class %s method %s signature against %s %s: " |
| 5575 | "Failed to resolve arg %u type %s with %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5576 | mirror::Class::PrettyDescriptor(klass.Get()).c_str(), |
| 5577 | ArtMethod::PrettyMethod(method).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5578 | super_klass->IsInterface() ? "interface" : "superclass", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5579 | mirror::Class::PrettyDescriptor(super_klass.Get()).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5580 | index, arg_type.c_str(), class_loader.c_str()); |
| 5581 | } |
| 5582 | |
| 5583 | static void ThrowSignatureMismatch(Handle<mirror::Class> klass, |
| 5584 | Handle<mirror::Class> super_klass, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5585 | ArtMethod* method, |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5586 | const std::string& error_msg) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5587 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5588 | ThrowLinkageError(klass.Get(), |
| 5589 | "Class %s method %s resolves differently in %s %s: %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5590 | mirror::Class::PrettyDescriptor(klass.Get()).c_str(), |
| 5591 | ArtMethod::PrettyMethod(method).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5592 | super_klass->IsInterface() ? "interface" : "superclass", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5593 | mirror::Class::PrettyDescriptor(super_klass.Get()).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5594 | error_msg.c_str()); |
| 5595 | } |
| 5596 | |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5597 | static bool HasSameSignatureWithDifferentClassLoaders(Thread* self, |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5598 | Handle<mirror::Class> klass, |
| 5599 | Handle<mirror::Class> super_klass, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5600 | ArtMethod* method1, |
| 5601 | ArtMethod* method2) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5602 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5603 | { |
| 5604 | StackHandleScope<1> hs(self); |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 5605 | Handle<mirror::Class> return_type(hs.NewHandle(method1->ResolveReturnType())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 5606 | if (UNLIKELY(return_type == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5607 | ThrowSignatureCheckResolveReturnTypeException(klass, super_klass, method1, method1); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5608 | return false; |
| 5609 | } |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 5610 | ObjPtr<mirror::Class> other_return_type = method2->ResolveReturnType(); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5611 | if (UNLIKELY(other_return_type == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5612 | ThrowSignatureCheckResolveReturnTypeException(klass, super_klass, method1, method2); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5613 | return false; |
| 5614 | } |
Vladimir Marko | 862f43c | 2015-02-10 18:22:57 +0000 | [diff] [blame] | 5615 | if (UNLIKELY(other_return_type != return_type.Get())) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5616 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 5617 | StringPrintf("Return types mismatch: %s(%p) vs %s(%p)", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5618 | return_type->PrettyClassAndClassLoader().c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5619 | return_type.Get(), |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5620 | other_return_type->PrettyClassAndClassLoader().c_str(), |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5621 | other_return_type.Ptr())); |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5622 | return false; |
| 5623 | } |
| 5624 | } |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 5625 | const dex::TypeList* types1 = method1->GetParameterTypeList(); |
| 5626 | const dex::TypeList* types2 = method2->GetParameterTypeList(); |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5627 | if (types1 == nullptr) { |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5628 | if (types2 != nullptr && types2->Size() != 0) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5629 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 5630 | StringPrintf("Type list mismatch with %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5631 | method2->PrettyMethod(true).c_str())); |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5632 | return false; |
| 5633 | } |
| 5634 | return true; |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5635 | } else if (UNLIKELY(types2 == nullptr)) { |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5636 | if (types1->Size() != 0) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5637 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 5638 | StringPrintf("Type list mismatch with %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5639 | method2->PrettyMethod(true).c_str())); |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5640 | return false; |
| 5641 | } |
| 5642 | return true; |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5643 | } |
| 5644 | uint32_t num_types = types1->Size(); |
| 5645 | if (UNLIKELY(num_types != types2->Size())) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5646 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 5647 | StringPrintf("Type list mismatch with %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5648 | method2->PrettyMethod(true).c_str())); |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5649 | return false; |
| 5650 | } |
| 5651 | for (uint32_t i = 0; i < num_types; ++i) { |
Vladimir Marko | 862f43c | 2015-02-10 18:22:57 +0000 | [diff] [blame] | 5652 | StackHandleScope<1> hs(self); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 5653 | dex::TypeIndex param_type_idx = types1->GetTypeItem(i).type_idx_; |
Vladimir Marko | 862f43c | 2015-02-10 18:22:57 +0000 | [diff] [blame] | 5654 | Handle<mirror::Class> param_type(hs.NewHandle( |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 5655 | method1->ResolveClassFromTypeIndex(param_type_idx))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 5656 | if (UNLIKELY(param_type == nullptr)) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5657 | ThrowSignatureCheckResolveArgException(klass, super_klass, method1, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5658 | method1, i, param_type_idx); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5659 | return false; |
| 5660 | } |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 5661 | dex::TypeIndex other_param_type_idx = types2->GetTypeItem(i).type_idx_; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5662 | ObjPtr<mirror::Class> other_param_type = |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 5663 | method2->ResolveClassFromTypeIndex(other_param_type_idx); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5664 | if (UNLIKELY(other_param_type == nullptr)) { |
| 5665 | ThrowSignatureCheckResolveArgException(klass, super_klass, method1, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5666 | method2, i, other_param_type_idx); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5667 | return false; |
| 5668 | } |
Vladimir Marko | 862f43c | 2015-02-10 18:22:57 +0000 | [diff] [blame] | 5669 | if (UNLIKELY(param_type.Get() != other_param_type)) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5670 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 5671 | StringPrintf("Parameter %u type mismatch: %s(%p) vs %s(%p)", |
| 5672 | i, |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5673 | param_type->PrettyClassAndClassLoader().c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5674 | param_type.Get(), |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5675 | other_param_type->PrettyClassAndClassLoader().c_str(), |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5676 | other_param_type.Ptr())); |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5677 | return false; |
| 5678 | } |
| 5679 | } |
| 5680 | return true; |
| 5681 | } |
| 5682 | |
| 5683 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5684 | bool ClassLinker::ValidateSuperClassDescriptors(Handle<mirror::Class> klass) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5685 | if (klass->IsInterface()) { |
| 5686 | return true; |
| 5687 | } |
Ian Rogers | 151f221 | 2014-05-06 11:27:27 -0700 | [diff] [blame] | 5688 | // Begin with the methods local to the superclass. |
Ian Rogers | ded66a0 | 2014-10-28 18:12:55 -0700 | [diff] [blame] | 5689 | Thread* self = Thread::Current(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5690 | StackHandleScope<1> hs(self); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5691 | MutableHandle<mirror::Class> super_klass(hs.NewHandle<mirror::Class>(nullptr)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5692 | if (klass->HasSuperClass() && |
| 5693 | klass->GetClassLoader() != klass->GetSuperClass()->GetClassLoader()) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5694 | super_klass.Assign(klass->GetSuperClass()); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 5695 | for (int i = klass->GetSuperClass()->GetVTableLength() - 1; i >= 0; --i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5696 | auto* m = klass->GetVTableEntry(i, image_pointer_size_); |
| 5697 | auto* super_m = klass->GetSuperClass()->GetVTableEntry(i, image_pointer_size_); |
| 5698 | if (m != super_m) { |
Vladimir Marko | 942fd31 | 2017-01-16 20:52:19 +0000 | [diff] [blame] | 5699 | if (UNLIKELY(!HasSameSignatureWithDifferentClassLoaders(self, |
| 5700 | klass, |
| 5701 | super_klass, |
| 5702 | m, |
| 5703 | super_m))) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5704 | self->AssertPendingException(); |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5705 | return false; |
| 5706 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5707 | } |
| 5708 | } |
| 5709 | } |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 5710 | for (int32_t i = 0; i < klass->GetIfTableCount(); ++i) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5711 | super_klass.Assign(klass->GetIfTable()->GetInterface(i)); |
| 5712 | if (klass->GetClassLoader() != super_klass->GetClassLoader()) { |
| 5713 | uint32_t num_methods = super_klass->NumVirtualMethods(); |
Ian Rogers | 151f221 | 2014-05-06 11:27:27 -0700 | [diff] [blame] | 5714 | for (uint32_t j = 0; j < num_methods; ++j) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5715 | auto* m = klass->GetIfTable()->GetMethodArray(i)->GetElementPtrSize<ArtMethod*>( |
| 5716 | j, image_pointer_size_); |
| 5717 | auto* super_m = super_klass->GetVirtualMethod(j, image_pointer_size_); |
| 5718 | if (m != super_m) { |
Vladimir Marko | 942fd31 | 2017-01-16 20:52:19 +0000 | [diff] [blame] | 5719 | if (UNLIKELY(!HasSameSignatureWithDifferentClassLoaders(self, |
| 5720 | klass, |
| 5721 | super_klass, |
| 5722 | m, |
| 5723 | super_m))) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5724 | self->AssertPendingException(); |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5725 | return false; |
| 5726 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5727 | } |
| 5728 | } |
| 5729 | } |
| 5730 | } |
| 5731 | return true; |
| 5732 | } |
| 5733 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5734 | bool ClassLinker::EnsureInitialized(Thread* self, |
| 5735 | Handle<mirror::Class> c, |
| 5736 | bool can_init_fields, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5737 | bool can_init_parents) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 5738 | DCHECK(c != nullptr); |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 5739 | |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 5740 | if (c->IsInitialized()) { |
Vladimir Marko | 8e11065 | 2019-07-30 10:14:41 +0100 | [diff] [blame] | 5741 | // If we've seen an initialized but not visibly initialized class |
| 5742 | // many times, request visible initialization. |
| 5743 | if (kRuntimeISA == InstructionSet::kX86 || kRuntimeISA == InstructionSet::kX86_64) { |
| 5744 | // Thanks to the x86 memory model classes skip the initialized status. |
| 5745 | DCHECK(c->IsVisiblyInitialized()); |
| 5746 | } else if (UNLIKELY(!c->IsVisiblyInitialized())) { |
| 5747 | if (self->IncrementMakeVisiblyInitializedCounter()) { |
| 5748 | MakeInitializedClassesVisiblyInitialized(self, /*wait=*/ false); |
| 5749 | } |
| 5750 | } |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 5751 | return true; |
| 5752 | } |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 5753 | // SubtypeCheckInfo::Initialized must happen-before any new-instance for that type. |
| 5754 | // |
| 5755 | // Ensure the bitstring is initialized before any of the class initialization |
| 5756 | // logic occurs. Once a class initializer starts running, objects can |
| 5757 | // escape into the heap and use the subtype checking code. |
| 5758 | // |
| 5759 | // Note: A class whose SubtypeCheckInfo is at least Initialized means it |
| 5760 | // can be used as a source for the IsSubClass check, and that all ancestors |
| 5761 | // of the class are Assigned (can be used as a target for IsSubClass check) |
| 5762 | // or Overflowed (can be used as a source for IsSubClass check). |
Vladimir Marko | 305c38b | 2018-02-14 11:50:07 +0000 | [diff] [blame] | 5763 | if (kBitstringSubtypeCheckEnabled) { |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 5764 | MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_); |
Vladimir Marko | 38b8b25 | 2018-01-02 19:07:06 +0000 | [diff] [blame] | 5765 | SubtypeCheck<ObjPtr<mirror::Class>>::EnsureInitialized(c.Get()); |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 5766 | // TODO: Avoid taking subtype_check_lock_ if SubtypeCheck is already initialized. |
| 5767 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5768 | const bool success = InitializeClass(self, c, can_init_fields, can_init_parents); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 5769 | if (!success) { |
| 5770 | if (can_init_fields && can_init_parents) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5771 | CHECK(self->IsExceptionPending()) << c->PrettyClass(); |
Vladimir Marko | ac57691 | 2021-03-31 11:16:22 +0100 | [diff] [blame] | 5772 | } else { |
| 5773 | // There may or may not be an exception pending. If there is, clear it. |
| 5774 | // We propagate the exception only if we can initialize fields and parents. |
| 5775 | self->ClearException(); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 5776 | } |
| 5777 | } else { |
| 5778 | self->AssertNoPendingException(); |
Ian Rogers | 595799e | 2012-01-11 17:32:51 -0800 | [diff] [blame] | 5779 | } |
| 5780 | return success; |
Elliott Hughes | f4c21c9 | 2011-08-19 17:31:31 -0700 | [diff] [blame] | 5781 | } |
| 5782 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5783 | void ClassLinker::FixupTemporaryDeclaringClass(ObjPtr<mirror::Class> temp_class, |
| 5784 | ObjPtr<mirror::Class> new_class) { |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 5785 | DCHECK_EQ(temp_class->NumInstanceFields(), 0u); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 5786 | for (ArtField& field : new_class->GetIFields()) { |
| 5787 | if (field.GetDeclaringClass() == temp_class) { |
| 5788 | field.SetDeclaringClass(new_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5789 | } |
| 5790 | } |
| 5791 | |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 5792 | DCHECK_EQ(temp_class->NumStaticFields(), 0u); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 5793 | for (ArtField& field : new_class->GetSFields()) { |
| 5794 | if (field.GetDeclaringClass() == temp_class) { |
| 5795 | field.SetDeclaringClass(new_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5796 | } |
| 5797 | } |
| 5798 | |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 5799 | DCHECK_EQ(temp_class->NumDirectMethods(), 0u); |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 5800 | DCHECK_EQ(temp_class->NumVirtualMethods(), 0u); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 5801 | for (auto& method : new_class->GetMethods(image_pointer_size_)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5802 | if (method.GetDeclaringClass() == temp_class) { |
| 5803 | method.SetDeclaringClass(new_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5804 | } |
| 5805 | } |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 5806 | |
| 5807 | // Make sure the remembered set and mod-union tables know that we updated some of the native |
| 5808 | // roots. |
Mathieu Chartier | 88ea61e | 2018-06-20 17:45:41 -0700 | [diff] [blame] | 5809 | WriteBarrier::ForEveryFieldWrite(new_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5810 | } |
| 5811 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5812 | void ClassLinker::RegisterClassLoader(ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | 5b83050 | 2016-03-02 10:30:23 -0800 | [diff] [blame] | 5813 | CHECK(class_loader->GetAllocator() == nullptr); |
| 5814 | CHECK(class_loader->GetClassTable() == nullptr); |
| 5815 | Thread* const self = Thread::Current(); |
| 5816 | ClassLoaderData data; |
Ian Rogers | 55256cb | 2017-12-21 17:07:11 -0800 | [diff] [blame] | 5817 | data.weak_root = self->GetJniEnv()->GetVm()->AddWeakGlobalRef(self, class_loader); |
Mathieu Chartier | 5b83050 | 2016-03-02 10:30:23 -0800 | [diff] [blame] | 5818 | // Create and set the class table. |
| 5819 | data.class_table = new ClassTable; |
| 5820 | class_loader->SetClassTable(data.class_table); |
| 5821 | // Create and set the linear allocator. |
| 5822 | data.allocator = Runtime::Current()->CreateLinearAlloc(); |
| 5823 | class_loader->SetAllocator(data.allocator); |
| 5824 | // Add to the list so that we know to free the data later. |
| 5825 | class_loaders_.push_back(data); |
| 5826 | } |
| 5827 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5828 | ClassTable* ClassLinker::InsertClassTableForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 5829 | if (class_loader == nullptr) { |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 5830 | return boot_class_table_.get(); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5831 | } |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 5832 | ClassTable* class_table = class_loader->GetClassTable(); |
| 5833 | if (class_table == nullptr) { |
Mathieu Chartier | 5b83050 | 2016-03-02 10:30:23 -0800 | [diff] [blame] | 5834 | RegisterClassLoader(class_loader); |
| 5835 | class_table = class_loader->GetClassTable(); |
| 5836 | DCHECK(class_table != nullptr); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 5837 | } |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5838 | return class_table; |
| 5839 | } |
| 5840 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5841 | ClassTable* ClassLinker::ClassTableForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) { |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 5842 | return class_loader == nullptr ? boot_class_table_.get() : class_loader->GetClassTable(); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5843 | } |
| 5844 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5845 | static ImTable* FindSuperImt(ObjPtr<mirror::Class> klass, PointerSize pointer_size) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5846 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5847 | while (klass->HasSuperClass()) { |
| 5848 | klass = klass->GetSuperClass(); |
| 5849 | if (klass->ShouldHaveImt()) { |
| 5850 | return klass->GetImt(pointer_size); |
| 5851 | } |
| 5852 | } |
| 5853 | return nullptr; |
| 5854 | } |
| 5855 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5856 | bool ClassLinker::LinkClass(Thread* self, |
| 5857 | const char* descriptor, |
| 5858 | Handle<mirror::Class> klass, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5859 | Handle<mirror::ObjectArray<mirror::Class>> interfaces, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5860 | MutableHandle<mirror::Class>* h_new_class_out) { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5861 | CHECK_EQ(ClassStatus::kLoaded, klass->GetStatus()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5862 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5863 | if (!LinkSuperClass(klass)) { |
| 5864 | return false; |
| 5865 | } |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5866 | ArtMethod* imt_data[ImTable::kSize]; |
| 5867 | // If there are any new conflicts compared to super class. |
| 5868 | bool new_conflict = false; |
Nicolas Geoffray | 918dcea | 2017-07-21 07:58:14 +0000 | [diff] [blame] | 5869 | std::fill_n(imt_data, arraysize(imt_data), Runtime::Current()->GetImtUnimplementedMethod()); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5870 | if (!LinkMethods(self, klass, interfaces, &new_conflict, imt_data)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5871 | return false; |
| 5872 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5873 | if (!LinkInstanceFields(self, klass)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5874 | return false; |
| 5875 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5876 | size_t class_size; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5877 | if (!LinkStaticFields(self, klass, &class_size)) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 5878 | return false; |
| 5879 | } |
| 5880 | CreateReferenceInstanceOffsets(klass); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5881 | CHECK_EQ(ClassStatus::kLoaded, klass->GetStatus()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5882 | |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5883 | ImTable* imt = nullptr; |
| 5884 | if (klass->ShouldHaveImt()) { |
| 5885 | // If there are any new conflicts compared to the super class we can not make a copy. There |
| 5886 | // can be cases where both will have a conflict method at the same slot without having the same |
| 5887 | // set of conflicts. In this case, we can not share the IMT since the conflict table slow path |
| 5888 | // will possibly create a table that is incorrect for either of the classes. |
| 5889 | // Same IMT with new_conflict does not happen very often. |
| 5890 | if (!new_conflict) { |
| 5891 | ImTable* super_imt = FindSuperImt(klass.Get(), image_pointer_size_); |
| 5892 | if (super_imt != nullptr) { |
| 5893 | bool imt_equals = true; |
| 5894 | for (size_t i = 0; i < ImTable::kSize && imt_equals; ++i) { |
| 5895 | imt_equals = imt_equals && (super_imt->Get(i, image_pointer_size_) == imt_data[i]); |
| 5896 | } |
| 5897 | if (imt_equals) { |
| 5898 | imt = super_imt; |
| 5899 | } |
| 5900 | } |
| 5901 | } |
| 5902 | if (imt == nullptr) { |
| 5903 | LinearAlloc* allocator = GetAllocatorForClassLoader(klass->GetClassLoader()); |
| 5904 | imt = reinterpret_cast<ImTable*>( |
| 5905 | allocator->Alloc(self, ImTable::SizeInBytes(image_pointer_size_))); |
| 5906 | if (imt == nullptr) { |
| 5907 | return false; |
| 5908 | } |
| 5909 | imt->Populate(imt_data, image_pointer_size_); |
| 5910 | } |
| 5911 | } |
| 5912 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5913 | if (!klass->IsTemp() || (!init_done_ && klass->GetClassSize() == class_size)) { |
| 5914 | // We don't need to retire this class as it has no embedded tables or it was created the |
| 5915 | // correct size during class linker initialization. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5916 | CHECK_EQ(klass->GetClassSize(), class_size) << klass->PrettyDescriptor(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5917 | |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5918 | if (klass->ShouldHaveEmbeddedVTable()) { |
| 5919 | klass->PopulateEmbeddedVTable(image_pointer_size_); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5920 | } |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5921 | if (klass->ShouldHaveImt()) { |
| 5922 | klass->SetImt(imt, image_pointer_size_); |
| 5923 | } |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 5924 | |
| 5925 | // Update CHA info based on whether we override methods. |
| 5926 | // Have to do this before setting the class as resolved which allows |
| 5927 | // instantiation of klass. |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 5928 | if (LIKELY(descriptor != nullptr) && cha_ != nullptr) { |
Andreas Gampe | c1ac9ee | 2017-07-24 22:35:49 -0700 | [diff] [blame] | 5929 | cha_->UpdateAfterLoadingOf(klass); |
| 5930 | } |
Nicolas Geoffray | 918dcea | 2017-07-21 07:58:14 +0000 | [diff] [blame] | 5931 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5932 | // This will notify waiters on klass that saw the not yet resolved |
| 5933 | // class in the class_table_ during EnsureResolved. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5934 | mirror::Class::SetStatus(klass, ClassStatus::kResolved, self); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 5935 | h_new_class_out->Assign(klass.Get()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5936 | } else { |
| 5937 | CHECK(!klass->IsResolved()); |
| 5938 | // Retire the temporary class and create the correctly sized resolved class. |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 5939 | StackHandleScope<1> hs(self); |
Vladimir Marko | 3068d58 | 2019-05-28 16:39:29 +0100 | [diff] [blame] | 5940 | Handle<mirror::Class> h_new_class = |
| 5941 | 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] | 5942 | // Set arrays to null since we don't want to have multiple classes with the same ArtField or |
| 5943 | // ArtMethod array pointers. If this occurs, it causes bugs in remembered sets since the GC |
| 5944 | // may not see any references to the target space and clean the card for a class if another |
| 5945 | // class had the same array pointer. |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 5946 | klass->SetMethodsPtrUnchecked(nullptr, 0, 0); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 5947 | klass->SetSFieldsPtrUnchecked(nullptr); |
| 5948 | klass->SetIFieldsPtrUnchecked(nullptr); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 5949 | if (UNLIKELY(h_new_class == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5950 | self->AssertPendingOOMException(); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5951 | mirror::Class::SetStatus(klass, ClassStatus::kErrorUnresolved, self); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5952 | return false; |
| 5953 | } |
| 5954 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 5955 | CHECK_EQ(h_new_class->GetClassSize(), class_size); |
| 5956 | ObjectLock<mirror::Class> lock(self, h_new_class); |
| 5957 | FixupTemporaryDeclaringClass(klass.Get(), h_new_class.Get()); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5958 | |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 5959 | if (LIKELY(descriptor != nullptr)) { |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 5960 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Vladimir Marko | 0984e48 | 2019-03-27 16:41:41 +0000 | [diff] [blame] | 5961 | const ObjPtr<mirror::ClassLoader> class_loader = h_new_class.Get()->GetClassLoader(); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5962 | ClassTable* const table = InsertClassTableForClassLoader(class_loader); |
Vladimir Marko | 0984e48 | 2019-03-27 16:41:41 +0000 | [diff] [blame] | 5963 | const ObjPtr<mirror::Class> existing = |
| 5964 | table->UpdateClass(descriptor, h_new_class.Get(), ComputeModifiedUtf8Hash(descriptor)); |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 5965 | if (class_loader != nullptr) { |
| 5966 | // We updated the class in the class table, perform the write barrier so that the GC knows |
| 5967 | // about the change. |
Mathieu Chartier | 88ea61e | 2018-06-20 17:45:41 -0700 | [diff] [blame] | 5968 | WriteBarrier::ForEveryFieldWrite(class_loader); |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 5969 | } |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5970 | CHECK_EQ(existing, klass.Get()); |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 5971 | if (log_new_roots_) { |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5972 | new_class_roots_.push_back(GcRoot<mirror::Class>(h_new_class.Get())); |
| 5973 | } |
| 5974 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5975 | |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 5976 | // Update CHA info based on whether we override methods. |
| 5977 | // Have to do this before setting the class as resolved which allows |
| 5978 | // instantiation of klass. |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 5979 | if (LIKELY(descriptor != nullptr) && cha_ != nullptr) { |
Andreas Gampe | c1ac9ee | 2017-07-24 22:35:49 -0700 | [diff] [blame] | 5980 | cha_->UpdateAfterLoadingOf(h_new_class); |
| 5981 | } |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 5982 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5983 | // This will notify waiters on temp class that saw the not yet resolved class in the |
| 5984 | // class_table_ during EnsureResolved. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5985 | mirror::Class::SetStatus(klass, ClassStatus::kRetired, self); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5986 | |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5987 | CHECK_EQ(h_new_class->GetStatus(), ClassStatus::kResolving); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5988 | // This will notify waiters on new_class that saw the not yet resolved |
| 5989 | // class in the class_table_ during EnsureResolved. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5990 | mirror::Class::SetStatus(h_new_class, ClassStatus::kResolved, self); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 5991 | // Return the new class. |
| 5992 | h_new_class_out->Assign(h_new_class.Get()); |
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 | return true; |
| 5995 | } |
| 5996 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5997 | bool ClassLinker::LoadSuperAndInterfaces(Handle<mirror::Class> klass, const DexFile& dex_file) { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5998 | CHECK_EQ(ClassStatus::kIdx, klass->GetStatus()); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 5999 | const dex::ClassDef& class_def = dex_file.GetClassDef(klass->GetDexClassDefIndex()); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 6000 | dex::TypeIndex super_class_idx = class_def.superclass_idx_; |
| 6001 | if (super_class_idx.IsValid()) { |
Roland Levillain | 90328ac | 2016-05-18 12:25:38 +0100 | [diff] [blame] | 6002 | // Check that a class does not inherit from itself directly. |
| 6003 | // |
| 6004 | // TODO: This is a cheap check to detect the straightforward case |
| 6005 | // of a class extending itself (b/28685551), but we should do a |
| 6006 | // proper cycle detection on loaded classes, to detect all cases |
| 6007 | // of class circularity errors (b/28830038). |
| 6008 | if (super_class_idx == class_def.class_idx_) { |
| 6009 | ThrowClassCircularityError(klass.Get(), |
| 6010 | "Class %s extends itself", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6011 | klass->PrettyDescriptor().c_str()); |
Roland Levillain | 90328ac | 2016-05-18 12:25:38 +0100 | [diff] [blame] | 6012 | return false; |
| 6013 | } |
| 6014 | |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 6015 | ObjPtr<mirror::Class> super_class = ResolveType(super_class_idx, klass.Get()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 6016 | if (super_class == nullptr) { |
Brian Carlstrom | 65ca077 | 2011-09-24 16:03:08 -0700 | [diff] [blame] | 6017 | DCHECK(Thread::Current()->IsExceptionPending()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6018 | return false; |
| 6019 | } |
Ian Rogers | be125a9 | 2012-01-11 15:19:49 -0800 | [diff] [blame] | 6020 | // Verify |
| 6021 | if (!klass->CanAccess(super_class)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 6022 | ThrowIllegalAccessError(klass.Get(), "Class %s extended by class %s is inaccessible", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6023 | super_class->PrettyDescriptor().c_str(), |
| 6024 | klass->PrettyDescriptor().c_str()); |
Ian Rogers | be125a9 | 2012-01-11 15:19:49 -0800 | [diff] [blame] | 6025 | return false; |
| 6026 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 6027 | CHECK(super_class->IsResolved()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 6028 | klass->SetSuperClass(super_class); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6029 | } |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 6030 | const dex::TypeList* interfaces = dex_file.GetInterfacesList(class_def); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 6031 | if (interfaces != nullptr) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 6032 | for (size_t i = 0; i < interfaces->Size(); i++) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 6033 | dex::TypeIndex idx = interfaces->GetTypeItem(i).type_idx_; |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 6034 | ObjPtr<mirror::Class> interface = ResolveType(idx, klass.Get()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 6035 | if (interface == nullptr) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 6036 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 6037 | return false; |
| 6038 | } |
| 6039 | // Verify |
| 6040 | if (!klass->CanAccess(interface)) { |
| 6041 | // TODO: the RI seemed to ignore this in my testing. |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 6042 | ThrowIllegalAccessError(klass.Get(), |
| 6043 | "Interface %s implemented by class %s is inaccessible", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6044 | interface->PrettyDescriptor().c_str(), |
| 6045 | klass->PrettyDescriptor().c_str()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 6046 | return false; |
| 6047 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6048 | } |
| 6049 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 6050 | // Mark the class as loaded. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 6051 | mirror::Class::SetStatus(klass, ClassStatus::kLoaded, nullptr); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6052 | return true; |
| 6053 | } |
| 6054 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 6055 | bool ClassLinker::LinkSuperClass(Handle<mirror::Class> klass) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6056 | CHECK(!klass->IsPrimitive()); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6057 | ObjPtr<mirror::Class> super = klass->GetSuperClass(); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 6058 | ObjPtr<mirror::Class> object_class = GetClassRoot<mirror::Object>(this); |
| 6059 | if (klass.Get() == object_class) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 6060 | if (super != nullptr) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 6061 | ThrowClassFormatError(klass.Get(), "java.lang.Object must not have a superclass"); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6062 | return false; |
| 6063 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6064 | return true; |
| 6065 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 6066 | if (super == nullptr) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 6067 | ThrowLinkageError(klass.Get(), "No superclass defined for class %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6068 | klass->PrettyDescriptor().c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6069 | return false; |
| 6070 | } |
| 6071 | // Verify |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 6072 | if (klass->IsInterface() && super != object_class) { |
Vladimir Marko | 1fcae9f | 2017-11-28 14:14:19 +0000 | [diff] [blame] | 6073 | ThrowClassFormatError(klass.Get(), "Interfaces must have java.lang.Object as superclass"); |
| 6074 | return false; |
| 6075 | } |
Vladimir Marko | b43b2d8 | 2017-07-18 17:46:38 +0100 | [diff] [blame] | 6076 | if (super->IsFinal()) { |
| 6077 | ThrowVerifyError(klass.Get(), |
| 6078 | "Superclass %s of %s is declared final", |
| 6079 | super->PrettyDescriptor().c_str(), |
| 6080 | klass->PrettyDescriptor().c_str()); |
| 6081 | return false; |
| 6082 | } |
| 6083 | if (super->IsInterface()) { |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 6084 | ThrowIncompatibleClassChangeError(klass.Get(), |
Vladimir Marko | b43b2d8 | 2017-07-18 17:46:38 +0100 | [diff] [blame] | 6085 | "Superclass %s of %s is an interface", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6086 | super->PrettyDescriptor().c_str(), |
Vladimir Marko | b43b2d8 | 2017-07-18 17:46:38 +0100 | [diff] [blame] | 6087 | klass->PrettyDescriptor().c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6088 | return false; |
| 6089 | } |
| 6090 | if (!klass->CanAccess(super)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 6091 | ThrowIllegalAccessError(klass.Get(), "Superclass %s is inaccessible to class %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6092 | super->PrettyDescriptor().c_str(), |
| 6093 | klass->PrettyDescriptor().c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6094 | return false; |
| 6095 | } |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 6096 | |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 6097 | // Inherit kAccClassIsFinalizable from the superclass in case this |
| 6098 | // class doesn't override finalize. |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 6099 | if (super->IsFinalizable()) { |
| 6100 | klass->SetFinalizable(); |
| 6101 | } |
| 6102 | |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 6103 | // Inherit class loader flag form super class. |
| 6104 | if (super->IsClassLoaderClass()) { |
| 6105 | klass->SetClassLoaderClass(); |
| 6106 | } |
| 6107 | |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 6108 | // Inherit reference flags (if any) from the superclass. |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 6109 | uint32_t reference_flags = (super->GetClassFlags() & mirror::kClassFlagReference); |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 6110 | if (reference_flags != 0) { |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 6111 | CHECK_EQ(klass->GetClassFlags(), 0u); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 6112 | klass->SetClassFlags(klass->GetClassFlags() | reference_flags); |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 6113 | } |
Elliott Hughes | 72ee0ae | 2011-10-10 17:31:28 -0700 | [diff] [blame] | 6114 | // Disallow custom direct subclasses of java.lang.ref.Reference. |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 6115 | if (init_done_ && super == GetClassRoot<mirror::Reference>(this)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 6116 | ThrowLinkageError(klass.Get(), |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 6117 | "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] | 6118 | klass->PrettyDescriptor().c_str()); |
Elliott Hughes | 72ee0ae | 2011-10-10 17:31:28 -0700 | [diff] [blame] | 6119 | return false; |
| 6120 | } |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 6121 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 6122 | if (kIsDebugBuild) { |
| 6123 | // Ensure super classes are fully resolved prior to resolving fields.. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 6124 | while (super != nullptr) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 6125 | CHECK(super->IsResolved()); |
| 6126 | super = super->GetSuperClass(); |
| 6127 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 6128 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6129 | return true; |
| 6130 | } |
| 6131 | |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 6132 | // Comparator for name and signature of a method, used in finding overriding methods. Implementation |
| 6133 | // avoids the use of handles, if it didn't then rather than compare dex files we could compare dex |
| 6134 | // caches in the implementation below. |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 6135 | class MethodNameAndSignatureComparator final : public ValueObject { |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 6136 | public: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6137 | explicit MethodNameAndSignatureComparator(ArtMethod* method) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 6138 | REQUIRES_SHARED(Locks::mutator_lock_) : |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 6139 | dex_file_(method->GetDexFile()), mid_(&dex_file_->GetMethodId(method->GetDexMethodIndex())), |
Vladimir Marko | b4bd92f | 2021-07-05 12:18:26 +0100 | [diff] [blame] | 6140 | name_view_() { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6141 | DCHECK(!method->IsProxyMethod()) << method->PrettyMethod(); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6142 | } |
| 6143 | |
Vladimir Marko | b4bd92f | 2021-07-05 12:18:26 +0100 | [diff] [blame] | 6144 | ALWAYS_INLINE std::string_view GetNameView() { |
| 6145 | if (name_view_.empty()) { |
| 6146 | name_view_ = dex_file_->StringViewByIdx(mid_->name_idx_); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6147 | } |
Vladimir Marko | b4bd92f | 2021-07-05 12:18:26 +0100 | [diff] [blame] | 6148 | return name_view_; |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 6149 | } |
| 6150 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6151 | bool HasSameNameAndSignature(ArtMethod* other) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 6152 | REQUIRES_SHARED(Locks::mutator_lock_) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6153 | DCHECK(!other->IsProxyMethod()) << other->PrettyMethod(); |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 6154 | const DexFile* other_dex_file = other->GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 6155 | const dex::MethodId& other_mid = other_dex_file->GetMethodId(other->GetDexMethodIndex()); |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 6156 | if (dex_file_ == other_dex_file) { |
| 6157 | return mid_->name_idx_ == other_mid.name_idx_ && mid_->proto_idx_ == other_mid.proto_idx_; |
| 6158 | } |
Vladimir Marko | b4bd92f | 2021-07-05 12:18:26 +0100 | [diff] [blame] | 6159 | return GetNameView() == other_dex_file->StringViewByIdx(other_mid.name_idx_) && |
| 6160 | dex_file_->GetMethodSignature(*mid_) == other_dex_file->GetMethodSignature(other_mid); |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 6161 | } |
| 6162 | |
| 6163 | private: |
| 6164 | // Dex file for the method to compare against. |
| 6165 | const DexFile* const dex_file_; |
| 6166 | // MethodId for the method to compare against. |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 6167 | const dex::MethodId* const mid_; |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 6168 | // Lazily computed name from the dex file's strings. |
Vladimir Marko | b4bd92f | 2021-07-05 12:18:26 +0100 | [diff] [blame] | 6169 | std::string_view name_view_; |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 6170 | }; |
| 6171 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6172 | ArtMethod* ClassLinker::AddMethodToConflictTable(ObjPtr<mirror::Class> klass, |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6173 | ArtMethod* conflict_method, |
| 6174 | ArtMethod* interface_method, |
Nicolas Geoffray | 47213e4 | 2020-12-30 15:12:00 +0000 | [diff] [blame] | 6175 | ArtMethod* method) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 6176 | ImtConflictTable* current_table = conflict_method->GetImtConflictTable(kRuntimePointerSize); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6177 | Runtime* const runtime = Runtime::Current(); |
| 6178 | LinearAlloc* linear_alloc = GetAllocatorForClassLoader(klass->GetClassLoader()); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6179 | |
| 6180 | // 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] | 6181 | ArtMethod* new_conflict_method = (conflict_method == runtime->GetImtConflictMethod()) |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6182 | ? runtime->CreateImtConflictMethod(linear_alloc) |
| 6183 | : conflict_method; |
| 6184 | |
| 6185 | // Allocate a new table. Note that we will leak this table at the next conflict, |
| 6186 | // but that's a tradeoff compared to making the table fixed size. |
| 6187 | void* data = linear_alloc->Alloc( |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6188 | Thread::Current(), ImtConflictTable::ComputeSizeWithOneMoreEntry(current_table, |
| 6189 | image_pointer_size_)); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6190 | if (data == nullptr) { |
| 6191 | LOG(ERROR) << "Failed to allocate conflict table"; |
| 6192 | return conflict_method; |
| 6193 | } |
| 6194 | ImtConflictTable* new_table = new (data) ImtConflictTable(current_table, |
| 6195 | interface_method, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6196 | method, |
| 6197 | image_pointer_size_); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6198 | |
| 6199 | // Do a fence to ensure threads see the data in the table before it is assigned |
| 6200 | // to the conflict method. |
| 6201 | // Note that there is a race in the presence of multiple threads and we may leak |
| 6202 | // memory from the LinearAlloc, but that's a tradeoff compared to using |
| 6203 | // atomic operations. |
Orion Hodson | 27b9676 | 2018-03-13 16:06:57 +0000 | [diff] [blame] | 6204 | std::atomic_thread_fence(std::memory_order_release); |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6205 | new_conflict_method->SetImtConflictTable(new_table, image_pointer_size_); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6206 | return new_conflict_method; |
| 6207 | } |
| 6208 | |
| 6209 | void ClassLinker::SetIMTRef(ArtMethod* unimplemented_method, |
| 6210 | ArtMethod* imt_conflict_method, |
| 6211 | ArtMethod* current_method, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6212 | /*out*/bool* new_conflict, |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6213 | /*out*/ArtMethod** imt_ref) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6214 | // Place method in imt if entry is empty, place conflict otherwise. |
| 6215 | if (*imt_ref == unimplemented_method) { |
| 6216 | *imt_ref = current_method; |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 6217 | } else if (!(*imt_ref)->IsRuntimeMethod()) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6218 | // If we are not a conflict and we have the same signature and name as the imt |
| 6219 | // entry, it must be that we overwrote a superclass vtable entry. |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 6220 | // Note that we have checked IsRuntimeMethod, as there may be multiple different |
| 6221 | // conflict methods. |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6222 | MethodNameAndSignatureComparator imt_comparator( |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6223 | (*imt_ref)->GetInterfaceMethodIfProxy(image_pointer_size_)); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6224 | if (imt_comparator.HasSameNameAndSignature( |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6225 | current_method->GetInterfaceMethodIfProxy(image_pointer_size_))) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6226 | *imt_ref = current_method; |
| 6227 | } else { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6228 | *imt_ref = imt_conflict_method; |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6229 | *new_conflict = true; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6230 | } |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 6231 | } else { |
| 6232 | // Place the default conflict method. Note that there may be an existing conflict |
| 6233 | // method in the IMT, but it could be one tailored to the super class, with a |
| 6234 | // specific ImtConflictTable. |
| 6235 | *imt_ref = imt_conflict_method; |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6236 | *new_conflict = true; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6237 | } |
| 6238 | } |
| 6239 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6240 | void ClassLinker::FillIMTAndConflictTables(ObjPtr<mirror::Class> klass) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6241 | DCHECK(klass->ShouldHaveImt()) << klass->PrettyClass(); |
| 6242 | DCHECK(!klass->IsTemp()) << klass->PrettyClass(); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6243 | ArtMethod* imt_data[ImTable::kSize]; |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6244 | Runtime* const runtime = Runtime::Current(); |
| 6245 | ArtMethod* const unimplemented_method = runtime->GetImtUnimplementedMethod(); |
| 6246 | ArtMethod* const conflict_method = runtime->GetImtConflictMethod(); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6247 | std::fill_n(imt_data, arraysize(imt_data), unimplemented_method); |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6248 | if (klass->GetIfTable() != nullptr) { |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6249 | bool new_conflict = false; |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6250 | FillIMTFromIfTable(klass->GetIfTable(), |
| 6251 | unimplemented_method, |
| 6252 | conflict_method, |
| 6253 | klass, |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 6254 | /*create_conflict_tables=*/true, |
| 6255 | /*ignore_copied_methods=*/false, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6256 | &new_conflict, |
| 6257 | &imt_data[0]); |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6258 | } |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6259 | // Compare the IMT with the super class including the conflict methods. If they are equivalent, |
| 6260 | // we can just use the same pointer. |
| 6261 | ImTable* imt = nullptr; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6262 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6263 | if (super_class != nullptr && super_class->ShouldHaveImt()) { |
| 6264 | ImTable* super_imt = super_class->GetImt(image_pointer_size_); |
| 6265 | bool same = true; |
| 6266 | for (size_t i = 0; same && i < ImTable::kSize; ++i) { |
| 6267 | ArtMethod* method = imt_data[i]; |
| 6268 | ArtMethod* super_method = super_imt->Get(i, image_pointer_size_); |
| 6269 | if (method != super_method) { |
| 6270 | bool is_conflict_table = method->IsRuntimeMethod() && |
| 6271 | method != unimplemented_method && |
| 6272 | method != conflict_method; |
| 6273 | // Verify conflict contents. |
| 6274 | bool super_conflict_table = super_method->IsRuntimeMethod() && |
| 6275 | super_method != unimplemented_method && |
| 6276 | super_method != conflict_method; |
| 6277 | if (!is_conflict_table || !super_conflict_table) { |
| 6278 | same = false; |
| 6279 | } else { |
| 6280 | ImtConflictTable* table1 = method->GetImtConflictTable(image_pointer_size_); |
| 6281 | ImtConflictTable* table2 = super_method->GetImtConflictTable(image_pointer_size_); |
| 6282 | same = same && table1->Equals(table2, image_pointer_size_); |
| 6283 | } |
| 6284 | } |
| 6285 | } |
| 6286 | if (same) { |
| 6287 | imt = super_imt; |
| 6288 | } |
| 6289 | } |
| 6290 | if (imt == nullptr) { |
| 6291 | imt = klass->GetImt(image_pointer_size_); |
| 6292 | DCHECK(imt != nullptr); |
| 6293 | imt->Populate(imt_data, image_pointer_size_); |
| 6294 | } else { |
| 6295 | klass->SetImt(imt, image_pointer_size_); |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6296 | } |
| 6297 | } |
| 6298 | |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6299 | ImtConflictTable* ClassLinker::CreateImtConflictTable(size_t count, |
| 6300 | LinearAlloc* linear_alloc, |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 6301 | PointerSize image_pointer_size) { |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6302 | void* data = linear_alloc->Alloc(Thread::Current(), |
| 6303 | ImtConflictTable::ComputeSize(count, |
| 6304 | image_pointer_size)); |
| 6305 | return (data != nullptr) ? new (data) ImtConflictTable(count, image_pointer_size) : nullptr; |
| 6306 | } |
| 6307 | |
| 6308 | ImtConflictTable* ClassLinker::CreateImtConflictTable(size_t count, LinearAlloc* linear_alloc) { |
| 6309 | return CreateImtConflictTable(count, linear_alloc, image_pointer_size_); |
| 6310 | } |
| 6311 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6312 | void ClassLinker::FillIMTFromIfTable(ObjPtr<mirror::IfTable> if_table, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6313 | ArtMethod* unimplemented_method, |
| 6314 | ArtMethod* imt_conflict_method, |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6315 | ObjPtr<mirror::Class> klass, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6316 | bool create_conflict_tables, |
| 6317 | bool ignore_copied_methods, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6318 | /*out*/bool* new_conflict, |
| 6319 | /*out*/ArtMethod** imt) { |
| 6320 | uint32_t conflict_counts[ImTable::kSize] = {}; |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6321 | for (size_t i = 0, length = if_table->Count(); i < length; ++i) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6322 | ObjPtr<mirror::Class> interface = if_table->GetInterface(i); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6323 | const size_t num_virtuals = interface->NumVirtualMethods(); |
| 6324 | const size_t method_array_count = if_table->GetMethodArrayCount(i); |
| 6325 | // Virtual methods can be larger than the if table methods if there are default methods. |
| 6326 | DCHECK_GE(num_virtuals, method_array_count); |
| 6327 | if (kIsDebugBuild) { |
| 6328 | if (klass->IsInterface()) { |
| 6329 | DCHECK_EQ(method_array_count, 0u); |
| 6330 | } else { |
| 6331 | DCHECK_EQ(interface->NumDeclaredVirtualMethods(), method_array_count); |
| 6332 | } |
| 6333 | } |
| 6334 | if (method_array_count == 0) { |
| 6335 | continue; |
| 6336 | } |
Vladimir Marko | 557fece | 2019-03-26 14:29:41 +0000 | [diff] [blame] | 6337 | ObjPtr<mirror::PointerArray> method_array = if_table->GetMethodArray(i); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6338 | for (size_t j = 0; j < method_array_count; ++j) { |
| 6339 | ArtMethod* implementation_method = |
| 6340 | method_array->GetElementPtrSize<ArtMethod*>(j, image_pointer_size_); |
| 6341 | if (ignore_copied_methods && implementation_method->IsCopied()) { |
| 6342 | continue; |
| 6343 | } |
| 6344 | DCHECK(implementation_method != nullptr); |
| 6345 | // Miranda methods cannot be used to implement an interface method, but they are safe to put |
| 6346 | // in the IMT since their entrypoint is the interface trampoline. If we put any copied methods |
| 6347 | // or interface methods in the IMT here they will not create extra conflicts since we compare |
| 6348 | // names and signatures in SetIMTRef. |
| 6349 | ArtMethod* interface_method = interface->GetVirtualMethod(j, image_pointer_size_); |
David Srbecky | e36e7f2 | 2018-11-14 14:21:23 +0000 | [diff] [blame] | 6350 | const uint32_t imt_index = interface_method->GetImtIndex(); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6351 | |
| 6352 | // There is only any conflicts if all of the interface methods for an IMT slot don't have |
| 6353 | // the same implementation method, keep track of this to avoid creating a conflict table in |
| 6354 | // this case. |
| 6355 | |
| 6356 | // Conflict table size for each IMT slot. |
| 6357 | ++conflict_counts[imt_index]; |
| 6358 | |
| 6359 | SetIMTRef(unimplemented_method, |
| 6360 | imt_conflict_method, |
| 6361 | implementation_method, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6362 | /*out*/new_conflict, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6363 | /*out*/&imt[imt_index]); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6364 | } |
| 6365 | } |
| 6366 | |
| 6367 | if (create_conflict_tables) { |
| 6368 | // Create the conflict tables. |
| 6369 | LinearAlloc* linear_alloc = GetAllocatorForClassLoader(klass->GetClassLoader()); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6370 | for (size_t i = 0; i < ImTable::kSize; ++i) { |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6371 | size_t conflicts = conflict_counts[i]; |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6372 | if (imt[i] == imt_conflict_method) { |
| 6373 | ImtConflictTable* new_table = CreateImtConflictTable(conflicts, linear_alloc); |
| 6374 | if (new_table != nullptr) { |
| 6375 | ArtMethod* new_conflict_method = |
| 6376 | Runtime::Current()->CreateImtConflictMethod(linear_alloc); |
| 6377 | new_conflict_method->SetImtConflictTable(new_table, image_pointer_size_); |
| 6378 | imt[i] = new_conflict_method; |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6379 | } else { |
| 6380 | LOG(ERROR) << "Failed to allocate conflict table"; |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6381 | imt[i] = imt_conflict_method; |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6382 | } |
| 6383 | } else { |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6384 | DCHECK_NE(imt[i], imt_conflict_method); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6385 | } |
| 6386 | } |
| 6387 | |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6388 | for (size_t i = 0, length = if_table->Count(); i < length; ++i) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6389 | ObjPtr<mirror::Class> interface = if_table->GetInterface(i); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6390 | const size_t method_array_count = if_table->GetMethodArrayCount(i); |
| 6391 | // Virtual methods can be larger than the if table methods if there are default methods. |
| 6392 | if (method_array_count == 0) { |
| 6393 | continue; |
| 6394 | } |
Vladimir Marko | 557fece | 2019-03-26 14:29:41 +0000 | [diff] [blame] | 6395 | ObjPtr<mirror::PointerArray> method_array = if_table->GetMethodArray(i); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6396 | for (size_t j = 0; j < method_array_count; ++j) { |
| 6397 | ArtMethod* implementation_method = |
| 6398 | method_array->GetElementPtrSize<ArtMethod*>(j, image_pointer_size_); |
| 6399 | if (ignore_copied_methods && implementation_method->IsCopied()) { |
| 6400 | continue; |
| 6401 | } |
| 6402 | DCHECK(implementation_method != nullptr); |
| 6403 | ArtMethod* interface_method = interface->GetVirtualMethod(j, image_pointer_size_); |
David Srbecky | e36e7f2 | 2018-11-14 14:21:23 +0000 | [diff] [blame] | 6404 | const uint32_t imt_index = interface_method->GetImtIndex(); |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6405 | if (!imt[imt_index]->IsRuntimeMethod() || |
| 6406 | imt[imt_index] == unimplemented_method || |
| 6407 | imt[imt_index] == imt_conflict_method) { |
| 6408 | continue; |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6409 | } |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6410 | ImtConflictTable* table = imt[imt_index]->GetImtConflictTable(image_pointer_size_); |
| 6411 | const size_t num_entries = table->NumEntries(image_pointer_size_); |
| 6412 | table->SetInterfaceMethod(num_entries, image_pointer_size_, interface_method); |
| 6413 | table->SetImplementationMethod(num_entries, image_pointer_size_, implementation_method); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6414 | } |
| 6415 | } |
| 6416 | } |
| 6417 | } |
| 6418 | |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6419 | namespace { |
| 6420 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6421 | // Simple helper function that checks that no subtypes of 'val' are contained within the 'classes' |
| 6422 | // set. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6423 | static bool NotSubinterfaceOfAny( |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6424 | const ScopedArenaHashSet<mirror::Class*>& classes, |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6425 | ObjPtr<mirror::Class> val) |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6426 | REQUIRES(Roles::uninterruptible_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 6427 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6428 | DCHECK(val != nullptr); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6429 | for (ObjPtr<mirror::Class> c : classes) { |
| 6430 | if (val->IsAssignableFrom(c)) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6431 | return false; |
| 6432 | } |
| 6433 | } |
| 6434 | return true; |
| 6435 | } |
| 6436 | |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6437 | // We record new interfaces by the index of the direct interface and the index in the |
| 6438 | // direct interface's `IfTable`, or `dex::kDexNoIndex` if it's the direct interface itself. |
| 6439 | struct NewInterfaceReference { |
| 6440 | uint32_t direct_interface_index; |
| 6441 | uint32_t direct_interface_iftable_index; |
| 6442 | }; |
| 6443 | |
| 6444 | class ProxyInterfacesAccessor { |
| 6445 | public: |
| 6446 | explicit ProxyInterfacesAccessor(Handle<mirror::ObjectArray<mirror::Class>> interfaces) |
| 6447 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 6448 | : interfaces_(interfaces) {} |
| 6449 | |
| 6450 | size_t GetLength() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 6451 | return interfaces_->GetLength(); |
| 6452 | } |
| 6453 | |
| 6454 | ObjPtr<mirror::Class> GetInterface(size_t index) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 6455 | DCHECK_LT(index, GetLength()); |
| 6456 | return interfaces_->GetWithoutChecks(index); |
| 6457 | } |
| 6458 | |
| 6459 | private: |
| 6460 | Handle<mirror::ObjectArray<mirror::Class>> interfaces_; |
| 6461 | }; |
| 6462 | |
| 6463 | class NonProxyInterfacesAccessor { |
| 6464 | public: |
| 6465 | NonProxyInterfacesAccessor(ClassLinker* class_linker, Handle<mirror::Class> klass) |
| 6466 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 6467 | : interfaces_(klass->GetInterfaceTypeList()), |
| 6468 | class_linker_(class_linker), |
| 6469 | klass_(klass) { |
| 6470 | DCHECK(!klass->IsProxyClass()); |
| 6471 | } |
| 6472 | |
| 6473 | size_t GetLength() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 6474 | return (interfaces_ != nullptr) ? interfaces_->Size() : 0u; |
| 6475 | } |
| 6476 | |
| 6477 | ObjPtr<mirror::Class> GetInterface(size_t index) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 6478 | DCHECK_LT(index, GetLength()); |
| 6479 | dex::TypeIndex type_index = interfaces_->GetTypeItem(index).type_idx_; |
| 6480 | return class_linker_->LookupResolvedType(type_index, klass_.Get()); |
| 6481 | } |
| 6482 | |
| 6483 | private: |
| 6484 | const dex::TypeList* interfaces_; |
| 6485 | ClassLinker* class_linker_; |
| 6486 | Handle<mirror::Class> klass_; |
| 6487 | }; |
| 6488 | |
| 6489 | // 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] | 6490 | // |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6491 | // Interfaces in the interface table must satisfy the following constraint: |
| 6492 | // all I, J: Interface | I <: J implies J precedes I |
| 6493 | // (note A <: B means that A is a subtype of B). We order this backwards so that we do not need |
| 6494 | // 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] | 6495 | // |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6496 | // This function returns a list of references for all interfaces in the transitive |
| 6497 | // closure of the direct interfaces that are not in the superclass interfaces. |
| 6498 | // The entries in the list are ordered to satisfy the interface table ordering |
| 6499 | // constraint and therefore the interface table formed by appending them to the |
| 6500 | // superclass interface table shall also satisfy that constraint. |
| 6501 | template <typename InterfaceAccessor> |
| 6502 | ALWAYS_INLINE |
| 6503 | static ArrayRef<const NewInterfaceReference> FindNewIfTableInterfaces( |
| 6504 | ObjPtr<mirror::IfTable> super_iftable, |
| 6505 | size_t super_ifcount, |
| 6506 | ScopedArenaAllocator* allocator, |
| 6507 | InterfaceAccessor&& interfaces, |
| 6508 | ArrayRef<NewInterfaceReference> initial_storage, |
| 6509 | /*out*/ScopedArenaVector<NewInterfaceReference>* supplemental_storage) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 6510 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 6511 | ScopedAssertNoThreadSuspension nts(__FUNCTION__); |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6512 | |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 6513 | // This is the set of all classes already in the iftable. Used to make checking |
| 6514 | // if a class has already been added quicker. |
| 6515 | constexpr size_t kBufferSize = 32; // 256 bytes on 64-bit architectures. |
| 6516 | mirror::Class* buffer[kBufferSize]; |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6517 | ScopedArenaHashSet<mirror::Class*> classes_in_iftable(buffer, kBufferSize, allocator->Adapter()); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6518 | // The first super_ifcount elements are from the superclass. We note that they are already added. |
| 6519 | for (size_t i = 0; i < super_ifcount; i++) { |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6520 | ObjPtr<mirror::Class> iface = super_iftable->GetInterface(i); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6521 | DCHECK(NotSubinterfaceOfAny(classes_in_iftable, iface)) << "Bad ordering."; |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6522 | classes_in_iftable.Put(iface.Ptr()); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6523 | } |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6524 | |
| 6525 | ArrayRef<NewInterfaceReference> current_storage = initial_storage; |
| 6526 | DCHECK_NE(current_storage.size(), 0u); |
| 6527 | size_t num_new_interfaces = 0u; |
| 6528 | auto insert_reference = [&](uint32_t direct_interface_index, |
| 6529 | uint32_t direct_interface_iface_index) { |
| 6530 | if (UNLIKELY(num_new_interfaces == current_storage.size())) { |
| 6531 | bool copy = current_storage.data() != supplemental_storage->data(); |
| 6532 | supplemental_storage->resize(2u * num_new_interfaces); |
| 6533 | if (copy) { |
| 6534 | std::copy_n(current_storage.data(), num_new_interfaces, supplemental_storage->data()); |
| 6535 | } |
| 6536 | current_storage = ArrayRef<NewInterfaceReference>(*supplemental_storage); |
| 6537 | } |
| 6538 | current_storage[num_new_interfaces] = {direct_interface_index, direct_interface_iface_index}; |
| 6539 | ++num_new_interfaces; |
| 6540 | }; |
| 6541 | |
| 6542 | for (size_t i = 0, num_interfaces = interfaces.GetLength(); i != num_interfaces; ++i) { |
| 6543 | ObjPtr<mirror::Class> interface = interfaces.GetInterface(i); |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 6544 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6545 | // Let us call the first filled_ifcount elements of iftable the current-iface-list. |
| 6546 | // At this point in the loop current-iface-list has the invariant that: |
| 6547 | // for every pair of interfaces I,J within it: |
| 6548 | // if index_of(I) < index_of(J) then I is not a subtype of J |
| 6549 | |
| 6550 | // If we have already seen this element then all of its super-interfaces must already be in the |
| 6551 | // current-iface-list so we can skip adding it. |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 6552 | if (classes_in_iftable.find(interface.Ptr()) == classes_in_iftable.end()) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6553 | // We haven't seen this interface so add all of its super-interfaces onto the |
| 6554 | // current-iface-list, skipping those already on it. |
| 6555 | int32_t ifcount = interface->GetIfTableCount(); |
| 6556 | for (int32_t j = 0; j < ifcount; j++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6557 | ObjPtr<mirror::Class> super_interface = interface->GetIfTable()->GetInterface(j); |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6558 | if (classes_in_iftable.find(super_interface.Ptr()) == classes_in_iftable.end()) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6559 | DCHECK(NotSubinterfaceOfAny(classes_in_iftable, super_interface)) << "Bad ordering."; |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6560 | classes_in_iftable.Put(super_interface.Ptr()); |
| 6561 | insert_reference(i, j); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6562 | } |
| 6563 | } |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6564 | // Add this interface reference after all of its super-interfaces. |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6565 | DCHECK(NotSubinterfaceOfAny(classes_in_iftable, interface)) << "Bad ordering"; |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6566 | classes_in_iftable.Put(interface.Ptr()); |
| 6567 | insert_reference(i, dex::kDexNoIndex); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6568 | } else if (kIsDebugBuild) { |
| 6569 | // Check all super-interfaces are already in the list. |
| 6570 | int32_t ifcount = interface->GetIfTableCount(); |
| 6571 | for (int32_t j = 0; j < ifcount; j++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6572 | ObjPtr<mirror::Class> super_interface = interface->GetIfTable()->GetInterface(j); |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6573 | DCHECK(classes_in_iftable.find(super_interface.Ptr()) != classes_in_iftable.end()) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6574 | << "Iftable does not contain " << mirror::Class::PrettyClass(super_interface) |
| 6575 | << ", a superinterface of " << interface->PrettyClass(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6576 | } |
| 6577 | } |
| 6578 | } |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6579 | return ArrayRef<const NewInterfaceReference>(current_storage.data(), num_new_interfaces); |
| 6580 | } |
| 6581 | |
| 6582 | template <typename InterfaceAccessor> |
| 6583 | static ObjPtr<mirror::IfTable> SetupInterfaceLookupTable( |
| 6584 | Thread* self, |
| 6585 | Handle<mirror::Class> klass, |
| 6586 | ScopedArenaAllocator* allocator, |
| 6587 | InterfaceAccessor&& interfaces) |
| 6588 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 6589 | DCHECK(klass->HasSuperClass()); |
| 6590 | ObjPtr<mirror::IfTable> super_iftable = klass->GetSuperClass()->GetIfTable(); |
Vladimir Marko | 0441d20 | 2022-02-18 13:55:15 +0000 | [diff] [blame] | 6591 | DCHECK(super_iftable != nullptr); |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6592 | const size_t num_interfaces = interfaces.GetLength(); |
| 6593 | |
Vladimir Marko | 0441d20 | 2022-02-18 13:55:15 +0000 | [diff] [blame] | 6594 | // If there are no new interfaces, return the interface table from superclass. |
| 6595 | // If any implementation methods are overridden, we shall copy the table and |
| 6596 | // the method arrays that contain any differences (copy-on-write). |
| 6597 | if (num_interfaces == 0) { |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6598 | return super_iftable; |
| 6599 | } |
| 6600 | |
| 6601 | // Check that every class being implemented is an interface. |
| 6602 | for (size_t i = 0; i != num_interfaces; ++i) { |
| 6603 | ObjPtr<mirror::Class> interface = interfaces.GetInterface(i); |
| 6604 | DCHECK(interface != nullptr); |
| 6605 | if (UNLIKELY(!interface->IsInterface())) { |
| 6606 | ThrowIncompatibleClassChangeError(klass.Get(), |
| 6607 | "Class %s implements non-interface class %s", |
| 6608 | klass->PrettyDescriptor().c_str(), |
| 6609 | interface->PrettyDescriptor().c_str()); |
| 6610 | return nullptr; |
| 6611 | } |
| 6612 | } |
| 6613 | |
| 6614 | static constexpr size_t kMaxStackReferences = 16; |
| 6615 | NewInterfaceReference initial_storage[kMaxStackReferences]; |
| 6616 | ScopedArenaVector<NewInterfaceReference> supplemental_storage(allocator->Adapter()); |
Vladimir Marko | 0441d20 | 2022-02-18 13:55:15 +0000 | [diff] [blame] | 6617 | const size_t super_ifcount = super_iftable->Count(); |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6618 | ArrayRef<const NewInterfaceReference> new_interface_references = |
| 6619 | FindNewIfTableInterfaces( |
| 6620 | super_iftable, |
| 6621 | super_ifcount, |
| 6622 | allocator, |
| 6623 | interfaces, |
| 6624 | ArrayRef<NewInterfaceReference>(initial_storage), |
| 6625 | &supplemental_storage); |
| 6626 | |
Vladimir Marko | 0441d20 | 2022-02-18 13:55:15 +0000 | [diff] [blame] | 6627 | // If all declared interfaces were already present in superclass interface table, |
| 6628 | // return the interface table from superclass. See above. |
| 6629 | if (UNLIKELY(new_interface_references.empty())) { |
| 6630 | return super_iftable; |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6631 | } |
| 6632 | |
| 6633 | // Create the interface table. |
| 6634 | size_t ifcount = super_ifcount + new_interface_references.size(); |
| 6635 | ObjPtr<mirror::IfTable> iftable = AllocIfTable(self, ifcount, super_iftable->GetClass()); |
| 6636 | if (UNLIKELY(iftable == nullptr)) { |
| 6637 | self->AssertPendingOOMException(); |
| 6638 | return nullptr; |
| 6639 | } |
| 6640 | // Fill in table with superclass's iftable. |
| 6641 | if (super_ifcount != 0) { |
| 6642 | // Reload `super_iftable` as it may have been clobbered by the allocation. |
| 6643 | super_iftable = klass->GetSuperClass()->GetIfTable(); |
Vladimir Marko | 0441d20 | 2022-02-18 13:55:15 +0000 | [diff] [blame] | 6644 | for (size_t i = 0; i != super_ifcount; i++) { |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6645 | ObjPtr<mirror::Class> super_interface = super_iftable->GetInterface(i); |
Vladimir Marko | 0441d20 | 2022-02-18 13:55:15 +0000 | [diff] [blame] | 6646 | DCHECK(super_interface != nullptr); |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6647 | iftable->SetInterface(i, super_interface); |
Vladimir Marko | 0441d20 | 2022-02-18 13:55:15 +0000 | [diff] [blame] | 6648 | ObjPtr<mirror::PointerArray> method_array = super_iftable->GetMethodArrayOrNull(i); |
| 6649 | if (method_array != nullptr) { |
| 6650 | iftable->SetMethodArray(i, method_array); |
| 6651 | } |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6652 | } |
| 6653 | } |
| 6654 | // Fill in the table with additional interfaces. |
| 6655 | size_t current_index = super_ifcount; |
| 6656 | for (NewInterfaceReference ref : new_interface_references) { |
| 6657 | ObjPtr<mirror::Class> direct_interface = interfaces.GetInterface(ref.direct_interface_index); |
| 6658 | ObjPtr<mirror::Class> new_interface = (ref.direct_interface_iftable_index != dex::kDexNoIndex) |
| 6659 | ? direct_interface->GetIfTable()->GetInterface(ref.direct_interface_iftable_index) |
| 6660 | : direct_interface; |
| 6661 | iftable->SetInterface(current_index, new_interface); |
| 6662 | ++current_index; |
| 6663 | } |
| 6664 | DCHECK_EQ(current_index, ifcount); |
| 6665 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6666 | if (kIsDebugBuild) { |
| 6667 | // Check that the iftable is ordered correctly. |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6668 | for (size_t i = 0; i < ifcount; i++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6669 | ObjPtr<mirror::Class> if_a = iftable->GetInterface(i); |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6670 | for (size_t j = i + 1; j < ifcount; j++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6671 | ObjPtr<mirror::Class> if_b = iftable->GetInterface(j); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6672 | // !(if_a <: if_b) |
| 6673 | CHECK(!if_b->IsAssignableFrom(if_a)) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6674 | << "Bad interface order: " << mirror::Class::PrettyClass(if_a) << " (index " << i |
| 6675 | << ") extends " |
| 6676 | << 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] | 6677 | << "interface list."; |
| 6678 | } |
| 6679 | } |
| 6680 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6681 | |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6682 | return iftable; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6683 | } |
| 6684 | |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6685 | // Check that all vtable entries are present in this class's virtuals or are the same as a |
| 6686 | // superclasses vtable entry. |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 6687 | void CheckClassOwnsVTableEntries(Thread* self, |
| 6688 | Handle<mirror::Class> klass, |
| 6689 | PointerSize pointer_size) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 6690 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6691 | StackHandleScope<2> hs(self); |
| 6692 | Handle<mirror::PointerArray> check_vtable(hs.NewHandle(klass->GetVTableDuringLinking())); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6693 | ObjPtr<mirror::Class> super_temp = (klass->HasSuperClass()) ? klass->GetSuperClass() : nullptr; |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6694 | Handle<mirror::Class> superclass(hs.NewHandle(super_temp)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 6695 | int32_t super_vtable_length = (superclass != nullptr) ? superclass->GetVTableLength() : 0; |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6696 | for (int32_t i = 0; i < check_vtable->GetLength(); ++i) { |
| 6697 | ArtMethod* m = check_vtable->GetElementPtrSize<ArtMethod*>(i, pointer_size); |
| 6698 | CHECK(m != nullptr); |
| 6699 | |
Alex Light | a41a3078 | 2017-03-29 11:33:19 -0700 | [diff] [blame] | 6700 | if (m->GetMethodIndexDuringLinking() != i) { |
| 6701 | LOG(WARNING) << m->PrettyMethod() |
| 6702 | << " has an unexpected method index for its spot in the vtable for class" |
| 6703 | << klass->PrettyClass(); |
| 6704 | } |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6705 | ArraySlice<ArtMethod> virtuals = klass->GetVirtualMethodsSliceUnchecked(pointer_size); |
| 6706 | auto is_same_method = [m] (const ArtMethod& meth) { |
| 6707 | return &meth == m; |
| 6708 | }; |
Alex Light | 3f98053 | 2017-03-17 15:10:32 -0700 | [diff] [blame] | 6709 | if (!((super_vtable_length > i && superclass->GetVTableEntry(i, pointer_size) == m) || |
| 6710 | std::find_if(virtuals.begin(), virtuals.end(), is_same_method) != virtuals.end())) { |
| 6711 | LOG(WARNING) << m->PrettyMethod() << " does not seem to be owned by current class " |
| 6712 | << klass->PrettyClass() << " or any of its superclasses!"; |
| 6713 | } |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6714 | } |
| 6715 | } |
| 6716 | |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6717 | // Check to make sure the vtable does not have duplicates. Duplicates could cause problems when a |
| 6718 | // method is overridden in a subclass. |
Andreas Gampe | a2fed08 | 2019-02-01 09:34:43 -0800 | [diff] [blame] | 6719 | template <PointerSize kPointerSize> |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 6720 | void CheckVTableHasNoDuplicates(Thread* self, Handle<mirror::Class> klass) |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6721 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 6722 | StackHandleScope<1> hs(self); |
| 6723 | Handle<mirror::PointerArray> vtable(hs.NewHandle(klass->GetVTableDuringLinking())); |
| 6724 | int32_t num_entries = vtable->GetLength(); |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 6725 | |
| 6726 | // Observations: |
| 6727 | // * The older implementation was O(n^2) and got too expensive for apps with larger classes. |
| 6728 | // * Many classes do not override Object functions (e.g., equals/hashCode/toString). Thus, |
| 6729 | // for many classes outside of libcore a cross-dexfile check has to be run anyways. |
| 6730 | // * In the cross-dexfile case, with the O(n^2), in the best case O(n) cross checks would have |
| 6731 | // to be done. It is thus OK in a single-pass algorithm to read all data, anyways. |
| 6732 | // * The single-pass algorithm will trade memory for speed, but that is OK. |
| 6733 | |
| 6734 | CHECK_GT(num_entries, 0); |
| 6735 | |
| 6736 | auto log_fn = [&vtable, &klass](int32_t i, int32_t j) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 6737 | ArtMethod* m1 = vtable->GetElementPtrSize<ArtMethod*, kPointerSize>(i); |
| 6738 | ArtMethod* m2 = vtable->GetElementPtrSize<ArtMethod*, kPointerSize>(j); |
| 6739 | LOG(WARNING) << "vtable entries " << i << " and " << j << " are identical for " |
| 6740 | << klass->PrettyClass() << " in method " << m1->PrettyMethod() |
| 6741 | << " (0x" << std::hex << reinterpret_cast<uintptr_t>(m2) << ") and " |
| 6742 | << m2->PrettyMethod() << " (0x" << std::hex |
| 6743 | << reinterpret_cast<uintptr_t>(m2) << ")"; |
| 6744 | }; |
| 6745 | struct BaseHashType { |
| 6746 | static size_t HashCombine(size_t seed, size_t val) { |
| 6747 | return seed ^ (val + 0x9e3779b9 + (seed << 6) + (seed >> 2)); |
| 6748 | } |
| 6749 | }; |
| 6750 | |
| 6751 | // Check assuming all entries come from the same dex file. |
| 6752 | { |
| 6753 | // Find the first interesting method and its dex file. |
| 6754 | int32_t start = 0; |
| 6755 | for (; start < num_entries; ++start) { |
| 6756 | ArtMethod* vtable_entry = vtable->GetElementPtrSize<ArtMethod*, kPointerSize>(start); |
| 6757 | // Don't bother if we cannot 'see' the vtable entry (i.e. it is a package-private member |
| 6758 | // maybe). |
| 6759 | if (!klass->CanAccessMember(vtable_entry->GetDeclaringClass(), |
| 6760 | vtable_entry->GetAccessFlags())) { |
| 6761 | continue; |
| 6762 | } |
| 6763 | break; |
| 6764 | } |
| 6765 | if (start == num_entries) { |
| 6766 | return; |
| 6767 | } |
| 6768 | const DexFile* dex_file = |
| 6769 | vtable->GetElementPtrSize<ArtMethod*, kPointerSize>(start)-> |
| 6770 | GetInterfaceMethodIfProxy(kPointerSize)->GetDexFile(); |
| 6771 | |
| 6772 | // Helper function to avoid logging if we have to run the cross-file checks. |
| 6773 | auto check_fn = [&](bool log_warn) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 6774 | // Use a map to store seen entries, as the storage space is too large for a bitvector. |
| 6775 | using PairType = std::pair<uint32_t, uint16_t>; |
| 6776 | struct PairHash : BaseHashType { |
| 6777 | size_t operator()(const PairType& key) const { |
| 6778 | return BaseHashType::HashCombine(BaseHashType::HashCombine(0, key.first), key.second); |
| 6779 | } |
| 6780 | }; |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 6781 | HashMap<PairType, int32_t, DefaultMapEmptyFn<PairType, int32_t>, PairHash> seen; |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 6782 | seen.reserve(2 * num_entries); |
| 6783 | bool need_slow_path = false; |
| 6784 | bool found_dup = false; |
| 6785 | for (int i = start; i < num_entries; ++i) { |
| 6786 | // Can use Unchecked here as the start loop already ensured that the arrays are correct |
| 6787 | // wrt/ kPointerSize. |
| 6788 | ArtMethod* vtable_entry = vtable->GetElementPtrSizeUnchecked<ArtMethod*, kPointerSize>(i); |
| 6789 | if (!klass->CanAccessMember(vtable_entry->GetDeclaringClass(), |
| 6790 | vtable_entry->GetAccessFlags())) { |
| 6791 | continue; |
| 6792 | } |
| 6793 | ArtMethod* m = vtable_entry->GetInterfaceMethodIfProxy(kPointerSize); |
| 6794 | if (dex_file != m->GetDexFile()) { |
| 6795 | need_slow_path = true; |
| 6796 | break; |
| 6797 | } |
| 6798 | const dex::MethodId* m_mid = &dex_file->GetMethodId(m->GetDexMethodIndex()); |
| 6799 | PairType pair = std::make_pair(m_mid->name_idx_.index_, m_mid->proto_idx_.index_); |
| 6800 | auto it = seen.find(pair); |
| 6801 | if (it != seen.end()) { |
| 6802 | found_dup = true; |
| 6803 | if (log_warn) { |
| 6804 | log_fn(it->second, i); |
| 6805 | } |
| 6806 | } else { |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 6807 | seen.insert(std::make_pair(pair, i)); |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 6808 | } |
| 6809 | } |
| 6810 | return std::make_pair(need_slow_path, found_dup); |
| 6811 | }; |
| 6812 | std::pair<bool, bool> result = check_fn(/* log_warn= */ false); |
| 6813 | if (!result.first) { |
| 6814 | if (result.second) { |
| 6815 | check_fn(/* log_warn= */ true); |
| 6816 | } |
| 6817 | return; |
| 6818 | } |
| 6819 | } |
| 6820 | |
| 6821 | // Need to check across dex files. |
| 6822 | struct Entry { |
| 6823 | size_t cached_hash = 0; |
Vladimir Marko | aa027b8 | 2021-01-06 20:34:20 +0000 | [diff] [blame] | 6824 | uint32_t name_len = 0; |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 6825 | const char* name = nullptr; |
| 6826 | Signature signature = Signature::NoSignature(); |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 6827 | |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 6828 | Entry() = default; |
| 6829 | Entry(const Entry& other) = default; |
| 6830 | Entry& operator=(const Entry& other) = default; |
| 6831 | |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 6832 | Entry(const DexFile* dex_file, const dex::MethodId& mid) |
Vladimir Marko | aa027b8 | 2021-01-06 20:34:20 +0000 | [diff] [blame] | 6833 | : name_len(0), // Explicit to enforce ordering with -Werror,-Wreorder-ctor. |
| 6834 | // This call writes `name_len` and it is therefore necessary that the |
| 6835 | // initializer for `name_len` comes before it, otherwise the value |
| 6836 | // from the call would be overwritten by that initializer. |
| 6837 | name(dex_file->StringDataAndUtf16LengthByIdx(mid.name_idx_, &name_len)), |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 6838 | signature(dex_file->GetMethodSignature(mid)) { |
Vladimir Marko | aa027b8 | 2021-01-06 20:34:20 +0000 | [diff] [blame] | 6839 | // The `name_len` has been initialized to the UTF16 length. Calculate length in bytes. |
| 6840 | if (name[name_len] != 0) { |
| 6841 | name_len += strlen(name + name_len); |
| 6842 | } |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 6843 | } |
| 6844 | |
| 6845 | bool operator==(const Entry& other) const { |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 6846 | return name_len == other.name_len && |
| 6847 | memcmp(name, other.name, name_len) == 0 && |
| 6848 | signature == other.signature; |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 6849 | } |
| 6850 | }; |
| 6851 | struct EntryHash { |
| 6852 | size_t operator()(const Entry& key) const { |
| 6853 | return key.cached_hash; |
| 6854 | } |
| 6855 | }; |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 6856 | HashMap<Entry, int32_t, DefaultMapEmptyFn<Entry, int32_t>, EntryHash> map; |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 6857 | for (int32_t i = 0; i < num_entries; ++i) { |
| 6858 | // Can use Unchecked here as the first loop already ensured that the arrays are correct |
| 6859 | // wrt/ kPointerSize. |
| 6860 | ArtMethod* vtable_entry = vtable->GetElementPtrSizeUnchecked<ArtMethod*, kPointerSize>(i); |
| 6861 | // Don't bother if we cannot 'see' the vtable entry (i.e. it is a package-private member |
| 6862 | // maybe). |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6863 | if (!klass->CanAccessMember(vtable_entry->GetDeclaringClass(), |
| 6864 | vtable_entry->GetAccessFlags())) { |
| 6865 | continue; |
| 6866 | } |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 6867 | ArtMethod* m = vtable_entry->GetInterfaceMethodIfProxy(kPointerSize); |
| 6868 | const DexFile* dex_file = m->GetDexFile(); |
| 6869 | const dex::MethodId& mid = dex_file->GetMethodId(m->GetDexMethodIndex()); |
| 6870 | |
| 6871 | Entry e(dex_file, mid); |
| 6872 | |
| 6873 | size_t string_hash = std::hash<std::string_view>()(std::string_view(e.name, e.name_len)); |
| 6874 | size_t sig_hash = std::hash<std::string>()(e.signature.ToString()); |
| 6875 | e.cached_hash = BaseHashType::HashCombine(BaseHashType::HashCombine(0u, string_hash), |
| 6876 | sig_hash); |
| 6877 | |
| 6878 | auto it = map.find(e); |
| 6879 | if (it != map.end()) { |
| 6880 | log_fn(it->second, i); |
| 6881 | } else { |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 6882 | map.insert(std::make_pair(e, i)); |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6883 | } |
| 6884 | } |
| 6885 | } |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 6886 | |
| 6887 | void CheckVTableHasNoDuplicates(Thread* self, |
| 6888 | Handle<mirror::Class> klass, |
| 6889 | PointerSize pointer_size) |
Andreas Gampe | a2fed08 | 2019-02-01 09:34:43 -0800 | [diff] [blame] | 6890 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 6891 | switch (pointer_size) { |
| 6892 | case PointerSize::k64: |
| 6893 | CheckVTableHasNoDuplicates<PointerSize::k64>(self, klass); |
| 6894 | break; |
| 6895 | case PointerSize::k32: |
| 6896 | CheckVTableHasNoDuplicates<PointerSize::k32>(self, klass); |
| 6897 | break; |
| 6898 | } |
| 6899 | } |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6900 | |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 6901 | static void CheckVTable(Thread* self, Handle<mirror::Class> klass, PointerSize pointer_size) |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6902 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 6903 | CheckClassOwnsVTableEntries(self, klass, pointer_size); |
| 6904 | CheckVTableHasNoDuplicates(self, klass, pointer_size); |
| 6905 | } |
| 6906 | |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 6907 | } // namespace |
| 6908 | |
Vladimir Marko | b91402f | 2021-12-21 15:55:06 +0000 | [diff] [blame] | 6909 | template <PointerSize kPointerSize> |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 6910 | class ClassLinker::LinkMethodsHelper { |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 6911 | public: |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 6912 | LinkMethodsHelper(ClassLinker* class_linker, |
| 6913 | Handle<mirror::Class> klass, |
| 6914 | Thread* self, |
| 6915 | Runtime* runtime) |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 6916 | : class_linker_(class_linker), |
| 6917 | klass_(klass), |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 6918 | self_(self), |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6919 | runtime_(runtime), |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 6920 | stack_(runtime->GetLinearAlloc()->GetArenaPool()), |
| 6921 | allocator_(&stack_), |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 6922 | copied_method_records_(copied_method_records_initial_buffer_, |
| 6923 | kCopiedMethodRecordInitialBufferSize, |
| 6924 | allocator_.Adapter()), |
| 6925 | num_new_copied_methods_(0u) { |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 6926 | } |
| 6927 | |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6928 | // Links the virtual and interface methods for the given class. |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 6929 | // |
| 6930 | // Arguments: |
| 6931 | // * self - The current thread. |
| 6932 | // * klass - class, whose vtable will be filled in. |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6933 | // * interfaces - implemented interfaces for a proxy class, otherwise null. |
| 6934 | // * out_new_conflict - whether there is a new conflict compared to the superclass. |
| 6935 | // * out_imt - interface method table to fill. |
| 6936 | bool LinkMethods( |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 6937 | Thread* self, |
| 6938 | Handle<mirror::Class> klass, |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6939 | Handle<mirror::ObjectArray<mirror::Class>> interfaces, |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 6940 | bool* out_new_conflict, |
| 6941 | ArtMethod** out_imt) |
| 6942 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 6943 | |
| 6944 | private: |
Vladimir Marko | 0441d20 | 2022-02-18 13:55:15 +0000 | [diff] [blame] | 6945 | // Allocate a pointer array. |
| 6946 | static ObjPtr<mirror::PointerArray> AllocPointerArray(Thread* self, size_t length) |
| 6947 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 6948 | |
| 6949 | // Allocate method arrays for interfaces. |
| 6950 | bool AllocateIfTableMethodArrays(Thread* self, |
| 6951 | Handle<mirror::Class> klass, |
| 6952 | Handle<mirror::IfTable> iftable) |
| 6953 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 6954 | |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 6955 | // Assign vtable indexes to declared virtual methods for a non-interface class other |
| 6956 | // 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] | 6957 | // This function also assigns vtable indexes for interface methods in new interfaces |
| 6958 | // 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] | 6959 | size_t AssignVTableIndexes(ObjPtr<mirror::Class> klass, |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 6960 | ObjPtr<mirror::Class> super_class, |
Vladimir Marko | 5171813 | 2022-02-07 16:31:08 +0000 | [diff] [blame] | 6961 | bool is_super_abstract, |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 6962 | size_t num_virtual_methods, |
| 6963 | ObjPtr<mirror::IfTable> iftable) |
| 6964 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 6965 | |
| 6966 | bool FindCopiedMethodsForInterface(ObjPtr<mirror::Class> klass, |
| 6967 | size_t num_virtual_methods, |
| 6968 | ObjPtr<mirror::IfTable> iftable) |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 6969 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 6970 | |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6971 | bool LinkJavaLangObjectMethods(Thread* self, Handle<mirror::Class> klass) |
Vladimir Marko | b91402f | 2021-12-21 15:55:06 +0000 | [diff] [blame] | 6972 | REQUIRES_SHARED(Locks::mutator_lock_) COLD_ATTR; |
| 6973 | |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 6974 | void ReallocMethods(ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_); |
Vladimir Marko | 0441d20 | 2022-02-18 13:55:15 +0000 | [diff] [blame] | 6975 | bool FinalizeIfTable(Handle<mirror::Class> klass, |
| 6976 | MutableHandle<mirror::IfTable> iftable, |
| 6977 | Handle<mirror::PointerArray> vtable, |
Vladimir Marko | 5171813 | 2022-02-07 16:31:08 +0000 | [diff] [blame] | 6978 | bool is_klass_abstract, |
| 6979 | bool is_super_abstract, |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 6980 | bool* out_new_conflict, |
| 6981 | ArtMethod** out_imt) |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6982 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 6983 | |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 6984 | void ClobberOldMethods(LengthPrefixedArray<ArtMethod>* old_methods, |
| 6985 | LengthPrefixedArray<ArtMethod>* methods) { |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 6986 | if (kIsDebugBuild && old_methods != nullptr) { |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 6987 | CHECK(methods != nullptr); |
| 6988 | // Put some random garbage in old methods to help find stale pointers. |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 6989 | if (methods != old_methods) { |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 6990 | // Need to make sure the GC is not running since it could be scanning the methods we are |
| 6991 | // about to overwrite. |
Vladimir Marko | ddf4fd3 | 2021-11-22 16:31:57 +0000 | [diff] [blame] | 6992 | ScopedThreadStateChange tsc(self_, ThreadState::kSuspended); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 6993 | gc::ScopedGCCriticalSection gcs(self_, |
| 6994 | gc::kGcCauseClassLinker, |
| 6995 | gc::kCollectorTypeClassLinker); |
| 6996 | const size_t old_size = LengthPrefixedArray<ArtMethod>::ComputeSize(old_methods->size(), |
Vladimir Marko | b91402f | 2021-12-21 15:55:06 +0000 | [diff] [blame] | 6997 | kMethodSize, |
| 6998 | kMethodAlignment); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 6999 | memset(old_methods, 0xFEu, old_size); |
| 7000 | } |
| 7001 | } |
| 7002 | } |
| 7003 | |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 7004 | NO_INLINE |
| 7005 | void LogNewVirtuals(LengthPrefixedArray<ArtMethod>* methods) const |
| 7006 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 7007 | ObjPtr<mirror::Class> klass = klass_.Get(); |
| 7008 | size_t num_new_copied_methods = num_new_copied_methods_; |
| 7009 | size_t old_method_count = methods->size() - num_new_copied_methods; |
| 7010 | size_t super_vtable_length = klass->GetSuperClass()->GetVTableLength(); |
| 7011 | size_t num_miranda_methods = 0u; |
| 7012 | size_t num_overriding_default_methods = 0u; |
| 7013 | size_t num_default_methods = 0u; |
| 7014 | size_t num_overriding_default_conflict_methods = 0u; |
| 7015 | size_t num_default_conflict_methods = 0u; |
| 7016 | for (size_t i = 0; i != num_new_copied_methods; ++i) { |
| 7017 | ArtMethod& m = methods->At(old_method_count + i, kMethodSize, kMethodAlignment); |
| 7018 | if (m.IsDefault()) { |
| 7019 | if (m.GetMethodIndexDuringLinking() < super_vtable_length) { |
| 7020 | ++num_overriding_default_methods; |
| 7021 | } else { |
| 7022 | ++num_default_methods; |
| 7023 | } |
| 7024 | } else if (m.IsDefaultConflicting()) { |
| 7025 | if (m.GetMethodIndexDuringLinking() < super_vtable_length) { |
| 7026 | ++num_overriding_default_conflict_methods; |
| 7027 | } else { |
| 7028 | ++num_default_conflict_methods; |
| 7029 | } |
| 7030 | } else { |
| 7031 | DCHECK(m.IsMiranda()); |
| 7032 | ++num_miranda_methods; |
| 7033 | } |
Vladimir Marko | 5cfb7bb | 2022-01-28 11:12:17 +0000 | [diff] [blame] | 7034 | } |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 7035 | VLOG(class_linker) << klass->PrettyClass() << ": miranda_methods=" << num_miranda_methods |
| 7036 | << " default_methods=" << num_default_methods |
| 7037 | << " overriding_default_methods=" << num_overriding_default_methods |
| 7038 | << " default_conflict_methods=" << num_default_conflict_methods |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7039 | << " overriding_default_conflict_methods=" |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 7040 | << num_overriding_default_conflict_methods; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7041 | } |
| 7042 | |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7043 | class MethodIndexEmptyFn { |
| 7044 | public: |
| 7045 | void MakeEmpty(uint32_t& item) const { |
| 7046 | item = dex::kDexNoIndex; |
| 7047 | } |
| 7048 | bool IsEmpty(const uint32_t& item) const { |
| 7049 | return item == dex::kDexNoIndex; |
| 7050 | } |
| 7051 | }; |
| 7052 | |
Vladimir Marko | 7ddae99 | 2022-01-18 14:27:20 +0000 | [diff] [blame] | 7053 | class VTableIndexCheckerDebug { |
| 7054 | protected: |
| 7055 | explicit VTableIndexCheckerDebug(size_t vtable_length) |
| 7056 | : vtable_length_(vtable_length) {} |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7057 | |
Vladimir Marko | 7ddae99 | 2022-01-18 14:27:20 +0000 | [diff] [blame] | 7058 | void CheckIndex(uint32_t index) const { |
| 7059 | CHECK_LT(index, vtable_length_); |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7060 | } |
| 7061 | |
| 7062 | private: |
Vladimir Marko | 7ddae99 | 2022-01-18 14:27:20 +0000 | [diff] [blame] | 7063 | uint32_t vtable_length_; |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7064 | }; |
| 7065 | |
Vladimir Marko | 7ddae99 | 2022-01-18 14:27:20 +0000 | [diff] [blame] | 7066 | class VTableIndexCheckerRelease { |
| 7067 | protected: |
| 7068 | explicit VTableIndexCheckerRelease(size_t vtable_length ATTRIBUTE_UNUSED) {} |
| 7069 | void CheckIndex(uint32_t index ATTRIBUTE_UNUSED) const {} |
| 7070 | }; |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7071 | |
Vladimir Marko | 7ddae99 | 2022-01-18 14:27:20 +0000 | [diff] [blame] | 7072 | using VTableIndexChecker = |
| 7073 | std::conditional_t<kIsDebugBuild, VTableIndexCheckerDebug, VTableIndexCheckerRelease>; |
| 7074 | |
| 7075 | class VTableAccessor : private VTableIndexChecker { |
| 7076 | public: |
| 7077 | VTableAccessor(uint8_t* raw_vtable, size_t vtable_length) |
| 7078 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 7079 | : VTableIndexChecker(vtable_length), |
| 7080 | raw_vtable_(raw_vtable) {} |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7081 | |
| 7082 | ArtMethod* GetVTableEntry(uint32_t index) const REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | 7ddae99 | 2022-01-18 14:27:20 +0000 | [diff] [blame] | 7083 | this->CheckIndex(index); |
| 7084 | uint8_t* entry = raw_vtable_ + static_cast<size_t>(kPointerSize) * index; |
| 7085 | if (kPointerSize == PointerSize::k64) { |
| 7086 | return reinterpret_cast64<ArtMethod*>(*reinterpret_cast<uint64_t*>(entry)); |
| 7087 | } else { |
| 7088 | return reinterpret_cast32<ArtMethod*>(*reinterpret_cast<uint32_t*>(entry)); |
| 7089 | } |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7090 | } |
| 7091 | |
| 7092 | private: |
Vladimir Marko | 7ddae99 | 2022-01-18 14:27:20 +0000 | [diff] [blame] | 7093 | uint8_t* raw_vtable_; |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7094 | }; |
| 7095 | |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7096 | class VTableSignatureHash { |
| 7097 | public: |
Vladimir Marko | 7ddae99 | 2022-01-18 14:27:20 +0000 | [diff] [blame] | 7098 | explicit VTableSignatureHash(VTableAccessor accessor) |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7099 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 7100 | : accessor_(accessor) {} |
| 7101 | |
| 7102 | // NO_THREAD_SAFETY_ANALYSIS: This is called from unannotated `HashSet<>` functions. |
| 7103 | size_t operator()(ArtMethod* method) const NO_THREAD_SAFETY_ANALYSIS { |
| 7104 | return ComputeMethodHash(method); |
| 7105 | } |
| 7106 | |
| 7107 | // NO_THREAD_SAFETY_ANALYSIS: This is called from unannotated `HashSet<>` functions. |
| 7108 | size_t operator()(uint32_t index) const NO_THREAD_SAFETY_ANALYSIS { |
| 7109 | return ComputeMethodHash(accessor_.GetVTableEntry(index)); |
| 7110 | } |
| 7111 | |
| 7112 | private: |
Vladimir Marko | 7ddae99 | 2022-01-18 14:27:20 +0000 | [diff] [blame] | 7113 | VTableAccessor accessor_; |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7114 | }; |
| 7115 | |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7116 | class VTableSignatureEqual { |
| 7117 | public: |
Vladimir Marko | 7ddae99 | 2022-01-18 14:27:20 +0000 | [diff] [blame] | 7118 | explicit VTableSignatureEqual(VTableAccessor accessor) |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7119 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 7120 | : accessor_(accessor) {} |
| 7121 | |
| 7122 | // NO_THREAD_SAFETY_ANALYSIS: This is called from unannotated `HashSet<>` functions. |
| 7123 | 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] | 7124 | return MethodSignatureEquals(accessor_.GetVTableEntry(lhs_index), rhs); |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7125 | } |
| 7126 | |
| 7127 | // NO_THREAD_SAFETY_ANALYSIS: This is called from unannotated `HashSet<>` functions. |
| 7128 | bool operator()(uint32_t lhs_index, uint32_t rhs_index) const NO_THREAD_SAFETY_ANALYSIS { |
| 7129 | return (*this)(lhs_index, accessor_.GetVTableEntry(rhs_index)); |
| 7130 | } |
| 7131 | |
| 7132 | private: |
Vladimir Marko | 7ddae99 | 2022-01-18 14:27:20 +0000 | [diff] [blame] | 7133 | VTableAccessor accessor_; |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7134 | }; |
| 7135 | |
Vladimir Marko | 7ddae99 | 2022-01-18 14:27:20 +0000 | [diff] [blame] | 7136 | using VTableSignatureSet = |
| 7137 | ScopedArenaHashSet<uint32_t, MethodIndexEmptyFn, VTableSignatureHash, VTableSignatureEqual>; |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7138 | |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 7139 | class DeclaredVirtualSignatureHash { |
| 7140 | public: |
| 7141 | explicit DeclaredVirtualSignatureHash(ObjPtr<mirror::Class> klass) |
| 7142 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 7143 | : klass_(klass) {} |
| 7144 | |
| 7145 | // NO_THREAD_SAFETY_ANALYSIS: This is called from unannotated `HashSet<>` functions. |
| 7146 | size_t operator()(ArtMethod* method) const NO_THREAD_SAFETY_ANALYSIS { |
| 7147 | return ComputeMethodHash(method); |
| 7148 | } |
| 7149 | |
| 7150 | // NO_THREAD_SAFETY_ANALYSIS: This is called from unannotated `HashSet<>` functions. |
| 7151 | size_t operator()(uint32_t index) const NO_THREAD_SAFETY_ANALYSIS { |
| 7152 | DCHECK_LT(index, klass_->NumDeclaredVirtualMethods()); |
| 7153 | ArtMethod* method = klass_->GetVirtualMethodDuringLinking(index, kPointerSize); |
| 7154 | return ComputeMethodHash(method->GetInterfaceMethodIfProxy(kPointerSize)); |
| 7155 | } |
| 7156 | |
| 7157 | private: |
| 7158 | ObjPtr<mirror::Class> klass_; |
| 7159 | }; |
| 7160 | |
| 7161 | class DeclaredVirtualSignatureEqual { |
| 7162 | public: |
| 7163 | explicit DeclaredVirtualSignatureEqual(ObjPtr<mirror::Class> klass) |
| 7164 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 7165 | : klass_(klass) {} |
| 7166 | |
| 7167 | // NO_THREAD_SAFETY_ANALYSIS: This is called from unannotated `HashSet<>` functions. |
| 7168 | bool operator()(uint32_t lhs_index, ArtMethod* rhs) const NO_THREAD_SAFETY_ANALYSIS { |
| 7169 | DCHECK_LT(lhs_index, klass_->NumDeclaredVirtualMethods()); |
| 7170 | ArtMethod* lhs = klass_->GetVirtualMethodDuringLinking(lhs_index, kPointerSize); |
| 7171 | return MethodSignatureEquals(lhs->GetInterfaceMethodIfProxy(kPointerSize), rhs); |
| 7172 | } |
| 7173 | |
| 7174 | // NO_THREAD_SAFETY_ANALYSIS: This is called from unannotated `HashSet<>` functions. |
| 7175 | bool operator()(uint32_t lhs_index, uint32_t rhs_index) const NO_THREAD_SAFETY_ANALYSIS { |
| 7176 | DCHECK_LT(lhs_index, klass_->NumDeclaredVirtualMethods()); |
| 7177 | DCHECK_LT(rhs_index, klass_->NumDeclaredVirtualMethods()); |
| 7178 | return lhs_index == rhs_index; |
| 7179 | } |
| 7180 | |
| 7181 | private: |
| 7182 | ObjPtr<mirror::Class> klass_; |
| 7183 | }; |
| 7184 | |
| 7185 | using DeclaredVirtualSignatureSet = ScopedArenaHashSet<uint32_t, |
| 7186 | MethodIndexEmptyFn, |
| 7187 | DeclaredVirtualSignatureHash, |
| 7188 | DeclaredVirtualSignatureEqual>; |
| 7189 | |
| 7190 | // Helper class to keep records for determining the correct copied method to create. |
| 7191 | class CopiedMethodRecord { |
| 7192 | public: |
| 7193 | enum class State : uint32_t { |
| 7194 | // Note: The `*Single` values are used when we know that there is only one interface |
| 7195 | // method with the given signature that's not masked; that method is the main method. |
| 7196 | // We use this knowledge for faster masking check, otherwise we need to search for |
| 7197 | // a masking method through methods of all interfaces that could potentially mask it. |
| 7198 | kAbstractSingle, |
| 7199 | kDefaultSingle, |
| 7200 | kAbstract, |
| 7201 | kDefault, |
| 7202 | kDefaultConflict, |
| 7203 | kUseSuperMethod, |
| 7204 | }; |
| 7205 | |
| 7206 | CopiedMethodRecord() |
| 7207 | : main_method_(nullptr), |
| 7208 | method_index_(0u), |
| 7209 | state_(State::kAbstractSingle) {} |
| 7210 | |
| 7211 | CopiedMethodRecord(ArtMethod* main_method, size_t vtable_index) |
| 7212 | : main_method_(main_method), |
| 7213 | method_index_(vtable_index), |
| 7214 | state_(State::kAbstractSingle) {} |
| 7215 | |
| 7216 | // Set main method. The new main method must be more specific implementation. |
| 7217 | void SetMainMethod(ArtMethod* main_method) { |
| 7218 | DCHECK(main_method_ != nullptr); |
| 7219 | main_method_ = main_method; |
| 7220 | } |
| 7221 | |
| 7222 | // The main method is the first encountered default method if any, |
| 7223 | // otherwise the first encountered abstract method. |
| 7224 | ArtMethod* GetMainMethod() const { |
| 7225 | return main_method_; |
| 7226 | } |
| 7227 | |
| 7228 | void SetMethodIndex(size_t method_index) { |
| 7229 | DCHECK_NE(method_index, dex::kDexNoIndex); |
| 7230 | method_index_ = method_index; |
| 7231 | } |
| 7232 | |
| 7233 | size_t GetMethodIndex() const { |
| 7234 | DCHECK_NE(method_index_, dex::kDexNoIndex); |
| 7235 | return method_index_; |
| 7236 | } |
| 7237 | |
| 7238 | void SetState(State state) { |
| 7239 | state_ = state; |
| 7240 | } |
| 7241 | |
| 7242 | State GetState() const { |
| 7243 | return state_; |
| 7244 | } |
| 7245 | |
| 7246 | ALWAYS_INLINE |
| 7247 | void UpdateStateForInterface(ObjPtr<mirror::Class> iface, |
| 7248 | ArtMethod* interface_method, |
| 7249 | ObjPtr<mirror::IfTable> iftable, |
| 7250 | size_t ifcount, |
| 7251 | size_t index) |
| 7252 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 7253 | DCHECK_EQ(ifcount, iftable->Count()); |
| 7254 | DCHECK_LT(index, ifcount); |
| 7255 | DCHECK(iface == interface_method->GetDeclaringClass()); |
| 7256 | DCHECK(iface == iftable->GetInterface(index)); |
| 7257 | DCHECK(interface_method->IsDefault()); |
| 7258 | if (GetState() != State::kDefaultConflict) { |
| 7259 | DCHECK(GetState() == State::kDefault); |
| 7260 | // We do not record all overriding methods, so we need to walk over all |
| 7261 | // interfaces that could mask the `interface_method`. |
| 7262 | if (ContainsOverridingMethodOf(iftable, index + 1, ifcount, iface, interface_method)) { |
| 7263 | return; // Found an overriding method that masks `interface_method`. |
| 7264 | } |
| 7265 | // We have a new default method that's not masked by any other method. |
| 7266 | SetState(State::kDefaultConflict); |
| 7267 | } |
| 7268 | } |
| 7269 | |
| 7270 | ALWAYS_INLINE |
| 7271 | void UpdateState(ObjPtr<mirror::Class> iface, |
| 7272 | ArtMethod* interface_method, |
| 7273 | size_t vtable_index, |
| 7274 | ObjPtr<mirror::IfTable> iftable, |
| 7275 | size_t ifcount, |
| 7276 | size_t index) |
| 7277 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 7278 | DCHECK_EQ(ifcount, iftable->Count()); |
| 7279 | DCHECK_LT(index, ifcount); |
| 7280 | if (kIsDebugBuild) { |
| 7281 | if (interface_method->IsCopied()) { |
| 7282 | // Called from `FinalizeState()` for a default method from superclass. |
| 7283 | // The `index` points to the last interface inherited from the superclass |
| 7284 | // as we need to search only the new interfaces for masking methods. |
| 7285 | DCHECK(interface_method->IsDefault()); |
| 7286 | } else { |
| 7287 | DCHECK(iface == interface_method->GetDeclaringClass()); |
| 7288 | DCHECK(iface == iftable->GetInterface(index)); |
| 7289 | } |
| 7290 | } |
| 7291 | DCHECK_EQ(vtable_index, method_index_); |
| 7292 | auto slow_is_masked = [=]() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 7293 | return ContainsImplementingMethod(iftable, index + 1, ifcount, iface, vtable_index); |
| 7294 | }; |
| 7295 | UpdateStateImpl(iface, interface_method, slow_is_masked); |
| 7296 | } |
| 7297 | |
| 7298 | ALWAYS_INLINE |
| 7299 | void FinalizeState(ArtMethod* super_method, |
| 7300 | size_t vtable_index, |
| 7301 | ObjPtr<mirror::IfTable> iftable, |
| 7302 | size_t ifcount, |
| 7303 | ObjPtr<mirror::IfTable> super_iftable, |
| 7304 | size_t super_ifcount) |
| 7305 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 7306 | DCHECK(super_method->IsCopied()); |
| 7307 | DCHECK_EQ(vtable_index, method_index_); |
| 7308 | DCHECK_EQ(vtable_index, super_method->GetMethodIndex()); |
| 7309 | DCHECK_NE(super_ifcount, 0u); |
| 7310 | if (super_method->IsDefault()) { |
| 7311 | if (UNLIKELY(super_method->IsDefaultConflicting())) { |
| 7312 | // Some of the default methods that contributed to the conflict in the superclass |
| 7313 | // may be masked by new interfaces. Walk over all the interfaces and update state |
| 7314 | // as long as the current state is not `kDefaultConflict`. |
| 7315 | size_t i = super_ifcount; |
| 7316 | while (GetState() != State::kDefaultConflict && i != 0u) { |
| 7317 | --i; |
| 7318 | ObjPtr<mirror::Class> iface = iftable->GetInterface(i); |
| 7319 | DCHECK(iface == super_iftable->GetInterface(i)); |
| 7320 | auto [found, index] = |
| 7321 | MethodArrayContains(super_iftable->GetMethodArrayOrNull(i), super_method); |
| 7322 | if (found) { |
| 7323 | ArtMethod* interface_method = iface->GetVirtualMethod(index, kPointerSize); |
| 7324 | auto slow_is_masked = [=]() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 7325 | // Note: The `iftable` has method arrays in range [super_ifcount, ifcount) filled |
| 7326 | // with vtable indexes but the range [0, super_ifcount) is empty, so we need to |
| 7327 | // use the `super_iftable` filled with implementation methods for that range. |
| 7328 | return ContainsImplementingMethod( |
| 7329 | super_iftable, i + 1u, super_ifcount, iface, super_method) || |
| 7330 | ContainsImplementingMethod( |
| 7331 | iftable, super_ifcount, ifcount, iface, vtable_index); |
| 7332 | }; |
| 7333 | UpdateStateImpl(iface, interface_method, slow_is_masked); |
| 7334 | } |
| 7335 | } |
| 7336 | if (GetState() == State::kDefaultConflict) { |
| 7337 | SetState(State::kUseSuperMethod); |
| 7338 | } |
| 7339 | } else { |
| 7340 | // There was exactly one default method in superclass interfaces that was |
| 7341 | // not masked by subinterfaces. Use `UpdateState()` to process it and pass |
| 7342 | // `super_ifcount - 1` as index for checking if it's been masked by new interfaces. |
| 7343 | ObjPtr<mirror::Class> iface = super_method->GetDeclaringClass(); |
| 7344 | UpdateState( |
| 7345 | iface, super_method, vtable_index, iftable, ifcount, /*index=*/ super_ifcount - 1u); |
| 7346 | if (GetMainMethod() == super_method) { |
| 7347 | DCHECK(GetState() == State::kDefault) << enum_cast<uint32_t>(GetState()); |
| 7348 | SetState(State::kUseSuperMethod); |
| 7349 | } |
| 7350 | } |
| 7351 | } else { |
| 7352 | DCHECK(super_method->IsMiranda()); |
| 7353 | // Any default methods with this signature in superclass interfaces have been |
| 7354 | // masked by subinterfaces. Check if we can reuse the miranda method. |
| 7355 | if (GetState() == State::kAbstractSingle || GetState() == State::kAbstract) { |
| 7356 | SetState(State::kUseSuperMethod); |
| 7357 | } |
| 7358 | } |
| 7359 | } |
| 7360 | |
| 7361 | private: |
| 7362 | template <typename Predicate> |
| 7363 | ALWAYS_INLINE |
| 7364 | void UpdateStateImpl(ObjPtr<mirror::Class> iface, |
| 7365 | ArtMethod* interface_method, |
| 7366 | Predicate&& slow_is_masked) |
| 7367 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 7368 | bool have_default = false; |
| 7369 | switch (GetState()) { |
| 7370 | case State::kDefaultSingle: |
| 7371 | have_default = true; |
| 7372 | FALLTHROUGH_INTENDED; |
| 7373 | case State::kAbstractSingle: |
| 7374 | if (GetMainMethod()->GetDeclaringClass()->Implements(iface)) { |
| 7375 | return; // The main method masks the `interface_method`. |
| 7376 | } |
| 7377 | if (!interface_method->IsDefault()) { |
| 7378 | SetState(have_default ? State::kDefault : State::kAbstract); |
| 7379 | return; |
| 7380 | } |
| 7381 | break; |
| 7382 | case State::kDefault: |
| 7383 | have_default = true; |
| 7384 | FALLTHROUGH_INTENDED; |
| 7385 | case State::kAbstract: |
| 7386 | if (!interface_method->IsDefault()) { |
| 7387 | return; // Keep the same state. We do not need to check for masking. |
| 7388 | } |
| 7389 | // We do not record all overriding methods, so we need to walk over all |
| 7390 | // interfaces that could mask the `interface_method`. The provided |
| 7391 | // predicate `slow_is_masked()` does that. |
| 7392 | if (slow_is_masked()) { |
| 7393 | return; // Found an overriding method that masks `interface_method`. |
| 7394 | } |
| 7395 | break; |
| 7396 | case State::kDefaultConflict: |
| 7397 | return; // The state cannot change anymore. |
| 7398 | default: |
| 7399 | LOG(FATAL) << "Unexpected state: " << enum_cast<uint32_t>(GetState()); |
| 7400 | UNREACHABLE(); |
| 7401 | } |
| 7402 | // We have a new default method that's not masked by any other method. |
| 7403 | DCHECK(interface_method->IsDefault()); |
| 7404 | if (have_default) { |
| 7405 | SetState(State::kDefaultConflict); |
| 7406 | } else { |
| 7407 | SetMainMethod(interface_method); |
| 7408 | SetState(State::kDefault); |
| 7409 | } |
| 7410 | } |
| 7411 | |
| 7412 | // Determine if the given `iftable` contains in the given range a subinterface of `iface` |
| 7413 | // that declares a method with the same name and signature as 'interface_method'. |
| 7414 | // |
| 7415 | // Arguments |
| 7416 | // - iftable: The iftable we are searching for an overriding method. |
| 7417 | // - begin: The start of the range to search. |
| 7418 | // - end: The end of the range to search. |
| 7419 | // - iface: The interface we are checking to see if anything overrides. |
| 7420 | // - interface_method: |
| 7421 | // The interface method providing a name and signature we're searching for. |
| 7422 | // |
| 7423 | // Returns whether an overriding method was found in any subinterface of `iface`. |
| 7424 | static bool ContainsOverridingMethodOf(ObjPtr<mirror::IfTable> iftable, |
| 7425 | size_t begin, |
| 7426 | size_t end, |
| 7427 | ObjPtr<mirror::Class> iface, |
| 7428 | ArtMethod* interface_method) |
| 7429 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 7430 | for (size_t i = begin; i != end; ++i) { |
| 7431 | ObjPtr<mirror::Class> current_iface = iftable->GetInterface(i); |
| 7432 | for (ArtMethod& current_method : current_iface->GetDeclaredVirtualMethods(kPointerSize)) { |
| 7433 | if (MethodSignatureEquals(¤t_method, interface_method)) { |
| 7434 | // Check if the i'th interface is a subtype of this one. |
| 7435 | if (current_iface->Implements(iface)) { |
| 7436 | return true; |
| 7437 | } |
| 7438 | break; |
| 7439 | } |
| 7440 | } |
| 7441 | } |
| 7442 | return false; |
| 7443 | } |
| 7444 | |
| 7445 | // Determine if the given `iftable` contains in the given range a subinterface of `iface` |
| 7446 | // that declares a method implemented by 'target'. This is an optimized version of |
| 7447 | // `ContainsOverridingMethodOf()` that searches implementation method arrays instead |
| 7448 | // of comparing signatures for declared interface methods. |
| 7449 | // |
| 7450 | // Arguments |
| 7451 | // - iftable: The iftable we are searching for an overriding method. |
| 7452 | // - begin: The start of the range to search. |
| 7453 | // - end: The end of the range to search. |
| 7454 | // - iface: The interface we are checking to see if anything overrides. |
| 7455 | // - target: The implementation method we're searching for. |
| 7456 | // Note that the new `iftable` is filled with vtable indexes for new interfaces, |
| 7457 | // so this needs to be the vtable index if we're searching that range. |
| 7458 | // |
| 7459 | // Returns whether the `target` was found in a method array for any subinterface of `iface`. |
| 7460 | template <typename TargetType> |
| 7461 | static bool ContainsImplementingMethod(ObjPtr<mirror::IfTable> iftable, |
| 7462 | size_t begin, |
| 7463 | size_t end, |
| 7464 | ObjPtr<mirror::Class> iface, |
| 7465 | TargetType target) |
| 7466 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 7467 | for (size_t i = begin; i != end; ++i) { |
| 7468 | if (MethodArrayContains(iftable->GetMethodArrayOrNull(i), target).first && |
| 7469 | iftable->GetInterface(i)->Implements(iface)) { |
| 7470 | return true; |
| 7471 | } |
| 7472 | } |
| 7473 | return false; |
| 7474 | } |
| 7475 | |
| 7476 | template <typename TargetType> |
| 7477 | static std::pair<bool, size_t> MethodArrayContains(ObjPtr<mirror::PointerArray> method_array, |
| 7478 | TargetType target) |
| 7479 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 7480 | size_t num_methods = (method_array != nullptr) ? method_array->GetLength() : 0u; |
| 7481 | for (size_t j = 0; j != num_methods; ++j) { |
| 7482 | if (method_array->GetElementPtrSize<TargetType, kPointerSize>(j) == target) { |
| 7483 | return {true, j}; |
| 7484 | } |
| 7485 | } |
| 7486 | return {false, 0}; |
| 7487 | } |
| 7488 | |
| 7489 | ArtMethod* main_method_; |
| 7490 | uint32_t method_index_; |
| 7491 | State state_; |
| 7492 | }; |
| 7493 | |
| 7494 | class CopiedMethodRecordEmptyFn { |
| 7495 | public: |
| 7496 | void MakeEmpty(CopiedMethodRecord& item) const { |
| 7497 | item = CopiedMethodRecord(); |
| 7498 | } |
| 7499 | bool IsEmpty(const CopiedMethodRecord& item) const { |
| 7500 | return item.GetMainMethod() == nullptr; |
| 7501 | } |
| 7502 | }; |
| 7503 | |
| 7504 | class CopiedMethodRecordHash { |
| 7505 | public: |
| 7506 | // NO_THREAD_SAFETY_ANALYSIS: This is called from unannotated `HashSet<>` functions. |
| 7507 | size_t operator()(ArtMethod* method) const NO_THREAD_SAFETY_ANALYSIS { |
| 7508 | DCHECK(method != nullptr); |
| 7509 | return ComputeMethodHash(method); |
| 7510 | } |
| 7511 | |
| 7512 | // NO_THREAD_SAFETY_ANALYSIS: This is called from unannotated `HashSet<>` functions. |
| 7513 | size_t operator()(const CopiedMethodRecord& record) const NO_THREAD_SAFETY_ANALYSIS { |
| 7514 | return (*this)(record.GetMainMethod()); |
| 7515 | } |
| 7516 | }; |
| 7517 | |
| 7518 | class CopiedMethodRecordEqual { |
| 7519 | public: |
| 7520 | // NO_THREAD_SAFETY_ANALYSIS: This is called from unannotated `HashSet<>` functions. |
| 7521 | bool operator()(const CopiedMethodRecord& lhs_record, |
| 7522 | ArtMethod* rhs) const NO_THREAD_SAFETY_ANALYSIS { |
| 7523 | ArtMethod* lhs = lhs_record.GetMainMethod(); |
| 7524 | DCHECK(lhs != nullptr); |
| 7525 | DCHECK(rhs != nullptr); |
| 7526 | return MethodSignatureEquals(lhs, rhs); |
| 7527 | } |
| 7528 | |
| 7529 | // NO_THREAD_SAFETY_ANALYSIS: This is called from unannotated `HashSet<>` functions. |
| 7530 | bool operator()(const CopiedMethodRecord& lhs_record, |
| 7531 | const CopiedMethodRecord& rhs_record) const NO_THREAD_SAFETY_ANALYSIS { |
| 7532 | return (*this)(lhs_record, rhs_record.GetMainMethod()); |
| 7533 | } |
| 7534 | }; |
| 7535 | |
| 7536 | using CopiedMethodRecordSet = ScopedArenaHashSet<CopiedMethodRecord, |
| 7537 | CopiedMethodRecordEmptyFn, |
| 7538 | CopiedMethodRecordHash, |
| 7539 | CopiedMethodRecordEqual>; |
| 7540 | |
Vladimir Marko | b91402f | 2021-12-21 15:55:06 +0000 | [diff] [blame] | 7541 | static constexpr size_t kMethodAlignment = ArtMethod::Alignment(kPointerSize); |
| 7542 | static constexpr size_t kMethodSize = ArtMethod::Size(kPointerSize); |
| 7543 | |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7544 | ClassLinker* class_linker_; |
| 7545 | Handle<mirror::Class> klass_; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7546 | Thread* const self_; |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 7547 | Runtime* const runtime_; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7548 | |
| 7549 | // These are allocated on the heap to begin, we then transfer to linear alloc when we re-create |
| 7550 | // the virtual methods array. |
| 7551 | // Need to use low 4GB arenas for compiler or else the pointers wont fit in 32 bit method array |
| 7552 | // during cross compilation. |
| 7553 | // Use the linear alloc pool since this one is in the low 4gb for the compiler. |
| 7554 | ArenaStack stack_; |
| 7555 | ScopedArenaAllocator allocator_; |
| 7556 | |
Vladimir Marko | d5d11d9 | 2021-02-02 16:24:25 +0000 | [diff] [blame] | 7557 | // If there are multiple methods with the same signature in the superclass vtable |
| 7558 | // (which can happen with a new virtual method having the same signature as an |
| 7559 | // inaccessible package-private method from another package in the superclass), |
| 7560 | // we keep singly-linked lists in this single array that maps vtable index to the |
| 7561 | // next vtable index in the list, `dex::kDexNoIndex` denotes the end of a list. |
| 7562 | ArrayRef<uint32_t> same_signature_vtable_lists_; |
| 7563 | |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 7564 | // Avoid large allocation for a few copied method records. |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 7565 | // Keep the initial buffer on the stack to avoid arena allocations |
| 7566 | // if there are no special cases (the first arena allocation is costly). |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 7567 | static constexpr size_t kCopiedMethodRecordInitialBufferSize = 16u; |
| 7568 | CopiedMethodRecord copied_method_records_initial_buffer_[kCopiedMethodRecordInitialBufferSize]; |
| 7569 | CopiedMethodRecordSet copied_method_records_; |
| 7570 | size_t num_new_copied_methods_; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7571 | }; |
| 7572 | |
Vladimir Marko | b91402f | 2021-12-21 15:55:06 +0000 | [diff] [blame] | 7573 | template <PointerSize kPointerSize> |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 7574 | NO_INLINE |
| 7575 | void ClassLinker::LinkMethodsHelper<kPointerSize>::ReallocMethods(ObjPtr<mirror::Class> klass) { |
| 7576 | // There should be no thread suspension in this function, |
| 7577 | // native allocations do not cause thread suspension. |
| 7578 | ScopedAssertNoThreadSuspension sants(__FUNCTION__); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7579 | |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 7580 | size_t num_new_copied_methods = num_new_copied_methods_; |
| 7581 | DCHECK_NE(num_new_copied_methods, 0u); |
| 7582 | const size_t old_method_count = klass->NumMethods(); |
| 7583 | 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] | 7584 | |
| 7585 | // Attempt to realloc to save RAM if possible. |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 7586 | LengthPrefixedArray<ArtMethod>* old_methods = klass->GetMethodsPtr(); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7587 | // The Realloced virtual methods aren't visible from the class roots, so there is no issue |
| 7588 | // where GCs could attempt to mark stale pointers due to memcpy. And since we overwrite the |
| 7589 | // realloced memory with out->CopyFrom, we are guaranteed to have objects in the to space since |
| 7590 | // CopyFrom has internal read barriers. |
| 7591 | // |
| 7592 | // TODO We should maybe move some of this into mirror::Class or at least into another method. |
| 7593 | const size_t old_size = LengthPrefixedArray<ArtMethod>::ComputeSize(old_method_count, |
Vladimir Marko | b91402f | 2021-12-21 15:55:06 +0000 | [diff] [blame] | 7594 | kMethodSize, |
| 7595 | kMethodAlignment); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7596 | const size_t new_size = LengthPrefixedArray<ArtMethod>::ComputeSize(new_method_count, |
Vladimir Marko | b91402f | 2021-12-21 15:55:06 +0000 | [diff] [blame] | 7597 | kMethodSize, |
| 7598 | kMethodAlignment); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7599 | const size_t old_methods_ptr_size = (old_methods != nullptr) ? old_size : 0; |
| 7600 | auto* methods = reinterpret_cast<LengthPrefixedArray<ArtMethod>*>( |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 7601 | class_linker_->GetAllocatorForClassLoader(klass->GetClassLoader())->Realloc( |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7602 | self_, old_methods, old_methods_ptr_size, new_size)); |
| 7603 | CHECK(methods != nullptr); // Native allocation failure aborts. |
| 7604 | |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7605 | if (methods != old_methods) { |
Vladimir Marko | b91402f | 2021-12-21 15:55:06 +0000 | [diff] [blame] | 7606 | StrideIterator<ArtMethod> out = methods->begin(kMethodSize, kMethodAlignment); |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 7607 | // Copy over the old methods. The `ArtMethod::CopyFrom()` is only necessary to not miss |
| 7608 | // read barriers since `LinearAlloc::Realloc()` won't do read barriers when it copies. |
| 7609 | for (auto& m : klass->GetMethods(kPointerSize)) { |
Vladimir Marko | b91402f | 2021-12-21 15:55:06 +0000 | [diff] [blame] | 7610 | out->CopyFrom(&m, kPointerSize); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7611 | ++out; |
| 7612 | } |
| 7613 | } |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 7614 | |
| 7615 | // Collect and sort copied method records by the vtable index. This places overriding |
| 7616 | // copied methods first, sorted by the vtable index already assigned in the superclass, |
| 7617 | // followed by copied methods with new signatures in the order in which we encountered |
| 7618 | // them when going over virtual methods of new interfaces. |
| 7619 | // This order is deterministic but implementation-defined. |
| 7620 | // |
| 7621 | // Avoid arena allocation for a few records (the first arena allocation is costly). |
| 7622 | constexpr size_t kSortedRecordsBufferSize = 16; |
| 7623 | CopiedMethodRecord* sorted_records_buffer[kSortedRecordsBufferSize]; |
| 7624 | CopiedMethodRecord** sorted_records = (num_new_copied_methods <= kSortedRecordsBufferSize) |
| 7625 | ? sorted_records_buffer |
| 7626 | : allocator_.AllocArray<CopiedMethodRecord*>(num_new_copied_methods); |
| 7627 | size_t filled_sorted_records = 0u; |
| 7628 | for (CopiedMethodRecord& record : copied_method_records_) { |
| 7629 | if (record.GetState() != CopiedMethodRecord::State::kUseSuperMethod) { |
| 7630 | DCHECK_LT(filled_sorted_records, num_new_copied_methods); |
| 7631 | sorted_records[filled_sorted_records] = &record; |
| 7632 | ++filled_sorted_records; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7633 | } |
| 7634 | } |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 7635 | DCHECK_EQ(filled_sorted_records, num_new_copied_methods); |
| 7636 | std::sort(sorted_records, |
| 7637 | sorted_records + num_new_copied_methods, |
| 7638 | [](const CopiedMethodRecord* lhs, const CopiedMethodRecord* rhs) { |
| 7639 | return lhs->GetMethodIndex() < rhs->GetMethodIndex(); |
| 7640 | }); |
| 7641 | |
| 7642 | if (klass->IsInterface()) { |
| 7643 | // Some records may have been pruned. Update method indexes in collected records. |
| 7644 | size_t interface_method_index = klass->NumDeclaredVirtualMethods(); |
| 7645 | for (size_t i = 0; i != num_new_copied_methods; ++i) { |
| 7646 | CopiedMethodRecord* record = sorted_records[i]; |
| 7647 | DCHECK_LE(interface_method_index, record->GetMethodIndex()); |
| 7648 | record->SetMethodIndex(interface_method_index); |
| 7649 | ++interface_method_index; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7650 | } |
| 7651 | } |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 7652 | |
| 7653 | // Add copied methods. |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7654 | methods->SetSize(new_method_count); |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 7655 | for (size_t i = 0; i != num_new_copied_methods; ++i) { |
| 7656 | const CopiedMethodRecord* record = sorted_records[i]; |
| 7657 | ArtMethod* interface_method = record->GetMainMethod(); |
| 7658 | DCHECK(!interface_method->IsCopied()); |
| 7659 | ArtMethod& new_method = methods->At(old_method_count + i, kMethodSize, kMethodAlignment); |
| 7660 | new_method.CopyFrom(interface_method, kPointerSize); |
| 7661 | new_method.SetMethodIndex(dchecked_integral_cast<uint16_t>(record->GetMethodIndex())); |
| 7662 | switch (record->GetState()) { |
| 7663 | case CopiedMethodRecord::State::kAbstractSingle: |
| 7664 | case CopiedMethodRecord::State::kAbstract: { |
| 7665 | DCHECK(!klass->IsInterface()); // We do not create miranda methods for interfaces. |
| 7666 | uint32_t access_flags = new_method.GetAccessFlags(); |
| 7667 | DCHECK_EQ(access_flags & (kAccAbstract | kAccIntrinsic | kAccDefault), kAccAbstract) |
| 7668 | << "Miranda method should be abstract but not intrinsic or default!"; |
| 7669 | new_method.SetAccessFlags(access_flags | kAccCopied); |
| 7670 | break; |
| 7671 | } |
| 7672 | case CopiedMethodRecord::State::kDefaultSingle: |
| 7673 | case CopiedMethodRecord::State::kDefault: { |
| 7674 | DCHECK(!klass->IsInterface()); // We do not copy default methods for interfaces. |
| 7675 | // Clear the kAccSkipAccessChecks flag if it is present. Since this class hasn't been |
| 7676 | // verified yet it shouldn't have methods that are skipping access checks. |
| 7677 | // TODO This is rather arbitrary. We should maybe support classes where only some of its |
| 7678 | // methods are skip_access_checks. |
| 7679 | DCHECK_EQ(new_method.GetAccessFlags() & kAccNative, 0u); |
| 7680 | constexpr uint32_t kSetFlags = kAccDefault | kAccCopied; |
| 7681 | constexpr uint32_t kMaskFlags = ~kAccSkipAccessChecks; |
| 7682 | new_method.SetAccessFlags((new_method.GetAccessFlags() | kSetFlags) & kMaskFlags); |
| 7683 | break; |
| 7684 | } |
| 7685 | case CopiedMethodRecord::State::kDefaultConflict: { |
| 7686 | // This is a type of default method (there are default method impls, just a conflict) |
| 7687 | // so mark this as a default. We use the `kAccAbstract` flag to distinguish it from |
| 7688 | // invokable copied default method without using a separate access flag but the default |
| 7689 | // conflicting method is technically not abstract and ArtMethod::IsAbstract() shall |
| 7690 | // return false. Also clear the kAccSkipAccessChecks bit since this class hasn't been |
| 7691 | // verified yet it shouldn't have methods that are skipping access checks. Also clear |
| 7692 | // potential kAccSingleImplementation to avoid CHA trying to inline the default method. |
| 7693 | uint32_t access_flags = new_method.GetAccessFlags(); |
| 7694 | DCHECK_EQ(access_flags & (kAccNative | kAccIntrinsic), 0u); |
| 7695 | constexpr uint32_t kSetFlags = kAccDefault | kAccAbstract | kAccCopied; |
| 7696 | constexpr uint32_t kMaskFlags = ~(kAccSkipAccessChecks | kAccSingleImplementation); |
| 7697 | new_method.SetAccessFlags((access_flags | kSetFlags) & kMaskFlags); |
| 7698 | DCHECK(new_method.IsDefaultConflicting()); |
| 7699 | DCHECK(!new_method.IsAbstract()); |
| 7700 | // The actual method might or might not be marked abstract since we just copied it from |
| 7701 | // a (possibly default) interface method. We need to set its entry point to be the bridge |
| 7702 | // so that the compiler will not invoke the implementation of whatever method we copied |
| 7703 | // from. |
| 7704 | EnsureThrowsInvocationError(class_linker_, &new_method); |
| 7705 | break; |
| 7706 | } |
| 7707 | default: |
| 7708 | LOG(FATAL) << "Unexpected state: " << enum_cast<uint32_t>(record->GetState()); |
| 7709 | UNREACHABLE(); |
| 7710 | } |
| 7711 | } |
| 7712 | |
| 7713 | if (VLOG_IS_ON(class_linker)) { |
| 7714 | LogNewVirtuals(methods); |
| 7715 | } |
| 7716 | |
| 7717 | class_linker_->UpdateClassMethods(klass, methods); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7718 | } |
| 7719 | |
Vladimir Marko | b91402f | 2021-12-21 15:55:06 +0000 | [diff] [blame] | 7720 | template <PointerSize kPointerSize> |
Vladimir Marko | 0441d20 | 2022-02-18 13:55:15 +0000 | [diff] [blame] | 7721 | bool ClassLinker::LinkMethodsHelper<kPointerSize>::FinalizeIfTable( |
| 7722 | Handle<mirror::Class> klass, |
| 7723 | MutableHandle<mirror::IfTable> iftable, |
| 7724 | Handle<mirror::PointerArray> vtable, |
Vladimir Marko | 5171813 | 2022-02-07 16:31:08 +0000 | [diff] [blame] | 7725 | bool is_klass_abstract, |
| 7726 | bool is_super_abstract, |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 7727 | bool* out_new_conflict, |
| 7728 | ArtMethod** out_imt) { |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 7729 | size_t ifcount = iftable->Count(); |
Vladimir Marko | 0441d20 | 2022-02-18 13:55:15 +0000 | [diff] [blame] | 7730 | // We do not need a read barrier here as the length is constant, both from-space and |
| 7731 | // to-space `IfTable`s shall yield the same result. See also `Class::GetIfTableCount()`. |
| 7732 | size_t super_ifcount = |
| 7733 | klass->GetSuperClass<kDefaultVerifyFlags, kWithoutReadBarrier>()->GetIfTableCount(); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7734 | |
Vladimir Marko | 0441d20 | 2022-02-18 13:55:15 +0000 | [diff] [blame] | 7735 | ClassLinker* class_linker = nullptr; |
Vladimir Marko | 5171813 | 2022-02-07 16:31:08 +0000 | [diff] [blame] | 7736 | ArtMethod* unimplemented_method = nullptr; |
| 7737 | ArtMethod* imt_conflict_method = nullptr; |
| 7738 | uintptr_t imt_methods_begin = 0u; |
| 7739 | size_t imt_methods_size = 0u; |
| 7740 | DCHECK_EQ(klass->ShouldHaveImt(), !is_klass_abstract); |
| 7741 | DCHECK_EQ(klass->GetSuperClass()->ShouldHaveImt(), !is_super_abstract); |
| 7742 | if (!is_klass_abstract) { |
Vladimir Marko | 0441d20 | 2022-02-18 13:55:15 +0000 | [diff] [blame] | 7743 | class_linker = class_linker_; |
Vladimir Marko | 5171813 | 2022-02-07 16:31:08 +0000 | [diff] [blame] | 7744 | unimplemented_method = runtime_->GetImtUnimplementedMethod(); |
| 7745 | imt_conflict_method = runtime_->GetImtConflictMethod(); |
| 7746 | if (is_super_abstract) { |
| 7747 | // There was no IMT in superclass to copy to `out_imt[]`, so we need |
| 7748 | // to fill it with all implementation methods from superclass. |
| 7749 | DCHECK_EQ(imt_methods_begin, 0u); |
| 7750 | imt_methods_size = std::numeric_limits<size_t>::max(); // No method at the last byte. |
| 7751 | } else { |
| 7752 | // If the superclass has IMT, we have already copied it to `out_imt[]` and |
| 7753 | // we do not need to call `SetIMTRef()` for interfaces from superclass when |
| 7754 | // the implementation method is already in the superclass, only for new methods. |
| 7755 | // For simplicity, use the entire method array including direct methods. |
| 7756 | LengthPrefixedArray<ArtMethod>* const new_methods = klass->GetMethodsPtr(); |
| 7757 | if (new_methods != nullptr) { |
| 7758 | DCHECK_NE(new_methods->size(), 0u); |
| 7759 | imt_methods_begin = reinterpret_cast<uintptr_t>(&new_methods->At(0)); |
| 7760 | imt_methods_size = new_methods->size() * kMethodSize; |
| 7761 | } |
| 7762 | } |
| 7763 | } |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7764 | |
Vladimir Marko | 0441d20 | 2022-02-18 13:55:15 +0000 | [diff] [blame] | 7765 | auto update_imt = [=](ObjPtr<mirror::Class> iface, size_t j, ArtMethod* implementation) |
| 7766 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 7767 | // Place method in imt if entry is empty, place conflict otherwise. |
| 7768 | ArtMethod** imt_ptr = &out_imt[iface->GetVirtualMethod(j, kPointerSize)->GetImtIndex()]; |
| 7769 | class_linker->SetIMTRef(unimplemented_method, |
| 7770 | imt_conflict_method, |
| 7771 | implementation, |
| 7772 | /*out*/out_new_conflict, |
| 7773 | /*out*/imt_ptr); |
| 7774 | }; |
| 7775 | |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 7776 | // For interfaces inherited from superclass, the new method arrays are empty, |
| 7777 | // so use vtable indexes from implementation methods from the superclass method array. |
| 7778 | for (size_t i = 0; i != super_ifcount; ++i) { |
| 7779 | ObjPtr<mirror::PointerArray> method_array = iftable->GetMethodArrayOrNull(i); |
Vladimir Marko | 0441d20 | 2022-02-18 13:55:15 +0000 | [diff] [blame] | 7780 | DCHECK(method_array == klass->GetSuperClass()->GetIfTable()->GetMethodArrayOrNull(i)); |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 7781 | if (method_array == nullptr) { |
| 7782 | continue; |
| 7783 | } |
| 7784 | size_t num_methods = method_array->GetLength(); |
| 7785 | ObjPtr<mirror::Class> iface = iftable->GetInterface(i); |
Vladimir Marko | 0441d20 | 2022-02-18 13:55:15 +0000 | [diff] [blame] | 7786 | size_t j = 0; |
| 7787 | // First loop has method array shared with the super class. |
| 7788 | for (; j != num_methods; ++j) { |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 7789 | ArtMethod* super_implementation = |
Vladimir Marko | 0441d20 | 2022-02-18 13:55:15 +0000 | [diff] [blame] | 7790 | method_array->GetElementPtrSize<ArtMethod*, kPointerSize>(j); |
| 7791 | size_t vtable_index = super_implementation->GetMethodIndex(); |
| 7792 | ArtMethod* implementation = |
| 7793 | vtable->GetElementPtrSize<ArtMethod*, kPointerSize>(vtable_index); |
| 7794 | // Check if we need to update IMT with this method, see above. |
| 7795 | if (reinterpret_cast<uintptr_t>(implementation) - imt_methods_begin < imt_methods_size) { |
| 7796 | update_imt(iface, j, implementation); |
| 7797 | } |
| 7798 | if (implementation != super_implementation) { |
| 7799 | // Copy-on-write and move to the next loop. |
| 7800 | Thread* self = self_; |
| 7801 | StackHandleScope<2u> hs(self); |
| 7802 | Handle<mirror::PointerArray> old_method_array = hs.NewHandle(method_array); |
| 7803 | HandleWrapperObjPtr<mirror::Class> h_iface = hs.NewHandleWrapper(&iface); |
| 7804 | if (ifcount == super_ifcount && iftable.Get() == klass->GetSuperClass()->GetIfTable()) { |
| 7805 | ObjPtr<mirror::IfTable> new_iftable = ObjPtr<mirror::IfTable>::DownCast( |
| 7806 | mirror::ObjectArray<mirror::Object>::CopyOf( |
| 7807 | iftable, self, ifcount * mirror::IfTable::kMax)); |
| 7808 | if (new_iftable == nullptr) { |
| 7809 | return false; |
| 7810 | } |
| 7811 | iftable.Assign(new_iftable); |
| 7812 | } |
| 7813 | method_array = ObjPtr<mirror::PointerArray>::DownCast( |
| 7814 | mirror::Array::CopyOf(old_method_array, self, num_methods)); |
| 7815 | if (method_array == nullptr) { |
| 7816 | return false; |
| 7817 | } |
| 7818 | iftable->SetMethodArray(i, method_array); |
| 7819 | method_array->SetElementPtrSize(j, implementation, kPointerSize); |
| 7820 | ++j; |
| 7821 | break; |
| 7822 | } |
| 7823 | } |
| 7824 | // Second loop (if non-empty) has method array different from the superclass. |
| 7825 | for (; j != num_methods; ++j) { |
| 7826 | ArtMethod* super_implementation = |
| 7827 | method_array->GetElementPtrSize<ArtMethod*, kPointerSize>(j); |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 7828 | size_t vtable_index = super_implementation->GetMethodIndex(); |
| 7829 | ArtMethod* implementation = |
| 7830 | vtable->GetElementPtrSize<ArtMethod*, kPointerSize>(vtable_index); |
| 7831 | method_array->SetElementPtrSize(j, implementation, kPointerSize); |
Vladimir Marko | 5171813 | 2022-02-07 16:31:08 +0000 | [diff] [blame] | 7832 | // Check if we need to update IMT with this method, see above. |
| 7833 | if (reinterpret_cast<uintptr_t>(implementation) - imt_methods_begin < imt_methods_size) { |
Vladimir Marko | 0441d20 | 2022-02-18 13:55:15 +0000 | [diff] [blame] | 7834 | update_imt(iface, j, implementation); |
Vladimir Marko | 5171813 | 2022-02-07 16:31:08 +0000 | [diff] [blame] | 7835 | } |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7836 | } |
| 7837 | } |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7838 | |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 7839 | // New interface method arrays contain vtable indexes. Translate them to methods. |
Vladimir Marko | 5171813 | 2022-02-07 16:31:08 +0000 | [diff] [blame] | 7840 | DCHECK_EQ(klass->ShouldHaveImt(), !is_klass_abstract); |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 7841 | for (size_t i = super_ifcount; i != ifcount; ++i) { |
| 7842 | ObjPtr<mirror::PointerArray> method_array = iftable->GetMethodArrayOrNull(i); |
| 7843 | if (method_array == nullptr) { |
| 7844 | continue; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7845 | } |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 7846 | size_t num_methods = method_array->GetLength(); |
| 7847 | ObjPtr<mirror::Class> iface = iftable->GetInterface(i); |
| 7848 | for (size_t j = 0; j != num_methods; ++j) { |
| 7849 | size_t vtable_index = method_array->GetElementPtrSize<size_t, kPointerSize>(j); |
| 7850 | ArtMethod* implementation = |
| 7851 | vtable->GetElementPtrSize<ArtMethod*, kPointerSize>(vtable_index); |
| 7852 | method_array->SetElementPtrSize(j, implementation, kPointerSize); |
Vladimir Marko | 5171813 | 2022-02-07 16:31:08 +0000 | [diff] [blame] | 7853 | if (!is_klass_abstract) { |
Vladimir Marko | 0441d20 | 2022-02-18 13:55:15 +0000 | [diff] [blame] | 7854 | update_imt(iface, j, implementation); |
Vladimir Marko | 5171813 | 2022-02-07 16:31:08 +0000 | [diff] [blame] | 7855 | } |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7856 | } |
| 7857 | } |
Vladimir Marko | 0441d20 | 2022-02-18 13:55:15 +0000 | [diff] [blame] | 7858 | |
| 7859 | return true; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7860 | } |
| 7861 | |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 7862 | NO_INLINE |
| 7863 | static void ThrowIllegalAccessErrorForImplementingMethod(ObjPtr<mirror::Class> klass, |
| 7864 | ArtMethod* vtable_method, |
| 7865 | ArtMethod* interface_method) |
| 7866 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 7867 | DCHECK(!vtable_method->IsAbstract()); |
| 7868 | DCHECK(!vtable_method->IsPublic()); |
| 7869 | ThrowIllegalAccessError( |
| 7870 | klass, |
| 7871 | "Method '%s' implementing interface method '%s' is not public", |
| 7872 | vtable_method->PrettyMethod().c_str(), |
| 7873 | interface_method->PrettyMethod().c_str()); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7874 | } |
| 7875 | |
Vladimir Marko | b91402f | 2021-12-21 15:55:06 +0000 | [diff] [blame] | 7876 | template <PointerSize kPointerSize> |
Vladimir Marko | 0441d20 | 2022-02-18 13:55:15 +0000 | [diff] [blame] | 7877 | ObjPtr<mirror::PointerArray> ClassLinker::LinkMethodsHelper<kPointerSize>::AllocPointerArray( |
| 7878 | Thread* self, size_t length) { |
| 7879 | using PointerArrayType = std::conditional_t< |
| 7880 | kPointerSize == PointerSize::k64, mirror::LongArray, mirror::IntArray>; |
| 7881 | ObjPtr<mirror::Array> array = PointerArrayType::Alloc(self, length); |
| 7882 | return ObjPtr<mirror::PointerArray>::DownCast(array); |
| 7883 | } |
| 7884 | |
| 7885 | template <PointerSize kPointerSize> |
| 7886 | bool ClassLinker::LinkMethodsHelper<kPointerSize>::AllocateIfTableMethodArrays( |
| 7887 | Thread* self, |
| 7888 | Handle<mirror::Class> klass, |
| 7889 | Handle<mirror::IfTable> iftable) { |
| 7890 | DCHECK(!klass->IsInterface()); |
| 7891 | DCHECK(klass_->HasSuperClass()); |
| 7892 | const size_t ifcount = iftable->Count(); |
| 7893 | // We do not need a read barrier here as the length is constant, both from-space and |
| 7894 | // to-space `IfTable`s shall yield the same result. See also `Class::GetIfTableCount()`. |
| 7895 | size_t super_ifcount = |
| 7896 | klass->GetSuperClass<kDefaultVerifyFlags, kWithoutReadBarrier>()->GetIfTableCount(); |
| 7897 | if (ifcount == super_ifcount) { |
| 7898 | DCHECK(iftable.Get() == klass_->GetSuperClass()->GetIfTable()); |
| 7899 | return true; |
| 7900 | } |
| 7901 | |
| 7902 | if (kIsDebugBuild) { |
| 7903 | // The method array references for superclass interfaces have been copied. |
| 7904 | // We shall allocate new arrays if needed (copy-on-write) in `FinalizeIfTable()`. |
| 7905 | ObjPtr<mirror::IfTable> super_iftable = klass_->GetSuperClass()->GetIfTable(); |
| 7906 | for (size_t i = 0; i != super_ifcount; ++i) { |
| 7907 | CHECK(iftable->GetInterface(i) == super_iftable->GetInterface(i)); |
| 7908 | CHECK(iftable->GetMethodArrayOrNull(i) == super_iftable->GetMethodArrayOrNull(i)); |
| 7909 | } |
| 7910 | } |
| 7911 | |
| 7912 | for (size_t i = super_ifcount; i < ifcount; ++i) { |
| 7913 | size_t num_methods = iftable->GetInterface(i)->NumDeclaredVirtualMethods(); |
| 7914 | if (num_methods > 0) { |
| 7915 | ObjPtr<mirror::PointerArray> method_array = AllocPointerArray(self, num_methods); |
| 7916 | if (UNLIKELY(method_array == nullptr)) { |
| 7917 | self->AssertPendingOOMException(); |
| 7918 | return false; |
| 7919 | } |
| 7920 | iftable->SetMethodArray(i, method_array); |
| 7921 | } |
| 7922 | } |
| 7923 | return true; |
| 7924 | } |
| 7925 | |
| 7926 | template <PointerSize kPointerSize> |
Vladimir Marko | bed84ef | 2022-01-21 13:57:14 +0000 | [diff] [blame] | 7927 | size_t ClassLinker::LinkMethodsHelper<kPointerSize>::AssignVTableIndexes( |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 7928 | ObjPtr<mirror::Class> klass, |
| 7929 | ObjPtr<mirror::Class> super_class, |
Vladimir Marko | 5171813 | 2022-02-07 16:31:08 +0000 | [diff] [blame] | 7930 | bool is_super_abstract, |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 7931 | size_t num_virtual_methods, |
| 7932 | ObjPtr<mirror::IfTable> iftable) { |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7933 | DCHECK(!klass->IsInterface()); |
| 7934 | DCHECK(klass->HasSuperClass()); |
| 7935 | DCHECK(klass->GetSuperClass() == super_class); |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7936 | |
| 7937 | // 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] | 7938 | // (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] | 7939 | std::optional<ScopedAssertNoThreadSuspension> sants(__FUNCTION__); |
| 7940 | |
| 7941 | // Prepare a hash table with virtual methods from the superclass. |
| 7942 | // For the unlikely cases that there are multiple methods with the same signature |
| 7943 | // but different vtable indexes, keep an array with indexes of the previous |
| 7944 | // methods with the same signature (walked as singly-linked lists). |
Vladimir Marko | 7ddae99 | 2022-01-18 14:27:20 +0000 | [diff] [blame] | 7945 | uint8_t* raw_super_vtable; |
| 7946 | size_t super_vtable_length; |
Vladimir Marko | 5171813 | 2022-02-07 16:31:08 +0000 | [diff] [blame] | 7947 | if (is_super_abstract) { |
| 7948 | DCHECK(!super_class->ShouldHaveEmbeddedVTable()); |
Vladimir Marko | 7ddae99 | 2022-01-18 14:27:20 +0000 | [diff] [blame] | 7949 | ObjPtr<mirror::PointerArray> super_vtable = super_class->GetVTableDuringLinking(); |
| 7950 | DCHECK(super_vtable != nullptr); |
| 7951 | raw_super_vtable = reinterpret_cast<uint8_t*>(super_vtable.Ptr()) + |
| 7952 | mirror::Array::DataOffset(static_cast<size_t>(kPointerSize)).Uint32Value(); |
| 7953 | super_vtable_length = super_vtable->GetLength(); |
Vladimir Marko | 5171813 | 2022-02-07 16:31:08 +0000 | [diff] [blame] | 7954 | } else { |
| 7955 | DCHECK(super_class->ShouldHaveEmbeddedVTable()); |
| 7956 | raw_super_vtable = reinterpret_cast<uint8_t*>(super_class.Ptr()) + |
| 7957 | mirror::Class::EmbeddedVTableOffset(kPointerSize).Uint32Value(); |
| 7958 | super_vtable_length = super_class->GetEmbeddedVTableLength(); |
Vladimir Marko | 7ddae99 | 2022-01-18 14:27:20 +0000 | [diff] [blame] | 7959 | } |
| 7960 | VTableAccessor super_vtable_accessor(raw_super_vtable, super_vtable_length); |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7961 | static constexpr double kMinLoadFactor = 0.3; |
| 7962 | static constexpr double kMaxLoadFactor = 0.5; |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 7963 | static constexpr size_t kMaxStackBuferSize = 256; |
| 7964 | const size_t super_vtable_buffer_size = super_vtable_length * 3; |
| 7965 | const size_t declared_virtuals_buffer_size = num_virtual_methods * 3; |
| 7966 | const size_t total_buffer_size = super_vtable_buffer_size + declared_virtuals_buffer_size; |
| 7967 | uint32_t* super_vtable_buffer_ptr = (total_buffer_size <= kMaxStackBuferSize) |
| 7968 | ? reinterpret_cast<uint32_t*>(alloca(total_buffer_size * sizeof(uint32_t))) |
| 7969 | : allocator_.AllocArray<uint32_t>(total_buffer_size); |
| 7970 | uint32_t* declared_virtuals_buffer_ptr = super_vtable_buffer_ptr + super_vtable_buffer_size; |
Vladimir Marko | 7ddae99 | 2022-01-18 14:27:20 +0000 | [diff] [blame] | 7971 | VTableSignatureSet super_vtable_signatures( |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7972 | kMinLoadFactor, |
| 7973 | kMaxLoadFactor, |
Vladimir Marko | 7ddae99 | 2022-01-18 14:27:20 +0000 | [diff] [blame] | 7974 | VTableSignatureHash(super_vtable_accessor), |
| 7975 | VTableSignatureEqual(super_vtable_accessor), |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 7976 | super_vtable_buffer_ptr, |
| 7977 | super_vtable_buffer_size, |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7978 | allocator_.Adapter()); |
Vladimir Marko | bed84ef | 2022-01-21 13:57:14 +0000 | [diff] [blame] | 7979 | ArrayRef<uint32_t> same_signature_vtable_lists; |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7980 | // Insert the first `mirror::Object::kVTableLength` indexes with pre-calculated hashes. |
| 7981 | DCHECK_GE(super_vtable_length, mirror::Object::kVTableLength); |
| 7982 | for (uint32_t i = 0; i != mirror::Object::kVTableLength; ++i) { |
| 7983 | size_t hash = class_linker_->object_virtual_method_hashes_[i]; |
Vladimir Marko | 163ebe2 | 2022-01-07 12:35:36 +0000 | [diff] [blame] | 7984 | // There are no duplicate signatures in `java.lang.Object`, so use `HashSet<>::PutWithHash()`. |
| 7985 | // This avoids equality comparison for the three `java.lang.Object.wait()` overloads. |
| 7986 | super_vtable_signatures.PutWithHash(i, hash); |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7987 | } |
| 7988 | // Insert the remaining indexes, check for duplicate signatures. |
| 7989 | if (super_vtable_length > mirror::Object::kVTableLength) { |
| 7990 | for (size_t i = mirror::Object::kVTableLength; i < super_vtable_length; ++i) { |
| 7991 | // Use `super_vtable_accessor` for getting the method for hash calculation. |
| 7992 | // Letting `HashSet<>::insert()` use the internal accessor copy in the hash |
| 7993 | // function prevents the compiler from optimizing this properly because the |
| 7994 | // compiler cannot prove that the accessor copy is immutable. |
| 7995 | size_t hash = ComputeMethodHash(super_vtable_accessor.GetVTableEntry(i)); |
| 7996 | auto [it, inserted] = super_vtable_signatures.InsertWithHash(i, hash); |
| 7997 | if (UNLIKELY(!inserted)) { |
Vladimir Marko | bed84ef | 2022-01-21 13:57:14 +0000 | [diff] [blame] | 7998 | if (same_signature_vtable_lists.empty()) { |
| 7999 | same_signature_vtable_lists = ArrayRef<uint32_t>( |
| 8000 | allocator_.AllocArray<uint32_t>(super_vtable_length), super_vtable_length); |
| 8001 | std::fill_n(same_signature_vtable_lists.data(), super_vtable_length, dex::kDexNoIndex); |
Vladimir Marko | d5d11d9 | 2021-02-02 16:24:25 +0000 | [diff] [blame] | 8002 | same_signature_vtable_lists_ = same_signature_vtable_lists; |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 8003 | } |
| 8004 | DCHECK_LT(*it, i); |
Vladimir Marko | bed84ef | 2022-01-21 13:57:14 +0000 | [diff] [blame] | 8005 | same_signature_vtable_lists[i] = *it; |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 8006 | *it = i; |
| 8007 | } |
| 8008 | } |
| 8009 | } |
| 8010 | |
| 8011 | // For each declared virtual method, look for a superclass virtual method |
| 8012 | // to override and assign a new vtable index if no method was overridden. |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 8013 | DeclaredVirtualSignatureSet declared_virtual_signatures( |
| 8014 | kMinLoadFactor, |
| 8015 | kMaxLoadFactor, |
| 8016 | DeclaredVirtualSignatureHash(klass), |
| 8017 | DeclaredVirtualSignatureEqual(klass), |
| 8018 | declared_virtuals_buffer_ptr, |
| 8019 | declared_virtuals_buffer_size, |
| 8020 | allocator_.Adapter()); |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 8021 | const bool is_proxy_class = klass->IsProxyClass(); |
| 8022 | size_t vtable_length = super_vtable_length; |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 8023 | for (size_t i = 0; i != num_virtual_methods; ++i) { |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 8024 | ArtMethod* virtual_method = klass->GetVirtualMethodDuringLinking(i, kPointerSize); |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 8025 | DCHECK(!virtual_method->IsStatic()) << virtual_method->PrettyMethod(); |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 8026 | ArtMethod* signature_method = UNLIKELY(is_proxy_class) |
| 8027 | ? virtual_method->GetInterfaceMethodForProxyUnchecked(kPointerSize) |
| 8028 | : virtual_method; |
| 8029 | size_t hash = ComputeMethodHash(signature_method); |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 8030 | declared_virtual_signatures.PutWithHash(i, hash); |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 8031 | auto it = super_vtable_signatures.FindWithHash(signature_method, hash); |
| 8032 | if (it != super_vtable_signatures.end()) { |
| 8033 | size_t super_index = *it; |
| 8034 | DCHECK_LT(super_index, super_vtable_length); |
| 8035 | ArtMethod* super_method = super_vtable_accessor.GetVTableEntry(super_index); |
| 8036 | // Historical note: Before Android 4.1, an inaccessible package-private |
| 8037 | // superclass method would have been incorrectly overridden. |
| 8038 | bool overrides = klass->CanAccessMember(super_method->GetDeclaringClass(), |
| 8039 | super_method->GetAccessFlags()); |
Vladimir Marko | d5d11d9 | 2021-02-02 16:24:25 +0000 | [diff] [blame] | 8040 | if (overrides && super_method->IsFinal()) { |
| 8041 | sants.reset(); |
| 8042 | ThrowLinkageError(klass, "Method %s overrides final method in class %s", |
| 8043 | virtual_method->PrettyMethod().c_str(), |
| 8044 | super_method->GetDeclaringClassDescriptor()); |
| 8045 | return 0u; |
| 8046 | } |
Vladimir Marko | bed84ef | 2022-01-21 13:57:14 +0000 | [diff] [blame] | 8047 | if (UNLIKELY(!same_signature_vtable_lists.empty())) { |
Vladimir Marko | d5d11d9 | 2021-02-02 16:24:25 +0000 | [diff] [blame] | 8048 | // We may override more than one method according to JLS, see b/211854716 . |
| 8049 | // We record the highest overridden vtable index here so that we can walk |
| 8050 | // the list to find other overridden methods when constructing the vtable. |
| 8051 | // However, we walk all the methods to check for final method overriding. |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 8052 | size_t current_index = super_index; |
Vladimir Marko | bed84ef | 2022-01-21 13:57:14 +0000 | [diff] [blame] | 8053 | while (same_signature_vtable_lists[current_index] != dex::kDexNoIndex) { |
| 8054 | DCHECK_LT(same_signature_vtable_lists[current_index], current_index); |
| 8055 | current_index = same_signature_vtable_lists[current_index]; |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 8056 | ArtMethod* current_method = super_vtable_accessor.GetVTableEntry(current_index); |
| 8057 | if (klass->CanAccessMember(current_method->GetDeclaringClass(), |
| 8058 | current_method->GetAccessFlags())) { |
Vladimir Marko | d5d11d9 | 2021-02-02 16:24:25 +0000 | [diff] [blame] | 8059 | if (current_method->IsFinal()) { |
| 8060 | sants.reset(); |
| 8061 | ThrowLinkageError(klass, "Method %s overrides final method in class %s", |
| 8062 | virtual_method->PrettyMethod().c_str(), |
| 8063 | current_method->GetDeclaringClassDescriptor()); |
| 8064 | return 0u; |
| 8065 | } |
| 8066 | if (!overrides) { |
| 8067 | overrides = true; |
| 8068 | super_index = current_index; |
| 8069 | super_method = current_method; |
| 8070 | } |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 8071 | } |
| 8072 | } |
| 8073 | } |
| 8074 | if (overrides) { |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 8075 | virtual_method->SetMethodIndex(super_index); |
| 8076 | continue; |
| 8077 | } |
| 8078 | } |
| 8079 | // The method does not override any method from superclass, so it needs a new vtable index. |
| 8080 | virtual_method->SetMethodIndex(vtable_length); |
| 8081 | ++vtable_length; |
| 8082 | } |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 8083 | |
| 8084 | // Assign vtable indexes for interface methods in new interfaces and store them |
| 8085 | // in implementation method arrays. These shall be replaced by actual method |
| 8086 | // pointers later. We do not need to do this for superclass interfaces as we can |
| 8087 | // get these vtable indexes from implementation methods in superclass iftable. |
| 8088 | // Record data for copied methods which shall be referenced by the vtable. |
| 8089 | const size_t ifcount = iftable->Count(); |
| 8090 | ObjPtr<mirror::IfTable> super_iftable = super_class->GetIfTable(); |
| 8091 | const size_t super_ifcount = super_iftable->Count(); |
| 8092 | for (size_t i = ifcount; i != super_ifcount; ) { |
| 8093 | --i; |
| 8094 | DCHECK_LT(i, ifcount); |
| 8095 | ObjPtr<mirror::Class> iface = iftable->GetInterface(i); |
| 8096 | ObjPtr<mirror::PointerArray> method_array = iftable->GetMethodArrayOrNull(i); |
| 8097 | size_t num_methods = (method_array != nullptr) ? method_array->GetLength() : 0u; |
| 8098 | for (size_t j = 0; j != num_methods; ++j) { |
| 8099 | ArtMethod* interface_method = iface->GetVirtualMethod(j, kPointerSize); |
| 8100 | size_t hash = ComputeMethodHash(interface_method); |
| 8101 | ArtMethod* vtable_method = nullptr; |
| 8102 | bool found = false; |
| 8103 | auto it1 = declared_virtual_signatures.FindWithHash(interface_method, hash); |
| 8104 | if (it1 != declared_virtual_signatures.end()) { |
| 8105 | vtable_method = klass->GetVirtualMethodDuringLinking(*it1, kPointerSize); |
| 8106 | found = true; |
| 8107 | } else { |
| 8108 | auto it2 = super_vtable_signatures.FindWithHash(interface_method, hash); |
| 8109 | if (it2 != super_vtable_signatures.end()) { |
Vladimir Marko | 608a915 | 2022-04-04 14:56:12 +0100 | [diff] [blame] | 8110 | // If there are multiple vtable methods with the same signature, the one with |
| 8111 | // the highest vtable index is not nessarily the one in most-derived class. |
| 8112 | // Find the most-derived method. See b/211854716 . |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 8113 | vtable_method = super_vtable_accessor.GetVTableEntry(*it2); |
Vladimir Marko | 608a915 | 2022-04-04 14:56:12 +0100 | [diff] [blame] | 8114 | if (UNLIKELY(!same_signature_vtable_lists.empty())) { |
| 8115 | size_t current_index = *it2; |
| 8116 | while (same_signature_vtable_lists[current_index] != dex::kDexNoIndex) { |
| 8117 | DCHECK_LT(same_signature_vtable_lists[current_index], current_index); |
| 8118 | current_index = same_signature_vtable_lists[current_index]; |
| 8119 | ArtMethod* current_method = super_vtable_accessor.GetVTableEntry(current_index); |
| 8120 | ObjPtr<mirror::Class> current_class = current_method->GetDeclaringClass(); |
| 8121 | if (current_class->IsSubClass(vtable_method->GetDeclaringClass())) { |
| 8122 | vtable_method = current_method; |
| 8123 | } |
| 8124 | } |
| 8125 | } |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 8126 | found = true; |
| 8127 | } |
| 8128 | } |
| 8129 | uint32_t vtable_index = vtable_length; |
| 8130 | if (found) { |
| 8131 | DCHECK(vtable_method != nullptr); |
| 8132 | if (!vtable_method->IsAbstract() && !vtable_method->IsPublic()) { |
Vladimir Marko | 608a915 | 2022-04-04 14:56:12 +0100 | [diff] [blame] | 8133 | // FIXME: Delay the exception until we actually try to call the method. b/211854716 |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 8134 | sants.reset(); |
| 8135 | ThrowIllegalAccessErrorForImplementingMethod(klass, vtable_method, interface_method); |
| 8136 | return 0u; |
| 8137 | } |
| 8138 | vtable_index = vtable_method->GetMethodIndexDuringLinking(); |
| 8139 | if (!vtable_method->IsOverridableByDefaultMethod()) { |
| 8140 | method_array->SetElementPtrSize(j, vtable_index, kPointerSize); |
| 8141 | continue; |
| 8142 | } |
| 8143 | } |
| 8144 | |
| 8145 | auto [it, inserted] = copied_method_records_.InsertWithHash( |
| 8146 | CopiedMethodRecord(interface_method, vtable_index), hash); |
| 8147 | if (found) { |
| 8148 | DCHECK_EQ(vtable_index, it->GetMethodIndex()); |
| 8149 | } else if (inserted) { |
| 8150 | DCHECK_EQ(vtable_index, it->GetMethodIndex()); |
| 8151 | DCHECK_EQ(vtable_index, vtable_length); |
| 8152 | ++vtable_length; |
| 8153 | } else { |
| 8154 | vtable_index = it->GetMethodIndex(); |
| 8155 | } |
| 8156 | method_array->SetElementPtrSize(j, it->GetMethodIndex(), kPointerSize); |
| 8157 | if (inserted) { |
| 8158 | it->SetState(interface_method->IsAbstract() ? CopiedMethodRecord::State::kAbstractSingle |
| 8159 | : CopiedMethodRecord::State::kDefaultSingle); |
| 8160 | } else { |
| 8161 | it->UpdateState(iface, interface_method, vtable_index, iftable, ifcount, i); |
| 8162 | } |
| 8163 | } |
| 8164 | } |
| 8165 | // Finalize copied method records and check if we can reuse some methods from superclass vtable. |
| 8166 | size_t num_new_copied_methods = copied_method_records_.size(); |
| 8167 | for (CopiedMethodRecord& record : copied_method_records_) { |
| 8168 | uint32_t vtable_index = record.GetMethodIndex(); |
| 8169 | if (vtable_index < super_vtable_length) { |
| 8170 | ArtMethod* super_method = super_vtable_accessor.GetVTableEntry(record.GetMethodIndex()); |
| 8171 | DCHECK(super_method->IsOverridableByDefaultMethod()); |
| 8172 | record.FinalizeState( |
| 8173 | super_method, vtable_index, iftable, ifcount, super_iftable, super_ifcount); |
| 8174 | if (record.GetState() == CopiedMethodRecord::State::kUseSuperMethod) { |
| 8175 | --num_new_copied_methods; |
| 8176 | } |
| 8177 | } |
| 8178 | } |
| 8179 | num_new_copied_methods_ = num_new_copied_methods; |
| 8180 | |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 8181 | if (UNLIKELY(!IsUint<16>(vtable_length))) { |
| 8182 | sants.reset(); |
| 8183 | ThrowClassFormatError(klass, "Too many methods defined on class: %zd", vtable_length); |
| 8184 | return 0u; |
| 8185 | } |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 8186 | |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 8187 | return vtable_length; |
| 8188 | } |
| 8189 | |
| 8190 | template <PointerSize kPointerSize> |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 8191 | bool ClassLinker::LinkMethodsHelper<kPointerSize>::FindCopiedMethodsForInterface( |
| 8192 | ObjPtr<mirror::Class> klass, |
| 8193 | size_t num_virtual_methods, |
| 8194 | ObjPtr<mirror::IfTable> iftable) { |
| 8195 | DCHECK(klass->IsInterface()); |
| 8196 | DCHECK(klass->HasSuperClass()); |
| 8197 | DCHECK(klass->GetSuperClass()->IsObjectClass()); |
| 8198 | DCHECK_EQ(klass->GetSuperClass()->GetIfTableCount(), 0); |
| 8199 | |
| 8200 | // There should be no thread suspension unless we want to throw an exception. |
| 8201 | // (We are using `ObjPtr<>`s that are invalidated by thread suspension.) |
| 8202 | std::optional<ScopedAssertNoThreadSuspension> sants(__FUNCTION__); |
| 8203 | |
| 8204 | // Prepare a `HashSet<>` with the declared virtual methods. These mask any methods |
| 8205 | // from superinterfaces, so we can filter out matching superinterface methods. |
| 8206 | static constexpr double kMinLoadFactor = 0.3; |
| 8207 | static constexpr double kMaxLoadFactor = 0.5; |
| 8208 | static constexpr size_t kMaxStackBuferSize = 256; |
| 8209 | const size_t declared_virtuals_buffer_size = num_virtual_methods * 3; |
| 8210 | uint32_t* declared_virtuals_buffer_ptr = (declared_virtuals_buffer_size <= kMaxStackBuferSize) |
| 8211 | ? reinterpret_cast<uint32_t*>(alloca(declared_virtuals_buffer_size * sizeof(uint32_t))) |
| 8212 | : allocator_.AllocArray<uint32_t>(declared_virtuals_buffer_size); |
| 8213 | DeclaredVirtualSignatureSet declared_virtual_signatures( |
| 8214 | kMinLoadFactor, |
| 8215 | kMaxLoadFactor, |
| 8216 | DeclaredVirtualSignatureHash(klass), |
| 8217 | DeclaredVirtualSignatureEqual(klass), |
| 8218 | declared_virtuals_buffer_ptr, |
| 8219 | declared_virtuals_buffer_size, |
| 8220 | allocator_.Adapter()); |
| 8221 | for (size_t i = 0; i != num_virtual_methods; ++i) { |
| 8222 | ArtMethod* virtual_method = klass->GetVirtualMethodDuringLinking(i, kPointerSize); |
| 8223 | DCHECK(!virtual_method->IsStatic()) << virtual_method->PrettyMethod(); |
| 8224 | size_t hash = ComputeMethodHash(virtual_method); |
| 8225 | declared_virtual_signatures.PutWithHash(i, hash); |
| 8226 | } |
| 8227 | |
| 8228 | // We do not create miranda methods for interface classes, so we do not need to track |
| 8229 | // non-default (abstract) interface methods. The downside is that we cannot use the |
| 8230 | // optimized code paths with `CopiedMethodRecord::State::kDefaultSingle` and since |
| 8231 | // we do not fill method arrays for interfaces, the method search actually has to |
| 8232 | // compare signatures instead of searching for the implementing method. |
| 8233 | const size_t ifcount = iftable->Count(); |
| 8234 | size_t new_method_index = num_virtual_methods; |
| 8235 | for (size_t i = ifcount; i != 0u; ) { |
| 8236 | --i; |
| 8237 | DCHECK_LT(i, ifcount); |
| 8238 | ObjPtr<mirror::Class> iface = iftable->GetInterface(i); |
| 8239 | if (!iface->HasDefaultMethods()) { |
| 8240 | continue; // No default methods to process. |
| 8241 | } |
| 8242 | size_t num_methods = iface->NumDeclaredVirtualMethods(); |
| 8243 | for (size_t j = 0; j != num_methods; ++j) { |
| 8244 | ArtMethod* interface_method = iface->GetVirtualMethod(j, kPointerSize); |
| 8245 | if (!interface_method->IsDefault()) { |
| 8246 | continue; // Do not process this non-default method. |
| 8247 | } |
| 8248 | size_t hash = ComputeMethodHash(interface_method); |
| 8249 | auto it1 = declared_virtual_signatures.FindWithHash(interface_method, hash); |
| 8250 | if (it1 != declared_virtual_signatures.end()) { |
| 8251 | ArtMethod* virtual_method = klass->GetVirtualMethodDuringLinking(*it1, kPointerSize); |
| 8252 | if (!virtual_method->IsAbstract() && !virtual_method->IsPublic()) { |
| 8253 | sants.reset(); |
| 8254 | ThrowIllegalAccessErrorForImplementingMethod(klass, virtual_method, interface_method); |
| 8255 | return false; |
| 8256 | } |
| 8257 | continue; // This default method is masked by a method declared in this interface. |
| 8258 | } |
| 8259 | |
| 8260 | CopiedMethodRecord new_record(interface_method, new_method_index); |
| 8261 | auto it = copied_method_records_.FindWithHash(new_record, hash); |
| 8262 | if (it == copied_method_records_.end()) { |
| 8263 | // Pretend that there is another default method and try to update the state. |
| 8264 | // If the `interface_method` is not masked, the state shall change to |
| 8265 | // `kDefaultConflict`; if it is masked, the state remains `kDefault`. |
| 8266 | new_record.SetState(CopiedMethodRecord::State::kDefault); |
| 8267 | new_record.UpdateStateForInterface(iface, interface_method, iftable, ifcount, i); |
| 8268 | if (new_record.GetState() == CopiedMethodRecord::State::kDefaultConflict) { |
| 8269 | // Insert the new record with the state `kDefault`. |
| 8270 | new_record.SetState(CopiedMethodRecord::State::kDefault); |
| 8271 | copied_method_records_.PutWithHash(new_record, hash); |
| 8272 | DCHECK_EQ(new_method_index, new_record.GetMethodIndex()); |
| 8273 | ++new_method_index; |
| 8274 | } |
| 8275 | } else { |
| 8276 | it->UpdateStateForInterface(iface, interface_method, iftable, ifcount, i); |
| 8277 | } |
| 8278 | } |
| 8279 | } |
| 8280 | |
| 8281 | // Prune records without conflict. (Method indexes are updated in `ReallocMethods()`.) |
| 8282 | // We do not copy normal default methods to subinterfaces, instead we find the |
| 8283 | // default method with `Class::FindVirtualMethodForInterfaceSuper()` when needed. |
| 8284 | size_t num_new_copied_methods = copied_method_records_.size(); |
| 8285 | for (CopiedMethodRecord& record : copied_method_records_) { |
| 8286 | if (record.GetState() != CopiedMethodRecord::State::kDefaultConflict) { |
| 8287 | DCHECK(record.GetState() == CopiedMethodRecord::State::kDefault); |
| 8288 | record.SetState(CopiedMethodRecord::State::kUseSuperMethod); |
| 8289 | --num_new_copied_methods; |
| 8290 | } |
| 8291 | } |
| 8292 | num_new_copied_methods_ = num_new_copied_methods; |
| 8293 | |
| 8294 | return true; |
| 8295 | } |
| 8296 | |
| 8297 | |
| 8298 | template <PointerSize kPointerSize> |
Vladimir Marko | b91402f | 2021-12-21 15:55:06 +0000 | [diff] [blame] | 8299 | FLATTEN |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 8300 | bool ClassLinker::LinkMethodsHelper<kPointerSize>::LinkMethods( |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 8301 | Thread* self, |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 8302 | Handle<mirror::Class> klass, |
| 8303 | Handle<mirror::ObjectArray<mirror::Class>> interfaces, |
| 8304 | bool* out_new_conflict, |
| 8305 | ArtMethod** out_imt) { |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 8306 | const size_t num_virtual_methods = klass->NumVirtualMethods(); |
| 8307 | if (klass->IsInterface()) { |
| 8308 | // No vtable. |
| 8309 | if (!IsUint<16>(num_virtual_methods)) { |
| 8310 | ThrowClassFormatError(klass.Get(), "Too many methods on interface: %zu", num_virtual_methods); |
| 8311 | return false; |
| 8312 | } |
| 8313 | bool has_defaults = false; |
| 8314 | // Assign each method an IMT index and set the default flag. |
| 8315 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Vladimir Marko | b91402f | 2021-12-21 15:55:06 +0000 | [diff] [blame] | 8316 | ArtMethod* m = klass->GetVirtualMethodDuringLinking(i, kPointerSize); |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 8317 | m->SetMethodIndex(i); |
| 8318 | if (!m->IsAbstract()) { |
| 8319 | // If the dex file does not support default methods, throw ClassFormatError. |
| 8320 | // This check is necessary to protect from odd cases, such as native default |
| 8321 | // methods, that the dex file verifier permits for old dex file versions. b/157170505 |
| 8322 | // FIXME: This should be `if (!m->GetDexFile()->SupportsDefaultMethods())` but we're |
| 8323 | // currently running CTS tests for default methods with dex file version 035 which |
| 8324 | // does not support default methods. So, we limit this to native methods. b/157718952 |
| 8325 | if (m->IsNative()) { |
| 8326 | DCHECK(!m->GetDexFile()->SupportsDefaultMethods()); |
| 8327 | ThrowClassFormatError(klass.Get(), |
| 8328 | "Dex file does not support default method '%s'", |
| 8329 | m->PrettyMethod().c_str()); |
| 8330 | return false; |
| 8331 | } |
Vladimir Marko | fd0aec4 | 2022-01-27 14:27:24 +0000 | [diff] [blame] | 8332 | if (!m->IsPublic()) { |
| 8333 | // The verifier should have caught the non-public method for dex version 37. |
| 8334 | // Just warn and skip it since this is from before default-methods so we don't |
| 8335 | // really need to care that it has code. |
| 8336 | LOG(WARNING) << "Default interface method " << m->PrettyMethod() << " is not public! " |
| 8337 | << "This will be a fatal error in subsequent versions of android. " |
| 8338 | << "Continuing anyway."; |
| 8339 | } |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 8340 | m->SetAccessFlags(m->GetAccessFlags() | kAccDefault); |
| 8341 | has_defaults = true; |
| 8342 | } |
| 8343 | } |
| 8344 | // Mark that we have default methods so that we won't need to scan the virtual_methods_ array |
| 8345 | // during initialization. This is a performance optimization. We could simply traverse the |
| 8346 | // virtual_methods_ array again during initialization. |
| 8347 | if (has_defaults) { |
| 8348 | klass->SetHasDefaultMethods(); |
| 8349 | } |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 8350 | ObjPtr<mirror::IfTable> iftable = SetupInterfaceLookupTable( |
| 8351 | self, klass, &allocator_, NonProxyInterfacesAccessor(class_linker_, klass)); |
| 8352 | if (UNLIKELY(iftable == nullptr)) { |
| 8353 | self->AssertPendingException(); |
| 8354 | return false; |
| 8355 | } |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 8356 | size_t ifcount = iftable->Count(); |
| 8357 | bool have_super_with_defaults = false; |
| 8358 | for (size_t i = 0; i != ifcount; ++i) { |
| 8359 | if (iftable->GetInterface(i)->HasDefaultMethods()) { |
| 8360 | have_super_with_defaults = true; |
| 8361 | break; |
| 8362 | } |
| 8363 | } |
| 8364 | LengthPrefixedArray<ArtMethod>* old_methods = kIsDebugBuild ? klass->GetMethodsPtr() : nullptr; |
| 8365 | if (have_super_with_defaults) { |
| 8366 | if (!FindCopiedMethodsForInterface(klass.Get(), num_virtual_methods, iftable)) { |
| 8367 | self->AssertPendingException(); |
| 8368 | return false; |
| 8369 | } |
| 8370 | if (num_new_copied_methods_ != 0u) { |
| 8371 | // Re-check the number of methods. |
| 8372 | size_t final_num_virtual_methods = num_virtual_methods + num_new_copied_methods_; |
| 8373 | if (!IsUint<16>(final_num_virtual_methods)) { |
| 8374 | ThrowClassFormatError( |
| 8375 | klass.Get(), "Too many methods on interface: %zu", final_num_virtual_methods); |
| 8376 | return false; |
| 8377 | } |
| 8378 | ReallocMethods(klass.Get()); |
| 8379 | } |
| 8380 | } |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 8381 | klass->SetIfTable(iftable); |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 8382 | if (kIsDebugBuild) { |
| 8383 | // May cause thread suspension, so do this after we're done with `ObjPtr<> iftable`. |
| 8384 | ClobberOldMethods(old_methods, klass->GetMethodsPtr()); |
| 8385 | } |
| 8386 | return true; |
Vladimir Marko | b91402f | 2021-12-21 15:55:06 +0000 | [diff] [blame] | 8387 | } else if (LIKELY(klass->HasSuperClass())) { |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 8388 | // We set up the interface lookup table now because we need it to determine if we need |
| 8389 | // to update any vtable entries with new default method implementations. |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 8390 | StackHandleScope<3> hs(self); |
Vladimir Marko | 0441d20 | 2022-02-18 13:55:15 +0000 | [diff] [blame] | 8391 | MutableHandle<mirror::IfTable> iftable = hs.NewHandle(UNLIKELY(klass->IsProxyClass()) |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 8392 | ? SetupInterfaceLookupTable(self, klass, &allocator_, ProxyInterfacesAccessor(interfaces)) |
| 8393 | : SetupInterfaceLookupTable( |
| 8394 | self, klass, &allocator_, NonProxyInterfacesAccessor(class_linker_, klass))); |
| 8395 | if (UNLIKELY(iftable == nullptr)) { |
| 8396 | self->AssertPendingException(); |
| 8397 | return false; |
| 8398 | } |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 8399 | |
Vladimir Marko | 5171813 | 2022-02-07 16:31:08 +0000 | [diff] [blame] | 8400 | // 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] | 8401 | Handle<mirror::Class> super_class = hs.NewHandle(klass->GetSuperClass()); |
Vladimir Marko | 5171813 | 2022-02-07 16:31:08 +0000 | [diff] [blame] | 8402 | bool is_klass_abstract = klass->IsAbstract(); |
| 8403 | bool is_super_abstract = super_class->IsAbstract(); |
| 8404 | DCHECK_EQ(klass->ShouldHaveImt(), !is_klass_abstract); |
| 8405 | DCHECK_EQ(super_class->ShouldHaveImt(), !is_super_abstract); |
| 8406 | if (!is_klass_abstract && !is_super_abstract) { |
| 8407 | ImTable* super_imt = super_class->GetImt(kPointerSize); |
| 8408 | for (size_t i = 0; i < ImTable::kSize; ++i) { |
| 8409 | out_imt[i] = super_imt->Get(i, kPointerSize); |
| 8410 | } |
| 8411 | } |
| 8412 | |
| 8413 | // If there are no new virtual methods and no new interfaces, we can simply reuse |
| 8414 | // 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] | 8415 | const size_t super_vtable_length = super_class->GetVTableLength(); |
Vladimir Marko | 0f71b19 | 2022-02-02 17:20:12 +0000 | [diff] [blame] | 8416 | if (num_virtual_methods == 0 && iftable.Get() == super_class->GetIfTable()) { |
Vladimir Marko | 5171813 | 2022-02-07 16:31:08 +0000 | [diff] [blame] | 8417 | DCHECK_EQ(is_super_abstract, !super_class->ShouldHaveEmbeddedVTable()); |
| 8418 | if (is_super_abstract) { |
| 8419 | DCHECK(super_class->IsAbstract() && !super_class->IsArrayClass()); |
| 8420 | ObjPtr<mirror::PointerArray> super_vtable = super_class->GetVTable(); |
| 8421 | CHECK(super_vtable != nullptr) << super_class->PrettyClass(); |
| 8422 | klass->SetVTable(super_vtable); |
| 8423 | // No IMT in the super class, we need to reconstruct it from the iftable. |
| 8424 | if (!is_klass_abstract && iftable->Count() != 0) { |
| 8425 | class_linker_->FillIMTFromIfTable(iftable.Get(), |
| 8426 | runtime_->GetImtUnimplementedMethod(), |
| 8427 | runtime_->GetImtConflictMethod(), |
| 8428 | klass.Get(), |
| 8429 | /*create_conflict_tables=*/false, |
| 8430 | /*ignore_copied_methods=*/false, |
| 8431 | out_new_conflict, |
| 8432 | out_imt); |
| 8433 | } |
| 8434 | } else { |
Vladimir Marko | 0441d20 | 2022-02-18 13:55:15 +0000 | [diff] [blame] | 8435 | ObjPtr<mirror::PointerArray> vtable = AllocPointerArray(self, super_vtable_length); |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 8436 | if (UNLIKELY(vtable == nullptr)) { |
| 8437 | self->AssertPendingOOMException(); |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 8438 | return false; |
| 8439 | } |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 8440 | for (size_t i = 0; i < super_vtable_length; i++) { |
| 8441 | vtable->SetElementPtrSize( |
| 8442 | i, super_class->GetEmbeddedVTableEntry(i, kPointerSize), kPointerSize); |
| 8443 | } |
| 8444 | klass->SetVTable(vtable); |
Vladimir Marko | 5171813 | 2022-02-07 16:31:08 +0000 | [diff] [blame] | 8445 | // The IMT was already copied from superclass if `klass` is not abstract. |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 8446 | } |
Vladimir Marko | bed84ef | 2022-01-21 13:57:14 +0000 | [diff] [blame] | 8447 | klass->SetIfTable(iftable.Get()); |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 8448 | return true; |
| 8449 | } |
| 8450 | |
Vladimir Marko | bed84ef | 2022-01-21 13:57:14 +0000 | [diff] [blame] | 8451 | // Allocate method arrays, so that we can link interface methods without thread suspension, |
| 8452 | // otherwise GC could miss visiting newly allocated copied methods. |
| 8453 | // TODO: Do not allocate copied methods during linking, store only records about what |
| 8454 | // we need to allocate and allocate it at the end. Start with superclass iftable and |
| 8455 | // perform copy-on-write when needed to facilitate maximum memory sharing. |
Vladimir Marko | 0441d20 | 2022-02-18 13:55:15 +0000 | [diff] [blame] | 8456 | if (!AllocateIfTableMethodArrays(self, klass, iftable)) { |
| 8457 | self->AssertPendingOOMException(); |
Vladimir Marko | bed84ef | 2022-01-21 13:57:14 +0000 | [diff] [blame] | 8458 | return false; |
| 8459 | } |
| 8460 | |
Vladimir Marko | 5171813 | 2022-02-07 16:31:08 +0000 | [diff] [blame] | 8461 | size_t final_vtable_size = AssignVTableIndexes( |
| 8462 | 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] | 8463 | if (final_vtable_size == 0u) { |
| 8464 | self->AssertPendingException(); |
| 8465 | return false; |
| 8466 | } |
| 8467 | DCHECK(IsUint<16>(final_vtable_size)); |
| 8468 | |
| 8469 | // Allocate the new vtable. |
Vladimir Marko | 0441d20 | 2022-02-18 13:55:15 +0000 | [diff] [blame] | 8470 | Handle<mirror::PointerArray> vtable = hs.NewHandle(AllocPointerArray(self, final_vtable_size)); |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 8471 | if (UNLIKELY(vtable == nullptr)) { |
| 8472 | self->AssertPendingOOMException(); |
| 8473 | return false; |
| 8474 | } |
| 8475 | |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 8476 | LengthPrefixedArray<ArtMethod>* old_methods = kIsDebugBuild ? klass->GetMethodsPtr() : nullptr; |
| 8477 | if (num_new_copied_methods_ != 0u) { |
| 8478 | ReallocMethods(klass.Get()); |
| 8479 | } |
| 8480 | |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 8481 | // Store new virtual methods in the new vtable. |
Vladimir Marko | d5d11d9 | 2021-02-02 16:24:25 +0000 | [diff] [blame] | 8482 | ArrayRef<uint32_t> same_signature_vtable_lists = same_signature_vtable_lists_; |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 8483 | for (ArtMethod& virtual_method : klass->GetVirtualMethodsSliceUnchecked(kPointerSize)) { |
Vladimir Marko | d5d11d9 | 2021-02-02 16:24:25 +0000 | [diff] [blame] | 8484 | uint32_t vtable_index = virtual_method.GetMethodIndexDuringLinking(); |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 8485 | vtable->SetElementPtrSize(vtable_index, &virtual_method, kPointerSize); |
Vladimir Marko | d5d11d9 | 2021-02-02 16:24:25 +0000 | [diff] [blame] | 8486 | if (UNLIKELY(vtable_index < same_signature_vtable_lists.size())) { |
| 8487 | // We may override more than one method according to JLS, see b/211854716 . |
| 8488 | // If we do, arbitrarily update the method index to the lowest overridden vtable index. |
| 8489 | while (same_signature_vtable_lists[vtable_index] != dex::kDexNoIndex) { |
| 8490 | DCHECK_LT(same_signature_vtable_lists[vtable_index], vtable_index); |
| 8491 | vtable_index = same_signature_vtable_lists[vtable_index]; |
| 8492 | ArtMethod* current_method = super_class->GetVTableEntry(vtable_index, kPointerSize); |
| 8493 | if (klass->CanAccessMember(current_method->GetDeclaringClass(), |
| 8494 | current_method->GetAccessFlags())) { |
| 8495 | DCHECK(!current_method->IsFinal()); |
| 8496 | vtable->SetElementPtrSize(vtable_index, &virtual_method, kPointerSize); |
| 8497 | virtual_method.SetMethodIndex(vtable_index); |
| 8498 | } |
| 8499 | } |
| 8500 | } |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 8501 | } |
| 8502 | |
| 8503 | // For non-overridden vtable slots, copy a method from `super_class`. |
| 8504 | for (size_t j = 0; j != super_vtable_length; ++j) { |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 8505 | if (vtable->GetElementPtrSize<ArtMethod*, kPointerSize>(j) == nullptr) { |
| 8506 | ArtMethod* super_method = super_class->GetVTableEntry(j, kPointerSize); |
| 8507 | vtable->SetElementPtrSize(j, super_method, kPointerSize); |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 8508 | } |
| 8509 | } |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 8510 | |
Vladimir Marko | 5171813 | 2022-02-07 16:31:08 +0000 | [diff] [blame] | 8511 | // Update the `iftable` (and IMT) with finalized virtual methods. |
Vladimir Marko | 0441d20 | 2022-02-18 13:55:15 +0000 | [diff] [blame] | 8512 | if (!FinalizeIfTable(klass, |
| 8513 | iftable, |
| 8514 | vtable, |
| 8515 | is_klass_abstract, |
| 8516 | is_super_abstract, |
| 8517 | out_new_conflict, |
| 8518 | out_imt)) { |
| 8519 | self->AssertPendingOOMException(); |
| 8520 | return false; |
| 8521 | } |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 8522 | |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 8523 | klass->SetVTable(vtable.Get()); |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame] | 8524 | klass->SetIfTable(iftable.Get()); |
| 8525 | if (kIsDebugBuild) { |
| 8526 | CheckVTable(self, klass, kPointerSize); |
| 8527 | ClobberOldMethods(old_methods, klass->GetMethodsPtr()); |
| 8528 | } |
| 8529 | return true; |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 8530 | } else { |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 8531 | return LinkJavaLangObjectMethods(self, klass); |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 8532 | } |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 8533 | } |
| 8534 | |
Vladimir Marko | b91402f | 2021-12-21 15:55:06 +0000 | [diff] [blame] | 8535 | template <PointerSize kPointerSize> |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 8536 | bool ClassLinker::LinkMethodsHelper<kPointerSize>::LinkJavaLangObjectMethods( |
Vladimir Marko | b91402f | 2021-12-21 15:55:06 +0000 | [diff] [blame] | 8537 | Thread* self, |
| 8538 | Handle<mirror::Class> klass) { |
| 8539 | DCHECK_EQ(klass.Get(), GetClassRoot<mirror::Object>(class_linker_)); |
| 8540 | DCHECK_EQ(klass->NumVirtualMethods(), mirror::Object::kVTableLength); |
| 8541 | static_assert(IsUint<16>(mirror::Object::kVTableLength)); |
Vladimir Marko | 0441d20 | 2022-02-18 13:55:15 +0000 | [diff] [blame] | 8542 | ObjPtr<mirror::PointerArray> vtable = AllocPointerArray(self, mirror::Object::kVTableLength); |
Vladimir Marko | b91402f | 2021-12-21 15:55:06 +0000 | [diff] [blame] | 8543 | if (UNLIKELY(vtable == nullptr)) { |
| 8544 | self->AssertPendingOOMException(); |
| 8545 | return false; |
| 8546 | } |
| 8547 | for (size_t i = 0; i < mirror::Object::kVTableLength; ++i) { |
| 8548 | ArtMethod* virtual_method = klass->GetVirtualMethodDuringLinking(i, kPointerSize); |
| 8549 | vtable->SetElementPtrSize(i, virtual_method, kPointerSize); |
| 8550 | virtual_method->SetMethodIndex(i); |
| 8551 | } |
| 8552 | klass->SetVTable(vtable); |
| 8553 | InitializeObjectVirtualMethodHashes( |
| 8554 | klass.Get(), |
| 8555 | kPointerSize, |
| 8556 | ArrayRef<uint32_t>(class_linker_->object_virtual_method_hashes_)); |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 8557 | // The interface table is already allocated but there are no interface methods to link. |
| 8558 | DCHECK(klass->GetIfTable() != nullptr); |
| 8559 | DCHECK_EQ(klass->GetIfTableCount(), 0); |
Vladimir Marko | b91402f | 2021-12-21 15:55:06 +0000 | [diff] [blame] | 8560 | return true; |
| 8561 | } |
| 8562 | |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 8563 | // Populate the class vtable and itable. Compute return type indices. |
| 8564 | bool ClassLinker::LinkMethods(Thread* self, |
| 8565 | Handle<mirror::Class> klass, |
| 8566 | Handle<mirror::ObjectArray<mirror::Class>> interfaces, |
| 8567 | bool* out_new_conflict, |
| 8568 | ArtMethod** out_imt) { |
| 8569 | self->AllowThreadSuspension(); |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 8570 | // Link virtual methods then interface methods. |
| 8571 | Runtime* const runtime = Runtime::Current(); |
Vladimir Marko | b91402f | 2021-12-21 15:55:06 +0000 | [diff] [blame] | 8572 | if (LIKELY(GetImagePointerSize() == kRuntimePointerSize)) { |
| 8573 | LinkMethodsHelper<kRuntimePointerSize> helper(this, klass, self, runtime); |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 8574 | return helper.LinkMethods(self, klass, interfaces, out_new_conflict, out_imt); |
Vladimir Marko | b91402f | 2021-12-21 15:55:06 +0000 | [diff] [blame] | 8575 | } else { |
| 8576 | constexpr PointerSize kOtherPointerSize = |
| 8577 | (kRuntimePointerSize == PointerSize::k64) ? PointerSize::k32 : PointerSize::k64; |
| 8578 | LinkMethodsHelper<kOtherPointerSize> helper(this, klass, self, runtime); |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 8579 | return helper.LinkMethods(self, klass, interfaces, out_new_conflict, out_imt); |
Vladimir Marko | b91402f | 2021-12-21 15:55:06 +0000 | [diff] [blame] | 8580 | } |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 8581 | } |
| 8582 | |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8583 | class ClassLinker::LinkFieldsHelper { |
| 8584 | public: |
| 8585 | static bool LinkFields(ClassLinker* class_linker, |
| 8586 | Thread* self, |
| 8587 | Handle<mirror::Class> klass, |
| 8588 | bool is_static, |
| 8589 | size_t* class_size) |
| 8590 | REQUIRES_SHARED(Locks::mutator_lock_); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 8591 | |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8592 | private: |
| 8593 | enum class FieldTypeOrder : uint16_t; |
| 8594 | class FieldGaps; |
| 8595 | |
| 8596 | struct FieldTypeOrderAndIndex { |
| 8597 | FieldTypeOrder field_type_order; |
| 8598 | uint16_t field_index; |
| 8599 | }; |
| 8600 | |
| 8601 | static FieldTypeOrder FieldTypeOrderFromFirstDescriptorCharacter(char first_char); |
| 8602 | |
| 8603 | template <size_t kSize> |
| 8604 | static MemberOffset AssignFieldOffset(ArtField* field, MemberOffset field_offset) |
| 8605 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 8606 | }; |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 8607 | |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8608 | // We use the following order of field types for assigning offsets. |
| 8609 | // Some fields can be shuffled forward to fill gaps, see `ClassLinker::LinkFields()`. |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8610 | enum class ClassLinker::LinkFieldsHelper::FieldTypeOrder : uint16_t { |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8611 | kReference = 0u, |
| 8612 | kLong, |
| 8613 | kDouble, |
| 8614 | kInt, |
| 8615 | kFloat, |
| 8616 | kChar, |
| 8617 | kShort, |
| 8618 | kBoolean, |
| 8619 | kByte, |
| 8620 | |
| 8621 | kLast64BitType = kDouble, |
| 8622 | kLast32BitType = kFloat, |
| 8623 | kLast16BitType = kShort, |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 8624 | }; |
| 8625 | |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8626 | ALWAYS_INLINE |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8627 | ClassLinker::LinkFieldsHelper::FieldTypeOrder |
| 8628 | ClassLinker::LinkFieldsHelper::FieldTypeOrderFromFirstDescriptorCharacter(char first_char) { |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8629 | switch (first_char) { |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8630 | case 'J': |
| 8631 | return FieldTypeOrder::kLong; |
| 8632 | case 'D': |
| 8633 | return FieldTypeOrder::kDouble; |
| 8634 | case 'I': |
| 8635 | return FieldTypeOrder::kInt; |
| 8636 | case 'F': |
| 8637 | return FieldTypeOrder::kFloat; |
| 8638 | case 'C': |
| 8639 | return FieldTypeOrder::kChar; |
| 8640 | case 'S': |
| 8641 | return FieldTypeOrder::kShort; |
| 8642 | case 'Z': |
| 8643 | return FieldTypeOrder::kBoolean; |
| 8644 | case 'B': |
| 8645 | return FieldTypeOrder::kByte; |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8646 | default: |
| 8647 | DCHECK(first_char == 'L' || first_char == '[') << first_char; |
| 8648 | return FieldTypeOrder::kReference; |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8649 | } |
| 8650 | } |
| 8651 | |
| 8652 | // Gaps where we can insert fields in object layout. |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8653 | class ClassLinker::LinkFieldsHelper::FieldGaps { |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8654 | public: |
| 8655 | template <uint32_t kSize> |
| 8656 | ALWAYS_INLINE MemberOffset AlignFieldOffset(MemberOffset field_offset) { |
| 8657 | static_assert(kSize == 2u || kSize == 4u || kSize == 8u); |
| 8658 | if (!IsAligned<kSize>(field_offset.Uint32Value())) { |
| 8659 | uint32_t gap_start = field_offset.Uint32Value(); |
| 8660 | field_offset = MemberOffset(RoundUp(gap_start, kSize)); |
| 8661 | AddGaps<kSize - 1u>(gap_start, field_offset.Uint32Value()); |
| 8662 | } |
| 8663 | return field_offset; |
| 8664 | } |
| 8665 | |
| 8666 | template <uint32_t kSize> |
| 8667 | bool HasGap() const { |
| 8668 | static_assert(kSize == 1u || kSize == 2u || kSize == 4u); |
| 8669 | return (kSize == 1u && gap1_offset_ != kNoOffset) || |
| 8670 | (kSize <= 2u && gap2_offset_ != kNoOffset) || |
| 8671 | gap4_offset_ != kNoOffset; |
| 8672 | } |
| 8673 | |
| 8674 | template <uint32_t kSize> |
| 8675 | MemberOffset ReleaseGap() { |
| 8676 | static_assert(kSize == 1u || kSize == 2u || kSize == 4u); |
| 8677 | uint32_t result; |
| 8678 | if (kSize == 1u && gap1_offset_ != kNoOffset) { |
| 8679 | DCHECK(gap2_offset_ == kNoOffset || gap2_offset_ > gap1_offset_); |
| 8680 | DCHECK(gap4_offset_ == kNoOffset || gap4_offset_ > gap1_offset_); |
| 8681 | result = gap1_offset_; |
| 8682 | gap1_offset_ = kNoOffset; |
| 8683 | } else if (kSize <= 2u && gap2_offset_ != kNoOffset) { |
| 8684 | DCHECK(gap4_offset_ == kNoOffset || gap4_offset_ > gap2_offset_); |
| 8685 | result = gap2_offset_; |
| 8686 | gap2_offset_ = kNoOffset; |
| 8687 | if (kSize < 2u) { |
| 8688 | AddGaps<1u>(result + kSize, result + 2u); |
| 8689 | } |
| 8690 | } else { |
| 8691 | DCHECK_NE(gap4_offset_, kNoOffset); |
| 8692 | result = gap4_offset_; |
| 8693 | gap4_offset_ = kNoOffset; |
| 8694 | if (kSize < 4u) { |
| 8695 | AddGaps<kSize | 2u>(result + kSize, result + 4u); |
| 8696 | } |
| 8697 | } |
| 8698 | return MemberOffset(result); |
| 8699 | } |
| 8700 | |
| 8701 | private: |
| 8702 | template <uint32_t kGapsToCheck> |
| 8703 | void AddGaps(uint32_t gap_start, uint32_t gap_end) { |
| 8704 | if ((kGapsToCheck & 1u) != 0u) { |
| 8705 | DCHECK_LT(gap_start, gap_end); |
| 8706 | DCHECK_ALIGNED(gap_end, 2u); |
| 8707 | if ((gap_start & 1u) != 0u) { |
| 8708 | DCHECK_EQ(gap1_offset_, kNoOffset); |
| 8709 | gap1_offset_ = gap_start; |
| 8710 | gap_start += 1u; |
| 8711 | if (kGapsToCheck == 1u || gap_start == gap_end) { |
| 8712 | DCHECK_EQ(gap_start, gap_end); |
| 8713 | return; |
| 8714 | } |
| 8715 | } |
| 8716 | } |
| 8717 | |
| 8718 | if ((kGapsToCheck & 2u) != 0u) { |
| 8719 | DCHECK_LT(gap_start, gap_end); |
| 8720 | DCHECK_ALIGNED(gap_start, 2u); |
| 8721 | DCHECK_ALIGNED(gap_end, 4u); |
| 8722 | if ((gap_start & 2u) != 0u) { |
| 8723 | DCHECK_EQ(gap2_offset_, kNoOffset); |
| 8724 | gap2_offset_ = gap_start; |
| 8725 | gap_start += 2u; |
| 8726 | if (kGapsToCheck <= 3u || gap_start == gap_end) { |
| 8727 | DCHECK_EQ(gap_start, gap_end); |
| 8728 | return; |
| 8729 | } |
| 8730 | } |
| 8731 | } |
| 8732 | |
| 8733 | if ((kGapsToCheck & 4u) != 0u) { |
| 8734 | DCHECK_LT(gap_start, gap_end); |
| 8735 | DCHECK_ALIGNED(gap_start, 4u); |
| 8736 | DCHECK_ALIGNED(gap_end, 8u); |
| 8737 | DCHECK_EQ(gap_start + 4u, gap_end); |
| 8738 | DCHECK_EQ(gap4_offset_, kNoOffset); |
| 8739 | gap4_offset_ = gap_start; |
| 8740 | return; |
| 8741 | } |
| 8742 | |
| 8743 | DCHECK(false) << "Remaining gap: " << gap_start << " to " << gap_end |
| 8744 | << " after checking " << kGapsToCheck; |
| 8745 | } |
| 8746 | |
| 8747 | static constexpr uint32_t kNoOffset = static_cast<uint32_t>(-1); |
| 8748 | |
| 8749 | uint32_t gap4_offset_ = kNoOffset; |
| 8750 | uint32_t gap2_offset_ = kNoOffset; |
| 8751 | uint32_t gap1_offset_ = kNoOffset; |
| 8752 | }; |
| 8753 | |
| 8754 | template <size_t kSize> |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8755 | ALWAYS_INLINE |
| 8756 | MemberOffset ClassLinker::LinkFieldsHelper::AssignFieldOffset(ArtField* field, |
| 8757 | MemberOffset field_offset) { |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8758 | DCHECK_ALIGNED(field_offset.Uint32Value(), kSize); |
| 8759 | DCHECK_EQ(Primitive::ComponentSize(field->GetTypeAsPrimitiveType()), kSize); |
| 8760 | field->SetOffset(field_offset); |
| 8761 | return MemberOffset(field_offset.Uint32Value() + kSize); |
| 8762 | } |
| 8763 | |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8764 | bool ClassLinker::LinkFieldsHelper::LinkFields(ClassLinker* class_linker, |
| 8765 | Thread* self, |
| 8766 | Handle<mirror::Class> klass, |
| 8767 | bool is_static, |
| 8768 | size_t* class_size) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 8769 | self->AllowThreadSuspension(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8770 | const size_t num_fields = is_static ? klass->NumStaticFields() : klass->NumInstanceFields(); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 8771 | LengthPrefixedArray<ArtField>* const fields = is_static ? klass->GetSFieldsPtr() : |
| 8772 | klass->GetIFieldsPtr(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8773 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 8774 | // Initialize field_offset |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 8775 | MemberOffset field_offset(0); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 8776 | if (is_static) { |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8777 | field_offset = klass->GetFirstReferenceStaticFieldOffsetDuringLinking( |
| 8778 | class_linker->GetImagePointerSize()); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 8779 | } else { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 8780 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 8781 | if (super_class != nullptr) { |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 8782 | CHECK(super_class->IsResolved()) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 8783 | << klass->PrettyClass() << " " << super_class->PrettyClass(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8784 | field_offset = MemberOffset(super_class->GetObjectSize()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8785 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8786 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8787 | |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 8788 | CHECK_EQ(num_fields == 0, fields == nullptr) << klass->PrettyClass(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8789 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 8790 | // we want a relatively stable order so that adding new fields |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 8791 | // minimizes disruption of C++ version such as Class and Method. |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 8792 | // |
| 8793 | // The overall sort order order is: |
| 8794 | // 1) All object reference fields, sorted alphabetically. |
| 8795 | // 2) All java long (64-bit) integer fields, sorted alphabetically. |
| 8796 | // 3) All java double (64-bit) floating point fields, sorted alphabetically. |
| 8797 | // 4) All java int (32-bit) integer fields, sorted alphabetically. |
| 8798 | // 5) All java float (32-bit) floating point fields, sorted alphabetically. |
| 8799 | // 6) All java char (16-bit) integer fields, sorted alphabetically. |
| 8800 | // 7) All java short (16-bit) integer fields, sorted alphabetically. |
| 8801 | // 8) All java boolean (8-bit) integer fields, sorted alphabetically. |
| 8802 | // 9) All java byte (8-bit) integer fields, sorted alphabetically. |
| 8803 | // |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8804 | // (References are first to increase the chance of reference visiting |
| 8805 | // being able to take a fast path using a bitmap of references at the |
| 8806 | // start of the object, see `Class::reference_instance_offsets_`.) |
| 8807 | // |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8808 | // Once the fields are sorted in this order we will attempt to fill any gaps |
| 8809 | // that might be present in the memory layout of the structure. |
| 8810 | // Note that we shall not fill gaps between the superclass fields. |
| 8811 | |
| 8812 | // Collect fields and their "type order index" (see numbered points above). |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 8813 | const char* old_no_suspend_cause = self->StartAssertNoThreadSuspension( |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8814 | "Using plain ArtField references"); |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8815 | constexpr size_t kStackBufferEntries = 64; // Avoid allocations for small number of fields. |
| 8816 | FieldTypeOrderAndIndex stack_buffer[kStackBufferEntries]; |
| 8817 | std::vector<FieldTypeOrderAndIndex> heap_buffer; |
| 8818 | ArrayRef<FieldTypeOrderAndIndex> sorted_fields; |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8819 | if (num_fields <= kStackBufferEntries) { |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8820 | sorted_fields = ArrayRef<FieldTypeOrderAndIndex>(stack_buffer, num_fields); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8821 | } else { |
| 8822 | heap_buffer.resize(num_fields); |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8823 | sorted_fields = ArrayRef<FieldTypeOrderAndIndex>(heap_buffer); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 8824 | } |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 8825 | size_t num_reference_fields = 0; |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8826 | size_t primitive_fields_start = num_fields; |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8827 | DCHECK_LE(num_fields, 1u << 16); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8828 | for (size_t i = 0; i != num_fields; ++i) { |
| 8829 | ArtField* field = &fields->At(i); |
| 8830 | const char* descriptor = field->GetTypeDescriptor(); |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8831 | FieldTypeOrder field_type_order = FieldTypeOrderFromFirstDescriptorCharacter(descriptor[0]); |
| 8832 | uint16_t field_index = dchecked_integral_cast<uint16_t>(i); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8833 | // Insert references to the start, other fields to the end. |
| 8834 | DCHECK_LT(num_reference_fields, primitive_fields_start); |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8835 | if (field_type_order == FieldTypeOrder::kReference) { |
| 8836 | sorted_fields[num_reference_fields] = { field_type_order, field_index }; |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8837 | ++num_reference_fields; |
| 8838 | } else { |
| 8839 | --primitive_fields_start; |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8840 | sorted_fields[primitive_fields_start] = { field_type_order, field_index }; |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8841 | } |
| 8842 | } |
| 8843 | DCHECK_EQ(num_reference_fields, primitive_fields_start); |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 8844 | |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8845 | // Reference fields are already sorted by field index (and dex field index). |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8846 | DCHECK(std::is_sorted( |
| 8847 | sorted_fields.begin(), |
| 8848 | sorted_fields.begin() + num_reference_fields, |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8849 | [fields](const auto& lhs, const auto& rhs) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 8850 | ArtField* lhs_field = &fields->At(lhs.field_index); |
| 8851 | ArtField* rhs_field = &fields->At(rhs.field_index); |
| 8852 | CHECK_EQ(lhs_field->GetTypeAsPrimitiveType(), Primitive::kPrimNot); |
| 8853 | CHECK_EQ(rhs_field->GetTypeAsPrimitiveType(), Primitive::kPrimNot); |
| 8854 | CHECK_EQ(lhs_field->GetDexFieldIndex() < rhs_field->GetDexFieldIndex(), |
| 8855 | lhs.field_index < rhs.field_index); |
| 8856 | return lhs_field->GetDexFieldIndex() < rhs_field->GetDexFieldIndex(); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8857 | })); |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8858 | // 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] | 8859 | DCHECK(std::is_sorted( |
| 8860 | sorted_fields.begin() + primitive_fields_start, |
| 8861 | sorted_fields.end(), |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8862 | [fields](const auto& lhs, const auto& rhs) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 8863 | ArtField* lhs_field = &fields->At(lhs.field_index); |
| 8864 | ArtField* rhs_field = &fields->At(rhs.field_index); |
| 8865 | CHECK_NE(lhs_field->GetTypeAsPrimitiveType(), Primitive::kPrimNot); |
| 8866 | CHECK_NE(rhs_field->GetTypeAsPrimitiveType(), Primitive::kPrimNot); |
| 8867 | CHECK_EQ(lhs_field->GetDexFieldIndex() > rhs_field->GetDexFieldIndex(), |
| 8868 | lhs.field_index > rhs.field_index); |
| 8869 | return lhs.field_index > rhs.field_index; |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8870 | })); |
| 8871 | // Sort the primitive fields by the field type order, then field index. |
| 8872 | std::sort(sorted_fields.begin() + primitive_fields_start, |
| 8873 | sorted_fields.end(), |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8874 | [](const auto& lhs, const auto& rhs) { |
| 8875 | if (lhs.field_type_order != rhs.field_type_order) { |
| 8876 | return lhs.field_type_order < rhs.field_type_order; |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8877 | } else { |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8878 | return lhs.field_index < rhs.field_index; |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8879 | } |
| 8880 | }); |
| 8881 | // Primitive fields are now sorted by field size (descending), then type, then field index. |
| 8882 | DCHECK(std::is_sorted( |
| 8883 | sorted_fields.begin() + primitive_fields_start, |
| 8884 | sorted_fields.end(), |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8885 | [fields](const auto& lhs, const auto& rhs) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 8886 | ArtField* lhs_field = &fields->At(lhs.field_index); |
| 8887 | ArtField* rhs_field = &fields->At(rhs.field_index); |
| 8888 | Primitive::Type lhs_type = lhs_field->GetTypeAsPrimitiveType(); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8889 | CHECK_NE(lhs_type, Primitive::kPrimNot); |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8890 | Primitive::Type rhs_type = rhs_field->GetTypeAsPrimitiveType(); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8891 | CHECK_NE(rhs_type, Primitive::kPrimNot); |
| 8892 | if (lhs_type != rhs_type) { |
| 8893 | size_t lhs_size = Primitive::ComponentSize(lhs_type); |
| 8894 | size_t rhs_size = Primitive::ComponentSize(rhs_type); |
| 8895 | return (lhs_size != rhs_size) ? (lhs_size > rhs_size) : (lhs_type < rhs_type); |
| 8896 | } else { |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8897 | return lhs_field->GetDexFieldIndex() < rhs_field->GetDexFieldIndex(); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8898 | } |
| 8899 | })); |
| 8900 | |
| 8901 | // Process reference fields. |
| 8902 | FieldGaps field_gaps; |
| 8903 | size_t index = 0u; |
| 8904 | if (num_reference_fields != 0u) { |
| 8905 | constexpr size_t kReferenceSize = sizeof(mirror::HeapReference<mirror::Object>); |
| 8906 | field_offset = field_gaps.AlignFieldOffset<kReferenceSize>(field_offset); |
| 8907 | for (; index != num_reference_fields; ++index) { |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8908 | ArtField* field = &fields->At(sorted_fields[index].field_index); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8909 | field_offset = AssignFieldOffset<kReferenceSize>(field, field_offset); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 8910 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 8911 | } |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8912 | // Process 64-bit fields. |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8913 | if (index != num_fields && |
| 8914 | sorted_fields[index].field_type_order <= FieldTypeOrder::kLast64BitType) { |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8915 | field_offset = field_gaps.AlignFieldOffset<8u>(field_offset); |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8916 | while (index != num_fields && |
| 8917 | sorted_fields[index].field_type_order <= FieldTypeOrder::kLast64BitType) { |
| 8918 | ArtField* field = &fields->At(sorted_fields[index].field_index); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8919 | field_offset = AssignFieldOffset<8u>(field, field_offset); |
| 8920 | ++index; |
Mathieu Chartier | 5565029 | 2020-09-14 12:21:04 -0700 | [diff] [blame] | 8921 | } |
Mathieu Chartier | 5565029 | 2020-09-14 12:21:04 -0700 | [diff] [blame] | 8922 | } |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8923 | // Process 32-bit fields. |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8924 | if (index != num_fields && |
| 8925 | sorted_fields[index].field_type_order <= FieldTypeOrder::kLast32BitType) { |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8926 | field_offset = field_gaps.AlignFieldOffset<4u>(field_offset); |
| 8927 | if (field_gaps.HasGap<4u>()) { |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8928 | ArtField* field = &fields->At(sorted_fields[index].field_index); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8929 | AssignFieldOffset<4u>(field, field_gaps.ReleaseGap<4u>()); // Ignore return value. |
| 8930 | ++index; |
| 8931 | DCHECK(!field_gaps.HasGap<4u>()); // There can be only one gap for a 32-bit field. |
| 8932 | } |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8933 | while (index != num_fields && |
| 8934 | sorted_fields[index].field_type_order <= FieldTypeOrder::kLast32BitType) { |
| 8935 | ArtField* field = &fields->At(sorted_fields[index].field_index); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8936 | field_offset = AssignFieldOffset<4u>(field, field_offset); |
| 8937 | ++index; |
| 8938 | } |
| 8939 | } |
| 8940 | // Process 16-bit fields. |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8941 | if (index != num_fields && |
| 8942 | sorted_fields[index].field_type_order <= FieldTypeOrder::kLast16BitType) { |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8943 | field_offset = field_gaps.AlignFieldOffset<2u>(field_offset); |
| 8944 | while (index != num_fields && |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8945 | sorted_fields[index].field_type_order <= FieldTypeOrder::kLast16BitType && |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8946 | field_gaps.HasGap<2u>()) { |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8947 | ArtField* field = &fields->At(sorted_fields[index].field_index); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8948 | AssignFieldOffset<2u>(field, field_gaps.ReleaseGap<2u>()); // Ignore return value. |
| 8949 | ++index; |
| 8950 | } |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8951 | while (index != num_fields && |
| 8952 | sorted_fields[index].field_type_order <= FieldTypeOrder::kLast16BitType) { |
| 8953 | ArtField* field = &fields->At(sorted_fields[index].field_index); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8954 | field_offset = AssignFieldOffset<2u>(field, field_offset); |
| 8955 | ++index; |
| 8956 | } |
| 8957 | } |
| 8958 | // Process 8-bit fields. |
| 8959 | for (; index != num_fields && field_gaps.HasGap<1u>(); ++index) { |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8960 | ArtField* field = &fields->At(sorted_fields[index].field_index); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8961 | AssignFieldOffset<1u>(field, field_gaps.ReleaseGap<1u>()); // Ignore return value. |
| 8962 | } |
| 8963 | for (; index != num_fields; ++index) { |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8964 | ArtField* field = &fields->At(sorted_fields[index].field_index); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8965 | field_offset = AssignFieldOffset<1u>(field, field_offset); |
| 8966 | } |
| 8967 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 8968 | self->EndAssertNoThreadSuspension(old_no_suspend_cause); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 8969 | |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 8970 | // 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] | 8971 | DCHECK_IMPLIES(class_linker->init_done_, !klass->DescriptorEquals("Ljava/lang/ref/Reference;")); |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8972 | if (!is_static && |
| 8973 | UNLIKELY(!class_linker->init_done_) && |
| 8974 | klass->DescriptorEquals("Ljava/lang/ref/Reference;")) { |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 8975 | // We know there are no non-reference fields in the Reference classes, and we know |
| 8976 | // that 'referent' is alphabetically last, so this is easy... |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 8977 | CHECK_EQ(num_reference_fields, num_fields) << klass->PrettyClass(); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 8978 | CHECK_STREQ(fields->At(num_fields - 1).GetName(), "referent") |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 8979 | << klass->PrettyClass(); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 8980 | --num_reference_fields; |
| 8981 | } |
| 8982 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 8983 | size_t size = field_offset.Uint32Value(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8984 | // Update klass |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 8985 | if (is_static) { |
| 8986 | klass->SetNumReferenceStaticFields(num_reference_fields); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 8987 | *class_size = size; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 8988 | } else { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8989 | klass->SetNumReferenceInstanceFields(num_reference_fields); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 8990 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 8991 | if (num_reference_fields == 0 || super_class == nullptr) { |
| 8992 | // 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] | 8993 | // super_class is null iff the class is java.lang.Object. |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 8994 | if (super_class == nullptr || |
| 8995 | (super_class->GetClassFlags() & mirror::kClassFlagNoReferenceFields) != 0) { |
| 8996 | klass->SetClassFlags(klass->GetClassFlags() | mirror::kClassFlagNoReferenceFields); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 8997 | } |
| 8998 | } |
| 8999 | if (kIsDebugBuild) { |
| 9000 | DCHECK_EQ(super_class == nullptr, klass->DescriptorEquals("Ljava/lang/Object;")); |
| 9001 | size_t total_reference_instance_fields = 0; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 9002 | ObjPtr<mirror::Class> cur_super = klass.Get(); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 9003 | while (cur_super != nullptr) { |
| 9004 | total_reference_instance_fields += cur_super->NumReferenceInstanceFieldsDuringLinking(); |
| 9005 | cur_super = cur_super->GetSuperClass(); |
| 9006 | } |
| 9007 | if (super_class == nullptr) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 9008 | CHECK_EQ(total_reference_instance_fields, 1u) << klass->PrettyDescriptor(); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 9009 | } else { |
| 9010 | // Check that there is at least num_reference_fields other than Object.class. |
| 9011 | CHECK_GE(total_reference_instance_fields, 1u + num_reference_fields) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 9012 | << klass->PrettyClass(); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 9013 | } |
| 9014 | } |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 9015 | if (!klass->IsVariableSize()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 9016 | std::string temp; |
| 9017 | DCHECK_GE(size, sizeof(mirror::Object)) << klass->GetDescriptor(&temp); |
| 9018 | size_t previous_size = klass->GetObjectSize(); |
| 9019 | if (previous_size != 0) { |
| 9020 | // Make sure that we didn't originally have an incorrect size. |
| 9021 | CHECK_EQ(previous_size, size) << klass->GetDescriptor(&temp); |
Mathieu Chartier | 79b4f38 | 2013-10-23 15:21:37 -0700 | [diff] [blame] | 9022 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 9023 | klass->SetObjectSize(size); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 9024 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 9025 | } |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 9026 | |
| 9027 | if (kIsDebugBuild) { |
| 9028 | // Make sure that the fields array is ordered by name but all reference |
| 9029 | // offsets are at the beginning as far as alignment allows. |
| 9030 | MemberOffset start_ref_offset = is_static |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 9031 | ? klass->GetFirstReferenceStaticFieldOffsetDuringLinking(class_linker->image_pointer_size_) |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 9032 | : klass->GetFirstReferenceInstanceFieldOffset(); |
| 9033 | MemberOffset end_ref_offset(start_ref_offset.Uint32Value() + |
| 9034 | num_reference_fields * |
| 9035 | sizeof(mirror::HeapReference<mirror::Object>)); |
| 9036 | MemberOffset current_ref_offset = start_ref_offset; |
| 9037 | for (size_t i = 0; i < num_fields; i++) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 9038 | ArtField* field = &fields->At(i); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 9039 | VLOG(class_linker) << "LinkFields: " << (is_static ? "static" : "instance") |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 9040 | << " class=" << klass->PrettyClass() << " field=" << field->PrettyField() |
| 9041 | << " offset=" << field->GetOffsetDuringLinking(); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 9042 | if (i != 0) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 9043 | ArtField* const prev_field = &fields->At(i - 1); |
Vladimir Marko | 7a7c1db | 2014-11-17 15:13:34 +0000 | [diff] [blame] | 9044 | // NOTE: The field names can be the same. This is not possible in the Java language |
| 9045 | // 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] | 9046 | DCHECK_LE(strcmp(prev_field->GetName(), field->GetName()), 0); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 9047 | } |
| 9048 | Primitive::Type type = field->GetTypeAsPrimitiveType(); |
| 9049 | bool is_primitive = type != Primitive::kPrimNot; |
| 9050 | if (klass->DescriptorEquals("Ljava/lang/ref/Reference;") && |
| 9051 | strcmp("referent", field->GetName()) == 0) { |
| 9052 | is_primitive = true; // We lied above, so we have to expect a lie here. |
| 9053 | } |
| 9054 | MemberOffset offset = field->GetOffsetDuringLinking(); |
| 9055 | if (is_primitive) { |
| 9056 | if (offset.Uint32Value() < end_ref_offset.Uint32Value()) { |
| 9057 | // Shuffled before references. |
| 9058 | size_t type_size = Primitive::ComponentSize(type); |
| 9059 | CHECK_LT(type_size, sizeof(mirror::HeapReference<mirror::Object>)); |
| 9060 | CHECK_LT(offset.Uint32Value(), start_ref_offset.Uint32Value()); |
| 9061 | CHECK_LE(offset.Uint32Value() + type_size, start_ref_offset.Uint32Value()); |
| 9062 | CHECK(!IsAligned<sizeof(mirror::HeapReference<mirror::Object>)>(offset.Uint32Value())); |
| 9063 | } |
| 9064 | } else { |
| 9065 | CHECK_EQ(current_ref_offset.Uint32Value(), offset.Uint32Value()); |
| 9066 | current_ref_offset = MemberOffset(current_ref_offset.Uint32Value() + |
| 9067 | sizeof(mirror::HeapReference<mirror::Object>)); |
| 9068 | } |
| 9069 | } |
| 9070 | CHECK_EQ(current_ref_offset.Uint32Value(), end_ref_offset.Uint32Value()); |
| 9071 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 9072 | return true; |
| 9073 | } |
| 9074 | |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 9075 | bool ClassLinker::LinkInstanceFields(Thread* self, Handle<mirror::Class> klass) { |
| 9076 | CHECK(klass != nullptr); |
| 9077 | return LinkFieldsHelper::LinkFields(this, self, klass, false, nullptr); |
| 9078 | } |
| 9079 | |
| 9080 | bool ClassLinker::LinkStaticFields(Thread* self, Handle<mirror::Class> klass, size_t* class_size) { |
| 9081 | CHECK(klass != nullptr); |
| 9082 | return LinkFieldsHelper::LinkFields(this, self, klass, true, class_size); |
| 9083 | } |
| 9084 | |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 9085 | // Set the bitmap of reference instance field offsets. |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 9086 | void ClassLinker::CreateReferenceInstanceOffsets(Handle<mirror::Class> klass) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 9087 | uint32_t reference_offsets = 0; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 9088 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 9089 | // 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] | 9090 | if (super_class != nullptr) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 9091 | reference_offsets = super_class->GetReferenceInstanceOffsets(); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 9092 | // Compute reference offsets unless our superclass overflowed. |
| 9093 | if (reference_offsets != mirror::Class::kClassWalkSuper) { |
| 9094 | size_t num_reference_fields = klass->NumReferenceInstanceFieldsDuringLinking(); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 9095 | if (num_reference_fields != 0u) { |
| 9096 | // All of the fields that contain object references are guaranteed be grouped in memory |
| 9097 | // starting at an appropriately aligned address after super class object data. |
| 9098 | uint32_t start_offset = RoundUp(super_class->GetObjectSize(), |
| 9099 | sizeof(mirror::HeapReference<mirror::Object>)); |
| 9100 | uint32_t start_bit = (start_offset - mirror::kObjectHeaderSize) / |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 9101 | sizeof(mirror::HeapReference<mirror::Object>); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 9102 | if (start_bit + num_reference_fields > 32) { |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 9103 | reference_offsets = mirror::Class::kClassWalkSuper; |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 9104 | } else { |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 9105 | reference_offsets |= (0xffffffffu << start_bit) & |
| 9106 | (0xffffffffu >> (32 - (start_bit + num_reference_fields))); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 9107 | } |
| 9108 | } |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 9109 | } |
| 9110 | } |
Mingyao Yang | faff0f0 | 2014-09-10 12:03:22 -0700 | [diff] [blame] | 9111 | klass->SetReferenceInstanceOffsets(reference_offsets); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 9112 | } |
| 9113 | |
Vladimir Marko | 18090d1 | 2018-06-01 16:53:12 +0100 | [diff] [blame] | 9114 | ObjPtr<mirror::String> ClassLinker::DoResolveString(dex::StringIndex string_idx, |
| 9115 | ObjPtr<mirror::DexCache> dex_cache) { |
| 9116 | StackHandleScope<1> hs(Thread::Current()); |
| 9117 | Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(dex_cache)); |
| 9118 | return DoResolveString(string_idx, h_dex_cache); |
| 9119 | } |
| 9120 | |
| 9121 | ObjPtr<mirror::String> ClassLinker::DoResolveString(dex::StringIndex string_idx, |
| 9122 | Handle<mirror::DexCache> dex_cache) { |
Vladimir Marko | a64b52d | 2017-12-08 16:27:49 +0000 | [diff] [blame] | 9123 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 9124 | uint32_t utf16_length; |
| 9125 | const char* utf8_data = dex_file.StringDataAndUtf16LengthByIdx(string_idx, &utf16_length); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 9126 | ObjPtr<mirror::String> string = intern_table_->InternStrong(utf16_length, utf8_data); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 9127 | if (string != nullptr) { |
| 9128 | dex_cache->SetResolvedString(string_idx, string); |
| 9129 | } |
Vladimir Marko | 28e012a | 2017-12-07 11:22:59 +0000 | [diff] [blame] | 9130 | return string; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 9131 | } |
| 9132 | |
Vladimir Marko | 18090d1 | 2018-06-01 16:53:12 +0100 | [diff] [blame] | 9133 | ObjPtr<mirror::String> ClassLinker::DoLookupString(dex::StringIndex string_idx, |
| 9134 | ObjPtr<mirror::DexCache> dex_cache) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 9135 | DCHECK(dex_cache != nullptr); |
Vladimir Marko | a64b52d | 2017-12-08 16:27:49 +0000 | [diff] [blame] | 9136 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 9137 | uint32_t utf16_length; |
| 9138 | const char* utf8_data = dex_file.StringDataAndUtf16LengthByIdx(string_idx, &utf16_length); |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 9139 | ObjPtr<mirror::String> string = |
| 9140 | intern_table_->LookupStrong(Thread::Current(), utf16_length, utf8_data); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 9141 | if (string != nullptr) { |
| 9142 | dex_cache->SetResolvedString(string_idx, string); |
| 9143 | } |
Vladimir Marko | 28e012a | 2017-12-07 11:22:59 +0000 | [diff] [blame] | 9144 | return string; |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 9145 | } |
| 9146 | |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 9147 | ObjPtr<mirror::Class> ClassLinker::DoLookupResolvedType(dex::TypeIndex type_idx, |
Vladimir Marko | 09c5ca4 | 2018-05-31 15:15:31 +0100 | [diff] [blame] | 9148 | ObjPtr<mirror::Class> referrer) { |
| 9149 | return DoLookupResolvedType(type_idx, referrer->GetDexCache(), referrer->GetClassLoader()); |
| 9150 | } |
| 9151 | |
| 9152 | ObjPtr<mirror::Class> ClassLinker::DoLookupResolvedType(dex::TypeIndex type_idx, |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 9153 | ObjPtr<mirror::DexCache> dex_cache, |
| 9154 | ObjPtr<mirror::ClassLoader> class_loader) { |
Nicolas Geoffray | ab91eef | 2021-09-14 09:48:51 +0100 | [diff] [blame] | 9155 | DCHECK(dex_cache->GetClassLoader() == class_loader); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 9156 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
| 9157 | const char* descriptor = dex_file.StringByTypeIdx(type_idx); |
Vladimir Marko | c63d967 | 2021-03-31 15:50:39 +0100 | [diff] [blame] | 9158 | ObjPtr<mirror::Class> type = LookupResolvedType(descriptor, class_loader); |
| 9159 | if (type != nullptr) { |
| 9160 | DCHECK(type->IsResolved()); |
| 9161 | dex_cache->SetResolvedType(type_idx, type); |
| 9162 | } |
| 9163 | return type; |
| 9164 | } |
| 9165 | |
| 9166 | ObjPtr<mirror::Class> ClassLinker::LookupResolvedType(const char* descriptor, |
| 9167 | ObjPtr<mirror::ClassLoader> class_loader) { |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 9168 | DCHECK_NE(*descriptor, '\0') << "descriptor is empty string"; |
| 9169 | ObjPtr<mirror::Class> type = nullptr; |
| 9170 | if (descriptor[1] == '\0') { |
| 9171 | // only the descriptors of primitive types should be 1 character long, also avoid class lookup |
| 9172 | // for primitive classes that aren't backed by dex files. |
Vladimir Marko | 9186b18 | 2018-11-06 14:55:54 +0000 | [diff] [blame] | 9173 | type = LookupPrimitiveClass(descriptor[0]); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 9174 | } else { |
| 9175 | Thread* const self = Thread::Current(); |
| 9176 | DCHECK(self != nullptr); |
| 9177 | const size_t hash = ComputeModifiedUtf8Hash(descriptor); |
| 9178 | // Find the class in the loaded classes table. |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 9179 | type = LookupClass(self, descriptor, hash, class_loader); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 9180 | } |
Vladimir Marko | c63d967 | 2021-03-31 15:50:39 +0100 | [diff] [blame] | 9181 | return (type != nullptr && type->IsResolved()) ? type : nullptr; |
Mathieu Chartier | b890130 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 9182 | } |
| 9183 | |
Andreas Gampe | b0625e0 | 2019-05-01 12:43:31 -0700 | [diff] [blame] | 9184 | template <typename RefType> |
| 9185 | ObjPtr<mirror::Class> ClassLinker::DoResolveType(dex::TypeIndex type_idx, RefType referrer) { |
Vladimir Marko | 09c5ca4 | 2018-05-31 15:15:31 +0100 | [diff] [blame] | 9186 | StackHandleScope<2> hs(Thread::Current()); |
| 9187 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache())); |
| 9188 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(referrer->GetClassLoader())); |
| 9189 | return DoResolveType(type_idx, dex_cache, class_loader); |
| 9190 | } |
| 9191 | |
Andreas Gampe | 4835d21 | 2018-11-21 14:55:10 -0800 | [diff] [blame] | 9192 | // Instantiate the above. |
| 9193 | template ObjPtr<mirror::Class> ClassLinker::DoResolveType(dex::TypeIndex type_idx, |
| 9194 | ArtField* referrer); |
| 9195 | template ObjPtr<mirror::Class> ClassLinker::DoResolveType(dex::TypeIndex type_idx, |
| 9196 | ArtMethod* referrer); |
| 9197 | template ObjPtr<mirror::Class> ClassLinker::DoResolveType(dex::TypeIndex type_idx, |
| 9198 | ObjPtr<mirror::Class> referrer); |
| 9199 | |
Vladimir Marko | 09c5ca4 | 2018-05-31 15:15:31 +0100 | [diff] [blame] | 9200 | ObjPtr<mirror::Class> ClassLinker::DoResolveType(dex::TypeIndex type_idx, |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 9201 | Handle<mirror::DexCache> dex_cache, |
| 9202 | Handle<mirror::ClassLoader> class_loader) { |
Nicolas Geoffray | ab91eef | 2021-09-14 09:48:51 +0100 | [diff] [blame] | 9203 | DCHECK(dex_cache->GetClassLoader() == class_loader.Get()); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 9204 | Thread* self = Thread::Current(); |
| 9205 | const char* descriptor = dex_cache->GetDexFile()->StringByTypeIdx(type_idx); |
| 9206 | ObjPtr<mirror::Class> resolved = FindClass(self, descriptor, class_loader); |
| 9207 | if (resolved != nullptr) { |
| 9208 | // TODO: we used to throw here if resolved's class loader was not the |
| 9209 | // boot class loader. This was to permit different classes with the |
| 9210 | // same name to be loaded simultaneously by different loaders |
| 9211 | dex_cache->SetResolvedType(type_idx, resolved); |
| 9212 | } else { |
| 9213 | CHECK(self->IsExceptionPending()) |
| 9214 | << "Expected pending exception for failed resolution of: " << descriptor; |
| 9215 | // Convert a ClassNotFoundException to a NoClassDefFoundError. |
| 9216 | StackHandleScope<1> hs(self); |
| 9217 | Handle<mirror::Throwable> cause(hs.NewHandle(self->GetException())); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 9218 | if (cause->InstanceOf(GetClassRoot(ClassRoot::kJavaLangClassNotFoundException, this))) { |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 9219 | DCHECK(resolved == nullptr); // No Handle needed to preserve resolved. |
| 9220 | self->ClearException(); |
| 9221 | ThrowNoClassDefFoundError("Failed resolution of: %s", descriptor); |
| 9222 | self->GetException()->SetCause(cause.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 9223 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 9224 | } |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 9225 | DCHECK((resolved == nullptr) || resolved->IsResolved()) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 9226 | << resolved->PrettyDescriptor() << " " << resolved->GetStatus(); |
Vladimir Marko | 28e012a | 2017-12-07 11:22:59 +0000 | [diff] [blame] | 9227 | return resolved; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 9228 | } |
| 9229 | |
Nicolas Geoffray | ea179f4 | 2018-02-08 22:30:18 +0000 | [diff] [blame] | 9230 | ArtMethod* ClassLinker::FindResolvedMethod(ObjPtr<mirror::Class> klass, |
| 9231 | ObjPtr<mirror::DexCache> dex_cache, |
| 9232 | ObjPtr<mirror::ClassLoader> class_loader, |
| 9233 | uint32_t method_idx) { |
Nicolas Geoffray | ab91eef | 2021-09-14 09:48:51 +0100 | [diff] [blame] | 9234 | DCHECK(dex_cache->GetClassLoader() == class_loader); |
Nicolas Geoffray | ea179f4 | 2018-02-08 22:30:18 +0000 | [diff] [blame] | 9235 | // Search for the method using dex_cache and method_idx. The Class::Find*Method() |
| 9236 | // functions can optimize the search if the dex_cache is the same as the DexCache |
| 9237 | // of the class, with fall-back to name and signature search otherwise. |
| 9238 | ArtMethod* resolved = nullptr; |
| 9239 | if (klass->IsInterface()) { |
| 9240 | resolved = klass->FindInterfaceMethod(dex_cache, method_idx, image_pointer_size_); |
| 9241 | } else { |
| 9242 | resolved = klass->FindClassMethod(dex_cache, method_idx, image_pointer_size_); |
| 9243 | } |
| 9244 | DCHECK(resolved == nullptr || resolved->GetDeclaringClassUnchecked() != nullptr); |
David Brazdil | 8ce3bfa | 2018-03-12 18:01:18 +0000 | [diff] [blame] | 9245 | if (resolved != nullptr && |
Nicolas Geoffray | fedff51 | 2021-02-07 21:36:33 +0000 | [diff] [blame] | 9246 | // We pass AccessMethod::kNone instead of kLinking to not warn yet on the |
| 9247 | // access, as we'll be looking if the method can be accessed through an |
| 9248 | // interface. |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 9249 | hiddenapi::ShouldDenyAccessToMember(resolved, |
| 9250 | hiddenapi::AccessContext(class_loader, dex_cache), |
Nicolas Geoffray | fedff51 | 2021-02-07 21:36:33 +0000 | [diff] [blame] | 9251 | hiddenapi::AccessMethod::kNone)) { |
| 9252 | // The resolved method that we have found cannot be accessed due to |
| 9253 | // hiddenapi (typically it is declared up the hierarchy and is not an SDK |
| 9254 | // 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] | 9255 | // part of the SDK. |
| 9256 | ArtMethod* itf_method = klass->FindAccessibleInterfaceMethod(resolved, image_pointer_size_); |
Nicolas Geoffray | fedff51 | 2021-02-07 21:36:33 +0000 | [diff] [blame] | 9257 | if (itf_method == nullptr) { |
| 9258 | // No interface method. Call ShouldDenyAccessToMember again but this time |
| 9259 | // with AccessMethod::kLinking to ensure that an appropriate warning is |
| 9260 | // logged. |
| 9261 | hiddenapi::ShouldDenyAccessToMember(resolved, |
| 9262 | hiddenapi::AccessContext(class_loader, dex_cache), |
| 9263 | hiddenapi::AccessMethod::kLinking); |
| 9264 | resolved = nullptr; |
| 9265 | } else { |
| 9266 | // We found an interface method that is accessible, continue with the resolved method. |
| 9267 | } |
David Brazdil | 8ce3bfa | 2018-03-12 18:01:18 +0000 | [diff] [blame] | 9268 | } |
Nicolas Geoffray | ea179f4 | 2018-02-08 22:30:18 +0000 | [diff] [blame] | 9269 | if (resolved != nullptr) { |
| 9270 | // In case of jmvti, the dex file gets verified before being registered, so first |
| 9271 | // check if it's registered before checking class tables. |
| 9272 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Santiago Aboy Solanes | 6cdabe1 | 2022-02-18 15:27:43 +0000 | [diff] [blame] | 9273 | DCHECK_IMPLIES( |
| 9274 | IsDexFileRegistered(Thread::Current(), dex_file), |
| 9275 | FindClassTable(Thread::Current(), dex_cache) == ClassTableForClassLoader(class_loader)) |
Nicolas Geoffray | ea179f4 | 2018-02-08 22:30:18 +0000 | [diff] [blame] | 9276 | << "DexFile referrer: " << dex_file.GetLocation() |
| 9277 | << " ClassLoader: " << DescribeLoaders(class_loader, ""); |
| 9278 | // 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] | 9279 | dex_cache->SetResolvedMethod(method_idx, resolved); |
Nicolas Geoffray | befa309 | 2018-02-22 14:50:01 +0000 | [diff] [blame] | 9280 | // Disable the following invariant check as the verifier breaks it. b/73760543 |
| 9281 | // const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx); |
| 9282 | // DCHECK(LookupResolvedType(method_id.class_idx_, dex_cache, class_loader) != nullptr) |
| 9283 | // << "Method: " << resolved->PrettyMethod() << ", " |
| 9284 | // << "Class: " << klass->PrettyClass() << " (" << klass->GetStatus() << "), " |
| 9285 | // << "DexFile referrer: " << dex_file.GetLocation(); |
Nicolas Geoffray | ea179f4 | 2018-02-08 22:30:18 +0000 | [diff] [blame] | 9286 | } |
| 9287 | return resolved; |
| 9288 | } |
| 9289 | |
David Brazdil | 4525e0b | 2018-04-05 16:57:32 +0100 | [diff] [blame] | 9290 | // Returns true if `method` is either null or hidden. |
| 9291 | // Does not print any warnings if it is hidden. |
| 9292 | static bool CheckNoSuchMethod(ArtMethod* method, |
| 9293 | ObjPtr<mirror::DexCache> dex_cache, |
| 9294 | ObjPtr<mirror::ClassLoader> class_loader) |
| 9295 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Nicolas Geoffray | c07f488 | 2021-09-13 09:20:33 +0100 | [diff] [blame] | 9296 | DCHECK(dex_cache->GetClassLoader().Ptr() == class_loader.Ptr()); |
David Brazdil | 4525e0b | 2018-04-05 16:57:32 +0100 | [diff] [blame] | 9297 | return method == nullptr || |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 9298 | hiddenapi::ShouldDenyAccessToMember(method, |
| 9299 | hiddenapi::AccessContext(class_loader, dex_cache), |
| 9300 | hiddenapi::AccessMethod::kNone); // no warnings |
David Brazdil | 4525e0b | 2018-04-05 16:57:32 +0100 | [diff] [blame] | 9301 | } |
| 9302 | |
| 9303 | ArtMethod* ClassLinker::FindIncompatibleMethod(ObjPtr<mirror::Class> klass, |
| 9304 | ObjPtr<mirror::DexCache> dex_cache, |
| 9305 | ObjPtr<mirror::ClassLoader> class_loader, |
| 9306 | uint32_t method_idx) { |
Nicolas Geoffray | ab91eef | 2021-09-14 09:48:51 +0100 | [diff] [blame] | 9307 | DCHECK(dex_cache->GetClassLoader() == class_loader); |
David Brazdil | 4525e0b | 2018-04-05 16:57:32 +0100 | [diff] [blame] | 9308 | if (klass->IsInterface()) { |
| 9309 | ArtMethod* method = klass->FindClassMethod(dex_cache, method_idx, image_pointer_size_); |
| 9310 | return CheckNoSuchMethod(method, dex_cache, class_loader) ? nullptr : method; |
| 9311 | } else { |
| 9312 | // If there was an interface method with the same signature, we would have |
| 9313 | // found it in the "copied" methods. Only DCHECK that the interface method |
| 9314 | // really does not exist. |
| 9315 | if (kIsDebugBuild) { |
| 9316 | ArtMethod* method = |
| 9317 | klass->FindInterfaceMethod(dex_cache, method_idx, image_pointer_size_); |
Nicolas Geoffray | 2464218 | 2022-02-14 14:40:51 +0000 | [diff] [blame] | 9318 | CHECK(CheckNoSuchMethod(method, dex_cache, class_loader) || |
| 9319 | (klass->FindAccessibleInterfaceMethod(method, image_pointer_size_) == nullptr)); |
David Brazdil | 4525e0b | 2018-04-05 16:57:32 +0100 | [diff] [blame] | 9320 | } |
| 9321 | return nullptr; |
| 9322 | } |
| 9323 | } |
| 9324 | |
Vladimir Marko | 8901119 | 2017-12-11 13:45:05 +0000 | [diff] [blame] | 9325 | ArtMethod* ClassLinker::ResolveMethodWithoutInvokeType(uint32_t method_idx, |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 9326 | Handle<mirror::DexCache> dex_cache, |
| 9327 | Handle<mirror::ClassLoader> class_loader) { |
Nicolas Geoffray | ab91eef | 2021-09-14 09:48:51 +0100 | [diff] [blame] | 9328 | DCHECK(dex_cache->GetClassLoader() == class_loader.Get()); |
David Srbecky | 5de5efe | 2021-02-15 21:23:00 +0000 | [diff] [blame] | 9329 | ArtMethod* resolved = dex_cache->GetResolvedMethod(method_idx); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 9330 | Thread::PoisonObjectPointersIfDebug(); |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 9331 | if (resolved != nullptr) { |
| 9332 | DCHECK(!resolved->IsRuntimeMethod()); |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 9333 | DCHECK(resolved->GetDeclaringClassUnchecked() != nullptr) << resolved->GetDexMethodIndex(); |
| 9334 | return resolved; |
| 9335 | } |
| 9336 | // Fail, get the declaring class. |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9337 | const dex::MethodId& method_id = dex_cache->GetDexFile()->GetMethodId(method_idx); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 9338 | 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] | 9339 | if (klass == nullptr) { |
| 9340 | Thread::Current()->AssertPendingException(); |
| 9341 | return nullptr; |
| 9342 | } |
| 9343 | if (klass->IsInterface()) { |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9344 | resolved = klass->FindInterfaceMethod(dex_cache.Get(), method_idx, image_pointer_size_); |
| 9345 | } else { |
| 9346 | resolved = klass->FindClassMethod(dex_cache.Get(), method_idx, image_pointer_size_); |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 9347 | } |
David Brazdil | 8ce3bfa | 2018-03-12 18:01:18 +0000 | [diff] [blame] | 9348 | if (resolved != nullptr && |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 9349 | hiddenapi::ShouldDenyAccessToMember( |
| 9350 | resolved, |
| 9351 | hiddenapi::AccessContext(class_loader.Get(), dex_cache.Get()), |
| 9352 | hiddenapi::AccessMethod::kLinking)) { |
David Brazdil | 8ce3bfa | 2018-03-12 18:01:18 +0000 | [diff] [blame] | 9353 | resolved = nullptr; |
| 9354 | } |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 9355 | return resolved; |
| 9356 | } |
| 9357 | |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 9358 | ArtField* ClassLinker::LookupResolvedField(uint32_t field_idx, |
| 9359 | ObjPtr<mirror::DexCache> dex_cache, |
| 9360 | ObjPtr<mirror::ClassLoader> class_loader, |
| 9361 | bool is_static) { |
Nicolas Geoffray | c07f488 | 2021-09-13 09:20:33 +0100 | [diff] [blame] | 9362 | DCHECK(dex_cache->GetClassLoader().Ptr() == class_loader.Ptr()); |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 9363 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9364 | const dex::FieldId& field_id = dex_file.GetFieldId(field_idx); |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 9365 | ObjPtr<mirror::Class> klass = dex_cache->GetResolvedType(field_id.class_idx_); |
| 9366 | if (klass == nullptr) { |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 9367 | klass = LookupResolvedType(field_id.class_idx_, dex_cache, class_loader); |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 9368 | } |
| 9369 | if (klass == nullptr) { |
| 9370 | // The class has not been resolved yet, so the field is also unresolved. |
| 9371 | return nullptr; |
| 9372 | } |
| 9373 | DCHECK(klass->IsResolved()); |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 9374 | |
David Brazdil | 1ab0fa8 | 2018-05-04 11:28:03 +0100 | [diff] [blame] | 9375 | return FindResolvedField(klass, dex_cache, class_loader, field_idx, is_static); |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 9376 | } |
| 9377 | |
Vladimir Marko | e11dd50 | 2017-12-08 14:09:45 +0000 | [diff] [blame] | 9378 | ArtField* ClassLinker::ResolveFieldJLS(uint32_t field_idx, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 9379 | Handle<mirror::DexCache> dex_cache, |
| 9380 | Handle<mirror::ClassLoader> class_loader) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 9381 | DCHECK(dex_cache != nullptr); |
Nicolas Geoffray | ab91eef | 2021-09-14 09:48:51 +0100 | [diff] [blame] | 9382 | DCHECK(dex_cache->GetClassLoader() == class_loader.Get()); |
David Srbecky | 5de5efe | 2021-02-15 21:23:00 +0000 | [diff] [blame] | 9383 | ArtField* resolved = dex_cache->GetResolvedField(field_idx); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 9384 | Thread::PoisonObjectPointersIfDebug(); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 9385 | if (resolved != nullptr) { |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 9386 | return resolved; |
| 9387 | } |
Vladimir Marko | e11dd50 | 2017-12-08 14:09:45 +0000 | [diff] [blame] | 9388 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9389 | const dex::FieldId& field_id = dex_file.GetFieldId(field_idx); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 9390 | 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] | 9391 | if (klass == nullptr) { |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 9392 | DCHECK(Thread::Current()->IsExceptionPending()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 9393 | return nullptr; |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 9394 | } |
| 9395 | |
David Brazdil | 1ab0fa8 | 2018-05-04 11:28:03 +0100 | [diff] [blame] | 9396 | resolved = FindResolvedFieldJLS(klass, dex_cache.Get(), class_loader.Get(), field_idx); |
| 9397 | if (resolved == nullptr) { |
| 9398 | const char* name = dex_file.GetFieldName(field_id); |
| 9399 | const char* type = dex_file.GetFieldTypeDescriptor(field_id); |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 9400 | ThrowNoSuchFieldError("", klass, type, name); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 9401 | } |
| 9402 | return resolved; |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 9403 | } |
| 9404 | |
David Brazdil | 1ab0fa8 | 2018-05-04 11:28:03 +0100 | [diff] [blame] | 9405 | ArtField* ClassLinker::FindResolvedField(ObjPtr<mirror::Class> klass, |
| 9406 | ObjPtr<mirror::DexCache> dex_cache, |
| 9407 | ObjPtr<mirror::ClassLoader> class_loader, |
| 9408 | uint32_t field_idx, |
| 9409 | bool is_static) { |
Nicolas Geoffray | ab91eef | 2021-09-14 09:48:51 +0100 | [diff] [blame] | 9410 | DCHECK(dex_cache->GetClassLoader() == class_loader); |
Vladimir Marko | b10668c | 2021-06-10 09:52:53 +0100 | [diff] [blame] | 9411 | ArtField* resolved = is_static ? klass->FindStaticField(dex_cache, field_idx) |
| 9412 | : klass->FindInstanceField(dex_cache, field_idx); |
David Brazdil | 1ab0fa8 | 2018-05-04 11:28:03 +0100 | [diff] [blame] | 9413 | if (resolved != nullptr && |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 9414 | hiddenapi::ShouldDenyAccessToMember(resolved, |
| 9415 | hiddenapi::AccessContext(class_loader, dex_cache), |
| 9416 | hiddenapi::AccessMethod::kLinking)) { |
David Brazdil | 1ab0fa8 | 2018-05-04 11:28:03 +0100 | [diff] [blame] | 9417 | resolved = nullptr; |
| 9418 | } |
| 9419 | |
| 9420 | if (resolved != nullptr) { |
David Srbecky | 5de5efe | 2021-02-15 21:23:00 +0000 | [diff] [blame] | 9421 | dex_cache->SetResolvedField(field_idx, resolved); |
David Brazdil | 1ab0fa8 | 2018-05-04 11:28:03 +0100 | [diff] [blame] | 9422 | } |
| 9423 | |
| 9424 | return resolved; |
| 9425 | } |
| 9426 | |
| 9427 | ArtField* ClassLinker::FindResolvedFieldJLS(ObjPtr<mirror::Class> klass, |
| 9428 | ObjPtr<mirror::DexCache> dex_cache, |
| 9429 | ObjPtr<mirror::ClassLoader> class_loader, |
| 9430 | uint32_t field_idx) { |
Nicolas Geoffray | c07f488 | 2021-09-13 09:20:33 +0100 | [diff] [blame] | 9431 | DCHECK(dex_cache->GetClassLoader().Ptr() == class_loader.Ptr()); |
Vladimir Marko | b10668c | 2021-06-10 09:52:53 +0100 | [diff] [blame] | 9432 | ArtField* resolved = klass->FindField(dex_cache, field_idx); |
David Brazdil | 1ab0fa8 | 2018-05-04 11:28:03 +0100 | [diff] [blame] | 9433 | |
| 9434 | if (resolved != nullptr && |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 9435 | hiddenapi::ShouldDenyAccessToMember(resolved, |
| 9436 | hiddenapi::AccessContext(class_loader, dex_cache), |
| 9437 | hiddenapi::AccessMethod::kLinking)) { |
David Brazdil | 1ab0fa8 | 2018-05-04 11:28:03 +0100 | [diff] [blame] | 9438 | resolved = nullptr; |
| 9439 | } |
| 9440 | |
| 9441 | if (resolved != nullptr) { |
David Srbecky | 5de5efe | 2021-02-15 21:23:00 +0000 | [diff] [blame] | 9442 | dex_cache->SetResolvedField(field_idx, resolved); |
David Brazdil | 1ab0fa8 | 2018-05-04 11:28:03 +0100 | [diff] [blame] | 9443 | } |
| 9444 | |
| 9445 | return resolved; |
| 9446 | } |
| 9447 | |
Vladimir Marko | af94020 | 2017-12-08 15:01:18 +0000 | [diff] [blame] | 9448 | ObjPtr<mirror::MethodType> ClassLinker::ResolveMethodType( |
| 9449 | Thread* self, |
Orion Hodson | 06d10a7 | 2018-05-14 08:53:38 +0100 | [diff] [blame] | 9450 | dex::ProtoIndex proto_idx, |
Vladimir Marko | af94020 | 2017-12-08 15:01:18 +0000 | [diff] [blame] | 9451 | Handle<mirror::DexCache> dex_cache, |
| 9452 | Handle<mirror::ClassLoader> class_loader) { |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9453 | DCHECK(Runtime::Current()->IsMethodHandlesEnabled()); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 9454 | DCHECK(dex_cache != nullptr); |
Nicolas Geoffray | ab91eef | 2021-09-14 09:48:51 +0100 | [diff] [blame] | 9455 | DCHECK(dex_cache->GetClassLoader() == class_loader.Get()); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9456 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 9457 | ObjPtr<mirror::MethodType> resolved = dex_cache->GetResolvedMethodType(proto_idx); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9458 | if (resolved != nullptr) { |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 9459 | return resolved; |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9460 | } |
| 9461 | |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9462 | StackHandleScope<4> hs(self); |
| 9463 | |
| 9464 | // First resolve the return type. |
Vladimir Marko | af94020 | 2017-12-08 15:01:18 +0000 | [diff] [blame] | 9465 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9466 | const dex::ProtoId& proto_id = dex_file.GetProtoId(proto_idx); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9467 | Handle<mirror::Class> return_type(hs.NewHandle( |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 9468 | ResolveType(proto_id.return_type_idx_, dex_cache, class_loader))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 9469 | if (return_type == nullptr) { |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9470 | DCHECK(self->IsExceptionPending()); |
| 9471 | return nullptr; |
| 9472 | } |
| 9473 | |
| 9474 | // Then resolve the argument types. |
| 9475 | // |
| 9476 | // TODO: Is there a better way to figure out the number of method arguments |
| 9477 | // other than by looking at the shorty ? |
| 9478 | const size_t num_method_args = strlen(dex_file.StringDataByIdx(proto_id.shorty_idx_)) - 1; |
| 9479 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 9480 | ObjPtr<mirror::Class> array_of_class = GetClassRoot<mirror::ObjectArray<mirror::Class>>(this); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9481 | Handle<mirror::ObjectArray<mirror::Class>> method_params(hs.NewHandle( |
| 9482 | mirror::ObjectArray<mirror::Class>::Alloc(self, array_of_class, num_method_args))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 9483 | if (method_params == nullptr) { |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9484 | DCHECK(self->IsExceptionPending()); |
| 9485 | return nullptr; |
| 9486 | } |
| 9487 | |
| 9488 | DexFileParameterIterator it(dex_file, proto_id); |
| 9489 | int32_t i = 0; |
| 9490 | MutableHandle<mirror::Class> param_class = hs.NewHandle<mirror::Class>(nullptr); |
| 9491 | for (; it.HasNext(); it.Next()) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 9492 | const dex::TypeIndex type_idx = it.GetTypeIdx(); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 9493 | param_class.Assign(ResolveType(type_idx, dex_cache, class_loader)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 9494 | if (param_class == nullptr) { |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9495 | DCHECK(self->IsExceptionPending()); |
| 9496 | return nullptr; |
| 9497 | } |
| 9498 | |
| 9499 | method_params->Set(i++, param_class.Get()); |
| 9500 | } |
| 9501 | |
| 9502 | DCHECK(!it.HasNext()); |
| 9503 | |
| 9504 | Handle<mirror::MethodType> type = hs.NewHandle( |
| 9505 | mirror::MethodType::Create(self, return_type, method_params)); |
Nicolas Geoffray | 4aebd58 | 2021-07-23 15:27:31 +0100 | [diff] [blame] | 9506 | if (type != nullptr) { |
| 9507 | dex_cache->SetResolvedMethodType(proto_idx, type.Get()); |
| 9508 | } |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9509 | |
| 9510 | return type.Get(); |
| 9511 | } |
| 9512 | |
Vladimir Marko | af94020 | 2017-12-08 15:01:18 +0000 | [diff] [blame] | 9513 | ObjPtr<mirror::MethodType> ClassLinker::ResolveMethodType(Thread* self, |
Orion Hodson | 06d10a7 | 2018-05-14 08:53:38 +0100 | [diff] [blame] | 9514 | dex::ProtoIndex proto_idx, |
Vladimir Marko | af94020 | 2017-12-08 15:01:18 +0000 | [diff] [blame] | 9515 | ArtMethod* referrer) { |
Orion Hodson | 2e59994 | 2017-09-22 16:17:41 +0100 | [diff] [blame] | 9516 | StackHandleScope<2> hs(self); |
Orion Hodson | 2e59994 | 2017-09-22 16:17:41 +0100 | [diff] [blame] | 9517 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache())); |
| 9518 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(referrer->GetClassLoader())); |
Vladimir Marko | af94020 | 2017-12-08 15:01:18 +0000 | [diff] [blame] | 9519 | return ResolveMethodType(self, proto_idx, dex_cache, class_loader); |
Orion Hodson | 2e59994 | 2017-09-22 16:17:41 +0100 | [diff] [blame] | 9520 | } |
| 9521 | |
Vladimir Marko | 5aead70 | 2019-03-27 11:00:36 +0000 | [diff] [blame] | 9522 | ObjPtr<mirror::MethodHandle> ClassLinker::ResolveMethodHandleForField( |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9523 | Thread* self, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9524 | const dex::MethodHandleItem& method_handle, |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9525 | ArtMethod* referrer) { |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9526 | DexFile::MethodHandleType handle_type = |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9527 | static_cast<DexFile::MethodHandleType>(method_handle.method_handle_type_); |
| 9528 | mirror::MethodHandle::Kind kind; |
Orion Hodson | fd7b2c2 | 2018-03-15 15:38:38 +0000 | [diff] [blame] | 9529 | bool is_put; |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9530 | bool is_static; |
| 9531 | int32_t num_params; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9532 | switch (handle_type) { |
| 9533 | case DexFile::MethodHandleType::kStaticPut: { |
Orion Hodson | 82b351f | 2017-07-05 14:34:25 +0100 | [diff] [blame] | 9534 | kind = mirror::MethodHandle::Kind::kStaticPut; |
Orion Hodson | fd7b2c2 | 2018-03-15 15:38:38 +0000 | [diff] [blame] | 9535 | is_put = true; |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9536 | is_static = true; |
| 9537 | num_params = 1; |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 9538 | break; |
| 9539 | } |
| 9540 | case DexFile::MethodHandleType::kStaticGet: { |
Orion Hodson | 82b351f | 2017-07-05 14:34:25 +0100 | [diff] [blame] | 9541 | kind = mirror::MethodHandle::Kind::kStaticGet; |
Orion Hodson | fd7b2c2 | 2018-03-15 15:38:38 +0000 | [diff] [blame] | 9542 | is_put = false; |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9543 | is_static = true; |
| 9544 | num_params = 0; |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 9545 | break; |
| 9546 | } |
| 9547 | case DexFile::MethodHandleType::kInstancePut: { |
Orion Hodson | 82b351f | 2017-07-05 14:34:25 +0100 | [diff] [blame] | 9548 | kind = mirror::MethodHandle::Kind::kInstancePut; |
Orion Hodson | fd7b2c2 | 2018-03-15 15:38:38 +0000 | [diff] [blame] | 9549 | is_put = true; |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9550 | is_static = false; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9551 | num_params = 2; |
| 9552 | break; |
| 9553 | } |
| 9554 | case DexFile::MethodHandleType::kInstanceGet: { |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9555 | kind = mirror::MethodHandle::Kind::kInstanceGet; |
Orion Hodson | fd7b2c2 | 2018-03-15 15:38:38 +0000 | [diff] [blame] | 9556 | is_put = false; |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9557 | is_static = false; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9558 | num_params = 1; |
| 9559 | break; |
| 9560 | } |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9561 | case DexFile::MethodHandleType::kInvokeStatic: |
Orion Hodson | 82b351f | 2017-07-05 14:34:25 +0100 | [diff] [blame] | 9562 | case DexFile::MethodHandleType::kInvokeInstance: |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9563 | case DexFile::MethodHandleType::kInvokeConstructor: |
Orion Hodson | 82b351f | 2017-07-05 14:34:25 +0100 | [diff] [blame] | 9564 | case DexFile::MethodHandleType::kInvokeDirect: |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9565 | case DexFile::MethodHandleType::kInvokeInterface: |
| 9566 | UNREACHABLE(); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9567 | } |
| 9568 | |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9569 | ArtField* target_field = |
| 9570 | ResolveField(method_handle.field_or_method_idx_, referrer, is_static); |
| 9571 | if (LIKELY(target_field != nullptr)) { |
| 9572 | ObjPtr<mirror::Class> target_class = target_field->GetDeclaringClass(); |
| 9573 | ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass(); |
| 9574 | if (UNLIKELY(!referring_class->CanAccessMember(target_class, target_field->GetAccessFlags()))) { |
| 9575 | ThrowIllegalAccessErrorField(referring_class, target_field); |
| 9576 | return nullptr; |
| 9577 | } |
Orion Hodson | fd7b2c2 | 2018-03-15 15:38:38 +0000 | [diff] [blame] | 9578 | if (UNLIKELY(is_put && target_field->IsFinal())) { |
| 9579 | ThrowIllegalAccessErrorField(referring_class, target_field); |
| 9580 | return nullptr; |
| 9581 | } |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9582 | } else { |
| 9583 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 9584 | return nullptr; |
| 9585 | } |
| 9586 | |
| 9587 | StackHandleScope<4> hs(self); |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 9588 | ObjPtr<mirror::Class> array_of_class = GetClassRoot<mirror::ObjectArray<mirror::Class>>(this); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9589 | Handle<mirror::ObjectArray<mirror::Class>> method_params(hs.NewHandle( |
| 9590 | mirror::ObjectArray<mirror::Class>::Alloc(self, array_of_class, num_params))); |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 9591 | if (UNLIKELY(method_params == nullptr)) { |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9592 | DCHECK(self->IsExceptionPending()); |
| 9593 | return nullptr; |
| 9594 | } |
| 9595 | |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9596 | Handle<mirror::Class> constructor_class; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9597 | Handle<mirror::Class> return_type; |
| 9598 | switch (handle_type) { |
| 9599 | case DexFile::MethodHandleType::kStaticPut: { |
Vladimir Marko | 4098a7a | 2017-11-06 16:00:51 +0000 | [diff] [blame] | 9600 | method_params->Set(0, target_field->ResolveType()); |
Vladimir Marko | 9186b18 | 2018-11-06 14:55:54 +0000 | [diff] [blame] | 9601 | return_type = hs.NewHandle(GetClassRoot(ClassRoot::kPrimitiveVoid, this)); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9602 | break; |
| 9603 | } |
| 9604 | case DexFile::MethodHandleType::kStaticGet: { |
Vladimir Marko | 4098a7a | 2017-11-06 16:00:51 +0000 | [diff] [blame] | 9605 | return_type = hs.NewHandle(target_field->ResolveType()); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9606 | break; |
| 9607 | } |
| 9608 | case DexFile::MethodHandleType::kInstancePut: { |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 9609 | method_params->Set(0, target_field->GetDeclaringClass()); |
Vladimir Marko | 4098a7a | 2017-11-06 16:00:51 +0000 | [diff] [blame] | 9610 | method_params->Set(1, target_field->ResolveType()); |
Vladimir Marko | 9186b18 | 2018-11-06 14:55:54 +0000 | [diff] [blame] | 9611 | return_type = hs.NewHandle(GetClassRoot(ClassRoot::kPrimitiveVoid, this)); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9612 | break; |
| 9613 | } |
| 9614 | case DexFile::MethodHandleType::kInstanceGet: { |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 9615 | method_params->Set(0, target_field->GetDeclaringClass()); |
Vladimir Marko | 4098a7a | 2017-11-06 16:00:51 +0000 | [diff] [blame] | 9616 | return_type = hs.NewHandle(target_field->ResolveType()); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9617 | break; |
| 9618 | } |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9619 | case DexFile::MethodHandleType::kInvokeStatic: |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 9620 | case DexFile::MethodHandleType::kInvokeInstance: |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9621 | case DexFile::MethodHandleType::kInvokeConstructor: |
| 9622 | case DexFile::MethodHandleType::kInvokeDirect: |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 9623 | case DexFile::MethodHandleType::kInvokeInterface: |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9624 | UNREACHABLE(); |
| 9625 | } |
| 9626 | |
| 9627 | for (int32_t i = 0; i < num_params; ++i) { |
| 9628 | if (UNLIKELY(method_params->Get(i) == nullptr)) { |
| 9629 | DCHECK(self->IsExceptionPending()); |
| 9630 | return nullptr; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9631 | } |
| 9632 | } |
| 9633 | |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9634 | if (UNLIKELY(return_type.IsNull())) { |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9635 | DCHECK(self->IsExceptionPending()); |
| 9636 | return nullptr; |
| 9637 | } |
| 9638 | |
| 9639 | Handle<mirror::MethodType> |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9640 | method_type(hs.NewHandle(mirror::MethodType::Create(self, return_type, method_params))); |
| 9641 | if (UNLIKELY(method_type.IsNull())) { |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9642 | DCHECK(self->IsExceptionPending()); |
| 9643 | return nullptr; |
| 9644 | } |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 9645 | |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9646 | uintptr_t target = reinterpret_cast<uintptr_t>(target_field); |
| 9647 | return mirror::MethodHandleImpl::Create(self, target, kind, method_type); |
| 9648 | } |
| 9649 | |
Vladimir Marko | 5aead70 | 2019-03-27 11:00:36 +0000 | [diff] [blame] | 9650 | ObjPtr<mirror::MethodHandle> ClassLinker::ResolveMethodHandleForMethod( |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9651 | Thread* self, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9652 | const dex::MethodHandleItem& method_handle, |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9653 | ArtMethod* referrer) { |
| 9654 | DexFile::MethodHandleType handle_type = |
| 9655 | static_cast<DexFile::MethodHandleType>(method_handle.method_handle_type_); |
| 9656 | mirror::MethodHandle::Kind kind; |
| 9657 | uint32_t receiver_count = 0; |
| 9658 | ArtMethod* target_method = nullptr; |
| 9659 | switch (handle_type) { |
| 9660 | case DexFile::MethodHandleType::kStaticPut: |
| 9661 | case DexFile::MethodHandleType::kStaticGet: |
| 9662 | case DexFile::MethodHandleType::kInstancePut: |
| 9663 | case DexFile::MethodHandleType::kInstanceGet: |
| 9664 | UNREACHABLE(); |
| 9665 | case DexFile::MethodHandleType::kInvokeStatic: { |
| 9666 | kind = mirror::MethodHandle::Kind::kInvokeStatic; |
| 9667 | receiver_count = 0; |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9668 | target_method = ResolveMethod<ResolveMode::kNoChecks>(self, |
| 9669 | method_handle.field_or_method_idx_, |
| 9670 | referrer, |
| 9671 | InvokeType::kStatic); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9672 | break; |
| 9673 | } |
| 9674 | case DexFile::MethodHandleType::kInvokeInstance: { |
| 9675 | kind = mirror::MethodHandle::Kind::kInvokeVirtual; |
| 9676 | receiver_count = 1; |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9677 | target_method = ResolveMethod<ResolveMode::kNoChecks>(self, |
| 9678 | method_handle.field_or_method_idx_, |
| 9679 | referrer, |
| 9680 | InvokeType::kVirtual); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9681 | break; |
| 9682 | } |
| 9683 | case DexFile::MethodHandleType::kInvokeConstructor: { |
| 9684 | // Constructors are currently implemented as a transform. They |
| 9685 | // are special cased later in this method. |
| 9686 | kind = mirror::MethodHandle::Kind::kInvokeTransform; |
| 9687 | receiver_count = 0; |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9688 | target_method = ResolveMethod<ResolveMode::kNoChecks>(self, |
| 9689 | method_handle.field_or_method_idx_, |
| 9690 | referrer, |
| 9691 | InvokeType::kDirect); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9692 | break; |
| 9693 | } |
| 9694 | case DexFile::MethodHandleType::kInvokeDirect: { |
| 9695 | kind = mirror::MethodHandle::Kind::kInvokeDirect; |
| 9696 | receiver_count = 1; |
| 9697 | StackHandleScope<2> hs(self); |
| 9698 | // A constant method handle with type kInvokeDirect can refer to |
| 9699 | // a method that is private or to a method in a super class. To |
| 9700 | // disambiguate the two options, we resolve the method ignoring |
| 9701 | // the invocation type to determine if the method is private. We |
| 9702 | // then resolve again specifying the intended invocation type to |
| 9703 | // force the appropriate checks. |
Vladimir Marko | 8901119 | 2017-12-11 13:45:05 +0000 | [diff] [blame] | 9704 | target_method = ResolveMethodWithoutInvokeType(method_handle.field_or_method_idx_, |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9705 | hs.NewHandle(referrer->GetDexCache()), |
| 9706 | hs.NewHandle(referrer->GetClassLoader())); |
| 9707 | if (UNLIKELY(target_method == nullptr)) { |
| 9708 | break; |
| 9709 | } |
| 9710 | |
| 9711 | if (target_method->IsPrivate()) { |
| 9712 | kind = mirror::MethodHandle::Kind::kInvokeDirect; |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9713 | target_method = ResolveMethod<ResolveMode::kNoChecks>(self, |
| 9714 | method_handle.field_or_method_idx_, |
| 9715 | referrer, |
| 9716 | InvokeType::kDirect); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9717 | } else { |
| 9718 | kind = mirror::MethodHandle::Kind::kInvokeSuper; |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9719 | target_method = ResolveMethod<ResolveMode::kNoChecks>(self, |
| 9720 | method_handle.field_or_method_idx_, |
| 9721 | referrer, |
| 9722 | InvokeType::kSuper); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9723 | if (UNLIKELY(target_method == nullptr)) { |
| 9724 | break; |
| 9725 | } |
| 9726 | // Find the method specified in the parent in referring class |
| 9727 | // so invoke-super invokes the method in the parent of the |
| 9728 | // referrer. |
| 9729 | target_method = |
| 9730 | referrer->GetDeclaringClass()->FindVirtualMethodForVirtual(target_method, |
| 9731 | kRuntimePointerSize); |
| 9732 | } |
| 9733 | break; |
| 9734 | } |
| 9735 | case DexFile::MethodHandleType::kInvokeInterface: { |
| 9736 | kind = mirror::MethodHandle::Kind::kInvokeInterface; |
| 9737 | receiver_count = 1; |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9738 | target_method = ResolveMethod<ResolveMode::kNoChecks>(self, |
| 9739 | method_handle.field_or_method_idx_, |
| 9740 | referrer, |
| 9741 | InvokeType::kInterface); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9742 | break; |
| 9743 | } |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 9744 | } |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9745 | |
| 9746 | if (UNLIKELY(target_method == nullptr)) { |
| 9747 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 9748 | return nullptr; |
| 9749 | } |
| 9750 | |
| 9751 | ObjPtr<mirror::Class> target_class = target_method->GetDeclaringClass(); |
| 9752 | ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass(); |
| 9753 | uint32_t access_flags = target_method->GetAccessFlags(); |
| 9754 | if (UNLIKELY(!referring_class->CanAccessMember(target_class, access_flags))) { |
| 9755 | ThrowIllegalAccessErrorMethod(referring_class, target_method); |
| 9756 | return nullptr; |
| 9757 | } |
| 9758 | |
| 9759 | // Calculate the number of parameters from the method shorty. We add the |
| 9760 | // receiver count (0 or 1) and deduct one for the return value. |
| 9761 | uint32_t shorty_length; |
| 9762 | target_method->GetShorty(&shorty_length); |
| 9763 | int32_t num_params = static_cast<int32_t>(shorty_length + receiver_count - 1); |
| 9764 | |
Orion Hodson | ecd5856 | 2018-09-24 11:27:33 +0100 | [diff] [blame] | 9765 | StackHandleScope<5> hs(self); |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 9766 | ObjPtr<mirror::Class> array_of_class = GetClassRoot<mirror::ObjectArray<mirror::Class>>(this); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9767 | Handle<mirror::ObjectArray<mirror::Class>> method_params(hs.NewHandle( |
| 9768 | mirror::ObjectArray<mirror::Class>::Alloc(self, array_of_class, num_params))); |
| 9769 | if (method_params.Get() == nullptr) { |
| 9770 | DCHECK(self->IsExceptionPending()); |
| 9771 | return nullptr; |
| 9772 | } |
| 9773 | |
Orion Hodson | ecd5856 | 2018-09-24 11:27:33 +0100 | [diff] [blame] | 9774 | const DexFile* dex_file = referrer->GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9775 | 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] | 9776 | int32_t index = 0; |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9777 | if (receiver_count != 0) { |
Orion Hodson | ecd5856 | 2018-09-24 11:27:33 +0100 | [diff] [blame] | 9778 | // Insert receiver. Use the class identified in the method handle rather than the declaring |
| 9779 | // class of the resolved method which may be super class or default interface method |
| 9780 | // (b/115964401). |
| 9781 | ObjPtr<mirror::Class> receiver_class = LookupResolvedType(method_id.class_idx_, referrer); |
| 9782 | // receiver_class should have been resolved when resolving the target method. |
| 9783 | DCHECK(receiver_class != nullptr); |
| 9784 | method_params->Set(index++, receiver_class); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9785 | } |
Orion Hodson | ecd5856 | 2018-09-24 11:27:33 +0100 | [diff] [blame] | 9786 | |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9787 | const dex::ProtoId& proto_id = dex_file->GetProtoId(method_id.proto_idx_); |
Orion Hodson | ecd5856 | 2018-09-24 11:27:33 +0100 | [diff] [blame] | 9788 | DexFileParameterIterator it(*dex_file, proto_id); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9789 | while (it.HasNext()) { |
Orion Hodson | da1cdd0 | 2018-01-31 18:08:28 +0000 | [diff] [blame] | 9790 | DCHECK_LT(index, num_params); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9791 | const dex::TypeIndex type_idx = it.GetTypeIdx(); |
Orion Hodson | ecd5856 | 2018-09-24 11:27:33 +0100 | [diff] [blame] | 9792 | ObjPtr<mirror::Class> klass = ResolveType(type_idx, referrer); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9793 | if (nullptr == klass) { |
| 9794 | DCHECK(self->IsExceptionPending()); |
| 9795 | return nullptr; |
| 9796 | } |
| 9797 | method_params->Set(index++, klass); |
| 9798 | it.Next(); |
| 9799 | } |
| 9800 | |
Orion Hodson | ecd5856 | 2018-09-24 11:27:33 +0100 | [diff] [blame] | 9801 | Handle<mirror::Class> return_type = |
| 9802 | hs.NewHandle(ResolveType(proto_id.return_type_idx_, referrer)); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9803 | if (UNLIKELY(return_type.IsNull())) { |
| 9804 | DCHECK(self->IsExceptionPending()); |
| 9805 | return nullptr; |
| 9806 | } |
| 9807 | |
| 9808 | Handle<mirror::MethodType> |
| 9809 | method_type(hs.NewHandle(mirror::MethodType::Create(self, return_type, method_params))); |
| 9810 | if (UNLIKELY(method_type.IsNull())) { |
| 9811 | DCHECK(self->IsExceptionPending()); |
| 9812 | return nullptr; |
| 9813 | } |
| 9814 | |
| 9815 | if (UNLIKELY(handle_type == DexFile::MethodHandleType::kInvokeConstructor)) { |
| 9816 | Handle<mirror::Class> constructor_class = hs.NewHandle(target_method->GetDeclaringClass()); |
| 9817 | Handle<mirror::MethodHandlesLookup> lookup = |
| 9818 | hs.NewHandle(mirror::MethodHandlesLookup::GetDefault(self)); |
| 9819 | return lookup->FindConstructor(self, constructor_class, method_type); |
| 9820 | } |
| 9821 | |
| 9822 | uintptr_t target = reinterpret_cast<uintptr_t>(target_method); |
| 9823 | return mirror::MethodHandleImpl::Create(self, target, kind, method_type); |
| 9824 | } |
| 9825 | |
Vladimir Marko | af94020 | 2017-12-08 15:01:18 +0000 | [diff] [blame] | 9826 | ObjPtr<mirror::MethodHandle> ClassLinker::ResolveMethodHandle(Thread* self, |
| 9827 | uint32_t method_handle_idx, |
| 9828 | ArtMethod* referrer) |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9829 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9830 | const DexFile* const dex_file = referrer->GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9831 | const dex::MethodHandleItem& method_handle = dex_file->GetMethodHandle(method_handle_idx); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9832 | switch (static_cast<DexFile::MethodHandleType>(method_handle.method_handle_type_)) { |
| 9833 | case DexFile::MethodHandleType::kStaticPut: |
| 9834 | case DexFile::MethodHandleType::kStaticGet: |
| 9835 | case DexFile::MethodHandleType::kInstancePut: |
| 9836 | case DexFile::MethodHandleType::kInstanceGet: |
| 9837 | return ResolveMethodHandleForField(self, method_handle, referrer); |
| 9838 | case DexFile::MethodHandleType::kInvokeStatic: |
| 9839 | case DexFile::MethodHandleType::kInvokeInstance: |
| 9840 | case DexFile::MethodHandleType::kInvokeConstructor: |
| 9841 | case DexFile::MethodHandleType::kInvokeDirect: |
| 9842 | case DexFile::MethodHandleType::kInvokeInterface: |
Orion Hodson | da1cdd0 | 2018-01-31 18:08:28 +0000 | [diff] [blame] | 9843 | return ResolveMethodHandleForMethod(self, method_handle, referrer); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9844 | } |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9845 | } |
| 9846 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 9847 | bool ClassLinker::IsQuickResolutionStub(const void* entry_point) const { |
| 9848 | return (entry_point == GetQuickResolutionStub()) || |
| 9849 | (quick_resolution_trampoline_ == entry_point); |
| 9850 | } |
| 9851 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 9852 | bool ClassLinker::IsQuickToInterpreterBridge(const void* entry_point) const { |
| 9853 | return (entry_point == GetQuickToInterpreterBridge()) || |
| 9854 | (quick_to_interpreter_bridge_trampoline_ == entry_point); |
| 9855 | } |
| 9856 | |
| 9857 | bool ClassLinker::IsQuickGenericJniStub(const void* entry_point) const { |
| 9858 | return (entry_point == GetQuickGenericJniStub()) || |
| 9859 | (quick_generic_jni_trampoline_ == entry_point); |
| 9860 | } |
| 9861 | |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 9862 | bool ClassLinker::IsJniDlsymLookupStub(const void* entry_point) const { |
Vladimir Marko | 7dac864 | 2019-11-06 17:09:30 +0000 | [diff] [blame] | 9863 | return entry_point == GetJniDlsymLookupStub() || |
| 9864 | (jni_dlsym_lookup_trampoline_ == entry_point); |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 9865 | } |
| 9866 | |
Vladimir Marko | fa458ac | 2020-02-12 14:08:07 +0000 | [diff] [blame] | 9867 | bool ClassLinker::IsJniDlsymLookupCriticalStub(const void* entry_point) const { |
| 9868 | return entry_point == GetJniDlsymLookupCriticalStub() || |
| 9869 | (jni_dlsym_lookup_critical_trampoline_ == entry_point); |
| 9870 | } |
| 9871 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 9872 | const void* ClassLinker::GetRuntimeQuickGenericJniStub() const { |
| 9873 | return GetQuickGenericJniStub(); |
| 9874 | } |
| 9875 | |
Alex Light | db01a09 | 2017-04-03 15:39:55 -0700 | [diff] [blame] | 9876 | void ClassLinker::SetEntryPointsForObsoleteMethod(ArtMethod* method) const { |
| 9877 | DCHECK(method->IsObsolete()); |
| 9878 | // We cannot mess with the entrypoints of native methods because they are used to determine how |
| 9879 | // large the method's quick stack frame is. Without this information we cannot walk the stacks. |
| 9880 | if (!method->IsNative()) { |
| 9881 | method->SetEntryPointFromQuickCompiledCode(GetInvokeObsoleteMethodStub()); |
| 9882 | } |
| 9883 | } |
| 9884 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 9885 | void ClassLinker::DumpForSigQuit(std::ostream& os) { |
Hans Boehm | fb3ad72 | 2021-08-16 16:53:17 +0000 | [diff] [blame] | 9886 | ScopedObjectAccess soa(Thread::Current()); |
| 9887 | ReaderMutexLock mu(soa.Self(), *Locks::classlinker_classes_lock_); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 9888 | os << "Zygote loaded classes=" << NumZygoteClasses() << " post zygote classes=" |
| 9889 | << NumNonZygoteClasses() << "\n"; |
Hans Boehm | fb3ad72 | 2021-08-16 16:53:17 +0000 | [diff] [blame] | 9890 | ReaderMutexLock mu2(soa.Self(), *Locks::dex_lock_); |
Nicolas Geoffray | a90c922 | 2018-09-07 13:19:19 +0100 | [diff] [blame] | 9891 | os << "Dumping registered class loaders\n"; |
| 9892 | size_t class_loader_index = 0; |
| 9893 | for (const ClassLoaderData& class_loader : class_loaders_) { |
| 9894 | ObjPtr<mirror::ClassLoader> loader = |
Hans Boehm | fb3ad72 | 2021-08-16 16:53:17 +0000 | [diff] [blame] | 9895 | ObjPtr<mirror::ClassLoader>::DownCast(soa.Self()->DecodeJObject(class_loader.weak_root)); |
Nicolas Geoffray | a90c922 | 2018-09-07 13:19:19 +0100 | [diff] [blame] | 9896 | if (loader != nullptr) { |
| 9897 | os << "#" << class_loader_index++ << " " << loader->GetClass()->PrettyDescriptor() << ": ["; |
| 9898 | bool saw_one_dex_file = false; |
David Srbecky | 6fbcc29 | 2021-02-23 01:05:32 +0000 | [diff] [blame] | 9899 | for (const auto& entry : dex_caches_) { |
| 9900 | const DexCacheData& dex_cache = entry.second; |
| 9901 | if (dex_cache.class_table == class_loader.class_table) { |
Nicolas Geoffray | a90c922 | 2018-09-07 13:19:19 +0100 | [diff] [blame] | 9902 | if (saw_one_dex_file) { |
| 9903 | os << ":"; |
| 9904 | } |
| 9905 | saw_one_dex_file = true; |
David Srbecky | 6fbcc29 | 2021-02-23 01:05:32 +0000 | [diff] [blame] | 9906 | os << entry.first->GetLocation(); |
Nicolas Geoffray | a90c922 | 2018-09-07 13:19:19 +0100 | [diff] [blame] | 9907 | } |
| 9908 | } |
| 9909 | os << "]"; |
| 9910 | bool found_parent = false; |
| 9911 | if (loader->GetParent() != nullptr) { |
| 9912 | size_t parent_index = 0; |
| 9913 | for (const ClassLoaderData& class_loader2 : class_loaders_) { |
| 9914 | ObjPtr<mirror::ClassLoader> loader2 = ObjPtr<mirror::ClassLoader>::DownCast( |
Hans Boehm | fb3ad72 | 2021-08-16 16:53:17 +0000 | [diff] [blame] | 9915 | soa.Self()->DecodeJObject(class_loader2.weak_root)); |
Nicolas Geoffray | a90c922 | 2018-09-07 13:19:19 +0100 | [diff] [blame] | 9916 | if (loader2 == loader->GetParent()) { |
| 9917 | os << ", parent #" << parent_index; |
| 9918 | found_parent = true; |
| 9919 | break; |
| 9920 | } |
| 9921 | parent_index++; |
| 9922 | } |
| 9923 | if (!found_parent) { |
| 9924 | os << ", unregistered parent of type " |
| 9925 | << loader->GetParent()->GetClass()->PrettyDescriptor(); |
| 9926 | } |
| 9927 | } else { |
| 9928 | os << ", no parent"; |
| 9929 | } |
| 9930 | os << "\n"; |
| 9931 | } |
| 9932 | } |
| 9933 | os << "Done dumping class loaders\n"; |
Andreas Gampe | 9b7f8b5 | 2019-06-07 08:59:29 -0700 | [diff] [blame] | 9934 | Runtime* runtime = Runtime::Current(); |
| 9935 | os << "Classes initialized: " << runtime->GetStat(KIND_GLOBAL_CLASS_INIT_COUNT) << " in " |
| 9936 | << PrettyDuration(runtime->GetStat(KIND_GLOBAL_CLASS_INIT_TIME)) << "\n"; |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 9937 | } |
| 9938 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 9939 | class CountClassesVisitor : public ClassLoaderVisitor { |
| 9940 | public: |
| 9941 | CountClassesVisitor() : num_zygote_classes(0), num_non_zygote_classes(0) {} |
| 9942 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 9943 | void Visit(ObjPtr<mirror::ClassLoader> class_loader) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 9944 | REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_) override { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 9945 | ClassTable* const class_table = class_loader->GetClassTable(); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 9946 | if (class_table != nullptr) { |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 9947 | num_zygote_classes += class_table->NumZygoteClasses(class_loader); |
| 9948 | num_non_zygote_classes += class_table->NumNonZygoteClasses(class_loader); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 9949 | } |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 9950 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 9951 | |
| 9952 | size_t num_zygote_classes; |
| 9953 | size_t num_non_zygote_classes; |
| 9954 | }; |
| 9955 | |
| 9956 | size_t ClassLinker::NumZygoteClasses() const { |
| 9957 | CountClassesVisitor visitor; |
| 9958 | VisitClassLoaders(&visitor); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 9959 | return visitor.num_zygote_classes + boot_class_table_->NumZygoteClasses(nullptr); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 9960 | } |
| 9961 | |
| 9962 | size_t ClassLinker::NumNonZygoteClasses() const { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 9963 | CountClassesVisitor visitor; |
| 9964 | VisitClassLoaders(&visitor); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 9965 | return visitor.num_non_zygote_classes + boot_class_table_->NumNonZygoteClasses(nullptr); |
Elliott Hughes | cac6cc7 | 2011-11-03 20:31:21 -0700 | [diff] [blame] | 9966 | } |
| 9967 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 9968 | size_t ClassLinker::NumLoadedClasses() { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 9969 | ReaderMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 9970 | // 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] | 9971 | return NumNonZygoteClasses(); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 9972 | } |
| 9973 | |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 9974 | pid_t ClassLinker::GetClassesLockOwner() { |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 9975 | return Locks::classlinker_classes_lock_->GetExclusiveOwnerTid(); |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 9976 | } |
| 9977 | |
| 9978 | pid_t ClassLinker::GetDexLockOwner() { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 9979 | return Locks::dex_lock_->GetExclusiveOwnerTid(); |
Brian Carlstrom | 24a3c2e | 2011-10-17 18:07:52 -0700 | [diff] [blame] | 9980 | } |
| 9981 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 9982 | void ClassLinker::SetClassRoot(ClassRoot class_root, ObjPtr<mirror::Class> klass) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 9983 | DCHECK(!init_done_); |
| 9984 | |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 9985 | DCHECK(klass != nullptr); |
| 9986 | DCHECK(klass->GetClassLoader() == nullptr); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 9987 | |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 9988 | mirror::ObjectArray<mirror::Class>* class_roots = class_roots_.Read(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 9989 | DCHECK(class_roots != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 9990 | DCHECK_LT(static_cast<uint32_t>(class_root), static_cast<uint32_t>(ClassRoot::kMax)); |
| 9991 | int32_t index = static_cast<int32_t>(class_root); |
| 9992 | DCHECK(class_roots->Get(index) == nullptr); |
| 9993 | class_roots->Set<false>(index, klass); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 9994 | } |
| 9995 | |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 9996 | ObjPtr<mirror::ClassLoader> ClassLinker::CreateWellKnownClassLoader( |
| 9997 | Thread* self, |
| 9998 | const std::vector<const DexFile*>& dex_files, |
| 9999 | Handle<mirror::Class> loader_class, |
Nicolas Geoffray | e167273 | 2018-11-30 01:09:49 +0000 | [diff] [blame] | 10000 | Handle<mirror::ClassLoader> parent_loader, |
Brad Stenning | 9c924e8 | 2021-10-11 19:09:00 -0700 | [diff] [blame] | 10001 | Handle<mirror::ObjectArray<mirror::ClassLoader>> shared_libraries, |
| 10002 | Handle<mirror::ObjectArray<mirror::ClassLoader>> shared_libraries_after) { |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 10003 | |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 10004 | StackHandleScope<5> hs(self); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 10005 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 10006 | ArtField* dex_elements_field = |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 10007 | jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList_dexElements); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 10008 | |
Vladimir Marko | 4098a7a | 2017-11-06 16:00:51 +0000 | [diff] [blame] | 10009 | Handle<mirror::Class> dex_elements_class(hs.NewHandle(dex_elements_field->ResolveType())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 10010 | DCHECK(dex_elements_class != nullptr); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 10011 | DCHECK(dex_elements_class->IsArrayClass()); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 10012 | Handle<mirror::ObjectArray<mirror::Object>> h_dex_elements(hs.NewHandle( |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 10013 | mirror::ObjectArray<mirror::Object>::Alloc(self, |
| 10014 | dex_elements_class.Get(), |
| 10015 | dex_files.size()))); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 10016 | Handle<mirror::Class> h_dex_element_class = |
| 10017 | hs.NewHandle(dex_elements_class->GetComponentType()); |
| 10018 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 10019 | ArtField* element_file_field = |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 10020 | jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 10021 | DCHECK_EQ(h_dex_element_class.Get(), element_file_field->GetDeclaringClass()); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 10022 | |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 10023 | ArtField* cookie_field = jni::DecodeArtField(WellKnownClasses::dalvik_system_DexFile_cookie); |
Vladimir Marko | 208f670 | 2017-12-08 12:00:50 +0000 | [diff] [blame] | 10024 | DCHECK_EQ(cookie_field->GetDeclaringClass(), element_file_field->LookupResolvedType()); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 10025 | |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 10026 | ArtField* file_name_field = jni::DecodeArtField(WellKnownClasses::dalvik_system_DexFile_fileName); |
Vladimir Marko | 208f670 | 2017-12-08 12:00:50 +0000 | [diff] [blame] | 10027 | DCHECK_EQ(file_name_field->GetDeclaringClass(), element_file_field->LookupResolvedType()); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 10028 | |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 10029 | // Fill the elements array. |
| 10030 | int32_t index = 0; |
| 10031 | for (const DexFile* dex_file : dex_files) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 10032 | StackHandleScope<4> hs2(self); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 10033 | |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 10034 | // CreateWellKnownClassLoader is only used by gtests and compiler. |
| 10035 | // 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] | 10036 | Handle<mirror::LongArray> h_long_array = hs2.NewHandle(mirror::LongArray::Alloc( |
| 10037 | self, |
| 10038 | kDexFileIndexStart + 1)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 10039 | DCHECK(h_long_array != nullptr); |
Vladimir Marko | 78baed5 | 2018-10-11 10:44:58 +0100 | [diff] [blame] | 10040 | h_long_array->Set(kDexFileIndexStart, reinterpret_cast64<int64_t>(dex_file)); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 10041 | |
Mathieu Chartier | 3738e98 | 2017-05-12 16:07:28 -0700 | [diff] [blame] | 10042 | // Note that this creates a finalizable dalvik.system.DexFile object and a corresponding |
| 10043 | // FinalizerReference which will never get cleaned up without a started runtime. |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 10044 | Handle<mirror::Object> h_dex_file = hs2.NewHandle( |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 10045 | cookie_field->GetDeclaringClass()->AllocObject(self)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 10046 | DCHECK(h_dex_file != nullptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 10047 | cookie_field->SetObject<false>(h_dex_file.Get(), h_long_array.Get()); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 10048 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 10049 | Handle<mirror::String> h_file_name = hs2.NewHandle( |
| 10050 | mirror::String::AllocFromModifiedUtf8(self, dex_file->GetLocation().c_str())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 10051 | DCHECK(h_file_name != nullptr); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 10052 | file_name_field->SetObject<false>(h_dex_file.Get(), h_file_name.Get()); |
| 10053 | |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 10054 | 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] | 10055 | DCHECK(h_element != nullptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 10056 | element_file_field->SetObject<false>(h_element.Get(), h_dex_file.Get()); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 10057 | |
| 10058 | h_dex_elements->Set(index, h_element.Get()); |
| 10059 | index++; |
| 10060 | } |
| 10061 | DCHECK_EQ(index, h_dex_elements->GetLength()); |
| 10062 | |
| 10063 | // Create DexPathList. |
| 10064 | Handle<mirror::Object> h_dex_path_list = hs.NewHandle( |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 10065 | dex_elements_field->GetDeclaringClass()->AllocObject(self)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 10066 | DCHECK(h_dex_path_list != nullptr); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 10067 | // Set elements. |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 10068 | 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] | 10069 | // Create an empty List for the "nativeLibraryDirectories," required for native tests. |
| 10070 | // Note: this code is uncommon(oatdump)/testing-only, so don't add further WellKnownClasses |
| 10071 | // elements. |
| 10072 | { |
| 10073 | ArtField* native_lib_dirs = dex_elements_field->GetDeclaringClass()-> |
| 10074 | FindDeclaredInstanceField("nativeLibraryDirectories", "Ljava/util/List;"); |
| 10075 | DCHECK(native_lib_dirs != nullptr); |
| 10076 | ObjPtr<mirror::Class> list_class = FindSystemClass(self, "Ljava/util/ArrayList;"); |
| 10077 | DCHECK(list_class != nullptr); |
| 10078 | { |
| 10079 | StackHandleScope<1> h_list_scope(self); |
| 10080 | Handle<mirror::Class> h_list_class(h_list_scope.NewHandle<mirror::Class>(list_class)); |
| 10081 | bool list_init = EnsureInitialized(self, h_list_class, true, true); |
| 10082 | DCHECK(list_init); |
| 10083 | list_class = h_list_class.Get(); |
| 10084 | } |
| 10085 | ObjPtr<mirror::Object> list_object = list_class->AllocObject(self); |
| 10086 | // Note: we leave the object uninitialized. This must never leak into any non-testing code, but |
| 10087 | // is fine for testing. While it violates a Java-code invariant (the elementData field is |
| 10088 | // normally never null), as long as one does not try to add elements, this will still |
| 10089 | // work. |
| 10090 | native_lib_dirs->SetObject<false>(h_dex_path_list.Get(), list_object); |
| 10091 | } |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 10092 | |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 10093 | // Create the class loader.. |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 10094 | Handle<mirror::ClassLoader> h_class_loader = hs.NewHandle<mirror::ClassLoader>( |
| 10095 | ObjPtr<mirror::ClassLoader>::DownCast(loader_class->AllocObject(self))); |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 10096 | DCHECK(h_class_loader != nullptr); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 10097 | // Set DexPathList. |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 10098 | ArtField* path_list_field = |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 10099 | jni::DecodeArtField(WellKnownClasses::dalvik_system_BaseDexClassLoader_pathList); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 10100 | DCHECK(path_list_field != nullptr); |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 10101 | 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] | 10102 | |
| 10103 | // Make a pretend boot-classpath. |
| 10104 | // TODO: Should we scan the image? |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 10105 | ArtField* const parent_field = |
Vladimir Marko | e300c4e | 2021-06-08 16:00:05 +0100 | [diff] [blame] | 10106 | jni::DecodeArtField(WellKnownClasses::java_lang_ClassLoader_parent); |
Roland Levillain | f39c9eb | 2015-05-26 15:02:07 +0100 | [diff] [blame] | 10107 | DCHECK(parent_field != nullptr); |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 10108 | if (parent_loader.Get() == nullptr) { |
| 10109 | ScopedObjectAccessUnchecked soa(self); |
| 10110 | ObjPtr<mirror::Object> boot_loader(soa.Decode<mirror::Class>( |
| 10111 | WellKnownClasses::java_lang_BootClassLoader)->AllocObject(self)); |
| 10112 | parent_field->SetObject<false>(h_class_loader.Get(), boot_loader); |
| 10113 | } else { |
| 10114 | parent_field->SetObject<false>(h_class_loader.Get(), parent_loader.Get()); |
| 10115 | } |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 10116 | |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 10117 | ArtField* shared_libraries_field = |
| 10118 | jni::DecodeArtField(WellKnownClasses::dalvik_system_BaseDexClassLoader_sharedLibraryLoaders); |
| 10119 | DCHECK(shared_libraries_field != nullptr); |
| 10120 | shared_libraries_field->SetObject<false>(h_class_loader.Get(), shared_libraries.Get()); |
| 10121 | |
Brad Stenning | 9c924e8 | 2021-10-11 19:09:00 -0700 | [diff] [blame] | 10122 | ArtField* shared_libraries_after_field = |
| 10123 | jni::DecodeArtField( |
| 10124 | WellKnownClasses::dalvik_system_BaseDexClassLoader_sharedLibraryLoadersAfter); |
| 10125 | DCHECK(shared_libraries_after_field != nullptr); |
| 10126 | shared_libraries_after_field->SetObject<false>(h_class_loader.Get(), |
| 10127 | shared_libraries_after.Get()); |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 10128 | return h_class_loader.Get(); |
| 10129 | } |
| 10130 | |
| 10131 | jobject ClassLinker::CreateWellKnownClassLoader(Thread* self, |
| 10132 | const std::vector<const DexFile*>& dex_files, |
| 10133 | jclass loader_class, |
Nicolas Geoffray | e167273 | 2018-11-30 01:09:49 +0000 | [diff] [blame] | 10134 | jobject parent_loader, |
Brad Stenning | 9c924e8 | 2021-10-11 19:09:00 -0700 | [diff] [blame] | 10135 | jobject shared_libraries, |
| 10136 | jobject shared_libraries_after) { |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 10137 | CHECK(self->GetJniEnv()->IsSameObject(loader_class, |
| 10138 | WellKnownClasses::dalvik_system_PathClassLoader) || |
| 10139 | self->GetJniEnv()->IsSameObject(loader_class, |
David Brazdil | 1a9ac53 | 2019-03-05 11:57:13 +0000 | [diff] [blame] | 10140 | WellKnownClasses::dalvik_system_DelegateLastClassLoader) || |
| 10141 | self->GetJniEnv()->IsSameObject(loader_class, |
| 10142 | WellKnownClasses::dalvik_system_InMemoryDexClassLoader)); |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 10143 | |
| 10144 | // SOAAlreadyRunnable is protected, and we need something to add a global reference. |
| 10145 | // We could move the jobject to the callers, but all call-sites do this... |
| 10146 | ScopedObjectAccessUnchecked soa(self); |
| 10147 | |
| 10148 | // For now, create a libcore-level DexFile for each ART DexFile. This "explodes" multidex. |
Brad Stenning | 9c924e8 | 2021-10-11 19:09:00 -0700 | [diff] [blame] | 10149 | StackHandleScope<5> hs(self); |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 10150 | |
| 10151 | Handle<mirror::Class> h_loader_class = |
| 10152 | hs.NewHandle<mirror::Class>(soa.Decode<mirror::Class>(loader_class)); |
Nicolas Geoffray | e167273 | 2018-11-30 01:09:49 +0000 | [diff] [blame] | 10153 | Handle<mirror::ClassLoader> h_parent = |
| 10154 | hs.NewHandle<mirror::ClassLoader>(soa.Decode<mirror::ClassLoader>(parent_loader)); |
| 10155 | Handle<mirror::ObjectArray<mirror::ClassLoader>> h_shared_libraries = |
| 10156 | hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::ClassLoader>>(shared_libraries)); |
Brad Stenning | 9c924e8 | 2021-10-11 19:09:00 -0700 | [diff] [blame] | 10157 | Handle<mirror::ObjectArray<mirror::ClassLoader>> h_shared_libraries_after = |
| 10158 | hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::ClassLoader>>(shared_libraries_after)); |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 10159 | |
| 10160 | ObjPtr<mirror::ClassLoader> loader = CreateWellKnownClassLoader( |
| 10161 | self, |
| 10162 | dex_files, |
| 10163 | h_loader_class, |
Nicolas Geoffray | e167273 | 2018-11-30 01:09:49 +0000 | [diff] [blame] | 10164 | h_parent, |
Brad Stenning | 9c924e8 | 2021-10-11 19:09:00 -0700 | [diff] [blame] | 10165 | h_shared_libraries, |
| 10166 | h_shared_libraries_after); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 10167 | |
| 10168 | // Make it a global ref and return. |
| 10169 | ScopedLocalRef<jobject> local_ref( |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 10170 | soa.Env(), soa.Env()->AddLocalReference<jobject>(loader)); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 10171 | return soa.Env()->NewGlobalRef(local_ref.get()); |
| 10172 | } |
| 10173 | |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 10174 | jobject ClassLinker::CreatePathClassLoader(Thread* self, |
| 10175 | const std::vector<const DexFile*>& dex_files) { |
| 10176 | return CreateWellKnownClassLoader(self, |
| 10177 | dex_files, |
| 10178 | WellKnownClasses::dalvik_system_PathClassLoader, |
| 10179 | nullptr); |
| 10180 | } |
| 10181 | |
Andreas Gampe | 8ac7595 | 2015-06-02 21:01:45 -0700 | [diff] [blame] | 10182 | void ClassLinker::DropFindArrayClassCache() { |
| 10183 | std::fill_n(find_array_class_cache_, kFindArrayCacheSize, GcRoot<mirror::Class>(nullptr)); |
| 10184 | find_array_class_cache_next_victim_ = 0; |
| 10185 | } |
| 10186 | |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 10187 | void ClassLinker::VisitClassLoaders(ClassLoaderVisitor* visitor) const { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 10188 | Thread* const self = Thread::Current(); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 10189 | for (const ClassLoaderData& data : class_loaders_) { |
Mathieu Chartier | 4843bd5 | 2015-10-01 17:08:44 -0700 | [diff] [blame] | 10190 | // 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] | 10191 | ObjPtr<mirror::ClassLoader> class_loader = ObjPtr<mirror::ClassLoader>::DownCast( |
| 10192 | self->DecodeJObject(data.weak_root)); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 10193 | if (class_loader != nullptr) { |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 10194 | visitor->Visit(class_loader); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 10195 | } |
| 10196 | } |
| 10197 | } |
| 10198 | |
Alexey Grebenkin | 252a4e4 | 2018-04-02 18:18:01 +0300 | [diff] [blame] | 10199 | void ClassLinker::VisitAllocators(AllocatorVisitor* visitor) const { |
| 10200 | for (const ClassLoaderData& data : class_loaders_) { |
| 10201 | LinearAlloc* alloc = data.allocator; |
| 10202 | if (alloc != nullptr && !visitor->Visit(alloc)) { |
| 10203 | break; |
| 10204 | } |
| 10205 | } |
| 10206 | } |
| 10207 | |
Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 10208 | void ClassLinker::InsertDexFileInToClassLoader(ObjPtr<mirror::Object> dex_file, |
| 10209 | ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 10210 | DCHECK(dex_file != nullptr); |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 10211 | Thread* const self = Thread::Current(); |
| 10212 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 10213 | ClassTable* const table = ClassTableForClassLoader(class_loader); |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 10214 | DCHECK(table != nullptr); |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 10215 | if (table->InsertStrongRoot(dex_file) && class_loader != nullptr) { |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 10216 | // It was not already inserted, perform the write barrier to let the GC know the class loader's |
| 10217 | // class table was modified. |
Mathieu Chartier | 88ea61e | 2018-06-20 17:45:41 -0700 | [diff] [blame] | 10218 | WriteBarrier::ForEveryFieldWrite(class_loader); |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 10219 | } |
| 10220 | } |
| 10221 | |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 10222 | void ClassLinker::CleanupClassLoaders() { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 10223 | Thread* const self = Thread::Current(); |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 10224 | std::vector<ClassLoaderData> to_delete; |
| 10225 | // Do the delete outside the lock to avoid lock violation in jit code cache. |
| 10226 | { |
| 10227 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 10228 | for (auto it = class_loaders_.begin(); it != class_loaders_.end(); ) { |
| 10229 | const ClassLoaderData& data = *it; |
| 10230 | // 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] | 10231 | ObjPtr<mirror::ClassLoader> class_loader = |
| 10232 | ObjPtr<mirror::ClassLoader>::DownCast(self->DecodeJObject(data.weak_root)); |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 10233 | if (class_loader != nullptr) { |
| 10234 | ++it; |
| 10235 | } else { |
| 10236 | VLOG(class_linker) << "Freeing class loader"; |
| 10237 | to_delete.push_back(data); |
| 10238 | it = class_loaders_.erase(it); |
| 10239 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 10240 | } |
| 10241 | } |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 10242 | for (ClassLoaderData& data : to_delete) { |
Alexey Grebenkin | be4c2bd | 2018-02-01 19:09:59 +0300 | [diff] [blame] | 10243 | // CHA unloading analysis and SingleImplementaion cleanups are required. |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 10244 | DeleteClassLoader(self, data, /*cleanup_cha=*/ true); |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 10245 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 10246 | } |
| 10247 | |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 10248 | class ClassLinker::FindVirtualMethodHolderVisitor : public ClassVisitor { |
| 10249 | public: |
| 10250 | FindVirtualMethodHolderVisitor(const ArtMethod* method, PointerSize pointer_size) |
| 10251 | : method_(method), |
| 10252 | pointer_size_(pointer_size) {} |
| 10253 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 10254 | bool operator()(ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_) override { |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 10255 | if (klass->GetVirtualMethodsSliceUnchecked(pointer_size_).Contains(method_)) { |
| 10256 | holder_ = klass; |
| 10257 | } |
| 10258 | // Return false to stop searching if holder_ is not null. |
| 10259 | return holder_ == nullptr; |
| 10260 | } |
| 10261 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 10262 | ObjPtr<mirror::Class> holder_ = nullptr; |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 10263 | const ArtMethod* const method_; |
| 10264 | const PointerSize pointer_size_; |
| 10265 | }; |
| 10266 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 10267 | ObjPtr<mirror::Class> ClassLinker::GetHoldingClassOfCopiedMethod(ArtMethod* method) { |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 10268 | ScopedTrace trace(__FUNCTION__); // Since this function is slow, have a trace to notify people. |
| 10269 | CHECK(method->IsCopied()); |
| 10270 | FindVirtualMethodHolderVisitor visitor(method, image_pointer_size_); |
| 10271 | VisitClasses(&visitor); |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 10272 | return visitor.holder_; |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 10273 | } |
| 10274 | |
Calin Juravle | 3378768 | 2019-07-26 14:27:18 -0700 | [diff] [blame] | 10275 | bool ClassLinker::DenyAccessBasedOnPublicSdk(ArtMethod* art_method ATTRIBUTE_UNUSED) const |
| 10276 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 10277 | // Should not be called on ClassLinker, only on AotClassLinker that overrides this. |
| 10278 | LOG(FATAL) << "UNREACHABLE"; |
| 10279 | UNREACHABLE(); |
| 10280 | } |
| 10281 | |
| 10282 | bool ClassLinker::DenyAccessBasedOnPublicSdk(ArtField* art_field ATTRIBUTE_UNUSED) const |
| 10283 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 10284 | // Should not be called on ClassLinker, only on AotClassLinker that overrides this. |
| 10285 | LOG(FATAL) << "UNREACHABLE"; |
| 10286 | UNREACHABLE(); |
| 10287 | } |
| 10288 | |
| 10289 | bool ClassLinker::DenyAccessBasedOnPublicSdk(const char* type_descriptor ATTRIBUTE_UNUSED) const { |
| 10290 | // Should not be called on ClassLinker, only on AotClassLinker that overrides this. |
| 10291 | LOG(FATAL) << "UNREACHABLE"; |
| 10292 | UNREACHABLE(); |
| 10293 | } |
| 10294 | |
Calin Juravle | 2c2724c | 2021-01-14 19:54:23 -0800 | [diff] [blame] | 10295 | void ClassLinker::SetEnablePublicSdkChecks(bool enabled ATTRIBUTE_UNUSED) { |
| 10296 | // Should not be called on ClassLinker, only on AotClassLinker that overrides this. |
| 10297 | LOG(FATAL) << "UNREACHABLE"; |
| 10298 | UNREACHABLE(); |
| 10299 | } |
| 10300 | |
Guo Li | 1ee0290 | 2022-07-13 15:57:31 +0800 | [diff] [blame] | 10301 | void ClassLinker::RemoveDexFromCaches(const DexFile& dex_file) { |
| 10302 | ReaderMutexLock mu(Thread::Current(), *Locks::dex_lock_); |
| 10303 | |
| 10304 | auto it = dex_caches_.find(&dex_file); |
| 10305 | if (it != dex_caches_.end()) { |
| 10306 | dex_caches_.erase(it); |
| 10307 | } |
| 10308 | } |
| 10309 | |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 10310 | // Instantiate ClassLinker::AllocClass. |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 10311 | template ObjPtr<mirror::Class> ClassLinker::AllocClass</* kMovable= */ true>( |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 10312 | Thread* self, |
| 10313 | ObjPtr<mirror::Class> java_lang_Class, |
| 10314 | uint32_t class_size); |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 10315 | template ObjPtr<mirror::Class> ClassLinker::AllocClass</* kMovable= */ false>( |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 10316 | Thread* self, |
| 10317 | ObjPtr<mirror::Class> java_lang_Class, |
| 10318 | uint32_t class_size); |
| 10319 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 10320 | } // namespace art |