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 | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1385 | auto it = image_interns_.find(GcRoot<mirror::String>(referred_str)); |
| 1386 | if (it != image_interns_.end() && it->Read() == referred_str) { |
| 1387 | ++count_; |
Chris Wailes | fbeef46 | 2018-10-19 14:16:35 -0700 | [diff] [blame] | 1388 | } |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1389 | } |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1390 | } |
| 1391 | |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1392 | void VisitRootIfNonNull( |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1393 | mirror::CompressedReference<mirror::Object>* root) const |
| 1394 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1395 | if (!root->IsNull()) { |
| 1396 | VisitRoot(root); |
| 1397 | } |
| 1398 | } |
| 1399 | |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1400 | void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1401 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1402 | TestObject(root->AsMirrorPtr()); |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1403 | } |
| 1404 | |
| 1405 | // Visit Class Fields |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1406 | void operator()(ObjPtr<mirror::Object> obj, |
| 1407 | MemberOffset offset, |
| 1408 | bool is_static ATTRIBUTE_UNUSED) const |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1409 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1410 | // References within image or across images don't need a read barrier. |
| 1411 | ObjPtr<mirror::Object> referred_obj = |
| 1412 | obj->GetFieldObject<mirror::Object, kVerifyNone, kWithoutReadBarrier>(offset); |
| 1413 | TestObject(referred_obj); |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1414 | } |
| 1415 | |
| 1416 | void operator()(ObjPtr<mirror::Class> klass ATTRIBUTE_UNUSED, |
| 1417 | ObjPtr<mirror::Reference> ref) const |
| 1418 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_) { |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1419 | operator()(ref, mirror::Reference::ReferentOffset(), /*is_static=*/ false); |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1420 | } |
| 1421 | |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1422 | size_t GetCount() const { |
| 1423 | return count_; |
| 1424 | } |
| 1425 | |
| 1426 | private: |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1427 | const gc::space::ImageSpace& space_; |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1428 | const InternTable::UnorderedSet& image_interns_; |
| 1429 | mutable size_t count_; // Modified from the `const` callbacks. |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1430 | }; |
| 1431 | |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1432 | /* |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1433 | * This function counts references to strings interned in the AppImage. |
| 1434 | * 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] | 1435 | */ |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1436 | size_t CountInternedStringReferences(gc::space::ImageSpace& space, |
| 1437 | const InternTable::UnorderedSet& image_interns) |
| 1438 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1439 | const gc::accounting::ContinuousSpaceBitmap* bitmap = space.GetMarkBitmap(); |
| 1440 | const ImageHeader& image_header = space.GetImageHeader(); |
| 1441 | const uint8_t* target_base = space.GetMemMap()->Begin(); |
| 1442 | const ImageSection& objects_section = image_header.GetObjectsSection(); |
Chris Wailes | fbeef46 | 2018-10-19 14:16:35 -0700 | [diff] [blame] | 1443 | |
| 1444 | auto objects_begin = reinterpret_cast<uintptr_t>(target_base + objects_section.Offset()); |
| 1445 | auto objects_end = reinterpret_cast<uintptr_t>(target_base + objects_section.End()); |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1446 | |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1447 | CountInternedStringReferencesVisitor visitor(space, image_interns); |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1448 | bitmap->VisitMarkedRange(objects_begin, |
| 1449 | objects_end, |
| 1450 | [&space, &visitor](mirror::Object* obj) |
| 1451 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1452 | if (space.HasAddress(obj)) { |
| 1453 | if (obj->IsDexCache()) { |
Chris Wailes | fbeef46 | 2018-10-19 14:16:35 -0700 | [diff] [blame] | 1454 | obj->VisitReferences</* kVisitNativeRoots= */ true, |
| 1455 | kVerifyNone, |
| 1456 | kWithoutReadBarrier>(visitor, visitor); |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1457 | } else { |
| 1458 | // Don't visit native roots for non-dex-cache as they can't contain |
| 1459 | // native references to strings. This is verified during compilation |
| 1460 | // by ImageWriter::VerifyNativeGCRootInvariants. |
Chris Wailes | fbeef46 | 2018-10-19 14:16:35 -0700 | [diff] [blame] | 1461 | obj->VisitReferences</* kVisitNativeRoots= */ false, |
| 1462 | kVerifyNone, |
| 1463 | kWithoutReadBarrier>(visitor, visitor); |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1464 | } |
| 1465 | } |
| 1466 | }); |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1467 | return visitor.GetCount(); |
| 1468 | } |
| 1469 | |
| 1470 | template <typename Visitor> |
| 1471 | static void VisitInternedStringReferences( |
| 1472 | gc::space::ImageSpace* space, |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1473 | const Visitor& visitor) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1474 | const uint8_t* target_base = space->Begin(); |
| 1475 | const ImageSection& sro_section = |
| 1476 | space->GetImageHeader().GetImageStringReferenceOffsetsSection(); |
| 1477 | const size_t num_string_offsets = sro_section.Size() / sizeof(AppImageReferenceOffsetInfo); |
| 1478 | |
| 1479 | VLOG(image) |
| 1480 | << "ClassLinker:AppImage:InternStrings:imageStringReferenceOffsetCount = " |
| 1481 | << num_string_offsets; |
| 1482 | |
| 1483 | const auto* sro_base = |
| 1484 | reinterpret_cast<const AppImageReferenceOffsetInfo*>(target_base + sro_section.Offset()); |
| 1485 | |
| 1486 | for (size_t offset_index = 0; offset_index < num_string_offsets; ++offset_index) { |
| 1487 | uint32_t base_offset = sro_base[offset_index].first; |
| 1488 | |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 1489 | uint32_t raw_member_offset = sro_base[offset_index].second; |
| 1490 | DCHECK_ALIGNED(base_offset, 2); |
| 1491 | DCHECK_ALIGNED(raw_member_offset, 2); |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1492 | |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 1493 | ObjPtr<mirror::Object> obj_ptr = |
| 1494 | reinterpret_cast<mirror::Object*>(space->Begin() + base_offset); |
| 1495 | MemberOffset member_offset(raw_member_offset); |
| 1496 | ObjPtr<mirror::String> referred_string = |
| 1497 | obj_ptr->GetFieldObject<mirror::String, |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1498 | kVerifyNone, |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 1499 | kWithoutReadBarrier, |
| 1500 | /* kIsVolatile= */ false>(member_offset); |
| 1501 | DCHECK(referred_string != nullptr); |
| 1502 | |
| 1503 | ObjPtr<mirror::String> visited = visitor(referred_string); |
| 1504 | if (visited != referred_string) { |
| 1505 | obj_ptr->SetFieldObject</* kTransactionActive= */ false, |
| 1506 | /* kCheckTransaction= */ false, |
| 1507 | kVerifyNone, |
| 1508 | /* kIsVolatile= */ false>(member_offset, visited); |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1509 | } |
| 1510 | } |
| 1511 | } |
| 1512 | |
| 1513 | static void VerifyInternedStringReferences(gc::space::ImageSpace* space) |
| 1514 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1515 | InternTable::UnorderedSet image_interns; |
| 1516 | const ImageSection& section = space->GetImageHeader().GetInternedStringsSection(); |
| 1517 | if (section.Size() > 0) { |
| 1518 | size_t read_count; |
| 1519 | const uint8_t* data = space->Begin() + section.Offset(); |
| 1520 | InternTable::UnorderedSet image_set(data, /*make_copy_of_data=*/ false, &read_count); |
| 1521 | image_set.swap(image_interns); |
| 1522 | } |
| 1523 | size_t num_recorded_refs = 0u; |
| 1524 | VisitInternedStringReferences( |
| 1525 | space, |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1526 | [&image_interns, &num_recorded_refs](ObjPtr<mirror::String> str) |
| 1527 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1528 | auto it = image_interns.find(GcRoot<mirror::String>(str)); |
| 1529 | CHECK(it != image_interns.end()); |
| 1530 | CHECK(it->Read() == str); |
| 1531 | ++num_recorded_refs; |
| 1532 | return str; |
| 1533 | }); |
| 1534 | size_t num_found_refs = CountInternedStringReferences(*space, image_interns); |
| 1535 | CHECK_EQ(num_recorded_refs, num_found_refs); |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1536 | } |
| 1537 | |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1538 | // new_class_set is the set of classes that were read from the class table section in the image. |
| 1539 | // If there was no class table section, it is null. |
| 1540 | // 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] | 1541 | class AppImageLoadingHelper { |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1542 | public: |
Vladimir Marko | 0f3c700 | 2017-09-07 14:15:56 +0100 | [diff] [blame] | 1543 | static void Update( |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1544 | ClassLinker* class_linker, |
| 1545 | gc::space::ImageSpace* space, |
| 1546 | Handle<mirror::ClassLoader> class_loader, |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 1547 | Handle<mirror::ObjectArray<mirror::DexCache>> dex_caches) |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1548 | REQUIRES(!Locks::dex_lock_) |
| 1549 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 1550 | |
Chris Wailes | fbeef46 | 2018-10-19 14:16:35 -0700 | [diff] [blame] | 1551 | static void HandleAppImageStrings(gc::space::ImageSpace* space) |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 1552 | REQUIRES_SHARED(Locks::mutator_lock_); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1553 | }; |
| 1554 | |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 1555 | void AppImageLoadingHelper::Update( |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1556 | ClassLinker* class_linker, |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1557 | gc::space::ImageSpace* space, |
| 1558 | Handle<mirror::ClassLoader> class_loader, |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 1559 | Handle<mirror::ObjectArray<mirror::DexCache>> dex_caches) |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1560 | REQUIRES(!Locks::dex_lock_) |
| 1561 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Chris Wailes | 2386636 | 2018-08-22 16:16:58 -0700 | [diff] [blame] | 1562 | ScopedTrace app_image_timing("AppImage:Updating"); |
| 1563 | |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1564 | if (kIsDebugBuild && ClassLinker::kAppImageMayContainStrings) { |
| 1565 | // In debug build, verify the string references before applying |
| 1566 | // the Runtime::LoadAppImageStartupCache() option. |
| 1567 | VerifyInternedStringReferences(space); |
| 1568 | } |
| 1569 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1570 | Thread* const self = Thread::Current(); |
Mathieu Chartier | a88abfa | 2019-02-04 11:08:29 -0800 | [diff] [blame] | 1571 | Runtime* const runtime = Runtime::Current(); |
| 1572 | gc::Heap* const heap = runtime->GetHeap(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1573 | const ImageHeader& header = space->GetImageHeader(); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1574 | { |
Vladimir Marko | 0f3c700 | 2017-09-07 14:15:56 +0100 | [diff] [blame] | 1575 | // Register dex caches with the class loader. |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1576 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Alex Light | a9bbc08 | 2019-11-14 14:51:41 -0800 | [diff] [blame] | 1577 | for (auto dex_cache : dex_caches.Iterate<mirror::DexCache>()) { |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1578 | const DexFile* const dex_file = dex_cache->GetDexFile(); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1579 | { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 1580 | WriterMutexLock mu2(self, *Locks::dex_lock_); |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 1581 | CHECK(class_linker->FindDexCacheDataLocked(*dex_file) == nullptr); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1582 | class_linker->RegisterDexFileLocked(*dex_file, dex_cache, class_loader.Get()); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1583 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1584 | } |
Mathieu Chartier | a0b9521 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 1585 | } |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1586 | |
Mathieu Chartier | 0933cc5 | 2018-03-23 14:25:08 -0700 | [diff] [blame] | 1587 | if (ClassLinker::kAppImageMayContainStrings) { |
Chris Wailes | fbeef46 | 2018-10-19 14:16:35 -0700 | [diff] [blame] | 1588 | HandleAppImageStrings(space); |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1589 | } |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1590 | |
Mathieu Chartier | a0b9521 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 1591 | if (kVerifyArtMethodDeclaringClasses) { |
Chris Wailes | 2386636 | 2018-08-22 16:16:58 -0700 | [diff] [blame] | 1592 | ScopedTrace timing("AppImage:VerifyDeclaringClasses"); |
Mathieu Chartier | a0b9521 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 1593 | ReaderMutexLock rmu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 9d5956a | 2019-03-22 11:29:08 -0700 | [diff] [blame] | 1594 | gc::accounting::HeapBitmap* live_bitmap = heap->GetLiveBitmap(); |
| 1595 | header.VisitPackedArtMethods([&](ArtMethod& method) |
| 1596 | REQUIRES_SHARED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) { |
| 1597 | ObjPtr<mirror::Class> klass = method.GetDeclaringClassUnchecked(); |
| 1598 | if (klass != nullptr) { |
| 1599 | CHECK(live_bitmap->Test(klass.Ptr())) << "Image method has unmarked declaring class"; |
| 1600 | } |
| 1601 | }, space->Begin(), kRuntimePointerSize); |
Mathieu Chartier | 03c1dd9 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 1602 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1603 | } |
| 1604 | |
Chris Wailes | fbeef46 | 2018-10-19 14:16:35 -0700 | [diff] [blame] | 1605 | void AppImageLoadingHelper::HandleAppImageStrings(gc::space::ImageSpace* space) { |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 1606 | // Iterate over the string reference offsets stored in the image and intern |
| 1607 | // the strings they point to. |
| 1608 | ScopedTrace timing("AppImage:InternString"); |
| 1609 | |
Mathieu Chartier | a88abfa | 2019-02-04 11:08:29 -0800 | [diff] [blame] | 1610 | Runtime* const runtime = Runtime::Current(); |
| 1611 | InternTable* const intern_table = runtime->GetInternTable(); |
| 1612 | |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 1613 | // Add the intern table, removing any conflicts. For conflicts, store the new address in a map |
| 1614 | // for faster lookup. |
| 1615 | // TODO: Optimize with a bitmap or bloom filter |
| 1616 | SafeMap<mirror::String*, mirror::String*> intern_remap; |
Mathieu Chartier | a88abfa | 2019-02-04 11:08:29 -0800 | [diff] [blame] | 1617 | auto func = [&](InternTable::UnorderedSet& interns) |
Mathieu Chartier | 41c0808 | 2018-10-31 11:50:26 -0700 | [diff] [blame] | 1618 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 1619 | REQUIRES(Locks::intern_table_lock_) { |
Mathieu Chartier | 8fc7558 | 2018-11-01 14:21:33 -0700 | [diff] [blame] | 1620 | const size_t non_boot_image_strings = intern_table->CountInterns( |
| 1621 | /*visit_boot_images=*/false, |
| 1622 | /*visit_non_boot_images=*/true); |
Chris Wailes | fbeef46 | 2018-10-19 14:16:35 -0700 | [diff] [blame] | 1623 | VLOG(image) << "AppImage:stringsInInternTableSize = " << interns.size(); |
Mathieu Chartier | 8fc7558 | 2018-11-01 14:21:33 -0700 | [diff] [blame] | 1624 | VLOG(image) << "AppImage:nonBootImageInternStrings = " << non_boot_image_strings; |
| 1625 | // Visit the smaller of the two sets to compute the intersection. |
| 1626 | if (interns.size() < non_boot_image_strings) { |
| 1627 | for (auto it = interns.begin(); it != interns.end(); ) { |
| 1628 | ObjPtr<mirror::String> string = it->Read(); |
| 1629 | ObjPtr<mirror::String> existing = intern_table->LookupWeakLocked(string); |
| 1630 | if (existing == nullptr) { |
| 1631 | existing = intern_table->LookupStrongLocked(string); |
| 1632 | } |
| 1633 | if (existing != nullptr) { |
| 1634 | intern_remap.Put(string.Ptr(), existing.Ptr()); |
| 1635 | it = interns.erase(it); |
| 1636 | } else { |
| 1637 | ++it; |
| 1638 | } |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 1639 | } |
Mathieu Chartier | 8fc7558 | 2018-11-01 14:21:33 -0700 | [diff] [blame] | 1640 | } else { |
| 1641 | intern_table->VisitInterns([&](const GcRoot<mirror::String>& root) |
| 1642 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 1643 | REQUIRES(Locks::intern_table_lock_) { |
| 1644 | auto it = interns.find(root); |
| 1645 | if (it != interns.end()) { |
| 1646 | ObjPtr<mirror::String> existing = root.Read(); |
| 1647 | intern_remap.Put(it->Read(), existing.Ptr()); |
| 1648 | it = interns.erase(it); |
| 1649 | } |
| 1650 | }, /*visit_boot_images=*/false, /*visit_non_boot_images=*/true); |
| 1651 | } |
David Srbecky | 346fd96 | 2020-07-27 16:51:00 +0100 | [diff] [blame] | 1652 | // Consistency check to ensure correctness. |
Mathieu Chartier | 8fc7558 | 2018-11-01 14:21:33 -0700 | [diff] [blame] | 1653 | if (kIsDebugBuild) { |
| 1654 | for (GcRoot<mirror::String>& root : interns) { |
| 1655 | ObjPtr<mirror::String> string = root.Read(); |
| 1656 | CHECK(intern_table->LookupWeakLocked(string) == nullptr) << string->ToModifiedUtf8(); |
| 1657 | CHECK(intern_table->LookupStrongLocked(string) == nullptr) << string->ToModifiedUtf8(); |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 1658 | } |
| 1659 | } |
Mathieu Chartier | a88abfa | 2019-02-04 11:08:29 -0800 | [diff] [blame] | 1660 | }; |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1661 | intern_table->AddImageStringsToTable(space, func); |
| 1662 | if (!intern_remap.empty()) { |
Mathieu Chartier | a88abfa | 2019-02-04 11:08:29 -0800 | [diff] [blame] | 1663 | VLOG(image) << "AppImage:conflictingInternStrings = " << intern_remap.size(); |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1664 | VisitInternedStringReferences( |
| 1665 | space, |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1666 | [&intern_remap](ObjPtr<mirror::String> str) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1667 | auto it = intern_remap.find(str.Ptr()); |
| 1668 | if (it != intern_remap.end()) { |
| 1669 | return ObjPtr<mirror::String>(it->second); |
| 1670 | } |
| 1671 | return str; |
| 1672 | }); |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 1673 | } |
| 1674 | } |
| 1675 | |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1676 | static std::unique_ptr<const DexFile> OpenOatDexFile(const OatFile* oat_file, |
| 1677 | const char* location, |
| 1678 | std::string* error_msg) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1679 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1680 | DCHECK(error_msg != nullptr); |
| 1681 | std::unique_ptr<const DexFile> dex_file; |
Andreas Gampe | b40d361 | 2018-06-26 15:49:42 -0700 | [diff] [blame] | 1682 | const OatDexFile* oat_dex_file = oat_file->GetOatDexFile(location, nullptr, error_msg); |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1683 | if (oat_dex_file == nullptr) { |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1684 | return std::unique_ptr<const DexFile>(); |
| 1685 | } |
| 1686 | std::string inner_error_msg; |
| 1687 | dex_file = oat_dex_file->OpenDexFile(&inner_error_msg); |
| 1688 | if (dex_file == nullptr) { |
| 1689 | *error_msg = StringPrintf("Failed to open dex file %s from within oat file %s error '%s'", |
| 1690 | location, |
| 1691 | oat_file->GetLocation().c_str(), |
| 1692 | inner_error_msg.c_str()); |
| 1693 | return std::unique_ptr<const DexFile>(); |
| 1694 | } |
| 1695 | |
| 1696 | if (dex_file->GetLocationChecksum() != oat_dex_file->GetDexFileLocationChecksum()) { |
| 1697 | *error_msg = StringPrintf("Checksums do not match for %s: %x vs %x", |
| 1698 | location, |
| 1699 | dex_file->GetLocationChecksum(), |
| 1700 | oat_dex_file->GetDexFileLocationChecksum()); |
| 1701 | return std::unique_ptr<const DexFile>(); |
| 1702 | } |
| 1703 | return dex_file; |
| 1704 | } |
| 1705 | |
| 1706 | bool ClassLinker::OpenImageDexFiles(gc::space::ImageSpace* space, |
| 1707 | std::vector<std::unique_ptr<const DexFile>>* out_dex_files, |
| 1708 | std::string* error_msg) { |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 1709 | ScopedAssertNoThreadSuspension nts(__FUNCTION__); |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1710 | const ImageHeader& header = space->GetImageHeader(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1711 | ObjPtr<mirror::Object> dex_caches_object = header.GetImageRoot(ImageHeader::kDexCaches); |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1712 | DCHECK(dex_caches_object != nullptr); |
Vladimir Marko | 4617d58 | 2019-03-28 13:48:31 +0000 | [diff] [blame] | 1713 | ObjPtr<mirror::ObjectArray<mirror::DexCache>> dex_caches = |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1714 | dex_caches_object->AsObjectArray<mirror::DexCache>(); |
| 1715 | const OatFile* oat_file = space->GetOatFile(); |
Alex Light | a9bbc08 | 2019-11-14 14:51:41 -0800 | [diff] [blame] | 1716 | for (auto dex_cache : dex_caches->Iterate()) { |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1717 | std::string dex_file_location(dex_cache->GetLocation()->ToModifiedUtf8()); |
| 1718 | std::unique_ptr<const DexFile> dex_file = OpenOatDexFile(oat_file, |
| 1719 | dex_file_location.c_str(), |
| 1720 | error_msg); |
| 1721 | if (dex_file == nullptr) { |
| 1722 | return false; |
| 1723 | } |
| 1724 | dex_cache->SetDexFile(dex_file.get()); |
| 1725 | out_dex_files->push_back(std::move(dex_file)); |
| 1726 | } |
| 1727 | return true; |
| 1728 | } |
| 1729 | |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1730 | // Helper class for ArtMethod checks when adding an image. Keeps all required functionality |
| 1731 | // together and caches some intermediate results. |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 1732 | class ImageChecker final { |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1733 | public: |
| 1734 | static void CheckObjects(gc::Heap* heap, ClassLinker* class_linker) |
| 1735 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 1736 | ImageChecker ic(heap, class_linker); |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 1737 | auto visitor = [&](mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1738 | DCHECK(obj != nullptr); |
| 1739 | CHECK(obj->GetClass() != nullptr) << "Null class in object " << obj; |
| 1740 | CHECK(obj->GetClass()->GetClass() != nullptr) << "Null class class " << obj; |
| 1741 | if (obj->IsClass()) { |
| 1742 | auto klass = obj->AsClass(); |
| 1743 | for (ArtField& field : klass->GetIFields()) { |
| 1744 | CHECK_EQ(field.GetDeclaringClass(), klass); |
| 1745 | } |
| 1746 | for (ArtField& field : klass->GetSFields()) { |
| 1747 | CHECK_EQ(field.GetDeclaringClass(), klass); |
| 1748 | } |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 1749 | const PointerSize pointer_size = ic.pointer_size_; |
Vladimir Marko | c524e9e | 2019-03-26 10:54:50 +0000 | [diff] [blame] | 1750 | for (ArtMethod& m : klass->GetMethods(pointer_size)) { |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 1751 | ic.CheckArtMethod(&m, klass); |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 1752 | } |
Vladimir Marko | c524e9e | 2019-03-26 10:54:50 +0000 | [diff] [blame] | 1753 | ObjPtr<mirror::PointerArray> vtable = klass->GetVTable(); |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 1754 | if (vtable != nullptr) { |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 1755 | ic.CheckArtMethodPointerArray(vtable, nullptr); |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 1756 | } |
| 1757 | if (klass->ShouldHaveImt()) { |
| 1758 | ImTable* imt = klass->GetImt(pointer_size); |
| 1759 | for (size_t i = 0; i < ImTable::kSize; ++i) { |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 1760 | ic.CheckArtMethod(imt->Get(i, pointer_size), nullptr); |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 1761 | } |
| 1762 | } |
| 1763 | if (klass->ShouldHaveEmbeddedVTable()) { |
| 1764 | for (int32_t i = 0; i < klass->GetEmbeddedVTableLength(); ++i) { |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 1765 | ic.CheckArtMethod(klass->GetEmbeddedVTableEntry(i, pointer_size), nullptr); |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 1766 | } |
| 1767 | } |
Vladimir Marko | c524e9e | 2019-03-26 10:54:50 +0000 | [diff] [blame] | 1768 | ObjPtr<mirror::IfTable> iftable = klass->GetIfTable(); |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 1769 | for (int32_t i = 0; i < klass->GetIfTableCount(); ++i) { |
| 1770 | if (iftable->GetMethodArrayCount(i) > 0) { |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 1771 | ic.CheckArtMethodPointerArray(iftable->GetMethodArray(i), nullptr); |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 1772 | } |
| 1773 | } |
| 1774 | } |
| 1775 | }; |
| 1776 | heap->VisitObjects(visitor); |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1777 | } |
| 1778 | |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1779 | private: |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 1780 | ImageChecker(gc::Heap* heap, ClassLinker* class_linker) |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1781 | : spaces_(heap->GetBootImageSpaces()), |
| 1782 | pointer_size_(class_linker->GetImagePointerSize()) { |
| 1783 | space_begin_.reserve(spaces_.size()); |
| 1784 | method_sections_.reserve(spaces_.size()); |
| 1785 | runtime_method_sections_.reserve(spaces_.size()); |
| 1786 | for (gc::space::ImageSpace* space : spaces_) { |
| 1787 | space_begin_.push_back(space->Begin()); |
| 1788 | auto& header = space->GetImageHeader(); |
| 1789 | method_sections_.push_back(&header.GetMethodsSection()); |
| 1790 | runtime_method_sections_.push_back(&header.GetRuntimeMethodsSection()); |
| 1791 | } |
| 1792 | } |
| 1793 | |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 1794 | void CheckArtMethod(ArtMethod* m, ObjPtr<mirror::Class> expected_class) |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1795 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1796 | if (m->IsRuntimeMethod()) { |
| 1797 | ObjPtr<mirror::Class> declaring_class = m->GetDeclaringClassUnchecked(); |
| 1798 | CHECK(declaring_class == nullptr) << declaring_class << " " << m->PrettyMethod(); |
| 1799 | } else if (m->IsCopied()) { |
| 1800 | CHECK(m->GetDeclaringClass() != nullptr) << m->PrettyMethod(); |
| 1801 | } else if (expected_class != nullptr) { |
| 1802 | CHECK_EQ(m->GetDeclaringClassUnchecked(), expected_class) << m->PrettyMethod(); |
| 1803 | } |
| 1804 | if (!spaces_.empty()) { |
| 1805 | bool contains = false; |
| 1806 | for (size_t i = 0; !contains && i != space_begin_.size(); ++i) { |
| 1807 | const size_t offset = reinterpret_cast<uint8_t*>(m) - space_begin_[i]; |
| 1808 | contains = method_sections_[i]->Contains(offset) || |
| 1809 | runtime_method_sections_[i]->Contains(offset); |
| 1810 | } |
| 1811 | CHECK(contains) << m << " not found"; |
| 1812 | } |
| 1813 | } |
| 1814 | |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 1815 | void CheckArtMethodPointerArray(ObjPtr<mirror::PointerArray> arr, |
| 1816 | ObjPtr<mirror::Class> expected_class) |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1817 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1818 | CHECK(arr != nullptr); |
| 1819 | for (int32_t j = 0; j < arr->GetLength(); ++j) { |
| 1820 | auto* method = arr->GetElementPtrSize<ArtMethod*>(j, pointer_size_); |
| 1821 | // expected_class == null means we are a dex cache. |
| 1822 | if (expected_class != nullptr) { |
| 1823 | CHECK(method != nullptr); |
| 1824 | } |
| 1825 | if (method != nullptr) { |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 1826 | CheckArtMethod(method, expected_class); |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1827 | } |
| 1828 | } |
| 1829 | } |
| 1830 | |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1831 | const std::vector<gc::space::ImageSpace*>& spaces_; |
| 1832 | const PointerSize pointer_size_; |
| 1833 | |
| 1834 | // Cached sections from the spaces. |
| 1835 | std::vector<const uint8_t*> space_begin_; |
| 1836 | std::vector<const ImageSection*> method_sections_; |
| 1837 | std::vector<const ImageSection*> runtime_method_sections_; |
| 1838 | }; |
| 1839 | |
Andreas Gampe | be7af22 | 2017-07-25 09:57:28 -0700 | [diff] [blame] | 1840 | static void VerifyAppImage(const ImageHeader& header, |
| 1841 | const Handle<mirror::ClassLoader>& class_loader, |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 1842 | ClassTable* class_table, |
| 1843 | gc::space::ImageSpace* space) |
Andreas Gampe | be7af22 | 2017-07-25 09:57:28 -0700 | [diff] [blame] | 1844 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 9d5956a | 2019-03-22 11:29:08 -0700 | [diff] [blame] | 1845 | header.VisitPackedArtMethods([&](ArtMethod& method) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1846 | ObjPtr<mirror::Class> klass = method.GetDeclaringClass(); |
| 1847 | if (klass != nullptr && !Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(klass)) { |
| 1848 | CHECK_EQ(class_table->LookupByDescriptor(klass), klass) |
| 1849 | << mirror::Class::PrettyClass(klass); |
| 1850 | } |
| 1851 | }, space->Begin(), kRuntimePointerSize); |
Andreas Gampe | be7af22 | 2017-07-25 09:57:28 -0700 | [diff] [blame] | 1852 | { |
| 1853 | // Verify that all direct interfaces of classes in the class table are also resolved. |
| 1854 | std::vector<ObjPtr<mirror::Class>> classes; |
| 1855 | auto verify_direct_interfaces_in_table = [&](ObjPtr<mirror::Class> klass) |
| 1856 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1857 | if (!klass->IsPrimitive() && klass->GetClassLoader() == class_loader.Get()) { |
| 1858 | classes.push_back(klass); |
| 1859 | } |
| 1860 | return true; |
| 1861 | }; |
| 1862 | class_table->Visit(verify_direct_interfaces_in_table); |
Andreas Gampe | be7af22 | 2017-07-25 09:57:28 -0700 | [diff] [blame] | 1863 | for (ObjPtr<mirror::Class> klass : classes) { |
| 1864 | for (uint32_t i = 0, num = klass->NumDirectInterfaces(); i != num; ++i) { |
Vladimir Marko | b10668c | 2021-06-10 09:52:53 +0100 | [diff] [blame] | 1865 | CHECK(klass->GetDirectInterface(i) != nullptr) |
Andreas Gampe | be7af22 | 2017-07-25 09:57:28 -0700 | [diff] [blame] | 1866 | << klass->PrettyDescriptor() << " iface #" << i; |
| 1867 | } |
| 1868 | } |
| 1869 | } |
Andreas Gampe | be7af22 | 2017-07-25 09:57:28 -0700 | [diff] [blame] | 1870 | } |
| 1871 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1872 | bool ClassLinker::AddImageSpace( |
| 1873 | gc::space::ImageSpace* space, |
| 1874 | Handle<mirror::ClassLoader> class_loader, |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1875 | std::vector<std::unique_ptr<const DexFile>>* out_dex_files, |
| 1876 | std::string* error_msg) { |
| 1877 | DCHECK(out_dex_files != nullptr); |
| 1878 | DCHECK(error_msg != nullptr); |
| 1879 | const uint64_t start_time = NanoTime(); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 1880 | const bool app_image = class_loader != nullptr; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1881 | const ImageHeader& header = space->GetImageHeader(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1882 | ObjPtr<mirror::Object> dex_caches_object = header.GetImageRoot(ImageHeader::kDexCaches); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1883 | DCHECK(dex_caches_object != nullptr); |
| 1884 | Runtime* const runtime = Runtime::Current(); |
| 1885 | gc::Heap* const heap = runtime->GetHeap(); |
| 1886 | Thread* const self = Thread::Current(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1887 | // Check that the image is what we are expecting. |
| 1888 | if (image_pointer_size_ != space->GetImageHeader().GetPointerSize()) { |
| 1889 | *error_msg = StringPrintf("Application image pointer size does not match runtime: %zu vs %zu", |
| 1890 | static_cast<size_t>(space->GetImageHeader().GetPointerSize()), |
| 1891 | image_pointer_size_); |
| 1892 | return false; |
| 1893 | } |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1894 | size_t expected_image_roots = ImageHeader::NumberOfImageRoots(app_image); |
| 1895 | if (static_cast<size_t>(header.GetImageRoots()->GetLength()) != expected_image_roots) { |
| 1896 | *error_msg = StringPrintf("Expected %zu image roots but got %d", |
| 1897 | expected_image_roots, |
| 1898 | header.GetImageRoots()->GetLength()); |
| 1899 | return false; |
| 1900 | } |
| 1901 | StackHandleScope<3> hs(self); |
| 1902 | Handle<mirror::ObjectArray<mirror::DexCache>> dex_caches( |
| 1903 | hs.NewHandle(dex_caches_object->AsObjectArray<mirror::DexCache>())); |
| 1904 | Handle<mirror::ObjectArray<mirror::Class>> class_roots(hs.NewHandle( |
| 1905 | header.GetImageRoot(ImageHeader::kClassRoots)->AsObjectArray<mirror::Class>())); |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1906 | MutableHandle<mirror::ClassLoader> image_class_loader(hs.NewHandle( |
Vladimir Marko | f75613c | 2018-06-05 12:51:04 +0100 | [diff] [blame] | 1907 | app_image ? header.GetImageRoot(ImageHeader::kAppImageClassLoader)->AsClassLoader() |
| 1908 | : nullptr)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 1909 | DCHECK(class_roots != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 1910 | if (class_roots->GetLength() != static_cast<int32_t>(ClassRoot::kMax)) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1911 | *error_msg = StringPrintf("Expected %d class roots but got %d", |
| 1912 | class_roots->GetLength(), |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 1913 | static_cast<int32_t>(ClassRoot::kMax)); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1914 | return false; |
| 1915 | } |
| 1916 | // 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] | 1917 | ObjPtr<mirror::ObjectArray<mirror::Class>> existing_class_roots = GetClassRoots(); |
| 1918 | for (size_t i = 0; i < static_cast<size_t>(ClassRoot::kMax); i++) { |
| 1919 | 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] | 1920 | *error_msg = "App image class roots must have pointer equality with runtime ones."; |
| 1921 | return false; |
| 1922 | } |
| 1923 | } |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1924 | const OatFile* oat_file = space->GetOatFile(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1925 | if (oat_file->GetOatHeader().GetDexFileCount() != |
| 1926 | static_cast<uint32_t>(dex_caches->GetLength())) { |
| 1927 | *error_msg = "Dex cache count and dex file count mismatch while trying to initialize from " |
| 1928 | "image"; |
| 1929 | return false; |
| 1930 | } |
| 1931 | |
Alex Light | a9bbc08 | 2019-11-14 14:51:41 -0800 | [diff] [blame] | 1932 | for (auto dex_cache : dex_caches.Iterate<mirror::DexCache>()) { |
David Brazdil | 3e8aae0 | 2019-03-26 18:48:02 +0000 | [diff] [blame] | 1933 | std::string dex_file_location = dex_cache->GetLocation()->ToModifiedUtf8(); |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1934 | std::unique_ptr<const DexFile> dex_file = OpenOatDexFile(oat_file, |
| 1935 | dex_file_location.c_str(), |
| 1936 | error_msg); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1937 | if (dex_file == nullptr) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1938 | return false; |
| 1939 | } |
| 1940 | |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 1941 | { |
David Srbecky | 33df0e3 | 2021-09-30 14:36:32 +0000 | [diff] [blame] | 1942 | // Native fields are all null. Initialize them. |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 1943 | WriterMutexLock mu(self, *Locks::dex_lock_); |
David Srbecky | 33df0e3 | 2021-09-30 14:36:32 +0000 | [diff] [blame] | 1944 | dex_cache->Initialize(dex_file.get(), class_loader.Get()); |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 1945 | } |
| 1946 | if (!app_image) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1947 | // Register dex files, keep track of existing ones that are conflicts. |
Mathieu Chartier | 0a19e21 | 2019-11-27 14:35:24 -0800 | [diff] [blame] | 1948 | AppendToBootClassPath(dex_file.get(), dex_cache); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1949 | } |
| 1950 | out_dex_files->push_back(std::move(dex_file)); |
| 1951 | } |
| 1952 | |
| 1953 | if (app_image) { |
| 1954 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Nicolas Geoffray | f0d3002 | 2018-11-20 17:45:38 +0000 | [diff] [blame] | 1955 | ScopedAssertNoThreadSuspension sants("Checking app image", soa.Self()); |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1956 | if (IsBootClassLoader(soa, image_class_loader.Get())) { |
| 1957 | *error_msg = "Unexpected BootClassLoader in app image"; |
| 1958 | return false; |
| 1959 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1960 | } |
| 1961 | |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 1962 | if (kCheckImageObjects) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1963 | if (!app_image) { |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 1964 | ImageChecker::CheckObjects(heap, this); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1965 | } |
| 1966 | } |
| 1967 | |
| 1968 | // Set entry point to interpreter if in InterpretOnly mode. |
| 1969 | if (!runtime->IsAotCompiler() && runtime->GetInstrumentation()->InterpretOnly()) { |
Mathieu Chartier | 9d5956a | 2019-03-22 11:29:08 -0700 | [diff] [blame] | 1970 | // Set image methods' entry point to interpreter. |
| 1971 | header.VisitPackedArtMethods([&](ArtMethod& method) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1972 | if (!method.IsRuntimeMethod()) { |
| 1973 | DCHECK(method.GetDeclaringClass() != nullptr); |
Ulya Trafimovich | 5439f05 | 2020-07-29 10:03:46 +0100 | [diff] [blame] | 1974 | if (!method.IsNative() && !method.IsResolutionMethod()) { |
| 1975 | method.SetEntryPointFromQuickCompiledCodePtrSize(GetQuickToInterpreterBridge(), |
| 1976 | image_pointer_size_); |
Mathieu Chartier | 9d5956a | 2019-03-22 11:29:08 -0700 | [diff] [blame] | 1977 | } |
| 1978 | } |
| 1979 | }, space->Begin(), image_pointer_size_); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1980 | } |
| 1981 | |
Nicolas Geoffray | 4717175 | 2020-08-31 15:03:20 +0100 | [diff] [blame] | 1982 | if (!runtime->IsAotCompiler()) { |
Nicolas Geoffray | bd728b0 | 2021-01-27 13:21:35 +0000 | [diff] [blame] | 1983 | ScopedTrace trace("AppImage:UpdateCodeItemAndNterp"); |
Nicolas Geoffray | 4717175 | 2020-08-31 15:03:20 +0100 | [diff] [blame] | 1984 | bool can_use_nterp = interpreter::CanRuntimeUseNterp(); |
Nicolas Geoffray | 58f916c | 2021-11-15 14:02:07 +0000 | [diff] [blame] | 1985 | uint16_t hotness_threshold = runtime->GetJITOptions()->GetWarmupThreshold(); |
Nicolas Geoffray | 7e2c963 | 2020-01-09 13:41:10 +0000 | [diff] [blame] | 1986 | header.VisitPackedArtMethods([&](ArtMethod& method) REQUIRES_SHARED(Locks::mutator_lock_) { |
Nicolas Geoffray | 4717175 | 2020-08-31 15:03:20 +0100 | [diff] [blame] | 1987 | // In the image, the `data` pointer field of the ArtMethod contains the code |
| 1988 | // item offset. Change this to the actual pointer to the code item. |
| 1989 | if (method.HasCodeItem()) { |
| 1990 | const dex::CodeItem* code_item = method.GetDexFile()->GetCodeItem( |
| 1991 | reinterpret_cast32<uint32_t>(method.GetDataPtrSize(image_pointer_size_))); |
zhaoxuyang | 7156ea2 | 2022-01-10 13:58:11 +0800 | [diff] [blame] | 1992 | method.SetCodeItem(code_item, method.GetDexFile()->IsCompactDexFile()); |
Nicolas Geoffray | 61673dc | 2021-11-06 13:58:31 +0000 | [diff] [blame] | 1993 | // The hotness counter may have changed since we compiled the image, so |
| 1994 | // reset it with the runtime value. |
Nicolas Geoffray | 58f916c | 2021-11-15 14:02:07 +0000 | [diff] [blame] | 1995 | method.ResetCounter(hotness_threshold); |
Nicolas Geoffray | 4717175 | 2020-08-31 15:03:20 +0100 | [diff] [blame] | 1996 | } |
Nicolas Geoffray | c39af94 | 2021-01-25 08:43:57 +0000 | [diff] [blame] | 1997 | if (method.GetEntryPointFromQuickCompiledCode() == nterp_trampoline_) { |
| 1998 | if (can_use_nterp) { |
Nicolas Geoffray | c8a694d | 2022-01-17 17:12:38 +0000 | [diff] [blame] | 1999 | // Set image methods' entry point that point to the nterp trampoline to the |
| 2000 | // nterp entry point. This allows taking the fast path when doing a |
| 2001 | // nterp->nterp call. |
Nicolas Geoffray | b1cf837 | 2021-02-02 13:32:20 +0000 | [diff] [blame] | 2002 | DCHECK(!NeedsClinitCheckBeforeCall(&method) || |
| 2003 | method.GetDeclaringClass()->IsVisiblyInitialized()); |
Nicolas Geoffray | c39af94 | 2021-01-25 08:43:57 +0000 | [diff] [blame] | 2004 | method.SetEntryPointFromQuickCompiledCode(interpreter::GetNterpEntryPoint()); |
| 2005 | } else { |
| 2006 | method.SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge()); |
| 2007 | } |
Nicolas Geoffray | 4717175 | 2020-08-31 15:03:20 +0100 | [diff] [blame] | 2008 | } |
Nicolas Geoffray | 7e2c963 | 2020-01-09 13:41:10 +0000 | [diff] [blame] | 2009 | }, space->Begin(), image_pointer_size_); |
| 2010 | } |
| 2011 | |
Nicolas Geoffray | 8c41a0b | 2020-02-06 16:52:11 +0000 | [diff] [blame] | 2012 | if (runtime->IsVerificationSoftFail()) { |
| 2013 | header.VisitPackedArtMethods([&](ArtMethod& method) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 2014 | if (!method.IsNative() && method.IsInvokable()) { |
| 2015 | method.ClearSkipAccessChecks(); |
| 2016 | } |
| 2017 | }, space->Begin(), image_pointer_size_); |
| 2018 | } |
| 2019 | |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 2020 | ClassTable* class_table = nullptr; |
| 2021 | { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2022 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 2023 | class_table = InsertClassTableForClassLoader(class_loader.Get()); |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 2024 | } |
| 2025 | // If we have a class table section, read it and use it for verification in |
| 2026 | // UpdateAppImageClassLoadersAndDexCaches. |
| 2027 | ClassTable::ClassSet temp_set; |
Vladimir Marko | 0f3c700 | 2017-09-07 14:15:56 +0100 | [diff] [blame] | 2028 | const ImageSection& class_table_section = header.GetClassTableSection(); |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 2029 | const bool added_class_table = class_table_section.Size() > 0u; |
| 2030 | if (added_class_table) { |
| 2031 | const uint64_t start_time2 = NanoTime(); |
| 2032 | size_t read_count = 0; |
| 2033 | temp_set = ClassTable::ClassSet(space->Begin() + class_table_section.Offset(), |
| 2034 | /*make copy*/false, |
| 2035 | &read_count); |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 2036 | VLOG(image) << "Adding class table classes took " << PrettyDuration(NanoTime() - start_time2); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2037 | } |
| 2038 | if (app_image) { |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 2039 | AppImageLoadingHelper::Update(this, space, class_loader, dex_caches); |
Mathieu Chartier | 456b492 | 2018-11-06 10:35:48 -0800 | [diff] [blame] | 2040 | |
| 2041 | { |
| 2042 | ScopedTrace trace("AppImage:UpdateClassLoaders"); |
| 2043 | // Update class loader and resolved strings. If added_class_table is false, the resolved |
| 2044 | // strings were forwarded UpdateAppImageClassLoadersAndDexCaches. |
Mathieu Chartier | bc1e0fa | 2018-11-14 16:18:18 -0800 | [diff] [blame] | 2045 | ObjPtr<mirror::ClassLoader> loader(class_loader.Get()); |
Mathieu Chartier | 456b492 | 2018-11-06 10:35:48 -0800 | [diff] [blame] | 2046 | for (const ClassTable::TableSlot& root : temp_set) { |
Mathieu Chartier | bc1e0fa | 2018-11-14 16:18:18 -0800 | [diff] [blame] | 2047 | // Note: We probably don't need the read barrier unless we copy the app image objects into |
| 2048 | // the region space. |
| 2049 | ObjPtr<mirror::Class> klass(root.Read()); |
| 2050 | // Do not update class loader for boot image classes where the app image |
| 2051 | // class loader is only the initiating loader but not the defining loader. |
| 2052 | // Avoid read barrier since we are comparing against null. |
| 2053 | if (klass->GetClassLoader<kDefaultVerifyFlags, kWithoutReadBarrier>() != nullptr) { |
Vladimir Marko | b68bb7a | 2020-03-17 10:55:25 +0000 | [diff] [blame] | 2054 | klass->SetClassLoader(loader); |
Mathieu Chartier | bc1e0fa | 2018-11-14 16:18:18 -0800 | [diff] [blame] | 2055 | } |
Mathieu Chartier | 456b492 | 2018-11-06 10:35:48 -0800 | [diff] [blame] | 2056 | } |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 2057 | } |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 2058 | |
Vladimir Marko | 305c38b | 2018-02-14 11:50:07 +0000 | [diff] [blame] | 2059 | if (kBitstringSubtypeCheckEnabled) { |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 2060 | // Every class in the app image has initially SubtypeCheckInfo in the |
| 2061 | // Uninitialized state. |
| 2062 | // |
| 2063 | // The SubtypeCheck invariants imply that a SubtypeCheckInfo is at least Initialized |
| 2064 | // after class initialization is complete. The app image ClassStatus as-is |
| 2065 | // are almost all ClassStatus::Initialized, and being in the |
| 2066 | // SubtypeCheckInfo::kUninitialized state is violating that invariant. |
| 2067 | // |
| 2068 | // Force every app image class's SubtypeCheck to be at least kIninitialized. |
| 2069 | // |
| 2070 | // See also ImageWriter::FixupClass. |
Chris Wailes | 2386636 | 2018-08-22 16:16:58 -0700 | [diff] [blame] | 2071 | ScopedTrace trace("AppImage:RecacluateSubtypeCheckBitstrings"); |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 2072 | MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_); |
| 2073 | for (const ClassTable::TableSlot& root : temp_set) { |
Vladimir Marko | 38b8b25 | 2018-01-02 19:07:06 +0000 | [diff] [blame] | 2074 | SubtypeCheck<ObjPtr<mirror::Class>>::EnsureInitialized(root.Read()); |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 2075 | } |
| 2076 | } |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 2077 | } |
| 2078 | if (!oat_file->GetBssGcRoots().empty()) { |
| 2079 | // Insert oat file to class table for visiting .bss GC roots. |
| 2080 | class_table->InsertOatFile(oat_file); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2081 | } |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 2082 | |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 2083 | if (added_class_table) { |
| 2084 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 2085 | class_table->AddClassSet(std::move(temp_set)); |
| 2086 | } |
Andreas Gampe | be7af22 | 2017-07-25 09:57:28 -0700 | [diff] [blame] | 2087 | |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 2088 | if (kIsDebugBuild && app_image) { |
| 2089 | // This verification needs to happen after the classes have been added to the class loader. |
| 2090 | // Since it ensures classes are in the class table. |
Chris Wailes | 2386636 | 2018-08-22 16:16:58 -0700 | [diff] [blame] | 2091 | ScopedTrace trace("AppImage:Verify"); |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 2092 | VerifyAppImage(header, class_loader, class_table, space); |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 2093 | } |
Andreas Gampe | be7af22 | 2017-07-25 09:57:28 -0700 | [diff] [blame] | 2094 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2095 | VLOG(class_linker) << "Adding image space took " << PrettyDuration(NanoTime() - start_time); |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 2096 | return true; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 2097 | } |
| 2098 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 2099 | void ClassLinker::VisitClassRoots(RootVisitor* visitor, VisitRootFlags flags) { |
Mathieu Chartier | 7778b88 | 2015-10-05 16:41:10 -0700 | [diff] [blame] | 2100 | // Acquire tracing_enabled before locking class linker lock to prevent lock order violation. Since |
| 2101 | // enabling tracing requires the mutator lock, there are no race conditions here. |
| 2102 | const bool tracing_enabled = Trace::IsTracingEnabled(); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2103 | Thread* const self = Thread::Current(); |
| 2104 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Vladimir Marko | 9b03cb4 | 2017-02-16 16:37:03 +0000 | [diff] [blame] | 2105 | if (kUseReadBarrier) { |
| 2106 | // We do not track new roots for CC. |
| 2107 | DCHECK_EQ(0, flags & (kVisitRootFlagNewRoots | |
| 2108 | kVisitRootFlagClearRootLog | |
| 2109 | kVisitRootFlagStartLoggingNewRoots | |
| 2110 | kVisitRootFlagStopLoggingNewRoots)); |
| 2111 | } |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2112 | if ((flags & kVisitRootFlagAllRoots) != 0) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2113 | // Argument for how root visiting deals with ArtField and ArtMethod roots. |
| 2114 | // There is 3 GC cases to handle: |
| 2115 | // Non moving concurrent: |
| 2116 | // 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] | 2117 | // live by the class and class roots. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2118 | // |
| 2119 | // Moving non-concurrent: |
| 2120 | // This case needs to call visit VisitNativeRoots in case the classes or dex cache arrays move. |
| 2121 | // To prevent missing roots, this case needs to ensure that there is no |
| 2122 | // suspend points between the point which we allocate ArtMethod arrays and place them in a |
| 2123 | // class which is in the class table. |
| 2124 | // |
| 2125 | // Moving concurrent: |
| 2126 | // Need to make sure to not copy ArtMethods without doing read barriers since the roots are |
| 2127 | // 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] | 2128 | // |
| 2129 | // Use an unbuffered visitor since the class table uses a temporary GcRoot for holding decoded |
| 2130 | // ClassTable::TableSlot. The buffered root visiting would access a stale stack location for |
| 2131 | // these objects. |
| 2132 | UnbufferedRootVisitor root_visitor(visitor, RootInfo(kRootStickyClass)); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 2133 | boot_class_table_->VisitRoots(root_visitor); |
Mathieu Chartier | 7778b88 | 2015-10-05 16:41:10 -0700 | [diff] [blame] | 2134 | // 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] | 2135 | if ((flags & kVisitRootFlagClassLoader) != 0 || tracing_enabled) { |
Mathieu Chartier | 7778b88 | 2015-10-05 16:41:10 -0700 | [diff] [blame] | 2136 | for (const ClassLoaderData& data : class_loaders_) { |
| 2137 | GcRoot<mirror::Object> root(GcRoot<mirror::Object>(self->DecodeJObject(data.weak_root))); |
| 2138 | root.VisitRoot(visitor, RootInfo(kRootVMInternal)); |
| 2139 | } |
| 2140 | } |
Vladimir Marko | 9b03cb4 | 2017-02-16 16:37:03 +0000 | [diff] [blame] | 2141 | } else if (!kUseReadBarrier && (flags & kVisitRootFlagNewRoots) != 0) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 2142 | for (auto& root : new_class_roots_) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2143 | ObjPtr<mirror::Class> old_ref = root.Read<kWithoutReadBarrier>(); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 2144 | root.VisitRoot(visitor, RootInfo(kRootStickyClass)); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2145 | ObjPtr<mirror::Class> new_ref = root.Read<kWithoutReadBarrier>(); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 2146 | // Concurrent moving GC marked new roots through the to-space invariant. |
| 2147 | CHECK_EQ(new_ref, old_ref); |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2148 | } |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 2149 | for (const OatFile* oat_file : new_bss_roots_boot_oat_files_) { |
| 2150 | for (GcRoot<mirror::Object>& root : oat_file->GetBssGcRoots()) { |
| 2151 | ObjPtr<mirror::Object> old_ref = root.Read<kWithoutReadBarrier>(); |
| 2152 | if (old_ref != nullptr) { |
| 2153 | DCHECK(old_ref->IsClass()); |
| 2154 | root.VisitRoot(visitor, RootInfo(kRootStickyClass)); |
| 2155 | ObjPtr<mirror::Object> new_ref = root.Read<kWithoutReadBarrier>(); |
| 2156 | // Concurrent moving GC marked new roots through the to-space invariant. |
| 2157 | CHECK_EQ(new_ref, old_ref); |
| 2158 | } |
| 2159 | } |
| 2160 | } |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2161 | } |
Vladimir Marko | 9b03cb4 | 2017-02-16 16:37:03 +0000 | [diff] [blame] | 2162 | if (!kUseReadBarrier && (flags & kVisitRootFlagClearRootLog) != 0) { |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2163 | new_class_roots_.clear(); |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 2164 | new_bss_roots_boot_oat_files_.clear(); |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2165 | } |
Vladimir Marko | 9b03cb4 | 2017-02-16 16:37:03 +0000 | [diff] [blame] | 2166 | if (!kUseReadBarrier && (flags & kVisitRootFlagStartLoggingNewRoots) != 0) { |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 2167 | log_new_roots_ = true; |
Vladimir Marko | 9b03cb4 | 2017-02-16 16:37:03 +0000 | [diff] [blame] | 2168 | } else if (!kUseReadBarrier && (flags & kVisitRootFlagStopLoggingNewRoots) != 0) { |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 2169 | log_new_roots_ = false; |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2170 | } |
| 2171 | // We deliberately ignore the class roots in the image since we |
| 2172 | // handle image roots by using the MS/CMS rescanning of dirty cards. |
| 2173 | } |
| 2174 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 2175 | // Keep in sync with InitCallback. Anything we visit, we need to |
| 2176 | // reinit references to when reinitializing a ClassLinker from a |
| 2177 | // mapped image. |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 2178 | void ClassLinker::VisitRoots(RootVisitor* visitor, VisitRootFlags flags) { |
Mathieu Chartier | 3100080 | 2015-06-14 14:14:37 -0700 | [diff] [blame] | 2179 | class_roots_.VisitRootIfNonNull(visitor, RootInfo(kRootVMInternal)); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 2180 | VisitClassRoots(visitor, flags); |
Mathieu Chartier | 6cfc2c0 | 2015-10-12 15:06:16 -0700 | [diff] [blame] | 2181 | // Instead of visiting the find_array_class_cache_ drop it so that it doesn't prevent class |
| 2182 | // unloading if we are marking roots. |
| 2183 | DropFindArrayClassCache(); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 2184 | } |
| 2185 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2186 | class VisitClassLoaderClassesVisitor : public ClassLoaderVisitor { |
| 2187 | public: |
| 2188 | explicit VisitClassLoaderClassesVisitor(ClassVisitor* visitor) |
| 2189 | : visitor_(visitor), |
| 2190 | done_(false) {} |
| 2191 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2192 | void Visit(ObjPtr<mirror::ClassLoader> class_loader) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 2193 | REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_) override { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2194 | ClassTable* const class_table = class_loader->GetClassTable(); |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2195 | if (!done_ && class_table != nullptr) { |
| 2196 | DefiningClassLoaderFilterVisitor visitor(class_loader, visitor_); |
| 2197 | if (!class_table->Visit(visitor)) { |
| 2198 | // If the visitor ClassTable returns false it means that we don't need to continue. |
| 2199 | done_ = true; |
| 2200 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2201 | } |
| 2202 | } |
| 2203 | |
| 2204 | private: |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2205 | // Class visitor that limits the class visits from a ClassTable to the classes with |
| 2206 | // the provided defining class loader. This filter is used to avoid multiple visits |
| 2207 | // of the same class which can be recorded for multiple initiating class loaders. |
| 2208 | class DefiningClassLoaderFilterVisitor : public ClassVisitor { |
| 2209 | public: |
| 2210 | DefiningClassLoaderFilterVisitor(ObjPtr<mirror::ClassLoader> defining_class_loader, |
| 2211 | ClassVisitor* visitor) |
| 2212 | : defining_class_loader_(defining_class_loader), visitor_(visitor) { } |
| 2213 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 2214 | bool operator()(ObjPtr<mirror::Class> klass) override REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2215 | if (klass->GetClassLoader() != defining_class_loader_) { |
| 2216 | return true; |
| 2217 | } |
| 2218 | return (*visitor_)(klass); |
| 2219 | } |
| 2220 | |
Vladimir Marko | 0984e48 | 2019-03-27 16:41:41 +0000 | [diff] [blame] | 2221 | const ObjPtr<mirror::ClassLoader> defining_class_loader_; |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2222 | ClassVisitor* const visitor_; |
| 2223 | }; |
| 2224 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2225 | ClassVisitor* const visitor_; |
| 2226 | // If done is true then we don't need to do any more visiting. |
| 2227 | bool done_; |
| 2228 | }; |
| 2229 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2230 | void ClassLinker::VisitClassesInternal(ClassVisitor* visitor) { |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 2231 | if (boot_class_table_->Visit(*visitor)) { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2232 | VisitClassLoaderClassesVisitor loader_visitor(visitor); |
| 2233 | VisitClassLoaders(&loader_visitor); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 2234 | } |
| 2235 | } |
| 2236 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2237 | void ClassLinker::VisitClasses(ClassVisitor* visitor) { |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 2238 | Thread* const self = Thread::Current(); |
| 2239 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 2240 | // Not safe to have thread suspension when we are holding a lock. |
| 2241 | if (self != nullptr) { |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 2242 | ScopedAssertNoThreadSuspension nts(__FUNCTION__); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2243 | VisitClassesInternal(visitor); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 2244 | } else { |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2245 | VisitClassesInternal(visitor); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2246 | } |
| 2247 | } |
| 2248 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2249 | class GetClassesInToVector : public ClassVisitor { |
| 2250 | public: |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 2251 | bool operator()(ObjPtr<mirror::Class> klass) override { |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2252 | classes_.push_back(klass); |
| 2253 | return true; |
| 2254 | } |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2255 | std::vector<ObjPtr<mirror::Class>> classes_; |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2256 | }; |
| 2257 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2258 | class GetClassInToObjectArray : public ClassVisitor { |
| 2259 | public: |
| 2260 | explicit GetClassInToObjectArray(mirror::ObjectArray<mirror::Class>* arr) |
| 2261 | : arr_(arr), index_(0) {} |
| 2262 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 2263 | bool operator()(ObjPtr<mirror::Class> klass) override REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2264 | ++index_; |
| 2265 | if (index_ <= arr_->GetLength()) { |
| 2266 | arr_->Set(index_ - 1, klass); |
| 2267 | return true; |
| 2268 | } |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2269 | return false; |
| 2270 | } |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2271 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2272 | bool Succeeded() const REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2273 | return index_ <= arr_->GetLength(); |
| 2274 | } |
| 2275 | |
| 2276 | private: |
| 2277 | mirror::ObjectArray<mirror::Class>* const arr_; |
| 2278 | int32_t index_; |
| 2279 | }; |
| 2280 | |
| 2281 | void ClassLinker::VisitClassesWithoutClassesLock(ClassVisitor* visitor) { |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2282 | // TODO: it may be possible to avoid secondary storage if we iterate over dex caches. The problem |
| 2283 | // is avoiding duplicates. |
| 2284 | if (!kMovingClasses) { |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 2285 | ScopedAssertNoThreadSuspension nts(__FUNCTION__); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2286 | GetClassesInToVector accumulator; |
| 2287 | VisitClasses(&accumulator); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2288 | for (ObjPtr<mirror::Class> klass : accumulator.classes_) { |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 2289 | if (!visitor->operator()(klass)) { |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2290 | return; |
| 2291 | } |
| 2292 | } |
| 2293 | } else { |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 2294 | Thread* const self = Thread::Current(); |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2295 | StackHandleScope<1> hs(self); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2296 | auto classes = hs.NewHandle<mirror::ObjectArray<mirror::Class>>(nullptr); |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2297 | // We size the array assuming classes won't be added to the class table during the visit. |
| 2298 | // If this assumption fails we iterate again. |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2299 | while (true) { |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2300 | size_t class_table_size; |
| 2301 | { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2302 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2303 | // Add 100 in case new classes get loaded when we are filling in the object array. |
| 2304 | class_table_size = NumZygoteClasses() + NumNonZygoteClasses() + 100; |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2305 | } |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 2306 | ObjPtr<mirror::Class> array_of_class = GetClassRoot<mirror::ObjectArray<mirror::Class>>(this); |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2307 | classes.Assign( |
| 2308 | mirror::ObjectArray<mirror::Class>::Alloc(self, array_of_class, class_table_size)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 2309 | CHECK(classes != nullptr); // OOME. |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2310 | GetClassInToObjectArray accumulator(classes.Get()); |
| 2311 | VisitClasses(&accumulator); |
| 2312 | if (accumulator.Succeeded()) { |
| 2313 | break; |
| 2314 | } |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2315 | } |
| 2316 | for (int32_t i = 0; i < classes->GetLength(); ++i) { |
| 2317 | // If the class table shrank during creation of the clases array we expect null elements. If |
| 2318 | // the class table grew then the loop repeats. If classes are created after the loop has |
| 2319 | // finished then we don't visit. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2320 | ObjPtr<mirror::Class> klass = classes->Get(i); |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 2321 | if (klass != nullptr && !visitor->operator()(klass)) { |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2322 | return; |
| 2323 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2324 | } |
| 2325 | } |
| 2326 | } |
| 2327 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 2328 | ClassLinker::~ClassLinker() { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2329 | Thread* const self = Thread::Current(); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 2330 | for (const ClassLoaderData& data : class_loaders_) { |
Alexey Grebenkin | be4c2bd | 2018-02-01 19:09:59 +0300 | [diff] [blame] | 2331 | // CHA unloading analysis is not needed. No negative consequences are expected because |
| 2332 | // all the classloaders are deleted at the same time. |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 2333 | DeleteClassLoader(self, data, /*cleanup_cha=*/ false); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 2334 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2335 | class_loaders_.clear(); |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 2336 | while (!running_visibly_initialized_callbacks_.empty()) { |
| 2337 | std::unique_ptr<VisiblyInitializedCallback> callback( |
| 2338 | std::addressof(running_visibly_initialized_callbacks_.front())); |
| 2339 | running_visibly_initialized_callbacks_.pop_front(); |
| 2340 | } |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 2341 | } |
| 2342 | |
Alexey Grebenkin | be4c2bd | 2018-02-01 19:09:59 +0300 | [diff] [blame] | 2343 | void ClassLinker::DeleteClassLoader(Thread* self, const ClassLoaderData& data, bool cleanup_cha) { |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 2344 | Runtime* const runtime = Runtime::Current(); |
| 2345 | JavaVMExt* const vm = runtime->GetJavaVM(); |
| 2346 | vm->DeleteWeakGlobalRef(self, data.weak_root); |
Calin Juravle | e5de54c | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 2347 | // 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] | 2348 | if (runtime->GetJit() != nullptr) { |
| 2349 | jit::JitCodeCache* code_cache = runtime->GetJit()->GetCodeCache(); |
| 2350 | if (code_cache != nullptr) { |
Mathieu Chartier | cf79cf5 | 2017-07-21 11:17:57 -0700 | [diff] [blame] | 2351 | // For the JIT case, RemoveMethodsIn removes the CHA dependencies. |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 2352 | code_cache->RemoveMethodsIn(self, *data.allocator); |
| 2353 | } |
Andreas Gampe | c1ac9ee | 2017-07-24 22:35:49 -0700 | [diff] [blame] | 2354 | } else if (cha_ != nullptr) { |
Mathieu Chartier | cf79cf5 | 2017-07-21 11:17:57 -0700 | [diff] [blame] | 2355 | // 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] | 2356 | cha_->RemoveDependenciesForLinearAlloc(data.allocator); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 2357 | } |
Alexey Grebenkin | be4c2bd | 2018-02-01 19:09:59 +0300 | [diff] [blame] | 2358 | // Cleanup references to single implementation ArtMethods that will be deleted. |
| 2359 | if (cleanup_cha) { |
| 2360 | CHAOnDeleteUpdateClassVisitor visitor(data.allocator); |
| 2361 | data.class_table->Visit<CHAOnDeleteUpdateClassVisitor, kWithoutReadBarrier>(visitor); |
| 2362 | } |
Vladimir Marko | 86c8752 | 2020-05-11 16:55:55 +0100 | [diff] [blame] | 2363 | { |
| 2364 | MutexLock lock(self, critical_native_code_with_clinit_check_lock_); |
| 2365 | auto end = critical_native_code_with_clinit_check_.end(); |
| 2366 | for (auto it = critical_native_code_with_clinit_check_.begin(); it != end; ) { |
| 2367 | if (data.allocator->ContainsUnsafe(it->first)) { |
| 2368 | it = critical_native_code_with_clinit_check_.erase(it); |
| 2369 | } else { |
| 2370 | ++it; |
| 2371 | } |
| 2372 | } |
| 2373 | } |
Alexey Grebenkin | be4c2bd | 2018-02-01 19:09:59 +0300 | [diff] [blame] | 2374 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 2375 | delete data.allocator; |
| 2376 | delete data.class_table; |
| 2377 | } |
| 2378 | |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 2379 | ObjPtr<mirror::PointerArray> ClassLinker::AllocPointerArray(Thread* self, size_t length) { |
| 2380 | return ObjPtr<mirror::PointerArray>::DownCast( |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 2381 | image_pointer_size_ == PointerSize::k64 |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 2382 | ? ObjPtr<mirror::Array>(mirror::LongArray::Alloc(self, length)) |
| 2383 | : ObjPtr<mirror::Array>(mirror::IntArray::Alloc(self, length))); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2384 | } |
| 2385 | |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 2386 | ObjPtr<mirror::DexCache> ClassLinker::AllocDexCache(Thread* self, const DexFile& dex_file) { |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2387 | StackHandleScope<1> hs(self); |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 2388 | auto dex_cache(hs.NewHandle(ObjPtr<mirror::DexCache>::DownCast( |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 2389 | GetClassRoot<mirror::DexCache>(this)->AllocObject(self)))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 2390 | if (dex_cache == nullptr) { |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2391 | self->AssertPendingOOMException(); |
| 2392 | return nullptr; |
| 2393 | } |
Vladimir Marko | 31c3daa | 2019-06-13 12:18:37 +0100 | [diff] [blame] | 2394 | // Use InternWeak() so that the location String can be collected when the ClassLoader |
| 2395 | // with this DexCache is collected. |
| 2396 | ObjPtr<mirror::String> location = intern_table_->InternWeak(dex_file.GetLocation().c_str()); |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2397 | if (location == nullptr) { |
| 2398 | self->AssertPendingOOMException(); |
| 2399 | return nullptr; |
| 2400 | } |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 2401 | dex_cache->SetLocation(location); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2402 | return dex_cache.Get(); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 2403 | } |
| 2404 | |
David Srbecky | 33df0e3 | 2021-09-30 14:36:32 +0000 | [diff] [blame] | 2405 | ObjPtr<mirror::DexCache> ClassLinker::AllocAndInitializeDexCache( |
| 2406 | Thread* self, const DexFile& dex_file, ObjPtr<mirror::ClassLoader> class_loader) { |
| 2407 | StackHandleScope<1> hs(self); |
| 2408 | Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader)); |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 2409 | ObjPtr<mirror::DexCache> dex_cache = AllocDexCache(self, dex_file); |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2410 | if (dex_cache != nullptr) { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 2411 | WriterMutexLock mu(self, *Locks::dex_lock_); |
David Srbecky | 33df0e3 | 2021-09-30 14:36:32 +0000 | [diff] [blame] | 2412 | dex_cache->Initialize(&dex_file, h_class_loader.Get()); |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2413 | } |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 2414 | return dex_cache; |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2415 | } |
| 2416 | |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 2417 | template <bool kMovable, typename PreFenceVisitor> |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 2418 | ObjPtr<mirror::Class> ClassLinker::AllocClass(Thread* self, |
| 2419 | ObjPtr<mirror::Class> java_lang_Class, |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 2420 | uint32_t class_size, |
| 2421 | const PreFenceVisitor& pre_fence_visitor) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 2422 | DCHECK_GE(class_size, sizeof(mirror::Class)); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 2423 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 2424 | ObjPtr<mirror::Object> k = (kMovingClasses && kMovable) ? |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 2425 | heap->AllocObject(self, java_lang_Class, class_size, pre_fence_visitor) : |
| 2426 | heap->AllocNonMovableObject(self, java_lang_Class, class_size, pre_fence_visitor); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 2427 | if (UNLIKELY(k == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2428 | self->AssertPendingOOMException(); |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 2429 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 2430 | } |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 2431 | return k->AsClass(); |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 2432 | } |
| 2433 | |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 2434 | template <bool kMovable> |
| 2435 | ObjPtr<mirror::Class> ClassLinker::AllocClass(Thread* self, |
| 2436 | ObjPtr<mirror::Class> java_lang_Class, |
| 2437 | uint32_t class_size) { |
| 2438 | mirror::Class::InitializeClassVisitor visitor(class_size); |
| 2439 | return AllocClass<kMovable>(self, java_lang_Class, class_size, visitor); |
| 2440 | } |
| 2441 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 2442 | ObjPtr<mirror::Class> ClassLinker::AllocClass(Thread* self, uint32_t class_size) { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 2443 | return AllocClass(self, GetClassRoot<mirror::Class>(this), class_size); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 2444 | } |
| 2445 | |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 2446 | void ClassLinker::AllocPrimitiveArrayClass(Thread* self, |
| 2447 | ClassRoot primitive_root, |
| 2448 | ClassRoot array_root) { |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 2449 | // We make this class non-movable for the unlikely case where it were to be |
| 2450 | // moved by a sticky-bit (minor) collection when using the Generational |
| 2451 | // Concurrent Copying (CC) collector, potentially creating a stale reference |
| 2452 | // in the `klass_` field of one of its instances allocated in the Large-Object |
| 2453 | // Space (LOS) -- see the comment about the dirty card scanning logic in |
| 2454 | // art::gc::collector::ConcurrentCopying::MarkingPhase. |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 2455 | ObjPtr<mirror::Class> array_class = AllocClass</* kMovable= */ false>( |
| 2456 | self, GetClassRoot<mirror::Class>(this), mirror::Array::ClassSize(image_pointer_size_)); |
| 2457 | ObjPtr<mirror::Class> component_type = GetClassRoot(primitive_root, this); |
| 2458 | DCHECK(component_type->IsPrimitive()); |
| 2459 | array_class->SetComponentType(component_type); |
| 2460 | SetClassRoot(array_root, array_class); |
| 2461 | } |
| 2462 | |
| 2463 | void ClassLinker::FinishArrayClassSetup(ObjPtr<mirror::Class> array_class) { |
| 2464 | ObjPtr<mirror::Class> java_lang_Object = GetClassRoot<mirror::Object>(this); |
| 2465 | array_class->SetSuperClass(java_lang_Object); |
| 2466 | array_class->SetVTable(java_lang_Object->GetVTable()); |
| 2467 | array_class->SetPrimitiveType(Primitive::kPrimNot); |
| 2468 | ObjPtr<mirror::Class> component_type = array_class->GetComponentType(); |
| 2469 | array_class->SetClassFlags(component_type->IsPrimitive() |
| 2470 | ? mirror::kClassFlagNoReferenceFields |
| 2471 | : mirror::kClassFlagObjectArray); |
| 2472 | array_class->SetClassLoader(component_type->GetClassLoader()); |
| 2473 | array_class->SetStatusForPrimitiveOrArray(ClassStatus::kLoaded); |
| 2474 | array_class->PopulateEmbeddedVTable(image_pointer_size_); |
| 2475 | ImTable* object_imt = java_lang_Object->GetImt(image_pointer_size_); |
| 2476 | array_class->SetImt(object_imt, image_pointer_size_); |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 2477 | DCHECK_EQ(array_class->NumMethods(), 0u); |
| 2478 | |
| 2479 | // don't need to set new_class->SetObjectSize(..) |
| 2480 | // because Object::SizeOf delegates to Array::SizeOf |
| 2481 | |
| 2482 | // All arrays have java/lang/Cloneable and java/io/Serializable as |
| 2483 | // interfaces. We need to set that up here, so that stuff like |
| 2484 | // "instanceof" works right. |
| 2485 | |
| 2486 | // Use the single, global copies of "interfaces" and "iftable" |
| 2487 | // (remember not to free them for arrays). |
| 2488 | { |
| 2489 | ObjPtr<mirror::IfTable> array_iftable = GetArrayIfTable(); |
| 2490 | CHECK(array_iftable != nullptr); |
| 2491 | array_class->SetIfTable(array_iftable); |
| 2492 | } |
| 2493 | |
| 2494 | // Inherit access flags from the component type. |
| 2495 | int access_flags = component_type->GetAccessFlags(); |
| 2496 | // Lose any implementation detail flags; in particular, arrays aren't finalizable. |
| 2497 | access_flags &= kAccJavaFlagsMask; |
| 2498 | // Arrays can't be used as a superclass or interface, so we want to add "abstract final" |
| 2499 | // and remove "interface". |
| 2500 | access_flags |= kAccAbstract | kAccFinal; |
| 2501 | access_flags &= ~kAccInterface; |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 2502 | |
Vladimir Marko | b68bb7a | 2020-03-17 10:55:25 +0000 | [diff] [blame] | 2503 | array_class->SetAccessFlagsDuringLinking(access_flags); |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 2504 | |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 2505 | // Array classes are fully initialized either during single threaded startup, |
| 2506 | // or from a pre-fence visitor, so visibly initialized. |
| 2507 | array_class->SetStatusForPrimitiveOrArray(ClassStatus::kVisiblyInitialized); |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 2508 | } |
| 2509 | |
| 2510 | void ClassLinker::FinishCoreArrayClassSetup(ClassRoot array_root) { |
| 2511 | // Do not hold lock on the array class object, the initialization of |
| 2512 | // core array classes is done while the process is still single threaded. |
| 2513 | ObjPtr<mirror::Class> array_class = GetClassRoot(array_root, this); |
| 2514 | FinishArrayClassSetup(array_class); |
| 2515 | |
| 2516 | std::string temp; |
| 2517 | const char* descriptor = array_class->GetDescriptor(&temp); |
| 2518 | size_t hash = ComputeModifiedUtf8Hash(descriptor); |
| 2519 | ObjPtr<mirror::Class> existing = InsertClass(descriptor, array_class, hash); |
| 2520 | CHECK(existing == nullptr); |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 2521 | } |
| 2522 | |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 2523 | ObjPtr<mirror::ObjectArray<mirror::StackTraceElement>> ClassLinker::AllocStackTraceElementArray( |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 2524 | Thread* self, |
| 2525 | size_t length) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2526 | return mirror::ObjectArray<mirror::StackTraceElement>::Alloc( |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 2527 | self, GetClassRoot<mirror::ObjectArray<mirror::StackTraceElement>>(this), length); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 2528 | } |
| 2529 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 2530 | ObjPtr<mirror::Class> ClassLinker::EnsureResolved(Thread* self, |
| 2531 | const char* descriptor, |
| 2532 | ObjPtr<mirror::Class> klass) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2533 | DCHECK(klass != nullptr); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2534 | if (kIsDebugBuild) { |
| 2535 | StackHandleScope<1> hs(self); |
| 2536 | HandleWrapperObjPtr<mirror::Class> h = hs.NewHandleWrapper(&klass); |
| 2537 | Thread::PoisonObjectPointersIfDebug(); |
| 2538 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2539 | |
| 2540 | // For temporary classes we must wait for them to be retired. |
| 2541 | if (init_done_ && klass->IsTemp()) { |
| 2542 | CHECK(!klass->IsResolved()); |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2543 | if (klass->IsErroneousUnresolved()) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2544 | ThrowEarlierClassFailure(klass); |
| 2545 | return nullptr; |
| 2546 | } |
| 2547 | StackHandleScope<1> hs(self); |
| 2548 | Handle<mirror::Class> h_class(hs.NewHandle(klass)); |
| 2549 | ObjectLock<mirror::Class> lock(self, h_class); |
| 2550 | // Loop and wait for the resolving thread to retire this class. |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2551 | while (!h_class->IsRetired() && !h_class->IsErroneousUnresolved()) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2552 | lock.WaitIgnoringInterrupts(); |
| 2553 | } |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2554 | if (h_class->IsErroneousUnresolved()) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2555 | ThrowEarlierClassFailure(h_class.Get()); |
| 2556 | return nullptr; |
| 2557 | } |
| 2558 | CHECK(h_class->IsRetired()); |
| 2559 | // Get the updated class from class table. |
Andreas Gampe | 34ee684 | 2014-12-02 15:43:52 -0800 | [diff] [blame] | 2560 | klass = LookupClass(self, descriptor, h_class.Get()->GetClassLoader()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2561 | } |
| 2562 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2563 | // Wait for the class if it has not already been linked. |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 2564 | size_t index = 0; |
| 2565 | // Maximum number of yield iterations until we start sleeping. |
| 2566 | static const size_t kNumYieldIterations = 1000; |
| 2567 | // How long each sleep is in us. |
| 2568 | static const size_t kSleepDurationUS = 1000; // 1 ms. |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2569 | while (!klass->IsResolved() && !klass->IsErroneousUnresolved()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2570 | StackHandleScope<1> hs(self); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2571 | HandleWrapperObjPtr<mirror::Class> h_class(hs.NewHandleWrapper(&klass)); |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 2572 | { |
| 2573 | ObjectTryLock<mirror::Class> lock(self, h_class); |
| 2574 | // Can not use a monitor wait here since it may block when returning and deadlock if another |
| 2575 | // thread has locked klass. |
| 2576 | if (lock.Acquired()) { |
| 2577 | // Check for circular dependencies between classes, the lock is required for SetStatus. |
| 2578 | if (!h_class->IsResolved() && h_class->GetClinitThreadId() == self->GetTid()) { |
| 2579 | ThrowClassCircularityError(h_class.Get()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 2580 | mirror::Class::SetStatus(h_class, ClassStatus::kErrorUnresolved, self); |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 2581 | return nullptr; |
| 2582 | } |
| 2583 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2584 | } |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 2585 | { |
| 2586 | // Handle wrapper deals with klass moving. |
Vladimir Marko | ddf4fd3 | 2021-11-22 16:31:57 +0000 | [diff] [blame] | 2587 | ScopedThreadSuspension sts(self, ThreadState::kSuspended); |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 2588 | if (index < kNumYieldIterations) { |
| 2589 | sched_yield(); |
| 2590 | } else { |
| 2591 | usleep(kSleepDurationUS); |
| 2592 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2593 | } |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 2594 | ++index; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2595 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2596 | |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2597 | if (klass->IsErroneousUnresolved()) { |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 2598 | ThrowEarlierClassFailure(klass); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 2599 | return nullptr; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2600 | } |
| 2601 | // Return the loaded class. No exceptions should be pending. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 2602 | CHECK(klass->IsResolved()) << klass->PrettyClass(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2603 | self->AssertNoPendingException(); |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 2604 | return klass; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2605 | } |
| 2606 | |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 2607 | using ClassPathEntry = std::pair<const DexFile*, const dex::ClassDef*>; |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2608 | |
| 2609 | // Search a collection of DexFiles for a descriptor |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2610 | ClassPathEntry FindInClassPath(const char* descriptor, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 2611 | size_t hash, const std::vector<const DexFile*>& class_path) { |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2612 | for (const DexFile* dex_file : class_path) { |
Mathieu Chartier | 0a19e21 | 2019-11-27 14:35:24 -0800 | [diff] [blame] | 2613 | DCHECK(dex_file != nullptr); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 2614 | const dex::ClassDef* dex_class_def = OatDexFile::FindClassDef(*dex_file, descriptor, hash); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2615 | if (dex_class_def != nullptr) { |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2616 | return ClassPathEntry(dex_file, dex_class_def); |
| 2617 | } |
| 2618 | } |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2619 | return ClassPathEntry(nullptr, nullptr); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2620 | } |
| 2621 | |
Nicolas Geoffray | e8445e5 | 2021-09-23 14:10:05 +0100 | [diff] [blame] | 2622 | // Helper macro to make sure each class loader lookup call handles the case the |
| 2623 | // class loader is not recognized, or the lookup threw an exception. |
| 2624 | #define RETURN_IF_UNRECOGNIZED_OR_FOUND_OR_EXCEPTION(call_, result_, thread_) \ |
| 2625 | do { \ |
| 2626 | auto local_call = call_; \ |
| 2627 | if (!local_call) { \ |
| 2628 | return false; \ |
| 2629 | } \ |
| 2630 | auto local_result = result_; \ |
| 2631 | if (local_result != nullptr) { \ |
| 2632 | return true; \ |
| 2633 | } \ |
| 2634 | auto local_thread = thread_; \ |
| 2635 | if (local_thread->IsExceptionPending()) { \ |
| 2636 | /* Pending exception means there was an error other than */ \ |
| 2637 | /* ClassNotFound that must be returned to the caller. */ \ |
| 2638 | return false; \ |
| 2639 | } \ |
| 2640 | } while (0) |
| 2641 | |
Nicolas Geoffray | 80a560c | 2018-10-26 13:48:51 +0100 | [diff] [blame] | 2642 | bool ClassLinker::FindClassInSharedLibraries(ScopedObjectAccessAlreadyRunnable& soa, |
| 2643 | Thread* self, |
| 2644 | const char* descriptor, |
| 2645 | size_t hash, |
| 2646 | Handle<mirror::ClassLoader> class_loader, |
| 2647 | /*out*/ ObjPtr<mirror::Class>* result) { |
| 2648 | ArtField* field = |
| 2649 | jni::DecodeArtField(WellKnownClasses::dalvik_system_BaseDexClassLoader_sharedLibraryLoaders); |
Brad Stenning | 9c924e8 | 2021-10-11 19:09:00 -0700 | [diff] [blame] | 2650 | return FindClassInSharedLibrariesHelper(soa, self, descriptor, hash, class_loader, field, result); |
| 2651 | } |
| 2652 | |
| 2653 | bool ClassLinker::FindClassInSharedLibrariesHelper(ScopedObjectAccessAlreadyRunnable& soa, |
| 2654 | Thread* self, |
| 2655 | const char* descriptor, |
| 2656 | size_t hash, |
| 2657 | Handle<mirror::ClassLoader> class_loader, |
| 2658 | ArtField* field, |
| 2659 | /*out*/ ObjPtr<mirror::Class>* result) { |
Nicolas Geoffray | 80a560c | 2018-10-26 13:48:51 +0100 | [diff] [blame] | 2660 | ObjPtr<mirror::Object> raw_shared_libraries = field->GetObject(class_loader.Get()); |
| 2661 | if (raw_shared_libraries == nullptr) { |
| 2662 | return true; |
| 2663 | } |
| 2664 | |
| 2665 | StackHandleScope<2> hs(self); |
| 2666 | Handle<mirror::ObjectArray<mirror::ClassLoader>> shared_libraries( |
| 2667 | hs.NewHandle(raw_shared_libraries->AsObjectArray<mirror::ClassLoader>())); |
| 2668 | MutableHandle<mirror::ClassLoader> temp_loader = hs.NewHandle<mirror::ClassLoader>(nullptr); |
Alex Light | a9bbc08 | 2019-11-14 14:51:41 -0800 | [diff] [blame] | 2669 | for (auto loader : shared_libraries.Iterate<mirror::ClassLoader>()) { |
| 2670 | temp_loader.Assign(loader); |
Nicolas Geoffray | e8445e5 | 2021-09-23 14:10:05 +0100 | [diff] [blame] | 2671 | RETURN_IF_UNRECOGNIZED_OR_FOUND_OR_EXCEPTION( |
| 2672 | FindClassInBaseDexClassLoader(soa, self, descriptor, hash, temp_loader, result), |
| 2673 | *result, |
| 2674 | self); |
Nicolas Geoffray | 80a560c | 2018-10-26 13:48:51 +0100 | [diff] [blame] | 2675 | } |
| 2676 | return true; |
| 2677 | } |
| 2678 | |
Brad Stenning | 9c924e8 | 2021-10-11 19:09:00 -0700 | [diff] [blame] | 2679 | bool ClassLinker::FindClassInSharedLibrariesAfter(ScopedObjectAccessAlreadyRunnable& soa, |
| 2680 | Thread* self, |
| 2681 | const char* descriptor, |
| 2682 | size_t hash, |
| 2683 | Handle<mirror::ClassLoader> class_loader, |
| 2684 | /*out*/ ObjPtr<mirror::Class>* result) { |
| 2685 | ArtField* field = jni::DecodeArtField( |
| 2686 | WellKnownClasses::dalvik_system_BaseDexClassLoader_sharedLibraryLoadersAfter); |
| 2687 | return FindClassInSharedLibrariesHelper(soa, self, descriptor, hash, class_loader, field, result); |
| 2688 | } |
| 2689 | |
Nicolas Geoffray | 7d8d8ff | 2016-11-02 12:38:05 +0000 | [diff] [blame] | 2690 | bool ClassLinker::FindClassInBaseDexClassLoader(ScopedObjectAccessAlreadyRunnable& soa, |
| 2691 | Thread* self, |
| 2692 | const char* descriptor, |
| 2693 | size_t hash, |
| 2694 | Handle<mirror::ClassLoader> class_loader, |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 2695 | /*out*/ ObjPtr<mirror::Class>* result) { |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 2696 | // Termination case: boot class loader. |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2697 | if (IsBootClassLoader(soa, class_loader.Get())) { |
Nicolas Geoffray | e8445e5 | 2021-09-23 14:10:05 +0100 | [diff] [blame] | 2698 | RETURN_IF_UNRECOGNIZED_OR_FOUND_OR_EXCEPTION( |
| 2699 | FindClassInBootClassLoaderClassPath(self, descriptor, hash, result), *result, self); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2700 | return true; |
| 2701 | } |
| 2702 | |
David Brazdil | 05909d8 | 2018-12-06 16:25:16 +0000 | [diff] [blame] | 2703 | if (IsPathOrDexClassLoader(soa, class_loader) || IsInMemoryDexClassLoader(soa, class_loader)) { |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 2704 | // For regular path or dex class loader the search order is: |
| 2705 | // - parent |
Nicolas Geoffray | 80a560c | 2018-10-26 13:48:51 +0100 | [diff] [blame] | 2706 | // - shared libraries |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 2707 | // - class loader dex files |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2708 | |
Nicolas Geoffray | e8445e5 | 2021-09-23 14:10:05 +0100 | [diff] [blame] | 2709 | // 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] | 2710 | StackHandleScope<1> hs(self); |
| 2711 | Handle<mirror::ClassLoader> h_parent(hs.NewHandle(class_loader->GetParent())); |
Nicolas Geoffray | e8445e5 | 2021-09-23 14:10:05 +0100 | [diff] [blame] | 2712 | RETURN_IF_UNRECOGNIZED_OR_FOUND_OR_EXCEPTION( |
| 2713 | FindClassInBaseDexClassLoader(soa, self, descriptor, hash, h_parent, result), |
| 2714 | *result, |
| 2715 | self); |
| 2716 | RETURN_IF_UNRECOGNIZED_OR_FOUND_OR_EXCEPTION( |
| 2717 | FindClassInSharedLibraries(soa, self, descriptor, hash, class_loader, result), |
| 2718 | *result, |
| 2719 | self); |
| 2720 | RETURN_IF_UNRECOGNIZED_OR_FOUND_OR_EXCEPTION( |
| 2721 | FindClassInBaseDexClassLoaderClassPath(soa, descriptor, hash, class_loader, result), |
| 2722 | *result, |
| 2723 | self); |
Brad Stenning | 9c924e8 | 2021-10-11 19:09:00 -0700 | [diff] [blame] | 2724 | RETURN_IF_UNRECOGNIZED_OR_FOUND_OR_EXCEPTION( |
| 2725 | FindClassInSharedLibrariesAfter(soa, self, descriptor, hash, class_loader, result), |
| 2726 | *result, |
| 2727 | self); |
Nicolas Geoffray | e8445e5 | 2021-09-23 14:10:05 +0100 | [diff] [blame] | 2728 | // We did not find a class, but the class loader chain was recognized, so we |
| 2729 | // return true. |
| 2730 | return true; |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2731 | } |
| 2732 | |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 2733 | if (IsDelegateLastClassLoader(soa, class_loader)) { |
| 2734 | // For delegate last, the search order is: |
| 2735 | // - boot class path |
Nicolas Geoffray | 80a560c | 2018-10-26 13:48:51 +0100 | [diff] [blame] | 2736 | // - shared libraries |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 2737 | // - class loader dex files |
| 2738 | // - parent |
Nicolas Geoffray | e8445e5 | 2021-09-23 14:10:05 +0100 | [diff] [blame] | 2739 | RETURN_IF_UNRECOGNIZED_OR_FOUND_OR_EXCEPTION( |
| 2740 | FindClassInBootClassLoaderClassPath(self, descriptor, hash, result), *result, self); |
| 2741 | RETURN_IF_UNRECOGNIZED_OR_FOUND_OR_EXCEPTION( |
| 2742 | FindClassInSharedLibraries(soa, self, descriptor, hash, class_loader, result), |
| 2743 | *result, |
| 2744 | self); |
| 2745 | RETURN_IF_UNRECOGNIZED_OR_FOUND_OR_EXCEPTION( |
| 2746 | FindClassInBaseDexClassLoaderClassPath(soa, descriptor, hash, class_loader, result), |
| 2747 | *result, |
| 2748 | self); |
Brad Stenning | 9c924e8 | 2021-10-11 19:09:00 -0700 | [diff] [blame] | 2749 | RETURN_IF_UNRECOGNIZED_OR_FOUND_OR_EXCEPTION( |
| 2750 | FindClassInSharedLibrariesAfter(soa, self, descriptor, hash, class_loader, result), |
| 2751 | *result, |
| 2752 | self); |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 2753 | |
Nicolas Geoffray | e8445e5 | 2021-09-23 14:10:05 +0100 | [diff] [blame] | 2754 | // 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] | 2755 | StackHandleScope<1> hs(self); |
| 2756 | Handle<mirror::ClassLoader> h_parent(hs.NewHandle(class_loader->GetParent())); |
Nicolas Geoffray | e8445e5 | 2021-09-23 14:10:05 +0100 | [diff] [blame] | 2757 | RETURN_IF_UNRECOGNIZED_OR_FOUND_OR_EXCEPTION( |
| 2758 | FindClassInBaseDexClassLoader(soa, self, descriptor, hash, h_parent, result), |
| 2759 | *result, |
| 2760 | self); |
| 2761 | // We did not find a class, but the class loader chain was recognized, so we |
| 2762 | // return true. |
| 2763 | return true; |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 2764 | } |
| 2765 | |
| 2766 | // Unsupported class loader. |
| 2767 | *result = nullptr; |
| 2768 | return false; |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 2769 | } |
| 2770 | |
Nicolas Geoffray | e8445e5 | 2021-09-23 14:10:05 +0100 | [diff] [blame] | 2771 | #undef RETURN_IF_UNRECOGNIZED_OR_FOUND_OR_EXCEPTION |
| 2772 | |
Andreas Gampe | 501c3b0 | 2019-04-17 21:54:27 +0000 | [diff] [blame] | 2773 | namespace { |
| 2774 | |
| 2775 | // Matches exceptions caught in DexFile.defineClass. |
| 2776 | ALWAYS_INLINE bool MatchesDexFileCaughtExceptions(ObjPtr<mirror::Throwable> throwable, |
| 2777 | ClassLinker* class_linker) |
| 2778 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 2779 | return |
| 2780 | // ClassNotFoundException. |
| 2781 | throwable->InstanceOf(GetClassRoot(ClassRoot::kJavaLangClassNotFoundException, |
| 2782 | class_linker)) |
| 2783 | || |
| 2784 | // NoClassDefFoundError. TODO: Reconsider this. b/130746382. |
| 2785 | throwable->InstanceOf(Runtime::Current()->GetPreAllocatedNoClassDefFoundError()->GetClass()); |
| 2786 | } |
| 2787 | |
| 2788 | // Clear exceptions caught in DexFile.defineClass. |
| 2789 | ALWAYS_INLINE void FilterDexFileCaughtExceptions(Thread* self, ClassLinker* class_linker) |
| 2790 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 2791 | if (MatchesDexFileCaughtExceptions(self->GetException(), class_linker)) { |
| 2792 | self->ClearException(); |
| 2793 | } |
| 2794 | } |
| 2795 | |
| 2796 | } // namespace |
| 2797 | |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 2798 | // Finds the class in the boot class loader. |
| 2799 | // 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] | 2800 | bool ClassLinker::FindClassInBootClassLoaderClassPath(Thread* self, |
| 2801 | const char* descriptor, |
| 2802 | size_t hash, |
| 2803 | /*out*/ ObjPtr<mirror::Class>* result) { |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 2804 | ClassPathEntry pair = FindInClassPath(descriptor, hash, boot_class_path_); |
| 2805 | if (pair.second != nullptr) { |
| 2806 | ObjPtr<mirror::Class> klass = LookupClass(self, descriptor, hash, nullptr); |
| 2807 | if (klass != nullptr) { |
Nicolas Geoffray | e8445e5 | 2021-09-23 14:10:05 +0100 | [diff] [blame] | 2808 | *result = EnsureResolved(self, descriptor, klass); |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2809 | } else { |
Nicolas Geoffray | e8445e5 | 2021-09-23 14:10:05 +0100 | [diff] [blame] | 2810 | *result = DefineClass(self, |
| 2811 | descriptor, |
| 2812 | hash, |
| 2813 | ScopedNullHandle<mirror::ClassLoader>(), |
| 2814 | *pair.first, |
| 2815 | *pair.second); |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2816 | } |
Nicolas Geoffray | e8445e5 | 2021-09-23 14:10:05 +0100 | [diff] [blame] | 2817 | if (*result == nullptr) { |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 2818 | CHECK(self->IsExceptionPending()) << descriptor; |
Andreas Gampe | 501c3b0 | 2019-04-17 21:54:27 +0000 | [diff] [blame] | 2819 | FilterDexFileCaughtExceptions(self, this); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2820 | } |
| 2821 | } |
Nicolas Geoffray | e8445e5 | 2021-09-23 14:10:05 +0100 | [diff] [blame] | 2822 | // The boot classloader is always a known lookup. |
| 2823 | return true; |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 2824 | } |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2825 | |
Nicolas Geoffray | e8445e5 | 2021-09-23 14:10:05 +0100 | [diff] [blame] | 2826 | bool ClassLinker::FindClassInBaseDexClassLoaderClassPath( |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 2827 | ScopedObjectAccessAlreadyRunnable& soa, |
| 2828 | const char* descriptor, |
| 2829 | size_t hash, |
Nicolas Geoffray | e8445e5 | 2021-09-23 14:10:05 +0100 | [diff] [blame] | 2830 | Handle<mirror::ClassLoader> class_loader, |
| 2831 | /*out*/ ObjPtr<mirror::Class>* result) { |
David Brazdil | 05909d8 | 2018-12-06 16:25:16 +0000 | [diff] [blame] | 2832 | DCHECK(IsPathOrDexClassLoader(soa, class_loader) || |
| 2833 | IsInMemoryDexClassLoader(soa, class_loader) || |
| 2834 | IsDelegateLastClassLoader(soa, class_loader)) |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 2835 | << "Unexpected class loader for descriptor " << descriptor; |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2836 | |
Vladimir Marko | 68c0758 | 2021-04-19 16:01:15 +0000 | [diff] [blame] | 2837 | const DexFile* dex_file = nullptr; |
| 2838 | const dex::ClassDef* class_def = nullptr; |
Andreas Gampe | b8e7c37 | 2018-02-20 18:24:55 -0800 | [diff] [blame] | 2839 | ObjPtr<mirror::Class> ret; |
Vladimir Marko | 68c0758 | 2021-04-19 16:01:15 +0000 | [diff] [blame] | 2840 | auto find_class_def = [&](const DexFile* cp_dex_file) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 2841 | const dex::ClassDef* cp_class_def = OatDexFile::FindClassDef(*cp_dex_file, descriptor, hash); |
| 2842 | if (cp_class_def != nullptr) { |
| 2843 | dex_file = cp_dex_file; |
| 2844 | class_def = cp_class_def; |
| 2845 | return false; // Found a class definition, stop visit. |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2846 | } |
Andreas Gampe | b8e7c37 | 2018-02-20 18:24:55 -0800 | [diff] [blame] | 2847 | return true; // Continue with the next DexFile. |
| 2848 | }; |
Vladimir Marko | 68c0758 | 2021-04-19 16:01:15 +0000 | [diff] [blame] | 2849 | VisitClassLoaderDexFiles(soa, class_loader, find_class_def); |
Andreas Gampe | b8e7c37 | 2018-02-20 18:24:55 -0800 | [diff] [blame] | 2850 | |
Vladimir Marko | 68c0758 | 2021-04-19 16:01:15 +0000 | [diff] [blame] | 2851 | if (class_def != nullptr) { |
Nicolas Geoffray | e8445e5 | 2021-09-23 14:10:05 +0100 | [diff] [blame] | 2852 | *result = DefineClass(soa.Self(), descriptor, hash, class_loader, *dex_file, *class_def); |
| 2853 | if (UNLIKELY(*result == nullptr)) { |
Vladimir Marko | 68c0758 | 2021-04-19 16:01:15 +0000 | [diff] [blame] | 2854 | CHECK(soa.Self()->IsExceptionPending()) << descriptor; |
| 2855 | FilterDexFileCaughtExceptions(soa.Self(), this); |
| 2856 | } else { |
| 2857 | DCHECK(!soa.Self()->IsExceptionPending()); |
| 2858 | } |
| 2859 | } |
Nicolas Geoffray | e8445e5 | 2021-09-23 14:10:05 +0100 | [diff] [blame] | 2860 | // A BaseDexClassLoader is always a known lookup. |
| 2861 | return true; |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2862 | } |
| 2863 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 2864 | ObjPtr<mirror::Class> ClassLinker::FindClass(Thread* self, |
| 2865 | const char* descriptor, |
| 2866 | Handle<mirror::ClassLoader> class_loader) { |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 2867 | DCHECK_NE(*descriptor, '\0') << "descriptor is empty string"; |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 2868 | DCHECK(self != nullptr); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2869 | self->AssertNoPendingException(); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 2870 | self->PoisonObjectPointers(); // For DefineClass, CreateArrayClass, etc... |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 2871 | if (descriptor[1] == '\0') { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2872 | // only the descriptors of primitive types should be 1 character long, also avoid class lookup |
| 2873 | // for primitive classes that aren't backed by dex files. |
| 2874 | return FindPrimitiveClass(descriptor[0]); |
| 2875 | } |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2876 | const size_t hash = ComputeModifiedUtf8Hash(descriptor); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2877 | // Find the class in the loaded classes table. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2878 | ObjPtr<mirror::Class> klass = LookupClass(self, descriptor, hash, class_loader.Get()); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2879 | if (klass != nullptr) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2880 | return EnsureResolved(self, descriptor, klass); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2881 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2882 | // Class is not yet loaded. |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 2883 | if (descriptor[0] != '[' && class_loader == nullptr) { |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 2884 | // 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] | 2885 | ClassPathEntry pair = FindInClassPath(descriptor, hash, boot_class_path_); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2886 | if (pair.second != nullptr) { |
Mathieu Chartier | 9865bde | 2015-12-21 09:58:16 -0800 | [diff] [blame] | 2887 | return DefineClass(self, |
| 2888 | descriptor, |
| 2889 | hash, |
| 2890 | ScopedNullHandle<mirror::ClassLoader>(), |
| 2891 | *pair.first, |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2892 | *pair.second); |
Ian Rogers | 6355745 | 2014-06-04 16:57:15 -0700 | [diff] [blame] | 2893 | } else { |
| 2894 | // The boot class loader is searched ahead of the application class loader, failures are |
| 2895 | // expected and will be wrapped in a ClassNotFoundException. Use the pre-allocated error to |
| 2896 | // trigger the chaining with a proper stack trace. |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 2897 | ObjPtr<mirror::Throwable> pre_allocated = |
| 2898 | Runtime::Current()->GetPreAllocatedNoClassDefFoundError(); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2899 | self->SetException(pre_allocated); |
Ian Rogers | 6355745 | 2014-06-04 16:57:15 -0700 | [diff] [blame] | 2900 | return nullptr; |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 2901 | } |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 2902 | } |
| 2903 | ObjPtr<mirror::Class> result_ptr; |
| 2904 | bool descriptor_equals; |
| 2905 | if (descriptor[0] == '[') { |
| 2906 | result_ptr = CreateArrayClass(self, descriptor, hash, class_loader); |
| 2907 | DCHECK_EQ(result_ptr == nullptr, self->IsExceptionPending()); |
| 2908 | DCHECK(result_ptr == nullptr || result_ptr->DescriptorEquals(descriptor)); |
| 2909 | descriptor_equals = true; |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 2910 | } else { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 2911 | ScopedObjectAccessUnchecked soa(self); |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2912 | bool known_hierarchy = |
| 2913 | FindClassInBaseDexClassLoader(soa, self, descriptor, hash, class_loader, &result_ptr); |
| 2914 | if (result_ptr != nullptr) { |
| 2915 | // The chain was understood and we found the class. We still need to add the class to |
| 2916 | // the class table to protect from racy programs that can try and redefine the path list |
| 2917 | // which would change the Class<?> returned for subsequent evaluation of const-class. |
| 2918 | DCHECK(known_hierarchy); |
| 2919 | DCHECK(result_ptr->DescriptorEquals(descriptor)); |
| 2920 | descriptor_equals = true; |
Andreas Gampe | 501c3b0 | 2019-04-17 21:54:27 +0000 | [diff] [blame] | 2921 | } else if (!self->IsExceptionPending()) { |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2922 | // Either the chain wasn't understood or the class wasn't found. |
Andreas Gampe | 501c3b0 | 2019-04-17 21:54:27 +0000 | [diff] [blame] | 2923 | // If there is a pending exception we didn't clear, it is a not a ClassNotFoundException and |
| 2924 | // we should return it instead of silently clearing and retrying. |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2925 | // |
| 2926 | // If the chain was understood but we did not find the class, let the Java-side |
| 2927 | // rediscover all this and throw the exception with the right stack trace. Note that |
| 2928 | // the Java-side could still succeed for racy programs if another thread is actively |
| 2929 | // modifying the class loader's path list. |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2930 | |
Alex Light | 185a461 | 2018-10-04 15:54:25 -0700 | [diff] [blame] | 2931 | // 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] | 2932 | if (self->IsRuntimeThread()) { |
Calin Juravle | ccd5695 | 2016-12-15 17:57:38 +0000 | [diff] [blame] | 2933 | // Oops, we can't call into java so we can't run actual class-loader code. |
| 2934 | // This is true for e.g. for the compiler (jit or aot). |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2935 | ObjPtr<mirror::Throwable> pre_allocated = |
| 2936 | Runtime::Current()->GetPreAllocatedNoClassDefFoundError(); |
| 2937 | self->SetException(pre_allocated); |
Vladimir Marko | 2c8c6b6 | 2016-12-01 17:42:00 +0000 | [diff] [blame] | 2938 | return nullptr; |
| 2939 | } |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2940 | |
Vladimir Marko | 5fdd778 | 2017-04-20 11:26:03 +0100 | [diff] [blame] | 2941 | // Inlined DescriptorToDot(descriptor) with extra validation. |
| 2942 | // |
| 2943 | // Throw NoClassDefFoundError early rather than potentially load a class only to fail |
| 2944 | // the DescriptorEquals() check below and give a confusing error message. For example, |
| 2945 | // when native code erroneously calls JNI GetFieldId() with signature "java/lang/String" |
| 2946 | // instead of "Ljava/lang/String;", the message below using the "dot" names would be |
| 2947 | // "class loader [...] returned class java.lang.String instead of java.lang.String". |
| 2948 | size_t descriptor_length = strlen(descriptor); |
| 2949 | if (UNLIKELY(descriptor[0] != 'L') || |
| 2950 | UNLIKELY(descriptor[descriptor_length - 1] != ';') || |
| 2951 | UNLIKELY(memchr(descriptor + 1, '.', descriptor_length - 2) != nullptr)) { |
| 2952 | ThrowNoClassDefFoundError("Invalid descriptor: %s.", descriptor); |
| 2953 | return nullptr; |
| 2954 | } |
Andreas Gampe | 501c3b0 | 2019-04-17 21:54:27 +0000 | [diff] [blame] | 2955 | |
Vladimir Marko | 5fdd778 | 2017-04-20 11:26:03 +0100 | [diff] [blame] | 2956 | std::string class_name_string(descriptor + 1, descriptor_length - 2); |
| 2957 | std::replace(class_name_string.begin(), class_name_string.end(), '/', '.'); |
Andreas Gampe | 87658f3 | 2019-04-18 18:39:02 +0000 | [diff] [blame] | 2958 | if (known_hierarchy && |
| 2959 | fast_class_not_found_exceptions_ && |
| 2960 | !Runtime::Current()->IsJavaDebuggable()) { |
| 2961 | // For known hierarchy, we know that the class is going to throw an exception. If we aren't |
| 2962 | // debuggable, optimize this path by throwing directly here without going back to Java |
| 2963 | // language. This reduces how many ClassNotFoundExceptions happen. |
| 2964 | self->ThrowNewExceptionF("Ljava/lang/ClassNotFoundException;", |
| 2965 | "%s", |
| 2966 | class_name_string.c_str()); |
| 2967 | } else { |
| 2968 | ScopedLocalRef<jobject> class_loader_object( |
| 2969 | soa.Env(), soa.AddLocalReference<jobject>(class_loader.Get())); |
| 2970 | ScopedLocalRef<jobject> result(soa.Env(), nullptr); |
| 2971 | { |
Vladimir Marko | ddf4fd3 | 2021-11-22 16:31:57 +0000 | [diff] [blame] | 2972 | ScopedThreadStateChange tsc(self, ThreadState::kNative); |
Andreas Gampe | 87658f3 | 2019-04-18 18:39:02 +0000 | [diff] [blame] | 2973 | ScopedLocalRef<jobject> class_name_object( |
| 2974 | soa.Env(), soa.Env()->NewStringUTF(class_name_string.c_str())); |
| 2975 | if (class_name_object.get() == nullptr) { |
| 2976 | DCHECK(self->IsExceptionPending()); // OOME. |
| 2977 | return nullptr; |
| 2978 | } |
| 2979 | CHECK(class_loader_object.get() != nullptr); |
| 2980 | result.reset(soa.Env()->CallObjectMethod(class_loader_object.get(), |
| 2981 | WellKnownClasses::java_lang_ClassLoader_loadClass, |
| 2982 | class_name_object.get())); |
| 2983 | } |
| 2984 | if (result.get() == nullptr && !self->IsExceptionPending()) { |
| 2985 | // broken loader - throw NPE to be compatible with Dalvik |
| 2986 | ThrowNullPointerException(StringPrintf("ClassLoader.loadClass returned null for %s", |
| 2987 | class_name_string.c_str()).c_str()); |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2988 | return nullptr; |
| 2989 | } |
Andreas Gampe | 87658f3 | 2019-04-18 18:39:02 +0000 | [diff] [blame] | 2990 | result_ptr = soa.Decode<mirror::Class>(result.get()); |
| 2991 | // Check the name of the returned class. |
| 2992 | descriptor_equals = (result_ptr != nullptr) && result_ptr->DescriptorEquals(descriptor); |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2993 | } |
Andreas Gampe | 501c3b0 | 2019-04-17 21:54:27 +0000 | [diff] [blame] | 2994 | } else { |
| 2995 | DCHECK(!MatchesDexFileCaughtExceptions(self->GetException(), this)); |
Vladimir Marko | 2c8c6b6 | 2016-12-01 17:42:00 +0000 | [diff] [blame] | 2996 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2997 | } |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 2998 | |
| 2999 | if (self->IsExceptionPending()) { |
| 3000 | // If the ClassLoader threw or array class allocation failed, pass that exception up. |
| 3001 | // However, to comply with the RI behavior, first check if another thread succeeded. |
| 3002 | result_ptr = LookupClass(self, descriptor, hash, class_loader.Get()); |
| 3003 | if (result_ptr != nullptr && !result_ptr->IsErroneous()) { |
| 3004 | self->ClearException(); |
| 3005 | return EnsureResolved(self, descriptor, result_ptr); |
| 3006 | } |
| 3007 | return nullptr; |
| 3008 | } |
| 3009 | |
| 3010 | // Try to insert the class to the class table, checking for mismatch. |
| 3011 | ObjPtr<mirror::Class> old; |
| 3012 | { |
| 3013 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 3014 | ClassTable* const class_table = InsertClassTableForClassLoader(class_loader.Get()); |
| 3015 | old = class_table->Lookup(descriptor, hash); |
| 3016 | if (old == nullptr) { |
| 3017 | old = result_ptr; // For the comparison below, after releasing the lock. |
| 3018 | if (descriptor_equals) { |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 3019 | class_table->InsertWithHash(result_ptr, hash); |
Mathieu Chartier | 88ea61e | 2018-06-20 17:45:41 -0700 | [diff] [blame] | 3020 | WriteBarrier::ForEveryFieldWrite(class_loader.Get()); |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 3021 | } // else throw below, after releasing the lock. |
| 3022 | } |
| 3023 | } |
| 3024 | if (UNLIKELY(old != result_ptr)) { |
| 3025 | // Return `old` (even if `!descriptor_equals`) to mimic the RI behavior for parallel |
| 3026 | // capable class loaders. (All class loaders are considered parallel capable on Android.) |
Vladimir Marko | dfc0de7 | 2019-04-01 10:57:55 +0100 | [diff] [blame] | 3027 | ObjPtr<mirror::Class> loader_class = class_loader->GetClass(); |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 3028 | const char* loader_class_name = |
| 3029 | loader_class->GetDexFile().StringByTypeIdx(loader_class->GetDexTypeIndex()); |
| 3030 | LOG(WARNING) << "Initiating class loader of type " << DescriptorToDot(loader_class_name) |
| 3031 | << " is not well-behaved; it returned a different Class for racing loadClass(\"" |
| 3032 | << DescriptorToDot(descriptor) << "\")."; |
| 3033 | return EnsureResolved(self, descriptor, old); |
| 3034 | } |
| 3035 | if (UNLIKELY(!descriptor_equals)) { |
| 3036 | std::string result_storage; |
| 3037 | const char* result_name = result_ptr->GetDescriptor(&result_storage); |
| 3038 | std::string loader_storage; |
| 3039 | const char* loader_class_name = class_loader->GetClass()->GetDescriptor(&loader_storage); |
| 3040 | ThrowNoClassDefFoundError( |
| 3041 | "Initiating class loader of type %s returned class %s instead of %s.", |
| 3042 | DescriptorToDot(loader_class_name).c_str(), |
| 3043 | DescriptorToDot(result_name).c_str(), |
| 3044 | DescriptorToDot(descriptor).c_str()); |
| 3045 | return nullptr; |
| 3046 | } |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 3047 | // Success. |
| 3048 | return result_ptr; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3049 | } |
| 3050 | |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 3051 | // Helper for maintaining DefineClass counting. We need to notify callbacks when we start/end a |
| 3052 | // define-class and how many recursive DefineClasses we are at in order to allow for doing things |
| 3053 | // like pausing class definition. |
| 3054 | struct ScopedDefiningClass { |
| 3055 | public: |
| 3056 | explicit ScopedDefiningClass(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_) |
| 3057 | : self_(self), returned_(false) { |
| 3058 | Locks::mutator_lock_->AssertSharedHeld(self_); |
| 3059 | Runtime::Current()->GetRuntimeCallbacks()->BeginDefineClass(); |
| 3060 | self_->IncrDefineClassCount(); |
| 3061 | } |
| 3062 | ~ScopedDefiningClass() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 3063 | Locks::mutator_lock_->AssertSharedHeld(self_); |
| 3064 | CHECK(returned_); |
| 3065 | } |
| 3066 | |
| 3067 | ObjPtr<mirror::Class> Finish(Handle<mirror::Class> h_klass) |
| 3068 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 3069 | CHECK(!returned_); |
| 3070 | self_->DecrDefineClassCount(); |
| 3071 | Runtime::Current()->GetRuntimeCallbacks()->EndDefineClass(); |
| 3072 | Thread::PoisonObjectPointersIfDebug(); |
| 3073 | returned_ = true; |
| 3074 | return h_klass.Get(); |
| 3075 | } |
| 3076 | |
| 3077 | ObjPtr<mirror::Class> Finish(ObjPtr<mirror::Class> klass) |
| 3078 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 3079 | StackHandleScope<1> hs(self_); |
| 3080 | Handle<mirror::Class> h_klass(hs.NewHandle(klass)); |
| 3081 | return Finish(h_klass); |
| 3082 | } |
| 3083 | |
| 3084 | ObjPtr<mirror::Class> Finish(nullptr_t np ATTRIBUTE_UNUSED) |
| 3085 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 3086 | ScopedNullHandle<mirror::Class> snh; |
| 3087 | return Finish(snh); |
| 3088 | } |
| 3089 | |
| 3090 | private: |
| 3091 | Thread* self_; |
| 3092 | bool returned_; |
| 3093 | }; |
| 3094 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 3095 | ObjPtr<mirror::Class> ClassLinker::DefineClass(Thread* self, |
| 3096 | const char* descriptor, |
| 3097 | size_t hash, |
| 3098 | Handle<mirror::ClassLoader> class_loader, |
| 3099 | const DexFile& dex_file, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 3100 | const dex::ClassDef& dex_class_def) { |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 3101 | ScopedDefiningClass sdc(self); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3102 | StackHandleScope<3> hs(self); |
Eric Holk | 74584e6 | 2021-02-18 14:39:17 -0800 | [diff] [blame] | 3103 | metrics::AutoTimer timer{GetMetrics()->ClassLoadingTotalTime()}; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3104 | auto klass = hs.NewHandle<mirror::Class>(nullptr); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3105 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3106 | // Load the class from the dex file. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3107 | if (UNLIKELY(!init_done_)) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3108 | // finish up init of hand crafted class_roots_ |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3109 | if (strcmp(descriptor, "Ljava/lang/Object;") == 0) { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 3110 | klass.Assign(GetClassRoot<mirror::Object>(this)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3111 | } else if (strcmp(descriptor, "Ljava/lang/Class;") == 0) { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 3112 | klass.Assign(GetClassRoot<mirror::Class>(this)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3113 | } else if (strcmp(descriptor, "Ljava/lang/String;") == 0) { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 3114 | klass.Assign(GetClassRoot<mirror::String>(this)); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 3115 | } else if (strcmp(descriptor, "Ljava/lang/ref/Reference;") == 0) { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 3116 | klass.Assign(GetClassRoot<mirror::Reference>(this)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3117 | } else if (strcmp(descriptor, "Ljava/lang/DexCache;") == 0) { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 3118 | klass.Assign(GetClassRoot<mirror::DexCache>(this)); |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 3119 | } else if (strcmp(descriptor, "Ldalvik/system/ClassExt;") == 0) { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 3120 | klass.Assign(GetClassRoot<mirror::ClassExt>(this)); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3121 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3122 | } |
| 3123 | |
Calin Juravle | 3378768 | 2019-07-26 14:27:18 -0700 | [diff] [blame] | 3124 | // For AOT-compilation of an app, we may use only a public SDK to resolve symbols. If the SDK |
| 3125 | // checks are configured (a non null SdkChecker) and the descriptor is not in the provided |
| 3126 | // public class path then we prevent the definition of the class. |
| 3127 | // |
| 3128 | // NOTE that we only do the checks for the boot classpath APIs. Anything else, like the app |
| 3129 | // classpath is not checked. |
| 3130 | if (class_loader == nullptr && |
| 3131 | Runtime::Current()->IsAotCompiler() && |
| 3132 | DenyAccessBasedOnPublicSdk(descriptor)) { |
| 3133 | ObjPtr<mirror::Throwable> pre_allocated = |
| 3134 | Runtime::Current()->GetPreAllocatedNoClassDefFoundError(); |
| 3135 | self->SetException(pre_allocated); |
| 3136 | return sdc.Finish(nullptr); |
| 3137 | } |
| 3138 | |
Alex Light | e9f6103 | 2018-09-24 16:04:51 -0700 | [diff] [blame] | 3139 | // This is to prevent the calls to ClassLoad and ClassPrepare which can cause java/user-supplied |
| 3140 | // code to be executed. We put it up here so we can avoid all the allocations associated with |
| 3141 | // creating the class. This can happen with (eg) jit threads. |
| 3142 | if (!self->CanLoadClasses()) { |
| 3143 | // Make sure we don't try to load anything, potentially causing an infinite loop. |
| 3144 | ObjPtr<mirror::Throwable> pre_allocated = |
| 3145 | Runtime::Current()->GetPreAllocatedNoClassDefFoundError(); |
| 3146 | self->SetException(pre_allocated); |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 3147 | return sdc.Finish(nullptr); |
Alex Light | e9f6103 | 2018-09-24 16:04:51 -0700 | [diff] [blame] | 3148 | } |
| 3149 | |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3150 | if (klass == nullptr) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3151 | // Allocate a class with the status of not ready. |
| 3152 | // Interface object should get the right size here. Regular class will |
| 3153 | // figure out the right size later and be replaced with one of the right |
| 3154 | // size when the class becomes resolved. |
Chang Xing | 0c2c222 | 2017-08-04 14:36:17 -0700 | [diff] [blame] | 3155 | if (CanAllocClass()) { |
| 3156 | klass.Assign(AllocClass(self, SizeOfClassWithoutEmbeddedTables(dex_file, dex_class_def))); |
| 3157 | } else { |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 3158 | return sdc.Finish(nullptr); |
Chang Xing | 0c2c222 | 2017-08-04 14:36:17 -0700 | [diff] [blame] | 3159 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3160 | } |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3161 | if (UNLIKELY(klass == nullptr)) { |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3162 | self->AssertPendingOOMException(); |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 3163 | return sdc.Finish(nullptr); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3164 | } |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 3165 | // Get the real dex file. This will return the input if there aren't any callbacks or they do |
| 3166 | // nothing. |
| 3167 | DexFile const* new_dex_file = nullptr; |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 3168 | dex::ClassDef const* new_class_def = nullptr; |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 3169 | // TODO We should ideally figure out some way to move this after we get a lock on the klass so it |
| 3170 | // will only be called once. |
| 3171 | Runtime::Current()->GetRuntimeCallbacks()->ClassPreDefine(descriptor, |
| 3172 | klass, |
| 3173 | class_loader, |
| 3174 | dex_file, |
| 3175 | dex_class_def, |
| 3176 | &new_dex_file, |
| 3177 | &new_class_def); |
Alex Light | 440b5d9 | 2017-01-24 15:32:25 -0800 | [diff] [blame] | 3178 | // Check to see if an exception happened during runtime callbacks. Return if so. |
| 3179 | if (self->IsExceptionPending()) { |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 3180 | return sdc.Finish(nullptr); |
Alex Light | 440b5d9 | 2017-01-24 15:32:25 -0800 | [diff] [blame] | 3181 | } |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 3182 | ObjPtr<mirror::DexCache> dex_cache = RegisterDexFile(*new_dex_file, class_loader.Get()); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3183 | if (dex_cache == nullptr) { |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3184 | self->AssertPendingException(); |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 3185 | return sdc.Finish(nullptr); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3186 | } |
| 3187 | klass->SetDexCache(dex_cache); |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 3188 | SetupClass(*new_dex_file, *new_class_def, klass, class_loader.Get()); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3189 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 3190 | // Mark the string class by setting its access flag. |
| 3191 | if (UNLIKELY(!init_done_)) { |
| 3192 | if (strcmp(descriptor, "Ljava/lang/String;") == 0) { |
| 3193 | klass->SetStringClass(); |
| 3194 | } |
| 3195 | } |
| 3196 | |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 3197 | ObjectLock<mirror::Class> lock(self, klass); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3198 | klass->SetClinitThreadId(self->GetTid()); |
Mathieu Chartier | 1e4841e | 2016-12-15 14:21:04 -0800 | [diff] [blame] | 3199 | // 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] | 3200 | klass->SetIfTable(GetClassRoot<mirror::Object>(this)->GetIfTable()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3201 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3202 | // Add the newly loaded class to the loaded classes table. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3203 | ObjPtr<mirror::Class> existing = InsertClass(descriptor, klass.Get(), hash); |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 3204 | if (existing != nullptr) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3205 | // We failed to insert because we raced with another thread. Calling EnsureResolved may cause |
| 3206 | // this thread to block. |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 3207 | return sdc.Finish(EnsureResolved(self, descriptor, existing)); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3208 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3209 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3210 | // Load the fields and other things after we are inserted in the table. This is so that we don't |
| 3211 | // end up allocating unfree-able linear alloc resources and then lose the race condition. The |
| 3212 | // other reason is that the field roots are only visited from the class table. So we need to be |
| 3213 | // inserted before we allocate / fill in these fields. |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 3214 | LoadClass(self, *new_dex_file, *new_class_def, klass); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3215 | if (self->IsExceptionPending()) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 3216 | VLOG(class_linker) << self->GetException()->Dump(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3217 | // An exception occured during load, set status to erroneous while holding klass' lock in case |
| 3218 | // notification is necessary. |
| 3219 | if (!klass->IsErroneous()) { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 3220 | mirror::Class::SetStatus(klass, ClassStatus::kErrorUnresolved, self); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3221 | } |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 3222 | return sdc.Finish(nullptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3223 | } |
| 3224 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3225 | // Finish loading (if necessary) by finding parents |
| 3226 | CHECK(!klass->IsLoaded()); |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 3227 | if (!LoadSuperAndInterfaces(klass, *new_dex_file)) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3228 | // Loading failed. |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 3229 | if (!klass->IsErroneous()) { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 3230 | mirror::Class::SetStatus(klass, ClassStatus::kErrorUnresolved, self); |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 3231 | } |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 3232 | return sdc.Finish(nullptr); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3233 | } |
| 3234 | CHECK(klass->IsLoaded()); |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 3235 | |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 3236 | // At this point the class is loaded. Publish a ClassLoad event. |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 3237 | // 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] | 3238 | Runtime::Current()->GetRuntimeCallbacks()->ClassLoad(klass); |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 3239 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3240 | // Link the class (if necessary) |
| 3241 | CHECK(!klass->IsResolved()); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3242 | // TODO: Use fast jobjects? |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3243 | auto interfaces = hs.NewHandle<mirror::ObjectArray<mirror::Class>>(nullptr); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3244 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 3245 | MutableHandle<mirror::Class> h_new_class = hs.NewHandle<mirror::Class>(nullptr); |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 3246 | if (!LinkClass(self, descriptor, klass, interfaces, &h_new_class)) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3247 | // Linking failed. |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 3248 | if (!klass->IsErroneous()) { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 3249 | mirror::Class::SetStatus(klass, ClassStatus::kErrorUnresolved, self); |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 3250 | } |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 3251 | return sdc.Finish(nullptr); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3252 | } |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 3253 | self->AssertNoPendingException(); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3254 | CHECK(h_new_class != nullptr) << descriptor; |
Vladimir Marko | f9e82e5 | 2021-10-06 08:34:40 +0100 | [diff] [blame] | 3255 | CHECK(h_new_class->IsResolved()) << descriptor << " " << h_new_class->GetStatus(); |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 3256 | |
Sebastien Hertz | a8a697f | 2015-01-15 12:28:47 +0100 | [diff] [blame] | 3257 | // Instrumentation may have updated entrypoints for all methods of all |
| 3258 | // classes. However it could not update methods of this class while we |
| 3259 | // were loading it. Now the class is resolved, we can update entrypoints |
| 3260 | // as required by instrumentation. |
| 3261 | if (Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled()) { |
| 3262 | // We must be in the kRunnable state to prevent instrumentation from |
| 3263 | // suspending all threads to update entrypoints while we are doing it |
| 3264 | // for this class. |
Vladimir Marko | ddf4fd3 | 2021-11-22 16:31:57 +0000 | [diff] [blame] | 3265 | DCHECK_EQ(self->GetState(), ThreadState::kRunnable); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 3266 | Runtime::Current()->GetInstrumentation()->InstallStubsForClass(h_new_class.Get()); |
Sebastien Hertz | a8a697f | 2015-01-15 12:28:47 +0100 | [diff] [blame] | 3267 | } |
| 3268 | |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 3269 | /* |
| 3270 | * We send CLASS_PREPARE events to the debugger from here. The |
| 3271 | * definition of "preparation" is creating the static fields for a |
| 3272 | * class and initializing them to the standard default values, but not |
| 3273 | * executing any code (that comes later, during "initialization"). |
| 3274 | * |
| 3275 | * We did the static preparation in LinkClass. |
| 3276 | * |
| 3277 | * The class has been prepared and resolved but possibly not yet verified |
| 3278 | * at this point. |
| 3279 | */ |
Andreas Gampe | ac30fa2 | 2017-01-18 21:02:36 -0800 | [diff] [blame] | 3280 | Runtime::Current()->GetRuntimeCallbacks()->ClassPrepare(klass, h_new_class); |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 3281 | |
Tamas Berghammer | 160e6df | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 3282 | // Notify native debugger of the new class and its layout. |
| 3283 | jit::Jit::NewTypeLoadedIfUsingJit(h_new_class.Get()); |
| 3284 | |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 3285 | return sdc.Finish(h_new_class); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3286 | } |
| 3287 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3288 | uint32_t ClassLinker::SizeOfClassWithoutEmbeddedTables(const DexFile& dex_file, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 3289 | const dex::ClassDef& dex_class_def) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 3290 | size_t num_ref = 0; |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 3291 | size_t num_8 = 0; |
| 3292 | size_t num_16 = 0; |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 3293 | size_t num_32 = 0; |
| 3294 | size_t num_64 = 0; |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3295 | ClassAccessor accessor(dex_file, dex_class_def); |
| 3296 | // We allow duplicate definitions of the same field in a class_data_item |
| 3297 | // but ignore the repeated indexes here, b/21868015. |
| 3298 | uint32_t last_field_idx = dex::kDexNoIndex; |
| 3299 | for (const ClassAccessor::Field& field : accessor.GetStaticFields()) { |
| 3300 | uint32_t field_idx = field.GetIndex(); |
| 3301 | // Ordering enforced by DexFileVerifier. |
| 3302 | DCHECK(last_field_idx == dex::kDexNoIndex || last_field_idx <= field_idx); |
| 3303 | if (UNLIKELY(field_idx == last_field_idx)) { |
| 3304 | continue; |
| 3305 | } |
| 3306 | last_field_idx = field_idx; |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 3307 | const dex::FieldId& field_id = dex_file.GetFieldId(field_idx); |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3308 | const char* descriptor = dex_file.GetFieldTypeDescriptor(field_id); |
| 3309 | char c = descriptor[0]; |
| 3310 | switch (c) { |
| 3311 | case 'L': |
| 3312 | case '[': |
| 3313 | num_ref++; |
| 3314 | break; |
| 3315 | case 'J': |
| 3316 | case 'D': |
| 3317 | num_64++; |
| 3318 | break; |
| 3319 | case 'I': |
| 3320 | case 'F': |
| 3321 | num_32++; |
| 3322 | break; |
| 3323 | case 'S': |
| 3324 | case 'C': |
| 3325 | num_16++; |
| 3326 | break; |
| 3327 | case 'B': |
| 3328 | case 'Z': |
| 3329 | num_8++; |
| 3330 | break; |
| 3331 | default: |
| 3332 | LOG(FATAL) << "Unknown descriptor: " << c; |
| 3333 | UNREACHABLE(); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 3334 | } |
| 3335 | } |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3336 | return mirror::Class::ComputeClassSize(false, |
| 3337 | 0, |
| 3338 | num_8, |
| 3339 | num_16, |
| 3340 | num_32, |
| 3341 | num_64, |
| 3342 | num_ref, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3343 | image_pointer_size_); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 3344 | } |
| 3345 | |
Vladimir Marko | 86c8752 | 2020-05-11 16:55:55 +0100 | [diff] [blame] | 3346 | void ClassLinker::FixupStaticTrampolines(Thread* self, ObjPtr<mirror::Class> klass) { |
Alex Light | 2d441b1 | 2018-06-08 15:33:21 -0700 | [diff] [blame] | 3347 | ScopedAssertNoThreadSuspension sants(__FUNCTION__); |
Vladimir Marko | cce414f | 2019-10-07 08:51:33 +0100 | [diff] [blame] | 3348 | DCHECK(klass->IsVisiblyInitialized()) << klass->PrettyDescriptor(); |
Vladimir Marko | 86c8752 | 2020-05-11 16:55:55 +0100 | [diff] [blame] | 3349 | size_t num_direct_methods = klass->NumDirectMethods(); |
| 3350 | if (num_direct_methods == 0) { |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 3351 | return; // No direct methods => no static methods. |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3352 | } |
Vladimir Marko | cce414f | 2019-10-07 08:51:33 +0100 | [diff] [blame] | 3353 | if (UNLIKELY(klass->IsProxyClass())) { |
| 3354 | return; |
| 3355 | } |
Vladimir Marko | 86c8752 | 2020-05-11 16:55:55 +0100 | [diff] [blame] | 3356 | PointerSize pointer_size = image_pointer_size_; |
| 3357 | if (std::any_of(klass->GetDirectMethods(pointer_size).begin(), |
| 3358 | klass->GetDirectMethods(pointer_size).end(), |
| 3359 | [](const ArtMethod& m) { return m.IsCriticalNative(); })) { |
| 3360 | // Store registered @CriticalNative methods, if any, to JNI entrypoints. |
| 3361 | // Direct methods are a contiguous chunk of memory, so use the ordering of the map. |
| 3362 | ArtMethod* first_method = klass->GetDirectMethod(0u, pointer_size); |
| 3363 | ArtMethod* last_method = klass->GetDirectMethod(num_direct_methods - 1u, pointer_size); |
| 3364 | MutexLock lock(self, critical_native_code_with_clinit_check_lock_); |
| 3365 | auto lb = critical_native_code_with_clinit_check_.lower_bound(first_method); |
| 3366 | while (lb != critical_native_code_with_clinit_check_.end() && lb->first <= last_method) { |
| 3367 | lb->first->SetEntryPointFromJni(lb->second); |
| 3368 | lb = critical_native_code_with_clinit_check_.erase(lb); |
| 3369 | } |
| 3370 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3371 | Runtime* runtime = Runtime::Current(); |
Nicolas Geoffray | 854af03 | 2021-12-21 08:32:42 +0000 | [diff] [blame] | 3372 | if (runtime->IsAotCompiler()) { |
| 3373 | // We should not update entrypoints when running the transactional |
| 3374 | // interpreter. |
| 3375 | return; |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3376 | } |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 3377 | |
Nicolas Geoffray | c8a694d | 2022-01-17 17:12:38 +0000 | [diff] [blame] | 3378 | instrumentation::Instrumentation* instrumentation = runtime->GetInstrumentation(); |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 3379 | // Link the code of methods skipped by LinkCode. |
Vladimir Marko | 86c8752 | 2020-05-11 16:55:55 +0100 | [diff] [blame] | 3380 | for (size_t method_index = 0; method_index < num_direct_methods; ++method_index) { |
| 3381 | ArtMethod* method = klass->GetDirectMethod(method_index, pointer_size); |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 3382 | if (!method->IsStatic()) { |
| 3383 | // Only update static methods. |
| 3384 | continue; |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3385 | } |
Nicolas Geoffray | 854af03 | 2021-12-21 08:32:42 +0000 | [diff] [blame] | 3386 | instrumentation->UpdateMethodsCode(method, instrumentation->GetCodeForInvoke(method)); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3387 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3388 | // Ignore virtual methods on the iterator. |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3389 | } |
| 3390 | |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3391 | // Does anything needed to make sure that the compiler will not generate a direct invoke to this |
| 3392 | // method. Should only be called on non-invokable methods. |
Nicolas Geoffray | f05f04b | 2019-10-31 11:50:41 +0000 | [diff] [blame] | 3393 | inline void EnsureThrowsInvocationError(ClassLinker* class_linker, ArtMethod* method) |
| 3394 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 3395 | DCHECK(method != nullptr); |
| 3396 | DCHECK(!method->IsInvokable()); |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3397 | method->SetEntryPointFromQuickCompiledCodePtrSize( |
| 3398 | class_linker->GetQuickToInterpreterBridgeTrampoline(), |
| 3399 | class_linker->GetImagePointerSize()); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 3400 | } |
| 3401 | |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3402 | static void LinkCode(ClassLinker* class_linker, |
| 3403 | ArtMethod* method, |
| 3404 | const OatFile::OatClass* oat_class, |
| 3405 | uint32_t class_def_method_index) REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | 2d441b1 | 2018-06-08 15:33:21 -0700 | [diff] [blame] | 3406 | ScopedAssertNoThreadSuspension sants(__FUNCTION__); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3407 | Runtime* const runtime = Runtime::Current(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 3408 | if (runtime->IsAotCompiler()) { |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 3409 | // The following code only applies to a non-compiler runtime. |
| 3410 | return; |
| 3411 | } |
Nicolas Geoffray | 5ee206f | 2019-10-08 15:09:17 +0100 | [diff] [blame] | 3412 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3413 | // Method shouldn't have already been linked. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 3414 | DCHECK(method->GetEntryPointFromQuickCompiledCode() == nullptr); |
Nicolas Geoffray | 854af03 | 2021-12-21 08:32:42 +0000 | [diff] [blame] | 3415 | DCHECK(!method->GetDeclaringClass()->IsVisiblyInitialized()); // Actually ClassStatus::Idx. |
Jeff Hao | 1674363 | 2013-05-08 10:59:04 -0700 | [diff] [blame] | 3416 | |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 3417 | if (!method->IsInvokable()) { |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3418 | EnsureThrowsInvocationError(class_linker, method); |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 3419 | return; |
| 3420 | } |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3421 | |
Nicolas Geoffray | 5ee206f | 2019-10-08 15:09:17 +0100 | [diff] [blame] | 3422 | const void* quick_code = nullptr; |
| 3423 | if (oat_class != nullptr) { |
| 3424 | // Every kind of method should at least get an invoke stub from the oat_method. |
| 3425 | // non-abstract methods also get their code pointers. |
| 3426 | const OatFile::OatMethod oat_method = oat_class->GetOatMethod(class_def_method_index); |
| 3427 | quick_code = oat_method.GetQuickCode(); |
| 3428 | } |
Nicolas Geoffray | 854af03 | 2021-12-21 08:32:42 +0000 | [diff] [blame] | 3429 | runtime->GetInstrumentation()->InitializeMethodsCode(method, quick_code); |
jeffhao | 26c0a1a | 2012-01-17 16:28:33 -0800 | [diff] [blame] | 3430 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3431 | if (method->IsNative()) { |
Vladimir Marko | 86c8752 | 2020-05-11 16:55:55 +0100 | [diff] [blame] | 3432 | // Set up the dlsym lookup stub. Do not go through `UnregisterNative()` |
| 3433 | // as the extra processing for @CriticalNative is not needed yet. |
| 3434 | method->SetEntryPointFromJni( |
| 3435 | method->IsCriticalNative() ? GetJniDlsymLookupCriticalStub() : GetJniDlsymLookupStub()); |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 3436 | } |
| 3437 | } |
| 3438 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3439 | void ClassLinker::SetupClass(const DexFile& dex_file, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 3440 | const dex::ClassDef& dex_class_def, |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3441 | Handle<mirror::Class> klass, |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3442 | ObjPtr<mirror::ClassLoader> class_loader) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3443 | CHECK(klass != nullptr); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3444 | CHECK(klass->GetDexCache() != nullptr); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 3445 | CHECK_EQ(ClassStatus::kNotReady, klass->GetStatus()); |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 3446 | const char* descriptor = dex_file.GetClassDescriptor(dex_class_def); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3447 | CHECK(descriptor != nullptr); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3448 | |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 3449 | klass->SetClass(GetClassRoot<mirror::Class>(this)); |
Andreas Gampe | 5182932 | 2014-08-25 15:05:04 -0700 | [diff] [blame] | 3450 | uint32_t access_flags = dex_class_def.GetJavaAccessFlags(); |
Brian Carlstrom | 8e3fb14 | 2013-10-09 21:00:27 -0700 | [diff] [blame] | 3451 | CHECK_EQ(access_flags & ~kAccJavaFlagsMask, 0U); |
Vladimir Marko | b68bb7a | 2020-03-17 10:55:25 +0000 | [diff] [blame] | 3452 | klass->SetAccessFlagsDuringLinking(access_flags); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3453 | klass->SetClassLoader(class_loader); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3454 | DCHECK_EQ(klass->GetPrimitiveType(), Primitive::kPrimNot); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 3455 | mirror::Class::SetStatus(klass, ClassStatus::kIdx, nullptr); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3456 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 3457 | klass->SetDexClassDefIndex(dex_file.GetIndexForClassDef(dex_class_def)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3458 | klass->SetDexTypeIndex(dex_class_def.class_idx_); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3459 | } |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3460 | |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3461 | LengthPrefixedArray<ArtField>* ClassLinker::AllocArtFieldArray(Thread* self, |
| 3462 | LinearAlloc* allocator, |
| 3463 | size_t length) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3464 | if (length == 0) { |
| 3465 | return nullptr; |
| 3466 | } |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 3467 | // If the ArtField alignment changes, review all uses of LengthPrefixedArray<ArtField>. |
| 3468 | static_assert(alignof(ArtField) == 4, "ArtField alignment is expected to be 4."); |
| 3469 | size_t storage_size = LengthPrefixedArray<ArtField>::ComputeSize(length); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3470 | void* array_storage = allocator->Alloc(self, storage_size); |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 3471 | auto* ret = new(array_storage) LengthPrefixedArray<ArtField>(length); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3472 | CHECK(ret != nullptr); |
| 3473 | std::uninitialized_fill_n(&ret->At(0), length, ArtField()); |
| 3474 | return ret; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3475 | } |
| 3476 | |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3477 | LengthPrefixedArray<ArtMethod>* ClassLinker::AllocArtMethodArray(Thread* self, |
| 3478 | LinearAlloc* allocator, |
| 3479 | size_t length) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3480 | if (length == 0) { |
| 3481 | return nullptr; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3482 | } |
Vladimir Marko | 1463285 | 2015-08-17 12:07:23 +0100 | [diff] [blame] | 3483 | const size_t method_alignment = ArtMethod::Alignment(image_pointer_size_); |
| 3484 | const size_t method_size = ArtMethod::Size(image_pointer_size_); |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 3485 | const size_t storage_size = |
| 3486 | LengthPrefixedArray<ArtMethod>::ComputeSize(length, method_size, method_alignment); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3487 | void* array_storage = allocator->Alloc(self, storage_size); |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 3488 | auto* ret = new (array_storage) LengthPrefixedArray<ArtMethod>(length); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3489 | CHECK(ret != nullptr); |
| 3490 | for (size_t i = 0; i < length; ++i) { |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 3491 | new(reinterpret_cast<void*>(&ret->At(i, method_size, method_alignment))) ArtMethod; |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3492 | } |
| 3493 | return ret; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3494 | } |
| 3495 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3496 | LinearAlloc* ClassLinker::GetAllocatorForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3497 | if (class_loader == nullptr) { |
| 3498 | return Runtime::Current()->GetLinearAlloc(); |
| 3499 | } |
| 3500 | LinearAlloc* allocator = class_loader->GetAllocator(); |
| 3501 | DCHECK(allocator != nullptr); |
| 3502 | return allocator; |
| 3503 | } |
| 3504 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3505 | LinearAlloc* ClassLinker::GetOrCreateAllocatorForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 3506 | if (class_loader == nullptr) { |
| 3507 | return Runtime::Current()->GetLinearAlloc(); |
| 3508 | } |
| 3509 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
| 3510 | LinearAlloc* allocator = class_loader->GetAllocator(); |
| 3511 | if (allocator == nullptr) { |
Mathieu Chartier | 5b83050 | 2016-03-02 10:30:23 -0800 | [diff] [blame] | 3512 | RegisterClassLoader(class_loader); |
| 3513 | allocator = class_loader->GetAllocator(); |
| 3514 | CHECK(allocator != nullptr); |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 3515 | } |
| 3516 | return allocator; |
| 3517 | } |
| 3518 | |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3519 | void ClassLinker::LoadClass(Thread* self, |
| 3520 | const DexFile& dex_file, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 3521 | const dex::ClassDef& dex_class_def, |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3522 | Handle<mirror::Class> klass) { |
David Brazdil | 20c765f | 2018-10-27 21:45:15 +0000 | [diff] [blame] | 3523 | ClassAccessor accessor(dex_file, |
| 3524 | dex_class_def, |
| 3525 | /* parse_hiddenapi_class_data= */ klass->IsBootStrapClassLoaded()); |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3526 | if (!accessor.HasClassData()) { |
| 3527 | return; |
| 3528 | } |
| 3529 | Runtime* const runtime = Runtime::Current(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3530 | { |
| 3531 | // Note: We cannot have thread suspension until the field and method arrays are setup or else |
| 3532 | // Class::VisitFieldRoots may miss some fields or methods. |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 3533 | ScopedAssertNoThreadSuspension nts(__FUNCTION__); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3534 | // Load static fields. |
Vladimir Marko | 23682bf | 2015-06-24 14:28:03 +0100 | [diff] [blame] | 3535 | // We allow duplicate definitions of the same field in a class_data_item |
| 3536 | // but ignore the repeated indexes here, b/21868015. |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3537 | LinearAlloc* const allocator = GetAllocatorForClassLoader(klass->GetClassLoader()); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3538 | LengthPrefixedArray<ArtField>* sfields = AllocArtFieldArray(self, |
| 3539 | allocator, |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3540 | accessor.NumStaticFields()); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3541 | LengthPrefixedArray<ArtField>* ifields = AllocArtFieldArray(self, |
| 3542 | allocator, |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3543 | accessor.NumInstanceFields()); |
| 3544 | size_t num_sfields = 0u; |
Vladimir Marko | 23682bf | 2015-06-24 14:28:03 +0100 | [diff] [blame] | 3545 | size_t num_ifields = 0u; |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3546 | uint32_t last_static_field_idx = 0u; |
| 3547 | uint32_t last_instance_field_idx = 0u; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 3548 | |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3549 | // Methods |
| 3550 | bool has_oat_class = false; |
| 3551 | const OatFile::OatClass oat_class = (runtime->IsStarted() && !runtime->IsAotCompiler()) |
| 3552 | ? OatFile::FindOatClass(dex_file, klass->GetDexClassDefIndex(), &has_oat_class) |
| 3553 | : OatFile::OatClass::Invalid(); |
| 3554 | const OatFile::OatClass* oat_class_ptr = has_oat_class ? &oat_class : nullptr; |
| 3555 | klass->SetMethodsPtr( |
| 3556 | AllocArtMethodArray(self, allocator, accessor.NumMethods()), |
| 3557 | accessor.NumDirectMethods(), |
| 3558 | accessor.NumVirtualMethods()); |
| 3559 | size_t class_def_method_index = 0; |
| 3560 | uint32_t last_dex_method_index = dex::kDexNoIndex; |
| 3561 | size_t last_class_def_method_index = 0; |
| 3562 | |
Nicolas Geoffray | 58f916c | 2021-11-15 14:02:07 +0000 | [diff] [blame] | 3563 | uint16_t hotness_threshold = runtime->GetJITOptions()->GetWarmupThreshold(); |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3564 | // Use the visitor since the ranged based loops are bit slower from seeking. Seeking to the |
| 3565 | // methods needs to decode all of the fields. |
| 3566 | accessor.VisitFieldsAndMethods([&]( |
| 3567 | const ClassAccessor::Field& field) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 3568 | uint32_t field_idx = field.GetIndex(); |
| 3569 | DCHECK_GE(field_idx, last_static_field_idx); // Ordering enforced by DexFileVerifier. |
| 3570 | if (num_sfields == 0 || LIKELY(field_idx > last_static_field_idx)) { |
| 3571 | LoadField(field, klass, &sfields->At(num_sfields)); |
| 3572 | ++num_sfields; |
| 3573 | last_static_field_idx = field_idx; |
| 3574 | } |
| 3575 | }, [&](const ClassAccessor::Field& field) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 3576 | uint32_t field_idx = field.GetIndex(); |
| 3577 | DCHECK_GE(field_idx, last_instance_field_idx); // Ordering enforced by DexFileVerifier. |
| 3578 | if (num_ifields == 0 || LIKELY(field_idx > last_instance_field_idx)) { |
| 3579 | LoadField(field, klass, &ifields->At(num_ifields)); |
| 3580 | ++num_ifields; |
| 3581 | last_instance_field_idx = field_idx; |
| 3582 | } |
| 3583 | }, [&](const ClassAccessor::Method& method) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 3584 | ArtMethod* art_method = klass->GetDirectMethodUnchecked(class_def_method_index, |
| 3585 | image_pointer_size_); |
| 3586 | LoadMethod(dex_file, method, klass, art_method); |
| 3587 | LinkCode(this, art_method, oat_class_ptr, class_def_method_index); |
| 3588 | uint32_t it_method_index = method.GetIndex(); |
| 3589 | if (last_dex_method_index == it_method_index) { |
| 3590 | // duplicate case |
| 3591 | art_method->SetMethodIndex(last_class_def_method_index); |
| 3592 | } else { |
| 3593 | art_method->SetMethodIndex(class_def_method_index); |
| 3594 | last_dex_method_index = it_method_index; |
| 3595 | last_class_def_method_index = class_def_method_index; |
| 3596 | } |
Nicolas Geoffray | 58f916c | 2021-11-15 14:02:07 +0000 | [diff] [blame] | 3597 | art_method->ResetCounter(hotness_threshold); |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3598 | ++class_def_method_index; |
| 3599 | }, [&](const ClassAccessor::Method& method) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 3600 | ArtMethod* art_method = klass->GetVirtualMethodUnchecked( |
| 3601 | class_def_method_index - accessor.NumDirectMethods(), |
| 3602 | image_pointer_size_); |
Nicolas Geoffray | 58f916c | 2021-11-15 14:02:07 +0000 | [diff] [blame] | 3603 | art_method->ResetCounter(hotness_threshold); |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3604 | LoadMethod(dex_file, method, klass, art_method); |
| 3605 | LinkCode(this, art_method, oat_class_ptr, class_def_method_index); |
| 3606 | ++class_def_method_index; |
| 3607 | }); |
| 3608 | |
| 3609 | if (UNLIKELY(num_ifields + num_sfields != accessor.NumFields())) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3610 | LOG(WARNING) << "Duplicate fields in class " << klass->PrettyDescriptor() |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3611 | << " (unique static fields: " << num_sfields << "/" << accessor.NumStaticFields() |
| 3612 | << ", unique instance fields: " << num_ifields << "/" << accessor.NumInstanceFields() |
| 3613 | << ")"; |
Vladimir Marko | 81819db | 2015-11-05 15:30:12 +0000 | [diff] [blame] | 3614 | // NOTE: Not shrinking the over-allocated sfields/ifields, just setting size. |
| 3615 | if (sfields != nullptr) { |
| 3616 | sfields->SetSize(num_sfields); |
| 3617 | } |
| 3618 | if (ifields != nullptr) { |
| 3619 | ifields->SetSize(num_ifields); |
| 3620 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3621 | } |
Vladimir Marko | 81819db | 2015-11-05 15:30:12 +0000 | [diff] [blame] | 3622 | // Set the field arrays. |
| 3623 | klass->SetSFieldsPtr(sfields); |
| 3624 | DCHECK_EQ(klass->NumStaticFields(), num_sfields); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3625 | klass->SetIFieldsPtr(ifields); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3626 | DCHECK_EQ(klass->NumInstanceFields(), num_ifields); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3627 | } |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 3628 | // 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] | 3629 | WriteBarrier::ForEveryFieldWrite(klass.Get()); |
Mathieu Chartier | f3f2a7a | 2015-04-14 15:43:10 -0700 | [diff] [blame] | 3630 | self->AllowThreadSuspension(); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3631 | } |
| 3632 | |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3633 | void ClassLinker::LoadField(const ClassAccessor::Field& field, |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3634 | Handle<mirror::Class> klass, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3635 | ArtField* dst) { |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3636 | const uint32_t field_idx = field.GetIndex(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3637 | dst->SetDexFieldIndex(field_idx); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3638 | dst->SetDeclaringClass(klass.Get()); |
David Brazdil | f6a8a55 | 2018-01-15 18:10:50 +0000 | [diff] [blame] | 3639 | |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 3640 | // Get access flags from the DexFile and set hiddenapi runtime access flags. |
| 3641 | dst->SetAccessFlags(field.GetAccessFlags() | hiddenapi::CreateRuntimeFlags(field)); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3642 | } |
| 3643 | |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 3644 | void ClassLinker::LoadMethod(const DexFile& dex_file, |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3645 | const ClassAccessor::Method& method, |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3646 | Handle<mirror::Class> klass, |
| 3647 | ArtMethod* dst) { |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3648 | const uint32_t dex_method_idx = method.GetIndex(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 3649 | const dex::MethodId& method_id = dex_file.GetMethodId(dex_method_idx); |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 3650 | const char* method_name = dex_file.StringDataByIdx(method_id.name_idx_); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 3651 | |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 3652 | ScopedAssertNoThreadSuspension ants("LoadMethod"); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 3653 | dst->SetDexMethodIndex(dex_method_idx); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3654 | dst->SetDeclaringClass(klass.Get()); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3655 | |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 3656 | // Get access flags from the DexFile and set hiddenapi runtime access flags. |
| 3657 | uint32_t access_flags = method.GetAccessFlags() | hiddenapi::CreateRuntimeFlags(method); |
David Brazdil | f6a8a55 | 2018-01-15 18:10:50 +0000 | [diff] [blame] | 3658 | |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 3659 | if (UNLIKELY(strcmp("finalize", method_name) == 0)) { |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3660 | // Set finalizable flag on declaring class. |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 3661 | if (strcmp("V", dex_file.GetShorty(method_id.proto_idx_)) == 0) { |
| 3662 | // Void return type. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3663 | if (klass->GetClassLoader() != nullptr) { // All non-boot finalizer methods are flagged. |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 3664 | klass->SetFinalizable(); |
| 3665 | } else { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3666 | std::string temp; |
| 3667 | const char* klass_descriptor = klass->GetDescriptor(&temp); |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 3668 | // The Enum class declares a "final" finalize() method to prevent subclasses from |
| 3669 | // introducing a finalizer. We don't want to set the finalizable flag for Enum or its |
| 3670 | // subclasses, so we exclude it here. |
| 3671 | // We also want to avoid setting the flag on Object, where we know that finalize() is |
| 3672 | // empty. |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3673 | if (strcmp(klass_descriptor, "Ljava/lang/Object;") != 0 && |
| 3674 | strcmp(klass_descriptor, "Ljava/lang/Enum;") != 0) { |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3675 | klass->SetFinalizable(); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3676 | } |
| 3677 | } |
| 3678 | } |
| 3679 | } else if (method_name[0] == '<') { |
| 3680 | // Fix broken access flags for initializers. Bug 11157540. |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 3681 | bool is_init = (strcmp("<init>", method_name) == 0); |
| 3682 | bool is_clinit = !is_init && (strcmp("<clinit>", method_name) == 0); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3683 | if (UNLIKELY(!is_init && !is_clinit)) { |
| 3684 | LOG(WARNING) << "Unexpected '<' at start of method name " << method_name; |
| 3685 | } else { |
| 3686 | if (UNLIKELY((access_flags & kAccConstructor) == 0)) { |
| 3687 | 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] | 3688 | << klass->PrettyDescriptor() << " in dex file " << dex_file.GetLocation(); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3689 | access_flags |= kAccConstructor; |
| 3690 | } |
| 3691 | } |
| 3692 | } |
Vladimir Marko | b0a6aee | 2017-10-27 10:34:04 +0100 | [diff] [blame] | 3693 | if (UNLIKELY((access_flags & kAccNative) != 0u)) { |
| 3694 | // Check if the native method is annotated with @FastNative or @CriticalNative. |
| 3695 | access_flags |= annotations::GetNativeMethodAnnotationAccessFlags( |
| 3696 | dex_file, dst->GetClassDef(), dex_method_idx); |
Kevin Jeon | 8515f7f | 2022-01-12 00:15:26 +0000 | [diff] [blame] | 3697 | } else if ((access_flags & kAccAbstract) == 0u && |
| 3698 | annotations::MethodIsNeverCompile(dex_file, dst->GetClassDef(), dex_method_idx)) { |
Kevin Jeon | 5573756 | 2021-11-19 20:39:30 +0000 | [diff] [blame] | 3699 | access_flags |= kAccCompileDontBother; |
| 3700 | } |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3701 | dst->SetAccessFlags(access_flags); |
David Srbecky | e36e7f2 | 2018-11-14 14:21:23 +0000 | [diff] [blame] | 3702 | // Must be done after SetAccessFlags since IsAbstract depends on it. |
| 3703 | if (klass->IsInterface() && dst->IsAbstract()) { |
| 3704 | dst->CalculateAndSetImtIndex(); |
| 3705 | } |
Nicolas Geoffray | 4717175 | 2020-08-31 15:03:20 +0100 | [diff] [blame] | 3706 | if (dst->HasCodeItem()) { |
| 3707 | DCHECK_NE(method.GetCodeItemOffset(), 0u); |
| 3708 | if (Runtime::Current()->IsAotCompiler()) { |
| 3709 | dst->SetDataPtrSize(reinterpret_cast32<void*>(method.GetCodeItemOffset()), image_pointer_size_); |
| 3710 | } else { |
zhaoxuyang | 7156ea2 | 2022-01-10 13:58:11 +0800 | [diff] [blame] | 3711 | dst->SetCodeItem(dst->GetDexFile()->GetCodeItem(method.GetCodeItemOffset()), |
| 3712 | dst->GetDexFile()->IsCompactDexFile()); |
Nicolas Geoffray | 4717175 | 2020-08-31 15:03:20 +0100 | [diff] [blame] | 3713 | } |
| 3714 | } else { |
| 3715 | dst->SetDataPtrSize(nullptr, image_pointer_size_); |
| 3716 | DCHECK_EQ(method.GetCodeItemOffset(), 0u); |
| 3717 | } |
Nicolas Geoffray | 43c9cd7 | 2021-03-10 15:09:19 +0000 | [diff] [blame] | 3718 | |
| 3719 | // Set optimization flags related to the shorty. |
Jared Duke | 968c813 | 2021-11-02 16:47:54 -0700 | [diff] [blame] | 3720 | uint32_t shorty_length; |
| 3721 | const char* shorty = dst->GetShorty(&shorty_length); |
Nicolas Geoffray | 43c9cd7 | 2021-03-10 15:09:19 +0000 | [diff] [blame] | 3722 | bool all_parameters_are_reference = true; |
| 3723 | bool all_parameters_are_reference_or_int = true; |
| 3724 | bool return_type_is_fp = (shorty[0] == 'F' || shorty[0] == 'D'); |
| 3725 | |
Jared Duke | 968c813 | 2021-11-02 16:47:54 -0700 | [diff] [blame] | 3726 | for (size_t i = 1; i < shorty_length; ++i) { |
Nicolas Geoffray | 43c9cd7 | 2021-03-10 15:09:19 +0000 | [diff] [blame] | 3727 | if (shorty[i] != 'L') { |
| 3728 | all_parameters_are_reference = false; |
| 3729 | if (shorty[i] == 'F' || shorty[i] == 'D' || shorty[i] == 'J') { |
| 3730 | all_parameters_are_reference_or_int = false; |
| 3731 | break; |
| 3732 | } |
| 3733 | } |
| 3734 | } |
| 3735 | |
| 3736 | if (!dst->IsNative() && all_parameters_are_reference) { |
| 3737 | dst->SetNterpEntryPointFastPathFlag(); |
| 3738 | } |
| 3739 | |
| 3740 | if (!return_type_is_fp && all_parameters_are_reference_or_int) { |
| 3741 | dst->SetNterpInvokeFastPathFlag(); |
| 3742 | } |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3743 | } |
| 3744 | |
Mathieu Chartier | 0a19e21 | 2019-11-27 14:35:24 -0800 | [diff] [blame] | 3745 | void ClassLinker::AppendToBootClassPath(Thread* self, const DexFile* dex_file) { |
David Srbecky | 33df0e3 | 2021-09-30 14:36:32 +0000 | [diff] [blame] | 3746 | ObjPtr<mirror::DexCache> dex_cache = |
| 3747 | AllocAndInitializeDexCache(self, *dex_file, /* class_loader= */ nullptr); |
Mathieu Chartier | 0a19e21 | 2019-11-27 14:35:24 -0800 | [diff] [blame] | 3748 | 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] | 3749 | AppendToBootClassPath(dex_file, dex_cache); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 3750 | } |
| 3751 | |
Mathieu Chartier | 0a19e21 | 2019-11-27 14:35:24 -0800 | [diff] [blame] | 3752 | void ClassLinker::AppendToBootClassPath(const DexFile* dex_file, |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3753 | ObjPtr<mirror::DexCache> dex_cache) { |
Mathieu Chartier | 0a19e21 | 2019-11-27 14:35:24 -0800 | [diff] [blame] | 3754 | CHECK(dex_file != nullptr); |
| 3755 | CHECK(dex_cache != nullptr) << dex_file->GetLocation(); |
| 3756 | boot_class_path_.push_back(dex_file); |
Andreas Gampe | be7af22 | 2017-07-25 09:57:28 -0700 | [diff] [blame] | 3757 | WriterMutexLock mu(Thread::Current(), *Locks::dex_lock_); |
Mathieu Chartier | 0a19e21 | 2019-11-27 14:35:24 -0800 | [diff] [blame] | 3758 | RegisterDexFileLocked(*dex_file, dex_cache, /* class_loader= */ nullptr); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 3759 | } |
| 3760 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 3761 | void ClassLinker::RegisterDexFileLocked(const DexFile& dex_file, |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3762 | ObjPtr<mirror::DexCache> dex_cache, |
| 3763 | ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3764 | Thread* const self = Thread::Current(); |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 3765 | Locks::dex_lock_->AssertExclusiveHeld(self); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3766 | CHECK(dex_cache != nullptr) << dex_file.GetLocation(); |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 3767 | CHECK_EQ(dex_cache->GetDexFile(), &dex_file) << dex_file.GetLocation(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 3768 | // For app images, the dex cache location may be a suffix of the dex file location since the |
| 3769 | // dex file location is an absolute path. |
Mathieu Chartier | 7617216 | 2016-01-26 14:54:06 -0800 | [diff] [blame] | 3770 | const std::string dex_cache_location = dex_cache->GetLocation()->ToModifiedUtf8(); |
| 3771 | const size_t dex_cache_length = dex_cache_location.length(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 3772 | CHECK_GT(dex_cache_length, 0u) << dex_file.GetLocation(); |
| 3773 | std::string dex_file_location = dex_file.GetLocation(); |
Nicolas Geoffray | e3e0f70 | 2019-03-12 07:02:02 +0000 | [diff] [blame] | 3774 | // The following paths checks don't work on preopt when using boot dex files, where the dex |
| 3775 | // cache location is the one on device, and the dex_file's location is the one on host. |
| 3776 | if (!(Runtime::Current()->IsAotCompiler() && class_loader == nullptr && !kIsTargetBuild)) { |
| 3777 | CHECK_GE(dex_file_location.length(), dex_cache_length) |
| 3778 | << dex_cache_location << " " << dex_file.GetLocation(); |
| 3779 | const std::string dex_file_suffix = dex_file_location.substr( |
| 3780 | dex_file_location.length() - dex_cache_length, |
| 3781 | dex_cache_length); |
| 3782 | // Example dex_cache location is SettingsProvider.apk and |
| 3783 | // dex file location is /system/priv-app/SettingsProvider/SettingsProvider.apk |
| 3784 | CHECK_EQ(dex_cache_location, dex_file_suffix); |
| 3785 | } |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 3786 | const OatFile* oat_file = |
| 3787 | (dex_file.GetOatDexFile() != nullptr) ? dex_file.GetOatDexFile()->GetOatFile() : nullptr; |
Vladimir Marko | b066d43 | 2018-01-03 13:14:37 +0000 | [diff] [blame] | 3788 | // Clean up pass to remove null dex caches; null dex caches can occur due to class unloading |
| 3789 | // and we are lazily removing null entries. Also check if we need to initialize OatFile data |
| 3790 | // (.data.bimg.rel.ro and .bss sections) needed for code execution. |
| 3791 | bool initialize_oat_file_data = (oat_file != nullptr) && oat_file->IsExecutable(); |
Ian Rogers | 55256cb | 2017-12-21 17:07:11 -0800 | [diff] [blame] | 3792 | JavaVMExt* const vm = self->GetJniEnv()->GetVm(); |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 3793 | for (auto it = dex_caches_.begin(); it != dex_caches_.end(); ) { |
David Srbecky | 6fbcc29 | 2021-02-23 01:05:32 +0000 | [diff] [blame] | 3794 | const DexCacheData& data = it->second; |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 3795 | if (self->IsJWeakCleared(data.weak_root)) { |
| 3796 | vm->DeleteWeakGlobalRef(self, data.weak_root); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3797 | it = dex_caches_.erase(it); |
| 3798 | } else { |
Vladimir Marko | b066d43 | 2018-01-03 13:14:37 +0000 | [diff] [blame] | 3799 | if (initialize_oat_file_data && |
David Srbecky | 6fbcc29 | 2021-02-23 01:05:32 +0000 | [diff] [blame] | 3800 | it->first->GetOatDexFile() != nullptr && |
| 3801 | it->first->GetOatDexFile()->GetOatFile() == oat_file) { |
Vladimir Marko | b066d43 | 2018-01-03 13:14:37 +0000 | [diff] [blame] | 3802 | initialize_oat_file_data = false; // Already initialized. |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 3803 | } |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3804 | ++it; |
| 3805 | } |
Brian Carlstrom | 81a9087 | 2015-08-28 09:07:14 -0700 | [diff] [blame] | 3806 | } |
Vladimir Marko | b066d43 | 2018-01-03 13:14:37 +0000 | [diff] [blame] | 3807 | if (initialize_oat_file_data) { |
Vladimir Marko | 1cedb4a | 2019-02-06 14:13:28 +0000 | [diff] [blame] | 3808 | oat_file->InitializeRelocations(); |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 3809 | } |
David Brazdil | a5c3a80 | 2019-03-08 14:59:41 +0000 | [diff] [blame] | 3810 | // Let hiddenapi assign a domain to the newly registered dex file. |
| 3811 | hiddenapi::InitializeDexFileDomain(dex_file, class_loader); |
| 3812 | |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3813 | jweak dex_cache_jweak = vm->AddWeakGlobalRef(self, dex_cache); |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 3814 | DexCacheData data; |
| 3815 | data.weak_root = dex_cache_jweak; |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3816 | data.class_table = ClassTableForClassLoader(class_loader); |
David Srbecky | 6fbcc29 | 2021-02-23 01:05:32 +0000 | [diff] [blame] | 3817 | AddNativeDebugInfoForDex(self, &dex_file); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3818 | DCHECK(data.class_table != nullptr); |
Mathieu Chartier | 72041a0 | 2017-07-14 18:23:25 -0700 | [diff] [blame] | 3819 | // Make sure to hold the dex cache live in the class table. This case happens for the boot class |
| 3820 | // path dex caches without an image. |
| 3821 | data.class_table->InsertStrongRoot(dex_cache); |
Andreas Gampe | 8a1a0f7 | 2020-03-03 16:07:45 -0800 | [diff] [blame] | 3822 | // Make sure that the dex cache holds the classloader live. |
| 3823 | dex_cache->SetClassLoader(class_loader); |
Mathieu Chartier | 72041a0 | 2017-07-14 18:23:25 -0700 | [diff] [blame] | 3824 | if (class_loader != nullptr) { |
| 3825 | // Since we added a strong root to the class table, do the write barrier as required for |
| 3826 | // remembered sets and generational GCs. |
Mathieu Chartier | 88ea61e | 2018-06-20 17:45:41 -0700 | [diff] [blame] | 3827 | WriteBarrier::ForEveryFieldWrite(class_loader); |
Mathieu Chartier | 72041a0 | 2017-07-14 18:23:25 -0700 | [diff] [blame] | 3828 | } |
David Srbecky | 6fbcc29 | 2021-02-23 01:05:32 +0000 | [diff] [blame] | 3829 | bool inserted = dex_caches_.emplace(&dex_file, std::move(data)).second; |
| 3830 | CHECK(inserted); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 3831 | } |
| 3832 | |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 3833 | ObjPtr<mirror::DexCache> ClassLinker::DecodeDexCacheLocked(Thread* self, const DexCacheData* data) { |
| 3834 | return data != nullptr |
| 3835 | ? ObjPtr<mirror::DexCache>::DownCast(self->DecodeJObject(data->weak_root)) |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3836 | : nullptr; |
| 3837 | } |
| 3838 | |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 3839 | bool ClassLinker::IsSameClassLoader( |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3840 | ObjPtr<mirror::DexCache> dex_cache, |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 3841 | const DexCacheData* data, |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3842 | ObjPtr<mirror::ClassLoader> class_loader) { |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 3843 | CHECK(data != nullptr); |
David Srbecky | 6fbcc29 | 2021-02-23 01:05:32 +0000 | [diff] [blame] | 3844 | DCHECK_EQ(FindDexCacheDataLocked(*dex_cache->GetDexFile()), data); |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 3845 | return data->class_table == ClassTableForClassLoader(class_loader); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3846 | } |
| 3847 | |
Alex Light | 07f0621 | 2017-06-01 14:01:43 -0700 | [diff] [blame] | 3848 | void ClassLinker::RegisterExistingDexCache(ObjPtr<mirror::DexCache> dex_cache, |
| 3849 | ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | ed4ee44 | 2018-06-05 14:23:35 -0700 | [diff] [blame] | 3850 | SCOPED_TRACE << __FUNCTION__ << " " << dex_cache->GetDexFile()->GetLocation(); |
Alex Light | 07f0621 | 2017-06-01 14:01:43 -0700 | [diff] [blame] | 3851 | Thread* self = Thread::Current(); |
| 3852 | StackHandleScope<2> hs(self); |
| 3853 | Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(dex_cache)); |
| 3854 | Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader)); |
| 3855 | const DexFile* dex_file = dex_cache->GetDexFile(); |
| 3856 | DCHECK(dex_file != nullptr) << "Attempt to register uninitialized dex_cache object!"; |
| 3857 | if (kIsDebugBuild) { |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 3858 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
| 3859 | const DexCacheData* old_data = FindDexCacheDataLocked(*dex_file); |
| 3860 | ObjPtr<mirror::DexCache> old_dex_cache = DecodeDexCacheLocked(self, old_data); |
Alex Light | 07f0621 | 2017-06-01 14:01:43 -0700 | [diff] [blame] | 3861 | DCHECK(old_dex_cache.IsNull()) << "Attempt to manually register a dex cache thats already " |
| 3862 | << "been registered on dex file " << dex_file->GetLocation(); |
| 3863 | } |
| 3864 | ClassTable* table; |
| 3865 | { |
| 3866 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 3867 | table = InsertClassTableForClassLoader(h_class_loader.Get()); |
| 3868 | } |
Ivan Maidanski | 2b69b9c | 2018-05-14 13:50:48 +0300 | [diff] [blame] | 3869 | // Avoid a deadlock between a garbage collecting thread running a checkpoint, |
| 3870 | // a thread holding the dex lock and blocking on a condition variable regarding |
| 3871 | // weak references access, and a thread blocking on the dex lock. |
Ivan Maidanski | 2b69b9c | 2018-05-14 13:50:48 +0300 | [diff] [blame] | 3872 | gc::ScopedGCCriticalSection gcs(self, gc::kGcCauseClassLinker, gc::kCollectorTypeClassLinker); |
Alex Light | 07f0621 | 2017-06-01 14:01:43 -0700 | [diff] [blame] | 3873 | WriterMutexLock mu(self, *Locks::dex_lock_); |
| 3874 | RegisterDexFileLocked(*dex_file, h_dex_cache.Get(), h_class_loader.Get()); |
| 3875 | table->InsertStrongRoot(h_dex_cache.Get()); |
| 3876 | if (h_class_loader.Get() != nullptr) { |
| 3877 | // Since we added a strong root to the class table, do the write barrier as required for |
| 3878 | // remembered sets and generational GCs. |
Mathieu Chartier | 88ea61e | 2018-06-20 17:45:41 -0700 | [diff] [blame] | 3879 | WriteBarrier::ForEveryFieldWrite(h_class_loader.Get()); |
Alex Light | 07f0621 | 2017-06-01 14:01:43 -0700 | [diff] [blame] | 3880 | } |
| 3881 | } |
| 3882 | |
Alex Light | de7f878 | 2020-02-24 10:14:22 -0800 | [diff] [blame] | 3883 | static void ThrowDexFileAlreadyRegisteredError(Thread* self, const DexFile& dex_file) |
| 3884 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 3885 | self->ThrowNewExceptionF("Ljava/lang/InternalError;", |
Alex Light | de7f878 | 2020-02-24 10:14:22 -0800 | [diff] [blame] | 3886 | "Attempt to register dex file %s with multiple class loaders", |
| 3887 | dex_file.GetLocation().c_str()); |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 3888 | } |
| 3889 | |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3890 | ObjPtr<mirror::DexCache> ClassLinker::RegisterDexFile(const DexFile& dex_file, |
| 3891 | ObjPtr<mirror::ClassLoader> class_loader) { |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 3892 | Thread* self = Thread::Current(); |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 3893 | ObjPtr<mirror::DexCache> old_dex_cache; |
| 3894 | bool registered_with_another_class_loader = false; |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 3895 | { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 3896 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 3897 | const DexCacheData* old_data = FindDexCacheDataLocked(dex_file); |
| 3898 | old_dex_cache = DecodeDexCacheLocked(self, old_data); |
| 3899 | if (old_dex_cache != nullptr) { |
| 3900 | if (IsSameClassLoader(old_dex_cache, old_data, class_loader)) { |
| 3901 | return old_dex_cache; |
| 3902 | } else { |
| 3903 | // TODO This is not very clean looking. Should maybe try to make a way to request exceptions |
| 3904 | // be thrown when it's safe to do so to simplify this. |
| 3905 | registered_with_another_class_loader = true; |
| 3906 | } |
| 3907 | } |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3908 | } |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 3909 | // We need to have released the dex_lock_ to allocate safely. |
| 3910 | if (registered_with_another_class_loader) { |
| 3911 | ThrowDexFileAlreadyRegisteredError(self, dex_file); |
| 3912 | return nullptr; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3913 | } |
Mathieu Chartier | ed4ee44 | 2018-06-05 14:23:35 -0700 | [diff] [blame] | 3914 | SCOPED_TRACE << __FUNCTION__ << " " << dex_file.GetLocation(); |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 3915 | LinearAlloc* const linear_alloc = GetOrCreateAllocatorForClassLoader(class_loader); |
| 3916 | DCHECK(linear_alloc != nullptr); |
| 3917 | ClassTable* table; |
| 3918 | { |
| 3919 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 3920 | table = InsertClassTableForClassLoader(class_loader); |
| 3921 | } |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 3922 | // Don't alloc while holding the lock, since allocation may need to |
| 3923 | // suspend all threads and another thread may need the dex_lock_ to |
| 3924 | // get to a suspend point. |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3925 | StackHandleScope<3> hs(self); |
| 3926 | Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader)); |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 3927 | Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(AllocDexCache(self, dex_file))); |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 3928 | { |
Ivan Maidanski | 2b69b9c | 2018-05-14 13:50:48 +0300 | [diff] [blame] | 3929 | // Avoid a deadlock between a garbage collecting thread running a checkpoint, |
| 3930 | // a thread holding the dex lock and blocking on a condition variable regarding |
| 3931 | // weak references access, and a thread blocking on the dex lock. |
Ivan Maidanski | 2b69b9c | 2018-05-14 13:50:48 +0300 | [diff] [blame] | 3932 | gc::ScopedGCCriticalSection gcs(self, gc::kGcCauseClassLinker, gc::kCollectorTypeClassLinker); |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 3933 | WriterMutexLock 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); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3936 | if (old_dex_cache == nullptr && h_dex_cache != nullptr) { |
David Srbecky | 33df0e3 | 2021-09-30 14:36:32 +0000 | [diff] [blame] | 3937 | // 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] | 3938 | // at the same time with the same dex cache. Since the .bss is shared this can cause failing |
| 3939 | // DCHECK that the arrays are null. |
David Srbecky | 33df0e3 | 2021-09-30 14:36:32 +0000 | [diff] [blame] | 3940 | h_dex_cache->Initialize(&dex_file, h_class_loader.Get()); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3941 | RegisterDexFileLocked(dex_file, h_dex_cache.Get(), h_class_loader.Get()); |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 3942 | } |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 3943 | if (old_dex_cache != nullptr) { |
| 3944 | // Another thread managed to initialize the dex cache faster, so use that DexCache. |
| 3945 | // If this thread encountered OOME, ignore it. |
| 3946 | DCHECK_EQ(h_dex_cache == nullptr, self->IsExceptionPending()); |
| 3947 | self->ClearException(); |
| 3948 | // We cannot call EnsureSameClassLoader() or allocate an exception while holding the |
| 3949 | // dex_lock_. |
| 3950 | if (IsSameClassLoader(old_dex_cache, old_data, h_class_loader.Get())) { |
| 3951 | return old_dex_cache; |
| 3952 | } else { |
| 3953 | registered_with_another_class_loader = true; |
| 3954 | } |
| 3955 | } |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3956 | } |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 3957 | if (registered_with_another_class_loader) { |
| 3958 | ThrowDexFileAlreadyRegisteredError(self, dex_file); |
| 3959 | return nullptr; |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3960 | } |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3961 | if (h_dex_cache == nullptr) { |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3962 | self->AssertPendingOOMException(); |
| 3963 | return nullptr; |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 3964 | } |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 3965 | table->InsertStrongRoot(h_dex_cache.Get()); |
Mathieu Chartier | a1467d0 | 2017-02-22 09:22:50 -0800 | [diff] [blame] | 3966 | if (h_class_loader.Get() != nullptr) { |
| 3967 | // Since we added a strong root to the class table, do the write barrier as required for |
| 3968 | // remembered sets and generational GCs. |
Mathieu Chartier | 88ea61e | 2018-06-20 17:45:41 -0700 | [diff] [blame] | 3969 | WriteBarrier::ForEveryFieldWrite(h_class_loader.Get()); |
Mathieu Chartier | a1467d0 | 2017-02-22 09:22:50 -0800 | [diff] [blame] | 3970 | } |
Nicolas Geoffray | 1d4f009 | 2020-08-07 14:01:05 +0100 | [diff] [blame] | 3971 | VLOG(class_linker) << "Registered dex file " << dex_file.GetLocation(); |
Nicolas Geoffray | 4f6bb44 | 2021-06-02 18:05:51 +0100 | [diff] [blame] | 3972 | PaletteNotifyDexFileLoaded(dex_file.GetLocation().c_str()); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3973 | return h_dex_cache.Get(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3974 | } |
| 3975 | |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3976 | bool ClassLinker::IsDexFileRegistered(Thread* self, const DexFile& dex_file) { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 3977 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 3978 | return DecodeDexCacheLocked(self, FindDexCacheDataLocked(dex_file)) != nullptr; |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3979 | } |
| 3980 | |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3981 | ObjPtr<mirror::DexCache> ClassLinker::FindDexCache(Thread* self, const DexFile& dex_file) { |
| 3982 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 3983 | const DexCacheData* dex_cache_data = FindDexCacheDataLocked(dex_file); |
| 3984 | ObjPtr<mirror::DexCache> dex_cache = DecodeDexCacheLocked(self, dex_cache_data); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3985 | if (dex_cache != nullptr) { |
| 3986 | return dex_cache; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 3987 | } |
Brian Carlstrom | 81a9087 | 2015-08-28 09:07:14 -0700 | [diff] [blame] | 3988 | // Failure, dump diagnostic and abort. |
David Srbecky | 6fbcc29 | 2021-02-23 01:05:32 +0000 | [diff] [blame] | 3989 | for (const auto& entry : dex_caches_) { |
| 3990 | const DexCacheData& data = entry.second; |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 3991 | if (DecodeDexCacheLocked(self, &data) != nullptr) { |
David Srbecky | 6fbcc29 | 2021-02-23 01:05:32 +0000 | [diff] [blame] | 3992 | LOG(FATAL_WITHOUT_ABORT) << "Registered dex file " << entry.first->GetLocation(); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3993 | } |
Brian Carlstrom | 81a9087 | 2015-08-28 09:07:14 -0700 | [diff] [blame] | 3994 | } |
Mathieu Chartier | 9e050df | 2017-08-09 10:05:47 -0700 | [diff] [blame] | 3995 | LOG(FATAL) << "Failed to find DexCache for DexFile " << dex_file.GetLocation() |
David Srbecky | 6fbcc29 | 2021-02-23 01:05:32 +0000 | [diff] [blame] | 3996 | << " " << &dex_file; |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 3997 | UNREACHABLE(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3998 | } |
| 3999 | |
Santiago Aboy Solanes | 4b8ea5d | 2021-11-19 10:14:54 +0000 | [diff] [blame] | 4000 | 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] | 4001 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
| 4002 | const DexCacheData* dex_cache_data = FindDexCacheDataLocked(oat_dex_file); |
| 4003 | ObjPtr<mirror::DexCache> dex_cache = DecodeDexCacheLocked(self, dex_cache_data); |
| 4004 | if (dex_cache != nullptr) { |
| 4005 | return dex_cache; |
| 4006 | } |
| 4007 | // Failure, dump diagnostic and abort. |
| 4008 | for (const auto& entry : dex_caches_) { |
| 4009 | const DexCacheData& data = entry.second; |
| 4010 | if (DecodeDexCacheLocked(self, &data) != nullptr) { |
| 4011 | LOG(FATAL_WITHOUT_ABORT) << "Registered dex file " << entry.first->GetLocation(); |
| 4012 | } |
| 4013 | } |
Santiago Aboy Solanes | 4b8ea5d | 2021-11-19 10:14:54 +0000 | [diff] [blame] | 4014 | 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] | 4015 | << " " << &oat_dex_file; |
| 4016 | UNREACHABLE(); |
| 4017 | } |
| 4018 | |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4019 | ClassTable* ClassLinker::FindClassTable(Thread* self, ObjPtr<mirror::DexCache> dex_cache) { |
| 4020 | const DexFile* dex_file = dex_cache->GetDexFile(); |
| 4021 | DCHECK(dex_file != nullptr); |
| 4022 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
David Srbecky | 6fbcc29 | 2021-02-23 01:05:32 +0000 | [diff] [blame] | 4023 | auto it = dex_caches_.find(dex_file); |
| 4024 | if (it != dex_caches_.end()) { |
| 4025 | const DexCacheData& data = it->second; |
| 4026 | ObjPtr<mirror::DexCache> registered_dex_cache = DecodeDexCacheLocked(self, &data); |
| 4027 | if (registered_dex_cache != nullptr) { |
| 4028 | CHECK_EQ(registered_dex_cache, dex_cache) << dex_file->GetLocation(); |
| 4029 | return data.class_table; |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4030 | } |
| 4031 | } |
| 4032 | return nullptr; |
| 4033 | } |
| 4034 | |
Santiago Aboy Solanes | 970ba21 | 2021-10-21 10:52:47 +0100 | [diff] [blame] | 4035 | const ClassLinker::DexCacheData* ClassLinker::FindDexCacheDataLocked( |
Santiago Aboy Solanes | 4b8ea5d | 2021-11-19 10:14:54 +0000 | [diff] [blame] | 4036 | const OatDexFile& oat_dex_file) { |
| 4037 | auto it = std::find_if(dex_caches_.begin(), dex_caches_.end(), [&](const auto& entry) { |
| 4038 | return entry.first->GetOatDexFile() == &oat_dex_file; |
Santiago Aboy Solanes | 970ba21 | 2021-10-21 10:52:47 +0100 | [diff] [blame] | 4039 | }); |
| 4040 | return it != dex_caches_.end() ? &it->second : nullptr; |
| 4041 | } |
| 4042 | |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 4043 | const ClassLinker::DexCacheData* ClassLinker::FindDexCacheDataLocked(const DexFile& dex_file) { |
David Srbecky | 6fbcc29 | 2021-02-23 01:05:32 +0000 | [diff] [blame] | 4044 | auto it = dex_caches_.find(&dex_file); |
| 4045 | return it != dex_caches_.end() ? &it->second : nullptr; |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4046 | } |
| 4047 | |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 4048 | void ClassLinker::CreatePrimitiveClass(Thread* self, |
| 4049 | Primitive::Type type, |
| 4050 | ClassRoot primitive_root) { |
Vladimir Marko | acb906d | 2018-05-30 10:23:49 +0100 | [diff] [blame] | 4051 | ObjPtr<mirror::Class> primitive_class = |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 4052 | AllocClass(self, mirror::Class::PrimitiveClassSize(image_pointer_size_)); |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 4053 | CHECK(primitive_class != nullptr) << "OOM for primitive class " << type; |
| 4054 | // Do not hold lock on the primitive class object, the initialization of |
| 4055 | // primitive classes is done while the process is still single threaded. |
Nicolas Geoffray | 66934ef | 2021-07-07 14:56:23 +0100 | [diff] [blame] | 4056 | primitive_class->SetAccessFlagsDuringLinking(kAccPublic | kAccFinal | kAccAbstract); |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 4057 | primitive_class->SetPrimitiveType(type); |
| 4058 | primitive_class->SetIfTable(GetClassRoot<mirror::Object>(this)->GetIfTable()); |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 4059 | DCHECK_EQ(primitive_class->NumMethods(), 0u); |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 4060 | // Primitive classes are initialized during single threaded startup, so visibly initialized. |
| 4061 | primitive_class->SetStatusForPrimitiveOrArray(ClassStatus::kVisiblyInitialized); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4062 | const char* descriptor = Primitive::Descriptor(type); |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 4063 | ObjPtr<mirror::Class> existing = InsertClass(descriptor, |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 4064 | primitive_class, |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 4065 | ComputeModifiedUtf8Hash(descriptor)); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4066 | CHECK(existing == nullptr) << "InitPrimitiveClass(" << type << ") failed"; |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 4067 | SetClassRoot(primitive_root, primitive_class); |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 4068 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4069 | |
Vladimir Marko | 0261055 | 2018-06-04 14:38:00 +0100 | [diff] [blame] | 4070 | inline ObjPtr<mirror::IfTable> ClassLinker::GetArrayIfTable() { |
| 4071 | return GetClassRoot<mirror::ObjectArray<mirror::Object>>(this)->GetIfTable(); |
| 4072 | } |
| 4073 | |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 4074 | // Create an array class (i.e. the class object for the array, not the |
| 4075 | // array itself). "descriptor" looks like "[C" or "[[[[B" or |
| 4076 | // "[Ljava/lang/String;". |
| 4077 | // |
| 4078 | // If "descriptor" refers to an array of primitives, look up the |
| 4079 | // primitive type's internally-generated class object. |
| 4080 | // |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 4081 | // "class_loader" is the class loader of the class that's referring to |
| 4082 | // us. It's used to ensure that we're looking for the element type in |
| 4083 | // the right context. It does NOT become the class loader for the |
| 4084 | // array class; that always comes from the base element class. |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 4085 | // |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 4086 | // Returns null with an exception raised on failure. |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 4087 | ObjPtr<mirror::Class> ClassLinker::CreateArrayClass(Thread* self, |
| 4088 | const char* descriptor, |
| 4089 | size_t hash, |
| 4090 | Handle<mirror::ClassLoader> class_loader) { |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 4091 | // Identify the underlying component type |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4092 | CHECK_EQ('[', descriptor[0]); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4093 | StackHandleScope<2> hs(self); |
Alex Light | e9f6103 | 2018-09-24 16:04:51 -0700 | [diff] [blame] | 4094 | |
| 4095 | // This is to prevent the calls to ClassLoad and ClassPrepare which can cause java/user-supplied |
| 4096 | // code to be executed. We put it up here so we can avoid all the allocations associated with |
| 4097 | // creating the class. This can happen with (eg) jit threads. |
| 4098 | if (!self->CanLoadClasses()) { |
| 4099 | // Make sure we don't try to load anything, potentially causing an infinite loop. |
| 4100 | ObjPtr<mirror::Throwable> pre_allocated = |
| 4101 | Runtime::Current()->GetPreAllocatedNoClassDefFoundError(); |
| 4102 | self->SetException(pre_allocated); |
| 4103 | return nullptr; |
| 4104 | } |
| 4105 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4106 | MutableHandle<mirror::Class> component_type(hs.NewHandle(FindClass(self, descriptor + 1, |
| 4107 | class_loader))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4108 | if (component_type == nullptr) { |
Mathieu Chartier | c0a9ea4 | 2014-02-03 16:36:49 -0800 | [diff] [blame] | 4109 | DCHECK(self->IsExceptionPending()); |
Andreas Gampe | dc13d7d | 2014-07-23 20:18:36 -0700 | [diff] [blame] | 4110 | // We need to accept erroneous classes as component types. |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 4111 | const size_t component_hash = ComputeModifiedUtf8Hash(descriptor + 1); |
| 4112 | component_type.Assign(LookupClass(self, descriptor + 1, component_hash, class_loader.Get())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4113 | if (component_type == nullptr) { |
Andreas Gampe | dc13d7d | 2014-07-23 20:18:36 -0700 | [diff] [blame] | 4114 | DCHECK(self->IsExceptionPending()); |
| 4115 | return nullptr; |
| 4116 | } else { |
| 4117 | self->ClearException(); |
| 4118 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 4119 | } |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 4120 | if (UNLIKELY(component_type->IsPrimitiveVoid())) { |
| 4121 | ThrowNoClassDefFoundError("Attempt to create array of void primitive type"); |
| 4122 | return nullptr; |
| 4123 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 4124 | // See if the component type is already loaded. Array classes are |
| 4125 | // always associated with the class loader of their underlying |
| 4126 | // element type -- an array of Strings goes with the loader for |
| 4127 | // java/lang/String -- so we need to look for it there. (The |
| 4128 | // caller should have checked for the existence of the class |
| 4129 | // before calling here, but they did so with *their* class loader, |
| 4130 | // not the component type's loader.) |
| 4131 | // |
| 4132 | // If we find it, the caller adds "loader" to the class' initiating |
| 4133 | // loader list, which should prevent us from going through this again. |
| 4134 | // |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4135 | // This call is unnecessary if "loader" and "component_type->GetClassLoader()" |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 4136 | // are the same, because our caller (FindClass) just did the |
| 4137 | // lookup. (Even if we get this wrong we still have correct behavior, |
| 4138 | // because we effectively do this lookup again when we add the new |
| 4139 | // class to the hash table --- necessary because of possible races with |
| 4140 | // other threads.) |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4141 | if (class_loader.Get() != component_type->GetClassLoader()) { |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 4142 | ObjPtr<mirror::Class> new_class = |
| 4143 | LookupClass(self, descriptor, hash, component_type->GetClassLoader()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4144 | if (new_class != nullptr) { |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 4145 | return new_class; |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 4146 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 4147 | } |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 4148 | // Core array classes, i.e. Object[], Class[], String[] and primitive |
| 4149 | // arrays, have special initialization and they should be found above. |
| 4150 | DCHECK(!component_type->IsObjectClass() || |
| 4151 | // Guard from false positives for errors before setting superclass. |
| 4152 | component_type->IsErroneousUnresolved()); |
| 4153 | DCHECK(!component_type->IsStringClass()); |
| 4154 | DCHECK(!component_type->IsClassClass()); |
| 4155 | DCHECK(!component_type->IsPrimitive()); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 4156 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 4157 | // Fill out the fields in the Class. |
| 4158 | // |
| 4159 | // It is possible to execute some methods against arrays, because |
| 4160 | // all arrays are subclasses of java_lang_Object_, so we need to set |
| 4161 | // up a vtable. We can just point at the one in java_lang_Object_. |
| 4162 | // |
| 4163 | // Array classes are simple enough that we don't need to do a full |
| 4164 | // link step. |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 4165 | size_t array_class_size = mirror::Array::ClassSize(image_pointer_size_); |
| 4166 | auto visitor = [this, array_class_size, component_type](ObjPtr<mirror::Object> obj, |
| 4167 | size_t usable_size) |
| 4168 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | b68bb7a | 2020-03-17 10:55:25 +0000 | [diff] [blame] | 4169 | ScopedAssertNoNewTransactionRecords sanntr("CreateArrayClass"); |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 4170 | mirror::Class::InitializeClassVisitor init_class(array_class_size); |
| 4171 | init_class(obj, usable_size); |
| 4172 | ObjPtr<mirror::Class> klass = ObjPtr<mirror::Class>::DownCast(obj); |
| 4173 | klass->SetComponentType(component_type.Get()); |
| 4174 | // Do not hold lock for initialization, the fence issued after the visitor |
| 4175 | // returns ensures memory visibility together with the implicit consume |
| 4176 | // semantics (for all supported architectures) for any thread that loads |
| 4177 | // the array class reference from any memory locations afterwards. |
| 4178 | FinishArrayClassSetup(klass); |
| 4179 | }; |
| 4180 | auto new_class = hs.NewHandle<mirror::Class>( |
| 4181 | AllocClass(self, GetClassRoot<mirror::Class>(this), array_class_size, visitor)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4182 | if (new_class == nullptr) { |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 4183 | self->AssertPendingOOMException(); |
| 4184 | return nullptr; |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 4185 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 4186 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4187 | ObjPtr<mirror::Class> existing = InsertClass(descriptor, new_class.Get(), hash); |
Mathieu Chartier | c0a9ea4 | 2014-02-03 16:36:49 -0800 | [diff] [blame] | 4188 | if (existing == nullptr) { |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4189 | // We postpone ClassLoad and ClassPrepare events to this point in time to avoid |
| 4190 | // duplicate events in case of races. Array classes don't really follow dedicated |
| 4191 | // load and prepare, anyways. |
| 4192 | Runtime::Current()->GetRuntimeCallbacks()->ClassLoad(new_class); |
| 4193 | Runtime::Current()->GetRuntimeCallbacks()->ClassPrepare(new_class, new_class); |
| 4194 | |
Tamas Berghammer | 160e6df | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 4195 | jit::Jit::NewTypeLoadedIfUsingJit(new_class.Get()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4196 | return new_class.Get(); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 4197 | } |
| 4198 | // Another thread must have loaded the class after we |
| 4199 | // started but before we finished. Abandon what we've |
| 4200 | // done. |
| 4201 | // |
| 4202 | // (Yes, this happens.) |
| 4203 | |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 4204 | return existing; |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 4205 | } |
| 4206 | |
Vladimir Marko | 9186b18 | 2018-11-06 14:55:54 +0000 | [diff] [blame] | 4207 | ObjPtr<mirror::Class> ClassLinker::LookupPrimitiveClass(char type) { |
| 4208 | ClassRoot class_root; |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 4209 | switch (type) { |
Vladimir Marko | 9186b18 | 2018-11-06 14:55:54 +0000 | [diff] [blame] | 4210 | case 'B': class_root = ClassRoot::kPrimitiveByte; break; |
| 4211 | case 'C': class_root = ClassRoot::kPrimitiveChar; break; |
| 4212 | case 'D': class_root = ClassRoot::kPrimitiveDouble; break; |
| 4213 | case 'F': class_root = ClassRoot::kPrimitiveFloat; break; |
| 4214 | case 'I': class_root = ClassRoot::kPrimitiveInt; break; |
| 4215 | case 'J': class_root = ClassRoot::kPrimitiveLong; break; |
| 4216 | case 'S': class_root = ClassRoot::kPrimitiveShort; break; |
| 4217 | case 'Z': class_root = ClassRoot::kPrimitiveBoolean; break; |
| 4218 | case 'V': class_root = ClassRoot::kPrimitiveVoid; break; |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 4219 | default: |
Vladimir Marko | 9186b18 | 2018-11-06 14:55:54 +0000 | [diff] [blame] | 4220 | return nullptr; |
Carl Shapiro | 744ad05 | 2011-08-06 15:53:36 -0700 | [diff] [blame] | 4221 | } |
Vladimir Marko | 9186b18 | 2018-11-06 14:55:54 +0000 | [diff] [blame] | 4222 | return GetClassRoot(class_root, this); |
| 4223 | } |
| 4224 | |
| 4225 | ObjPtr<mirror::Class> ClassLinker::FindPrimitiveClass(char type) { |
| 4226 | ObjPtr<mirror::Class> result = LookupPrimitiveClass(type); |
| 4227 | if (UNLIKELY(result == nullptr)) { |
| 4228 | std::string printable_type(PrintableChar(type)); |
| 4229 | ThrowNoClassDefFoundError("Not a primitive type: %s", printable_type.c_str()); |
| 4230 | } |
| 4231 | return result; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4232 | } |
| 4233 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 4234 | ObjPtr<mirror::Class> ClassLinker::InsertClass(const char* descriptor, |
| 4235 | ObjPtr<mirror::Class> klass, |
| 4236 | size_t hash) { |
Alex Light | e9f6103 | 2018-09-24 16:04:51 -0700 | [diff] [blame] | 4237 | DCHECK(Thread::Current()->CanLoadClasses()); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 4238 | if (VLOG_IS_ON(class_linker)) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4239 | ObjPtr<mirror::DexCache> dex_cache = klass->GetDexCache(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 4240 | std::string source; |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4241 | if (dex_cache != nullptr) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 4242 | source += " from "; |
| 4243 | source += dex_cache->GetLocation()->ToModifiedUtf8(); |
| 4244 | } |
| 4245 | LOG(INFO) << "Loaded class " << descriptor << source; |
| 4246 | } |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 4247 | { |
| 4248 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Vladimir Marko | 0984e48 | 2019-03-27 16:41:41 +0000 | [diff] [blame] | 4249 | const ObjPtr<mirror::ClassLoader> class_loader = klass->GetClassLoader(); |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 4250 | ClassTable* const class_table = InsertClassTableForClassLoader(class_loader); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4251 | ObjPtr<mirror::Class> existing = class_table->Lookup(descriptor, hash); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4252 | if (existing != nullptr) { |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 4253 | return existing; |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 4254 | } |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 4255 | VerifyObject(klass); |
| 4256 | class_table->InsertWithHash(klass, hash); |
| 4257 | if (class_loader != nullptr) { |
| 4258 | // 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] | 4259 | WriteBarrier::ForEveryFieldWrite(class_loader); |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 4260 | } |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 4261 | if (log_new_roots_) { |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 4262 | new_class_roots_.push_back(GcRoot<mirror::Class>(klass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4263 | } |
| 4264 | } |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 4265 | if (kIsDebugBuild) { |
| 4266 | // Test that copied methods correctly can find their holder. |
| 4267 | for (ArtMethod& method : klass->GetCopiedMethods(image_pointer_size_)) { |
| 4268 | CHECK_EQ(GetHoldingClassOfCopiedMethod(&method), klass); |
| 4269 | } |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 4270 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4271 | return nullptr; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4272 | } |
| 4273 | |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 4274 | void ClassLinker::WriteBarrierForBootOatFileBssRoots(const OatFile* oat_file) { |
Mathieu Chartier | a1467d0 | 2017-02-22 09:22:50 -0800 | [diff] [blame] | 4275 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
| 4276 | DCHECK(!oat_file->GetBssGcRoots().empty()) << oat_file->GetLocation(); |
| 4277 | if (log_new_roots_ && !ContainsElement(new_bss_roots_boot_oat_files_, oat_file)) { |
| 4278 | new_bss_roots_boot_oat_files_.push_back(oat_file); |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 4279 | } |
| 4280 | } |
| 4281 | |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4282 | // TODO This should really be in mirror::Class. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4283 | void ClassLinker::UpdateClassMethods(ObjPtr<mirror::Class> klass, |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4284 | LengthPrefixedArray<ArtMethod>* new_methods) { |
| 4285 | klass->SetMethodsPtrUnchecked(new_methods, |
| 4286 | klass->NumDirectMethods(), |
| 4287 | klass->NumDeclaredVirtualMethods()); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4288 | // 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] | 4289 | WriteBarrier::ForEveryFieldWrite(klass); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4290 | } |
| 4291 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 4292 | ObjPtr<mirror::Class> ClassLinker::LookupClass(Thread* self, |
| 4293 | const char* descriptor, |
| 4294 | ObjPtr<mirror::ClassLoader> class_loader) { |
Andreas Gampe | 2ff3b97 | 2017-06-05 18:14:53 -0700 | [diff] [blame] | 4295 | return LookupClass(self, descriptor, ComputeModifiedUtf8Hash(descriptor), class_loader); |
| 4296 | } |
| 4297 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 4298 | ObjPtr<mirror::Class> ClassLinker::LookupClass(Thread* self, |
| 4299 | const char* descriptor, |
| 4300 | size_t hash, |
| 4301 | ObjPtr<mirror::ClassLoader> class_loader) { |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 4302 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 4303 | ClassTable* const class_table = ClassTableForClassLoader(class_loader); |
| 4304 | if (class_table != nullptr) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4305 | ObjPtr<mirror::Class> result = class_table->Lookup(descriptor, hash); |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 4306 | if (result != nullptr) { |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 4307 | return result; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4308 | } |
Sameer Abu Asal | 2c6de22 | 2013-05-02 17:38:59 -0700 | [diff] [blame] | 4309 | } |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 4310 | return nullptr; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4311 | } |
| 4312 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4313 | class MoveClassTableToPreZygoteVisitor : public ClassLoaderVisitor { |
| 4314 | public: |
Igor Murashkin | 2ffb703 | 2017-11-08 13:35:21 -0800 | [diff] [blame] | 4315 | MoveClassTableToPreZygoteVisitor() {} |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4316 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4317 | void Visit(ObjPtr<mirror::ClassLoader> class_loader) |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4318 | REQUIRES(Locks::classlinker_classes_lock_) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 4319 | REQUIRES_SHARED(Locks::mutator_lock_) override { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4320 | ClassTable* const class_table = class_loader->GetClassTable(); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 4321 | if (class_table != nullptr) { |
| 4322 | class_table->FreezeSnapshot(); |
| 4323 | } |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 4324 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4325 | }; |
| 4326 | |
| 4327 | void ClassLinker::MoveClassTableToPreZygote() { |
| 4328 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 4329 | boot_class_table_->FreezeSnapshot(); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4330 | MoveClassTableToPreZygoteVisitor visitor; |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 4331 | VisitClassLoaders(&visitor); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 4332 | } |
| 4333 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4334 | // Look up classes by hash and descriptor and put all matching ones in the result array. |
| 4335 | class LookupClassesVisitor : public ClassLoaderVisitor { |
| 4336 | public: |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4337 | LookupClassesVisitor(const char* descriptor, |
| 4338 | size_t hash, |
| 4339 | std::vector<ObjPtr<mirror::Class>>* result) |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4340 | : descriptor_(descriptor), |
| 4341 | hash_(hash), |
| 4342 | result_(result) {} |
| 4343 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4344 | void Visit(ObjPtr<mirror::ClassLoader> class_loader) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 4345 | REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_) override { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4346 | ClassTable* const class_table = class_loader->GetClassTable(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4347 | ObjPtr<mirror::Class> klass = class_table->Lookup(descriptor_, hash_); |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 4348 | // Add `klass` only if `class_loader` is its defining (not just initiating) class loader. |
| 4349 | if (klass != nullptr && klass->GetClassLoader() == class_loader) { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4350 | result_->push_back(klass); |
| 4351 | } |
| 4352 | } |
| 4353 | |
| 4354 | private: |
| 4355 | const char* const descriptor_; |
| 4356 | const size_t hash_; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4357 | std::vector<ObjPtr<mirror::Class>>* const result_; |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4358 | }; |
| 4359 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4360 | void ClassLinker::LookupClasses(const char* descriptor, |
| 4361 | std::vector<ObjPtr<mirror::Class>>& result) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4362 | result.clear(); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4363 | Thread* const self = Thread::Current(); |
| 4364 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 4365 | const size_t hash = ComputeModifiedUtf8Hash(descriptor); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 4366 | ObjPtr<mirror::Class> klass = boot_class_table_->Lookup(descriptor, hash); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 4367 | if (klass != nullptr) { |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 4368 | DCHECK(klass->GetClassLoader() == nullptr); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 4369 | result.push_back(klass); |
| 4370 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4371 | LookupClassesVisitor visitor(descriptor, hash, &result); |
| 4372 | VisitClassLoaders(&visitor); |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 4373 | } |
| 4374 | |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4375 | bool ClassLinker::AttemptSupertypeVerification(Thread* self, |
Nicolas Geoffray | 5b0b2e1 | 2021-03-19 14:48:40 +0000 | [diff] [blame] | 4376 | verifier::VerifierDeps* verifier_deps, |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4377 | Handle<mirror::Class> klass, |
| 4378 | Handle<mirror::Class> supertype) { |
| 4379 | DCHECK(self != nullptr); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4380 | DCHECK(klass != nullptr); |
| 4381 | DCHECK(supertype != nullptr); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4382 | |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4383 | if (!supertype->IsVerified() && !supertype->IsErroneous()) { |
Nicolas Geoffray | 5b0b2e1 | 2021-03-19 14:48:40 +0000 | [diff] [blame] | 4384 | VerifyClass(self, verifier_deps, supertype); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4385 | } |
Nicolas Geoffray | 7cc3ae5 | 2017-03-07 14:33:37 +0000 | [diff] [blame] | 4386 | |
Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 4387 | if (supertype->IsVerified() |
| 4388 | || supertype->ShouldVerifyAtRuntime() |
| 4389 | || supertype->IsVerifiedNeedsAccessChecks()) { |
Nicolas Geoffray | 7cc3ae5 | 2017-03-07 14:33:37 +0000 | [diff] [blame] | 4390 | // The supertype is either verified, or we soft failed at AOT time. |
| 4391 | DCHECK(supertype->IsVerified() || Runtime::Current()->IsAotCompiler()); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4392 | return true; |
| 4393 | } |
| 4394 | // If we got this far then we have a hard failure. |
| 4395 | std::string error_msg = |
| 4396 | StringPrintf("Rejecting class %s that attempts to sub-type erroneous class %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4397 | klass->PrettyDescriptor().c_str(), |
| 4398 | supertype->PrettyDescriptor().c_str()); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4399 | LOG(WARNING) << error_msg << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8(); |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4400 | StackHandleScope<1> hs(self); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4401 | Handle<mirror::Throwable> cause(hs.NewHandle(self->GetException())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4402 | if (cause != nullptr) { |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4403 | // Set during VerifyClass call (if at all). |
| 4404 | self->ClearException(); |
| 4405 | } |
| 4406 | // Change into a verify error. |
| 4407 | ThrowVerifyError(klass.Get(), "%s", error_msg.c_str()); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4408 | if (cause != nullptr) { |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4409 | self->GetException()->SetCause(cause.Get()); |
| 4410 | } |
| 4411 | ClassReference ref(klass->GetDexCache()->GetDexFile(), klass->GetDexClassDefIndex()); |
| 4412 | if (Runtime::Current()->IsAotCompiler()) { |
| 4413 | Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref); |
| 4414 | } |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4415 | // Need to grab the lock to change status. |
| 4416 | ObjectLock<mirror::Class> super_lock(self, klass); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4417 | mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4418 | return false; |
| 4419 | } |
| 4420 | |
Nicolas Geoffray | 5b0b2e1 | 2021-03-19 14:48:40 +0000 | [diff] [blame] | 4421 | verifier::FailureKind ClassLinker::VerifyClass(Thread* self, |
| 4422 | verifier::VerifierDeps* verifier_deps, |
| 4423 | Handle<mirror::Class> klass, |
| 4424 | verifier::HardFailLogMode log_level) { |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4425 | { |
| 4426 | // TODO: assert that the monitor on the Class is held |
| 4427 | ObjectLock<mirror::Class> lock(self, klass); |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 4428 | |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4429 | // Is somebody verifying this now? |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4430 | ClassStatus old_status = klass->GetStatus(); |
Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 4431 | while (old_status == ClassStatus::kVerifying) { |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4432 | lock.WaitIgnoringInterrupts(); |
Mathieu Chartier | 5ef7020 | 2017-06-29 10:45:10 -0700 | [diff] [blame] | 4433 | // WaitIgnoringInterrupts can still receive an interrupt and return early, in this |
| 4434 | // case we may see the same status again. b/62912904. This is why the check is |
| 4435 | // greater or equal. |
| 4436 | CHECK(klass->IsErroneous() || (klass->GetStatus() >= old_status)) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4437 | << "Class '" << klass->PrettyClass() |
| 4438 | << "' performed an illegal verification state transition from " << old_status |
| 4439 | << " to " << klass->GetStatus(); |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4440 | old_status = klass->GetStatus(); |
| 4441 | } |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 4442 | |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4443 | // The class might already be erroneous, for example at compile time if we attempted to verify |
| 4444 | // this class as a parent to another. |
| 4445 | if (klass->IsErroneous()) { |
| 4446 | ThrowEarlierClassFailure(klass.Get()); |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4447 | return verifier::FailureKind::kHardFailure; |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4448 | } |
Brian Carlstrom | 9b5ee88 | 2012-02-28 09:48:54 -0800 | [diff] [blame] | 4449 | |
Nicolas Geoffray | 7cc3ae5 | 2017-03-07 14:33:37 +0000 | [diff] [blame] | 4450 | // Don't attempt to re-verify if already verified. |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4451 | if (klass->IsVerified()) { |
Nicolas Geoffray | 8078996 | 2021-04-30 16:50:39 +0100 | [diff] [blame] | 4452 | if (verifier_deps != nullptr && |
| 4453 | verifier_deps->ContainsDexFile(klass->GetDexFile()) && |
| 4454 | !verifier_deps->HasRecordedVerifiedStatus(klass->GetDexFile(), *klass->GetClassDef()) && |
| 4455 | !Runtime::Current()->IsAotCompiler()) { |
| 4456 | // If the klass is verified, but `verifier_deps` did not record it, this |
| 4457 | // means we are running background verification of a secondary dex file. |
| 4458 | // Re-run the verifier to populate `verifier_deps`. |
| 4459 | // No need to run the verification when running on the AOT Compiler, as |
| 4460 | // the driver handles those multithreaded cases already. |
| 4461 | std::string error_msg; |
| 4462 | verifier::FailureKind failure = |
| 4463 | PerformClassVerification(self, verifier_deps, klass, log_level, &error_msg); |
| 4464 | // We could have soft failures, so just check that we don't have a hard |
| 4465 | // failure. |
| 4466 | DCHECK_NE(failure, verifier::FailureKind::kHardFailure) << error_msg; |
| 4467 | } |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4468 | return verifier::FailureKind::kNoFailure; |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4469 | } |
Nicolas Geoffray | 7cc3ae5 | 2017-03-07 14:33:37 +0000 | [diff] [blame] | 4470 | |
Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 4471 | if (klass->IsVerifiedNeedsAccessChecks()) { |
| 4472 | if (!Runtime::Current()->IsAotCompiler()) { |
| 4473 | // Mark the class as having a verification attempt to avoid re-running |
Nicolas Geoffray | 66934ef | 2021-07-07 14:56:23 +0100 | [diff] [blame] | 4474 | // the verifier. |
Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 4475 | mirror::Class::SetStatus(klass, ClassStatus::kVerified, self); |
| 4476 | } |
| 4477 | return verifier::FailureKind::kAccessChecksFailure; |
| 4478 | } |
| 4479 | |
Nicolas Geoffray | 7cc3ae5 | 2017-03-07 14:33:37 +0000 | [diff] [blame] | 4480 | // For AOT, don't attempt to re-verify if we have already found we should |
| 4481 | // verify at runtime. |
Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 4482 | if (klass->ShouldVerifyAtRuntime()) { |
| 4483 | CHECK(Runtime::Current()->IsAotCompiler()); |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4484 | return verifier::FailureKind::kSoftFailure; |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4485 | } |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 4486 | |
Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 4487 | DCHECK_EQ(klass->GetStatus(), ClassStatus::kResolved); |
| 4488 | mirror::Class::SetStatus(klass, ClassStatus::kVerifying, self); |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4489 | |
| 4490 | // Skip verification if disabled. |
| 4491 | if (!Runtime::Current()->IsVerificationEnabled()) { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4492 | mirror::Class::SetStatus(klass, ClassStatus::kVerified, self); |
Nicolas Geoffray | 66934ef | 2021-07-07 14:56:23 +0100 | [diff] [blame] | 4493 | UpdateClassAfterVerification(klass, image_pointer_size_, verifier::FailureKind::kNoFailure); |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4494 | return verifier::FailureKind::kNoFailure; |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4495 | } |
Jeff Hao | 4a200f5 | 2014-04-01 14:58:49 -0700 | [diff] [blame] | 4496 | } |
| 4497 | |
Bharadwaj Kalandhabhatta | 271c1e1 | 2017-06-27 11:14:49 -0700 | [diff] [blame] | 4498 | VLOG(class_linker) << "Beginning verification for class: " |
| 4499 | << klass->PrettyDescriptor() |
| 4500 | << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8(); |
| 4501 | |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 4502 | // Verify super class. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4503 | StackHandleScope<2> hs(self); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4504 | MutableHandle<mirror::Class> supertype(hs.NewHandle(klass->GetSuperClass())); |
| 4505 | // 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] | 4506 | if (supertype != nullptr && |
| 4507 | !AttemptSupertypeVerification(self, verifier_deps, klass, supertype)) { |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4508 | CHECK(self->IsExceptionPending()) << "Verification error should be pending."; |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4509 | return verifier::FailureKind::kHardFailure; |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4510 | } |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 4511 | |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4512 | // Verify all default super-interfaces. |
| 4513 | // |
| 4514 | // (1) Don't bother if the superclass has already had a soft verification failure. |
| 4515 | // |
| 4516 | // (2) Interfaces shouldn't bother to do this recursive verification because they cannot cause |
| 4517 | // recursive initialization by themselves. This is because when an interface is initialized |
| 4518 | // directly it must not initialize its superinterfaces. We are allowed to verify regardless |
| 4519 | // but choose not to for an optimization. If the interfaces is being verified due to a class |
| 4520 | // initialization (which would need all the default interfaces to be verified) the class code |
| 4521 | // will trigger the recursive verification anyway. |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4522 | if ((supertype == nullptr || supertype->IsVerified()) // See (1) |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4523 | && !klass->IsInterface()) { // See (2) |
| 4524 | int32_t iftable_count = klass->GetIfTableCount(); |
| 4525 | MutableHandle<mirror::Class> iface(hs.NewHandle<mirror::Class>(nullptr)); |
| 4526 | // Loop through all interfaces this class has defined. It doesn't matter the order. |
| 4527 | for (int32_t i = 0; i < iftable_count; i++) { |
| 4528 | iface.Assign(klass->GetIfTable()->GetInterface(i)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4529 | DCHECK(iface != nullptr); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4530 | // We only care if we have default interfaces and can skip if we are already verified... |
| 4531 | if (LIKELY(!iface->HasDefaultMethods() || iface->IsVerified())) { |
| 4532 | continue; |
Nicolas Geoffray | 5b0b2e1 | 2021-03-19 14:48:40 +0000 | [diff] [blame] | 4533 | } else if (UNLIKELY(!AttemptSupertypeVerification(self, verifier_deps, klass, iface))) { |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4534 | // We had a hard failure while verifying this interface. Just return immediately. |
| 4535 | CHECK(self->IsExceptionPending()) << "Verification error should be pending."; |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4536 | return verifier::FailureKind::kHardFailure; |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4537 | } else if (UNLIKELY(!iface->IsVerified())) { |
| 4538 | // We softly failed to verify the iface. Stop checking and clean up. |
| 4539 | // Put the iface into the supertype handle so we know what caused us to fail. |
| 4540 | supertype.Assign(iface.Get()); |
| 4541 | break; |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 4542 | } |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 4543 | } |
| 4544 | } |
| 4545 | |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4546 | // At this point if verification failed, then supertype is the "first" supertype that failed |
| 4547 | // verification (without a specific order). If verification succeeded, then supertype is either |
| 4548 | // null or the original superclass of klass and is verified. |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4549 | DCHECK(supertype == nullptr || |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4550 | supertype.Get() == klass->GetSuperClass() || |
| 4551 | !supertype->IsVerified()); |
| 4552 | |
Elliott Hughes | 634eb2e | 2012-03-22 16:06:28 -0700 | [diff] [blame] | 4553 | // 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] | 4554 | const DexFile& dex_file = *klass->GetDexCache()->GetDexFile(); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4555 | ClassStatus oat_file_class_status(ClassStatus::kNotReady); |
Nicolas Geoffray | 6df4511 | 2021-02-07 21:51:58 +0000 | [diff] [blame] | 4556 | bool preverified = VerifyClassUsingOatFile(self, dex_file, klass, oat_file_class_status); |
Bharadwaj Kalandhabhatta | 271c1e1 | 2017-06-27 11:14:49 -0700 | [diff] [blame] | 4557 | |
| 4558 | VLOG(class_linker) << "Class preverified status for class " |
| 4559 | << klass->PrettyDescriptor() |
| 4560 | << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8() |
| 4561 | << ": " |
Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 4562 | << preverified |
| 4563 | << "( " << oat_file_class_status << ")"; |
Bharadwaj Kalandhabhatta | 271c1e1 | 2017-06-27 11:14:49 -0700 | [diff] [blame] | 4564 | |
Nicolas Geoffray | 44dc8a3 | 2021-06-21 15:23:49 +0100 | [diff] [blame] | 4565 | // If the oat file says the class had an error, re-run the verifier. That way we will either: |
| 4566 | // 1) Be successful at runtime, or |
| 4567 | // 2) Get a precise error message. |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 4568 | DCHECK(!mirror::Class::IsErroneous(oat_file_class_status) || !preverified); |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 4569 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4570 | std::string error_msg; |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4571 | verifier::FailureKind verifier_failure = verifier::FailureKind::kNoFailure; |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 4572 | if (!preverified) { |
Nicolas Geoffray | 5b0b2e1 | 2021-03-19 14:48:40 +0000 | [diff] [blame] | 4573 | verifier_failure = PerformClassVerification(self, verifier_deps, klass, log_level, &error_msg); |
Nicolas Geoffray | 66934ef | 2021-07-07 14:56:23 +0100 | [diff] [blame] | 4574 | } else if (oat_file_class_status == ClassStatus::kVerifiedNeedsAccessChecks) { |
| 4575 | verifier_failure = verifier::FailureKind::kAccessChecksFailure; |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 4576 | } |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4577 | |
| 4578 | // Verification is done, grab the lock again. |
| 4579 | ObjectLock<mirror::Class> lock(self, klass); |
Nicolas Geoffray | 66934ef | 2021-07-07 14:56:23 +0100 | [diff] [blame] | 4580 | self->AssertNoPendingException(); |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4581 | |
Nicolas Geoffray | 66934ef | 2021-07-07 14:56:23 +0100 | [diff] [blame] | 4582 | if (verifier_failure == verifier::FailureKind::kHardFailure) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4583 | VLOG(verifier) << "Verification failed on class " << klass->PrettyDescriptor() |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 4584 | << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8() |
| 4585 | << " because: " << error_msg; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4586 | ThrowVerifyError(klass.Get(), "%s", error_msg.c_str()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4587 | mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self); |
Nicolas Geoffray | 66934ef | 2021-07-07 14:56:23 +0100 | [diff] [blame] | 4588 | return verifier_failure; |
jeffhao | 5cfd6fb | 2011-09-27 13:54:29 -0700 | [diff] [blame] | 4589 | } |
Nicolas Geoffray | 66934ef | 2021-07-07 14:56:23 +0100 | [diff] [blame] | 4590 | |
| 4591 | // Make sure all classes referenced by catch blocks are resolved. |
| 4592 | ResolveClassExceptionHandlerTypes(klass); |
| 4593 | |
Nicolas Geoffray | 486dda0 | 2017-09-11 14:15:52 +0100 | [diff] [blame] | 4594 | if (Runtime::Current()->IsAotCompiler()) { |
Nicolas Geoffray | 66934ef | 2021-07-07 14:56:23 +0100 | [diff] [blame] | 4595 | if (supertype != nullptr && supertype->ShouldVerifyAtRuntime()) { |
| 4596 | // Regardless of our own verification result, we need to verify the class |
| 4597 | // at runtime if the super class is not verified. This is required in case |
| 4598 | // we generate an app/boot image. |
Nicolas Geoffray | 66934ef | 2021-07-07 14:56:23 +0100 | [diff] [blame] | 4599 | mirror::Class::SetStatus(klass, ClassStatus::kRetryVerificationAtRuntime, self); |
| 4600 | } else if (verifier_failure == verifier::FailureKind::kNoFailure) { |
| 4601 | mirror::Class::SetStatus(klass, ClassStatus::kVerified, self); |
| 4602 | } else if (verifier_failure == verifier::FailureKind::kSoftFailure || |
| 4603 | verifier_failure == verifier::FailureKind::kTypeChecksFailure) { |
| 4604 | mirror::Class::SetStatus(klass, ClassStatus::kRetryVerificationAtRuntime, self); |
| 4605 | } else { |
| 4606 | mirror::Class::SetStatus(klass, ClassStatus::kVerifiedNeedsAccessChecks, self); |
| 4607 | } |
| 4608 | // Notify the compiler about the verification status, in case the class |
| 4609 | // was verified implicitly (eg super class of a compiled class). When the |
| 4610 | // compiler unloads dex file after compilation, we still want to keep |
| 4611 | // verification states. |
Nicolas Geoffray | 486dda0 | 2017-09-11 14:15:52 +0100 | [diff] [blame] | 4612 | Runtime::Current()->GetCompilerCallbacks()->UpdateClassState( |
| 4613 | ClassReference(&klass->GetDexFile(), klass->GetDexClassDefIndex()), klass->GetStatus()); |
Nicolas Geoffray | 66934ef | 2021-07-07 14:56:23 +0100 | [diff] [blame] | 4614 | } else { |
| 4615 | mirror::Class::SetStatus(klass, ClassStatus::kVerified, self); |
Nicolas Geoffray | 486dda0 | 2017-09-11 14:15:52 +0100 | [diff] [blame] | 4616 | } |
Nicolas Geoffray | 66934ef | 2021-07-07 14:56:23 +0100 | [diff] [blame] | 4617 | |
| 4618 | UpdateClassAfterVerification(klass, image_pointer_size_, verifier_failure); |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame] | 4619 | return verifier_failure; |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 4620 | } |
| 4621 | |
Mathieu Chartier | 9e050df | 2017-08-09 10:05:47 -0700 | [diff] [blame] | 4622 | verifier::FailureKind ClassLinker::PerformClassVerification(Thread* self, |
Nicolas Geoffray | 5b0b2e1 | 2021-03-19 14:48:40 +0000 | [diff] [blame] | 4623 | verifier::VerifierDeps* verifier_deps, |
Mathieu Chartier | 9e050df | 2017-08-09 10:05:47 -0700 | [diff] [blame] | 4624 | Handle<mirror::Class> klass, |
| 4625 | verifier::HardFailLogMode log_level, |
| 4626 | std::string* error_msg) { |
| 4627 | Runtime* const runtime = Runtime::Current(); |
Nicolas Geoffray | 7744b69 | 2021-07-06 16:19:32 +0100 | [diff] [blame] | 4628 | StackHandleScope<2> hs(self); |
| 4629 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache())); |
| 4630 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader())); |
Andreas Gampe | a43ba3d | 2019-03-13 15:49:20 -0700 | [diff] [blame] | 4631 | return verifier::ClassVerifier::VerifyClass(self, |
Nicolas Geoffray | 5b0b2e1 | 2021-03-19 14:48:40 +0000 | [diff] [blame] | 4632 | verifier_deps, |
Nicolas Geoffray | 7744b69 | 2021-07-06 16:19:32 +0100 | [diff] [blame] | 4633 | dex_cache->GetDexFile(), |
| 4634 | klass, |
| 4635 | dex_cache, |
| 4636 | class_loader, |
| 4637 | *klass->GetClassDef(), |
Andreas Gampe | a43ba3d | 2019-03-13 15:49:20 -0700 | [diff] [blame] | 4638 | runtime->GetCompilerCallbacks(), |
Andreas Gampe | a43ba3d | 2019-03-13 15:49:20 -0700 | [diff] [blame] | 4639 | log_level, |
| 4640 | Runtime::Current()->GetTargetSdkVersion(), |
| 4641 | error_msg); |
Mathieu Chartier | 9e050df | 2017-08-09 10:05:47 -0700 | [diff] [blame] | 4642 | } |
| 4643 | |
Nicolas Geoffray | 6df4511 | 2021-02-07 21:51:58 +0000 | [diff] [blame] | 4644 | bool ClassLinker::VerifyClassUsingOatFile(Thread* self, |
| 4645 | const DexFile& dex_file, |
| 4646 | Handle<mirror::Class> klass, |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4647 | ClassStatus& oat_file_class_status) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 4648 | // If we're compiling, we can only verify the class using the oat file if |
| 4649 | // 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] | 4650 | // the compilation unit (app - dependencies). We will let the compiler callback |
| 4651 | // tell us about the latter. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 4652 | if (Runtime::Current()->IsAotCompiler()) { |
Andreas Gampe | e993458 | 2018-01-19 21:23:04 -0800 | [diff] [blame] | 4653 | CompilerCallbacks* callbacks = Runtime::Current()->GetCompilerCallbacks(); |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 4654 | // We are compiling an app (not the image). |
Nicolas Geoffray | 6df4511 | 2021-02-07 21:51:58 +0000 | [diff] [blame] | 4655 | if (!callbacks->CanUseOatStatusForVerification(klass.Get())) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 4656 | return false; |
| 4657 | } |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4658 | } |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 4659 | |
Andreas Gampe | b40d361 | 2018-06-26 15:49:42 -0700 | [diff] [blame] | 4660 | const OatDexFile* oat_dex_file = dex_file.GetOatDexFile(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4661 | // 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] | 4662 | if (oat_dex_file == nullptr || oat_dex_file->GetOatFile() == nullptr) { |
Anwar Ghuloum | ad256bb | 2013-07-18 14:58:55 -0700 | [diff] [blame] | 4663 | return false; |
| 4664 | } |
| 4665 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 4666 | uint16_t class_def_index = klass->GetDexClassDefIndex(); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 4667 | oat_file_class_status = oat_dex_file->GetOatClass(class_def_index).GetStatus(); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4668 | if (oat_file_class_status >= ClassStatus::kVerified) { |
Mathieu Chartier | a079e3a | 2016-03-16 19:08:31 -0700 | [diff] [blame] | 4669 | return true; |
| 4670 | } |
Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 4671 | if (oat_file_class_status >= ClassStatus::kVerifiedNeedsAccessChecks) { |
| 4672 | // We return that the clas has already been verified, and the caller should |
| 4673 | // check the class status to ensure we run with access checks. |
| 4674 | return true; |
| 4675 | } |
Nicolas Geoffray | 6df4511 | 2021-02-07 21:51:58 +0000 | [diff] [blame] | 4676 | |
| 4677 | // Check the class status with the vdex file. |
| 4678 | const OatFile* oat_file = oat_dex_file->GetOatFile(); |
| 4679 | if (oat_file != nullptr) { |
Nicolas Geoffray | 44dc8a3 | 2021-06-21 15:23:49 +0100 | [diff] [blame] | 4680 | ClassStatus vdex_status = oat_file->GetVdexFile()->ComputeClassStatus(self, klass); |
| 4681 | if (vdex_status >= ClassStatus::kVerifiedNeedsAccessChecks) { |
Nicolas Geoffray | 327cfcf | 2021-10-12 14:13:25 +0100 | [diff] [blame] | 4682 | VLOG(verifier) << "Vdex verification success for " << klass->PrettyClass(); |
Nicolas Geoffray | 44dc8a3 | 2021-06-21 15:23:49 +0100 | [diff] [blame] | 4683 | oat_file_class_status = vdex_status; |
Nicolas Geoffray | 6df4511 | 2021-02-07 21:51:58 +0000 | [diff] [blame] | 4684 | return true; |
| 4685 | } |
| 4686 | } |
| 4687 | |
Mathieu Chartier | a079e3a | 2016-03-16 19:08:31 -0700 | [diff] [blame] | 4688 | // If we only verified a subset of the classes at compile time, we can end up with classes that |
| 4689 | // were resolved by the verifier. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4690 | if (oat_file_class_status == ClassStatus::kResolved) { |
Mathieu Chartier | a079e3a | 2016-03-16 19:08:31 -0700 | [diff] [blame] | 4691 | return false; |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4692 | } |
Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 4693 | // We never expect a .oat file to have kRetryVerificationAtRuntime statuses. |
| 4694 | CHECK_NE(oat_file_class_status, ClassStatus::kRetryVerificationAtRuntime) |
| 4695 | << klass->PrettyClass() << " " << dex_file.GetLocation(); |
| 4696 | |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 4697 | if (mirror::Class::IsErroneous(oat_file_class_status)) { |
Nicolas Geoffray | 44dc8a3 | 2021-06-21 15:23:49 +0100 | [diff] [blame] | 4698 | // Compile time verification failed with a hard error. We'll re-run |
| 4699 | // verification, which might be successful at runtime. |
jeffhao | 1ac2944 | 2012-03-26 11:37:32 -0700 | [diff] [blame] | 4700 | return false; |
| 4701 | } |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4702 | if (oat_file_class_status == ClassStatus::kNotReady) { |
Ian Rogers | c476227 | 2012-02-01 15:55:55 -0800 | [diff] [blame] | 4703 | // Status is uninitialized if we couldn't determine the status at compile time, for example, |
| 4704 | // not loading the class. |
| 4705 | // TODO: when the verifier doesn't rely on Class-es failing to resolve/load the type hierarchy |
| 4706 | // isn't a problem and this case shouldn't occur |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4707 | return false; |
| 4708 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 4709 | std::string temp; |
Elliott Hughes | 634eb2e | 2012-03-22 16:06:28 -0700 | [diff] [blame] | 4710 | LOG(FATAL) << "Unexpected class status: " << oat_file_class_status |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4711 | << " " << dex_file.GetLocation() << " " << klass->PrettyClass() << " " |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 4712 | << klass->GetDescriptor(&temp); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 4713 | UNREACHABLE(); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4714 | } |
| 4715 | |
Alex Light | 5a55986 | 2016-01-29 12:24:48 -0800 | [diff] [blame] | 4716 | void ClassLinker::ResolveClassExceptionHandlerTypes(Handle<mirror::Class> klass) { |
Alex Light | 51a64d5 | 2015-12-17 13:55:59 -0800 | [diff] [blame] | 4717 | for (ArtMethod& method : klass->GetMethods(image_pointer_size_)) { |
Alex Light | 5a55986 | 2016-01-29 12:24:48 -0800 | [diff] [blame] | 4718 | ResolveMethodExceptionHandlerTypes(&method); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4719 | } |
| 4720 | } |
| 4721 | |
Alex Light | 5a55986 | 2016-01-29 12:24:48 -0800 | [diff] [blame] | 4722 | void ClassLinker::ResolveMethodExceptionHandlerTypes(ArtMethod* method) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4723 | // similar to DexVerifier::ScanTryCatchBlocks and dex2oat's ResolveExceptionsForMethod. |
David Sehr | 0225f8e | 2018-01-31 08:52:24 +0000 | [diff] [blame] | 4724 | CodeItemDataAccessor accessor(method->DexInstructionData()); |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 4725 | if (!accessor.HasCodeItem()) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4726 | return; // native or abstract method |
| 4727 | } |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 4728 | if (accessor.TriesSize() == 0) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4729 | return; // nothing to process |
| 4730 | } |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 4731 | const uint8_t* handlers_ptr = accessor.GetCatchHandlerData(0); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4732 | uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4733 | for (uint32_t idx = 0; idx < handlers_size; idx++) { |
| 4734 | CatchHandlerIterator iterator(handlers_ptr); |
| 4735 | for (; iterator.HasNext(); iterator.Next()) { |
| 4736 | // Ensure exception types are resolved so that they don't need resolution to be delivered, |
| 4737 | // unresolved exception types will be ignored by exception delivery |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 4738 | if (iterator.GetHandlerTypeIndex().IsValid()) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4739 | ObjPtr<mirror::Class> exception_type = ResolveType(iterator.GetHandlerTypeIndex(), method); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4740 | if (exception_type == nullptr) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4741 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 4742 | Thread::Current()->ClearException(); |
| 4743 | } |
| 4744 | } |
| 4745 | } |
| 4746 | handlers_ptr = iterator.EndDataPointer(); |
| 4747 | } |
| 4748 | } |
| 4749 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 4750 | ObjPtr<mirror::Class> ClassLinker::CreateProxyClass(ScopedObjectAccessAlreadyRunnable& soa, |
| 4751 | jstring name, |
| 4752 | jobjectArray interfaces, |
| 4753 | jobject loader, |
| 4754 | jobjectArray methods, |
| 4755 | jobjectArray throws) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4756 | Thread* self = soa.Self(); |
Alex Light | e9f6103 | 2018-09-24 16:04:51 -0700 | [diff] [blame] | 4757 | |
| 4758 | // This is to prevent the calls to ClassLoad and ClassPrepare which can cause java/user-supplied |
| 4759 | // code to be executed. We put it up here so we can avoid all the allocations associated with |
| 4760 | // creating the class. This can happen with (eg) jit-threads. |
| 4761 | if (!self->CanLoadClasses()) { |
| 4762 | // Make sure we don't try to load anything, potentially causing an infinite loop. |
| 4763 | ObjPtr<mirror::Throwable> pre_allocated = |
| 4764 | Runtime::Current()->GetPreAllocatedNoClassDefFoundError(); |
| 4765 | self->SetException(pre_allocated); |
| 4766 | return nullptr; |
| 4767 | } |
| 4768 | |
Alex Light | 133987d | 2020-03-26 19:22:12 +0000 | [diff] [blame] | 4769 | StackHandleScope<12> hs(self); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4770 | MutableHandle<mirror::Class> temp_klass(hs.NewHandle( |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 4771 | AllocClass(self, GetClassRoot<mirror::Class>(this), sizeof(mirror::Class)))); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4772 | if (temp_klass == nullptr) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4773 | CHECK(self->IsExceptionPending()); // OOME. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4774 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4775 | } |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4776 | DCHECK(temp_klass->GetClass() != nullptr); |
| 4777 | temp_klass->SetObjectSize(sizeof(mirror::Proxy)); |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 4778 | // Set the class access flags incl. VerificationAttempted, so we do not try to set the flag on |
| 4779 | // the methods. |
Nicolas Geoffray | 66934ef | 2021-07-07 14:56:23 +0100 | [diff] [blame] | 4780 | temp_klass->SetAccessFlagsDuringLinking(kAccClassIsProxy | kAccPublic | kAccFinal); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4781 | temp_klass->SetClassLoader(soa.Decode<mirror::ClassLoader>(loader)); |
| 4782 | DCHECK_EQ(temp_klass->GetPrimitiveType(), Primitive::kPrimNot); |
| 4783 | temp_klass->SetName(soa.Decode<mirror::String>(name)); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 4784 | temp_klass->SetDexCache(GetClassRoot<mirror::Proxy>(this)->GetDexCache()); |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 4785 | // Object has an empty iftable, copy it for that reason. |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 4786 | temp_klass->SetIfTable(GetClassRoot<mirror::Object>(this)->GetIfTable()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4787 | mirror::Class::SetStatus(temp_klass, ClassStatus::kIdx, self); |
Vladimir Marko | 3892e62 | 2019-03-15 15:22:18 +0000 | [diff] [blame] | 4788 | std::string storage; |
| 4789 | const char* descriptor = temp_klass->GetDescriptor(&storage); |
| 4790 | const size_t hash = ComputeModifiedUtf8Hash(descriptor); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4791 | |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 4792 | // 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] | 4793 | LinearAlloc* const allocator = GetOrCreateAllocatorForClassLoader(temp_klass->GetClassLoader()); |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 4794 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4795 | // Insert the class before loading the fields as the field roots |
| 4796 | // (ArtField::declaring_class_) are only visited from the class |
| 4797 | // table. There can't be any suspend points between inserting the |
| 4798 | // class and setting the field arrays below. |
Vladimir Marko | 3892e62 | 2019-03-15 15:22:18 +0000 | [diff] [blame] | 4799 | ObjPtr<mirror::Class> existing = InsertClass(descriptor, temp_klass.Get(), hash); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4800 | CHECK(existing == nullptr); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4801 | |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 4802 | // Instance fields are inherited, but we add a couple of static fields... |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4803 | const size_t num_fields = 2; |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 4804 | LengthPrefixedArray<ArtField>* sfields = AllocArtFieldArray(self, allocator, num_fields); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4805 | temp_klass->SetSFieldsPtr(sfields); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4806 | |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 4807 | // 1. Create a static field 'interfaces' that holds the _declared_ interfaces implemented by |
| 4808 | // our proxy, so Class.getInterfaces doesn't return the flattened set. |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4809 | ArtField& interfaces_sfield = sfields->At(0); |
| 4810 | interfaces_sfield.SetDexFieldIndex(0); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4811 | interfaces_sfield.SetDeclaringClass(temp_klass.Get()); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4812 | interfaces_sfield.SetAccessFlags(kAccStatic | kAccPublic | kAccFinal); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4813 | |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 4814 | // 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] | 4815 | ArtField& throws_sfield = sfields->At(1); |
| 4816 | throws_sfield.SetDexFieldIndex(1); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4817 | throws_sfield.SetDeclaringClass(temp_klass.Get()); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4818 | throws_sfield.SetAccessFlags(kAccStatic | kAccPublic | kAccFinal); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4819 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4820 | // Proxies have 1 direct method, the constructor |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4821 | const size_t num_direct_methods = 1; |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4822 | |
Alex Light | 133987d | 2020-03-26 19:22:12 +0000 | [diff] [blame] | 4823 | // The array we get passed contains all methods, including private and static |
| 4824 | // ones that aren't proxied. We need to filter those out since only interface |
| 4825 | // methods (non-private & virtual) are actually proxied. |
| 4826 | Handle<mirror::ObjectArray<mirror::Method>> h_methods = |
| 4827 | hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::Method>>(methods)); |
Vladimir Marko | 679730e | 2018-05-25 15:06:48 +0100 | [diff] [blame] | 4828 | DCHECK_EQ(h_methods->GetClass(), GetClassRoot<mirror::ObjectArray<mirror::Method>>()) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4829 | << mirror::Class::PrettyClass(h_methods->GetClass()); |
Alex Light | 133987d | 2020-03-26 19:22:12 +0000 | [diff] [blame] | 4830 | // List of the actual virtual methods this class will have. |
| 4831 | std::vector<ArtMethod*> proxied_methods; |
| 4832 | std::vector<size_t> proxied_throws_idx; |
| 4833 | proxied_methods.reserve(h_methods->GetLength()); |
| 4834 | proxied_throws_idx.reserve(h_methods->GetLength()); |
| 4835 | // Filter out to only the non-private virtual methods. |
| 4836 | for (auto [mirror, idx] : ZipCount(h_methods.Iterate<mirror::Method>())) { |
| 4837 | ArtMethod* m = mirror->GetArtMethod(); |
| 4838 | if (!m->IsPrivate() && !m->IsStatic()) { |
| 4839 | proxied_methods.push_back(m); |
| 4840 | proxied_throws_idx.push_back(idx); |
| 4841 | } |
| 4842 | } |
| 4843 | const size_t num_virtual_methods = proxied_methods.size(); |
Alex Light | bc11509 | 2020-03-27 11:25:16 -0700 | [diff] [blame] | 4844 | // We also need to filter out the 'throws'. The 'throws' are a Class[][] that |
| 4845 | // contains an array of all the classes each function is declared to throw. |
| 4846 | // This is used to wrap unexpected exceptions in a |
| 4847 | // UndeclaredThrowableException exception. This array is in the same order as |
| 4848 | // the methods array and like the methods array must be filtered to remove any |
| 4849 | // non-proxied methods. |
Alex Light | 133987d | 2020-03-26 19:22:12 +0000 | [diff] [blame] | 4850 | const bool has_filtered_methods = |
| 4851 | static_cast<int32_t>(num_virtual_methods) != h_methods->GetLength(); |
| 4852 | MutableHandle<mirror::ObjectArray<mirror::ObjectArray<mirror::Class>>> original_proxied_throws( |
| 4853 | hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::ObjectArray<mirror::Class>>>(throws))); |
| 4854 | MutableHandle<mirror::ObjectArray<mirror::ObjectArray<mirror::Class>>> proxied_throws( |
| 4855 | hs.NewHandle<mirror::ObjectArray<mirror::ObjectArray<mirror::Class>>>( |
| 4856 | (has_filtered_methods) |
| 4857 | ? mirror::ObjectArray<mirror::ObjectArray<mirror::Class>>::Alloc( |
| 4858 | self, original_proxied_throws->GetClass(), num_virtual_methods) |
| 4859 | : original_proxied_throws.Get())); |
Alex Light | bc11509 | 2020-03-27 11:25:16 -0700 | [diff] [blame] | 4860 | if (proxied_throws.IsNull() && !original_proxied_throws.IsNull()) { |
| 4861 | self->AssertPendingOOMException(); |
| 4862 | return nullptr; |
| 4863 | } |
Alex Light | 133987d | 2020-03-26 19:22:12 +0000 | [diff] [blame] | 4864 | if (has_filtered_methods) { |
| 4865 | for (auto [orig_idx, new_idx] : ZipCount(MakeIterationRange(proxied_throws_idx))) { |
| 4866 | DCHECK_LE(new_idx, orig_idx); |
| 4867 | proxied_throws->Set(new_idx, original_proxied_throws->Get(orig_idx)); |
| 4868 | } |
| 4869 | } |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4870 | |
| 4871 | // Create the methods array. |
| 4872 | LengthPrefixedArray<ArtMethod>* proxy_class_methods = AllocArtMethodArray( |
| 4873 | self, allocator, num_direct_methods + num_virtual_methods); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4874 | // Currently AllocArtMethodArray cannot return null, but the OOM logic is left there in case we |
| 4875 | // want to throw OOM in the future. |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4876 | if (UNLIKELY(proxy_class_methods == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4877 | self->AssertPendingOOMException(); |
| 4878 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4879 | } |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4880 | temp_klass->SetMethodsPtr(proxy_class_methods, num_direct_methods, num_virtual_methods); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4881 | |
| 4882 | // Create the single direct method. |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4883 | CreateProxyConstructor(temp_klass, temp_klass->GetDirectMethodUnchecked(0, image_pointer_size_)); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4884 | |
| 4885 | // Create virtual method using specified prototypes. |
| 4886 | // TODO These should really use the iterators. |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4887 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4888 | auto* virtual_method = temp_klass->GetVirtualMethodUnchecked(i, image_pointer_size_); |
Alex Light | 133987d | 2020-03-26 19:22:12 +0000 | [diff] [blame] | 4889 | auto* prototype = proxied_methods[i]; |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4890 | CreateProxyMethod(temp_klass, prototype, virtual_method); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4891 | DCHECK(virtual_method->GetDeclaringClass() != nullptr); |
| 4892 | DCHECK(prototype->GetDeclaringClass() != nullptr); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4893 | } |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4894 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4895 | // The super class is java.lang.reflect.Proxy |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 4896 | temp_klass->SetSuperClass(GetClassRoot<mirror::Proxy>(this)); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4897 | // Now effectively in the loaded state. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4898 | mirror::Class::SetStatus(temp_klass, ClassStatus::kLoaded, self); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4899 | self->AssertNoPendingException(); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4900 | |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4901 | // At this point the class is loaded. Publish a ClassLoad event. |
| 4902 | // Note: this may be a temporary class. It is a listener's responsibility to handle this. |
| 4903 | Runtime::Current()->GetRuntimeCallbacks()->ClassLoad(temp_klass); |
| 4904 | |
| 4905 | MutableHandle<mirror::Class> klass = hs.NewHandle<mirror::Class>(nullptr); |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 4906 | { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4907 | // Must hold lock on object when resolved. |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4908 | ObjectLock<mirror::Class> resolution_lock(self, temp_klass); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4909 | // Link the fields and virtual methods, creating vtable and iftables. |
| 4910 | // The new class will replace the old one in the class table. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4911 | Handle<mirror::ObjectArray<mirror::Class>> h_interfaces( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 4912 | hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::Class>>(interfaces))); |
Vladimir Marko | 3892e62 | 2019-03-15 15:22:18 +0000 | [diff] [blame] | 4913 | if (!LinkClass(self, descriptor, temp_klass, h_interfaces, &klass)) { |
Vladimir Marko | a4d28dd | 2021-06-30 11:28:06 +0100 | [diff] [blame] | 4914 | if (!temp_klass->IsErroneous()) { |
| 4915 | mirror::Class::SetStatus(temp_klass, ClassStatus::kErrorUnresolved, self); |
| 4916 | } |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 4917 | return nullptr; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4918 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4919 | } |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4920 | CHECK(temp_klass->IsRetired()); |
| 4921 | CHECK_NE(temp_klass.Get(), klass.Get()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4922 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4923 | CHECK_EQ(interfaces_sfield.GetDeclaringClass(), klass.Get()); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 4924 | interfaces_sfield.SetObject<false>( |
| 4925 | klass.Get(), |
Mathieu Chartier | f8ac97f | 2016-10-05 15:56:52 -0700 | [diff] [blame] | 4926 | soa.Decode<mirror::ObjectArray<mirror::Class>>(interfaces)); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4927 | CHECK_EQ(throws_sfield.GetDeclaringClass(), klass.Get()); |
| 4928 | throws_sfield.SetObject<false>( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 4929 | klass.Get(), |
Alex Light | 133987d | 2020-03-26 19:22:12 +0000 | [diff] [blame] | 4930 | proxied_throws.Get()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4931 | |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4932 | Runtime::Current()->GetRuntimeCallbacks()->ClassPrepare(temp_klass, klass); |
| 4933 | |
Vladimir Marko | 305c38b | 2018-02-14 11:50:07 +0000 | [diff] [blame] | 4934 | // SubtypeCheckInfo::Initialized must happen-before any new-instance for that type. |
| 4935 | // See also ClassLinker::EnsureInitialized(). |
| 4936 | if (kBitstringSubtypeCheckEnabled) { |
| 4937 | MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_); |
| 4938 | SubtypeCheck<ObjPtr<mirror::Class>>::EnsureInitialized(klass.Get()); |
| 4939 | // TODO: Avoid taking subtype_check_lock_ if SubtypeCheck for j.l.r.Proxy is already assigned. |
| 4940 | } |
| 4941 | |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 4942 | VisiblyInitializedCallback* callback = nullptr; |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 4943 | { |
| 4944 | // Lock on klass is released. Lock new class object. |
| 4945 | ObjectLock<mirror::Class> initialization_lock(self, klass); |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 4946 | // Conservatively go through the ClassStatus::kInitialized state. |
| 4947 | callback = MarkClassInitialized(self, klass); |
| 4948 | } |
| 4949 | if (callback != nullptr) { |
| 4950 | callback->MakeVisible(self); |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 4951 | } |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4952 | |
David Srbecky | 346fd96 | 2020-07-27 16:51:00 +0100 | [diff] [blame] | 4953 | // Consistency checks. |
Elliott Hughes | 67d9200 | 2012-03-26 15:08:51 -0700 | [diff] [blame] | 4954 | if (kIsDebugBuild) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4955 | CHECK(klass->GetIFieldsPtr() == nullptr); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4956 | CheckProxyConstructor(klass->GetDirectMethod(0, image_pointer_size_)); |
| 4957 | |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4958 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4959 | auto* virtual_method = klass->GetVirtualMethodUnchecked(i, image_pointer_size_); |
Alex Light | 133987d | 2020-03-26 19:22:12 +0000 | [diff] [blame] | 4960 | CheckProxyMethod(virtual_method, proxied_methods[i]); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4961 | } |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 4962 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4963 | StackHandleScope<1> hs2(self); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 4964 | Handle<mirror::String> decoded_name = hs2.NewHandle(soa.Decode<mirror::String>(name)); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 4965 | std::string interfaces_field_name(StringPrintf("java.lang.Class[] %s.interfaces", |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4966 | decoded_name->ToModifiedUtf8().c_str())); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4967 | CHECK_EQ(ArtField::PrettyField(klass->GetStaticField(0)), interfaces_field_name); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 4968 | |
| 4969 | std::string throws_field_name(StringPrintf("java.lang.Class[][] %s.throws", |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4970 | decoded_name->ToModifiedUtf8().c_str())); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4971 | CHECK_EQ(ArtField::PrettyField(klass->GetStaticField(1)), throws_field_name); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4972 | |
Narayan Kamath | 6b2dc31 | 2017-03-14 13:26:12 +0000 | [diff] [blame] | 4973 | CHECK_EQ(klass.Get()->GetProxyInterfaces(), |
Mathieu Chartier | f8ac97f | 2016-10-05 15:56:52 -0700 | [diff] [blame] | 4974 | soa.Decode<mirror::ObjectArray<mirror::Class>>(interfaces)); |
Narayan Kamath | 6b2dc31 | 2017-03-14 13:26:12 +0000 | [diff] [blame] | 4975 | CHECK_EQ(klass.Get()->GetProxyThrows(), |
Alex Light | 133987d | 2020-03-26 19:22:12 +0000 | [diff] [blame] | 4976 | proxied_throws.Get()); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4977 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4978 | return klass.Get(); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4979 | } |
| 4980 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4981 | void ClassLinker::CreateProxyConstructor(Handle<mirror::Class> klass, ArtMethod* out) { |
| 4982 | // Create constructor for Proxy that must initialize the method. |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 4983 | ObjPtr<mirror::Class> proxy_class = GetClassRoot<mirror::Proxy>(this); |
| 4984 | CHECK_EQ(proxy_class->NumDirectMethods(), 21u); |
Przemyslaw Szczepaniak | f11cd29 | 2016-08-17 17:46:38 +0100 | [diff] [blame] | 4985 | |
Igor Murashkin | 9d3d752 | 2017-02-27 10:39:49 -0800 | [diff] [blame] | 4986 | // Find the <init>(InvocationHandler)V method. The exact method offset varies depending |
| 4987 | // 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] | 4988 | ArtMethod* proxy_constructor = |
| 4989 | jni::DecodeArtMethod(WellKnownClasses::java_lang_reflect_Proxy_init); |
Igor Murashkin | 9d3d752 | 2017-02-27 10:39:49 -0800 | [diff] [blame] | 4990 | DCHECK(proxy_constructor != nullptr) |
| 4991 | << "Could not find <init> method in java.lang.reflect.Proxy"; |
| 4992 | |
Jeff Hao | db8a664 | 2014-08-14 17:18:52 -0700 | [diff] [blame] | 4993 | // Clone the existing constructor of Proxy (our constructor would just invoke it so steal its |
| 4994 | // code_ too) |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4995 | DCHECK(out != nullptr); |
| 4996 | out->CopyFrom(proxy_constructor, image_pointer_size_); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 4997 | // 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] | 4998 | // 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] | 4999 | // 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] | 5000 | out->SetAccessFlags((out->GetAccessFlags() & ~kAccProtected) | |
| 5001 | kAccPublic | |
| 5002 | kAccCompileDontBother); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5003 | out->SetDeclaringClass(klass.Get()); |
Vladimir Marko | d1ee20f | 2017-08-17 09:21:16 +0000 | [diff] [blame] | 5004 | |
| 5005 | // Set the original constructor method. |
| 5006 | out->SetDataPtrSize(proxy_constructor, image_pointer_size_); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 5007 | } |
| 5008 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5009 | void ClassLinker::CheckProxyConstructor(ArtMethod* constructor) const { |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 5010 | CHECK(constructor->IsConstructor()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5011 | auto* np = constructor->GetInterfaceMethodIfProxy(image_pointer_size_); |
| 5012 | CHECK_STREQ(np->GetName(), "<init>"); |
| 5013 | CHECK_STREQ(np->GetSignature().ToString().c_str(), "(Ljava/lang/reflect/InvocationHandler;)V"); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 5014 | DCHECK(constructor->IsPublic()); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 5015 | } |
| 5016 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5017 | void ClassLinker::CreateProxyMethod(Handle<mirror::Class> klass, ArtMethod* prototype, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5018 | ArtMethod* out) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 5019 | // 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] | 5020 | // as necessary |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5021 | DCHECK(out != nullptr); |
| 5022 | out->CopyFrom(prototype, image_pointer_size_); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 5023 | |
Alex Light | e9dd04f | 2016-03-16 16:09:45 -0700 | [diff] [blame] | 5024 | // Set class to be the concrete proxy class. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5025 | out->SetDeclaringClass(klass.Get()); |
Vladimir Marko | de0d0de | 2021-03-18 14:12:35 +0000 | [diff] [blame] | 5026 | // 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] | 5027 | // preference to the invocation handler. |
Vladimir Marko | de0d0de | 2021-03-18 14:12:35 +0000 | [diff] [blame] | 5028 | const uint32_t kRemoveFlags = kAccAbstract | kAccDefault; |
Alex Light | e9dd04f | 2016-03-16 16:09:45 -0700 | [diff] [blame] | 5029 | // Make the method final. |
Mathieu Chartier | 201e297 | 2017-06-05 18:34:53 -0700 | [diff] [blame] | 5030 | // Mark kAccCompileDontBother so that we don't take JIT samples for the method. b/62349349 |
| 5031 | const uint32_t kAddFlags = kAccFinal | kAccCompileDontBother; |
Alex Light | e9dd04f | 2016-03-16 16:09:45 -0700 | [diff] [blame] | 5032 | out->SetAccessFlags((out->GetAccessFlags() & ~kRemoveFlags) | kAddFlags); |
| 5033 | |
Vladimir Marko | d1ee20f | 2017-08-17 09:21:16 +0000 | [diff] [blame] | 5034 | // Set the original interface method. |
| 5035 | out->SetDataPtrSize(prototype, image_pointer_size_); |
| 5036 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 5037 | // At runtime the method looks like a reference and argument saving method, clone the code |
| 5038 | // related parameters from this method. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5039 | out->SetEntryPointFromQuickCompiledCode(GetQuickProxyInvokeHandler()); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 5040 | } |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 5041 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5042 | void ClassLinker::CheckProxyMethod(ArtMethod* method, ArtMethod* prototype) const { |
David Srbecky | 346fd96 | 2020-07-27 16:51:00 +0100 | [diff] [blame] | 5043 | // Basic consistency checks. |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 5044 | CHECK(!prototype->IsFinal()); |
| 5045 | CHECK(method->IsFinal()); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5046 | CHECK(method->IsInvokable()); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 5047 | |
| 5048 | // The proxy method doesn't have its own dex cache or dex file and so it steals those of its |
| 5049 | // 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] | 5050 | CHECK_EQ(prototype->GetDexMethodIndex(), method->GetDexMethodIndex()); |
Vladimir Marko | 5c3e9d1 | 2017-08-30 16:43:54 +0100 | [diff] [blame] | 5051 | CHECK_EQ(prototype, method->GetInterfaceMethodIfProxy(image_pointer_size_)); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 5052 | } |
| 5053 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5054 | bool ClassLinker::CanWeInitializeClass(ObjPtr<mirror::Class> klass, bool can_init_statics, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5055 | bool can_init_parents) { |
Brian Carlstrom | 610e49f | 2013-11-04 17:07:22 -0800 | [diff] [blame] | 5056 | if (can_init_statics && can_init_parents) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5057 | return true; |
| 5058 | } |
| 5059 | if (!can_init_statics) { |
| 5060 | // Check if there's a class initializer. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5061 | ArtMethod* clinit = klass->FindClassInitializer(image_pointer_size_); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5062 | if (clinit != nullptr) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5063 | return false; |
| 5064 | } |
| 5065 | // Check if there are encoded static values needing initialization. |
| 5066 | if (klass->NumStaticFields() != 0) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 5067 | const dex::ClassDef* dex_class_def = klass->GetClassDef(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5068 | DCHECK(dex_class_def != nullptr); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5069 | if (dex_class_def->static_values_off_ != 0) { |
| 5070 | return false; |
| 5071 | } |
| 5072 | } |
Vladimir Marko | 889b72d | 2019-11-12 11:01:13 +0000 | [diff] [blame] | 5073 | } |
| 5074 | // If we are a class we need to initialize all interfaces with default methods when we are |
| 5075 | // initialized. Check all of them. |
| 5076 | if (!klass->IsInterface()) { |
| 5077 | size_t num_interfaces = klass->GetIfTableCount(); |
| 5078 | for (size_t i = 0; i < num_interfaces; i++) { |
| 5079 | ObjPtr<mirror::Class> iface = klass->GetIfTable()->GetInterface(i); |
| 5080 | if (iface->HasDefaultMethods() && !iface->IsInitialized()) { |
| 5081 | if (!can_init_parents || !CanWeInitializeClass(iface, can_init_statics, can_init_parents)) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5082 | return false; |
| 5083 | } |
| 5084 | } |
| 5085 | } |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5086 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5087 | if (klass->IsInterface() || !klass->HasSuperClass()) { |
| 5088 | return true; |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5089 | } |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5090 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Vladimir Marko | 889b72d | 2019-11-12 11:01:13 +0000 | [diff] [blame] | 5091 | if (super_class->IsInitialized()) { |
| 5092 | return true; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5093 | } |
Vladimir Marko | 889b72d | 2019-11-12 11:01:13 +0000 | [diff] [blame] | 5094 | 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] | 5095 | } |
| 5096 | |
Mathieu Chartier | 2336954 | 2020-03-04 08:24:11 -0800 | [diff] [blame] | 5097 | bool ClassLinker::InitializeClass(Thread* self, |
| 5098 | Handle<mirror::Class> klass, |
| 5099 | bool can_init_statics, |
| 5100 | bool can_init_parents) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5101 | // see JLS 3rd edition, 12.4.2 "Detailed Initialization Procedure" for the locking protocol |
| 5102 | |
| 5103 | // Are we already initialized and therefore done? |
| 5104 | // Note: we differ from the JLS here as we don't do this under the lock, this is benign as |
| 5105 | // an initialized class will never change its state. |
| 5106 | if (klass->IsInitialized()) { |
| 5107 | return true; |
| 5108 | } |
| 5109 | |
| 5110 | // 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] | 5111 | if (!CanWeInitializeClass(klass.Get(), can_init_statics, can_init_parents)) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5112 | return false; |
| 5113 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5114 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5115 | self->AllowThreadSuspension(); |
Mathieu Chartier | 2336954 | 2020-03-04 08:24:11 -0800 | [diff] [blame] | 5116 | Runtime* const runtime = Runtime::Current(); |
| 5117 | const bool stats_enabled = runtime->HasStatsEnabled(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5118 | uint64_t t0; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5119 | { |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 5120 | ObjectLock<mirror::Class> lock(self, klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5121 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5122 | // Re-check under the lock in case another thread initialized ahead of us. |
| 5123 | if (klass->IsInitialized()) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5124 | return true; |
| 5125 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5126 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5127 | // 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] | 5128 | if (klass->IsErroneous()) { |
Andreas Gampe | 7b3063b | 2019-01-07 14:12:52 -0800 | [diff] [blame] | 5129 | ThrowEarlierClassFailure(klass.Get(), true, /* log= */ true); |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 5130 | VlogClassInitializationFailure(klass); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5131 | return false; |
| 5132 | } |
| 5133 | |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 5134 | CHECK(klass->IsResolved() && !klass->IsErroneousResolved()) |
| 5135 | << klass->PrettyClass() << ": state=" << klass->GetStatus(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5136 | |
| 5137 | if (!klass->IsVerified()) { |
Nicolas Geoffray | 5b0b2e1 | 2021-03-19 14:48:40 +0000 | [diff] [blame] | 5138 | VerifyClass(self, /*verifier_deps= */ nullptr, klass); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5139 | if (!klass->IsVerified()) { |
| 5140 | // We failed to verify, expect either the klass to be erroneous or verification failed at |
| 5141 | // compile time. |
| 5142 | if (klass->IsErroneous()) { |
Andreas Gampe | fc49fa0 | 2016-04-21 12:21:55 -0700 | [diff] [blame] | 5143 | // The class is erroneous. This may be a verifier error, or another thread attempted |
| 5144 | // verification and/or initialization and failed. We can distinguish those cases by |
| 5145 | // whether an exception is already pending. |
| 5146 | if (self->IsExceptionPending()) { |
| 5147 | // Check that it's a VerifyError. |
Nicolas Geoffray | 4dc6589 | 2021-07-05 17:43:35 +0100 | [diff] [blame] | 5148 | DCHECK(IsVerifyError(self->GetException())); |
Andreas Gampe | fc49fa0 | 2016-04-21 12:21:55 -0700 | [diff] [blame] | 5149 | } else { |
| 5150 | // Check that another thread attempted initialization. |
| 5151 | DCHECK_NE(0, klass->GetClinitThreadId()); |
| 5152 | DCHECK_NE(self->GetTid(), klass->GetClinitThreadId()); |
| 5153 | // Need to rethrow the previous failure now. |
| 5154 | ThrowEarlierClassFailure(klass.Get(), true); |
| 5155 | } |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 5156 | VlogClassInitializationFailure(klass); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5157 | } else { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 5158 | CHECK(Runtime::Current()->IsAotCompiler()); |
Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 5159 | CHECK(klass->ShouldVerifyAtRuntime() || klass->IsVerifiedNeedsAccessChecks()); |
Vladimir Marko | d79b37b | 2018-11-02 13:06:22 +0000 | [diff] [blame] | 5160 | self->AssertNoPendingException(); |
| 5161 | self->SetException(Runtime::Current()->GetPreAllocatedNoClassDefFoundError()); |
jeffhao | a9b3bf4 | 2012-06-06 17:18:39 -0700 | [diff] [blame] | 5162 | } |
Vladimir Marko | d79b37b | 2018-11-02 13:06:22 +0000 | [diff] [blame] | 5163 | self->AssertPendingException(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5164 | return false; |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 5165 | } else { |
| 5166 | self->AssertNoPendingException(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5167 | } |
Andreas Gampe | fc49fa0 | 2016-04-21 12:21:55 -0700 | [diff] [blame] | 5168 | |
| 5169 | // A separate thread could have moved us all the way to initialized. A "simple" example |
| 5170 | // involves a subclass of the current class being initialized at the same time (which |
| 5171 | // will implicitly initialize the superclass, if scheduled that way). b/28254258 |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 5172 | DCHECK(!klass->IsErroneous()) << klass->GetStatus(); |
Andreas Gampe | fc49fa0 | 2016-04-21 12:21:55 -0700 | [diff] [blame] | 5173 | if (klass->IsInitialized()) { |
| 5174 | return true; |
| 5175 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5176 | } |
| 5177 | |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5178 | // If the class is ClassStatus::kInitializing, either this thread is |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5179 | // initializing higher up the stack or another thread has beat us |
| 5180 | // to initializing and we need to wait. Either way, this |
| 5181 | // invocation of InitializeClass will not be responsible for |
| 5182 | // running <clinit> and will return. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5183 | if (klass->GetStatus() == ClassStatus::kInitializing) { |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 5184 | // Could have got an exception during verification. |
| 5185 | if (self->IsExceptionPending()) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 5186 | VlogClassInitializationFailure(klass); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 5187 | return false; |
| 5188 | } |
Elliott Hughes | 005ab2e | 2011-09-11 17:15:31 -0700 | [diff] [blame] | 5189 | // We caught somebody else in the act; was it us? |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 5190 | if (klass->GetClinitThreadId() == self->GetTid()) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5191 | // Yes. That's fine. Return so we can continue initializing. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5192 | return true; |
| 5193 | } |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5194 | // No. That's fine. Wait for another thread to finish initializing. |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5195 | return WaitForInitializeClass(klass, self, lock); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5196 | } |
| 5197 | |
Jeff Hao | e2e4034 | 2017-07-19 10:45:18 -0700 | [diff] [blame] | 5198 | // Try to get the oat class's status for this class if the oat file is present. The compiler |
| 5199 | // tries to validate superclass descriptors, and writes the result into the oat file. |
| 5200 | // Runtime correctness is guaranteed by classpath checks done on loading. If the classpath |
| 5201 | // is different at runtime than it was at compile time, the oat file is rejected. So if the |
| 5202 | // 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] | 5203 | bool has_oat_class = false; |
Jeff Hao | e2e4034 | 2017-07-19 10:45:18 -0700 | [diff] [blame] | 5204 | const OatFile::OatClass oat_class = (runtime->IsStarted() && !runtime->IsAotCompiler()) |
| 5205 | ? OatFile::FindOatClass(klass->GetDexFile(), klass->GetDexClassDefIndex(), &has_oat_class) |
| 5206 | : OatFile::OatClass::Invalid(); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5207 | if (oat_class.GetStatus() < ClassStatus::kSuperclassValidated && |
Jeff Hao | 0cb1728 | 2017-07-12 14:51:49 -0700 | [diff] [blame] | 5208 | !ValidateSuperClassDescriptors(klass)) { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5209 | mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5210 | return false; |
| 5211 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5212 | self->AllowThreadSuspension(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5213 | |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5214 | CHECK_EQ(klass->GetStatus(), ClassStatus::kVerified) << klass->PrettyClass() |
Andreas Gampe | 9510ccd | 2016-04-20 09:55:25 -0700 | [diff] [blame] | 5215 | << " self.tid=" << self->GetTid() << " clinit.tid=" << klass->GetClinitThreadId(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5216 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5217 | // From here out other threads may observe that we're initializing and so changes of state |
| 5218 | // require the a notification. |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 5219 | klass->SetClinitThreadId(self->GetTid()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5220 | mirror::Class::SetStatus(klass, ClassStatus::kInitializing, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5221 | |
Mathieu Chartier | 2336954 | 2020-03-04 08:24:11 -0800 | [diff] [blame] | 5222 | t0 = stats_enabled ? NanoTime() : 0u; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5223 | } |
| 5224 | |
Andreas Gampe | af86470 | 2019-07-23 14:05:35 -0700 | [diff] [blame] | 5225 | uint64_t t_sub = 0; |
| 5226 | |
Brian Carlstrom | 6d3f72c | 2013-08-21 18:06:34 -0700 | [diff] [blame] | 5227 | // Initialize super classes, must be done while initializing for the JLS. |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5228 | if (!klass->IsInterface() && klass->HasSuperClass()) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5229 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5230 | if (!super_class->IsInitialized()) { |
| 5231 | CHECK(!super_class->IsInterface()); |
| 5232 | CHECK(can_init_parents); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5233 | StackHandleScope<1> hs(self); |
| 5234 | Handle<mirror::Class> handle_scope_super(hs.NewHandle(super_class)); |
Mathieu Chartier | 2336954 | 2020-03-04 08:24:11 -0800 | [diff] [blame] | 5235 | uint64_t super_t0 = stats_enabled ? NanoTime() : 0u; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5236 | bool super_initialized = InitializeClass(self, handle_scope_super, can_init_statics, true); |
Mathieu Chartier | 2336954 | 2020-03-04 08:24:11 -0800 | [diff] [blame] | 5237 | uint64_t super_t1 = stats_enabled ? NanoTime() : 0u; |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5238 | if (!super_initialized) { |
| 5239 | // The super class was verified ahead of entering initializing, we should only be here if |
| 5240 | // the super class became erroneous due to initialization. |
Chang Xing | adbb91c | 2017-07-17 11:23:55 -0700 | [diff] [blame] | 5241 | // For the case of aot compiler, the super class might also be initializing but we don't |
| 5242 | // want to process circular dependencies in pre-compile. |
| 5243 | CHECK(self->IsExceptionPending()) |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 5244 | << "Super class initialization failed for " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5245 | << handle_scope_super->PrettyDescriptor() |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5246 | << " that has unexpected status " << handle_scope_super->GetStatus() |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5247 | << "\nPending exception:\n" |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 5248 | << (self->GetException() != nullptr ? self->GetException()->Dump() : ""); |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 5249 | ObjectLock<mirror::Class> lock(self, klass); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5250 | // Initialization failed because the super-class is erroneous. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5251 | mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5252 | return false; |
| 5253 | } |
Andreas Gampe | af86470 | 2019-07-23 14:05:35 -0700 | [diff] [blame] | 5254 | t_sub = super_t1 - super_t0; |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 5255 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5256 | } |
| 5257 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5258 | if (!klass->IsInterface()) { |
| 5259 | // Initialize interfaces with default methods for the JLS. |
| 5260 | size_t num_direct_interfaces = klass->NumDirectInterfaces(); |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 5261 | // Only setup the (expensive) handle scope if we actually need to. |
| 5262 | if (UNLIKELY(num_direct_interfaces > 0)) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5263 | StackHandleScope<1> hs_iface(self); |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 5264 | MutableHandle<mirror::Class> handle_scope_iface(hs_iface.NewHandle<mirror::Class>(nullptr)); |
| 5265 | for (size_t i = 0; i < num_direct_interfaces; i++) { |
Vladimir Marko | b10668c | 2021-06-10 09:52:53 +0100 | [diff] [blame] | 5266 | handle_scope_iface.Assign(klass->GetDirectInterface(i)); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 5267 | CHECK(handle_scope_iface != nullptr) << klass->PrettyDescriptor() << " iface #" << i; |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 5268 | CHECK(handle_scope_iface->IsInterface()); |
| 5269 | if (handle_scope_iface->HasBeenRecursivelyInitialized()) { |
| 5270 | // We have already done this for this interface. Skip it. |
| 5271 | continue; |
| 5272 | } |
| 5273 | // We cannot just call initialize class directly because we need to ensure that ALL |
| 5274 | // interfaces with default methods are initialized. Non-default interface initialization |
| 5275 | // will not affect other non-default super-interfaces. |
Mathieu Chartier | 2336954 | 2020-03-04 08:24:11 -0800 | [diff] [blame] | 5276 | // This is not very precise, misses all walking. |
| 5277 | uint64_t inf_t0 = stats_enabled ? NanoTime() : 0u; |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 5278 | bool iface_initialized = InitializeDefaultInterfaceRecursive(self, |
| 5279 | handle_scope_iface, |
| 5280 | can_init_statics, |
| 5281 | can_init_parents); |
Mathieu Chartier | 2336954 | 2020-03-04 08:24:11 -0800 | [diff] [blame] | 5282 | uint64_t inf_t1 = stats_enabled ? NanoTime() : 0u; |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 5283 | if (!iface_initialized) { |
| 5284 | ObjectLock<mirror::Class> lock(self, klass); |
| 5285 | // Initialization failed because one of our interfaces with default methods is erroneous. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5286 | mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self); |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 5287 | return false; |
| 5288 | } |
Andreas Gampe | af86470 | 2019-07-23 14:05:35 -0700 | [diff] [blame] | 5289 | t_sub += inf_t1 - inf_t0; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5290 | } |
| 5291 | } |
| 5292 | } |
| 5293 | |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 5294 | const size_t num_static_fields = klass->NumStaticFields(); |
| 5295 | if (num_static_fields > 0) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 5296 | const dex::ClassDef* dex_class_def = klass->GetClassDef(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5297 | CHECK(dex_class_def != nullptr); |
Hiroshi Yamauchi | 67ef46a | 2014-08-21 15:59:43 -0700 | [diff] [blame] | 5298 | StackHandleScope<3> hs(self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5299 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader())); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 5300 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache())); |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 5301 | |
| 5302 | // Eagerly fill in static fields so that the we don't have to do as many expensive |
| 5303 | // Class::FindStaticField in ResolveField. |
| 5304 | for (size_t i = 0; i < num_static_fields; ++i) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 5305 | ArtField* field = klass->GetStaticField(i); |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 5306 | const uint32_t field_idx = field->GetDexFieldIndex(); |
David Srbecky | 5de5efe | 2021-02-15 21:23:00 +0000 | [diff] [blame] | 5307 | ArtField* resolved_field = dex_cache->GetResolvedField(field_idx); |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 5308 | if (resolved_field == nullptr) { |
David Brazdil | 1ab0fa8 | 2018-05-04 11:28:03 +0100 | [diff] [blame] | 5309 | // 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] | 5310 | DCHECK(!hiddenapi::ShouldDenyAccessToMember( |
| 5311 | field, |
| 5312 | hiddenapi::AccessContext(class_loader.Get(), dex_cache.Get()), |
| 5313 | hiddenapi::AccessMethod::kNone)); |
David Srbecky | 5de5efe | 2021-02-15 21:23:00 +0000 | [diff] [blame] | 5314 | dex_cache->SetResolvedField(field_idx, field); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5315 | } else { |
| 5316 | DCHECK_EQ(field, resolved_field); |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 5317 | } |
| 5318 | } |
| 5319 | |
Vladimir Marko | e11dd50 | 2017-12-08 14:09:45 +0000 | [diff] [blame] | 5320 | annotations::RuntimeEncodedStaticFieldValueIterator value_it(dex_cache, |
| 5321 | class_loader, |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 5322 | this, |
| 5323 | *dex_class_def); |
Vladimir Marko | e11dd50 | 2017-12-08 14:09:45 +0000 | [diff] [blame] | 5324 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 5325 | |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 5326 | if (value_it.HasNext()) { |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 5327 | ClassAccessor accessor(dex_file, *dex_class_def); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5328 | CHECK(can_init_statics); |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 5329 | for (const ClassAccessor::Field& field : accessor.GetStaticFields()) { |
| 5330 | if (!value_it.HasNext()) { |
| 5331 | break; |
| 5332 | } |
| 5333 | ArtField* art_field = ResolveField(field.GetIndex(), |
| 5334 | dex_cache, |
| 5335 | class_loader, |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 5336 | /* is_static= */ true); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 5337 | if (Runtime::Current()->IsActiveTransaction()) { |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 5338 | value_it.ReadValueToField<true>(art_field); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 5339 | } else { |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 5340 | value_it.ReadValueToField<false>(art_field); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 5341 | } |
Mathieu Chartier | da595be | 2016-08-10 13:57:39 -0700 | [diff] [blame] | 5342 | if (self->IsExceptionPending()) { |
| 5343 | break; |
| 5344 | } |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 5345 | value_it.Next(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5346 | } |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 5347 | DCHECK(self->IsExceptionPending() || !value_it.HasNext()); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5348 | } |
| 5349 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5350 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5351 | |
Mathieu Chartier | da595be | 2016-08-10 13:57:39 -0700 | [diff] [blame] | 5352 | if (!self->IsExceptionPending()) { |
| 5353 | ArtMethod* clinit = klass->FindClassInitializer(image_pointer_size_); |
| 5354 | if (clinit != nullptr) { |
| 5355 | CHECK(can_init_statics); |
| 5356 | JValue result; |
| 5357 | clinit->Invoke(self, nullptr, 0, &result, "V"); |
| 5358 | } |
| 5359 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5360 | self->AllowThreadSuspension(); |
Mathieu Chartier | 2336954 | 2020-03-04 08:24:11 -0800 | [diff] [blame] | 5361 | uint64_t t1 = stats_enabled ? NanoTime() : 0u; |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 5362 | |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 5363 | VisiblyInitializedCallback* callback = nullptr; |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 5364 | bool success = true; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5365 | { |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 5366 | ObjectLock<mirror::Class> lock(self, klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5367 | |
| 5368 | if (self->IsExceptionPending()) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 5369 | WrapExceptionInInitializer(klass); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5370 | mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self); |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 5371 | success = false; |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 5372 | } else if (Runtime::Current()->IsTransactionAborted()) { |
| 5373 | // The exception thrown when the transaction aborted has been caught and cleared |
| 5374 | // so we need to throw it again now. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5375 | VLOG(compiler) << "Return from class initializer of " |
| 5376 | << mirror::Class::PrettyDescriptor(klass.Get()) |
Sebastien Hertz | bd9cf9f | 2015-03-03 12:16:13 +0100 | [diff] [blame] | 5377 | << " without exception while transaction was aborted: re-throw it now."; |
Mathieu Chartier | 2336954 | 2020-03-04 08:24:11 -0800 | [diff] [blame] | 5378 | runtime->ThrowTransactionAbortError(self); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5379 | mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self); |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 5380 | success = false; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5381 | } else { |
Mathieu Chartier | 2336954 | 2020-03-04 08:24:11 -0800 | [diff] [blame] | 5382 | if (stats_enabled) { |
| 5383 | RuntimeStats* global_stats = runtime->GetStats(); |
| 5384 | RuntimeStats* thread_stats = self->GetStats(); |
| 5385 | ++global_stats->class_init_count; |
| 5386 | ++thread_stats->class_init_count; |
| 5387 | global_stats->class_init_time_ns += (t1 - t0 - t_sub); |
| 5388 | thread_stats->class_init_time_ns += (t1 - t0 - t_sub); |
| 5389 | } |
Ian Rogers | e6bb3b2 | 2013-08-19 21:51:45 -0700 | [diff] [blame] | 5390 | // Set the class as initialized except if failed to initialize static fields. |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 5391 | callback = MarkClassInitialized(self, klass); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5392 | if (VLOG_IS_ON(class_linker)) { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 5393 | std::string temp; |
| 5394 | LOG(INFO) << "Initialized class " << klass->GetDescriptor(&temp) << " from " << |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 5395 | klass->GetLocation(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 5396 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5397 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5398 | } |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 5399 | if (callback != nullptr) { |
| 5400 | callback->MakeVisible(self); |
| 5401 | } |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 5402 | return success; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5403 | } |
| 5404 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5405 | // We recursively run down the tree of interfaces. We need to do this in the order they are declared |
| 5406 | // and perform the initialization only on those interfaces that contain default methods. |
| 5407 | bool ClassLinker::InitializeDefaultInterfaceRecursive(Thread* self, |
| 5408 | Handle<mirror::Class> iface, |
| 5409 | bool can_init_statics, |
| 5410 | bool can_init_parents) { |
| 5411 | CHECK(iface->IsInterface()); |
| 5412 | size_t num_direct_ifaces = iface->NumDirectInterfaces(); |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 5413 | // Only create the (expensive) handle scope if we need it. |
| 5414 | if (UNLIKELY(num_direct_ifaces > 0)) { |
| 5415 | StackHandleScope<1> hs(self); |
| 5416 | MutableHandle<mirror::Class> handle_super_iface(hs.NewHandle<mirror::Class>(nullptr)); |
| 5417 | // First we initialize all of iface's super-interfaces recursively. |
| 5418 | for (size_t i = 0; i < num_direct_ifaces; i++) { |
Vladimir Marko | b10668c | 2021-06-10 09:52:53 +0100 | [diff] [blame] | 5419 | ObjPtr<mirror::Class> super_iface = iface->GetDirectInterface(i); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 5420 | CHECK(super_iface != nullptr) << iface->PrettyDescriptor() << " iface #" << i; |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 5421 | if (!super_iface->HasBeenRecursivelyInitialized()) { |
| 5422 | // Recursive step |
| 5423 | handle_super_iface.Assign(super_iface); |
| 5424 | if (!InitializeDefaultInterfaceRecursive(self, |
| 5425 | handle_super_iface, |
| 5426 | can_init_statics, |
| 5427 | can_init_parents)) { |
| 5428 | return false; |
| 5429 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5430 | } |
| 5431 | } |
| 5432 | } |
| 5433 | |
| 5434 | bool result = true; |
| 5435 | // Then we initialize 'iface' if it has default methods. We do not need to (and in fact must not) |
| 5436 | // initialize if we don't have default methods. |
| 5437 | if (iface->HasDefaultMethods()) { |
| 5438 | result = EnsureInitialized(self, iface, can_init_statics, can_init_parents); |
| 5439 | } |
| 5440 | |
| 5441 | // Mark that this interface has undergone recursive default interface initialization so we know we |
| 5442 | // can skip it on any later class initializations. We do this even if we are not a default |
| 5443 | // interface since we can still avoid the traversal. This is purely a performance optimization. |
| 5444 | if (result) { |
| 5445 | // TODO This should be done in a better way |
Andreas Gampe | 976b298 | 2018-03-02 17:54:22 -0800 | [diff] [blame] | 5446 | // Note: Use a try-lock to avoid blocking when someone else is holding the lock on this |
| 5447 | // interface. It is bad (Java) style, but not impossible. Marking the recursive |
| 5448 | // initialization is a performance optimization (to avoid another idempotent visit |
| 5449 | // for other implementing classes/interfaces), and can be revisited later. |
| 5450 | ObjectTryLock<mirror::Class> lock(self, iface); |
| 5451 | if (lock.Acquired()) { |
| 5452 | iface->SetRecursivelyInitialized(); |
| 5453 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5454 | } |
| 5455 | return result; |
| 5456 | } |
| 5457 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5458 | bool ClassLinker::WaitForInitializeClass(Handle<mirror::Class> klass, |
| 5459 | Thread* self, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5460 | ObjectLock<mirror::Class>& lock) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5461 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5462 | while (true) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 5463 | self->AssertNoPendingException(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5464 | CHECK(!klass->IsInitialized()); |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5465 | lock.WaitIgnoringInterrupts(); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5466 | |
| 5467 | // When we wake up, repeat the test for init-in-progress. If |
| 5468 | // there's an exception pending (only possible if |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 5469 | // we were not using WaitIgnoringInterrupts), bail out. |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5470 | if (self->IsExceptionPending()) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 5471 | WrapExceptionInInitializer(klass); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5472 | mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5473 | return false; |
| 5474 | } |
| 5475 | // Spurious wakeup? Go back to waiting. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5476 | if (klass->GetStatus() == ClassStatus::kInitializing) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5477 | continue; |
| 5478 | } |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5479 | if (klass->GetStatus() == ClassStatus::kVerified && |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 5480 | Runtime::Current()->IsAotCompiler()) { |
Ian Rogers | 3d1548d | 2012-09-24 14:08:03 -0700 | [diff] [blame] | 5481 | // Compile time initialization failed. |
| 5482 | return false; |
| 5483 | } |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5484 | if (klass->IsErroneous()) { |
| 5485 | // The caller wants an exception, but it was thrown in a |
| 5486 | // different thread. Synthesize one here. |
Brian Carlstrom | df14324 | 2011-10-10 18:05:34 -0700 | [diff] [blame] | 5487 | ThrowNoClassDefFoundError("<clinit> failed for class %s; see exception in other thread", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5488 | klass->PrettyDescriptor().c_str()); |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 5489 | VlogClassInitializationFailure(klass); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5490 | return false; |
| 5491 | } |
| 5492 | if (klass->IsInitialized()) { |
| 5493 | return true; |
| 5494 | } |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5495 | LOG(FATAL) << "Unexpected class status. " << klass->PrettyClass() << " is " |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 5496 | << klass->GetStatus(); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5497 | } |
Ian Rogers | 0714083 | 2014-09-30 15:43:59 -0700 | [diff] [blame] | 5498 | UNREACHABLE(); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5499 | } |
| 5500 | |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5501 | static void ThrowSignatureCheckResolveReturnTypeException(Handle<mirror::Class> klass, |
| 5502 | Handle<mirror::Class> super_klass, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5503 | ArtMethod* method, |
| 5504 | ArtMethod* m) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5505 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5506 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 5507 | DCHECK(!m->IsProxyMethod()); |
| 5508 | const DexFile* dex_file = m->GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 5509 | const dex::MethodId& method_id = dex_file->GetMethodId(m->GetDexMethodIndex()); |
| 5510 | const dex::ProtoId& proto_id = dex_file->GetMethodPrototype(method_id); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 5511 | dex::TypeIndex return_type_idx = proto_id.return_type_idx_; |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5512 | std::string return_type = dex_file->PrettyType(return_type_idx); |
| 5513 | std::string class_loader = mirror::Object::PrettyTypeOf(m->GetDeclaringClass()->GetClassLoader()); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5514 | ThrowWrappedLinkageError(klass.Get(), |
| 5515 | "While checking class %s method %s signature against %s %s: " |
| 5516 | "Failed to resolve return type %s with %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5517 | mirror::Class::PrettyDescriptor(klass.Get()).c_str(), |
| 5518 | ArtMethod::PrettyMethod(method).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5519 | super_klass->IsInterface() ? "interface" : "superclass", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5520 | mirror::Class::PrettyDescriptor(super_klass.Get()).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5521 | return_type.c_str(), class_loader.c_str()); |
| 5522 | } |
| 5523 | |
| 5524 | static void ThrowSignatureCheckResolveArgException(Handle<mirror::Class> klass, |
| 5525 | Handle<mirror::Class> super_klass, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5526 | ArtMethod* method, |
| 5527 | ArtMethod* m, |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5528 | uint32_t index, |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 5529 | dex::TypeIndex arg_type_idx) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5530 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5531 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 5532 | DCHECK(!m->IsProxyMethod()); |
| 5533 | const DexFile* dex_file = m->GetDexFile(); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5534 | std::string arg_type = dex_file->PrettyType(arg_type_idx); |
| 5535 | std::string class_loader = mirror::Object::PrettyTypeOf(m->GetDeclaringClass()->GetClassLoader()); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5536 | ThrowWrappedLinkageError(klass.Get(), |
| 5537 | "While checking class %s method %s signature against %s %s: " |
| 5538 | "Failed to resolve arg %u type %s with %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5539 | mirror::Class::PrettyDescriptor(klass.Get()).c_str(), |
| 5540 | ArtMethod::PrettyMethod(method).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5541 | super_klass->IsInterface() ? "interface" : "superclass", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5542 | mirror::Class::PrettyDescriptor(super_klass.Get()).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5543 | index, arg_type.c_str(), class_loader.c_str()); |
| 5544 | } |
| 5545 | |
| 5546 | static void ThrowSignatureMismatch(Handle<mirror::Class> klass, |
| 5547 | Handle<mirror::Class> super_klass, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5548 | ArtMethod* method, |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5549 | const std::string& error_msg) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5550 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5551 | ThrowLinkageError(klass.Get(), |
| 5552 | "Class %s method %s resolves differently in %s %s: %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5553 | mirror::Class::PrettyDescriptor(klass.Get()).c_str(), |
| 5554 | ArtMethod::PrettyMethod(method).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5555 | super_klass->IsInterface() ? "interface" : "superclass", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5556 | mirror::Class::PrettyDescriptor(super_klass.Get()).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5557 | error_msg.c_str()); |
| 5558 | } |
| 5559 | |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5560 | static bool HasSameSignatureWithDifferentClassLoaders(Thread* self, |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5561 | Handle<mirror::Class> klass, |
| 5562 | Handle<mirror::Class> super_klass, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5563 | ArtMethod* method1, |
| 5564 | ArtMethod* method2) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5565 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5566 | { |
| 5567 | StackHandleScope<1> hs(self); |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 5568 | Handle<mirror::Class> return_type(hs.NewHandle(method1->ResolveReturnType())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 5569 | if (UNLIKELY(return_type == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5570 | ThrowSignatureCheckResolveReturnTypeException(klass, super_klass, method1, method1); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5571 | return false; |
| 5572 | } |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 5573 | ObjPtr<mirror::Class> other_return_type = method2->ResolveReturnType(); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5574 | if (UNLIKELY(other_return_type == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5575 | ThrowSignatureCheckResolveReturnTypeException(klass, super_klass, method1, method2); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5576 | return false; |
| 5577 | } |
Vladimir Marko | 862f43c | 2015-02-10 18:22:57 +0000 | [diff] [blame] | 5578 | if (UNLIKELY(other_return_type != return_type.Get())) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5579 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 5580 | StringPrintf("Return types mismatch: %s(%p) vs %s(%p)", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5581 | return_type->PrettyClassAndClassLoader().c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5582 | return_type.Get(), |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5583 | other_return_type->PrettyClassAndClassLoader().c_str(), |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5584 | other_return_type.Ptr())); |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5585 | return false; |
| 5586 | } |
| 5587 | } |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 5588 | const dex::TypeList* types1 = method1->GetParameterTypeList(); |
| 5589 | const dex::TypeList* types2 = method2->GetParameterTypeList(); |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5590 | if (types1 == nullptr) { |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5591 | if (types2 != nullptr && types2->Size() != 0) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5592 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 5593 | StringPrintf("Type list mismatch with %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5594 | method2->PrettyMethod(true).c_str())); |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5595 | return false; |
| 5596 | } |
| 5597 | return true; |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5598 | } else if (UNLIKELY(types2 == nullptr)) { |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5599 | if (types1->Size() != 0) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5600 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 5601 | StringPrintf("Type list mismatch with %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5602 | method2->PrettyMethod(true).c_str())); |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5603 | return false; |
| 5604 | } |
| 5605 | return true; |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5606 | } |
| 5607 | uint32_t num_types = types1->Size(); |
| 5608 | if (UNLIKELY(num_types != types2->Size())) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5609 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 5610 | StringPrintf("Type list mismatch with %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5611 | method2->PrettyMethod(true).c_str())); |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5612 | return false; |
| 5613 | } |
| 5614 | for (uint32_t i = 0; i < num_types; ++i) { |
Vladimir Marko | 862f43c | 2015-02-10 18:22:57 +0000 | [diff] [blame] | 5615 | StackHandleScope<1> hs(self); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 5616 | dex::TypeIndex param_type_idx = types1->GetTypeItem(i).type_idx_; |
Vladimir Marko | 862f43c | 2015-02-10 18:22:57 +0000 | [diff] [blame] | 5617 | Handle<mirror::Class> param_type(hs.NewHandle( |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 5618 | method1->ResolveClassFromTypeIndex(param_type_idx))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 5619 | if (UNLIKELY(param_type == nullptr)) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5620 | ThrowSignatureCheckResolveArgException(klass, super_klass, method1, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5621 | method1, i, param_type_idx); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5622 | return false; |
| 5623 | } |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 5624 | dex::TypeIndex other_param_type_idx = types2->GetTypeItem(i).type_idx_; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5625 | ObjPtr<mirror::Class> other_param_type = |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 5626 | method2->ResolveClassFromTypeIndex(other_param_type_idx); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5627 | if (UNLIKELY(other_param_type == nullptr)) { |
| 5628 | ThrowSignatureCheckResolveArgException(klass, super_klass, method1, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5629 | method2, i, other_param_type_idx); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5630 | return false; |
| 5631 | } |
Vladimir Marko | 862f43c | 2015-02-10 18:22:57 +0000 | [diff] [blame] | 5632 | if (UNLIKELY(param_type.Get() != other_param_type)) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5633 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 5634 | StringPrintf("Parameter %u type mismatch: %s(%p) vs %s(%p)", |
| 5635 | i, |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5636 | param_type->PrettyClassAndClassLoader().c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5637 | param_type.Get(), |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5638 | other_param_type->PrettyClassAndClassLoader().c_str(), |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5639 | other_param_type.Ptr())); |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5640 | return false; |
| 5641 | } |
| 5642 | } |
| 5643 | return true; |
| 5644 | } |
| 5645 | |
| 5646 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5647 | bool ClassLinker::ValidateSuperClassDescriptors(Handle<mirror::Class> klass) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5648 | if (klass->IsInterface()) { |
| 5649 | return true; |
| 5650 | } |
Ian Rogers | 151f221 | 2014-05-06 11:27:27 -0700 | [diff] [blame] | 5651 | // Begin with the methods local to the superclass. |
Ian Rogers | ded66a0 | 2014-10-28 18:12:55 -0700 | [diff] [blame] | 5652 | Thread* self = Thread::Current(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5653 | StackHandleScope<1> hs(self); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5654 | MutableHandle<mirror::Class> super_klass(hs.NewHandle<mirror::Class>(nullptr)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5655 | if (klass->HasSuperClass() && |
| 5656 | klass->GetClassLoader() != klass->GetSuperClass()->GetClassLoader()) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5657 | super_klass.Assign(klass->GetSuperClass()); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 5658 | for (int i = klass->GetSuperClass()->GetVTableLength() - 1; i >= 0; --i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5659 | auto* m = klass->GetVTableEntry(i, image_pointer_size_); |
| 5660 | auto* super_m = klass->GetSuperClass()->GetVTableEntry(i, image_pointer_size_); |
| 5661 | if (m != super_m) { |
Vladimir Marko | 942fd31 | 2017-01-16 20:52:19 +0000 | [diff] [blame] | 5662 | if (UNLIKELY(!HasSameSignatureWithDifferentClassLoaders(self, |
| 5663 | klass, |
| 5664 | super_klass, |
| 5665 | m, |
| 5666 | super_m))) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5667 | self->AssertPendingException(); |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5668 | return false; |
| 5669 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5670 | } |
| 5671 | } |
| 5672 | } |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 5673 | for (int32_t i = 0; i < klass->GetIfTableCount(); ++i) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5674 | super_klass.Assign(klass->GetIfTable()->GetInterface(i)); |
| 5675 | if (klass->GetClassLoader() != super_klass->GetClassLoader()) { |
| 5676 | uint32_t num_methods = super_klass->NumVirtualMethods(); |
Ian Rogers | 151f221 | 2014-05-06 11:27:27 -0700 | [diff] [blame] | 5677 | for (uint32_t j = 0; j < num_methods; ++j) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5678 | auto* m = klass->GetIfTable()->GetMethodArray(i)->GetElementPtrSize<ArtMethod*>( |
| 5679 | j, image_pointer_size_); |
| 5680 | auto* super_m = super_klass->GetVirtualMethod(j, image_pointer_size_); |
| 5681 | if (m != super_m) { |
Vladimir Marko | 942fd31 | 2017-01-16 20:52:19 +0000 | [diff] [blame] | 5682 | if (UNLIKELY(!HasSameSignatureWithDifferentClassLoaders(self, |
| 5683 | klass, |
| 5684 | super_klass, |
| 5685 | m, |
| 5686 | super_m))) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5687 | self->AssertPendingException(); |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5688 | return false; |
| 5689 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5690 | } |
| 5691 | } |
| 5692 | } |
| 5693 | } |
| 5694 | return true; |
| 5695 | } |
| 5696 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5697 | bool ClassLinker::EnsureInitialized(Thread* self, |
| 5698 | Handle<mirror::Class> c, |
| 5699 | bool can_init_fields, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5700 | bool can_init_parents) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 5701 | DCHECK(c != nullptr); |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 5702 | |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 5703 | if (c->IsInitialized()) { |
Vladimir Marko | 8e11065 | 2019-07-30 10:14:41 +0100 | [diff] [blame] | 5704 | // If we've seen an initialized but not visibly initialized class |
| 5705 | // many times, request visible initialization. |
| 5706 | if (kRuntimeISA == InstructionSet::kX86 || kRuntimeISA == InstructionSet::kX86_64) { |
| 5707 | // Thanks to the x86 memory model classes skip the initialized status. |
| 5708 | DCHECK(c->IsVisiblyInitialized()); |
| 5709 | } else if (UNLIKELY(!c->IsVisiblyInitialized())) { |
| 5710 | if (self->IncrementMakeVisiblyInitializedCounter()) { |
| 5711 | MakeInitializedClassesVisiblyInitialized(self, /*wait=*/ false); |
| 5712 | } |
| 5713 | } |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 5714 | return true; |
| 5715 | } |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 5716 | // SubtypeCheckInfo::Initialized must happen-before any new-instance for that type. |
| 5717 | // |
| 5718 | // Ensure the bitstring is initialized before any of the class initialization |
| 5719 | // logic occurs. Once a class initializer starts running, objects can |
| 5720 | // escape into the heap and use the subtype checking code. |
| 5721 | // |
| 5722 | // Note: A class whose SubtypeCheckInfo is at least Initialized means it |
| 5723 | // can be used as a source for the IsSubClass check, and that all ancestors |
| 5724 | // of the class are Assigned (can be used as a target for IsSubClass check) |
| 5725 | // or Overflowed (can be used as a source for IsSubClass check). |
Vladimir Marko | 305c38b | 2018-02-14 11:50:07 +0000 | [diff] [blame] | 5726 | if (kBitstringSubtypeCheckEnabled) { |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 5727 | MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_); |
Vladimir Marko | 38b8b25 | 2018-01-02 19:07:06 +0000 | [diff] [blame] | 5728 | SubtypeCheck<ObjPtr<mirror::Class>>::EnsureInitialized(c.Get()); |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 5729 | // TODO: Avoid taking subtype_check_lock_ if SubtypeCheck is already initialized. |
| 5730 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5731 | const bool success = InitializeClass(self, c, can_init_fields, can_init_parents); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 5732 | if (!success) { |
| 5733 | if (can_init_fields && can_init_parents) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5734 | CHECK(self->IsExceptionPending()) << c->PrettyClass(); |
Vladimir Marko | ac57691 | 2021-03-31 11:16:22 +0100 | [diff] [blame] | 5735 | } else { |
| 5736 | // There may or may not be an exception pending. If there is, clear it. |
| 5737 | // We propagate the exception only if we can initialize fields and parents. |
| 5738 | self->ClearException(); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 5739 | } |
| 5740 | } else { |
| 5741 | self->AssertNoPendingException(); |
Ian Rogers | 595799e | 2012-01-11 17:32:51 -0800 | [diff] [blame] | 5742 | } |
| 5743 | return success; |
Elliott Hughes | f4c21c9 | 2011-08-19 17:31:31 -0700 | [diff] [blame] | 5744 | } |
| 5745 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5746 | void ClassLinker::FixupTemporaryDeclaringClass(ObjPtr<mirror::Class> temp_class, |
| 5747 | ObjPtr<mirror::Class> new_class) { |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 5748 | DCHECK_EQ(temp_class->NumInstanceFields(), 0u); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 5749 | for (ArtField& field : new_class->GetIFields()) { |
| 5750 | if (field.GetDeclaringClass() == temp_class) { |
| 5751 | field.SetDeclaringClass(new_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5752 | } |
| 5753 | } |
| 5754 | |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 5755 | DCHECK_EQ(temp_class->NumStaticFields(), 0u); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 5756 | for (ArtField& field : new_class->GetSFields()) { |
| 5757 | if (field.GetDeclaringClass() == temp_class) { |
| 5758 | field.SetDeclaringClass(new_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5759 | } |
| 5760 | } |
| 5761 | |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 5762 | DCHECK_EQ(temp_class->NumDirectMethods(), 0u); |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 5763 | DCHECK_EQ(temp_class->NumVirtualMethods(), 0u); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 5764 | for (auto& method : new_class->GetMethods(image_pointer_size_)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5765 | if (method.GetDeclaringClass() == temp_class) { |
| 5766 | method.SetDeclaringClass(new_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5767 | } |
| 5768 | } |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 5769 | |
| 5770 | // Make sure the remembered set and mod-union tables know that we updated some of the native |
| 5771 | // roots. |
Mathieu Chartier | 88ea61e | 2018-06-20 17:45:41 -0700 | [diff] [blame] | 5772 | WriteBarrier::ForEveryFieldWrite(new_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5773 | } |
| 5774 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5775 | void ClassLinker::RegisterClassLoader(ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | 5b83050 | 2016-03-02 10:30:23 -0800 | [diff] [blame] | 5776 | CHECK(class_loader->GetAllocator() == nullptr); |
| 5777 | CHECK(class_loader->GetClassTable() == nullptr); |
| 5778 | Thread* const self = Thread::Current(); |
| 5779 | ClassLoaderData data; |
Ian Rogers | 55256cb | 2017-12-21 17:07:11 -0800 | [diff] [blame] | 5780 | data.weak_root = self->GetJniEnv()->GetVm()->AddWeakGlobalRef(self, class_loader); |
Mathieu Chartier | 5b83050 | 2016-03-02 10:30:23 -0800 | [diff] [blame] | 5781 | // Create and set the class table. |
| 5782 | data.class_table = new ClassTable; |
| 5783 | class_loader->SetClassTable(data.class_table); |
| 5784 | // Create and set the linear allocator. |
| 5785 | data.allocator = Runtime::Current()->CreateLinearAlloc(); |
| 5786 | class_loader->SetAllocator(data.allocator); |
| 5787 | // Add to the list so that we know to free the data later. |
| 5788 | class_loaders_.push_back(data); |
| 5789 | } |
| 5790 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5791 | ClassTable* ClassLinker::InsertClassTableForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 5792 | if (class_loader == nullptr) { |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 5793 | return boot_class_table_.get(); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5794 | } |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 5795 | ClassTable* class_table = class_loader->GetClassTable(); |
| 5796 | if (class_table == nullptr) { |
Mathieu Chartier | 5b83050 | 2016-03-02 10:30:23 -0800 | [diff] [blame] | 5797 | RegisterClassLoader(class_loader); |
| 5798 | class_table = class_loader->GetClassTable(); |
| 5799 | DCHECK(class_table != nullptr); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 5800 | } |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5801 | return class_table; |
| 5802 | } |
| 5803 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5804 | ClassTable* ClassLinker::ClassTableForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) { |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 5805 | return class_loader == nullptr ? boot_class_table_.get() : class_loader->GetClassTable(); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5806 | } |
| 5807 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5808 | static ImTable* FindSuperImt(ObjPtr<mirror::Class> klass, PointerSize pointer_size) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5809 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5810 | while (klass->HasSuperClass()) { |
| 5811 | klass = klass->GetSuperClass(); |
| 5812 | if (klass->ShouldHaveImt()) { |
| 5813 | return klass->GetImt(pointer_size); |
| 5814 | } |
| 5815 | } |
| 5816 | return nullptr; |
| 5817 | } |
| 5818 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5819 | bool ClassLinker::LinkClass(Thread* self, |
| 5820 | const char* descriptor, |
| 5821 | Handle<mirror::Class> klass, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5822 | Handle<mirror::ObjectArray<mirror::Class>> interfaces, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5823 | MutableHandle<mirror::Class>* h_new_class_out) { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5824 | CHECK_EQ(ClassStatus::kLoaded, klass->GetStatus()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5825 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5826 | if (!LinkSuperClass(klass)) { |
| 5827 | return false; |
| 5828 | } |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5829 | ArtMethod* imt_data[ImTable::kSize]; |
| 5830 | // If there are any new conflicts compared to super class. |
| 5831 | bool new_conflict = false; |
Nicolas Geoffray | 918dcea | 2017-07-21 07:58:14 +0000 | [diff] [blame] | 5832 | std::fill_n(imt_data, arraysize(imt_data), Runtime::Current()->GetImtUnimplementedMethod()); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5833 | if (!LinkMethods(self, klass, interfaces, &new_conflict, imt_data)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5834 | return false; |
| 5835 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5836 | if (!LinkInstanceFields(self, klass)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5837 | return false; |
| 5838 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5839 | size_t class_size; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5840 | if (!LinkStaticFields(self, klass, &class_size)) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 5841 | return false; |
| 5842 | } |
| 5843 | CreateReferenceInstanceOffsets(klass); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5844 | CHECK_EQ(ClassStatus::kLoaded, klass->GetStatus()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5845 | |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5846 | ImTable* imt = nullptr; |
| 5847 | if (klass->ShouldHaveImt()) { |
| 5848 | // If there are any new conflicts compared to the super class we can not make a copy. There |
| 5849 | // can be cases where both will have a conflict method at the same slot without having the same |
| 5850 | // set of conflicts. In this case, we can not share the IMT since the conflict table slow path |
| 5851 | // will possibly create a table that is incorrect for either of the classes. |
| 5852 | // Same IMT with new_conflict does not happen very often. |
| 5853 | if (!new_conflict) { |
| 5854 | ImTable* super_imt = FindSuperImt(klass.Get(), image_pointer_size_); |
| 5855 | if (super_imt != nullptr) { |
| 5856 | bool imt_equals = true; |
| 5857 | for (size_t i = 0; i < ImTable::kSize && imt_equals; ++i) { |
| 5858 | imt_equals = imt_equals && (super_imt->Get(i, image_pointer_size_) == imt_data[i]); |
| 5859 | } |
| 5860 | if (imt_equals) { |
| 5861 | imt = super_imt; |
| 5862 | } |
| 5863 | } |
| 5864 | } |
| 5865 | if (imt == nullptr) { |
| 5866 | LinearAlloc* allocator = GetAllocatorForClassLoader(klass->GetClassLoader()); |
| 5867 | imt = reinterpret_cast<ImTable*>( |
| 5868 | allocator->Alloc(self, ImTable::SizeInBytes(image_pointer_size_))); |
| 5869 | if (imt == nullptr) { |
| 5870 | return false; |
| 5871 | } |
| 5872 | imt->Populate(imt_data, image_pointer_size_); |
| 5873 | } |
| 5874 | } |
| 5875 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5876 | if (!klass->IsTemp() || (!init_done_ && klass->GetClassSize() == class_size)) { |
| 5877 | // We don't need to retire this class as it has no embedded tables or it was created the |
| 5878 | // correct size during class linker initialization. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5879 | CHECK_EQ(klass->GetClassSize(), class_size) << klass->PrettyDescriptor(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5880 | |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5881 | if (klass->ShouldHaveEmbeddedVTable()) { |
| 5882 | klass->PopulateEmbeddedVTable(image_pointer_size_); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5883 | } |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5884 | if (klass->ShouldHaveImt()) { |
| 5885 | klass->SetImt(imt, image_pointer_size_); |
| 5886 | } |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 5887 | |
| 5888 | // Update CHA info based on whether we override methods. |
| 5889 | // Have to do this before setting the class as resolved which allows |
| 5890 | // instantiation of klass. |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 5891 | if (LIKELY(descriptor != nullptr) && cha_ != nullptr) { |
Andreas Gampe | c1ac9ee | 2017-07-24 22:35:49 -0700 | [diff] [blame] | 5892 | cha_->UpdateAfterLoadingOf(klass); |
| 5893 | } |
Nicolas Geoffray | 918dcea | 2017-07-21 07:58:14 +0000 | [diff] [blame] | 5894 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5895 | // This will notify waiters on klass that saw the not yet resolved |
| 5896 | // class in the class_table_ during EnsureResolved. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5897 | mirror::Class::SetStatus(klass, ClassStatus::kResolved, self); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 5898 | h_new_class_out->Assign(klass.Get()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5899 | } else { |
| 5900 | CHECK(!klass->IsResolved()); |
| 5901 | // Retire the temporary class and create the correctly sized resolved class. |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 5902 | StackHandleScope<1> hs(self); |
Vladimir Marko | 3068d58 | 2019-05-28 16:39:29 +0100 | [diff] [blame] | 5903 | Handle<mirror::Class> h_new_class = |
| 5904 | 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] | 5905 | // Set arrays to null since we don't want to have multiple classes with the same ArtField or |
| 5906 | // ArtMethod array pointers. If this occurs, it causes bugs in remembered sets since the GC |
| 5907 | // may not see any references to the target space and clean the card for a class if another |
| 5908 | // class had the same array pointer. |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 5909 | klass->SetMethodsPtrUnchecked(nullptr, 0, 0); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 5910 | klass->SetSFieldsPtrUnchecked(nullptr); |
| 5911 | klass->SetIFieldsPtrUnchecked(nullptr); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 5912 | if (UNLIKELY(h_new_class == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5913 | self->AssertPendingOOMException(); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5914 | mirror::Class::SetStatus(klass, ClassStatus::kErrorUnresolved, self); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5915 | return false; |
| 5916 | } |
| 5917 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 5918 | CHECK_EQ(h_new_class->GetClassSize(), class_size); |
| 5919 | ObjectLock<mirror::Class> lock(self, h_new_class); |
| 5920 | FixupTemporaryDeclaringClass(klass.Get(), h_new_class.Get()); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5921 | |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 5922 | if (LIKELY(descriptor != nullptr)) { |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 5923 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Vladimir Marko | 0984e48 | 2019-03-27 16:41:41 +0000 | [diff] [blame] | 5924 | const ObjPtr<mirror::ClassLoader> class_loader = h_new_class.Get()->GetClassLoader(); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5925 | ClassTable* const table = InsertClassTableForClassLoader(class_loader); |
Vladimir Marko | 0984e48 | 2019-03-27 16:41:41 +0000 | [diff] [blame] | 5926 | const ObjPtr<mirror::Class> existing = |
| 5927 | table->UpdateClass(descriptor, h_new_class.Get(), ComputeModifiedUtf8Hash(descriptor)); |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 5928 | if (class_loader != nullptr) { |
| 5929 | // We updated the class in the class table, perform the write barrier so that the GC knows |
| 5930 | // about the change. |
Mathieu Chartier | 88ea61e | 2018-06-20 17:45:41 -0700 | [diff] [blame] | 5931 | WriteBarrier::ForEveryFieldWrite(class_loader); |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 5932 | } |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5933 | CHECK_EQ(existing, klass.Get()); |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 5934 | if (log_new_roots_) { |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5935 | new_class_roots_.push_back(GcRoot<mirror::Class>(h_new_class.Get())); |
| 5936 | } |
| 5937 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5938 | |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 5939 | // Update CHA info based on whether we override methods. |
| 5940 | // Have to do this before setting the class as resolved which allows |
| 5941 | // instantiation of klass. |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 5942 | if (LIKELY(descriptor != nullptr) && cha_ != nullptr) { |
Andreas Gampe | c1ac9ee | 2017-07-24 22:35:49 -0700 | [diff] [blame] | 5943 | cha_->UpdateAfterLoadingOf(h_new_class); |
| 5944 | } |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 5945 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5946 | // This will notify waiters on temp class that saw the not yet resolved class in the |
| 5947 | // class_table_ during EnsureResolved. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5948 | mirror::Class::SetStatus(klass, ClassStatus::kRetired, self); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5949 | |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5950 | CHECK_EQ(h_new_class->GetStatus(), ClassStatus::kResolving); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5951 | // This will notify waiters on new_class that saw the not yet resolved |
| 5952 | // class in the class_table_ during EnsureResolved. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5953 | mirror::Class::SetStatus(h_new_class, ClassStatus::kResolved, self); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 5954 | // Return the new class. |
| 5955 | h_new_class_out->Assign(h_new_class.Get()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5956 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5957 | return true; |
| 5958 | } |
| 5959 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5960 | bool ClassLinker::LoadSuperAndInterfaces(Handle<mirror::Class> klass, const DexFile& dex_file) { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5961 | CHECK_EQ(ClassStatus::kIdx, klass->GetStatus()); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 5962 | const dex::ClassDef& class_def = dex_file.GetClassDef(klass->GetDexClassDefIndex()); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 5963 | dex::TypeIndex super_class_idx = class_def.superclass_idx_; |
| 5964 | if (super_class_idx.IsValid()) { |
Roland Levillain | 90328ac | 2016-05-18 12:25:38 +0100 | [diff] [blame] | 5965 | // Check that a class does not inherit from itself directly. |
| 5966 | // |
| 5967 | // TODO: This is a cheap check to detect the straightforward case |
| 5968 | // of a class extending itself (b/28685551), but we should do a |
| 5969 | // proper cycle detection on loaded classes, to detect all cases |
| 5970 | // of class circularity errors (b/28830038). |
| 5971 | if (super_class_idx == class_def.class_idx_) { |
| 5972 | ThrowClassCircularityError(klass.Get(), |
| 5973 | "Class %s extends itself", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5974 | klass->PrettyDescriptor().c_str()); |
Roland Levillain | 90328ac | 2016-05-18 12:25:38 +0100 | [diff] [blame] | 5975 | return false; |
| 5976 | } |
| 5977 | |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 5978 | ObjPtr<mirror::Class> super_class = ResolveType(super_class_idx, klass.Get()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5979 | if (super_class == nullptr) { |
Brian Carlstrom | 65ca077 | 2011-09-24 16:03:08 -0700 | [diff] [blame] | 5980 | DCHECK(Thread::Current()->IsExceptionPending()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5981 | return false; |
| 5982 | } |
Ian Rogers | be125a9 | 2012-01-11 15:19:49 -0800 | [diff] [blame] | 5983 | // Verify |
| 5984 | if (!klass->CanAccess(super_class)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5985 | ThrowIllegalAccessError(klass.Get(), "Class %s extended by class %s is inaccessible", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5986 | super_class->PrettyDescriptor().c_str(), |
| 5987 | klass->PrettyDescriptor().c_str()); |
Ian Rogers | be125a9 | 2012-01-11 15:19:49 -0800 | [diff] [blame] | 5988 | return false; |
| 5989 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5990 | CHECK(super_class->IsResolved()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 5991 | klass->SetSuperClass(super_class); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5992 | } |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 5993 | const dex::TypeList* interfaces = dex_file.GetInterfacesList(class_def); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5994 | if (interfaces != nullptr) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 5995 | for (size_t i = 0; i < interfaces->Size(); i++) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 5996 | dex::TypeIndex idx = interfaces->GetTypeItem(i).type_idx_; |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 5997 | ObjPtr<mirror::Class> interface = ResolveType(idx, klass.Get()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5998 | if (interface == nullptr) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 5999 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 6000 | return false; |
| 6001 | } |
| 6002 | // Verify |
| 6003 | if (!klass->CanAccess(interface)) { |
| 6004 | // TODO: the RI seemed to ignore this in my testing. |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 6005 | ThrowIllegalAccessError(klass.Get(), |
| 6006 | "Interface %s implemented by class %s is inaccessible", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6007 | interface->PrettyDescriptor().c_str(), |
| 6008 | klass->PrettyDescriptor().c_str()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 6009 | return false; |
| 6010 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6011 | } |
| 6012 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 6013 | // Mark the class as loaded. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 6014 | mirror::Class::SetStatus(klass, ClassStatus::kLoaded, nullptr); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6015 | return true; |
| 6016 | } |
| 6017 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 6018 | bool ClassLinker::LinkSuperClass(Handle<mirror::Class> klass) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6019 | CHECK(!klass->IsPrimitive()); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6020 | ObjPtr<mirror::Class> super = klass->GetSuperClass(); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 6021 | ObjPtr<mirror::Class> object_class = GetClassRoot<mirror::Object>(this); |
| 6022 | if (klass.Get() == object_class) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 6023 | if (super != nullptr) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 6024 | ThrowClassFormatError(klass.Get(), "java.lang.Object must not have a superclass"); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6025 | return false; |
| 6026 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6027 | return true; |
| 6028 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 6029 | if (super == nullptr) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 6030 | ThrowLinkageError(klass.Get(), "No superclass defined for class %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6031 | klass->PrettyDescriptor().c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6032 | return false; |
| 6033 | } |
| 6034 | // Verify |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 6035 | if (klass->IsInterface() && super != object_class) { |
Vladimir Marko | 1fcae9f | 2017-11-28 14:14:19 +0000 | [diff] [blame] | 6036 | ThrowClassFormatError(klass.Get(), "Interfaces must have java.lang.Object as superclass"); |
| 6037 | return false; |
| 6038 | } |
Vladimir Marko | b43b2d8 | 2017-07-18 17:46:38 +0100 | [diff] [blame] | 6039 | if (super->IsFinal()) { |
| 6040 | ThrowVerifyError(klass.Get(), |
| 6041 | "Superclass %s of %s is declared final", |
| 6042 | super->PrettyDescriptor().c_str(), |
| 6043 | klass->PrettyDescriptor().c_str()); |
| 6044 | return false; |
| 6045 | } |
| 6046 | if (super->IsInterface()) { |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 6047 | ThrowIncompatibleClassChangeError(klass.Get(), |
Vladimir Marko | b43b2d8 | 2017-07-18 17:46:38 +0100 | [diff] [blame] | 6048 | "Superclass %s of %s is an interface", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6049 | super->PrettyDescriptor().c_str(), |
Vladimir Marko | b43b2d8 | 2017-07-18 17:46:38 +0100 | [diff] [blame] | 6050 | klass->PrettyDescriptor().c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6051 | return false; |
| 6052 | } |
| 6053 | if (!klass->CanAccess(super)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 6054 | ThrowIllegalAccessError(klass.Get(), "Superclass %s is inaccessible to class %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6055 | super->PrettyDescriptor().c_str(), |
| 6056 | klass->PrettyDescriptor().c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6057 | return false; |
| 6058 | } |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 6059 | |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 6060 | // Inherit kAccClassIsFinalizable from the superclass in case this |
| 6061 | // class doesn't override finalize. |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 6062 | if (super->IsFinalizable()) { |
| 6063 | klass->SetFinalizable(); |
| 6064 | } |
| 6065 | |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 6066 | // Inherit class loader flag form super class. |
| 6067 | if (super->IsClassLoaderClass()) { |
| 6068 | klass->SetClassLoaderClass(); |
| 6069 | } |
| 6070 | |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 6071 | // Inherit reference flags (if any) from the superclass. |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 6072 | uint32_t reference_flags = (super->GetClassFlags() & mirror::kClassFlagReference); |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 6073 | if (reference_flags != 0) { |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 6074 | CHECK_EQ(klass->GetClassFlags(), 0u); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 6075 | klass->SetClassFlags(klass->GetClassFlags() | reference_flags); |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 6076 | } |
Elliott Hughes | 72ee0ae | 2011-10-10 17:31:28 -0700 | [diff] [blame] | 6077 | // Disallow custom direct subclasses of java.lang.ref.Reference. |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 6078 | if (init_done_ && super == GetClassRoot<mirror::Reference>(this)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 6079 | ThrowLinkageError(klass.Get(), |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 6080 | "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] | 6081 | klass->PrettyDescriptor().c_str()); |
Elliott Hughes | 72ee0ae | 2011-10-10 17:31:28 -0700 | [diff] [blame] | 6082 | return false; |
| 6083 | } |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 6084 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 6085 | if (kIsDebugBuild) { |
| 6086 | // Ensure super classes are fully resolved prior to resolving fields.. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 6087 | while (super != nullptr) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 6088 | CHECK(super->IsResolved()); |
| 6089 | super = super->GetSuperClass(); |
| 6090 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 6091 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6092 | return true; |
| 6093 | } |
| 6094 | |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 6095 | // Comparator for name and signature of a method, used in finding overriding methods. Implementation |
| 6096 | // avoids the use of handles, if it didn't then rather than compare dex files we could compare dex |
| 6097 | // caches in the implementation below. |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 6098 | class MethodNameAndSignatureComparator final : public ValueObject { |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 6099 | public: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6100 | explicit MethodNameAndSignatureComparator(ArtMethod* method) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 6101 | REQUIRES_SHARED(Locks::mutator_lock_) : |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 6102 | dex_file_(method->GetDexFile()), mid_(&dex_file_->GetMethodId(method->GetDexMethodIndex())), |
Vladimir Marko | b4bd92f | 2021-07-05 12:18:26 +0100 | [diff] [blame] | 6103 | name_view_() { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6104 | DCHECK(!method->IsProxyMethod()) << method->PrettyMethod(); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6105 | } |
| 6106 | |
Vladimir Marko | b4bd92f | 2021-07-05 12:18:26 +0100 | [diff] [blame] | 6107 | ALWAYS_INLINE std::string_view GetNameView() { |
| 6108 | if (name_view_.empty()) { |
| 6109 | name_view_ = dex_file_->StringViewByIdx(mid_->name_idx_); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6110 | } |
Vladimir Marko | b4bd92f | 2021-07-05 12:18:26 +0100 | [diff] [blame] | 6111 | return name_view_; |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 6112 | } |
| 6113 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6114 | bool HasSameNameAndSignature(ArtMethod* other) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 6115 | REQUIRES_SHARED(Locks::mutator_lock_) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6116 | DCHECK(!other->IsProxyMethod()) << other->PrettyMethod(); |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 6117 | const DexFile* other_dex_file = other->GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 6118 | const dex::MethodId& other_mid = other_dex_file->GetMethodId(other->GetDexMethodIndex()); |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 6119 | if (dex_file_ == other_dex_file) { |
| 6120 | return mid_->name_idx_ == other_mid.name_idx_ && mid_->proto_idx_ == other_mid.proto_idx_; |
| 6121 | } |
Vladimir Marko | b4bd92f | 2021-07-05 12:18:26 +0100 | [diff] [blame] | 6122 | return GetNameView() == other_dex_file->StringViewByIdx(other_mid.name_idx_) && |
| 6123 | dex_file_->GetMethodSignature(*mid_) == other_dex_file->GetMethodSignature(other_mid); |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 6124 | } |
| 6125 | |
| 6126 | private: |
| 6127 | // Dex file for the method to compare against. |
| 6128 | const DexFile* const dex_file_; |
| 6129 | // MethodId for the method to compare against. |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 6130 | const dex::MethodId* const mid_; |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 6131 | // Lazily computed name from the dex file's strings. |
Vladimir Marko | b4bd92f | 2021-07-05 12:18:26 +0100 | [diff] [blame] | 6132 | std::string_view name_view_; |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 6133 | }; |
| 6134 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6135 | ArtMethod* ClassLinker::AddMethodToConflictTable(ObjPtr<mirror::Class> klass, |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6136 | ArtMethod* conflict_method, |
| 6137 | ArtMethod* interface_method, |
Nicolas Geoffray | 47213e4 | 2020-12-30 15:12:00 +0000 | [diff] [blame] | 6138 | ArtMethod* method) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 6139 | ImtConflictTable* current_table = conflict_method->GetImtConflictTable(kRuntimePointerSize); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6140 | Runtime* const runtime = Runtime::Current(); |
| 6141 | LinearAlloc* linear_alloc = GetAllocatorForClassLoader(klass->GetClassLoader()); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6142 | |
| 6143 | // 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] | 6144 | ArtMethod* new_conflict_method = (conflict_method == runtime->GetImtConflictMethod()) |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6145 | ? runtime->CreateImtConflictMethod(linear_alloc) |
| 6146 | : conflict_method; |
| 6147 | |
| 6148 | // Allocate a new table. Note that we will leak this table at the next conflict, |
| 6149 | // but that's a tradeoff compared to making the table fixed size. |
| 6150 | void* data = linear_alloc->Alloc( |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6151 | Thread::Current(), ImtConflictTable::ComputeSizeWithOneMoreEntry(current_table, |
| 6152 | image_pointer_size_)); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6153 | if (data == nullptr) { |
| 6154 | LOG(ERROR) << "Failed to allocate conflict table"; |
| 6155 | return conflict_method; |
| 6156 | } |
| 6157 | ImtConflictTable* new_table = new (data) ImtConflictTable(current_table, |
| 6158 | interface_method, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6159 | method, |
| 6160 | image_pointer_size_); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6161 | |
| 6162 | // Do a fence to ensure threads see the data in the table before it is assigned |
| 6163 | // to the conflict method. |
| 6164 | // Note that there is a race in the presence of multiple threads and we may leak |
| 6165 | // memory from the LinearAlloc, but that's a tradeoff compared to using |
| 6166 | // atomic operations. |
Orion Hodson | 27b9676 | 2018-03-13 16:06:57 +0000 | [diff] [blame] | 6167 | std::atomic_thread_fence(std::memory_order_release); |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6168 | new_conflict_method->SetImtConflictTable(new_table, image_pointer_size_); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6169 | return new_conflict_method; |
| 6170 | } |
| 6171 | |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 6172 | bool ClassLinker::AllocateIfTableMethodArrays(Thread* self, |
| 6173 | Handle<mirror::Class> klass, |
| 6174 | Handle<mirror::IfTable> iftable) { |
| 6175 | DCHECK(!klass->IsInterface()); |
Vladimir Marko | bed84ef | 2022-01-21 13:57:14 +0000 | [diff] [blame] | 6176 | DCHECK(klass->HasSuperClass()); |
| 6177 | const size_t ifcount = iftable->Count(); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 6178 | for (size_t i = 0; i < ifcount; ++i) { |
| 6179 | size_t num_methods = iftable->GetInterface(i)->NumDeclaredVirtualMethods(); |
| 6180 | if (num_methods > 0) { |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame^] | 6181 | ObjPtr<mirror::PointerArray> method_array = AllocPointerArray(self, num_methods); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 6182 | if (UNLIKELY(method_array == nullptr)) { |
| 6183 | self->AssertPendingOOMException(); |
| 6184 | return false; |
| 6185 | } |
| 6186 | iftable->SetMethodArray(i, method_array); |
| 6187 | } |
| 6188 | } |
| 6189 | return true; |
| 6190 | } |
| 6191 | |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6192 | void ClassLinker::SetIMTRef(ArtMethod* unimplemented_method, |
| 6193 | ArtMethod* imt_conflict_method, |
| 6194 | ArtMethod* current_method, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6195 | /*out*/bool* new_conflict, |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6196 | /*out*/ArtMethod** imt_ref) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6197 | // Place method in imt if entry is empty, place conflict otherwise. |
| 6198 | if (*imt_ref == unimplemented_method) { |
| 6199 | *imt_ref = current_method; |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 6200 | } else if (!(*imt_ref)->IsRuntimeMethod()) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6201 | // If we are not a conflict and we have the same signature and name as the imt |
| 6202 | // entry, it must be that we overwrote a superclass vtable entry. |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 6203 | // Note that we have checked IsRuntimeMethod, as there may be multiple different |
| 6204 | // conflict methods. |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6205 | MethodNameAndSignatureComparator imt_comparator( |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6206 | (*imt_ref)->GetInterfaceMethodIfProxy(image_pointer_size_)); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6207 | if (imt_comparator.HasSameNameAndSignature( |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6208 | current_method->GetInterfaceMethodIfProxy(image_pointer_size_))) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6209 | *imt_ref = current_method; |
| 6210 | } else { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6211 | *imt_ref = imt_conflict_method; |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6212 | *new_conflict = true; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6213 | } |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 6214 | } else { |
| 6215 | // Place the default conflict method. Note that there may be an existing conflict |
| 6216 | // method in the IMT, but it could be one tailored to the super class, with a |
| 6217 | // specific ImtConflictTable. |
| 6218 | *imt_ref = imt_conflict_method; |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6219 | *new_conflict = true; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6220 | } |
| 6221 | } |
| 6222 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6223 | void ClassLinker::FillIMTAndConflictTables(ObjPtr<mirror::Class> klass) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6224 | DCHECK(klass->ShouldHaveImt()) << klass->PrettyClass(); |
| 6225 | DCHECK(!klass->IsTemp()) << klass->PrettyClass(); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6226 | ArtMethod* imt_data[ImTable::kSize]; |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6227 | Runtime* const runtime = Runtime::Current(); |
| 6228 | ArtMethod* const unimplemented_method = runtime->GetImtUnimplementedMethod(); |
| 6229 | ArtMethod* const conflict_method = runtime->GetImtConflictMethod(); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6230 | std::fill_n(imt_data, arraysize(imt_data), unimplemented_method); |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6231 | if (klass->GetIfTable() != nullptr) { |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6232 | bool new_conflict = false; |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6233 | FillIMTFromIfTable(klass->GetIfTable(), |
| 6234 | unimplemented_method, |
| 6235 | conflict_method, |
| 6236 | klass, |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 6237 | /*create_conflict_tables=*/true, |
| 6238 | /*ignore_copied_methods=*/false, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6239 | &new_conflict, |
| 6240 | &imt_data[0]); |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6241 | } |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6242 | // Compare the IMT with the super class including the conflict methods. If they are equivalent, |
| 6243 | // we can just use the same pointer. |
| 6244 | ImTable* imt = nullptr; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6245 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6246 | if (super_class != nullptr && super_class->ShouldHaveImt()) { |
| 6247 | ImTable* super_imt = super_class->GetImt(image_pointer_size_); |
| 6248 | bool same = true; |
| 6249 | for (size_t i = 0; same && i < ImTable::kSize; ++i) { |
| 6250 | ArtMethod* method = imt_data[i]; |
| 6251 | ArtMethod* super_method = super_imt->Get(i, image_pointer_size_); |
| 6252 | if (method != super_method) { |
| 6253 | bool is_conflict_table = method->IsRuntimeMethod() && |
| 6254 | method != unimplemented_method && |
| 6255 | method != conflict_method; |
| 6256 | // Verify conflict contents. |
| 6257 | bool super_conflict_table = super_method->IsRuntimeMethod() && |
| 6258 | super_method != unimplemented_method && |
| 6259 | super_method != conflict_method; |
| 6260 | if (!is_conflict_table || !super_conflict_table) { |
| 6261 | same = false; |
| 6262 | } else { |
| 6263 | ImtConflictTable* table1 = method->GetImtConflictTable(image_pointer_size_); |
| 6264 | ImtConflictTable* table2 = super_method->GetImtConflictTable(image_pointer_size_); |
| 6265 | same = same && table1->Equals(table2, image_pointer_size_); |
| 6266 | } |
| 6267 | } |
| 6268 | } |
| 6269 | if (same) { |
| 6270 | imt = super_imt; |
| 6271 | } |
| 6272 | } |
| 6273 | if (imt == nullptr) { |
| 6274 | imt = klass->GetImt(image_pointer_size_); |
| 6275 | DCHECK(imt != nullptr); |
| 6276 | imt->Populate(imt_data, image_pointer_size_); |
| 6277 | } else { |
| 6278 | klass->SetImt(imt, image_pointer_size_); |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6279 | } |
| 6280 | } |
| 6281 | |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6282 | ImtConflictTable* ClassLinker::CreateImtConflictTable(size_t count, |
| 6283 | LinearAlloc* linear_alloc, |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 6284 | PointerSize image_pointer_size) { |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6285 | void* data = linear_alloc->Alloc(Thread::Current(), |
| 6286 | ImtConflictTable::ComputeSize(count, |
| 6287 | image_pointer_size)); |
| 6288 | return (data != nullptr) ? new (data) ImtConflictTable(count, image_pointer_size) : nullptr; |
| 6289 | } |
| 6290 | |
| 6291 | ImtConflictTable* ClassLinker::CreateImtConflictTable(size_t count, LinearAlloc* linear_alloc) { |
| 6292 | return CreateImtConflictTable(count, linear_alloc, image_pointer_size_); |
| 6293 | } |
| 6294 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6295 | void ClassLinker::FillIMTFromIfTable(ObjPtr<mirror::IfTable> if_table, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6296 | ArtMethod* unimplemented_method, |
| 6297 | ArtMethod* imt_conflict_method, |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6298 | ObjPtr<mirror::Class> klass, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6299 | bool create_conflict_tables, |
| 6300 | bool ignore_copied_methods, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6301 | /*out*/bool* new_conflict, |
| 6302 | /*out*/ArtMethod** imt) { |
| 6303 | uint32_t conflict_counts[ImTable::kSize] = {}; |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6304 | for (size_t i = 0, length = if_table->Count(); i < length; ++i) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6305 | ObjPtr<mirror::Class> interface = if_table->GetInterface(i); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6306 | const size_t num_virtuals = interface->NumVirtualMethods(); |
| 6307 | const size_t method_array_count = if_table->GetMethodArrayCount(i); |
| 6308 | // Virtual methods can be larger than the if table methods if there are default methods. |
| 6309 | DCHECK_GE(num_virtuals, method_array_count); |
| 6310 | if (kIsDebugBuild) { |
| 6311 | if (klass->IsInterface()) { |
| 6312 | DCHECK_EQ(method_array_count, 0u); |
| 6313 | } else { |
| 6314 | DCHECK_EQ(interface->NumDeclaredVirtualMethods(), method_array_count); |
| 6315 | } |
| 6316 | } |
| 6317 | if (method_array_count == 0) { |
| 6318 | continue; |
| 6319 | } |
Vladimir Marko | 557fece | 2019-03-26 14:29:41 +0000 | [diff] [blame] | 6320 | ObjPtr<mirror::PointerArray> method_array = if_table->GetMethodArray(i); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6321 | for (size_t j = 0; j < method_array_count; ++j) { |
| 6322 | ArtMethod* implementation_method = |
| 6323 | method_array->GetElementPtrSize<ArtMethod*>(j, image_pointer_size_); |
| 6324 | if (ignore_copied_methods && implementation_method->IsCopied()) { |
| 6325 | continue; |
| 6326 | } |
| 6327 | DCHECK(implementation_method != nullptr); |
| 6328 | // Miranda methods cannot be used to implement an interface method, but they are safe to put |
| 6329 | // in the IMT since their entrypoint is the interface trampoline. If we put any copied methods |
| 6330 | // or interface methods in the IMT here they will not create extra conflicts since we compare |
| 6331 | // names and signatures in SetIMTRef. |
| 6332 | ArtMethod* interface_method = interface->GetVirtualMethod(j, image_pointer_size_); |
David Srbecky | e36e7f2 | 2018-11-14 14:21:23 +0000 | [diff] [blame] | 6333 | const uint32_t imt_index = interface_method->GetImtIndex(); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6334 | |
| 6335 | // There is only any conflicts if all of the interface methods for an IMT slot don't have |
| 6336 | // the same implementation method, keep track of this to avoid creating a conflict table in |
| 6337 | // this case. |
| 6338 | |
| 6339 | // Conflict table size for each IMT slot. |
| 6340 | ++conflict_counts[imt_index]; |
| 6341 | |
| 6342 | SetIMTRef(unimplemented_method, |
| 6343 | imt_conflict_method, |
| 6344 | implementation_method, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6345 | /*out*/new_conflict, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6346 | /*out*/&imt[imt_index]); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6347 | } |
| 6348 | } |
| 6349 | |
| 6350 | if (create_conflict_tables) { |
| 6351 | // Create the conflict tables. |
| 6352 | LinearAlloc* linear_alloc = GetAllocatorForClassLoader(klass->GetClassLoader()); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6353 | for (size_t i = 0; i < ImTable::kSize; ++i) { |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6354 | size_t conflicts = conflict_counts[i]; |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6355 | if (imt[i] == imt_conflict_method) { |
| 6356 | ImtConflictTable* new_table = CreateImtConflictTable(conflicts, linear_alloc); |
| 6357 | if (new_table != nullptr) { |
| 6358 | ArtMethod* new_conflict_method = |
| 6359 | Runtime::Current()->CreateImtConflictMethod(linear_alloc); |
| 6360 | new_conflict_method->SetImtConflictTable(new_table, image_pointer_size_); |
| 6361 | imt[i] = new_conflict_method; |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6362 | } else { |
| 6363 | LOG(ERROR) << "Failed to allocate conflict table"; |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6364 | imt[i] = imt_conflict_method; |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6365 | } |
| 6366 | } else { |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6367 | DCHECK_NE(imt[i], imt_conflict_method); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6368 | } |
| 6369 | } |
| 6370 | |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6371 | for (size_t i = 0, length = if_table->Count(); i < length; ++i) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6372 | ObjPtr<mirror::Class> interface = if_table->GetInterface(i); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6373 | const size_t method_array_count = if_table->GetMethodArrayCount(i); |
| 6374 | // Virtual methods can be larger than the if table methods if there are default methods. |
| 6375 | if (method_array_count == 0) { |
| 6376 | continue; |
| 6377 | } |
Vladimir Marko | 557fece | 2019-03-26 14:29:41 +0000 | [diff] [blame] | 6378 | ObjPtr<mirror::PointerArray> method_array = if_table->GetMethodArray(i); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6379 | for (size_t j = 0; j < method_array_count; ++j) { |
| 6380 | ArtMethod* implementation_method = |
| 6381 | method_array->GetElementPtrSize<ArtMethod*>(j, image_pointer_size_); |
| 6382 | if (ignore_copied_methods && implementation_method->IsCopied()) { |
| 6383 | continue; |
| 6384 | } |
| 6385 | DCHECK(implementation_method != nullptr); |
| 6386 | ArtMethod* interface_method = interface->GetVirtualMethod(j, image_pointer_size_); |
David Srbecky | e36e7f2 | 2018-11-14 14:21:23 +0000 | [diff] [blame] | 6387 | const uint32_t imt_index = interface_method->GetImtIndex(); |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6388 | if (!imt[imt_index]->IsRuntimeMethod() || |
| 6389 | imt[imt_index] == unimplemented_method || |
| 6390 | imt[imt_index] == imt_conflict_method) { |
| 6391 | continue; |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6392 | } |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6393 | ImtConflictTable* table = imt[imt_index]->GetImtConflictTable(image_pointer_size_); |
| 6394 | const size_t num_entries = table->NumEntries(image_pointer_size_); |
| 6395 | table->SetInterfaceMethod(num_entries, image_pointer_size_, interface_method); |
| 6396 | table->SetImplementationMethod(num_entries, image_pointer_size_, implementation_method); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6397 | } |
| 6398 | } |
| 6399 | } |
| 6400 | } |
| 6401 | |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6402 | namespace { |
| 6403 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6404 | // Simple helper function that checks that no subtypes of 'val' are contained within the 'classes' |
| 6405 | // set. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6406 | static bool NotSubinterfaceOfAny( |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6407 | const ScopedArenaHashSet<mirror::Class*>& classes, |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6408 | ObjPtr<mirror::Class> val) |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6409 | REQUIRES(Roles::uninterruptible_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 6410 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6411 | DCHECK(val != nullptr); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6412 | for (ObjPtr<mirror::Class> c : classes) { |
| 6413 | if (val->IsAssignableFrom(c)) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6414 | return false; |
| 6415 | } |
| 6416 | } |
| 6417 | return true; |
| 6418 | } |
| 6419 | |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6420 | // We record new interfaces by the index of the direct interface and the index in the |
| 6421 | // direct interface's `IfTable`, or `dex::kDexNoIndex` if it's the direct interface itself. |
| 6422 | struct NewInterfaceReference { |
| 6423 | uint32_t direct_interface_index; |
| 6424 | uint32_t direct_interface_iftable_index; |
| 6425 | }; |
| 6426 | |
| 6427 | class ProxyInterfacesAccessor { |
| 6428 | public: |
| 6429 | explicit ProxyInterfacesAccessor(Handle<mirror::ObjectArray<mirror::Class>> interfaces) |
| 6430 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 6431 | : interfaces_(interfaces) {} |
| 6432 | |
| 6433 | size_t GetLength() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 6434 | return interfaces_->GetLength(); |
| 6435 | } |
| 6436 | |
| 6437 | ObjPtr<mirror::Class> GetInterface(size_t index) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 6438 | DCHECK_LT(index, GetLength()); |
| 6439 | return interfaces_->GetWithoutChecks(index); |
| 6440 | } |
| 6441 | |
| 6442 | private: |
| 6443 | Handle<mirror::ObjectArray<mirror::Class>> interfaces_; |
| 6444 | }; |
| 6445 | |
| 6446 | class NonProxyInterfacesAccessor { |
| 6447 | public: |
| 6448 | NonProxyInterfacesAccessor(ClassLinker* class_linker, Handle<mirror::Class> klass) |
| 6449 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 6450 | : interfaces_(klass->GetInterfaceTypeList()), |
| 6451 | class_linker_(class_linker), |
| 6452 | klass_(klass) { |
| 6453 | DCHECK(!klass->IsProxyClass()); |
| 6454 | } |
| 6455 | |
| 6456 | size_t GetLength() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 6457 | return (interfaces_ != nullptr) ? interfaces_->Size() : 0u; |
| 6458 | } |
| 6459 | |
| 6460 | ObjPtr<mirror::Class> GetInterface(size_t index) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 6461 | DCHECK_LT(index, GetLength()); |
| 6462 | dex::TypeIndex type_index = interfaces_->GetTypeItem(index).type_idx_; |
| 6463 | return class_linker_->LookupResolvedType(type_index, klass_.Get()); |
| 6464 | } |
| 6465 | |
| 6466 | private: |
| 6467 | const dex::TypeList* interfaces_; |
| 6468 | ClassLinker* class_linker_; |
| 6469 | Handle<mirror::Class> klass_; |
| 6470 | }; |
| 6471 | |
| 6472 | // 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] | 6473 | // |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6474 | // Interfaces in the interface table must satisfy the following constraint: |
| 6475 | // all I, J: Interface | I <: J implies J precedes I |
| 6476 | // (note A <: B means that A is a subtype of B). We order this backwards so that we do not need |
| 6477 | // 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] | 6478 | // |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6479 | // This function returns a list of references for all interfaces in the transitive |
| 6480 | // closure of the direct interfaces that are not in the superclass interfaces. |
| 6481 | // The entries in the list are ordered to satisfy the interface table ordering |
| 6482 | // constraint and therefore the interface table formed by appending them to the |
| 6483 | // superclass interface table shall also satisfy that constraint. |
| 6484 | template <typename InterfaceAccessor> |
| 6485 | ALWAYS_INLINE |
| 6486 | static ArrayRef<const NewInterfaceReference> FindNewIfTableInterfaces( |
| 6487 | ObjPtr<mirror::IfTable> super_iftable, |
| 6488 | size_t super_ifcount, |
| 6489 | ScopedArenaAllocator* allocator, |
| 6490 | InterfaceAccessor&& interfaces, |
| 6491 | ArrayRef<NewInterfaceReference> initial_storage, |
| 6492 | /*out*/ScopedArenaVector<NewInterfaceReference>* supplemental_storage) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 6493 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 6494 | ScopedAssertNoThreadSuspension nts(__FUNCTION__); |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6495 | |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 6496 | // This is the set of all classes already in the iftable. Used to make checking |
| 6497 | // if a class has already been added quicker. |
| 6498 | constexpr size_t kBufferSize = 32; // 256 bytes on 64-bit architectures. |
| 6499 | mirror::Class* buffer[kBufferSize]; |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6500 | ScopedArenaHashSet<mirror::Class*> classes_in_iftable(buffer, kBufferSize, allocator->Adapter()); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6501 | // The first super_ifcount elements are from the superclass. We note that they are already added. |
| 6502 | for (size_t i = 0; i < super_ifcount; i++) { |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6503 | ObjPtr<mirror::Class> iface = super_iftable->GetInterface(i); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6504 | DCHECK(NotSubinterfaceOfAny(classes_in_iftable, iface)) << "Bad ordering."; |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6505 | classes_in_iftable.Put(iface.Ptr()); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6506 | } |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6507 | |
| 6508 | ArrayRef<NewInterfaceReference> current_storage = initial_storage; |
| 6509 | DCHECK_NE(current_storage.size(), 0u); |
| 6510 | size_t num_new_interfaces = 0u; |
| 6511 | auto insert_reference = [&](uint32_t direct_interface_index, |
| 6512 | uint32_t direct_interface_iface_index) { |
| 6513 | if (UNLIKELY(num_new_interfaces == current_storage.size())) { |
| 6514 | bool copy = current_storage.data() != supplemental_storage->data(); |
| 6515 | supplemental_storage->resize(2u * num_new_interfaces); |
| 6516 | if (copy) { |
| 6517 | std::copy_n(current_storage.data(), num_new_interfaces, supplemental_storage->data()); |
| 6518 | } |
| 6519 | current_storage = ArrayRef<NewInterfaceReference>(*supplemental_storage); |
| 6520 | } |
| 6521 | current_storage[num_new_interfaces] = {direct_interface_index, direct_interface_iface_index}; |
| 6522 | ++num_new_interfaces; |
| 6523 | }; |
| 6524 | |
| 6525 | for (size_t i = 0, num_interfaces = interfaces.GetLength(); i != num_interfaces; ++i) { |
| 6526 | ObjPtr<mirror::Class> interface = interfaces.GetInterface(i); |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 6527 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6528 | // Let us call the first filled_ifcount elements of iftable the current-iface-list. |
| 6529 | // At this point in the loop current-iface-list has the invariant that: |
| 6530 | // for every pair of interfaces I,J within it: |
| 6531 | // if index_of(I) < index_of(J) then I is not a subtype of J |
| 6532 | |
| 6533 | // If we have already seen this element then all of its super-interfaces must already be in the |
| 6534 | // current-iface-list so we can skip adding it. |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 6535 | if (classes_in_iftable.find(interface.Ptr()) == classes_in_iftable.end()) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6536 | // We haven't seen this interface so add all of its super-interfaces onto the |
| 6537 | // current-iface-list, skipping those already on it. |
| 6538 | int32_t ifcount = interface->GetIfTableCount(); |
| 6539 | for (int32_t j = 0; j < ifcount; j++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6540 | ObjPtr<mirror::Class> super_interface = interface->GetIfTable()->GetInterface(j); |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6541 | if (classes_in_iftable.find(super_interface.Ptr()) == classes_in_iftable.end()) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6542 | DCHECK(NotSubinterfaceOfAny(classes_in_iftable, super_interface)) << "Bad ordering."; |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6543 | classes_in_iftable.Put(super_interface.Ptr()); |
| 6544 | insert_reference(i, j); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6545 | } |
| 6546 | } |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6547 | // Add this interface reference after all of its super-interfaces. |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6548 | DCHECK(NotSubinterfaceOfAny(classes_in_iftable, interface)) << "Bad ordering"; |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6549 | classes_in_iftable.Put(interface.Ptr()); |
| 6550 | insert_reference(i, dex::kDexNoIndex); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6551 | } else if (kIsDebugBuild) { |
| 6552 | // Check all super-interfaces are already in the list. |
| 6553 | int32_t ifcount = interface->GetIfTableCount(); |
| 6554 | for (int32_t j = 0; j < ifcount; j++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6555 | ObjPtr<mirror::Class> super_interface = interface->GetIfTable()->GetInterface(j); |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6556 | DCHECK(classes_in_iftable.find(super_interface.Ptr()) != classes_in_iftable.end()) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6557 | << "Iftable does not contain " << mirror::Class::PrettyClass(super_interface) |
| 6558 | << ", a superinterface of " << interface->PrettyClass(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6559 | } |
| 6560 | } |
| 6561 | } |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6562 | return ArrayRef<const NewInterfaceReference>(current_storage.data(), num_new_interfaces); |
| 6563 | } |
| 6564 | |
| 6565 | template <typename InterfaceAccessor> |
| 6566 | static ObjPtr<mirror::IfTable> SetupInterfaceLookupTable( |
| 6567 | Thread* self, |
| 6568 | Handle<mirror::Class> klass, |
| 6569 | ScopedArenaAllocator* allocator, |
| 6570 | InterfaceAccessor&& interfaces) |
| 6571 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 6572 | DCHECK(klass->HasSuperClass()); |
| 6573 | ObjPtr<mirror::IfTable> super_iftable = klass->GetSuperClass()->GetIfTable(); |
| 6574 | const size_t super_ifcount = super_iftable->Count(); |
| 6575 | const size_t num_interfaces = interfaces.GetLength(); |
| 6576 | |
| 6577 | // If there are no new interfaces, we can recycle parent's interface table if the class |
| 6578 | // inherits no interfaces from the superclass (this is always the case for interfaces as |
| 6579 | // their superclass `java.lang.Object` does not implement any interface), or there are no |
| 6580 | // new virtuals, or all interfaces inherited from the superclass are just marker interfaces. |
| 6581 | auto is_marker_iface = [=](size_t index) REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE { |
| 6582 | return super_iftable->GetMethodArrayCount(index) == 0; |
| 6583 | }; |
| 6584 | auto can_reuse_super_iftable = [=]() REQUIRES_SHARED(Locks::mutator_lock_) ALWAYS_INLINE{ |
| 6585 | if (super_ifcount == 0u) { |
| 6586 | return true; |
| 6587 | } |
| 6588 | DCHECK(!klass->IsInterface()); |
| 6589 | return klass->NumDeclaredVirtualMethods() == 0u || |
| 6590 | std::all_of(CountIter(0), CountIter(super_ifcount), is_marker_iface); |
| 6591 | }; |
| 6592 | if (num_interfaces == 0 && can_reuse_super_iftable()) { |
| 6593 | return super_iftable; |
| 6594 | } |
| 6595 | |
| 6596 | // Check that every class being implemented is an interface. |
| 6597 | for (size_t i = 0; i != num_interfaces; ++i) { |
| 6598 | ObjPtr<mirror::Class> interface = interfaces.GetInterface(i); |
| 6599 | DCHECK(interface != nullptr); |
| 6600 | if (UNLIKELY(!interface->IsInterface())) { |
| 6601 | ThrowIncompatibleClassChangeError(klass.Get(), |
| 6602 | "Class %s implements non-interface class %s", |
| 6603 | klass->PrettyDescriptor().c_str(), |
| 6604 | interface->PrettyDescriptor().c_str()); |
| 6605 | return nullptr; |
| 6606 | } |
| 6607 | } |
| 6608 | |
| 6609 | static constexpr size_t kMaxStackReferences = 16; |
| 6610 | NewInterfaceReference initial_storage[kMaxStackReferences]; |
| 6611 | ScopedArenaVector<NewInterfaceReference> supplemental_storage(allocator->Adapter()); |
| 6612 | ArrayRef<const NewInterfaceReference> new_interface_references = |
| 6613 | FindNewIfTableInterfaces( |
| 6614 | super_iftable, |
| 6615 | super_ifcount, |
| 6616 | allocator, |
| 6617 | interfaces, |
| 6618 | ArrayRef<NewInterfaceReference>(initial_storage), |
| 6619 | &supplemental_storage); |
| 6620 | |
| 6621 | // If all declared interfaces were already present in superclass interface table, we can |
| 6622 | // re-check if other conditions of reusing the superclass interface table are satisfied. |
| 6623 | if (UNLIKELY(num_interfaces != 0u && new_interface_references.empty())) { |
| 6624 | DCHECK(!klass->IsInterface()); |
| 6625 | if (can_reuse_super_iftable()) { |
| 6626 | return super_iftable; |
| 6627 | } |
| 6628 | } |
| 6629 | |
| 6630 | // Create the interface table. |
| 6631 | size_t ifcount = super_ifcount + new_interface_references.size(); |
| 6632 | ObjPtr<mirror::IfTable> iftable = AllocIfTable(self, ifcount, super_iftable->GetClass()); |
| 6633 | if (UNLIKELY(iftable == nullptr)) { |
| 6634 | self->AssertPendingOOMException(); |
| 6635 | return nullptr; |
| 6636 | } |
| 6637 | // Fill in table with superclass's iftable. |
| 6638 | if (super_ifcount != 0) { |
| 6639 | // Reload `super_iftable` as it may have been clobbered by the allocation. |
| 6640 | super_iftable = klass->GetSuperClass()->GetIfTable(); |
| 6641 | for (size_t i = 0; i < super_ifcount; i++) { |
| 6642 | ObjPtr<mirror::Class> super_interface = super_iftable->GetInterface(i); |
| 6643 | iftable->SetInterface(i, super_interface); |
| 6644 | } |
| 6645 | } |
| 6646 | // Fill in the table with additional interfaces. |
| 6647 | size_t current_index = super_ifcount; |
| 6648 | for (NewInterfaceReference ref : new_interface_references) { |
| 6649 | ObjPtr<mirror::Class> direct_interface = interfaces.GetInterface(ref.direct_interface_index); |
| 6650 | ObjPtr<mirror::Class> new_interface = (ref.direct_interface_iftable_index != dex::kDexNoIndex) |
| 6651 | ? direct_interface->GetIfTable()->GetInterface(ref.direct_interface_iftable_index) |
| 6652 | : direct_interface; |
| 6653 | iftable->SetInterface(current_index, new_interface); |
| 6654 | ++current_index; |
| 6655 | } |
| 6656 | DCHECK_EQ(current_index, ifcount); |
| 6657 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6658 | if (kIsDebugBuild) { |
| 6659 | // Check that the iftable is ordered correctly. |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6660 | for (size_t i = 0; i < ifcount; i++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6661 | ObjPtr<mirror::Class> if_a = iftable->GetInterface(i); |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6662 | for (size_t j = i + 1; j < ifcount; j++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6663 | ObjPtr<mirror::Class> if_b = iftable->GetInterface(j); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6664 | // !(if_a <: if_b) |
| 6665 | CHECK(!if_b->IsAssignableFrom(if_a)) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6666 | << "Bad interface order: " << mirror::Class::PrettyClass(if_a) << " (index " << i |
| 6667 | << ") extends " |
| 6668 | << 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] | 6669 | << "interface list."; |
| 6670 | } |
| 6671 | } |
| 6672 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6673 | |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6674 | return iftable; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6675 | } |
| 6676 | |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6677 | // Check that all vtable entries are present in this class's virtuals or are the same as a |
| 6678 | // superclasses vtable entry. |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 6679 | void CheckClassOwnsVTableEntries(Thread* self, |
| 6680 | Handle<mirror::Class> klass, |
| 6681 | PointerSize pointer_size) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 6682 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6683 | StackHandleScope<2> hs(self); |
| 6684 | Handle<mirror::PointerArray> check_vtable(hs.NewHandle(klass->GetVTableDuringLinking())); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6685 | ObjPtr<mirror::Class> super_temp = (klass->HasSuperClass()) ? klass->GetSuperClass() : nullptr; |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6686 | Handle<mirror::Class> superclass(hs.NewHandle(super_temp)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 6687 | int32_t super_vtable_length = (superclass != nullptr) ? superclass->GetVTableLength() : 0; |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6688 | for (int32_t i = 0; i < check_vtable->GetLength(); ++i) { |
| 6689 | ArtMethod* m = check_vtable->GetElementPtrSize<ArtMethod*>(i, pointer_size); |
| 6690 | CHECK(m != nullptr); |
| 6691 | |
Alex Light | a41a3078 | 2017-03-29 11:33:19 -0700 | [diff] [blame] | 6692 | if (m->GetMethodIndexDuringLinking() != i) { |
| 6693 | LOG(WARNING) << m->PrettyMethod() |
| 6694 | << " has an unexpected method index for its spot in the vtable for class" |
| 6695 | << klass->PrettyClass(); |
| 6696 | } |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6697 | ArraySlice<ArtMethod> virtuals = klass->GetVirtualMethodsSliceUnchecked(pointer_size); |
| 6698 | auto is_same_method = [m] (const ArtMethod& meth) { |
| 6699 | return &meth == m; |
| 6700 | }; |
Alex Light | 3f98053 | 2017-03-17 15:10:32 -0700 | [diff] [blame] | 6701 | if (!((super_vtable_length > i && superclass->GetVTableEntry(i, pointer_size) == m) || |
| 6702 | std::find_if(virtuals.begin(), virtuals.end(), is_same_method) != virtuals.end())) { |
| 6703 | LOG(WARNING) << m->PrettyMethod() << " does not seem to be owned by current class " |
| 6704 | << klass->PrettyClass() << " or any of its superclasses!"; |
| 6705 | } |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6706 | } |
| 6707 | } |
| 6708 | |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6709 | // Check to make sure the vtable does not have duplicates. Duplicates could cause problems when a |
| 6710 | // method is overridden in a subclass. |
Andreas Gampe | a2fed08 | 2019-02-01 09:34:43 -0800 | [diff] [blame] | 6711 | template <PointerSize kPointerSize> |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 6712 | void CheckVTableHasNoDuplicates(Thread* self, Handle<mirror::Class> klass) |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6713 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 6714 | StackHandleScope<1> hs(self); |
| 6715 | Handle<mirror::PointerArray> vtable(hs.NewHandle(klass->GetVTableDuringLinking())); |
| 6716 | int32_t num_entries = vtable->GetLength(); |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 6717 | |
| 6718 | // Observations: |
| 6719 | // * The older implementation was O(n^2) and got too expensive for apps with larger classes. |
| 6720 | // * Many classes do not override Object functions (e.g., equals/hashCode/toString). Thus, |
| 6721 | // for many classes outside of libcore a cross-dexfile check has to be run anyways. |
| 6722 | // * In the cross-dexfile case, with the O(n^2), in the best case O(n) cross checks would have |
| 6723 | // to be done. It is thus OK in a single-pass algorithm to read all data, anyways. |
| 6724 | // * The single-pass algorithm will trade memory for speed, but that is OK. |
| 6725 | |
| 6726 | CHECK_GT(num_entries, 0); |
| 6727 | |
| 6728 | auto log_fn = [&vtable, &klass](int32_t i, int32_t j) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 6729 | ArtMethod* m1 = vtable->GetElementPtrSize<ArtMethod*, kPointerSize>(i); |
| 6730 | ArtMethod* m2 = vtable->GetElementPtrSize<ArtMethod*, kPointerSize>(j); |
| 6731 | LOG(WARNING) << "vtable entries " << i << " and " << j << " are identical for " |
| 6732 | << klass->PrettyClass() << " in method " << m1->PrettyMethod() |
| 6733 | << " (0x" << std::hex << reinterpret_cast<uintptr_t>(m2) << ") and " |
| 6734 | << m2->PrettyMethod() << " (0x" << std::hex |
| 6735 | << reinterpret_cast<uintptr_t>(m2) << ")"; |
| 6736 | }; |
| 6737 | struct BaseHashType { |
| 6738 | static size_t HashCombine(size_t seed, size_t val) { |
| 6739 | return seed ^ (val + 0x9e3779b9 + (seed << 6) + (seed >> 2)); |
| 6740 | } |
| 6741 | }; |
| 6742 | |
| 6743 | // Check assuming all entries come from the same dex file. |
| 6744 | { |
| 6745 | // Find the first interesting method and its dex file. |
| 6746 | int32_t start = 0; |
| 6747 | for (; start < num_entries; ++start) { |
| 6748 | ArtMethod* vtable_entry = vtable->GetElementPtrSize<ArtMethod*, kPointerSize>(start); |
| 6749 | // Don't bother if we cannot 'see' the vtable entry (i.e. it is a package-private member |
| 6750 | // maybe). |
| 6751 | if (!klass->CanAccessMember(vtable_entry->GetDeclaringClass(), |
| 6752 | vtable_entry->GetAccessFlags())) { |
| 6753 | continue; |
| 6754 | } |
| 6755 | break; |
| 6756 | } |
| 6757 | if (start == num_entries) { |
| 6758 | return; |
| 6759 | } |
| 6760 | const DexFile* dex_file = |
| 6761 | vtable->GetElementPtrSize<ArtMethod*, kPointerSize>(start)-> |
| 6762 | GetInterfaceMethodIfProxy(kPointerSize)->GetDexFile(); |
| 6763 | |
| 6764 | // Helper function to avoid logging if we have to run the cross-file checks. |
| 6765 | auto check_fn = [&](bool log_warn) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 6766 | // Use a map to store seen entries, as the storage space is too large for a bitvector. |
| 6767 | using PairType = std::pair<uint32_t, uint16_t>; |
| 6768 | struct PairHash : BaseHashType { |
| 6769 | size_t operator()(const PairType& key) const { |
| 6770 | return BaseHashType::HashCombine(BaseHashType::HashCombine(0, key.first), key.second); |
| 6771 | } |
| 6772 | }; |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 6773 | HashMap<PairType, int32_t, DefaultMapEmptyFn<PairType, int32_t>, PairHash> seen; |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 6774 | seen.reserve(2 * num_entries); |
| 6775 | bool need_slow_path = false; |
| 6776 | bool found_dup = false; |
| 6777 | for (int i = start; i < num_entries; ++i) { |
| 6778 | // Can use Unchecked here as the start loop already ensured that the arrays are correct |
| 6779 | // wrt/ kPointerSize. |
| 6780 | ArtMethod* vtable_entry = vtable->GetElementPtrSizeUnchecked<ArtMethod*, kPointerSize>(i); |
| 6781 | if (!klass->CanAccessMember(vtable_entry->GetDeclaringClass(), |
| 6782 | vtable_entry->GetAccessFlags())) { |
| 6783 | continue; |
| 6784 | } |
| 6785 | ArtMethod* m = vtable_entry->GetInterfaceMethodIfProxy(kPointerSize); |
| 6786 | if (dex_file != m->GetDexFile()) { |
| 6787 | need_slow_path = true; |
| 6788 | break; |
| 6789 | } |
| 6790 | const dex::MethodId* m_mid = &dex_file->GetMethodId(m->GetDexMethodIndex()); |
| 6791 | PairType pair = std::make_pair(m_mid->name_idx_.index_, m_mid->proto_idx_.index_); |
| 6792 | auto it = seen.find(pair); |
| 6793 | if (it != seen.end()) { |
| 6794 | found_dup = true; |
| 6795 | if (log_warn) { |
| 6796 | log_fn(it->second, i); |
| 6797 | } |
| 6798 | } else { |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 6799 | seen.insert(std::make_pair(pair, i)); |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 6800 | } |
| 6801 | } |
| 6802 | return std::make_pair(need_slow_path, found_dup); |
| 6803 | }; |
| 6804 | std::pair<bool, bool> result = check_fn(/* log_warn= */ false); |
| 6805 | if (!result.first) { |
| 6806 | if (result.second) { |
| 6807 | check_fn(/* log_warn= */ true); |
| 6808 | } |
| 6809 | return; |
| 6810 | } |
| 6811 | } |
| 6812 | |
| 6813 | // Need to check across dex files. |
| 6814 | struct Entry { |
| 6815 | size_t cached_hash = 0; |
Vladimir Marko | aa027b8 | 2021-01-06 20:34:20 +0000 | [diff] [blame] | 6816 | uint32_t name_len = 0; |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 6817 | const char* name = nullptr; |
| 6818 | Signature signature = Signature::NoSignature(); |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 6819 | |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 6820 | Entry() = default; |
| 6821 | Entry(const Entry& other) = default; |
| 6822 | Entry& operator=(const Entry& other) = default; |
| 6823 | |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 6824 | Entry(const DexFile* dex_file, const dex::MethodId& mid) |
Vladimir Marko | aa027b8 | 2021-01-06 20:34:20 +0000 | [diff] [blame] | 6825 | : name_len(0), // Explicit to enforce ordering with -Werror,-Wreorder-ctor. |
| 6826 | // This call writes `name_len` and it is therefore necessary that the |
| 6827 | // initializer for `name_len` comes before it, otherwise the value |
| 6828 | // from the call would be overwritten by that initializer. |
| 6829 | name(dex_file->StringDataAndUtf16LengthByIdx(mid.name_idx_, &name_len)), |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 6830 | signature(dex_file->GetMethodSignature(mid)) { |
Vladimir Marko | aa027b8 | 2021-01-06 20:34:20 +0000 | [diff] [blame] | 6831 | // The `name_len` has been initialized to the UTF16 length. Calculate length in bytes. |
| 6832 | if (name[name_len] != 0) { |
| 6833 | name_len += strlen(name + name_len); |
| 6834 | } |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 6835 | } |
| 6836 | |
| 6837 | bool operator==(const Entry& other) const { |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 6838 | return name_len == other.name_len && |
| 6839 | memcmp(name, other.name, name_len) == 0 && |
| 6840 | signature == other.signature; |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 6841 | } |
| 6842 | }; |
| 6843 | struct EntryHash { |
| 6844 | size_t operator()(const Entry& key) const { |
| 6845 | return key.cached_hash; |
| 6846 | } |
| 6847 | }; |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 6848 | HashMap<Entry, int32_t, DefaultMapEmptyFn<Entry, int32_t>, EntryHash> map; |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 6849 | for (int32_t i = 0; i < num_entries; ++i) { |
| 6850 | // Can use Unchecked here as the first loop already ensured that the arrays are correct |
| 6851 | // wrt/ kPointerSize. |
| 6852 | ArtMethod* vtable_entry = vtable->GetElementPtrSizeUnchecked<ArtMethod*, kPointerSize>(i); |
| 6853 | // Don't bother if we cannot 'see' the vtable entry (i.e. it is a package-private member |
| 6854 | // maybe). |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6855 | if (!klass->CanAccessMember(vtable_entry->GetDeclaringClass(), |
| 6856 | vtable_entry->GetAccessFlags())) { |
| 6857 | continue; |
| 6858 | } |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 6859 | ArtMethod* m = vtable_entry->GetInterfaceMethodIfProxy(kPointerSize); |
| 6860 | const DexFile* dex_file = m->GetDexFile(); |
| 6861 | const dex::MethodId& mid = dex_file->GetMethodId(m->GetDexMethodIndex()); |
| 6862 | |
| 6863 | Entry e(dex_file, mid); |
| 6864 | |
| 6865 | size_t string_hash = std::hash<std::string_view>()(std::string_view(e.name, e.name_len)); |
| 6866 | size_t sig_hash = std::hash<std::string>()(e.signature.ToString()); |
| 6867 | e.cached_hash = BaseHashType::HashCombine(BaseHashType::HashCombine(0u, string_hash), |
| 6868 | sig_hash); |
| 6869 | |
| 6870 | auto it = map.find(e); |
| 6871 | if (it != map.end()) { |
| 6872 | log_fn(it->second, i); |
| 6873 | } else { |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 6874 | map.insert(std::make_pair(e, i)); |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6875 | } |
| 6876 | } |
| 6877 | } |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 6878 | |
| 6879 | void CheckVTableHasNoDuplicates(Thread* self, |
| 6880 | Handle<mirror::Class> klass, |
| 6881 | PointerSize pointer_size) |
Andreas Gampe | a2fed08 | 2019-02-01 09:34:43 -0800 | [diff] [blame] | 6882 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 6883 | switch (pointer_size) { |
| 6884 | case PointerSize::k64: |
| 6885 | CheckVTableHasNoDuplicates<PointerSize::k64>(self, klass); |
| 6886 | break; |
| 6887 | case PointerSize::k32: |
| 6888 | CheckVTableHasNoDuplicates<PointerSize::k32>(self, klass); |
| 6889 | break; |
| 6890 | } |
| 6891 | } |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6892 | |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 6893 | static void CheckVTable(Thread* self, Handle<mirror::Class> klass, PointerSize pointer_size) |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 6894 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 6895 | CheckClassOwnsVTableEntries(self, klass, pointer_size); |
| 6896 | CheckVTableHasNoDuplicates(self, klass, pointer_size); |
| 6897 | } |
| 6898 | |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 6899 | } // namespace |
| 6900 | |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6901 | void ClassLinker::FillImtFromSuperClass(Handle<mirror::Class> klass, |
| 6902 | ArtMethod* unimplemented_method, |
| 6903 | ArtMethod* imt_conflict_method, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6904 | bool* new_conflict, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6905 | ArtMethod** imt) { |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 6906 | DCHECK(klass->HasSuperClass()); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6907 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6908 | if (super_class->ShouldHaveImt()) { |
| 6909 | ImTable* super_imt = super_class->GetImt(image_pointer_size_); |
| 6910 | for (size_t i = 0; i < ImTable::kSize; ++i) { |
| 6911 | imt[i] = super_imt->Get(i, image_pointer_size_); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 6912 | } |
| 6913 | } else { |
| 6914 | // No imt in the super class, need to reconstruct from the iftable. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6915 | ObjPtr<mirror::IfTable> if_table = super_class->GetIfTable(); |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 6916 | if (if_table->Count() != 0) { |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame^] | 6917 | // Ignore copied methods, we will handle these later. |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6918 | FillIMTFromIfTable(if_table, |
| 6919 | unimplemented_method, |
| 6920 | imt_conflict_method, |
| 6921 | klass.Get(), |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 6922 | /*create_conflict_tables=*/false, |
| 6923 | /*ignore_copied_methods=*/true, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6924 | /*out*/new_conflict, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6925 | /*out*/imt); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 6926 | } |
| 6927 | } |
| 6928 | } |
| 6929 | |
Vladimir Marko | b91402f | 2021-12-21 15:55:06 +0000 | [diff] [blame] | 6930 | template <PointerSize kPointerSize> |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 6931 | class ClassLinker::LinkMethodsHelper { |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 6932 | public: |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 6933 | LinkMethodsHelper(ClassLinker* class_linker, |
| 6934 | Handle<mirror::Class> klass, |
| 6935 | Thread* self, |
| 6936 | Runtime* runtime) |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 6937 | : class_linker_(class_linker), |
| 6938 | klass_(klass), |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 6939 | self_(self), |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6940 | runtime_(runtime), |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 6941 | stack_(runtime->GetLinearAlloc()->GetArenaPool()), |
| 6942 | allocator_(&stack_), |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame^] | 6943 | copied_method_records_(copied_method_records_initial_buffer_, |
| 6944 | kCopiedMethodRecordInitialBufferSize, |
| 6945 | allocator_.Adapter()), |
| 6946 | num_new_copied_methods_(0u) { |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 6947 | } |
| 6948 | |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6949 | // Links the virtual and interface methods for the given class. |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 6950 | // |
| 6951 | // Arguments: |
| 6952 | // * self - The current thread. |
| 6953 | // * klass - class, whose vtable will be filled in. |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6954 | // * interfaces - implemented interfaces for a proxy class, otherwise null. |
| 6955 | // * out_new_conflict - whether there is a new conflict compared to the superclass. |
| 6956 | // * out_imt - interface method table to fill. |
| 6957 | bool LinkMethods( |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 6958 | Thread* self, |
| 6959 | Handle<mirror::Class> klass, |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6960 | Handle<mirror::ObjectArray<mirror::Class>> interfaces, |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 6961 | bool* out_new_conflict, |
| 6962 | ArtMethod** out_imt) |
| 6963 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 6964 | |
| 6965 | private: |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 6966 | // Assign vtable indexes to declared virtual methods for a non-interface class other |
| 6967 | // 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^] | 6968 | // This function also assigns vtable indexes for interface methods in new interfaces |
| 6969 | // 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] | 6970 | size_t AssignVTableIndexes(ObjPtr<mirror::Class> klass, |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 6971 | ObjPtr<mirror::Class> super_class, |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame^] | 6972 | size_t num_virtual_methods, |
| 6973 | ObjPtr<mirror::IfTable> iftable) |
| 6974 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 6975 | |
| 6976 | bool FindCopiedMethodsForInterface(ObjPtr<mirror::Class> klass, |
| 6977 | size_t num_virtual_methods, |
| 6978 | ObjPtr<mirror::IfTable> iftable) |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 6979 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 6980 | |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6981 | bool LinkJavaLangObjectMethods(Thread* self, Handle<mirror::Class> klass) |
Vladimir Marko | b91402f | 2021-12-21 15:55:06 +0000 | [diff] [blame] | 6982 | REQUIRES_SHARED(Locks::mutator_lock_) COLD_ATTR; |
| 6983 | |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame^] | 6984 | void ReallocMethods(ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_); |
| 6985 | void FinalizeIfTable(ObjPtr<mirror::IfTable> iftable, |
| 6986 | ObjPtr<mirror::PointerArray> vtable, |
| 6987 | bool* out_new_conflict, |
| 6988 | ArtMethod** out_imt) |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 6989 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 6990 | |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 6991 | void ClobberOldMethods(LengthPrefixedArray<ArtMethod>* old_methods, |
| 6992 | LengthPrefixedArray<ArtMethod>* methods) { |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame^] | 6993 | if (kIsDebugBuild && old_methods != nullptr) { |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 6994 | CHECK(methods != nullptr); |
| 6995 | // Put some random garbage in old methods to help find stale pointers. |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame^] | 6996 | if (methods != old_methods) { |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 6997 | // Need to make sure the GC is not running since it could be scanning the methods we are |
| 6998 | // about to overwrite. |
Vladimir Marko | ddf4fd3 | 2021-11-22 16:31:57 +0000 | [diff] [blame] | 6999 | ScopedThreadStateChange tsc(self_, ThreadState::kSuspended); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7000 | gc::ScopedGCCriticalSection gcs(self_, |
| 7001 | gc::kGcCauseClassLinker, |
| 7002 | gc::kCollectorTypeClassLinker); |
| 7003 | const size_t old_size = LengthPrefixedArray<ArtMethod>::ComputeSize(old_methods->size(), |
Vladimir Marko | b91402f | 2021-12-21 15:55:06 +0000 | [diff] [blame] | 7004 | kMethodSize, |
| 7005 | kMethodAlignment); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7006 | memset(old_methods, 0xFEu, old_size); |
| 7007 | } |
| 7008 | } |
| 7009 | } |
| 7010 | |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame^] | 7011 | NO_INLINE |
| 7012 | void LogNewVirtuals(LengthPrefixedArray<ArtMethod>* methods) const |
| 7013 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 7014 | ObjPtr<mirror::Class> klass = klass_.Get(); |
| 7015 | size_t num_new_copied_methods = num_new_copied_methods_; |
| 7016 | size_t old_method_count = methods->size() - num_new_copied_methods; |
| 7017 | size_t super_vtable_length = klass->GetSuperClass()->GetVTableLength(); |
| 7018 | size_t num_miranda_methods = 0u; |
| 7019 | size_t num_overriding_default_methods = 0u; |
| 7020 | size_t num_default_methods = 0u; |
| 7021 | size_t num_overriding_default_conflict_methods = 0u; |
| 7022 | size_t num_default_conflict_methods = 0u; |
| 7023 | for (size_t i = 0; i != num_new_copied_methods; ++i) { |
| 7024 | ArtMethod& m = methods->At(old_method_count + i, kMethodSize, kMethodAlignment); |
| 7025 | if (m.IsDefault()) { |
| 7026 | if (m.GetMethodIndexDuringLinking() < super_vtable_length) { |
| 7027 | ++num_overriding_default_methods; |
| 7028 | } else { |
| 7029 | ++num_default_methods; |
| 7030 | } |
| 7031 | } else if (m.IsDefaultConflicting()) { |
| 7032 | if (m.GetMethodIndexDuringLinking() < super_vtable_length) { |
| 7033 | ++num_overriding_default_conflict_methods; |
| 7034 | } else { |
| 7035 | ++num_default_conflict_methods; |
| 7036 | } |
| 7037 | } else { |
| 7038 | DCHECK(m.IsMiranda()); |
| 7039 | ++num_miranda_methods; |
| 7040 | } |
Vladimir Marko | 5cfb7bb | 2022-01-28 11:12:17 +0000 | [diff] [blame] | 7041 | } |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame^] | 7042 | VLOG(class_linker) << klass->PrettyClass() << ": miranda_methods=" << num_miranda_methods |
| 7043 | << " default_methods=" << num_default_methods |
| 7044 | << " overriding_default_methods=" << num_overriding_default_methods |
| 7045 | << " default_conflict_methods=" << num_default_conflict_methods |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7046 | << " overriding_default_conflict_methods=" |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame^] | 7047 | << num_overriding_default_conflict_methods; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7048 | } |
| 7049 | |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7050 | class MethodIndexEmptyFn { |
| 7051 | public: |
| 7052 | void MakeEmpty(uint32_t& item) const { |
| 7053 | item = dex::kDexNoIndex; |
| 7054 | } |
| 7055 | bool IsEmpty(const uint32_t& item) const { |
| 7056 | return item == dex::kDexNoIndex; |
| 7057 | } |
| 7058 | }; |
| 7059 | |
Vladimir Marko | 7ddae99 | 2022-01-18 14:27:20 +0000 | [diff] [blame] | 7060 | class VTableIndexCheckerDebug { |
| 7061 | protected: |
| 7062 | explicit VTableIndexCheckerDebug(size_t vtable_length) |
| 7063 | : vtable_length_(vtable_length) {} |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7064 | |
Vladimir Marko | 7ddae99 | 2022-01-18 14:27:20 +0000 | [diff] [blame] | 7065 | void CheckIndex(uint32_t index) const { |
| 7066 | CHECK_LT(index, vtable_length_); |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7067 | } |
| 7068 | |
| 7069 | private: |
Vladimir Marko | 7ddae99 | 2022-01-18 14:27:20 +0000 | [diff] [blame] | 7070 | uint32_t vtable_length_; |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7071 | }; |
| 7072 | |
Vladimir Marko | 7ddae99 | 2022-01-18 14:27:20 +0000 | [diff] [blame] | 7073 | class VTableIndexCheckerRelease { |
| 7074 | protected: |
| 7075 | explicit VTableIndexCheckerRelease(size_t vtable_length ATTRIBUTE_UNUSED) {} |
| 7076 | void CheckIndex(uint32_t index ATTRIBUTE_UNUSED) const {} |
| 7077 | }; |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7078 | |
Vladimir Marko | 7ddae99 | 2022-01-18 14:27:20 +0000 | [diff] [blame] | 7079 | using VTableIndexChecker = |
| 7080 | std::conditional_t<kIsDebugBuild, VTableIndexCheckerDebug, VTableIndexCheckerRelease>; |
| 7081 | |
| 7082 | class VTableAccessor : private VTableIndexChecker { |
| 7083 | public: |
| 7084 | VTableAccessor(uint8_t* raw_vtable, size_t vtable_length) |
| 7085 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 7086 | : VTableIndexChecker(vtable_length), |
| 7087 | raw_vtable_(raw_vtable) {} |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7088 | |
| 7089 | ArtMethod* GetVTableEntry(uint32_t index) const REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | 7ddae99 | 2022-01-18 14:27:20 +0000 | [diff] [blame] | 7090 | this->CheckIndex(index); |
| 7091 | uint8_t* entry = raw_vtable_ + static_cast<size_t>(kPointerSize) * index; |
| 7092 | if (kPointerSize == PointerSize::k64) { |
| 7093 | return reinterpret_cast64<ArtMethod*>(*reinterpret_cast<uint64_t*>(entry)); |
| 7094 | } else { |
| 7095 | return reinterpret_cast32<ArtMethod*>(*reinterpret_cast<uint32_t*>(entry)); |
| 7096 | } |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7097 | } |
| 7098 | |
| 7099 | private: |
Vladimir Marko | 7ddae99 | 2022-01-18 14:27:20 +0000 | [diff] [blame] | 7100 | uint8_t* raw_vtable_; |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7101 | }; |
| 7102 | |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7103 | class VTableSignatureHash { |
| 7104 | public: |
Vladimir Marko | 7ddae99 | 2022-01-18 14:27:20 +0000 | [diff] [blame] | 7105 | explicit VTableSignatureHash(VTableAccessor accessor) |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7106 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 7107 | : accessor_(accessor) {} |
| 7108 | |
| 7109 | // NO_THREAD_SAFETY_ANALYSIS: This is called from unannotated `HashSet<>` functions. |
| 7110 | size_t operator()(ArtMethod* method) const NO_THREAD_SAFETY_ANALYSIS { |
| 7111 | return ComputeMethodHash(method); |
| 7112 | } |
| 7113 | |
| 7114 | // NO_THREAD_SAFETY_ANALYSIS: This is called from unannotated `HashSet<>` functions. |
| 7115 | size_t operator()(uint32_t index) const NO_THREAD_SAFETY_ANALYSIS { |
| 7116 | return ComputeMethodHash(accessor_.GetVTableEntry(index)); |
| 7117 | } |
| 7118 | |
| 7119 | private: |
Vladimir Marko | 7ddae99 | 2022-01-18 14:27:20 +0000 | [diff] [blame] | 7120 | VTableAccessor accessor_; |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7121 | }; |
| 7122 | |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7123 | class VTableSignatureEqual { |
| 7124 | public: |
Vladimir Marko | 7ddae99 | 2022-01-18 14:27:20 +0000 | [diff] [blame] | 7125 | explicit VTableSignatureEqual(VTableAccessor accessor) |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7126 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 7127 | : accessor_(accessor) {} |
| 7128 | |
| 7129 | // NO_THREAD_SAFETY_ANALYSIS: This is called from unannotated `HashSet<>` functions. |
| 7130 | 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] | 7131 | return MethodSignatureEquals(accessor_.GetVTableEntry(lhs_index), rhs); |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7132 | } |
| 7133 | |
| 7134 | // NO_THREAD_SAFETY_ANALYSIS: This is called from unannotated `HashSet<>` functions. |
| 7135 | bool operator()(uint32_t lhs_index, uint32_t rhs_index) const NO_THREAD_SAFETY_ANALYSIS { |
| 7136 | return (*this)(lhs_index, accessor_.GetVTableEntry(rhs_index)); |
| 7137 | } |
| 7138 | |
| 7139 | private: |
Vladimir Marko | 7ddae99 | 2022-01-18 14:27:20 +0000 | [diff] [blame] | 7140 | VTableAccessor accessor_; |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7141 | }; |
| 7142 | |
Vladimir Marko | 7ddae99 | 2022-01-18 14:27:20 +0000 | [diff] [blame] | 7143 | using VTableSignatureSet = |
| 7144 | ScopedArenaHashSet<uint32_t, MethodIndexEmptyFn, VTableSignatureHash, VTableSignatureEqual>; |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7145 | |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame^] | 7146 | class DeclaredVirtualSignatureHash { |
| 7147 | public: |
| 7148 | explicit DeclaredVirtualSignatureHash(ObjPtr<mirror::Class> klass) |
| 7149 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 7150 | : klass_(klass) {} |
| 7151 | |
| 7152 | // NO_THREAD_SAFETY_ANALYSIS: This is called from unannotated `HashSet<>` functions. |
| 7153 | size_t operator()(ArtMethod* method) const NO_THREAD_SAFETY_ANALYSIS { |
| 7154 | return ComputeMethodHash(method); |
| 7155 | } |
| 7156 | |
| 7157 | // NO_THREAD_SAFETY_ANALYSIS: This is called from unannotated `HashSet<>` functions. |
| 7158 | size_t operator()(uint32_t index) const NO_THREAD_SAFETY_ANALYSIS { |
| 7159 | DCHECK_LT(index, klass_->NumDeclaredVirtualMethods()); |
| 7160 | ArtMethod* method = klass_->GetVirtualMethodDuringLinking(index, kPointerSize); |
| 7161 | return ComputeMethodHash(method->GetInterfaceMethodIfProxy(kPointerSize)); |
| 7162 | } |
| 7163 | |
| 7164 | private: |
| 7165 | ObjPtr<mirror::Class> klass_; |
| 7166 | }; |
| 7167 | |
| 7168 | class DeclaredVirtualSignatureEqual { |
| 7169 | public: |
| 7170 | explicit DeclaredVirtualSignatureEqual(ObjPtr<mirror::Class> klass) |
| 7171 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 7172 | : klass_(klass) {} |
| 7173 | |
| 7174 | // NO_THREAD_SAFETY_ANALYSIS: This is called from unannotated `HashSet<>` functions. |
| 7175 | bool operator()(uint32_t lhs_index, ArtMethod* rhs) const NO_THREAD_SAFETY_ANALYSIS { |
| 7176 | DCHECK_LT(lhs_index, klass_->NumDeclaredVirtualMethods()); |
| 7177 | ArtMethod* lhs = klass_->GetVirtualMethodDuringLinking(lhs_index, kPointerSize); |
| 7178 | return MethodSignatureEquals(lhs->GetInterfaceMethodIfProxy(kPointerSize), rhs); |
| 7179 | } |
| 7180 | |
| 7181 | // NO_THREAD_SAFETY_ANALYSIS: This is called from unannotated `HashSet<>` functions. |
| 7182 | bool operator()(uint32_t lhs_index, uint32_t rhs_index) const NO_THREAD_SAFETY_ANALYSIS { |
| 7183 | DCHECK_LT(lhs_index, klass_->NumDeclaredVirtualMethods()); |
| 7184 | DCHECK_LT(rhs_index, klass_->NumDeclaredVirtualMethods()); |
| 7185 | return lhs_index == rhs_index; |
| 7186 | } |
| 7187 | |
| 7188 | private: |
| 7189 | ObjPtr<mirror::Class> klass_; |
| 7190 | }; |
| 7191 | |
| 7192 | using DeclaredVirtualSignatureSet = ScopedArenaHashSet<uint32_t, |
| 7193 | MethodIndexEmptyFn, |
| 7194 | DeclaredVirtualSignatureHash, |
| 7195 | DeclaredVirtualSignatureEqual>; |
| 7196 | |
| 7197 | // Helper class to keep records for determining the correct copied method to create. |
| 7198 | class CopiedMethodRecord { |
| 7199 | public: |
| 7200 | enum class State : uint32_t { |
| 7201 | // Note: The `*Single` values are used when we know that there is only one interface |
| 7202 | // method with the given signature that's not masked; that method is the main method. |
| 7203 | // We use this knowledge for faster masking check, otherwise we need to search for |
| 7204 | // a masking method through methods of all interfaces that could potentially mask it. |
| 7205 | kAbstractSingle, |
| 7206 | kDefaultSingle, |
| 7207 | kAbstract, |
| 7208 | kDefault, |
| 7209 | kDefaultConflict, |
| 7210 | kUseSuperMethod, |
| 7211 | }; |
| 7212 | |
| 7213 | CopiedMethodRecord() |
| 7214 | : main_method_(nullptr), |
| 7215 | method_index_(0u), |
| 7216 | state_(State::kAbstractSingle) {} |
| 7217 | |
| 7218 | CopiedMethodRecord(ArtMethod* main_method, size_t vtable_index) |
| 7219 | : main_method_(main_method), |
| 7220 | method_index_(vtable_index), |
| 7221 | state_(State::kAbstractSingle) {} |
| 7222 | |
| 7223 | // Set main method. The new main method must be more specific implementation. |
| 7224 | void SetMainMethod(ArtMethod* main_method) { |
| 7225 | DCHECK(main_method_ != nullptr); |
| 7226 | main_method_ = main_method; |
| 7227 | } |
| 7228 | |
| 7229 | // The main method is the first encountered default method if any, |
| 7230 | // otherwise the first encountered abstract method. |
| 7231 | ArtMethod* GetMainMethod() const { |
| 7232 | return main_method_; |
| 7233 | } |
| 7234 | |
| 7235 | void SetMethodIndex(size_t method_index) { |
| 7236 | DCHECK_NE(method_index, dex::kDexNoIndex); |
| 7237 | method_index_ = method_index; |
| 7238 | } |
| 7239 | |
| 7240 | size_t GetMethodIndex() const { |
| 7241 | DCHECK_NE(method_index_, dex::kDexNoIndex); |
| 7242 | return method_index_; |
| 7243 | } |
| 7244 | |
| 7245 | void SetState(State state) { |
| 7246 | state_ = state; |
| 7247 | } |
| 7248 | |
| 7249 | State GetState() const { |
| 7250 | return state_; |
| 7251 | } |
| 7252 | |
| 7253 | ALWAYS_INLINE |
| 7254 | void UpdateStateForInterface(ObjPtr<mirror::Class> iface, |
| 7255 | ArtMethod* interface_method, |
| 7256 | ObjPtr<mirror::IfTable> iftable, |
| 7257 | size_t ifcount, |
| 7258 | size_t index) |
| 7259 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 7260 | DCHECK_EQ(ifcount, iftable->Count()); |
| 7261 | DCHECK_LT(index, ifcount); |
| 7262 | DCHECK(iface == interface_method->GetDeclaringClass()); |
| 7263 | DCHECK(iface == iftable->GetInterface(index)); |
| 7264 | DCHECK(interface_method->IsDefault()); |
| 7265 | if (GetState() != State::kDefaultConflict) { |
| 7266 | DCHECK(GetState() == State::kDefault); |
| 7267 | // We do not record all overriding methods, so we need to walk over all |
| 7268 | // interfaces that could mask the `interface_method`. |
| 7269 | if (ContainsOverridingMethodOf(iftable, index + 1, ifcount, iface, interface_method)) { |
| 7270 | return; // Found an overriding method that masks `interface_method`. |
| 7271 | } |
| 7272 | // We have a new default method that's not masked by any other method. |
| 7273 | SetState(State::kDefaultConflict); |
| 7274 | } |
| 7275 | } |
| 7276 | |
| 7277 | ALWAYS_INLINE |
| 7278 | void UpdateState(ObjPtr<mirror::Class> iface, |
| 7279 | ArtMethod* interface_method, |
| 7280 | size_t vtable_index, |
| 7281 | ObjPtr<mirror::IfTable> iftable, |
| 7282 | size_t ifcount, |
| 7283 | size_t index) |
| 7284 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 7285 | DCHECK_EQ(ifcount, iftable->Count()); |
| 7286 | DCHECK_LT(index, ifcount); |
| 7287 | if (kIsDebugBuild) { |
| 7288 | if (interface_method->IsCopied()) { |
| 7289 | // Called from `FinalizeState()` for a default method from superclass. |
| 7290 | // The `index` points to the last interface inherited from the superclass |
| 7291 | // as we need to search only the new interfaces for masking methods. |
| 7292 | DCHECK(interface_method->IsDefault()); |
| 7293 | } else { |
| 7294 | DCHECK(iface == interface_method->GetDeclaringClass()); |
| 7295 | DCHECK(iface == iftable->GetInterface(index)); |
| 7296 | } |
| 7297 | } |
| 7298 | DCHECK_EQ(vtable_index, method_index_); |
| 7299 | auto slow_is_masked = [=]() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 7300 | return ContainsImplementingMethod(iftable, index + 1, ifcount, iface, vtable_index); |
| 7301 | }; |
| 7302 | UpdateStateImpl(iface, interface_method, slow_is_masked); |
| 7303 | } |
| 7304 | |
| 7305 | ALWAYS_INLINE |
| 7306 | void FinalizeState(ArtMethod* super_method, |
| 7307 | size_t vtable_index, |
| 7308 | ObjPtr<mirror::IfTable> iftable, |
| 7309 | size_t ifcount, |
| 7310 | ObjPtr<mirror::IfTable> super_iftable, |
| 7311 | size_t super_ifcount) |
| 7312 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 7313 | DCHECK(super_method->IsCopied()); |
| 7314 | DCHECK_EQ(vtable_index, method_index_); |
| 7315 | DCHECK_EQ(vtable_index, super_method->GetMethodIndex()); |
| 7316 | DCHECK_NE(super_ifcount, 0u); |
| 7317 | if (super_method->IsDefault()) { |
| 7318 | if (UNLIKELY(super_method->IsDefaultConflicting())) { |
| 7319 | // Some of the default methods that contributed to the conflict in the superclass |
| 7320 | // may be masked by new interfaces. Walk over all the interfaces and update state |
| 7321 | // as long as the current state is not `kDefaultConflict`. |
| 7322 | size_t i = super_ifcount; |
| 7323 | while (GetState() != State::kDefaultConflict && i != 0u) { |
| 7324 | --i; |
| 7325 | ObjPtr<mirror::Class> iface = iftable->GetInterface(i); |
| 7326 | DCHECK(iface == super_iftable->GetInterface(i)); |
| 7327 | auto [found, index] = |
| 7328 | MethodArrayContains(super_iftable->GetMethodArrayOrNull(i), super_method); |
| 7329 | if (found) { |
| 7330 | ArtMethod* interface_method = iface->GetVirtualMethod(index, kPointerSize); |
| 7331 | auto slow_is_masked = [=]() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 7332 | // Note: The `iftable` has method arrays in range [super_ifcount, ifcount) filled |
| 7333 | // with vtable indexes but the range [0, super_ifcount) is empty, so we need to |
| 7334 | // use the `super_iftable` filled with implementation methods for that range. |
| 7335 | return ContainsImplementingMethod( |
| 7336 | super_iftable, i + 1u, super_ifcount, iface, super_method) || |
| 7337 | ContainsImplementingMethod( |
| 7338 | iftable, super_ifcount, ifcount, iface, vtable_index); |
| 7339 | }; |
| 7340 | UpdateStateImpl(iface, interface_method, slow_is_masked); |
| 7341 | } |
| 7342 | } |
| 7343 | if (GetState() == State::kDefaultConflict) { |
| 7344 | SetState(State::kUseSuperMethod); |
| 7345 | } |
| 7346 | } else { |
| 7347 | // There was exactly one default method in superclass interfaces that was |
| 7348 | // not masked by subinterfaces. Use `UpdateState()` to process it and pass |
| 7349 | // `super_ifcount - 1` as index for checking if it's been masked by new interfaces. |
| 7350 | ObjPtr<mirror::Class> iface = super_method->GetDeclaringClass(); |
| 7351 | UpdateState( |
| 7352 | iface, super_method, vtable_index, iftable, ifcount, /*index=*/ super_ifcount - 1u); |
| 7353 | if (GetMainMethod() == super_method) { |
| 7354 | DCHECK(GetState() == State::kDefault) << enum_cast<uint32_t>(GetState()); |
| 7355 | SetState(State::kUseSuperMethod); |
| 7356 | } |
| 7357 | } |
| 7358 | } else { |
| 7359 | DCHECK(super_method->IsMiranda()); |
| 7360 | // Any default methods with this signature in superclass interfaces have been |
| 7361 | // masked by subinterfaces. Check if we can reuse the miranda method. |
| 7362 | if (GetState() == State::kAbstractSingle || GetState() == State::kAbstract) { |
| 7363 | SetState(State::kUseSuperMethod); |
| 7364 | } |
| 7365 | } |
| 7366 | } |
| 7367 | |
| 7368 | private: |
| 7369 | template <typename Predicate> |
| 7370 | ALWAYS_INLINE |
| 7371 | void UpdateStateImpl(ObjPtr<mirror::Class> iface, |
| 7372 | ArtMethod* interface_method, |
| 7373 | Predicate&& slow_is_masked) |
| 7374 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 7375 | bool have_default = false; |
| 7376 | switch (GetState()) { |
| 7377 | case State::kDefaultSingle: |
| 7378 | have_default = true; |
| 7379 | FALLTHROUGH_INTENDED; |
| 7380 | case State::kAbstractSingle: |
| 7381 | if (GetMainMethod()->GetDeclaringClass()->Implements(iface)) { |
| 7382 | return; // The main method masks the `interface_method`. |
| 7383 | } |
| 7384 | if (!interface_method->IsDefault()) { |
| 7385 | SetState(have_default ? State::kDefault : State::kAbstract); |
| 7386 | return; |
| 7387 | } |
| 7388 | break; |
| 7389 | case State::kDefault: |
| 7390 | have_default = true; |
| 7391 | FALLTHROUGH_INTENDED; |
| 7392 | case State::kAbstract: |
| 7393 | if (!interface_method->IsDefault()) { |
| 7394 | return; // Keep the same state. We do not need to check for masking. |
| 7395 | } |
| 7396 | // We do not record all overriding methods, so we need to walk over all |
| 7397 | // interfaces that could mask the `interface_method`. The provided |
| 7398 | // predicate `slow_is_masked()` does that. |
| 7399 | if (slow_is_masked()) { |
| 7400 | return; // Found an overriding method that masks `interface_method`. |
| 7401 | } |
| 7402 | break; |
| 7403 | case State::kDefaultConflict: |
| 7404 | return; // The state cannot change anymore. |
| 7405 | default: |
| 7406 | LOG(FATAL) << "Unexpected state: " << enum_cast<uint32_t>(GetState()); |
| 7407 | UNREACHABLE(); |
| 7408 | } |
| 7409 | // We have a new default method that's not masked by any other method. |
| 7410 | DCHECK(interface_method->IsDefault()); |
| 7411 | if (have_default) { |
| 7412 | SetState(State::kDefaultConflict); |
| 7413 | } else { |
| 7414 | SetMainMethod(interface_method); |
| 7415 | SetState(State::kDefault); |
| 7416 | } |
| 7417 | } |
| 7418 | |
| 7419 | // Determine if the given `iftable` contains in the given range a subinterface of `iface` |
| 7420 | // that declares a method with the same name and signature as 'interface_method'. |
| 7421 | // |
| 7422 | // Arguments |
| 7423 | // - iftable: The iftable we are searching for an overriding method. |
| 7424 | // - begin: The start of the range to search. |
| 7425 | // - end: The end of the range to search. |
| 7426 | // - iface: The interface we are checking to see if anything overrides. |
| 7427 | // - interface_method: |
| 7428 | // The interface method providing a name and signature we're searching for. |
| 7429 | // |
| 7430 | // Returns whether an overriding method was found in any subinterface of `iface`. |
| 7431 | static bool ContainsOverridingMethodOf(ObjPtr<mirror::IfTable> iftable, |
| 7432 | size_t begin, |
| 7433 | size_t end, |
| 7434 | ObjPtr<mirror::Class> iface, |
| 7435 | ArtMethod* interface_method) |
| 7436 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 7437 | for (size_t i = begin; i != end; ++i) { |
| 7438 | ObjPtr<mirror::Class> current_iface = iftable->GetInterface(i); |
| 7439 | for (ArtMethod& current_method : current_iface->GetDeclaredVirtualMethods(kPointerSize)) { |
| 7440 | if (MethodSignatureEquals(¤t_method, interface_method)) { |
| 7441 | // Check if the i'th interface is a subtype of this one. |
| 7442 | if (current_iface->Implements(iface)) { |
| 7443 | return true; |
| 7444 | } |
| 7445 | break; |
| 7446 | } |
| 7447 | } |
| 7448 | } |
| 7449 | return false; |
| 7450 | } |
| 7451 | |
| 7452 | // Determine if the given `iftable` contains in the given range a subinterface of `iface` |
| 7453 | // that declares a method implemented by 'target'. This is an optimized version of |
| 7454 | // `ContainsOverridingMethodOf()` that searches implementation method arrays instead |
| 7455 | // of comparing signatures for declared interface methods. |
| 7456 | // |
| 7457 | // Arguments |
| 7458 | // - iftable: The iftable we are searching for an overriding method. |
| 7459 | // - begin: The start of the range to search. |
| 7460 | // - end: The end of the range to search. |
| 7461 | // - iface: The interface we are checking to see if anything overrides. |
| 7462 | // - target: The implementation method we're searching for. |
| 7463 | // Note that the new `iftable` is filled with vtable indexes for new interfaces, |
| 7464 | // so this needs to be the vtable index if we're searching that range. |
| 7465 | // |
| 7466 | // Returns whether the `target` was found in a method array for any subinterface of `iface`. |
| 7467 | template <typename TargetType> |
| 7468 | static bool ContainsImplementingMethod(ObjPtr<mirror::IfTable> iftable, |
| 7469 | size_t begin, |
| 7470 | size_t end, |
| 7471 | ObjPtr<mirror::Class> iface, |
| 7472 | TargetType target) |
| 7473 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 7474 | for (size_t i = begin; i != end; ++i) { |
| 7475 | if (MethodArrayContains(iftable->GetMethodArrayOrNull(i), target).first && |
| 7476 | iftable->GetInterface(i)->Implements(iface)) { |
| 7477 | return true; |
| 7478 | } |
| 7479 | } |
| 7480 | return false; |
| 7481 | } |
| 7482 | |
| 7483 | template <typename TargetType> |
| 7484 | static std::pair<bool, size_t> MethodArrayContains(ObjPtr<mirror::PointerArray> method_array, |
| 7485 | TargetType target) |
| 7486 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 7487 | size_t num_methods = (method_array != nullptr) ? method_array->GetLength() : 0u; |
| 7488 | for (size_t j = 0; j != num_methods; ++j) { |
| 7489 | if (method_array->GetElementPtrSize<TargetType, kPointerSize>(j) == target) { |
| 7490 | return {true, j}; |
| 7491 | } |
| 7492 | } |
| 7493 | return {false, 0}; |
| 7494 | } |
| 7495 | |
| 7496 | ArtMethod* main_method_; |
| 7497 | uint32_t method_index_; |
| 7498 | State state_; |
| 7499 | }; |
| 7500 | |
| 7501 | class CopiedMethodRecordEmptyFn { |
| 7502 | public: |
| 7503 | void MakeEmpty(CopiedMethodRecord& item) const { |
| 7504 | item = CopiedMethodRecord(); |
| 7505 | } |
| 7506 | bool IsEmpty(const CopiedMethodRecord& item) const { |
| 7507 | return item.GetMainMethod() == nullptr; |
| 7508 | } |
| 7509 | }; |
| 7510 | |
| 7511 | class CopiedMethodRecordHash { |
| 7512 | public: |
| 7513 | // NO_THREAD_SAFETY_ANALYSIS: This is called from unannotated `HashSet<>` functions. |
| 7514 | size_t operator()(ArtMethod* method) const NO_THREAD_SAFETY_ANALYSIS { |
| 7515 | DCHECK(method != nullptr); |
| 7516 | return ComputeMethodHash(method); |
| 7517 | } |
| 7518 | |
| 7519 | // NO_THREAD_SAFETY_ANALYSIS: This is called from unannotated `HashSet<>` functions. |
| 7520 | size_t operator()(const CopiedMethodRecord& record) const NO_THREAD_SAFETY_ANALYSIS { |
| 7521 | return (*this)(record.GetMainMethod()); |
| 7522 | } |
| 7523 | }; |
| 7524 | |
| 7525 | class CopiedMethodRecordEqual { |
| 7526 | public: |
| 7527 | // NO_THREAD_SAFETY_ANALYSIS: This is called from unannotated `HashSet<>` functions. |
| 7528 | bool operator()(const CopiedMethodRecord& lhs_record, |
| 7529 | ArtMethod* rhs) const NO_THREAD_SAFETY_ANALYSIS { |
| 7530 | ArtMethod* lhs = lhs_record.GetMainMethod(); |
| 7531 | DCHECK(lhs != nullptr); |
| 7532 | DCHECK(rhs != nullptr); |
| 7533 | return MethodSignatureEquals(lhs, rhs); |
| 7534 | } |
| 7535 | |
| 7536 | // NO_THREAD_SAFETY_ANALYSIS: This is called from unannotated `HashSet<>` functions. |
| 7537 | bool operator()(const CopiedMethodRecord& lhs_record, |
| 7538 | const CopiedMethodRecord& rhs_record) const NO_THREAD_SAFETY_ANALYSIS { |
| 7539 | return (*this)(lhs_record, rhs_record.GetMainMethod()); |
| 7540 | } |
| 7541 | }; |
| 7542 | |
| 7543 | using CopiedMethodRecordSet = ScopedArenaHashSet<CopiedMethodRecord, |
| 7544 | CopiedMethodRecordEmptyFn, |
| 7545 | CopiedMethodRecordHash, |
| 7546 | CopiedMethodRecordEqual>; |
| 7547 | |
Vladimir Marko | b91402f | 2021-12-21 15:55:06 +0000 | [diff] [blame] | 7548 | static constexpr size_t kMethodAlignment = ArtMethod::Alignment(kPointerSize); |
| 7549 | static constexpr size_t kMethodSize = ArtMethod::Size(kPointerSize); |
| 7550 | |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7551 | ClassLinker* class_linker_; |
| 7552 | Handle<mirror::Class> klass_; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7553 | Thread* const self_; |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 7554 | Runtime* const runtime_; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7555 | |
| 7556 | // These are allocated on the heap to begin, we then transfer to linear alloc when we re-create |
| 7557 | // the virtual methods array. |
| 7558 | // Need to use low 4GB arenas for compiler or else the pointers wont fit in 32 bit method array |
| 7559 | // during cross compilation. |
| 7560 | // Use the linear alloc pool since this one is in the low 4gb for the compiler. |
| 7561 | ArenaStack stack_; |
| 7562 | ScopedArenaAllocator allocator_; |
| 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 | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame^] | 7721 | void ClassLinker::LinkMethodsHelper<kPointerSize>::FinalizeIfTable( |
| 7722 | ObjPtr<mirror::IfTable> iftable, |
| 7723 | ObjPtr<mirror::PointerArray> vtable, |
| 7724 | bool* out_new_conflict, |
| 7725 | ArtMethod** out_imt) { |
| 7726 | ObjPtr<mirror::IfTable> super_iftable = klass_->GetSuperClass()->GetIfTable(); |
| 7727 | size_t ifcount = iftable->Count(); |
| 7728 | size_t super_ifcount = super_iftable->Count(); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7729 | |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame^] | 7730 | ArtMethod* const unimplemented_method = runtime_->GetImtUnimplementedMethod(); |
| 7731 | ArtMethod* const imt_conflict_method = runtime_->GetImtConflictMethod(); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7732 | |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame^] | 7733 | // For interfaces inherited from superclass, the new method arrays are empty, |
| 7734 | // so use vtable indexes from implementation methods from the superclass method array. |
| 7735 | for (size_t i = 0; i != super_ifcount; ++i) { |
| 7736 | ObjPtr<mirror::PointerArray> method_array = iftable->GetMethodArrayOrNull(i); |
| 7737 | if (method_array == nullptr) { |
| 7738 | continue; |
| 7739 | } |
| 7740 | size_t num_methods = method_array->GetLength(); |
| 7741 | ObjPtr<mirror::Class> iface = iftable->GetInterface(i); |
| 7742 | ObjPtr<mirror::PointerArray> super_method_array = super_iftable->GetMethodArrayOrNull(i); |
| 7743 | for (size_t j = 0; j != num_methods; ++j) { |
| 7744 | ArtMethod* super_implementation = |
| 7745 | super_method_array->GetElementPtrSize<ArtMethod*, kPointerSize>(j); |
| 7746 | size_t vtable_index = super_implementation->GetMethodIndex(); |
| 7747 | ArtMethod* implementation = |
| 7748 | vtable->GetElementPtrSize<ArtMethod*, kPointerSize>(vtable_index); |
| 7749 | method_array->SetElementPtrSize(j, implementation, kPointerSize); |
| 7750 | // Place method in imt if entry is empty, place conflict otherwise. |
| 7751 | ArtMethod** imt_ptr = &out_imt[iface->GetVirtualMethod(j, kPointerSize)->GetImtIndex()]; |
| 7752 | class_linker_->SetIMTRef(unimplemented_method, |
| 7753 | imt_conflict_method, |
| 7754 | implementation, |
| 7755 | /*out*/out_new_conflict, |
| 7756 | /*out*/imt_ptr); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7757 | } |
| 7758 | } |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7759 | |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame^] | 7760 | // New interface method arrays contain vtable indexes. Translate them to methods. |
| 7761 | for (size_t i = super_ifcount; i != ifcount; ++i) { |
| 7762 | ObjPtr<mirror::PointerArray> method_array = iftable->GetMethodArrayOrNull(i); |
| 7763 | if (method_array == nullptr) { |
| 7764 | continue; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7765 | } |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame^] | 7766 | size_t num_methods = method_array->GetLength(); |
| 7767 | ObjPtr<mirror::Class> iface = iftable->GetInterface(i); |
| 7768 | for (size_t j = 0; j != num_methods; ++j) { |
| 7769 | size_t vtable_index = method_array->GetElementPtrSize<size_t, kPointerSize>(j); |
| 7770 | ArtMethod* implementation = |
| 7771 | vtable->GetElementPtrSize<ArtMethod*, kPointerSize>(vtable_index); |
| 7772 | method_array->SetElementPtrSize(j, implementation, kPointerSize); |
| 7773 | // Place method in imt if entry is empty, place conflict otherwise. |
| 7774 | ArtMethod** imt_ptr = &out_imt[iface->GetVirtualMethod(j, kPointerSize)->GetImtIndex()]; |
| 7775 | class_linker_->SetIMTRef(unimplemented_method, |
| 7776 | imt_conflict_method, |
| 7777 | implementation, |
| 7778 | /*out*/out_new_conflict, |
| 7779 | /*out*/imt_ptr); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7780 | } |
| 7781 | } |
| 7782 | } |
| 7783 | |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame^] | 7784 | NO_INLINE |
| 7785 | static void ThrowIllegalAccessErrorForImplementingMethod(ObjPtr<mirror::Class> klass, |
| 7786 | ArtMethod* vtable_method, |
| 7787 | ArtMethod* interface_method) |
| 7788 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 7789 | DCHECK(!vtable_method->IsAbstract()); |
| 7790 | DCHECK(!vtable_method->IsPublic()); |
| 7791 | ThrowIllegalAccessError( |
| 7792 | klass, |
| 7793 | "Method '%s' implementing interface method '%s' is not public", |
| 7794 | vtable_method->PrettyMethod().c_str(), |
| 7795 | interface_method->PrettyMethod().c_str()); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7796 | } |
| 7797 | |
Vladimir Marko | b91402f | 2021-12-21 15:55:06 +0000 | [diff] [blame] | 7798 | template <PointerSize kPointerSize> |
Vladimir Marko | bed84ef | 2022-01-21 13:57:14 +0000 | [diff] [blame] | 7799 | size_t ClassLinker::LinkMethodsHelper<kPointerSize>::AssignVTableIndexes( |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame^] | 7800 | ObjPtr<mirror::Class> klass, |
| 7801 | ObjPtr<mirror::Class> super_class, |
| 7802 | size_t num_virtual_methods, |
| 7803 | ObjPtr<mirror::IfTable> iftable) { |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7804 | DCHECK(!klass->IsInterface()); |
| 7805 | DCHECK(klass->HasSuperClass()); |
| 7806 | DCHECK(klass->GetSuperClass() == super_class); |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7807 | |
| 7808 | // 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] | 7809 | // (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] | 7810 | std::optional<ScopedAssertNoThreadSuspension> sants(__FUNCTION__); |
| 7811 | |
| 7812 | // Prepare a hash table with virtual methods from the superclass. |
| 7813 | // For the unlikely cases that there are multiple methods with the same signature |
| 7814 | // but different vtable indexes, keep an array with indexes of the previous |
| 7815 | // methods with the same signature (walked as singly-linked lists). |
Vladimir Marko | 7ddae99 | 2022-01-18 14:27:20 +0000 | [diff] [blame] | 7816 | uint8_t* raw_super_vtable; |
| 7817 | size_t super_vtable_length; |
| 7818 | if (super_class->ShouldHaveEmbeddedVTable()) { |
| 7819 | raw_super_vtable = reinterpret_cast<uint8_t*>(super_class.Ptr()) + |
| 7820 | mirror::Class::EmbeddedVTableOffset(kPointerSize).Uint32Value(); |
| 7821 | super_vtable_length = super_class->GetEmbeddedVTableLength(); |
| 7822 | } else { |
| 7823 | ObjPtr<mirror::PointerArray> super_vtable = super_class->GetVTableDuringLinking(); |
| 7824 | DCHECK(super_vtable != nullptr); |
| 7825 | raw_super_vtable = reinterpret_cast<uint8_t*>(super_vtable.Ptr()) + |
| 7826 | mirror::Array::DataOffset(static_cast<size_t>(kPointerSize)).Uint32Value(); |
| 7827 | super_vtable_length = super_vtable->GetLength(); |
| 7828 | } |
| 7829 | VTableAccessor super_vtable_accessor(raw_super_vtable, super_vtable_length); |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7830 | static constexpr double kMinLoadFactor = 0.3; |
| 7831 | static constexpr double kMaxLoadFactor = 0.5; |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame^] | 7832 | static constexpr size_t kMaxStackBuferSize = 256; |
| 7833 | const size_t super_vtable_buffer_size = super_vtable_length * 3; |
| 7834 | const size_t declared_virtuals_buffer_size = num_virtual_methods * 3; |
| 7835 | const size_t total_buffer_size = super_vtable_buffer_size + declared_virtuals_buffer_size; |
| 7836 | uint32_t* super_vtable_buffer_ptr = (total_buffer_size <= kMaxStackBuferSize) |
| 7837 | ? reinterpret_cast<uint32_t*>(alloca(total_buffer_size * sizeof(uint32_t))) |
| 7838 | : allocator_.AllocArray<uint32_t>(total_buffer_size); |
| 7839 | 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] | 7840 | VTableSignatureSet super_vtable_signatures( |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7841 | kMinLoadFactor, |
| 7842 | kMaxLoadFactor, |
Vladimir Marko | 7ddae99 | 2022-01-18 14:27:20 +0000 | [diff] [blame] | 7843 | VTableSignatureHash(super_vtable_accessor), |
| 7844 | VTableSignatureEqual(super_vtable_accessor), |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame^] | 7845 | super_vtable_buffer_ptr, |
| 7846 | super_vtable_buffer_size, |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7847 | allocator_.Adapter()); |
Vladimir Marko | bed84ef | 2022-01-21 13:57:14 +0000 | [diff] [blame] | 7848 | ArrayRef<uint32_t> same_signature_vtable_lists; |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7849 | // Insert the first `mirror::Object::kVTableLength` indexes with pre-calculated hashes. |
| 7850 | DCHECK_GE(super_vtable_length, mirror::Object::kVTableLength); |
| 7851 | for (uint32_t i = 0; i != mirror::Object::kVTableLength; ++i) { |
| 7852 | size_t hash = class_linker_->object_virtual_method_hashes_[i]; |
Vladimir Marko | 163ebe2 | 2022-01-07 12:35:36 +0000 | [diff] [blame] | 7853 | // There are no duplicate signatures in `java.lang.Object`, so use `HashSet<>::PutWithHash()`. |
| 7854 | // This avoids equality comparison for the three `java.lang.Object.wait()` overloads. |
| 7855 | super_vtable_signatures.PutWithHash(i, hash); |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7856 | } |
| 7857 | // Insert the remaining indexes, check for duplicate signatures. |
| 7858 | if (super_vtable_length > mirror::Object::kVTableLength) { |
| 7859 | for (size_t i = mirror::Object::kVTableLength; i < super_vtable_length; ++i) { |
| 7860 | // Use `super_vtable_accessor` for getting the method for hash calculation. |
| 7861 | // Letting `HashSet<>::insert()` use the internal accessor copy in the hash |
| 7862 | // function prevents the compiler from optimizing this properly because the |
| 7863 | // compiler cannot prove that the accessor copy is immutable. |
| 7864 | size_t hash = ComputeMethodHash(super_vtable_accessor.GetVTableEntry(i)); |
| 7865 | auto [it, inserted] = super_vtable_signatures.InsertWithHash(i, hash); |
| 7866 | if (UNLIKELY(!inserted)) { |
Vladimir Marko | bed84ef | 2022-01-21 13:57:14 +0000 | [diff] [blame] | 7867 | if (same_signature_vtable_lists.empty()) { |
| 7868 | same_signature_vtable_lists = ArrayRef<uint32_t>( |
| 7869 | allocator_.AllocArray<uint32_t>(super_vtable_length), super_vtable_length); |
| 7870 | std::fill_n(same_signature_vtable_lists.data(), super_vtable_length, dex::kDexNoIndex); |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7871 | } |
| 7872 | DCHECK_LT(*it, i); |
Vladimir Marko | bed84ef | 2022-01-21 13:57:14 +0000 | [diff] [blame] | 7873 | same_signature_vtable_lists[i] = *it; |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7874 | *it = i; |
| 7875 | } |
| 7876 | } |
| 7877 | } |
| 7878 | |
| 7879 | // For each declared virtual method, look for a superclass virtual method |
| 7880 | // 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^] | 7881 | DeclaredVirtualSignatureSet declared_virtual_signatures( |
| 7882 | kMinLoadFactor, |
| 7883 | kMaxLoadFactor, |
| 7884 | DeclaredVirtualSignatureHash(klass), |
| 7885 | DeclaredVirtualSignatureEqual(klass), |
| 7886 | declared_virtuals_buffer_ptr, |
| 7887 | declared_virtuals_buffer_size, |
| 7888 | allocator_.Adapter()); |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7889 | const bool is_proxy_class = klass->IsProxyClass(); |
| 7890 | size_t vtable_length = super_vtable_length; |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame^] | 7891 | for (size_t i = 0; i != num_virtual_methods; ++i) { |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7892 | ArtMethod* virtual_method = klass->GetVirtualMethodDuringLinking(i, kPointerSize); |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame^] | 7893 | DCHECK(!virtual_method->IsStatic()) << virtual_method->PrettyMethod(); |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7894 | ArtMethod* signature_method = UNLIKELY(is_proxy_class) |
| 7895 | ? virtual_method->GetInterfaceMethodForProxyUnchecked(kPointerSize) |
| 7896 | : virtual_method; |
| 7897 | size_t hash = ComputeMethodHash(signature_method); |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame^] | 7898 | declared_virtual_signatures.PutWithHash(i, hash); |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7899 | auto it = super_vtable_signatures.FindWithHash(signature_method, hash); |
| 7900 | if (it != super_vtable_signatures.end()) { |
| 7901 | size_t super_index = *it; |
| 7902 | DCHECK_LT(super_index, super_vtable_length); |
| 7903 | ArtMethod* super_method = super_vtable_accessor.GetVTableEntry(super_index); |
| 7904 | // Historical note: Before Android 4.1, an inaccessible package-private |
| 7905 | // superclass method would have been incorrectly overridden. |
| 7906 | bool overrides = klass->CanAccessMember(super_method->GetDeclaringClass(), |
| 7907 | super_method->GetAccessFlags()); |
Vladimir Marko | bed84ef | 2022-01-21 13:57:14 +0000 | [diff] [blame] | 7908 | if (UNLIKELY(!same_signature_vtable_lists.empty())) { |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7909 | // We override only the first accessible virtual method from superclass. |
| 7910 | // TODO: Override all methods that need to be overridden according to JLS. b/211854716 |
| 7911 | size_t current_index = super_index; |
Vladimir Marko | bed84ef | 2022-01-21 13:57:14 +0000 | [diff] [blame] | 7912 | while (same_signature_vtable_lists[current_index] != dex::kDexNoIndex) { |
| 7913 | DCHECK_LT(same_signature_vtable_lists[current_index], current_index); |
| 7914 | current_index = same_signature_vtable_lists[current_index]; |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 7915 | ArtMethod* current_method = super_vtable_accessor.GetVTableEntry(current_index); |
| 7916 | if (klass->CanAccessMember(current_method->GetDeclaringClass(), |
| 7917 | current_method->GetAccessFlags())) { |
| 7918 | overrides = true; |
| 7919 | super_index = current_index; |
| 7920 | super_method = current_method; |
| 7921 | } |
| 7922 | } |
| 7923 | } |
| 7924 | if (overrides) { |
| 7925 | if (super_method->IsFinal()) { |
| 7926 | sants.reset(); |
| 7927 | ThrowLinkageError(klass, "Method %s overrides final method in class %s", |
| 7928 | virtual_method->PrettyMethod().c_str(), |
| 7929 | super_method->GetDeclaringClassDescriptor()); |
| 7930 | return 0u; |
| 7931 | } |
| 7932 | virtual_method->SetMethodIndex(super_index); |
| 7933 | continue; |
| 7934 | } |
| 7935 | } |
| 7936 | // The method does not override any method from superclass, so it needs a new vtable index. |
| 7937 | virtual_method->SetMethodIndex(vtable_length); |
| 7938 | ++vtable_length; |
| 7939 | } |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame^] | 7940 | |
| 7941 | // Assign vtable indexes for interface methods in new interfaces and store them |
| 7942 | // in implementation method arrays. These shall be replaced by actual method |
| 7943 | // pointers later. We do not need to do this for superclass interfaces as we can |
| 7944 | // get these vtable indexes from implementation methods in superclass iftable. |
| 7945 | // Record data for copied methods which shall be referenced by the vtable. |
| 7946 | const size_t ifcount = iftable->Count(); |
| 7947 | ObjPtr<mirror::IfTable> super_iftable = super_class->GetIfTable(); |
| 7948 | const size_t super_ifcount = super_iftable->Count(); |
| 7949 | for (size_t i = ifcount; i != super_ifcount; ) { |
| 7950 | --i; |
| 7951 | DCHECK_LT(i, ifcount); |
| 7952 | ObjPtr<mirror::Class> iface = iftable->GetInterface(i); |
| 7953 | ObjPtr<mirror::PointerArray> method_array = iftable->GetMethodArrayOrNull(i); |
| 7954 | size_t num_methods = (method_array != nullptr) ? method_array->GetLength() : 0u; |
| 7955 | for (size_t j = 0; j != num_methods; ++j) { |
| 7956 | ArtMethod* interface_method = iface->GetVirtualMethod(j, kPointerSize); |
| 7957 | size_t hash = ComputeMethodHash(interface_method); |
| 7958 | ArtMethod* vtable_method = nullptr; |
| 7959 | bool found = false; |
| 7960 | auto it1 = declared_virtual_signatures.FindWithHash(interface_method, hash); |
| 7961 | if (it1 != declared_virtual_signatures.end()) { |
| 7962 | vtable_method = klass->GetVirtualMethodDuringLinking(*it1, kPointerSize); |
| 7963 | found = true; |
| 7964 | } else { |
| 7965 | auto it2 = super_vtable_signatures.FindWithHash(interface_method, hash); |
| 7966 | if (it2 != super_vtable_signatures.end()) { |
| 7967 | // FIXME: If there are multiple vtable methods with the same signature, the one |
| 7968 | // with the highest vtable index is not nessarily the one in most-derived class. |
| 7969 | // However, we're preserving old behavior for now. b/211854716 |
| 7970 | vtable_method = super_vtable_accessor.GetVTableEntry(*it2); |
| 7971 | found = true; |
| 7972 | } |
| 7973 | } |
| 7974 | uint32_t vtable_index = vtable_length; |
| 7975 | if (found) { |
| 7976 | DCHECK(vtable_method != nullptr); |
| 7977 | if (!vtable_method->IsAbstract() && !vtable_method->IsPublic()) { |
| 7978 | sants.reset(); |
| 7979 | ThrowIllegalAccessErrorForImplementingMethod(klass, vtable_method, interface_method); |
| 7980 | return 0u; |
| 7981 | } |
| 7982 | vtable_index = vtable_method->GetMethodIndexDuringLinking(); |
| 7983 | if (!vtable_method->IsOverridableByDefaultMethod()) { |
| 7984 | method_array->SetElementPtrSize(j, vtable_index, kPointerSize); |
| 7985 | continue; |
| 7986 | } |
| 7987 | } |
| 7988 | |
| 7989 | auto [it, inserted] = copied_method_records_.InsertWithHash( |
| 7990 | CopiedMethodRecord(interface_method, vtable_index), hash); |
| 7991 | if (found) { |
| 7992 | DCHECK_EQ(vtable_index, it->GetMethodIndex()); |
| 7993 | } else if (inserted) { |
| 7994 | DCHECK_EQ(vtable_index, it->GetMethodIndex()); |
| 7995 | DCHECK_EQ(vtable_index, vtable_length); |
| 7996 | ++vtable_length; |
| 7997 | } else { |
| 7998 | vtable_index = it->GetMethodIndex(); |
| 7999 | } |
| 8000 | method_array->SetElementPtrSize(j, it->GetMethodIndex(), kPointerSize); |
| 8001 | if (inserted) { |
| 8002 | it->SetState(interface_method->IsAbstract() ? CopiedMethodRecord::State::kAbstractSingle |
| 8003 | : CopiedMethodRecord::State::kDefaultSingle); |
| 8004 | } else { |
| 8005 | it->UpdateState(iface, interface_method, vtable_index, iftable, ifcount, i); |
| 8006 | } |
| 8007 | } |
| 8008 | } |
| 8009 | // Finalize copied method records and check if we can reuse some methods from superclass vtable. |
| 8010 | size_t num_new_copied_methods = copied_method_records_.size(); |
| 8011 | for (CopiedMethodRecord& record : copied_method_records_) { |
| 8012 | uint32_t vtable_index = record.GetMethodIndex(); |
| 8013 | if (vtable_index < super_vtable_length) { |
| 8014 | ArtMethod* super_method = super_vtable_accessor.GetVTableEntry(record.GetMethodIndex()); |
| 8015 | DCHECK(super_method->IsOverridableByDefaultMethod()); |
| 8016 | record.FinalizeState( |
| 8017 | super_method, vtable_index, iftable, ifcount, super_iftable, super_ifcount); |
| 8018 | if (record.GetState() == CopiedMethodRecord::State::kUseSuperMethod) { |
| 8019 | --num_new_copied_methods; |
| 8020 | } |
| 8021 | } |
| 8022 | } |
| 8023 | num_new_copied_methods_ = num_new_copied_methods; |
| 8024 | |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 8025 | if (UNLIKELY(!IsUint<16>(vtable_length))) { |
| 8026 | sants.reset(); |
| 8027 | ThrowClassFormatError(klass, "Too many methods defined on class: %zd", vtable_length); |
| 8028 | return 0u; |
| 8029 | } |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame^] | 8030 | |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 8031 | return vtable_length; |
| 8032 | } |
| 8033 | |
| 8034 | template <PointerSize kPointerSize> |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame^] | 8035 | bool ClassLinker::LinkMethodsHelper<kPointerSize>::FindCopiedMethodsForInterface( |
| 8036 | ObjPtr<mirror::Class> klass, |
| 8037 | size_t num_virtual_methods, |
| 8038 | ObjPtr<mirror::IfTable> iftable) { |
| 8039 | DCHECK(klass->IsInterface()); |
| 8040 | DCHECK(klass->HasSuperClass()); |
| 8041 | DCHECK(klass->GetSuperClass()->IsObjectClass()); |
| 8042 | DCHECK_EQ(klass->GetSuperClass()->GetIfTableCount(), 0); |
| 8043 | |
| 8044 | // There should be no thread suspension unless we want to throw an exception. |
| 8045 | // (We are using `ObjPtr<>`s that are invalidated by thread suspension.) |
| 8046 | std::optional<ScopedAssertNoThreadSuspension> sants(__FUNCTION__); |
| 8047 | |
| 8048 | // Prepare a `HashSet<>` with the declared virtual methods. These mask any methods |
| 8049 | // from superinterfaces, so we can filter out matching superinterface methods. |
| 8050 | static constexpr double kMinLoadFactor = 0.3; |
| 8051 | static constexpr double kMaxLoadFactor = 0.5; |
| 8052 | static constexpr size_t kMaxStackBuferSize = 256; |
| 8053 | const size_t declared_virtuals_buffer_size = num_virtual_methods * 3; |
| 8054 | uint32_t* declared_virtuals_buffer_ptr = (declared_virtuals_buffer_size <= kMaxStackBuferSize) |
| 8055 | ? reinterpret_cast<uint32_t*>(alloca(declared_virtuals_buffer_size * sizeof(uint32_t))) |
| 8056 | : allocator_.AllocArray<uint32_t>(declared_virtuals_buffer_size); |
| 8057 | DeclaredVirtualSignatureSet declared_virtual_signatures( |
| 8058 | kMinLoadFactor, |
| 8059 | kMaxLoadFactor, |
| 8060 | DeclaredVirtualSignatureHash(klass), |
| 8061 | DeclaredVirtualSignatureEqual(klass), |
| 8062 | declared_virtuals_buffer_ptr, |
| 8063 | declared_virtuals_buffer_size, |
| 8064 | allocator_.Adapter()); |
| 8065 | for (size_t i = 0; i != num_virtual_methods; ++i) { |
| 8066 | ArtMethod* virtual_method = klass->GetVirtualMethodDuringLinking(i, kPointerSize); |
| 8067 | DCHECK(!virtual_method->IsStatic()) << virtual_method->PrettyMethod(); |
| 8068 | size_t hash = ComputeMethodHash(virtual_method); |
| 8069 | declared_virtual_signatures.PutWithHash(i, hash); |
| 8070 | } |
| 8071 | |
| 8072 | // We do not create miranda methods for interface classes, so we do not need to track |
| 8073 | // non-default (abstract) interface methods. The downside is that we cannot use the |
| 8074 | // optimized code paths with `CopiedMethodRecord::State::kDefaultSingle` and since |
| 8075 | // we do not fill method arrays for interfaces, the method search actually has to |
| 8076 | // compare signatures instead of searching for the implementing method. |
| 8077 | const size_t ifcount = iftable->Count(); |
| 8078 | size_t new_method_index = num_virtual_methods; |
| 8079 | for (size_t i = ifcount; i != 0u; ) { |
| 8080 | --i; |
| 8081 | DCHECK_LT(i, ifcount); |
| 8082 | ObjPtr<mirror::Class> iface = iftable->GetInterface(i); |
| 8083 | if (!iface->HasDefaultMethods()) { |
| 8084 | continue; // No default methods to process. |
| 8085 | } |
| 8086 | size_t num_methods = iface->NumDeclaredVirtualMethods(); |
| 8087 | for (size_t j = 0; j != num_methods; ++j) { |
| 8088 | ArtMethod* interface_method = iface->GetVirtualMethod(j, kPointerSize); |
| 8089 | if (!interface_method->IsDefault()) { |
| 8090 | continue; // Do not process this non-default method. |
| 8091 | } |
| 8092 | size_t hash = ComputeMethodHash(interface_method); |
| 8093 | auto it1 = declared_virtual_signatures.FindWithHash(interface_method, hash); |
| 8094 | if (it1 != declared_virtual_signatures.end()) { |
| 8095 | ArtMethod* virtual_method = klass->GetVirtualMethodDuringLinking(*it1, kPointerSize); |
| 8096 | if (!virtual_method->IsAbstract() && !virtual_method->IsPublic()) { |
| 8097 | sants.reset(); |
| 8098 | ThrowIllegalAccessErrorForImplementingMethod(klass, virtual_method, interface_method); |
| 8099 | return false; |
| 8100 | } |
| 8101 | continue; // This default method is masked by a method declared in this interface. |
| 8102 | } |
| 8103 | |
| 8104 | CopiedMethodRecord new_record(interface_method, new_method_index); |
| 8105 | auto it = copied_method_records_.FindWithHash(new_record, hash); |
| 8106 | if (it == copied_method_records_.end()) { |
| 8107 | // Pretend that there is another default method and try to update the state. |
| 8108 | // If the `interface_method` is not masked, the state shall change to |
| 8109 | // `kDefaultConflict`; if it is masked, the state remains `kDefault`. |
| 8110 | new_record.SetState(CopiedMethodRecord::State::kDefault); |
| 8111 | new_record.UpdateStateForInterface(iface, interface_method, iftable, ifcount, i); |
| 8112 | if (new_record.GetState() == CopiedMethodRecord::State::kDefaultConflict) { |
| 8113 | // Insert the new record with the state `kDefault`. |
| 8114 | new_record.SetState(CopiedMethodRecord::State::kDefault); |
| 8115 | copied_method_records_.PutWithHash(new_record, hash); |
| 8116 | DCHECK_EQ(new_method_index, new_record.GetMethodIndex()); |
| 8117 | ++new_method_index; |
| 8118 | } |
| 8119 | } else { |
| 8120 | it->UpdateStateForInterface(iface, interface_method, iftable, ifcount, i); |
| 8121 | } |
| 8122 | } |
| 8123 | } |
| 8124 | |
| 8125 | // Prune records without conflict. (Method indexes are updated in `ReallocMethods()`.) |
| 8126 | // We do not copy normal default methods to subinterfaces, instead we find the |
| 8127 | // default method with `Class::FindVirtualMethodForInterfaceSuper()` when needed. |
| 8128 | size_t num_new_copied_methods = copied_method_records_.size(); |
| 8129 | for (CopiedMethodRecord& record : copied_method_records_) { |
| 8130 | if (record.GetState() != CopiedMethodRecord::State::kDefaultConflict) { |
| 8131 | DCHECK(record.GetState() == CopiedMethodRecord::State::kDefault); |
| 8132 | record.SetState(CopiedMethodRecord::State::kUseSuperMethod); |
| 8133 | --num_new_copied_methods; |
| 8134 | } |
| 8135 | } |
| 8136 | num_new_copied_methods_ = num_new_copied_methods; |
| 8137 | |
| 8138 | return true; |
| 8139 | } |
| 8140 | |
| 8141 | |
| 8142 | template <PointerSize kPointerSize> |
Vladimir Marko | b91402f | 2021-12-21 15:55:06 +0000 | [diff] [blame] | 8143 | FLATTEN |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 8144 | bool ClassLinker::LinkMethodsHelper<kPointerSize>::LinkMethods( |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 8145 | Thread* self, |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 8146 | Handle<mirror::Class> klass, |
| 8147 | Handle<mirror::ObjectArray<mirror::Class>> interfaces, |
| 8148 | bool* out_new_conflict, |
| 8149 | ArtMethod** out_imt) { |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 8150 | const size_t num_virtual_methods = klass->NumVirtualMethods(); |
| 8151 | if (klass->IsInterface()) { |
| 8152 | // No vtable. |
| 8153 | if (!IsUint<16>(num_virtual_methods)) { |
| 8154 | ThrowClassFormatError(klass.Get(), "Too many methods on interface: %zu", num_virtual_methods); |
| 8155 | return false; |
| 8156 | } |
| 8157 | bool has_defaults = false; |
| 8158 | // Assign each method an IMT index and set the default flag. |
| 8159 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Vladimir Marko | b91402f | 2021-12-21 15:55:06 +0000 | [diff] [blame] | 8160 | ArtMethod* m = klass->GetVirtualMethodDuringLinking(i, kPointerSize); |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 8161 | m->SetMethodIndex(i); |
| 8162 | if (!m->IsAbstract()) { |
| 8163 | // If the dex file does not support default methods, throw ClassFormatError. |
| 8164 | // This check is necessary to protect from odd cases, such as native default |
| 8165 | // methods, that the dex file verifier permits for old dex file versions. b/157170505 |
| 8166 | // FIXME: This should be `if (!m->GetDexFile()->SupportsDefaultMethods())` but we're |
| 8167 | // currently running CTS tests for default methods with dex file version 035 which |
| 8168 | // does not support default methods. So, we limit this to native methods. b/157718952 |
| 8169 | if (m->IsNative()) { |
| 8170 | DCHECK(!m->GetDexFile()->SupportsDefaultMethods()); |
| 8171 | ThrowClassFormatError(klass.Get(), |
| 8172 | "Dex file does not support default method '%s'", |
| 8173 | m->PrettyMethod().c_str()); |
| 8174 | return false; |
| 8175 | } |
Vladimir Marko | fd0aec4 | 2022-01-27 14:27:24 +0000 | [diff] [blame] | 8176 | if (!m->IsPublic()) { |
| 8177 | // The verifier should have caught the non-public method for dex version 37. |
| 8178 | // Just warn and skip it since this is from before default-methods so we don't |
| 8179 | // really need to care that it has code. |
| 8180 | LOG(WARNING) << "Default interface method " << m->PrettyMethod() << " is not public! " |
| 8181 | << "This will be a fatal error in subsequent versions of android. " |
| 8182 | << "Continuing anyway."; |
| 8183 | } |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 8184 | m->SetAccessFlags(m->GetAccessFlags() | kAccDefault); |
| 8185 | has_defaults = true; |
| 8186 | } |
| 8187 | } |
| 8188 | // Mark that we have default methods so that we won't need to scan the virtual_methods_ array |
| 8189 | // during initialization. This is a performance optimization. We could simply traverse the |
| 8190 | // virtual_methods_ array again during initialization. |
| 8191 | if (has_defaults) { |
| 8192 | klass->SetHasDefaultMethods(); |
| 8193 | } |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 8194 | ObjPtr<mirror::IfTable> iftable = SetupInterfaceLookupTable( |
| 8195 | self, klass, &allocator_, NonProxyInterfacesAccessor(class_linker_, klass)); |
| 8196 | if (UNLIKELY(iftable == nullptr)) { |
| 8197 | self->AssertPendingException(); |
| 8198 | return false; |
| 8199 | } |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame^] | 8200 | size_t ifcount = iftable->Count(); |
| 8201 | bool have_super_with_defaults = false; |
| 8202 | for (size_t i = 0; i != ifcount; ++i) { |
| 8203 | if (iftable->GetInterface(i)->HasDefaultMethods()) { |
| 8204 | have_super_with_defaults = true; |
| 8205 | break; |
| 8206 | } |
| 8207 | } |
| 8208 | LengthPrefixedArray<ArtMethod>* old_methods = kIsDebugBuild ? klass->GetMethodsPtr() : nullptr; |
| 8209 | if (have_super_with_defaults) { |
| 8210 | if (!FindCopiedMethodsForInterface(klass.Get(), num_virtual_methods, iftable)) { |
| 8211 | self->AssertPendingException(); |
| 8212 | return false; |
| 8213 | } |
| 8214 | if (num_new_copied_methods_ != 0u) { |
| 8215 | // Re-check the number of methods. |
| 8216 | size_t final_num_virtual_methods = num_virtual_methods + num_new_copied_methods_; |
| 8217 | if (!IsUint<16>(final_num_virtual_methods)) { |
| 8218 | ThrowClassFormatError( |
| 8219 | klass.Get(), "Too many methods on interface: %zu", final_num_virtual_methods); |
| 8220 | return false; |
| 8221 | } |
| 8222 | ReallocMethods(klass.Get()); |
| 8223 | } |
| 8224 | } |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 8225 | klass->SetIfTable(iftable); |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame^] | 8226 | if (kIsDebugBuild) { |
| 8227 | // May cause thread suspension, so do this after we're done with `ObjPtr<> iftable`. |
| 8228 | ClobberOldMethods(old_methods, klass->GetMethodsPtr()); |
| 8229 | } |
| 8230 | return true; |
Vladimir Marko | b91402f | 2021-12-21 15:55:06 +0000 | [diff] [blame] | 8231 | } else if (LIKELY(klass->HasSuperClass())) { |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 8232 | // Copy IMT from superclass. It shall be updated later if needed. |
| 8233 | class_linker_->FillImtFromSuperClass(klass, |
| 8234 | runtime_->GetImtUnimplementedMethod(), |
| 8235 | runtime_->GetImtConflictMethod(), |
| 8236 | out_new_conflict, |
| 8237 | out_imt); |
| 8238 | |
| 8239 | // We set up the interface lookup table now because we need it to determine if we need |
| 8240 | // to update any vtable entries with new default method implementations. |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 8241 | StackHandleScope<3> hs(self); |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 8242 | Handle<mirror::IfTable> iftable = hs.NewHandle(UNLIKELY(klass->IsProxyClass()) |
| 8243 | ? SetupInterfaceLookupTable(self, klass, &allocator_, ProxyInterfacesAccessor(interfaces)) |
| 8244 | : SetupInterfaceLookupTable( |
| 8245 | self, klass, &allocator_, NonProxyInterfacesAccessor(class_linker_, klass))); |
| 8246 | if (UNLIKELY(iftable == nullptr)) { |
| 8247 | self->AssertPendingException(); |
| 8248 | return false; |
| 8249 | } |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 8250 | |
| 8251 | const size_t super_vtable_length = klass->GetSuperClass()->GetVTableLength(); |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 8252 | Handle<mirror::Class> super_class(hs.NewHandle(klass->GetSuperClass())); |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 8253 | |
| 8254 | // If there are no new virtual methods and no new interfaces, we can simply reuse |
| 8255 | // the vtable from superclass. We may need to make a copy if it's embedded. |
Vladimir Marko | bed84ef | 2022-01-21 13:57:14 +0000 | [diff] [blame] | 8256 | if (num_virtual_methods == 0 && |
| 8257 | static_cast<size_t>(super_class->GetIfTableCount()) == iftable->Count()) { |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 8258 | if (super_class->ShouldHaveEmbeddedVTable()) { |
| 8259 | ObjPtr<mirror::PointerArray> vtable = |
| 8260 | class_linker_->AllocPointerArray(self, super_vtable_length); |
| 8261 | if (UNLIKELY(vtable == nullptr)) { |
| 8262 | self->AssertPendingOOMException(); |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 8263 | return false; |
| 8264 | } |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 8265 | for (size_t i = 0; i < super_vtable_length; i++) { |
| 8266 | vtable->SetElementPtrSize( |
| 8267 | i, super_class->GetEmbeddedVTableEntry(i, kPointerSize), kPointerSize); |
| 8268 | } |
| 8269 | klass->SetVTable(vtable); |
| 8270 | } else { |
| 8271 | DCHECK(super_class->IsAbstract() && !super_class->IsArrayClass()); |
| 8272 | ObjPtr<mirror::PointerArray> super_vtable = super_class->GetVTable(); |
| 8273 | CHECK(super_vtable != nullptr) << super_class->PrettyClass(); |
| 8274 | klass->SetVTable(super_vtable); |
| 8275 | } |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 8276 | // The interface table from superclass has also been reused by `SetupInterfaceLookupTable()`. |
| 8277 | DCHECK(iftable.Get() == super_class->GetIfTable()) << klass->PrettyDescriptor(); |
Vladimir Marko | bed84ef | 2022-01-21 13:57:14 +0000 | [diff] [blame] | 8278 | klass->SetIfTable(iftable.Get()); |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 8279 | return true; |
| 8280 | } |
| 8281 | |
Vladimir Marko | bed84ef | 2022-01-21 13:57:14 +0000 | [diff] [blame] | 8282 | // Allocate method arrays, so that we can link interface methods without thread suspension, |
| 8283 | // otherwise GC could miss visiting newly allocated copied methods. |
| 8284 | // TODO: Do not allocate copied methods during linking, store only records about what |
| 8285 | // we need to allocate and allocate it at the end. Start with superclass iftable and |
| 8286 | // perform copy-on-write when needed to facilitate maximum memory sharing. |
| 8287 | if (!class_linker_->AllocateIfTableMethodArrays(self, klass, iftable)) { |
| 8288 | return false; |
| 8289 | } |
| 8290 | |
Vladimir Marko | 7ddae99 | 2022-01-18 14:27:20 +0000 | [diff] [blame] | 8291 | size_t final_vtable_size = |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame^] | 8292 | AssignVTableIndexes(klass.Get(), super_class.Get(), num_virtual_methods, iftable.Get()); |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 8293 | if (final_vtable_size == 0u) { |
| 8294 | self->AssertPendingException(); |
| 8295 | return false; |
| 8296 | } |
| 8297 | DCHECK(IsUint<16>(final_vtable_size)); |
| 8298 | |
| 8299 | // Allocate the new vtable. |
| 8300 | Handle<mirror::PointerArray> vtable = |
| 8301 | hs.NewHandle(class_linker_->AllocPointerArray(self, final_vtable_size)); |
| 8302 | if (UNLIKELY(vtable == nullptr)) { |
| 8303 | self->AssertPendingOOMException(); |
| 8304 | return false; |
| 8305 | } |
| 8306 | |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame^] | 8307 | LengthPrefixedArray<ArtMethod>* old_methods = kIsDebugBuild ? klass->GetMethodsPtr() : nullptr; |
| 8308 | if (num_new_copied_methods_ != 0u) { |
| 8309 | ReallocMethods(klass.Get()); |
| 8310 | } |
| 8311 | |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 8312 | // Store new virtual methods in the new vtable. |
| 8313 | for (ArtMethod& virtual_method : klass->GetVirtualMethodsSliceUnchecked(kPointerSize)) { |
| 8314 | int32_t vtable_index = virtual_method.GetMethodIndexDuringLinking(); |
| 8315 | vtable->SetElementPtrSize(vtable_index, &virtual_method, kPointerSize); |
| 8316 | } |
| 8317 | |
| 8318 | // For non-overridden vtable slots, copy a method from `super_class`. |
| 8319 | for (size_t j = 0; j != super_vtable_length; ++j) { |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame^] | 8320 | if (vtable->GetElementPtrSize<ArtMethod*, kPointerSize>(j) == nullptr) { |
| 8321 | ArtMethod* super_method = super_class->GetVTableEntry(j, kPointerSize); |
| 8322 | vtable->SetElementPtrSize(j, super_method, kPointerSize); |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 8323 | } |
| 8324 | } |
Vladimir Marko | 8670e04 | 2021-12-21 17:55:48 +0000 | [diff] [blame] | 8325 | |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame^] | 8326 | // Update the `iftable` with finalized virtual methods. |
| 8327 | FinalizeIfTable(iftable.Get(), vtable.Get(), out_new_conflict, out_imt); |
| 8328 | |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 8329 | klass->SetVTable(vtable.Get()); |
Vladimir Marko | 19366b8 | 2022-01-18 10:41:28 +0000 | [diff] [blame^] | 8330 | klass->SetIfTable(iftable.Get()); |
| 8331 | if (kIsDebugBuild) { |
| 8332 | CheckVTable(self, klass, kPointerSize); |
| 8333 | ClobberOldMethods(old_methods, klass->GetMethodsPtr()); |
| 8334 | } |
| 8335 | return true; |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 8336 | } else { |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 8337 | return LinkJavaLangObjectMethods(self, klass); |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 8338 | } |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 8339 | } |
| 8340 | |
Vladimir Marko | b91402f | 2021-12-21 15:55:06 +0000 | [diff] [blame] | 8341 | template <PointerSize kPointerSize> |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 8342 | bool ClassLinker::LinkMethodsHelper<kPointerSize>::LinkJavaLangObjectMethods( |
Vladimir Marko | b91402f | 2021-12-21 15:55:06 +0000 | [diff] [blame] | 8343 | Thread* self, |
| 8344 | Handle<mirror::Class> klass) { |
| 8345 | DCHECK_EQ(klass.Get(), GetClassRoot<mirror::Object>(class_linker_)); |
| 8346 | DCHECK_EQ(klass->NumVirtualMethods(), mirror::Object::kVTableLength); |
| 8347 | static_assert(IsUint<16>(mirror::Object::kVTableLength)); |
| 8348 | ObjPtr<mirror::PointerArray> vtable = |
| 8349 | class_linker_->AllocPointerArray(self, mirror::Object::kVTableLength); |
| 8350 | if (UNLIKELY(vtable == nullptr)) { |
| 8351 | self->AssertPendingOOMException(); |
| 8352 | return false; |
| 8353 | } |
| 8354 | for (size_t i = 0; i < mirror::Object::kVTableLength; ++i) { |
| 8355 | ArtMethod* virtual_method = klass->GetVirtualMethodDuringLinking(i, kPointerSize); |
| 8356 | vtable->SetElementPtrSize(i, virtual_method, kPointerSize); |
| 8357 | virtual_method->SetMethodIndex(i); |
| 8358 | } |
| 8359 | klass->SetVTable(vtable); |
| 8360 | InitializeObjectVirtualMethodHashes( |
| 8361 | klass.Get(), |
| 8362 | kPointerSize, |
| 8363 | ArrayRef<uint32_t>(class_linker_->object_virtual_method_hashes_)); |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 8364 | // The interface table is already allocated but there are no interface methods to link. |
| 8365 | DCHECK(klass->GetIfTable() != nullptr); |
| 8366 | DCHECK_EQ(klass->GetIfTableCount(), 0); |
Vladimir Marko | b91402f | 2021-12-21 15:55:06 +0000 | [diff] [blame] | 8367 | return true; |
| 8368 | } |
| 8369 | |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 8370 | // Populate the class vtable and itable. Compute return type indices. |
| 8371 | bool ClassLinker::LinkMethods(Thread* self, |
| 8372 | Handle<mirror::Class> klass, |
| 8373 | Handle<mirror::ObjectArray<mirror::Class>> interfaces, |
| 8374 | bool* out_new_conflict, |
| 8375 | ArtMethod** out_imt) { |
| 8376 | self->AllowThreadSuspension(); |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 8377 | // Link virtual methods then interface methods. |
| 8378 | Runtime* const runtime = Runtime::Current(); |
Vladimir Marko | b91402f | 2021-12-21 15:55:06 +0000 | [diff] [blame] | 8379 | if (LIKELY(GetImagePointerSize() == kRuntimePointerSize)) { |
| 8380 | LinkMethodsHelper<kRuntimePointerSize> helper(this, klass, self, runtime); |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 8381 | return helper.LinkMethods(self, klass, interfaces, out_new_conflict, out_imt); |
Vladimir Marko | b91402f | 2021-12-21 15:55:06 +0000 | [diff] [blame] | 8382 | } else { |
| 8383 | constexpr PointerSize kOtherPointerSize = |
| 8384 | (kRuntimePointerSize == PointerSize::k64) ? PointerSize::k32 : PointerSize::k64; |
| 8385 | LinkMethodsHelper<kOtherPointerSize> helper(this, klass, self, runtime); |
Vladimir Marko | 78f62d8 | 2022-01-10 16:25:19 +0000 | [diff] [blame] | 8386 | return helper.LinkMethods(self, klass, interfaces, out_new_conflict, out_imt); |
Vladimir Marko | b91402f | 2021-12-21 15:55:06 +0000 | [diff] [blame] | 8387 | } |
Vladimir Marko | bc89367 | 2021-11-10 15:25:46 +0000 | [diff] [blame] | 8388 | } |
| 8389 | |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8390 | class ClassLinker::LinkFieldsHelper { |
| 8391 | public: |
| 8392 | static bool LinkFields(ClassLinker* class_linker, |
| 8393 | Thread* self, |
| 8394 | Handle<mirror::Class> klass, |
| 8395 | bool is_static, |
| 8396 | size_t* class_size) |
| 8397 | REQUIRES_SHARED(Locks::mutator_lock_); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 8398 | |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8399 | private: |
| 8400 | enum class FieldTypeOrder : uint16_t; |
| 8401 | class FieldGaps; |
| 8402 | |
| 8403 | struct FieldTypeOrderAndIndex { |
| 8404 | FieldTypeOrder field_type_order; |
| 8405 | uint16_t field_index; |
| 8406 | }; |
| 8407 | |
| 8408 | static FieldTypeOrder FieldTypeOrderFromFirstDescriptorCharacter(char first_char); |
| 8409 | |
| 8410 | template <size_t kSize> |
| 8411 | static MemberOffset AssignFieldOffset(ArtField* field, MemberOffset field_offset) |
| 8412 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 8413 | }; |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 8414 | |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8415 | // We use the following order of field types for assigning offsets. |
| 8416 | // Some fields can be shuffled forward to fill gaps, see `ClassLinker::LinkFields()`. |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8417 | enum class ClassLinker::LinkFieldsHelper::FieldTypeOrder : uint16_t { |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8418 | kReference = 0u, |
| 8419 | kLong, |
| 8420 | kDouble, |
| 8421 | kInt, |
| 8422 | kFloat, |
| 8423 | kChar, |
| 8424 | kShort, |
| 8425 | kBoolean, |
| 8426 | kByte, |
| 8427 | |
| 8428 | kLast64BitType = kDouble, |
| 8429 | kLast32BitType = kFloat, |
| 8430 | kLast16BitType = kShort, |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 8431 | }; |
| 8432 | |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8433 | ALWAYS_INLINE |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8434 | ClassLinker::LinkFieldsHelper::FieldTypeOrder |
| 8435 | ClassLinker::LinkFieldsHelper::FieldTypeOrderFromFirstDescriptorCharacter(char first_char) { |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8436 | switch (first_char) { |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8437 | case 'J': |
| 8438 | return FieldTypeOrder::kLong; |
| 8439 | case 'D': |
| 8440 | return FieldTypeOrder::kDouble; |
| 8441 | case 'I': |
| 8442 | return FieldTypeOrder::kInt; |
| 8443 | case 'F': |
| 8444 | return FieldTypeOrder::kFloat; |
| 8445 | case 'C': |
| 8446 | return FieldTypeOrder::kChar; |
| 8447 | case 'S': |
| 8448 | return FieldTypeOrder::kShort; |
| 8449 | case 'Z': |
| 8450 | return FieldTypeOrder::kBoolean; |
| 8451 | case 'B': |
| 8452 | return FieldTypeOrder::kByte; |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8453 | default: |
| 8454 | DCHECK(first_char == 'L' || first_char == '[') << first_char; |
| 8455 | return FieldTypeOrder::kReference; |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8456 | } |
| 8457 | } |
| 8458 | |
| 8459 | // Gaps where we can insert fields in object layout. |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8460 | class ClassLinker::LinkFieldsHelper::FieldGaps { |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8461 | public: |
| 8462 | template <uint32_t kSize> |
| 8463 | ALWAYS_INLINE MemberOffset AlignFieldOffset(MemberOffset field_offset) { |
| 8464 | static_assert(kSize == 2u || kSize == 4u || kSize == 8u); |
| 8465 | if (!IsAligned<kSize>(field_offset.Uint32Value())) { |
| 8466 | uint32_t gap_start = field_offset.Uint32Value(); |
| 8467 | field_offset = MemberOffset(RoundUp(gap_start, kSize)); |
| 8468 | AddGaps<kSize - 1u>(gap_start, field_offset.Uint32Value()); |
| 8469 | } |
| 8470 | return field_offset; |
| 8471 | } |
| 8472 | |
| 8473 | template <uint32_t kSize> |
| 8474 | bool HasGap() const { |
| 8475 | static_assert(kSize == 1u || kSize == 2u || kSize == 4u); |
| 8476 | return (kSize == 1u && gap1_offset_ != kNoOffset) || |
| 8477 | (kSize <= 2u && gap2_offset_ != kNoOffset) || |
| 8478 | gap4_offset_ != kNoOffset; |
| 8479 | } |
| 8480 | |
| 8481 | template <uint32_t kSize> |
| 8482 | MemberOffset ReleaseGap() { |
| 8483 | static_assert(kSize == 1u || kSize == 2u || kSize == 4u); |
| 8484 | uint32_t result; |
| 8485 | if (kSize == 1u && gap1_offset_ != kNoOffset) { |
| 8486 | DCHECK(gap2_offset_ == kNoOffset || gap2_offset_ > gap1_offset_); |
| 8487 | DCHECK(gap4_offset_ == kNoOffset || gap4_offset_ > gap1_offset_); |
| 8488 | result = gap1_offset_; |
| 8489 | gap1_offset_ = kNoOffset; |
| 8490 | } else if (kSize <= 2u && gap2_offset_ != kNoOffset) { |
| 8491 | DCHECK(gap4_offset_ == kNoOffset || gap4_offset_ > gap2_offset_); |
| 8492 | result = gap2_offset_; |
| 8493 | gap2_offset_ = kNoOffset; |
| 8494 | if (kSize < 2u) { |
| 8495 | AddGaps<1u>(result + kSize, result + 2u); |
| 8496 | } |
| 8497 | } else { |
| 8498 | DCHECK_NE(gap4_offset_, kNoOffset); |
| 8499 | result = gap4_offset_; |
| 8500 | gap4_offset_ = kNoOffset; |
| 8501 | if (kSize < 4u) { |
| 8502 | AddGaps<kSize | 2u>(result + kSize, result + 4u); |
| 8503 | } |
| 8504 | } |
| 8505 | return MemberOffset(result); |
| 8506 | } |
| 8507 | |
| 8508 | private: |
| 8509 | template <uint32_t kGapsToCheck> |
| 8510 | void AddGaps(uint32_t gap_start, uint32_t gap_end) { |
| 8511 | if ((kGapsToCheck & 1u) != 0u) { |
| 8512 | DCHECK_LT(gap_start, gap_end); |
| 8513 | DCHECK_ALIGNED(gap_end, 2u); |
| 8514 | if ((gap_start & 1u) != 0u) { |
| 8515 | DCHECK_EQ(gap1_offset_, kNoOffset); |
| 8516 | gap1_offset_ = gap_start; |
| 8517 | gap_start += 1u; |
| 8518 | if (kGapsToCheck == 1u || gap_start == gap_end) { |
| 8519 | DCHECK_EQ(gap_start, gap_end); |
| 8520 | return; |
| 8521 | } |
| 8522 | } |
| 8523 | } |
| 8524 | |
| 8525 | if ((kGapsToCheck & 2u) != 0u) { |
| 8526 | DCHECK_LT(gap_start, gap_end); |
| 8527 | DCHECK_ALIGNED(gap_start, 2u); |
| 8528 | DCHECK_ALIGNED(gap_end, 4u); |
| 8529 | if ((gap_start & 2u) != 0u) { |
| 8530 | DCHECK_EQ(gap2_offset_, kNoOffset); |
| 8531 | gap2_offset_ = gap_start; |
| 8532 | gap_start += 2u; |
| 8533 | if (kGapsToCheck <= 3u || gap_start == gap_end) { |
| 8534 | DCHECK_EQ(gap_start, gap_end); |
| 8535 | return; |
| 8536 | } |
| 8537 | } |
| 8538 | } |
| 8539 | |
| 8540 | if ((kGapsToCheck & 4u) != 0u) { |
| 8541 | DCHECK_LT(gap_start, gap_end); |
| 8542 | DCHECK_ALIGNED(gap_start, 4u); |
| 8543 | DCHECK_ALIGNED(gap_end, 8u); |
| 8544 | DCHECK_EQ(gap_start + 4u, gap_end); |
| 8545 | DCHECK_EQ(gap4_offset_, kNoOffset); |
| 8546 | gap4_offset_ = gap_start; |
| 8547 | return; |
| 8548 | } |
| 8549 | |
| 8550 | DCHECK(false) << "Remaining gap: " << gap_start << " to " << gap_end |
| 8551 | << " after checking " << kGapsToCheck; |
| 8552 | } |
| 8553 | |
| 8554 | static constexpr uint32_t kNoOffset = static_cast<uint32_t>(-1); |
| 8555 | |
| 8556 | uint32_t gap4_offset_ = kNoOffset; |
| 8557 | uint32_t gap2_offset_ = kNoOffset; |
| 8558 | uint32_t gap1_offset_ = kNoOffset; |
| 8559 | }; |
| 8560 | |
| 8561 | template <size_t kSize> |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8562 | ALWAYS_INLINE |
| 8563 | MemberOffset ClassLinker::LinkFieldsHelper::AssignFieldOffset(ArtField* field, |
| 8564 | MemberOffset field_offset) { |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8565 | DCHECK_ALIGNED(field_offset.Uint32Value(), kSize); |
| 8566 | DCHECK_EQ(Primitive::ComponentSize(field->GetTypeAsPrimitiveType()), kSize); |
| 8567 | field->SetOffset(field_offset); |
| 8568 | return MemberOffset(field_offset.Uint32Value() + kSize); |
| 8569 | } |
| 8570 | |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8571 | bool ClassLinker::LinkFieldsHelper::LinkFields(ClassLinker* class_linker, |
| 8572 | Thread* self, |
| 8573 | Handle<mirror::Class> klass, |
| 8574 | bool is_static, |
| 8575 | size_t* class_size) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 8576 | self->AllowThreadSuspension(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8577 | const size_t num_fields = is_static ? klass->NumStaticFields() : klass->NumInstanceFields(); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 8578 | LengthPrefixedArray<ArtField>* const fields = is_static ? klass->GetSFieldsPtr() : |
| 8579 | klass->GetIFieldsPtr(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8580 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 8581 | // Initialize field_offset |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 8582 | MemberOffset field_offset(0); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 8583 | if (is_static) { |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8584 | field_offset = klass->GetFirstReferenceStaticFieldOffsetDuringLinking( |
| 8585 | class_linker->GetImagePointerSize()); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 8586 | } else { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 8587 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 8588 | if (super_class != nullptr) { |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 8589 | CHECK(super_class->IsResolved()) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 8590 | << klass->PrettyClass() << " " << super_class->PrettyClass(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8591 | field_offset = MemberOffset(super_class->GetObjectSize()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8592 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8593 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8594 | |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 8595 | CHECK_EQ(num_fields == 0, fields == nullptr) << klass->PrettyClass(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8596 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 8597 | // we want a relatively stable order so that adding new fields |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 8598 | // minimizes disruption of C++ version such as Class and Method. |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 8599 | // |
| 8600 | // The overall sort order order is: |
| 8601 | // 1) All object reference fields, sorted alphabetically. |
| 8602 | // 2) All java long (64-bit) integer fields, sorted alphabetically. |
| 8603 | // 3) All java double (64-bit) floating point fields, sorted alphabetically. |
| 8604 | // 4) All java int (32-bit) integer fields, sorted alphabetically. |
| 8605 | // 5) All java float (32-bit) floating point fields, sorted alphabetically. |
| 8606 | // 6) All java char (16-bit) integer fields, sorted alphabetically. |
| 8607 | // 7) All java short (16-bit) integer fields, sorted alphabetically. |
| 8608 | // 8) All java boolean (8-bit) integer fields, sorted alphabetically. |
| 8609 | // 9) All java byte (8-bit) integer fields, sorted alphabetically. |
| 8610 | // |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8611 | // (References are first to increase the chance of reference visiting |
| 8612 | // being able to take a fast path using a bitmap of references at the |
| 8613 | // start of the object, see `Class::reference_instance_offsets_`.) |
| 8614 | // |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8615 | // Once the fields are sorted in this order we will attempt to fill any gaps |
| 8616 | // that might be present in the memory layout of the structure. |
| 8617 | // Note that we shall not fill gaps between the superclass fields. |
| 8618 | |
| 8619 | // Collect fields and their "type order index" (see numbered points above). |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 8620 | const char* old_no_suspend_cause = self->StartAssertNoThreadSuspension( |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8621 | "Using plain ArtField references"); |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8622 | constexpr size_t kStackBufferEntries = 64; // Avoid allocations for small number of fields. |
| 8623 | FieldTypeOrderAndIndex stack_buffer[kStackBufferEntries]; |
| 8624 | std::vector<FieldTypeOrderAndIndex> heap_buffer; |
| 8625 | ArrayRef<FieldTypeOrderAndIndex> sorted_fields; |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8626 | if (num_fields <= kStackBufferEntries) { |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8627 | sorted_fields = ArrayRef<FieldTypeOrderAndIndex>(stack_buffer, num_fields); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8628 | } else { |
| 8629 | heap_buffer.resize(num_fields); |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8630 | sorted_fields = ArrayRef<FieldTypeOrderAndIndex>(heap_buffer); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 8631 | } |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 8632 | size_t num_reference_fields = 0; |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8633 | size_t primitive_fields_start = num_fields; |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8634 | DCHECK_LE(num_fields, 1u << 16); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8635 | for (size_t i = 0; i != num_fields; ++i) { |
| 8636 | ArtField* field = &fields->At(i); |
| 8637 | const char* descriptor = field->GetTypeDescriptor(); |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8638 | FieldTypeOrder field_type_order = FieldTypeOrderFromFirstDescriptorCharacter(descriptor[0]); |
| 8639 | uint16_t field_index = dchecked_integral_cast<uint16_t>(i); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8640 | // Insert references to the start, other fields to the end. |
| 8641 | DCHECK_LT(num_reference_fields, primitive_fields_start); |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8642 | if (field_type_order == FieldTypeOrder::kReference) { |
| 8643 | sorted_fields[num_reference_fields] = { field_type_order, field_index }; |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8644 | ++num_reference_fields; |
| 8645 | } else { |
| 8646 | --primitive_fields_start; |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8647 | sorted_fields[primitive_fields_start] = { field_type_order, field_index }; |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8648 | } |
| 8649 | } |
| 8650 | DCHECK_EQ(num_reference_fields, primitive_fields_start); |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 8651 | |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8652 | // Reference fields are already sorted by field index (and dex field index). |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8653 | DCHECK(std::is_sorted( |
| 8654 | sorted_fields.begin(), |
| 8655 | sorted_fields.begin() + num_reference_fields, |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8656 | [fields](const auto& lhs, const auto& rhs) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 8657 | ArtField* lhs_field = &fields->At(lhs.field_index); |
| 8658 | ArtField* rhs_field = &fields->At(rhs.field_index); |
| 8659 | CHECK_EQ(lhs_field->GetTypeAsPrimitiveType(), Primitive::kPrimNot); |
| 8660 | CHECK_EQ(rhs_field->GetTypeAsPrimitiveType(), Primitive::kPrimNot); |
| 8661 | CHECK_EQ(lhs_field->GetDexFieldIndex() < rhs_field->GetDexFieldIndex(), |
| 8662 | lhs.field_index < rhs.field_index); |
| 8663 | return lhs_field->GetDexFieldIndex() < rhs_field->GetDexFieldIndex(); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8664 | })); |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8665 | // 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] | 8666 | DCHECK(std::is_sorted( |
| 8667 | sorted_fields.begin() + primitive_fields_start, |
| 8668 | sorted_fields.end(), |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8669 | [fields](const auto& lhs, const auto& rhs) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 8670 | ArtField* lhs_field = &fields->At(lhs.field_index); |
| 8671 | ArtField* rhs_field = &fields->At(rhs.field_index); |
| 8672 | CHECK_NE(lhs_field->GetTypeAsPrimitiveType(), Primitive::kPrimNot); |
| 8673 | CHECK_NE(rhs_field->GetTypeAsPrimitiveType(), Primitive::kPrimNot); |
| 8674 | CHECK_EQ(lhs_field->GetDexFieldIndex() > rhs_field->GetDexFieldIndex(), |
| 8675 | lhs.field_index > rhs.field_index); |
| 8676 | return lhs.field_index > rhs.field_index; |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8677 | })); |
| 8678 | // Sort the primitive fields by the field type order, then field index. |
| 8679 | std::sort(sorted_fields.begin() + primitive_fields_start, |
| 8680 | sorted_fields.end(), |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8681 | [](const auto& lhs, const auto& rhs) { |
| 8682 | if (lhs.field_type_order != rhs.field_type_order) { |
| 8683 | return lhs.field_type_order < rhs.field_type_order; |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8684 | } else { |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8685 | return lhs.field_index < rhs.field_index; |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8686 | } |
| 8687 | }); |
| 8688 | // Primitive fields are now sorted by field size (descending), then type, then field index. |
| 8689 | DCHECK(std::is_sorted( |
| 8690 | sorted_fields.begin() + primitive_fields_start, |
| 8691 | sorted_fields.end(), |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8692 | [fields](const auto& lhs, const auto& rhs) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 8693 | ArtField* lhs_field = &fields->At(lhs.field_index); |
| 8694 | ArtField* rhs_field = &fields->At(rhs.field_index); |
| 8695 | Primitive::Type lhs_type = lhs_field->GetTypeAsPrimitiveType(); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8696 | CHECK_NE(lhs_type, Primitive::kPrimNot); |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8697 | Primitive::Type rhs_type = rhs_field->GetTypeAsPrimitiveType(); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8698 | CHECK_NE(rhs_type, Primitive::kPrimNot); |
| 8699 | if (lhs_type != rhs_type) { |
| 8700 | size_t lhs_size = Primitive::ComponentSize(lhs_type); |
| 8701 | size_t rhs_size = Primitive::ComponentSize(rhs_type); |
| 8702 | return (lhs_size != rhs_size) ? (lhs_size > rhs_size) : (lhs_type < rhs_type); |
| 8703 | } else { |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8704 | return lhs_field->GetDexFieldIndex() < rhs_field->GetDexFieldIndex(); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8705 | } |
| 8706 | })); |
| 8707 | |
| 8708 | // Process reference fields. |
| 8709 | FieldGaps field_gaps; |
| 8710 | size_t index = 0u; |
| 8711 | if (num_reference_fields != 0u) { |
| 8712 | constexpr size_t kReferenceSize = sizeof(mirror::HeapReference<mirror::Object>); |
| 8713 | field_offset = field_gaps.AlignFieldOffset<kReferenceSize>(field_offset); |
| 8714 | for (; index != num_reference_fields; ++index) { |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8715 | ArtField* field = &fields->At(sorted_fields[index].field_index); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8716 | field_offset = AssignFieldOffset<kReferenceSize>(field, field_offset); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 8717 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 8718 | } |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8719 | // Process 64-bit fields. |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8720 | if (index != num_fields && |
| 8721 | sorted_fields[index].field_type_order <= FieldTypeOrder::kLast64BitType) { |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8722 | field_offset = field_gaps.AlignFieldOffset<8u>(field_offset); |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8723 | while (index != num_fields && |
| 8724 | sorted_fields[index].field_type_order <= FieldTypeOrder::kLast64BitType) { |
| 8725 | ArtField* field = &fields->At(sorted_fields[index].field_index); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8726 | field_offset = AssignFieldOffset<8u>(field, field_offset); |
| 8727 | ++index; |
Mathieu Chartier | 5565029 | 2020-09-14 12:21:04 -0700 | [diff] [blame] | 8728 | } |
Mathieu Chartier | 5565029 | 2020-09-14 12:21:04 -0700 | [diff] [blame] | 8729 | } |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8730 | // Process 32-bit fields. |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8731 | if (index != num_fields && |
| 8732 | sorted_fields[index].field_type_order <= FieldTypeOrder::kLast32BitType) { |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8733 | field_offset = field_gaps.AlignFieldOffset<4u>(field_offset); |
| 8734 | if (field_gaps.HasGap<4u>()) { |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8735 | ArtField* field = &fields->At(sorted_fields[index].field_index); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8736 | AssignFieldOffset<4u>(field, field_gaps.ReleaseGap<4u>()); // Ignore return value. |
| 8737 | ++index; |
| 8738 | DCHECK(!field_gaps.HasGap<4u>()); // There can be only one gap for a 32-bit field. |
| 8739 | } |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8740 | while (index != num_fields && |
| 8741 | sorted_fields[index].field_type_order <= FieldTypeOrder::kLast32BitType) { |
| 8742 | ArtField* field = &fields->At(sorted_fields[index].field_index); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8743 | field_offset = AssignFieldOffset<4u>(field, field_offset); |
| 8744 | ++index; |
| 8745 | } |
| 8746 | } |
| 8747 | // Process 16-bit fields. |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8748 | if (index != num_fields && |
| 8749 | sorted_fields[index].field_type_order <= FieldTypeOrder::kLast16BitType) { |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8750 | field_offset = field_gaps.AlignFieldOffset<2u>(field_offset); |
| 8751 | while (index != num_fields && |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8752 | sorted_fields[index].field_type_order <= FieldTypeOrder::kLast16BitType && |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8753 | field_gaps.HasGap<2u>()) { |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8754 | ArtField* field = &fields->At(sorted_fields[index].field_index); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8755 | AssignFieldOffset<2u>(field, field_gaps.ReleaseGap<2u>()); // Ignore return value. |
| 8756 | ++index; |
| 8757 | } |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8758 | while (index != num_fields && |
| 8759 | sorted_fields[index].field_type_order <= FieldTypeOrder::kLast16BitType) { |
| 8760 | ArtField* field = &fields->At(sorted_fields[index].field_index); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8761 | field_offset = AssignFieldOffset<2u>(field, field_offset); |
| 8762 | ++index; |
| 8763 | } |
| 8764 | } |
| 8765 | // Process 8-bit fields. |
| 8766 | for (; index != num_fields && field_gaps.HasGap<1u>(); ++index) { |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8767 | ArtField* field = &fields->At(sorted_fields[index].field_index); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8768 | AssignFieldOffset<1u>(field, field_gaps.ReleaseGap<1u>()); // Ignore return value. |
| 8769 | } |
| 8770 | for (; index != num_fields; ++index) { |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8771 | ArtField* field = &fields->At(sorted_fields[index].field_index); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8772 | field_offset = AssignFieldOffset<1u>(field, field_offset); |
| 8773 | } |
| 8774 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 8775 | self->EndAssertNoThreadSuspension(old_no_suspend_cause); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 8776 | |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 8777 | // We lie to the GC about the java.lang.ref.Reference.referent field, so it doesn't scan it. |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8778 | DCHECK(!class_linker->init_done_ || !klass->DescriptorEquals("Ljava/lang/ref/Reference;")); |
| 8779 | if (!is_static && |
| 8780 | UNLIKELY(!class_linker->init_done_) && |
| 8781 | klass->DescriptorEquals("Ljava/lang/ref/Reference;")) { |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 8782 | // We know there are no non-reference fields in the Reference classes, and we know |
| 8783 | // that 'referent' is alphabetically last, so this is easy... |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 8784 | CHECK_EQ(num_reference_fields, num_fields) << klass->PrettyClass(); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 8785 | CHECK_STREQ(fields->At(num_fields - 1).GetName(), "referent") |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 8786 | << klass->PrettyClass(); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 8787 | --num_reference_fields; |
| 8788 | } |
| 8789 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 8790 | size_t size = field_offset.Uint32Value(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8791 | // Update klass |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 8792 | if (is_static) { |
| 8793 | klass->SetNumReferenceStaticFields(num_reference_fields); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 8794 | *class_size = size; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 8795 | } else { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8796 | klass->SetNumReferenceInstanceFields(num_reference_fields); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 8797 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 8798 | if (num_reference_fields == 0 || super_class == nullptr) { |
| 8799 | // 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] | 8800 | // super_class is null iff the class is java.lang.Object. |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 8801 | if (super_class == nullptr || |
| 8802 | (super_class->GetClassFlags() & mirror::kClassFlagNoReferenceFields) != 0) { |
| 8803 | klass->SetClassFlags(klass->GetClassFlags() | mirror::kClassFlagNoReferenceFields); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 8804 | } |
| 8805 | } |
| 8806 | if (kIsDebugBuild) { |
| 8807 | DCHECK_EQ(super_class == nullptr, klass->DescriptorEquals("Ljava/lang/Object;")); |
| 8808 | size_t total_reference_instance_fields = 0; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 8809 | ObjPtr<mirror::Class> cur_super = klass.Get(); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 8810 | while (cur_super != nullptr) { |
| 8811 | total_reference_instance_fields += cur_super->NumReferenceInstanceFieldsDuringLinking(); |
| 8812 | cur_super = cur_super->GetSuperClass(); |
| 8813 | } |
| 8814 | if (super_class == nullptr) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 8815 | CHECK_EQ(total_reference_instance_fields, 1u) << klass->PrettyDescriptor(); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 8816 | } else { |
| 8817 | // Check that there is at least num_reference_fields other than Object.class. |
| 8818 | CHECK_GE(total_reference_instance_fields, 1u + num_reference_fields) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 8819 | << klass->PrettyClass(); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 8820 | } |
| 8821 | } |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 8822 | if (!klass->IsVariableSize()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8823 | std::string temp; |
| 8824 | DCHECK_GE(size, sizeof(mirror::Object)) << klass->GetDescriptor(&temp); |
| 8825 | size_t previous_size = klass->GetObjectSize(); |
| 8826 | if (previous_size != 0) { |
| 8827 | // Make sure that we didn't originally have an incorrect size. |
| 8828 | CHECK_EQ(previous_size, size) << klass->GetDescriptor(&temp); |
Mathieu Chartier | 79b4f38 | 2013-10-23 15:21:37 -0700 | [diff] [blame] | 8829 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8830 | klass->SetObjectSize(size); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8831 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8832 | } |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 8833 | |
| 8834 | if (kIsDebugBuild) { |
| 8835 | // Make sure that the fields array is ordered by name but all reference |
| 8836 | // offsets are at the beginning as far as alignment allows. |
| 8837 | MemberOffset start_ref_offset = is_static |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8838 | ? klass->GetFirstReferenceStaticFieldOffsetDuringLinking(class_linker->image_pointer_size_) |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 8839 | : klass->GetFirstReferenceInstanceFieldOffset(); |
| 8840 | MemberOffset end_ref_offset(start_ref_offset.Uint32Value() + |
| 8841 | num_reference_fields * |
| 8842 | sizeof(mirror::HeapReference<mirror::Object>)); |
| 8843 | MemberOffset current_ref_offset = start_ref_offset; |
| 8844 | for (size_t i = 0; i < num_fields; i++) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 8845 | ArtField* field = &fields->At(i); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8846 | VLOG(class_linker) << "LinkFields: " << (is_static ? "static" : "instance") |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 8847 | << " class=" << klass->PrettyClass() << " field=" << field->PrettyField() |
| 8848 | << " offset=" << field->GetOffsetDuringLinking(); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 8849 | if (i != 0) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 8850 | ArtField* const prev_field = &fields->At(i - 1); |
Vladimir Marko | 7a7c1db | 2014-11-17 15:13:34 +0000 | [diff] [blame] | 8851 | // NOTE: The field names can be the same. This is not possible in the Java language |
| 8852 | // 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] | 8853 | DCHECK_LE(strcmp(prev_field->GetName(), field->GetName()), 0); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 8854 | } |
| 8855 | Primitive::Type type = field->GetTypeAsPrimitiveType(); |
| 8856 | bool is_primitive = type != Primitive::kPrimNot; |
| 8857 | if (klass->DescriptorEquals("Ljava/lang/ref/Reference;") && |
| 8858 | strcmp("referent", field->GetName()) == 0) { |
| 8859 | is_primitive = true; // We lied above, so we have to expect a lie here. |
| 8860 | } |
| 8861 | MemberOffset offset = field->GetOffsetDuringLinking(); |
| 8862 | if (is_primitive) { |
| 8863 | if (offset.Uint32Value() < end_ref_offset.Uint32Value()) { |
| 8864 | // Shuffled before references. |
| 8865 | size_t type_size = Primitive::ComponentSize(type); |
| 8866 | CHECK_LT(type_size, sizeof(mirror::HeapReference<mirror::Object>)); |
| 8867 | CHECK_LT(offset.Uint32Value(), start_ref_offset.Uint32Value()); |
| 8868 | CHECK_LE(offset.Uint32Value() + type_size, start_ref_offset.Uint32Value()); |
| 8869 | CHECK(!IsAligned<sizeof(mirror::HeapReference<mirror::Object>)>(offset.Uint32Value())); |
| 8870 | } |
| 8871 | } else { |
| 8872 | CHECK_EQ(current_ref_offset.Uint32Value(), offset.Uint32Value()); |
| 8873 | current_ref_offset = MemberOffset(current_ref_offset.Uint32Value() + |
| 8874 | sizeof(mirror::HeapReference<mirror::Object>)); |
| 8875 | } |
| 8876 | } |
| 8877 | CHECK_EQ(current_ref_offset.Uint32Value(), end_ref_offset.Uint32Value()); |
| 8878 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 8879 | return true; |
| 8880 | } |
| 8881 | |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8882 | bool ClassLinker::LinkInstanceFields(Thread* self, Handle<mirror::Class> klass) { |
| 8883 | CHECK(klass != nullptr); |
| 8884 | return LinkFieldsHelper::LinkFields(this, self, klass, false, nullptr); |
| 8885 | } |
| 8886 | |
| 8887 | bool ClassLinker::LinkStaticFields(Thread* self, Handle<mirror::Class> klass, size_t* class_size) { |
| 8888 | CHECK(klass != nullptr); |
| 8889 | return LinkFieldsHelper::LinkFields(this, self, klass, true, class_size); |
| 8890 | } |
| 8891 | |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 8892 | // Set the bitmap of reference instance field offsets. |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 8893 | void ClassLinker::CreateReferenceInstanceOffsets(Handle<mirror::Class> klass) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8894 | uint32_t reference_offsets = 0; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 8895 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 8896 | // 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] | 8897 | if (super_class != nullptr) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8898 | reference_offsets = super_class->GetReferenceInstanceOffsets(); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 8899 | // Compute reference offsets unless our superclass overflowed. |
| 8900 | if (reference_offsets != mirror::Class::kClassWalkSuper) { |
| 8901 | size_t num_reference_fields = klass->NumReferenceInstanceFieldsDuringLinking(); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 8902 | if (num_reference_fields != 0u) { |
| 8903 | // All of the fields that contain object references are guaranteed be grouped in memory |
| 8904 | // starting at an appropriately aligned address after super class object data. |
| 8905 | uint32_t start_offset = RoundUp(super_class->GetObjectSize(), |
| 8906 | sizeof(mirror::HeapReference<mirror::Object>)); |
| 8907 | uint32_t start_bit = (start_offset - mirror::kObjectHeaderSize) / |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 8908 | sizeof(mirror::HeapReference<mirror::Object>); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 8909 | if (start_bit + num_reference_fields > 32) { |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 8910 | reference_offsets = mirror::Class::kClassWalkSuper; |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 8911 | } else { |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 8912 | reference_offsets |= (0xffffffffu << start_bit) & |
| 8913 | (0xffffffffu >> (32 - (start_bit + num_reference_fields))); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 8914 | } |
| 8915 | } |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 8916 | } |
| 8917 | } |
Mingyao Yang | faff0f0 | 2014-09-10 12:03:22 -0700 | [diff] [blame] | 8918 | klass->SetReferenceInstanceOffsets(reference_offsets); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 8919 | } |
| 8920 | |
Vladimir Marko | 18090d1 | 2018-06-01 16:53:12 +0100 | [diff] [blame] | 8921 | ObjPtr<mirror::String> ClassLinker::DoResolveString(dex::StringIndex string_idx, |
| 8922 | ObjPtr<mirror::DexCache> dex_cache) { |
| 8923 | StackHandleScope<1> hs(Thread::Current()); |
| 8924 | Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(dex_cache)); |
| 8925 | return DoResolveString(string_idx, h_dex_cache); |
| 8926 | } |
| 8927 | |
| 8928 | ObjPtr<mirror::String> ClassLinker::DoResolveString(dex::StringIndex string_idx, |
| 8929 | Handle<mirror::DexCache> dex_cache) { |
Vladimir Marko | a64b52d | 2017-12-08 16:27:49 +0000 | [diff] [blame] | 8930 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 8931 | uint32_t utf16_length; |
| 8932 | const char* utf8_data = dex_file.StringDataAndUtf16LengthByIdx(string_idx, &utf16_length); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 8933 | ObjPtr<mirror::String> string = intern_table_->InternStrong(utf16_length, utf8_data); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 8934 | if (string != nullptr) { |
| 8935 | dex_cache->SetResolvedString(string_idx, string); |
| 8936 | } |
Vladimir Marko | 28e012a | 2017-12-07 11:22:59 +0000 | [diff] [blame] | 8937 | return string; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 8938 | } |
| 8939 | |
Vladimir Marko | 18090d1 | 2018-06-01 16:53:12 +0100 | [diff] [blame] | 8940 | ObjPtr<mirror::String> ClassLinker::DoLookupString(dex::StringIndex string_idx, |
| 8941 | ObjPtr<mirror::DexCache> dex_cache) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8942 | DCHECK(dex_cache != nullptr); |
Vladimir Marko | a64b52d | 2017-12-08 16:27:49 +0000 | [diff] [blame] | 8943 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 8944 | uint32_t utf16_length; |
| 8945 | const char* utf8_data = dex_file.StringDataAndUtf16LengthByIdx(string_idx, &utf16_length); |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 8946 | ObjPtr<mirror::String> string = |
| 8947 | intern_table_->LookupStrong(Thread::Current(), utf16_length, utf8_data); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 8948 | if (string != nullptr) { |
| 8949 | dex_cache->SetResolvedString(string_idx, string); |
| 8950 | } |
Vladimir Marko | 28e012a | 2017-12-07 11:22:59 +0000 | [diff] [blame] | 8951 | return string; |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 8952 | } |
| 8953 | |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 8954 | ObjPtr<mirror::Class> ClassLinker::DoLookupResolvedType(dex::TypeIndex type_idx, |
Vladimir Marko | 09c5ca4 | 2018-05-31 15:15:31 +0100 | [diff] [blame] | 8955 | ObjPtr<mirror::Class> referrer) { |
| 8956 | return DoLookupResolvedType(type_idx, referrer->GetDexCache(), referrer->GetClassLoader()); |
| 8957 | } |
| 8958 | |
| 8959 | ObjPtr<mirror::Class> ClassLinker::DoLookupResolvedType(dex::TypeIndex type_idx, |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 8960 | ObjPtr<mirror::DexCache> dex_cache, |
| 8961 | ObjPtr<mirror::ClassLoader> class_loader) { |
Nicolas Geoffray | ab91eef | 2021-09-14 09:48:51 +0100 | [diff] [blame] | 8962 | DCHECK(dex_cache->GetClassLoader() == class_loader); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 8963 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
| 8964 | const char* descriptor = dex_file.StringByTypeIdx(type_idx); |
Vladimir Marko | c63d967 | 2021-03-31 15:50:39 +0100 | [diff] [blame] | 8965 | ObjPtr<mirror::Class> type = LookupResolvedType(descriptor, class_loader); |
| 8966 | if (type != nullptr) { |
| 8967 | DCHECK(type->IsResolved()); |
| 8968 | dex_cache->SetResolvedType(type_idx, type); |
| 8969 | } |
| 8970 | return type; |
| 8971 | } |
| 8972 | |
| 8973 | ObjPtr<mirror::Class> ClassLinker::LookupResolvedType(const char* descriptor, |
| 8974 | ObjPtr<mirror::ClassLoader> class_loader) { |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 8975 | DCHECK_NE(*descriptor, '\0') << "descriptor is empty string"; |
| 8976 | ObjPtr<mirror::Class> type = nullptr; |
| 8977 | if (descriptor[1] == '\0') { |
| 8978 | // only the descriptors of primitive types should be 1 character long, also avoid class lookup |
| 8979 | // for primitive classes that aren't backed by dex files. |
Vladimir Marko | 9186b18 | 2018-11-06 14:55:54 +0000 | [diff] [blame] | 8980 | type = LookupPrimitiveClass(descriptor[0]); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 8981 | } else { |
| 8982 | Thread* const self = Thread::Current(); |
| 8983 | DCHECK(self != nullptr); |
| 8984 | const size_t hash = ComputeModifiedUtf8Hash(descriptor); |
| 8985 | // Find the class in the loaded classes table. |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 8986 | type = LookupClass(self, descriptor, hash, class_loader); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 8987 | } |
Vladimir Marko | c63d967 | 2021-03-31 15:50:39 +0100 | [diff] [blame] | 8988 | return (type != nullptr && type->IsResolved()) ? type : nullptr; |
Mathieu Chartier | b890130 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 8989 | } |
| 8990 | |
Andreas Gampe | b0625e0 | 2019-05-01 12:43:31 -0700 | [diff] [blame] | 8991 | template <typename RefType> |
| 8992 | ObjPtr<mirror::Class> ClassLinker::DoResolveType(dex::TypeIndex type_idx, RefType referrer) { |
Vladimir Marko | 09c5ca4 | 2018-05-31 15:15:31 +0100 | [diff] [blame] | 8993 | StackHandleScope<2> hs(Thread::Current()); |
| 8994 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache())); |
| 8995 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(referrer->GetClassLoader())); |
| 8996 | return DoResolveType(type_idx, dex_cache, class_loader); |
| 8997 | } |
| 8998 | |
Andreas Gampe | 4835d21 | 2018-11-21 14:55:10 -0800 | [diff] [blame] | 8999 | // Instantiate the above. |
| 9000 | template ObjPtr<mirror::Class> ClassLinker::DoResolveType(dex::TypeIndex type_idx, |
| 9001 | ArtField* referrer); |
| 9002 | template ObjPtr<mirror::Class> ClassLinker::DoResolveType(dex::TypeIndex type_idx, |
| 9003 | ArtMethod* referrer); |
| 9004 | template ObjPtr<mirror::Class> ClassLinker::DoResolveType(dex::TypeIndex type_idx, |
| 9005 | ObjPtr<mirror::Class> referrer); |
| 9006 | |
Vladimir Marko | 09c5ca4 | 2018-05-31 15:15:31 +0100 | [diff] [blame] | 9007 | ObjPtr<mirror::Class> ClassLinker::DoResolveType(dex::TypeIndex type_idx, |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 9008 | Handle<mirror::DexCache> dex_cache, |
| 9009 | Handle<mirror::ClassLoader> class_loader) { |
Nicolas Geoffray | ab91eef | 2021-09-14 09:48:51 +0100 | [diff] [blame] | 9010 | DCHECK(dex_cache->GetClassLoader() == class_loader.Get()); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 9011 | Thread* self = Thread::Current(); |
| 9012 | const char* descriptor = dex_cache->GetDexFile()->StringByTypeIdx(type_idx); |
| 9013 | ObjPtr<mirror::Class> resolved = FindClass(self, descriptor, class_loader); |
| 9014 | if (resolved != nullptr) { |
| 9015 | // TODO: we used to throw here if resolved's class loader was not the |
| 9016 | // boot class loader. This was to permit different classes with the |
| 9017 | // same name to be loaded simultaneously by different loaders |
| 9018 | dex_cache->SetResolvedType(type_idx, resolved); |
| 9019 | } else { |
| 9020 | CHECK(self->IsExceptionPending()) |
| 9021 | << "Expected pending exception for failed resolution of: " << descriptor; |
| 9022 | // Convert a ClassNotFoundException to a NoClassDefFoundError. |
| 9023 | StackHandleScope<1> hs(self); |
| 9024 | Handle<mirror::Throwable> cause(hs.NewHandle(self->GetException())); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 9025 | if (cause->InstanceOf(GetClassRoot(ClassRoot::kJavaLangClassNotFoundException, this))) { |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 9026 | DCHECK(resolved == nullptr); // No Handle needed to preserve resolved. |
| 9027 | self->ClearException(); |
| 9028 | ThrowNoClassDefFoundError("Failed resolution of: %s", descriptor); |
| 9029 | self->GetException()->SetCause(cause.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 9030 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 9031 | } |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 9032 | DCHECK((resolved == nullptr) || resolved->IsResolved()) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 9033 | << resolved->PrettyDescriptor() << " " << resolved->GetStatus(); |
Vladimir Marko | 28e012a | 2017-12-07 11:22:59 +0000 | [diff] [blame] | 9034 | return resolved; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 9035 | } |
| 9036 | |
Nicolas Geoffray | ea179f4 | 2018-02-08 22:30:18 +0000 | [diff] [blame] | 9037 | ArtMethod* ClassLinker::FindResolvedMethod(ObjPtr<mirror::Class> klass, |
| 9038 | ObjPtr<mirror::DexCache> dex_cache, |
| 9039 | ObjPtr<mirror::ClassLoader> class_loader, |
| 9040 | uint32_t method_idx) { |
Nicolas Geoffray | ab91eef | 2021-09-14 09:48:51 +0100 | [diff] [blame] | 9041 | DCHECK(dex_cache->GetClassLoader() == class_loader); |
Nicolas Geoffray | ea179f4 | 2018-02-08 22:30:18 +0000 | [diff] [blame] | 9042 | // Search for the method using dex_cache and method_idx. The Class::Find*Method() |
| 9043 | // functions can optimize the search if the dex_cache is the same as the DexCache |
| 9044 | // of the class, with fall-back to name and signature search otherwise. |
| 9045 | ArtMethod* resolved = nullptr; |
| 9046 | if (klass->IsInterface()) { |
| 9047 | resolved = klass->FindInterfaceMethod(dex_cache, method_idx, image_pointer_size_); |
| 9048 | } else { |
| 9049 | resolved = klass->FindClassMethod(dex_cache, method_idx, image_pointer_size_); |
| 9050 | } |
| 9051 | DCHECK(resolved == nullptr || resolved->GetDeclaringClassUnchecked() != nullptr); |
David Brazdil | 8ce3bfa | 2018-03-12 18:01:18 +0000 | [diff] [blame] | 9052 | if (resolved != nullptr && |
Nicolas Geoffray | fedff51 | 2021-02-07 21:36:33 +0000 | [diff] [blame] | 9053 | // We pass AccessMethod::kNone instead of kLinking to not warn yet on the |
| 9054 | // access, as we'll be looking if the method can be accessed through an |
| 9055 | // interface. |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 9056 | hiddenapi::ShouldDenyAccessToMember(resolved, |
| 9057 | hiddenapi::AccessContext(class_loader, dex_cache), |
Nicolas Geoffray | fedff51 | 2021-02-07 21:36:33 +0000 | [diff] [blame] | 9058 | hiddenapi::AccessMethod::kNone)) { |
| 9059 | // The resolved method that we have found cannot be accessed due to |
| 9060 | // hiddenapi (typically it is declared up the hierarchy and is not an SDK |
| 9061 | // 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] | 9062 | // part of the SDK. |
| 9063 | ArtMethod* itf_method = klass->FindAccessibleInterfaceMethod(resolved, image_pointer_size_); |
Nicolas Geoffray | fedff51 | 2021-02-07 21:36:33 +0000 | [diff] [blame] | 9064 | if (itf_method == nullptr) { |
| 9065 | // No interface method. Call ShouldDenyAccessToMember again but this time |
| 9066 | // with AccessMethod::kLinking to ensure that an appropriate warning is |
| 9067 | // logged. |
| 9068 | hiddenapi::ShouldDenyAccessToMember(resolved, |
| 9069 | hiddenapi::AccessContext(class_loader, dex_cache), |
| 9070 | hiddenapi::AccessMethod::kLinking); |
| 9071 | resolved = nullptr; |
| 9072 | } else { |
| 9073 | // We found an interface method that is accessible, continue with the resolved method. |
| 9074 | } |
David Brazdil | 8ce3bfa | 2018-03-12 18:01:18 +0000 | [diff] [blame] | 9075 | } |
Nicolas Geoffray | ea179f4 | 2018-02-08 22:30:18 +0000 | [diff] [blame] | 9076 | if (resolved != nullptr) { |
| 9077 | // In case of jmvti, the dex file gets verified before being registered, so first |
| 9078 | // check if it's registered before checking class tables. |
| 9079 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Nicolas Geoffray | befa309 | 2018-02-22 14:50:01 +0000 | [diff] [blame] | 9080 | DCHECK(!IsDexFileRegistered(Thread::Current(), dex_file) || |
| 9081 | FindClassTable(Thread::Current(), dex_cache) == ClassTableForClassLoader(class_loader)) |
Nicolas Geoffray | ea179f4 | 2018-02-08 22:30:18 +0000 | [diff] [blame] | 9082 | << "DexFile referrer: " << dex_file.GetLocation() |
| 9083 | << " ClassLoader: " << DescribeLoaders(class_loader, ""); |
| 9084 | // 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] | 9085 | dex_cache->SetResolvedMethod(method_idx, resolved); |
Nicolas Geoffray | befa309 | 2018-02-22 14:50:01 +0000 | [diff] [blame] | 9086 | // Disable the following invariant check as the verifier breaks it. b/73760543 |
| 9087 | // const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx); |
| 9088 | // DCHECK(LookupResolvedType(method_id.class_idx_, dex_cache, class_loader) != nullptr) |
| 9089 | // << "Method: " << resolved->PrettyMethod() << ", " |
| 9090 | // << "Class: " << klass->PrettyClass() << " (" << klass->GetStatus() << "), " |
| 9091 | // << "DexFile referrer: " << dex_file.GetLocation(); |
Nicolas Geoffray | ea179f4 | 2018-02-08 22:30:18 +0000 | [diff] [blame] | 9092 | } |
| 9093 | return resolved; |
| 9094 | } |
| 9095 | |
David Brazdil | 4525e0b | 2018-04-05 16:57:32 +0100 | [diff] [blame] | 9096 | // Returns true if `method` is either null or hidden. |
| 9097 | // Does not print any warnings if it is hidden. |
| 9098 | static bool CheckNoSuchMethod(ArtMethod* method, |
| 9099 | ObjPtr<mirror::DexCache> dex_cache, |
| 9100 | ObjPtr<mirror::ClassLoader> class_loader) |
| 9101 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Nicolas Geoffray | c07f488 | 2021-09-13 09:20:33 +0100 | [diff] [blame] | 9102 | DCHECK(dex_cache->GetClassLoader().Ptr() == class_loader.Ptr()); |
David Brazdil | 4525e0b | 2018-04-05 16:57:32 +0100 | [diff] [blame] | 9103 | return method == nullptr || |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 9104 | hiddenapi::ShouldDenyAccessToMember(method, |
| 9105 | hiddenapi::AccessContext(class_loader, dex_cache), |
| 9106 | hiddenapi::AccessMethod::kNone); // no warnings |
David Brazdil | 4525e0b | 2018-04-05 16:57:32 +0100 | [diff] [blame] | 9107 | } |
| 9108 | |
| 9109 | ArtMethod* ClassLinker::FindIncompatibleMethod(ObjPtr<mirror::Class> klass, |
| 9110 | ObjPtr<mirror::DexCache> dex_cache, |
| 9111 | ObjPtr<mirror::ClassLoader> class_loader, |
| 9112 | uint32_t method_idx) { |
Nicolas Geoffray | ab91eef | 2021-09-14 09:48:51 +0100 | [diff] [blame] | 9113 | DCHECK(dex_cache->GetClassLoader() == class_loader); |
David Brazdil | 4525e0b | 2018-04-05 16:57:32 +0100 | [diff] [blame] | 9114 | if (klass->IsInterface()) { |
| 9115 | ArtMethod* method = klass->FindClassMethod(dex_cache, method_idx, image_pointer_size_); |
| 9116 | return CheckNoSuchMethod(method, dex_cache, class_loader) ? nullptr : method; |
| 9117 | } else { |
| 9118 | // If there was an interface method with the same signature, we would have |
| 9119 | // found it in the "copied" methods. Only DCHECK that the interface method |
| 9120 | // really does not exist. |
| 9121 | if (kIsDebugBuild) { |
| 9122 | ArtMethod* method = |
| 9123 | klass->FindInterfaceMethod(dex_cache, method_idx, image_pointer_size_); |
| 9124 | DCHECK(CheckNoSuchMethod(method, dex_cache, class_loader)); |
| 9125 | } |
| 9126 | return nullptr; |
| 9127 | } |
| 9128 | } |
| 9129 | |
Vladimir Marko | 8901119 | 2017-12-11 13:45:05 +0000 | [diff] [blame] | 9130 | ArtMethod* ClassLinker::ResolveMethodWithoutInvokeType(uint32_t method_idx, |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 9131 | Handle<mirror::DexCache> dex_cache, |
| 9132 | Handle<mirror::ClassLoader> class_loader) { |
Nicolas Geoffray | ab91eef | 2021-09-14 09:48:51 +0100 | [diff] [blame] | 9133 | DCHECK(dex_cache->GetClassLoader() == class_loader.Get()); |
David Srbecky | 5de5efe | 2021-02-15 21:23:00 +0000 | [diff] [blame] | 9134 | ArtMethod* resolved = dex_cache->GetResolvedMethod(method_idx); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 9135 | Thread::PoisonObjectPointersIfDebug(); |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 9136 | if (resolved != nullptr) { |
| 9137 | DCHECK(!resolved->IsRuntimeMethod()); |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 9138 | DCHECK(resolved->GetDeclaringClassUnchecked() != nullptr) << resolved->GetDexMethodIndex(); |
| 9139 | return resolved; |
| 9140 | } |
| 9141 | // Fail, get the declaring class. |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9142 | const dex::MethodId& method_id = dex_cache->GetDexFile()->GetMethodId(method_idx); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 9143 | 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] | 9144 | if (klass == nullptr) { |
| 9145 | Thread::Current()->AssertPendingException(); |
| 9146 | return nullptr; |
| 9147 | } |
| 9148 | if (klass->IsInterface()) { |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9149 | resolved = klass->FindInterfaceMethod(dex_cache.Get(), method_idx, image_pointer_size_); |
| 9150 | } else { |
| 9151 | resolved = klass->FindClassMethod(dex_cache.Get(), method_idx, image_pointer_size_); |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 9152 | } |
David Brazdil | 8ce3bfa | 2018-03-12 18:01:18 +0000 | [diff] [blame] | 9153 | if (resolved != nullptr && |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 9154 | hiddenapi::ShouldDenyAccessToMember( |
| 9155 | resolved, |
| 9156 | hiddenapi::AccessContext(class_loader.Get(), dex_cache.Get()), |
| 9157 | hiddenapi::AccessMethod::kLinking)) { |
David Brazdil | 8ce3bfa | 2018-03-12 18:01:18 +0000 | [diff] [blame] | 9158 | resolved = nullptr; |
| 9159 | } |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 9160 | return resolved; |
| 9161 | } |
| 9162 | |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 9163 | ArtField* ClassLinker::LookupResolvedField(uint32_t field_idx, |
| 9164 | ObjPtr<mirror::DexCache> dex_cache, |
| 9165 | ObjPtr<mirror::ClassLoader> class_loader, |
| 9166 | bool is_static) { |
Nicolas Geoffray | c07f488 | 2021-09-13 09:20:33 +0100 | [diff] [blame] | 9167 | DCHECK(dex_cache->GetClassLoader().Ptr() == class_loader.Ptr()); |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 9168 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9169 | const dex::FieldId& field_id = dex_file.GetFieldId(field_idx); |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 9170 | ObjPtr<mirror::Class> klass = dex_cache->GetResolvedType(field_id.class_idx_); |
| 9171 | if (klass == nullptr) { |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 9172 | klass = LookupResolvedType(field_id.class_idx_, dex_cache, class_loader); |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 9173 | } |
| 9174 | if (klass == nullptr) { |
| 9175 | // The class has not been resolved yet, so the field is also unresolved. |
| 9176 | return nullptr; |
| 9177 | } |
| 9178 | DCHECK(klass->IsResolved()); |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 9179 | |
David Brazdil | 1ab0fa8 | 2018-05-04 11:28:03 +0100 | [diff] [blame] | 9180 | return FindResolvedField(klass, dex_cache, class_loader, field_idx, is_static); |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 9181 | } |
| 9182 | |
Vladimir Marko | e11dd50 | 2017-12-08 14:09:45 +0000 | [diff] [blame] | 9183 | ArtField* ClassLinker::ResolveFieldJLS(uint32_t field_idx, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 9184 | Handle<mirror::DexCache> dex_cache, |
| 9185 | Handle<mirror::ClassLoader> class_loader) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 9186 | DCHECK(dex_cache != nullptr); |
Nicolas Geoffray | ab91eef | 2021-09-14 09:48:51 +0100 | [diff] [blame] | 9187 | DCHECK(dex_cache->GetClassLoader() == class_loader.Get()); |
David Srbecky | 5de5efe | 2021-02-15 21:23:00 +0000 | [diff] [blame] | 9188 | ArtField* resolved = dex_cache->GetResolvedField(field_idx); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 9189 | Thread::PoisonObjectPointersIfDebug(); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 9190 | if (resolved != nullptr) { |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 9191 | return resolved; |
| 9192 | } |
Vladimir Marko | e11dd50 | 2017-12-08 14:09:45 +0000 | [diff] [blame] | 9193 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9194 | const dex::FieldId& field_id = dex_file.GetFieldId(field_idx); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 9195 | 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] | 9196 | if (klass == nullptr) { |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 9197 | DCHECK(Thread::Current()->IsExceptionPending()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 9198 | return nullptr; |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 9199 | } |
| 9200 | |
David Brazdil | 1ab0fa8 | 2018-05-04 11:28:03 +0100 | [diff] [blame] | 9201 | resolved = FindResolvedFieldJLS(klass, dex_cache.Get(), class_loader.Get(), field_idx); |
| 9202 | if (resolved == nullptr) { |
| 9203 | const char* name = dex_file.GetFieldName(field_id); |
| 9204 | const char* type = dex_file.GetFieldTypeDescriptor(field_id); |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 9205 | ThrowNoSuchFieldError("", klass, type, name); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 9206 | } |
| 9207 | return resolved; |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 9208 | } |
| 9209 | |
David Brazdil | 1ab0fa8 | 2018-05-04 11:28:03 +0100 | [diff] [blame] | 9210 | ArtField* ClassLinker::FindResolvedField(ObjPtr<mirror::Class> klass, |
| 9211 | ObjPtr<mirror::DexCache> dex_cache, |
| 9212 | ObjPtr<mirror::ClassLoader> class_loader, |
| 9213 | uint32_t field_idx, |
| 9214 | bool is_static) { |
Nicolas Geoffray | ab91eef | 2021-09-14 09:48:51 +0100 | [diff] [blame] | 9215 | DCHECK(dex_cache->GetClassLoader() == class_loader); |
Vladimir Marko | b10668c | 2021-06-10 09:52:53 +0100 | [diff] [blame] | 9216 | ArtField* resolved = is_static ? klass->FindStaticField(dex_cache, field_idx) |
| 9217 | : klass->FindInstanceField(dex_cache, field_idx); |
David Brazdil | 1ab0fa8 | 2018-05-04 11:28:03 +0100 | [diff] [blame] | 9218 | if (resolved != nullptr && |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 9219 | hiddenapi::ShouldDenyAccessToMember(resolved, |
| 9220 | hiddenapi::AccessContext(class_loader, dex_cache), |
| 9221 | hiddenapi::AccessMethod::kLinking)) { |
David Brazdil | 1ab0fa8 | 2018-05-04 11:28:03 +0100 | [diff] [blame] | 9222 | resolved = nullptr; |
| 9223 | } |
| 9224 | |
| 9225 | if (resolved != nullptr) { |
David Srbecky | 5de5efe | 2021-02-15 21:23:00 +0000 | [diff] [blame] | 9226 | dex_cache->SetResolvedField(field_idx, resolved); |
David Brazdil | 1ab0fa8 | 2018-05-04 11:28:03 +0100 | [diff] [blame] | 9227 | } |
| 9228 | |
| 9229 | return resolved; |
| 9230 | } |
| 9231 | |
| 9232 | ArtField* ClassLinker::FindResolvedFieldJLS(ObjPtr<mirror::Class> klass, |
| 9233 | ObjPtr<mirror::DexCache> dex_cache, |
| 9234 | ObjPtr<mirror::ClassLoader> class_loader, |
| 9235 | uint32_t field_idx) { |
Nicolas Geoffray | c07f488 | 2021-09-13 09:20:33 +0100 | [diff] [blame] | 9236 | DCHECK(dex_cache->GetClassLoader().Ptr() == class_loader.Ptr()); |
Vladimir Marko | b10668c | 2021-06-10 09:52:53 +0100 | [diff] [blame] | 9237 | ArtField* resolved = klass->FindField(dex_cache, field_idx); |
David Brazdil | 1ab0fa8 | 2018-05-04 11:28:03 +0100 | [diff] [blame] | 9238 | |
| 9239 | if (resolved != nullptr && |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 9240 | hiddenapi::ShouldDenyAccessToMember(resolved, |
| 9241 | hiddenapi::AccessContext(class_loader, dex_cache), |
| 9242 | hiddenapi::AccessMethod::kLinking)) { |
David Brazdil | 1ab0fa8 | 2018-05-04 11:28:03 +0100 | [diff] [blame] | 9243 | resolved = nullptr; |
| 9244 | } |
| 9245 | |
| 9246 | if (resolved != nullptr) { |
David Srbecky | 5de5efe | 2021-02-15 21:23:00 +0000 | [diff] [blame] | 9247 | dex_cache->SetResolvedField(field_idx, resolved); |
David Brazdil | 1ab0fa8 | 2018-05-04 11:28:03 +0100 | [diff] [blame] | 9248 | } |
| 9249 | |
| 9250 | return resolved; |
| 9251 | } |
| 9252 | |
Vladimir Marko | af94020 | 2017-12-08 15:01:18 +0000 | [diff] [blame] | 9253 | ObjPtr<mirror::MethodType> ClassLinker::ResolveMethodType( |
| 9254 | Thread* self, |
Orion Hodson | 06d10a7 | 2018-05-14 08:53:38 +0100 | [diff] [blame] | 9255 | dex::ProtoIndex proto_idx, |
Vladimir Marko | af94020 | 2017-12-08 15:01:18 +0000 | [diff] [blame] | 9256 | Handle<mirror::DexCache> dex_cache, |
| 9257 | Handle<mirror::ClassLoader> class_loader) { |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9258 | DCHECK(Runtime::Current()->IsMethodHandlesEnabled()); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 9259 | DCHECK(dex_cache != nullptr); |
Nicolas Geoffray | ab91eef | 2021-09-14 09:48:51 +0100 | [diff] [blame] | 9260 | DCHECK(dex_cache->GetClassLoader() == class_loader.Get()); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9261 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 9262 | ObjPtr<mirror::MethodType> resolved = dex_cache->GetResolvedMethodType(proto_idx); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9263 | if (resolved != nullptr) { |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 9264 | return resolved; |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9265 | } |
| 9266 | |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9267 | StackHandleScope<4> hs(self); |
| 9268 | |
| 9269 | // First resolve the return type. |
Vladimir Marko | af94020 | 2017-12-08 15:01:18 +0000 | [diff] [blame] | 9270 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9271 | const dex::ProtoId& proto_id = dex_file.GetProtoId(proto_idx); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9272 | Handle<mirror::Class> return_type(hs.NewHandle( |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 9273 | ResolveType(proto_id.return_type_idx_, dex_cache, class_loader))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 9274 | if (return_type == nullptr) { |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9275 | DCHECK(self->IsExceptionPending()); |
| 9276 | return nullptr; |
| 9277 | } |
| 9278 | |
| 9279 | // Then resolve the argument types. |
| 9280 | // |
| 9281 | // TODO: Is there a better way to figure out the number of method arguments |
| 9282 | // other than by looking at the shorty ? |
| 9283 | const size_t num_method_args = strlen(dex_file.StringDataByIdx(proto_id.shorty_idx_)) - 1; |
| 9284 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 9285 | ObjPtr<mirror::Class> array_of_class = GetClassRoot<mirror::ObjectArray<mirror::Class>>(this); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9286 | Handle<mirror::ObjectArray<mirror::Class>> method_params(hs.NewHandle( |
| 9287 | mirror::ObjectArray<mirror::Class>::Alloc(self, array_of_class, num_method_args))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 9288 | if (method_params == nullptr) { |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9289 | DCHECK(self->IsExceptionPending()); |
| 9290 | return nullptr; |
| 9291 | } |
| 9292 | |
| 9293 | DexFileParameterIterator it(dex_file, proto_id); |
| 9294 | int32_t i = 0; |
| 9295 | MutableHandle<mirror::Class> param_class = hs.NewHandle<mirror::Class>(nullptr); |
| 9296 | for (; it.HasNext(); it.Next()) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 9297 | const dex::TypeIndex type_idx = it.GetTypeIdx(); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 9298 | param_class.Assign(ResolveType(type_idx, dex_cache, class_loader)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 9299 | if (param_class == nullptr) { |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9300 | DCHECK(self->IsExceptionPending()); |
| 9301 | return nullptr; |
| 9302 | } |
| 9303 | |
| 9304 | method_params->Set(i++, param_class.Get()); |
| 9305 | } |
| 9306 | |
| 9307 | DCHECK(!it.HasNext()); |
| 9308 | |
| 9309 | Handle<mirror::MethodType> type = hs.NewHandle( |
| 9310 | mirror::MethodType::Create(self, return_type, method_params)); |
Nicolas Geoffray | 4aebd58 | 2021-07-23 15:27:31 +0100 | [diff] [blame] | 9311 | if (type != nullptr) { |
| 9312 | dex_cache->SetResolvedMethodType(proto_idx, type.Get()); |
| 9313 | } |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9314 | |
| 9315 | return type.Get(); |
| 9316 | } |
| 9317 | |
Vladimir Marko | af94020 | 2017-12-08 15:01:18 +0000 | [diff] [blame] | 9318 | ObjPtr<mirror::MethodType> ClassLinker::ResolveMethodType(Thread* self, |
Orion Hodson | 06d10a7 | 2018-05-14 08:53:38 +0100 | [diff] [blame] | 9319 | dex::ProtoIndex proto_idx, |
Vladimir Marko | af94020 | 2017-12-08 15:01:18 +0000 | [diff] [blame] | 9320 | ArtMethod* referrer) { |
Orion Hodson | 2e59994 | 2017-09-22 16:17:41 +0100 | [diff] [blame] | 9321 | StackHandleScope<2> hs(self); |
Orion Hodson | 2e59994 | 2017-09-22 16:17:41 +0100 | [diff] [blame] | 9322 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache())); |
| 9323 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(referrer->GetClassLoader())); |
Vladimir Marko | af94020 | 2017-12-08 15:01:18 +0000 | [diff] [blame] | 9324 | return ResolveMethodType(self, proto_idx, dex_cache, class_loader); |
Orion Hodson | 2e59994 | 2017-09-22 16:17:41 +0100 | [diff] [blame] | 9325 | } |
| 9326 | |
Vladimir Marko | 5aead70 | 2019-03-27 11:00:36 +0000 | [diff] [blame] | 9327 | ObjPtr<mirror::MethodHandle> ClassLinker::ResolveMethodHandleForField( |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9328 | Thread* self, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9329 | const dex::MethodHandleItem& method_handle, |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9330 | ArtMethod* referrer) { |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9331 | DexFile::MethodHandleType handle_type = |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9332 | static_cast<DexFile::MethodHandleType>(method_handle.method_handle_type_); |
| 9333 | mirror::MethodHandle::Kind kind; |
Orion Hodson | fd7b2c2 | 2018-03-15 15:38:38 +0000 | [diff] [blame] | 9334 | bool is_put; |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9335 | bool is_static; |
| 9336 | int32_t num_params; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9337 | switch (handle_type) { |
| 9338 | case DexFile::MethodHandleType::kStaticPut: { |
Orion Hodson | 82b351f | 2017-07-05 14:34:25 +0100 | [diff] [blame] | 9339 | kind = mirror::MethodHandle::Kind::kStaticPut; |
Orion Hodson | fd7b2c2 | 2018-03-15 15:38:38 +0000 | [diff] [blame] | 9340 | is_put = true; |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9341 | is_static = true; |
| 9342 | num_params = 1; |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 9343 | break; |
| 9344 | } |
| 9345 | case DexFile::MethodHandleType::kStaticGet: { |
Orion Hodson | 82b351f | 2017-07-05 14:34:25 +0100 | [diff] [blame] | 9346 | kind = mirror::MethodHandle::Kind::kStaticGet; |
Orion Hodson | fd7b2c2 | 2018-03-15 15:38:38 +0000 | [diff] [blame] | 9347 | is_put = false; |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9348 | is_static = true; |
| 9349 | num_params = 0; |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 9350 | break; |
| 9351 | } |
| 9352 | case DexFile::MethodHandleType::kInstancePut: { |
Orion Hodson | 82b351f | 2017-07-05 14:34:25 +0100 | [diff] [blame] | 9353 | kind = mirror::MethodHandle::Kind::kInstancePut; |
Orion Hodson | fd7b2c2 | 2018-03-15 15:38:38 +0000 | [diff] [blame] | 9354 | is_put = true; |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9355 | is_static = false; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9356 | num_params = 2; |
| 9357 | break; |
| 9358 | } |
| 9359 | case DexFile::MethodHandleType::kInstanceGet: { |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9360 | kind = mirror::MethodHandle::Kind::kInstanceGet; |
Orion Hodson | fd7b2c2 | 2018-03-15 15:38:38 +0000 | [diff] [blame] | 9361 | is_put = false; |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9362 | is_static = false; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9363 | num_params = 1; |
| 9364 | break; |
| 9365 | } |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9366 | case DexFile::MethodHandleType::kInvokeStatic: |
Orion Hodson | 82b351f | 2017-07-05 14:34:25 +0100 | [diff] [blame] | 9367 | case DexFile::MethodHandleType::kInvokeInstance: |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9368 | case DexFile::MethodHandleType::kInvokeConstructor: |
Orion Hodson | 82b351f | 2017-07-05 14:34:25 +0100 | [diff] [blame] | 9369 | case DexFile::MethodHandleType::kInvokeDirect: |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9370 | case DexFile::MethodHandleType::kInvokeInterface: |
| 9371 | UNREACHABLE(); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9372 | } |
| 9373 | |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9374 | ArtField* target_field = |
| 9375 | ResolveField(method_handle.field_or_method_idx_, referrer, is_static); |
| 9376 | if (LIKELY(target_field != nullptr)) { |
| 9377 | ObjPtr<mirror::Class> target_class = target_field->GetDeclaringClass(); |
| 9378 | ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass(); |
| 9379 | if (UNLIKELY(!referring_class->CanAccessMember(target_class, target_field->GetAccessFlags()))) { |
| 9380 | ThrowIllegalAccessErrorField(referring_class, target_field); |
| 9381 | return nullptr; |
| 9382 | } |
Orion Hodson | fd7b2c2 | 2018-03-15 15:38:38 +0000 | [diff] [blame] | 9383 | if (UNLIKELY(is_put && target_field->IsFinal())) { |
| 9384 | ThrowIllegalAccessErrorField(referring_class, target_field); |
| 9385 | return nullptr; |
| 9386 | } |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9387 | } else { |
| 9388 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 9389 | return nullptr; |
| 9390 | } |
| 9391 | |
| 9392 | StackHandleScope<4> hs(self); |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 9393 | ObjPtr<mirror::Class> array_of_class = GetClassRoot<mirror::ObjectArray<mirror::Class>>(this); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9394 | Handle<mirror::ObjectArray<mirror::Class>> method_params(hs.NewHandle( |
| 9395 | mirror::ObjectArray<mirror::Class>::Alloc(self, array_of_class, num_params))); |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 9396 | if (UNLIKELY(method_params == nullptr)) { |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9397 | DCHECK(self->IsExceptionPending()); |
| 9398 | return nullptr; |
| 9399 | } |
| 9400 | |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9401 | Handle<mirror::Class> constructor_class; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9402 | Handle<mirror::Class> return_type; |
| 9403 | switch (handle_type) { |
| 9404 | case DexFile::MethodHandleType::kStaticPut: { |
Vladimir Marko | 4098a7a | 2017-11-06 16:00:51 +0000 | [diff] [blame] | 9405 | method_params->Set(0, target_field->ResolveType()); |
Vladimir Marko | 9186b18 | 2018-11-06 14:55:54 +0000 | [diff] [blame] | 9406 | return_type = hs.NewHandle(GetClassRoot(ClassRoot::kPrimitiveVoid, this)); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9407 | break; |
| 9408 | } |
| 9409 | case DexFile::MethodHandleType::kStaticGet: { |
Vladimir Marko | 4098a7a | 2017-11-06 16:00:51 +0000 | [diff] [blame] | 9410 | return_type = hs.NewHandle(target_field->ResolveType()); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9411 | break; |
| 9412 | } |
| 9413 | case DexFile::MethodHandleType::kInstancePut: { |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 9414 | method_params->Set(0, target_field->GetDeclaringClass()); |
Vladimir Marko | 4098a7a | 2017-11-06 16:00:51 +0000 | [diff] [blame] | 9415 | method_params->Set(1, target_field->ResolveType()); |
Vladimir Marko | 9186b18 | 2018-11-06 14:55:54 +0000 | [diff] [blame] | 9416 | return_type = hs.NewHandle(GetClassRoot(ClassRoot::kPrimitiveVoid, this)); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9417 | break; |
| 9418 | } |
| 9419 | case DexFile::MethodHandleType::kInstanceGet: { |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 9420 | method_params->Set(0, target_field->GetDeclaringClass()); |
Vladimir Marko | 4098a7a | 2017-11-06 16:00:51 +0000 | [diff] [blame] | 9421 | return_type = hs.NewHandle(target_field->ResolveType()); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9422 | break; |
| 9423 | } |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9424 | case DexFile::MethodHandleType::kInvokeStatic: |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 9425 | case DexFile::MethodHandleType::kInvokeInstance: |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9426 | case DexFile::MethodHandleType::kInvokeConstructor: |
| 9427 | case DexFile::MethodHandleType::kInvokeDirect: |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 9428 | case DexFile::MethodHandleType::kInvokeInterface: |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9429 | UNREACHABLE(); |
| 9430 | } |
| 9431 | |
| 9432 | for (int32_t i = 0; i < num_params; ++i) { |
| 9433 | if (UNLIKELY(method_params->Get(i) == nullptr)) { |
| 9434 | DCHECK(self->IsExceptionPending()); |
| 9435 | return nullptr; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9436 | } |
| 9437 | } |
| 9438 | |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9439 | if (UNLIKELY(return_type.IsNull())) { |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9440 | DCHECK(self->IsExceptionPending()); |
| 9441 | return nullptr; |
| 9442 | } |
| 9443 | |
| 9444 | Handle<mirror::MethodType> |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9445 | method_type(hs.NewHandle(mirror::MethodType::Create(self, return_type, method_params))); |
| 9446 | if (UNLIKELY(method_type.IsNull())) { |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9447 | DCHECK(self->IsExceptionPending()); |
| 9448 | return nullptr; |
| 9449 | } |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 9450 | |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9451 | uintptr_t target = reinterpret_cast<uintptr_t>(target_field); |
| 9452 | return mirror::MethodHandleImpl::Create(self, target, kind, method_type); |
| 9453 | } |
| 9454 | |
Vladimir Marko | 5aead70 | 2019-03-27 11:00:36 +0000 | [diff] [blame] | 9455 | ObjPtr<mirror::MethodHandle> ClassLinker::ResolveMethodHandleForMethod( |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9456 | Thread* self, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9457 | const dex::MethodHandleItem& method_handle, |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9458 | ArtMethod* referrer) { |
| 9459 | DexFile::MethodHandleType handle_type = |
| 9460 | static_cast<DexFile::MethodHandleType>(method_handle.method_handle_type_); |
| 9461 | mirror::MethodHandle::Kind kind; |
| 9462 | uint32_t receiver_count = 0; |
| 9463 | ArtMethod* target_method = nullptr; |
| 9464 | switch (handle_type) { |
| 9465 | case DexFile::MethodHandleType::kStaticPut: |
| 9466 | case DexFile::MethodHandleType::kStaticGet: |
| 9467 | case DexFile::MethodHandleType::kInstancePut: |
| 9468 | case DexFile::MethodHandleType::kInstanceGet: |
| 9469 | UNREACHABLE(); |
| 9470 | case DexFile::MethodHandleType::kInvokeStatic: { |
| 9471 | kind = mirror::MethodHandle::Kind::kInvokeStatic; |
| 9472 | receiver_count = 0; |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9473 | target_method = ResolveMethod<ResolveMode::kNoChecks>(self, |
| 9474 | method_handle.field_or_method_idx_, |
| 9475 | referrer, |
| 9476 | InvokeType::kStatic); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9477 | break; |
| 9478 | } |
| 9479 | case DexFile::MethodHandleType::kInvokeInstance: { |
| 9480 | kind = mirror::MethodHandle::Kind::kInvokeVirtual; |
| 9481 | receiver_count = 1; |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9482 | target_method = ResolveMethod<ResolveMode::kNoChecks>(self, |
| 9483 | method_handle.field_or_method_idx_, |
| 9484 | referrer, |
| 9485 | InvokeType::kVirtual); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9486 | break; |
| 9487 | } |
| 9488 | case DexFile::MethodHandleType::kInvokeConstructor: { |
| 9489 | // Constructors are currently implemented as a transform. They |
| 9490 | // are special cased later in this method. |
| 9491 | kind = mirror::MethodHandle::Kind::kInvokeTransform; |
| 9492 | receiver_count = 0; |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9493 | target_method = ResolveMethod<ResolveMode::kNoChecks>(self, |
| 9494 | method_handle.field_or_method_idx_, |
| 9495 | referrer, |
| 9496 | InvokeType::kDirect); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9497 | break; |
| 9498 | } |
| 9499 | case DexFile::MethodHandleType::kInvokeDirect: { |
| 9500 | kind = mirror::MethodHandle::Kind::kInvokeDirect; |
| 9501 | receiver_count = 1; |
| 9502 | StackHandleScope<2> hs(self); |
| 9503 | // A constant method handle with type kInvokeDirect can refer to |
| 9504 | // a method that is private or to a method in a super class. To |
| 9505 | // disambiguate the two options, we resolve the method ignoring |
| 9506 | // the invocation type to determine if the method is private. We |
| 9507 | // then resolve again specifying the intended invocation type to |
| 9508 | // force the appropriate checks. |
Vladimir Marko | 8901119 | 2017-12-11 13:45:05 +0000 | [diff] [blame] | 9509 | target_method = ResolveMethodWithoutInvokeType(method_handle.field_or_method_idx_, |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9510 | hs.NewHandle(referrer->GetDexCache()), |
| 9511 | hs.NewHandle(referrer->GetClassLoader())); |
| 9512 | if (UNLIKELY(target_method == nullptr)) { |
| 9513 | break; |
| 9514 | } |
| 9515 | |
| 9516 | if (target_method->IsPrivate()) { |
| 9517 | kind = mirror::MethodHandle::Kind::kInvokeDirect; |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9518 | target_method = ResolveMethod<ResolveMode::kNoChecks>(self, |
| 9519 | method_handle.field_or_method_idx_, |
| 9520 | referrer, |
| 9521 | InvokeType::kDirect); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9522 | } else { |
| 9523 | kind = mirror::MethodHandle::Kind::kInvokeSuper; |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9524 | target_method = ResolveMethod<ResolveMode::kNoChecks>(self, |
| 9525 | method_handle.field_or_method_idx_, |
| 9526 | referrer, |
| 9527 | InvokeType::kSuper); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9528 | if (UNLIKELY(target_method == nullptr)) { |
| 9529 | break; |
| 9530 | } |
| 9531 | // Find the method specified in the parent in referring class |
| 9532 | // so invoke-super invokes the method in the parent of the |
| 9533 | // referrer. |
| 9534 | target_method = |
| 9535 | referrer->GetDeclaringClass()->FindVirtualMethodForVirtual(target_method, |
| 9536 | kRuntimePointerSize); |
| 9537 | } |
| 9538 | break; |
| 9539 | } |
| 9540 | case DexFile::MethodHandleType::kInvokeInterface: { |
| 9541 | kind = mirror::MethodHandle::Kind::kInvokeInterface; |
| 9542 | receiver_count = 1; |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9543 | target_method = ResolveMethod<ResolveMode::kNoChecks>(self, |
| 9544 | method_handle.field_or_method_idx_, |
| 9545 | referrer, |
| 9546 | InvokeType::kInterface); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9547 | break; |
| 9548 | } |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 9549 | } |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9550 | |
| 9551 | if (UNLIKELY(target_method == nullptr)) { |
| 9552 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 9553 | return nullptr; |
| 9554 | } |
| 9555 | |
| 9556 | ObjPtr<mirror::Class> target_class = target_method->GetDeclaringClass(); |
| 9557 | ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass(); |
| 9558 | uint32_t access_flags = target_method->GetAccessFlags(); |
| 9559 | if (UNLIKELY(!referring_class->CanAccessMember(target_class, access_flags))) { |
| 9560 | ThrowIllegalAccessErrorMethod(referring_class, target_method); |
| 9561 | return nullptr; |
| 9562 | } |
| 9563 | |
| 9564 | // Calculate the number of parameters from the method shorty. We add the |
| 9565 | // receiver count (0 or 1) and deduct one for the return value. |
| 9566 | uint32_t shorty_length; |
| 9567 | target_method->GetShorty(&shorty_length); |
| 9568 | int32_t num_params = static_cast<int32_t>(shorty_length + receiver_count - 1); |
| 9569 | |
Orion Hodson | ecd5856 | 2018-09-24 11:27:33 +0100 | [diff] [blame] | 9570 | StackHandleScope<5> hs(self); |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 9571 | ObjPtr<mirror::Class> array_of_class = GetClassRoot<mirror::ObjectArray<mirror::Class>>(this); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9572 | Handle<mirror::ObjectArray<mirror::Class>> method_params(hs.NewHandle( |
| 9573 | mirror::ObjectArray<mirror::Class>::Alloc(self, array_of_class, num_params))); |
| 9574 | if (method_params.Get() == nullptr) { |
| 9575 | DCHECK(self->IsExceptionPending()); |
| 9576 | return nullptr; |
| 9577 | } |
| 9578 | |
Orion Hodson | ecd5856 | 2018-09-24 11:27:33 +0100 | [diff] [blame] | 9579 | const DexFile* dex_file = referrer->GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9580 | 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] | 9581 | int32_t index = 0; |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9582 | if (receiver_count != 0) { |
Orion Hodson | ecd5856 | 2018-09-24 11:27:33 +0100 | [diff] [blame] | 9583 | // Insert receiver. Use the class identified in the method handle rather than the declaring |
| 9584 | // class of the resolved method which may be super class or default interface method |
| 9585 | // (b/115964401). |
| 9586 | ObjPtr<mirror::Class> receiver_class = LookupResolvedType(method_id.class_idx_, referrer); |
| 9587 | // receiver_class should have been resolved when resolving the target method. |
| 9588 | DCHECK(receiver_class != nullptr); |
| 9589 | method_params->Set(index++, receiver_class); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9590 | } |
Orion Hodson | ecd5856 | 2018-09-24 11:27:33 +0100 | [diff] [blame] | 9591 | |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9592 | const dex::ProtoId& proto_id = dex_file->GetProtoId(method_id.proto_idx_); |
Orion Hodson | ecd5856 | 2018-09-24 11:27:33 +0100 | [diff] [blame] | 9593 | DexFileParameterIterator it(*dex_file, proto_id); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9594 | while (it.HasNext()) { |
Orion Hodson | da1cdd0 | 2018-01-31 18:08:28 +0000 | [diff] [blame] | 9595 | DCHECK_LT(index, num_params); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9596 | const dex::TypeIndex type_idx = it.GetTypeIdx(); |
Orion Hodson | ecd5856 | 2018-09-24 11:27:33 +0100 | [diff] [blame] | 9597 | ObjPtr<mirror::Class> klass = ResolveType(type_idx, referrer); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9598 | if (nullptr == klass) { |
| 9599 | DCHECK(self->IsExceptionPending()); |
| 9600 | return nullptr; |
| 9601 | } |
| 9602 | method_params->Set(index++, klass); |
| 9603 | it.Next(); |
| 9604 | } |
| 9605 | |
Orion Hodson | ecd5856 | 2018-09-24 11:27:33 +0100 | [diff] [blame] | 9606 | Handle<mirror::Class> return_type = |
| 9607 | hs.NewHandle(ResolveType(proto_id.return_type_idx_, referrer)); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9608 | if (UNLIKELY(return_type.IsNull())) { |
| 9609 | DCHECK(self->IsExceptionPending()); |
| 9610 | return nullptr; |
| 9611 | } |
| 9612 | |
| 9613 | Handle<mirror::MethodType> |
| 9614 | method_type(hs.NewHandle(mirror::MethodType::Create(self, return_type, method_params))); |
| 9615 | if (UNLIKELY(method_type.IsNull())) { |
| 9616 | DCHECK(self->IsExceptionPending()); |
| 9617 | return nullptr; |
| 9618 | } |
| 9619 | |
| 9620 | if (UNLIKELY(handle_type == DexFile::MethodHandleType::kInvokeConstructor)) { |
| 9621 | Handle<mirror::Class> constructor_class = hs.NewHandle(target_method->GetDeclaringClass()); |
| 9622 | Handle<mirror::MethodHandlesLookup> lookup = |
| 9623 | hs.NewHandle(mirror::MethodHandlesLookup::GetDefault(self)); |
| 9624 | return lookup->FindConstructor(self, constructor_class, method_type); |
| 9625 | } |
| 9626 | |
| 9627 | uintptr_t target = reinterpret_cast<uintptr_t>(target_method); |
| 9628 | return mirror::MethodHandleImpl::Create(self, target, kind, method_type); |
| 9629 | } |
| 9630 | |
Vladimir Marko | af94020 | 2017-12-08 15:01:18 +0000 | [diff] [blame] | 9631 | ObjPtr<mirror::MethodHandle> ClassLinker::ResolveMethodHandle(Thread* self, |
| 9632 | uint32_t method_handle_idx, |
| 9633 | ArtMethod* referrer) |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9634 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9635 | const DexFile* const dex_file = referrer->GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9636 | const dex::MethodHandleItem& method_handle = dex_file->GetMethodHandle(method_handle_idx); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9637 | switch (static_cast<DexFile::MethodHandleType>(method_handle.method_handle_type_)) { |
| 9638 | case DexFile::MethodHandleType::kStaticPut: |
| 9639 | case DexFile::MethodHandleType::kStaticGet: |
| 9640 | case DexFile::MethodHandleType::kInstancePut: |
| 9641 | case DexFile::MethodHandleType::kInstanceGet: |
| 9642 | return ResolveMethodHandleForField(self, method_handle, referrer); |
| 9643 | case DexFile::MethodHandleType::kInvokeStatic: |
| 9644 | case DexFile::MethodHandleType::kInvokeInstance: |
| 9645 | case DexFile::MethodHandleType::kInvokeConstructor: |
| 9646 | case DexFile::MethodHandleType::kInvokeDirect: |
| 9647 | case DexFile::MethodHandleType::kInvokeInterface: |
Orion Hodson | da1cdd0 | 2018-01-31 18:08:28 +0000 | [diff] [blame] | 9648 | return ResolveMethodHandleForMethod(self, method_handle, referrer); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9649 | } |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9650 | } |
| 9651 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 9652 | bool ClassLinker::IsQuickResolutionStub(const void* entry_point) const { |
| 9653 | return (entry_point == GetQuickResolutionStub()) || |
| 9654 | (quick_resolution_trampoline_ == entry_point); |
| 9655 | } |
| 9656 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 9657 | bool ClassLinker::IsQuickToInterpreterBridge(const void* entry_point) const { |
| 9658 | return (entry_point == GetQuickToInterpreterBridge()) || |
| 9659 | (quick_to_interpreter_bridge_trampoline_ == entry_point); |
| 9660 | } |
| 9661 | |
| 9662 | bool ClassLinker::IsQuickGenericJniStub(const void* entry_point) const { |
| 9663 | return (entry_point == GetQuickGenericJniStub()) || |
| 9664 | (quick_generic_jni_trampoline_ == entry_point); |
| 9665 | } |
| 9666 | |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 9667 | bool ClassLinker::IsJniDlsymLookupStub(const void* entry_point) const { |
Vladimir Marko | 7dac864 | 2019-11-06 17:09:30 +0000 | [diff] [blame] | 9668 | return entry_point == GetJniDlsymLookupStub() || |
| 9669 | (jni_dlsym_lookup_trampoline_ == entry_point); |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 9670 | } |
| 9671 | |
Vladimir Marko | fa458ac | 2020-02-12 14:08:07 +0000 | [diff] [blame] | 9672 | bool ClassLinker::IsJniDlsymLookupCriticalStub(const void* entry_point) const { |
| 9673 | return entry_point == GetJniDlsymLookupCriticalStub() || |
| 9674 | (jni_dlsym_lookup_critical_trampoline_ == entry_point); |
| 9675 | } |
| 9676 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 9677 | const void* ClassLinker::GetRuntimeQuickGenericJniStub() const { |
| 9678 | return GetQuickGenericJniStub(); |
| 9679 | } |
| 9680 | |
Alex Light | db01a09 | 2017-04-03 15:39:55 -0700 | [diff] [blame] | 9681 | void ClassLinker::SetEntryPointsForObsoleteMethod(ArtMethod* method) const { |
| 9682 | DCHECK(method->IsObsolete()); |
| 9683 | // We cannot mess with the entrypoints of native methods because they are used to determine how |
| 9684 | // large the method's quick stack frame is. Without this information we cannot walk the stacks. |
| 9685 | if (!method->IsNative()) { |
| 9686 | method->SetEntryPointFromQuickCompiledCode(GetInvokeObsoleteMethodStub()); |
| 9687 | } |
| 9688 | } |
| 9689 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 9690 | void ClassLinker::DumpForSigQuit(std::ostream& os) { |
Hans Boehm | fb3ad72 | 2021-08-16 16:53:17 +0000 | [diff] [blame] | 9691 | ScopedObjectAccess soa(Thread::Current()); |
| 9692 | ReaderMutexLock mu(soa.Self(), *Locks::classlinker_classes_lock_); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 9693 | os << "Zygote loaded classes=" << NumZygoteClasses() << " post zygote classes=" |
| 9694 | << NumNonZygoteClasses() << "\n"; |
Hans Boehm | fb3ad72 | 2021-08-16 16:53:17 +0000 | [diff] [blame] | 9695 | ReaderMutexLock mu2(soa.Self(), *Locks::dex_lock_); |
Nicolas Geoffray | a90c922 | 2018-09-07 13:19:19 +0100 | [diff] [blame] | 9696 | os << "Dumping registered class loaders\n"; |
| 9697 | size_t class_loader_index = 0; |
| 9698 | for (const ClassLoaderData& class_loader : class_loaders_) { |
| 9699 | ObjPtr<mirror::ClassLoader> loader = |
Hans Boehm | fb3ad72 | 2021-08-16 16:53:17 +0000 | [diff] [blame] | 9700 | ObjPtr<mirror::ClassLoader>::DownCast(soa.Self()->DecodeJObject(class_loader.weak_root)); |
Nicolas Geoffray | a90c922 | 2018-09-07 13:19:19 +0100 | [diff] [blame] | 9701 | if (loader != nullptr) { |
| 9702 | os << "#" << class_loader_index++ << " " << loader->GetClass()->PrettyDescriptor() << ": ["; |
| 9703 | bool saw_one_dex_file = false; |
David Srbecky | 6fbcc29 | 2021-02-23 01:05:32 +0000 | [diff] [blame] | 9704 | for (const auto& entry : dex_caches_) { |
| 9705 | const DexCacheData& dex_cache = entry.second; |
| 9706 | if (dex_cache.class_table == class_loader.class_table) { |
Nicolas Geoffray | a90c922 | 2018-09-07 13:19:19 +0100 | [diff] [blame] | 9707 | if (saw_one_dex_file) { |
| 9708 | os << ":"; |
| 9709 | } |
| 9710 | saw_one_dex_file = true; |
David Srbecky | 6fbcc29 | 2021-02-23 01:05:32 +0000 | [diff] [blame] | 9711 | os << entry.first->GetLocation(); |
Nicolas Geoffray | a90c922 | 2018-09-07 13:19:19 +0100 | [diff] [blame] | 9712 | } |
| 9713 | } |
| 9714 | os << "]"; |
| 9715 | bool found_parent = false; |
| 9716 | if (loader->GetParent() != nullptr) { |
| 9717 | size_t parent_index = 0; |
| 9718 | for (const ClassLoaderData& class_loader2 : class_loaders_) { |
| 9719 | ObjPtr<mirror::ClassLoader> loader2 = ObjPtr<mirror::ClassLoader>::DownCast( |
Hans Boehm | fb3ad72 | 2021-08-16 16:53:17 +0000 | [diff] [blame] | 9720 | soa.Self()->DecodeJObject(class_loader2.weak_root)); |
Nicolas Geoffray | a90c922 | 2018-09-07 13:19:19 +0100 | [diff] [blame] | 9721 | if (loader2 == loader->GetParent()) { |
| 9722 | os << ", parent #" << parent_index; |
| 9723 | found_parent = true; |
| 9724 | break; |
| 9725 | } |
| 9726 | parent_index++; |
| 9727 | } |
| 9728 | if (!found_parent) { |
| 9729 | os << ", unregistered parent of type " |
| 9730 | << loader->GetParent()->GetClass()->PrettyDescriptor(); |
| 9731 | } |
| 9732 | } else { |
| 9733 | os << ", no parent"; |
| 9734 | } |
| 9735 | os << "\n"; |
| 9736 | } |
| 9737 | } |
| 9738 | os << "Done dumping class loaders\n"; |
Andreas Gampe | 9b7f8b5 | 2019-06-07 08:59:29 -0700 | [diff] [blame] | 9739 | Runtime* runtime = Runtime::Current(); |
| 9740 | os << "Classes initialized: " << runtime->GetStat(KIND_GLOBAL_CLASS_INIT_COUNT) << " in " |
| 9741 | << PrettyDuration(runtime->GetStat(KIND_GLOBAL_CLASS_INIT_TIME)) << "\n"; |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 9742 | } |
| 9743 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 9744 | class CountClassesVisitor : public ClassLoaderVisitor { |
| 9745 | public: |
| 9746 | CountClassesVisitor() : num_zygote_classes(0), num_non_zygote_classes(0) {} |
| 9747 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 9748 | void Visit(ObjPtr<mirror::ClassLoader> class_loader) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 9749 | REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_) override { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 9750 | ClassTable* const class_table = class_loader->GetClassTable(); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 9751 | if (class_table != nullptr) { |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 9752 | num_zygote_classes += class_table->NumZygoteClasses(class_loader); |
| 9753 | num_non_zygote_classes += class_table->NumNonZygoteClasses(class_loader); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 9754 | } |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 9755 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 9756 | |
| 9757 | size_t num_zygote_classes; |
| 9758 | size_t num_non_zygote_classes; |
| 9759 | }; |
| 9760 | |
| 9761 | size_t ClassLinker::NumZygoteClasses() const { |
| 9762 | CountClassesVisitor visitor; |
| 9763 | VisitClassLoaders(&visitor); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 9764 | return visitor.num_zygote_classes + boot_class_table_->NumZygoteClasses(nullptr); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 9765 | } |
| 9766 | |
| 9767 | size_t ClassLinker::NumNonZygoteClasses() const { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 9768 | CountClassesVisitor visitor; |
| 9769 | VisitClassLoaders(&visitor); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 9770 | return visitor.num_non_zygote_classes + boot_class_table_->NumNonZygoteClasses(nullptr); |
Elliott Hughes | cac6cc7 | 2011-11-03 20:31:21 -0700 | [diff] [blame] | 9771 | } |
| 9772 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 9773 | size_t ClassLinker::NumLoadedClasses() { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 9774 | ReaderMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 9775 | // 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] | 9776 | return NumNonZygoteClasses(); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 9777 | } |
| 9778 | |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 9779 | pid_t ClassLinker::GetClassesLockOwner() { |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 9780 | return Locks::classlinker_classes_lock_->GetExclusiveOwnerTid(); |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 9781 | } |
| 9782 | |
| 9783 | pid_t ClassLinker::GetDexLockOwner() { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 9784 | return Locks::dex_lock_->GetExclusiveOwnerTid(); |
Brian Carlstrom | 24a3c2e | 2011-10-17 18:07:52 -0700 | [diff] [blame] | 9785 | } |
| 9786 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 9787 | void ClassLinker::SetClassRoot(ClassRoot class_root, ObjPtr<mirror::Class> klass) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 9788 | DCHECK(!init_done_); |
| 9789 | |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 9790 | DCHECK(klass != nullptr); |
| 9791 | DCHECK(klass->GetClassLoader() == nullptr); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 9792 | |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 9793 | mirror::ObjectArray<mirror::Class>* class_roots = class_roots_.Read(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 9794 | DCHECK(class_roots != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 9795 | DCHECK_LT(static_cast<uint32_t>(class_root), static_cast<uint32_t>(ClassRoot::kMax)); |
| 9796 | int32_t index = static_cast<int32_t>(class_root); |
| 9797 | DCHECK(class_roots->Get(index) == nullptr); |
| 9798 | class_roots->Set<false>(index, klass); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 9799 | } |
| 9800 | |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 9801 | ObjPtr<mirror::ClassLoader> ClassLinker::CreateWellKnownClassLoader( |
| 9802 | Thread* self, |
| 9803 | const std::vector<const DexFile*>& dex_files, |
| 9804 | Handle<mirror::Class> loader_class, |
Nicolas Geoffray | e167273 | 2018-11-30 01:09:49 +0000 | [diff] [blame] | 9805 | Handle<mirror::ClassLoader> parent_loader, |
Brad Stenning | 9c924e8 | 2021-10-11 19:09:00 -0700 | [diff] [blame] | 9806 | Handle<mirror::ObjectArray<mirror::ClassLoader>> shared_libraries, |
| 9807 | Handle<mirror::ObjectArray<mirror::ClassLoader>> shared_libraries_after) { |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 9808 | |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 9809 | StackHandleScope<5> hs(self); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9810 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 9811 | ArtField* dex_elements_field = |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 9812 | jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList_dexElements); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9813 | |
Vladimir Marko | 4098a7a | 2017-11-06 16:00:51 +0000 | [diff] [blame] | 9814 | Handle<mirror::Class> dex_elements_class(hs.NewHandle(dex_elements_field->ResolveType())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 9815 | DCHECK(dex_elements_class != nullptr); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9816 | DCHECK(dex_elements_class->IsArrayClass()); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 9817 | Handle<mirror::ObjectArray<mirror::Object>> h_dex_elements(hs.NewHandle( |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 9818 | mirror::ObjectArray<mirror::Object>::Alloc(self, |
| 9819 | dex_elements_class.Get(), |
| 9820 | dex_files.size()))); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9821 | Handle<mirror::Class> h_dex_element_class = |
| 9822 | hs.NewHandle(dex_elements_class->GetComponentType()); |
| 9823 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 9824 | ArtField* element_file_field = |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 9825 | jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 9826 | DCHECK_EQ(h_dex_element_class.Get(), element_file_field->GetDeclaringClass()); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9827 | |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 9828 | ArtField* cookie_field = jni::DecodeArtField(WellKnownClasses::dalvik_system_DexFile_cookie); |
Vladimir Marko | 208f670 | 2017-12-08 12:00:50 +0000 | [diff] [blame] | 9829 | DCHECK_EQ(cookie_field->GetDeclaringClass(), element_file_field->LookupResolvedType()); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9830 | |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 9831 | ArtField* file_name_field = jni::DecodeArtField(WellKnownClasses::dalvik_system_DexFile_fileName); |
Vladimir Marko | 208f670 | 2017-12-08 12:00:50 +0000 | [diff] [blame] | 9832 | DCHECK_EQ(file_name_field->GetDeclaringClass(), element_file_field->LookupResolvedType()); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 9833 | |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9834 | // Fill the elements array. |
| 9835 | int32_t index = 0; |
| 9836 | for (const DexFile* dex_file : dex_files) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 9837 | StackHandleScope<4> hs2(self); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9838 | |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 9839 | // CreateWellKnownClassLoader is only used by gtests and compiler. |
| 9840 | // 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] | 9841 | Handle<mirror::LongArray> h_long_array = hs2.NewHandle(mirror::LongArray::Alloc( |
| 9842 | self, |
| 9843 | kDexFileIndexStart + 1)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 9844 | DCHECK(h_long_array != nullptr); |
Vladimir Marko | 78baed5 | 2018-10-11 10:44:58 +0100 | [diff] [blame] | 9845 | h_long_array->Set(kDexFileIndexStart, reinterpret_cast64<int64_t>(dex_file)); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9846 | |
Mathieu Chartier | 3738e98 | 2017-05-12 16:07:28 -0700 | [diff] [blame] | 9847 | // Note that this creates a finalizable dalvik.system.DexFile object and a corresponding |
| 9848 | // FinalizerReference which will never get cleaned up without a started runtime. |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9849 | Handle<mirror::Object> h_dex_file = hs2.NewHandle( |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 9850 | cookie_field->GetDeclaringClass()->AllocObject(self)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 9851 | DCHECK(h_dex_file != nullptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 9852 | cookie_field->SetObject<false>(h_dex_file.Get(), h_long_array.Get()); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9853 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 9854 | Handle<mirror::String> h_file_name = hs2.NewHandle( |
| 9855 | mirror::String::AllocFromModifiedUtf8(self, dex_file->GetLocation().c_str())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 9856 | DCHECK(h_file_name != nullptr); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 9857 | file_name_field->SetObject<false>(h_dex_file.Get(), h_file_name.Get()); |
| 9858 | |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9859 | 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] | 9860 | DCHECK(h_element != nullptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 9861 | element_file_field->SetObject<false>(h_element.Get(), h_dex_file.Get()); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9862 | |
| 9863 | h_dex_elements->Set(index, h_element.Get()); |
| 9864 | index++; |
| 9865 | } |
| 9866 | DCHECK_EQ(index, h_dex_elements->GetLength()); |
| 9867 | |
| 9868 | // Create DexPathList. |
| 9869 | Handle<mirror::Object> h_dex_path_list = hs.NewHandle( |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 9870 | dex_elements_field->GetDeclaringClass()->AllocObject(self)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 9871 | DCHECK(h_dex_path_list != nullptr); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9872 | // Set elements. |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 9873 | 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] | 9874 | // Create an empty List for the "nativeLibraryDirectories," required for native tests. |
| 9875 | // Note: this code is uncommon(oatdump)/testing-only, so don't add further WellKnownClasses |
| 9876 | // elements. |
| 9877 | { |
| 9878 | ArtField* native_lib_dirs = dex_elements_field->GetDeclaringClass()-> |
| 9879 | FindDeclaredInstanceField("nativeLibraryDirectories", "Ljava/util/List;"); |
| 9880 | DCHECK(native_lib_dirs != nullptr); |
| 9881 | ObjPtr<mirror::Class> list_class = FindSystemClass(self, "Ljava/util/ArrayList;"); |
| 9882 | DCHECK(list_class != nullptr); |
| 9883 | { |
| 9884 | StackHandleScope<1> h_list_scope(self); |
| 9885 | Handle<mirror::Class> h_list_class(h_list_scope.NewHandle<mirror::Class>(list_class)); |
| 9886 | bool list_init = EnsureInitialized(self, h_list_class, true, true); |
| 9887 | DCHECK(list_init); |
| 9888 | list_class = h_list_class.Get(); |
| 9889 | } |
| 9890 | ObjPtr<mirror::Object> list_object = list_class->AllocObject(self); |
| 9891 | // Note: we leave the object uninitialized. This must never leak into any non-testing code, but |
| 9892 | // is fine for testing. While it violates a Java-code invariant (the elementData field is |
| 9893 | // normally never null), as long as one does not try to add elements, this will still |
| 9894 | // work. |
| 9895 | native_lib_dirs->SetObject<false>(h_dex_path_list.Get(), list_object); |
| 9896 | } |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9897 | |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 9898 | // Create the class loader.. |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 9899 | Handle<mirror::ClassLoader> h_class_loader = hs.NewHandle<mirror::ClassLoader>( |
| 9900 | ObjPtr<mirror::ClassLoader>::DownCast(loader_class->AllocObject(self))); |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 9901 | DCHECK(h_class_loader != nullptr); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9902 | // Set DexPathList. |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 9903 | ArtField* path_list_field = |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 9904 | jni::DecodeArtField(WellKnownClasses::dalvik_system_BaseDexClassLoader_pathList); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 9905 | DCHECK(path_list_field != nullptr); |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 9906 | 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] | 9907 | |
| 9908 | // Make a pretend boot-classpath. |
| 9909 | // TODO: Should we scan the image? |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 9910 | ArtField* const parent_field = |
Vladimir Marko | e300c4e | 2021-06-08 16:00:05 +0100 | [diff] [blame] | 9911 | jni::DecodeArtField(WellKnownClasses::java_lang_ClassLoader_parent); |
Roland Levillain | f39c9eb | 2015-05-26 15:02:07 +0100 | [diff] [blame] | 9912 | DCHECK(parent_field != nullptr); |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 9913 | if (parent_loader.Get() == nullptr) { |
| 9914 | ScopedObjectAccessUnchecked soa(self); |
| 9915 | ObjPtr<mirror::Object> boot_loader(soa.Decode<mirror::Class>( |
| 9916 | WellKnownClasses::java_lang_BootClassLoader)->AllocObject(self)); |
| 9917 | parent_field->SetObject<false>(h_class_loader.Get(), boot_loader); |
| 9918 | } else { |
| 9919 | parent_field->SetObject<false>(h_class_loader.Get(), parent_loader.Get()); |
| 9920 | } |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 9921 | |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 9922 | ArtField* shared_libraries_field = |
| 9923 | jni::DecodeArtField(WellKnownClasses::dalvik_system_BaseDexClassLoader_sharedLibraryLoaders); |
| 9924 | DCHECK(shared_libraries_field != nullptr); |
| 9925 | shared_libraries_field->SetObject<false>(h_class_loader.Get(), shared_libraries.Get()); |
| 9926 | |
Brad Stenning | 9c924e8 | 2021-10-11 19:09:00 -0700 | [diff] [blame] | 9927 | ArtField* shared_libraries_after_field = |
| 9928 | jni::DecodeArtField( |
| 9929 | WellKnownClasses::dalvik_system_BaseDexClassLoader_sharedLibraryLoadersAfter); |
| 9930 | DCHECK(shared_libraries_after_field != nullptr); |
| 9931 | shared_libraries_after_field->SetObject<false>(h_class_loader.Get(), |
| 9932 | shared_libraries_after.Get()); |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 9933 | return h_class_loader.Get(); |
| 9934 | } |
| 9935 | |
| 9936 | jobject ClassLinker::CreateWellKnownClassLoader(Thread* self, |
| 9937 | const std::vector<const DexFile*>& dex_files, |
| 9938 | jclass loader_class, |
Nicolas Geoffray | e167273 | 2018-11-30 01:09:49 +0000 | [diff] [blame] | 9939 | jobject parent_loader, |
Brad Stenning | 9c924e8 | 2021-10-11 19:09:00 -0700 | [diff] [blame] | 9940 | jobject shared_libraries, |
| 9941 | jobject shared_libraries_after) { |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 9942 | CHECK(self->GetJniEnv()->IsSameObject(loader_class, |
| 9943 | WellKnownClasses::dalvik_system_PathClassLoader) || |
| 9944 | self->GetJniEnv()->IsSameObject(loader_class, |
David Brazdil | 1a9ac53 | 2019-03-05 11:57:13 +0000 | [diff] [blame] | 9945 | WellKnownClasses::dalvik_system_DelegateLastClassLoader) || |
| 9946 | self->GetJniEnv()->IsSameObject(loader_class, |
| 9947 | WellKnownClasses::dalvik_system_InMemoryDexClassLoader)); |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 9948 | |
| 9949 | // SOAAlreadyRunnable is protected, and we need something to add a global reference. |
| 9950 | // We could move the jobject to the callers, but all call-sites do this... |
| 9951 | ScopedObjectAccessUnchecked soa(self); |
| 9952 | |
| 9953 | // 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] | 9954 | StackHandleScope<5> hs(self); |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 9955 | |
| 9956 | Handle<mirror::Class> h_loader_class = |
| 9957 | hs.NewHandle<mirror::Class>(soa.Decode<mirror::Class>(loader_class)); |
Nicolas Geoffray | e167273 | 2018-11-30 01:09:49 +0000 | [diff] [blame] | 9958 | Handle<mirror::ClassLoader> h_parent = |
| 9959 | hs.NewHandle<mirror::ClassLoader>(soa.Decode<mirror::ClassLoader>(parent_loader)); |
| 9960 | Handle<mirror::ObjectArray<mirror::ClassLoader>> h_shared_libraries = |
| 9961 | hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::ClassLoader>>(shared_libraries)); |
Brad Stenning | 9c924e8 | 2021-10-11 19:09:00 -0700 | [diff] [blame] | 9962 | Handle<mirror::ObjectArray<mirror::ClassLoader>> h_shared_libraries_after = |
| 9963 | hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::ClassLoader>>(shared_libraries_after)); |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 9964 | |
| 9965 | ObjPtr<mirror::ClassLoader> loader = CreateWellKnownClassLoader( |
| 9966 | self, |
| 9967 | dex_files, |
| 9968 | h_loader_class, |
Nicolas Geoffray | e167273 | 2018-11-30 01:09:49 +0000 | [diff] [blame] | 9969 | h_parent, |
Brad Stenning | 9c924e8 | 2021-10-11 19:09:00 -0700 | [diff] [blame] | 9970 | h_shared_libraries, |
| 9971 | h_shared_libraries_after); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9972 | |
| 9973 | // Make it a global ref and return. |
| 9974 | ScopedLocalRef<jobject> local_ref( |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 9975 | soa.Env(), soa.Env()->AddLocalReference<jobject>(loader)); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9976 | return soa.Env()->NewGlobalRef(local_ref.get()); |
| 9977 | } |
| 9978 | |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 9979 | jobject ClassLinker::CreatePathClassLoader(Thread* self, |
| 9980 | const std::vector<const DexFile*>& dex_files) { |
| 9981 | return CreateWellKnownClassLoader(self, |
| 9982 | dex_files, |
| 9983 | WellKnownClasses::dalvik_system_PathClassLoader, |
| 9984 | nullptr); |
| 9985 | } |
| 9986 | |
Andreas Gampe | 8ac7595 | 2015-06-02 21:01:45 -0700 | [diff] [blame] | 9987 | void ClassLinker::DropFindArrayClassCache() { |
| 9988 | std::fill_n(find_array_class_cache_, kFindArrayCacheSize, GcRoot<mirror::Class>(nullptr)); |
| 9989 | find_array_class_cache_next_victim_ = 0; |
| 9990 | } |
| 9991 | |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 9992 | void ClassLinker::VisitClassLoaders(ClassLoaderVisitor* visitor) const { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 9993 | Thread* const self = Thread::Current(); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 9994 | for (const ClassLoaderData& data : class_loaders_) { |
Mathieu Chartier | 4843bd5 | 2015-10-01 17:08:44 -0700 | [diff] [blame] | 9995 | // 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] | 9996 | ObjPtr<mirror::ClassLoader> class_loader = ObjPtr<mirror::ClassLoader>::DownCast( |
| 9997 | self->DecodeJObject(data.weak_root)); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 9998 | if (class_loader != nullptr) { |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 9999 | visitor->Visit(class_loader); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 10000 | } |
| 10001 | } |
| 10002 | } |
| 10003 | |
Alexey Grebenkin | 252a4e4 | 2018-04-02 18:18:01 +0300 | [diff] [blame] | 10004 | void ClassLinker::VisitAllocators(AllocatorVisitor* visitor) const { |
| 10005 | for (const ClassLoaderData& data : class_loaders_) { |
| 10006 | LinearAlloc* alloc = data.allocator; |
| 10007 | if (alloc != nullptr && !visitor->Visit(alloc)) { |
| 10008 | break; |
| 10009 | } |
| 10010 | } |
| 10011 | } |
| 10012 | |
Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 10013 | void ClassLinker::InsertDexFileInToClassLoader(ObjPtr<mirror::Object> dex_file, |
| 10014 | ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 10015 | DCHECK(dex_file != nullptr); |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 10016 | Thread* const self = Thread::Current(); |
| 10017 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 10018 | ClassTable* const table = ClassTableForClassLoader(class_loader); |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 10019 | DCHECK(table != nullptr); |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 10020 | if (table->InsertStrongRoot(dex_file) && class_loader != nullptr) { |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 10021 | // It was not already inserted, perform the write barrier to let the GC know the class loader's |
| 10022 | // class table was modified. |
Mathieu Chartier | 88ea61e | 2018-06-20 17:45:41 -0700 | [diff] [blame] | 10023 | WriteBarrier::ForEveryFieldWrite(class_loader); |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 10024 | } |
| 10025 | } |
| 10026 | |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 10027 | void ClassLinker::CleanupClassLoaders() { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 10028 | Thread* const self = Thread::Current(); |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 10029 | std::vector<ClassLoaderData> to_delete; |
| 10030 | // Do the delete outside the lock to avoid lock violation in jit code cache. |
| 10031 | { |
| 10032 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 10033 | for (auto it = class_loaders_.begin(); it != class_loaders_.end(); ) { |
| 10034 | const ClassLoaderData& data = *it; |
| 10035 | // 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] | 10036 | ObjPtr<mirror::ClassLoader> class_loader = |
| 10037 | ObjPtr<mirror::ClassLoader>::DownCast(self->DecodeJObject(data.weak_root)); |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 10038 | if (class_loader != nullptr) { |
| 10039 | ++it; |
| 10040 | } else { |
| 10041 | VLOG(class_linker) << "Freeing class loader"; |
| 10042 | to_delete.push_back(data); |
| 10043 | it = class_loaders_.erase(it); |
| 10044 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 10045 | } |
| 10046 | } |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 10047 | for (ClassLoaderData& data : to_delete) { |
Alexey Grebenkin | be4c2bd | 2018-02-01 19:09:59 +0300 | [diff] [blame] | 10048 | // CHA unloading analysis and SingleImplementaion cleanups are required. |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 10049 | DeleteClassLoader(self, data, /*cleanup_cha=*/ true); |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 10050 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 10051 | } |
| 10052 | |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 10053 | class ClassLinker::FindVirtualMethodHolderVisitor : public ClassVisitor { |
| 10054 | public: |
| 10055 | FindVirtualMethodHolderVisitor(const ArtMethod* method, PointerSize pointer_size) |
| 10056 | : method_(method), |
| 10057 | pointer_size_(pointer_size) {} |
| 10058 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 10059 | bool operator()(ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_) override { |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 10060 | if (klass->GetVirtualMethodsSliceUnchecked(pointer_size_).Contains(method_)) { |
| 10061 | holder_ = klass; |
| 10062 | } |
| 10063 | // Return false to stop searching if holder_ is not null. |
| 10064 | return holder_ == nullptr; |
| 10065 | } |
| 10066 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 10067 | ObjPtr<mirror::Class> holder_ = nullptr; |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 10068 | const ArtMethod* const method_; |
| 10069 | const PointerSize pointer_size_; |
| 10070 | }; |
| 10071 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 10072 | ObjPtr<mirror::Class> ClassLinker::GetHoldingClassOfCopiedMethod(ArtMethod* method) { |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 10073 | ScopedTrace trace(__FUNCTION__); // Since this function is slow, have a trace to notify people. |
| 10074 | CHECK(method->IsCopied()); |
| 10075 | FindVirtualMethodHolderVisitor visitor(method, image_pointer_size_); |
| 10076 | VisitClasses(&visitor); |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 10077 | return visitor.holder_; |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 10078 | } |
| 10079 | |
Calin Juravle | 3378768 | 2019-07-26 14:27:18 -0700 | [diff] [blame] | 10080 | bool ClassLinker::DenyAccessBasedOnPublicSdk(ArtMethod* art_method ATTRIBUTE_UNUSED) const |
| 10081 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 10082 | // Should not be called on ClassLinker, only on AotClassLinker that overrides this. |
| 10083 | LOG(FATAL) << "UNREACHABLE"; |
| 10084 | UNREACHABLE(); |
| 10085 | } |
| 10086 | |
| 10087 | bool ClassLinker::DenyAccessBasedOnPublicSdk(ArtField* art_field ATTRIBUTE_UNUSED) const |
| 10088 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 10089 | // Should not be called on ClassLinker, only on AotClassLinker that overrides this. |
| 10090 | LOG(FATAL) << "UNREACHABLE"; |
| 10091 | UNREACHABLE(); |
| 10092 | } |
| 10093 | |
| 10094 | bool ClassLinker::DenyAccessBasedOnPublicSdk(const char* type_descriptor ATTRIBUTE_UNUSED) const { |
| 10095 | // Should not be called on ClassLinker, only on AotClassLinker that overrides this. |
| 10096 | LOG(FATAL) << "UNREACHABLE"; |
| 10097 | UNREACHABLE(); |
| 10098 | } |
| 10099 | |
Calin Juravle | 2c2724c | 2021-01-14 19:54:23 -0800 | [diff] [blame] | 10100 | void ClassLinker::SetEnablePublicSdkChecks(bool enabled ATTRIBUTE_UNUSED) { |
| 10101 | // Should not be called on ClassLinker, only on AotClassLinker that overrides this. |
| 10102 | LOG(FATAL) << "UNREACHABLE"; |
| 10103 | UNREACHABLE(); |
| 10104 | } |
| 10105 | |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 10106 | // Instantiate ClassLinker::AllocClass. |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 10107 | template ObjPtr<mirror::Class> ClassLinker::AllocClass</* kMovable= */ true>( |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 10108 | Thread* self, |
| 10109 | ObjPtr<mirror::Class> java_lang_Class, |
| 10110 | uint32_t class_size); |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 10111 | template ObjPtr<mirror::Class> ClassLinker::AllocClass</* kMovable= */ false>( |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 10112 | Thread* self, |
| 10113 | ObjPtr<mirror::Class> java_lang_Class, |
| 10114 | uint32_t class_size); |
| 10115 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 10116 | } // namespace art |