Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 16 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 17 | #include "class_linker.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 18 | |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 19 | #include <unistd.h> |
| 20 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 21 | #include <algorithm> |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 22 | #include <deque> |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 23 | #include <forward_list> |
Ian Rogers | cf7f191 | 2014-10-22 22:06:39 -0700 | [diff] [blame] | 24 | #include <iostream> |
Vladimir Marko | 2130053 | 2017-01-24 18:06:55 +0000 | [diff] [blame] | 25 | #include <map> |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 26 | #include <memory> |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 27 | #include <queue> |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 28 | #include <string> |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 29 | #include <string_view> |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 30 | #include <tuple> |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 31 | #include <utility> |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 32 | #include <vector> |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 33 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 34 | #include "android-base/stringprintf.h" |
| 35 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 36 | #include "art_field-inl.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 37 | #include "art_method-inl.h" |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 38 | #include "barrier.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 39 | #include "base/arena_allocator.h" |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 40 | #include "base/casts.h" |
Andreas Gampe | 19f5416 | 2019-05-14 16:16:28 -0700 | [diff] [blame] | 41 | #include "base/file_utils.h" |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 42 | #include "base/hash_map.h" |
| 43 | #include "base/hash_set.h" |
David Sehr | 67bf42e | 2018-02-26 16:43:04 -0800 | [diff] [blame] | 44 | #include "base/leb128.h" |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 45 | #include "base/logging.h" |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 46 | #include "base/mutex-inl.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 47 | #include "base/os.h" |
| 48 | #include "base/quasi_atomic.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 49 | #include "base/scoped_arena_containers.h" |
Narayan Kamath | d1c606f | 2014-06-09 16:50:19 +0100 | [diff] [blame] | 50 | #include "base/scoped_flock.h" |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 51 | #include "base/stl_util.h" |
Vladimir Marko | b9c29f6 | 2019-03-20 14:22:51 +0000 | [diff] [blame] | 52 | #include "base/string_view_cpp20.h" |
Mathieu Chartier | 32ce2ad | 2016-03-04 14:58:03 -0800 | [diff] [blame] | 53 | #include "base/systrace.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 54 | #include "base/time_utils.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 55 | #include "base/unix_file/fd_file.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 56 | #include "base/utils.h" |
Andreas Gampe | b9aec2c | 2015-04-23 22:23:47 -0700 | [diff] [blame] | 57 | #include "base/value_object.h" |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 58 | #include "cha.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 59 | #include "class_linker-inl.h" |
Calin Juravle | 57d0acc | 2017-07-11 17:41:30 -0700 | [diff] [blame] | 60 | #include "class_loader_utils.h" |
Vladimir Marko | 5868ada | 2020-05-12 11:50:34 +0100 | [diff] [blame] | 61 | #include "class_root-inl.h" |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 62 | #include "class_table-inl.h" |
Vladimir Marko | 2b5eaa2 | 2013-12-13 13:59:30 +0000 | [diff] [blame] | 63 | #include "compiler_callbacks.h" |
Vladimir Marko | 606adb3 | 2018-04-05 14:49:24 +0100 | [diff] [blame] | 64 | #include "debug_print.h" |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 65 | #include "debugger.h" |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 66 | #include "dex/class_accessor-inl.h" |
David Sehr | b2ec9f5 | 2018-02-21 13:20:31 -0800 | [diff] [blame] | 67 | #include "dex/descriptors_names.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 68 | #include "dex/dex_file-inl.h" |
| 69 | #include "dex/dex_file_exception_helpers.h" |
| 70 | #include "dex/dex_file_loader.h" |
Andreas Gampe | ad1aa63 | 2019-01-02 10:30:54 -0800 | [diff] [blame] | 71 | #include "dex/signature-inl.h" |
David Sehr | 0225f8e | 2018-01-31 08:52:24 +0000 | [diff] [blame] | 72 | #include "dex/utf.h" |
Vladimir Marko | 5115a4d | 2019-10-17 14:56:47 +0100 | [diff] [blame] | 73 | #include "entrypoints/entrypoint_utils-inl.h" |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 74 | #include "entrypoints/runtime_asm_entrypoints.h" |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 75 | #include "experimental_flags.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 76 | #include "gc/accounting/card_table-inl.h" |
Mathieu Chartier | 03c1dd9 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 77 | #include "gc/accounting/heap_bitmap-inl.h" |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 78 | #include "gc/accounting/space_bitmap-inl.h" |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 79 | #include "gc/heap-visit-objects-inl.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 80 | #include "gc/heap.h" |
Mathieu Chartier | 1b1e31f | 2016-05-19 10:13:04 -0700 | [diff] [blame] | 81 | #include "gc/scoped_gc_critical_section.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 82 | #include "gc/space/image_space.h" |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 83 | #include "gc/space/space-inl.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 84 | #include "gc_root-inl.h" |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 85 | #include "handle_scope-inl.h" |
Andreas Gampe | aa12001 | 2018-03-28 16:23:24 -0700 | [diff] [blame] | 86 | #include "hidden_api.h" |
Mathieu Chartier | 4a26f17 | 2016-01-26 14:26:18 -0800 | [diff] [blame] | 87 | #include "image-inl.h" |
Andreas Gampe | 75a7db6 | 2016-09-26 12:04:26 -0700 | [diff] [blame] | 88 | #include "imt_conflict_table.h" |
| 89 | #include "imtable-inl.h" |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 90 | #include "intern_table-inl.h" |
Ian Rogers | 64b6d14 | 2012-10-29 16:34:15 -0700 | [diff] [blame] | 91 | #include "interpreter/interpreter.h" |
Nicolas Geoffray | 0315efa | 2020-06-26 11:42:39 +0100 | [diff] [blame] | 92 | #include "interpreter/mterp/nterp.h" |
David Srbecky | fb3de3d | 2018-01-29 16:11:49 +0000 | [diff] [blame] | 93 | #include "jit/debugger_interface.h" |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 94 | #include "jit/jit.h" |
| 95 | #include "jit/jit_code_cache.h" |
Vladimir Marko | a3ad0cd | 2018-05-04 10:06:38 +0100 | [diff] [blame] | 96 | #include "jni/java_vm_ext.h" |
| 97 | #include "jni/jni_internal.h" |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 98 | #include "linear_alloc.h" |
Andreas Gampe | 8e0f043 | 2018-10-24 13:38:03 -0700 | [diff] [blame] | 99 | #include "mirror/array-alloc-inl.h" |
| 100 | #include "mirror/array-inl.h" |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 101 | #include "mirror/call_site.h" |
Andreas Gampe | 70f5fd0 | 2018-10-24 19:58:37 -0700 | [diff] [blame] | 102 | #include "mirror/class-alloc-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 103 | #include "mirror/class-inl.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 104 | #include "mirror/class.h" |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 105 | #include "mirror/class_ext.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 106 | #include "mirror/class_loader.h" |
Ian Rogers | 39ebcb8 | 2013-05-30 16:57:23 -0700 | [diff] [blame] | 107 | #include "mirror/dex_cache-inl.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 108 | #include "mirror/dex_cache.h" |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 109 | #include "mirror/emulated_stack_frame.h" |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 110 | #include "mirror/field.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 111 | #include "mirror/iftable-inl.h" |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 112 | #include "mirror/method.h" |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 113 | #include "mirror/method_handle_impl.h" |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 114 | #include "mirror/method_handles_lookup.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 115 | #include "mirror/method_type.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 116 | #include "mirror/object-inl.h" |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 117 | #include "mirror/object-refvisitor-inl.h" |
Alex Light | a9bbc08 | 2019-11-14 14:51:41 -0800 | [diff] [blame] | 118 | #include "mirror/object.h" |
Andreas Gampe | 52ecb65 | 2018-10-24 15:18:21 -0700 | [diff] [blame] | 119 | #include "mirror/object_array-alloc-inl.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 120 | #include "mirror/object_array-inl.h" |
Alex Light | 133987d | 2020-03-26 19:22:12 +0000 | [diff] [blame] | 121 | #include "mirror/object_array.h" |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 122 | #include "mirror/object_reference.h" |
| 123 | #include "mirror/object_reference-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 124 | #include "mirror/proxy.h" |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 125 | #include "mirror/reference-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 126 | #include "mirror/stack_trace_element.h" |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 127 | #include "mirror/string-inl.h" |
Andreas Gampe | 501c3b0 | 2019-04-17 21:54:27 +0000 | [diff] [blame] | 128 | #include "mirror/throwable.h" |
Orion Hodson | 005ac51 | 2017-10-24 15:43:43 +0100 | [diff] [blame] | 129 | #include "mirror/var_handle.h" |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 130 | #include "native/dalvik_system_DexFile.h" |
Andreas Gampe | 373a9b5 | 2017-10-18 09:01:57 -0700 | [diff] [blame] | 131 | #include "nativehelper/scoped_local_ref.h" |
Nicolas Geoffray | c39af94 | 2021-01-25 08:43:57 +0000 | [diff] [blame] | 132 | #include "nterp_helpers.h" |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 133 | #include "oat.h" |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 134 | #include "oat_file-inl.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 135 | #include "oat_file.h" |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 136 | #include "oat_file_assistant.h" |
| 137 | #include "oat_file_manager.h" |
| 138 | #include "object_lock.h" |
David Sehr | 82d046e | 2018-04-23 08:14:19 -0700 | [diff] [blame] | 139 | #include "profile/profile_compilation_info.h" |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 140 | #include "runtime.h" |
Andreas Gampe | ac30fa2 | 2017-01-18 21:02:36 -0800 | [diff] [blame] | 141 | #include "runtime_callbacks.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 142 | #include "scoped_thread_state_change-inl.h" |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 143 | #include "thread-inl.h" |
Alex Light | 133987d | 2020-03-26 19:22:12 +0000 | [diff] [blame] | 144 | #include "thread.h" |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 145 | #include "thread_list.h" |
Mathieu Chartier | 7778b88 | 2015-10-05 16:41:10 -0700 | [diff] [blame] | 146 | #include "trace.h" |
Vladimir Marko | b68bb7a | 2020-03-17 10:55:25 +0000 | [diff] [blame] | 147 | #include "transaction.h" |
Nicolas Geoffray | 6df4511 | 2021-02-07 21:51:58 +0000 | [diff] [blame^] | 148 | #include "vdex_file.h" |
Andreas Gampe | a43ba3d | 2019-03-13 15:49:20 -0700 | [diff] [blame] | 149 | #include "verifier/class_verifier.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 150 | #include "well_known_classes.h" |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 151 | |
Nicolas Geoffray | 0039182 | 2019-12-10 10:17:23 +0000 | [diff] [blame] | 152 | #include "interpreter/interpreter_mterp_impl.h" |
| 153 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 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 | |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 172 | static bool HasInitWithString(Thread* self, ClassLinker* class_linker, const char* descriptor) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 173 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 174 | ArtMethod* method = self->GetCurrentMethod(nullptr); |
Andreas Gampe | bfdcdc1 | 2015-04-22 18:10:36 -0700 | [diff] [blame] | 175 | StackHandleScope<1> hs(self); |
| 176 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(method != nullptr ? |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 177 | method->GetDeclaringClass()->GetClassLoader() : nullptr)); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 178 | ObjPtr<mirror::Class> exception_class = class_linker->FindClass(self, descriptor, class_loader); |
Andreas Gampe | bfdcdc1 | 2015-04-22 18:10:36 -0700 | [diff] [blame] | 179 | |
| 180 | if (exception_class == nullptr) { |
| 181 | // No exc class ~ no <init>-with-string. |
| 182 | CHECK(self->IsExceptionPending()); |
| 183 | self->ClearException(); |
| 184 | return false; |
| 185 | } |
| 186 | |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 187 | ArtMethod* exception_init_method = exception_class->FindConstructor( |
| 188 | "(Ljava/lang/String;)V", class_linker->GetImagePointerSize()); |
Andreas Gampe | bfdcdc1 | 2015-04-22 18:10:36 -0700 | [diff] [blame] | 189 | return exception_init_method != nullptr; |
| 190 | } |
| 191 | |
Vladimir Marko | bb206de | 2019-03-28 10:30:32 +0000 | [diff] [blame] | 192 | static ObjPtr<mirror::Object> GetVerifyError(ObjPtr<mirror::Class> c) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 193 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 194 | ObjPtr<mirror::ClassExt> ext(c->GetExtData()); |
| 195 | if (ext == nullptr) { |
| 196 | return nullptr; |
| 197 | } else { |
| 198 | return ext->GetVerifyError(); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | // Helper for ThrowEarlierClassFailure. Throws the stored error. |
| 203 | static void HandleEarlierVerifyError(Thread* self, |
| 204 | ClassLinker* class_linker, |
| 205 | ObjPtr<mirror::Class> c) |
| 206 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 207 | ObjPtr<mirror::Object> obj = GetVerifyError(c); |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 208 | DCHECK(obj != nullptr); |
| 209 | self->AssertNoPendingException(); |
| 210 | if (obj->IsClass()) { |
| 211 | // Previous error has been stored as class. Create a new exception of that type. |
| 212 | |
| 213 | // It's possible the exception doesn't have a <init>(String). |
| 214 | std::string temp; |
| 215 | const char* descriptor = obj->AsClass()->GetDescriptor(&temp); |
| 216 | |
| 217 | if (HasInitWithString(self, class_linker, descriptor)) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 218 | self->ThrowNewException(descriptor, c->PrettyDescriptor().c_str()); |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 219 | } else { |
| 220 | self->ThrowNewException(descriptor, nullptr); |
| 221 | } |
| 222 | } else { |
| 223 | // Previous error has been stored as an instance. Just rethrow. |
Vladimir Marko | c13fbd8 | 2018-06-04 16:16:28 +0100 | [diff] [blame] | 224 | ObjPtr<mirror::Class> throwable_class = GetClassRoot<mirror::Throwable>(class_linker); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 225 | ObjPtr<mirror::Class> error_class = obj->GetClass(); |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 226 | CHECK(throwable_class->IsAssignableFrom(error_class)); |
| 227 | self->SetException(obj->AsThrowable()); |
| 228 | } |
| 229 | self->AssertPendingException(); |
| 230 | } |
| 231 | |
Nicolas Geoffray | 60d4abc | 2020-07-27 13:58:51 +0000 | [diff] [blame] | 232 | static void ChangeInterpreterBridgeToNterp(ArtMethod* method, ClassLinker* class_linker) |
| 233 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 234 | Runtime* runtime = Runtime::Current(); |
| 235 | if (class_linker->IsQuickToInterpreterBridge(method->GetEntryPointFromQuickCompiledCode()) && |
Nicolas Geoffray | c39af94 | 2021-01-25 08:43:57 +0000 | [diff] [blame] | 236 | CanMethodUseNterp(method)) { |
Nicolas Geoffray | 60d4abc | 2020-07-27 13:58:51 +0000 | [diff] [blame] | 237 | if (method->GetDeclaringClass()->IsVisiblyInitialized() || |
| 238 | !NeedsClinitCheckBeforeCall(method)) { |
| 239 | runtime->GetInstrumentation()->UpdateMethodsCode(method, interpreter::GetNterpEntryPoint()); |
| 240 | } else { |
| 241 | // Put the resolution stub, which will initialize the class and then |
| 242 | // call the method with nterp. |
| 243 | runtime->GetInstrumentation()->UpdateMethodsCode(method, GetQuickResolutionStub()); |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | |
Andreas Gampe | 5b20b35 | 2018-10-11 19:03:20 -0700 | [diff] [blame] | 248 | // Ensures that methods have the kAccSkipAccessChecks bit set. We use the |
| 249 | // kAccVerificationAttempted bit on the class access flags to determine whether this has been done |
| 250 | // before. |
Andreas Gampe | 5b20b35 | 2018-10-11 19:03:20 -0700 | [diff] [blame] | 251 | static void EnsureSkipAccessChecksMethods(Handle<mirror::Class> klass, PointerSize pointer_size) |
| 252 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Nicolas Geoffray | 0039182 | 2019-12-10 10:17:23 +0000 | [diff] [blame] | 253 | Runtime* runtime = Runtime::Current(); |
| 254 | ClassLinker* class_linker = runtime->GetClassLinker(); |
Andreas Gampe | 5b20b35 | 2018-10-11 19:03:20 -0700 | [diff] [blame] | 255 | if (!klass->WasVerificationAttempted()) { |
| 256 | klass->SetSkipAccessChecksFlagOnAllMethods(pointer_size); |
| 257 | klass->SetVerificationAttempted(); |
Nicolas Geoffray | 0039182 | 2019-12-10 10:17:23 +0000 | [diff] [blame] | 258 | // Now that the class has passed verification, try to set nterp entrypoints |
| 259 | // to methods that currently use the switch interpreter. |
| 260 | if (interpreter::CanRuntimeUseNterp()) { |
| 261 | for (ArtMethod& m : klass->GetMethods(pointer_size)) { |
Nicolas Geoffray | 60d4abc | 2020-07-27 13:58:51 +0000 | [diff] [blame] | 262 | ChangeInterpreterBridgeToNterp(&m, class_linker); |
Nicolas Geoffray | 0039182 | 2019-12-10 10:17:23 +0000 | [diff] [blame] | 263 | } |
| 264 | } |
Andreas Gampe | 5b20b35 | 2018-10-11 19:03:20 -0700 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 268 | // Callback responsible for making a batch of classes visibly initialized |
| 269 | // after all threads have called it from a checkpoint, ensuring visibility. |
| 270 | class ClassLinker::VisiblyInitializedCallback final |
| 271 | : public Closure, public IntrusiveForwardListNode<VisiblyInitializedCallback> { |
| 272 | public: |
| 273 | explicit VisiblyInitializedCallback(ClassLinker* class_linker) |
| 274 | : class_linker_(class_linker), |
| 275 | num_classes_(0u), |
| 276 | thread_visibility_counter_(0), |
| 277 | barriers_() { |
| 278 | std::fill_n(classes_, kMaxClasses, nullptr); |
| 279 | } |
| 280 | |
| 281 | bool IsEmpty() const { |
| 282 | DCHECK_LE(num_classes_, kMaxClasses); |
| 283 | return num_classes_ == 0u; |
| 284 | } |
| 285 | |
| 286 | bool IsFull() const { |
| 287 | DCHECK_LE(num_classes_, kMaxClasses); |
| 288 | return num_classes_ == kMaxClasses; |
| 289 | } |
| 290 | |
| 291 | void AddClass(Thread* self, ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 292 | DCHECK_EQ(klass->GetStatus(), ClassStatus::kInitialized); |
| 293 | DCHECK(!IsFull()); |
| 294 | classes_[num_classes_] = self->GetJniEnv()->GetVm()->AddWeakGlobalRef(self, klass); |
| 295 | ++num_classes_; |
| 296 | } |
| 297 | |
| 298 | void AddBarrier(Barrier* barrier) { |
| 299 | barriers_.push_front(barrier); |
| 300 | } |
| 301 | |
| 302 | std::forward_list<Barrier*> GetAndClearBarriers() { |
| 303 | std::forward_list<Barrier*> result; |
| 304 | result.swap(barriers_); |
| 305 | result.reverse(); // Return barriers in insertion order. |
| 306 | return result; |
| 307 | } |
| 308 | |
| 309 | void MakeVisible(Thread* self) { |
| 310 | DCHECK_EQ(thread_visibility_counter_.load(std::memory_order_relaxed), 0); |
| 311 | size_t count = Runtime::Current()->GetThreadList()->RunCheckpoint(this); |
| 312 | AdjustThreadVisibilityCounter(self, count); |
| 313 | } |
| 314 | |
| 315 | void Run(Thread* self) override { |
| 316 | self->ClearMakeVisiblyInitializedCounter(); |
| 317 | AdjustThreadVisibilityCounter(self, -1); |
| 318 | } |
| 319 | |
| 320 | private: |
| 321 | void AdjustThreadVisibilityCounter(Thread* self, ssize_t adjustment) { |
| 322 | ssize_t old = thread_visibility_counter_.fetch_add(adjustment, std::memory_order_relaxed); |
| 323 | if (old + adjustment == 0) { |
| 324 | // All threads passed the checkpoint. Mark classes as visibly initialized. |
| 325 | { |
| 326 | ScopedObjectAccess soa(self); |
| 327 | StackHandleScope<1u> hs(self); |
| 328 | MutableHandle<mirror::Class> klass = hs.NewHandle<mirror::Class>(nullptr); |
| 329 | JavaVMExt* vm = self->GetJniEnv()->GetVm(); |
| 330 | for (size_t i = 0, num = num_classes_; i != num; ++i) { |
| 331 | klass.Assign(ObjPtr<mirror::Class>::DownCast(self->DecodeJObject(classes_[i]))); |
| 332 | vm->DeleteWeakGlobalRef(self, classes_[i]); |
| 333 | if (klass != nullptr) { |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 334 | mirror::Class::SetStatus(klass, ClassStatus::kVisiblyInitialized, self); |
Vladimir Marko | 86c8752 | 2020-05-11 16:55:55 +0100 | [diff] [blame] | 335 | class_linker_->FixupStaticTrampolines(self, klass.Get()); |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 336 | } |
| 337 | } |
| 338 | num_classes_ = 0u; |
| 339 | } |
| 340 | class_linker_->VisiblyInitializedCallbackDone(self, this); |
| 341 | } |
| 342 | } |
| 343 | |
Vladimir Marko | 9f18fbc | 2019-07-31 15:06:12 +0100 | [diff] [blame] | 344 | static constexpr size_t kMaxClasses = 16; |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 345 | |
| 346 | ClassLinker* const class_linker_; |
| 347 | size_t num_classes_; |
| 348 | jweak classes_[kMaxClasses]; |
| 349 | |
| 350 | // The thread visibility counter starts at 0 and it is incremented by the number of |
| 351 | // threads that need to run this callback (by the thread that request the callback |
| 352 | // to be run) and decremented once for each `Run()` execution. When it reaches 0, |
| 353 | // whether after the increment or after a decrement, we know that `Run()` was executed |
| 354 | // for all threads and therefore we can mark the classes as visibly initialized. |
| 355 | std::atomic<ssize_t> thread_visibility_counter_; |
| 356 | |
| 357 | // List of barries to `Pass()` for threads that wait for the callback to complete. |
| 358 | std::forward_list<Barrier*> barriers_; |
| 359 | }; |
| 360 | |
| 361 | void ClassLinker::MakeInitializedClassesVisiblyInitialized(Thread* self, bool wait) { |
| 362 | if (kRuntimeISA == InstructionSet::kX86 || kRuntimeISA == InstructionSet::kX86_64) { |
| 363 | return; // Nothing to do. Thanks to the x86 memory model classes skip the initialized status. |
| 364 | } |
| 365 | std::optional<Barrier> maybe_barrier; // Avoid constructing the Barrier for `wait == false`. |
| 366 | if (wait) { |
| 367 | maybe_barrier.emplace(0); |
| 368 | } |
| 369 | int wait_count = 0; |
| 370 | VisiblyInitializedCallback* callback = nullptr; |
| 371 | { |
| 372 | MutexLock lock(self, visibly_initialized_callback_lock_); |
| 373 | if (visibly_initialized_callback_ != nullptr && !visibly_initialized_callback_->IsEmpty()) { |
| 374 | callback = visibly_initialized_callback_.release(); |
| 375 | running_visibly_initialized_callbacks_.push_front(*callback); |
| 376 | } |
| 377 | if (wait) { |
| 378 | DCHECK(maybe_barrier.has_value()); |
| 379 | Barrier* barrier = std::addressof(*maybe_barrier); |
| 380 | for (VisiblyInitializedCallback& cb : running_visibly_initialized_callbacks_) { |
| 381 | cb.AddBarrier(barrier); |
| 382 | ++wait_count; |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | if (callback != nullptr) { |
| 387 | callback->MakeVisible(self); |
| 388 | } |
| 389 | if (wait_count != 0) { |
| 390 | DCHECK(maybe_barrier.has_value()); |
| 391 | maybe_barrier->Increment(self, wait_count); |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | void ClassLinker::VisiblyInitializedCallbackDone(Thread* self, |
| 396 | VisiblyInitializedCallback* callback) { |
| 397 | MutexLock lock(self, visibly_initialized_callback_lock_); |
| 398 | // Pass the barriers if requested. |
| 399 | for (Barrier* barrier : callback->GetAndClearBarriers()) { |
| 400 | barrier->Pass(self); |
| 401 | } |
| 402 | // Remove the callback from the list of running callbacks. |
| 403 | auto before = running_visibly_initialized_callbacks_.before_begin(); |
| 404 | auto it = running_visibly_initialized_callbacks_.begin(); |
| 405 | DCHECK(it != running_visibly_initialized_callbacks_.end()); |
| 406 | while (std::addressof(*it) != callback) { |
| 407 | before = it; |
| 408 | ++it; |
| 409 | DCHECK(it != running_visibly_initialized_callbacks_.end()); |
| 410 | } |
| 411 | running_visibly_initialized_callbacks_.erase_after(before); |
| 412 | // Reuse or destroy the callback object. |
| 413 | if (visibly_initialized_callback_ == nullptr) { |
| 414 | visibly_initialized_callback_.reset(callback); |
| 415 | } else { |
| 416 | delete callback; |
| 417 | } |
| 418 | } |
| 419 | |
Alex Light | fb11957 | 2019-09-18 15:04:53 -0700 | [diff] [blame] | 420 | void ClassLinker::ForceClassInitialized(Thread* self, Handle<mirror::Class> klass) { |
| 421 | ClassLinker::VisiblyInitializedCallback* cb = MarkClassInitialized(self, klass); |
| 422 | if (cb != nullptr) { |
| 423 | cb->MakeVisible(self); |
| 424 | } |
| 425 | ScopedThreadSuspension sts(self, ThreadState::kSuspended); |
| 426 | MakeInitializedClassesVisiblyInitialized(self, /*wait=*/true); |
| 427 | } |
| 428 | |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 429 | ClassLinker::VisiblyInitializedCallback* ClassLinker::MarkClassInitialized( |
| 430 | Thread* self, Handle<mirror::Class> klass) { |
| 431 | if (kRuntimeISA == InstructionSet::kX86 || kRuntimeISA == InstructionSet::kX86_64) { |
| 432 | // Thanks to the x86 memory model, we do not need any memory fences and |
| 433 | // we can immediately mark the class as visibly initialized. |
| 434 | mirror::Class::SetStatus(klass, ClassStatus::kVisiblyInitialized, self); |
Vladimir Marko | 86c8752 | 2020-05-11 16:55:55 +0100 | [diff] [blame] | 435 | FixupStaticTrampolines(self, klass.Get()); |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 436 | return nullptr; |
| 437 | } |
| 438 | if (Runtime::Current()->IsActiveTransaction()) { |
| 439 | // Transactions are single-threaded, so we can mark the class as visibly intialized. |
| 440 | // (Otherwise we'd need to track the callback's entry in the transaction for rollback.) |
| 441 | mirror::Class::SetStatus(klass, ClassStatus::kVisiblyInitialized, self); |
Vladimir Marko | 86c8752 | 2020-05-11 16:55:55 +0100 | [diff] [blame] | 442 | FixupStaticTrampolines(self, klass.Get()); |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 443 | return nullptr; |
| 444 | } |
| 445 | mirror::Class::SetStatus(klass, ClassStatus::kInitialized, self); |
| 446 | MutexLock lock(self, visibly_initialized_callback_lock_); |
| 447 | if (visibly_initialized_callback_ == nullptr) { |
| 448 | visibly_initialized_callback_.reset(new VisiblyInitializedCallback(this)); |
| 449 | } |
| 450 | DCHECK(!visibly_initialized_callback_->IsFull()); |
| 451 | visibly_initialized_callback_->AddClass(self, klass.Get()); |
| 452 | |
| 453 | if (visibly_initialized_callback_->IsFull()) { |
| 454 | VisiblyInitializedCallback* callback = visibly_initialized_callback_.release(); |
| 455 | running_visibly_initialized_callbacks_.push_front(*callback); |
| 456 | return callback; |
| 457 | } else { |
| 458 | return nullptr; |
| 459 | } |
| 460 | } |
| 461 | |
Vladimir Marko | 86c8752 | 2020-05-11 16:55:55 +0100 | [diff] [blame] | 462 | const void* ClassLinker::RegisterNative( |
| 463 | Thread* self, ArtMethod* method, const void* native_method) { |
| 464 | CHECK(method->IsNative()) << method->PrettyMethod(); |
| 465 | CHECK(native_method != nullptr) << method->PrettyMethod(); |
| 466 | void* new_native_method = nullptr; |
| 467 | Runtime* runtime = Runtime::Current(); |
| 468 | runtime->GetRuntimeCallbacks()->RegisterNativeMethod(method, |
| 469 | native_method, |
| 470 | /*out*/&new_native_method); |
| 471 | if (method->IsCriticalNative()) { |
| 472 | MutexLock lock(self, critical_native_code_with_clinit_check_lock_); |
| 473 | // Remove old registered method if any. |
| 474 | auto it = critical_native_code_with_clinit_check_.find(method); |
| 475 | if (it != critical_native_code_with_clinit_check_.end()) { |
| 476 | critical_native_code_with_clinit_check_.erase(it); |
| 477 | } |
| 478 | // To ensure correct memory visibility, we need the class to be visibly |
| 479 | // initialized before we can set the JNI entrypoint. |
| 480 | if (method->GetDeclaringClass()->IsVisiblyInitialized()) { |
| 481 | method->SetEntryPointFromJni(new_native_method); |
| 482 | } else { |
| 483 | critical_native_code_with_clinit_check_.emplace(method, new_native_method); |
| 484 | } |
| 485 | } else { |
| 486 | method->SetEntryPointFromJni(new_native_method); |
| 487 | } |
| 488 | return new_native_method; |
| 489 | } |
| 490 | |
| 491 | void ClassLinker::UnregisterNative(Thread* self, ArtMethod* method) { |
| 492 | CHECK(method->IsNative()) << method->PrettyMethod(); |
| 493 | // Restore stub to lookup native pointer via dlsym. |
| 494 | if (method->IsCriticalNative()) { |
| 495 | MutexLock lock(self, critical_native_code_with_clinit_check_lock_); |
| 496 | auto it = critical_native_code_with_clinit_check_.find(method); |
| 497 | if (it != critical_native_code_with_clinit_check_.end()) { |
| 498 | critical_native_code_with_clinit_check_.erase(it); |
| 499 | } |
| 500 | method->SetEntryPointFromJni(GetJniDlsymLookupCriticalStub()); |
| 501 | } else { |
| 502 | method->SetEntryPointFromJni(GetJniDlsymLookupStub()); |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | const void* ClassLinker::GetRegisteredNative(Thread* self, ArtMethod* method) { |
| 507 | if (method->IsCriticalNative()) { |
| 508 | MutexLock lock(self, critical_native_code_with_clinit_check_lock_); |
| 509 | auto it = critical_native_code_with_clinit_check_.find(method); |
| 510 | if (it != critical_native_code_with_clinit_check_.end()) { |
| 511 | return it->second; |
| 512 | } |
| 513 | const void* native_code = method->GetEntryPointFromJni(); |
| 514 | return IsJniDlsymLookupCriticalStub(native_code) ? nullptr : native_code; |
| 515 | } else { |
| 516 | const void* native_code = method->GetEntryPointFromJni(); |
| 517 | return IsJniDlsymLookupStub(native_code) ? nullptr : native_code; |
| 518 | } |
| 519 | } |
| 520 | |
Andreas Gampe | 7b3063b | 2019-01-07 14:12:52 -0800 | [diff] [blame] | 521 | void ClassLinker::ThrowEarlierClassFailure(ObjPtr<mirror::Class> c, |
| 522 | bool wrap_in_no_class_def, |
| 523 | bool log) { |
Elliott Hughes | 5c59994 | 2012-06-13 16:45:05 -0700 | [diff] [blame] | 524 | // The class failed to initialize on a previous attempt, so we want to throw |
| 525 | // a NoClassDefFoundError (v2 2.17.5). The exception to this rule is if we |
| 526 | // failed in verification, in which case v2 5.4.1 says we need to re-throw |
| 527 | // the previous error. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 528 | Runtime* const runtime = Runtime::Current(); |
| 529 | if (!runtime->IsAotCompiler()) { // Give info if this occurs at runtime. |
Andreas Gampe | 3d6b470 | 2015-09-21 08:35:52 -0700 | [diff] [blame] | 530 | std::string extra; |
Vladimir Marko | bb206de | 2019-03-28 10:30:32 +0000 | [diff] [blame] | 531 | ObjPtr<mirror::Object> verify_error = GetVerifyError(c); |
| 532 | if (verify_error != nullptr) { |
Andreas Gampe | 369c851 | 2016-01-28 15:31:39 -0800 | [diff] [blame] | 533 | if (verify_error->IsClass()) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 534 | extra = mirror::Class::PrettyDescriptor(verify_error->AsClass()); |
Andreas Gampe | 369c851 | 2016-01-28 15:31:39 -0800 | [diff] [blame] | 535 | } else { |
| 536 | extra = verify_error->AsThrowable()->Dump(); |
| 537 | } |
Andreas Gampe | 3d6b470 | 2015-09-21 08:35:52 -0700 | [diff] [blame] | 538 | } |
Andreas Gampe | 7b3063b | 2019-01-07 14:12:52 -0800 | [diff] [blame] | 539 | if (log) { |
| 540 | LOG(INFO) << "Rejecting re-init on previously-failed class " << c->PrettyClass() |
| 541 | << ": " << extra; |
| 542 | } |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 543 | } |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 544 | |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 545 | CHECK(c->IsErroneous()) << c->PrettyClass() << " " << c->GetStatus(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 546 | Thread* self = Thread::Current(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 547 | if (runtime->IsAotCompiler()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 548 | // At compile time, accurate errors and NCDFE are disabled to speed compilation. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 549 | ObjPtr<mirror::Throwable> pre_allocated = runtime->GetPreAllocatedNoClassDefFoundError(); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 550 | self->SetException(pre_allocated); |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 551 | } else { |
Vladimir Marko | bb206de | 2019-03-28 10:30:32 +0000 | [diff] [blame] | 552 | ObjPtr<mirror::Object> verify_error = GetVerifyError(c); |
| 553 | if (verify_error != nullptr) { |
Andreas Gampe | cb08695 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 554 | // Rethrow stored error. |
Andreas Gampe | 99babb6 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 555 | HandleEarlierVerifyError(self, this, c); |
Andreas Gampe | cb08695 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 556 | } |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 557 | // TODO This might be wrong if we hit an OOME while allocating the ClassExt. In that case we |
| 558 | // might have meant to go down the earlier if statement with the original error but it got |
| 559 | // swallowed by the OOM so we end up here. |
Vladimir Marko | bb206de | 2019-03-28 10:30:32 +0000 | [diff] [blame] | 560 | if (verify_error == nullptr || wrap_in_no_class_def) { |
Andreas Gampe | cb08695 | 2015-11-02 16:20:00 -0800 | [diff] [blame] | 561 | // If there isn't a recorded earlier error, or this is a repeat throw from initialization, |
| 562 | // the top-level exception must be a NoClassDefFoundError. The potentially already pending |
| 563 | // exception will be a cause. |
| 564 | self->ThrowNewWrappedException("Ljava/lang/NoClassDefFoundError;", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 565 | c->PrettyDescriptor().c_str()); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 566 | } |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 567 | } |
| 568 | } |
| 569 | |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 570 | static void VlogClassInitializationFailure(Handle<mirror::Class> klass) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 571 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 572 | if (VLOG_IS_ON(class_linker)) { |
| 573 | std::string temp; |
| 574 | LOG(INFO) << "Failed to initialize class " << klass->GetDescriptor(&temp) << " from " |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 575 | << klass->GetLocation() << "\n" << Thread::Current()->GetException()->Dump(); |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 576 | } |
| 577 | } |
| 578 | |
| 579 | static void WrapExceptionInInitializer(Handle<mirror::Class> klass) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 580 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 581 | Thread* self = Thread::Current(); |
| 582 | JNIEnv* env = self->GetJniEnv(); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 583 | |
| 584 | ScopedLocalRef<jthrowable> cause(env, env->ExceptionOccurred()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 585 | CHECK(cause.get() != nullptr); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 586 | |
David Srbecky | 346fd96 | 2020-07-27 16:51:00 +0100 | [diff] [blame] | 587 | // Boot classpath classes should not fail initialization. This is a consistency debug check. |
| 588 | // 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] | 589 | if (klass->GetClassLoader() == nullptr && !Runtime::Current()->IsAotCompiler()) { |
Andreas Gampe | 22f71d2 | 2016-11-21 10:10:08 -0800 | [diff] [blame] | 590 | std::string tmp; |
Alex Light | 5047d9f | 2018-03-09 15:44:31 -0800 | [diff] [blame] | 591 | // We want to LOG(FATAL) on debug builds since this really shouldn't be happening but we need to |
| 592 | // make sure to only do it if we don't have AsyncExceptions being thrown around since those |
| 593 | // could have caused the error. |
| 594 | bool known_impossible = kIsDebugBuild && !Runtime::Current()->AreAsyncExceptionsThrown(); |
| 595 | LOG(known_impossible ? FATAL : WARNING) << klass->GetDescriptor(&tmp) |
| 596 | << " failed initialization: " |
| 597 | << self->GetException()->Dump(); |
Andreas Gampe | 22f71d2 | 2016-11-21 10:10:08 -0800 | [diff] [blame] | 598 | } |
| 599 | |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 600 | env->ExceptionClear(); |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 601 | bool is_error = env->IsInstanceOf(cause.get(), WellKnownClasses::java_lang_Error); |
| 602 | env->Throw(cause.get()); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 603 | |
Elliott Hughes | a4f9474 | 2012-05-29 16:28:38 -0700 | [diff] [blame] | 604 | // We only wrap non-Error exceptions; an Error can just be used as-is. |
| 605 | if (!is_error) { |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 606 | self->ThrowNewWrappedException("Ljava/lang/ExceptionInInitializerError;", nullptr); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 607 | } |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 608 | VlogClassInitializationFailure(klass); |
Elliott Hughes | 4d0207c | 2011-10-03 19:14:34 -0700 | [diff] [blame] | 609 | } |
| 610 | |
Andreas Gampe | 87658f3 | 2019-04-18 18:39:02 +0000 | [diff] [blame] | 611 | ClassLinker::ClassLinker(InternTable* intern_table, bool fast_class_not_found_exceptions) |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 612 | : boot_class_table_(new ClassTable()), |
| 613 | failed_dex_cache_class_lookups_(0), |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 614 | class_roots_(nullptr), |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 615 | find_array_class_cache_next_victim_(0), |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 616 | init_done_(false), |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 617 | log_new_roots_(false), |
Jeff Hao | 0aba0ba | 2013-06-03 14:49:28 -0700 | [diff] [blame] | 618 | intern_table_(intern_table), |
Andreas Gampe | 87658f3 | 2019-04-18 18:39:02 +0000 | [diff] [blame] | 619 | fast_class_not_found_exceptions_(fast_class_not_found_exceptions), |
Vladimir Marko | 7dac864 | 2019-11-06 17:09:30 +0000 | [diff] [blame] | 620 | jni_dlsym_lookup_trampoline_(nullptr), |
Vladimir Marko | fa458ac | 2020-02-12 14:08:07 +0000 | [diff] [blame] | 621 | jni_dlsym_lookup_critical_trampoline_(nullptr), |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 622 | quick_resolution_trampoline_(nullptr), |
Andreas Gampe | 2da8823 | 2014-02-27 12:26:20 -0800 | [diff] [blame] | 623 | quick_imt_conflict_trampoline_(nullptr), |
Vladimir Marko | 8a63057 | 2014-04-09 18:45:35 +0100 | [diff] [blame] | 624 | quick_generic_jni_trampoline_(nullptr), |
Mathieu Chartier | 2d72101 | 2014-11-10 11:08:06 -0800 | [diff] [blame] | 625 | quick_to_interpreter_bridge_trampoline_(nullptr), |
Nicolas Geoffray | c39af94 | 2021-01-25 08:43:57 +0000 | [diff] [blame] | 626 | nterp_trampoline_(nullptr), |
Andreas Gampe | c1ac9ee | 2017-07-24 22:35:49 -0700 | [diff] [blame] | 627 | image_pointer_size_(kRuntimePointerSize), |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 628 | visibly_initialized_callback_lock_("visibly initialized callback lock"), |
| 629 | visibly_initialized_callback_(nullptr), |
Vladimir Marko | 86c8752 | 2020-05-11 16:55:55 +0100 | [diff] [blame] | 630 | critical_native_code_with_clinit_check_lock_("critical native code with clinit check lock"), |
| 631 | critical_native_code_with_clinit_check_(), |
Andreas Gampe | 7dface3 | 2017-07-25 21:32:59 -0700 | [diff] [blame] | 632 | cha_(Runtime::Current()->IsAotCompiler() ? nullptr : new ClassHierarchyAnalysis()) { |
| 633 | // For CHA disabled during Aot, see b/34193647. |
| 634 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 635 | CHECK(intern_table_ != nullptr); |
Andreas Gampe | 8ac7595 | 2015-06-02 21:01:45 -0700 | [diff] [blame] | 636 | static_assert(kFindArrayCacheSize == arraysize(find_array_class_cache_), |
| 637 | "Array cache size wrong."); |
| 638 | std::fill_n(find_array_class_cache_, kFindArrayCacheSize, GcRoot<mirror::Class>(nullptr)); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 639 | } |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 640 | |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 641 | void ClassLinker::CheckSystemClass(Thread* self, Handle<mirror::Class> c1, const char* descriptor) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 642 | ObjPtr<mirror::Class> c2 = FindSystemClass(self, descriptor); |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 643 | if (c2 == nullptr) { |
| 644 | LOG(FATAL) << "Could not find class " << descriptor; |
| 645 | UNREACHABLE(); |
| 646 | } |
| 647 | if (c1.Get() != c2) { |
| 648 | std::ostringstream os1, os2; |
| 649 | c1->DumpClass(os1, mirror::Class::kDumpClassFullDetail); |
| 650 | c2->DumpClass(os2, mirror::Class::kDumpClassFullDetail); |
| 651 | LOG(FATAL) << "InitWithoutImage: Class mismatch for " << descriptor |
| 652 | << ". This is most likely the result of a broken build. Make sure that " |
| 653 | << "libcore and art projects match.\n\n" |
| 654 | << os1.str() << "\n\n" << os2.str(); |
| 655 | UNREACHABLE(); |
| 656 | } |
| 657 | } |
| 658 | |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 659 | bool ClassLinker::InitWithoutImage(std::vector<std::unique_ptr<const DexFile>> boot_class_path, |
| 660 | std::string* error_msg) { |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 661 | VLOG(startup) << "ClassLinker::Init"; |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 662 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 663 | Thread* const self = Thread::Current(); |
| 664 | Runtime* const runtime = Runtime::Current(); |
| 665 | gc::Heap* const heap = runtime->GetHeap(); |
| 666 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 667 | CHECK(!heap->HasBootImageSpace()) << "Runtime has image. We should use it."; |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 668 | CHECK(!init_done_); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 669 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 670 | // Use the pointer size from the runtime since we are probably creating the image. |
| 671 | image_pointer_size_ = InstructionSetPointerSize(runtime->GetInstructionSet()); |
| 672 | |
Elliott Hughes | 3064683 | 2011-10-13 16:59:46 -0700 | [diff] [blame] | 673 | // java_lang_Class comes first, it's needed for AllocClass |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 674 | // 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] | 675 | heap->IncrementDisableMovingGC(self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 676 | StackHandleScope<64> hs(self); // 64 is picked arbitrarily. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 677 | auto class_class_size = mirror::Class::ClassClassSize(image_pointer_size_); |
Mathieu Chartier | d7a7f2f | 2018-09-07 11:57:18 -0700 | [diff] [blame] | 678 | // Allocate the object as non-movable so that there are no cases where Object::IsClass returns |
| 679 | // the incorrect result when comparing to-space vs from-space. |
Vladimir Marko | d7e9bbf | 2019-03-28 13:18:57 +0000 | [diff] [blame] | 680 | Handle<mirror::Class> java_lang_Class(hs.NewHandle(ObjPtr<mirror::Class>::DownCast( |
Vladimir Marko | 991cd5c | 2019-05-30 14:23:39 +0100 | [diff] [blame] | 681 | heap->AllocNonMovableObject(self, nullptr, class_class_size, VoidFunctor())))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 682 | CHECK(java_lang_Class != nullptr); |
Vladimir Marko | 317892b | 2018-05-31 11:11:32 +0100 | [diff] [blame] | 683 | java_lang_Class->SetClassFlags(mirror::kClassFlagClass); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 684 | java_lang_Class->SetClass(java_lang_Class.Get()); |
Hiroshi Yamauchi | 12b58b2 | 2016-11-01 11:55:29 -0700 | [diff] [blame] | 685 | if (kUseBakerReadBarrier) { |
| 686 | java_lang_Class->AssertReadBarrierState(); |
Hiroshi Yamauchi | 9d04a20 | 2014-01-31 13:35:49 -0800 | [diff] [blame] | 687 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 688 | java_lang_Class->SetClassSize(class_class_size); |
Hiroshi Yamauchi | f0edfc3 | 2014-09-25 11:46:46 -0700 | [diff] [blame] | 689 | java_lang_Class->SetPrimitiveType(Primitive::kPrimNot); |
Mathieu Chartier | 1d27b34 | 2014-01-28 12:51:09 -0800 | [diff] [blame] | 690 | heap->DecrementDisableMovingGC(self); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 691 | // AllocClass(ObjPtr<mirror::Class>) can now be used |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 692 | |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 693 | // Class[] is used for reflection support. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 694 | 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] | 695 | Handle<mirror::Class> class_array_class(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 696 | AllocClass(self, java_lang_Class.Get(), class_array_class_size))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 697 | class_array_class->SetComponentType(java_lang_Class.Get()); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 698 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 699 | // 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] | 700 | Handle<mirror::Class> java_lang_Object(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 701 | AllocClass(self, java_lang_Class.Get(), mirror::Object::ClassSize(image_pointer_size_)))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 702 | CHECK(java_lang_Object != nullptr); |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 703 | // backfill Object as the super class of Class. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 704 | java_lang_Class->SetSuperClass(java_lang_Object.Get()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 705 | mirror::Class::SetStatus(java_lang_Object, ClassStatus::kLoaded, self); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 706 | |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 707 | java_lang_Object->SetObjectSize(sizeof(mirror::Object)); |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 708 | // Allocate in non-movable so that it's possible to check if a JNI weak global ref has been |
| 709 | // cleared without triggering the read barrier and unintentionally mark the sentinel alive. |
Vladimir Marko | 991cd5c | 2019-05-30 14:23:39 +0100 | [diff] [blame] | 710 | runtime->SetSentinel(heap->AllocNonMovableObject(self, |
| 711 | java_lang_Object.Get(), |
| 712 | java_lang_Object->GetObjectSize(), |
| 713 | VoidFunctor())); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 714 | |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 715 | // Initialize the SubtypeCheck bitstring for java.lang.Object and java.lang.Class. |
Vladimir Marko | 305c38b | 2018-02-14 11:50:07 +0000 | [diff] [blame] | 716 | if (kBitstringSubtypeCheckEnabled) { |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 717 | // It might seem the lock here is unnecessary, however all the SubtypeCheck |
| 718 | // functions are annotated to require locks all the way down. |
| 719 | // |
| 720 | // We take the lock here to avoid using NO_THREAD_SAFETY_ANALYSIS. |
| 721 | MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_); |
Vladimir Marko | 38b8b25 | 2018-01-02 19:07:06 +0000 | [diff] [blame] | 722 | SubtypeCheck<ObjPtr<mirror::Class>>::EnsureInitialized(java_lang_Object.Get()); |
| 723 | SubtypeCheck<ObjPtr<mirror::Class>>::EnsureInitialized(java_lang_Class.Get()); |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 724 | } |
| 725 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 726 | // Object[] next to hold class roots. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 727 | Handle<mirror::Class> object_array_class(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 728 | AllocClass(self, java_lang_Class.Get(), |
| 729 | mirror::ObjectArray<mirror::Object>::ClassSize(image_pointer_size_)))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 730 | object_array_class->SetComponentType(java_lang_Object.Get()); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 731 | |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 732 | // Setup java.lang.String. |
| 733 | // |
| 734 | // We make this class non-movable for the unlikely case where it were to be |
| 735 | // moved by a sticky-bit (minor) collection when using the Generational |
| 736 | // Concurrent Copying (CC) collector, potentially creating a stale reference |
| 737 | // in the `klass_` field of one of its instances allocated in the Large-Object |
| 738 | // Space (LOS) -- see the comment about the dirty card scanning logic in |
| 739 | // art::gc::collector::ConcurrentCopying::MarkingPhase. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 740 | Handle<mirror::Class> java_lang_String(hs.NewHandle( |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 741 | AllocClass</* kMovable= */ false>( |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 742 | self, java_lang_Class.Get(), mirror::String::ClassSize(image_pointer_size_)))); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 743 | java_lang_String->SetStringClass(); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 744 | mirror::Class::SetStatus(java_lang_String, ClassStatus::kResolved, self); |
Jesse Wilson | 1415074 | 2011-07-29 19:04:44 -0400 | [diff] [blame] | 745 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 746 | // Setup java.lang.ref.Reference. |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 747 | Handle<mirror::Class> java_lang_ref_Reference(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 748 | AllocClass(self, java_lang_Class.Get(), mirror::Reference::ClassSize(image_pointer_size_)))); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 749 | java_lang_ref_Reference->SetObjectSize(mirror::Reference::InstanceSize()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 750 | mirror::Class::SetStatus(java_lang_ref_Reference, ClassStatus::kResolved, self); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 751 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 752 | // 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] | 753 | class_roots_ = GcRoot<mirror::ObjectArray<mirror::Class>>( |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 754 | mirror::ObjectArray<mirror::Class>::Alloc(self, |
| 755 | object_array_class.Get(), |
| 756 | static_cast<int32_t>(ClassRoot::kMax))); |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 757 | CHECK(!class_roots_.IsNull()); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 758 | SetClassRoot(ClassRoot::kJavaLangClass, java_lang_Class.Get()); |
| 759 | SetClassRoot(ClassRoot::kJavaLangObject, java_lang_Object.Get()); |
| 760 | SetClassRoot(ClassRoot::kClassArrayClass, class_array_class.Get()); |
| 761 | SetClassRoot(ClassRoot::kObjectArrayClass, object_array_class.Get()); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 762 | SetClassRoot(ClassRoot::kJavaLangString, java_lang_String.Get()); |
| 763 | SetClassRoot(ClassRoot::kJavaLangRefReference, java_lang_ref_Reference.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 764 | |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 765 | // Fill in the empty iftable. Needs to be done after the kObjectArrayClass root is set. |
| 766 | java_lang_Object->SetIfTable(AllocIfTable(self, 0)); |
| 767 | |
Vladimir Marko | 0261055 | 2018-06-04 14:38:00 +0100 | [diff] [blame] | 768 | // Create array interface entries to populate once we can load system classes. |
| 769 | object_array_class->SetIfTable(AllocIfTable(self, 2)); |
| 770 | DCHECK_EQ(GetArrayIfTable(), object_array_class->GetIfTable()); |
| 771 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 772 | // Setup the primitive type classes. |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 773 | CreatePrimitiveClass(self, Primitive::kPrimBoolean, ClassRoot::kPrimitiveBoolean); |
| 774 | CreatePrimitiveClass(self, Primitive::kPrimByte, ClassRoot::kPrimitiveByte); |
| 775 | CreatePrimitiveClass(self, Primitive::kPrimChar, ClassRoot::kPrimitiveChar); |
| 776 | CreatePrimitiveClass(self, Primitive::kPrimShort, ClassRoot::kPrimitiveShort); |
| 777 | CreatePrimitiveClass(self, Primitive::kPrimInt, ClassRoot::kPrimitiveInt); |
| 778 | CreatePrimitiveClass(self, Primitive::kPrimLong, ClassRoot::kPrimitiveLong); |
| 779 | CreatePrimitiveClass(self, Primitive::kPrimFloat, ClassRoot::kPrimitiveFloat); |
| 780 | CreatePrimitiveClass(self, Primitive::kPrimDouble, ClassRoot::kPrimitiveDouble); |
| 781 | CreatePrimitiveClass(self, Primitive::kPrimVoid, ClassRoot::kPrimitiveVoid); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 782 | |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 783 | // Allocate the primitive array classes. We need only the native pointer |
| 784 | // array at this point (int[] or long[], depending on architecture) but |
| 785 | // we shall perform the same setup steps for all primitive array classes. |
| 786 | AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveBoolean, ClassRoot::kBooleanArrayClass); |
| 787 | AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveByte, ClassRoot::kByteArrayClass); |
| 788 | AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveChar, ClassRoot::kCharArrayClass); |
| 789 | AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveShort, ClassRoot::kShortArrayClass); |
| 790 | AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveInt, ClassRoot::kIntArrayClass); |
| 791 | AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveLong, ClassRoot::kLongArrayClass); |
| 792 | AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveFloat, ClassRoot::kFloatArrayClass); |
| 793 | AllocPrimitiveArrayClass(self, ClassRoot::kPrimitiveDouble, ClassRoot::kDoubleArrayClass); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 794 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 795 | // now that these are registered, we can use AllocClass() and AllocObjectArray |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 796 | |
Ian Rogers | 52813c9 | 2012-10-11 11:50:38 -0700 | [diff] [blame] | 797 | // Set up DexCache. This cannot be done later since AppendToBootClassPath calls AllocDexCache. |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 798 | Handle<mirror::Class> java_lang_DexCache(hs.NewHandle( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 799 | AllocClass(self, java_lang_Class.Get(), mirror::DexCache::ClassSize(image_pointer_size_)))); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 800 | SetClassRoot(ClassRoot::kJavaLangDexCache, java_lang_DexCache.Get()); |
Vladimir Marko | 05792b9 | 2015-08-03 11:56:49 +0100 | [diff] [blame] | 801 | java_lang_DexCache->SetDexCacheClass(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 802 | java_lang_DexCache->SetObjectSize(mirror::DexCache::InstanceSize()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 803 | mirror::Class::SetStatus(java_lang_DexCache, ClassStatus::kResolved, self); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 804 | |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 805 | |
| 806 | // Setup dalvik.system.ClassExt |
| 807 | Handle<mirror::Class> dalvik_system_ClassExt(hs.NewHandle( |
| 808 | AllocClass(self, java_lang_Class.Get(), mirror::ClassExt::ClassSize(image_pointer_size_)))); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 809 | SetClassRoot(ClassRoot::kDalvikSystemClassExt, dalvik_system_ClassExt.Get()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 810 | mirror::Class::SetStatus(dalvik_system_ClassExt, ClassStatus::kResolved, self); |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 811 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 812 | // Set up array classes for string, field, method |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 813 | Handle<mirror::Class> object_array_string(hs.NewHandle( |
| 814 | AllocClass(self, java_lang_Class.Get(), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 815 | mirror::ObjectArray<mirror::String>::ClassSize(image_pointer_size_)))); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 816 | object_array_string->SetComponentType(java_lang_String.Get()); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 817 | SetClassRoot(ClassRoot::kJavaLangStringArrayClass, object_array_string.Get()); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 818 | |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 819 | LinearAlloc* linear_alloc = runtime->GetLinearAlloc(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 820 | // Create runtime resolution and imt conflict methods. |
| 821 | runtime->SetResolutionMethod(runtime->CreateResolutionMethod()); |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 822 | runtime->SetImtConflictMethod(runtime->CreateImtConflictMethod(linear_alloc)); |
| 823 | runtime->SetImtUnimplementedMethod(runtime->CreateImtConflictMethod(linear_alloc)); |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 824 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 825 | // Setup boot_class_path_ and register class_path now that we can use AllocObjectArray to create |
| 826 | // DexCache instances. Needs to be after String, Field, Method arrays since AllocDexCache uses |
| 827 | // these roots. |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 828 | if (boot_class_path.empty()) { |
| 829 | *error_msg = "Boot classpath is empty."; |
| 830 | return false; |
| 831 | } |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 832 | for (auto& dex_file : boot_class_path) { |
Mathieu Chartier | 0a19e21 | 2019-11-27 14:35:24 -0800 | [diff] [blame] | 833 | if (dex_file == nullptr) { |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 834 | *error_msg = "Null dex file."; |
| 835 | return false; |
| 836 | } |
Mathieu Chartier | 0a19e21 | 2019-11-27 14:35:24 -0800 | [diff] [blame] | 837 | AppendToBootClassPath(self, dex_file.get()); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 838 | boot_dex_files_.push_back(std::move(dex_file)); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 839 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 840 | |
| 841 | // now we can use FindSystemClass |
| 842 | |
Dmitry Petrochenko | f0972a4 | 2014-05-16 17:43:39 +0700 | [diff] [blame] | 843 | // Set up GenericJNI entrypoint. That is mainly a hack for common_compiler_test.h so that |
| 844 | // we do not need friend classes or a publicly exposed setter. |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 845 | quick_generic_jni_trampoline_ = GetQuickGenericJniStub(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 846 | if (!runtime->IsAotCompiler()) { |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 847 | // 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] | 848 | jni_dlsym_lookup_trampoline_ = GetJniDlsymLookupStub(); |
Vladimir Marko | fa458ac | 2020-02-12 14:08:07 +0000 | [diff] [blame] | 849 | jni_dlsym_lookup_critical_trampoline_ = GetJniDlsymLookupCriticalStub(); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 850 | quick_resolution_trampoline_ = GetQuickResolutionStub(); |
| 851 | quick_imt_conflict_trampoline_ = GetQuickImtConflictStub(); |
Vladimir Marko | 7dac864 | 2019-11-06 17:09:30 +0000 | [diff] [blame] | 852 | quick_generic_jni_trampoline_ = GetQuickGenericJniStub(); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 853 | quick_to_interpreter_bridge_trampoline_ = GetQuickToInterpreterBridge(); |
Nicolas Geoffray | c39af94 | 2021-01-25 08:43:57 +0000 | [diff] [blame] | 854 | nterp_trampoline_ = interpreter::GetNterpEntryPoint(); |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 855 | } |
Dmitry Petrochenko | f0972a4 | 2014-05-16 17:43:39 +0700 | [diff] [blame] | 856 | |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 857 | // 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] | 858 | mirror::Class::SetStatus(java_lang_Object, ClassStatus::kNotReady, self); |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 859 | CheckSystemClass(self, java_lang_Object, "Ljava/lang/Object;"); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 860 | CHECK_EQ(java_lang_Object->GetObjectSize(), mirror::Object::InstanceSize()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 861 | mirror::Class::SetStatus(java_lang_String, ClassStatus::kNotReady, self); |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 862 | CheckSystemClass(self, java_lang_String, "Ljava/lang/String;"); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 863 | mirror::Class::SetStatus(java_lang_DexCache, ClassStatus::kNotReady, self); |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 864 | CheckSystemClass(self, java_lang_DexCache, "Ljava/lang/DexCache;"); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 865 | CHECK_EQ(java_lang_DexCache->GetObjectSize(), mirror::DexCache::InstanceSize()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 866 | mirror::Class::SetStatus(dalvik_system_ClassExt, ClassStatus::kNotReady, self); |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 867 | CheckSystemClass(self, dalvik_system_ClassExt, "Ldalvik/system/ClassExt;"); |
| 868 | CHECK_EQ(dalvik_system_ClassExt->GetObjectSize(), mirror::ClassExt::InstanceSize()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 869 | |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 870 | // Run Class through FindSystemClass. This initializes the dex_cache_ fields and register it |
| 871 | // in class_table_. |
| 872 | CheckSystemClass(self, java_lang_Class, "Ljava/lang/Class;"); |
Elliott Hughes | 418d20f | 2011-09-22 14:00:39 -0700 | [diff] [blame] | 873 | |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 874 | // Setup core array classes, i.e. Object[], String[] and Class[] and primitive |
| 875 | // arrays - can't be done until Object has a vtable and component classes are loaded. |
| 876 | FinishCoreArrayClassSetup(ClassRoot::kObjectArrayClass); |
| 877 | FinishCoreArrayClassSetup(ClassRoot::kClassArrayClass); |
| 878 | FinishCoreArrayClassSetup(ClassRoot::kJavaLangStringArrayClass); |
| 879 | FinishCoreArrayClassSetup(ClassRoot::kBooleanArrayClass); |
| 880 | FinishCoreArrayClassSetup(ClassRoot::kByteArrayClass); |
| 881 | FinishCoreArrayClassSetup(ClassRoot::kCharArrayClass); |
| 882 | FinishCoreArrayClassSetup(ClassRoot::kShortArrayClass); |
| 883 | FinishCoreArrayClassSetup(ClassRoot::kIntArrayClass); |
| 884 | FinishCoreArrayClassSetup(ClassRoot::kLongArrayClass); |
| 885 | FinishCoreArrayClassSetup(ClassRoot::kFloatArrayClass); |
| 886 | FinishCoreArrayClassSetup(ClassRoot::kDoubleArrayClass); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 887 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 888 | // Setup the single, global copy of "iftable". |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 889 | auto java_lang_Cloneable = hs.NewHandle(FindSystemClass(self, "Ljava/lang/Cloneable;")); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 890 | CHECK(java_lang_Cloneable != nullptr); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 891 | auto java_io_Serializable = hs.NewHandle(FindSystemClass(self, "Ljava/io/Serializable;")); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 892 | CHECK(java_io_Serializable != nullptr); |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 893 | // We assume that Cloneable/Serializable don't have superinterfaces -- normally we'd have to |
| 894 | // crawl up and explicitly list all of the supers as well. |
Vladimir Marko | 0261055 | 2018-06-04 14:38:00 +0100 | [diff] [blame] | 895 | object_array_class->GetIfTable()->SetInterface(0, java_lang_Cloneable.Get()); |
| 896 | object_array_class->GetIfTable()->SetInterface(1, java_io_Serializable.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 897 | |
David Srbecky | 346fd96 | 2020-07-27 16:51:00 +0100 | [diff] [blame] | 898 | // Check Class[] and Object[]'s interfaces. GetDirectInterface may cause thread suspension. |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 899 | CHECK_EQ(java_lang_Cloneable.Get(), |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 900 | mirror::Class::GetDirectInterface(self, class_array_class.Get(), 0)); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 901 | CHECK_EQ(java_io_Serializable.Get(), |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 902 | mirror::Class::GetDirectInterface(self, class_array_class.Get(), 1)); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 903 | CHECK_EQ(java_lang_Cloneable.Get(), |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 904 | mirror::Class::GetDirectInterface(self, object_array_class.Get(), 0)); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 905 | CHECK_EQ(java_io_Serializable.Get(), |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 906 | mirror::Class::GetDirectInterface(self, object_array_class.Get(), 1)); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 907 | |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 908 | CHECK_EQ(object_array_string.Get(), |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 909 | FindSystemClass(self, GetClassRootDescriptor(ClassRoot::kJavaLangStringArrayClass))); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 910 | |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 911 | // 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] | 912 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 913 | // Create java.lang.reflect.Proxy root. |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 914 | SetClassRoot(ClassRoot::kJavaLangReflectProxy, |
| 915 | FindSystemClass(self, "Ljava/lang/reflect/Proxy;")); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 916 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 917 | // Create java.lang.reflect.Field.class root. |
Vladimir Marko | acb906d | 2018-05-30 10:23:49 +0100 | [diff] [blame] | 918 | ObjPtr<mirror::Class> class_root = FindSystemClass(self, "Ljava/lang/reflect/Field;"); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 919 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 920 | SetClassRoot(ClassRoot::kJavaLangReflectField, class_root); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 921 | |
| 922 | // Create java.lang.reflect.Field array root. |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 923 | class_root = FindSystemClass(self, "[Ljava/lang/reflect/Field;"); |
| 924 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 925 | SetClassRoot(ClassRoot::kJavaLangReflectFieldArrayClass, class_root); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 926 | |
| 927 | // Create java.lang.reflect.Constructor.class root and array root. |
| 928 | class_root = FindSystemClass(self, "Ljava/lang/reflect/Constructor;"); |
| 929 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 930 | SetClassRoot(ClassRoot::kJavaLangReflectConstructor, class_root); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 931 | class_root = FindSystemClass(self, "[Ljava/lang/reflect/Constructor;"); |
| 932 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 933 | SetClassRoot(ClassRoot::kJavaLangReflectConstructorArrayClass, class_root); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 934 | |
| 935 | // Create java.lang.reflect.Method.class root and array root. |
| 936 | class_root = FindSystemClass(self, "Ljava/lang/reflect/Method;"); |
| 937 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 938 | SetClassRoot(ClassRoot::kJavaLangReflectMethod, class_root); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 939 | class_root = FindSystemClass(self, "[Ljava/lang/reflect/Method;"); |
| 940 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 941 | SetClassRoot(ClassRoot::kJavaLangReflectMethodArrayClass, class_root); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 942 | |
Orion Hodson | 005ac51 | 2017-10-24 15:43:43 +0100 | [diff] [blame] | 943 | // Create java.lang.invoke.CallSite.class root |
| 944 | class_root = FindSystemClass(self, "Ljava/lang/invoke/CallSite;"); |
| 945 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 946 | SetClassRoot(ClassRoot::kJavaLangInvokeCallSite, class_root); |
Orion Hodson | 005ac51 | 2017-10-24 15:43:43 +0100 | [diff] [blame] | 947 | |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 948 | // Create java.lang.invoke.MethodType.class root |
| 949 | class_root = FindSystemClass(self, "Ljava/lang/invoke/MethodType;"); |
| 950 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 951 | SetClassRoot(ClassRoot::kJavaLangInvokeMethodType, class_root); |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 952 | |
| 953 | // Create java.lang.invoke.MethodHandleImpl.class root |
| 954 | class_root = FindSystemClass(self, "Ljava/lang/invoke/MethodHandleImpl;"); |
| 955 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 956 | SetClassRoot(ClassRoot::kJavaLangInvokeMethodHandleImpl, class_root); |
Vladimir Marko | c7aa87e | 2018-05-24 15:19:52 +0100 | [diff] [blame] | 957 | SetClassRoot(ClassRoot::kJavaLangInvokeMethodHandle, class_root->GetSuperClass()); |
Narayan Kamath | afa4827 | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 958 | |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 959 | // Create java.lang.invoke.MethodHandles.Lookup.class root |
| 960 | class_root = FindSystemClass(self, "Ljava/lang/invoke/MethodHandles$Lookup;"); |
| 961 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 962 | SetClassRoot(ClassRoot::kJavaLangInvokeMethodHandlesLookup, class_root); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 963 | |
Orion Hodson | 005ac51 | 2017-10-24 15:43:43 +0100 | [diff] [blame] | 964 | // Create java.lang.invoke.VarHandle.class root |
| 965 | class_root = FindSystemClass(self, "Ljava/lang/invoke/VarHandle;"); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 966 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 967 | SetClassRoot(ClassRoot::kJavaLangInvokeVarHandle, class_root); |
Orion Hodson | 005ac51 | 2017-10-24 15:43:43 +0100 | [diff] [blame] | 968 | |
| 969 | // Create java.lang.invoke.FieldVarHandle.class root |
| 970 | class_root = FindSystemClass(self, "Ljava/lang/invoke/FieldVarHandle;"); |
| 971 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 972 | SetClassRoot(ClassRoot::kJavaLangInvokeFieldVarHandle, class_root); |
Orion Hodson | 005ac51 | 2017-10-24 15:43:43 +0100 | [diff] [blame] | 973 | |
| 974 | // Create java.lang.invoke.ArrayElementVarHandle.class root |
| 975 | class_root = FindSystemClass(self, "Ljava/lang/invoke/ArrayElementVarHandle;"); |
| 976 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 977 | SetClassRoot(ClassRoot::kJavaLangInvokeArrayElementVarHandle, class_root); |
Orion Hodson | 005ac51 | 2017-10-24 15:43:43 +0100 | [diff] [blame] | 978 | |
| 979 | // Create java.lang.invoke.ByteArrayViewVarHandle.class root |
| 980 | class_root = FindSystemClass(self, "Ljava/lang/invoke/ByteArrayViewVarHandle;"); |
| 981 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 982 | SetClassRoot(ClassRoot::kJavaLangInvokeByteArrayViewVarHandle, class_root); |
Orion Hodson | 005ac51 | 2017-10-24 15:43:43 +0100 | [diff] [blame] | 983 | |
| 984 | // Create java.lang.invoke.ByteBufferViewVarHandle.class root |
| 985 | class_root = FindSystemClass(self, "Ljava/lang/invoke/ByteBufferViewVarHandle;"); |
| 986 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 987 | SetClassRoot(ClassRoot::kJavaLangInvokeByteBufferViewVarHandle, class_root); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 988 | |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 989 | class_root = FindSystemClass(self, "Ldalvik/system/EmulatedStackFrame;"); |
| 990 | CHECK(class_root != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 991 | SetClassRoot(ClassRoot::kDalvikSystemEmulatedStackFrame, class_root); |
Narayan Kamath | 000e188 | 2016-10-24 17:14:25 +0100 | [diff] [blame] | 992 | |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 993 | // 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] | 994 | // finish initializing Reference class |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 995 | mirror::Class::SetStatus(java_lang_ref_Reference, ClassStatus::kNotReady, self); |
Andreas Gampe | 7ba5a67 | 2016-02-04 21:45:01 -0800 | [diff] [blame] | 996 | CheckSystemClass(self, java_lang_ref_Reference, "Ljava/lang/ref/Reference;"); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 997 | CHECK_EQ(java_lang_ref_Reference->GetObjectSize(), mirror::Reference::InstanceSize()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 998 | CHECK_EQ(java_lang_ref_Reference->GetClassSize(), |
| 999 | mirror::Reference::ClassSize(image_pointer_size_)); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 1000 | class_root = FindSystemClass(self, "Ljava/lang/ref/FinalizerReference;"); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 1001 | CHECK_EQ(class_root->GetClassFlags(), mirror::kClassFlagNormal); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 1002 | class_root->SetClassFlags(class_root->GetClassFlags() | mirror::kClassFlagFinalizerReference); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 1003 | class_root = FindSystemClass(self, "Ljava/lang/ref/PhantomReference;"); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 1004 | CHECK_EQ(class_root->GetClassFlags(), mirror::kClassFlagNormal); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 1005 | class_root->SetClassFlags(class_root->GetClassFlags() | mirror::kClassFlagPhantomReference); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 1006 | class_root = FindSystemClass(self, "Ljava/lang/ref/SoftReference;"); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 1007 | CHECK_EQ(class_root->GetClassFlags(), mirror::kClassFlagNormal); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 1008 | class_root->SetClassFlags(class_root->GetClassFlags() | mirror::kClassFlagSoftReference); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 1009 | class_root = FindSystemClass(self, "Ljava/lang/ref/WeakReference;"); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 1010 | CHECK_EQ(class_root->GetClassFlags(), mirror::kClassFlagNormal); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 1011 | class_root->SetClassFlags(class_root->GetClassFlags() | mirror::kClassFlagWeakReference); |
Brian Carlstrom | 1f87008 | 2011-08-23 16:02:11 -0700 | [diff] [blame] | 1012 | |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 1013 | // Setup the ClassLoader, verifying the object_size_. |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 1014 | class_root = FindSystemClass(self, "Ljava/lang/ClassLoader;"); |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 1015 | class_root->SetClassLoaderClass(); |
Mathieu Chartier | fc58af4 | 2015-04-16 18:00:39 -0700 | [diff] [blame] | 1016 | CHECK_EQ(class_root->GetObjectSize(), mirror::ClassLoader::InstanceSize()); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 1017 | SetClassRoot(ClassRoot::kJavaLangClassLoader, class_root); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 1018 | |
jeffhao | 8cd6dda | 2012-02-22 10:15:34 -0800 | [diff] [blame] | 1019 | // Set up java.lang.Throwable, java.lang.ClassNotFoundException, and |
Ian Rogers | 23435d0 | 2012-09-24 11:23:12 -0700 | [diff] [blame] | 1020 | // java.lang.StackTraceElement as a convenience. |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 1021 | SetClassRoot(ClassRoot::kJavaLangThrowable, FindSystemClass(self, "Ljava/lang/Throwable;")); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 1022 | SetClassRoot(ClassRoot::kJavaLangClassNotFoundException, |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 1023 | FindSystemClass(self, "Ljava/lang/ClassNotFoundException;")); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 1024 | SetClassRoot(ClassRoot::kJavaLangStackTraceElement, |
| 1025 | FindSystemClass(self, "Ljava/lang/StackTraceElement;")); |
| 1026 | SetClassRoot(ClassRoot::kJavaLangStackTraceElementArrayClass, |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 1027 | FindSystemClass(self, "[Ljava/lang/StackTraceElement;")); |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 1028 | SetClassRoot(ClassRoot::kJavaLangClassLoaderArrayClass, |
| 1029 | FindSystemClass(self, "[Ljava/lang/ClassLoader;")); |
Elliott Hughes | d8ddfd5 | 2011-08-15 14:32:53 -0700 | [diff] [blame] | 1030 | |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 1031 | // Create conflict tables that depend on the class linker. |
| 1032 | runtime->FixupConflictTables(); |
| 1033 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1034 | FinishInit(self); |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 1035 | |
Brian Carlstrom | a004aa9 | 2012-02-08 18:05:09 -0800 | [diff] [blame] | 1036 | VLOG(startup) << "ClassLinker::InitFromCompiler exiting"; |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 1037 | |
| 1038 | return true; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1039 | } |
| 1040 | |
Andreas Gampe | 9abc31e | 2018-05-17 11:47:09 -0700 | [diff] [blame] | 1041 | static void CreateStringInitBindings(Thread* self, ClassLinker* class_linker) |
| 1042 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1043 | // Find String.<init> -> StringFactory bindings. |
| 1044 | ObjPtr<mirror::Class> string_factory_class = |
| 1045 | class_linker->FindSystemClass(self, "Ljava/lang/StringFactory;"); |
| 1046 | CHECK(string_factory_class != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 1047 | ObjPtr<mirror::Class> string_class = GetClassRoot<mirror::String>(class_linker); |
Andreas Gampe | 9abc31e | 2018-05-17 11:47:09 -0700 | [diff] [blame] | 1048 | WellKnownClasses::InitStringInit(string_class, string_factory_class); |
| 1049 | // Update the primordial thread. |
| 1050 | self->InitStringEntryPoints(); |
| 1051 | } |
| 1052 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1053 | void ClassLinker::FinishInit(Thread* self) { |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1054 | VLOG(startup) << "ClassLinker::FinishInit entering"; |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 1055 | |
Andreas Gampe | 9abc31e | 2018-05-17 11:47:09 -0700 | [diff] [blame] | 1056 | CreateStringInitBindings(self, this); |
| 1057 | |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 1058 | // Let the heap know some key offsets into java.lang.ref instances |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 1059 | // Note: we hard code the field indexes here rather than using FindInstanceField |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 1060 | // as the types of the field can't be resolved prior to the runtime being |
| 1061 | // fully initialized |
Andreas Gampe | 7b2450e | 2018-06-19 10:45:54 -0700 | [diff] [blame] | 1062 | StackHandleScope<3> hs(self); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 1063 | Handle<mirror::Class> java_lang_ref_Reference = |
| 1064 | hs.NewHandle(GetClassRoot<mirror::Reference>(this)); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1065 | Handle<mirror::Class> java_lang_ref_FinalizerReference = |
| 1066 | hs.NewHandle(FindSystemClass(self, "Ljava/lang/ref/FinalizerReference;")); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 1067 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1068 | ArtField* pendingNext = java_lang_ref_Reference->GetInstanceField(0); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1069 | CHECK_STREQ(pendingNext->GetName(), "pendingNext"); |
| 1070 | CHECK_STREQ(pendingNext->GetTypeDescriptor(), "Ljava/lang/ref/Reference;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 1071 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1072 | ArtField* queue = java_lang_ref_Reference->GetInstanceField(1); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1073 | CHECK_STREQ(queue->GetName(), "queue"); |
| 1074 | CHECK_STREQ(queue->GetTypeDescriptor(), "Ljava/lang/ref/ReferenceQueue;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 1075 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1076 | ArtField* queueNext = java_lang_ref_Reference->GetInstanceField(2); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1077 | CHECK_STREQ(queueNext->GetName(), "queueNext"); |
| 1078 | CHECK_STREQ(queueNext->GetTypeDescriptor(), "Ljava/lang/ref/Reference;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 1079 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1080 | ArtField* referent = java_lang_ref_Reference->GetInstanceField(3); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1081 | CHECK_STREQ(referent->GetName(), "referent"); |
| 1082 | CHECK_STREQ(referent->GetTypeDescriptor(), "Ljava/lang/Object;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 1083 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 1084 | ArtField* zombie = java_lang_ref_FinalizerReference->GetInstanceField(2); |
Mathieu Chartier | 61c5ebc | 2014-06-05 17:42:53 -0700 | [diff] [blame] | 1085 | CHECK_STREQ(zombie->GetName(), "zombie"); |
| 1086 | CHECK_STREQ(zombie->GetTypeDescriptor(), "Ljava/lang/Object;"); |
Brian Carlstrom | 1619286 | 2011-09-12 17:50:06 -0700 | [diff] [blame] | 1087 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1088 | // ensure all class_roots_ are initialized |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 1089 | 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] | 1090 | ClassRoot class_root = static_cast<ClassRoot>(i); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1091 | ObjPtr<mirror::Class> klass = GetClassRoot(class_root); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 1092 | CHECK(klass != nullptr); |
| 1093 | DCHECK(klass->IsArrayClass() || klass->IsPrimitive() || klass->GetDexCache() != nullptr); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1094 | // note SetClassRoot does additional validation. |
| 1095 | // if possible add new checks there to catch errors early |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 1096 | } |
| 1097 | |
Vladimir Marko | 0261055 | 2018-06-04 14:38:00 +0100 | [diff] [blame] | 1098 | CHECK(GetArrayIfTable() != nullptr); |
Elliott Hughes | 92f14b2 | 2011-10-06 12:29:54 -0700 | [diff] [blame] | 1099 | |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 1100 | // disable the slow paths in FindClass and CreatePrimitiveClass now |
| 1101 | // that Object, Class, and Object[] are setup |
| 1102 | init_done_ = true; |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 1103 | |
Andreas Gampe | 7b2450e | 2018-06-19 10:45:54 -0700 | [diff] [blame] | 1104 | // Under sanitization, the small carve-out to handle stack overflow might not be enough to |
| 1105 | // initialize the StackOverflowError class (as it might require running the verifier). Instead, |
| 1106 | // ensure that the class will be initialized. |
| 1107 | if (kMemoryToolIsAvailable && !Runtime::Current()->IsAotCompiler()) { |
Andreas Gampe | e0bbab9 | 2019-07-25 12:28:22 -0700 | [diff] [blame] | 1108 | verifier::ClassVerifier::Init(this); // Need to prepare the verifier. |
Andreas Gampe | 7b2450e | 2018-06-19 10:45:54 -0700 | [diff] [blame] | 1109 | |
| 1110 | ObjPtr<mirror::Class> soe_klass = FindSystemClass(self, "Ljava/lang/StackOverflowError;"); |
| 1111 | if (soe_klass == nullptr || !EnsureInitialized(self, hs.NewHandle(soe_klass), true, true)) { |
| 1112 | // Strange, but don't crash. |
| 1113 | LOG(WARNING) << "Could not prepare StackOverflowError."; |
| 1114 | self->ClearException(); |
| 1115 | } |
| 1116 | } |
| 1117 | |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 1118 | VLOG(startup) << "ClassLinker::FinishInit exiting"; |
Brian Carlstrom | 75cb3b4 | 2011-07-28 02:13:36 -0700 | [diff] [blame] | 1119 | } |
| 1120 | |
Vladimir Marko | dcfcce4 | 2018-06-27 10:00:28 +0000 | [diff] [blame] | 1121 | void ClassLinker::RunRootClinits(Thread* self) { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 1122 | for (size_t i = 0; i < static_cast<size_t>(ClassRoot::kMax); ++i) { |
| 1123 | ObjPtr<mirror::Class> c = GetClassRoot(ClassRoot(i), this); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 1124 | if (!c->IsArrayClass() && !c->IsPrimitive()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 1125 | StackHandleScope<1> hs(self); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 1126 | Handle<mirror::Class> h_class(hs.NewHandle(c)); |
David Srbecky | 08110ef | 2020-05-20 19:33:43 +0100 | [diff] [blame] | 1127 | if (!EnsureInitialized(self, h_class, true, true)) { |
| 1128 | LOG(FATAL) << "Exception when initializing " << h_class->PrettyClass() |
| 1129 | << ": " << self->GetException()->Dump(); |
| 1130 | } |
Vladimir Marko | dcfcce4 | 2018-06-27 10:00:28 +0000 | [diff] [blame] | 1131 | } else { |
| 1132 | DCHECK(c->IsInitialized()); |
Elliott Hughes | 2a20cfd | 2011-09-23 19:30:41 -0700 | [diff] [blame] | 1133 | } |
| 1134 | } |
| 1135 | } |
| 1136 | |
Vladimir Marko | 4335474 | 2021-02-03 15:37:01 +0000 | [diff] [blame] | 1137 | static void InitializeObjectVirtualMethodHashes(ObjPtr<mirror::Class> java_lang_Object, |
| 1138 | PointerSize pointer_size, |
| 1139 | /*out*/ ArrayRef<uint32_t> virtual_method_hashes) |
| 1140 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1141 | ArraySlice<ArtMethod> virtual_methods = java_lang_Object->GetVirtualMethods(pointer_size); |
| 1142 | DCHECK_EQ(virtual_method_hashes.size(), virtual_methods.size()); |
| 1143 | for (size_t i = 0; i != virtual_method_hashes.size(); ++i) { |
| 1144 | const char* name = virtual_methods[i].GetName(); |
| 1145 | virtual_method_hashes[i] = ComputeModifiedUtf8Hash(name); |
| 1146 | } |
| 1147 | } |
| 1148 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1149 | struct TrampolineCheckData { |
| 1150 | const void* quick_resolution_trampoline; |
| 1151 | const void* quick_imt_conflict_trampoline; |
| 1152 | const void* quick_generic_jni_trampoline; |
| 1153 | const void* quick_to_interpreter_bridge_trampoline; |
Nicolas Geoffray | c39af94 | 2021-01-25 08:43:57 +0000 | [diff] [blame] | 1154 | const void* nterp_trampoline; |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1155 | PointerSize pointer_size; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1156 | ArtMethod* m; |
| 1157 | bool error; |
| 1158 | }; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1159 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1160 | bool ClassLinker::InitFromBootImage(std::string* error_msg) { |
| 1161 | VLOG(startup) << __FUNCTION__ << " entering"; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 1162 | CHECK(!init_done_); |
| 1163 | |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 1164 | Runtime* const runtime = Runtime::Current(); |
| 1165 | Thread* const self = Thread::Current(); |
| 1166 | gc::Heap* const heap = runtime->GetHeap(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1167 | std::vector<gc::space::ImageSpace*> spaces = heap->GetBootImageSpaces(); |
| 1168 | CHECK(!spaces.empty()); |
Vladimir Marko | 024d69f | 2019-06-13 10:52:32 +0100 | [diff] [blame] | 1169 | const ImageHeader& image_header = spaces[0]->GetImageHeader(); |
| 1170 | uint32_t pointer_size_unchecked = image_header.GetPointerSizeUnchecked(); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1171 | if (!ValidPointerSize(pointer_size_unchecked)) { |
| 1172 | *error_msg = StringPrintf("Invalid image pointer size: %u", pointer_size_unchecked); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1173 | return false; |
| 1174 | } |
Vladimir Marko | 3364d18 | 2019-03-13 13:55:01 +0000 | [diff] [blame] | 1175 | image_pointer_size_ = image_header.GetPointerSize(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1176 | if (!runtime->IsAotCompiler()) { |
| 1177 | // Only the Aot compiler supports having an image with a different pointer size than the |
| 1178 | // runtime. This happens on the host for compiling 32 bit tests since we use a 64 bit libart |
| 1179 | // 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] | 1180 | if (image_pointer_size_ != kRuntimePointerSize) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1181 | *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] | 1182 | static_cast<size_t>(image_pointer_size_), |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1183 | sizeof(void*)); |
| 1184 | return false; |
| 1185 | } |
| 1186 | } |
Vladimir Marko | 3364d18 | 2019-03-13 13:55:01 +0000 | [diff] [blame] | 1187 | DCHECK(!runtime->HasResolutionMethod()); |
| 1188 | runtime->SetResolutionMethod(image_header.GetImageMethod(ImageHeader::kResolutionMethod)); |
| 1189 | runtime->SetImtConflictMethod(image_header.GetImageMethod(ImageHeader::kImtConflictMethod)); |
| 1190 | runtime->SetImtUnimplementedMethod( |
| 1191 | image_header.GetImageMethod(ImageHeader::kImtUnimplementedMethod)); |
| 1192 | runtime->SetCalleeSaveMethod( |
| 1193 | image_header.GetImageMethod(ImageHeader::kSaveAllCalleeSavesMethod), |
| 1194 | CalleeSaveType::kSaveAllCalleeSaves); |
| 1195 | runtime->SetCalleeSaveMethod( |
| 1196 | image_header.GetImageMethod(ImageHeader::kSaveRefsOnlyMethod), |
| 1197 | CalleeSaveType::kSaveRefsOnly); |
| 1198 | runtime->SetCalleeSaveMethod( |
| 1199 | image_header.GetImageMethod(ImageHeader::kSaveRefsAndArgsMethod), |
| 1200 | CalleeSaveType::kSaveRefsAndArgs); |
| 1201 | runtime->SetCalleeSaveMethod( |
| 1202 | image_header.GetImageMethod(ImageHeader::kSaveEverythingMethod), |
| 1203 | CalleeSaveType::kSaveEverything); |
| 1204 | runtime->SetCalleeSaveMethod( |
| 1205 | image_header.GetImageMethod(ImageHeader::kSaveEverythingMethodForClinit), |
| 1206 | CalleeSaveType::kSaveEverythingForClinit); |
| 1207 | runtime->SetCalleeSaveMethod( |
| 1208 | image_header.GetImageMethod(ImageHeader::kSaveEverythingMethodForSuspendCheck), |
| 1209 | CalleeSaveType::kSaveEverythingForSuspendCheck); |
| 1210 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1211 | std::vector<const OatFile*> oat_files = |
| 1212 | runtime->GetOatFileManager().RegisterImageOatFiles(spaces); |
| 1213 | DCHECK(!oat_files.empty()); |
| 1214 | const OatHeader& default_oat_header = oat_files[0]->GetOatHeader(); |
Vladimir Marko | 7dac864 | 2019-11-06 17:09:30 +0000 | [diff] [blame] | 1215 | jni_dlsym_lookup_trampoline_ = default_oat_header.GetJniDlsymLookupTrampoline(); |
Vladimir Marko | fa458ac | 2020-02-12 14:08:07 +0000 | [diff] [blame] | 1216 | jni_dlsym_lookup_critical_trampoline_ = default_oat_header.GetJniDlsymLookupCriticalTrampoline(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1217 | quick_resolution_trampoline_ = default_oat_header.GetQuickResolutionTrampoline(); |
| 1218 | quick_imt_conflict_trampoline_ = default_oat_header.GetQuickImtConflictTrampoline(); |
| 1219 | quick_generic_jni_trampoline_ = default_oat_header.GetQuickGenericJniTrampoline(); |
| 1220 | quick_to_interpreter_bridge_trampoline_ = default_oat_header.GetQuickToInterpreterBridge(); |
Nicolas Geoffray | c39af94 | 2021-01-25 08:43:57 +0000 | [diff] [blame] | 1221 | nterp_trampoline_ = default_oat_header.GetNterpTrampoline(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1222 | if (kIsDebugBuild) { |
| 1223 | // Check that the other images use the same trampoline. |
| 1224 | for (size_t i = 1; i < oat_files.size(); ++i) { |
| 1225 | const OatHeader& ith_oat_header = oat_files[i]->GetOatHeader(); |
Vladimir Marko | 7dac864 | 2019-11-06 17:09:30 +0000 | [diff] [blame] | 1226 | const void* ith_jni_dlsym_lookup_trampoline_ = |
| 1227 | ith_oat_header.GetJniDlsymLookupTrampoline(); |
Vladimir Marko | fa458ac | 2020-02-12 14:08:07 +0000 | [diff] [blame] | 1228 | const void* ith_jni_dlsym_lookup_critical_trampoline_ = |
| 1229 | ith_oat_header.GetJniDlsymLookupCriticalTrampoline(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1230 | const void* ith_quick_resolution_trampoline = |
| 1231 | ith_oat_header.GetQuickResolutionTrampoline(); |
| 1232 | const void* ith_quick_imt_conflict_trampoline = |
| 1233 | ith_oat_header.GetQuickImtConflictTrampoline(); |
| 1234 | const void* ith_quick_generic_jni_trampoline = |
| 1235 | ith_oat_header.GetQuickGenericJniTrampoline(); |
| 1236 | const void* ith_quick_to_interpreter_bridge_trampoline = |
| 1237 | ith_oat_header.GetQuickToInterpreterBridge(); |
Nicolas Geoffray | c39af94 | 2021-01-25 08:43:57 +0000 | [diff] [blame] | 1238 | const void* ith_nterp_trampoline = |
| 1239 | ith_oat_header.GetNterpTrampoline(); |
Vladimir Marko | 7dac864 | 2019-11-06 17:09:30 +0000 | [diff] [blame] | 1240 | if (ith_jni_dlsym_lookup_trampoline_ != jni_dlsym_lookup_trampoline_ || |
Vladimir Marko | fa458ac | 2020-02-12 14:08:07 +0000 | [diff] [blame] | 1241 | ith_jni_dlsym_lookup_critical_trampoline_ != jni_dlsym_lookup_critical_trampoline_ || |
Vladimir Marko | 7dac864 | 2019-11-06 17:09:30 +0000 | [diff] [blame] | 1242 | ith_quick_resolution_trampoline != quick_resolution_trampoline_ || |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1243 | ith_quick_imt_conflict_trampoline != quick_imt_conflict_trampoline_ || |
| 1244 | ith_quick_generic_jni_trampoline != quick_generic_jni_trampoline_ || |
Nicolas Geoffray | c39af94 | 2021-01-25 08:43:57 +0000 | [diff] [blame] | 1245 | ith_quick_to_interpreter_bridge_trampoline != quick_to_interpreter_bridge_trampoline_ || |
| 1246 | ith_nterp_trampoline != nterp_trampoline_) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1247 | // Make sure that all methods in this image do not contain those trampolines as |
| 1248 | // entrypoints. Otherwise the class-linker won't be able to work with a single set. |
| 1249 | TrampolineCheckData data; |
| 1250 | data.error = false; |
| 1251 | data.pointer_size = GetImagePointerSize(); |
| 1252 | data.quick_resolution_trampoline = ith_quick_resolution_trampoline; |
| 1253 | data.quick_imt_conflict_trampoline = ith_quick_imt_conflict_trampoline; |
| 1254 | data.quick_generic_jni_trampoline = ith_quick_generic_jni_trampoline; |
| 1255 | data.quick_to_interpreter_bridge_trampoline = ith_quick_to_interpreter_bridge_trampoline; |
Nicolas Geoffray | c39af94 | 2021-01-25 08:43:57 +0000 | [diff] [blame] | 1256 | data.nterp_trampoline = ith_nterp_trampoline; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1257 | ReaderMutexLock mu(self, *Locks::heap_bitmap_lock_); |
Andreas Gampe | 0c18338 | 2017-07-13 22:26:24 -0700 | [diff] [blame] | 1258 | auto visitor = [&](mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1259 | if (obj->IsClass()) { |
| 1260 | ObjPtr<mirror::Class> klass = obj->AsClass(); |
| 1261 | for (ArtMethod& m : klass->GetMethods(data.pointer_size)) { |
| 1262 | const void* entrypoint = |
| 1263 | m.GetEntryPointFromQuickCompiledCodePtrSize(data.pointer_size); |
| 1264 | if (entrypoint == data.quick_resolution_trampoline || |
| 1265 | entrypoint == data.quick_imt_conflict_trampoline || |
| 1266 | entrypoint == data.quick_generic_jni_trampoline || |
| 1267 | entrypoint == data.quick_to_interpreter_bridge_trampoline) { |
| 1268 | data.m = &m; |
| 1269 | data.error = true; |
| 1270 | return; |
| 1271 | } |
| 1272 | } |
| 1273 | } |
| 1274 | }; |
| 1275 | spaces[i]->GetLiveBitmap()->Walk(visitor); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1276 | if (data.error) { |
| 1277 | ArtMethod* m = data.m; |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1278 | LOG(ERROR) << "Found a broken ArtMethod: " << ArtMethod::PrettyMethod(m); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1279 | *error_msg = "Found an ArtMethod with a bad entrypoint"; |
| 1280 | return false; |
| 1281 | } |
| 1282 | } |
| 1283 | } |
| 1284 | } |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 1285 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1286 | class_roots_ = GcRoot<mirror::ObjectArray<mirror::Class>>( |
Vladimir Marko | d7e9bbf | 2019-03-28 13:18:57 +0000 | [diff] [blame] | 1287 | ObjPtr<mirror::ObjectArray<mirror::Class>>::DownCast( |
Vladimir Marko | 024d69f | 2019-06-13 10:52:32 +0100 | [diff] [blame] | 1288 | image_header.GetImageRoot(ImageHeader::kClassRoots))); |
Vladimir Marko | f75613c | 2018-06-05 12:51:04 +0100 | [diff] [blame] | 1289 | DCHECK_EQ(GetClassRoot<mirror::Class>(this)->GetClassFlags(), mirror::kClassFlagClass); |
Mathieu Chartier | 02b6a78 | 2012-10-26 13:51:26 -0700 | [diff] [blame] | 1290 | |
Vladimir Marko | 024d69f | 2019-06-13 10:52:32 +0100 | [diff] [blame] | 1291 | DCHECK_EQ(GetClassRoot<mirror::Object>(this)->GetObjectSize(), sizeof(mirror::Object)); |
| 1292 | ObjPtr<mirror::ObjectArray<mirror::Object>> boot_image_live_objects = |
| 1293 | ObjPtr<mirror::ObjectArray<mirror::Object>>::DownCast( |
| 1294 | image_header.GetImageRoot(ImageHeader::kBootImageLiveObjects)); |
| 1295 | runtime->SetSentinel(boot_image_live_objects->Get(ImageHeader::kClearedJniWeakSentinel)); |
| 1296 | DCHECK(runtime->GetSentinel().Read()->GetClass() == GetClassRoot<mirror::Object>(this)); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 1297 | |
Vladimir Marko | d190851 | 2018-11-22 14:57:28 +0000 | [diff] [blame] | 1298 | for (size_t i = 0u, size = spaces.size(); i != size; ++i) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1299 | // Boot class loader, use a null handle. |
| 1300 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
Vladimir Marko | d190851 | 2018-11-22 14:57:28 +0000 | [diff] [blame] | 1301 | if (!AddImageSpace(spaces[i], |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1302 | ScopedNullHandle<mirror::ClassLoader>(), |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1303 | /*out*/&dex_files, |
| 1304 | error_msg)) { |
| 1305 | return false; |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1306 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1307 | // Append opened dex files at the end. |
| 1308 | boot_dex_files_.insert(boot_dex_files_.end(), |
| 1309 | std::make_move_iterator(dex_files.begin()), |
| 1310 | std::make_move_iterator(dex_files.end())); |
Mathieu Chartier | 208a5cb | 2015-12-02 15:44:07 -0800 | [diff] [blame] | 1311 | } |
Mathieu Chartier | be8303d | 2017-08-17 17:39:39 -0700 | [diff] [blame] | 1312 | for (const std::unique_ptr<const DexFile>& dex_file : boot_dex_files_) { |
| 1313 | OatDexFile::MadviseDexFile(*dex_file, MadviseState::kMadviseStateAtLoad); |
| 1314 | } |
Vladimir Marko | 4335474 | 2021-02-03 15:37:01 +0000 | [diff] [blame] | 1315 | InitializeObjectVirtualMethodHashes(GetClassRoot<mirror::Object>(this), |
| 1316 | image_pointer_size_, |
| 1317 | ArrayRef<uint32_t>(object_virtual_method_hashes_)); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 1318 | FinishInit(self); |
Brian Carlstrom | 0a5b14d | 2011-09-27 13:29:15 -0700 | [diff] [blame] | 1319 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1320 | VLOG(startup) << __FUNCTION__ << " exiting"; |
| 1321 | return true; |
| 1322 | } |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 1323 | |
Vladimir Marko | 4433c43 | 2018-12-04 14:57:47 +0000 | [diff] [blame] | 1324 | void ClassLinker::AddExtraBootDexFiles( |
| 1325 | Thread* self, |
| 1326 | std::vector<std::unique_ptr<const DexFile>>&& additional_dex_files) { |
| 1327 | for (std::unique_ptr<const DexFile>& dex_file : additional_dex_files) { |
Mathieu Chartier | 0a19e21 | 2019-11-27 14:35:24 -0800 | [diff] [blame] | 1328 | AppendToBootClassPath(self, dex_file.get()); |
Orion Hodson | 771708f | 2021-01-06 15:45:16 +0000 | [diff] [blame] | 1329 | if (kIsDebugBuild) { |
| 1330 | for (const auto& boot_dex_file : boot_dex_files_) { |
| 1331 | DCHECK_NE(boot_dex_file->GetLocation(), dex_file->GetLocation()); |
| 1332 | } |
| 1333 | } |
Vladimir Marko | 4433c43 | 2018-12-04 14:57:47 +0000 | [diff] [blame] | 1334 | boot_dex_files_.push_back(std::move(dex_file)); |
| 1335 | } |
| 1336 | } |
| 1337 | |
Jeff Hao | 5872d7c | 2016-04-27 11:07:41 -0700 | [diff] [blame] | 1338 | bool ClassLinker::IsBootClassLoader(ScopedObjectAccessAlreadyRunnable& soa, |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1339 | ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1340 | return class_loader == nullptr || |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 1341 | soa.Decode<mirror::Class>(WellKnownClasses::java_lang_BootClassLoader) == |
| 1342 | class_loader->GetClass(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1343 | } |
| 1344 | |
Alexey Grebenkin | be4c2bd | 2018-02-01 19:09:59 +0300 | [diff] [blame] | 1345 | class CHAOnDeleteUpdateClassVisitor { |
| 1346 | public: |
| 1347 | explicit CHAOnDeleteUpdateClassVisitor(LinearAlloc* alloc) |
| 1348 | : allocator_(alloc), cha_(Runtime::Current()->GetClassLinker()->GetClassHierarchyAnalysis()), |
| 1349 | pointer_size_(Runtime::Current()->GetClassLinker()->GetImagePointerSize()), |
| 1350 | self_(Thread::Current()) {} |
| 1351 | |
| 1352 | bool operator()(ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1353 | // This class is going to be unloaded. Tell CHA about it. |
| 1354 | cha_->ResetSingleImplementationInHierarchy(klass, allocator_, pointer_size_); |
| 1355 | return true; |
| 1356 | } |
| 1357 | private: |
| 1358 | const LinearAlloc* allocator_; |
| 1359 | const ClassHierarchyAnalysis* cha_; |
| 1360 | const PointerSize pointer_size_; |
| 1361 | const Thread* self_; |
| 1362 | }; |
| 1363 | |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1364 | /* |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1365 | * A class used to ensure that all references to strings interned in an AppImage have been |
| 1366 | * 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] | 1367 | */ |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1368 | class CountInternedStringReferencesVisitor { |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1369 | public: |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1370 | CountInternedStringReferencesVisitor(const gc::space::ImageSpace& space, |
| 1371 | const InternTable::UnorderedSet& image_interns) |
| 1372 | : space_(space), |
| 1373 | image_interns_(image_interns), |
| 1374 | count_(0u) {} |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1375 | |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1376 | void TestObject(ObjPtr<mirror::Object> referred_obj) const |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1377 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1378 | if (referred_obj != nullptr && |
| 1379 | space_.HasAddress(referred_obj.Ptr()) && |
| 1380 | referred_obj->IsString()) { |
| 1381 | ObjPtr<mirror::String> referred_str = referred_obj->AsString(); |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1382 | auto it = image_interns_.find(GcRoot<mirror::String>(referred_str)); |
| 1383 | if (it != image_interns_.end() && it->Read() == referred_str) { |
| 1384 | ++count_; |
Chris Wailes | fbeef46 | 2018-10-19 14:16:35 -0700 | [diff] [blame] | 1385 | } |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1386 | } |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1387 | } |
| 1388 | |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1389 | void VisitRootIfNonNull( |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1390 | mirror::CompressedReference<mirror::Object>* root) const |
| 1391 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1392 | if (!root->IsNull()) { |
| 1393 | VisitRoot(root); |
| 1394 | } |
| 1395 | } |
| 1396 | |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1397 | void VisitRoot(mirror::CompressedReference<mirror::Object>* root) const |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1398 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1399 | TestObject(root->AsMirrorPtr()); |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1400 | } |
| 1401 | |
| 1402 | // Visit Class Fields |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1403 | void operator()(ObjPtr<mirror::Object> obj, |
| 1404 | MemberOffset offset, |
| 1405 | bool is_static ATTRIBUTE_UNUSED) const |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1406 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1407 | // References within image or across images don't need a read barrier. |
| 1408 | ObjPtr<mirror::Object> referred_obj = |
| 1409 | obj->GetFieldObject<mirror::Object, kVerifyNone, kWithoutReadBarrier>(offset); |
| 1410 | TestObject(referred_obj); |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1411 | } |
| 1412 | |
| 1413 | void operator()(ObjPtr<mirror::Class> klass ATTRIBUTE_UNUSED, |
| 1414 | ObjPtr<mirror::Reference> ref) const |
| 1415 | REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(Locks::heap_bitmap_lock_) { |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1416 | operator()(ref, mirror::Reference::ReferentOffset(), /*is_static=*/ false); |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1417 | } |
| 1418 | |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1419 | size_t GetCount() const { |
| 1420 | return count_; |
| 1421 | } |
| 1422 | |
| 1423 | private: |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1424 | const gc::space::ImageSpace& space_; |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1425 | const InternTable::UnorderedSet& image_interns_; |
| 1426 | mutable size_t count_; // Modified from the `const` callbacks. |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1427 | }; |
| 1428 | |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1429 | /* |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1430 | * This function counts references to strings interned in the AppImage. |
| 1431 | * 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] | 1432 | */ |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1433 | size_t CountInternedStringReferences(gc::space::ImageSpace& space, |
| 1434 | const InternTable::UnorderedSet& image_interns) |
| 1435 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1436 | const gc::accounting::ContinuousSpaceBitmap* bitmap = space.GetMarkBitmap(); |
| 1437 | const ImageHeader& image_header = space.GetImageHeader(); |
| 1438 | const uint8_t* target_base = space.GetMemMap()->Begin(); |
| 1439 | const ImageSection& objects_section = image_header.GetObjectsSection(); |
Chris Wailes | fbeef46 | 2018-10-19 14:16:35 -0700 | [diff] [blame] | 1440 | |
| 1441 | auto objects_begin = reinterpret_cast<uintptr_t>(target_base + objects_section.Offset()); |
| 1442 | auto objects_end = reinterpret_cast<uintptr_t>(target_base + objects_section.End()); |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1443 | |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1444 | CountInternedStringReferencesVisitor visitor(space, image_interns); |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1445 | bitmap->VisitMarkedRange(objects_begin, |
| 1446 | objects_end, |
| 1447 | [&space, &visitor](mirror::Object* obj) |
| 1448 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1449 | if (space.HasAddress(obj)) { |
| 1450 | if (obj->IsDexCache()) { |
Chris Wailes | fbeef46 | 2018-10-19 14:16:35 -0700 | [diff] [blame] | 1451 | obj->VisitReferences</* kVisitNativeRoots= */ true, |
| 1452 | kVerifyNone, |
| 1453 | kWithoutReadBarrier>(visitor, visitor); |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1454 | } else { |
| 1455 | // Don't visit native roots for non-dex-cache as they can't contain |
| 1456 | // native references to strings. This is verified during compilation |
| 1457 | // by ImageWriter::VerifyNativeGCRootInvariants. |
Chris Wailes | fbeef46 | 2018-10-19 14:16:35 -0700 | [diff] [blame] | 1458 | obj->VisitReferences</* kVisitNativeRoots= */ false, |
| 1459 | kVerifyNone, |
| 1460 | kWithoutReadBarrier>(visitor, visitor); |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1461 | } |
| 1462 | } |
| 1463 | }); |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1464 | return visitor.GetCount(); |
| 1465 | } |
| 1466 | |
| 1467 | template <typename Visitor> |
| 1468 | static void VisitInternedStringReferences( |
| 1469 | gc::space::ImageSpace* space, |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1470 | const Visitor& visitor) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1471 | const uint8_t* target_base = space->Begin(); |
| 1472 | const ImageSection& sro_section = |
| 1473 | space->GetImageHeader().GetImageStringReferenceOffsetsSection(); |
| 1474 | const size_t num_string_offsets = sro_section.Size() / sizeof(AppImageReferenceOffsetInfo); |
| 1475 | |
| 1476 | VLOG(image) |
| 1477 | << "ClassLinker:AppImage:InternStrings:imageStringReferenceOffsetCount = " |
| 1478 | << num_string_offsets; |
| 1479 | |
| 1480 | const auto* sro_base = |
| 1481 | reinterpret_cast<const AppImageReferenceOffsetInfo*>(target_base + sro_section.Offset()); |
| 1482 | |
| 1483 | for (size_t offset_index = 0; offset_index < num_string_offsets; ++offset_index) { |
| 1484 | uint32_t base_offset = sro_base[offset_index].first; |
| 1485 | |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 1486 | uint32_t raw_member_offset = sro_base[offset_index].second; |
| 1487 | DCHECK_ALIGNED(base_offset, 2); |
| 1488 | DCHECK_ALIGNED(raw_member_offset, 2); |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1489 | |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 1490 | ObjPtr<mirror::Object> obj_ptr = |
| 1491 | reinterpret_cast<mirror::Object*>(space->Begin() + base_offset); |
| 1492 | MemberOffset member_offset(raw_member_offset); |
| 1493 | ObjPtr<mirror::String> referred_string = |
| 1494 | obj_ptr->GetFieldObject<mirror::String, |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1495 | kVerifyNone, |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 1496 | kWithoutReadBarrier, |
| 1497 | /* kIsVolatile= */ false>(member_offset); |
| 1498 | DCHECK(referred_string != nullptr); |
| 1499 | |
| 1500 | ObjPtr<mirror::String> visited = visitor(referred_string); |
| 1501 | if (visited != referred_string) { |
| 1502 | obj_ptr->SetFieldObject</* kTransactionActive= */ false, |
| 1503 | /* kCheckTransaction= */ false, |
| 1504 | kVerifyNone, |
| 1505 | /* kIsVolatile= */ false>(member_offset, visited); |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1506 | } |
| 1507 | } |
| 1508 | } |
| 1509 | |
| 1510 | static void VerifyInternedStringReferences(gc::space::ImageSpace* space) |
| 1511 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1512 | InternTable::UnorderedSet image_interns; |
| 1513 | const ImageSection& section = space->GetImageHeader().GetInternedStringsSection(); |
| 1514 | if (section.Size() > 0) { |
| 1515 | size_t read_count; |
| 1516 | const uint8_t* data = space->Begin() + section.Offset(); |
| 1517 | InternTable::UnorderedSet image_set(data, /*make_copy_of_data=*/ false, &read_count); |
| 1518 | image_set.swap(image_interns); |
| 1519 | } |
| 1520 | size_t num_recorded_refs = 0u; |
| 1521 | VisitInternedStringReferences( |
| 1522 | space, |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1523 | [&image_interns, &num_recorded_refs](ObjPtr<mirror::String> str) |
| 1524 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1525 | auto it = image_interns.find(GcRoot<mirror::String>(str)); |
| 1526 | CHECK(it != image_interns.end()); |
| 1527 | CHECK(it->Read() == str); |
| 1528 | ++num_recorded_refs; |
| 1529 | return str; |
| 1530 | }); |
| 1531 | size_t num_found_refs = CountInternedStringReferences(*space, image_interns); |
| 1532 | CHECK_EQ(num_recorded_refs, num_found_refs); |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1533 | } |
| 1534 | |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1535 | // new_class_set is the set of classes that were read from the class table section in the image. |
| 1536 | // If there was no class table section, it is null. |
| 1537 | // 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] | 1538 | class AppImageLoadingHelper { |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1539 | public: |
Vladimir Marko | 0f3c700 | 2017-09-07 14:15:56 +0100 | [diff] [blame] | 1540 | static void Update( |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1541 | ClassLinker* class_linker, |
| 1542 | gc::space::ImageSpace* space, |
| 1543 | Handle<mirror::ClassLoader> class_loader, |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 1544 | Handle<mirror::ObjectArray<mirror::DexCache>> dex_caches) |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1545 | REQUIRES(!Locks::dex_lock_) |
| 1546 | REQUIRES_SHARED(Locks::mutator_lock_); |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 1547 | |
Chris Wailes | fbeef46 | 2018-10-19 14:16:35 -0700 | [diff] [blame] | 1548 | static void HandleAppImageStrings(gc::space::ImageSpace* space) |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 1549 | REQUIRES_SHARED(Locks::mutator_lock_); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1550 | }; |
| 1551 | |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 1552 | void AppImageLoadingHelper::Update( |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1553 | ClassLinker* class_linker, |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1554 | gc::space::ImageSpace* space, |
| 1555 | Handle<mirror::ClassLoader> class_loader, |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 1556 | Handle<mirror::ObjectArray<mirror::DexCache>> dex_caches) |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1557 | REQUIRES(!Locks::dex_lock_) |
| 1558 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Chris Wailes | 2386636 | 2018-08-22 16:16:58 -0700 | [diff] [blame] | 1559 | ScopedTrace app_image_timing("AppImage:Updating"); |
| 1560 | |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1561 | if (kIsDebugBuild && ClassLinker::kAppImageMayContainStrings) { |
| 1562 | // In debug build, verify the string references before applying |
| 1563 | // the Runtime::LoadAppImageStartupCache() option. |
| 1564 | VerifyInternedStringReferences(space); |
| 1565 | } |
| 1566 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1567 | Thread* const self = Thread::Current(); |
Mathieu Chartier | a88abfa | 2019-02-04 11:08:29 -0800 | [diff] [blame] | 1568 | Runtime* const runtime = Runtime::Current(); |
| 1569 | gc::Heap* const heap = runtime->GetHeap(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1570 | const ImageHeader& header = space->GetImageHeader(); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1571 | { |
Vladimir Marko | 0f3c700 | 2017-09-07 14:15:56 +0100 | [diff] [blame] | 1572 | // Register dex caches with the class loader. |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1573 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Alex Light | a9bbc08 | 2019-11-14 14:51:41 -0800 | [diff] [blame] | 1574 | for (auto dex_cache : dex_caches.Iterate<mirror::DexCache>()) { |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1575 | const DexFile* const dex_file = dex_cache->GetDexFile(); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1576 | { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 1577 | WriterMutexLock mu2(self, *Locks::dex_lock_); |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 1578 | CHECK(class_linker->FindDexCacheDataLocked(*dex_file) == nullptr); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 1579 | class_linker->RegisterDexFileLocked(*dex_file, dex_cache, class_loader.Get()); |
Mathieu Chartier | 064e9d4 | 2016-03-07 17:41:39 -0800 | [diff] [blame] | 1580 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1581 | } |
Mathieu Chartier | a0b9521 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 1582 | } |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1583 | |
Mathieu Chartier | 0933cc5 | 2018-03-23 14:25:08 -0700 | [diff] [blame] | 1584 | if (ClassLinker::kAppImageMayContainStrings) { |
Chris Wailes | fbeef46 | 2018-10-19 14:16:35 -0700 | [diff] [blame] | 1585 | HandleAppImageStrings(space); |
Chang Xing | ba17dbd | 2017-06-28 21:27:56 +0000 | [diff] [blame] | 1586 | } |
Chris Wailes | 0c61be4 | 2018-09-26 17:27:34 -0700 | [diff] [blame] | 1587 | |
Mathieu Chartier | a0b9521 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 1588 | if (kVerifyArtMethodDeclaringClasses) { |
Chris Wailes | 2386636 | 2018-08-22 16:16:58 -0700 | [diff] [blame] | 1589 | ScopedTrace timing("AppImage:VerifyDeclaringClasses"); |
Mathieu Chartier | a0b9521 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 1590 | ReaderMutexLock rmu(self, *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 9d5956a | 2019-03-22 11:29:08 -0700 | [diff] [blame] | 1591 | gc::accounting::HeapBitmap* live_bitmap = heap->GetLiveBitmap(); |
| 1592 | header.VisitPackedArtMethods([&](ArtMethod& method) |
| 1593 | REQUIRES_SHARED(Locks::mutator_lock_, Locks::heap_bitmap_lock_) { |
| 1594 | ObjPtr<mirror::Class> klass = method.GetDeclaringClassUnchecked(); |
| 1595 | if (klass != nullptr) { |
| 1596 | CHECK(live_bitmap->Test(klass.Ptr())) << "Image method has unmarked declaring class"; |
| 1597 | } |
| 1598 | }, space->Begin(), kRuntimePointerSize); |
Mathieu Chartier | 03c1dd9 | 2016-03-07 16:13:54 -0800 | [diff] [blame] | 1599 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1600 | } |
| 1601 | |
Chris Wailes | fbeef46 | 2018-10-19 14:16:35 -0700 | [diff] [blame] | 1602 | void AppImageLoadingHelper::HandleAppImageStrings(gc::space::ImageSpace* space) { |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 1603 | // Iterate over the string reference offsets stored in the image and intern |
| 1604 | // the strings they point to. |
| 1605 | ScopedTrace timing("AppImage:InternString"); |
| 1606 | |
Mathieu Chartier | a88abfa | 2019-02-04 11:08:29 -0800 | [diff] [blame] | 1607 | Runtime* const runtime = Runtime::Current(); |
| 1608 | InternTable* const intern_table = runtime->GetInternTable(); |
| 1609 | |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 1610 | // Add the intern table, removing any conflicts. For conflicts, store the new address in a map |
| 1611 | // for faster lookup. |
| 1612 | // TODO: Optimize with a bitmap or bloom filter |
| 1613 | SafeMap<mirror::String*, mirror::String*> intern_remap; |
Mathieu Chartier | a88abfa | 2019-02-04 11:08:29 -0800 | [diff] [blame] | 1614 | auto func = [&](InternTable::UnorderedSet& interns) |
Mathieu Chartier | 41c0808 | 2018-10-31 11:50:26 -0700 | [diff] [blame] | 1615 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 1616 | REQUIRES(Locks::intern_table_lock_) { |
Mathieu Chartier | 8fc7558 | 2018-11-01 14:21:33 -0700 | [diff] [blame] | 1617 | const size_t non_boot_image_strings = intern_table->CountInterns( |
| 1618 | /*visit_boot_images=*/false, |
| 1619 | /*visit_non_boot_images=*/true); |
Chris Wailes | fbeef46 | 2018-10-19 14:16:35 -0700 | [diff] [blame] | 1620 | VLOG(image) << "AppImage:stringsInInternTableSize = " << interns.size(); |
Mathieu Chartier | 8fc7558 | 2018-11-01 14:21:33 -0700 | [diff] [blame] | 1621 | VLOG(image) << "AppImage:nonBootImageInternStrings = " << non_boot_image_strings; |
| 1622 | // Visit the smaller of the two sets to compute the intersection. |
| 1623 | if (interns.size() < non_boot_image_strings) { |
| 1624 | for (auto it = interns.begin(); it != interns.end(); ) { |
| 1625 | ObjPtr<mirror::String> string = it->Read(); |
| 1626 | ObjPtr<mirror::String> existing = intern_table->LookupWeakLocked(string); |
| 1627 | if (existing == nullptr) { |
| 1628 | existing = intern_table->LookupStrongLocked(string); |
| 1629 | } |
| 1630 | if (existing != nullptr) { |
| 1631 | intern_remap.Put(string.Ptr(), existing.Ptr()); |
| 1632 | it = interns.erase(it); |
| 1633 | } else { |
| 1634 | ++it; |
| 1635 | } |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 1636 | } |
Mathieu Chartier | 8fc7558 | 2018-11-01 14:21:33 -0700 | [diff] [blame] | 1637 | } else { |
| 1638 | intern_table->VisitInterns([&](const GcRoot<mirror::String>& root) |
| 1639 | REQUIRES_SHARED(Locks::mutator_lock_) |
| 1640 | REQUIRES(Locks::intern_table_lock_) { |
| 1641 | auto it = interns.find(root); |
| 1642 | if (it != interns.end()) { |
| 1643 | ObjPtr<mirror::String> existing = root.Read(); |
| 1644 | intern_remap.Put(it->Read(), existing.Ptr()); |
| 1645 | it = interns.erase(it); |
| 1646 | } |
| 1647 | }, /*visit_boot_images=*/false, /*visit_non_boot_images=*/true); |
| 1648 | } |
David Srbecky | 346fd96 | 2020-07-27 16:51:00 +0100 | [diff] [blame] | 1649 | // Consistency check to ensure correctness. |
Mathieu Chartier | 8fc7558 | 2018-11-01 14:21:33 -0700 | [diff] [blame] | 1650 | if (kIsDebugBuild) { |
| 1651 | for (GcRoot<mirror::String>& root : interns) { |
| 1652 | ObjPtr<mirror::String> string = root.Read(); |
| 1653 | CHECK(intern_table->LookupWeakLocked(string) == nullptr) << string->ToModifiedUtf8(); |
| 1654 | CHECK(intern_table->LookupStrongLocked(string) == nullptr) << string->ToModifiedUtf8(); |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 1655 | } |
| 1656 | } |
Mathieu Chartier | a88abfa | 2019-02-04 11:08:29 -0800 | [diff] [blame] | 1657 | }; |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1658 | intern_table->AddImageStringsToTable(space, func); |
| 1659 | if (!intern_remap.empty()) { |
Mathieu Chartier | a88abfa | 2019-02-04 11:08:29 -0800 | [diff] [blame] | 1660 | VLOG(image) << "AppImage:conflictingInternStrings = " << intern_remap.size(); |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1661 | VisitInternedStringReferences( |
| 1662 | space, |
Vladimir Marko | 8e05f09 | 2019-06-10 11:10:38 +0100 | [diff] [blame] | 1663 | [&intern_remap](ObjPtr<mirror::String> str) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1664 | auto it = intern_remap.find(str.Ptr()); |
| 1665 | if (it != intern_remap.end()) { |
| 1666 | return ObjPtr<mirror::String>(it->second); |
| 1667 | } |
| 1668 | return str; |
| 1669 | }); |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 1670 | } |
| 1671 | } |
| 1672 | |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1673 | static std::unique_ptr<const DexFile> OpenOatDexFile(const OatFile* oat_file, |
| 1674 | const char* location, |
| 1675 | std::string* error_msg) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1676 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1677 | DCHECK(error_msg != nullptr); |
| 1678 | std::unique_ptr<const DexFile> dex_file; |
Andreas Gampe | b40d361 | 2018-06-26 15:49:42 -0700 | [diff] [blame] | 1679 | const OatDexFile* oat_dex_file = oat_file->GetOatDexFile(location, nullptr, error_msg); |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1680 | if (oat_dex_file == nullptr) { |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1681 | return std::unique_ptr<const DexFile>(); |
| 1682 | } |
| 1683 | std::string inner_error_msg; |
| 1684 | dex_file = oat_dex_file->OpenDexFile(&inner_error_msg); |
| 1685 | if (dex_file == nullptr) { |
| 1686 | *error_msg = StringPrintf("Failed to open dex file %s from within oat file %s error '%s'", |
| 1687 | location, |
| 1688 | oat_file->GetLocation().c_str(), |
| 1689 | inner_error_msg.c_str()); |
| 1690 | return std::unique_ptr<const DexFile>(); |
| 1691 | } |
| 1692 | |
| 1693 | if (dex_file->GetLocationChecksum() != oat_dex_file->GetDexFileLocationChecksum()) { |
| 1694 | *error_msg = StringPrintf("Checksums do not match for %s: %x vs %x", |
| 1695 | location, |
| 1696 | dex_file->GetLocationChecksum(), |
| 1697 | oat_dex_file->GetDexFileLocationChecksum()); |
| 1698 | return std::unique_ptr<const DexFile>(); |
| 1699 | } |
| 1700 | return dex_file; |
| 1701 | } |
| 1702 | |
| 1703 | bool ClassLinker::OpenImageDexFiles(gc::space::ImageSpace* space, |
| 1704 | std::vector<std::unique_ptr<const DexFile>>* out_dex_files, |
| 1705 | std::string* error_msg) { |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 1706 | ScopedAssertNoThreadSuspension nts(__FUNCTION__); |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1707 | const ImageHeader& header = space->GetImageHeader(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1708 | ObjPtr<mirror::Object> dex_caches_object = header.GetImageRoot(ImageHeader::kDexCaches); |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1709 | DCHECK(dex_caches_object != nullptr); |
Vladimir Marko | 4617d58 | 2019-03-28 13:48:31 +0000 | [diff] [blame] | 1710 | ObjPtr<mirror::ObjectArray<mirror::DexCache>> dex_caches = |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1711 | dex_caches_object->AsObjectArray<mirror::DexCache>(); |
| 1712 | const OatFile* oat_file = space->GetOatFile(); |
Alex Light | a9bbc08 | 2019-11-14 14:51:41 -0800 | [diff] [blame] | 1713 | for (auto dex_cache : dex_caches->Iterate()) { |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1714 | std::string dex_file_location(dex_cache->GetLocation()->ToModifiedUtf8()); |
| 1715 | std::unique_ptr<const DexFile> dex_file = OpenOatDexFile(oat_file, |
| 1716 | dex_file_location.c_str(), |
| 1717 | error_msg); |
| 1718 | if (dex_file == nullptr) { |
| 1719 | return false; |
| 1720 | } |
| 1721 | dex_cache->SetDexFile(dex_file.get()); |
| 1722 | out_dex_files->push_back(std::move(dex_file)); |
| 1723 | } |
| 1724 | return true; |
| 1725 | } |
| 1726 | |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1727 | // Helper class for ArtMethod checks when adding an image. Keeps all required functionality |
| 1728 | // together and caches some intermediate results. |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 1729 | class ImageChecker final { |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1730 | public: |
| 1731 | static void CheckObjects(gc::Heap* heap, ClassLinker* class_linker) |
| 1732 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 1733 | ImageChecker ic(heap, class_linker); |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 1734 | auto visitor = [&](mirror::Object* obj) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1735 | DCHECK(obj != nullptr); |
| 1736 | CHECK(obj->GetClass() != nullptr) << "Null class in object " << obj; |
| 1737 | CHECK(obj->GetClass()->GetClass() != nullptr) << "Null class class " << obj; |
| 1738 | if (obj->IsClass()) { |
| 1739 | auto klass = obj->AsClass(); |
| 1740 | for (ArtField& field : klass->GetIFields()) { |
| 1741 | CHECK_EQ(field.GetDeclaringClass(), klass); |
| 1742 | } |
| 1743 | for (ArtField& field : klass->GetSFields()) { |
| 1744 | CHECK_EQ(field.GetDeclaringClass(), klass); |
| 1745 | } |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 1746 | const PointerSize pointer_size = ic.pointer_size_; |
Vladimir Marko | c524e9e | 2019-03-26 10:54:50 +0000 | [diff] [blame] | 1747 | for (ArtMethod& m : klass->GetMethods(pointer_size)) { |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 1748 | ic.CheckArtMethod(&m, klass); |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 1749 | } |
Vladimir Marko | c524e9e | 2019-03-26 10:54:50 +0000 | [diff] [blame] | 1750 | ObjPtr<mirror::PointerArray> vtable = klass->GetVTable(); |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 1751 | if (vtable != nullptr) { |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 1752 | ic.CheckArtMethodPointerArray(vtable, nullptr); |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 1753 | } |
| 1754 | if (klass->ShouldHaveImt()) { |
| 1755 | ImTable* imt = klass->GetImt(pointer_size); |
| 1756 | for (size_t i = 0; i < ImTable::kSize; ++i) { |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 1757 | ic.CheckArtMethod(imt->Get(i, pointer_size), nullptr); |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 1758 | } |
| 1759 | } |
| 1760 | if (klass->ShouldHaveEmbeddedVTable()) { |
| 1761 | for (int32_t i = 0; i < klass->GetEmbeddedVTableLength(); ++i) { |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 1762 | ic.CheckArtMethod(klass->GetEmbeddedVTableEntry(i, pointer_size), nullptr); |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 1763 | } |
| 1764 | } |
Vladimir Marko | c524e9e | 2019-03-26 10:54:50 +0000 | [diff] [blame] | 1765 | ObjPtr<mirror::IfTable> iftable = klass->GetIfTable(); |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 1766 | for (int32_t i = 0; i < klass->GetIfTableCount(); ++i) { |
| 1767 | if (iftable->GetMethodArrayCount(i) > 0) { |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 1768 | ic.CheckArtMethodPointerArray(iftable->GetMethodArray(i), nullptr); |
Andreas Gampe | 1c158a0 | 2017-07-13 17:26:19 -0700 | [diff] [blame] | 1769 | } |
| 1770 | } |
| 1771 | } |
| 1772 | }; |
| 1773 | heap->VisitObjects(visitor); |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1774 | } |
| 1775 | |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1776 | private: |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 1777 | ImageChecker(gc::Heap* heap, ClassLinker* class_linker) |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1778 | : spaces_(heap->GetBootImageSpaces()), |
| 1779 | pointer_size_(class_linker->GetImagePointerSize()) { |
| 1780 | space_begin_.reserve(spaces_.size()); |
| 1781 | method_sections_.reserve(spaces_.size()); |
| 1782 | runtime_method_sections_.reserve(spaces_.size()); |
| 1783 | for (gc::space::ImageSpace* space : spaces_) { |
| 1784 | space_begin_.push_back(space->Begin()); |
| 1785 | auto& header = space->GetImageHeader(); |
| 1786 | method_sections_.push_back(&header.GetMethodsSection()); |
| 1787 | runtime_method_sections_.push_back(&header.GetRuntimeMethodsSection()); |
| 1788 | } |
| 1789 | } |
| 1790 | |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 1791 | void CheckArtMethod(ArtMethod* m, ObjPtr<mirror::Class> expected_class) |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1792 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1793 | if (m->IsRuntimeMethod()) { |
| 1794 | ObjPtr<mirror::Class> declaring_class = m->GetDeclaringClassUnchecked(); |
| 1795 | CHECK(declaring_class == nullptr) << declaring_class << " " << m->PrettyMethod(); |
| 1796 | } else if (m->IsCopied()) { |
| 1797 | CHECK(m->GetDeclaringClass() != nullptr) << m->PrettyMethod(); |
| 1798 | } else if (expected_class != nullptr) { |
| 1799 | CHECK_EQ(m->GetDeclaringClassUnchecked(), expected_class) << m->PrettyMethod(); |
| 1800 | } |
| 1801 | if (!spaces_.empty()) { |
| 1802 | bool contains = false; |
| 1803 | for (size_t i = 0; !contains && i != space_begin_.size(); ++i) { |
| 1804 | const size_t offset = reinterpret_cast<uint8_t*>(m) - space_begin_[i]; |
| 1805 | contains = method_sections_[i]->Contains(offset) || |
| 1806 | runtime_method_sections_[i]->Contains(offset); |
| 1807 | } |
| 1808 | CHECK(contains) << m << " not found"; |
| 1809 | } |
| 1810 | } |
| 1811 | |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 1812 | void CheckArtMethodPointerArray(ObjPtr<mirror::PointerArray> arr, |
| 1813 | ObjPtr<mirror::Class> expected_class) |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1814 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1815 | CHECK(arr != nullptr); |
| 1816 | for (int32_t j = 0; j < arr->GetLength(); ++j) { |
| 1817 | auto* method = arr->GetElementPtrSize<ArtMethod*>(j, pointer_size_); |
| 1818 | // expected_class == null means we are a dex cache. |
| 1819 | if (expected_class != nullptr) { |
| 1820 | CHECK(method != nullptr); |
| 1821 | } |
| 1822 | if (method != nullptr) { |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 1823 | CheckArtMethod(method, expected_class); |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1824 | } |
| 1825 | } |
| 1826 | } |
| 1827 | |
Andreas Gampe | 0793bec | 2016-12-01 11:37:33 -0800 | [diff] [blame] | 1828 | const std::vector<gc::space::ImageSpace*>& spaces_; |
| 1829 | const PointerSize pointer_size_; |
| 1830 | |
| 1831 | // Cached sections from the spaces. |
| 1832 | std::vector<const uint8_t*> space_begin_; |
| 1833 | std::vector<const ImageSection*> method_sections_; |
| 1834 | std::vector<const ImageSection*> runtime_method_sections_; |
| 1835 | }; |
| 1836 | |
Andreas Gampe | be7af22 | 2017-07-25 09:57:28 -0700 | [diff] [blame] | 1837 | static void VerifyAppImage(const ImageHeader& header, |
| 1838 | const Handle<mirror::ClassLoader>& class_loader, |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 1839 | ClassTable* class_table, |
| 1840 | gc::space::ImageSpace* space) |
Andreas Gampe | be7af22 | 2017-07-25 09:57:28 -0700 | [diff] [blame] | 1841 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 9d5956a | 2019-03-22 11:29:08 -0700 | [diff] [blame] | 1842 | header.VisitPackedArtMethods([&](ArtMethod& method) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1843 | ObjPtr<mirror::Class> klass = method.GetDeclaringClass(); |
| 1844 | if (klass != nullptr && !Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(klass)) { |
| 1845 | CHECK_EQ(class_table->LookupByDescriptor(klass), klass) |
| 1846 | << mirror::Class::PrettyClass(klass); |
| 1847 | } |
| 1848 | }, space->Begin(), kRuntimePointerSize); |
Andreas Gampe | be7af22 | 2017-07-25 09:57:28 -0700 | [diff] [blame] | 1849 | { |
| 1850 | // Verify that all direct interfaces of classes in the class table are also resolved. |
| 1851 | std::vector<ObjPtr<mirror::Class>> classes; |
| 1852 | auto verify_direct_interfaces_in_table = [&](ObjPtr<mirror::Class> klass) |
| 1853 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1854 | if (!klass->IsPrimitive() && klass->GetClassLoader() == class_loader.Get()) { |
| 1855 | classes.push_back(klass); |
| 1856 | } |
| 1857 | return true; |
| 1858 | }; |
| 1859 | class_table->Visit(verify_direct_interfaces_in_table); |
| 1860 | Thread* self = Thread::Current(); |
| 1861 | for (ObjPtr<mirror::Class> klass : classes) { |
| 1862 | for (uint32_t i = 0, num = klass->NumDirectInterfaces(); i != num; ++i) { |
| 1863 | CHECK(klass->GetDirectInterface(self, klass, i) != nullptr) |
| 1864 | << klass->PrettyDescriptor() << " iface #" << i; |
| 1865 | } |
| 1866 | } |
| 1867 | } |
Andreas Gampe | be7af22 | 2017-07-25 09:57:28 -0700 | [diff] [blame] | 1868 | } |
| 1869 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1870 | bool ClassLinker::AddImageSpace( |
| 1871 | gc::space::ImageSpace* space, |
| 1872 | Handle<mirror::ClassLoader> class_loader, |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1873 | std::vector<std::unique_ptr<const DexFile>>* out_dex_files, |
| 1874 | std::string* error_msg) { |
| 1875 | DCHECK(out_dex_files != nullptr); |
| 1876 | DCHECK(error_msg != nullptr); |
| 1877 | const uint64_t start_time = NanoTime(); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 1878 | const bool app_image = class_loader != nullptr; |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1879 | const ImageHeader& header = space->GetImageHeader(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 1880 | ObjPtr<mirror::Object> dex_caches_object = header.GetImageRoot(ImageHeader::kDexCaches); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1881 | DCHECK(dex_caches_object != nullptr); |
| 1882 | Runtime* const runtime = Runtime::Current(); |
| 1883 | gc::Heap* const heap = runtime->GetHeap(); |
| 1884 | Thread* const self = Thread::Current(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1885 | // Check that the image is what we are expecting. |
| 1886 | if (image_pointer_size_ != space->GetImageHeader().GetPointerSize()) { |
| 1887 | *error_msg = StringPrintf("Application image pointer size does not match runtime: %zu vs %zu", |
| 1888 | static_cast<size_t>(space->GetImageHeader().GetPointerSize()), |
| 1889 | image_pointer_size_); |
| 1890 | return false; |
| 1891 | } |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1892 | size_t expected_image_roots = ImageHeader::NumberOfImageRoots(app_image); |
| 1893 | if (static_cast<size_t>(header.GetImageRoots()->GetLength()) != expected_image_roots) { |
| 1894 | *error_msg = StringPrintf("Expected %zu image roots but got %d", |
| 1895 | expected_image_roots, |
| 1896 | header.GetImageRoots()->GetLength()); |
| 1897 | return false; |
| 1898 | } |
| 1899 | StackHandleScope<3> hs(self); |
| 1900 | Handle<mirror::ObjectArray<mirror::DexCache>> dex_caches( |
| 1901 | hs.NewHandle(dex_caches_object->AsObjectArray<mirror::DexCache>())); |
| 1902 | Handle<mirror::ObjectArray<mirror::Class>> class_roots(hs.NewHandle( |
| 1903 | header.GetImageRoot(ImageHeader::kClassRoots)->AsObjectArray<mirror::Class>())); |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1904 | MutableHandle<mirror::ClassLoader> image_class_loader(hs.NewHandle( |
Vladimir Marko | f75613c | 2018-06-05 12:51:04 +0100 | [diff] [blame] | 1905 | app_image ? header.GetImageRoot(ImageHeader::kAppImageClassLoader)->AsClassLoader() |
| 1906 | : nullptr)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 1907 | DCHECK(class_roots != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 1908 | if (class_roots->GetLength() != static_cast<int32_t>(ClassRoot::kMax)) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1909 | *error_msg = StringPrintf("Expected %d class roots but got %d", |
| 1910 | class_roots->GetLength(), |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 1911 | static_cast<int32_t>(ClassRoot::kMax)); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1912 | return false; |
| 1913 | } |
| 1914 | // 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] | 1915 | ObjPtr<mirror::ObjectArray<mirror::Class>> existing_class_roots = GetClassRoots(); |
| 1916 | for (size_t i = 0; i < static_cast<size_t>(ClassRoot::kMax); i++) { |
| 1917 | 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] | 1918 | *error_msg = "App image class roots must have pointer equality with runtime ones."; |
| 1919 | return false; |
| 1920 | } |
| 1921 | } |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1922 | const OatFile* oat_file = space->GetOatFile(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1923 | if (oat_file->GetOatHeader().GetDexFileCount() != |
| 1924 | static_cast<uint32_t>(dex_caches->GetLength())) { |
| 1925 | *error_msg = "Dex cache count and dex file count mismatch while trying to initialize from " |
| 1926 | "image"; |
| 1927 | return false; |
| 1928 | } |
| 1929 | |
Alex Light | a9bbc08 | 2019-11-14 14:51:41 -0800 | [diff] [blame] | 1930 | for (auto dex_cache : dex_caches.Iterate<mirror::DexCache>()) { |
David Brazdil | 3e8aae0 | 2019-03-26 18:48:02 +0000 | [diff] [blame] | 1931 | std::string dex_file_location = dex_cache->GetLocation()->ToModifiedUtf8(); |
Mathieu Chartier | bcb6a72 | 2016-03-08 16:49:58 -0800 | [diff] [blame] | 1932 | std::unique_ptr<const DexFile> dex_file = OpenOatDexFile(oat_file, |
| 1933 | dex_file_location.c_str(), |
| 1934 | error_msg); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1935 | if (dex_file == nullptr) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1936 | return false; |
| 1937 | } |
| 1938 | |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 1939 | LinearAlloc* linear_alloc = GetOrCreateAllocatorForClassLoader(class_loader.Get()); |
| 1940 | DCHECK(linear_alloc != nullptr); |
| 1941 | DCHECK_EQ(linear_alloc == Runtime::Current()->GetLinearAlloc(), !app_image); |
| 1942 | { |
| 1943 | // Native fields are all null. Initialize them and allocate native memory. |
| 1944 | WriterMutexLock mu(self, *Locks::dex_lock_); |
| 1945 | dex_cache->InitializeNativeFields(dex_file.get(), linear_alloc); |
| 1946 | } |
| 1947 | if (!app_image) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1948 | // Register dex files, keep track of existing ones that are conflicts. |
Mathieu Chartier | 0a19e21 | 2019-11-27 14:35:24 -0800 | [diff] [blame] | 1949 | AppendToBootClassPath(dex_file.get(), dex_cache); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1950 | } |
| 1951 | out_dex_files->push_back(std::move(dex_file)); |
| 1952 | } |
| 1953 | |
| 1954 | if (app_image) { |
| 1955 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
Nicolas Geoffray | f0d3002 | 2018-11-20 17:45:38 +0000 | [diff] [blame] | 1956 | ScopedAssertNoThreadSuspension sants("Checking app image", soa.Self()); |
Vladimir Marko | eca3eda | 2016-11-09 16:26:44 +0000 | [diff] [blame] | 1957 | if (IsBootClassLoader(soa, image_class_loader.Get())) { |
| 1958 | *error_msg = "Unexpected BootClassLoader in app image"; |
| 1959 | return false; |
| 1960 | } |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1961 | } |
| 1962 | |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 1963 | if (kCheckImageObjects) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1964 | if (!app_image) { |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 1965 | ImageChecker::CheckObjects(heap, this); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1966 | } |
| 1967 | } |
| 1968 | |
| 1969 | // Set entry point to interpreter if in InterpretOnly mode. |
| 1970 | if (!runtime->IsAotCompiler() && runtime->GetInstrumentation()->InterpretOnly()) { |
Mathieu Chartier | 9d5956a | 2019-03-22 11:29:08 -0700 | [diff] [blame] | 1971 | // Set image methods' entry point to interpreter. |
| 1972 | header.VisitPackedArtMethods([&](ArtMethod& method) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1973 | if (!method.IsRuntimeMethod()) { |
| 1974 | DCHECK(method.GetDeclaringClass() != nullptr); |
Ulya Trafimovich | 5439f05 | 2020-07-29 10:03:46 +0100 | [diff] [blame] | 1975 | if (!method.IsNative() && !method.IsResolutionMethod()) { |
| 1976 | method.SetEntryPointFromQuickCompiledCodePtrSize(GetQuickToInterpreterBridge(), |
| 1977 | image_pointer_size_); |
Mathieu Chartier | 9d5956a | 2019-03-22 11:29:08 -0700 | [diff] [blame] | 1978 | } |
| 1979 | } |
| 1980 | }, space->Begin(), image_pointer_size_); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 1981 | } |
| 1982 | |
Nicolas Geoffray | 4717175 | 2020-08-31 15:03:20 +0100 | [diff] [blame] | 1983 | if (!runtime->IsAotCompiler()) { |
Nicolas Geoffray | bd728b0 | 2021-01-27 13:21:35 +0000 | [diff] [blame] | 1984 | ScopedTrace trace("AppImage:UpdateCodeItemAndNterp"); |
Nicolas Geoffray | 4717175 | 2020-08-31 15:03:20 +0100 | [diff] [blame] | 1985 | bool can_use_nterp = interpreter::CanRuntimeUseNterp(); |
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_))); |
Nicolas Geoffray | e1d2dce | 2020-09-21 10:06:31 +0100 | [diff] [blame] | 1992 | method.SetCodeItem(code_item); |
Nicolas Geoffray | 4717175 | 2020-08-31 15:03:20 +0100 | [diff] [blame] | 1993 | } |
| 1994 | // Set image methods' entry point that point to the interpreter bridge to the |
| 1995 | // nterp entry point. |
Nicolas Geoffray | c39af94 | 2021-01-25 08:43:57 +0000 | [diff] [blame] | 1996 | if (method.GetEntryPointFromQuickCompiledCode() == nterp_trampoline_) { |
| 1997 | if (can_use_nterp) { |
Nicolas Geoffray | b1cf837 | 2021-02-02 13:32:20 +0000 | [diff] [blame] | 1998 | DCHECK(!NeedsClinitCheckBeforeCall(&method) || |
| 1999 | method.GetDeclaringClass()->IsVisiblyInitialized()); |
Nicolas Geoffray | c39af94 | 2021-01-25 08:43:57 +0000 | [diff] [blame] | 2000 | method.SetEntryPointFromQuickCompiledCode(interpreter::GetNterpEntryPoint()); |
| 2001 | } else { |
| 2002 | method.SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge()); |
| 2003 | } |
Nicolas Geoffray | 4717175 | 2020-08-31 15:03:20 +0100 | [diff] [blame] | 2004 | } |
Nicolas Geoffray | 7e2c963 | 2020-01-09 13:41:10 +0000 | [diff] [blame] | 2005 | }, space->Begin(), image_pointer_size_); |
| 2006 | } |
| 2007 | |
Nicolas Geoffray | 8c41a0b | 2020-02-06 16:52:11 +0000 | [diff] [blame] | 2008 | if (runtime->IsVerificationSoftFail()) { |
| 2009 | header.VisitPackedArtMethods([&](ArtMethod& method) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 2010 | if (!method.IsNative() && method.IsInvokable()) { |
| 2011 | method.ClearSkipAccessChecks(); |
| 2012 | } |
| 2013 | }, space->Begin(), image_pointer_size_); |
| 2014 | } |
| 2015 | |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 2016 | ClassTable* class_table = nullptr; |
| 2017 | { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2018 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 2019 | class_table = InsertClassTableForClassLoader(class_loader.Get()); |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 2020 | } |
| 2021 | // If we have a class table section, read it and use it for verification in |
| 2022 | // UpdateAppImageClassLoadersAndDexCaches. |
| 2023 | ClassTable::ClassSet temp_set; |
Vladimir Marko | 0f3c700 | 2017-09-07 14:15:56 +0100 | [diff] [blame] | 2024 | const ImageSection& class_table_section = header.GetClassTableSection(); |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 2025 | const bool added_class_table = class_table_section.Size() > 0u; |
| 2026 | if (added_class_table) { |
| 2027 | const uint64_t start_time2 = NanoTime(); |
| 2028 | size_t read_count = 0; |
| 2029 | temp_set = ClassTable::ClassSet(space->Begin() + class_table_section.Offset(), |
| 2030 | /*make copy*/false, |
| 2031 | &read_count); |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 2032 | VLOG(image) << "Adding class table classes took " << PrettyDuration(NanoTime() - start_time2); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2033 | } |
| 2034 | if (app_image) { |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 2035 | AppImageLoadingHelper::Update(this, space, class_loader, dex_caches); |
Mathieu Chartier | 456b492 | 2018-11-06 10:35:48 -0800 | [diff] [blame] | 2036 | |
| 2037 | { |
| 2038 | ScopedTrace trace("AppImage:UpdateClassLoaders"); |
| 2039 | // Update class loader and resolved strings. If added_class_table is false, the resolved |
| 2040 | // strings were forwarded UpdateAppImageClassLoadersAndDexCaches. |
Mathieu Chartier | bc1e0fa | 2018-11-14 16:18:18 -0800 | [diff] [blame] | 2041 | ObjPtr<mirror::ClassLoader> loader(class_loader.Get()); |
Mathieu Chartier | 456b492 | 2018-11-06 10:35:48 -0800 | [diff] [blame] | 2042 | for (const ClassTable::TableSlot& root : temp_set) { |
Mathieu Chartier | bc1e0fa | 2018-11-14 16:18:18 -0800 | [diff] [blame] | 2043 | // Note: We probably don't need the read barrier unless we copy the app image objects into |
| 2044 | // the region space. |
| 2045 | ObjPtr<mirror::Class> klass(root.Read()); |
| 2046 | // Do not update class loader for boot image classes where the app image |
| 2047 | // class loader is only the initiating loader but not the defining loader. |
| 2048 | // Avoid read barrier since we are comparing against null. |
| 2049 | if (klass->GetClassLoader<kDefaultVerifyFlags, kWithoutReadBarrier>() != nullptr) { |
Vladimir Marko | b68bb7a | 2020-03-17 10:55:25 +0000 | [diff] [blame] | 2050 | klass->SetClassLoader(loader); |
Mathieu Chartier | bc1e0fa | 2018-11-14 16:18:18 -0800 | [diff] [blame] | 2051 | } |
Mathieu Chartier | 456b492 | 2018-11-06 10:35:48 -0800 | [diff] [blame] | 2052 | } |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 2053 | } |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 2054 | |
Vladimir Marko | 305c38b | 2018-02-14 11:50:07 +0000 | [diff] [blame] | 2055 | if (kBitstringSubtypeCheckEnabled) { |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 2056 | // Every class in the app image has initially SubtypeCheckInfo in the |
| 2057 | // Uninitialized state. |
| 2058 | // |
| 2059 | // The SubtypeCheck invariants imply that a SubtypeCheckInfo is at least Initialized |
| 2060 | // after class initialization is complete. The app image ClassStatus as-is |
| 2061 | // are almost all ClassStatus::Initialized, and being in the |
| 2062 | // SubtypeCheckInfo::kUninitialized state is violating that invariant. |
| 2063 | // |
| 2064 | // Force every app image class's SubtypeCheck to be at least kIninitialized. |
| 2065 | // |
| 2066 | // See also ImageWriter::FixupClass. |
Chris Wailes | 2386636 | 2018-08-22 16:16:58 -0700 | [diff] [blame] | 2067 | ScopedTrace trace("AppImage:RecacluateSubtypeCheckBitstrings"); |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 2068 | MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_); |
| 2069 | for (const ClassTable::TableSlot& root : temp_set) { |
Vladimir Marko | 38b8b25 | 2018-01-02 19:07:06 +0000 | [diff] [blame] | 2070 | SubtypeCheck<ObjPtr<mirror::Class>>::EnsureInitialized(root.Read()); |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 2071 | } |
| 2072 | } |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 2073 | } |
| 2074 | if (!oat_file->GetBssGcRoots().empty()) { |
| 2075 | // Insert oat file to class table for visiting .bss GC roots. |
| 2076 | class_table->InsertOatFile(oat_file); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2077 | } |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 2078 | |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 2079 | if (added_class_table) { |
| 2080 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 2081 | class_table->AddClassSet(std::move(temp_set)); |
| 2082 | } |
Andreas Gampe | be7af22 | 2017-07-25 09:57:28 -0700 | [diff] [blame] | 2083 | |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 2084 | if (kIsDebugBuild && app_image) { |
| 2085 | // This verification needs to happen after the classes have been added to the class loader. |
| 2086 | // Since it ensures classes are in the class table. |
Chris Wailes | 2386636 | 2018-08-22 16:16:58 -0700 | [diff] [blame] | 2087 | ScopedTrace trace("AppImage:Verify"); |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 2088 | VerifyAppImage(header, class_loader, class_table, space); |
Mathieu Chartier | 6973100 | 2016-03-02 16:08:31 -0800 | [diff] [blame] | 2089 | } |
Andreas Gampe | be7af22 | 2017-07-25 09:57:28 -0700 | [diff] [blame] | 2090 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 2091 | VLOG(class_linker) << "Adding image space took " << PrettyDuration(NanoTime() - start_time); |
Andreas Gampe | 3db9c5d | 2015-11-17 11:52:46 -0800 | [diff] [blame] | 2092 | return true; |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 2093 | } |
| 2094 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2095 | bool ClassLinker::ClassInClassTable(ObjPtr<mirror::Class> klass) { |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 2096 | ClassTable* const class_table = ClassTableForClassLoader(klass->GetClassLoader()); |
| 2097 | return class_table != nullptr && class_table->Contains(klass); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2098 | } |
| 2099 | |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 2100 | void ClassLinker::VisitClassRoots(RootVisitor* visitor, VisitRootFlags flags) { |
Mathieu Chartier | 7778b88 | 2015-10-05 16:41:10 -0700 | [diff] [blame] | 2101 | // Acquire tracing_enabled before locking class linker lock to prevent lock order violation. Since |
| 2102 | // enabling tracing requires the mutator lock, there are no race conditions here. |
| 2103 | const bool tracing_enabled = Trace::IsTracingEnabled(); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2104 | Thread* const self = Thread::Current(); |
| 2105 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Vladimir Marko | 9b03cb4 | 2017-02-16 16:37:03 +0000 | [diff] [blame] | 2106 | if (kUseReadBarrier) { |
| 2107 | // We do not track new roots for CC. |
| 2108 | DCHECK_EQ(0, flags & (kVisitRootFlagNewRoots | |
| 2109 | kVisitRootFlagClearRootLog | |
| 2110 | kVisitRootFlagStartLoggingNewRoots | |
| 2111 | kVisitRootFlagStopLoggingNewRoots)); |
| 2112 | } |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2113 | if ((flags & kVisitRootFlagAllRoots) != 0) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2114 | // Argument for how root visiting deals with ArtField and ArtMethod roots. |
| 2115 | // There is 3 GC cases to handle: |
| 2116 | // Non moving concurrent: |
| 2117 | // 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] | 2118 | // live by the class and class roots. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2119 | // |
| 2120 | // Moving non-concurrent: |
| 2121 | // This case needs to call visit VisitNativeRoots in case the classes or dex cache arrays move. |
| 2122 | // To prevent missing roots, this case needs to ensure that there is no |
| 2123 | // suspend points between the point which we allocate ArtMethod arrays and place them in a |
| 2124 | // class which is in the class table. |
| 2125 | // |
| 2126 | // Moving concurrent: |
| 2127 | // Need to make sure to not copy ArtMethods without doing read barriers since the roots are |
| 2128 | // 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] | 2129 | // |
| 2130 | // Use an unbuffered visitor since the class table uses a temporary GcRoot for holding decoded |
| 2131 | // ClassTable::TableSlot. The buffered root visiting would access a stale stack location for |
| 2132 | // these objects. |
| 2133 | UnbufferedRootVisitor root_visitor(visitor, RootInfo(kRootStickyClass)); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 2134 | boot_class_table_->VisitRoots(root_visitor); |
Mathieu Chartier | 7778b88 | 2015-10-05 16:41:10 -0700 | [diff] [blame] | 2135 | // 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] | 2136 | if ((flags & kVisitRootFlagClassLoader) != 0 || tracing_enabled) { |
Mathieu Chartier | 7778b88 | 2015-10-05 16:41:10 -0700 | [diff] [blame] | 2137 | for (const ClassLoaderData& data : class_loaders_) { |
| 2138 | GcRoot<mirror::Object> root(GcRoot<mirror::Object>(self->DecodeJObject(data.weak_root))); |
| 2139 | root.VisitRoot(visitor, RootInfo(kRootVMInternal)); |
| 2140 | } |
| 2141 | } |
Vladimir Marko | 9b03cb4 | 2017-02-16 16:37:03 +0000 | [diff] [blame] | 2142 | } else if (!kUseReadBarrier && (flags & kVisitRootFlagNewRoots) != 0) { |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 2143 | for (auto& root : new_class_roots_) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2144 | ObjPtr<mirror::Class> old_ref = root.Read<kWithoutReadBarrier>(); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 2145 | root.VisitRoot(visitor, RootInfo(kRootStickyClass)); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2146 | ObjPtr<mirror::Class> new_ref = root.Read<kWithoutReadBarrier>(); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 2147 | // Concurrent moving GC marked new roots through the to-space invariant. |
| 2148 | CHECK_EQ(new_ref, old_ref); |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2149 | } |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 2150 | for (const OatFile* oat_file : new_bss_roots_boot_oat_files_) { |
| 2151 | for (GcRoot<mirror::Object>& root : oat_file->GetBssGcRoots()) { |
| 2152 | ObjPtr<mirror::Object> old_ref = root.Read<kWithoutReadBarrier>(); |
| 2153 | if (old_ref != nullptr) { |
| 2154 | DCHECK(old_ref->IsClass()); |
| 2155 | root.VisitRoot(visitor, RootInfo(kRootStickyClass)); |
| 2156 | ObjPtr<mirror::Object> new_ref = root.Read<kWithoutReadBarrier>(); |
| 2157 | // Concurrent moving GC marked new roots through the to-space invariant. |
| 2158 | CHECK_EQ(new_ref, old_ref); |
| 2159 | } |
| 2160 | } |
| 2161 | } |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2162 | } |
Vladimir Marko | 9b03cb4 | 2017-02-16 16:37:03 +0000 | [diff] [blame] | 2163 | if (!kUseReadBarrier && (flags & kVisitRootFlagClearRootLog) != 0) { |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2164 | new_class_roots_.clear(); |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 2165 | new_bss_roots_boot_oat_files_.clear(); |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2166 | } |
Vladimir Marko | 9b03cb4 | 2017-02-16 16:37:03 +0000 | [diff] [blame] | 2167 | if (!kUseReadBarrier && (flags & kVisitRootFlagStartLoggingNewRoots) != 0) { |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 2168 | log_new_roots_ = true; |
Vladimir Marko | 9b03cb4 | 2017-02-16 16:37:03 +0000 | [diff] [blame] | 2169 | } else if (!kUseReadBarrier && (flags & kVisitRootFlagStopLoggingNewRoots) != 0) { |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 2170 | log_new_roots_ = false; |
Mathieu Chartier | 52e4b43 | 2014-06-10 11:22:31 -0700 | [diff] [blame] | 2171 | } |
| 2172 | // We deliberately ignore the class roots in the image since we |
| 2173 | // handle image roots by using the MS/CMS rescanning of dirty cards. |
| 2174 | } |
| 2175 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 2176 | // Keep in sync with InitCallback. Anything we visit, we need to |
| 2177 | // reinit references to when reinitializing a ClassLinker from a |
| 2178 | // mapped image. |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 2179 | void ClassLinker::VisitRoots(RootVisitor* visitor, VisitRootFlags flags) { |
Mathieu Chartier | 3100080 | 2015-06-14 14:14:37 -0700 | [diff] [blame] | 2180 | class_roots_.VisitRootIfNonNull(visitor, RootInfo(kRootVMInternal)); |
Mathieu Chartier | bb87e0f | 2015-04-03 11:21:55 -0700 | [diff] [blame] | 2181 | VisitClassRoots(visitor, flags); |
Mathieu Chartier | 6cfc2c0 | 2015-10-12 15:06:16 -0700 | [diff] [blame] | 2182 | // Instead of visiting the find_array_class_cache_ drop it so that it doesn't prevent class |
| 2183 | // unloading if we are marking roots. |
| 2184 | DropFindArrayClassCache(); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 2185 | } |
| 2186 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2187 | class VisitClassLoaderClassesVisitor : public ClassLoaderVisitor { |
| 2188 | public: |
| 2189 | explicit VisitClassLoaderClassesVisitor(ClassVisitor* visitor) |
| 2190 | : visitor_(visitor), |
| 2191 | done_(false) {} |
| 2192 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2193 | void Visit(ObjPtr<mirror::ClassLoader> class_loader) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 2194 | REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_) override { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2195 | ClassTable* const class_table = class_loader->GetClassTable(); |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2196 | if (!done_ && class_table != nullptr) { |
| 2197 | DefiningClassLoaderFilterVisitor visitor(class_loader, visitor_); |
| 2198 | if (!class_table->Visit(visitor)) { |
| 2199 | // If the visitor ClassTable returns false it means that we don't need to continue. |
| 2200 | done_ = true; |
| 2201 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2202 | } |
| 2203 | } |
| 2204 | |
| 2205 | private: |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2206 | // Class visitor that limits the class visits from a ClassTable to the classes with |
| 2207 | // the provided defining class loader. This filter is used to avoid multiple visits |
| 2208 | // of the same class which can be recorded for multiple initiating class loaders. |
| 2209 | class DefiningClassLoaderFilterVisitor : public ClassVisitor { |
| 2210 | public: |
| 2211 | DefiningClassLoaderFilterVisitor(ObjPtr<mirror::ClassLoader> defining_class_loader, |
| 2212 | ClassVisitor* visitor) |
| 2213 | : defining_class_loader_(defining_class_loader), visitor_(visitor) { } |
| 2214 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 2215 | bool operator()(ObjPtr<mirror::Class> klass) override REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2216 | if (klass->GetClassLoader() != defining_class_loader_) { |
| 2217 | return true; |
| 2218 | } |
| 2219 | return (*visitor_)(klass); |
| 2220 | } |
| 2221 | |
Vladimir Marko | 0984e48 | 2019-03-27 16:41:41 +0000 | [diff] [blame] | 2222 | const ObjPtr<mirror::ClassLoader> defining_class_loader_; |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2223 | ClassVisitor* const visitor_; |
| 2224 | }; |
| 2225 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2226 | ClassVisitor* const visitor_; |
| 2227 | // If done is true then we don't need to do any more visiting. |
| 2228 | bool done_; |
| 2229 | }; |
| 2230 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2231 | void ClassLinker::VisitClassesInternal(ClassVisitor* visitor) { |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 2232 | if (boot_class_table_->Visit(*visitor)) { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2233 | VisitClassLoaderClassesVisitor loader_visitor(visitor); |
| 2234 | VisitClassLoaders(&loader_visitor); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 2235 | } |
| 2236 | } |
| 2237 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2238 | void ClassLinker::VisitClasses(ClassVisitor* visitor) { |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 2239 | Thread* const self = Thread::Current(); |
| 2240 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 2241 | // Not safe to have thread suspension when we are holding a lock. |
| 2242 | if (self != nullptr) { |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 2243 | ScopedAssertNoThreadSuspension nts(__FUNCTION__); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2244 | VisitClassesInternal(visitor); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 2245 | } else { |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2246 | VisitClassesInternal(visitor); |
Elliott Hughes | a215526 | 2011-11-16 16:26:58 -0800 | [diff] [blame] | 2247 | } |
| 2248 | } |
| 2249 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2250 | class GetClassesInToVector : public ClassVisitor { |
| 2251 | public: |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 2252 | bool operator()(ObjPtr<mirror::Class> klass) override { |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2253 | classes_.push_back(klass); |
| 2254 | return true; |
| 2255 | } |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2256 | std::vector<ObjPtr<mirror::Class>> classes_; |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2257 | }; |
| 2258 | |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2259 | class GetClassInToObjectArray : public ClassVisitor { |
| 2260 | public: |
| 2261 | explicit GetClassInToObjectArray(mirror::ObjectArray<mirror::Class>* arr) |
| 2262 | : arr_(arr), index_(0) {} |
| 2263 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 2264 | bool operator()(ObjPtr<mirror::Class> klass) override REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2265 | ++index_; |
| 2266 | if (index_ <= arr_->GetLength()) { |
| 2267 | arr_->Set(index_ - 1, klass); |
| 2268 | return true; |
| 2269 | } |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2270 | return false; |
| 2271 | } |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2272 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 2273 | bool Succeeded() const REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2274 | return index_ <= arr_->GetLength(); |
| 2275 | } |
| 2276 | |
| 2277 | private: |
| 2278 | mirror::ObjectArray<mirror::Class>* const arr_; |
| 2279 | int32_t index_; |
| 2280 | }; |
| 2281 | |
| 2282 | void ClassLinker::VisitClassesWithoutClassesLock(ClassVisitor* visitor) { |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2283 | // TODO: it may be possible to avoid secondary storage if we iterate over dex caches. The problem |
| 2284 | // is avoiding duplicates. |
| 2285 | if (!kMovingClasses) { |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 2286 | ScopedAssertNoThreadSuspension nts(__FUNCTION__); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2287 | GetClassesInToVector accumulator; |
| 2288 | VisitClasses(&accumulator); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2289 | for (ObjPtr<mirror::Class> klass : accumulator.classes_) { |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 2290 | if (!visitor->operator()(klass)) { |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2291 | return; |
| 2292 | } |
| 2293 | } |
| 2294 | } else { |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 2295 | Thread* const self = Thread::Current(); |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2296 | StackHandleScope<1> hs(self); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2297 | auto classes = hs.NewHandle<mirror::ObjectArray<mirror::Class>>(nullptr); |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2298 | // We size the array assuming classes won't be added to the class table during the visit. |
| 2299 | // If this assumption fails we iterate again. |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2300 | while (true) { |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2301 | size_t class_table_size; |
| 2302 | { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2303 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2304 | // Add 100 in case new classes get loaded when we are filling in the object array. |
| 2305 | class_table_size = NumZygoteClasses() + NumNonZygoteClasses() + 100; |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2306 | } |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 2307 | ObjPtr<mirror::Class> array_of_class = GetClassRoot<mirror::ObjectArray<mirror::Class>>(this); |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2308 | classes.Assign( |
| 2309 | mirror::ObjectArray<mirror::Class>::Alloc(self, array_of_class, class_table_size)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 2310 | CHECK(classes != nullptr); // OOME. |
Mathieu Chartier | e0671ce | 2015-07-28 17:23:28 -0700 | [diff] [blame] | 2311 | GetClassInToObjectArray accumulator(classes.Get()); |
| 2312 | VisitClasses(&accumulator); |
| 2313 | if (accumulator.Succeeded()) { |
| 2314 | break; |
| 2315 | } |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2316 | } |
| 2317 | for (int32_t i = 0; i < classes->GetLength(); ++i) { |
| 2318 | // If the class table shrank during creation of the clases array we expect null elements. If |
| 2319 | // the class table grew then the loop repeats. If classes are created after the loop has |
| 2320 | // finished then we don't visit. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2321 | ObjPtr<mirror::Class> klass = classes->Get(i); |
Mathieu Chartier | 1aa8ec2 | 2016-02-01 10:34:47 -0800 | [diff] [blame] | 2322 | if (klass != nullptr && !visitor->operator()(klass)) { |
Ian Rogers | dbf3be0 | 2014-08-29 15:40:08 -0700 | [diff] [blame] | 2323 | return; |
| 2324 | } |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2325 | } |
| 2326 | } |
| 2327 | } |
| 2328 | |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 2329 | ClassLinker::~ClassLinker() { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2330 | Thread* const self = Thread::Current(); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 2331 | for (const ClassLoaderData& data : class_loaders_) { |
Alexey Grebenkin | be4c2bd | 2018-02-01 19:09:59 +0300 | [diff] [blame] | 2332 | // CHA unloading analysis is not needed. No negative consequences are expected because |
| 2333 | // all the classloaders are deleted at the same time. |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 2334 | DeleteClassLoader(self, data, /*cleanup_cha=*/ false); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 2335 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 2336 | class_loaders_.clear(); |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 2337 | while (!running_visibly_initialized_callbacks_.empty()) { |
| 2338 | std::unique_ptr<VisiblyInitializedCallback> callback( |
| 2339 | std::addressof(running_visibly_initialized_callbacks_.front())); |
| 2340 | running_visibly_initialized_callbacks_.pop_front(); |
| 2341 | } |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 2342 | } |
| 2343 | |
Alexey Grebenkin | be4c2bd | 2018-02-01 19:09:59 +0300 | [diff] [blame] | 2344 | void ClassLinker::DeleteClassLoader(Thread* self, const ClassLoaderData& data, bool cleanup_cha) { |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 2345 | Runtime* const runtime = Runtime::Current(); |
| 2346 | JavaVMExt* const vm = runtime->GetJavaVM(); |
| 2347 | vm->DeleteWeakGlobalRef(self, data.weak_root); |
Calin Juravle | e5de54c | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 2348 | // 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] | 2349 | if (runtime->GetJit() != nullptr) { |
| 2350 | jit::JitCodeCache* code_cache = runtime->GetJit()->GetCodeCache(); |
| 2351 | if (code_cache != nullptr) { |
Mathieu Chartier | cf79cf5 | 2017-07-21 11:17:57 -0700 | [diff] [blame] | 2352 | // For the JIT case, RemoveMethodsIn removes the CHA dependencies. |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 2353 | code_cache->RemoveMethodsIn(self, *data.allocator); |
| 2354 | } |
Andreas Gampe | c1ac9ee | 2017-07-24 22:35:49 -0700 | [diff] [blame] | 2355 | } else if (cha_ != nullptr) { |
Mathieu Chartier | cf79cf5 | 2017-07-21 11:17:57 -0700 | [diff] [blame] | 2356 | // 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] | 2357 | cha_->RemoveDependenciesForLinearAlloc(data.allocator); |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 2358 | } |
Alexey Grebenkin | be4c2bd | 2018-02-01 19:09:59 +0300 | [diff] [blame] | 2359 | // Cleanup references to single implementation ArtMethods that will be deleted. |
| 2360 | if (cleanup_cha) { |
| 2361 | CHAOnDeleteUpdateClassVisitor visitor(data.allocator); |
| 2362 | data.class_table->Visit<CHAOnDeleteUpdateClassVisitor, kWithoutReadBarrier>(visitor); |
| 2363 | } |
Vladimir Marko | 86c8752 | 2020-05-11 16:55:55 +0100 | [diff] [blame] | 2364 | { |
| 2365 | MutexLock lock(self, critical_native_code_with_clinit_check_lock_); |
| 2366 | auto end = critical_native_code_with_clinit_check_.end(); |
| 2367 | for (auto it = critical_native_code_with_clinit_check_.begin(); it != end; ) { |
| 2368 | if (data.allocator->ContainsUnsafe(it->first)) { |
| 2369 | it = critical_native_code_with_clinit_check_.erase(it); |
| 2370 | } else { |
| 2371 | ++it; |
| 2372 | } |
| 2373 | } |
| 2374 | } |
Alexey Grebenkin | be4c2bd | 2018-02-01 19:09:59 +0300 | [diff] [blame] | 2375 | |
Nicolas Geoffray | 1dad3f6 | 2015-10-23 14:59:54 +0100 | [diff] [blame] | 2376 | delete data.allocator; |
| 2377 | delete data.class_table; |
| 2378 | } |
| 2379 | |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 2380 | ObjPtr<mirror::PointerArray> ClassLinker::AllocPointerArray(Thread* self, size_t length) { |
| 2381 | return ObjPtr<mirror::PointerArray>::DownCast( |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 2382 | image_pointer_size_ == PointerSize::k64 |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 2383 | ? ObjPtr<mirror::Array>(mirror::LongArray::Alloc(self, length)) |
| 2384 | : ObjPtr<mirror::Array>(mirror::IntArray::Alloc(self, length))); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 2385 | } |
| 2386 | |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 2387 | ObjPtr<mirror::DexCache> ClassLinker::AllocDexCache(Thread* self, const DexFile& dex_file) { |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2388 | StackHandleScope<1> hs(self); |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 2389 | auto dex_cache(hs.NewHandle(ObjPtr<mirror::DexCache>::DownCast( |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 2390 | GetClassRoot<mirror::DexCache>(this)->AllocObject(self)))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 2391 | if (dex_cache == nullptr) { |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2392 | self->AssertPendingOOMException(); |
| 2393 | return nullptr; |
| 2394 | } |
Vladimir Marko | 31c3daa | 2019-06-13 12:18:37 +0100 | [diff] [blame] | 2395 | // Use InternWeak() so that the location String can be collected when the ClassLoader |
| 2396 | // with this DexCache is collected. |
| 2397 | ObjPtr<mirror::String> location = intern_table_->InternWeak(dex_file.GetLocation().c_str()); |
Mathieu Chartier | 6c60d84 | 2016-09-15 10:24:43 -0700 | [diff] [blame] | 2398 | if (location == nullptr) { |
| 2399 | self->AssertPendingOOMException(); |
| 2400 | return nullptr; |
| 2401 | } |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 2402 | dex_cache->SetLocation(location); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2403 | return dex_cache.Get(); |
Brian Carlstrom | a080803 | 2011-07-18 00:39:23 -0700 | [diff] [blame] | 2404 | } |
| 2405 | |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 2406 | ObjPtr<mirror::DexCache> ClassLinker::AllocAndInitializeDexCache(Thread* self, |
| 2407 | const DexFile& dex_file, |
| 2408 | LinearAlloc* linear_alloc) { |
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 | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 2412 | dex_cache->InitializeNativeFields(&dex_file, linear_alloc); |
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_); |
| 2477 | // Skip EnsureSkipAccessChecksMethods(). We can skip the verified status, |
| 2478 | // the kAccVerificationAttempted flag is added below, and there are no |
| 2479 | // methods that need the kAccSkipAccessChecks flag. |
| 2480 | DCHECK_EQ(array_class->NumMethods(), 0u); |
| 2481 | |
| 2482 | // don't need to set new_class->SetObjectSize(..) |
| 2483 | // because Object::SizeOf delegates to Array::SizeOf |
| 2484 | |
| 2485 | // All arrays have java/lang/Cloneable and java/io/Serializable as |
| 2486 | // interfaces. We need to set that up here, so that stuff like |
| 2487 | // "instanceof" works right. |
| 2488 | |
| 2489 | // Use the single, global copies of "interfaces" and "iftable" |
| 2490 | // (remember not to free them for arrays). |
| 2491 | { |
| 2492 | ObjPtr<mirror::IfTable> array_iftable = GetArrayIfTable(); |
| 2493 | CHECK(array_iftable != nullptr); |
| 2494 | array_class->SetIfTable(array_iftable); |
| 2495 | } |
| 2496 | |
| 2497 | // Inherit access flags from the component type. |
| 2498 | int access_flags = component_type->GetAccessFlags(); |
| 2499 | // Lose any implementation detail flags; in particular, arrays aren't finalizable. |
| 2500 | access_flags &= kAccJavaFlagsMask; |
| 2501 | // Arrays can't be used as a superclass or interface, so we want to add "abstract final" |
| 2502 | // and remove "interface". |
| 2503 | access_flags |= kAccAbstract | kAccFinal; |
| 2504 | access_flags &= ~kAccInterface; |
| 2505 | // Arrays are access-checks-clean and preverified. |
| 2506 | access_flags |= kAccVerificationAttempted; |
| 2507 | |
Vladimir Marko | b68bb7a | 2020-03-17 10:55:25 +0000 | [diff] [blame] | 2508 | array_class->SetAccessFlagsDuringLinking(access_flags); |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 2509 | |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 2510 | // Array classes are fully initialized either during single threaded startup, |
| 2511 | // or from a pre-fence visitor, so visibly initialized. |
| 2512 | array_class->SetStatusForPrimitiveOrArray(ClassStatus::kVisiblyInitialized); |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 2513 | } |
| 2514 | |
| 2515 | void ClassLinker::FinishCoreArrayClassSetup(ClassRoot array_root) { |
| 2516 | // Do not hold lock on the array class object, the initialization of |
| 2517 | // core array classes is done while the process is still single threaded. |
| 2518 | ObjPtr<mirror::Class> array_class = GetClassRoot(array_root, this); |
| 2519 | FinishArrayClassSetup(array_class); |
| 2520 | |
| 2521 | std::string temp; |
| 2522 | const char* descriptor = array_class->GetDescriptor(&temp); |
| 2523 | size_t hash = ComputeModifiedUtf8Hash(descriptor); |
| 2524 | ObjPtr<mirror::Class> existing = InsertClass(descriptor, array_class, hash); |
| 2525 | CHECK(existing == nullptr); |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 2526 | } |
| 2527 | |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 2528 | ObjPtr<mirror::ObjectArray<mirror::StackTraceElement>> ClassLinker::AllocStackTraceElementArray( |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 2529 | Thread* self, |
| 2530 | size_t length) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 2531 | return mirror::ObjectArray<mirror::StackTraceElement>::Alloc( |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 2532 | self, GetClassRoot<mirror::ObjectArray<mirror::StackTraceElement>>(this), length); |
Shih-wei Liao | 55df06b | 2011-08-26 14:39:27 -0700 | [diff] [blame] | 2533 | } |
| 2534 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 2535 | ObjPtr<mirror::Class> ClassLinker::EnsureResolved(Thread* self, |
| 2536 | const char* descriptor, |
| 2537 | ObjPtr<mirror::Class> klass) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2538 | DCHECK(klass != nullptr); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2539 | if (kIsDebugBuild) { |
| 2540 | StackHandleScope<1> hs(self); |
| 2541 | HandleWrapperObjPtr<mirror::Class> h = hs.NewHandleWrapper(&klass); |
| 2542 | Thread::PoisonObjectPointersIfDebug(); |
| 2543 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2544 | |
| 2545 | // For temporary classes we must wait for them to be retired. |
| 2546 | if (init_done_ && klass->IsTemp()) { |
| 2547 | CHECK(!klass->IsResolved()); |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2548 | if (klass->IsErroneousUnresolved()) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2549 | ThrowEarlierClassFailure(klass); |
| 2550 | return nullptr; |
| 2551 | } |
| 2552 | StackHandleScope<1> hs(self); |
| 2553 | Handle<mirror::Class> h_class(hs.NewHandle(klass)); |
| 2554 | ObjectLock<mirror::Class> lock(self, h_class); |
| 2555 | // Loop and wait for the resolving thread to retire this class. |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2556 | while (!h_class->IsRetired() && !h_class->IsErroneousUnresolved()) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2557 | lock.WaitIgnoringInterrupts(); |
| 2558 | } |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2559 | if (h_class->IsErroneousUnresolved()) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2560 | ThrowEarlierClassFailure(h_class.Get()); |
| 2561 | return nullptr; |
| 2562 | } |
| 2563 | CHECK(h_class->IsRetired()); |
| 2564 | // Get the updated class from class table. |
Andreas Gampe | 34ee684 | 2014-12-02 15:43:52 -0800 | [diff] [blame] | 2565 | klass = LookupClass(self, descriptor, h_class.Get()->GetClassLoader()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2566 | } |
| 2567 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2568 | // Wait for the class if it has not already been linked. |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 2569 | size_t index = 0; |
| 2570 | // Maximum number of yield iterations until we start sleeping. |
| 2571 | static const size_t kNumYieldIterations = 1000; |
| 2572 | // How long each sleep is in us. |
| 2573 | static const size_t kSleepDurationUS = 1000; // 1 ms. |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2574 | while (!klass->IsResolved() && !klass->IsErroneousUnresolved()) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 2575 | StackHandleScope<1> hs(self); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2576 | HandleWrapperObjPtr<mirror::Class> h_class(hs.NewHandleWrapper(&klass)); |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 2577 | { |
| 2578 | ObjectTryLock<mirror::Class> lock(self, h_class); |
| 2579 | // Can not use a monitor wait here since it may block when returning and deadlock if another |
| 2580 | // thread has locked klass. |
| 2581 | if (lock.Acquired()) { |
| 2582 | // Check for circular dependencies between classes, the lock is required for SetStatus. |
| 2583 | if (!h_class->IsResolved() && h_class->GetClinitThreadId() == self->GetTid()) { |
| 2584 | ThrowClassCircularityError(h_class.Get()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 2585 | mirror::Class::SetStatus(h_class, ClassStatus::kErrorUnresolved, self); |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 2586 | return nullptr; |
| 2587 | } |
| 2588 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2589 | } |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 2590 | { |
| 2591 | // Handle wrapper deals with klass moving. |
| 2592 | ScopedThreadSuspension sts(self, kSuspended); |
| 2593 | if (index < kNumYieldIterations) { |
| 2594 | sched_yield(); |
| 2595 | } else { |
| 2596 | usleep(kSleepDurationUS); |
| 2597 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2598 | } |
Mathieu Chartier | 4b0ef1c | 2016-07-29 16:26:01 -0700 | [diff] [blame] | 2599 | ++index; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2600 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2601 | |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 2602 | if (klass->IsErroneousUnresolved()) { |
Elliott Hughes | 4a2b417 | 2011-09-20 17:08:25 -0700 | [diff] [blame] | 2603 | ThrowEarlierClassFailure(klass); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 2604 | return nullptr; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 2605 | } |
| 2606 | // Return the loaded class. No exceptions should be pending. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 2607 | CHECK(klass->IsResolved()) << klass->PrettyClass(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 2608 | self->AssertNoPendingException(); |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 2609 | return klass; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2610 | } |
| 2611 | |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 2612 | using ClassPathEntry = std::pair<const DexFile*, const dex::ClassDef*>; |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2613 | |
| 2614 | // Search a collection of DexFiles for a descriptor |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2615 | ClassPathEntry FindInClassPath(const char* descriptor, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 2616 | size_t hash, const std::vector<const DexFile*>& class_path) { |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2617 | for (const DexFile* dex_file : class_path) { |
Mathieu Chartier | 0a19e21 | 2019-11-27 14:35:24 -0800 | [diff] [blame] | 2618 | DCHECK(dex_file != nullptr); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 2619 | const dex::ClassDef* dex_class_def = OatDexFile::FindClassDef(*dex_file, descriptor, hash); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 2620 | if (dex_class_def != nullptr) { |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2621 | return ClassPathEntry(dex_file, dex_class_def); |
| 2622 | } |
| 2623 | } |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2624 | return ClassPathEntry(nullptr, nullptr); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2625 | } |
| 2626 | |
Nicolas Geoffray | 80a560c | 2018-10-26 13:48:51 +0100 | [diff] [blame] | 2627 | bool ClassLinker::FindClassInSharedLibraries(ScopedObjectAccessAlreadyRunnable& soa, |
| 2628 | Thread* self, |
| 2629 | const char* descriptor, |
| 2630 | size_t hash, |
| 2631 | Handle<mirror::ClassLoader> class_loader, |
| 2632 | /*out*/ ObjPtr<mirror::Class>* result) { |
| 2633 | ArtField* field = |
| 2634 | jni::DecodeArtField(WellKnownClasses::dalvik_system_BaseDexClassLoader_sharedLibraryLoaders); |
| 2635 | ObjPtr<mirror::Object> raw_shared_libraries = field->GetObject(class_loader.Get()); |
| 2636 | if (raw_shared_libraries == nullptr) { |
| 2637 | return true; |
| 2638 | } |
| 2639 | |
| 2640 | StackHandleScope<2> hs(self); |
| 2641 | Handle<mirror::ObjectArray<mirror::ClassLoader>> shared_libraries( |
| 2642 | hs.NewHandle(raw_shared_libraries->AsObjectArray<mirror::ClassLoader>())); |
| 2643 | MutableHandle<mirror::ClassLoader> temp_loader = hs.NewHandle<mirror::ClassLoader>(nullptr); |
Alex Light | a9bbc08 | 2019-11-14 14:51:41 -0800 | [diff] [blame] | 2644 | for (auto loader : shared_libraries.Iterate<mirror::ClassLoader>()) { |
| 2645 | temp_loader.Assign(loader); |
Nicolas Geoffray | 80a560c | 2018-10-26 13:48:51 +0100 | [diff] [blame] | 2646 | if (!FindClassInBaseDexClassLoader(soa, self, descriptor, hash, temp_loader, result)) { |
| 2647 | return false; // One of the shared libraries is not supported. |
| 2648 | } |
| 2649 | if (*result != nullptr) { |
| 2650 | return true; // Found the class up the chain. |
| 2651 | } |
| 2652 | } |
| 2653 | return true; |
| 2654 | } |
| 2655 | |
Nicolas Geoffray | 7d8d8ff | 2016-11-02 12:38:05 +0000 | [diff] [blame] | 2656 | bool ClassLinker::FindClassInBaseDexClassLoader(ScopedObjectAccessAlreadyRunnable& soa, |
| 2657 | Thread* self, |
| 2658 | const char* descriptor, |
| 2659 | size_t hash, |
| 2660 | Handle<mirror::ClassLoader> class_loader, |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 2661 | /*out*/ ObjPtr<mirror::Class>* result) { |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 2662 | // Termination case: boot class loader. |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2663 | if (IsBootClassLoader(soa, class_loader.Get())) { |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 2664 | *result = FindClassInBootClassLoaderClassPath(self, descriptor, hash); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2665 | return true; |
| 2666 | } |
| 2667 | |
David Brazdil | 05909d8 | 2018-12-06 16:25:16 +0000 | [diff] [blame] | 2668 | if (IsPathOrDexClassLoader(soa, class_loader) || IsInMemoryDexClassLoader(soa, class_loader)) { |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 2669 | // For regular path or dex class loader the search order is: |
| 2670 | // - parent |
Nicolas Geoffray | 80a560c | 2018-10-26 13:48:51 +0100 | [diff] [blame] | 2671 | // - shared libraries |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 2672 | // - class loader dex files |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2673 | |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 2674 | // Handles as RegisterDexFile may allocate dex caches (and cause thread suspension). |
| 2675 | StackHandleScope<1> hs(self); |
| 2676 | Handle<mirror::ClassLoader> h_parent(hs.NewHandle(class_loader->GetParent())); |
| 2677 | if (!FindClassInBaseDexClassLoader(soa, self, descriptor, hash, h_parent, result)) { |
| 2678 | return false; // One of the parents is not supported. |
| 2679 | } |
| 2680 | if (*result != nullptr) { |
| 2681 | return true; // Found the class up the chain. |
| 2682 | } |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2683 | |
Nicolas Geoffray | 80a560c | 2018-10-26 13:48:51 +0100 | [diff] [blame] | 2684 | if (!FindClassInSharedLibraries(soa, self, descriptor, hash, class_loader, result)) { |
| 2685 | return false; // One of the shared library loader is not supported. |
| 2686 | } |
| 2687 | if (*result != nullptr) { |
| 2688 | return true; // Found the class in a shared library. |
| 2689 | } |
| 2690 | |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 2691 | // Search the current class loader classpath. |
| 2692 | *result = FindClassInBaseDexClassLoaderClassPath(soa, descriptor, hash, class_loader); |
Andreas Gampe | 501c3b0 | 2019-04-17 21:54:27 +0000 | [diff] [blame] | 2693 | return !soa.Self()->IsExceptionPending(); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2694 | } |
| 2695 | |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 2696 | if (IsDelegateLastClassLoader(soa, class_loader)) { |
| 2697 | // For delegate last, the search order is: |
| 2698 | // - boot class path |
Nicolas Geoffray | 80a560c | 2018-10-26 13:48:51 +0100 | [diff] [blame] | 2699 | // - shared libraries |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 2700 | // - class loader dex files |
| 2701 | // - parent |
| 2702 | *result = FindClassInBootClassLoaderClassPath(self, descriptor, hash); |
| 2703 | if (*result != nullptr) { |
| 2704 | return true; // The class is part of the boot class path. |
| 2705 | } |
Andreas Gampe | 501c3b0 | 2019-04-17 21:54:27 +0000 | [diff] [blame] | 2706 | if (self->IsExceptionPending()) { |
| 2707 | // Pending exception means there was an error other than ClassNotFound that must be returned |
| 2708 | // to the caller. |
| 2709 | return false; |
| 2710 | } |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 2711 | |
Nicolas Geoffray | 80a560c | 2018-10-26 13:48:51 +0100 | [diff] [blame] | 2712 | if (!FindClassInSharedLibraries(soa, self, descriptor, hash, class_loader, result)) { |
| 2713 | return false; // One of the shared library loader is not supported. |
| 2714 | } |
| 2715 | if (*result != nullptr) { |
| 2716 | return true; // Found the class in a shared library. |
| 2717 | } |
| 2718 | |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 2719 | *result = FindClassInBaseDexClassLoaderClassPath(soa, descriptor, hash, class_loader); |
| 2720 | if (*result != nullptr) { |
| 2721 | return true; // Found the class in the current class loader |
| 2722 | } |
Andreas Gampe | 501c3b0 | 2019-04-17 21:54:27 +0000 | [diff] [blame] | 2723 | if (self->IsExceptionPending()) { |
| 2724 | // Pending exception means there was an error other than ClassNotFound that must be returned |
| 2725 | // to the caller. |
| 2726 | return false; |
| 2727 | } |
Calin Juravle | cdd4912 | 2017-07-05 20:09:53 -0700 | [diff] [blame] | 2728 | |
| 2729 | // Handles as RegisterDexFile may allocate dex caches (and cause thread suspension). |
| 2730 | StackHandleScope<1> hs(self); |
| 2731 | Handle<mirror::ClassLoader> h_parent(hs.NewHandle(class_loader->GetParent())); |
| 2732 | return FindClassInBaseDexClassLoader(soa, self, descriptor, hash, h_parent, result); |
| 2733 | } |
| 2734 | |
| 2735 | // Unsupported class loader. |
| 2736 | *result = nullptr; |
| 2737 | return false; |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 2738 | } |
| 2739 | |
Andreas Gampe | 501c3b0 | 2019-04-17 21:54:27 +0000 | [diff] [blame] | 2740 | namespace { |
| 2741 | |
| 2742 | // Matches exceptions caught in DexFile.defineClass. |
| 2743 | ALWAYS_INLINE bool MatchesDexFileCaughtExceptions(ObjPtr<mirror::Throwable> throwable, |
| 2744 | ClassLinker* class_linker) |
| 2745 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 2746 | return |
| 2747 | // ClassNotFoundException. |
| 2748 | throwable->InstanceOf(GetClassRoot(ClassRoot::kJavaLangClassNotFoundException, |
| 2749 | class_linker)) |
| 2750 | || |
| 2751 | // NoClassDefFoundError. TODO: Reconsider this. b/130746382. |
| 2752 | throwable->InstanceOf(Runtime::Current()->GetPreAllocatedNoClassDefFoundError()->GetClass()); |
| 2753 | } |
| 2754 | |
| 2755 | // Clear exceptions caught in DexFile.defineClass. |
| 2756 | ALWAYS_INLINE void FilterDexFileCaughtExceptions(Thread* self, ClassLinker* class_linker) |
| 2757 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 2758 | if (MatchesDexFileCaughtExceptions(self->GetException(), class_linker)) { |
| 2759 | self->ClearException(); |
| 2760 | } |
| 2761 | } |
| 2762 | |
| 2763 | } // namespace |
| 2764 | |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 2765 | // Finds the class in the boot class loader. |
| 2766 | // If the class is found the method returns the resolved class. Otherwise it returns null. |
| 2767 | ObjPtr<mirror::Class> ClassLinker::FindClassInBootClassLoaderClassPath(Thread* self, |
| 2768 | const char* descriptor, |
| 2769 | size_t hash) { |
| 2770 | ObjPtr<mirror::Class> result = nullptr; |
| 2771 | ClassPathEntry pair = FindInClassPath(descriptor, hash, boot_class_path_); |
| 2772 | if (pair.second != nullptr) { |
| 2773 | ObjPtr<mirror::Class> klass = LookupClass(self, descriptor, hash, nullptr); |
| 2774 | if (klass != nullptr) { |
| 2775 | result = EnsureResolved(self, descriptor, klass); |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2776 | } else { |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 2777 | result = DefineClass(self, |
| 2778 | descriptor, |
| 2779 | hash, |
| 2780 | ScopedNullHandle<mirror::ClassLoader>(), |
| 2781 | *pair.first, |
| 2782 | *pair.second); |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2783 | } |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 2784 | if (result == nullptr) { |
| 2785 | CHECK(self->IsExceptionPending()) << descriptor; |
Andreas Gampe | 501c3b0 | 2019-04-17 21:54:27 +0000 | [diff] [blame] | 2786 | FilterDexFileCaughtExceptions(self, this); |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2787 | } |
| 2788 | } |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 2789 | return result; |
| 2790 | } |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2791 | |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 2792 | ObjPtr<mirror::Class> ClassLinker::FindClassInBaseDexClassLoaderClassPath( |
| 2793 | ScopedObjectAccessAlreadyRunnable& soa, |
| 2794 | const char* descriptor, |
| 2795 | size_t hash, |
| 2796 | Handle<mirror::ClassLoader> class_loader) { |
David Brazdil | 05909d8 | 2018-12-06 16:25:16 +0000 | [diff] [blame] | 2797 | DCHECK(IsPathOrDexClassLoader(soa, class_loader) || |
| 2798 | IsInMemoryDexClassLoader(soa, class_loader) || |
| 2799 | IsDelegateLastClassLoader(soa, class_loader)) |
Calin Juravle | 415dc3d | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 2800 | << "Unexpected class loader for descriptor " << descriptor; |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2801 | |
Andreas Gampe | b8e7c37 | 2018-02-20 18:24:55 -0800 | [diff] [blame] | 2802 | ObjPtr<mirror::Class> ret; |
| 2803 | auto define_class = [&](const DexFile* cp_dex_file) REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 2804 | const dex::ClassDef* dex_class_def = OatDexFile::FindClassDef(*cp_dex_file, descriptor, hash); |
Andreas Gampe | b8e7c37 | 2018-02-20 18:24:55 -0800 | [diff] [blame] | 2805 | if (dex_class_def != nullptr) { |
| 2806 | ObjPtr<mirror::Class> klass = DefineClass(soa.Self(), |
| 2807 | descriptor, |
| 2808 | hash, |
| 2809 | class_loader, |
| 2810 | *cp_dex_file, |
| 2811 | *dex_class_def); |
| 2812 | if (klass == nullptr) { |
| 2813 | CHECK(soa.Self()->IsExceptionPending()) << descriptor; |
Andreas Gampe | 501c3b0 | 2019-04-17 21:54:27 +0000 | [diff] [blame] | 2814 | FilterDexFileCaughtExceptions(soa.Self(), this); |
Andreas Gampe | b8e7c37 | 2018-02-20 18:24:55 -0800 | [diff] [blame] | 2815 | // TODO: Is it really right to break here, and not check the other dex files? |
Andreas Gampe | 501c3b0 | 2019-04-17 21:54:27 +0000 | [diff] [blame] | 2816 | } else { |
| 2817 | DCHECK(!soa.Self()->IsExceptionPending()); |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2818 | } |
Andreas Gampe | b8e7c37 | 2018-02-20 18:24:55 -0800 | [diff] [blame] | 2819 | ret = klass; |
| 2820 | return false; // Found a Class (or error == nullptr), stop visit. |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2821 | } |
Andreas Gampe | b8e7c37 | 2018-02-20 18:24:55 -0800 | [diff] [blame] | 2822 | return true; // Continue with the next DexFile. |
| 2823 | }; |
| 2824 | |
| 2825 | VisitClassLoaderDexFiles(soa, class_loader, define_class); |
| 2826 | return ret; |
Mathieu Chartier | ab0ed82 | 2014-09-11 14:21:41 -0700 | [diff] [blame] | 2827 | } |
| 2828 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 2829 | ObjPtr<mirror::Class> ClassLinker::FindClass(Thread* self, |
| 2830 | const char* descriptor, |
| 2831 | Handle<mirror::ClassLoader> class_loader) { |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 2832 | DCHECK_NE(*descriptor, '\0') << "descriptor is empty string"; |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 2833 | DCHECK(self != nullptr); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 2834 | self->AssertNoPendingException(); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 2835 | self->PoisonObjectPointers(); // For DefineClass, CreateArrayClass, etc... |
Elliott Hughes | c3b77c7 | 2011-12-15 20:56:48 -0800 | [diff] [blame] | 2836 | if (descriptor[1] == '\0') { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 2837 | // only the descriptors of primitive types should be 1 character long, also avoid class lookup |
| 2838 | // for primitive classes that aren't backed by dex files. |
| 2839 | return FindPrimitiveClass(descriptor[0]); |
| 2840 | } |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 2841 | const size_t hash = ComputeModifiedUtf8Hash(descriptor); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2842 | // Find the class in the loaded classes table. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 2843 | ObjPtr<mirror::Class> klass = LookupClass(self, descriptor, hash, class_loader.Get()); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2844 | if (klass != nullptr) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 2845 | return EnsureResolved(self, descriptor, klass); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2846 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2847 | // Class is not yet loaded. |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 2848 | if (descriptor[0] != '[' && class_loader == nullptr) { |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 2849 | // 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] | 2850 | ClassPathEntry pair = FindInClassPath(descriptor, hash, boot_class_path_); |
Ian Rogers | 68b5685 | 2014-08-29 20:19:11 -0700 | [diff] [blame] | 2851 | if (pair.second != nullptr) { |
Mathieu Chartier | 9865bde | 2015-12-21 09:58:16 -0800 | [diff] [blame] | 2852 | return DefineClass(self, |
| 2853 | descriptor, |
| 2854 | hash, |
| 2855 | ScopedNullHandle<mirror::ClassLoader>(), |
| 2856 | *pair.first, |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 2857 | *pair.second); |
Ian Rogers | 6355745 | 2014-06-04 16:57:15 -0700 | [diff] [blame] | 2858 | } else { |
| 2859 | // The boot class loader is searched ahead of the application class loader, failures are |
| 2860 | // expected and will be wrapped in a ClassNotFoundException. Use the pre-allocated error to |
| 2861 | // trigger the chaining with a proper stack trace. |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 2862 | ObjPtr<mirror::Throwable> pre_allocated = |
| 2863 | Runtime::Current()->GetPreAllocatedNoClassDefFoundError(); |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 2864 | self->SetException(pre_allocated); |
Ian Rogers | 6355745 | 2014-06-04 16:57:15 -0700 | [diff] [blame] | 2865 | return nullptr; |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 2866 | } |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 2867 | } |
| 2868 | ObjPtr<mirror::Class> result_ptr; |
| 2869 | bool descriptor_equals; |
| 2870 | if (descriptor[0] == '[') { |
| 2871 | result_ptr = CreateArrayClass(self, descriptor, hash, class_loader); |
| 2872 | DCHECK_EQ(result_ptr == nullptr, self->IsExceptionPending()); |
| 2873 | DCHECK(result_ptr == nullptr || result_ptr->DescriptorEquals(descriptor)); |
| 2874 | descriptor_equals = true; |
Jesse Wilson | 47daf87 | 2011-11-23 11:42:45 -0500 | [diff] [blame] | 2875 | } else { |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 2876 | ScopedObjectAccessUnchecked soa(self); |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2877 | bool known_hierarchy = |
| 2878 | FindClassInBaseDexClassLoader(soa, self, descriptor, hash, class_loader, &result_ptr); |
| 2879 | if (result_ptr != nullptr) { |
| 2880 | // The chain was understood and we found the class. We still need to add the class to |
| 2881 | // the class table to protect from racy programs that can try and redefine the path list |
| 2882 | // which would change the Class<?> returned for subsequent evaluation of const-class. |
| 2883 | DCHECK(known_hierarchy); |
| 2884 | DCHECK(result_ptr->DescriptorEquals(descriptor)); |
| 2885 | descriptor_equals = true; |
Andreas Gampe | 501c3b0 | 2019-04-17 21:54:27 +0000 | [diff] [blame] | 2886 | } else if (!self->IsExceptionPending()) { |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2887 | // Either the chain wasn't understood or the class wasn't found. |
Andreas Gampe | 501c3b0 | 2019-04-17 21:54:27 +0000 | [diff] [blame] | 2888 | // If there is a pending exception we didn't clear, it is a not a ClassNotFoundException and |
| 2889 | // we should return it instead of silently clearing and retrying. |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2890 | // |
| 2891 | // If the chain was understood but we did not find the class, let the Java-side |
| 2892 | // rediscover all this and throw the exception with the right stack trace. Note that |
| 2893 | // the Java-side could still succeed for racy programs if another thread is actively |
| 2894 | // modifying the class loader's path list. |
Andreas Gampe | f865ea9 | 2015-04-13 22:14:19 -0700 | [diff] [blame] | 2895 | |
Alex Light | 185a461 | 2018-10-04 15:54:25 -0700 | [diff] [blame] | 2896 | // 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] | 2897 | if (self->IsRuntimeThread()) { |
Calin Juravle | ccd5695 | 2016-12-15 17:57:38 +0000 | [diff] [blame] | 2898 | // Oops, we can't call into java so we can't run actual class-loader code. |
| 2899 | // This is true for e.g. for the compiler (jit or aot). |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2900 | ObjPtr<mirror::Throwable> pre_allocated = |
| 2901 | Runtime::Current()->GetPreAllocatedNoClassDefFoundError(); |
| 2902 | self->SetException(pre_allocated); |
Vladimir Marko | 2c8c6b6 | 2016-12-01 17:42:00 +0000 | [diff] [blame] | 2903 | return nullptr; |
| 2904 | } |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2905 | |
Vladimir Marko | 5fdd778 | 2017-04-20 11:26:03 +0100 | [diff] [blame] | 2906 | // Inlined DescriptorToDot(descriptor) with extra validation. |
| 2907 | // |
| 2908 | // Throw NoClassDefFoundError early rather than potentially load a class only to fail |
| 2909 | // the DescriptorEquals() check below and give a confusing error message. For example, |
| 2910 | // when native code erroneously calls JNI GetFieldId() with signature "java/lang/String" |
| 2911 | // instead of "Ljava/lang/String;", the message below using the "dot" names would be |
| 2912 | // "class loader [...] returned class java.lang.String instead of java.lang.String". |
| 2913 | size_t descriptor_length = strlen(descriptor); |
| 2914 | if (UNLIKELY(descriptor[0] != 'L') || |
| 2915 | UNLIKELY(descriptor[descriptor_length - 1] != ';') || |
| 2916 | UNLIKELY(memchr(descriptor + 1, '.', descriptor_length - 2) != nullptr)) { |
| 2917 | ThrowNoClassDefFoundError("Invalid descriptor: %s.", descriptor); |
| 2918 | return nullptr; |
| 2919 | } |
Andreas Gampe | 501c3b0 | 2019-04-17 21:54:27 +0000 | [diff] [blame] | 2920 | |
Vladimir Marko | 5fdd778 | 2017-04-20 11:26:03 +0100 | [diff] [blame] | 2921 | std::string class_name_string(descriptor + 1, descriptor_length - 2); |
| 2922 | std::replace(class_name_string.begin(), class_name_string.end(), '/', '.'); |
Andreas Gampe | 87658f3 | 2019-04-18 18:39:02 +0000 | [diff] [blame] | 2923 | if (known_hierarchy && |
| 2924 | fast_class_not_found_exceptions_ && |
| 2925 | !Runtime::Current()->IsJavaDebuggable()) { |
| 2926 | // For known hierarchy, we know that the class is going to throw an exception. If we aren't |
| 2927 | // debuggable, optimize this path by throwing directly here without going back to Java |
| 2928 | // language. This reduces how many ClassNotFoundExceptions happen. |
| 2929 | self->ThrowNewExceptionF("Ljava/lang/ClassNotFoundException;", |
| 2930 | "%s", |
| 2931 | class_name_string.c_str()); |
| 2932 | } else { |
| 2933 | ScopedLocalRef<jobject> class_loader_object( |
| 2934 | soa.Env(), soa.AddLocalReference<jobject>(class_loader.Get())); |
| 2935 | ScopedLocalRef<jobject> result(soa.Env(), nullptr); |
| 2936 | { |
| 2937 | ScopedThreadStateChange tsc(self, kNative); |
| 2938 | ScopedLocalRef<jobject> class_name_object( |
| 2939 | soa.Env(), soa.Env()->NewStringUTF(class_name_string.c_str())); |
| 2940 | if (class_name_object.get() == nullptr) { |
| 2941 | DCHECK(self->IsExceptionPending()); // OOME. |
| 2942 | return nullptr; |
| 2943 | } |
| 2944 | CHECK(class_loader_object.get() != nullptr); |
| 2945 | result.reset(soa.Env()->CallObjectMethod(class_loader_object.get(), |
| 2946 | WellKnownClasses::java_lang_ClassLoader_loadClass, |
| 2947 | class_name_object.get())); |
| 2948 | } |
| 2949 | if (result.get() == nullptr && !self->IsExceptionPending()) { |
| 2950 | // broken loader - throw NPE to be compatible with Dalvik |
| 2951 | ThrowNullPointerException(StringPrintf("ClassLoader.loadClass returned null for %s", |
| 2952 | class_name_string.c_str()).c_str()); |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2953 | return nullptr; |
| 2954 | } |
Andreas Gampe | 87658f3 | 2019-04-18 18:39:02 +0000 | [diff] [blame] | 2955 | result_ptr = soa.Decode<mirror::Class>(result.get()); |
| 2956 | // Check the name of the returned class. |
| 2957 | descriptor_equals = (result_ptr != nullptr) && result_ptr->DescriptorEquals(descriptor); |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 2958 | } |
Andreas Gampe | 501c3b0 | 2019-04-17 21:54:27 +0000 | [diff] [blame] | 2959 | } else { |
| 2960 | DCHECK(!MatchesDexFileCaughtExceptions(self->GetException(), this)); |
Vladimir Marko | 2c8c6b6 | 2016-12-01 17:42:00 +0000 | [diff] [blame] | 2961 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 2962 | } |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 2963 | |
| 2964 | if (self->IsExceptionPending()) { |
| 2965 | // If the ClassLoader threw or array class allocation failed, pass that exception up. |
| 2966 | // However, to comply with the RI behavior, first check if another thread succeeded. |
| 2967 | result_ptr = LookupClass(self, descriptor, hash, class_loader.Get()); |
| 2968 | if (result_ptr != nullptr && !result_ptr->IsErroneous()) { |
| 2969 | self->ClearException(); |
| 2970 | return EnsureResolved(self, descriptor, result_ptr); |
| 2971 | } |
| 2972 | return nullptr; |
| 2973 | } |
| 2974 | |
| 2975 | // Try to insert the class to the class table, checking for mismatch. |
| 2976 | ObjPtr<mirror::Class> old; |
| 2977 | { |
| 2978 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 2979 | ClassTable* const class_table = InsertClassTableForClassLoader(class_loader.Get()); |
| 2980 | old = class_table->Lookup(descriptor, hash); |
| 2981 | if (old == nullptr) { |
| 2982 | old = result_ptr; // For the comparison below, after releasing the lock. |
| 2983 | if (descriptor_equals) { |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 2984 | class_table->InsertWithHash(result_ptr, hash); |
Mathieu Chartier | 88ea61e | 2018-06-20 17:45:41 -0700 | [diff] [blame] | 2985 | WriteBarrier::ForEveryFieldWrite(class_loader.Get()); |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 2986 | } // else throw below, after releasing the lock. |
| 2987 | } |
| 2988 | } |
| 2989 | if (UNLIKELY(old != result_ptr)) { |
| 2990 | // Return `old` (even if `!descriptor_equals`) to mimic the RI behavior for parallel |
| 2991 | // capable class loaders. (All class loaders are considered parallel capable on Android.) |
Vladimir Marko | dfc0de7 | 2019-04-01 10:57:55 +0100 | [diff] [blame] | 2992 | ObjPtr<mirror::Class> loader_class = class_loader->GetClass(); |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 2993 | const char* loader_class_name = |
| 2994 | loader_class->GetDexFile().StringByTypeIdx(loader_class->GetDexTypeIndex()); |
| 2995 | LOG(WARNING) << "Initiating class loader of type " << DescriptorToDot(loader_class_name) |
| 2996 | << " is not well-behaved; it returned a different Class for racing loadClass(\"" |
| 2997 | << DescriptorToDot(descriptor) << "\")."; |
| 2998 | return EnsureResolved(self, descriptor, old); |
| 2999 | } |
| 3000 | if (UNLIKELY(!descriptor_equals)) { |
| 3001 | std::string result_storage; |
| 3002 | const char* result_name = result_ptr->GetDescriptor(&result_storage); |
| 3003 | std::string loader_storage; |
| 3004 | const char* loader_class_name = class_loader->GetClass()->GetDescriptor(&loader_storage); |
| 3005 | ThrowNoClassDefFoundError( |
| 3006 | "Initiating class loader of type %s returned class %s instead of %s.", |
| 3007 | DescriptorToDot(loader_class_name).c_str(), |
| 3008 | DescriptorToDot(result_name).c_str(), |
| 3009 | DescriptorToDot(descriptor).c_str()); |
| 3010 | return nullptr; |
| 3011 | } |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 3012 | // Success. |
| 3013 | return result_ptr; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3014 | } |
| 3015 | |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 3016 | // Helper for maintaining DefineClass counting. We need to notify callbacks when we start/end a |
| 3017 | // define-class and how many recursive DefineClasses we are at in order to allow for doing things |
| 3018 | // like pausing class definition. |
| 3019 | struct ScopedDefiningClass { |
| 3020 | public: |
| 3021 | explicit ScopedDefiningClass(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_) |
| 3022 | : self_(self), returned_(false) { |
| 3023 | Locks::mutator_lock_->AssertSharedHeld(self_); |
| 3024 | Runtime::Current()->GetRuntimeCallbacks()->BeginDefineClass(); |
| 3025 | self_->IncrDefineClassCount(); |
| 3026 | } |
| 3027 | ~ScopedDefiningClass() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 3028 | Locks::mutator_lock_->AssertSharedHeld(self_); |
| 3029 | CHECK(returned_); |
| 3030 | } |
| 3031 | |
| 3032 | ObjPtr<mirror::Class> Finish(Handle<mirror::Class> h_klass) |
| 3033 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 3034 | CHECK(!returned_); |
| 3035 | self_->DecrDefineClassCount(); |
| 3036 | Runtime::Current()->GetRuntimeCallbacks()->EndDefineClass(); |
| 3037 | Thread::PoisonObjectPointersIfDebug(); |
| 3038 | returned_ = true; |
| 3039 | return h_klass.Get(); |
| 3040 | } |
| 3041 | |
| 3042 | ObjPtr<mirror::Class> Finish(ObjPtr<mirror::Class> klass) |
| 3043 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 3044 | StackHandleScope<1> hs(self_); |
| 3045 | Handle<mirror::Class> h_klass(hs.NewHandle(klass)); |
| 3046 | return Finish(h_klass); |
| 3047 | } |
| 3048 | |
| 3049 | ObjPtr<mirror::Class> Finish(nullptr_t np ATTRIBUTE_UNUSED) |
| 3050 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 3051 | ScopedNullHandle<mirror::Class> snh; |
| 3052 | return Finish(snh); |
| 3053 | } |
| 3054 | |
| 3055 | private: |
| 3056 | Thread* self_; |
| 3057 | bool returned_; |
| 3058 | }; |
| 3059 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 3060 | ObjPtr<mirror::Class> ClassLinker::DefineClass(Thread* self, |
| 3061 | const char* descriptor, |
| 3062 | size_t hash, |
| 3063 | Handle<mirror::ClassLoader> class_loader, |
| 3064 | const DexFile& dex_file, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 3065 | const dex::ClassDef& dex_class_def) { |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 3066 | ScopedDefiningClass sdc(self); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3067 | StackHandleScope<3> hs(self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3068 | auto klass = hs.NewHandle<mirror::Class>(nullptr); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3069 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3070 | // Load the class from the dex file. |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3071 | if (UNLIKELY(!init_done_)) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3072 | // finish up init of hand crafted class_roots_ |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3073 | if (strcmp(descriptor, "Ljava/lang/Object;") == 0) { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 3074 | klass.Assign(GetClassRoot<mirror::Object>(this)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3075 | } else if (strcmp(descriptor, "Ljava/lang/Class;") == 0) { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 3076 | klass.Assign(GetClassRoot<mirror::Class>(this)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3077 | } else if (strcmp(descriptor, "Ljava/lang/String;") == 0) { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 3078 | klass.Assign(GetClassRoot<mirror::String>(this)); |
Fred Shih | 4ee7a66 | 2014-07-11 09:59:27 -0700 | [diff] [blame] | 3079 | } else if (strcmp(descriptor, "Ljava/lang/ref/Reference;") == 0) { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 3080 | klass.Assign(GetClassRoot<mirror::Reference>(this)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 3081 | } else if (strcmp(descriptor, "Ljava/lang/DexCache;") == 0) { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 3082 | klass.Assign(GetClassRoot<mirror::DexCache>(this)); |
Alex Light | d625158 | 2016-10-31 11:12:30 -0700 | [diff] [blame] | 3083 | } else if (strcmp(descriptor, "Ldalvik/system/ClassExt;") == 0) { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 3084 | klass.Assign(GetClassRoot<mirror::ClassExt>(this)); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3085 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3086 | } |
| 3087 | |
Vladimir Marko | b9c29f6 | 2019-03-20 14:22:51 +0000 | [diff] [blame] | 3088 | // For AOT-compilation of an app, we may use a shortened boot class path that excludes |
| 3089 | // some runtime modules. Prevent definition of classes in app class loader that could clash |
| 3090 | // with these modules as these classes could be resolved differently during execution. |
| 3091 | if (class_loader != nullptr && |
| 3092 | Runtime::Current()->IsAotCompiler() && |
Vladimir Marko | d1f7351 | 2020-04-02 10:50:35 +0100 | [diff] [blame] | 3093 | IsUpdatableBootClassPathDescriptor(descriptor)) { |
Vladimir Marko | b9c29f6 | 2019-03-20 14:22:51 +0000 | [diff] [blame] | 3094 | ObjPtr<mirror::Throwable> pre_allocated = |
| 3095 | Runtime::Current()->GetPreAllocatedNoClassDefFoundError(); |
| 3096 | self->SetException(pre_allocated); |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 3097 | return sdc.Finish(nullptr); |
Vladimir Marko | b9c29f6 | 2019-03-20 14:22:51 +0000 | [diff] [blame] | 3098 | } |
| 3099 | |
Calin Juravle | 3378768 | 2019-07-26 14:27:18 -0700 | [diff] [blame] | 3100 | // For AOT-compilation of an app, we may use only a public SDK to resolve symbols. If the SDK |
| 3101 | // checks are configured (a non null SdkChecker) and the descriptor is not in the provided |
| 3102 | // public class path then we prevent the definition of the class. |
| 3103 | // |
| 3104 | // NOTE that we only do the checks for the boot classpath APIs. Anything else, like the app |
| 3105 | // classpath is not checked. |
| 3106 | if (class_loader == nullptr && |
| 3107 | Runtime::Current()->IsAotCompiler() && |
| 3108 | DenyAccessBasedOnPublicSdk(descriptor)) { |
| 3109 | ObjPtr<mirror::Throwable> pre_allocated = |
| 3110 | Runtime::Current()->GetPreAllocatedNoClassDefFoundError(); |
| 3111 | self->SetException(pre_allocated); |
| 3112 | return sdc.Finish(nullptr); |
| 3113 | } |
| 3114 | |
Alex Light | e9f6103 | 2018-09-24 16:04:51 -0700 | [diff] [blame] | 3115 | // This is to prevent the calls to ClassLoad and ClassPrepare which can cause java/user-supplied |
| 3116 | // code to be executed. We put it up here so we can avoid all the allocations associated with |
| 3117 | // creating the class. This can happen with (eg) jit threads. |
| 3118 | if (!self->CanLoadClasses()) { |
| 3119 | // Make sure we don't try to load anything, potentially causing an infinite loop. |
| 3120 | ObjPtr<mirror::Throwable> pre_allocated = |
| 3121 | Runtime::Current()->GetPreAllocatedNoClassDefFoundError(); |
| 3122 | self->SetException(pre_allocated); |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 3123 | return sdc.Finish(nullptr); |
Alex Light | e9f6103 | 2018-09-24 16:04:51 -0700 | [diff] [blame] | 3124 | } |
| 3125 | |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3126 | if (klass == nullptr) { |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3127 | // Allocate a class with the status of not ready. |
| 3128 | // Interface object should get the right size here. Regular class will |
| 3129 | // figure out the right size later and be replaced with one of the right |
| 3130 | // size when the class becomes resolved. |
Chang Xing | 0c2c222 | 2017-08-04 14:36:17 -0700 | [diff] [blame] | 3131 | if (CanAllocClass()) { |
| 3132 | klass.Assign(AllocClass(self, SizeOfClassWithoutEmbeddedTables(dex_file, dex_class_def))); |
| 3133 | } else { |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 3134 | return sdc.Finish(nullptr); |
Chang Xing | 0c2c222 | 2017-08-04 14:36:17 -0700 | [diff] [blame] | 3135 | } |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3136 | } |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3137 | if (UNLIKELY(klass == nullptr)) { |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3138 | self->AssertPendingOOMException(); |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 3139 | return sdc.Finish(nullptr); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 3140 | } |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 3141 | // Get the real dex file. This will return the input if there aren't any callbacks or they do |
| 3142 | // nothing. |
| 3143 | DexFile const* new_dex_file = nullptr; |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 3144 | dex::ClassDef const* new_class_def = nullptr; |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 3145 | // TODO We should ideally figure out some way to move this after we get a lock on the klass so it |
| 3146 | // will only be called once. |
| 3147 | Runtime::Current()->GetRuntimeCallbacks()->ClassPreDefine(descriptor, |
| 3148 | klass, |
| 3149 | class_loader, |
| 3150 | dex_file, |
| 3151 | dex_class_def, |
| 3152 | &new_dex_file, |
| 3153 | &new_class_def); |
Alex Light | 440b5d9 | 2017-01-24 15:32:25 -0800 | [diff] [blame] | 3154 | // Check to see if an exception happened during runtime callbacks. Return if so. |
| 3155 | if (self->IsExceptionPending()) { |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 3156 | return sdc.Finish(nullptr); |
Alex Light | 440b5d9 | 2017-01-24 15:32:25 -0800 | [diff] [blame] | 3157 | } |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 3158 | ObjPtr<mirror::DexCache> dex_cache = RegisterDexFile(*new_dex_file, class_loader.Get()); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3159 | if (dex_cache == nullptr) { |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3160 | self->AssertPendingException(); |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 3161 | return sdc.Finish(nullptr); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3162 | } |
| 3163 | klass->SetDexCache(dex_cache); |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 3164 | SetupClass(*new_dex_file, *new_class_def, klass, class_loader.Get()); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3165 | |
Jeff Hao | 848f70a | 2014-01-15 13:49:50 -0800 | [diff] [blame] | 3166 | // Mark the string class by setting its access flag. |
| 3167 | if (UNLIKELY(!init_done_)) { |
| 3168 | if (strcmp(descriptor, "Ljava/lang/String;") == 0) { |
| 3169 | klass->SetStringClass(); |
| 3170 | } |
| 3171 | } |
| 3172 | |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 3173 | ObjectLock<mirror::Class> lock(self, klass); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3174 | klass->SetClinitThreadId(self->GetTid()); |
Mathieu Chartier | 1e4841e | 2016-12-15 14:21:04 -0800 | [diff] [blame] | 3175 | // 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] | 3176 | klass->SetIfTable(GetClassRoot<mirror::Object>(this)->GetIfTable()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3177 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3178 | // Add the newly loaded class to the loaded classes table. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3179 | ObjPtr<mirror::Class> existing = InsertClass(descriptor, klass.Get(), hash); |
Ian Rogers | c114b5f | 2014-07-21 08:55:01 -0700 | [diff] [blame] | 3180 | if (existing != nullptr) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3181 | // We failed to insert because we raced with another thread. Calling EnsureResolved may cause |
| 3182 | // this thread to block. |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 3183 | return sdc.Finish(EnsureResolved(self, descriptor, existing)); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3184 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3185 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3186 | // Load the fields and other things after we are inserted in the table. This is so that we don't |
| 3187 | // end up allocating unfree-able linear alloc resources and then lose the race condition. The |
| 3188 | // other reason is that the field roots are only visited from the class table. So we need to be |
| 3189 | // inserted before we allocate / fill in these fields. |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 3190 | LoadClass(self, *new_dex_file, *new_class_def, klass); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3191 | if (self->IsExceptionPending()) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 3192 | VLOG(class_linker) << self->GetException()->Dump(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3193 | // An exception occured during load, set status to erroneous while holding klass' lock in case |
| 3194 | // notification is necessary. |
| 3195 | if (!klass->IsErroneous()) { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 3196 | mirror::Class::SetStatus(klass, ClassStatus::kErrorUnresolved, self); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3197 | } |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 3198 | return sdc.Finish(nullptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3199 | } |
| 3200 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3201 | // Finish loading (if necessary) by finding parents |
| 3202 | CHECK(!klass->IsLoaded()); |
Alex Light | b0f1192 | 2017-01-23 14:25:17 -0800 | [diff] [blame] | 3203 | if (!LoadSuperAndInterfaces(klass, *new_dex_file)) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3204 | // Loading failed. |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 3205 | if (!klass->IsErroneous()) { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 3206 | mirror::Class::SetStatus(klass, ClassStatus::kErrorUnresolved, self); |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 3207 | } |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 3208 | return sdc.Finish(nullptr); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3209 | } |
| 3210 | CHECK(klass->IsLoaded()); |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 3211 | |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 3212 | // At this point the class is loaded. Publish a ClassLoad event. |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 3213 | // 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] | 3214 | Runtime::Current()->GetRuntimeCallbacks()->ClassLoad(klass); |
Andreas Gampe | 0f01b58 | 2017-01-18 15:22:37 -0800 | [diff] [blame] | 3215 | |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3216 | // Link the class (if necessary) |
| 3217 | CHECK(!klass->IsResolved()); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 3218 | // TODO: Use fast jobjects? |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3219 | auto interfaces = hs.NewHandle<mirror::ObjectArray<mirror::Class>>(nullptr); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3220 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 3221 | MutableHandle<mirror::Class> h_new_class = hs.NewHandle<mirror::Class>(nullptr); |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 3222 | if (!LinkClass(self, descriptor, klass, interfaces, &h_new_class)) { |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3223 | // Linking failed. |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 3224 | if (!klass->IsErroneous()) { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 3225 | mirror::Class::SetStatus(klass, ClassStatus::kErrorUnresolved, self); |
Ian Rogers | ecd4d9a | 2014-07-22 00:59:52 -0700 | [diff] [blame] | 3226 | } |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 3227 | return sdc.Finish(nullptr); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 3228 | } |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 3229 | self->AssertNoPendingException(); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3230 | CHECK(h_new_class != nullptr) << descriptor; |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 3231 | CHECK(h_new_class->IsResolved() && !h_new_class->IsErroneousResolved()) << descriptor; |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 3232 | |
Sebastien Hertz | a8a697f | 2015-01-15 12:28:47 +0100 | [diff] [blame] | 3233 | // Instrumentation may have updated entrypoints for all methods of all |
| 3234 | // classes. However it could not update methods of this class while we |
| 3235 | // were loading it. Now the class is resolved, we can update entrypoints |
| 3236 | // as required by instrumentation. |
| 3237 | if (Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled()) { |
| 3238 | // We must be in the kRunnable state to prevent instrumentation from |
| 3239 | // suspending all threads to update entrypoints while we are doing it |
| 3240 | // for this class. |
| 3241 | DCHECK_EQ(self->GetState(), kRunnable); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 3242 | Runtime::Current()->GetInstrumentation()->InstallStubsForClass(h_new_class.Get()); |
Sebastien Hertz | a8a697f | 2015-01-15 12:28:47 +0100 | [diff] [blame] | 3243 | } |
| 3244 | |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 3245 | /* |
| 3246 | * We send CLASS_PREPARE events to the debugger from here. The |
| 3247 | * definition of "preparation" is creating the static fields for a |
| 3248 | * class and initializing them to the standard default values, but not |
| 3249 | * executing any code (that comes later, during "initialization"). |
| 3250 | * |
| 3251 | * We did the static preparation in LinkClass. |
| 3252 | * |
| 3253 | * The class has been prepared and resolved but possibly not yet verified |
| 3254 | * at this point. |
| 3255 | */ |
Andreas Gampe | ac30fa2 | 2017-01-18 21:02:36 -0800 | [diff] [blame] | 3256 | Runtime::Current()->GetRuntimeCallbacks()->ClassPrepare(klass, h_new_class); |
Elliott Hughes | 4740cdf | 2011-12-07 14:07:12 -0800 | [diff] [blame] | 3257 | |
Tamas Berghammer | 160e6df | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 3258 | // Notify native debugger of the new class and its layout. |
| 3259 | jit::Jit::NewTypeLoadedIfUsingJit(h_new_class.Get()); |
| 3260 | |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 3261 | return sdc.Finish(h_new_class); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 3262 | } |
| 3263 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 3264 | uint32_t ClassLinker::SizeOfClassWithoutEmbeddedTables(const DexFile& dex_file, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 3265 | const dex::ClassDef& dex_class_def) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 3266 | size_t num_ref = 0; |
Fred Shih | 37f05ef | 2014-07-16 18:38:08 -0700 | [diff] [blame] | 3267 | size_t num_8 = 0; |
| 3268 | size_t num_16 = 0; |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 3269 | size_t num_32 = 0; |
| 3270 | size_t num_64 = 0; |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3271 | ClassAccessor accessor(dex_file, dex_class_def); |
| 3272 | // We allow duplicate definitions of the same field in a class_data_item |
| 3273 | // but ignore the repeated indexes here, b/21868015. |
| 3274 | uint32_t last_field_idx = dex::kDexNoIndex; |
| 3275 | for (const ClassAccessor::Field& field : accessor.GetStaticFields()) { |
| 3276 | uint32_t field_idx = field.GetIndex(); |
| 3277 | // Ordering enforced by DexFileVerifier. |
| 3278 | DCHECK(last_field_idx == dex::kDexNoIndex || last_field_idx <= field_idx); |
| 3279 | if (UNLIKELY(field_idx == last_field_idx)) { |
| 3280 | continue; |
| 3281 | } |
| 3282 | last_field_idx = field_idx; |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 3283 | const dex::FieldId& field_id = dex_file.GetFieldId(field_idx); |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3284 | const char* descriptor = dex_file.GetFieldTypeDescriptor(field_id); |
| 3285 | char c = descriptor[0]; |
| 3286 | switch (c) { |
| 3287 | case 'L': |
| 3288 | case '[': |
| 3289 | num_ref++; |
| 3290 | break; |
| 3291 | case 'J': |
| 3292 | case 'D': |
| 3293 | num_64++; |
| 3294 | break; |
| 3295 | case 'I': |
| 3296 | case 'F': |
| 3297 | num_32++; |
| 3298 | break; |
| 3299 | case 'S': |
| 3300 | case 'C': |
| 3301 | num_16++; |
| 3302 | break; |
| 3303 | case 'B': |
| 3304 | case 'Z': |
| 3305 | num_8++; |
| 3306 | break; |
| 3307 | default: |
| 3308 | LOG(FATAL) << "Unknown descriptor: " << c; |
| 3309 | UNREACHABLE(); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 3310 | } |
| 3311 | } |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3312 | return mirror::Class::ComputeClassSize(false, |
| 3313 | 0, |
| 3314 | num_8, |
| 3315 | num_16, |
| 3316 | num_32, |
| 3317 | num_64, |
| 3318 | num_ref, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3319 | image_pointer_size_); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 3320 | } |
| 3321 | |
Alex Light | fc49fec | 2018-01-16 22:28:36 +0000 | [diff] [blame] | 3322 | // Special case to get oat code without overwriting a trampoline. |
| 3323 | const void* ClassLinker::GetQuickOatCodeFor(ArtMethod* method) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3324 | CHECK(method->IsInvokable()) << method->PrettyMethod(); |
Nicolas Geoffray | a7a4759 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 3325 | if (method->IsProxyMethod()) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 3326 | return GetQuickProxyInvokeHandler(); |
Jeff Hao | 8df6cea | 2013-07-29 13:54:48 -0700 | [diff] [blame] | 3327 | } |
Nicolas Geoffray | 3238440 | 2019-07-17 20:06:44 +0100 | [diff] [blame] | 3328 | const void* code = method->GetOatMethodQuickCode(GetImagePointerSize()); |
Alex Light | fc49fec | 2018-01-16 22:28:36 +0000 | [diff] [blame] | 3329 | if (code != nullptr) { |
| 3330 | return code; |
Mathieu Chartier | 2535abe | 2015-02-17 10:38:49 -0800 | [diff] [blame] | 3331 | } |
Nicolas Geoffray | 3238440 | 2019-07-17 20:06:44 +0100 | [diff] [blame] | 3332 | |
| 3333 | jit::Jit* jit = Runtime::Current()->GetJit(); |
| 3334 | if (jit != nullptr) { |
| 3335 | code = jit->GetCodeCache()->GetSavedEntryPointOfPreCompiledMethod(method); |
| 3336 | if (code != nullptr) { |
| 3337 | return code; |
| 3338 | } |
| 3339 | } |
| 3340 | |
Alex Light | fc49fec | 2018-01-16 22:28:36 +0000 | [diff] [blame] | 3341 | if (method->IsNative()) { |
| 3342 | // No code and native? Use generic trampoline. |
| 3343 | return GetQuickGenericJniStub(); |
| 3344 | } |
Nicolas Geoffray | 0039182 | 2019-12-10 10:17:23 +0000 | [diff] [blame] | 3345 | |
Nicolas Geoffray | c39af94 | 2021-01-25 08:43:57 +0000 | [diff] [blame] | 3346 | if (interpreter::CanRuntimeUseNterp() && CanMethodUseNterp(method)) { |
Nicolas Geoffray | 0039182 | 2019-12-10 10:17:23 +0000 | [diff] [blame] | 3347 | return interpreter::GetNterpEntryPoint(); |
| 3348 | } |
| 3349 | |
Alex Light | fc49fec | 2018-01-16 22:28:36 +0000 | [diff] [blame] | 3350 | return GetQuickToInterpreterBridge(); |
TDYa127 | 8532191 | 2012-04-01 15:24:56 -0700 | [diff] [blame] | 3351 | } |
| 3352 | |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 3353 | bool ClassLinker::ShouldUseInterpreterEntrypoint(ArtMethod* method, const void* quick_code) { |
Alex Light | 2d441b1 | 2018-06-08 15:33:21 -0700 | [diff] [blame] | 3354 | ScopedAssertNoThreadSuspension sants(__FUNCTION__); |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 3355 | if (UNLIKELY(method->IsNative() || method->IsProxyMethod())) { |
| 3356 | return false; |
| 3357 | } |
| 3358 | |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 3359 | if (quick_code == nullptr) { |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 3360 | return true; |
| 3361 | } |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 3362 | |
| 3363 | Runtime* runtime = Runtime::Current(); |
| 3364 | instrumentation::Instrumentation* instr = runtime->GetInstrumentation(); |
| 3365 | if (instr->InterpretOnly()) { |
| 3366 | return true; |
| 3367 | } |
| 3368 | |
| 3369 | if (runtime->GetClassLinker()->IsQuickToInterpreterBridge(quick_code)) { |
| 3370 | // Doing this check avoids doing compiled/interpreter transitions. |
| 3371 | return true; |
| 3372 | } |
| 3373 | |
Alex Light | fc58809 | 2020-01-23 15:39:08 -0800 | [diff] [blame] | 3374 | if (Thread::Current()->IsForceInterpreter()) { |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 3375 | // Force the use of interpreter when it is required by the debugger. |
| 3376 | return true; |
| 3377 | } |
| 3378 | |
Alex Light | 8f34aba | 2017-10-09 13:46:32 -0700 | [diff] [blame] | 3379 | if (Thread::Current()->IsAsyncExceptionPending()) { |
| 3380 | // Force use of interpreter to handle async-exceptions |
| 3381 | return true; |
| 3382 | } |
| 3383 | |
Alex Light | 2d441b1 | 2018-06-08 15:33:21 -0700 | [diff] [blame] | 3384 | if (quick_code == GetQuickInstrumentationEntryPoint()) { |
| 3385 | const void* instr_target = instr->GetCodeForInvoke(method); |
| 3386 | DCHECK_NE(instr_target, GetQuickInstrumentationEntryPoint()) << method->PrettyMethod(); |
| 3387 | return ShouldUseInterpreterEntrypoint(method, instr_target); |
| 3388 | } |
| 3389 | |
Nicolas Geoffray | 433b79a | 2017-01-30 20:54:45 +0000 | [diff] [blame] | 3390 | if (runtime->IsJavaDebuggable()) { |
| 3391 | // For simplicity, we ignore precompiled code and go to the interpreter |
| 3392 | // assuming we don't already have jitted code. |
| 3393 | // We could look at the oat file where `quick_code` is being defined, |
| 3394 | // and check whether it's been compiled debuggable, but we decided to |
| 3395 | // only rely on the JIT for debuggable apps. |
Alex Light | 6b16d89 | 2016-11-11 11:21:04 -0800 | [diff] [blame] | 3396 | jit::Jit* jit = Runtime::Current()->GetJit(); |
| 3397 | return (jit == nullptr) || !jit->GetCodeCache()->ContainsPc(quick_code); |
| 3398 | } |
| 3399 | |
Nicolas Geoffray | c9de61c | 2018-11-27 17:34:31 +0000 | [diff] [blame] | 3400 | if (runtime->IsNativeDebuggable()) { |
Calin Juravle | e5de54c | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 3401 | DCHECK(runtime->UseJitCompilation() && runtime->GetJit()->JitAtFirstUse()); |
David Srbecky | f448016 | 2016-03-16 00:06:24 +0000 | [diff] [blame] | 3402 | // If we are doing native debugging, ignore application's AOT code, |
Nicolas Geoffray | 433b79a | 2017-01-30 20:54:45 +0000 | [diff] [blame] | 3403 | // since we want to JIT it (at first use) with extra stackmaps for native |
| 3404 | // debugging. We keep however all AOT code from the boot image, |
| 3405 | // since the JIT-at-first-use is blocking and would result in non-negligible |
| 3406 | // startup performance impact. |
David Srbecky | f448016 | 2016-03-16 00:06:24 +0000 | [diff] [blame] | 3407 | return !runtime->GetHeap()->IsInBootImageOatFile(quick_code); |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 3408 | } |
| 3409 | |
| 3410 | return false; |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 3411 | } |
| 3412 | |
Vladimir Marko | 86c8752 | 2020-05-11 16:55:55 +0100 | [diff] [blame] | 3413 | void ClassLinker::FixupStaticTrampolines(Thread* self, ObjPtr<mirror::Class> klass) { |
Alex Light | 2d441b1 | 2018-06-08 15:33:21 -0700 | [diff] [blame] | 3414 | ScopedAssertNoThreadSuspension sants(__FUNCTION__); |
Vladimir Marko | cce414f | 2019-10-07 08:51:33 +0100 | [diff] [blame] | 3415 | DCHECK(klass->IsVisiblyInitialized()) << klass->PrettyDescriptor(); |
Vladimir Marko | 86c8752 | 2020-05-11 16:55:55 +0100 | [diff] [blame] | 3416 | size_t num_direct_methods = klass->NumDirectMethods(); |
| 3417 | if (num_direct_methods == 0) { |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 3418 | return; // No direct methods => no static methods. |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3419 | } |
Vladimir Marko | cce414f | 2019-10-07 08:51:33 +0100 | [diff] [blame] | 3420 | if (UNLIKELY(klass->IsProxyClass())) { |
| 3421 | return; |
| 3422 | } |
Vladimir Marko | 86c8752 | 2020-05-11 16:55:55 +0100 | [diff] [blame] | 3423 | PointerSize pointer_size = image_pointer_size_; |
| 3424 | if (std::any_of(klass->GetDirectMethods(pointer_size).begin(), |
| 3425 | klass->GetDirectMethods(pointer_size).end(), |
| 3426 | [](const ArtMethod& m) { return m.IsCriticalNative(); })) { |
| 3427 | // Store registered @CriticalNative methods, if any, to JNI entrypoints. |
| 3428 | // Direct methods are a contiguous chunk of memory, so use the ordering of the map. |
| 3429 | ArtMethod* first_method = klass->GetDirectMethod(0u, pointer_size); |
| 3430 | ArtMethod* last_method = klass->GetDirectMethod(num_direct_methods - 1u, pointer_size); |
| 3431 | MutexLock lock(self, critical_native_code_with_clinit_check_lock_); |
| 3432 | auto lb = critical_native_code_with_clinit_check_.lower_bound(first_method); |
| 3433 | while (lb != critical_native_code_with_clinit_check_.end() && lb->first <= last_method) { |
| 3434 | lb->first->SetEntryPointFromJni(lb->second); |
| 3435 | lb = critical_native_code_with_clinit_check_.erase(lb); |
| 3436 | } |
| 3437 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3438 | Runtime* runtime = Runtime::Current(); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 3439 | if (!runtime->IsStarted()) { |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 3440 | if (runtime->IsAotCompiler() || runtime->GetHeap()->HasBootImageSpace()) { |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 3441 | return; // OAT file unavailable. |
| 3442 | } |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3443 | } |
Alex Light | 64ad14d | 2014-08-19 14:23:13 -0700 | [diff] [blame] | 3444 | |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 3445 | const DexFile& dex_file = klass->GetDexFile(); |
Ian Rogers | 97b52f8 | 2014-08-14 11:34:07 -0700 | [diff] [blame] | 3446 | bool has_oat_class; |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3447 | OatFile::OatClass oat_class = OatFile::FindOatClass(dex_file, |
| 3448 | klass->GetDexClassDefIndex(), |
| 3449 | &has_oat_class); |
Ian Rogers | 1c82982 | 2013-09-30 18:18:50 -0700 | [diff] [blame] | 3450 | // Link the code of methods skipped by LinkCode. |
Vladimir Marko | 86c8752 | 2020-05-11 16:55:55 +0100 | [diff] [blame] | 3451 | for (size_t method_index = 0; method_index < num_direct_methods; ++method_index) { |
| 3452 | ArtMethod* method = klass->GetDirectMethod(method_index, pointer_size); |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 3453 | if (!method->IsStatic()) { |
| 3454 | // Only update static methods. |
| 3455 | continue; |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3456 | } |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 3457 | const void* quick_code = nullptr; |
Nicolas Geoffray | 0039182 | 2019-12-10 10:17:23 +0000 | [diff] [blame] | 3458 | |
| 3459 | // In order: |
| 3460 | // 1) Check if we have AOT Code. |
| 3461 | // 2) Check if we have JIT Code. |
| 3462 | // 3) Check if we can use Nterp. |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 3463 | if (has_oat_class) { |
| 3464 | OatFile::OatMethod oat_method = oat_class.GetOatMethod(method_index); |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 3465 | quick_code = oat_method.GetQuickCode(); |
| 3466 | } |
Nicolas Geoffray | 0039182 | 2019-12-10 10:17:23 +0000 | [diff] [blame] | 3467 | |
Vladimir Marko | cce414f | 2019-10-07 08:51:33 +0100 | [diff] [blame] | 3468 | jit::Jit* jit = runtime->GetJit(); |
Nicolas Geoffray | 3238440 | 2019-07-17 20:06:44 +0100 | [diff] [blame] | 3469 | if (quick_code == nullptr && jit != nullptr) { |
| 3470 | quick_code = jit->GetCodeCache()->GetSavedEntryPointOfPreCompiledMethod(method); |
Nicolas Geoffray | 7989ac9 | 2019-04-10 12:42:30 +0100 | [diff] [blame] | 3471 | } |
Nicolas Geoffray | 0039182 | 2019-12-10 10:17:23 +0000 | [diff] [blame] | 3472 | |
| 3473 | if (quick_code == nullptr && |
| 3474 | interpreter::CanRuntimeUseNterp() && |
Nicolas Geoffray | c39af94 | 2021-01-25 08:43:57 +0000 | [diff] [blame] | 3475 | CanMethodUseNterp(method)) { |
Nicolas Geoffray | 0039182 | 2019-12-10 10:17:23 +0000 | [diff] [blame] | 3476 | quick_code = interpreter::GetNterpEntryPoint(); |
| 3477 | } |
| 3478 | |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 3479 | // Check whether the method is native, in which case it's generic JNI. |
Ulya Trafimovich | 5439f05 | 2020-07-29 10:03:46 +0100 | [diff] [blame] | 3480 | if (quick_code == nullptr && method->IsNative()) { |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 3481 | quick_code = GetQuickGenericJniStub(); |
| 3482 | } else if (ShouldUseInterpreterEntrypoint(method, quick_code)) { |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 3483 | // Use interpreter entry point. |
Nicolas Geoffray | 0039182 | 2019-12-10 10:17:23 +0000 | [diff] [blame] | 3484 | if (IsQuickToInterpreterBridge(method->GetEntryPointFromQuickCompiledCode())) { |
| 3485 | // If we have the trampoline or the bridge already, no need to update. |
| 3486 | // This saves in not dirtying boot image memory. |
| 3487 | continue; |
| 3488 | } |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 3489 | quick_code = GetQuickToInterpreterBridge(); |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 3490 | } |
Nicolas Geoffray | 0039182 | 2019-12-10 10:17:23 +0000 | [diff] [blame] | 3491 | CHECK(quick_code != nullptr); |
Elliott Hughes | 956af0f | 2014-12-11 14:34:28 -0800 | [diff] [blame] | 3492 | runtime->GetInstrumentation()->UpdateMethodsCode(method, quick_code); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3493 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3494 | // Ignore virtual methods on the iterator. |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3495 | } |
| 3496 | |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3497 | // Does anything needed to make sure that the compiler will not generate a direct invoke to this |
| 3498 | // method. Should only be called on non-invokable methods. |
Nicolas Geoffray | f05f04b | 2019-10-31 11:50:41 +0000 | [diff] [blame] | 3499 | inline void EnsureThrowsInvocationError(ClassLinker* class_linker, ArtMethod* method) |
| 3500 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 3501 | DCHECK(method != nullptr); |
| 3502 | DCHECK(!method->IsInvokable()); |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3503 | method->SetEntryPointFromQuickCompiledCodePtrSize( |
| 3504 | class_linker->GetQuickToInterpreterBridgeTrampoline(), |
| 3505 | class_linker->GetImagePointerSize()); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 3506 | } |
| 3507 | |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3508 | static void LinkCode(ClassLinker* class_linker, |
| 3509 | ArtMethod* method, |
| 3510 | const OatFile::OatClass* oat_class, |
| 3511 | uint32_t class_def_method_index) REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | 2d441b1 | 2018-06-08 15:33:21 -0700 | [diff] [blame] | 3512 | ScopedAssertNoThreadSuspension sants(__FUNCTION__); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3513 | Runtime* const runtime = Runtime::Current(); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 3514 | if (runtime->IsAotCompiler()) { |
Nicolas Geoffray | 4fcdc94 | 2014-07-22 10:48:00 +0100 | [diff] [blame] | 3515 | // The following code only applies to a non-compiler runtime. |
| 3516 | return; |
| 3517 | } |
Nicolas Geoffray | 5ee206f | 2019-10-08 15:09:17 +0100 | [diff] [blame] | 3518 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3519 | // Method shouldn't have already been linked. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 3520 | DCHECK(method->GetEntryPointFromQuickCompiledCode() == nullptr); |
Jeff Hao | 1674363 | 2013-05-08 10:59:04 -0700 | [diff] [blame] | 3521 | |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 3522 | if (!method->IsInvokable()) { |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 3523 | EnsureThrowsInvocationError(class_linker, method); |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 3524 | return; |
| 3525 | } |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 3526 | |
Nicolas Geoffray | 5ee206f | 2019-10-08 15:09:17 +0100 | [diff] [blame] | 3527 | const void* quick_code = nullptr; |
| 3528 | if (oat_class != nullptr) { |
| 3529 | // Every kind of method should at least get an invoke stub from the oat_method. |
| 3530 | // non-abstract methods also get their code pointers. |
| 3531 | const OatFile::OatMethod oat_method = oat_class->GetOatMethod(class_def_method_index); |
| 3532 | quick_code = oat_method.GetQuickCode(); |
| 3533 | } |
| 3534 | |
| 3535 | bool enter_interpreter = class_linker->ShouldUseInterpreterEntrypoint(method, quick_code); |
| 3536 | |
| 3537 | // Note: this mimics the logic in image_writer.cc that installs the resolution |
| 3538 | // stub only if we have compiled code and the method needs a class initialization |
| 3539 | // check. |
Ulya Trafimovich | 5439f05 | 2020-07-29 10:03:46 +0100 | [diff] [blame] | 3540 | if (quick_code == nullptr) { |
Nicolas Geoffray | 5ee206f | 2019-10-08 15:09:17 +0100 | [diff] [blame] | 3541 | method->SetEntryPointFromQuickCompiledCode( |
| 3542 | method->IsNative() ? GetQuickGenericJniStub() : GetQuickToInterpreterBridge()); |
| 3543 | } else if (enter_interpreter) { |
| 3544 | method->SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge()); |
Vladimir Marko | 5115a4d | 2019-10-17 14:56:47 +0100 | [diff] [blame] | 3545 | } else if (NeedsClinitCheckBeforeCall(method)) { |
| 3546 | DCHECK(!method->GetDeclaringClass()->IsVisiblyInitialized()); // Actually ClassStatus::Idx. |
| 3547 | // If we do have code but the method needs a class initialization check before calling |
| 3548 | // that code, install the resolution stub that will perform the check. |
Sebastien Hertz | 7d658cf | 2013-07-09 10:56:11 +0200 | [diff] [blame] | 3549 | // It will be replaced by the proper entry point by ClassLinker::FixupStaticTrampolines |
| 3550 | // after initializing class (see ClassLinker::InitializeClass method). |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 3551 | method->SetEntryPointFromQuickCompiledCode(GetQuickResolutionStub()); |
Nicolas Geoffray | 5ee206f | 2019-10-08 15:09:17 +0100 | [diff] [blame] | 3552 | } else { |
| 3553 | method->SetEntryPointFromQuickCompiledCode(quick_code); |
Ian Rogers | 0d6de04 | 2012-02-29 08:50:26 -0800 | [diff] [blame] | 3554 | } |
jeffhao | 26c0a1a | 2012-01-17 16:28:33 -0800 | [diff] [blame] | 3555 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 3556 | if (method->IsNative()) { |
Vladimir Marko | 86c8752 | 2020-05-11 16:55:55 +0100 | [diff] [blame] | 3557 | // Set up the dlsym lookup stub. Do not go through `UnregisterNative()` |
| 3558 | // as the extra processing for @CriticalNative is not needed yet. |
| 3559 | method->SetEntryPointFromJni( |
| 3560 | method->IsCriticalNative() ? GetJniDlsymLookupCriticalStub() : GetJniDlsymLookupStub()); |
Andreas Gampe | 9054683 | 2014-03-12 18:07:19 -0700 | [diff] [blame] | 3561 | |
Tamas Berghammer | dd5e5e9 | 2016-02-12 16:29:00 +0000 | [diff] [blame] | 3562 | if (enter_interpreter || quick_code == nullptr) { |
Nicolas Geoffray | 5ee206f | 2019-10-08 15:09:17 +0100 | [diff] [blame] | 3563 | // We have a native method here without code. Then it should have the generic JNI |
| 3564 | // trampoline as entrypoint. |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 3565 | // TODO: this doesn't handle all the cases where trampolines may be installed. |
Nicolas Geoffray | 5ee206f | 2019-10-08 15:09:17 +0100 | [diff] [blame] | 3566 | DCHECK(class_linker->IsQuickGenericJniStub(method->GetEntryPointFromQuickCompiledCode())); |
Andreas Gampe | 9054683 | 2014-03-12 18:07:19 -0700 | [diff] [blame] | 3567 | } |
Brian Carlstrom | 92827a5 | 2011-10-10 15:50:01 -0700 | [diff] [blame] | 3568 | } |
| 3569 | } |
| 3570 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3571 | void ClassLinker::SetupClass(const DexFile& dex_file, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 3572 | const dex::ClassDef& dex_class_def, |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3573 | Handle<mirror::Class> klass, |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3574 | ObjPtr<mirror::ClassLoader> class_loader) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3575 | CHECK(klass != nullptr); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3576 | CHECK(klass->GetDexCache() != nullptr); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 3577 | CHECK_EQ(ClassStatus::kNotReady, klass->GetStatus()); |
Brian Carlstrom | f615a61 | 2011-07-23 12:50:34 -0700 | [diff] [blame] | 3578 | const char* descriptor = dex_file.GetClassDescriptor(dex_class_def); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3579 | CHECK(descriptor != nullptr); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3580 | |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 3581 | klass->SetClass(GetClassRoot<mirror::Class>(this)); |
Andreas Gampe | 5182932 | 2014-08-25 15:05:04 -0700 | [diff] [blame] | 3582 | uint32_t access_flags = dex_class_def.GetJavaAccessFlags(); |
Brian Carlstrom | 8e3fb14 | 2013-10-09 21:00:27 -0700 | [diff] [blame] | 3583 | CHECK_EQ(access_flags & ~kAccJavaFlagsMask, 0U); |
Vladimir Marko | b68bb7a | 2020-03-17 10:55:25 +0000 | [diff] [blame] | 3584 | klass->SetAccessFlagsDuringLinking(access_flags); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 3585 | klass->SetClassLoader(class_loader); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 3586 | DCHECK_EQ(klass->GetPrimitiveType(), Primitive::kPrimNot); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 3587 | mirror::Class::SetStatus(klass, ClassStatus::kIdx, nullptr); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3588 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 3589 | klass->SetDexClassDefIndex(dex_file.GetIndexForClassDef(dex_class_def)); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3590 | klass->SetDexTypeIndex(dex_class_def.class_idx_); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3591 | } |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3592 | |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3593 | LengthPrefixedArray<ArtField>* ClassLinker::AllocArtFieldArray(Thread* self, |
| 3594 | LinearAlloc* allocator, |
| 3595 | size_t length) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3596 | if (length == 0) { |
| 3597 | return nullptr; |
| 3598 | } |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 3599 | // If the ArtField alignment changes, review all uses of LengthPrefixedArray<ArtField>. |
| 3600 | static_assert(alignof(ArtField) == 4, "ArtField alignment is expected to be 4."); |
| 3601 | size_t storage_size = LengthPrefixedArray<ArtField>::ComputeSize(length); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3602 | void* array_storage = allocator->Alloc(self, storage_size); |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 3603 | auto* ret = new(array_storage) LengthPrefixedArray<ArtField>(length); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3604 | CHECK(ret != nullptr); |
| 3605 | std::uninitialized_fill_n(&ret->At(0), length, ArtField()); |
| 3606 | return ret; |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3607 | } |
| 3608 | |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3609 | LengthPrefixedArray<ArtMethod>* ClassLinker::AllocArtMethodArray(Thread* self, |
| 3610 | LinearAlloc* allocator, |
| 3611 | size_t length) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3612 | if (length == 0) { |
| 3613 | return nullptr; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3614 | } |
Vladimir Marko | 1463285 | 2015-08-17 12:07:23 +0100 | [diff] [blame] | 3615 | const size_t method_alignment = ArtMethod::Alignment(image_pointer_size_); |
| 3616 | const size_t method_size = ArtMethod::Size(image_pointer_size_); |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 3617 | const size_t storage_size = |
| 3618 | LengthPrefixedArray<ArtMethod>::ComputeSize(length, method_size, method_alignment); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3619 | void* array_storage = allocator->Alloc(self, storage_size); |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 3620 | auto* ret = new (array_storage) LengthPrefixedArray<ArtMethod>(length); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3621 | CHECK(ret != nullptr); |
| 3622 | for (size_t i = 0; i < length; ++i) { |
Vladimir Marko | cf36d49 | 2015-08-12 19:27:26 +0100 | [diff] [blame] | 3623 | new(reinterpret_cast<void*>(&ret->At(i, method_size, method_alignment))) ArtMethod; |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3624 | } |
| 3625 | return ret; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3626 | } |
| 3627 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3628 | LinearAlloc* ClassLinker::GetAllocatorForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3629 | if (class_loader == nullptr) { |
| 3630 | return Runtime::Current()->GetLinearAlloc(); |
| 3631 | } |
| 3632 | LinearAlloc* allocator = class_loader->GetAllocator(); |
| 3633 | DCHECK(allocator != nullptr); |
| 3634 | return allocator; |
| 3635 | } |
| 3636 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 3637 | LinearAlloc* ClassLinker::GetOrCreateAllocatorForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 3638 | if (class_loader == nullptr) { |
| 3639 | return Runtime::Current()->GetLinearAlloc(); |
| 3640 | } |
| 3641 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
| 3642 | LinearAlloc* allocator = class_loader->GetAllocator(); |
| 3643 | if (allocator == nullptr) { |
Mathieu Chartier | 5b83050 | 2016-03-02 10:30:23 -0800 | [diff] [blame] | 3644 | RegisterClassLoader(class_loader); |
| 3645 | allocator = class_loader->GetAllocator(); |
| 3646 | CHECK(allocator != nullptr); |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 3647 | } |
| 3648 | return allocator; |
| 3649 | } |
| 3650 | |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3651 | void ClassLinker::LoadClass(Thread* self, |
| 3652 | const DexFile& dex_file, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 3653 | const dex::ClassDef& dex_class_def, |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3654 | Handle<mirror::Class> klass) { |
David Brazdil | 20c765f | 2018-10-27 21:45:15 +0000 | [diff] [blame] | 3655 | ClassAccessor accessor(dex_file, |
| 3656 | dex_class_def, |
| 3657 | /* parse_hiddenapi_class_data= */ klass->IsBootStrapClassLoaded()); |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3658 | if (!accessor.HasClassData()) { |
| 3659 | return; |
| 3660 | } |
| 3661 | Runtime* const runtime = Runtime::Current(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3662 | { |
| 3663 | // Note: We cannot have thread suspension until the field and method arrays are setup or else |
| 3664 | // Class::VisitFieldRoots may miss some fields or methods. |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 3665 | ScopedAssertNoThreadSuspension nts(__FUNCTION__); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3666 | // Load static fields. |
Vladimir Marko | 23682bf | 2015-06-24 14:28:03 +0100 | [diff] [blame] | 3667 | // We allow duplicate definitions of the same field in a class_data_item |
| 3668 | // but ignore the repeated indexes here, b/21868015. |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3669 | LinearAlloc* const allocator = GetAllocatorForClassLoader(klass->GetClassLoader()); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3670 | LengthPrefixedArray<ArtField>* sfields = AllocArtFieldArray(self, |
| 3671 | allocator, |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3672 | accessor.NumStaticFields()); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 3673 | LengthPrefixedArray<ArtField>* ifields = AllocArtFieldArray(self, |
| 3674 | allocator, |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3675 | accessor.NumInstanceFields()); |
| 3676 | size_t num_sfields = 0u; |
Vladimir Marko | 23682bf | 2015-06-24 14:28:03 +0100 | [diff] [blame] | 3677 | size_t num_ifields = 0u; |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3678 | uint32_t last_static_field_idx = 0u; |
| 3679 | uint32_t last_instance_field_idx = 0u; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 3680 | |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3681 | // Methods |
| 3682 | bool has_oat_class = false; |
| 3683 | const OatFile::OatClass oat_class = (runtime->IsStarted() && !runtime->IsAotCompiler()) |
| 3684 | ? OatFile::FindOatClass(dex_file, klass->GetDexClassDefIndex(), &has_oat_class) |
| 3685 | : OatFile::OatClass::Invalid(); |
| 3686 | const OatFile::OatClass* oat_class_ptr = has_oat_class ? &oat_class : nullptr; |
| 3687 | klass->SetMethodsPtr( |
| 3688 | AllocArtMethodArray(self, allocator, accessor.NumMethods()), |
| 3689 | accessor.NumDirectMethods(), |
| 3690 | accessor.NumVirtualMethods()); |
| 3691 | size_t class_def_method_index = 0; |
| 3692 | uint32_t last_dex_method_index = dex::kDexNoIndex; |
| 3693 | size_t last_class_def_method_index = 0; |
| 3694 | |
| 3695 | // Use the visitor since the ranged based loops are bit slower from seeking. Seeking to the |
| 3696 | // methods needs to decode all of the fields. |
| 3697 | accessor.VisitFieldsAndMethods([&]( |
| 3698 | const ClassAccessor::Field& field) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 3699 | uint32_t field_idx = field.GetIndex(); |
| 3700 | DCHECK_GE(field_idx, last_static_field_idx); // Ordering enforced by DexFileVerifier. |
| 3701 | if (num_sfields == 0 || LIKELY(field_idx > last_static_field_idx)) { |
| 3702 | LoadField(field, klass, &sfields->At(num_sfields)); |
| 3703 | ++num_sfields; |
| 3704 | last_static_field_idx = field_idx; |
| 3705 | } |
| 3706 | }, [&](const ClassAccessor::Field& field) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 3707 | uint32_t field_idx = field.GetIndex(); |
| 3708 | DCHECK_GE(field_idx, last_instance_field_idx); // Ordering enforced by DexFileVerifier. |
| 3709 | if (num_ifields == 0 || LIKELY(field_idx > last_instance_field_idx)) { |
| 3710 | LoadField(field, klass, &ifields->At(num_ifields)); |
| 3711 | ++num_ifields; |
| 3712 | last_instance_field_idx = field_idx; |
| 3713 | } |
| 3714 | }, [&](const ClassAccessor::Method& method) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 3715 | ArtMethod* art_method = klass->GetDirectMethodUnchecked(class_def_method_index, |
| 3716 | image_pointer_size_); |
| 3717 | LoadMethod(dex_file, method, klass, art_method); |
| 3718 | LinkCode(this, art_method, oat_class_ptr, class_def_method_index); |
| 3719 | uint32_t it_method_index = method.GetIndex(); |
| 3720 | if (last_dex_method_index == it_method_index) { |
| 3721 | // duplicate case |
| 3722 | art_method->SetMethodIndex(last_class_def_method_index); |
| 3723 | } else { |
| 3724 | art_method->SetMethodIndex(class_def_method_index); |
| 3725 | last_dex_method_index = it_method_index; |
| 3726 | last_class_def_method_index = class_def_method_index; |
| 3727 | } |
| 3728 | ++class_def_method_index; |
| 3729 | }, [&](const ClassAccessor::Method& method) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 3730 | ArtMethod* art_method = klass->GetVirtualMethodUnchecked( |
| 3731 | class_def_method_index - accessor.NumDirectMethods(), |
| 3732 | image_pointer_size_); |
| 3733 | LoadMethod(dex_file, method, klass, art_method); |
| 3734 | LinkCode(this, art_method, oat_class_ptr, class_def_method_index); |
| 3735 | ++class_def_method_index; |
| 3736 | }); |
| 3737 | |
| 3738 | if (UNLIKELY(num_ifields + num_sfields != accessor.NumFields())) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 3739 | LOG(WARNING) << "Duplicate fields in class " << klass->PrettyDescriptor() |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3740 | << " (unique static fields: " << num_sfields << "/" << accessor.NumStaticFields() |
| 3741 | << ", unique instance fields: " << num_ifields << "/" << accessor.NumInstanceFields() |
| 3742 | << ")"; |
Vladimir Marko | 81819db | 2015-11-05 15:30:12 +0000 | [diff] [blame] | 3743 | // NOTE: Not shrinking the over-allocated sfields/ifields, just setting size. |
| 3744 | if (sfields != nullptr) { |
| 3745 | sfields->SetSize(num_sfields); |
| 3746 | } |
| 3747 | if (ifields != nullptr) { |
| 3748 | ifields->SetSize(num_ifields); |
| 3749 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3750 | } |
Vladimir Marko | 81819db | 2015-11-05 15:30:12 +0000 | [diff] [blame] | 3751 | // Set the field arrays. |
| 3752 | klass->SetSFieldsPtr(sfields); |
| 3753 | DCHECK_EQ(klass->NumStaticFields(), num_sfields); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 3754 | klass->SetIFieldsPtr(ifields); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 3755 | DCHECK_EQ(klass->NumInstanceFields(), num_ifields); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 3756 | } |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 3757 | // 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] | 3758 | WriteBarrier::ForEveryFieldWrite(klass.Get()); |
Mathieu Chartier | f3f2a7a | 2015-04-14 15:43:10 -0700 | [diff] [blame] | 3759 | self->AllowThreadSuspension(); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3760 | } |
| 3761 | |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3762 | void ClassLinker::LoadField(const ClassAccessor::Field& field, |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3763 | Handle<mirror::Class> klass, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 3764 | ArtField* dst) { |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3765 | const uint32_t field_idx = field.GetIndex(); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 3766 | dst->SetDexFieldIndex(field_idx); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3767 | dst->SetDeclaringClass(klass.Get()); |
David Brazdil | f6a8a55 | 2018-01-15 18:10:50 +0000 | [diff] [blame] | 3768 | |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 3769 | // Get access flags from the DexFile and set hiddenapi runtime access flags. |
| 3770 | dst->SetAccessFlags(field.GetAccessFlags() | hiddenapi::CreateRuntimeFlags(field)); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3771 | } |
| 3772 | |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 3773 | void ClassLinker::LoadMethod(const DexFile& dex_file, |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3774 | const ClassAccessor::Method& method, |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 3775 | Handle<mirror::Class> klass, |
| 3776 | ArtMethod* dst) { |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 3777 | const uint32_t dex_method_idx = method.GetIndex(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 3778 | const dex::MethodId& method_id = dex_file.GetMethodId(dex_method_idx); |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 3779 | const char* method_name = dex_file.StringDataByIdx(method_id.name_idx_); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 3780 | |
Mathieu Chartier | 268764d | 2016-09-13 12:09:38 -0700 | [diff] [blame] | 3781 | ScopedAssertNoThreadSuspension ants("LoadMethod"); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 3782 | dst->SetDexMethodIndex(dex_method_idx); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 3783 | dst->SetDeclaringClass(klass.Get()); |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3784 | |
David Brazdil | 8586569 | 2018-10-30 17:26:20 +0000 | [diff] [blame] | 3785 | // Get access flags from the DexFile and set hiddenapi runtime access flags. |
| 3786 | uint32_t access_flags = method.GetAccessFlags() | hiddenapi::CreateRuntimeFlags(method); |
David Brazdil | f6a8a55 | 2018-01-15 18:10:50 +0000 | [diff] [blame] | 3787 | |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 3788 | if (UNLIKELY(strcmp("finalize", method_name) == 0)) { |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3789 | // Set finalizable flag on declaring class. |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 3790 | if (strcmp("V", dex_file.GetShorty(method_id.proto_idx_)) == 0) { |
| 3791 | // Void return type. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 3792 | if (klass->GetClassLoader() != nullptr) { // All non-boot finalizer methods are flagged. |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 3793 | klass->SetFinalizable(); |
| 3794 | } else { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3795 | std::string temp; |
| 3796 | const char* klass_descriptor = klass->GetDescriptor(&temp); |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 3797 | // The Enum class declares a "final" finalize() method to prevent subclasses from |
| 3798 | // introducing a finalizer. We don't want to set the finalizable flag for Enum or its |
| 3799 | // subclasses, so we exclude it here. |
| 3800 | // We also want to avoid setting the flag on Object, where we know that finalize() is |
| 3801 | // empty. |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 3802 | if (strcmp(klass_descriptor, "Ljava/lang/Object;") != 0 && |
| 3803 | strcmp(klass_descriptor, "Ljava/lang/Enum;") != 0) { |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3804 | klass->SetFinalizable(); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3805 | } |
| 3806 | } |
| 3807 | } |
| 3808 | } else if (method_name[0] == '<') { |
| 3809 | // Fix broken access flags for initializers. Bug 11157540. |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 3810 | bool is_init = (strcmp("<init>", method_name) == 0); |
| 3811 | bool is_clinit = !is_init && (strcmp("<clinit>", method_name) == 0); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3812 | if (UNLIKELY(!is_init && !is_clinit)) { |
| 3813 | LOG(WARNING) << "Unexpected '<' at start of method name " << method_name; |
| 3814 | } else { |
| 3815 | if (UNLIKELY((access_flags & kAccConstructor) == 0)) { |
| 3816 | 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] | 3817 | << klass->PrettyDescriptor() << " in dex file " << dex_file.GetLocation(); |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3818 | access_flags |= kAccConstructor; |
| 3819 | } |
| 3820 | } |
| 3821 | } |
Vladimir Marko | b0a6aee | 2017-10-27 10:34:04 +0100 | [diff] [blame] | 3822 | if (UNLIKELY((access_flags & kAccNative) != 0u)) { |
| 3823 | // Check if the native method is annotated with @FastNative or @CriticalNative. |
| 3824 | access_flags |= annotations::GetNativeMethodAnnotationAccessFlags( |
| 3825 | dex_file, dst->GetClassDef(), dex_method_idx); |
| 3826 | } |
Ian Rogers | 241b5de | 2013-10-09 17:58:57 -0700 | [diff] [blame] | 3827 | dst->SetAccessFlags(access_flags); |
David Srbecky | e36e7f2 | 2018-11-14 14:21:23 +0000 | [diff] [blame] | 3828 | // Must be done after SetAccessFlags since IsAbstract depends on it. |
| 3829 | if (klass->IsInterface() && dst->IsAbstract()) { |
| 3830 | dst->CalculateAndSetImtIndex(); |
| 3831 | } |
Nicolas Geoffray | 4717175 | 2020-08-31 15:03:20 +0100 | [diff] [blame] | 3832 | if (dst->HasCodeItem()) { |
| 3833 | DCHECK_NE(method.GetCodeItemOffset(), 0u); |
| 3834 | if (Runtime::Current()->IsAotCompiler()) { |
| 3835 | dst->SetDataPtrSize(reinterpret_cast32<void*>(method.GetCodeItemOffset()), image_pointer_size_); |
| 3836 | } else { |
Nicolas Geoffray | e1d2dce | 2020-09-21 10:06:31 +0100 | [diff] [blame] | 3837 | dst->SetCodeItem(dst->GetDexFile()->GetCodeItem(method.GetCodeItemOffset())); |
Nicolas Geoffray | 4717175 | 2020-08-31 15:03:20 +0100 | [diff] [blame] | 3838 | } |
| 3839 | } else { |
| 3840 | dst->SetDataPtrSize(nullptr, image_pointer_size_); |
| 3841 | DCHECK_EQ(method.GetCodeItemOffset(), 0u); |
| 3842 | } |
Brian Carlstrom | 934486c | 2011-07-12 23:42:50 -0700 | [diff] [blame] | 3843 | } |
| 3844 | |
Mathieu Chartier | 0a19e21 | 2019-11-27 14:35:24 -0800 | [diff] [blame] | 3845 | void ClassLinker::AppendToBootClassPath(Thread* self, const DexFile* dex_file) { |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3846 | ObjPtr<mirror::DexCache> dex_cache = AllocAndInitializeDexCache( |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 3847 | self, |
Mathieu Chartier | 0a19e21 | 2019-11-27 14:35:24 -0800 | [diff] [blame] | 3848 | *dex_file, |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3849 | Runtime::Current()->GetLinearAlloc()); |
Mathieu Chartier | 0a19e21 | 2019-11-27 14:35:24 -0800 | [diff] [blame] | 3850 | 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] | 3851 | AppendToBootClassPath(dex_file, dex_cache); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 3852 | } |
| 3853 | |
Mathieu Chartier | 0a19e21 | 2019-11-27 14:35:24 -0800 | [diff] [blame] | 3854 | void ClassLinker::AppendToBootClassPath(const DexFile* dex_file, |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3855 | ObjPtr<mirror::DexCache> dex_cache) { |
Mathieu Chartier | 0a19e21 | 2019-11-27 14:35:24 -0800 | [diff] [blame] | 3856 | CHECK(dex_file != nullptr); |
| 3857 | CHECK(dex_cache != nullptr) << dex_file->GetLocation(); |
| 3858 | boot_class_path_.push_back(dex_file); |
Andreas Gampe | be7af22 | 2017-07-25 09:57:28 -0700 | [diff] [blame] | 3859 | WriterMutexLock mu(Thread::Current(), *Locks::dex_lock_); |
Mathieu Chartier | 0a19e21 | 2019-11-27 14:35:24 -0800 | [diff] [blame] | 3860 | RegisterDexFileLocked(*dex_file, dex_cache, /* class_loader= */ nullptr); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 3861 | } |
| 3862 | |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 3863 | void ClassLinker::RegisterDexFileLocked(const DexFile& dex_file, |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3864 | ObjPtr<mirror::DexCache> dex_cache, |
| 3865 | ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3866 | Thread* const self = Thread::Current(); |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 3867 | Locks::dex_lock_->AssertExclusiveHeld(self); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3868 | CHECK(dex_cache != nullptr) << dex_file.GetLocation(); |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 3869 | CHECK_EQ(dex_cache->GetDexFile(), &dex_file) << dex_file.GetLocation(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 3870 | // For app images, the dex cache location may be a suffix of the dex file location since the |
| 3871 | // dex file location is an absolute path. |
Mathieu Chartier | 7617216 | 2016-01-26 14:54:06 -0800 | [diff] [blame] | 3872 | const std::string dex_cache_location = dex_cache->GetLocation()->ToModifiedUtf8(); |
| 3873 | const size_t dex_cache_length = dex_cache_location.length(); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 3874 | CHECK_GT(dex_cache_length, 0u) << dex_file.GetLocation(); |
| 3875 | std::string dex_file_location = dex_file.GetLocation(); |
Nicolas Geoffray | e3e0f70 | 2019-03-12 07:02:02 +0000 | [diff] [blame] | 3876 | // The following paths checks don't work on preopt when using boot dex files, where the dex |
| 3877 | // cache location is the one on device, and the dex_file's location is the one on host. |
| 3878 | if (!(Runtime::Current()->IsAotCompiler() && class_loader == nullptr && !kIsTargetBuild)) { |
| 3879 | CHECK_GE(dex_file_location.length(), dex_cache_length) |
| 3880 | << dex_cache_location << " " << dex_file.GetLocation(); |
| 3881 | const std::string dex_file_suffix = dex_file_location.substr( |
| 3882 | dex_file_location.length() - dex_cache_length, |
| 3883 | dex_cache_length); |
| 3884 | // Example dex_cache location is SettingsProvider.apk and |
| 3885 | // dex file location is /system/priv-app/SettingsProvider/SettingsProvider.apk |
| 3886 | CHECK_EQ(dex_cache_location, dex_file_suffix); |
| 3887 | } |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 3888 | const OatFile* oat_file = |
| 3889 | (dex_file.GetOatDexFile() != nullptr) ? dex_file.GetOatDexFile()->GetOatFile() : nullptr; |
Vladimir Marko | b066d43 | 2018-01-03 13:14:37 +0000 | [diff] [blame] | 3890 | // Clean up pass to remove null dex caches; null dex caches can occur due to class unloading |
| 3891 | // and we are lazily removing null entries. Also check if we need to initialize OatFile data |
| 3892 | // (.data.bimg.rel.ro and .bss sections) needed for code execution. |
| 3893 | bool initialize_oat_file_data = (oat_file != nullptr) && oat_file->IsExecutable(); |
Ian Rogers | 55256cb | 2017-12-21 17:07:11 -0800 | [diff] [blame] | 3894 | JavaVMExt* const vm = self->GetJniEnv()->GetVm(); |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 3895 | for (auto it = dex_caches_.begin(); it != dex_caches_.end(); ) { |
| 3896 | DexCacheData data = *it; |
| 3897 | if (self->IsJWeakCleared(data.weak_root)) { |
| 3898 | vm->DeleteWeakGlobalRef(self, data.weak_root); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3899 | it = dex_caches_.erase(it); |
| 3900 | } else { |
Vladimir Marko | b066d43 | 2018-01-03 13:14:37 +0000 | [diff] [blame] | 3901 | if (initialize_oat_file_data && |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 3902 | it->dex_file->GetOatDexFile() != nullptr && |
| 3903 | it->dex_file->GetOatDexFile()->GetOatFile() == oat_file) { |
Vladimir Marko | b066d43 | 2018-01-03 13:14:37 +0000 | [diff] [blame] | 3904 | initialize_oat_file_data = false; // Already initialized. |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 3905 | } |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 3906 | ++it; |
| 3907 | } |
Brian Carlstrom | 81a9087 | 2015-08-28 09:07:14 -0700 | [diff] [blame] | 3908 | } |
Vladimir Marko | b066d43 | 2018-01-03 13:14:37 +0000 | [diff] [blame] | 3909 | if (initialize_oat_file_data) { |
Vladimir Marko | 1cedb4a | 2019-02-06 14:13:28 +0000 | [diff] [blame] | 3910 | oat_file->InitializeRelocations(); |
Vladimir Marko | 0eb882b | 2017-05-15 13:39:18 +0100 | [diff] [blame] | 3911 | } |
David Brazdil | a5c3a80 | 2019-03-08 14:59:41 +0000 | [diff] [blame] | 3912 | // Let hiddenapi assign a domain to the newly registered dex file. |
| 3913 | hiddenapi::InitializeDexFileDomain(dex_file, class_loader); |
| 3914 | |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3915 | jweak dex_cache_jweak = vm->AddWeakGlobalRef(self, dex_cache); |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 3916 | DexCacheData data; |
| 3917 | data.weak_root = dex_cache_jweak; |
| 3918 | data.dex_file = dex_cache->GetDexFile(); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3919 | data.class_table = ClassTableForClassLoader(class_loader); |
David Srbecky | afc60cd | 2018-12-05 11:59:31 +0000 | [diff] [blame] | 3920 | AddNativeDebugInfoForDex(self, data.dex_file); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3921 | DCHECK(data.class_table != nullptr); |
Mathieu Chartier | 72041a0 | 2017-07-14 18:23:25 -0700 | [diff] [blame] | 3922 | // Make sure to hold the dex cache live in the class table. This case happens for the boot class |
| 3923 | // path dex caches without an image. |
| 3924 | data.class_table->InsertStrongRoot(dex_cache); |
Andreas Gampe | 8a1a0f7 | 2020-03-03 16:07:45 -0800 | [diff] [blame] | 3925 | // Make sure that the dex cache holds the classloader live. |
| 3926 | dex_cache->SetClassLoader(class_loader); |
Mathieu Chartier | 72041a0 | 2017-07-14 18:23:25 -0700 | [diff] [blame] | 3927 | if (class_loader != nullptr) { |
| 3928 | // Since we added a strong root to the class table, do the write barrier as required for |
| 3929 | // remembered sets and generational GCs. |
Mathieu Chartier | 88ea61e | 2018-06-20 17:45:41 -0700 | [diff] [blame] | 3930 | WriteBarrier::ForEveryFieldWrite(class_loader); |
Mathieu Chartier | 72041a0 | 2017-07-14 18:23:25 -0700 | [diff] [blame] | 3931 | } |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 3932 | dex_caches_.push_back(data); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 3933 | } |
| 3934 | |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 3935 | ObjPtr<mirror::DexCache> ClassLinker::DecodeDexCacheLocked(Thread* self, const DexCacheData* data) { |
| 3936 | return data != nullptr |
| 3937 | ? ObjPtr<mirror::DexCache>::DownCast(self->DecodeJObject(data->weak_root)) |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3938 | : nullptr; |
| 3939 | } |
| 3940 | |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 3941 | bool ClassLinker::IsSameClassLoader( |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3942 | ObjPtr<mirror::DexCache> dex_cache, |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 3943 | const DexCacheData* data, |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3944 | ObjPtr<mirror::ClassLoader> class_loader) { |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 3945 | CHECK(data != nullptr); |
| 3946 | DCHECK_EQ(dex_cache->GetDexFile(), data->dex_file); |
| 3947 | return data->class_table == ClassTableForClassLoader(class_loader); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3948 | } |
| 3949 | |
Alex Light | 07f0621 | 2017-06-01 14:01:43 -0700 | [diff] [blame] | 3950 | void ClassLinker::RegisterExistingDexCache(ObjPtr<mirror::DexCache> dex_cache, |
| 3951 | ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | ed4ee44 | 2018-06-05 14:23:35 -0700 | [diff] [blame] | 3952 | SCOPED_TRACE << __FUNCTION__ << " " << dex_cache->GetDexFile()->GetLocation(); |
Alex Light | 07f0621 | 2017-06-01 14:01:43 -0700 | [diff] [blame] | 3953 | Thread* self = Thread::Current(); |
| 3954 | StackHandleScope<2> hs(self); |
| 3955 | Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(dex_cache)); |
| 3956 | Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader)); |
| 3957 | const DexFile* dex_file = dex_cache->GetDexFile(); |
| 3958 | DCHECK(dex_file != nullptr) << "Attempt to register uninitialized dex_cache object!"; |
| 3959 | if (kIsDebugBuild) { |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 3960 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
| 3961 | const DexCacheData* old_data = FindDexCacheDataLocked(*dex_file); |
| 3962 | ObjPtr<mirror::DexCache> old_dex_cache = DecodeDexCacheLocked(self, old_data); |
Alex Light | 07f0621 | 2017-06-01 14:01:43 -0700 | [diff] [blame] | 3963 | DCHECK(old_dex_cache.IsNull()) << "Attempt to manually register a dex cache thats already " |
| 3964 | << "been registered on dex file " << dex_file->GetLocation(); |
| 3965 | } |
| 3966 | ClassTable* table; |
| 3967 | { |
| 3968 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 3969 | table = InsertClassTableForClassLoader(h_class_loader.Get()); |
| 3970 | } |
Ivan Maidanski | 2b69b9c | 2018-05-14 13:50:48 +0300 | [diff] [blame] | 3971 | // Avoid a deadlock between a garbage collecting thread running a checkpoint, |
| 3972 | // a thread holding the dex lock and blocking on a condition variable regarding |
| 3973 | // weak references access, and a thread blocking on the dex lock. |
Ivan Maidanski | 2b69b9c | 2018-05-14 13:50:48 +0300 | [diff] [blame] | 3974 | gc::ScopedGCCriticalSection gcs(self, gc::kGcCauseClassLinker, gc::kCollectorTypeClassLinker); |
Alex Light | 07f0621 | 2017-06-01 14:01:43 -0700 | [diff] [blame] | 3975 | WriterMutexLock mu(self, *Locks::dex_lock_); |
| 3976 | RegisterDexFileLocked(*dex_file, h_dex_cache.Get(), h_class_loader.Get()); |
| 3977 | table->InsertStrongRoot(h_dex_cache.Get()); |
| 3978 | if (h_class_loader.Get() != nullptr) { |
| 3979 | // Since we added a strong root to the class table, do the write barrier as required for |
| 3980 | // remembered sets and generational GCs. |
Mathieu Chartier | 88ea61e | 2018-06-20 17:45:41 -0700 | [diff] [blame] | 3981 | WriteBarrier::ForEveryFieldWrite(h_class_loader.Get()); |
Alex Light | 07f0621 | 2017-06-01 14:01:43 -0700 | [diff] [blame] | 3982 | } |
| 3983 | } |
| 3984 | |
Alex Light | de7f878 | 2020-02-24 10:14:22 -0800 | [diff] [blame] | 3985 | static void ThrowDexFileAlreadyRegisteredError(Thread* self, const DexFile& dex_file) |
| 3986 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 3987 | self->ThrowNewExceptionF("Ljava/lang/InternalError;", |
Alex Light | de7f878 | 2020-02-24 10:14:22 -0800 | [diff] [blame] | 3988 | "Attempt to register dex file %s with multiple class loaders", |
| 3989 | dex_file.GetLocation().c_str()); |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 3990 | } |
| 3991 | |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 3992 | ObjPtr<mirror::DexCache> ClassLinker::RegisterDexFile(const DexFile& dex_file, |
| 3993 | ObjPtr<mirror::ClassLoader> class_loader) { |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 3994 | Thread* self = Thread::Current(); |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 3995 | ObjPtr<mirror::DexCache> old_dex_cache; |
| 3996 | bool registered_with_another_class_loader = false; |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 3997 | { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 3998 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 3999 | const DexCacheData* old_data = FindDexCacheDataLocked(dex_file); |
| 4000 | old_dex_cache = DecodeDexCacheLocked(self, old_data); |
| 4001 | if (old_dex_cache != nullptr) { |
| 4002 | if (IsSameClassLoader(old_dex_cache, old_data, class_loader)) { |
| 4003 | return old_dex_cache; |
| 4004 | } else { |
| 4005 | // TODO This is not very clean looking. Should maybe try to make a way to request exceptions |
| 4006 | // be thrown when it's safe to do so to simplify this. |
| 4007 | registered_with_another_class_loader = true; |
| 4008 | } |
| 4009 | } |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4010 | } |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 4011 | // We need to have released the dex_lock_ to allocate safely. |
| 4012 | if (registered_with_another_class_loader) { |
| 4013 | ThrowDexFileAlreadyRegisteredError(self, dex_file); |
| 4014 | return nullptr; |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 4015 | } |
Mathieu Chartier | ed4ee44 | 2018-06-05 14:23:35 -0700 | [diff] [blame] | 4016 | SCOPED_TRACE << __FUNCTION__ << " " << dex_file.GetLocation(); |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 4017 | LinearAlloc* const linear_alloc = GetOrCreateAllocatorForClassLoader(class_loader); |
| 4018 | DCHECK(linear_alloc != nullptr); |
| 4019 | ClassTable* table; |
| 4020 | { |
| 4021 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 4022 | table = InsertClassTableForClassLoader(class_loader); |
| 4023 | } |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 4024 | // Don't alloc while holding the lock, since allocation may need to |
| 4025 | // suspend all threads and another thread may need the dex_lock_ to |
| 4026 | // get to a suspend point. |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4027 | StackHandleScope<3> hs(self); |
| 4028 | Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader)); |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 4029 | Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(AllocDexCache(self, dex_file))); |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 4030 | { |
Ivan Maidanski | 2b69b9c | 2018-05-14 13:50:48 +0300 | [diff] [blame] | 4031 | // Avoid a deadlock between a garbage collecting thread running a checkpoint, |
| 4032 | // a thread holding the dex lock and blocking on a condition variable regarding |
| 4033 | // weak references access, and a thread blocking on the dex lock. |
Ivan Maidanski | 2b69b9c | 2018-05-14 13:50:48 +0300 | [diff] [blame] | 4034 | gc::ScopedGCCriticalSection gcs(self, gc::kGcCauseClassLinker, gc::kCollectorTypeClassLinker); |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 4035 | WriterMutexLock mu(self, *Locks::dex_lock_); |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 4036 | const DexCacheData* old_data = FindDexCacheDataLocked(dex_file); |
| 4037 | old_dex_cache = DecodeDexCacheLocked(self, old_data); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4038 | if (old_dex_cache == nullptr && h_dex_cache != nullptr) { |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 4039 | // Do InitializeNativeFields while holding dex lock to make sure two threads don't call it |
| 4040 | // at the same time with the same dex cache. Since the .bss is shared this can cause failing |
| 4041 | // DCHECK that the arrays are null. |
| 4042 | h_dex_cache->InitializeNativeFields(&dex_file, linear_alloc); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4043 | RegisterDexFileLocked(dex_file, h_dex_cache.Get(), h_class_loader.Get()); |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 4044 | } |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 4045 | if (old_dex_cache != nullptr) { |
| 4046 | // Another thread managed to initialize the dex cache faster, so use that DexCache. |
| 4047 | // If this thread encountered OOME, ignore it. |
| 4048 | DCHECK_EQ(h_dex_cache == nullptr, self->IsExceptionPending()); |
| 4049 | self->ClearException(); |
| 4050 | // We cannot call EnsureSameClassLoader() or allocate an exception while holding the |
| 4051 | // dex_lock_. |
| 4052 | if (IsSameClassLoader(old_dex_cache, old_data, h_class_loader.Get())) { |
| 4053 | return old_dex_cache; |
| 4054 | } else { |
| 4055 | registered_with_another_class_loader = true; |
| 4056 | } |
| 4057 | } |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4058 | } |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 4059 | if (registered_with_another_class_loader) { |
| 4060 | ThrowDexFileAlreadyRegisteredError(self, dex_file); |
| 4061 | return nullptr; |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4062 | } |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4063 | if (h_dex_cache == nullptr) { |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4064 | self->AssertPendingOOMException(); |
| 4065 | return nullptr; |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 4066 | } |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 4067 | table->InsertStrongRoot(h_dex_cache.Get()); |
Mathieu Chartier | a1467d0 | 2017-02-22 09:22:50 -0800 | [diff] [blame] | 4068 | if (h_class_loader.Get() != nullptr) { |
| 4069 | // Since we added a strong root to the class table, do the write barrier as required for |
| 4070 | // remembered sets and generational GCs. |
Mathieu Chartier | 88ea61e | 2018-06-20 17:45:41 -0700 | [diff] [blame] | 4071 | WriteBarrier::ForEveryFieldWrite(h_class_loader.Get()); |
Mathieu Chartier | a1467d0 | 2017-02-22 09:22:50 -0800 | [diff] [blame] | 4072 | } |
Nicolas Geoffray | 1d4f009 | 2020-08-07 14:01:05 +0100 | [diff] [blame] | 4073 | PaletteHooks* hooks = nullptr; |
| 4074 | VLOG(class_linker) << "Registered dex file " << dex_file.GetLocation(); |
Orion Hodson | c5323fe | 2021-02-04 21:20:30 +0000 | [diff] [blame] | 4075 | if (PaletteGetHooks(&hooks) == PALETTE_STATUS_OK) { |
Nicolas Geoffray | 1d4f009 | 2020-08-07 14:01:05 +0100 | [diff] [blame] | 4076 | hooks->NotifyDexFileLoaded(dex_file.GetLocation().c_str()); |
| 4077 | } |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 4078 | return h_dex_cache.Get(); |
Brian Carlstrom | aded5f7 | 2011-10-07 17:15:04 -0700 | [diff] [blame] | 4079 | } |
| 4080 | |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4081 | bool ClassLinker::IsDexFileRegistered(Thread* self, const DexFile& dex_file) { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 4082 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 4083 | return DecodeDexCacheLocked(self, FindDexCacheDataLocked(dex_file)) != nullptr; |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 4084 | } |
| 4085 | |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4086 | ObjPtr<mirror::DexCache> ClassLinker::FindDexCache(Thread* self, const DexFile& dex_file) { |
| 4087 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 4088 | const DexCacheData* dex_cache_data = FindDexCacheDataLocked(dex_file); |
| 4089 | ObjPtr<mirror::DexCache> dex_cache = DecodeDexCacheLocked(self, dex_cache_data); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4090 | if (dex_cache != nullptr) { |
| 4091 | return dex_cache; |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 4092 | } |
Brian Carlstrom | 81a9087 | 2015-08-28 09:07:14 -0700 | [diff] [blame] | 4093 | // Failure, dump diagnostic and abort. |
Hiroshi Yamauchi | 04302db | 2015-11-11 23:45:34 -0800 | [diff] [blame] | 4094 | for (const DexCacheData& data : dex_caches_) { |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 4095 | if (DecodeDexCacheLocked(self, &data) != nullptr) { |
Andreas Gampe | 37c5846 | 2017-03-27 15:14:27 -0700 | [diff] [blame] | 4096 | LOG(FATAL_WITHOUT_ABORT) << "Registered dex file " << data.dex_file->GetLocation(); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 4097 | } |
Brian Carlstrom | 81a9087 | 2015-08-28 09:07:14 -0700 | [diff] [blame] | 4098 | } |
Mathieu Chartier | 9e050df | 2017-08-09 10:05:47 -0700 | [diff] [blame] | 4099 | LOG(FATAL) << "Failed to find DexCache for DexFile " << dex_file.GetLocation() |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 4100 | << " " << &dex_file << " " << dex_cache_data->dex_file; |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 4101 | UNREACHABLE(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4102 | } |
| 4103 | |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4104 | ClassTable* ClassLinker::FindClassTable(Thread* self, ObjPtr<mirror::DexCache> dex_cache) { |
| 4105 | const DexFile* dex_file = dex_cache->GetDexFile(); |
| 4106 | DCHECK(dex_file != nullptr); |
| 4107 | ReaderMutexLock mu(self, *Locks::dex_lock_); |
| 4108 | // Search assuming unique-ness of dex file. |
| 4109 | for (const DexCacheData& data : dex_caches_) { |
| 4110 | // Avoid decoding (and read barriers) other unrelated dex caches. |
| 4111 | if (data.dex_file == dex_file) { |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 4112 | ObjPtr<mirror::DexCache> registered_dex_cache = DecodeDexCacheLocked(self, &data); |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4113 | if (registered_dex_cache != nullptr) { |
| 4114 | CHECK_EQ(registered_dex_cache, dex_cache) << dex_file->GetLocation(); |
| 4115 | return data.class_table; |
| 4116 | } |
| 4117 | } |
| 4118 | } |
| 4119 | return nullptr; |
| 4120 | } |
| 4121 | |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 4122 | const ClassLinker::DexCacheData* ClassLinker::FindDexCacheDataLocked(const DexFile& dex_file) { |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4123 | // Search assuming unique-ness of dex file. |
| 4124 | for (const DexCacheData& data : dex_caches_) { |
| 4125 | // Avoid decoding (and read barriers) other unrelated dex caches. |
| 4126 | if (data.dex_file == &dex_file) { |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 4127 | return &data; |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4128 | } |
| 4129 | } |
Alex Light | 725da8f | 2020-02-19 14:46:33 -0800 | [diff] [blame] | 4130 | return nullptr; |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 4131 | } |
| 4132 | |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 4133 | void ClassLinker::CreatePrimitiveClass(Thread* self, |
| 4134 | Primitive::Type type, |
| 4135 | ClassRoot primitive_root) { |
Vladimir Marko | acb906d | 2018-05-30 10:23:49 +0100 | [diff] [blame] | 4136 | ObjPtr<mirror::Class> primitive_class = |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 4137 | AllocClass(self, mirror::Class::PrimitiveClassSize(image_pointer_size_)); |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 4138 | CHECK(primitive_class != nullptr) << "OOM for primitive class " << type; |
| 4139 | // Do not hold lock on the primitive class object, the initialization of |
| 4140 | // primitive classes is done while the process is still single threaded. |
Vladimir Marko | b68bb7a | 2020-03-17 10:55:25 +0000 | [diff] [blame] | 4141 | primitive_class->SetAccessFlagsDuringLinking( |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 4142 | kAccPublic | kAccFinal | kAccAbstract | kAccVerificationAttempted); |
| 4143 | primitive_class->SetPrimitiveType(type); |
| 4144 | primitive_class->SetIfTable(GetClassRoot<mirror::Object>(this)->GetIfTable()); |
| 4145 | // Skip EnsureSkipAccessChecksMethods(). We can skip the verified status, |
| 4146 | // the kAccVerificationAttempted flag was added above, and there are no |
| 4147 | // methods that need the kAccSkipAccessChecks flag. |
| 4148 | DCHECK_EQ(primitive_class->NumMethods(), 0u); |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 4149 | // Primitive classes are initialized during single threaded startup, so visibly initialized. |
| 4150 | primitive_class->SetStatusForPrimitiveOrArray(ClassStatus::kVisiblyInitialized); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4151 | const char* descriptor = Primitive::Descriptor(type); |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 4152 | ObjPtr<mirror::Class> existing = InsertClass(descriptor, |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 4153 | primitive_class, |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 4154 | ComputeModifiedUtf8Hash(descriptor)); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4155 | CHECK(existing == nullptr) << "InitPrimitiveClass(" << type << ") failed"; |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 4156 | SetClassRoot(primitive_root, primitive_class); |
Carl Shapiro | 565f507 | 2011-07-10 13:39:43 -0700 | [diff] [blame] | 4157 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4158 | |
Vladimir Marko | 0261055 | 2018-06-04 14:38:00 +0100 | [diff] [blame] | 4159 | inline ObjPtr<mirror::IfTable> ClassLinker::GetArrayIfTable() { |
| 4160 | return GetClassRoot<mirror::ObjectArray<mirror::Object>>(this)->GetIfTable(); |
| 4161 | } |
| 4162 | |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 4163 | // Create an array class (i.e. the class object for the array, not the |
| 4164 | // array itself). "descriptor" looks like "[C" or "[[[[B" or |
| 4165 | // "[Ljava/lang/String;". |
| 4166 | // |
| 4167 | // If "descriptor" refers to an array of primitives, look up the |
| 4168 | // primitive type's internally-generated class object. |
| 4169 | // |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 4170 | // "class_loader" is the class loader of the class that's referring to |
| 4171 | // us. It's used to ensure that we're looking for the element type in |
| 4172 | // the right context. It does NOT become the class loader for the |
| 4173 | // array class; that always comes from the base element class. |
Brian Carlstrom | be97785 | 2011-07-19 14:54:54 -0700 | [diff] [blame] | 4174 | // |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 4175 | // Returns null with an exception raised on failure. |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 4176 | ObjPtr<mirror::Class> ClassLinker::CreateArrayClass(Thread* self, |
| 4177 | const char* descriptor, |
| 4178 | size_t hash, |
| 4179 | Handle<mirror::ClassLoader> class_loader) { |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 4180 | // Identify the underlying component type |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4181 | CHECK_EQ('[', descriptor[0]); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4182 | StackHandleScope<2> hs(self); |
Alex Light | e9f6103 | 2018-09-24 16:04:51 -0700 | [diff] [blame] | 4183 | |
| 4184 | // This is to prevent the calls to ClassLoad and ClassPrepare which can cause java/user-supplied |
| 4185 | // code to be executed. We put it up here so we can avoid all the allocations associated with |
| 4186 | // creating the class. This can happen with (eg) jit threads. |
| 4187 | if (!self->CanLoadClasses()) { |
| 4188 | // Make sure we don't try to load anything, potentially causing an infinite loop. |
| 4189 | ObjPtr<mirror::Throwable> pre_allocated = |
| 4190 | Runtime::Current()->GetPreAllocatedNoClassDefFoundError(); |
| 4191 | self->SetException(pre_allocated); |
| 4192 | return nullptr; |
| 4193 | } |
| 4194 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 4195 | MutableHandle<mirror::Class> component_type(hs.NewHandle(FindClass(self, descriptor + 1, |
| 4196 | class_loader))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4197 | if (component_type == nullptr) { |
Mathieu Chartier | c0a9ea4 | 2014-02-03 16:36:49 -0800 | [diff] [blame] | 4198 | DCHECK(self->IsExceptionPending()); |
Andreas Gampe | dc13d7d | 2014-07-23 20:18:36 -0700 | [diff] [blame] | 4199 | // We need to accept erroneous classes as component types. |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 4200 | const size_t component_hash = ComputeModifiedUtf8Hash(descriptor + 1); |
| 4201 | component_type.Assign(LookupClass(self, descriptor + 1, component_hash, class_loader.Get())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4202 | if (component_type == nullptr) { |
Andreas Gampe | dc13d7d | 2014-07-23 20:18:36 -0700 | [diff] [blame] | 4203 | DCHECK(self->IsExceptionPending()); |
| 4204 | return nullptr; |
| 4205 | } else { |
| 4206 | self->ClearException(); |
| 4207 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 4208 | } |
Ian Rogers | 2d10b20 | 2014-05-12 19:15:18 -0700 | [diff] [blame] | 4209 | if (UNLIKELY(component_type->IsPrimitiveVoid())) { |
| 4210 | ThrowNoClassDefFoundError("Attempt to create array of void primitive type"); |
| 4211 | return nullptr; |
| 4212 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 4213 | // See if the component type is already loaded. Array classes are |
| 4214 | // always associated with the class loader of their underlying |
| 4215 | // element type -- an array of Strings goes with the loader for |
| 4216 | // java/lang/String -- so we need to look for it there. (The |
| 4217 | // caller should have checked for the existence of the class |
| 4218 | // before calling here, but they did so with *their* class loader, |
| 4219 | // not the component type's loader.) |
| 4220 | // |
| 4221 | // If we find it, the caller adds "loader" to the class' initiating |
| 4222 | // loader list, which should prevent us from going through this again. |
| 4223 | // |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 4224 | // This call is unnecessary if "loader" and "component_type->GetClassLoader()" |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 4225 | // are the same, because our caller (FindClass) just did the |
| 4226 | // lookup. (Even if we get this wrong we still have correct behavior, |
| 4227 | // because we effectively do this lookup again when we add the new |
| 4228 | // class to the hash table --- necessary because of possible races with |
| 4229 | // other threads.) |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4230 | if (class_loader.Get() != component_type->GetClassLoader()) { |
Vladimir Marko | 6ad2f6d | 2017-01-18 15:22:59 +0000 | [diff] [blame] | 4231 | ObjPtr<mirror::Class> new_class = |
| 4232 | LookupClass(self, descriptor, hash, component_type->GetClassLoader()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4233 | if (new_class != nullptr) { |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 4234 | return new_class; |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 4235 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 4236 | } |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 4237 | // Core array classes, i.e. Object[], Class[], String[] and primitive |
| 4238 | // arrays, have special initialization and they should be found above. |
| 4239 | DCHECK(!component_type->IsObjectClass() || |
| 4240 | // Guard from false positives for errors before setting superclass. |
| 4241 | component_type->IsErroneousUnresolved()); |
| 4242 | DCHECK(!component_type->IsStringClass()); |
| 4243 | DCHECK(!component_type->IsClassClass()); |
| 4244 | DCHECK(!component_type->IsPrimitive()); |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 4245 | |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 4246 | // Fill out the fields in the Class. |
| 4247 | // |
| 4248 | // It is possible to execute some methods against arrays, because |
| 4249 | // all arrays are subclasses of java_lang_Object_, so we need to set |
| 4250 | // up a vtable. We can just point at the one in java_lang_Object_. |
| 4251 | // |
| 4252 | // Array classes are simple enough that we don't need to do a full |
| 4253 | // link step. |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 4254 | size_t array_class_size = mirror::Array::ClassSize(image_pointer_size_); |
| 4255 | auto visitor = [this, array_class_size, component_type](ObjPtr<mirror::Object> obj, |
| 4256 | size_t usable_size) |
| 4257 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | b68bb7a | 2020-03-17 10:55:25 +0000 | [diff] [blame] | 4258 | ScopedAssertNoNewTransactionRecords sanntr("CreateArrayClass"); |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 4259 | mirror::Class::InitializeClassVisitor init_class(array_class_size); |
| 4260 | init_class(obj, usable_size); |
| 4261 | ObjPtr<mirror::Class> klass = ObjPtr<mirror::Class>::DownCast(obj); |
| 4262 | klass->SetComponentType(component_type.Get()); |
| 4263 | // Do not hold lock for initialization, the fence issued after the visitor |
| 4264 | // returns ensures memory visibility together with the implicit consume |
| 4265 | // semantics (for all supported architectures) for any thread that loads |
| 4266 | // the array class reference from any memory locations afterwards. |
| 4267 | FinishArrayClassSetup(klass); |
| 4268 | }; |
| 4269 | auto new_class = hs.NewHandle<mirror::Class>( |
| 4270 | AllocClass(self, GetClassRoot<mirror::Class>(this), array_class_size, visitor)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4271 | if (new_class == nullptr) { |
Vladimir Marko | 70e2a76 | 2019-07-12 16:49:00 +0100 | [diff] [blame] | 4272 | self->AssertPendingOOMException(); |
| 4273 | return nullptr; |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 4274 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 4275 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4276 | ObjPtr<mirror::Class> existing = InsertClass(descriptor, new_class.Get(), hash); |
Mathieu Chartier | c0a9ea4 | 2014-02-03 16:36:49 -0800 | [diff] [blame] | 4277 | if (existing == nullptr) { |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4278 | // We postpone ClassLoad and ClassPrepare events to this point in time to avoid |
| 4279 | // duplicate events in case of races. Array classes don't really follow dedicated |
| 4280 | // load and prepare, anyways. |
| 4281 | Runtime::Current()->GetRuntimeCallbacks()->ClassLoad(new_class); |
| 4282 | Runtime::Current()->GetRuntimeCallbacks()->ClassPrepare(new_class, new_class); |
| 4283 | |
Tamas Berghammer | 160e6df | 2016-01-05 14:29:02 +0000 | [diff] [blame] | 4284 | jit::Jit::NewTypeLoadedIfUsingJit(new_class.Get()); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4285 | return new_class.Get(); |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 4286 | } |
| 4287 | // Another thread must have loaded the class after we |
| 4288 | // started but before we finished. Abandon what we've |
| 4289 | // done. |
| 4290 | // |
| 4291 | // (Yes, this happens.) |
| 4292 | |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 4293 | return existing; |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 4294 | } |
| 4295 | |
Vladimir Marko | 9186b18 | 2018-11-06 14:55:54 +0000 | [diff] [blame] | 4296 | ObjPtr<mirror::Class> ClassLinker::LookupPrimitiveClass(char type) { |
| 4297 | ClassRoot class_root; |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 4298 | switch (type) { |
Vladimir Marko | 9186b18 | 2018-11-06 14:55:54 +0000 | [diff] [blame] | 4299 | case 'B': class_root = ClassRoot::kPrimitiveByte; break; |
| 4300 | case 'C': class_root = ClassRoot::kPrimitiveChar; break; |
| 4301 | case 'D': class_root = ClassRoot::kPrimitiveDouble; break; |
| 4302 | case 'F': class_root = ClassRoot::kPrimitiveFloat; break; |
| 4303 | case 'I': class_root = ClassRoot::kPrimitiveInt; break; |
| 4304 | case 'J': class_root = ClassRoot::kPrimitiveLong; break; |
| 4305 | case 'S': class_root = ClassRoot::kPrimitiveShort; break; |
| 4306 | case 'Z': class_root = ClassRoot::kPrimitiveBoolean; break; |
| 4307 | case 'V': class_root = ClassRoot::kPrimitiveVoid; break; |
Ian Rogers | 62f0512 | 2014-03-21 11:21:29 -0700 | [diff] [blame] | 4308 | default: |
Vladimir Marko | 9186b18 | 2018-11-06 14:55:54 +0000 | [diff] [blame] | 4309 | return nullptr; |
Carl Shapiro | 744ad05 | 2011-08-06 15:53:36 -0700 | [diff] [blame] | 4310 | } |
Vladimir Marko | 9186b18 | 2018-11-06 14:55:54 +0000 | [diff] [blame] | 4311 | return GetClassRoot(class_root, this); |
| 4312 | } |
| 4313 | |
| 4314 | ObjPtr<mirror::Class> ClassLinker::FindPrimitiveClass(char type) { |
| 4315 | ObjPtr<mirror::Class> result = LookupPrimitiveClass(type); |
| 4316 | if (UNLIKELY(result == nullptr)) { |
| 4317 | std::string printable_type(PrintableChar(type)); |
| 4318 | ThrowNoClassDefFoundError("Not a primitive type: %s", printable_type.c_str()); |
| 4319 | } |
| 4320 | return result; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4321 | } |
| 4322 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 4323 | ObjPtr<mirror::Class> ClassLinker::InsertClass(const char* descriptor, |
| 4324 | ObjPtr<mirror::Class> klass, |
| 4325 | size_t hash) { |
Alex Light | e9f6103 | 2018-09-24 16:04:51 -0700 | [diff] [blame] | 4326 | DCHECK(Thread::Current()->CanLoadClasses()); |
Elliott Hughes | 4dd9b4d | 2011-12-12 18:29:24 -0800 | [diff] [blame] | 4327 | if (VLOG_IS_ON(class_linker)) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4328 | ObjPtr<mirror::DexCache> dex_cache = klass->GetDexCache(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 4329 | std::string source; |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4330 | if (dex_cache != nullptr) { |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 4331 | source += " from "; |
| 4332 | source += dex_cache->GetLocation()->ToModifiedUtf8(); |
| 4333 | } |
| 4334 | LOG(INFO) << "Loaded class " << descriptor << source; |
| 4335 | } |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 4336 | { |
| 4337 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Vladimir Marko | 0984e48 | 2019-03-27 16:41:41 +0000 | [diff] [blame] | 4338 | const ObjPtr<mirror::ClassLoader> class_loader = klass->GetClassLoader(); |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 4339 | ClassTable* const class_table = InsertClassTableForClassLoader(class_loader); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4340 | ObjPtr<mirror::Class> existing = class_table->Lookup(descriptor, hash); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4341 | if (existing != nullptr) { |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 4342 | return existing; |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 4343 | } |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 4344 | VerifyObject(klass); |
| 4345 | class_table->InsertWithHash(klass, hash); |
| 4346 | if (class_loader != nullptr) { |
| 4347 | // 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] | 4348 | WriteBarrier::ForEveryFieldWrite(class_loader); |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 4349 | } |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 4350 | if (log_new_roots_) { |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 4351 | new_class_roots_.push_back(GcRoot<mirror::Class>(klass)); |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4352 | } |
| 4353 | } |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 4354 | if (kIsDebugBuild) { |
| 4355 | // Test that copied methods correctly can find their holder. |
| 4356 | for (ArtMethod& method : klass->GetCopiedMethods(image_pointer_size_)) { |
| 4357 | CHECK_EQ(GetHoldingClassOfCopiedMethod(&method), klass); |
| 4358 | } |
Mathieu Chartier | 893263b | 2014-03-04 11:07:42 -0800 | [diff] [blame] | 4359 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4360 | return nullptr; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 4361 | } |
| 4362 | |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 4363 | void ClassLinker::WriteBarrierForBootOatFileBssRoots(const OatFile* oat_file) { |
Mathieu Chartier | a1467d0 | 2017-02-22 09:22:50 -0800 | [diff] [blame] | 4364 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
| 4365 | DCHECK(!oat_file->GetBssGcRoots().empty()) << oat_file->GetLocation(); |
| 4366 | if (log_new_roots_ && !ContainsElement(new_bss_roots_boot_oat_files_, oat_file)) { |
| 4367 | new_bss_roots_boot_oat_files_.push_back(oat_file); |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 4368 | } |
| 4369 | } |
| 4370 | |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4371 | // TODO This should really be in mirror::Class. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4372 | void ClassLinker::UpdateClassMethods(ObjPtr<mirror::Class> klass, |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4373 | LengthPrefixedArray<ArtMethod>* new_methods) { |
| 4374 | klass->SetMethodsPtrUnchecked(new_methods, |
| 4375 | klass->NumDirectMethods(), |
| 4376 | klass->NumDeclaredVirtualMethods()); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4377 | // 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] | 4378 | WriteBarrier::ForEveryFieldWrite(klass); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4379 | } |
| 4380 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 4381 | ObjPtr<mirror::Class> ClassLinker::LookupClass(Thread* self, |
| 4382 | const char* descriptor, |
| 4383 | ObjPtr<mirror::ClassLoader> class_loader) { |
Andreas Gampe | 2ff3b97 | 2017-06-05 18:14:53 -0700 | [diff] [blame] | 4384 | return LookupClass(self, descriptor, ComputeModifiedUtf8Hash(descriptor), class_loader); |
| 4385 | } |
| 4386 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 4387 | ObjPtr<mirror::Class> ClassLinker::LookupClass(Thread* self, |
| 4388 | const char* descriptor, |
| 4389 | size_t hash, |
| 4390 | ObjPtr<mirror::ClassLoader> class_loader) { |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 4391 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 4392 | ClassTable* const class_table = ClassTableForClassLoader(class_loader); |
| 4393 | if (class_table != nullptr) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4394 | ObjPtr<mirror::Class> result = class_table->Lookup(descriptor, hash); |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 4395 | if (result != nullptr) { |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 4396 | return result; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4397 | } |
Sameer Abu Asal | 2c6de22 | 2013-05-02 17:38:59 -0700 | [diff] [blame] | 4398 | } |
Vladimir Marko | 1a1de67 | 2016-10-13 12:53:15 +0100 | [diff] [blame] | 4399 | return nullptr; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4400 | } |
| 4401 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4402 | class MoveClassTableToPreZygoteVisitor : public ClassLoaderVisitor { |
| 4403 | public: |
Igor Murashkin | 2ffb703 | 2017-11-08 13:35:21 -0800 | [diff] [blame] | 4404 | MoveClassTableToPreZygoteVisitor() {} |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4405 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4406 | void Visit(ObjPtr<mirror::ClassLoader> class_loader) |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4407 | REQUIRES(Locks::classlinker_classes_lock_) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 4408 | REQUIRES_SHARED(Locks::mutator_lock_) override { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4409 | ClassTable* const class_table = class_loader->GetClassTable(); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 4410 | if (class_table != nullptr) { |
| 4411 | class_table->FreezeSnapshot(); |
| 4412 | } |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 4413 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4414 | }; |
| 4415 | |
| 4416 | void ClassLinker::MoveClassTableToPreZygote() { |
| 4417 | WriterMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 4418 | boot_class_table_->FreezeSnapshot(); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4419 | MoveClassTableToPreZygoteVisitor visitor; |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 4420 | VisitClassLoaders(&visitor); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 4421 | } |
| 4422 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4423 | // Look up classes by hash and descriptor and put all matching ones in the result array. |
| 4424 | class LookupClassesVisitor : public ClassLoaderVisitor { |
| 4425 | public: |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4426 | LookupClassesVisitor(const char* descriptor, |
| 4427 | size_t hash, |
| 4428 | std::vector<ObjPtr<mirror::Class>>* result) |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4429 | : descriptor_(descriptor), |
| 4430 | hash_(hash), |
| 4431 | result_(result) {} |
| 4432 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4433 | void Visit(ObjPtr<mirror::ClassLoader> class_loader) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 4434 | REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_) override { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4435 | ClassTable* const class_table = class_loader->GetClassTable(); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4436 | ObjPtr<mirror::Class> klass = class_table->Lookup(descriptor_, hash_); |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 4437 | // Add `klass` only if `class_loader` is its defining (not just initiating) class loader. |
| 4438 | if (klass != nullptr && klass->GetClassLoader() == class_loader) { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4439 | result_->push_back(klass); |
| 4440 | } |
| 4441 | } |
| 4442 | |
| 4443 | private: |
| 4444 | const char* const descriptor_; |
| 4445 | const size_t hash_; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4446 | std::vector<ObjPtr<mirror::Class>>* const result_; |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4447 | }; |
| 4448 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4449 | void ClassLinker::LookupClasses(const char* descriptor, |
| 4450 | std::vector<ObjPtr<mirror::Class>>& result) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 4451 | result.clear(); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4452 | Thread* const self = Thread::Current(); |
| 4453 | ReaderMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 4454 | const size_t hash = ComputeModifiedUtf8Hash(descriptor); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 4455 | ObjPtr<mirror::Class> klass = boot_class_table_->Lookup(descriptor, hash); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 4456 | if (klass != nullptr) { |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 4457 | DCHECK(klass->GetClassLoader() == nullptr); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 4458 | result.push_back(klass); |
| 4459 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 4460 | LookupClassesVisitor visitor(descriptor, hash, &result); |
| 4461 | VisitClassLoaders(&visitor); |
Elliott Hughes | 6fa602d | 2011-12-02 17:54:25 -0800 | [diff] [blame] | 4462 | } |
| 4463 | |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4464 | bool ClassLinker::AttemptSupertypeVerification(Thread* self, |
| 4465 | Handle<mirror::Class> klass, |
| 4466 | Handle<mirror::Class> supertype) { |
| 4467 | DCHECK(self != nullptr); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4468 | DCHECK(klass != nullptr); |
| 4469 | DCHECK(supertype != nullptr); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4470 | |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4471 | if (!supertype->IsVerified() && !supertype->IsErroneous()) { |
| 4472 | VerifyClass(self, supertype); |
| 4473 | } |
Nicolas Geoffray | 7cc3ae5 | 2017-03-07 14:33:37 +0000 | [diff] [blame] | 4474 | |
Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 4475 | if (supertype->IsVerified() |
| 4476 | || supertype->ShouldVerifyAtRuntime() |
| 4477 | || supertype->IsVerifiedNeedsAccessChecks()) { |
Nicolas Geoffray | 7cc3ae5 | 2017-03-07 14:33:37 +0000 | [diff] [blame] | 4478 | // The supertype is either verified, or we soft failed at AOT time. |
| 4479 | DCHECK(supertype->IsVerified() || Runtime::Current()->IsAotCompiler()); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4480 | return true; |
| 4481 | } |
| 4482 | // If we got this far then we have a hard failure. |
| 4483 | std::string error_msg = |
| 4484 | StringPrintf("Rejecting class %s that attempts to sub-type erroneous class %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4485 | klass->PrettyDescriptor().c_str(), |
| 4486 | supertype->PrettyDescriptor().c_str()); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4487 | LOG(WARNING) << error_msg << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8(); |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4488 | StackHandleScope<1> hs(self); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4489 | Handle<mirror::Throwable> cause(hs.NewHandle(self->GetException())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4490 | if (cause != nullptr) { |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4491 | // Set during VerifyClass call (if at all). |
| 4492 | self->ClearException(); |
| 4493 | } |
| 4494 | // Change into a verify error. |
| 4495 | ThrowVerifyError(klass.Get(), "%s", error_msg.c_str()); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4496 | if (cause != nullptr) { |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4497 | self->GetException()->SetCause(cause.Get()); |
| 4498 | } |
| 4499 | ClassReference ref(klass->GetDexCache()->GetDexFile(), klass->GetDexClassDefIndex()); |
| 4500 | if (Runtime::Current()->IsAotCompiler()) { |
| 4501 | Runtime::Current()->GetCompilerCallbacks()->ClassRejected(ref); |
| 4502 | } |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4503 | // Need to grab the lock to change status. |
| 4504 | ObjectLock<mirror::Class> super_lock(self, klass); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4505 | mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4506 | return false; |
| 4507 | } |
| 4508 | |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4509 | verifier::FailureKind ClassLinker::VerifyClass( |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame] | 4510 | Thread* self, Handle<mirror::Class> klass, verifier::HardFailLogMode log_level) { |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4511 | { |
| 4512 | // TODO: assert that the monitor on the Class is held |
| 4513 | ObjectLock<mirror::Class> lock(self, klass); |
Elliott Hughes | d9c67be | 2012-02-02 19:54:06 -0800 | [diff] [blame] | 4514 | |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4515 | // Is somebody verifying this now? |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4516 | ClassStatus old_status = klass->GetStatus(); |
Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 4517 | while (old_status == ClassStatus::kVerifying) { |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4518 | lock.WaitIgnoringInterrupts(); |
Mathieu Chartier | 5ef7020 | 2017-06-29 10:45:10 -0700 | [diff] [blame] | 4519 | // WaitIgnoringInterrupts can still receive an interrupt and return early, in this |
| 4520 | // case we may see the same status again. b/62912904. This is why the check is |
| 4521 | // greater or equal. |
| 4522 | CHECK(klass->IsErroneous() || (klass->GetStatus() >= old_status)) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4523 | << "Class '" << klass->PrettyClass() |
| 4524 | << "' performed an illegal verification state transition from " << old_status |
| 4525 | << " to " << klass->GetStatus(); |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4526 | old_status = klass->GetStatus(); |
| 4527 | } |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 4528 | |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4529 | // The class might already be erroneous, for example at compile time if we attempted to verify |
| 4530 | // this class as a parent to another. |
| 4531 | if (klass->IsErroneous()) { |
| 4532 | ThrowEarlierClassFailure(klass.Get()); |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4533 | return verifier::FailureKind::kHardFailure; |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4534 | } |
Brian Carlstrom | 9b5ee88 | 2012-02-28 09:48:54 -0800 | [diff] [blame] | 4535 | |
Nicolas Geoffray | 7cc3ae5 | 2017-03-07 14:33:37 +0000 | [diff] [blame] | 4536 | // Don't attempt to re-verify if already verified. |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4537 | if (klass->IsVerified()) { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 4538 | EnsureSkipAccessChecksMethods(klass, image_pointer_size_); |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4539 | return verifier::FailureKind::kNoFailure; |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4540 | } |
Nicolas Geoffray | 7cc3ae5 | 2017-03-07 14:33:37 +0000 | [diff] [blame] | 4541 | |
Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 4542 | if (klass->IsVerifiedNeedsAccessChecks()) { |
| 4543 | if (!Runtime::Current()->IsAotCompiler()) { |
| 4544 | // Mark the class as having a verification attempt to avoid re-running |
| 4545 | // the verifier and avoid calling EnsureSkipAccessChecksMethods. |
| 4546 | klass->SetVerificationAttempted(); |
| 4547 | mirror::Class::SetStatus(klass, ClassStatus::kVerified, self); |
| 4548 | } |
| 4549 | return verifier::FailureKind::kAccessChecksFailure; |
| 4550 | } |
| 4551 | |
Nicolas Geoffray | 7cc3ae5 | 2017-03-07 14:33:37 +0000 | [diff] [blame] | 4552 | // For AOT, don't attempt to re-verify if we have already found we should |
| 4553 | // verify at runtime. |
Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 4554 | if (klass->ShouldVerifyAtRuntime()) { |
| 4555 | CHECK(Runtime::Current()->IsAotCompiler()); |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4556 | return verifier::FailureKind::kSoftFailure; |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4557 | } |
jeffhao | 98eacac | 2011-09-14 16:11:53 -0700 | [diff] [blame] | 4558 | |
Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 4559 | DCHECK_EQ(klass->GetStatus(), ClassStatus::kResolved); |
| 4560 | mirror::Class::SetStatus(klass, ClassStatus::kVerifying, self); |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4561 | |
| 4562 | // Skip verification if disabled. |
| 4563 | if (!Runtime::Current()->IsVerificationEnabled()) { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4564 | mirror::Class::SetStatus(klass, ClassStatus::kVerified, self); |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 4565 | EnsureSkipAccessChecksMethods(klass, image_pointer_size_); |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4566 | return verifier::FailureKind::kNoFailure; |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4567 | } |
Jeff Hao | 4a200f5 | 2014-04-01 14:58:49 -0700 | [diff] [blame] | 4568 | } |
| 4569 | |
Bharadwaj Kalandhabhatta | 271c1e1 | 2017-06-27 11:14:49 -0700 | [diff] [blame] | 4570 | VLOG(class_linker) << "Beginning verification for class: " |
| 4571 | << klass->PrettyDescriptor() |
| 4572 | << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8(); |
| 4573 | |
Ian Rogers | 9ffb039 | 2012-09-10 11:56:50 -0700 | [diff] [blame] | 4574 | // Verify super class. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4575 | StackHandleScope<2> hs(self); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4576 | MutableHandle<mirror::Class> supertype(hs.NewHandle(klass->GetSuperClass())); |
| 4577 | // If we have a superclass and we get a hard verification failure we can return immediately. |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4578 | if (supertype != nullptr && !AttemptSupertypeVerification(self, klass, supertype)) { |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4579 | CHECK(self->IsExceptionPending()) << "Verification error should be pending."; |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4580 | return verifier::FailureKind::kHardFailure; |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4581 | } |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 4582 | |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4583 | // Verify all default super-interfaces. |
| 4584 | // |
| 4585 | // (1) Don't bother if the superclass has already had a soft verification failure. |
| 4586 | // |
| 4587 | // (2) Interfaces shouldn't bother to do this recursive verification because they cannot cause |
| 4588 | // recursive initialization by themselves. This is because when an interface is initialized |
| 4589 | // directly it must not initialize its superinterfaces. We are allowed to verify regardless |
| 4590 | // but choose not to for an optimization. If the interfaces is being verified due to a class |
| 4591 | // initialization (which would need all the default interfaces to be verified) the class code |
| 4592 | // will trigger the recursive verification anyway. |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4593 | if ((supertype == nullptr || supertype->IsVerified()) // See (1) |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4594 | && !klass->IsInterface()) { // See (2) |
| 4595 | int32_t iftable_count = klass->GetIfTableCount(); |
| 4596 | MutableHandle<mirror::Class> iface(hs.NewHandle<mirror::Class>(nullptr)); |
| 4597 | // Loop through all interfaces this class has defined. It doesn't matter the order. |
| 4598 | for (int32_t i = 0; i < iftable_count; i++) { |
| 4599 | iface.Assign(klass->GetIfTable()->GetInterface(i)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4600 | DCHECK(iface != nullptr); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4601 | // We only care if we have default interfaces and can skip if we are already verified... |
| 4602 | if (LIKELY(!iface->HasDefaultMethods() || iface->IsVerified())) { |
| 4603 | continue; |
| 4604 | } else if (UNLIKELY(!AttemptSupertypeVerification(self, klass, iface))) { |
| 4605 | // We had a hard failure while verifying this interface. Just return immediately. |
| 4606 | CHECK(self->IsExceptionPending()) << "Verification error should be pending."; |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4607 | return verifier::FailureKind::kHardFailure; |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4608 | } else if (UNLIKELY(!iface->IsVerified())) { |
| 4609 | // We softly failed to verify the iface. Stop checking and clean up. |
| 4610 | // Put the iface into the supertype handle so we know what caused us to fail. |
| 4611 | supertype.Assign(iface.Get()); |
| 4612 | break; |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 4613 | } |
Ian Rogers | 1c5eb70 | 2012-02-01 09:18:34 -0800 | [diff] [blame] | 4614 | } |
| 4615 | } |
| 4616 | |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4617 | // At this point if verification failed, then supertype is the "first" supertype that failed |
| 4618 | // verification (without a specific order). If verification succeeded, then supertype is either |
| 4619 | // null or the original superclass of klass and is verified. |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 4620 | DCHECK(supertype == nullptr || |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4621 | supertype.Get() == klass->GetSuperClass() || |
| 4622 | !supertype->IsVerified()); |
| 4623 | |
Elliott Hughes | 634eb2e | 2012-03-22 16:06:28 -0700 | [diff] [blame] | 4624 | // 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] | 4625 | const DexFile& dex_file = *klass->GetDexCache()->GetDexFile(); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4626 | ClassStatus oat_file_class_status(ClassStatus::kNotReady); |
Nicolas Geoffray | 6df4511 | 2021-02-07 21:51:58 +0000 | [diff] [blame^] | 4627 | bool preverified = VerifyClassUsingOatFile(self, dex_file, klass, oat_file_class_status); |
Bharadwaj Kalandhabhatta | 271c1e1 | 2017-06-27 11:14:49 -0700 | [diff] [blame] | 4628 | |
| 4629 | VLOG(class_linker) << "Class preverified status for class " |
| 4630 | << klass->PrettyDescriptor() |
| 4631 | << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8() |
| 4632 | << ": " |
Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 4633 | << preverified |
| 4634 | << "( " << oat_file_class_status << ")"; |
Bharadwaj Kalandhabhatta | 271c1e1 | 2017-06-27 11:14:49 -0700 | [diff] [blame] | 4635 | |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 4636 | // If the oat file says the class had an error, re-run the verifier. That way we will get a |
| 4637 | // precise error message. To ensure a rerun, test: |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 4638 | // mirror::Class::IsErroneous(oat_file_class_status) => !preverified |
| 4639 | DCHECK(!mirror::Class::IsErroneous(oat_file_class_status) || !preverified); |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 4640 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4641 | std::string error_msg; |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4642 | verifier::FailureKind verifier_failure = verifier::FailureKind::kNoFailure; |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 4643 | if (!preverified) { |
Mathieu Chartier | 9e050df | 2017-08-09 10:05:47 -0700 | [diff] [blame] | 4644 | verifier_failure = PerformClassVerification(self, klass, log_level, &error_msg); |
jeffhao | f1e6b7c | 2012-06-05 18:33:30 -0700 | [diff] [blame] | 4645 | } |
Andreas Gampe | 884f3b8 | 2016-03-30 19:52:58 -0700 | [diff] [blame] | 4646 | |
| 4647 | // Verification is done, grab the lock again. |
| 4648 | ObjectLock<mirror::Class> lock(self, klass); |
| 4649 | |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4650 | if (preverified || verifier_failure != verifier::FailureKind::kHardFailure) { |
| 4651 | if (!preverified && verifier_failure != verifier::FailureKind::kNoFailure) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4652 | VLOG(class_linker) << "Soft verification failure in class " |
| 4653 | << klass->PrettyDescriptor() |
| 4654 | << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8() |
| 4655 | << " because: " << error_msg; |
Ian Rogers | 529781d | 2012-07-23 17:24:29 -0700 | [diff] [blame] | 4656 | } |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 4657 | self->AssertNoPendingException(); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 4658 | // Make sure all classes referenced by catch blocks are resolved. |
Alex Light | 5a55986 | 2016-01-29 12:24:48 -0800 | [diff] [blame] | 4659 | ResolveClassExceptionHandlerTypes(klass); |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4660 | if (verifier_failure == verifier::FailureKind::kNoFailure) { |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4661 | // Even though there were no verifier failures we need to respect whether the super-class and |
| 4662 | // super-default-interfaces were verified or requiring runtime reverification. |
Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 4663 | if (supertype == nullptr |
| 4664 | || supertype->IsVerified() |
| 4665 | || supertype->IsVerifiedNeedsAccessChecks()) { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4666 | mirror::Class::SetStatus(klass, ClassStatus::kVerified, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4667 | } else { |
Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 4668 | CHECK(Runtime::Current()->IsAotCompiler()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4669 | CHECK_EQ(supertype->GetStatus(), ClassStatus::kRetryVerificationAtRuntime); |
| 4670 | mirror::Class::SetStatus(klass, ClassStatus::kRetryVerificationAtRuntime, self); |
Alex Light | f1f1049 | 2015-10-07 16:08:36 -0700 | [diff] [blame] | 4671 | // Pretend a soft failure occurred so that we don't consider the class verified below. |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4672 | verifier_failure = verifier::FailureKind::kSoftFailure; |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 4673 | } |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 4674 | } else { |
Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 4675 | CHECK(verifier_failure == verifier::FailureKind::kSoftFailure || |
Nicolas Geoffray | d1728bf | 2021-01-12 14:02:29 +0000 | [diff] [blame] | 4676 | verifier_failure == verifier::FailureKind::kTypeChecksFailure || |
Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 4677 | verifier_failure == verifier::FailureKind::kAccessChecksFailure); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 4678 | // Soft failures at compile time should be retried at runtime. Soft |
| 4679 | // failures at runtime will be handled by slow paths in the generated |
| 4680 | // code. Set status accordingly. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 4681 | if (Runtime::Current()->IsAotCompiler()) { |
Nicolas Geoffray | d1728bf | 2021-01-12 14:02:29 +0000 | [diff] [blame] | 4682 | if (verifier_failure == verifier::FailureKind::kSoftFailure || |
| 4683 | verifier_failure == verifier::FailureKind::kTypeChecksFailure) { |
Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 4684 | mirror::Class::SetStatus(klass, ClassStatus::kRetryVerificationAtRuntime, self); |
| 4685 | } else { |
| 4686 | mirror::Class::SetStatus(klass, ClassStatus::kVerifiedNeedsAccessChecks, self); |
| 4687 | } |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 4688 | } else { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4689 | mirror::Class::SetStatus(klass, ClassStatus::kVerified, self); |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 4690 | // As this is a fake verified status, make sure the methods are _not_ marked |
| 4691 | // kAccSkipAccessChecks later. |
| 4692 | klass->SetVerificationAttempted(); |
jeffhao | e4f0b2a | 2012-08-30 11:18:57 -0700 | [diff] [blame] | 4693 | } |
| 4694 | } |
jeffhao | 5cfd6fb | 2011-09-27 13:54:29 -0700 | [diff] [blame] | 4695 | } else { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4696 | VLOG(verifier) << "Verification failed on class " << klass->PrettyDescriptor() |
Andreas Gampe | ec6e6c1 | 2015-11-05 20:39:56 -0800 | [diff] [blame] | 4697 | << " in " << klass->GetDexCache()->GetLocation()->ToModifiedUtf8() |
| 4698 | << " because: " << error_msg; |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 4699 | self->AssertNoPendingException(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 4700 | ThrowVerifyError(klass.Get(), "%s", error_msg.c_str()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4701 | mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self); |
jeffhao | 5cfd6fb | 2011-09-27 13:54:29 -0700 | [diff] [blame] | 4702 | } |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 4703 | if (preverified || verifier_failure == verifier::FailureKind::kNoFailure) { |
Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 4704 | if (oat_file_class_status == ClassStatus::kVerifiedNeedsAccessChecks || |
| 4705 | UNLIKELY(Runtime::Current()->IsVerificationSoftFail())) { |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 4706 | // Never skip access checks if the verification soft fail is forced. |
| 4707 | // Mark the class as having a verification attempt to avoid re-running the verifier. |
| 4708 | klass->SetVerificationAttempted(); |
| 4709 | } else { |
Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 4710 | // Class is verified so we don't need to do any access check on its methods. |
| 4711 | // Let the interpreter know it by setting the kAccSkipAccessChecks flag onto each |
| 4712 | // method. |
| 4713 | // Note: we're going here during compilation and at runtime. When we set the |
| 4714 | // kAccSkipAccessChecks flag when compiling image classes, the flag is recorded |
| 4715 | // in the image and is set when loading the image. |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 4716 | EnsureSkipAccessChecksMethods(klass, image_pointer_size_); |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 4717 | } |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 4718 | } |
Nicolas Geoffray | 486dda0 | 2017-09-11 14:15:52 +0100 | [diff] [blame] | 4719 | // Done verifying. Notify the compiler about the verification status, in case the class |
| 4720 | // was verified implicitly (eg super class of a compiled class). |
| 4721 | if (Runtime::Current()->IsAotCompiler()) { |
| 4722 | Runtime::Current()->GetCompilerCallbacks()->UpdateClassState( |
| 4723 | ClassReference(&klass->GetDexFile(), klass->GetDexClassDefIndex()), klass->GetStatus()); |
| 4724 | } |
Nicolas Geoffray | 0802518 | 2016-10-25 17:20:18 +0100 | [diff] [blame] | 4725 | return verifier_failure; |
Andreas Gampe | 4849859 | 2014-09-10 19:48:05 -0700 | [diff] [blame] | 4726 | } |
| 4727 | |
Mathieu Chartier | 9e050df | 2017-08-09 10:05:47 -0700 | [diff] [blame] | 4728 | verifier::FailureKind ClassLinker::PerformClassVerification(Thread* self, |
| 4729 | Handle<mirror::Class> klass, |
| 4730 | verifier::HardFailLogMode log_level, |
| 4731 | std::string* error_msg) { |
| 4732 | Runtime* const runtime = Runtime::Current(); |
Andreas Gampe | a43ba3d | 2019-03-13 15:49:20 -0700 | [diff] [blame] | 4733 | return verifier::ClassVerifier::VerifyClass(self, |
| 4734 | klass.Get(), |
| 4735 | runtime->GetCompilerCallbacks(), |
| 4736 | runtime->IsAotCompiler(), |
| 4737 | log_level, |
| 4738 | Runtime::Current()->GetTargetSdkVersion(), |
| 4739 | error_msg); |
Mathieu Chartier | 9e050df | 2017-08-09 10:05:47 -0700 | [diff] [blame] | 4740 | } |
| 4741 | |
Nicolas Geoffray | 6df4511 | 2021-02-07 21:51:58 +0000 | [diff] [blame^] | 4742 | bool ClassLinker::VerifyClassUsingOatFile(Thread* self, |
| 4743 | const DexFile& dex_file, |
| 4744 | Handle<mirror::Class> klass, |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4745 | ClassStatus& oat_file_class_status) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 4746 | // If we're compiling, we can only verify the class using the oat file if |
| 4747 | // 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] | 4748 | // the compilation unit (app - dependencies). We will let the compiler callback |
| 4749 | // tell us about the latter. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 4750 | if (Runtime::Current()->IsAotCompiler()) { |
Andreas Gampe | e993458 | 2018-01-19 21:23:04 -0800 | [diff] [blame] | 4751 | CompilerCallbacks* callbacks = Runtime::Current()->GetCompilerCallbacks(); |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 4752 | // We are compiling an app (not the image). |
Nicolas Geoffray | 6df4511 | 2021-02-07 21:51:58 +0000 | [diff] [blame^] | 4753 | if (!callbacks->CanUseOatStatusForVerification(klass.Get())) { |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 4754 | return false; |
| 4755 | } |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4756 | } |
Anwar Ghuloum | 044d283 | 2013-07-17 15:22:31 -0700 | [diff] [blame] | 4757 | |
Andreas Gampe | b40d361 | 2018-06-26 15:49:42 -0700 | [diff] [blame] | 4758 | const OatDexFile* oat_dex_file = dex_file.GetOatDexFile(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4759 | // 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] | 4760 | if (oat_dex_file == nullptr || oat_dex_file->GetOatFile() == nullptr) { |
Anwar Ghuloum | ad256bb | 2013-07-18 14:58:55 -0700 | [diff] [blame] | 4761 | return false; |
| 4762 | } |
| 4763 | |
Ian Rogers | 8b2c0b9 | 2013-09-19 02:56:49 -0700 | [diff] [blame] | 4764 | uint16_t class_def_index = klass->GetDexClassDefIndex(); |
Vladimir Marko | d3c5beb | 2014-04-11 16:32:51 +0100 | [diff] [blame] | 4765 | oat_file_class_status = oat_dex_file->GetOatClass(class_def_index).GetStatus(); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4766 | if (oat_file_class_status >= ClassStatus::kVerified) { |
Mathieu Chartier | a079e3a | 2016-03-16 19:08:31 -0700 | [diff] [blame] | 4767 | return true; |
| 4768 | } |
Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 4769 | if (oat_file_class_status >= ClassStatus::kVerifiedNeedsAccessChecks) { |
| 4770 | // We return that the clas has already been verified, and the caller should |
| 4771 | // check the class status to ensure we run with access checks. |
| 4772 | return true; |
| 4773 | } |
Nicolas Geoffray | 6df4511 | 2021-02-07 21:51:58 +0000 | [diff] [blame^] | 4774 | |
| 4775 | // Check the class status with the vdex file. |
| 4776 | const OatFile* oat_file = oat_dex_file->GetOatFile(); |
| 4777 | if (oat_file != nullptr) { |
| 4778 | oat_file_class_status = oat_file->GetVdexFile()->ComputeClassStatus(self, klass); |
| 4779 | if (oat_file_class_status >= ClassStatus::kVerifiedNeedsAccessChecks) { |
| 4780 | return true; |
| 4781 | } |
| 4782 | } |
| 4783 | |
Mathieu Chartier | a079e3a | 2016-03-16 19:08:31 -0700 | [diff] [blame] | 4784 | // If we only verified a subset of the classes at compile time, we can end up with classes that |
| 4785 | // were resolved by the verifier. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4786 | if (oat_file_class_status == ClassStatus::kResolved) { |
Mathieu Chartier | a079e3a | 2016-03-16 19:08:31 -0700 | [diff] [blame] | 4787 | return false; |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4788 | } |
Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 4789 | // We never expect a .oat file to have kRetryVerificationAtRuntime statuses. |
| 4790 | CHECK_NE(oat_file_class_status, ClassStatus::kRetryVerificationAtRuntime) |
| 4791 | << klass->PrettyClass() << " " << dex_file.GetLocation(); |
| 4792 | |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 4793 | if (mirror::Class::IsErroneous(oat_file_class_status)) { |
jeffhao | 1ac2944 | 2012-03-26 11:37:32 -0700 | [diff] [blame] | 4794 | // Compile time verification failed with a hard error. This is caused by invalid instructions |
| 4795 | // in the class. These errors are unrecoverable. |
| 4796 | return false; |
| 4797 | } |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4798 | if (oat_file_class_status == ClassStatus::kNotReady) { |
Ian Rogers | c476227 | 2012-02-01 15:55:55 -0800 | [diff] [blame] | 4799 | // Status is uninitialized if we couldn't determine the status at compile time, for example, |
| 4800 | // not loading the class. |
| 4801 | // TODO: when the verifier doesn't rely on Class-es failing to resolve/load the type hierarchy |
| 4802 | // isn't a problem and this case shouldn't occur |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4803 | return false; |
| 4804 | } |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 4805 | std::string temp; |
Elliott Hughes | 634eb2e | 2012-03-22 16:06:28 -0700 | [diff] [blame] | 4806 | LOG(FATAL) << "Unexpected class status: " << oat_file_class_status |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4807 | << " " << dex_file.GetLocation() << " " << klass->PrettyClass() << " " |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 4808 | << klass->GetDescriptor(&temp); |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 4809 | UNREACHABLE(); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4810 | } |
| 4811 | |
Alex Light | 5a55986 | 2016-01-29 12:24:48 -0800 | [diff] [blame] | 4812 | void ClassLinker::ResolveClassExceptionHandlerTypes(Handle<mirror::Class> klass) { |
Alex Light | 51a64d5 | 2015-12-17 13:55:59 -0800 | [diff] [blame] | 4813 | for (ArtMethod& method : klass->GetMethods(image_pointer_size_)) { |
Alex Light | 5a55986 | 2016-01-29 12:24:48 -0800 | [diff] [blame] | 4814 | ResolveMethodExceptionHandlerTypes(&method); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4815 | } |
| 4816 | } |
| 4817 | |
Alex Light | 5a55986 | 2016-01-29 12:24:48 -0800 | [diff] [blame] | 4818 | void ClassLinker::ResolveMethodExceptionHandlerTypes(ArtMethod* method) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4819 | // similar to DexVerifier::ScanTryCatchBlocks and dex2oat's ResolveExceptionsForMethod. |
David Sehr | 0225f8e | 2018-01-31 08:52:24 +0000 | [diff] [blame] | 4820 | CodeItemDataAccessor accessor(method->DexInstructionData()); |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 4821 | if (!accessor.HasCodeItem()) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4822 | return; // native or abstract method |
| 4823 | } |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 4824 | if (accessor.TriesSize() == 0) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4825 | return; // nothing to process |
| 4826 | } |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 4827 | const uint8_t* handlers_ptr = accessor.GetCatchHandlerData(0); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4828 | uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr); |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4829 | for (uint32_t idx = 0; idx < handlers_size; idx++) { |
| 4830 | CatchHandlerIterator iterator(handlers_ptr); |
| 4831 | for (; iterator.HasNext(); iterator.Next()) { |
| 4832 | // Ensure exception types are resolved so that they don't need resolution to be delivered, |
| 4833 | // unresolved exception types will be ignored by exception delivery |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 4834 | if (iterator.GetHandlerTypeIndex().IsValid()) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 4835 | ObjPtr<mirror::Class> exception_type = ResolveType(iterator.GetHandlerTypeIndex(), method); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4836 | if (exception_type == nullptr) { |
Brian Carlstrom | e7d856b | 2012-01-11 18:10:55 -0800 | [diff] [blame] | 4837 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 4838 | Thread::Current()->ClearException(); |
| 4839 | } |
| 4840 | } |
| 4841 | } |
| 4842 | handlers_ptr = iterator.EndDataPointer(); |
| 4843 | } |
| 4844 | } |
| 4845 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 4846 | ObjPtr<mirror::Class> ClassLinker::CreateProxyClass(ScopedObjectAccessAlreadyRunnable& soa, |
| 4847 | jstring name, |
| 4848 | jobjectArray interfaces, |
| 4849 | jobject loader, |
| 4850 | jobjectArray methods, |
| 4851 | jobjectArray throws) { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 4852 | Thread* self = soa.Self(); |
Alex Light | e9f6103 | 2018-09-24 16:04:51 -0700 | [diff] [blame] | 4853 | |
| 4854 | // This is to prevent the calls to ClassLoad and ClassPrepare which can cause java/user-supplied |
| 4855 | // code to be executed. We put it up here so we can avoid all the allocations associated with |
| 4856 | // creating the class. This can happen with (eg) jit-threads. |
| 4857 | if (!self->CanLoadClasses()) { |
| 4858 | // Make sure we don't try to load anything, potentially causing an infinite loop. |
| 4859 | ObjPtr<mirror::Throwable> pre_allocated = |
| 4860 | Runtime::Current()->GetPreAllocatedNoClassDefFoundError(); |
| 4861 | self->SetException(pre_allocated); |
| 4862 | return nullptr; |
| 4863 | } |
| 4864 | |
Alex Light | 133987d | 2020-03-26 19:22:12 +0000 | [diff] [blame] | 4865 | StackHandleScope<12> hs(self); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4866 | MutableHandle<mirror::Class> temp_klass(hs.NewHandle( |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 4867 | AllocClass(self, GetClassRoot<mirror::Class>(this), sizeof(mirror::Class)))); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4868 | if (temp_klass == nullptr) { |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4869 | CHECK(self->IsExceptionPending()); // OOME. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 4870 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4871 | } |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4872 | DCHECK(temp_klass->GetClass() != nullptr); |
| 4873 | temp_klass->SetObjectSize(sizeof(mirror::Proxy)); |
Igor Murashkin | df707e4 | 2016-02-02 16:56:50 -0800 | [diff] [blame] | 4874 | // Set the class access flags incl. VerificationAttempted, so we do not try to set the flag on |
| 4875 | // the methods. |
Vladimir Marko | b68bb7a | 2020-03-17 10:55:25 +0000 | [diff] [blame] | 4876 | temp_klass->SetAccessFlagsDuringLinking( |
| 4877 | kAccClassIsProxy | kAccPublic | kAccFinal | kAccVerificationAttempted); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4878 | temp_klass->SetClassLoader(soa.Decode<mirror::ClassLoader>(loader)); |
| 4879 | DCHECK_EQ(temp_klass->GetPrimitiveType(), Primitive::kPrimNot); |
| 4880 | temp_klass->SetName(soa.Decode<mirror::String>(name)); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 4881 | temp_klass->SetDexCache(GetClassRoot<mirror::Proxy>(this)->GetDexCache()); |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 4882 | // Object has an empty iftable, copy it for that reason. |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 4883 | temp_klass->SetIfTable(GetClassRoot<mirror::Object>(this)->GetIfTable()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4884 | mirror::Class::SetStatus(temp_klass, ClassStatus::kIdx, self); |
Vladimir Marko | 3892e62 | 2019-03-15 15:22:18 +0000 | [diff] [blame] | 4885 | std::string storage; |
| 4886 | const char* descriptor = temp_klass->GetDescriptor(&storage); |
| 4887 | const size_t hash = ComputeModifiedUtf8Hash(descriptor); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4888 | |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 4889 | // 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] | 4890 | LinearAlloc* const allocator = GetOrCreateAllocatorForClassLoader(temp_klass->GetClassLoader()); |
Mathieu Chartier | d57d454 | 2015-10-14 10:55:30 -0700 | [diff] [blame] | 4891 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4892 | // Insert the class before loading the fields as the field roots |
| 4893 | // (ArtField::declaring_class_) are only visited from the class |
| 4894 | // table. There can't be any suspend points between inserting the |
| 4895 | // class and setting the field arrays below. |
Vladimir Marko | 3892e62 | 2019-03-15 15:22:18 +0000 | [diff] [blame] | 4896 | ObjPtr<mirror::Class> existing = InsertClass(descriptor, temp_klass.Get(), hash); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4897 | CHECK(existing == nullptr); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4898 | |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 4899 | // Instance fields are inherited, but we add a couple of static fields... |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4900 | const size_t num_fields = 2; |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 4901 | LengthPrefixedArray<ArtField>* sfields = AllocArtFieldArray(self, allocator, num_fields); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4902 | temp_klass->SetSFieldsPtr(sfields); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4903 | |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 4904 | // 1. Create a static field 'interfaces' that holds the _declared_ interfaces implemented by |
| 4905 | // our proxy, so Class.getInterfaces doesn't return the flattened set. |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4906 | ArtField& interfaces_sfield = sfields->At(0); |
| 4907 | interfaces_sfield.SetDexFieldIndex(0); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4908 | interfaces_sfield.SetDeclaringClass(temp_klass.Get()); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4909 | interfaces_sfield.SetAccessFlags(kAccStatic | kAccPublic | kAccFinal); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 4910 | |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 4911 | // 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] | 4912 | ArtField& throws_sfield = sfields->At(1); |
| 4913 | throws_sfield.SetDexFieldIndex(1); |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4914 | throws_sfield.SetDeclaringClass(temp_klass.Get()); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 4915 | throws_sfield.SetAccessFlags(kAccStatic | kAccPublic | kAccFinal); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4916 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 4917 | // Proxies have 1 direct method, the constructor |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4918 | const size_t num_direct_methods = 1; |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4919 | |
Alex Light | 133987d | 2020-03-26 19:22:12 +0000 | [diff] [blame] | 4920 | // The array we get passed contains all methods, including private and static |
| 4921 | // ones that aren't proxied. We need to filter those out since only interface |
| 4922 | // methods (non-private & virtual) are actually proxied. |
| 4923 | Handle<mirror::ObjectArray<mirror::Method>> h_methods = |
| 4924 | hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::Method>>(methods)); |
Vladimir Marko | 679730e | 2018-05-25 15:06:48 +0100 | [diff] [blame] | 4925 | DCHECK_EQ(h_methods->GetClass(), GetClassRoot<mirror::ObjectArray<mirror::Method>>()) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 4926 | << mirror::Class::PrettyClass(h_methods->GetClass()); |
Alex Light | 133987d | 2020-03-26 19:22:12 +0000 | [diff] [blame] | 4927 | // List of the actual virtual methods this class will have. |
| 4928 | std::vector<ArtMethod*> proxied_methods; |
| 4929 | std::vector<size_t> proxied_throws_idx; |
| 4930 | proxied_methods.reserve(h_methods->GetLength()); |
| 4931 | proxied_throws_idx.reserve(h_methods->GetLength()); |
| 4932 | // Filter out to only the non-private virtual methods. |
| 4933 | for (auto [mirror, idx] : ZipCount(h_methods.Iterate<mirror::Method>())) { |
| 4934 | ArtMethod* m = mirror->GetArtMethod(); |
| 4935 | if (!m->IsPrivate() && !m->IsStatic()) { |
| 4936 | proxied_methods.push_back(m); |
| 4937 | proxied_throws_idx.push_back(idx); |
| 4938 | } |
| 4939 | } |
| 4940 | const size_t num_virtual_methods = proxied_methods.size(); |
Alex Light | bc11509 | 2020-03-27 11:25:16 -0700 | [diff] [blame] | 4941 | // We also need to filter out the 'throws'. The 'throws' are a Class[][] that |
| 4942 | // contains an array of all the classes each function is declared to throw. |
| 4943 | // This is used to wrap unexpected exceptions in a |
| 4944 | // UndeclaredThrowableException exception. This array is in the same order as |
| 4945 | // the methods array and like the methods array must be filtered to remove any |
| 4946 | // non-proxied methods. |
Alex Light | 133987d | 2020-03-26 19:22:12 +0000 | [diff] [blame] | 4947 | const bool has_filtered_methods = |
| 4948 | static_cast<int32_t>(num_virtual_methods) != h_methods->GetLength(); |
| 4949 | MutableHandle<mirror::ObjectArray<mirror::ObjectArray<mirror::Class>>> original_proxied_throws( |
| 4950 | hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::ObjectArray<mirror::Class>>>(throws))); |
| 4951 | MutableHandle<mirror::ObjectArray<mirror::ObjectArray<mirror::Class>>> proxied_throws( |
| 4952 | hs.NewHandle<mirror::ObjectArray<mirror::ObjectArray<mirror::Class>>>( |
| 4953 | (has_filtered_methods) |
| 4954 | ? mirror::ObjectArray<mirror::ObjectArray<mirror::Class>>::Alloc( |
| 4955 | self, original_proxied_throws->GetClass(), num_virtual_methods) |
| 4956 | : original_proxied_throws.Get())); |
Alex Light | bc11509 | 2020-03-27 11:25:16 -0700 | [diff] [blame] | 4957 | if (proxied_throws.IsNull() && !original_proxied_throws.IsNull()) { |
| 4958 | self->AssertPendingOOMException(); |
| 4959 | return nullptr; |
| 4960 | } |
Alex Light | 133987d | 2020-03-26 19:22:12 +0000 | [diff] [blame] | 4961 | if (has_filtered_methods) { |
| 4962 | for (auto [orig_idx, new_idx] : ZipCount(MakeIterationRange(proxied_throws_idx))) { |
| 4963 | DCHECK_LE(new_idx, orig_idx); |
| 4964 | proxied_throws->Set(new_idx, original_proxied_throws->Get(orig_idx)); |
| 4965 | } |
| 4966 | } |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4967 | |
| 4968 | // Create the methods array. |
| 4969 | LengthPrefixedArray<ArtMethod>* proxy_class_methods = AllocArtMethodArray( |
| 4970 | self, allocator, num_direct_methods + num_virtual_methods); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4971 | // Currently AllocArtMethodArray cannot return null, but the OOM logic is left there in case we |
| 4972 | // want to throw OOM in the future. |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4973 | if (UNLIKELY(proxy_class_methods == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4974 | self->AssertPendingOOMException(); |
| 4975 | return nullptr; |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 4976 | } |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4977 | temp_klass->SetMethodsPtr(proxy_class_methods, num_direct_methods, num_virtual_methods); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4978 | |
| 4979 | // Create the single direct method. |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4980 | CreateProxyConstructor(temp_klass, temp_klass->GetDirectMethodUnchecked(0, image_pointer_size_)); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 4981 | |
| 4982 | // Create virtual method using specified prototypes. |
| 4983 | // TODO These should really use the iterators. |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4984 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4985 | auto* virtual_method = temp_klass->GetVirtualMethodUnchecked(i, image_pointer_size_); |
Alex Light | 133987d | 2020-03-26 19:22:12 +0000 | [diff] [blame] | 4986 | auto* prototype = proxied_methods[i]; |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4987 | CreateProxyMethod(temp_klass, prototype, virtual_method); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 4988 | DCHECK(virtual_method->GetDeclaringClass() != nullptr); |
| 4989 | DCHECK(prototype->GetDeclaringClass() != nullptr); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 4990 | } |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4991 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4992 | // The super class is java.lang.reflect.Proxy |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 4993 | temp_klass->SetSuperClass(GetClassRoot<mirror::Proxy>(this)); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 4994 | // Now effectively in the loaded state. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 4995 | mirror::Class::SetStatus(temp_klass, ClassStatus::kLoaded, self); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 4996 | self->AssertNoPendingException(); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 4997 | |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 4998 | // At this point the class is loaded. Publish a ClassLoad event. |
| 4999 | // Note: this may be a temporary class. It is a listener's responsibility to handle this. |
| 5000 | Runtime::Current()->GetRuntimeCallbacks()->ClassLoad(temp_klass); |
| 5001 | |
| 5002 | MutableHandle<mirror::Class> klass = hs.NewHandle<mirror::Class>(nullptr); |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 5003 | { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5004 | // Must hold lock on object when resolved. |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 5005 | ObjectLock<mirror::Class> resolution_lock(self, temp_klass); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 5006 | // Link the fields and virtual methods, creating vtable and iftables. |
| 5007 | // The new class will replace the old one in the class table. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5008 | Handle<mirror::ObjectArray<mirror::Class>> h_interfaces( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 5009 | hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::Class>>(interfaces))); |
Vladimir Marko | 3892e62 | 2019-03-15 15:22:18 +0000 | [diff] [blame] | 5010 | if (!LinkClass(self, descriptor, temp_klass, h_interfaces, &klass)) { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5011 | mirror::Class::SetStatus(temp_klass, ClassStatus::kErrorUnresolved, self); |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 5012 | return nullptr; |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 5013 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5014 | } |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 5015 | CHECK(temp_klass->IsRetired()); |
| 5016 | CHECK_NE(temp_klass.Get(), klass.Get()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5017 | |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 5018 | CHECK_EQ(interfaces_sfield.GetDeclaringClass(), klass.Get()); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 5019 | interfaces_sfield.SetObject<false>( |
| 5020 | klass.Get(), |
Mathieu Chartier | f8ac97f | 2016-10-05 15:56:52 -0700 | [diff] [blame] | 5021 | soa.Decode<mirror::ObjectArray<mirror::Class>>(interfaces)); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 5022 | CHECK_EQ(throws_sfield.GetDeclaringClass(), klass.Get()); |
| 5023 | throws_sfield.SetObject<false>( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 5024 | klass.Get(), |
Alex Light | 133987d | 2020-03-26 19:22:12 +0000 | [diff] [blame] | 5025 | proxied_throws.Get()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5026 | |
Andreas Gampe | 6cfd4c9 | 2017-04-06 08:03:32 -0700 | [diff] [blame] | 5027 | Runtime::Current()->GetRuntimeCallbacks()->ClassPrepare(temp_klass, klass); |
| 5028 | |
Vladimir Marko | 305c38b | 2018-02-14 11:50:07 +0000 | [diff] [blame] | 5029 | // SubtypeCheckInfo::Initialized must happen-before any new-instance for that type. |
| 5030 | // See also ClassLinker::EnsureInitialized(). |
| 5031 | if (kBitstringSubtypeCheckEnabled) { |
| 5032 | MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_); |
| 5033 | SubtypeCheck<ObjPtr<mirror::Class>>::EnsureInitialized(klass.Get()); |
| 5034 | // TODO: Avoid taking subtype_check_lock_ if SubtypeCheck for j.l.r.Proxy is already assigned. |
| 5035 | } |
| 5036 | |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 5037 | VisiblyInitializedCallback* callback = nullptr; |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5038 | { |
| 5039 | // Lock on klass is released. Lock new class object. |
| 5040 | ObjectLock<mirror::Class> initialization_lock(self, klass); |
Andreas Gampe | 5b20b35 | 2018-10-11 19:03:20 -0700 | [diff] [blame] | 5041 | EnsureSkipAccessChecksMethods(klass, image_pointer_size_); |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 5042 | // Conservatively go through the ClassStatus::kInitialized state. |
| 5043 | callback = MarkClassInitialized(self, klass); |
| 5044 | } |
| 5045 | if (callback != nullptr) { |
| 5046 | callback->MakeVisible(self); |
Ian Rogers | c898258 | 2012-09-07 16:53:25 -0700 | [diff] [blame] | 5047 | } |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 5048 | |
David Srbecky | 346fd96 | 2020-07-27 16:51:00 +0100 | [diff] [blame] | 5049 | // Consistency checks. |
Elliott Hughes | 67d9200 | 2012-03-26 15:08:51 -0700 | [diff] [blame] | 5050 | if (kIsDebugBuild) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 5051 | CHECK(klass->GetIFieldsPtr() == nullptr); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5052 | CheckProxyConstructor(klass->GetDirectMethod(0, image_pointer_size_)); |
| 5053 | |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 5054 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5055 | auto* virtual_method = klass->GetVirtualMethodUnchecked(i, image_pointer_size_); |
Alex Light | 133987d | 2020-03-26 19:22:12 +0000 | [diff] [blame] | 5056 | CheckProxyMethod(virtual_method, proxied_methods[i]); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 5057 | } |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 5058 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 5059 | StackHandleScope<1> hs2(self); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 5060 | Handle<mirror::String> decoded_name = hs2.NewHandle(soa.Decode<mirror::String>(name)); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 5061 | std::string interfaces_field_name(StringPrintf("java.lang.Class[] %s.interfaces", |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 5062 | decoded_name->ToModifiedUtf8().c_str())); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5063 | CHECK_EQ(ArtField::PrettyField(klass->GetStaticField(0)), interfaces_field_name); |
Elliott Hughes | 2ed52c4 | 2012-03-21 16:56:56 -0700 | [diff] [blame] | 5064 | |
| 5065 | std::string throws_field_name(StringPrintf("java.lang.Class[][] %s.throws", |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 5066 | decoded_name->ToModifiedUtf8().c_str())); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5067 | CHECK_EQ(ArtField::PrettyField(klass->GetStaticField(1)), throws_field_name); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 5068 | |
Narayan Kamath | 6b2dc31 | 2017-03-14 13:26:12 +0000 | [diff] [blame] | 5069 | CHECK_EQ(klass.Get()->GetProxyInterfaces(), |
Mathieu Chartier | f8ac97f | 2016-10-05 15:56:52 -0700 | [diff] [blame] | 5070 | soa.Decode<mirror::ObjectArray<mirror::Class>>(interfaces)); |
Narayan Kamath | 6b2dc31 | 2017-03-14 13:26:12 +0000 | [diff] [blame] | 5071 | CHECK_EQ(klass.Get()->GetProxyThrows(), |
Alex Light | 133987d | 2020-03-26 19:22:12 +0000 | [diff] [blame] | 5072 | proxied_throws.Get()); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 5073 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5074 | return klass.Get(); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 5075 | } |
| 5076 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5077 | void ClassLinker::CreateProxyConstructor(Handle<mirror::Class> klass, ArtMethod* out) { |
| 5078 | // Create constructor for Proxy that must initialize the method. |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 5079 | ObjPtr<mirror::Class> proxy_class = GetClassRoot<mirror::Proxy>(this); |
| 5080 | CHECK_EQ(proxy_class->NumDirectMethods(), 21u); |
Przemyslaw Szczepaniak | f11cd29 | 2016-08-17 17:46:38 +0100 | [diff] [blame] | 5081 | |
Igor Murashkin | 9d3d752 | 2017-02-27 10:39:49 -0800 | [diff] [blame] | 5082 | // Find the <init>(InvocationHandler)V method. The exact method offset varies depending |
| 5083 | // 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] | 5084 | ArtMethod* proxy_constructor = |
| 5085 | jni::DecodeArtMethod(WellKnownClasses::java_lang_reflect_Proxy_init); |
Igor Murashkin | 9d3d752 | 2017-02-27 10:39:49 -0800 | [diff] [blame] | 5086 | DCHECK(proxy_constructor != nullptr) |
| 5087 | << "Could not find <init> method in java.lang.reflect.Proxy"; |
| 5088 | |
Jeff Hao | db8a664 | 2014-08-14 17:18:52 -0700 | [diff] [blame] | 5089 | // Clone the existing constructor of Proxy (our constructor would just invoke it so steal its |
| 5090 | // code_ too) |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5091 | DCHECK(out != nullptr); |
| 5092 | out->CopyFrom(proxy_constructor, image_pointer_size_); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 5093 | // 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] | 5094 | // 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] | 5095 | // 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] | 5096 | out->SetAccessFlags((out->GetAccessFlags() & ~kAccProtected) | |
| 5097 | kAccPublic | |
| 5098 | kAccCompileDontBother); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5099 | out->SetDeclaringClass(klass.Get()); |
Vladimir Marko | d1ee20f | 2017-08-17 09:21:16 +0000 | [diff] [blame] | 5100 | |
| 5101 | // Set the original constructor method. |
| 5102 | out->SetDataPtrSize(proxy_constructor, image_pointer_size_); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 5103 | } |
| 5104 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5105 | void ClassLinker::CheckProxyConstructor(ArtMethod* constructor) const { |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 5106 | CHECK(constructor->IsConstructor()); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5107 | auto* np = constructor->GetInterfaceMethodIfProxy(image_pointer_size_); |
| 5108 | CHECK_STREQ(np->GetName(), "<init>"); |
| 5109 | CHECK_STREQ(np->GetSignature().ToString().c_str(), "(Ljava/lang/reflect/InvocationHandler;)V"); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 5110 | DCHECK(constructor->IsPublic()); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 5111 | } |
| 5112 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5113 | void ClassLinker::CreateProxyMethod(Handle<mirror::Class> klass, ArtMethod* prototype, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5114 | ArtMethod* out) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 5115 | // 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] | 5116 | // as necessary |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5117 | DCHECK(out != nullptr); |
| 5118 | out->CopyFrom(prototype, image_pointer_size_); |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 5119 | |
Alex Light | e9dd04f | 2016-03-16 16:09:45 -0700 | [diff] [blame] | 5120 | // Set class to be the concrete proxy class. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5121 | out->SetDeclaringClass(klass.Get()); |
Alex Light | e9dd04f | 2016-03-16 16:09:45 -0700 | [diff] [blame] | 5122 | // Clear the abstract, default and conflict flags to ensure that defaults aren't picked in |
| 5123 | // preference to the invocation handler. |
| 5124 | const uint32_t kRemoveFlags = kAccAbstract | kAccDefault | kAccDefaultConflict; |
| 5125 | // Make the method final. |
Mathieu Chartier | 201e297 | 2017-06-05 18:34:53 -0700 | [diff] [blame] | 5126 | // Mark kAccCompileDontBother so that we don't take JIT samples for the method. b/62349349 |
| 5127 | const uint32_t kAddFlags = kAccFinal | kAccCompileDontBother; |
Alex Light | e9dd04f | 2016-03-16 16:09:45 -0700 | [diff] [blame] | 5128 | out->SetAccessFlags((out->GetAccessFlags() & ~kRemoveFlags) | kAddFlags); |
| 5129 | |
Vladimir Marko | d1ee20f | 2017-08-17 09:21:16 +0000 | [diff] [blame] | 5130 | // Set the original interface method. |
| 5131 | out->SetDataPtrSize(prototype, image_pointer_size_); |
| 5132 | |
Ian Rogers | 466bb25 | 2011-10-14 03:29:56 -0700 | [diff] [blame] | 5133 | // At runtime the method looks like a reference and argument saving method, clone the code |
| 5134 | // related parameters from this method. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5135 | out->SetEntryPointFromQuickCompiledCode(GetQuickProxyInvokeHandler()); |
Ian Rogers | c2b4447 | 2011-12-14 21:17:17 -0800 | [diff] [blame] | 5136 | } |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 5137 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5138 | void ClassLinker::CheckProxyMethod(ArtMethod* method, ArtMethod* prototype) const { |
David Srbecky | 346fd96 | 2020-07-27 16:51:00 +0100 | [diff] [blame] | 5139 | // Basic consistency checks. |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 5140 | CHECK(!prototype->IsFinal()); |
| 5141 | CHECK(method->IsFinal()); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 5142 | CHECK(method->IsInvokable()); |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 5143 | |
| 5144 | // The proxy method doesn't have its own dex cache or dex file and so it steals those of its |
| 5145 | // 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] | 5146 | CHECK_EQ(prototype->GetDexMethodIndex(), method->GetDexMethodIndex()); |
Vladimir Marko | 5c3e9d1 | 2017-08-30 16:43:54 +0100 | [diff] [blame] | 5147 | CHECK_EQ(prototype, method->GetInterfaceMethodIfProxy(image_pointer_size_)); |
Jesse Wilson | 95caa79 | 2011-10-12 18:14:17 -0400 | [diff] [blame] | 5148 | } |
| 5149 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5150 | bool ClassLinker::CanWeInitializeClass(ObjPtr<mirror::Class> klass, bool can_init_statics, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5151 | bool can_init_parents) { |
Brian Carlstrom | 610e49f | 2013-11-04 17:07:22 -0800 | [diff] [blame] | 5152 | if (can_init_statics && can_init_parents) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5153 | return true; |
| 5154 | } |
| 5155 | if (!can_init_statics) { |
| 5156 | // Check if there's a class initializer. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5157 | ArtMethod* clinit = klass->FindClassInitializer(image_pointer_size_); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5158 | if (clinit != nullptr) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5159 | return false; |
| 5160 | } |
| 5161 | // Check if there are encoded static values needing initialization. |
| 5162 | if (klass->NumStaticFields() != 0) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 5163 | const dex::ClassDef* dex_class_def = klass->GetClassDef(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5164 | DCHECK(dex_class_def != nullptr); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5165 | if (dex_class_def->static_values_off_ != 0) { |
| 5166 | return false; |
| 5167 | } |
| 5168 | } |
Vladimir Marko | 889b72d | 2019-11-12 11:01:13 +0000 | [diff] [blame] | 5169 | } |
| 5170 | // If we are a class we need to initialize all interfaces with default methods when we are |
| 5171 | // initialized. Check all of them. |
| 5172 | if (!klass->IsInterface()) { |
| 5173 | size_t num_interfaces = klass->GetIfTableCount(); |
| 5174 | for (size_t i = 0; i < num_interfaces; i++) { |
| 5175 | ObjPtr<mirror::Class> iface = klass->GetIfTable()->GetInterface(i); |
| 5176 | if (iface->HasDefaultMethods() && !iface->IsInitialized()) { |
| 5177 | if (!can_init_parents || !CanWeInitializeClass(iface, can_init_statics, can_init_parents)) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5178 | return false; |
| 5179 | } |
| 5180 | } |
| 5181 | } |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5182 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5183 | if (klass->IsInterface() || !klass->HasSuperClass()) { |
| 5184 | return true; |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5185 | } |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5186 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Vladimir Marko | 889b72d | 2019-11-12 11:01:13 +0000 | [diff] [blame] | 5187 | if (super_class->IsInitialized()) { |
| 5188 | return true; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5189 | } |
Vladimir Marko | 889b72d | 2019-11-12 11:01:13 +0000 | [diff] [blame] | 5190 | 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] | 5191 | } |
| 5192 | |
Mathieu Chartier | 2336954 | 2020-03-04 08:24:11 -0800 | [diff] [blame] | 5193 | bool ClassLinker::InitializeClass(Thread* self, |
| 5194 | Handle<mirror::Class> klass, |
| 5195 | bool can_init_statics, |
| 5196 | bool can_init_parents) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5197 | // see JLS 3rd edition, 12.4.2 "Detailed Initialization Procedure" for the locking protocol |
| 5198 | |
| 5199 | // Are we already initialized and therefore done? |
| 5200 | // Note: we differ from the JLS here as we don't do this under the lock, this is benign as |
| 5201 | // an initialized class will never change its state. |
| 5202 | if (klass->IsInitialized()) { |
| 5203 | return true; |
| 5204 | } |
| 5205 | |
| 5206 | // 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] | 5207 | if (!CanWeInitializeClass(klass.Get(), can_init_statics, can_init_parents)) { |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5208 | return false; |
| 5209 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5210 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5211 | self->AllowThreadSuspension(); |
Mathieu Chartier | 2336954 | 2020-03-04 08:24:11 -0800 | [diff] [blame] | 5212 | Runtime* const runtime = Runtime::Current(); |
| 5213 | const bool stats_enabled = runtime->HasStatsEnabled(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5214 | uint64_t t0; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5215 | { |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 5216 | ObjectLock<mirror::Class> lock(self, klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5217 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5218 | // Re-check under the lock in case another thread initialized ahead of us. |
| 5219 | if (klass->IsInitialized()) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5220 | return true; |
| 5221 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5222 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5223 | // 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] | 5224 | if (klass->IsErroneous()) { |
Andreas Gampe | 7b3063b | 2019-01-07 14:12:52 -0800 | [diff] [blame] | 5225 | ThrowEarlierClassFailure(klass.Get(), true, /* log= */ true); |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 5226 | VlogClassInitializationFailure(klass); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5227 | return false; |
| 5228 | } |
| 5229 | |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 5230 | CHECK(klass->IsResolved() && !klass->IsErroneousResolved()) |
| 5231 | << klass->PrettyClass() << ": state=" << klass->GetStatus(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5232 | |
| 5233 | if (!klass->IsVerified()) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5234 | VerifyClass(self, klass); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5235 | if (!klass->IsVerified()) { |
| 5236 | // We failed to verify, expect either the klass to be erroneous or verification failed at |
| 5237 | // compile time. |
| 5238 | if (klass->IsErroneous()) { |
Andreas Gampe | fc49fa0 | 2016-04-21 12:21:55 -0700 | [diff] [blame] | 5239 | // The class is erroneous. This may be a verifier error, or another thread attempted |
| 5240 | // verification and/or initialization and failed. We can distinguish those cases by |
| 5241 | // whether an exception is already pending. |
| 5242 | if (self->IsExceptionPending()) { |
| 5243 | // Check that it's a VerifyError. |
| 5244 | DCHECK_EQ("java.lang.Class<java.lang.VerifyError>", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5245 | mirror::Class::PrettyClass(self->GetException()->GetClass())); |
Andreas Gampe | fc49fa0 | 2016-04-21 12:21:55 -0700 | [diff] [blame] | 5246 | } else { |
| 5247 | // Check that another thread attempted initialization. |
| 5248 | DCHECK_NE(0, klass->GetClinitThreadId()); |
| 5249 | DCHECK_NE(self->GetTid(), klass->GetClinitThreadId()); |
| 5250 | // Need to rethrow the previous failure now. |
| 5251 | ThrowEarlierClassFailure(klass.Get(), true); |
| 5252 | } |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 5253 | VlogClassInitializationFailure(klass); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5254 | } else { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 5255 | CHECK(Runtime::Current()->IsAotCompiler()); |
Nicolas Geoffray | 1715efa | 2020-06-05 18:34:49 +0100 | [diff] [blame] | 5256 | CHECK(klass->ShouldVerifyAtRuntime() || klass->IsVerifiedNeedsAccessChecks()); |
Vladimir Marko | d79b37b | 2018-11-02 13:06:22 +0000 | [diff] [blame] | 5257 | self->AssertNoPendingException(); |
| 5258 | self->SetException(Runtime::Current()->GetPreAllocatedNoClassDefFoundError()); |
jeffhao | a9b3bf4 | 2012-06-06 17:18:39 -0700 | [diff] [blame] | 5259 | } |
Vladimir Marko | d79b37b | 2018-11-02 13:06:22 +0000 | [diff] [blame] | 5260 | self->AssertPendingException(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5261 | return false; |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 5262 | } else { |
| 5263 | self->AssertNoPendingException(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5264 | } |
Andreas Gampe | fc49fa0 | 2016-04-21 12:21:55 -0700 | [diff] [blame] | 5265 | |
| 5266 | // A separate thread could have moved us all the way to initialized. A "simple" example |
| 5267 | // involves a subclass of the current class being initialized at the same time (which |
| 5268 | // will implicitly initialize the superclass, if scheduled that way). b/28254258 |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 5269 | DCHECK(!klass->IsErroneous()) << klass->GetStatus(); |
Andreas Gampe | fc49fa0 | 2016-04-21 12:21:55 -0700 | [diff] [blame] | 5270 | if (klass->IsInitialized()) { |
| 5271 | return true; |
| 5272 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5273 | } |
| 5274 | |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5275 | // If the class is ClassStatus::kInitializing, either this thread is |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5276 | // initializing higher up the stack or another thread has beat us |
| 5277 | // to initializing and we need to wait. Either way, this |
| 5278 | // invocation of InitializeClass will not be responsible for |
| 5279 | // running <clinit> and will return. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5280 | if (klass->GetStatus() == ClassStatus::kInitializing) { |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 5281 | // Could have got an exception during verification. |
| 5282 | if (self->IsExceptionPending()) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 5283 | VlogClassInitializationFailure(klass); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 5284 | return false; |
| 5285 | } |
Elliott Hughes | 005ab2e | 2011-09-11 17:15:31 -0700 | [diff] [blame] | 5286 | // We caught somebody else in the act; was it us? |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 5287 | if (klass->GetClinitThreadId() == self->GetTid()) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5288 | // Yes. That's fine. Return so we can continue initializing. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5289 | return true; |
| 5290 | } |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5291 | // No. That's fine. Wait for another thread to finish initializing. |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5292 | return WaitForInitializeClass(klass, self, lock); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5293 | } |
| 5294 | |
Jeff Hao | e2e4034 | 2017-07-19 10:45:18 -0700 | [diff] [blame] | 5295 | // Try to get the oat class's status for this class if the oat file is present. The compiler |
| 5296 | // tries to validate superclass descriptors, and writes the result into the oat file. |
| 5297 | // Runtime correctness is guaranteed by classpath checks done on loading. If the classpath |
| 5298 | // is different at runtime than it was at compile time, the oat file is rejected. So if the |
| 5299 | // 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] | 5300 | bool has_oat_class = false; |
Jeff Hao | e2e4034 | 2017-07-19 10:45:18 -0700 | [diff] [blame] | 5301 | const OatFile::OatClass oat_class = (runtime->IsStarted() && !runtime->IsAotCompiler()) |
| 5302 | ? OatFile::FindOatClass(klass->GetDexFile(), klass->GetDexClassDefIndex(), &has_oat_class) |
| 5303 | : OatFile::OatClass::Invalid(); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5304 | if (oat_class.GetStatus() < ClassStatus::kSuperclassValidated && |
Jeff Hao | 0cb1728 | 2017-07-12 14:51:49 -0700 | [diff] [blame] | 5305 | !ValidateSuperClassDescriptors(klass)) { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5306 | mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5307 | return false; |
| 5308 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5309 | self->AllowThreadSuspension(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5310 | |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5311 | CHECK_EQ(klass->GetStatus(), ClassStatus::kVerified) << klass->PrettyClass() |
Andreas Gampe | 9510ccd | 2016-04-20 09:55:25 -0700 | [diff] [blame] | 5312 | << " self.tid=" << self->GetTid() << " clinit.tid=" << klass->GetClinitThreadId(); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5313 | |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5314 | // From here out other threads may observe that we're initializing and so changes of state |
| 5315 | // require the a notification. |
Elliott Hughes | dcc2474 | 2011-09-07 14:02:44 -0700 | [diff] [blame] | 5316 | klass->SetClinitThreadId(self->GetTid()); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5317 | mirror::Class::SetStatus(klass, ClassStatus::kInitializing, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5318 | |
Mathieu Chartier | 2336954 | 2020-03-04 08:24:11 -0800 | [diff] [blame] | 5319 | t0 = stats_enabled ? NanoTime() : 0u; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5320 | } |
| 5321 | |
Andreas Gampe | af86470 | 2019-07-23 14:05:35 -0700 | [diff] [blame] | 5322 | uint64_t t_sub = 0; |
| 5323 | |
Brian Carlstrom | 6d3f72c | 2013-08-21 18:06:34 -0700 | [diff] [blame] | 5324 | // Initialize super classes, must be done while initializing for the JLS. |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5325 | if (!klass->IsInterface() && klass->HasSuperClass()) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5326 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5327 | if (!super_class->IsInitialized()) { |
| 5328 | CHECK(!super_class->IsInterface()); |
| 5329 | CHECK(can_init_parents); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5330 | StackHandleScope<1> hs(self); |
| 5331 | Handle<mirror::Class> handle_scope_super(hs.NewHandle(super_class)); |
Mathieu Chartier | 2336954 | 2020-03-04 08:24:11 -0800 | [diff] [blame] | 5332 | uint64_t super_t0 = stats_enabled ? NanoTime() : 0u; |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5333 | bool super_initialized = InitializeClass(self, handle_scope_super, can_init_statics, true); |
Mathieu Chartier | 2336954 | 2020-03-04 08:24:11 -0800 | [diff] [blame] | 5334 | uint64_t super_t1 = stats_enabled ? NanoTime() : 0u; |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5335 | if (!super_initialized) { |
| 5336 | // The super class was verified ahead of entering initializing, we should only be here if |
| 5337 | // the super class became erroneous due to initialization. |
Chang Xing | adbb91c | 2017-07-17 11:23:55 -0700 | [diff] [blame] | 5338 | // For the case of aot compiler, the super class might also be initializing but we don't |
| 5339 | // want to process circular dependencies in pre-compile. |
| 5340 | CHECK(self->IsExceptionPending()) |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 5341 | << "Super class initialization failed for " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5342 | << handle_scope_super->PrettyDescriptor() |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5343 | << " that has unexpected status " << handle_scope_super->GetStatus() |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5344 | << "\nPending exception:\n" |
Nicolas Geoffray | 14691c5 | 2015-03-05 10:40:17 +0000 | [diff] [blame] | 5345 | << (self->GetException() != nullptr ? self->GetException()->Dump() : ""); |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 5346 | ObjectLock<mirror::Class> lock(self, klass); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5347 | // Initialization failed because the super-class is erroneous. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5348 | mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5349 | return false; |
| 5350 | } |
Andreas Gampe | af86470 | 2019-07-23 14:05:35 -0700 | [diff] [blame] | 5351 | t_sub = super_t1 - super_t0; |
Ian Rogers | 1bddec3 | 2012-02-04 12:27:34 -0800 | [diff] [blame] | 5352 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5353 | } |
| 5354 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5355 | if (!klass->IsInterface()) { |
| 5356 | // Initialize interfaces with default methods for the JLS. |
| 5357 | size_t num_direct_interfaces = klass->NumDirectInterfaces(); |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 5358 | // Only setup the (expensive) handle scope if we actually need to. |
| 5359 | if (UNLIKELY(num_direct_interfaces > 0)) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5360 | StackHandleScope<1> hs_iface(self); |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 5361 | MutableHandle<mirror::Class> handle_scope_iface(hs_iface.NewHandle<mirror::Class>(nullptr)); |
| 5362 | for (size_t i = 0; i < num_direct_interfaces; i++) { |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 5363 | handle_scope_iface.Assign(mirror::Class::GetDirectInterface(self, klass.Get(), i)); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 5364 | CHECK(handle_scope_iface != nullptr) << klass->PrettyDescriptor() << " iface #" << i; |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 5365 | CHECK(handle_scope_iface->IsInterface()); |
| 5366 | if (handle_scope_iface->HasBeenRecursivelyInitialized()) { |
| 5367 | // We have already done this for this interface. Skip it. |
| 5368 | continue; |
| 5369 | } |
| 5370 | // We cannot just call initialize class directly because we need to ensure that ALL |
| 5371 | // interfaces with default methods are initialized. Non-default interface initialization |
| 5372 | // will not affect other non-default super-interfaces. |
Mathieu Chartier | 2336954 | 2020-03-04 08:24:11 -0800 | [diff] [blame] | 5373 | // This is not very precise, misses all walking. |
| 5374 | uint64_t inf_t0 = stats_enabled ? NanoTime() : 0u; |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 5375 | bool iface_initialized = InitializeDefaultInterfaceRecursive(self, |
| 5376 | handle_scope_iface, |
| 5377 | can_init_statics, |
| 5378 | can_init_parents); |
Mathieu Chartier | 2336954 | 2020-03-04 08:24:11 -0800 | [diff] [blame] | 5379 | uint64_t inf_t1 = stats_enabled ? NanoTime() : 0u; |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 5380 | if (!iface_initialized) { |
| 5381 | ObjectLock<mirror::Class> lock(self, klass); |
| 5382 | // Initialization failed because one of our interfaces with default methods is erroneous. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5383 | mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self); |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 5384 | return false; |
| 5385 | } |
Andreas Gampe | af86470 | 2019-07-23 14:05:35 -0700 | [diff] [blame] | 5386 | t_sub += inf_t1 - inf_t0; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5387 | } |
| 5388 | } |
| 5389 | } |
| 5390 | |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 5391 | const size_t num_static_fields = klass->NumStaticFields(); |
| 5392 | if (num_static_fields > 0) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 5393 | const dex::ClassDef* dex_class_def = klass->GetClassDef(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 5394 | CHECK(dex_class_def != nullptr); |
Hiroshi Yamauchi | 67ef46a | 2014-08-21 15:59:43 -0700 | [diff] [blame] | 5395 | StackHandleScope<3> hs(self); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 5396 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(klass->GetClassLoader())); |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 5397 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(klass->GetDexCache())); |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 5398 | |
| 5399 | // Eagerly fill in static fields so that the we don't have to do as many expensive |
| 5400 | // Class::FindStaticField in ResolveField. |
| 5401 | for (size_t i = 0; i < num_static_fields; ++i) { |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 5402 | ArtField* field = klass->GetStaticField(i); |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 5403 | const uint32_t field_idx = field->GetDexFieldIndex(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 5404 | ArtField* resolved_field = dex_cache->GetResolvedField(field_idx, image_pointer_size_); |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 5405 | if (resolved_field == nullptr) { |
David Brazdil | 1ab0fa8 | 2018-05-04 11:28:03 +0100 | [diff] [blame] | 5406 | // 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] | 5407 | DCHECK(!hiddenapi::ShouldDenyAccessToMember( |
| 5408 | field, |
| 5409 | hiddenapi::AccessContext(class_loader.Get(), dex_cache.Get()), |
| 5410 | hiddenapi::AccessMethod::kNone)); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 5411 | dex_cache->SetResolvedField(field_idx, field, image_pointer_size_); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 5412 | } else { |
| 5413 | DCHECK_EQ(field, resolved_field); |
Mathieu Chartier | 05d89ee | 2014-10-28 13:57:04 -0700 | [diff] [blame] | 5414 | } |
| 5415 | } |
| 5416 | |
Vladimir Marko | e11dd50 | 2017-12-08 14:09:45 +0000 | [diff] [blame] | 5417 | annotations::RuntimeEncodedStaticFieldValueIterator value_it(dex_cache, |
| 5418 | class_loader, |
David Sehr | 9323e6e | 2016-09-13 08:58:35 -0700 | [diff] [blame] | 5419 | this, |
| 5420 | *dex_class_def); |
Vladimir Marko | e11dd50 | 2017-12-08 14:09:45 +0000 | [diff] [blame] | 5421 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 5422 | |
Hiroshi Yamauchi | 8850011 | 2014-08-22 12:12:56 -0700 | [diff] [blame] | 5423 | if (value_it.HasNext()) { |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 5424 | ClassAccessor accessor(dex_file, *dex_class_def); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5425 | CHECK(can_init_statics); |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 5426 | for (const ClassAccessor::Field& field : accessor.GetStaticFields()) { |
| 5427 | if (!value_it.HasNext()) { |
| 5428 | break; |
| 5429 | } |
| 5430 | ArtField* art_field = ResolveField(field.GetIndex(), |
| 5431 | dex_cache, |
| 5432 | class_loader, |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 5433 | /* is_static= */ true); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 5434 | if (Runtime::Current()->IsActiveTransaction()) { |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 5435 | value_it.ReadValueToField<true>(art_field); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 5436 | } else { |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 5437 | value_it.ReadValueToField<false>(art_field); |
Sebastien Hertz | d2fe10a | 2014-01-15 10:20:56 +0100 | [diff] [blame] | 5438 | } |
Mathieu Chartier | da595be | 2016-08-10 13:57:39 -0700 | [diff] [blame] | 5439 | if (self->IsExceptionPending()) { |
| 5440 | break; |
| 5441 | } |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 5442 | value_it.Next(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5443 | } |
Mathieu Chartier | 1f1cb9f | 2018-06-04 09:22:46 -0700 | [diff] [blame] | 5444 | DCHECK(self->IsExceptionPending() || !value_it.HasNext()); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5445 | } |
| 5446 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5447 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5448 | |
Mathieu Chartier | da595be | 2016-08-10 13:57:39 -0700 | [diff] [blame] | 5449 | if (!self->IsExceptionPending()) { |
| 5450 | ArtMethod* clinit = klass->FindClassInitializer(image_pointer_size_); |
| 5451 | if (clinit != nullptr) { |
| 5452 | CHECK(can_init_statics); |
| 5453 | JValue result; |
| 5454 | clinit->Invoke(self, nullptr, 0, &result, "V"); |
| 5455 | } |
| 5456 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5457 | self->AllowThreadSuspension(); |
Mathieu Chartier | 2336954 | 2020-03-04 08:24:11 -0800 | [diff] [blame] | 5458 | uint64_t t1 = stats_enabled ? NanoTime() : 0u; |
Elliott Hughes | 83df2ac | 2011-10-11 16:37:54 -0700 | [diff] [blame] | 5459 | |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 5460 | VisiblyInitializedCallback* callback = nullptr; |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 5461 | bool success = true; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5462 | { |
Mathieu Chartier | db2633c | 2014-05-16 09:59:29 -0700 | [diff] [blame] | 5463 | ObjectLock<mirror::Class> lock(self, klass); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5464 | |
| 5465 | if (self->IsExceptionPending()) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 5466 | WrapExceptionInInitializer(klass); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5467 | mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self); |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 5468 | success = false; |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 5469 | } else if (Runtime::Current()->IsTransactionAborted()) { |
| 5470 | // The exception thrown when the transaction aborted has been caught and cleared |
| 5471 | // so we need to throw it again now. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5472 | VLOG(compiler) << "Return from class initializer of " |
| 5473 | << mirror::Class::PrettyDescriptor(klass.Get()) |
Sebastien Hertz | bd9cf9f | 2015-03-03 12:16:13 +0100 | [diff] [blame] | 5474 | << " without exception while transaction was aborted: re-throw it now."; |
Mathieu Chartier | 2336954 | 2020-03-04 08:24:11 -0800 | [diff] [blame] | 5475 | runtime->ThrowTransactionAbortError(self); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5476 | mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self); |
Sebastien Hertz | 1c80bec | 2015-02-03 11:58:06 +0100 | [diff] [blame] | 5477 | success = false; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5478 | } else { |
Mathieu Chartier | 2336954 | 2020-03-04 08:24:11 -0800 | [diff] [blame] | 5479 | if (stats_enabled) { |
| 5480 | RuntimeStats* global_stats = runtime->GetStats(); |
| 5481 | RuntimeStats* thread_stats = self->GetStats(); |
| 5482 | ++global_stats->class_init_count; |
| 5483 | ++thread_stats->class_init_count; |
| 5484 | global_stats->class_init_time_ns += (t1 - t0 - t_sub); |
| 5485 | thread_stats->class_init_time_ns += (t1 - t0 - t_sub); |
| 5486 | } |
Ian Rogers | e6bb3b2 | 2013-08-19 21:51:45 -0700 | [diff] [blame] | 5487 | // Set the class as initialized except if failed to initialize static fields. |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 5488 | callback = MarkClassInitialized(self, klass); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5489 | if (VLOG_IS_ON(class_linker)) { |
Ian Rogers | 1ff3c98 | 2014-08-12 02:30:58 -0700 | [diff] [blame] | 5490 | std::string temp; |
| 5491 | LOG(INFO) << "Initialized class " << klass->GetDescriptor(&temp) << " from " << |
Mathieu Chartier | f832284 | 2014-05-16 10:59:25 -0700 | [diff] [blame] | 5492 | klass->GetLocation(); |
Brian Carlstrom | ae82698 | 2011-11-09 01:33:42 -0800 | [diff] [blame] | 5493 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5494 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5495 | } |
Vladimir Marko | bf12191 | 2019-06-04 13:49:05 +0100 | [diff] [blame] | 5496 | if (callback != nullptr) { |
| 5497 | callback->MakeVisible(self); |
| 5498 | } |
Ian Rogers | bdfb1a5 | 2012-01-12 14:05:22 -0800 | [diff] [blame] | 5499 | return success; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5500 | } |
| 5501 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5502 | // We recursively run down the tree of interfaces. We need to do this in the order they are declared |
| 5503 | // and perform the initialization only on those interfaces that contain default methods. |
| 5504 | bool ClassLinker::InitializeDefaultInterfaceRecursive(Thread* self, |
| 5505 | Handle<mirror::Class> iface, |
| 5506 | bool can_init_statics, |
| 5507 | bool can_init_parents) { |
| 5508 | CHECK(iface->IsInterface()); |
| 5509 | size_t num_direct_ifaces = iface->NumDirectInterfaces(); |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 5510 | // Only create the (expensive) handle scope if we need it. |
| 5511 | if (UNLIKELY(num_direct_ifaces > 0)) { |
| 5512 | StackHandleScope<1> hs(self); |
| 5513 | MutableHandle<mirror::Class> handle_super_iface(hs.NewHandle<mirror::Class>(nullptr)); |
| 5514 | // First we initialize all of iface's super-interfaces recursively. |
| 5515 | for (size_t i = 0; i < num_direct_ifaces; i++) { |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 5516 | ObjPtr<mirror::Class> super_iface = mirror::Class::GetDirectInterface(self, iface.Get(), i); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 5517 | CHECK(super_iface != nullptr) << iface->PrettyDescriptor() << " iface #" << i; |
Alex Light | 56a40f5 | 2015-10-14 11:07:41 -0700 | [diff] [blame] | 5518 | if (!super_iface->HasBeenRecursivelyInitialized()) { |
| 5519 | // Recursive step |
| 5520 | handle_super_iface.Assign(super_iface); |
| 5521 | if (!InitializeDefaultInterfaceRecursive(self, |
| 5522 | handle_super_iface, |
| 5523 | can_init_statics, |
| 5524 | can_init_parents)) { |
| 5525 | return false; |
| 5526 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5527 | } |
| 5528 | } |
| 5529 | } |
| 5530 | |
| 5531 | bool result = true; |
| 5532 | // Then we initialize 'iface' if it has default methods. We do not need to (and in fact must not) |
| 5533 | // initialize if we don't have default methods. |
| 5534 | if (iface->HasDefaultMethods()) { |
| 5535 | result = EnsureInitialized(self, iface, can_init_statics, can_init_parents); |
| 5536 | } |
| 5537 | |
| 5538 | // Mark that this interface has undergone recursive default interface initialization so we know we |
| 5539 | // can skip it on any later class initializations. We do this even if we are not a default |
| 5540 | // interface since we can still avoid the traversal. This is purely a performance optimization. |
| 5541 | if (result) { |
| 5542 | // TODO This should be done in a better way |
Andreas Gampe | 976b298 | 2018-03-02 17:54:22 -0800 | [diff] [blame] | 5543 | // Note: Use a try-lock to avoid blocking when someone else is holding the lock on this |
| 5544 | // interface. It is bad (Java) style, but not impossible. Marking the recursive |
| 5545 | // initialization is a performance optimization (to avoid another idempotent visit |
| 5546 | // for other implementing classes/interfaces), and can be revisited later. |
| 5547 | ObjectTryLock<mirror::Class> lock(self, iface); |
| 5548 | if (lock.Acquired()) { |
| 5549 | iface->SetRecursivelyInitialized(); |
| 5550 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 5551 | } |
| 5552 | return result; |
| 5553 | } |
| 5554 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5555 | bool ClassLinker::WaitForInitializeClass(Handle<mirror::Class> klass, |
| 5556 | Thread* self, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5557 | ObjectLock<mirror::Class>& lock) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5558 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5559 | while (true) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 5560 | self->AssertNoPendingException(); |
Ian Rogers | 8f3c9ae | 2013-08-20 17:26:41 -0700 | [diff] [blame] | 5561 | CHECK(!klass->IsInitialized()); |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5562 | lock.WaitIgnoringInterrupts(); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5563 | |
| 5564 | // When we wake up, repeat the test for init-in-progress. If |
| 5565 | // there's an exception pending (only possible if |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 5566 | // we were not using WaitIgnoringInterrupts), bail out. |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5567 | if (self->IsExceptionPending()) { |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 5568 | WrapExceptionInInitializer(klass); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5569 | mirror::Class::SetStatus(klass, ClassStatus::kErrorResolved, self); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5570 | return false; |
| 5571 | } |
| 5572 | // Spurious wakeup? Go back to waiting. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5573 | if (klass->GetStatus() == ClassStatus::kInitializing) { |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5574 | continue; |
| 5575 | } |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5576 | if (klass->GetStatus() == ClassStatus::kVerified && |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 5577 | Runtime::Current()->IsAotCompiler()) { |
Ian Rogers | 3d1548d | 2012-09-24 14:08:03 -0700 | [diff] [blame] | 5578 | // Compile time initialization failed. |
| 5579 | return false; |
| 5580 | } |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5581 | if (klass->IsErroneous()) { |
| 5582 | // The caller wants an exception, but it was thrown in a |
| 5583 | // different thread. Synthesize one here. |
Brian Carlstrom | df14324 | 2011-10-10 18:05:34 -0700 | [diff] [blame] | 5584 | ThrowNoClassDefFoundError("<clinit> failed for class %s; see exception in other thread", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5585 | klass->PrettyDescriptor().c_str()); |
Brian Carlstrom | b23eab1 | 2014-10-08 17:55:21 -0700 | [diff] [blame] | 5586 | VlogClassInitializationFailure(klass); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5587 | return false; |
| 5588 | } |
| 5589 | if (klass->IsInitialized()) { |
| 5590 | return true; |
| 5591 | } |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5592 | LOG(FATAL) << "Unexpected class status. " << klass->PrettyClass() << " is " |
Mathieu Chartier | c528dba | 2013-11-26 12:00:11 -0800 | [diff] [blame] | 5593 | << klass->GetStatus(); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5594 | } |
Ian Rogers | 0714083 | 2014-09-30 15:43:59 -0700 | [diff] [blame] | 5595 | UNREACHABLE(); |
Brian Carlstrom | d1422f8 | 2011-09-28 11:37:09 -0700 | [diff] [blame] | 5596 | } |
| 5597 | |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5598 | static void ThrowSignatureCheckResolveReturnTypeException(Handle<mirror::Class> klass, |
| 5599 | Handle<mirror::Class> super_klass, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5600 | ArtMethod* method, |
| 5601 | ArtMethod* m) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5602 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5603 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 5604 | DCHECK(!m->IsProxyMethod()); |
| 5605 | const DexFile* dex_file = m->GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 5606 | const dex::MethodId& method_id = dex_file->GetMethodId(m->GetDexMethodIndex()); |
| 5607 | const dex::ProtoId& proto_id = dex_file->GetMethodPrototype(method_id); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 5608 | dex::TypeIndex return_type_idx = proto_id.return_type_idx_; |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5609 | std::string return_type = dex_file->PrettyType(return_type_idx); |
| 5610 | std::string class_loader = mirror::Object::PrettyTypeOf(m->GetDeclaringClass()->GetClassLoader()); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5611 | ThrowWrappedLinkageError(klass.Get(), |
| 5612 | "While checking class %s method %s signature against %s %s: " |
| 5613 | "Failed to resolve return type %s with %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5614 | mirror::Class::PrettyDescriptor(klass.Get()).c_str(), |
| 5615 | ArtMethod::PrettyMethod(method).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5616 | super_klass->IsInterface() ? "interface" : "superclass", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5617 | mirror::Class::PrettyDescriptor(super_klass.Get()).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5618 | return_type.c_str(), class_loader.c_str()); |
| 5619 | } |
| 5620 | |
| 5621 | static void ThrowSignatureCheckResolveArgException(Handle<mirror::Class> klass, |
| 5622 | Handle<mirror::Class> super_klass, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5623 | ArtMethod* method, |
| 5624 | ArtMethod* m, |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5625 | uint32_t index, |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 5626 | dex::TypeIndex arg_type_idx) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5627 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5628 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 5629 | DCHECK(!m->IsProxyMethod()); |
| 5630 | const DexFile* dex_file = m->GetDexFile(); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5631 | std::string arg_type = dex_file->PrettyType(arg_type_idx); |
| 5632 | std::string class_loader = mirror::Object::PrettyTypeOf(m->GetDeclaringClass()->GetClassLoader()); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5633 | ThrowWrappedLinkageError(klass.Get(), |
| 5634 | "While checking class %s method %s signature against %s %s: " |
| 5635 | "Failed to resolve arg %u type %s with %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5636 | mirror::Class::PrettyDescriptor(klass.Get()).c_str(), |
| 5637 | ArtMethod::PrettyMethod(method).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5638 | super_klass->IsInterface() ? "interface" : "superclass", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5639 | mirror::Class::PrettyDescriptor(super_klass.Get()).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5640 | index, arg_type.c_str(), class_loader.c_str()); |
| 5641 | } |
| 5642 | |
| 5643 | static void ThrowSignatureMismatch(Handle<mirror::Class> klass, |
| 5644 | Handle<mirror::Class> super_klass, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5645 | ArtMethod* method, |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5646 | const std::string& error_msg) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5647 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5648 | ThrowLinkageError(klass.Get(), |
| 5649 | "Class %s method %s resolves differently in %s %s: %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5650 | mirror::Class::PrettyDescriptor(klass.Get()).c_str(), |
| 5651 | ArtMethod::PrettyMethod(method).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5652 | super_klass->IsInterface() ? "interface" : "superclass", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5653 | mirror::Class::PrettyDescriptor(super_klass.Get()).c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5654 | error_msg.c_str()); |
| 5655 | } |
| 5656 | |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5657 | static bool HasSameSignatureWithDifferentClassLoaders(Thread* self, |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5658 | Handle<mirror::Class> klass, |
| 5659 | Handle<mirror::Class> super_klass, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5660 | ArtMethod* method1, |
| 5661 | ArtMethod* method2) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5662 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5663 | { |
| 5664 | StackHandleScope<1> hs(self); |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 5665 | Handle<mirror::Class> return_type(hs.NewHandle(method1->ResolveReturnType())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 5666 | if (UNLIKELY(return_type == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5667 | ThrowSignatureCheckResolveReturnTypeException(klass, super_klass, method1, method1); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5668 | return false; |
| 5669 | } |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 5670 | ObjPtr<mirror::Class> other_return_type = method2->ResolveReturnType(); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5671 | if (UNLIKELY(other_return_type == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5672 | ThrowSignatureCheckResolveReturnTypeException(klass, super_klass, method1, method2); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5673 | return false; |
| 5674 | } |
Vladimir Marko | 862f43c | 2015-02-10 18:22:57 +0000 | [diff] [blame] | 5675 | if (UNLIKELY(other_return_type != return_type.Get())) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5676 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 5677 | StringPrintf("Return types mismatch: %s(%p) vs %s(%p)", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5678 | return_type->PrettyClassAndClassLoader().c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5679 | return_type.Get(), |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5680 | other_return_type->PrettyClassAndClassLoader().c_str(), |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5681 | other_return_type.Ptr())); |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5682 | return false; |
| 5683 | } |
| 5684 | } |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 5685 | const dex::TypeList* types1 = method1->GetParameterTypeList(); |
| 5686 | const dex::TypeList* types2 = method2->GetParameterTypeList(); |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5687 | if (types1 == nullptr) { |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5688 | if (types2 != nullptr && types2->Size() != 0) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5689 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 5690 | StringPrintf("Type list mismatch with %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5691 | method2->PrettyMethod(true).c_str())); |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5692 | return false; |
| 5693 | } |
| 5694 | return true; |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5695 | } else if (UNLIKELY(types2 == nullptr)) { |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5696 | if (types1->Size() != 0) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5697 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 5698 | StringPrintf("Type list mismatch with %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5699 | method2->PrettyMethod(true).c_str())); |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5700 | return false; |
| 5701 | } |
| 5702 | return true; |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5703 | } |
| 5704 | uint32_t num_types = types1->Size(); |
| 5705 | if (UNLIKELY(num_types != types2->Size())) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5706 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 5707 | StringPrintf("Type list mismatch with %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5708 | method2->PrettyMethod(true).c_str())); |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5709 | return false; |
| 5710 | } |
| 5711 | for (uint32_t i = 0; i < num_types; ++i) { |
Vladimir Marko | 862f43c | 2015-02-10 18:22:57 +0000 | [diff] [blame] | 5712 | StackHandleScope<1> hs(self); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 5713 | dex::TypeIndex param_type_idx = types1->GetTypeItem(i).type_idx_; |
Vladimir Marko | 862f43c | 2015-02-10 18:22:57 +0000 | [diff] [blame] | 5714 | Handle<mirror::Class> param_type(hs.NewHandle( |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 5715 | method1->ResolveClassFromTypeIndex(param_type_idx))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 5716 | if (UNLIKELY(param_type == nullptr)) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5717 | ThrowSignatureCheckResolveArgException(klass, super_klass, method1, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5718 | method1, i, param_type_idx); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5719 | return false; |
| 5720 | } |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 5721 | dex::TypeIndex other_param_type_idx = types2->GetTypeItem(i).type_idx_; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5722 | ObjPtr<mirror::Class> other_param_type = |
Vladimir Marko | b45528c | 2017-07-27 14:14:28 +0100 | [diff] [blame] | 5723 | method2->ResolveClassFromTypeIndex(other_param_type_idx); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5724 | if (UNLIKELY(other_param_type == nullptr)) { |
| 5725 | ThrowSignatureCheckResolveArgException(klass, super_klass, method1, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5726 | method2, i, other_param_type_idx); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5727 | return false; |
| 5728 | } |
Vladimir Marko | 862f43c | 2015-02-10 18:22:57 +0000 | [diff] [blame] | 5729 | if (UNLIKELY(param_type.Get() != other_param_type)) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5730 | ThrowSignatureMismatch(klass, super_klass, method1, |
| 5731 | StringPrintf("Parameter %u type mismatch: %s(%p) vs %s(%p)", |
| 5732 | i, |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5733 | param_type->PrettyClassAndClassLoader().c_str(), |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5734 | param_type.Get(), |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5735 | other_param_type->PrettyClassAndClassLoader().c_str(), |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5736 | other_param_type.Ptr())); |
Ian Rogers | b5fb207 | 2014-12-02 17:22:02 -0800 | [diff] [blame] | 5737 | return false; |
| 5738 | } |
| 5739 | } |
| 5740 | return true; |
| 5741 | } |
| 5742 | |
| 5743 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5744 | bool ClassLinker::ValidateSuperClassDescriptors(Handle<mirror::Class> klass) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5745 | if (klass->IsInterface()) { |
| 5746 | return true; |
| 5747 | } |
Ian Rogers | 151f221 | 2014-05-06 11:27:27 -0700 | [diff] [blame] | 5748 | // Begin with the methods local to the superclass. |
Ian Rogers | ded66a0 | 2014-10-28 18:12:55 -0700 | [diff] [blame] | 5749 | Thread* self = Thread::Current(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5750 | StackHandleScope<1> hs(self); |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5751 | MutableHandle<mirror::Class> super_klass(hs.NewHandle<mirror::Class>(nullptr)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5752 | if (klass->HasSuperClass() && |
| 5753 | klass->GetClassLoader() != klass->GetSuperClass()->GetClassLoader()) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5754 | super_klass.Assign(klass->GetSuperClass()); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 5755 | for (int i = klass->GetSuperClass()->GetVTableLength() - 1; i >= 0; --i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5756 | auto* m = klass->GetVTableEntry(i, image_pointer_size_); |
| 5757 | auto* super_m = klass->GetSuperClass()->GetVTableEntry(i, image_pointer_size_); |
| 5758 | if (m != super_m) { |
Vladimir Marko | 942fd31 | 2017-01-16 20:52:19 +0000 | [diff] [blame] | 5759 | if (UNLIKELY(!HasSameSignatureWithDifferentClassLoaders(self, |
| 5760 | klass, |
| 5761 | super_klass, |
| 5762 | m, |
| 5763 | super_m))) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5764 | self->AssertPendingException(); |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5765 | return false; |
| 5766 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5767 | } |
| 5768 | } |
| 5769 | } |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 5770 | for (int32_t i = 0; i < klass->GetIfTableCount(); ++i) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5771 | super_klass.Assign(klass->GetIfTable()->GetInterface(i)); |
| 5772 | if (klass->GetClassLoader() != super_klass->GetClassLoader()) { |
| 5773 | uint32_t num_methods = super_klass->NumVirtualMethods(); |
Ian Rogers | 151f221 | 2014-05-06 11:27:27 -0700 | [diff] [blame] | 5774 | for (uint32_t j = 0; j < num_methods; ++j) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5775 | auto* m = klass->GetIfTable()->GetMethodArray(i)->GetElementPtrSize<ArtMethod*>( |
| 5776 | j, image_pointer_size_); |
| 5777 | auto* super_m = super_klass->GetVirtualMethod(j, image_pointer_size_); |
| 5778 | if (m != super_m) { |
Vladimir Marko | 942fd31 | 2017-01-16 20:52:19 +0000 | [diff] [blame] | 5779 | if (UNLIKELY(!HasSameSignatureWithDifferentClassLoaders(self, |
| 5780 | klass, |
| 5781 | super_klass, |
| 5782 | m, |
| 5783 | super_m))) { |
Vladimir Marko | d5e5a0e | 2015-05-08 12:26:59 +0100 | [diff] [blame] | 5784 | self->AssertPendingException(); |
Andreas Gampe | d8ca52e | 2015-02-13 15:23:18 -0800 | [diff] [blame] | 5785 | return false; |
| 5786 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5787 | } |
| 5788 | } |
| 5789 | } |
| 5790 | } |
| 5791 | return true; |
| 5792 | } |
| 5793 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5794 | bool ClassLinker::EnsureInitialized(Thread* self, |
| 5795 | Handle<mirror::Class> c, |
| 5796 | bool can_init_fields, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5797 | bool can_init_parents) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 5798 | DCHECK(c != nullptr); |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 5799 | |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 5800 | if (c->IsInitialized()) { |
Vladimir Marko | 8e11065 | 2019-07-30 10:14:41 +0100 | [diff] [blame] | 5801 | // If we've seen an initialized but not visibly initialized class |
| 5802 | // many times, request visible initialization. |
| 5803 | if (kRuntimeISA == InstructionSet::kX86 || kRuntimeISA == InstructionSet::kX86_64) { |
| 5804 | // Thanks to the x86 memory model classes skip the initialized status. |
| 5805 | DCHECK(c->IsVisiblyInitialized()); |
| 5806 | } else if (UNLIKELY(!c->IsVisiblyInitialized())) { |
| 5807 | if (self->IncrementMakeVisiblyInitializedCounter()) { |
| 5808 | MakeInitializedClassesVisiblyInitialized(self, /*wait=*/ false); |
| 5809 | } |
| 5810 | } |
Andreas Gampe | 5b20b35 | 2018-10-11 19:03:20 -0700 | [diff] [blame] | 5811 | DCHECK(c->WasVerificationAttempted()) << c->PrettyClassAndClassLoader(); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 5812 | return true; |
| 5813 | } |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 5814 | // SubtypeCheckInfo::Initialized must happen-before any new-instance for that type. |
| 5815 | // |
| 5816 | // Ensure the bitstring is initialized before any of the class initialization |
| 5817 | // logic occurs. Once a class initializer starts running, objects can |
| 5818 | // escape into the heap and use the subtype checking code. |
| 5819 | // |
| 5820 | // Note: A class whose SubtypeCheckInfo is at least Initialized means it |
| 5821 | // can be used as a source for the IsSubClass check, and that all ancestors |
| 5822 | // of the class are Assigned (can be used as a target for IsSubClass check) |
| 5823 | // or Overflowed (can be used as a source for IsSubClass check). |
Vladimir Marko | 305c38b | 2018-02-14 11:50:07 +0000 | [diff] [blame] | 5824 | if (kBitstringSubtypeCheckEnabled) { |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 5825 | MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_); |
Vladimir Marko | 38b8b25 | 2018-01-02 19:07:06 +0000 | [diff] [blame] | 5826 | SubtypeCheck<ObjPtr<mirror::Class>>::EnsureInitialized(c.Get()); |
Igor Murashkin | 86083f7 | 2017-10-27 10:59:04 -0700 | [diff] [blame] | 5827 | // TODO: Avoid taking subtype_check_lock_ if SubtypeCheck is already initialized. |
| 5828 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5829 | const bool success = InitializeClass(self, c, can_init_fields, can_init_parents); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 5830 | if (!success) { |
| 5831 | if (can_init_fields && can_init_parents) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5832 | CHECK(self->IsExceptionPending()) << c->PrettyClass(); |
Mathieu Chartier | 524507a | 2014-08-27 15:28:28 -0700 | [diff] [blame] | 5833 | } |
| 5834 | } else { |
| 5835 | self->AssertNoPendingException(); |
Ian Rogers | 595799e | 2012-01-11 17:32:51 -0800 | [diff] [blame] | 5836 | } |
| 5837 | return success; |
Elliott Hughes | f4c21c9 | 2011-08-19 17:31:31 -0700 | [diff] [blame] | 5838 | } |
| 5839 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5840 | void ClassLinker::FixupTemporaryDeclaringClass(ObjPtr<mirror::Class> temp_class, |
| 5841 | ObjPtr<mirror::Class> new_class) { |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 5842 | DCHECK_EQ(temp_class->NumInstanceFields(), 0u); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 5843 | for (ArtField& field : new_class->GetIFields()) { |
| 5844 | if (field.GetDeclaringClass() == temp_class) { |
| 5845 | field.SetDeclaringClass(new_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5846 | } |
| 5847 | } |
| 5848 | |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 5849 | DCHECK_EQ(temp_class->NumStaticFields(), 0u); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 5850 | for (ArtField& field : new_class->GetSFields()) { |
| 5851 | if (field.GetDeclaringClass() == temp_class) { |
| 5852 | field.SetDeclaringClass(new_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5853 | } |
| 5854 | } |
| 5855 | |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 5856 | DCHECK_EQ(temp_class->NumDirectMethods(), 0u); |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 5857 | DCHECK_EQ(temp_class->NumVirtualMethods(), 0u); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 5858 | for (auto& method : new_class->GetMethods(image_pointer_size_)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 5859 | if (method.GetDeclaringClass() == temp_class) { |
| 5860 | method.SetDeclaringClass(new_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5861 | } |
| 5862 | } |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 5863 | |
| 5864 | // Make sure the remembered set and mod-union tables know that we updated some of the native |
| 5865 | // roots. |
Mathieu Chartier | 88ea61e | 2018-06-20 17:45:41 -0700 | [diff] [blame] | 5866 | WriteBarrier::ForEveryFieldWrite(new_class); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5867 | } |
| 5868 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5869 | void ClassLinker::RegisterClassLoader(ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | 5b83050 | 2016-03-02 10:30:23 -0800 | [diff] [blame] | 5870 | CHECK(class_loader->GetAllocator() == nullptr); |
| 5871 | CHECK(class_loader->GetClassTable() == nullptr); |
| 5872 | Thread* const self = Thread::Current(); |
| 5873 | ClassLoaderData data; |
Ian Rogers | 55256cb | 2017-12-21 17:07:11 -0800 | [diff] [blame] | 5874 | data.weak_root = self->GetJniEnv()->GetVm()->AddWeakGlobalRef(self, class_loader); |
Mathieu Chartier | 5b83050 | 2016-03-02 10:30:23 -0800 | [diff] [blame] | 5875 | // Create and set the class table. |
| 5876 | data.class_table = new ClassTable; |
| 5877 | class_loader->SetClassTable(data.class_table); |
| 5878 | // Create and set the linear allocator. |
| 5879 | data.allocator = Runtime::Current()->CreateLinearAlloc(); |
| 5880 | class_loader->SetAllocator(data.allocator); |
| 5881 | // Add to the list so that we know to free the data later. |
| 5882 | class_loaders_.push_back(data); |
| 5883 | } |
| 5884 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5885 | ClassTable* ClassLinker::InsertClassTableForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 5886 | if (class_loader == nullptr) { |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 5887 | return boot_class_table_.get(); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5888 | } |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 5889 | ClassTable* class_table = class_loader->GetClassTable(); |
| 5890 | if (class_table == nullptr) { |
Mathieu Chartier | 5b83050 | 2016-03-02 10:30:23 -0800 | [diff] [blame] | 5891 | RegisterClassLoader(class_loader); |
| 5892 | class_table = class_loader->GetClassTable(); |
| 5893 | DCHECK(class_table != nullptr); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 5894 | } |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5895 | return class_table; |
| 5896 | } |
| 5897 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5898 | ClassTable* ClassLinker::ClassTableForClassLoader(ObjPtr<mirror::ClassLoader> class_loader) { |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 5899 | return class_loader == nullptr ? boot_class_table_.get() : class_loader->GetClassTable(); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 5900 | } |
| 5901 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 5902 | static ImTable* FindSuperImt(ObjPtr<mirror::Class> klass, PointerSize pointer_size) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 5903 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5904 | while (klass->HasSuperClass()) { |
| 5905 | klass = klass->GetSuperClass(); |
| 5906 | if (klass->ShouldHaveImt()) { |
| 5907 | return klass->GetImt(pointer_size); |
| 5908 | } |
| 5909 | } |
| 5910 | return nullptr; |
| 5911 | } |
| 5912 | |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 5913 | bool ClassLinker::LinkClass(Thread* self, |
| 5914 | const char* descriptor, |
| 5915 | Handle<mirror::Class> klass, |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 5916 | Handle<mirror::ObjectArray<mirror::Class>> interfaces, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5917 | MutableHandle<mirror::Class>* h_new_class_out) { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5918 | CHECK_EQ(ClassStatus::kLoaded, klass->GetStatus()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5919 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5920 | if (!LinkSuperClass(klass)) { |
| 5921 | return false; |
| 5922 | } |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5923 | ArtMethod* imt_data[ImTable::kSize]; |
| 5924 | // If there are any new conflicts compared to super class. |
| 5925 | bool new_conflict = false; |
Nicolas Geoffray | 918dcea | 2017-07-21 07:58:14 +0000 | [diff] [blame] | 5926 | std::fill_n(imt_data, arraysize(imt_data), Runtime::Current()->GetImtUnimplementedMethod()); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5927 | if (!LinkMethods(self, klass, interfaces, &new_conflict, imt_data)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5928 | return false; |
| 5929 | } |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 5930 | if (!LinkInstanceFields(self, klass)) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 5931 | return false; |
| 5932 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5933 | size_t class_size; |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 5934 | if (!LinkStaticFields(self, klass, &class_size)) { |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 5935 | return false; |
| 5936 | } |
| 5937 | CreateReferenceInstanceOffsets(klass); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5938 | CHECK_EQ(ClassStatus::kLoaded, klass->GetStatus()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5939 | |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5940 | ImTable* imt = nullptr; |
| 5941 | if (klass->ShouldHaveImt()) { |
| 5942 | // If there are any new conflicts compared to the super class we can not make a copy. There |
| 5943 | // can be cases where both will have a conflict method at the same slot without having the same |
| 5944 | // set of conflicts. In this case, we can not share the IMT since the conflict table slow path |
| 5945 | // will possibly create a table that is incorrect for either of the classes. |
| 5946 | // Same IMT with new_conflict does not happen very often. |
| 5947 | if (!new_conflict) { |
| 5948 | ImTable* super_imt = FindSuperImt(klass.Get(), image_pointer_size_); |
| 5949 | if (super_imt != nullptr) { |
| 5950 | bool imt_equals = true; |
| 5951 | for (size_t i = 0; i < ImTable::kSize && imt_equals; ++i) { |
| 5952 | imt_equals = imt_equals && (super_imt->Get(i, image_pointer_size_) == imt_data[i]); |
| 5953 | } |
| 5954 | if (imt_equals) { |
| 5955 | imt = super_imt; |
| 5956 | } |
| 5957 | } |
| 5958 | } |
| 5959 | if (imt == nullptr) { |
| 5960 | LinearAlloc* allocator = GetAllocatorForClassLoader(klass->GetClassLoader()); |
| 5961 | imt = reinterpret_cast<ImTable*>( |
| 5962 | allocator->Alloc(self, ImTable::SizeInBytes(image_pointer_size_))); |
| 5963 | if (imt == nullptr) { |
| 5964 | return false; |
| 5965 | } |
| 5966 | imt->Populate(imt_data, image_pointer_size_); |
| 5967 | } |
| 5968 | } |
| 5969 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5970 | if (!klass->IsTemp() || (!init_done_ && klass->GetClassSize() == class_size)) { |
| 5971 | // We don't need to retire this class as it has no embedded tables or it was created the |
| 5972 | // correct size during class linker initialization. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 5973 | CHECK_EQ(klass->GetClassSize(), class_size) << klass->PrettyDescriptor(); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5974 | |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5975 | if (klass->ShouldHaveEmbeddedVTable()) { |
| 5976 | klass->PopulateEmbeddedVTable(image_pointer_size_); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5977 | } |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 5978 | if (klass->ShouldHaveImt()) { |
| 5979 | klass->SetImt(imt, image_pointer_size_); |
| 5980 | } |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 5981 | |
| 5982 | // Update CHA info based on whether we override methods. |
| 5983 | // Have to do this before setting the class as resolved which allows |
| 5984 | // instantiation of klass. |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 5985 | if (LIKELY(descriptor != nullptr) && cha_ != nullptr) { |
Andreas Gampe | c1ac9ee | 2017-07-24 22:35:49 -0700 | [diff] [blame] | 5986 | cha_->UpdateAfterLoadingOf(klass); |
| 5987 | } |
Nicolas Geoffray | 918dcea | 2017-07-21 07:58:14 +0000 | [diff] [blame] | 5988 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5989 | // This will notify waiters on klass that saw the not yet resolved |
| 5990 | // class in the class_table_ during EnsureResolved. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 5991 | mirror::Class::SetStatus(klass, ClassStatus::kResolved, self); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 5992 | h_new_class_out->Assign(klass.Get()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 5993 | } else { |
| 5994 | CHECK(!klass->IsResolved()); |
| 5995 | // Retire the temporary class and create the correctly sized resolved class. |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 5996 | StackHandleScope<1> hs(self); |
Vladimir Marko | 3068d58 | 2019-05-28 16:39:29 +0100 | [diff] [blame] | 5997 | Handle<mirror::Class> h_new_class = |
| 5998 | 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] | 5999 | // Set arrays to null since we don't want to have multiple classes with the same ArtField or |
| 6000 | // ArtMethod array pointers. If this occurs, it causes bugs in remembered sets since the GC |
| 6001 | // may not see any references to the target space and clean the card for a class if another |
| 6002 | // class had the same array pointer. |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6003 | klass->SetMethodsPtrUnchecked(nullptr, 0, 0); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 6004 | klass->SetSFieldsPtrUnchecked(nullptr); |
| 6005 | klass->SetIFieldsPtrUnchecked(nullptr); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 6006 | if (UNLIKELY(h_new_class == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6007 | self->AssertPendingOOMException(); |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 6008 | mirror::Class::SetStatus(klass, ClassStatus::kErrorUnresolved, self); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 6009 | return false; |
| 6010 | } |
| 6011 | |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 6012 | CHECK_EQ(h_new_class->GetClassSize(), class_size); |
| 6013 | ObjectLock<mirror::Class> lock(self, h_new_class); |
| 6014 | FixupTemporaryDeclaringClass(klass.Get(), h_new_class.Get()); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 6015 | |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 6016 | if (LIKELY(descriptor != nullptr)) { |
Mathieu Chartier | eb837eb | 2015-07-29 17:25:41 -0700 | [diff] [blame] | 6017 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Vladimir Marko | 0984e48 | 2019-03-27 16:41:41 +0000 | [diff] [blame] | 6018 | const ObjPtr<mirror::ClassLoader> class_loader = h_new_class.Get()->GetClassLoader(); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 6019 | ClassTable* const table = InsertClassTableForClassLoader(class_loader); |
Vladimir Marko | 0984e48 | 2019-03-27 16:41:41 +0000 | [diff] [blame] | 6020 | const ObjPtr<mirror::Class> existing = |
| 6021 | table->UpdateClass(descriptor, h_new_class.Get(), ComputeModifiedUtf8Hash(descriptor)); |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 6022 | if (class_loader != nullptr) { |
| 6023 | // We updated the class in the class table, perform the write barrier so that the GC knows |
| 6024 | // about the change. |
Mathieu Chartier | 88ea61e | 2018-06-20 17:45:41 -0700 | [diff] [blame] | 6025 | WriteBarrier::ForEveryFieldWrite(class_loader); |
Mathieu Chartier | 05aa4d3 | 2015-09-19 12:44:38 -0700 | [diff] [blame] | 6026 | } |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 6027 | CHECK_EQ(existing, klass.Get()); |
Vladimir Marko | 1998cd0 | 2017-01-13 13:02:58 +0000 | [diff] [blame] | 6028 | if (log_new_roots_) { |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 6029 | new_class_roots_.push_back(GcRoot<mirror::Class>(h_new_class.Get())); |
| 6030 | } |
| 6031 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 6032 | |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 6033 | // Update CHA info based on whether we override methods. |
| 6034 | // Have to do this before setting the class as resolved which allows |
| 6035 | // instantiation of klass. |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 6036 | if (LIKELY(descriptor != nullptr) && cha_ != nullptr) { |
Andreas Gampe | c1ac9ee | 2017-07-24 22:35:49 -0700 | [diff] [blame] | 6037 | cha_->UpdateAfterLoadingOf(h_new_class); |
| 6038 | } |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 6039 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 6040 | // This will notify waiters on temp class that saw the not yet resolved class in the |
| 6041 | // class_table_ during EnsureResolved. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 6042 | mirror::Class::SetStatus(klass, ClassStatus::kRetired, self); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 6043 | |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 6044 | CHECK_EQ(h_new_class->GetStatus(), ClassStatus::kResolving); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 6045 | // This will notify waiters on new_class that saw the not yet resolved |
| 6046 | // class in the class_table_ during EnsureResolved. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 6047 | mirror::Class::SetStatus(h_new_class, ClassStatus::kResolved, self); |
Hiroshi Yamauchi | 679b1cf | 2015-05-21 12:05:27 -0700 | [diff] [blame] | 6048 | // Return the new class. |
| 6049 | h_new_class_out->Assign(h_new_class.Get()); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 6050 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6051 | return true; |
| 6052 | } |
| 6053 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 6054 | bool ClassLinker::LoadSuperAndInterfaces(Handle<mirror::Class> klass, const DexFile& dex_file) { |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 6055 | CHECK_EQ(ClassStatus::kIdx, klass->GetStatus()); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 6056 | const dex::ClassDef& class_def = dex_file.GetClassDef(klass->GetDexClassDefIndex()); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 6057 | dex::TypeIndex super_class_idx = class_def.superclass_idx_; |
| 6058 | if (super_class_idx.IsValid()) { |
Roland Levillain | 90328ac | 2016-05-18 12:25:38 +0100 | [diff] [blame] | 6059 | // Check that a class does not inherit from itself directly. |
| 6060 | // |
| 6061 | // TODO: This is a cheap check to detect the straightforward case |
| 6062 | // of a class extending itself (b/28685551), but we should do a |
| 6063 | // proper cycle detection on loaded classes, to detect all cases |
| 6064 | // of class circularity errors (b/28830038). |
| 6065 | if (super_class_idx == class_def.class_idx_) { |
| 6066 | ThrowClassCircularityError(klass.Get(), |
| 6067 | "Class %s extends itself", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6068 | klass->PrettyDescriptor().c_str()); |
Roland Levillain | 90328ac | 2016-05-18 12:25:38 +0100 | [diff] [blame] | 6069 | return false; |
| 6070 | } |
| 6071 | |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 6072 | ObjPtr<mirror::Class> super_class = ResolveType(super_class_idx, klass.Get()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 6073 | if (super_class == nullptr) { |
Brian Carlstrom | 65ca077 | 2011-09-24 16:03:08 -0700 | [diff] [blame] | 6074 | DCHECK(Thread::Current()->IsExceptionPending()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6075 | return false; |
| 6076 | } |
Ian Rogers | be125a9 | 2012-01-11 15:19:49 -0800 | [diff] [blame] | 6077 | // Verify |
| 6078 | if (!klass->CanAccess(super_class)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 6079 | ThrowIllegalAccessError(klass.Get(), "Class %s extended by class %s is inaccessible", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6080 | super_class->PrettyDescriptor().c_str(), |
| 6081 | klass->PrettyDescriptor().c_str()); |
Ian Rogers | be125a9 | 2012-01-11 15:19:49 -0800 | [diff] [blame] | 6082 | return false; |
| 6083 | } |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 6084 | CHECK(super_class->IsResolved()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 6085 | klass->SetSuperClass(super_class); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6086 | } |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 6087 | const dex::TypeList* interfaces = dex_file.GetInterfacesList(class_def); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 6088 | if (interfaces != nullptr) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 6089 | for (size_t i = 0; i < interfaces->Size(); i++) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 6090 | dex::TypeIndex idx = interfaces->GetTypeItem(i).type_idx_; |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 6091 | ObjPtr<mirror::Class> interface = ResolveType(idx, klass.Get()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 6092 | if (interface == nullptr) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 6093 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 6094 | return false; |
| 6095 | } |
| 6096 | // Verify |
| 6097 | if (!klass->CanAccess(interface)) { |
| 6098 | // TODO: the RI seemed to ignore this in my testing. |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 6099 | ThrowIllegalAccessError(klass.Get(), |
| 6100 | "Interface %s implemented by class %s is inaccessible", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6101 | interface->PrettyDescriptor().c_str(), |
| 6102 | klass->PrettyDescriptor().c_str()); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 6103 | return false; |
| 6104 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6105 | } |
| 6106 | } |
Brian Carlstrom | 74eb46a | 2011-08-02 20:10:14 -0700 | [diff] [blame] | 6107 | // Mark the class as loaded. |
Vladimir Marko | 2c64a83 | 2018-01-04 11:31:56 +0000 | [diff] [blame] | 6108 | mirror::Class::SetStatus(klass, ClassStatus::kLoaded, nullptr); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6109 | return true; |
| 6110 | } |
| 6111 | |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 6112 | bool ClassLinker::LinkSuperClass(Handle<mirror::Class> klass) { |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6113 | CHECK(!klass->IsPrimitive()); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6114 | ObjPtr<mirror::Class> super = klass->GetSuperClass(); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 6115 | ObjPtr<mirror::Class> object_class = GetClassRoot<mirror::Object>(this); |
| 6116 | if (klass.Get() == object_class) { |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 6117 | if (super != nullptr) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 6118 | ThrowClassFormatError(klass.Get(), "java.lang.Object must not have a superclass"); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6119 | return false; |
| 6120 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6121 | return true; |
| 6122 | } |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 6123 | if (super == nullptr) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 6124 | ThrowLinkageError(klass.Get(), "No superclass defined for class %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6125 | klass->PrettyDescriptor().c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6126 | return false; |
| 6127 | } |
| 6128 | // Verify |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 6129 | if (klass->IsInterface() && super != object_class) { |
Vladimir Marko | 1fcae9f | 2017-11-28 14:14:19 +0000 | [diff] [blame] | 6130 | ThrowClassFormatError(klass.Get(), "Interfaces must have java.lang.Object as superclass"); |
| 6131 | return false; |
| 6132 | } |
Vladimir Marko | b43b2d8 | 2017-07-18 17:46:38 +0100 | [diff] [blame] | 6133 | if (super->IsFinal()) { |
| 6134 | ThrowVerifyError(klass.Get(), |
| 6135 | "Superclass %s of %s is declared final", |
| 6136 | super->PrettyDescriptor().c_str(), |
| 6137 | klass->PrettyDescriptor().c_str()); |
| 6138 | return false; |
| 6139 | } |
| 6140 | if (super->IsInterface()) { |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 6141 | ThrowIncompatibleClassChangeError(klass.Get(), |
Vladimir Marko | b43b2d8 | 2017-07-18 17:46:38 +0100 | [diff] [blame] | 6142 | "Superclass %s of %s is an interface", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6143 | super->PrettyDescriptor().c_str(), |
Vladimir Marko | b43b2d8 | 2017-07-18 17:46:38 +0100 | [diff] [blame] | 6144 | klass->PrettyDescriptor().c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6145 | return false; |
| 6146 | } |
| 6147 | if (!klass->CanAccess(super)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 6148 | ThrowIllegalAccessError(klass.Get(), "Superclass %s is inaccessible to class %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6149 | super->PrettyDescriptor().c_str(), |
| 6150 | klass->PrettyDescriptor().c_str()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6151 | return false; |
| 6152 | } |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 6153 | |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 6154 | // Inherit kAccClassIsFinalizable from the superclass in case this |
| 6155 | // class doesn't override finalize. |
Elliott Hughes | 20cde90 | 2011-10-04 17:37:27 -0700 | [diff] [blame] | 6156 | if (super->IsFinalizable()) { |
| 6157 | klass->SetFinalizable(); |
| 6158 | } |
| 6159 | |
Mathieu Chartier | e4275c0 | 2015-08-06 15:34:15 -0700 | [diff] [blame] | 6160 | // Inherit class loader flag form super class. |
| 6161 | if (super->IsClassLoaderClass()) { |
| 6162 | klass->SetClassLoaderClass(); |
| 6163 | } |
| 6164 | |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 6165 | // Inherit reference flags (if any) from the superclass. |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 6166 | uint32_t reference_flags = (super->GetClassFlags() & mirror::kClassFlagReference); |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 6167 | if (reference_flags != 0) { |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 6168 | CHECK_EQ(klass->GetClassFlags(), 0u); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 6169 | klass->SetClassFlags(klass->GetClassFlags() | reference_flags); |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 6170 | } |
Elliott Hughes | 72ee0ae | 2011-10-10 17:31:28 -0700 | [diff] [blame] | 6171 | // Disallow custom direct subclasses of java.lang.ref.Reference. |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 6172 | if (init_done_ && super == GetClassRoot<mirror::Reference>(this)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 6173 | ThrowLinkageError(klass.Get(), |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 6174 | "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] | 6175 | klass->PrettyDescriptor().c_str()); |
Elliott Hughes | 72ee0ae | 2011-10-10 17:31:28 -0700 | [diff] [blame] | 6176 | return false; |
| 6177 | } |
Elliott Hughes | 2da5036 | 2011-10-10 16:57:08 -0700 | [diff] [blame] | 6178 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 6179 | if (kIsDebugBuild) { |
| 6180 | // Ensure super classes are fully resolved prior to resolving fields.. |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 6181 | while (super != nullptr) { |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 6182 | CHECK(super->IsResolved()); |
| 6183 | super = super->GetSuperClass(); |
| 6184 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 6185 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6186 | return true; |
| 6187 | } |
| 6188 | |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 6189 | // A wrapper class representing the result of a method translation used for linking methods and |
| 6190 | // updating superclass default methods. For each method in a classes vtable there are 4 states it |
| 6191 | // could be in: |
| 6192 | // 1) No translation is necessary. In this case there is no MethodTranslation object for it. This |
| 6193 | // is the standard case and is true when the method is not overridable by a default method, |
| 6194 | // the class defines a concrete implementation of the method, the default method implementation |
| 6195 | // remains the same, or an abstract method stayed abstract. |
| 6196 | // 2) The method must be translated to a different default method. We note this with |
| 6197 | // CreateTranslatedMethod. |
| 6198 | // 3) The method must be replaced with a conflict method. This happens when a superclass |
| 6199 | // implements an interface with a default method and this class implements an unrelated |
| 6200 | // interface that also defines that default method. We note this with CreateConflictingMethod. |
| 6201 | // 4) The method must be replaced with an abstract miranda method. This happens when a superclass |
| 6202 | // implements an interface with a default method and this class implements a subinterface of |
| 6203 | // the superclass's interface which declares the default method abstract. We note this with |
| 6204 | // CreateAbstractMethod. |
| 6205 | // |
| 6206 | // When a method translation is unnecessary (case #1), we don't put it into the |
| 6207 | // default_translation maps. So an instance of MethodTranslation must be in one of #2-#4. |
| 6208 | class ClassLinker::MethodTranslation { |
| 6209 | public: |
| 6210 | MethodTranslation() : translation_(nullptr), type_(Type::kInvalid) {} |
| 6211 | |
| 6212 | // This slot must become a default conflict method. |
| 6213 | static MethodTranslation CreateConflictingMethod() { |
| 6214 | return MethodTranslation(Type::kConflict, /*translation=*/nullptr); |
| 6215 | } |
| 6216 | |
| 6217 | // This slot must become an abstract method. |
| 6218 | static MethodTranslation CreateAbstractMethod() { |
| 6219 | return MethodTranslation(Type::kAbstract, /*translation=*/nullptr); |
| 6220 | } |
| 6221 | |
| 6222 | // Use the given method as the current value for this vtable slot during translation. |
| 6223 | static MethodTranslation CreateTranslatedMethod(ArtMethod* new_method) { |
| 6224 | return MethodTranslation(Type::kTranslation, new_method); |
| 6225 | } |
| 6226 | |
| 6227 | // Returns true if this is a method that must become a conflict method. |
| 6228 | bool IsInConflict() const { |
| 6229 | return type_ == Type::kConflict; |
| 6230 | } |
| 6231 | |
| 6232 | // Returns true if this is a method that must become an abstract method. |
| 6233 | bool IsAbstract() const { |
| 6234 | return type_ == Type::kAbstract; |
| 6235 | } |
| 6236 | |
| 6237 | // Returns true if this is a method that must become a different method. |
| 6238 | bool IsTranslation() const { |
| 6239 | return type_ == Type::kTranslation; |
| 6240 | } |
| 6241 | |
| 6242 | // Get the translated version of this method. |
| 6243 | ArtMethod* GetTranslation() const { |
| 6244 | DCHECK(IsTranslation()); |
| 6245 | DCHECK(translation_ != nullptr); |
| 6246 | return translation_; |
| 6247 | } |
| 6248 | |
| 6249 | private: |
| 6250 | enum class Type { |
| 6251 | kInvalid, |
| 6252 | kTranslation, |
| 6253 | kConflict, |
| 6254 | kAbstract, |
| 6255 | }; |
| 6256 | |
| 6257 | MethodTranslation(Type type, ArtMethod* translation) |
| 6258 | : translation_(translation), type_(type) {} |
| 6259 | |
| 6260 | ArtMethod* translation_; |
| 6261 | Type type_; |
| 6262 | }; |
| 6263 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 6264 | // Populate the class vtable and itable. Compute return type indices. |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 6265 | bool ClassLinker::LinkMethods(Thread* self, |
| 6266 | Handle<mirror::Class> klass, |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6267 | Handle<mirror::ObjectArray<mirror::Class>> interfaces, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6268 | bool* out_new_conflict, |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 6269 | ArtMethod** out_imt) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 6270 | self->AllowThreadSuspension(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6271 | // A map from vtable indexes to the method they need to be updated to point to. Used because we |
| 6272 | // need to have default methods be in the virtuals array of each class but we don't set that up |
| 6273 | // until LinkInterfaceMethods. |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 6274 | constexpr size_t kBufferSize = 8; // Avoid malloc/free for a few translations. |
| 6275 | std::pair<size_t, ClassLinker::MethodTranslation> buffer[kBufferSize]; |
| 6276 | HashMap<size_t, ClassLinker::MethodTranslation> default_translations(buffer, kBufferSize); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6277 | // Link virtual methods then interface methods. |
| 6278 | // We set up the interface lookup table first because we need it to determine if we need to update |
| 6279 | // any vtable entries with new default method implementations. |
| 6280 | return SetupInterfaceLookupTable(self, klass, interfaces) |
| 6281 | && LinkVirtualMethods(self, klass, /*out*/ &default_translations) |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6282 | && LinkInterfaceMethods(self, klass, default_translations, out_new_conflict, out_imt); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6283 | } |
| 6284 | |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 6285 | // Comparator for name and signature of a method, used in finding overriding methods. Implementation |
| 6286 | // avoids the use of handles, if it didn't then rather than compare dex files we could compare dex |
| 6287 | // caches in the implementation below. |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 6288 | class MethodNameAndSignatureComparator final : public ValueObject { |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 6289 | public: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6290 | explicit MethodNameAndSignatureComparator(ArtMethod* method) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 6291 | REQUIRES_SHARED(Locks::mutator_lock_) : |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 6292 | dex_file_(method->GetDexFile()), mid_(&dex_file_->GetMethodId(method->GetDexMethodIndex())), |
| 6293 | name_(nullptr), name_len_(0) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6294 | DCHECK(!method->IsProxyMethod()) << method->PrettyMethod(); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6295 | } |
| 6296 | |
| 6297 | const char* GetName() { |
| 6298 | if (name_ == nullptr) { |
| 6299 | name_ = dex_file_->StringDataAndUtf16LengthByIdx(mid_->name_idx_, &name_len_); |
| 6300 | } |
| 6301 | return name_; |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 6302 | } |
| 6303 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6304 | bool HasSameNameAndSignature(ArtMethod* other) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 6305 | REQUIRES_SHARED(Locks::mutator_lock_) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6306 | DCHECK(!other->IsProxyMethod()) << other->PrettyMethod(); |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 6307 | const DexFile* other_dex_file = other->GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 6308 | const dex::MethodId& other_mid = other_dex_file->GetMethodId(other->GetDexMethodIndex()); |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 6309 | if (dex_file_ == other_dex_file) { |
| 6310 | return mid_->name_idx_ == other_mid.name_idx_ && mid_->proto_idx_ == other_mid.proto_idx_; |
| 6311 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6312 | GetName(); // Only used to make sure its calculated. |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 6313 | uint32_t other_name_len; |
| 6314 | const char* other_name = other_dex_file->StringDataAndUtf16LengthByIdx(other_mid.name_idx_, |
| 6315 | &other_name_len); |
| 6316 | if (name_len_ != other_name_len || strcmp(name_, other_name) != 0) { |
| 6317 | return false; |
| 6318 | } |
| 6319 | return dex_file_->GetMethodSignature(*mid_) == other_dex_file->GetMethodSignature(other_mid); |
| 6320 | } |
| 6321 | |
| 6322 | private: |
| 6323 | // Dex file for the method to compare against. |
| 6324 | const DexFile* const dex_file_; |
| 6325 | // MethodId for the method to compare against. |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 6326 | const dex::MethodId* const mid_; |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 6327 | // Lazily computed name from the dex file's strings. |
| 6328 | const char* name_; |
| 6329 | // Lazily computed name length. |
| 6330 | uint32_t name_len_; |
| 6331 | }; |
| 6332 | |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6333 | class LinkVirtualHashTable { |
| 6334 | public: |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 6335 | LinkVirtualHashTable(Handle<mirror::Class> klass, |
| 6336 | size_t hash_size, |
| 6337 | uint32_t* hash_table, |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 6338 | PointerSize image_pointer_size) |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 6339 | : klass_(klass), |
| 6340 | hash_size_(hash_size), |
| 6341 | hash_table_(hash_table), |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6342 | image_pointer_size_(image_pointer_size) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6343 | std::fill(hash_table_, hash_table_ + hash_size_, invalid_index_); |
| 6344 | } |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 6345 | |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 6346 | void Add(uint32_t virtual_method_index) REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6347 | ArtMethod* local_method = klass_->GetVirtualMethodDuringLinking( |
| 6348 | virtual_method_index, image_pointer_size_); |
| 6349 | const char* name = local_method->GetInterfaceMethodIfProxy(image_pointer_size_)->GetName(); |
Mathieu Chartier | e7c9a8c | 2014-11-06 16:35:45 -0800 | [diff] [blame] | 6350 | uint32_t hash = ComputeModifiedUtf8Hash(name); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6351 | uint32_t index = hash % hash_size_; |
| 6352 | // Linear probe until we have an empty slot. |
| 6353 | while (hash_table_[index] != invalid_index_) { |
| 6354 | if (++index == hash_size_) { |
| 6355 | index = 0; |
| 6356 | } |
| 6357 | } |
| 6358 | hash_table_[index] = virtual_method_index; |
| 6359 | } |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 6360 | |
Vladimir Marko | 4335474 | 2021-02-03 15:37:01 +0000 | [diff] [blame] | 6361 | uint32_t FindAndRemove(MethodNameAndSignatureComparator* comparator, uint32_t hash) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 6362 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | 4335474 | 2021-02-03 15:37:01 +0000 | [diff] [blame] | 6363 | DCHECK_EQ(hash, ComputeModifiedUtf8Hash(comparator->GetName())); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6364 | size_t index = hash % hash_size_; |
| 6365 | while (true) { |
| 6366 | const uint32_t value = hash_table_[index]; |
| 6367 | // Since linear probe makes continuous blocks, hitting an invalid index means we are done |
| 6368 | // the block and can safely assume not found. |
| 6369 | if (value == invalid_index_) { |
| 6370 | break; |
| 6371 | } |
| 6372 | if (value != removed_index_) { // This signifies not already overriden. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6373 | ArtMethod* virtual_method = |
| 6374 | klass_->GetVirtualMethodDuringLinking(value, image_pointer_size_); |
| 6375 | if (comparator->HasSameNameAndSignature( |
| 6376 | virtual_method->GetInterfaceMethodIfProxy(image_pointer_size_))) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6377 | hash_table_[index] = removed_index_; |
| 6378 | return value; |
| 6379 | } |
| 6380 | } |
| 6381 | if (++index == hash_size_) { |
| 6382 | index = 0; |
| 6383 | } |
| 6384 | } |
| 6385 | return GetNotFoundIndex(); |
| 6386 | } |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 6387 | |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6388 | static uint32_t GetNotFoundIndex() { |
| 6389 | return invalid_index_; |
| 6390 | } |
| 6391 | |
| 6392 | private: |
| 6393 | static const uint32_t invalid_index_; |
| 6394 | static const uint32_t removed_index_; |
| 6395 | |
| 6396 | Handle<mirror::Class> klass_; |
| 6397 | const size_t hash_size_; |
| 6398 | uint32_t* const hash_table_; |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 6399 | const PointerSize image_pointer_size_; |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6400 | }; |
| 6401 | |
| 6402 | const uint32_t LinkVirtualHashTable::invalid_index_ = std::numeric_limits<uint32_t>::max(); |
| 6403 | const uint32_t LinkVirtualHashTable::removed_index_ = std::numeric_limits<uint32_t>::max() - 1; |
| 6404 | |
Stephen Hines | 1ddd913 | 2017-02-08 01:51:18 -0800 | [diff] [blame] | 6405 | bool ClassLinker::LinkVirtualMethods( |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6406 | Thread* self, |
| 6407 | Handle<mirror::Class> klass, |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 6408 | /*out*/HashMap<size_t, ClassLinker::MethodTranslation>* default_translations) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6409 | const size_t num_virtual_methods = klass->NumVirtualMethods(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6410 | if (klass->IsInterface()) { |
| 6411 | // No vtable. |
| 6412 | if (!IsUint<16>(num_virtual_methods)) { |
| 6413 | ThrowClassFormatError(klass.Get(), "Too many methods on interface: %zu", num_virtual_methods); |
| 6414 | return false; |
| 6415 | } |
| 6416 | bool has_defaults = false; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6417 | // Assign each method an IMT index and set the default flag. |
| 6418 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
| 6419 | ArtMethod* m = klass->GetVirtualMethodDuringLinking(i, image_pointer_size_); |
| 6420 | m->SetMethodIndex(i); |
| 6421 | if (!m->IsAbstract()) { |
Vladimir Marko | 1c993cd | 2020-05-28 09:30:06 +0000 | [diff] [blame] | 6422 | // If the dex file does not support default methods, throw ClassFormatError. |
| 6423 | // This check is necessary to protect from odd cases, such as native default |
| 6424 | // methods, that the dex file verifier permits for old dex file versions. b/157170505 |
| 6425 | // FIXME: This should be `if (!m->GetDexFile()->SupportsDefaultMethods())` but we're |
| 6426 | // currently running CTS tests for default methods with dex file version 035 which |
| 6427 | // does not support default methods. So, we limit this to native methods. b/157718952 |
| 6428 | if (m->IsNative()) { |
| 6429 | DCHECK(!m->GetDexFile()->SupportsDefaultMethods()); |
| 6430 | ThrowClassFormatError(klass.Get(), |
| 6431 | "Dex file does not support default method '%s'", |
| 6432 | m->PrettyMethod().c_str()); |
| 6433 | return false; |
| 6434 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6435 | m->SetAccessFlags(m->GetAccessFlags() | kAccDefault); |
| 6436 | has_defaults = true; |
| 6437 | } |
| 6438 | } |
| 6439 | // Mark that we have default methods so that we won't need to scan the virtual_methods_ array |
| 6440 | // during initialization. This is a performance optimization. We could simply traverse the |
| 6441 | // virtual_methods_ array again during initialization. |
| 6442 | if (has_defaults) { |
| 6443 | klass->SetHasDefaultMethods(); |
| 6444 | } |
| 6445 | return true; |
| 6446 | } else if (klass->HasSuperClass()) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6447 | const size_t super_vtable_length = klass->GetSuperClass()->GetVTableLength(); |
| 6448 | const size_t max_count = num_virtual_methods + super_vtable_length; |
Vladimir Marko | 3068d58 | 2019-05-28 16:39:29 +0100 | [diff] [blame] | 6449 | StackHandleScope<3> hs(self); |
Mingyao Yang | 38eecb0 | 2014-08-13 14:51:03 -0700 | [diff] [blame] | 6450 | Handle<mirror::Class> super_class(hs.NewHandle(klass->GetSuperClass())); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6451 | MutableHandle<mirror::PointerArray> vtable; |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6452 | if (super_class->ShouldHaveEmbeddedVTable()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6453 | vtable = hs.NewHandle(AllocPointerArray(self, max_count)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 6454 | if (UNLIKELY(vtable == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6455 | self->AssertPendingOOMException(); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 6456 | return false; |
| 6457 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6458 | for (size_t i = 0; i < super_vtable_length; i++) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6459 | vtable->SetElementPtrSize( |
| 6460 | i, super_class->GetEmbeddedVTableEntry(i, image_pointer_size_), image_pointer_size_); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 6461 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6462 | // We might need to change vtable if we have new virtual methods or new interfaces (since that |
| 6463 | // might give us new default methods). If no new interfaces then we can skip the rest since |
| 6464 | // the class cannot override any of the super-class's methods. This is required for |
| 6465 | // correctness since without it we might not update overridden default method vtable entries |
| 6466 | // correctly. |
| 6467 | if (num_virtual_methods == 0 && super_class->GetIfTableCount() == klass->GetIfTableCount()) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6468 | klass->SetVTable(vtable.Get()); |
| 6469 | return true; |
| 6470 | } |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 6471 | } else { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6472 | DCHECK(super_class->IsAbstract() && !super_class->IsArrayClass()); |
Vladimir Marko | 3068d58 | 2019-05-28 16:39:29 +0100 | [diff] [blame] | 6473 | Handle<mirror::PointerArray> super_vtable = hs.NewHandle(super_class->GetVTable()); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6474 | CHECK(super_vtable != nullptr) << super_class->PrettyClass(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6475 | // We might need to change vtable if we have new virtual methods or new interfaces (since that |
| 6476 | // might give us new default methods). See comment above. |
| 6477 | if (num_virtual_methods == 0 && super_class->GetIfTableCount() == klass->GetIfTableCount()) { |
Vladimir Marko | 3068d58 | 2019-05-28 16:39:29 +0100 | [diff] [blame] | 6478 | klass->SetVTable(super_vtable.Get()); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6479 | return true; |
| 6480 | } |
Vladimir Marko | 3068d58 | 2019-05-28 16:39:29 +0100 | [diff] [blame] | 6481 | vtable = hs.NewHandle(ObjPtr<mirror::PointerArray>::DownCast( |
| 6482 | mirror::Array::CopyOf(super_vtable, self, max_count))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 6483 | if (UNLIKELY(vtable == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6484 | self->AssertPendingOOMException(); |
Mingyao Yang | 2cdbad7 | 2014-07-16 10:44:41 -0700 | [diff] [blame] | 6485 | return false; |
| 6486 | } |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 6487 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6488 | // How the algorithm works: |
| 6489 | // 1. Populate hash table by adding num_virtual_methods from klass. The values in the hash |
| 6490 | // table are: invalid_index for unused slots, index super_vtable_length + i for a virtual |
| 6491 | // method which has not been matched to a vtable method, and j if the virtual method at the |
| 6492 | // index overrode the super virtual method at index j. |
| 6493 | // 2. Loop through super virtual methods, if they overwrite, update hash table to j |
| 6494 | // (j < super_vtable_length) to avoid redundant checks. (TODO maybe use this info for reducing |
| 6495 | // the need for the initial vtable which we later shrink back down). |
| 6496 | // 3. Add non overridden methods to the end of the vtable. |
| 6497 | static constexpr size_t kMaxStackHash = 250; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6498 | // + 1 so that even if we only have new default methods we will still be able to use this hash |
| 6499 | // table (i.e. it will never have 0 size). |
| 6500 | const size_t hash_table_size = num_virtual_methods * 3 + 1; |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6501 | uint32_t* hash_table_ptr; |
| 6502 | std::unique_ptr<uint32_t[]> hash_heap_storage; |
| 6503 | if (hash_table_size <= kMaxStackHash) { |
| 6504 | hash_table_ptr = reinterpret_cast<uint32_t*>( |
| 6505 | alloca(hash_table_size * sizeof(*hash_table_ptr))); |
| 6506 | } else { |
| 6507 | hash_heap_storage.reset(new uint32_t[hash_table_size]); |
| 6508 | hash_table_ptr = hash_heap_storage.get(); |
| 6509 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6510 | LinkVirtualHashTable hash_table(klass, hash_table_size, hash_table_ptr, image_pointer_size_); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6511 | // Add virtual methods to the hash table. |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6512 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6513 | DCHECK(klass->GetVirtualMethodDuringLinking( |
| 6514 | i, image_pointer_size_)->GetDeclaringClass() != nullptr); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6515 | hash_table.Add(i); |
| 6516 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6517 | // Loop through each super vtable method and see if they are overridden by a method we added to |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6518 | // the hash table. |
| 6519 | for (size_t j = 0; j < super_vtable_length; ++j) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6520 | // Search the hash table to see if we are overridden by any method. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6521 | ArtMethod* super_method = vtable->GetElementPtrSize<ArtMethod*>(j, image_pointer_size_); |
Alex Light | c7a420c | 2016-10-18 14:33:18 -0700 | [diff] [blame] | 6522 | if (!klass->CanAccessMember(super_method->GetDeclaringClass(), |
| 6523 | super_method->GetAccessFlags())) { |
| 6524 | // Continue on to the next method since this one is package private and canot be overridden. |
| 6525 | // Before Android 4.1, the package-private method super_method might have been incorrectly |
| 6526 | // overridden. |
| 6527 | continue; |
| 6528 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6529 | MethodNameAndSignatureComparator super_method_name_comparator( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6530 | super_method->GetInterfaceMethodIfProxy(image_pointer_size_)); |
Alex Light | c7a420c | 2016-10-18 14:33:18 -0700 | [diff] [blame] | 6531 | // We remove the method so that subsequent lookups will be faster by making the hash-map |
| 6532 | // smaller as we go on. |
Vladimir Marko | 4335474 | 2021-02-03 15:37:01 +0000 | [diff] [blame] | 6533 | uint32_t hash = (j < mirror::Object::kVTableLength) |
| 6534 | ? object_virtual_method_hashes_[j] |
| 6535 | : ComputeModifiedUtf8Hash(super_method_name_comparator.GetName()); |
| 6536 | uint32_t hash_index = hash_table.FindAndRemove(&super_method_name_comparator, hash); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6537 | if (hash_index != hash_table.GetNotFoundIndex()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6538 | ArtMethod* virtual_method = klass->GetVirtualMethodDuringLinking( |
| 6539 | hash_index, image_pointer_size_); |
Alex Light | c7a420c | 2016-10-18 14:33:18 -0700 | [diff] [blame] | 6540 | if (super_method->IsFinal()) { |
| 6541 | ThrowLinkageError(klass.Get(), "Method %s overrides final method in class %s", |
| 6542 | virtual_method->PrettyMethod().c_str(), |
| 6543 | super_method->GetDeclaringClassDescriptor()); |
| 6544 | return false; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6545 | } |
Alex Light | c7a420c | 2016-10-18 14:33:18 -0700 | [diff] [blame] | 6546 | vtable->SetElementPtrSize(j, virtual_method, image_pointer_size_); |
| 6547 | virtual_method->SetMethodIndex(j); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6548 | } else if (super_method->IsOverridableByDefaultMethod()) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6549 | // We didn't directly override this method but we might through default methods... |
| 6550 | // Check for default method update. |
| 6551 | ArtMethod* default_method = nullptr; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6552 | switch (FindDefaultMethodImplementation(self, |
| 6553 | super_method, |
| 6554 | klass, |
| 6555 | /*out*/&default_method)) { |
| 6556 | case DefaultMethodSearchResult::kDefaultConflict: { |
| 6557 | // A conflict was found looking for default methods. Note this (assuming it wasn't |
| 6558 | // pre-existing) in the translations map. |
| 6559 | if (UNLIKELY(!super_method->IsDefaultConflicting())) { |
| 6560 | // Don't generate another conflict method to reduce memory use as an optimization. |
| 6561 | default_translations->insert( |
| 6562 | {j, ClassLinker::MethodTranslation::CreateConflictingMethod()}); |
| 6563 | } |
| 6564 | break; |
| 6565 | } |
| 6566 | case DefaultMethodSearchResult::kAbstractFound: { |
| 6567 | // No conflict but method is abstract. |
| 6568 | // We note that this vtable entry must be made abstract. |
| 6569 | if (UNLIKELY(!super_method->IsAbstract())) { |
| 6570 | default_translations->insert( |
| 6571 | {j, ClassLinker::MethodTranslation::CreateAbstractMethod()}); |
| 6572 | } |
| 6573 | break; |
| 6574 | } |
| 6575 | case DefaultMethodSearchResult::kDefaultFound: { |
| 6576 | if (UNLIKELY(super_method->IsDefaultConflicting() || |
| 6577 | default_method->GetDeclaringClass() != super_method->GetDeclaringClass())) { |
| 6578 | // Found a default method implementation that is new. |
| 6579 | // TODO Refactor this add default methods to virtuals here and not in |
| 6580 | // LinkInterfaceMethods maybe. |
| 6581 | // The problem is default methods might override previously present |
| 6582 | // default-method or miranda-method vtable entries from the superclass. |
| 6583 | // Unfortunately we need these to be entries in this class's virtuals. We do not |
| 6584 | // give these entries there until LinkInterfaceMethods so we pass this map around |
| 6585 | // to let it know which vtable entries need to be updated. |
| 6586 | // Make a note that vtable entry j must be updated, store what it needs to be updated |
| 6587 | // to. We will allocate a virtual method slot in LinkInterfaceMethods and fix it up |
| 6588 | // then. |
| 6589 | default_translations->insert( |
| 6590 | {j, ClassLinker::MethodTranslation::CreateTranslatedMethod(default_method)}); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6591 | VLOG(class_linker) << "Method " << super_method->PrettyMethod() |
| 6592 | << " overridden by default " |
| 6593 | << default_method->PrettyMethod() |
| 6594 | << " in " << mirror::Class::PrettyClass(klass.Get()); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6595 | } |
| 6596 | break; |
| 6597 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6598 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6599 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6600 | } |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6601 | size_t actual_count = super_vtable_length; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6602 | // Add the non-overridden methods at the end. |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6603 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6604 | ArtMethod* local_method = klass->GetVirtualMethodDuringLinking(i, image_pointer_size_); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6605 | size_t method_idx = local_method->GetMethodIndexDuringLinking(); |
| 6606 | if (method_idx < super_vtable_length && |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6607 | local_method == vtable->GetElementPtrSize<ArtMethod*>(method_idx, image_pointer_size_)) { |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6608 | continue; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6609 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6610 | vtable->SetElementPtrSize(actual_count, local_method, image_pointer_size_); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 6611 | local_method->SetMethodIndex(actual_count); |
| 6612 | ++actual_count; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6613 | } |
Andreas Gampe | ab1eb0d | 2015-02-13 19:23:55 -0800 | [diff] [blame] | 6614 | if (!IsUint<16>(actual_count)) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 6615 | ThrowClassFormatError(klass.Get(), "Too many methods defined on class: %zd", actual_count); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6616 | return false; |
| 6617 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 6618 | // Shrink vtable if possible |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6619 | CHECK_LE(actual_count, max_count); |
| 6620 | if (actual_count < max_count) { |
Vladimir Marko | 3068d58 | 2019-05-28 16:39:29 +0100 | [diff] [blame] | 6621 | vtable.Assign(ObjPtr<mirror::PointerArray>::DownCast( |
| 6622 | mirror::Array::CopyOf(vtable, self, actual_count))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 6623 | if (UNLIKELY(vtable == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6624 | self->AssertPendingOOMException(); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 6625 | return false; |
| 6626 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6627 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 6628 | klass->SetVTable(vtable.Get()); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6629 | } else { |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 6630 | CHECK_EQ(klass.Get(), GetClassRoot<mirror::Object>(this)); |
Andreas Gampe | ab1eb0d | 2015-02-13 19:23:55 -0800 | [diff] [blame] | 6631 | if (!IsUint<16>(num_virtual_methods)) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6632 | ThrowClassFormatError(klass.Get(), "Too many methods: %d", |
| 6633 | static_cast<int>(num_virtual_methods)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6634 | return false; |
| 6635 | } |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 6636 | ObjPtr<mirror::PointerArray> vtable = AllocPointerArray(self, num_virtual_methods); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6637 | if (UNLIKELY(vtable == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6638 | self->AssertPendingOOMException(); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 6639 | return false; |
| 6640 | } |
Brian Carlstrom | a40f9bc | 2011-07-26 21:26:07 -0700 | [diff] [blame] | 6641 | for (size_t i = 0; i < num_virtual_methods; ++i) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 6642 | ArtMethod* virtual_method = klass->GetVirtualMethodDuringLinking(i, image_pointer_size_); |
| 6643 | vtable->SetElementPtrSize(i, virtual_method, image_pointer_size_); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 6644 | virtual_method->SetMethodIndex(i & 0xFFFF); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6645 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 6646 | klass->SetVTable(vtable); |
Vladimir Marko | 4335474 | 2021-02-03 15:37:01 +0000 | [diff] [blame] | 6647 | InitializeObjectVirtualMethodHashes(klass.Get(), |
| 6648 | image_pointer_size_, |
| 6649 | ArrayRef<uint32_t>(object_virtual_method_hashes_)); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 6650 | } |
| 6651 | return true; |
| 6652 | } |
| 6653 | |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6654 | // Determine if the given iface has any subinterface in the given list that declares the method |
| 6655 | // specified by 'target'. |
| 6656 | // |
| 6657 | // Arguments |
| 6658 | // - self: The thread we are running on |
| 6659 | // - target: A comparator that will match any method that overrides the method we are checking for |
| 6660 | // - iftable: The iftable we are searching for an overriding method on. |
| 6661 | // - ifstart: The index of the interface we are checking to see if anything overrides |
| 6662 | // - iface: The interface we are checking to see if anything overrides. |
| 6663 | // - image_pointer_size: |
| 6664 | // The image pointer size. |
| 6665 | // |
| 6666 | // Returns |
| 6667 | // - True: There is some method that matches the target comparator defined in an interface that |
| 6668 | // is a subtype of iface. |
| 6669 | // - False: There is no method that matches the target comparator in any interface that is a subtype |
| 6670 | // of iface. |
| 6671 | static bool ContainsOverridingMethodOf(Thread* self, |
| 6672 | MethodNameAndSignatureComparator& target, |
| 6673 | Handle<mirror::IfTable> iftable, |
| 6674 | size_t ifstart, |
| 6675 | Handle<mirror::Class> iface, |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 6676 | PointerSize image_pointer_size) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 6677 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6678 | DCHECK(self != nullptr); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 6679 | DCHECK(iface != nullptr); |
| 6680 | DCHECK(iftable != nullptr); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6681 | DCHECK_GE(ifstart, 0u); |
| 6682 | DCHECK_LT(ifstart, iftable->Count()); |
| 6683 | DCHECK_EQ(iface.Get(), iftable->GetInterface(ifstart)); |
| 6684 | DCHECK(iface->IsInterface()); |
| 6685 | |
| 6686 | size_t iftable_count = iftable->Count(); |
| 6687 | StackHandleScope<1> hs(self); |
| 6688 | MutableHandle<mirror::Class> current_iface(hs.NewHandle<mirror::Class>(nullptr)); |
| 6689 | for (size_t k = ifstart + 1; k < iftable_count; k++) { |
| 6690 | // Skip ifstart since our current interface obviously cannot override itself. |
| 6691 | current_iface.Assign(iftable->GetInterface(k)); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6692 | // Iterate through every method on this interface. The order does not matter. |
| 6693 | for (ArtMethod& current_method : current_iface->GetDeclaredVirtualMethods(image_pointer_size)) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6694 | if (UNLIKELY(target.HasSameNameAndSignature( |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6695 | current_method.GetInterfaceMethodIfProxy(image_pointer_size)))) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6696 | // Check if the i'th interface is a subtype of this one. |
| 6697 | if (iface->IsAssignableFrom(current_iface.Get())) { |
| 6698 | return true; |
| 6699 | } |
| 6700 | break; |
| 6701 | } |
| 6702 | } |
| 6703 | } |
| 6704 | return false; |
| 6705 | } |
| 6706 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6707 | // Find the default method implementation for 'interface_method' in 'klass'. Stores it into |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6708 | // out_default_method and returns kDefaultFound on success. If no default method was found return |
| 6709 | // kAbstractFound and store nullptr into out_default_method. If an error occurs (such as a |
| 6710 | // default_method conflict) it will return kDefaultConflict. |
| 6711 | ClassLinker::DefaultMethodSearchResult ClassLinker::FindDefaultMethodImplementation( |
| 6712 | Thread* self, |
| 6713 | ArtMethod* target_method, |
| 6714 | Handle<mirror::Class> klass, |
| 6715 | /*out*/ArtMethod** out_default_method) const { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6716 | DCHECK(self != nullptr); |
| 6717 | DCHECK(target_method != nullptr); |
| 6718 | DCHECK(out_default_method != nullptr); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6719 | |
| 6720 | *out_default_method = nullptr; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6721 | |
| 6722 | // We organize the interface table so that, for interface I any subinterfaces J follow it in the |
| 6723 | // table. This lets us walk the table backwards when searching for default methods. The first one |
| 6724 | // we encounter is the best candidate since it is the most specific. Once we have found it we keep |
| 6725 | // track of it and then continue checking all other interfaces, since we need to throw an error if |
| 6726 | // we encounter conflicting default method implementations (one is not a subtype of the other). |
| 6727 | // |
| 6728 | // The order of unrelated interfaces does not matter and is not defined. |
| 6729 | size_t iftable_count = klass->GetIfTableCount(); |
| 6730 | if (iftable_count == 0) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6731 | // No interfaces. We have already reset out to null so just return kAbstractFound. |
| 6732 | return DefaultMethodSearchResult::kAbstractFound; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6733 | } |
| 6734 | |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6735 | StackHandleScope<3> hs(self); |
| 6736 | MutableHandle<mirror::Class> chosen_iface(hs.NewHandle<mirror::Class>(nullptr)); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6737 | MutableHandle<mirror::IfTable> iftable(hs.NewHandle(klass->GetIfTable())); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6738 | MutableHandle<mirror::Class> iface(hs.NewHandle<mirror::Class>(nullptr)); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6739 | MethodNameAndSignatureComparator target_name_comparator( |
| 6740 | target_method->GetInterfaceMethodIfProxy(image_pointer_size_)); |
| 6741 | // Iterates over the klass's iftable in reverse |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6742 | for (size_t k = iftable_count; k != 0; ) { |
| 6743 | --k; |
| 6744 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6745 | DCHECK_LT(k, iftable->Count()); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6746 | |
| 6747 | iface.Assign(iftable->GetInterface(k)); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 6748 | // Iterate through every declared method on this interface. The order does not matter. |
| 6749 | for (auto& method_iter : iface->GetDeclaredVirtualMethods(image_pointer_size_)) { |
| 6750 | ArtMethod* current_method = &method_iter; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6751 | // Skip abstract methods and methods with different names. |
| 6752 | if (current_method->IsAbstract() || |
| 6753 | !target_name_comparator.HasSameNameAndSignature( |
| 6754 | current_method->GetInterfaceMethodIfProxy(image_pointer_size_))) { |
| 6755 | continue; |
Alex Light | d7c10c2 | 2016-03-31 10:03:07 -0700 | [diff] [blame] | 6756 | } else if (!current_method->IsPublic()) { |
| 6757 | // The verifier should have caught the non-public method for dex version 37. Just warn and |
| 6758 | // skip it since this is from before default-methods so we don't really need to care that it |
| 6759 | // has code. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6760 | LOG(WARNING) << "Interface method " << current_method->PrettyMethod() |
| 6761 | << " is not public! " |
Alex Light | d7c10c2 | 2016-03-31 10:03:07 -0700 | [diff] [blame] | 6762 | << "This will be a fatal error in subsequent versions of android. " |
| 6763 | << "Continuing anyway."; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6764 | } |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 6765 | if (UNLIKELY(chosen_iface != nullptr)) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6766 | // We have multiple default impls of the same method. This is a potential default conflict. |
| 6767 | // We need to check if this possibly conflicting method is either a superclass of the chosen |
| 6768 | // default implementation or is overridden by a non-default interface method. In either case |
| 6769 | // there is no conflict. |
| 6770 | if (!iface->IsAssignableFrom(chosen_iface.Get()) && |
| 6771 | !ContainsOverridingMethodOf(self, |
| 6772 | target_name_comparator, |
| 6773 | iftable, |
| 6774 | k, |
| 6775 | iface, |
| 6776 | image_pointer_size_)) { |
Nicolas Geoffray | 7f3e0db | 2016-01-28 09:29:31 +0000 | [diff] [blame] | 6777 | VLOG(class_linker) << "Conflicting default method implementations found: " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6778 | << current_method->PrettyMethod() << " and " |
| 6779 | << ArtMethod::PrettyMethod(*out_default_method) << " in class " |
| 6780 | << klass->PrettyClass() << " conflict."; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6781 | *out_default_method = nullptr; |
| 6782 | return DefaultMethodSearchResult::kDefaultConflict; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6783 | } else { |
| 6784 | break; // Continue checking at the next interface. |
| 6785 | } |
| 6786 | } else { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6787 | // chosen_iface == null |
| 6788 | if (!ContainsOverridingMethodOf(self, |
| 6789 | target_name_comparator, |
| 6790 | iftable, |
| 6791 | k, |
| 6792 | iface, |
| 6793 | image_pointer_size_)) { |
| 6794 | // Don't set this as the chosen interface if something else is overriding it (because that |
| 6795 | // other interface would be potentially chosen instead if it was default). If the other |
| 6796 | // interface was abstract then we wouldn't select this interface as chosen anyway since |
| 6797 | // the abstract method masks it. |
| 6798 | *out_default_method = current_method; |
| 6799 | chosen_iface.Assign(iface.Get()); |
| 6800 | // We should now finish traversing the graph to find if we have default methods that |
| 6801 | // conflict. |
| 6802 | } else { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6803 | VLOG(class_linker) << "A default method '" << current_method->PrettyMethod() |
| 6804 | << "' was " |
| 6805 | << "skipped because it was overridden by an abstract method in a " |
| 6806 | << "subinterface on class '" << klass->PrettyClass() << "'"; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6807 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6808 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6809 | break; |
| 6810 | } |
| 6811 | } |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6812 | if (*out_default_method != nullptr) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6813 | VLOG(class_linker) << "Default method '" << (*out_default_method)->PrettyMethod() |
| 6814 | << "' selected " |
| 6815 | << "as the implementation for '" << target_method->PrettyMethod() |
| 6816 | << "' in '" << klass->PrettyClass() << "'"; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6817 | return DefaultMethodSearchResult::kDefaultFound; |
| 6818 | } else { |
| 6819 | return DefaultMethodSearchResult::kAbstractFound; |
| 6820 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6821 | } |
| 6822 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6823 | ArtMethod* ClassLinker::AddMethodToConflictTable(ObjPtr<mirror::Class> klass, |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6824 | ArtMethod* conflict_method, |
| 6825 | ArtMethod* interface_method, |
Nicolas Geoffray | 47213e4 | 2020-12-30 15:12:00 +0000 | [diff] [blame] | 6826 | ArtMethod* method) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 6827 | ImtConflictTable* current_table = conflict_method->GetImtConflictTable(kRuntimePointerSize); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6828 | Runtime* const runtime = Runtime::Current(); |
| 6829 | LinearAlloc* linear_alloc = GetAllocatorForClassLoader(klass->GetClassLoader()); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6830 | |
| 6831 | // 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] | 6832 | ArtMethod* new_conflict_method = (conflict_method == runtime->GetImtConflictMethod()) |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6833 | ? runtime->CreateImtConflictMethod(linear_alloc) |
| 6834 | : conflict_method; |
| 6835 | |
| 6836 | // Allocate a new table. Note that we will leak this table at the next conflict, |
| 6837 | // but that's a tradeoff compared to making the table fixed size. |
| 6838 | void* data = linear_alloc->Alloc( |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6839 | Thread::Current(), ImtConflictTable::ComputeSizeWithOneMoreEntry(current_table, |
| 6840 | image_pointer_size_)); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6841 | if (data == nullptr) { |
| 6842 | LOG(ERROR) << "Failed to allocate conflict table"; |
| 6843 | return conflict_method; |
| 6844 | } |
| 6845 | ImtConflictTable* new_table = new (data) ImtConflictTable(current_table, |
| 6846 | interface_method, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6847 | method, |
| 6848 | image_pointer_size_); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6849 | |
| 6850 | // Do a fence to ensure threads see the data in the table before it is assigned |
| 6851 | // to the conflict method. |
| 6852 | // Note that there is a race in the presence of multiple threads and we may leak |
| 6853 | // memory from the LinearAlloc, but that's a tradeoff compared to using |
| 6854 | // atomic operations. |
Orion Hodson | 27b9676 | 2018-03-13 16:06:57 +0000 | [diff] [blame] | 6855 | std::atomic_thread_fence(std::memory_order_release); |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6856 | new_conflict_method->SetImtConflictTable(new_table, image_pointer_size_); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6857 | return new_conflict_method; |
| 6858 | } |
| 6859 | |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 6860 | bool ClassLinker::AllocateIfTableMethodArrays(Thread* self, |
| 6861 | Handle<mirror::Class> klass, |
| 6862 | Handle<mirror::IfTable> iftable) { |
| 6863 | DCHECK(!klass->IsInterface()); |
| 6864 | const bool has_superclass = klass->HasSuperClass(); |
| 6865 | const bool extend_super_iftable = has_superclass; |
| 6866 | const size_t ifcount = klass->GetIfTableCount(); |
| 6867 | const size_t super_ifcount = has_superclass ? klass->GetSuperClass()->GetIfTableCount() : 0U; |
| 6868 | for (size_t i = 0; i < ifcount; ++i) { |
| 6869 | size_t num_methods = iftable->GetInterface(i)->NumDeclaredVirtualMethods(); |
| 6870 | if (num_methods > 0) { |
| 6871 | const bool is_super = i < super_ifcount; |
| 6872 | // This is an interface implemented by a super-class. Therefore we can just copy the method |
| 6873 | // array from the superclass. |
| 6874 | const bool super_interface = is_super && extend_super_iftable; |
| 6875 | ObjPtr<mirror::PointerArray> method_array; |
| 6876 | if (super_interface) { |
| 6877 | ObjPtr<mirror::IfTable> if_table = klass->GetSuperClass()->GetIfTable(); |
| 6878 | DCHECK(if_table != nullptr); |
| 6879 | DCHECK(if_table->GetMethodArray(i) != nullptr); |
| 6880 | // If we are working on a super interface, try extending the existing method array. |
Vladimir Marko | 3068d58 | 2019-05-28 16:39:29 +0100 | [diff] [blame] | 6881 | StackHandleScope<1u> hs(self); |
| 6882 | Handle<mirror::PointerArray> old_array = hs.NewHandle(if_table->GetMethodArray(i)); |
| 6883 | method_array = |
| 6884 | ObjPtr<mirror::PointerArray>::DownCast(mirror::Object::Clone(old_array, self)); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 6885 | } else { |
| 6886 | method_array = AllocPointerArray(self, num_methods); |
| 6887 | } |
| 6888 | if (UNLIKELY(method_array == nullptr)) { |
| 6889 | self->AssertPendingOOMException(); |
| 6890 | return false; |
| 6891 | } |
| 6892 | iftable->SetMethodArray(i, method_array); |
| 6893 | } |
| 6894 | } |
| 6895 | return true; |
| 6896 | } |
| 6897 | |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6898 | void ClassLinker::SetIMTRef(ArtMethod* unimplemented_method, |
| 6899 | ArtMethod* imt_conflict_method, |
| 6900 | ArtMethod* current_method, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6901 | /*out*/bool* new_conflict, |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6902 | /*out*/ArtMethod** imt_ref) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6903 | // Place method in imt if entry is empty, place conflict otherwise. |
| 6904 | if (*imt_ref == unimplemented_method) { |
| 6905 | *imt_ref = current_method; |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 6906 | } else if (!(*imt_ref)->IsRuntimeMethod()) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6907 | // If we are not a conflict and we have the same signature and name as the imt |
| 6908 | // entry, it must be that we overwrote a superclass vtable entry. |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 6909 | // Note that we have checked IsRuntimeMethod, as there may be multiple different |
| 6910 | // conflict methods. |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6911 | MethodNameAndSignatureComparator imt_comparator( |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6912 | (*imt_ref)->GetInterfaceMethodIfProxy(image_pointer_size_)); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6913 | if (imt_comparator.HasSameNameAndSignature( |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 6914 | current_method->GetInterfaceMethodIfProxy(image_pointer_size_))) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6915 | *imt_ref = current_method; |
| 6916 | } else { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 6917 | *imt_ref = imt_conflict_method; |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6918 | *new_conflict = true; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6919 | } |
Nicolas Geoffray | 796d630 | 2016-03-13 22:22:31 +0000 | [diff] [blame] | 6920 | } else { |
| 6921 | // Place the default conflict method. Note that there may be an existing conflict |
| 6922 | // method in the IMT, but it could be one tailored to the super class, with a |
| 6923 | // specific ImtConflictTable. |
| 6924 | *imt_ref = imt_conflict_method; |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6925 | *new_conflict = true; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 6926 | } |
| 6927 | } |
| 6928 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6929 | void ClassLinker::FillIMTAndConflictTables(ObjPtr<mirror::Class> klass) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 6930 | DCHECK(klass->ShouldHaveImt()) << klass->PrettyClass(); |
| 6931 | DCHECK(!klass->IsTemp()) << klass->PrettyClass(); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6932 | ArtMethod* imt_data[ImTable::kSize]; |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6933 | Runtime* const runtime = Runtime::Current(); |
| 6934 | ArtMethod* const unimplemented_method = runtime->GetImtUnimplementedMethod(); |
| 6935 | ArtMethod* const conflict_method = runtime->GetImtConflictMethod(); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6936 | std::fill_n(imt_data, arraysize(imt_data), unimplemented_method); |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6937 | if (klass->GetIfTable() != nullptr) { |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6938 | bool new_conflict = false; |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6939 | FillIMTFromIfTable(klass->GetIfTable(), |
| 6940 | unimplemented_method, |
| 6941 | conflict_method, |
| 6942 | klass, |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 6943 | /*create_conflict_tables=*/true, |
| 6944 | /*ignore_copied_methods=*/false, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6945 | &new_conflict, |
| 6946 | &imt_data[0]); |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6947 | } |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6948 | // Compare the IMT with the super class including the conflict methods. If they are equivalent, |
| 6949 | // we can just use the same pointer. |
| 6950 | ImTable* imt = nullptr; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 6951 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 6952 | if (super_class != nullptr && super_class->ShouldHaveImt()) { |
| 6953 | ImTable* super_imt = super_class->GetImt(image_pointer_size_); |
| 6954 | bool same = true; |
| 6955 | for (size_t i = 0; same && i < ImTable::kSize; ++i) { |
| 6956 | ArtMethod* method = imt_data[i]; |
| 6957 | ArtMethod* super_method = super_imt->Get(i, image_pointer_size_); |
| 6958 | if (method != super_method) { |
| 6959 | bool is_conflict_table = method->IsRuntimeMethod() && |
| 6960 | method != unimplemented_method && |
| 6961 | method != conflict_method; |
| 6962 | // Verify conflict contents. |
| 6963 | bool super_conflict_table = super_method->IsRuntimeMethod() && |
| 6964 | super_method != unimplemented_method && |
| 6965 | super_method != conflict_method; |
| 6966 | if (!is_conflict_table || !super_conflict_table) { |
| 6967 | same = false; |
| 6968 | } else { |
| 6969 | ImtConflictTable* table1 = method->GetImtConflictTable(image_pointer_size_); |
| 6970 | ImtConflictTable* table2 = super_method->GetImtConflictTable(image_pointer_size_); |
| 6971 | same = same && table1->Equals(table2, image_pointer_size_); |
| 6972 | } |
| 6973 | } |
| 6974 | } |
| 6975 | if (same) { |
| 6976 | imt = super_imt; |
| 6977 | } |
| 6978 | } |
| 6979 | if (imt == nullptr) { |
| 6980 | imt = klass->GetImt(image_pointer_size_); |
| 6981 | DCHECK(imt != nullptr); |
| 6982 | imt->Populate(imt_data, image_pointer_size_); |
| 6983 | } else { |
| 6984 | klass->SetImt(imt, image_pointer_size_); |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6985 | } |
| 6986 | } |
| 6987 | |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6988 | ImtConflictTable* ClassLinker::CreateImtConflictTable(size_t count, |
| 6989 | LinearAlloc* linear_alloc, |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 6990 | PointerSize image_pointer_size) { |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 6991 | void* data = linear_alloc->Alloc(Thread::Current(), |
| 6992 | ImtConflictTable::ComputeSize(count, |
| 6993 | image_pointer_size)); |
| 6994 | return (data != nullptr) ? new (data) ImtConflictTable(count, image_pointer_size) : nullptr; |
| 6995 | } |
| 6996 | |
| 6997 | ImtConflictTable* ClassLinker::CreateImtConflictTable(size_t count, LinearAlloc* linear_alloc) { |
| 6998 | return CreateImtConflictTable(count, linear_alloc, image_pointer_size_); |
| 6999 | } |
| 7000 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7001 | void ClassLinker::FillIMTFromIfTable(ObjPtr<mirror::IfTable> if_table, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 7002 | ArtMethod* unimplemented_method, |
| 7003 | ArtMethod* imt_conflict_method, |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7004 | ObjPtr<mirror::Class> klass, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 7005 | bool create_conflict_tables, |
| 7006 | bool ignore_copied_methods, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 7007 | /*out*/bool* new_conflict, |
| 7008 | /*out*/ArtMethod** imt) { |
| 7009 | uint32_t conflict_counts[ImTable::kSize] = {}; |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 7010 | for (size_t i = 0, length = if_table->Count(); i < length; ++i) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7011 | ObjPtr<mirror::Class> interface = if_table->GetInterface(i); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 7012 | const size_t num_virtuals = interface->NumVirtualMethods(); |
| 7013 | const size_t method_array_count = if_table->GetMethodArrayCount(i); |
| 7014 | // Virtual methods can be larger than the if table methods if there are default methods. |
| 7015 | DCHECK_GE(num_virtuals, method_array_count); |
| 7016 | if (kIsDebugBuild) { |
| 7017 | if (klass->IsInterface()) { |
| 7018 | DCHECK_EQ(method_array_count, 0u); |
| 7019 | } else { |
| 7020 | DCHECK_EQ(interface->NumDeclaredVirtualMethods(), method_array_count); |
| 7021 | } |
| 7022 | } |
| 7023 | if (method_array_count == 0) { |
| 7024 | continue; |
| 7025 | } |
Vladimir Marko | 557fece | 2019-03-26 14:29:41 +0000 | [diff] [blame] | 7026 | ObjPtr<mirror::PointerArray> method_array = if_table->GetMethodArray(i); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 7027 | for (size_t j = 0; j < method_array_count; ++j) { |
| 7028 | ArtMethod* implementation_method = |
| 7029 | method_array->GetElementPtrSize<ArtMethod*>(j, image_pointer_size_); |
| 7030 | if (ignore_copied_methods && implementation_method->IsCopied()) { |
| 7031 | continue; |
| 7032 | } |
| 7033 | DCHECK(implementation_method != nullptr); |
| 7034 | // Miranda methods cannot be used to implement an interface method, but they are safe to put |
| 7035 | // in the IMT since their entrypoint is the interface trampoline. If we put any copied methods |
| 7036 | // or interface methods in the IMT here they will not create extra conflicts since we compare |
| 7037 | // names and signatures in SetIMTRef. |
| 7038 | ArtMethod* interface_method = interface->GetVirtualMethod(j, image_pointer_size_); |
David Srbecky | e36e7f2 | 2018-11-14 14:21:23 +0000 | [diff] [blame] | 7039 | const uint32_t imt_index = interface_method->GetImtIndex(); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 7040 | |
| 7041 | // There is only any conflicts if all of the interface methods for an IMT slot don't have |
| 7042 | // the same implementation method, keep track of this to avoid creating a conflict table in |
| 7043 | // this case. |
| 7044 | |
| 7045 | // Conflict table size for each IMT slot. |
| 7046 | ++conflict_counts[imt_index]; |
| 7047 | |
| 7048 | SetIMTRef(unimplemented_method, |
| 7049 | imt_conflict_method, |
| 7050 | implementation_method, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 7051 | /*out*/new_conflict, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 7052 | /*out*/&imt[imt_index]); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 7053 | } |
| 7054 | } |
| 7055 | |
| 7056 | if (create_conflict_tables) { |
| 7057 | // Create the conflict tables. |
| 7058 | LinearAlloc* linear_alloc = GetAllocatorForClassLoader(klass->GetClassLoader()); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 7059 | for (size_t i = 0; i < ImTable::kSize; ++i) { |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 7060 | size_t conflicts = conflict_counts[i]; |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 7061 | if (imt[i] == imt_conflict_method) { |
| 7062 | ImtConflictTable* new_table = CreateImtConflictTable(conflicts, linear_alloc); |
| 7063 | if (new_table != nullptr) { |
| 7064 | ArtMethod* new_conflict_method = |
| 7065 | Runtime::Current()->CreateImtConflictMethod(linear_alloc); |
| 7066 | new_conflict_method->SetImtConflictTable(new_table, image_pointer_size_); |
| 7067 | imt[i] = new_conflict_method; |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 7068 | } else { |
| 7069 | LOG(ERROR) << "Failed to allocate conflict table"; |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 7070 | imt[i] = imt_conflict_method; |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 7071 | } |
| 7072 | } else { |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 7073 | DCHECK_NE(imt[i], imt_conflict_method); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 7074 | } |
| 7075 | } |
| 7076 | |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 7077 | for (size_t i = 0, length = if_table->Count(); i < length; ++i) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7078 | ObjPtr<mirror::Class> interface = if_table->GetInterface(i); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 7079 | const size_t method_array_count = if_table->GetMethodArrayCount(i); |
| 7080 | // Virtual methods can be larger than the if table methods if there are default methods. |
| 7081 | if (method_array_count == 0) { |
| 7082 | continue; |
| 7083 | } |
Vladimir Marko | 557fece | 2019-03-26 14:29:41 +0000 | [diff] [blame] | 7084 | ObjPtr<mirror::PointerArray> method_array = if_table->GetMethodArray(i); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 7085 | for (size_t j = 0; j < method_array_count; ++j) { |
| 7086 | ArtMethod* implementation_method = |
| 7087 | method_array->GetElementPtrSize<ArtMethod*>(j, image_pointer_size_); |
| 7088 | if (ignore_copied_methods && implementation_method->IsCopied()) { |
| 7089 | continue; |
| 7090 | } |
| 7091 | DCHECK(implementation_method != nullptr); |
| 7092 | ArtMethod* interface_method = interface->GetVirtualMethod(j, image_pointer_size_); |
David Srbecky | e36e7f2 | 2018-11-14 14:21:23 +0000 | [diff] [blame] | 7093 | const uint32_t imt_index = interface_method->GetImtIndex(); |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 7094 | if (!imt[imt_index]->IsRuntimeMethod() || |
| 7095 | imt[imt_index] == unimplemented_method || |
| 7096 | imt[imt_index] == imt_conflict_method) { |
| 7097 | continue; |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 7098 | } |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 7099 | ImtConflictTable* table = imt[imt_index]->GetImtConflictTable(image_pointer_size_); |
| 7100 | const size_t num_entries = table->NumEntries(image_pointer_size_); |
| 7101 | table->SetInterfaceMethod(num_entries, image_pointer_size_, interface_method); |
| 7102 | table->SetImplementationMethod(num_entries, image_pointer_size_, implementation_method); |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 7103 | } |
| 7104 | } |
| 7105 | } |
| 7106 | } |
| 7107 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7108 | // Simple helper function that checks that no subtypes of 'val' are contained within the 'classes' |
| 7109 | // set. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7110 | static bool NotSubinterfaceOfAny( |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 7111 | const HashSet<mirror::Class*>& classes, |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7112 | ObjPtr<mirror::Class> val) |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7113 | REQUIRES(Roles::uninterruptible_) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 7114 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7115 | DCHECK(val != nullptr); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7116 | for (ObjPtr<mirror::Class> c : classes) { |
| 7117 | if (val->IsAssignableFrom(c)) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7118 | return false; |
| 7119 | } |
| 7120 | } |
| 7121 | return true; |
| 7122 | } |
| 7123 | |
| 7124 | // Fills in and flattens the interface inheritance hierarchy. |
| 7125 | // |
| 7126 | // By the end of this function all interfaces in the transitive closure of to_process are added to |
| 7127 | // the iftable and every interface precedes all of its sub-interfaces in this list. |
| 7128 | // |
| 7129 | // all I, J: Interface | I <: J implies J precedes I |
| 7130 | // |
| 7131 | // (note A <: B means that A is a subtype of B) |
| 7132 | // |
| 7133 | // This returns the total number of items in the iftable. The iftable might be resized down after |
| 7134 | // this call. |
| 7135 | // |
| 7136 | // We order this backwards so that we do not need to reorder superclass interfaces when new |
| 7137 | // interfaces are added in subclass's interface tables. |
| 7138 | // |
| 7139 | // Upon entry into this function iftable is a copy of the superclass's iftable with the first |
| 7140 | // super_ifcount entries filled in with the transitive closure of the interfaces of the superclass. |
| 7141 | // The other entries are uninitialized. We will fill in the remaining entries in this function. The |
| 7142 | // iftable must be large enough to hold all interfaces without changing its size. |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 7143 | static size_t FillIfTable(Thread* self, |
| 7144 | ObjPtr<mirror::Class> klass, |
| 7145 | ObjPtr<mirror::ObjectArray<mirror::Class>> interfaces, |
| 7146 | ObjPtr<mirror::IfTable> iftable, |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7147 | size_t super_ifcount, |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 7148 | size_t num_interfaces) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 7149 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 7150 | ScopedAssertNoThreadSuspension nts(__FUNCTION__); |
| 7151 | // This is the set of all classes already in the iftable. Used to make checking |
| 7152 | // if a class has already been added quicker. |
| 7153 | constexpr size_t kBufferSize = 32; // 256 bytes on 64-bit architectures. |
| 7154 | mirror::Class* buffer[kBufferSize]; |
| 7155 | HashSet<mirror::Class*> classes_in_iftable(buffer, kBufferSize); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7156 | // The first super_ifcount elements are from the superclass. We note that they are already added. |
| 7157 | for (size_t i = 0; i < super_ifcount; i++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7158 | ObjPtr<mirror::Class> iface = iftable->GetInterface(i); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7159 | DCHECK(NotSubinterfaceOfAny(classes_in_iftable, iface)) << "Bad ordering."; |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 7160 | classes_in_iftable.insert(iface.Ptr()); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7161 | } |
| 7162 | size_t filled_ifcount = super_ifcount; |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 7163 | const bool have_interfaces = interfaces != nullptr; |
| 7164 | for (size_t i = 0; i != num_interfaces; ++i) { |
| 7165 | ObjPtr<mirror::Class> interface = have_interfaces |
| 7166 | ? interfaces->Get(i) |
| 7167 | : mirror::Class::GetDirectInterface(self, klass, i); |
| 7168 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7169 | // Let us call the first filled_ifcount elements of iftable the current-iface-list. |
| 7170 | // At this point in the loop current-iface-list has the invariant that: |
| 7171 | // for every pair of interfaces I,J within it: |
| 7172 | // if index_of(I) < index_of(J) then I is not a subtype of J |
| 7173 | |
| 7174 | // If we have already seen this element then all of its super-interfaces must already be in the |
| 7175 | // current-iface-list so we can skip adding it. |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 7176 | if (classes_in_iftable.find(interface.Ptr()) == classes_in_iftable.end()) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7177 | // We haven't seen this interface so add all of its super-interfaces onto the |
| 7178 | // current-iface-list, skipping those already on it. |
| 7179 | int32_t ifcount = interface->GetIfTableCount(); |
| 7180 | for (int32_t j = 0; j < ifcount; j++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7181 | ObjPtr<mirror::Class> super_interface = interface->GetIfTable()->GetInterface(j); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7182 | if (!ContainsElement(classes_in_iftable, super_interface)) { |
| 7183 | DCHECK(NotSubinterfaceOfAny(classes_in_iftable, super_interface)) << "Bad ordering."; |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 7184 | classes_in_iftable.insert(super_interface.Ptr()); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7185 | iftable->SetInterface(filled_ifcount, super_interface); |
| 7186 | filled_ifcount++; |
| 7187 | } |
| 7188 | } |
| 7189 | DCHECK(NotSubinterfaceOfAny(classes_in_iftable, interface)) << "Bad ordering"; |
| 7190 | // Place this interface onto the current-iface-list after all of its super-interfaces. |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 7191 | classes_in_iftable.insert(interface.Ptr()); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7192 | iftable->SetInterface(filled_ifcount, interface); |
| 7193 | filled_ifcount++; |
| 7194 | } else if (kIsDebugBuild) { |
| 7195 | // Check all super-interfaces are already in the list. |
| 7196 | int32_t ifcount = interface->GetIfTableCount(); |
| 7197 | for (int32_t j = 0; j < ifcount; j++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7198 | ObjPtr<mirror::Class> super_interface = interface->GetIfTable()->GetInterface(j); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7199 | DCHECK(ContainsElement(classes_in_iftable, super_interface)) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 7200 | << "Iftable does not contain " << mirror::Class::PrettyClass(super_interface) |
| 7201 | << ", a superinterface of " << interface->PrettyClass(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7202 | } |
| 7203 | } |
| 7204 | } |
| 7205 | if (kIsDebugBuild) { |
| 7206 | // Check that the iftable is ordered correctly. |
| 7207 | for (size_t i = 0; i < filled_ifcount; i++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7208 | ObjPtr<mirror::Class> if_a = iftable->GetInterface(i); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7209 | for (size_t j = i + 1; j < filled_ifcount; j++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7210 | ObjPtr<mirror::Class> if_b = iftable->GetInterface(j); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7211 | // !(if_a <: if_b) |
| 7212 | CHECK(!if_b->IsAssignableFrom(if_a)) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 7213 | << "Bad interface order: " << mirror::Class::PrettyClass(if_a) << " (index " << i |
| 7214 | << ") extends " |
| 7215 | << 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] | 7216 | << "interface list."; |
| 7217 | } |
| 7218 | } |
| 7219 | } |
| 7220 | return filled_ifcount; |
| 7221 | } |
| 7222 | |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 7223 | bool ClassLinker::SetupInterfaceLookupTable(Thread* self, |
| 7224 | Handle<mirror::Class> klass, |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7225 | Handle<mirror::ObjectArray<mirror::Class>> interfaces) { |
| 7226 | StackHandleScope<1> hs(self); |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 7227 | const bool has_superclass = klass->HasSuperClass(); |
| 7228 | const size_t super_ifcount = has_superclass ? klass->GetSuperClass()->GetIfTableCount() : 0U; |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 7229 | const bool have_interfaces = interfaces != nullptr; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7230 | const size_t num_interfaces = |
| 7231 | have_interfaces ? interfaces->GetLength() : klass->NumDirectInterfaces(); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 7232 | if (num_interfaces == 0) { |
| 7233 | if (super_ifcount == 0) { |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 7234 | if (LIKELY(has_superclass)) { |
| 7235 | klass->SetIfTable(klass->GetSuperClass()->GetIfTable()); |
| 7236 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 7237 | // Class implements no interfaces. |
| 7238 | DCHECK_EQ(klass->GetIfTableCount(), 0); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 7239 | return true; |
| 7240 | } |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 7241 | // Class implements same interfaces as parent, are any of these not marker interfaces? |
| 7242 | bool has_non_marker_interface = false; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7243 | ObjPtr<mirror::IfTable> super_iftable = klass->GetSuperClass()->GetIfTable(); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 7244 | for (size_t i = 0; i < super_ifcount; ++i) { |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 7245 | if (super_iftable->GetMethodArrayCount(i) > 0) { |
| 7246 | has_non_marker_interface = true; |
| 7247 | break; |
| 7248 | } |
| 7249 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 7250 | // Class just inherits marker interfaces from parent so recycle parent's iftable. |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 7251 | if (!has_non_marker_interface) { |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 7252 | klass->SetIfTable(super_iftable); |
| 7253 | return true; |
| 7254 | } |
| 7255 | } |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 7256 | size_t ifcount = super_ifcount + num_interfaces; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7257 | // Check that every class being implemented is an interface. |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 7258 | for (size_t i = 0; i < num_interfaces; i++) { |
Mathieu Chartier | 28bd2e4 | 2016-10-04 13:54:57 -0700 | [diff] [blame] | 7259 | ObjPtr<mirror::Class> interface = have_interfaces |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 7260 | ? interfaces->GetWithoutChecks(i) |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 7261 | : mirror::Class::GetDirectInterface(self, klass.Get(), i); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 7262 | DCHECK(interface != nullptr); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 7263 | if (UNLIKELY(!interface->IsInterface())) { |
| 7264 | std::string temp; |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 7265 | ThrowIncompatibleClassChangeError(klass.Get(), |
| 7266 | "Class %s implements non-interface class %s", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 7267 | klass->PrettyDescriptor().c_str(), |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 7268 | PrettyDescriptor(interface->GetDescriptor(&temp)).c_str()); |
| 7269 | return false; |
| 7270 | } |
| 7271 | ifcount += interface->GetIfTableCount(); |
| 7272 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7273 | // Create the interface function table. |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 7274 | MutableHandle<mirror::IfTable> iftable(hs.NewHandle(AllocIfTable(self, ifcount))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 7275 | if (UNLIKELY(iftable == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7276 | self->AssertPendingOOMException(); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 7277 | return false; |
| 7278 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7279 | // Fill in table with superclass's iftable. |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7280 | if (super_ifcount != 0) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7281 | ObjPtr<mirror::IfTable> super_iftable = klass->GetSuperClass()->GetIfTable(); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 7282 | for (size_t i = 0; i < super_ifcount; i++) { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7283 | ObjPtr<mirror::Class> super_interface = super_iftable->GetInterface(i); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 7284 | iftable->SetInterface(i, super_interface); |
Brian Carlstrom | 4b620ff | 2011-09-11 01:11:01 -0700 | [diff] [blame] | 7285 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 7286 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7287 | |
| 7288 | // Note that AllowThreadSuspension is to thread suspension as pthread_testcancel is to pthread |
| 7289 | // cancellation. That is it will suspend if one has a pending suspend request but otherwise |
| 7290 | // doesn't really do anything. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 7291 | self->AllowThreadSuspension(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7292 | |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 7293 | const size_t new_ifcount = FillIfTable( |
| 7294 | self, klass.Get(), interfaces.Get(), iftable.Get(), super_ifcount, num_interfaces); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7295 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 7296 | self->AllowThreadSuspension(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7297 | |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 7298 | // Shrink iftable in case duplicates were found |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7299 | if (new_ifcount < ifcount) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 7300 | DCHECK_NE(num_interfaces, 0U); |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 7301 | iftable.Assign(ObjPtr<mirror::IfTable>::DownCast( |
Vladimir Marko | 3068d58 | 2019-05-28 16:39:29 +0100 | [diff] [blame] | 7302 | mirror::IfTable::CopyOf(iftable, self, new_ifcount * mirror::IfTable::kMax))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 7303 | if (UNLIKELY(iftable == nullptr)) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 7304 | self->AssertPendingOOMException(); |
Ian Rogers | a436fde | 2013-08-27 23:34:06 -0700 | [diff] [blame] | 7305 | return false; |
| 7306 | } |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7307 | ifcount = new_ifcount; |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 7308 | } else { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7309 | DCHECK_EQ(new_ifcount, ifcount); |
Ian Rogers | b52b01a | 2012-01-12 17:01:38 -0800 | [diff] [blame] | 7310 | } |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 7311 | klass->SetIfTable(iftable.Get()); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 7312 | return true; |
| 7313 | } |
| 7314 | |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 7315 | // Finds the method with a name/signature that matches cmp in the given lists of methods. The list |
| 7316 | // of methods must be unique. |
| 7317 | static ArtMethod* FindSameNameAndSignature(MethodNameAndSignatureComparator& cmp ATTRIBUTE_UNUSED) { |
| 7318 | return nullptr; |
| 7319 | } |
| 7320 | |
| 7321 | template <typename ... Types> |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7322 | static ArtMethod* FindSameNameAndSignature(MethodNameAndSignatureComparator& cmp, |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 7323 | const ScopedArenaVector<ArtMethod*>& list, |
| 7324 | const Types& ... rest) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 7325 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7326 | for (ArtMethod* method : list) { |
| 7327 | if (cmp.HasSameNameAndSignature(method)) { |
| 7328 | return method; |
| 7329 | } |
| 7330 | } |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 7331 | return FindSameNameAndSignature(cmp, rest...); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 7332 | } |
| 7333 | |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 7334 | namespace { |
| 7335 | |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 7336 | // Check that all vtable entries are present in this class's virtuals or are the same as a |
| 7337 | // superclasses vtable entry. |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 7338 | void CheckClassOwnsVTableEntries(Thread* self, |
| 7339 | Handle<mirror::Class> klass, |
| 7340 | PointerSize pointer_size) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 7341 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 7342 | StackHandleScope<2> hs(self); |
| 7343 | Handle<mirror::PointerArray> check_vtable(hs.NewHandle(klass->GetVTableDuringLinking())); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7344 | ObjPtr<mirror::Class> super_temp = (klass->HasSuperClass()) ? klass->GetSuperClass() : nullptr; |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 7345 | Handle<mirror::Class> superclass(hs.NewHandle(super_temp)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 7346 | int32_t super_vtable_length = (superclass != nullptr) ? superclass->GetVTableLength() : 0; |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 7347 | for (int32_t i = 0; i < check_vtable->GetLength(); ++i) { |
| 7348 | ArtMethod* m = check_vtable->GetElementPtrSize<ArtMethod*>(i, pointer_size); |
| 7349 | CHECK(m != nullptr); |
| 7350 | |
Alex Light | a41a3078 | 2017-03-29 11:33:19 -0700 | [diff] [blame] | 7351 | if (m->GetMethodIndexDuringLinking() != i) { |
| 7352 | LOG(WARNING) << m->PrettyMethod() |
| 7353 | << " has an unexpected method index for its spot in the vtable for class" |
| 7354 | << klass->PrettyClass(); |
| 7355 | } |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 7356 | ArraySlice<ArtMethod> virtuals = klass->GetVirtualMethodsSliceUnchecked(pointer_size); |
| 7357 | auto is_same_method = [m] (const ArtMethod& meth) { |
| 7358 | return &meth == m; |
| 7359 | }; |
Alex Light | 3f98053 | 2017-03-17 15:10:32 -0700 | [diff] [blame] | 7360 | if (!((super_vtable_length > i && superclass->GetVTableEntry(i, pointer_size) == m) || |
| 7361 | std::find_if(virtuals.begin(), virtuals.end(), is_same_method) != virtuals.end())) { |
| 7362 | LOG(WARNING) << m->PrettyMethod() << " does not seem to be owned by current class " |
| 7363 | << klass->PrettyClass() << " or any of its superclasses!"; |
| 7364 | } |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 7365 | } |
| 7366 | } |
| 7367 | |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 7368 | // Check to make sure the vtable does not have duplicates. Duplicates could cause problems when a |
| 7369 | // method is overridden in a subclass. |
Andreas Gampe | a2fed08 | 2019-02-01 09:34:43 -0800 | [diff] [blame] | 7370 | template <PointerSize kPointerSize> |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 7371 | void CheckVTableHasNoDuplicates(Thread* self, Handle<mirror::Class> klass) |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 7372 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 7373 | StackHandleScope<1> hs(self); |
| 7374 | Handle<mirror::PointerArray> vtable(hs.NewHandle(klass->GetVTableDuringLinking())); |
| 7375 | int32_t num_entries = vtable->GetLength(); |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 7376 | |
| 7377 | // Observations: |
| 7378 | // * The older implementation was O(n^2) and got too expensive for apps with larger classes. |
| 7379 | // * Many classes do not override Object functions (e.g., equals/hashCode/toString). Thus, |
| 7380 | // for many classes outside of libcore a cross-dexfile check has to be run anyways. |
| 7381 | // * In the cross-dexfile case, with the O(n^2), in the best case O(n) cross checks would have |
| 7382 | // to be done. It is thus OK in a single-pass algorithm to read all data, anyways. |
| 7383 | // * The single-pass algorithm will trade memory for speed, but that is OK. |
| 7384 | |
| 7385 | CHECK_GT(num_entries, 0); |
| 7386 | |
| 7387 | auto log_fn = [&vtable, &klass](int32_t i, int32_t j) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 7388 | ArtMethod* m1 = vtable->GetElementPtrSize<ArtMethod*, kPointerSize>(i); |
| 7389 | ArtMethod* m2 = vtable->GetElementPtrSize<ArtMethod*, kPointerSize>(j); |
| 7390 | LOG(WARNING) << "vtable entries " << i << " and " << j << " are identical for " |
| 7391 | << klass->PrettyClass() << " in method " << m1->PrettyMethod() |
| 7392 | << " (0x" << std::hex << reinterpret_cast<uintptr_t>(m2) << ") and " |
| 7393 | << m2->PrettyMethod() << " (0x" << std::hex |
| 7394 | << reinterpret_cast<uintptr_t>(m2) << ")"; |
| 7395 | }; |
| 7396 | struct BaseHashType { |
| 7397 | static size_t HashCombine(size_t seed, size_t val) { |
| 7398 | return seed ^ (val + 0x9e3779b9 + (seed << 6) + (seed >> 2)); |
| 7399 | } |
| 7400 | }; |
| 7401 | |
| 7402 | // Check assuming all entries come from the same dex file. |
| 7403 | { |
| 7404 | // Find the first interesting method and its dex file. |
| 7405 | int32_t start = 0; |
| 7406 | for (; start < num_entries; ++start) { |
| 7407 | ArtMethod* vtable_entry = vtable->GetElementPtrSize<ArtMethod*, kPointerSize>(start); |
| 7408 | // Don't bother if we cannot 'see' the vtable entry (i.e. it is a package-private member |
| 7409 | // maybe). |
| 7410 | if (!klass->CanAccessMember(vtable_entry->GetDeclaringClass(), |
| 7411 | vtable_entry->GetAccessFlags())) { |
| 7412 | continue; |
| 7413 | } |
| 7414 | break; |
| 7415 | } |
| 7416 | if (start == num_entries) { |
| 7417 | return; |
| 7418 | } |
| 7419 | const DexFile* dex_file = |
| 7420 | vtable->GetElementPtrSize<ArtMethod*, kPointerSize>(start)-> |
| 7421 | GetInterfaceMethodIfProxy(kPointerSize)->GetDexFile(); |
| 7422 | |
| 7423 | // Helper function to avoid logging if we have to run the cross-file checks. |
| 7424 | auto check_fn = [&](bool log_warn) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 7425 | // Use a map to store seen entries, as the storage space is too large for a bitvector. |
| 7426 | using PairType = std::pair<uint32_t, uint16_t>; |
| 7427 | struct PairHash : BaseHashType { |
| 7428 | size_t operator()(const PairType& key) const { |
| 7429 | return BaseHashType::HashCombine(BaseHashType::HashCombine(0, key.first), key.second); |
| 7430 | } |
| 7431 | }; |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 7432 | HashMap<PairType, int32_t, DefaultMapEmptyFn<PairType, int32_t>, PairHash> seen; |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 7433 | seen.reserve(2 * num_entries); |
| 7434 | bool need_slow_path = false; |
| 7435 | bool found_dup = false; |
| 7436 | for (int i = start; i < num_entries; ++i) { |
| 7437 | // Can use Unchecked here as the start loop already ensured that the arrays are correct |
| 7438 | // wrt/ kPointerSize. |
| 7439 | ArtMethod* vtable_entry = vtable->GetElementPtrSizeUnchecked<ArtMethod*, kPointerSize>(i); |
| 7440 | if (!klass->CanAccessMember(vtable_entry->GetDeclaringClass(), |
| 7441 | vtable_entry->GetAccessFlags())) { |
| 7442 | continue; |
| 7443 | } |
| 7444 | ArtMethod* m = vtable_entry->GetInterfaceMethodIfProxy(kPointerSize); |
| 7445 | if (dex_file != m->GetDexFile()) { |
| 7446 | need_slow_path = true; |
| 7447 | break; |
| 7448 | } |
| 7449 | const dex::MethodId* m_mid = &dex_file->GetMethodId(m->GetDexMethodIndex()); |
| 7450 | PairType pair = std::make_pair(m_mid->name_idx_.index_, m_mid->proto_idx_.index_); |
| 7451 | auto it = seen.find(pair); |
| 7452 | if (it != seen.end()) { |
| 7453 | found_dup = true; |
| 7454 | if (log_warn) { |
| 7455 | log_fn(it->second, i); |
| 7456 | } |
| 7457 | } else { |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 7458 | seen.insert(std::make_pair(pair, i)); |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 7459 | } |
| 7460 | } |
| 7461 | return std::make_pair(need_slow_path, found_dup); |
| 7462 | }; |
| 7463 | std::pair<bool, bool> result = check_fn(/* log_warn= */ false); |
| 7464 | if (!result.first) { |
| 7465 | if (result.second) { |
| 7466 | check_fn(/* log_warn= */ true); |
| 7467 | } |
| 7468 | return; |
| 7469 | } |
| 7470 | } |
| 7471 | |
| 7472 | // Need to check across dex files. |
| 7473 | struct Entry { |
| 7474 | size_t cached_hash = 0; |
Vladimir Marko | aa027b8 | 2021-01-06 20:34:20 +0000 | [diff] [blame] | 7475 | uint32_t name_len = 0; |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 7476 | const char* name = nullptr; |
| 7477 | Signature signature = Signature::NoSignature(); |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 7478 | |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 7479 | Entry() = default; |
| 7480 | Entry(const Entry& other) = default; |
| 7481 | Entry& operator=(const Entry& other) = default; |
| 7482 | |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 7483 | Entry(const DexFile* dex_file, const dex::MethodId& mid) |
Vladimir Marko | aa027b8 | 2021-01-06 20:34:20 +0000 | [diff] [blame] | 7484 | : name_len(0), // Explicit to enforce ordering with -Werror,-Wreorder-ctor. |
| 7485 | // This call writes `name_len` and it is therefore necessary that the |
| 7486 | // initializer for `name_len` comes before it, otherwise the value |
| 7487 | // from the call would be overwritten by that initializer. |
| 7488 | name(dex_file->StringDataAndUtf16LengthByIdx(mid.name_idx_, &name_len)), |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 7489 | signature(dex_file->GetMethodSignature(mid)) { |
Vladimir Marko | aa027b8 | 2021-01-06 20:34:20 +0000 | [diff] [blame] | 7490 | // The `name_len` has been initialized to the UTF16 length. Calculate length in bytes. |
| 7491 | if (name[name_len] != 0) { |
| 7492 | name_len += strlen(name + name_len); |
| 7493 | } |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 7494 | } |
| 7495 | |
| 7496 | bool operator==(const Entry& other) const { |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 7497 | return name_len == other.name_len && |
| 7498 | memcmp(name, other.name, name_len) == 0 && |
| 7499 | signature == other.signature; |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 7500 | } |
| 7501 | }; |
| 7502 | struct EntryHash { |
| 7503 | size_t operator()(const Entry& key) const { |
| 7504 | return key.cached_hash; |
| 7505 | } |
| 7506 | }; |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 7507 | HashMap<Entry, int32_t, DefaultMapEmptyFn<Entry, int32_t>, EntryHash> map; |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 7508 | for (int32_t i = 0; i < num_entries; ++i) { |
| 7509 | // Can use Unchecked here as the first loop already ensured that the arrays are correct |
| 7510 | // wrt/ kPointerSize. |
| 7511 | ArtMethod* vtable_entry = vtable->GetElementPtrSizeUnchecked<ArtMethod*, kPointerSize>(i); |
| 7512 | // Don't bother if we cannot 'see' the vtable entry (i.e. it is a package-private member |
| 7513 | // maybe). |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 7514 | if (!klass->CanAccessMember(vtable_entry->GetDeclaringClass(), |
| 7515 | vtable_entry->GetAccessFlags())) { |
| 7516 | continue; |
| 7517 | } |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 7518 | ArtMethod* m = vtable_entry->GetInterfaceMethodIfProxy(kPointerSize); |
| 7519 | const DexFile* dex_file = m->GetDexFile(); |
| 7520 | const dex::MethodId& mid = dex_file->GetMethodId(m->GetDexMethodIndex()); |
| 7521 | |
| 7522 | Entry e(dex_file, mid); |
| 7523 | |
| 7524 | size_t string_hash = std::hash<std::string_view>()(std::string_view(e.name, e.name_len)); |
| 7525 | size_t sig_hash = std::hash<std::string>()(e.signature.ToString()); |
| 7526 | e.cached_hash = BaseHashType::HashCombine(BaseHashType::HashCombine(0u, string_hash), |
| 7527 | sig_hash); |
| 7528 | |
| 7529 | auto it = map.find(e); |
| 7530 | if (it != map.end()) { |
| 7531 | log_fn(it->second, i); |
| 7532 | } else { |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 7533 | map.insert(std::make_pair(e, i)); |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 7534 | } |
| 7535 | } |
| 7536 | } |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 7537 | |
| 7538 | void CheckVTableHasNoDuplicates(Thread* self, |
| 7539 | Handle<mirror::Class> klass, |
| 7540 | PointerSize pointer_size) |
Andreas Gampe | a2fed08 | 2019-02-01 09:34:43 -0800 | [diff] [blame] | 7541 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 7542 | switch (pointer_size) { |
| 7543 | case PointerSize::k64: |
| 7544 | CheckVTableHasNoDuplicates<PointerSize::k64>(self, klass); |
| 7545 | break; |
| 7546 | case PointerSize::k32: |
| 7547 | CheckVTableHasNoDuplicates<PointerSize::k32>(self, klass); |
| 7548 | break; |
| 7549 | } |
| 7550 | } |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 7551 | |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 7552 | static void CheckVTable(Thread* self, Handle<mirror::Class> klass, PointerSize pointer_size) |
Alex Light | 1f3925d | 2016-09-07 12:04:20 -0700 | [diff] [blame] | 7553 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 7554 | CheckClassOwnsVTableEntries(self, klass, pointer_size); |
| 7555 | CheckVTableHasNoDuplicates(self, klass, pointer_size); |
| 7556 | } |
| 7557 | |
Andreas Gampe | 9f3928f | 2019-02-04 11:19:31 -0800 | [diff] [blame] | 7558 | } // namespace |
| 7559 | |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 7560 | void ClassLinker::FillImtFromSuperClass(Handle<mirror::Class> klass, |
| 7561 | ArtMethod* unimplemented_method, |
| 7562 | ArtMethod* imt_conflict_method, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 7563 | bool* new_conflict, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 7564 | ArtMethod** imt) { |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7565 | DCHECK(klass->HasSuperClass()); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7566 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 7567 | if (super_class->ShouldHaveImt()) { |
| 7568 | ImTable* super_imt = super_class->GetImt(image_pointer_size_); |
| 7569 | for (size_t i = 0; i < ImTable::kSize; ++i) { |
| 7570 | imt[i] = super_imt->Get(i, image_pointer_size_); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7571 | } |
| 7572 | } else { |
| 7573 | // No imt in the super class, need to reconstruct from the iftable. |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 7574 | ObjPtr<mirror::IfTable> if_table = super_class->GetIfTable(); |
Mathieu Chartier | 6beced4 | 2016-11-15 15:51:31 -0800 | [diff] [blame] | 7575 | if (if_table->Count() != 0) { |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 7576 | // Ignore copied methods since we will handle these in LinkInterfaceMethods. |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 7577 | FillIMTFromIfTable(if_table, |
| 7578 | unimplemented_method, |
| 7579 | imt_conflict_method, |
| 7580 | klass.Get(), |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 7581 | /*create_conflict_tables=*/false, |
| 7582 | /*ignore_copied_methods=*/true, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 7583 | /*out*/new_conflict, |
Mathieu Chartier | cdca476 | 2016-04-28 09:44:54 -0700 | [diff] [blame] | 7584 | /*out*/imt); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 7585 | } |
| 7586 | } |
| 7587 | } |
| 7588 | |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7589 | class ClassLinker::LinkInterfaceMethodsHelper { |
| 7590 | public: |
| 7591 | LinkInterfaceMethodsHelper(ClassLinker* class_linker, |
| 7592 | Handle<mirror::Class> klass, |
| 7593 | Thread* self, |
| 7594 | Runtime* runtime) |
| 7595 | : class_linker_(class_linker), |
| 7596 | klass_(klass), |
| 7597 | method_alignment_(ArtMethod::Alignment(class_linker->GetImagePointerSize())), |
| 7598 | method_size_(ArtMethod::Size(class_linker->GetImagePointerSize())), |
| 7599 | self_(self), |
| 7600 | stack_(runtime->GetLinearAlloc()->GetArenaPool()), |
| 7601 | allocator_(&stack_), |
| 7602 | default_conflict_methods_(allocator_.Adapter()), |
| 7603 | overriding_default_conflict_methods_(allocator_.Adapter()), |
| 7604 | miranda_methods_(allocator_.Adapter()), |
| 7605 | default_methods_(allocator_.Adapter()), |
| 7606 | overriding_default_methods_(allocator_.Adapter()), |
| 7607 | move_table_(allocator_.Adapter()) { |
| 7608 | } |
| 7609 | |
| 7610 | ArtMethod* FindMethod(ArtMethod* interface_method, |
| 7611 | MethodNameAndSignatureComparator& interface_name_comparator, |
| 7612 | ArtMethod* vtable_impl) |
| 7613 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 7614 | |
| 7615 | ArtMethod* GetOrCreateMirandaMethod(ArtMethod* interface_method, |
| 7616 | MethodNameAndSignatureComparator& interface_name_comparator) |
| 7617 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 7618 | |
| 7619 | bool HasNewVirtuals() const { |
| 7620 | return !(miranda_methods_.empty() && |
| 7621 | default_methods_.empty() && |
| 7622 | overriding_default_methods_.empty() && |
| 7623 | overriding_default_conflict_methods_.empty() && |
| 7624 | default_conflict_methods_.empty()); |
| 7625 | } |
| 7626 | |
| 7627 | void ReallocMethods() REQUIRES_SHARED(Locks::mutator_lock_); |
| 7628 | |
| 7629 | ObjPtr<mirror::PointerArray> UpdateVtable( |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 7630 | const HashMap<size_t, ClassLinker::MethodTranslation>& default_translations, |
Vladimir Marko | 3068d58 | 2019-05-28 16:39:29 +0100 | [diff] [blame] | 7631 | Handle<mirror::PointerArray> old_vtable) REQUIRES_SHARED(Locks::mutator_lock_); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7632 | |
| 7633 | void UpdateIfTable(Handle<mirror::IfTable> iftable) REQUIRES_SHARED(Locks::mutator_lock_); |
| 7634 | |
| 7635 | void UpdateIMT(ArtMethod** out_imt); |
| 7636 | |
| 7637 | void CheckNoStaleMethodsInDexCache() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 7638 | if (kIsDebugBuild) { |
| 7639 | PointerSize pointer_size = class_linker_->GetImagePointerSize(); |
| 7640 | // Check that there are no stale methods are in the dex cache array. |
| 7641 | auto* resolved_methods = klass_->GetDexCache()->GetResolvedMethods(); |
| 7642 | for (size_t i = 0, count = klass_->GetDexCache()->NumResolvedMethods(); i < count; ++i) { |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 7643 | auto pair = mirror::DexCache::GetNativePairPtrSize(resolved_methods, i, pointer_size); |
| 7644 | ArtMethod* m = pair.object; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7645 | CHECK(move_table_.find(m) == move_table_.end() || |
| 7646 | // The original versions of copied methods will still be present so allow those too. |
| 7647 | // Note that if the first check passes this might fail to GetDeclaringClass(). |
| 7648 | std::find_if(m->GetDeclaringClass()->GetMethods(pointer_size).begin(), |
| 7649 | m->GetDeclaringClass()->GetMethods(pointer_size).end(), |
| 7650 | [m] (ArtMethod& meth) { |
| 7651 | return &meth == m; |
| 7652 | }) != m->GetDeclaringClass()->GetMethods(pointer_size).end()) |
| 7653 | << "Obsolete method " << m->PrettyMethod() << " is in dex cache!"; |
| 7654 | } |
| 7655 | } |
| 7656 | } |
| 7657 | |
| 7658 | void ClobberOldMethods(LengthPrefixedArray<ArtMethod>* old_methods, |
| 7659 | LengthPrefixedArray<ArtMethod>* methods) { |
| 7660 | if (kIsDebugBuild) { |
| 7661 | CHECK(methods != nullptr); |
| 7662 | // Put some random garbage in old methods to help find stale pointers. |
| 7663 | if (methods != old_methods && old_methods != nullptr) { |
| 7664 | // Need to make sure the GC is not running since it could be scanning the methods we are |
| 7665 | // about to overwrite. |
| 7666 | ScopedThreadStateChange tsc(self_, kSuspended); |
| 7667 | gc::ScopedGCCriticalSection gcs(self_, |
| 7668 | gc::kGcCauseClassLinker, |
| 7669 | gc::kCollectorTypeClassLinker); |
| 7670 | const size_t old_size = LengthPrefixedArray<ArtMethod>::ComputeSize(old_methods->size(), |
| 7671 | method_size_, |
| 7672 | method_alignment_); |
| 7673 | memset(old_methods, 0xFEu, old_size); |
| 7674 | } |
| 7675 | } |
| 7676 | } |
| 7677 | |
| 7678 | private: |
| 7679 | size_t NumberOfNewVirtuals() const { |
| 7680 | return miranda_methods_.size() + |
| 7681 | default_methods_.size() + |
| 7682 | overriding_default_conflict_methods_.size() + |
| 7683 | overriding_default_methods_.size() + |
| 7684 | default_conflict_methods_.size(); |
| 7685 | } |
| 7686 | |
| 7687 | bool FillTables() REQUIRES_SHARED(Locks::mutator_lock_) { |
| 7688 | return !klass_->IsInterface(); |
| 7689 | } |
| 7690 | |
| 7691 | void LogNewVirtuals() const REQUIRES_SHARED(Locks::mutator_lock_) { |
| 7692 | DCHECK(!klass_->IsInterface() || (default_methods_.empty() && miranda_methods_.empty())) |
| 7693 | << "Interfaces should only have default-conflict methods appended to them."; |
| 7694 | VLOG(class_linker) << mirror::Class::PrettyClass(klass_.Get()) << ": miranda_methods=" |
| 7695 | << miranda_methods_.size() |
| 7696 | << " default_methods=" << default_methods_.size() |
| 7697 | << " overriding_default_methods=" << overriding_default_methods_.size() |
| 7698 | << " default_conflict_methods=" << default_conflict_methods_.size() |
| 7699 | << " overriding_default_conflict_methods=" |
| 7700 | << overriding_default_conflict_methods_.size(); |
| 7701 | } |
| 7702 | |
| 7703 | ClassLinker* class_linker_; |
| 7704 | Handle<mirror::Class> klass_; |
| 7705 | size_t method_alignment_; |
| 7706 | size_t method_size_; |
| 7707 | Thread* const self_; |
| 7708 | |
| 7709 | // These are allocated on the heap to begin, we then transfer to linear alloc when we re-create |
| 7710 | // the virtual methods array. |
| 7711 | // Need to use low 4GB arenas for compiler or else the pointers wont fit in 32 bit method array |
| 7712 | // during cross compilation. |
| 7713 | // Use the linear alloc pool since this one is in the low 4gb for the compiler. |
| 7714 | ArenaStack stack_; |
| 7715 | ScopedArenaAllocator allocator_; |
| 7716 | |
| 7717 | ScopedArenaVector<ArtMethod*> default_conflict_methods_; |
| 7718 | ScopedArenaVector<ArtMethod*> overriding_default_conflict_methods_; |
| 7719 | ScopedArenaVector<ArtMethod*> miranda_methods_; |
| 7720 | ScopedArenaVector<ArtMethod*> default_methods_; |
| 7721 | ScopedArenaVector<ArtMethod*> overriding_default_methods_; |
| 7722 | |
| 7723 | ScopedArenaUnorderedMap<ArtMethod*, ArtMethod*> move_table_; |
| 7724 | }; |
| 7725 | |
| 7726 | ArtMethod* ClassLinker::LinkInterfaceMethodsHelper::FindMethod( |
| 7727 | ArtMethod* interface_method, |
| 7728 | MethodNameAndSignatureComparator& interface_name_comparator, |
| 7729 | ArtMethod* vtable_impl) { |
| 7730 | ArtMethod* current_method = nullptr; |
| 7731 | switch (class_linker_->FindDefaultMethodImplementation(self_, |
| 7732 | interface_method, |
| 7733 | klass_, |
| 7734 | /*out*/¤t_method)) { |
| 7735 | case DefaultMethodSearchResult::kDefaultConflict: { |
| 7736 | // Default method conflict. |
| 7737 | DCHECK(current_method == nullptr); |
| 7738 | ArtMethod* default_conflict_method = nullptr; |
| 7739 | if (vtable_impl != nullptr && vtable_impl->IsDefaultConflicting()) { |
| 7740 | // We can reuse the method from the superclass, don't bother adding it to virtuals. |
| 7741 | default_conflict_method = vtable_impl; |
| 7742 | } else { |
| 7743 | // See if we already have a conflict method for this method. |
| 7744 | ArtMethod* preexisting_conflict = FindSameNameAndSignature( |
| 7745 | interface_name_comparator, |
| 7746 | default_conflict_methods_, |
| 7747 | overriding_default_conflict_methods_); |
| 7748 | if (LIKELY(preexisting_conflict != nullptr)) { |
| 7749 | // We already have another conflict we can reuse. |
| 7750 | default_conflict_method = preexisting_conflict; |
| 7751 | } else { |
| 7752 | // Note that we do this even if we are an interface since we need to create this and |
| 7753 | // cannot reuse another classes. |
| 7754 | // Create a new conflict method for this to use. |
| 7755 | default_conflict_method = reinterpret_cast<ArtMethod*>(allocator_.Alloc(method_size_)); |
| 7756 | new(default_conflict_method) ArtMethod(interface_method, |
| 7757 | class_linker_->GetImagePointerSize()); |
| 7758 | if (vtable_impl == nullptr) { |
| 7759 | // Save the conflict method. We need to add it to the vtable. |
| 7760 | default_conflict_methods_.push_back(default_conflict_method); |
| 7761 | } else { |
| 7762 | // Save the conflict method but it is already in the vtable. |
| 7763 | overriding_default_conflict_methods_.push_back(default_conflict_method); |
| 7764 | } |
| 7765 | } |
| 7766 | } |
| 7767 | current_method = default_conflict_method; |
| 7768 | break; |
| 7769 | } // case kDefaultConflict |
| 7770 | case DefaultMethodSearchResult::kDefaultFound: { |
| 7771 | DCHECK(current_method != nullptr); |
| 7772 | // Found a default method. |
| 7773 | if (vtable_impl != nullptr && |
| 7774 | current_method->GetDeclaringClass() == vtable_impl->GetDeclaringClass()) { |
| 7775 | // We found a default method but it was the same one we already have from our |
| 7776 | // superclass. Don't bother adding it to our vtable again. |
| 7777 | current_method = vtable_impl; |
| 7778 | } else if (LIKELY(FillTables())) { |
| 7779 | // Interfaces don't need to copy default methods since they don't have vtables. |
| 7780 | // Only record this default method if it is new to save space. |
| 7781 | // TODO It might be worthwhile to copy default methods on interfaces anyway since it |
| 7782 | // would make lookup for interface super much faster. (We would only need to scan |
| 7783 | // the iftable to find if there is a NSME or AME.) |
| 7784 | ArtMethod* old = FindSameNameAndSignature(interface_name_comparator, |
| 7785 | default_methods_, |
| 7786 | overriding_default_methods_); |
| 7787 | if (old == nullptr) { |
| 7788 | // We found a default method implementation and there were no conflicts. |
| 7789 | if (vtable_impl == nullptr) { |
| 7790 | // Save the default method. We need to add it to the vtable. |
| 7791 | default_methods_.push_back(current_method); |
| 7792 | } else { |
| 7793 | // Save the default method but it is already in the vtable. |
| 7794 | overriding_default_methods_.push_back(current_method); |
| 7795 | } |
| 7796 | } else { |
| 7797 | CHECK(old == current_method) << "Multiple default implementations selected!"; |
| 7798 | } |
| 7799 | } |
| 7800 | break; |
| 7801 | } // case kDefaultFound |
| 7802 | case DefaultMethodSearchResult::kAbstractFound: { |
| 7803 | DCHECK(current_method == nullptr); |
| 7804 | // Abstract method masks all defaults. |
| 7805 | if (vtable_impl != nullptr && |
| 7806 | vtable_impl->IsAbstract() && |
| 7807 | !vtable_impl->IsDefaultConflicting()) { |
| 7808 | // We need to make this an abstract method but the version in the vtable already is so |
| 7809 | // don't do anything. |
| 7810 | current_method = vtable_impl; |
| 7811 | } |
| 7812 | break; |
| 7813 | } // case kAbstractFound |
| 7814 | } |
| 7815 | return current_method; |
| 7816 | } |
| 7817 | |
| 7818 | ArtMethod* ClassLinker::LinkInterfaceMethodsHelper::GetOrCreateMirandaMethod( |
| 7819 | ArtMethod* interface_method, |
| 7820 | MethodNameAndSignatureComparator& interface_name_comparator) { |
| 7821 | // Find out if there is already a miranda method we can use. |
| 7822 | ArtMethod* miranda_method = FindSameNameAndSignature(interface_name_comparator, |
| 7823 | miranda_methods_); |
| 7824 | if (miranda_method == nullptr) { |
| 7825 | DCHECK(interface_method->IsAbstract()) << interface_method->PrettyMethod(); |
| 7826 | miranda_method = reinterpret_cast<ArtMethod*>(allocator_.Alloc(method_size_)); |
| 7827 | CHECK(miranda_method != nullptr); |
| 7828 | // Point the interface table at a phantom slot. |
| 7829 | new(miranda_method) ArtMethod(interface_method, class_linker_->GetImagePointerSize()); |
| 7830 | miranda_methods_.push_back(miranda_method); |
| 7831 | } |
| 7832 | return miranda_method; |
| 7833 | } |
| 7834 | |
| 7835 | void ClassLinker::LinkInterfaceMethodsHelper::ReallocMethods() { |
| 7836 | LogNewVirtuals(); |
| 7837 | |
| 7838 | const size_t old_method_count = klass_->NumMethods(); |
| 7839 | const size_t new_method_count = old_method_count + NumberOfNewVirtuals(); |
| 7840 | DCHECK_NE(old_method_count, new_method_count); |
| 7841 | |
| 7842 | // Attempt to realloc to save RAM if possible. |
| 7843 | LengthPrefixedArray<ArtMethod>* old_methods = klass_->GetMethodsPtr(); |
| 7844 | // The Realloced virtual methods aren't visible from the class roots, so there is no issue |
| 7845 | // where GCs could attempt to mark stale pointers due to memcpy. And since we overwrite the |
| 7846 | // realloced memory with out->CopyFrom, we are guaranteed to have objects in the to space since |
| 7847 | // CopyFrom has internal read barriers. |
| 7848 | // |
| 7849 | // TODO We should maybe move some of this into mirror::Class or at least into another method. |
| 7850 | const size_t old_size = LengthPrefixedArray<ArtMethod>::ComputeSize(old_method_count, |
| 7851 | method_size_, |
| 7852 | method_alignment_); |
| 7853 | const size_t new_size = LengthPrefixedArray<ArtMethod>::ComputeSize(new_method_count, |
| 7854 | method_size_, |
| 7855 | method_alignment_); |
| 7856 | const size_t old_methods_ptr_size = (old_methods != nullptr) ? old_size : 0; |
| 7857 | auto* methods = reinterpret_cast<LengthPrefixedArray<ArtMethod>*>( |
Nicolas Geoffray | 48b40cc | 2017-08-07 16:52:40 +0100 | [diff] [blame] | 7858 | class_linker_->GetAllocatorForClassLoader(klass_->GetClassLoader())->Realloc( |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7859 | self_, old_methods, old_methods_ptr_size, new_size)); |
| 7860 | CHECK(methods != nullptr); // Native allocation failure aborts. |
| 7861 | |
| 7862 | PointerSize pointer_size = class_linker_->GetImagePointerSize(); |
| 7863 | if (methods != old_methods) { |
| 7864 | // Maps from heap allocated miranda method to linear alloc miranda method. |
| 7865 | StrideIterator<ArtMethod> out = methods->begin(method_size_, method_alignment_); |
| 7866 | // Copy over the old methods. |
| 7867 | for (auto& m : klass_->GetMethods(pointer_size)) { |
| 7868 | move_table_.emplace(&m, &*out); |
| 7869 | // The CopyFrom is only necessary to not miss read barriers since Realloc won't do read |
| 7870 | // barriers when it copies. |
| 7871 | out->CopyFrom(&m, pointer_size); |
| 7872 | ++out; |
| 7873 | } |
| 7874 | } |
| 7875 | StrideIterator<ArtMethod> out(methods->begin(method_size_, method_alignment_) + old_method_count); |
| 7876 | // Copy over miranda methods before copying vtable since CopyOf may cause thread suspension and |
| 7877 | // we want the roots of the miranda methods to get visited. |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7878 | for (size_t i = 0; i < miranda_methods_.size(); ++i) { |
| 7879 | ArtMethod* mir_method = miranda_methods_[i]; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7880 | ArtMethod& new_method = *out; |
| 7881 | new_method.CopyFrom(mir_method, pointer_size); |
| 7882 | new_method.SetAccessFlags(new_method.GetAccessFlags() | kAccMiranda | kAccCopied); |
| 7883 | DCHECK_NE(new_method.GetAccessFlags() & kAccAbstract, 0u) |
| 7884 | << "Miranda method should be abstract!"; |
| 7885 | move_table_.emplace(mir_method, &new_method); |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7886 | // Update the entry in the method array, as the array will be used for future lookups, |
| 7887 | // where thread suspension is allowed. |
| 7888 | // As such, the array should not contain locally allocated ArtMethod, otherwise the GC |
| 7889 | // would not see them. |
| 7890 | miranda_methods_[i] = &new_method; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7891 | ++out; |
| 7892 | } |
| 7893 | // We need to copy the default methods into our own method table since the runtime requires that |
| 7894 | // every method on a class's vtable be in that respective class's virtual method table. |
| 7895 | // NOTE This means that two classes might have the same implementation of a method from the same |
| 7896 | // interface but will have different ArtMethod*s for them. This also means we cannot compare a |
| 7897 | // default method found on a class with one found on the declaring interface directly and must |
| 7898 | // look at the declaring class to determine if they are the same. |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7899 | for (ScopedArenaVector<ArtMethod*>* methods_vec : {&default_methods_, |
| 7900 | &overriding_default_methods_}) { |
| 7901 | for (size_t i = 0; i < methods_vec->size(); ++i) { |
| 7902 | ArtMethod* def_method = (*methods_vec)[i]; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7903 | ArtMethod& new_method = *out; |
| 7904 | new_method.CopyFrom(def_method, pointer_size); |
| 7905 | // Clear the kAccSkipAccessChecks flag if it is present. Since this class hasn't been |
| 7906 | // verified yet it shouldn't have methods that are skipping access checks. |
| 7907 | // TODO This is rather arbitrary. We should maybe support classes where only some of its |
| 7908 | // methods are skip_access_checks. |
Vladimir Marko | b0a6aee | 2017-10-27 10:34:04 +0100 | [diff] [blame] | 7909 | DCHECK_EQ(new_method.GetAccessFlags() & kAccNative, 0u); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7910 | constexpr uint32_t kSetFlags = kAccDefault | kAccCopied; |
| 7911 | constexpr uint32_t kMaskFlags = ~kAccSkipAccessChecks; |
| 7912 | new_method.SetAccessFlags((new_method.GetAccessFlags() | kSetFlags) & kMaskFlags); |
| 7913 | move_table_.emplace(def_method, &new_method); |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7914 | // Update the entry in the method array, as the array will be used for future lookups, |
| 7915 | // where thread suspension is allowed. |
| 7916 | // As such, the array should not contain locally allocated ArtMethod, otherwise the GC |
| 7917 | // would not see them. |
| 7918 | (*methods_vec)[i] = &new_method; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7919 | ++out; |
| 7920 | } |
| 7921 | } |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7922 | for (ScopedArenaVector<ArtMethod*>* methods_vec : {&default_conflict_methods_, |
| 7923 | &overriding_default_conflict_methods_}) { |
| 7924 | for (size_t i = 0; i < methods_vec->size(); ++i) { |
| 7925 | ArtMethod* conf_method = (*methods_vec)[i]; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7926 | ArtMethod& new_method = *out; |
| 7927 | new_method.CopyFrom(conf_method, pointer_size); |
| 7928 | // This is a type of default method (there are default method impls, just a conflict) so |
| 7929 | // mark this as a default, non-abstract method, since thats what it is. Also clear the |
| 7930 | // kAccSkipAccessChecks bit since this class hasn't been verified yet it shouldn't have |
| 7931 | // methods that are skipping access checks. |
Nicolas Geoffray | 7aca9d5 | 2018-09-07 11:13:33 +0100 | [diff] [blame] | 7932 | // Also clear potential kAccSingleImplementation to avoid CHA trying to inline |
| 7933 | // the default method. |
Vladimir Marko | b0a6aee | 2017-10-27 10:34:04 +0100 | [diff] [blame] | 7934 | DCHECK_EQ(new_method.GetAccessFlags() & kAccNative, 0u); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7935 | constexpr uint32_t kSetFlags = kAccDefault | kAccDefaultConflict | kAccCopied; |
Nicolas Geoffray | 7aca9d5 | 2018-09-07 11:13:33 +0100 | [diff] [blame] | 7936 | constexpr uint32_t kMaskFlags = |
| 7937 | ~(kAccAbstract | kAccSkipAccessChecks | kAccSingleImplementation); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7938 | new_method.SetAccessFlags((new_method.GetAccessFlags() | kSetFlags) & kMaskFlags); |
| 7939 | DCHECK(new_method.IsDefaultConflicting()); |
| 7940 | // The actual method might or might not be marked abstract since we just copied it from a |
| 7941 | // (possibly default) interface method. We need to set it entry point to be the bridge so |
| 7942 | // that the compiler will not invoke the implementation of whatever method we copied from. |
| 7943 | EnsureThrowsInvocationError(class_linker_, &new_method); |
| 7944 | move_table_.emplace(conf_method, &new_method); |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7945 | // Update the entry in the method array, as the array will be used for future lookups, |
| 7946 | // where thread suspension is allowed. |
| 7947 | // As such, the array should not contain locally allocated ArtMethod, otherwise the GC |
| 7948 | // would not see them. |
| 7949 | (*methods_vec)[i] = &new_method; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7950 | ++out; |
| 7951 | } |
| 7952 | } |
| 7953 | methods->SetSize(new_method_count); |
| 7954 | class_linker_->UpdateClassMethods(klass_.Get(), methods); |
| 7955 | } |
| 7956 | |
| 7957 | ObjPtr<mirror::PointerArray> ClassLinker::LinkInterfaceMethodsHelper::UpdateVtable( |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 7958 | const HashMap<size_t, ClassLinker::MethodTranslation>& default_translations, |
Vladimir Marko | 3068d58 | 2019-05-28 16:39:29 +0100 | [diff] [blame] | 7959 | Handle<mirror::PointerArray> old_vtable) { |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7960 | // Update the vtable to the new method structures. We can skip this for interfaces since they |
| 7961 | // do not have vtables. |
| 7962 | const size_t old_vtable_count = old_vtable->GetLength(); |
| 7963 | const size_t new_vtable_count = old_vtable_count + |
| 7964 | miranda_methods_.size() + |
| 7965 | default_methods_.size() + |
| 7966 | default_conflict_methods_.size(); |
| 7967 | |
Vladimir Marko | 3068d58 | 2019-05-28 16:39:29 +0100 | [diff] [blame] | 7968 | ObjPtr<mirror::PointerArray> vtable = ObjPtr<mirror::PointerArray>::DownCast( |
| 7969 | mirror::Array::CopyOf(old_vtable, self_, new_vtable_count)); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7970 | if (UNLIKELY(vtable == nullptr)) { |
| 7971 | self_->AssertPendingOOMException(); |
| 7972 | return nullptr; |
| 7973 | } |
| 7974 | |
| 7975 | size_t vtable_pos = old_vtable_count; |
| 7976 | PointerSize pointer_size = class_linker_->GetImagePointerSize(); |
| 7977 | // Update all the newly copied method's indexes so they denote their placement in the vtable. |
| 7978 | for (const ScopedArenaVector<ArtMethod*>& methods_vec : {default_methods_, |
| 7979 | default_conflict_methods_, |
| 7980 | miranda_methods_}) { |
| 7981 | // These are the functions that are not already in the vtable! |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 7982 | for (ArtMethod* new_vtable_method : methods_vec) { |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7983 | // Leave the declaring class alone the method's dex_code_item_offset_ and dex_method_index_ |
| 7984 | // fields are references into the dex file the method was defined in. Since the ArtMethod |
| 7985 | // does not store that information it uses declaring_class_->dex_cache_. |
| 7986 | new_vtable_method->SetMethodIndex(0xFFFF & vtable_pos); |
| 7987 | vtable->SetElementPtrSize(vtable_pos, new_vtable_method, pointer_size); |
| 7988 | ++vtable_pos; |
| 7989 | } |
| 7990 | } |
| 7991 | DCHECK_EQ(vtable_pos, new_vtable_count); |
| 7992 | |
| 7993 | // Update old vtable methods. We use the default_translations map to figure out what each |
| 7994 | // vtable entry should be updated to, if they need to be at all. |
| 7995 | for (size_t i = 0; i < old_vtable_count; ++i) { |
| 7996 | ArtMethod* translated_method = vtable->GetElementPtrSize<ArtMethod*>(i, pointer_size); |
| 7997 | // Try and find what we need to change this method to. |
| 7998 | auto translation_it = default_translations.find(i); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 7999 | if (translation_it != default_translations.end()) { |
| 8000 | if (translation_it->second.IsInConflict()) { |
| 8001 | // Find which conflict method we are to use for this method. |
| 8002 | MethodNameAndSignatureComparator old_method_comparator( |
| 8003 | translated_method->GetInterfaceMethodIfProxy(pointer_size)); |
| 8004 | // We only need to look through overriding_default_conflict_methods since this is an |
| 8005 | // overridden method we are fixing up here. |
| 8006 | ArtMethod* new_conflict_method = FindSameNameAndSignature( |
| 8007 | old_method_comparator, overriding_default_conflict_methods_); |
| 8008 | CHECK(new_conflict_method != nullptr) << "Expected a conflict method!"; |
| 8009 | translated_method = new_conflict_method; |
| 8010 | } else if (translation_it->second.IsAbstract()) { |
| 8011 | // Find which miranda method we are to use for this method. |
| 8012 | MethodNameAndSignatureComparator old_method_comparator( |
| 8013 | translated_method->GetInterfaceMethodIfProxy(pointer_size)); |
| 8014 | ArtMethod* miranda_method = FindSameNameAndSignature(old_method_comparator, |
| 8015 | miranda_methods_); |
| 8016 | DCHECK(miranda_method != nullptr); |
| 8017 | translated_method = miranda_method; |
| 8018 | } else { |
| 8019 | // Normal default method (changed from an older default or abstract interface method). |
| 8020 | DCHECK(translation_it->second.IsTranslation()); |
| 8021 | translated_method = translation_it->second.GetTranslation(); |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 8022 | auto it = move_table_.find(translated_method); |
| 8023 | DCHECK(it != move_table_.end()); |
| 8024 | translated_method = it->second; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 8025 | } |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 8026 | } else { |
| 8027 | auto it = move_table_.find(translated_method); |
| 8028 | translated_method = (it != move_table_.end()) ? it->second : nullptr; |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 8029 | } |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 8030 | |
| 8031 | if (translated_method != nullptr) { |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 8032 | // Make sure the new_methods index is set. |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 8033 | if (translated_method->GetMethodIndexDuringLinking() != i) { |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 8034 | if (kIsDebugBuild) { |
| 8035 | auto* methods = klass_->GetMethodsPtr(); |
| 8036 | CHECK_LE(reinterpret_cast<uintptr_t>(&*methods->begin(method_size_, method_alignment_)), |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 8037 | reinterpret_cast<uintptr_t>(translated_method)); |
| 8038 | CHECK_LT(reinterpret_cast<uintptr_t>(translated_method), |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 8039 | reinterpret_cast<uintptr_t>(&*methods->end(method_size_, method_alignment_))); |
| 8040 | } |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 8041 | translated_method->SetMethodIndex(0xFFFF & i); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 8042 | } |
Nicolas Geoffray | 0376a5c | 2017-01-12 15:15:45 +0000 | [diff] [blame] | 8043 | vtable->SetElementPtrSize(i, translated_method, pointer_size); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 8044 | } |
| 8045 | } |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 8046 | klass_->SetVTable(vtable); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 8047 | return vtable; |
| 8048 | } |
| 8049 | |
| 8050 | void ClassLinker::LinkInterfaceMethodsHelper::UpdateIfTable(Handle<mirror::IfTable> iftable) { |
| 8051 | PointerSize pointer_size = class_linker_->GetImagePointerSize(); |
| 8052 | const size_t ifcount = klass_->GetIfTableCount(); |
| 8053 | // Go fix up all the stale iftable pointers. |
| 8054 | for (size_t i = 0; i < ifcount; ++i) { |
| 8055 | for (size_t j = 0, count = iftable->GetMethodArrayCount(i); j < count; ++j) { |
Vladimir Marko | 557fece | 2019-03-26 14:29:41 +0000 | [diff] [blame] | 8056 | ObjPtr<mirror::PointerArray> method_array = iftable->GetMethodArray(i); |
| 8057 | ArtMethod* m = method_array->GetElementPtrSize<ArtMethod*>(j, pointer_size); |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 8058 | DCHECK(m != nullptr) << klass_->PrettyClass(); |
| 8059 | auto it = move_table_.find(m); |
| 8060 | if (it != move_table_.end()) { |
| 8061 | auto* new_m = it->second; |
| 8062 | DCHECK(new_m != nullptr) << klass_->PrettyClass(); |
| 8063 | method_array->SetElementPtrSize(j, new_m, pointer_size); |
| 8064 | } |
| 8065 | } |
| 8066 | } |
| 8067 | } |
| 8068 | |
| 8069 | void ClassLinker::LinkInterfaceMethodsHelper::UpdateIMT(ArtMethod** out_imt) { |
| 8070 | // Fix up IMT next. |
| 8071 | for (size_t i = 0; i < ImTable::kSize; ++i) { |
| 8072 | auto it = move_table_.find(out_imt[i]); |
| 8073 | if (it != move_table_.end()) { |
| 8074 | out_imt[i] = it->second; |
| 8075 | } |
| 8076 | } |
| 8077 | } |
| 8078 | |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8079 | // TODO This method needs to be split up into several smaller methods. |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 8080 | bool ClassLinker::LinkInterfaceMethods( |
| 8081 | Thread* self, |
| 8082 | Handle<mirror::Class> klass, |
Vladimir Marko | 782fb71 | 2020-12-23 12:47:31 +0000 | [diff] [blame] | 8083 | const HashMap<size_t, ClassLinker::MethodTranslation>& default_translations, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 8084 | bool* out_new_conflict, |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 8085 | ArtMethod** out_imt) { |
| 8086 | StackHandleScope<3> hs(self); |
| 8087 | Runtime* const runtime = Runtime::Current(); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8088 | |
| 8089 | const bool is_interface = klass->IsInterface(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 8090 | const bool has_superclass = klass->HasSuperClass(); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8091 | const bool fill_tables = !is_interface; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 8092 | const size_t super_ifcount = has_superclass ? klass->GetSuperClass()->GetIfTableCount() : 0U; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 8093 | const size_t ifcount = klass->GetIfTableCount(); |
| 8094 | |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 8095 | Handle<mirror::IfTable> iftable(hs.NewHandle(klass->GetIfTable())); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8096 | |
| 8097 | MutableHandle<mirror::PointerArray> vtable(hs.NewHandle(klass->GetVTableDuringLinking())); |
| 8098 | ArtMethod* const unimplemented_method = runtime->GetImtUnimplementedMethod(); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 8099 | ArtMethod* const imt_conflict_method = runtime->GetImtConflictMethod(); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 8100 | // Copy the IMT from the super class if possible. |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8101 | const bool extend_super_iftable = has_superclass; |
| 8102 | if (has_superclass && fill_tables) { |
| 8103 | FillImtFromSuperClass(klass, |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8104 | unimplemented_method, |
| 8105 | imt_conflict_method, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 8106 | out_new_conflict, |
Mathieu Chartier | 49b5ced | 2016-04-14 10:49:19 -0700 | [diff] [blame] | 8107 | out_imt); |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 8108 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8109 | // Allocate method arrays before since we don't want miss visiting miranda method roots due to |
| 8110 | // thread suspension. |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8111 | if (fill_tables) { |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 8112 | if (!AllocateIfTableMethodArrays(self, klass, iftable)) { |
| 8113 | return false; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8114 | } |
| 8115 | } |
| 8116 | |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 8117 | LinkInterfaceMethodsHelper helper(this, klass, self, runtime); |
| 8118 | |
Igor Murashkin | b1d8c31 | 2015-08-04 11:18:43 -0700 | [diff] [blame] | 8119 | auto* old_cause = self->StartAssertNoThreadSuspension( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8120 | "Copying ArtMethods for LinkInterfaceMethods"); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 8121 | // Going in reverse to ensure that we will hit abstract methods that override defaults before the |
| 8122 | // defaults. This means we don't need to do any trickery when creating the Miranda methods, since |
| 8123 | // they will already be null. This has the additional benefit that the declarer of a miranda |
| 8124 | // method will actually declare an abstract method. |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 8125 | for (size_t i = ifcount; i != 0u; ) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 8126 | --i; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 8127 | DCHECK_LT(i, ifcount); |
| 8128 | |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8129 | size_t num_methods = iftable->GetInterface(i)->NumDeclaredVirtualMethods(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8130 | if (num_methods > 0) { |
| 8131 | StackHandleScope<2> hs2(self); |
| 8132 | const bool is_super = i < super_ifcount; |
| 8133 | const bool super_interface = is_super && extend_super_iftable; |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8134 | // We don't actually create or fill these tables for interfaces, we just copy some methods for |
| 8135 | // conflict methods. Just set this as nullptr in those cases. |
| 8136 | Handle<mirror::PointerArray> method_array(fill_tables |
| 8137 | ? hs2.NewHandle(iftable->GetMethodArray(i)) |
| 8138 | : hs2.NewHandle<mirror::PointerArray>(nullptr)); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8139 | |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 8140 | ArraySlice<ArtMethod> input_virtual_methods; |
Mathieu Chartier | 9865bde | 2015-12-21 09:58:16 -0800 | [diff] [blame] | 8141 | ScopedNullHandle<mirror::PointerArray> null_handle; |
| 8142 | Handle<mirror::PointerArray> input_vtable_array(null_handle); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8143 | int32_t input_array_length = 0; |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 8144 | |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 8145 | // TODO Cleanup Needed: In the presence of default methods this optimization is rather dirty |
| 8146 | // and confusing. Default methods should always look through all the superclasses |
| 8147 | // because they are the last choice of an implementation. We get around this by looking |
| 8148 | // at the super-classes iftable methods (copied into method_array previously) when we are |
| 8149 | // looking for the implementation of a super-interface method but that is rather dirty. |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 8150 | bool using_virtuals; |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8151 | if (super_interface || is_interface) { |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 8152 | // If we are overwriting a super class interface, try to only virtual methods instead of the |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8153 | // whole vtable. |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 8154 | using_virtuals = true; |
Alex Light | a467a6e | 2020-03-23 16:07:29 -0700 | [diff] [blame] | 8155 | input_virtual_methods = klass->GetDeclaredVirtualMethodsSlice(image_pointer_size_); |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 8156 | input_array_length = input_virtual_methods.size(); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8157 | } else { |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 8158 | // For a new interface, however, we need the whole vtable in case a new |
| 8159 | // interface method is implemented in the whole superclass. |
| 8160 | using_virtuals = false; |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8161 | DCHECK(vtable != nullptr); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8162 | input_vtable_array = vtable; |
| 8163 | input_array_length = input_vtable_array->GetLength(); |
| 8164 | } |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 8165 | |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 8166 | // For each method in interface |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 8167 | for (size_t j = 0; j < num_methods; ++j) { |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 8168 | auto* interface_method = iftable->GetInterface(i)->GetVirtualMethod(j, image_pointer_size_); |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 8169 | MethodNameAndSignatureComparator interface_name_comparator( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8170 | interface_method->GetInterfaceMethodIfProxy(image_pointer_size_)); |
David Srbecky | e36e7f2 | 2018-11-14 14:21:23 +0000 | [diff] [blame] | 8171 | uint32_t imt_index = interface_method->GetImtIndex(); |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 8172 | ArtMethod** imt_ptr = &out_imt[imt_index]; |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 8173 | // For each method listed in the interface's method list, find the |
| 8174 | // matching method in our class's method list. We want to favor the |
| 8175 | // subclass over the superclass, which just requires walking |
| 8176 | // back from the end of the vtable. (This only matters if the |
| 8177 | // superclass defines a private method and this class redefines |
| 8178 | // it -- otherwise it would use the same vtable slot. In .dex files |
| 8179 | // those don't end up in the virtual method table, so it shouldn't |
| 8180 | // matter which direction we go. We walk it backward anyway.) |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 8181 | // |
| 8182 | // To find defaults we need to do the same but also go over interfaces. |
| 8183 | bool found_impl = false; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 8184 | ArtMethod* vtable_impl = nullptr; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 8185 | for (int32_t k = input_array_length - 1; k >= 0; --k) { |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 8186 | ArtMethod* vtable_method = using_virtuals ? |
| 8187 | &input_virtual_methods[k] : |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8188 | input_vtable_array->GetElementPtrSize<ArtMethod*>(k, image_pointer_size_); |
| 8189 | ArtMethod* vtable_method_for_name_comparison = |
| 8190 | vtable_method->GetInterfaceMethodIfProxy(image_pointer_size_); |
Alex Light | a467a6e | 2020-03-23 16:07:29 -0700 | [diff] [blame] | 8191 | DCHECK(!vtable_method->IsStatic()) << vtable_method->PrettyMethod(); |
Ian Rogers | 03b6eaf | 2014-10-28 09:34:57 -0700 | [diff] [blame] | 8192 | if (interface_name_comparator.HasSameNameAndSignature( |
Mathieu Chartier | 9f3629d | 2014-10-28 18:23:02 -0700 | [diff] [blame] | 8193 | vtable_method_for_name_comparison)) { |
Mathieu Chartier | 2d2621a | 2014-10-23 16:48:06 -0700 | [diff] [blame] | 8194 | if (!vtable_method->IsAbstract() && !vtable_method->IsPublic()) { |
Mathieu Chartier | 4d122c1 | 2015-06-17 14:14:36 -0700 | [diff] [blame] | 8195 | // Must do EndAssertNoThreadSuspension before throw since the throw can cause |
| 8196 | // allocations. |
| 8197 | self->EndAssertNoThreadSuspension(old_cause); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8198 | ThrowIllegalAccessError(klass.Get(), |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 8199 | "Method '%s' implementing interface method '%s' is not public", |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 8200 | vtable_method->PrettyMethod().c_str(), |
| 8201 | interface_method->PrettyMethod().c_str()); |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 8202 | return false; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 8203 | } else if (UNLIKELY(vtable_method->IsOverridableByDefaultMethod())) { |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 8204 | // We might have a newer, better, default method for this, so we just skip it. If we |
| 8205 | // are still using this we will select it again when scanning for default methods. To |
| 8206 | // obviate the need to copy the method again we will make a note that we already found |
| 8207 | // a default here. |
| 8208 | // TODO This should be much cleaner. |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 8209 | vtable_impl = vtable_method; |
Alex Light | eb7c144 | 2015-08-31 13:17:42 -0700 | [diff] [blame] | 8210 | break; |
| 8211 | } else { |
| 8212 | found_impl = true; |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8213 | if (LIKELY(fill_tables)) { |
| 8214 | method_array->SetElementPtrSize(j, vtable_method, image_pointer_size_); |
| 8215 | // Place method in imt if entry is empty, place conflict otherwise. |
| 8216 | SetIMTRef(unimplemented_method, |
| 8217 | imt_conflict_method, |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8218 | vtable_method, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 8219 | /*out*/out_new_conflict, |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8220 | /*out*/imt_ptr); |
| 8221 | } |
Ian Rogers | 9bc8191 | 2012-10-11 21:43:36 -0700 | [diff] [blame] | 8222 | break; |
| 8223 | } |
| 8224 | } |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 8225 | } |
| 8226 | // Continue on to the next method if we are done. |
| 8227 | if (LIKELY(found_impl)) { |
| 8228 | continue; |
| 8229 | } else if (LIKELY(super_interface)) { |
| 8230 | // Don't look for a default implementation when the super-method is implemented directly |
| 8231 | // by the class. |
| 8232 | // |
| 8233 | // See if we can use the superclasses method and skip searching everything else. |
| 8234 | // Note: !found_impl && super_interface |
| 8235 | CHECK(extend_super_iftable); |
| 8236 | // If this is a super_interface method it is possible we shouldn't override it because a |
| 8237 | // superclass could have implemented it directly. We get the method the superclass used |
| 8238 | // to implement this to know if we can override it with a default method. Doing this is |
| 8239 | // safe since we know that the super_iftable is filled in so we can simply pull it from |
| 8240 | // there. We don't bother if this is not a super-classes interface since in that case we |
| 8241 | // have scanned the entire vtable anyway and would have found it. |
| 8242 | // TODO This is rather dirty but it is faster than searching through the entire vtable |
| 8243 | // every time. |
| 8244 | ArtMethod* supers_method = |
| 8245 | method_array->GetElementPtrSize<ArtMethod*>(j, image_pointer_size_); |
| 8246 | DCHECK(supers_method != nullptr); |
| 8247 | DCHECK(interface_name_comparator.HasSameNameAndSignature(supers_method)); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8248 | if (LIKELY(!supers_method->IsOverridableByDefaultMethod())) { |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 8249 | // The method is not overridable by a default method (i.e. it is directly implemented |
| 8250 | // in some class). Therefore move onto the next interface method. |
| 8251 | continue; |
Alex Light | d6c2bfa | 2016-05-02 18:51:34 -0700 | [diff] [blame] | 8252 | } else { |
| 8253 | // If the super-classes method is override-able by a default method we need to keep |
| 8254 | // track of it since though it is override-able it is not guaranteed to be 'overridden'. |
| 8255 | // If it turns out not to be overridden and we did not keep track of it we might add it |
Alex Light | 66630be | 2016-05-04 09:23:09 -0700 | [diff] [blame] | 8256 | // to the vtable twice, causing corruption (vtable entries having inconsistent and |
| 8257 | // illegal states, incorrect vtable size, and incorrect or inconsistent iftable entries) |
| 8258 | // in this class and any subclasses. |
Alex Light | d6c2bfa | 2016-05-02 18:51:34 -0700 | [diff] [blame] | 8259 | DCHECK(vtable_impl == nullptr || vtable_impl == supers_method) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 8260 | << "vtable_impl was " << ArtMethod::PrettyMethod(vtable_impl) |
| 8261 | << " and not 'nullptr' or " |
| 8262 | << supers_method->PrettyMethod() |
| 8263 | << " as expected. IFTable appears to be corrupt!"; |
Alex Light | d6c2bfa | 2016-05-02 18:51:34 -0700 | [diff] [blame] | 8264 | vtable_impl = supers_method; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 8265 | } |
| 8266 | } |
| 8267 | // If we haven't found it yet we should search through the interfaces for default methods. |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 8268 | ArtMethod* current_method = helper.FindMethod(interface_method, |
| 8269 | interface_name_comparator, |
| 8270 | vtable_impl); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8271 | if (LIKELY(fill_tables)) { |
Alex Light | 1277108 | 2016-01-26 16:07:41 -0800 | [diff] [blame] | 8272 | if (current_method == nullptr && !super_interface) { |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8273 | // We could not find an implementation for this method and since it is a brand new |
| 8274 | // interface we searched the entire vtable (and all default methods) for an |
| 8275 | // implementation but couldn't find one. We therefore need to make a miranda method. |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 8276 | current_method = helper.GetOrCreateMirandaMethod(interface_method, |
| 8277 | interface_name_comparator); |
Alex Light | 1277108 | 2016-01-26 16:07:41 -0800 | [diff] [blame] | 8278 | } |
| 8279 | |
| 8280 | if (current_method != nullptr) { |
| 8281 | // We found a default method implementation. Record it in the iftable and IMT. |
| 8282 | method_array->SetElementPtrSize(j, current_method, image_pointer_size_); |
| 8283 | SetIMTRef(unimplemented_method, |
| 8284 | imt_conflict_method, |
Alex Light | 1277108 | 2016-01-26 16:07:41 -0800 | [diff] [blame] | 8285 | current_method, |
Artem Udovichenko | a62cb9b | 2016-06-30 09:18:25 +0000 | [diff] [blame] | 8286 | /*out*/out_new_conflict, |
Alex Light | 1277108 | 2016-01-26 16:07:41 -0800 | [diff] [blame] | 8287 | /*out*/imt_ptr); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 8288 | } |
| 8289 | } |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8290 | } // For each method in interface end. |
| 8291 | } // if (num_methods > 0) |
| 8292 | } // For each interface. |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8293 | // TODO don't extend virtuals of interface unless necessary (when is it?). |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 8294 | if (helper.HasNewVirtuals()) { |
| 8295 | LengthPrefixedArray<ArtMethod>* old_methods = kIsDebugBuild ? klass->GetMethodsPtr() : nullptr; |
| 8296 | helper.ReallocMethods(); // No return value to check. Native allocation failure aborts. |
| 8297 | LengthPrefixedArray<ArtMethod>* methods = kIsDebugBuild ? klass->GetMethodsPtr() : nullptr; |
| 8298 | |
Mathieu Chartier | d4d83b8 | 2015-06-19 20:24:45 -0700 | [diff] [blame] | 8299 | // Done copying methods, they are all roots in the class now, so we can end the no thread |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8300 | // suspension assert. |
| 8301 | self->EndAssertNoThreadSuspension(old_cause); |
Mathieu Chartier | d4d83b8 | 2015-06-19 20:24:45 -0700 | [diff] [blame] | 8302 | |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8303 | if (fill_tables) { |
Vladimir Marko | 3068d58 | 2019-05-28 16:39:29 +0100 | [diff] [blame] | 8304 | vtable.Assign(helper.UpdateVtable(default_translations, vtable)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8305 | if (UNLIKELY(vtable == nullptr)) { |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 8306 | // The helper has already called self->AssertPendingOOMException(); |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8307 | return false; |
| 8308 | } |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 8309 | helper.UpdateIfTable(iftable); |
| 8310 | helper.UpdateIMT(out_imt); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8311 | } |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8312 | |
Vladimir Marko | 921094a | 2017-01-12 18:37:06 +0000 | [diff] [blame] | 8313 | helper.CheckNoStaleMethodsInDexCache(); |
| 8314 | helper.ClobberOldMethods(old_methods, methods); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8315 | } else { |
| 8316 | self->EndAssertNoThreadSuspension(old_cause); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 8317 | } |
Alex Light | 705ad49 | 2015-09-21 11:36:30 -0700 | [diff] [blame] | 8318 | if (kIsDebugBuild && !is_interface) { |
Orion Hodson | 5880c77 | 2020-07-28 20:12:08 +0100 | [diff] [blame] | 8319 | CheckVTable(self, klass, image_pointer_size_); |
Elliott Hughes | 4681c80 | 2011-09-25 18:04:37 -0700 | [diff] [blame] | 8320 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 8321 | return true; |
| 8322 | } |
| 8323 | |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8324 | class ClassLinker::LinkFieldsHelper { |
| 8325 | public: |
| 8326 | static bool LinkFields(ClassLinker* class_linker, |
| 8327 | Thread* self, |
| 8328 | Handle<mirror::Class> klass, |
| 8329 | bool is_static, |
| 8330 | size_t* class_size) |
| 8331 | REQUIRES_SHARED(Locks::mutator_lock_); |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 8332 | |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8333 | private: |
| 8334 | enum class FieldTypeOrder : uint16_t; |
| 8335 | class FieldGaps; |
| 8336 | |
| 8337 | struct FieldTypeOrderAndIndex { |
| 8338 | FieldTypeOrder field_type_order; |
| 8339 | uint16_t field_index; |
| 8340 | }; |
| 8341 | |
| 8342 | static FieldTypeOrder FieldTypeOrderFromFirstDescriptorCharacter(char first_char); |
| 8343 | |
| 8344 | template <size_t kSize> |
| 8345 | static MemberOffset AssignFieldOffset(ArtField* field, MemberOffset field_offset) |
| 8346 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 8347 | }; |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 8348 | |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8349 | // We use the following order of field types for assigning offsets. |
| 8350 | // Some fields can be shuffled forward to fill gaps, see `ClassLinker::LinkFields()`. |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8351 | enum class ClassLinker::LinkFieldsHelper::FieldTypeOrder : uint16_t { |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8352 | kReference = 0u, |
| 8353 | kLong, |
| 8354 | kDouble, |
| 8355 | kInt, |
| 8356 | kFloat, |
| 8357 | kChar, |
| 8358 | kShort, |
| 8359 | kBoolean, |
| 8360 | kByte, |
| 8361 | |
| 8362 | kLast64BitType = kDouble, |
| 8363 | kLast32BitType = kFloat, |
| 8364 | kLast16BitType = kShort, |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 8365 | }; |
| 8366 | |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8367 | ALWAYS_INLINE |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8368 | ClassLinker::LinkFieldsHelper::FieldTypeOrder |
| 8369 | ClassLinker::LinkFieldsHelper::FieldTypeOrderFromFirstDescriptorCharacter(char first_char) { |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8370 | switch (first_char) { |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8371 | case 'J': |
| 8372 | return FieldTypeOrder::kLong; |
| 8373 | case 'D': |
| 8374 | return FieldTypeOrder::kDouble; |
| 8375 | case 'I': |
| 8376 | return FieldTypeOrder::kInt; |
| 8377 | case 'F': |
| 8378 | return FieldTypeOrder::kFloat; |
| 8379 | case 'C': |
| 8380 | return FieldTypeOrder::kChar; |
| 8381 | case 'S': |
| 8382 | return FieldTypeOrder::kShort; |
| 8383 | case 'Z': |
| 8384 | return FieldTypeOrder::kBoolean; |
| 8385 | case 'B': |
| 8386 | return FieldTypeOrder::kByte; |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8387 | default: |
| 8388 | DCHECK(first_char == 'L' || first_char == '[') << first_char; |
| 8389 | return FieldTypeOrder::kReference; |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8390 | } |
| 8391 | } |
| 8392 | |
| 8393 | // Gaps where we can insert fields in object layout. |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8394 | class ClassLinker::LinkFieldsHelper::FieldGaps { |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8395 | public: |
| 8396 | template <uint32_t kSize> |
| 8397 | ALWAYS_INLINE MemberOffset AlignFieldOffset(MemberOffset field_offset) { |
| 8398 | static_assert(kSize == 2u || kSize == 4u || kSize == 8u); |
| 8399 | if (!IsAligned<kSize>(field_offset.Uint32Value())) { |
| 8400 | uint32_t gap_start = field_offset.Uint32Value(); |
| 8401 | field_offset = MemberOffset(RoundUp(gap_start, kSize)); |
| 8402 | AddGaps<kSize - 1u>(gap_start, field_offset.Uint32Value()); |
| 8403 | } |
| 8404 | return field_offset; |
| 8405 | } |
| 8406 | |
| 8407 | template <uint32_t kSize> |
| 8408 | bool HasGap() const { |
| 8409 | static_assert(kSize == 1u || kSize == 2u || kSize == 4u); |
| 8410 | return (kSize == 1u && gap1_offset_ != kNoOffset) || |
| 8411 | (kSize <= 2u && gap2_offset_ != kNoOffset) || |
| 8412 | gap4_offset_ != kNoOffset; |
| 8413 | } |
| 8414 | |
| 8415 | template <uint32_t kSize> |
| 8416 | MemberOffset ReleaseGap() { |
| 8417 | static_assert(kSize == 1u || kSize == 2u || kSize == 4u); |
| 8418 | uint32_t result; |
| 8419 | if (kSize == 1u && gap1_offset_ != kNoOffset) { |
| 8420 | DCHECK(gap2_offset_ == kNoOffset || gap2_offset_ > gap1_offset_); |
| 8421 | DCHECK(gap4_offset_ == kNoOffset || gap4_offset_ > gap1_offset_); |
| 8422 | result = gap1_offset_; |
| 8423 | gap1_offset_ = kNoOffset; |
| 8424 | } else if (kSize <= 2u && gap2_offset_ != kNoOffset) { |
| 8425 | DCHECK(gap4_offset_ == kNoOffset || gap4_offset_ > gap2_offset_); |
| 8426 | result = gap2_offset_; |
| 8427 | gap2_offset_ = kNoOffset; |
| 8428 | if (kSize < 2u) { |
| 8429 | AddGaps<1u>(result + kSize, result + 2u); |
| 8430 | } |
| 8431 | } else { |
| 8432 | DCHECK_NE(gap4_offset_, kNoOffset); |
| 8433 | result = gap4_offset_; |
| 8434 | gap4_offset_ = kNoOffset; |
| 8435 | if (kSize < 4u) { |
| 8436 | AddGaps<kSize | 2u>(result + kSize, result + 4u); |
| 8437 | } |
| 8438 | } |
| 8439 | return MemberOffset(result); |
| 8440 | } |
| 8441 | |
| 8442 | private: |
| 8443 | template <uint32_t kGapsToCheck> |
| 8444 | void AddGaps(uint32_t gap_start, uint32_t gap_end) { |
| 8445 | if ((kGapsToCheck & 1u) != 0u) { |
| 8446 | DCHECK_LT(gap_start, gap_end); |
| 8447 | DCHECK_ALIGNED(gap_end, 2u); |
| 8448 | if ((gap_start & 1u) != 0u) { |
| 8449 | DCHECK_EQ(gap1_offset_, kNoOffset); |
| 8450 | gap1_offset_ = gap_start; |
| 8451 | gap_start += 1u; |
| 8452 | if (kGapsToCheck == 1u || gap_start == gap_end) { |
| 8453 | DCHECK_EQ(gap_start, gap_end); |
| 8454 | return; |
| 8455 | } |
| 8456 | } |
| 8457 | } |
| 8458 | |
| 8459 | if ((kGapsToCheck & 2u) != 0u) { |
| 8460 | DCHECK_LT(gap_start, gap_end); |
| 8461 | DCHECK_ALIGNED(gap_start, 2u); |
| 8462 | DCHECK_ALIGNED(gap_end, 4u); |
| 8463 | if ((gap_start & 2u) != 0u) { |
| 8464 | DCHECK_EQ(gap2_offset_, kNoOffset); |
| 8465 | gap2_offset_ = gap_start; |
| 8466 | gap_start += 2u; |
| 8467 | if (kGapsToCheck <= 3u || gap_start == gap_end) { |
| 8468 | DCHECK_EQ(gap_start, gap_end); |
| 8469 | return; |
| 8470 | } |
| 8471 | } |
| 8472 | } |
| 8473 | |
| 8474 | if ((kGapsToCheck & 4u) != 0u) { |
| 8475 | DCHECK_LT(gap_start, gap_end); |
| 8476 | DCHECK_ALIGNED(gap_start, 4u); |
| 8477 | DCHECK_ALIGNED(gap_end, 8u); |
| 8478 | DCHECK_EQ(gap_start + 4u, gap_end); |
| 8479 | DCHECK_EQ(gap4_offset_, kNoOffset); |
| 8480 | gap4_offset_ = gap_start; |
| 8481 | return; |
| 8482 | } |
| 8483 | |
| 8484 | DCHECK(false) << "Remaining gap: " << gap_start << " to " << gap_end |
| 8485 | << " after checking " << kGapsToCheck; |
| 8486 | } |
| 8487 | |
| 8488 | static constexpr uint32_t kNoOffset = static_cast<uint32_t>(-1); |
| 8489 | |
| 8490 | uint32_t gap4_offset_ = kNoOffset; |
| 8491 | uint32_t gap2_offset_ = kNoOffset; |
| 8492 | uint32_t gap1_offset_ = kNoOffset; |
| 8493 | }; |
| 8494 | |
| 8495 | template <size_t kSize> |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8496 | ALWAYS_INLINE |
| 8497 | MemberOffset ClassLinker::LinkFieldsHelper::AssignFieldOffset(ArtField* field, |
| 8498 | MemberOffset field_offset) { |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8499 | DCHECK_ALIGNED(field_offset.Uint32Value(), kSize); |
| 8500 | DCHECK_EQ(Primitive::ComponentSize(field->GetTypeAsPrimitiveType()), kSize); |
| 8501 | field->SetOffset(field_offset); |
| 8502 | return MemberOffset(field_offset.Uint32Value() + kSize); |
| 8503 | } |
| 8504 | |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8505 | bool ClassLinker::LinkFieldsHelper::LinkFields(ClassLinker* class_linker, |
| 8506 | Thread* self, |
| 8507 | Handle<mirror::Class> klass, |
| 8508 | bool is_static, |
| 8509 | size_t* class_size) { |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 8510 | self->AllowThreadSuspension(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8511 | const size_t num_fields = is_static ? klass->NumStaticFields() : klass->NumInstanceFields(); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 8512 | LengthPrefixedArray<ArtField>* const fields = is_static ? klass->GetSFieldsPtr() : |
| 8513 | klass->GetIFieldsPtr(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8514 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 8515 | // Initialize field_offset |
Brian Carlstrom | 693267a | 2011-09-06 09:25:34 -0700 | [diff] [blame] | 8516 | MemberOffset field_offset(0); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 8517 | if (is_static) { |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8518 | field_offset = klass->GetFirstReferenceStaticFieldOffsetDuringLinking( |
| 8519 | class_linker->GetImagePointerSize()); |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 8520 | } else { |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 8521 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 8522 | if (super_class != nullptr) { |
Brian Carlstrom | f363283 | 2014-05-20 15:36:53 -0700 | [diff] [blame] | 8523 | CHECK(super_class->IsResolved()) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 8524 | << klass->PrettyClass() << " " << super_class->PrettyClass(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8525 | field_offset = MemberOffset(super_class->GetObjectSize()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8526 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8527 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8528 | |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 8529 | CHECK_EQ(num_fields == 0, fields == nullptr) << klass->PrettyClass(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8530 | |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 8531 | // we want a relatively stable order so that adding new fields |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 8532 | // minimizes disruption of C++ version such as Class and Method. |
Alex Light | e64300b | 2015-12-15 15:02:47 -0800 | [diff] [blame] | 8533 | // |
| 8534 | // The overall sort order order is: |
| 8535 | // 1) All object reference fields, sorted alphabetically. |
| 8536 | // 2) All java long (64-bit) integer fields, sorted alphabetically. |
| 8537 | // 3) All java double (64-bit) floating point fields, sorted alphabetically. |
| 8538 | // 4) All java int (32-bit) integer fields, sorted alphabetically. |
| 8539 | // 5) All java float (32-bit) floating point fields, sorted alphabetically. |
| 8540 | // 6) All java char (16-bit) integer fields, sorted alphabetically. |
| 8541 | // 7) All java short (16-bit) integer fields, sorted alphabetically. |
| 8542 | // 8) All java boolean (8-bit) integer fields, sorted alphabetically. |
| 8543 | // 9) All java byte (8-bit) integer fields, sorted alphabetically. |
| 8544 | // |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8545 | // (References are first to increase the chance of reference visiting |
| 8546 | // being able to take a fast path using a bitmap of references at the |
| 8547 | // start of the object, see `Class::reference_instance_offsets_`.) |
| 8548 | // |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8549 | // Once the fields are sorted in this order we will attempt to fill any gaps |
| 8550 | // that might be present in the memory layout of the structure. |
| 8551 | // Note that we shall not fill gaps between the superclass fields. |
| 8552 | |
| 8553 | // Collect fields and their "type order index" (see numbered points above). |
Mathieu Chartier | 2d5f39e | 2014-09-19 17:52:37 -0700 | [diff] [blame] | 8554 | const char* old_no_suspend_cause = self->StartAssertNoThreadSuspension( |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8555 | "Using plain ArtField references"); |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8556 | constexpr size_t kStackBufferEntries = 64; // Avoid allocations for small number of fields. |
| 8557 | FieldTypeOrderAndIndex stack_buffer[kStackBufferEntries]; |
| 8558 | std::vector<FieldTypeOrderAndIndex> heap_buffer; |
| 8559 | ArrayRef<FieldTypeOrderAndIndex> sorted_fields; |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8560 | if (num_fields <= kStackBufferEntries) { |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8561 | sorted_fields = ArrayRef<FieldTypeOrderAndIndex>(stack_buffer, num_fields); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8562 | } else { |
| 8563 | heap_buffer.resize(num_fields); |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8564 | sorted_fields = ArrayRef<FieldTypeOrderAndIndex>(heap_buffer); |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 8565 | } |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 8566 | size_t num_reference_fields = 0; |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8567 | size_t primitive_fields_start = num_fields; |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8568 | DCHECK_LE(num_fields, 1u << 16); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8569 | for (size_t i = 0; i != num_fields; ++i) { |
| 8570 | ArtField* field = &fields->At(i); |
| 8571 | const char* descriptor = field->GetTypeDescriptor(); |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8572 | FieldTypeOrder field_type_order = FieldTypeOrderFromFirstDescriptorCharacter(descriptor[0]); |
| 8573 | uint16_t field_index = dchecked_integral_cast<uint16_t>(i); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8574 | // Insert references to the start, other fields to the end. |
| 8575 | DCHECK_LT(num_reference_fields, primitive_fields_start); |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8576 | if (field_type_order == FieldTypeOrder::kReference) { |
| 8577 | sorted_fields[num_reference_fields] = { field_type_order, field_index }; |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8578 | ++num_reference_fields; |
| 8579 | } else { |
| 8580 | --primitive_fields_start; |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8581 | sorted_fields[primitive_fields_start] = { field_type_order, field_index }; |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8582 | } |
| 8583 | } |
| 8584 | DCHECK_EQ(num_reference_fields, primitive_fields_start); |
Fred Shih | 381e4ca | 2014-08-25 17:24:27 -0700 | [diff] [blame] | 8585 | |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8586 | // Reference fields are already sorted by field index (and dex field index). |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8587 | DCHECK(std::is_sorted( |
| 8588 | sorted_fields.begin(), |
| 8589 | sorted_fields.begin() + num_reference_fields, |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8590 | [fields](const auto& lhs, const auto& rhs) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 8591 | ArtField* lhs_field = &fields->At(lhs.field_index); |
| 8592 | ArtField* rhs_field = &fields->At(rhs.field_index); |
| 8593 | CHECK_EQ(lhs_field->GetTypeAsPrimitiveType(), Primitive::kPrimNot); |
| 8594 | CHECK_EQ(rhs_field->GetTypeAsPrimitiveType(), Primitive::kPrimNot); |
| 8595 | CHECK_EQ(lhs_field->GetDexFieldIndex() < rhs_field->GetDexFieldIndex(), |
| 8596 | lhs.field_index < rhs.field_index); |
| 8597 | return lhs_field->GetDexFieldIndex() < rhs_field->GetDexFieldIndex(); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8598 | })); |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8599 | // 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] | 8600 | DCHECK(std::is_sorted( |
| 8601 | sorted_fields.begin() + primitive_fields_start, |
| 8602 | sorted_fields.end(), |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8603 | [fields](const auto& lhs, const auto& rhs) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 8604 | ArtField* lhs_field = &fields->At(lhs.field_index); |
| 8605 | ArtField* rhs_field = &fields->At(rhs.field_index); |
| 8606 | CHECK_NE(lhs_field->GetTypeAsPrimitiveType(), Primitive::kPrimNot); |
| 8607 | CHECK_NE(rhs_field->GetTypeAsPrimitiveType(), Primitive::kPrimNot); |
| 8608 | CHECK_EQ(lhs_field->GetDexFieldIndex() > rhs_field->GetDexFieldIndex(), |
| 8609 | lhs.field_index > rhs.field_index); |
| 8610 | return lhs.field_index > rhs.field_index; |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8611 | })); |
| 8612 | // Sort the primitive fields by the field type order, then field index. |
| 8613 | std::sort(sorted_fields.begin() + primitive_fields_start, |
| 8614 | sorted_fields.end(), |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8615 | [](const auto& lhs, const auto& rhs) { |
| 8616 | if (lhs.field_type_order != rhs.field_type_order) { |
| 8617 | return lhs.field_type_order < rhs.field_type_order; |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8618 | } else { |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8619 | return lhs.field_index < rhs.field_index; |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8620 | } |
| 8621 | }); |
| 8622 | // Primitive fields are now sorted by field size (descending), then type, then field index. |
| 8623 | DCHECK(std::is_sorted( |
| 8624 | sorted_fields.begin() + primitive_fields_start, |
| 8625 | sorted_fields.end(), |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8626 | [fields](const auto& lhs, const auto& rhs) REQUIRES_SHARED(Locks::mutator_lock_) { |
| 8627 | ArtField* lhs_field = &fields->At(lhs.field_index); |
| 8628 | ArtField* rhs_field = &fields->At(rhs.field_index); |
| 8629 | Primitive::Type lhs_type = lhs_field->GetTypeAsPrimitiveType(); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8630 | CHECK_NE(lhs_type, Primitive::kPrimNot); |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8631 | Primitive::Type rhs_type = rhs_field->GetTypeAsPrimitiveType(); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8632 | CHECK_NE(rhs_type, Primitive::kPrimNot); |
| 8633 | if (lhs_type != rhs_type) { |
| 8634 | size_t lhs_size = Primitive::ComponentSize(lhs_type); |
| 8635 | size_t rhs_size = Primitive::ComponentSize(rhs_type); |
| 8636 | return (lhs_size != rhs_size) ? (lhs_size > rhs_size) : (lhs_type < rhs_type); |
| 8637 | } else { |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8638 | return lhs_field->GetDexFieldIndex() < rhs_field->GetDexFieldIndex(); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8639 | } |
| 8640 | })); |
| 8641 | |
| 8642 | // Process reference fields. |
| 8643 | FieldGaps field_gaps; |
| 8644 | size_t index = 0u; |
| 8645 | if (num_reference_fields != 0u) { |
| 8646 | constexpr size_t kReferenceSize = sizeof(mirror::HeapReference<mirror::Object>); |
| 8647 | field_offset = field_gaps.AlignFieldOffset<kReferenceSize>(field_offset); |
| 8648 | for (; index != num_reference_fields; ++index) { |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8649 | ArtField* field = &fields->At(sorted_fields[index].field_index); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8650 | field_offset = AssignFieldOffset<kReferenceSize>(field, field_offset); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 8651 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 8652 | } |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8653 | // Process 64-bit fields. |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8654 | if (index != num_fields && |
| 8655 | sorted_fields[index].field_type_order <= FieldTypeOrder::kLast64BitType) { |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8656 | field_offset = field_gaps.AlignFieldOffset<8u>(field_offset); |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8657 | while (index != num_fields && |
| 8658 | sorted_fields[index].field_type_order <= FieldTypeOrder::kLast64BitType) { |
| 8659 | ArtField* field = &fields->At(sorted_fields[index].field_index); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8660 | field_offset = AssignFieldOffset<8u>(field, field_offset); |
| 8661 | ++index; |
Mathieu Chartier | 5565029 | 2020-09-14 12:21:04 -0700 | [diff] [blame] | 8662 | } |
Mathieu Chartier | 5565029 | 2020-09-14 12:21:04 -0700 | [diff] [blame] | 8663 | } |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8664 | // Process 32-bit fields. |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8665 | if (index != num_fields && |
| 8666 | sorted_fields[index].field_type_order <= FieldTypeOrder::kLast32BitType) { |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8667 | field_offset = field_gaps.AlignFieldOffset<4u>(field_offset); |
| 8668 | if (field_gaps.HasGap<4u>()) { |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8669 | ArtField* field = &fields->At(sorted_fields[index].field_index); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8670 | AssignFieldOffset<4u>(field, field_gaps.ReleaseGap<4u>()); // Ignore return value. |
| 8671 | ++index; |
| 8672 | DCHECK(!field_gaps.HasGap<4u>()); // There can be only one gap for a 32-bit field. |
| 8673 | } |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8674 | while (index != num_fields && |
| 8675 | sorted_fields[index].field_type_order <= FieldTypeOrder::kLast32BitType) { |
| 8676 | ArtField* field = &fields->At(sorted_fields[index].field_index); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8677 | field_offset = AssignFieldOffset<4u>(field, field_offset); |
| 8678 | ++index; |
| 8679 | } |
| 8680 | } |
| 8681 | // Process 16-bit fields. |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8682 | if (index != num_fields && |
| 8683 | sorted_fields[index].field_type_order <= FieldTypeOrder::kLast16BitType) { |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8684 | field_offset = field_gaps.AlignFieldOffset<2u>(field_offset); |
| 8685 | while (index != num_fields && |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8686 | sorted_fields[index].field_type_order <= FieldTypeOrder::kLast16BitType && |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8687 | field_gaps.HasGap<2u>()) { |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8688 | ArtField* field = &fields->At(sorted_fields[index].field_index); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8689 | AssignFieldOffset<2u>(field, field_gaps.ReleaseGap<2u>()); // Ignore return value. |
| 8690 | ++index; |
| 8691 | } |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8692 | while (index != num_fields && |
| 8693 | sorted_fields[index].field_type_order <= FieldTypeOrder::kLast16BitType) { |
| 8694 | ArtField* field = &fields->At(sorted_fields[index].field_index); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8695 | field_offset = AssignFieldOffset<2u>(field, field_offset); |
| 8696 | ++index; |
| 8697 | } |
| 8698 | } |
| 8699 | // Process 8-bit fields. |
| 8700 | for (; index != num_fields && field_gaps.HasGap<1u>(); ++index) { |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8701 | ArtField* field = &fields->At(sorted_fields[index].field_index); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8702 | AssignFieldOffset<1u>(field, field_gaps.ReleaseGap<1u>()); // Ignore return value. |
| 8703 | } |
| 8704 | for (; index != num_fields; ++index) { |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8705 | ArtField* field = &fields->At(sorted_fields[index].field_index); |
Vladimir Marko | c7993d5 | 2021-01-27 15:20:56 +0000 | [diff] [blame] | 8706 | field_offset = AssignFieldOffset<1u>(field, field_offset); |
| 8707 | } |
| 8708 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 8709 | self->EndAssertNoThreadSuspension(old_no_suspend_cause); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 8710 | |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 8711 | // 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] | 8712 | DCHECK(!class_linker->init_done_ || !klass->DescriptorEquals("Ljava/lang/ref/Reference;")); |
| 8713 | if (!is_static && |
| 8714 | UNLIKELY(!class_linker->init_done_) && |
| 8715 | klass->DescriptorEquals("Ljava/lang/ref/Reference;")) { |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 8716 | // We know there are no non-reference fields in the Reference classes, and we know |
| 8717 | // that 'referent' is alphabetically last, so this is easy... |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 8718 | CHECK_EQ(num_reference_fields, num_fields) << klass->PrettyClass(); |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 8719 | CHECK_STREQ(fields->At(num_fields - 1).GetName(), "referent") |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 8720 | << klass->PrettyClass(); |
Elliott Hughes | adb460d | 2011-10-05 17:02:34 -0700 | [diff] [blame] | 8721 | --num_reference_fields; |
| 8722 | } |
| 8723 | |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 8724 | size_t size = field_offset.Uint32Value(); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8725 | // Update klass |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 8726 | if (is_static) { |
| 8727 | klass->SetNumReferenceStaticFields(num_reference_fields); |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 8728 | *class_size = size; |
Brian Carlstrom | 3320cf4 | 2011-10-04 14:58:28 -0700 | [diff] [blame] | 8729 | } else { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8730 | klass->SetNumReferenceInstanceFields(num_reference_fields); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 8731 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 8732 | if (num_reference_fields == 0 || super_class == nullptr) { |
| 8733 | // 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] | 8734 | // super_class is null iff the class is java.lang.Object. |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 8735 | if (super_class == nullptr || |
| 8736 | (super_class->GetClassFlags() & mirror::kClassFlagNoReferenceFields) != 0) { |
| 8737 | klass->SetClassFlags(klass->GetClassFlags() | mirror::kClassFlagNoReferenceFields); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 8738 | } |
| 8739 | } |
| 8740 | if (kIsDebugBuild) { |
| 8741 | DCHECK_EQ(super_class == nullptr, klass->DescriptorEquals("Ljava/lang/Object;")); |
| 8742 | size_t total_reference_instance_fields = 0; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 8743 | ObjPtr<mirror::Class> cur_super = klass.Get(); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 8744 | while (cur_super != nullptr) { |
| 8745 | total_reference_instance_fields += cur_super->NumReferenceInstanceFieldsDuringLinking(); |
| 8746 | cur_super = cur_super->GetSuperClass(); |
| 8747 | } |
| 8748 | if (super_class == nullptr) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 8749 | CHECK_EQ(total_reference_instance_fields, 1u) << klass->PrettyDescriptor(); |
Mathieu Chartier | 66c2d2d | 2015-08-25 14:32:32 -0700 | [diff] [blame] | 8750 | } else { |
| 8751 | // Check that there is at least num_reference_fields other than Object.class. |
| 8752 | CHECK_GE(total_reference_instance_fields, 1u + num_reference_fields) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 8753 | << klass->PrettyClass(); |
Mathieu Chartier | 52a7f5c | 2015-08-18 18:35:52 -0700 | [diff] [blame] | 8754 | } |
| 8755 | } |
Brian Carlstrom | dbc0525 | 2011-09-09 01:59:59 -0700 | [diff] [blame] | 8756 | if (!klass->IsVariableSize()) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8757 | std::string temp; |
| 8758 | DCHECK_GE(size, sizeof(mirror::Object)) << klass->GetDescriptor(&temp); |
| 8759 | size_t previous_size = klass->GetObjectSize(); |
| 8760 | if (previous_size != 0) { |
| 8761 | // Make sure that we didn't originally have an incorrect size. |
| 8762 | CHECK_EQ(previous_size, size) << klass->GetDescriptor(&temp); |
Mathieu Chartier | 79b4f38 | 2013-10-23 15:21:37 -0700 | [diff] [blame] | 8763 | } |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 8764 | klass->SetObjectSize(size); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8765 | } |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8766 | } |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 8767 | |
| 8768 | if (kIsDebugBuild) { |
| 8769 | // Make sure that the fields array is ordered by name but all reference |
| 8770 | // offsets are at the beginning as far as alignment allows. |
| 8771 | MemberOffset start_ref_offset = is_static |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8772 | ? klass->GetFirstReferenceStaticFieldOffsetDuringLinking(class_linker->image_pointer_size_) |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 8773 | : klass->GetFirstReferenceInstanceFieldOffset(); |
| 8774 | MemberOffset end_ref_offset(start_ref_offset.Uint32Value() + |
| 8775 | num_reference_fields * |
| 8776 | sizeof(mirror::HeapReference<mirror::Object>)); |
| 8777 | MemberOffset current_ref_offset = start_ref_offset; |
| 8778 | for (size_t i = 0; i < num_fields; i++) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 8779 | ArtField* field = &fields->At(i); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 8780 | VLOG(class_linker) << "LinkFields: " << (is_static ? "static" : "instance") |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 8781 | << " class=" << klass->PrettyClass() << " field=" << field->PrettyField() |
| 8782 | << " offset=" << field->GetOffsetDuringLinking(); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 8783 | if (i != 0) { |
Mathieu Chartier | 54d220e | 2015-07-30 16:20:06 -0700 | [diff] [blame] | 8784 | ArtField* const prev_field = &fields->At(i - 1); |
Vladimir Marko | 7a7c1db | 2014-11-17 15:13:34 +0000 | [diff] [blame] | 8785 | // NOTE: The field names can be the same. This is not possible in the Java language |
| 8786 | // 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] | 8787 | DCHECK_LE(strcmp(prev_field->GetName(), field->GetName()), 0); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 8788 | } |
| 8789 | Primitive::Type type = field->GetTypeAsPrimitiveType(); |
| 8790 | bool is_primitive = type != Primitive::kPrimNot; |
| 8791 | if (klass->DescriptorEquals("Ljava/lang/ref/Reference;") && |
| 8792 | strcmp("referent", field->GetName()) == 0) { |
| 8793 | is_primitive = true; // We lied above, so we have to expect a lie here. |
| 8794 | } |
| 8795 | MemberOffset offset = field->GetOffsetDuringLinking(); |
| 8796 | if (is_primitive) { |
| 8797 | if (offset.Uint32Value() < end_ref_offset.Uint32Value()) { |
| 8798 | // Shuffled before references. |
| 8799 | size_t type_size = Primitive::ComponentSize(type); |
| 8800 | CHECK_LT(type_size, sizeof(mirror::HeapReference<mirror::Object>)); |
| 8801 | CHECK_LT(offset.Uint32Value(), start_ref_offset.Uint32Value()); |
| 8802 | CHECK_LE(offset.Uint32Value() + type_size, start_ref_offset.Uint32Value()); |
| 8803 | CHECK(!IsAligned<sizeof(mirror::HeapReference<mirror::Object>)>(offset.Uint32Value())); |
| 8804 | } |
| 8805 | } else { |
| 8806 | CHECK_EQ(current_ref_offset.Uint32Value(), offset.Uint32Value()); |
| 8807 | current_ref_offset = MemberOffset(current_ref_offset.Uint32Value() + |
| 8808 | sizeof(mirror::HeapReference<mirror::Object>)); |
| 8809 | } |
| 8810 | } |
| 8811 | CHECK_EQ(current_ref_offset.Uint32Value(), end_ref_offset.Uint32Value()); |
| 8812 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 8813 | return true; |
| 8814 | } |
| 8815 | |
Vladimir Marko | 42bee50 | 2021-01-28 14:58:35 +0000 | [diff] [blame] | 8816 | bool ClassLinker::LinkInstanceFields(Thread* self, Handle<mirror::Class> klass) { |
| 8817 | CHECK(klass != nullptr); |
| 8818 | return LinkFieldsHelper::LinkFields(this, self, klass, false, nullptr); |
| 8819 | } |
| 8820 | |
| 8821 | bool ClassLinker::LinkStaticFields(Thread* self, Handle<mirror::Class> klass, size_t* class_size) { |
| 8822 | CHECK(klass != nullptr); |
| 8823 | return LinkFieldsHelper::LinkFields(this, self, klass, true, class_size); |
| 8824 | } |
| 8825 | |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 8826 | // Set the bitmap of reference instance field offsets. |
Andreas Gampe | 5a4b8a2 | 2014-09-11 08:30:08 -0700 | [diff] [blame] | 8827 | void ClassLinker::CreateReferenceInstanceOffsets(Handle<mirror::Class> klass) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8828 | uint32_t reference_offsets = 0; |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 8829 | ObjPtr<mirror::Class> super_class = klass->GetSuperClass(); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 8830 | // 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] | 8831 | if (super_class != nullptr) { |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8832 | reference_offsets = super_class->GetReferenceInstanceOffsets(); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 8833 | // Compute reference offsets unless our superclass overflowed. |
| 8834 | if (reference_offsets != mirror::Class::kClassWalkSuper) { |
| 8835 | size_t num_reference_fields = klass->NumReferenceInstanceFieldsDuringLinking(); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 8836 | if (num_reference_fields != 0u) { |
| 8837 | // All of the fields that contain object references are guaranteed be grouped in memory |
| 8838 | // starting at an appropriately aligned address after super class object data. |
| 8839 | uint32_t start_offset = RoundUp(super_class->GetObjectSize(), |
| 8840 | sizeof(mirror::HeapReference<mirror::Object>)); |
| 8841 | uint32_t start_bit = (start_offset - mirror::kObjectHeaderSize) / |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 8842 | sizeof(mirror::HeapReference<mirror::Object>); |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 8843 | if (start_bit + num_reference_fields > 32) { |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 8844 | reference_offsets = mirror::Class::kClassWalkSuper; |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 8845 | } else { |
Vladimir Marko | 76649e8 | 2014-11-10 18:32:59 +0000 | [diff] [blame] | 8846 | reference_offsets |= (0xffffffffu << start_bit) & |
| 8847 | (0xffffffffu >> (32 - (start_bit + num_reference_fields))); |
Ian Rogers | cdc1aaf | 2014-10-09 13:21:38 -0700 | [diff] [blame] | 8848 | } |
| 8849 | } |
Brian Carlstrom | 4873d46 | 2011-08-21 15:23:39 -0700 | [diff] [blame] | 8850 | } |
| 8851 | } |
Mingyao Yang | faff0f0 | 2014-09-10 12:03:22 -0700 | [diff] [blame] | 8852 | klass->SetReferenceInstanceOffsets(reference_offsets); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 8853 | } |
| 8854 | |
Vladimir Marko | 18090d1 | 2018-06-01 16:53:12 +0100 | [diff] [blame] | 8855 | ObjPtr<mirror::String> ClassLinker::DoResolveString(dex::StringIndex string_idx, |
| 8856 | ObjPtr<mirror::DexCache> dex_cache) { |
| 8857 | StackHandleScope<1> hs(Thread::Current()); |
| 8858 | Handle<mirror::DexCache> h_dex_cache(hs.NewHandle(dex_cache)); |
| 8859 | return DoResolveString(string_idx, h_dex_cache); |
| 8860 | } |
| 8861 | |
| 8862 | ObjPtr<mirror::String> ClassLinker::DoResolveString(dex::StringIndex string_idx, |
| 8863 | Handle<mirror::DexCache> dex_cache) { |
Vladimir Marko | a64b52d | 2017-12-08 16:27:49 +0000 | [diff] [blame] | 8864 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 8865 | uint32_t utf16_length; |
| 8866 | const char* utf8_data = dex_file.StringDataAndUtf16LengthByIdx(string_idx, &utf16_length); |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 8867 | ObjPtr<mirror::String> string = intern_table_->InternStrong(utf16_length, utf8_data); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 8868 | if (string != nullptr) { |
| 8869 | dex_cache->SetResolvedString(string_idx, string); |
| 8870 | } |
Vladimir Marko | 28e012a | 2017-12-07 11:22:59 +0000 | [diff] [blame] | 8871 | return string; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 8872 | } |
| 8873 | |
Vladimir Marko | 18090d1 | 2018-06-01 16:53:12 +0100 | [diff] [blame] | 8874 | ObjPtr<mirror::String> ClassLinker::DoLookupString(dex::StringIndex string_idx, |
| 8875 | ObjPtr<mirror::DexCache> dex_cache) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 8876 | DCHECK(dex_cache != nullptr); |
Vladimir Marko | a64b52d | 2017-12-08 16:27:49 +0000 | [diff] [blame] | 8877 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 8878 | uint32_t utf16_length; |
| 8879 | const char* utf8_data = dex_file.StringDataAndUtf16LengthByIdx(string_idx, &utf16_length); |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 8880 | ObjPtr<mirror::String> string = |
| 8881 | intern_table_->LookupStrong(Thread::Current(), utf16_length, utf8_data); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 8882 | if (string != nullptr) { |
| 8883 | dex_cache->SetResolvedString(string_idx, string); |
| 8884 | } |
Vladimir Marko | 28e012a | 2017-12-07 11:22:59 +0000 | [diff] [blame] | 8885 | return string; |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 8886 | } |
| 8887 | |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 8888 | ObjPtr<mirror::Class> ClassLinker::DoLookupResolvedType(dex::TypeIndex type_idx, |
Vladimir Marko | 09c5ca4 | 2018-05-31 15:15:31 +0100 | [diff] [blame] | 8889 | ObjPtr<mirror::Class> referrer) { |
| 8890 | return DoLookupResolvedType(type_idx, referrer->GetDexCache(), referrer->GetClassLoader()); |
| 8891 | } |
| 8892 | |
| 8893 | ObjPtr<mirror::Class> ClassLinker::DoLookupResolvedType(dex::TypeIndex type_idx, |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 8894 | ObjPtr<mirror::DexCache> dex_cache, |
| 8895 | ObjPtr<mirror::ClassLoader> class_loader) { |
| 8896 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
| 8897 | const char* descriptor = dex_file.StringByTypeIdx(type_idx); |
| 8898 | DCHECK_NE(*descriptor, '\0') << "descriptor is empty string"; |
| 8899 | ObjPtr<mirror::Class> type = nullptr; |
| 8900 | if (descriptor[1] == '\0') { |
| 8901 | // only the descriptors of primitive types should be 1 character long, also avoid class lookup |
| 8902 | // for primitive classes that aren't backed by dex files. |
Vladimir Marko | 9186b18 | 2018-11-06 14:55:54 +0000 | [diff] [blame] | 8903 | type = LookupPrimitiveClass(descriptor[0]); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 8904 | } else { |
| 8905 | Thread* const self = Thread::Current(); |
| 8906 | DCHECK(self != nullptr); |
| 8907 | const size_t hash = ComputeModifiedUtf8Hash(descriptor); |
| 8908 | // Find the class in the loaded classes table. |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 8909 | type = LookupClass(self, descriptor, hash, class_loader); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 8910 | } |
| 8911 | if (type != nullptr) { |
| 8912 | if (type->IsResolved()) { |
| 8913 | dex_cache->SetResolvedType(type_idx, type); |
Mathieu Chartier | b890130 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 8914 | } else { |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 8915 | type = nullptr; |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 8916 | } |
Mathieu Chartier | b890130 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 8917 | } |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 8918 | return type; |
Mathieu Chartier | b890130 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 8919 | } |
| 8920 | |
Andreas Gampe | b0625e0 | 2019-05-01 12:43:31 -0700 | [diff] [blame] | 8921 | template <typename RefType> |
| 8922 | ObjPtr<mirror::Class> ClassLinker::DoResolveType(dex::TypeIndex type_idx, RefType referrer) { |
Vladimir Marko | 09c5ca4 | 2018-05-31 15:15:31 +0100 | [diff] [blame] | 8923 | StackHandleScope<2> hs(Thread::Current()); |
| 8924 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache())); |
| 8925 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(referrer->GetClassLoader())); |
| 8926 | return DoResolveType(type_idx, dex_cache, class_loader); |
| 8927 | } |
| 8928 | |
Andreas Gampe | 4835d21 | 2018-11-21 14:55:10 -0800 | [diff] [blame] | 8929 | // Instantiate the above. |
| 8930 | template ObjPtr<mirror::Class> ClassLinker::DoResolveType(dex::TypeIndex type_idx, |
| 8931 | ArtField* referrer); |
| 8932 | template ObjPtr<mirror::Class> ClassLinker::DoResolveType(dex::TypeIndex type_idx, |
| 8933 | ArtMethod* referrer); |
| 8934 | template ObjPtr<mirror::Class> ClassLinker::DoResolveType(dex::TypeIndex type_idx, |
| 8935 | ObjPtr<mirror::Class> referrer); |
| 8936 | |
Vladimir Marko | 09c5ca4 | 2018-05-31 15:15:31 +0100 | [diff] [blame] | 8937 | ObjPtr<mirror::Class> ClassLinker::DoResolveType(dex::TypeIndex type_idx, |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 8938 | Handle<mirror::DexCache> dex_cache, |
| 8939 | Handle<mirror::ClassLoader> class_loader) { |
| 8940 | Thread* self = Thread::Current(); |
| 8941 | const char* descriptor = dex_cache->GetDexFile()->StringByTypeIdx(type_idx); |
| 8942 | ObjPtr<mirror::Class> resolved = FindClass(self, descriptor, class_loader); |
| 8943 | if (resolved != nullptr) { |
| 8944 | // TODO: we used to throw here if resolved's class loader was not the |
| 8945 | // boot class loader. This was to permit different classes with the |
| 8946 | // same name to be loaded simultaneously by different loaders |
| 8947 | dex_cache->SetResolvedType(type_idx, resolved); |
| 8948 | } else { |
| 8949 | CHECK(self->IsExceptionPending()) |
| 8950 | << "Expected pending exception for failed resolution of: " << descriptor; |
| 8951 | // Convert a ClassNotFoundException to a NoClassDefFoundError. |
| 8952 | StackHandleScope<1> hs(self); |
| 8953 | Handle<mirror::Throwable> cause(hs.NewHandle(self->GetException())); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 8954 | if (cause->InstanceOf(GetClassRoot(ClassRoot::kJavaLangClassNotFoundException, this))) { |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 8955 | DCHECK(resolved == nullptr); // No Handle needed to preserve resolved. |
| 8956 | self->ClearException(); |
| 8957 | ThrowNoClassDefFoundError("Failed resolution of: %s", descriptor); |
| 8958 | self->GetException()->SetCause(cause.Get()); |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 8959 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 8960 | } |
Vladimir Marko | 72ab684 | 2017-01-20 19:32:50 +0000 | [diff] [blame] | 8961 | DCHECK((resolved == nullptr) || resolved->IsResolved()) |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 8962 | << resolved->PrettyDescriptor() << " " << resolved->GetStatus(); |
Vladimir Marko | 28e012a | 2017-12-07 11:22:59 +0000 | [diff] [blame] | 8963 | return resolved; |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 8964 | } |
| 8965 | |
Nicolas Geoffray | ea179f4 | 2018-02-08 22:30:18 +0000 | [diff] [blame] | 8966 | ArtMethod* ClassLinker::FindResolvedMethod(ObjPtr<mirror::Class> klass, |
| 8967 | ObjPtr<mirror::DexCache> dex_cache, |
| 8968 | ObjPtr<mirror::ClassLoader> class_loader, |
| 8969 | uint32_t method_idx) { |
| 8970 | // Search for the method using dex_cache and method_idx. The Class::Find*Method() |
| 8971 | // functions can optimize the search if the dex_cache is the same as the DexCache |
| 8972 | // of the class, with fall-back to name and signature search otherwise. |
| 8973 | ArtMethod* resolved = nullptr; |
| 8974 | if (klass->IsInterface()) { |
| 8975 | resolved = klass->FindInterfaceMethod(dex_cache, method_idx, image_pointer_size_); |
| 8976 | } else { |
| 8977 | resolved = klass->FindClassMethod(dex_cache, method_idx, image_pointer_size_); |
| 8978 | } |
| 8979 | DCHECK(resolved == nullptr || resolved->GetDeclaringClassUnchecked() != nullptr); |
David Brazdil | 8ce3bfa | 2018-03-12 18:01:18 +0000 | [diff] [blame] | 8980 | if (resolved != nullptr && |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 8981 | hiddenapi::ShouldDenyAccessToMember(resolved, |
| 8982 | hiddenapi::AccessContext(class_loader, dex_cache), |
| 8983 | hiddenapi::AccessMethod::kLinking)) { |
David Brazdil | 8ce3bfa | 2018-03-12 18:01:18 +0000 | [diff] [blame] | 8984 | resolved = nullptr; |
| 8985 | } |
Nicolas Geoffray | ea179f4 | 2018-02-08 22:30:18 +0000 | [diff] [blame] | 8986 | if (resolved != nullptr) { |
| 8987 | // In case of jmvti, the dex file gets verified before being registered, so first |
| 8988 | // check if it's registered before checking class tables. |
| 8989 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Nicolas Geoffray | befa309 | 2018-02-22 14:50:01 +0000 | [diff] [blame] | 8990 | DCHECK(!IsDexFileRegistered(Thread::Current(), dex_file) || |
| 8991 | FindClassTable(Thread::Current(), dex_cache) == ClassTableForClassLoader(class_loader)) |
Nicolas Geoffray | ea179f4 | 2018-02-08 22:30:18 +0000 | [diff] [blame] | 8992 | << "DexFile referrer: " << dex_file.GetLocation() |
| 8993 | << " ClassLoader: " << DescribeLoaders(class_loader, ""); |
| 8994 | // Be a good citizen and update the dex cache to speed subsequent calls. |
| 8995 | dex_cache->SetResolvedMethod(method_idx, resolved, image_pointer_size_); |
Nicolas Geoffray | befa309 | 2018-02-22 14:50:01 +0000 | [diff] [blame] | 8996 | // Disable the following invariant check as the verifier breaks it. b/73760543 |
| 8997 | // const DexFile::MethodId& method_id = dex_file.GetMethodId(method_idx); |
| 8998 | // DCHECK(LookupResolvedType(method_id.class_idx_, dex_cache, class_loader) != nullptr) |
| 8999 | // << "Method: " << resolved->PrettyMethod() << ", " |
| 9000 | // << "Class: " << klass->PrettyClass() << " (" << klass->GetStatus() << "), " |
| 9001 | // << "DexFile referrer: " << dex_file.GetLocation(); |
Nicolas Geoffray | ea179f4 | 2018-02-08 22:30:18 +0000 | [diff] [blame] | 9002 | } |
| 9003 | return resolved; |
| 9004 | } |
| 9005 | |
David Brazdil | 4525e0b | 2018-04-05 16:57:32 +0100 | [diff] [blame] | 9006 | // Returns true if `method` is either null or hidden. |
| 9007 | // Does not print any warnings if it is hidden. |
| 9008 | static bool CheckNoSuchMethod(ArtMethod* method, |
| 9009 | ObjPtr<mirror::DexCache> dex_cache, |
| 9010 | ObjPtr<mirror::ClassLoader> class_loader) |
| 9011 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 9012 | return method == nullptr || |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 9013 | hiddenapi::ShouldDenyAccessToMember(method, |
| 9014 | hiddenapi::AccessContext(class_loader, dex_cache), |
| 9015 | hiddenapi::AccessMethod::kNone); // no warnings |
David Brazdil | 4525e0b | 2018-04-05 16:57:32 +0100 | [diff] [blame] | 9016 | } |
| 9017 | |
| 9018 | ArtMethod* ClassLinker::FindIncompatibleMethod(ObjPtr<mirror::Class> klass, |
| 9019 | ObjPtr<mirror::DexCache> dex_cache, |
| 9020 | ObjPtr<mirror::ClassLoader> class_loader, |
| 9021 | uint32_t method_idx) { |
| 9022 | if (klass->IsInterface()) { |
| 9023 | ArtMethod* method = klass->FindClassMethod(dex_cache, method_idx, image_pointer_size_); |
| 9024 | return CheckNoSuchMethod(method, dex_cache, class_loader) ? nullptr : method; |
| 9025 | } else { |
| 9026 | // If there was an interface method with the same signature, we would have |
| 9027 | // found it in the "copied" methods. Only DCHECK that the interface method |
| 9028 | // really does not exist. |
| 9029 | if (kIsDebugBuild) { |
| 9030 | ArtMethod* method = |
| 9031 | klass->FindInterfaceMethod(dex_cache, method_idx, image_pointer_size_); |
| 9032 | DCHECK(CheckNoSuchMethod(method, dex_cache, class_loader)); |
| 9033 | } |
| 9034 | return nullptr; |
| 9035 | } |
| 9036 | } |
| 9037 | |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 9038 | template <ClassLinker::ResolveMode kResolveMode> |
Vladimir Marko | 8901119 | 2017-12-11 13:45:05 +0000 | [diff] [blame] | 9039 | ArtMethod* ClassLinker::ResolveMethod(uint32_t method_idx, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 9040 | Handle<mirror::DexCache> dex_cache, |
| 9041 | Handle<mirror::ClassLoader> class_loader, |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 9042 | ArtMethod* referrer, |
| 9043 | InvokeType type) { |
Nicolas Geoffray | 605c591 | 2020-04-08 15:12:39 +0100 | [diff] [blame] | 9044 | DCHECK(!Thread::Current()->IsExceptionPending()) << Thread::Current()->GetException()->Dump(); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 9045 | DCHECK(dex_cache != nullptr); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9046 | DCHECK(referrer == nullptr || !referrer->IsProxyMethod()); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 9047 | // Check for hit in the dex cache. |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9048 | PointerSize pointer_size = image_pointer_size_; |
| 9049 | ArtMethod* resolved = dex_cache->GetResolvedMethod(method_idx, pointer_size); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 9050 | Thread::PoisonObjectPointersIfDebug(); |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 9051 | DCHECK(resolved == nullptr || !resolved->IsRuntimeMethod()); |
| 9052 | bool valid_dex_cache_method = resolved != nullptr; |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9053 | if (kResolveMode == ResolveMode::kNoChecks && valid_dex_cache_method) { |
| 9054 | // We have a valid method from the DexCache and no checks to perform. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 9055 | DCHECK(resolved->GetDeclaringClassUnchecked() != nullptr) << resolved->GetDexMethodIndex(); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 9056 | return resolved; |
| 9057 | } |
Vladimir Marko | 8901119 | 2017-12-11 13:45:05 +0000 | [diff] [blame] | 9058 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9059 | const dex::MethodId& method_id = dex_file.GetMethodId(method_idx); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9060 | ObjPtr<mirror::Class> klass = nullptr; |
| 9061 | if (valid_dex_cache_method) { |
| 9062 | // We have a valid method from the DexCache but we need to perform ICCE and IAE checks. |
| 9063 | DCHECK(resolved->GetDeclaringClassUnchecked() != nullptr) << resolved->GetDexMethodIndex(); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 9064 | klass = LookupResolvedType(method_id.class_idx_, dex_cache.Get(), class_loader.Get()); |
Vladimir Marko | 6f1bd46 | 2017-12-06 17:45:03 +0000 | [diff] [blame] | 9065 | if (UNLIKELY(klass == nullptr)) { |
Nicolas Geoffray | befa309 | 2018-02-22 14:50:01 +0000 | [diff] [blame] | 9066 | // We normaly should not end up here. However the verifier currently doesn't guarantee |
| 9067 | // the invariant of having the klass in the class table. b/73760543 |
| 9068 | klass = ResolveType(method_id.class_idx_, dex_cache, class_loader); |
Nicolas Geoffray | 0761419 | 2020-04-28 17:31:31 +0100 | [diff] [blame] | 9069 | if (klass == nullptr) { |
| 9070 | // This can only happen if the current thread is not allowed to load |
| 9071 | // classes. |
| 9072 | DCHECK(!Thread::Current()->CanLoadClasses()); |
| 9073 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 9074 | return nullptr; |
| 9075 | } |
Vladimir Marko | 6f1bd46 | 2017-12-06 17:45:03 +0000 | [diff] [blame] | 9076 | } |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9077 | } else { |
| 9078 | // The method was not in the DexCache, resolve the declaring class. |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 9079 | klass = ResolveType(method_id.class_idx_, dex_cache, class_loader); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9080 | if (klass == nullptr) { |
| 9081 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 9082 | return nullptr; |
| 9083 | } |
| 9084 | } |
| 9085 | |
| 9086 | // Check if the invoke type matches the class type. |
| 9087 | if (kResolveMode == ResolveMode::kCheckICCEAndIAE && |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 9088 | CheckInvokeClassMismatch</* kThrow= */ true>( |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9089 | dex_cache.Get(), type, [klass]() { return klass; })) { |
Elliott Hughes | cc5f9a9 | 2011-09-28 19:17:29 -0700 | [diff] [blame] | 9090 | DCHECK(Thread::Current()->IsExceptionPending()); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 9091 | return nullptr; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 9092 | } |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9093 | |
| 9094 | if (!valid_dex_cache_method) { |
Nicolas Geoffray | ea179f4 | 2018-02-08 22:30:18 +0000 | [diff] [blame] | 9095 | resolved = FindResolvedMethod(klass, dex_cache.Get(), class_loader.Get(), method_idx); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 9096 | } |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9097 | |
| 9098 | // Note: We can check for IllegalAccessError only if we have a referrer. |
| 9099 | if (kResolveMode == ResolveMode::kCheckICCEAndIAE && resolved != nullptr && referrer != nullptr) { |
| 9100 | ObjPtr<mirror::Class> methods_class = resolved->GetDeclaringClass(); |
| 9101 | ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass(); |
| 9102 | if (!referring_class->CheckResolvedMethodAccess(methods_class, |
| 9103 | resolved, |
| 9104 | dex_cache.Get(), |
| 9105 | method_idx, |
| 9106 | type)) { |
| 9107 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 9108 | return nullptr; |
| 9109 | } |
| 9110 | } |
| 9111 | |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 9112 | // If we found a method, check for incompatible class changes. |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9113 | if (LIKELY(resolved != nullptr) && |
| 9114 | LIKELY(kResolveMode == ResolveMode::kNoChecks || |
| 9115 | !resolved->CheckIncompatibleClassChange(type))) { |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 9116 | return resolved; |
| 9117 | } else { |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9118 | // If we had a method, or if we can find one with another lookup type, |
| 9119 | // it's an incompatible-class-change error. |
| 9120 | if (resolved == nullptr) { |
David Brazdil | 4525e0b | 2018-04-05 16:57:32 +0100 | [diff] [blame] | 9121 | resolved = FindIncompatibleMethod(klass, dex_cache.Get(), class_loader.Get(), method_idx); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9122 | } |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 9123 | if (resolved != nullptr) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 9124 | ThrowIncompatibleClassChangeError(type, resolved->GetInvokeType(), resolved, referrer); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 9125 | } else { |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9126 | // We failed to find the method (using all lookup types), so throw a NoSuchMethodError. |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 9127 | const char* name = dex_file.StringDataByIdx(method_id.name_idx_); |
| 9128 | const Signature signature = dex_file.GetMethodSignature(method_id); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9129 | ThrowNoSuchMethodError(type, klass, name, signature); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 9130 | } |
Ian Rogers | e0a02da | 2014-12-02 14:10:53 -0800 | [diff] [blame] | 9131 | Thread::Current()->AssertPendingException(); |
Andreas Gampe | eff0f5d | 2014-08-13 21:49:37 -0700 | [diff] [blame] | 9132 | return nullptr; |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 9133 | } |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 9134 | } |
| 9135 | |
Vladimir Marko | 8901119 | 2017-12-11 13:45:05 +0000 | [diff] [blame] | 9136 | ArtMethod* ClassLinker::ResolveMethodWithoutInvokeType(uint32_t method_idx, |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 9137 | Handle<mirror::DexCache> dex_cache, |
| 9138 | Handle<mirror::ClassLoader> class_loader) { |
| 9139 | ArtMethod* resolved = dex_cache->GetResolvedMethod(method_idx, image_pointer_size_); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 9140 | Thread::PoisonObjectPointersIfDebug(); |
Vladimir Marko | 07bfbac | 2017-07-06 14:55:02 +0100 | [diff] [blame] | 9141 | if (resolved != nullptr) { |
| 9142 | DCHECK(!resolved->IsRuntimeMethod()); |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 9143 | DCHECK(resolved->GetDeclaringClassUnchecked() != nullptr) << resolved->GetDexMethodIndex(); |
| 9144 | return resolved; |
| 9145 | } |
| 9146 | // Fail, get the declaring class. |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9147 | const dex::MethodId& method_id = dex_cache->GetDexFile()->GetMethodId(method_idx); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 9148 | 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] | 9149 | if (klass == nullptr) { |
| 9150 | Thread::Current()->AssertPendingException(); |
| 9151 | return nullptr; |
| 9152 | } |
| 9153 | if (klass->IsInterface()) { |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9154 | resolved = klass->FindInterfaceMethod(dex_cache.Get(), method_idx, image_pointer_size_); |
| 9155 | } else { |
| 9156 | resolved = klass->FindClassMethod(dex_cache.Get(), method_idx, image_pointer_size_); |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 9157 | } |
David Brazdil | 8ce3bfa | 2018-03-12 18:01:18 +0000 | [diff] [blame] | 9158 | if (resolved != nullptr && |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 9159 | hiddenapi::ShouldDenyAccessToMember( |
| 9160 | resolved, |
| 9161 | hiddenapi::AccessContext(class_loader.Get(), dex_cache.Get()), |
| 9162 | hiddenapi::AccessMethod::kLinking)) { |
David Brazdil | 8ce3bfa | 2018-03-12 18:01:18 +0000 | [diff] [blame] | 9163 | resolved = nullptr; |
| 9164 | } |
Jeff Hao | 13e748b | 2015-08-25 20:44:19 +0000 | [diff] [blame] | 9165 | return resolved; |
| 9166 | } |
| 9167 | |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 9168 | ArtField* ClassLinker::LookupResolvedField(uint32_t field_idx, |
| 9169 | ObjPtr<mirror::DexCache> dex_cache, |
| 9170 | ObjPtr<mirror::ClassLoader> class_loader, |
| 9171 | bool is_static) { |
| 9172 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9173 | const dex::FieldId& field_id = dex_file.GetFieldId(field_idx); |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 9174 | ObjPtr<mirror::Class> klass = dex_cache->GetResolvedType(field_id.class_idx_); |
| 9175 | if (klass == nullptr) { |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 9176 | klass = LookupResolvedType(field_id.class_idx_, dex_cache, class_loader); |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 9177 | } |
| 9178 | if (klass == nullptr) { |
| 9179 | // The class has not been resolved yet, so the field is also unresolved. |
| 9180 | return nullptr; |
| 9181 | } |
| 9182 | DCHECK(klass->IsResolved()); |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 9183 | |
David Brazdil | 1ab0fa8 | 2018-05-04 11:28:03 +0100 | [diff] [blame] | 9184 | return FindResolvedField(klass, dex_cache, class_loader, field_idx, is_static); |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 9185 | } |
| 9186 | |
Vladimir Marko | e11dd50 | 2017-12-08 14:09:45 +0000 | [diff] [blame] | 9187 | ArtField* ClassLinker::ResolveField(uint32_t field_idx, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 9188 | Handle<mirror::DexCache> dex_cache, |
Mathieu Chartier | c77f3ab | 2015-09-03 19:41:50 -0700 | [diff] [blame] | 9189 | Handle<mirror::ClassLoader> class_loader, |
| 9190 | bool is_static) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 9191 | DCHECK(dex_cache != nullptr); |
Nicolas Geoffray | f368882 | 2020-03-25 15:04:03 +0000 | [diff] [blame] | 9192 | DCHECK(!Thread::Current()->IsExceptionPending()) << Thread::Current()->GetException()->Dump(); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 9193 | ArtField* resolved = dex_cache->GetResolvedField(field_idx, image_pointer_size_); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 9194 | Thread::PoisonObjectPointersIfDebug(); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 9195 | if (resolved != nullptr) { |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 9196 | return resolved; |
| 9197 | } |
Vladimir Marko | e11dd50 | 2017-12-08 14:09:45 +0000 | [diff] [blame] | 9198 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9199 | const dex::FieldId& field_id = dex_file.GetFieldId(field_idx); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 9200 | 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] | 9201 | if (klass == nullptr) { |
Ian Rogers | 9f1ab12 | 2011-12-12 08:52:43 -0800 | [diff] [blame] | 9202 | DCHECK(Thread::Current()->IsExceptionPending()); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 9203 | return nullptr; |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 9204 | } |
| 9205 | |
David Brazdil | 1ab0fa8 | 2018-05-04 11:28:03 +0100 | [diff] [blame] | 9206 | resolved = FindResolvedField(klass, dex_cache.Get(), class_loader.Get(), field_idx, is_static); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 9207 | if (resolved == nullptr) { |
Ian Rogers | 7b0c5b4 | 2012-02-16 15:29:07 -0800 | [diff] [blame] | 9208 | const char* name = dex_file.GetFieldName(field_id); |
| 9209 | const char* type = dex_file.GetFieldTypeDescriptor(field_id); |
David Brazdil | 8ce3bfa | 2018-03-12 18:01:18 +0000 | [diff] [blame] | 9210 | ThrowNoSuchFieldError(is_static ? "static " : "instance ", klass, type, name); |
David Brazdil | 8ce3bfa | 2018-03-12 18:01:18 +0000 | [diff] [blame] | 9211 | } |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 9212 | return resolved; |
| 9213 | } |
| 9214 | |
Vladimir Marko | e11dd50 | 2017-12-08 14:09:45 +0000 | [diff] [blame] | 9215 | ArtField* ClassLinker::ResolveFieldJLS(uint32_t field_idx, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 9216 | Handle<mirror::DexCache> dex_cache, |
| 9217 | Handle<mirror::ClassLoader> class_loader) { |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 9218 | DCHECK(dex_cache != nullptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 9219 | ArtField* resolved = dex_cache->GetResolvedField(field_idx, image_pointer_size_); |
Mathieu Chartier | a59d9b2 | 2016-09-26 18:13:17 -0700 | [diff] [blame] | 9220 | Thread::PoisonObjectPointersIfDebug(); |
Andreas Gampe | 58a5af8 | 2014-07-31 16:23:49 -0700 | [diff] [blame] | 9221 | if (resolved != nullptr) { |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 9222 | return resolved; |
| 9223 | } |
Vladimir Marko | e11dd50 | 2017-12-08 14:09:45 +0000 | [diff] [blame] | 9224 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9225 | const dex::FieldId& field_id = dex_file.GetFieldId(field_idx); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 9226 | 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] | 9227 | if (klass == nullptr) { |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 9228 | DCHECK(Thread::Current()->IsExceptionPending()); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 9229 | return nullptr; |
Ian Rogers | b067ac2 | 2011-12-13 18:05:09 -0800 | [diff] [blame] | 9230 | } |
| 9231 | |
David Brazdil | 1ab0fa8 | 2018-05-04 11:28:03 +0100 | [diff] [blame] | 9232 | resolved = FindResolvedFieldJLS(klass, dex_cache.Get(), class_loader.Get(), field_idx); |
| 9233 | if (resolved == nullptr) { |
| 9234 | const char* name = dex_file.GetFieldName(field_id); |
| 9235 | const char* type = dex_file.GetFieldTypeDescriptor(field_id); |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 9236 | ThrowNoSuchFieldError("", klass, type, name); |
Brian Carlstrom | 9ea1cb1 | 2011-08-24 23:18:18 -0700 | [diff] [blame] | 9237 | } |
| 9238 | return resolved; |
Carl Shapiro | 5fafe2b | 2011-07-09 15:34:41 -0700 | [diff] [blame] | 9239 | } |
| 9240 | |
David Brazdil | 1ab0fa8 | 2018-05-04 11:28:03 +0100 | [diff] [blame] | 9241 | ArtField* ClassLinker::FindResolvedField(ObjPtr<mirror::Class> klass, |
| 9242 | ObjPtr<mirror::DexCache> dex_cache, |
| 9243 | ObjPtr<mirror::ClassLoader> class_loader, |
| 9244 | uint32_t field_idx, |
| 9245 | bool is_static) { |
| 9246 | ArtField* resolved = nullptr; |
| 9247 | Thread* self = is_static ? Thread::Current() : nullptr; |
| 9248 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
| 9249 | |
| 9250 | resolved = is_static ? mirror::Class::FindStaticField(self, klass, dex_cache, field_idx) |
| 9251 | : klass->FindInstanceField(dex_cache, field_idx); |
| 9252 | |
| 9253 | if (resolved == nullptr) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9254 | const dex::FieldId& field_id = dex_file.GetFieldId(field_idx); |
David Brazdil | 1ab0fa8 | 2018-05-04 11:28:03 +0100 | [diff] [blame] | 9255 | const char* name = dex_file.GetFieldName(field_id); |
| 9256 | const char* type = dex_file.GetFieldTypeDescriptor(field_id); |
| 9257 | resolved = is_static ? mirror::Class::FindStaticField(self, klass, name, type) |
| 9258 | : klass->FindInstanceField(name, type); |
| 9259 | } |
| 9260 | |
| 9261 | if (resolved != nullptr && |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 9262 | hiddenapi::ShouldDenyAccessToMember(resolved, |
| 9263 | hiddenapi::AccessContext(class_loader, dex_cache), |
| 9264 | hiddenapi::AccessMethod::kLinking)) { |
David Brazdil | 1ab0fa8 | 2018-05-04 11:28:03 +0100 | [diff] [blame] | 9265 | resolved = nullptr; |
| 9266 | } |
| 9267 | |
| 9268 | if (resolved != nullptr) { |
| 9269 | dex_cache->SetResolvedField(field_idx, resolved, image_pointer_size_); |
| 9270 | } |
| 9271 | |
| 9272 | return resolved; |
| 9273 | } |
| 9274 | |
| 9275 | ArtField* ClassLinker::FindResolvedFieldJLS(ObjPtr<mirror::Class> klass, |
| 9276 | ObjPtr<mirror::DexCache> dex_cache, |
| 9277 | ObjPtr<mirror::ClassLoader> class_loader, |
| 9278 | uint32_t field_idx) { |
| 9279 | ArtField* resolved = nullptr; |
| 9280 | Thread* self = Thread::Current(); |
| 9281 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9282 | const dex::FieldId& field_id = dex_file.GetFieldId(field_idx); |
David Brazdil | 1ab0fa8 | 2018-05-04 11:28:03 +0100 | [diff] [blame] | 9283 | |
| 9284 | const char* name = dex_file.GetFieldName(field_id); |
| 9285 | const char* type = dex_file.GetFieldTypeDescriptor(field_id); |
| 9286 | resolved = mirror::Class::FindField(self, klass, name, type); |
| 9287 | |
| 9288 | if (resolved != nullptr && |
David Brazdil | f50ac10 | 2018-10-17 18:00:06 +0100 | [diff] [blame] | 9289 | hiddenapi::ShouldDenyAccessToMember(resolved, |
| 9290 | hiddenapi::AccessContext(class_loader, dex_cache), |
| 9291 | hiddenapi::AccessMethod::kLinking)) { |
David Brazdil | 1ab0fa8 | 2018-05-04 11:28:03 +0100 | [diff] [blame] | 9292 | resolved = nullptr; |
| 9293 | } |
| 9294 | |
| 9295 | if (resolved != nullptr) { |
| 9296 | dex_cache->SetResolvedField(field_idx, resolved, image_pointer_size_); |
| 9297 | } |
| 9298 | |
| 9299 | return resolved; |
| 9300 | } |
| 9301 | |
Vladimir Marko | af94020 | 2017-12-08 15:01:18 +0000 | [diff] [blame] | 9302 | ObjPtr<mirror::MethodType> ClassLinker::ResolveMethodType( |
| 9303 | Thread* self, |
Orion Hodson | 06d10a7 | 2018-05-14 08:53:38 +0100 | [diff] [blame] | 9304 | dex::ProtoIndex proto_idx, |
Vladimir Marko | af94020 | 2017-12-08 15:01:18 +0000 | [diff] [blame] | 9305 | Handle<mirror::DexCache> dex_cache, |
| 9306 | Handle<mirror::ClassLoader> class_loader) { |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9307 | DCHECK(Runtime::Current()->IsMethodHandlesEnabled()); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 9308 | DCHECK(dex_cache != nullptr); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9309 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 9310 | ObjPtr<mirror::MethodType> resolved = dex_cache->GetResolvedMethodType(proto_idx); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9311 | if (resolved != nullptr) { |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 9312 | return resolved; |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9313 | } |
| 9314 | |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9315 | StackHandleScope<4> hs(self); |
| 9316 | |
| 9317 | // First resolve the return type. |
Vladimir Marko | af94020 | 2017-12-08 15:01:18 +0000 | [diff] [blame] | 9318 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9319 | const dex::ProtoId& proto_id = dex_file.GetProtoId(proto_idx); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9320 | Handle<mirror::Class> return_type(hs.NewHandle( |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 9321 | ResolveType(proto_id.return_type_idx_, dex_cache, class_loader))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 9322 | if (return_type == nullptr) { |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9323 | DCHECK(self->IsExceptionPending()); |
| 9324 | return nullptr; |
| 9325 | } |
| 9326 | |
| 9327 | // Then resolve the argument types. |
| 9328 | // |
| 9329 | // TODO: Is there a better way to figure out the number of method arguments |
| 9330 | // other than by looking at the shorty ? |
| 9331 | const size_t num_method_args = strlen(dex_file.StringDataByIdx(proto_id.shorty_idx_)) - 1; |
| 9332 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 9333 | ObjPtr<mirror::Class> array_of_class = GetClassRoot<mirror::ObjectArray<mirror::Class>>(this); |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9334 | Handle<mirror::ObjectArray<mirror::Class>> method_params(hs.NewHandle( |
| 9335 | mirror::ObjectArray<mirror::Class>::Alloc(self, array_of_class, num_method_args))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 9336 | if (method_params == nullptr) { |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9337 | DCHECK(self->IsExceptionPending()); |
| 9338 | return nullptr; |
| 9339 | } |
| 9340 | |
| 9341 | DexFileParameterIterator it(dex_file, proto_id); |
| 9342 | int32_t i = 0; |
| 9343 | MutableHandle<mirror::Class> param_class = hs.NewHandle<mirror::Class>(nullptr); |
| 9344 | for (; it.HasNext(); it.Next()) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 9345 | const dex::TypeIndex type_idx = it.GetTypeIdx(); |
Vladimir Marko | 666ee3d | 2017-12-11 18:37:36 +0000 | [diff] [blame] | 9346 | param_class.Assign(ResolveType(type_idx, dex_cache, class_loader)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 9347 | if (param_class == nullptr) { |
Narayan Kamath | 25352fc | 2016-08-03 12:46:58 +0100 | [diff] [blame] | 9348 | DCHECK(self->IsExceptionPending()); |
| 9349 | return nullptr; |
| 9350 | } |
| 9351 | |
| 9352 | method_params->Set(i++, param_class.Get()); |
| 9353 | } |
| 9354 | |
| 9355 | DCHECK(!it.HasNext()); |
| 9356 | |
| 9357 | Handle<mirror::MethodType> type = hs.NewHandle( |
| 9358 | mirror::MethodType::Create(self, return_type, method_params)); |
| 9359 | dex_cache->SetResolvedMethodType(proto_idx, type.Get()); |
| 9360 | |
| 9361 | return type.Get(); |
| 9362 | } |
| 9363 | |
Vladimir Marko | af94020 | 2017-12-08 15:01:18 +0000 | [diff] [blame] | 9364 | ObjPtr<mirror::MethodType> ClassLinker::ResolveMethodType(Thread* self, |
Orion Hodson | 06d10a7 | 2018-05-14 08:53:38 +0100 | [diff] [blame] | 9365 | dex::ProtoIndex proto_idx, |
Vladimir Marko | af94020 | 2017-12-08 15:01:18 +0000 | [diff] [blame] | 9366 | ArtMethod* referrer) { |
Orion Hodson | 2e59994 | 2017-09-22 16:17:41 +0100 | [diff] [blame] | 9367 | StackHandleScope<2> hs(self); |
Orion Hodson | 2e59994 | 2017-09-22 16:17:41 +0100 | [diff] [blame] | 9368 | Handle<mirror::DexCache> dex_cache(hs.NewHandle(referrer->GetDexCache())); |
| 9369 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle(referrer->GetClassLoader())); |
Vladimir Marko | af94020 | 2017-12-08 15:01:18 +0000 | [diff] [blame] | 9370 | return ResolveMethodType(self, proto_idx, dex_cache, class_loader); |
Orion Hodson | 2e59994 | 2017-09-22 16:17:41 +0100 | [diff] [blame] | 9371 | } |
| 9372 | |
Vladimir Marko | 5aead70 | 2019-03-27 11:00:36 +0000 | [diff] [blame] | 9373 | ObjPtr<mirror::MethodHandle> ClassLinker::ResolveMethodHandleForField( |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9374 | Thread* self, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9375 | const dex::MethodHandleItem& method_handle, |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9376 | ArtMethod* referrer) { |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9377 | DexFile::MethodHandleType handle_type = |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9378 | static_cast<DexFile::MethodHandleType>(method_handle.method_handle_type_); |
| 9379 | mirror::MethodHandle::Kind kind; |
Orion Hodson | fd7b2c2 | 2018-03-15 15:38:38 +0000 | [diff] [blame] | 9380 | bool is_put; |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9381 | bool is_static; |
| 9382 | int32_t num_params; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9383 | switch (handle_type) { |
| 9384 | case DexFile::MethodHandleType::kStaticPut: { |
Orion Hodson | 82b351f | 2017-07-05 14:34:25 +0100 | [diff] [blame] | 9385 | kind = mirror::MethodHandle::Kind::kStaticPut; |
Orion Hodson | fd7b2c2 | 2018-03-15 15:38:38 +0000 | [diff] [blame] | 9386 | is_put = true; |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9387 | is_static = true; |
| 9388 | num_params = 1; |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 9389 | break; |
| 9390 | } |
| 9391 | case DexFile::MethodHandleType::kStaticGet: { |
Orion Hodson | 82b351f | 2017-07-05 14:34:25 +0100 | [diff] [blame] | 9392 | kind = mirror::MethodHandle::Kind::kStaticGet; |
Orion Hodson | fd7b2c2 | 2018-03-15 15:38:38 +0000 | [diff] [blame] | 9393 | is_put = false; |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9394 | is_static = true; |
| 9395 | num_params = 0; |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 9396 | break; |
| 9397 | } |
| 9398 | case DexFile::MethodHandleType::kInstancePut: { |
Orion Hodson | 82b351f | 2017-07-05 14:34:25 +0100 | [diff] [blame] | 9399 | kind = mirror::MethodHandle::Kind::kInstancePut; |
Orion Hodson | fd7b2c2 | 2018-03-15 15:38:38 +0000 | [diff] [blame] | 9400 | is_put = true; |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9401 | is_static = false; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9402 | num_params = 2; |
| 9403 | break; |
| 9404 | } |
| 9405 | case DexFile::MethodHandleType::kInstanceGet: { |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9406 | kind = mirror::MethodHandle::Kind::kInstanceGet; |
Orion Hodson | fd7b2c2 | 2018-03-15 15:38:38 +0000 | [diff] [blame] | 9407 | is_put = false; |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9408 | is_static = false; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9409 | num_params = 1; |
| 9410 | break; |
| 9411 | } |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9412 | case DexFile::MethodHandleType::kInvokeStatic: |
Orion Hodson | 82b351f | 2017-07-05 14:34:25 +0100 | [diff] [blame] | 9413 | case DexFile::MethodHandleType::kInvokeInstance: |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9414 | case DexFile::MethodHandleType::kInvokeConstructor: |
Orion Hodson | 82b351f | 2017-07-05 14:34:25 +0100 | [diff] [blame] | 9415 | case DexFile::MethodHandleType::kInvokeDirect: |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9416 | case DexFile::MethodHandleType::kInvokeInterface: |
| 9417 | UNREACHABLE(); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9418 | } |
| 9419 | |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9420 | ArtField* target_field = |
| 9421 | ResolveField(method_handle.field_or_method_idx_, referrer, is_static); |
| 9422 | if (LIKELY(target_field != nullptr)) { |
| 9423 | ObjPtr<mirror::Class> target_class = target_field->GetDeclaringClass(); |
| 9424 | ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass(); |
| 9425 | if (UNLIKELY(!referring_class->CanAccessMember(target_class, target_field->GetAccessFlags()))) { |
| 9426 | ThrowIllegalAccessErrorField(referring_class, target_field); |
| 9427 | return nullptr; |
| 9428 | } |
Orion Hodson | fd7b2c2 | 2018-03-15 15:38:38 +0000 | [diff] [blame] | 9429 | if (UNLIKELY(is_put && target_field->IsFinal())) { |
| 9430 | ThrowIllegalAccessErrorField(referring_class, target_field); |
| 9431 | return nullptr; |
| 9432 | } |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9433 | } else { |
| 9434 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 9435 | return nullptr; |
| 9436 | } |
| 9437 | |
| 9438 | StackHandleScope<4> hs(self); |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 9439 | ObjPtr<mirror::Class> array_of_class = GetClassRoot<mirror::ObjectArray<mirror::Class>>(this); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9440 | Handle<mirror::ObjectArray<mirror::Class>> method_params(hs.NewHandle( |
| 9441 | mirror::ObjectArray<mirror::Class>::Alloc(self, array_of_class, num_params))); |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 9442 | if (UNLIKELY(method_params == nullptr)) { |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9443 | DCHECK(self->IsExceptionPending()); |
| 9444 | return nullptr; |
| 9445 | } |
| 9446 | |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9447 | Handle<mirror::Class> constructor_class; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9448 | Handle<mirror::Class> return_type; |
| 9449 | switch (handle_type) { |
| 9450 | case DexFile::MethodHandleType::kStaticPut: { |
Vladimir Marko | 4098a7a | 2017-11-06 16:00:51 +0000 | [diff] [blame] | 9451 | method_params->Set(0, target_field->ResolveType()); |
Vladimir Marko | 9186b18 | 2018-11-06 14:55:54 +0000 | [diff] [blame] | 9452 | return_type = hs.NewHandle(GetClassRoot(ClassRoot::kPrimitiveVoid, this)); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9453 | break; |
| 9454 | } |
| 9455 | case DexFile::MethodHandleType::kStaticGet: { |
Vladimir Marko | 4098a7a | 2017-11-06 16:00:51 +0000 | [diff] [blame] | 9456 | return_type = hs.NewHandle(target_field->ResolveType()); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9457 | break; |
| 9458 | } |
| 9459 | case DexFile::MethodHandleType::kInstancePut: { |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 9460 | method_params->Set(0, target_field->GetDeclaringClass()); |
Vladimir Marko | 4098a7a | 2017-11-06 16:00:51 +0000 | [diff] [blame] | 9461 | method_params->Set(1, target_field->ResolveType()); |
Vladimir Marko | 9186b18 | 2018-11-06 14:55:54 +0000 | [diff] [blame] | 9462 | return_type = hs.NewHandle(GetClassRoot(ClassRoot::kPrimitiveVoid, this)); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9463 | break; |
| 9464 | } |
| 9465 | case DexFile::MethodHandleType::kInstanceGet: { |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 9466 | method_params->Set(0, target_field->GetDeclaringClass()); |
Vladimir Marko | 4098a7a | 2017-11-06 16:00:51 +0000 | [diff] [blame] | 9467 | return_type = hs.NewHandle(target_field->ResolveType()); |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9468 | break; |
| 9469 | } |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9470 | case DexFile::MethodHandleType::kInvokeStatic: |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 9471 | case DexFile::MethodHandleType::kInvokeInstance: |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9472 | case DexFile::MethodHandleType::kInvokeConstructor: |
| 9473 | case DexFile::MethodHandleType::kInvokeDirect: |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 9474 | case DexFile::MethodHandleType::kInvokeInterface: |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9475 | UNREACHABLE(); |
| 9476 | } |
| 9477 | |
| 9478 | for (int32_t i = 0; i < num_params; ++i) { |
| 9479 | if (UNLIKELY(method_params->Get(i) == nullptr)) { |
| 9480 | DCHECK(self->IsExceptionPending()); |
| 9481 | return nullptr; |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9482 | } |
| 9483 | } |
| 9484 | |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9485 | if (UNLIKELY(return_type.IsNull())) { |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9486 | DCHECK(self->IsExceptionPending()); |
| 9487 | return nullptr; |
| 9488 | } |
| 9489 | |
| 9490 | Handle<mirror::MethodType> |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9491 | method_type(hs.NewHandle(mirror::MethodType::Create(self, return_type, method_params))); |
| 9492 | if (UNLIKELY(method_type.IsNull())) { |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9493 | DCHECK(self->IsExceptionPending()); |
| 9494 | return nullptr; |
| 9495 | } |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 9496 | |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9497 | uintptr_t target = reinterpret_cast<uintptr_t>(target_field); |
| 9498 | return mirror::MethodHandleImpl::Create(self, target, kind, method_type); |
| 9499 | } |
| 9500 | |
Vladimir Marko | 5aead70 | 2019-03-27 11:00:36 +0000 | [diff] [blame] | 9501 | ObjPtr<mirror::MethodHandle> ClassLinker::ResolveMethodHandleForMethod( |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9502 | Thread* self, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9503 | const dex::MethodHandleItem& method_handle, |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9504 | ArtMethod* referrer) { |
| 9505 | DexFile::MethodHandleType handle_type = |
| 9506 | static_cast<DexFile::MethodHandleType>(method_handle.method_handle_type_); |
| 9507 | mirror::MethodHandle::Kind kind; |
| 9508 | uint32_t receiver_count = 0; |
| 9509 | ArtMethod* target_method = nullptr; |
| 9510 | switch (handle_type) { |
| 9511 | case DexFile::MethodHandleType::kStaticPut: |
| 9512 | case DexFile::MethodHandleType::kStaticGet: |
| 9513 | case DexFile::MethodHandleType::kInstancePut: |
| 9514 | case DexFile::MethodHandleType::kInstanceGet: |
| 9515 | UNREACHABLE(); |
| 9516 | case DexFile::MethodHandleType::kInvokeStatic: { |
| 9517 | kind = mirror::MethodHandle::Kind::kInvokeStatic; |
| 9518 | receiver_count = 0; |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9519 | target_method = ResolveMethod<ResolveMode::kNoChecks>(self, |
| 9520 | method_handle.field_or_method_idx_, |
| 9521 | referrer, |
| 9522 | InvokeType::kStatic); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9523 | break; |
| 9524 | } |
| 9525 | case DexFile::MethodHandleType::kInvokeInstance: { |
| 9526 | kind = mirror::MethodHandle::Kind::kInvokeVirtual; |
| 9527 | receiver_count = 1; |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9528 | target_method = ResolveMethod<ResolveMode::kNoChecks>(self, |
| 9529 | method_handle.field_or_method_idx_, |
| 9530 | referrer, |
| 9531 | InvokeType::kVirtual); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9532 | break; |
| 9533 | } |
| 9534 | case DexFile::MethodHandleType::kInvokeConstructor: { |
| 9535 | // Constructors are currently implemented as a transform. They |
| 9536 | // are special cased later in this method. |
| 9537 | kind = mirror::MethodHandle::Kind::kInvokeTransform; |
| 9538 | receiver_count = 0; |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9539 | target_method = ResolveMethod<ResolveMode::kNoChecks>(self, |
| 9540 | method_handle.field_or_method_idx_, |
| 9541 | referrer, |
| 9542 | InvokeType::kDirect); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9543 | break; |
| 9544 | } |
| 9545 | case DexFile::MethodHandleType::kInvokeDirect: { |
| 9546 | kind = mirror::MethodHandle::Kind::kInvokeDirect; |
| 9547 | receiver_count = 1; |
| 9548 | StackHandleScope<2> hs(self); |
| 9549 | // A constant method handle with type kInvokeDirect can refer to |
| 9550 | // a method that is private or to a method in a super class. To |
| 9551 | // disambiguate the two options, we resolve the method ignoring |
| 9552 | // the invocation type to determine if the method is private. We |
| 9553 | // then resolve again specifying the intended invocation type to |
| 9554 | // force the appropriate checks. |
Vladimir Marko | 8901119 | 2017-12-11 13:45:05 +0000 | [diff] [blame] | 9555 | target_method = ResolveMethodWithoutInvokeType(method_handle.field_or_method_idx_, |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9556 | hs.NewHandle(referrer->GetDexCache()), |
| 9557 | hs.NewHandle(referrer->GetClassLoader())); |
| 9558 | if (UNLIKELY(target_method == nullptr)) { |
| 9559 | break; |
| 9560 | } |
| 9561 | |
| 9562 | if (target_method->IsPrivate()) { |
| 9563 | kind = mirror::MethodHandle::Kind::kInvokeDirect; |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9564 | target_method = ResolveMethod<ResolveMode::kNoChecks>(self, |
| 9565 | method_handle.field_or_method_idx_, |
| 9566 | referrer, |
| 9567 | InvokeType::kDirect); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9568 | } else { |
| 9569 | kind = mirror::MethodHandle::Kind::kInvokeSuper; |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9570 | target_method = ResolveMethod<ResolveMode::kNoChecks>(self, |
| 9571 | method_handle.field_or_method_idx_, |
| 9572 | referrer, |
| 9573 | InvokeType::kSuper); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9574 | if (UNLIKELY(target_method == nullptr)) { |
| 9575 | break; |
| 9576 | } |
| 9577 | // Find the method specified in the parent in referring class |
| 9578 | // so invoke-super invokes the method in the parent of the |
| 9579 | // referrer. |
| 9580 | target_method = |
| 9581 | referrer->GetDeclaringClass()->FindVirtualMethodForVirtual(target_method, |
| 9582 | kRuntimePointerSize); |
| 9583 | } |
| 9584 | break; |
| 9585 | } |
| 9586 | case DexFile::MethodHandleType::kInvokeInterface: { |
| 9587 | kind = mirror::MethodHandle::Kind::kInvokeInterface; |
| 9588 | receiver_count = 1; |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 9589 | target_method = ResolveMethod<ResolveMode::kNoChecks>(self, |
| 9590 | method_handle.field_or_method_idx_, |
| 9591 | referrer, |
| 9592 | InvokeType::kInterface); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9593 | break; |
| 9594 | } |
Orion Hodson | 631827d | 2017-04-10 14:53:47 +0100 | [diff] [blame] | 9595 | } |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9596 | |
| 9597 | if (UNLIKELY(target_method == nullptr)) { |
| 9598 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 9599 | return nullptr; |
| 9600 | } |
| 9601 | |
| 9602 | ObjPtr<mirror::Class> target_class = target_method->GetDeclaringClass(); |
| 9603 | ObjPtr<mirror::Class> referring_class = referrer->GetDeclaringClass(); |
| 9604 | uint32_t access_flags = target_method->GetAccessFlags(); |
| 9605 | if (UNLIKELY(!referring_class->CanAccessMember(target_class, access_flags))) { |
| 9606 | ThrowIllegalAccessErrorMethod(referring_class, target_method); |
| 9607 | return nullptr; |
| 9608 | } |
| 9609 | |
| 9610 | // Calculate the number of parameters from the method shorty. We add the |
| 9611 | // receiver count (0 or 1) and deduct one for the return value. |
| 9612 | uint32_t shorty_length; |
| 9613 | target_method->GetShorty(&shorty_length); |
| 9614 | int32_t num_params = static_cast<int32_t>(shorty_length + receiver_count - 1); |
| 9615 | |
Orion Hodson | ecd5856 | 2018-09-24 11:27:33 +0100 | [diff] [blame] | 9616 | StackHandleScope<5> hs(self); |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 9617 | ObjPtr<mirror::Class> array_of_class = GetClassRoot<mirror::ObjectArray<mirror::Class>>(this); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9618 | Handle<mirror::ObjectArray<mirror::Class>> method_params(hs.NewHandle( |
| 9619 | mirror::ObjectArray<mirror::Class>::Alloc(self, array_of_class, num_params))); |
| 9620 | if (method_params.Get() == nullptr) { |
| 9621 | DCHECK(self->IsExceptionPending()); |
| 9622 | return nullptr; |
| 9623 | } |
| 9624 | |
Orion Hodson | ecd5856 | 2018-09-24 11:27:33 +0100 | [diff] [blame] | 9625 | const DexFile* dex_file = referrer->GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9626 | 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] | 9627 | int32_t index = 0; |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9628 | if (receiver_count != 0) { |
Orion Hodson | ecd5856 | 2018-09-24 11:27:33 +0100 | [diff] [blame] | 9629 | // Insert receiver. Use the class identified in the method handle rather than the declaring |
| 9630 | // class of the resolved method which may be super class or default interface method |
| 9631 | // (b/115964401). |
| 9632 | ObjPtr<mirror::Class> receiver_class = LookupResolvedType(method_id.class_idx_, referrer); |
| 9633 | // receiver_class should have been resolved when resolving the target method. |
| 9634 | DCHECK(receiver_class != nullptr); |
| 9635 | method_params->Set(index++, receiver_class); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9636 | } |
Orion Hodson | ecd5856 | 2018-09-24 11:27:33 +0100 | [diff] [blame] | 9637 | |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9638 | const dex::ProtoId& proto_id = dex_file->GetProtoId(method_id.proto_idx_); |
Orion Hodson | ecd5856 | 2018-09-24 11:27:33 +0100 | [diff] [blame] | 9639 | DexFileParameterIterator it(*dex_file, proto_id); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9640 | while (it.HasNext()) { |
Orion Hodson | da1cdd0 | 2018-01-31 18:08:28 +0000 | [diff] [blame] | 9641 | DCHECK_LT(index, num_params); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9642 | const dex::TypeIndex type_idx = it.GetTypeIdx(); |
Orion Hodson | ecd5856 | 2018-09-24 11:27:33 +0100 | [diff] [blame] | 9643 | ObjPtr<mirror::Class> klass = ResolveType(type_idx, referrer); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9644 | if (nullptr == klass) { |
| 9645 | DCHECK(self->IsExceptionPending()); |
| 9646 | return nullptr; |
| 9647 | } |
| 9648 | method_params->Set(index++, klass); |
| 9649 | it.Next(); |
| 9650 | } |
| 9651 | |
Orion Hodson | ecd5856 | 2018-09-24 11:27:33 +0100 | [diff] [blame] | 9652 | Handle<mirror::Class> return_type = |
| 9653 | hs.NewHandle(ResolveType(proto_id.return_type_idx_, referrer)); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9654 | if (UNLIKELY(return_type.IsNull())) { |
| 9655 | DCHECK(self->IsExceptionPending()); |
| 9656 | return nullptr; |
| 9657 | } |
| 9658 | |
| 9659 | Handle<mirror::MethodType> |
| 9660 | method_type(hs.NewHandle(mirror::MethodType::Create(self, return_type, method_params))); |
| 9661 | if (UNLIKELY(method_type.IsNull())) { |
| 9662 | DCHECK(self->IsExceptionPending()); |
| 9663 | return nullptr; |
| 9664 | } |
| 9665 | |
| 9666 | if (UNLIKELY(handle_type == DexFile::MethodHandleType::kInvokeConstructor)) { |
| 9667 | Handle<mirror::Class> constructor_class = hs.NewHandle(target_method->GetDeclaringClass()); |
| 9668 | Handle<mirror::MethodHandlesLookup> lookup = |
| 9669 | hs.NewHandle(mirror::MethodHandlesLookup::GetDefault(self)); |
| 9670 | return lookup->FindConstructor(self, constructor_class, method_type); |
| 9671 | } |
| 9672 | |
| 9673 | uintptr_t target = reinterpret_cast<uintptr_t>(target_method); |
| 9674 | return mirror::MethodHandleImpl::Create(self, target, kind, method_type); |
| 9675 | } |
| 9676 | |
Vladimir Marko | af94020 | 2017-12-08 15:01:18 +0000 | [diff] [blame] | 9677 | ObjPtr<mirror::MethodHandle> ClassLinker::ResolveMethodHandle(Thread* self, |
| 9678 | uint32_t method_handle_idx, |
| 9679 | ArtMethod* referrer) |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9680 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9681 | const DexFile* const dex_file = referrer->GetDexFile(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 9682 | const dex::MethodHandleItem& method_handle = dex_file->GetMethodHandle(method_handle_idx); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9683 | switch (static_cast<DexFile::MethodHandleType>(method_handle.method_handle_type_)) { |
| 9684 | case DexFile::MethodHandleType::kStaticPut: |
| 9685 | case DexFile::MethodHandleType::kStaticGet: |
| 9686 | case DexFile::MethodHandleType::kInstancePut: |
| 9687 | case DexFile::MethodHandleType::kInstanceGet: |
| 9688 | return ResolveMethodHandleForField(self, method_handle, referrer); |
| 9689 | case DexFile::MethodHandleType::kInvokeStatic: |
| 9690 | case DexFile::MethodHandleType::kInvokeInstance: |
| 9691 | case DexFile::MethodHandleType::kInvokeConstructor: |
| 9692 | case DexFile::MethodHandleType::kInvokeDirect: |
| 9693 | case DexFile::MethodHandleType::kInvokeInterface: |
Orion Hodson | da1cdd0 | 2018-01-31 18:08:28 +0000 | [diff] [blame] | 9694 | return ResolveMethodHandleForMethod(self, method_handle, referrer); |
Orion Hodson | f8db2c3 | 2017-07-07 20:07:12 +0100 | [diff] [blame] | 9695 | } |
Orion Hodson | c069a30 | 2017-01-18 09:23:12 +0000 | [diff] [blame] | 9696 | } |
| 9697 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 9698 | bool ClassLinker::IsQuickResolutionStub(const void* entry_point) const { |
| 9699 | return (entry_point == GetQuickResolutionStub()) || |
| 9700 | (quick_resolution_trampoline_ == entry_point); |
| 9701 | } |
| 9702 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 9703 | bool ClassLinker::IsQuickToInterpreterBridge(const void* entry_point) const { |
| 9704 | return (entry_point == GetQuickToInterpreterBridge()) || |
| 9705 | (quick_to_interpreter_bridge_trampoline_ == entry_point); |
| 9706 | } |
| 9707 | |
| 9708 | bool ClassLinker::IsQuickGenericJniStub(const void* entry_point) const { |
| 9709 | return (entry_point == GetQuickGenericJniStub()) || |
| 9710 | (quick_generic_jni_trampoline_ == entry_point); |
| 9711 | } |
| 9712 | |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 9713 | bool ClassLinker::IsJniDlsymLookupStub(const void* entry_point) const { |
Vladimir Marko | 7dac864 | 2019-11-06 17:09:30 +0000 | [diff] [blame] | 9714 | return entry_point == GetJniDlsymLookupStub() || |
| 9715 | (jni_dlsym_lookup_trampoline_ == entry_point); |
David Sehr | a49e053 | 2017-08-25 08:05:29 -0700 | [diff] [blame] | 9716 | } |
| 9717 | |
Vladimir Marko | fa458ac | 2020-02-12 14:08:07 +0000 | [diff] [blame] | 9718 | bool ClassLinker::IsJniDlsymLookupCriticalStub(const void* entry_point) const { |
| 9719 | return entry_point == GetJniDlsymLookupCriticalStub() || |
| 9720 | (jni_dlsym_lookup_critical_trampoline_ == entry_point); |
| 9721 | } |
| 9722 | |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 9723 | const void* ClassLinker::GetRuntimeQuickGenericJniStub() const { |
| 9724 | return GetQuickGenericJniStub(); |
| 9725 | } |
| 9726 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 9727 | void ClassLinker::SetEntryPointsToInterpreter(ArtMethod* method) const { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 9728 | if (!method->IsNative()) { |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 9729 | method->SetEntryPointFromQuickCompiledCode(GetQuickToInterpreterBridge()); |
| 9730 | } else { |
Goran Jakovljevic | c16268f | 2017-07-27 10:03:32 +0200 | [diff] [blame] | 9731 | method->SetEntryPointFromQuickCompiledCode(GetQuickGenericJniStub()); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 9732 | } |
| 9733 | } |
| 9734 | |
Alex Light | db01a09 | 2017-04-03 15:39:55 -0700 | [diff] [blame] | 9735 | void ClassLinker::SetEntryPointsForObsoleteMethod(ArtMethod* method) const { |
| 9736 | DCHECK(method->IsObsolete()); |
| 9737 | // We cannot mess with the entrypoints of native methods because they are used to determine how |
| 9738 | // large the method's quick stack frame is. Without this information we cannot walk the stacks. |
| 9739 | if (!method->IsNative()) { |
| 9740 | method->SetEntryPointFromQuickCompiledCode(GetInvokeObsoleteMethodStub()); |
| 9741 | } |
| 9742 | } |
| 9743 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 9744 | void ClassLinker::DumpForSigQuit(std::ostream& os) { |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 9745 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 9746 | ReaderMutexLock mu(soa.Self(), *Locks::classlinker_classes_lock_); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 9747 | os << "Zygote loaded classes=" << NumZygoteClasses() << " post zygote classes=" |
| 9748 | << NumNonZygoteClasses() << "\n"; |
Nicolas Geoffray | a90c922 | 2018-09-07 13:19:19 +0100 | [diff] [blame] | 9749 | ReaderMutexLock mu2(soa.Self(), *Locks::dex_lock_); |
| 9750 | os << "Dumping registered class loaders\n"; |
| 9751 | size_t class_loader_index = 0; |
| 9752 | for (const ClassLoaderData& class_loader : class_loaders_) { |
| 9753 | ObjPtr<mirror::ClassLoader> loader = |
| 9754 | ObjPtr<mirror::ClassLoader>::DownCast(soa.Self()->DecodeJObject(class_loader.weak_root)); |
| 9755 | if (loader != nullptr) { |
| 9756 | os << "#" << class_loader_index++ << " " << loader->GetClass()->PrettyDescriptor() << ": ["; |
| 9757 | bool saw_one_dex_file = false; |
| 9758 | for (const DexCacheData& dex_cache : dex_caches_) { |
| 9759 | if (dex_cache.IsValid() && dex_cache.class_table == class_loader.class_table) { |
| 9760 | if (saw_one_dex_file) { |
| 9761 | os << ":"; |
| 9762 | } |
| 9763 | saw_one_dex_file = true; |
| 9764 | os << dex_cache.dex_file->GetLocation(); |
| 9765 | } |
| 9766 | } |
| 9767 | os << "]"; |
| 9768 | bool found_parent = false; |
| 9769 | if (loader->GetParent() != nullptr) { |
| 9770 | size_t parent_index = 0; |
| 9771 | for (const ClassLoaderData& class_loader2 : class_loaders_) { |
| 9772 | ObjPtr<mirror::ClassLoader> loader2 = ObjPtr<mirror::ClassLoader>::DownCast( |
| 9773 | soa.Self()->DecodeJObject(class_loader2.weak_root)); |
| 9774 | if (loader2 == loader->GetParent()) { |
| 9775 | os << ", parent #" << parent_index; |
| 9776 | found_parent = true; |
| 9777 | break; |
| 9778 | } |
| 9779 | parent_index++; |
| 9780 | } |
| 9781 | if (!found_parent) { |
| 9782 | os << ", unregistered parent of type " |
| 9783 | << loader->GetParent()->GetClass()->PrettyDescriptor(); |
| 9784 | } |
| 9785 | } else { |
| 9786 | os << ", no parent"; |
| 9787 | } |
| 9788 | os << "\n"; |
| 9789 | } |
| 9790 | } |
| 9791 | os << "Done dumping class loaders\n"; |
Andreas Gampe | 9b7f8b5 | 2019-06-07 08:59:29 -0700 | [diff] [blame] | 9792 | Runtime* runtime = Runtime::Current(); |
| 9793 | os << "Classes initialized: " << runtime->GetStat(KIND_GLOBAL_CLASS_INIT_COUNT) << " in " |
| 9794 | << PrettyDuration(runtime->GetStat(KIND_GLOBAL_CLASS_INIT_TIME)) << "\n"; |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 9795 | } |
| 9796 | |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 9797 | class CountClassesVisitor : public ClassLoaderVisitor { |
| 9798 | public: |
| 9799 | CountClassesVisitor() : num_zygote_classes(0), num_non_zygote_classes(0) {} |
| 9800 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 9801 | void Visit(ObjPtr<mirror::ClassLoader> class_loader) |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 9802 | REQUIRES_SHARED(Locks::classlinker_classes_lock_, Locks::mutator_lock_) override { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 9803 | ClassTable* const class_table = class_loader->GetClassTable(); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 9804 | if (class_table != nullptr) { |
Vladimir Marko | c5798bf | 2016-12-09 10:20:54 +0000 | [diff] [blame] | 9805 | num_zygote_classes += class_table->NumZygoteClasses(class_loader); |
| 9806 | num_non_zygote_classes += class_table->NumNonZygoteClasses(class_loader); |
Mathieu Chartier | 6b06953 | 2015-08-05 15:08:12 -0700 | [diff] [blame] | 9807 | } |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 9808 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 9809 | |
| 9810 | size_t num_zygote_classes; |
| 9811 | size_t num_non_zygote_classes; |
| 9812 | }; |
| 9813 | |
| 9814 | size_t ClassLinker::NumZygoteClasses() const { |
| 9815 | CountClassesVisitor visitor; |
| 9816 | VisitClassLoaders(&visitor); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 9817 | return visitor.num_zygote_classes + boot_class_table_->NumZygoteClasses(nullptr); |
Mathieu Chartier | cc5ebdf | 2015-07-27 11:19:43 -0700 | [diff] [blame] | 9818 | } |
| 9819 | |
| 9820 | size_t ClassLinker::NumNonZygoteClasses() const { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 9821 | CountClassesVisitor visitor; |
| 9822 | VisitClassLoaders(&visitor); |
Andreas Gampe | 2af9902 | 2017-04-25 08:32:59 -0700 | [diff] [blame] | 9823 | return visitor.num_non_zygote_classes + boot_class_table_->NumNonZygoteClasses(nullptr); |
Elliott Hughes | cac6cc7 | 2011-11-03 20:31:21 -0700 | [diff] [blame] | 9824 | } |
| 9825 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 9826 | size_t ClassLinker::NumLoadedClasses() { |
Ian Rogers | 1bf8d4d | 2013-05-30 00:18:49 -0700 | [diff] [blame] | 9827 | ReaderMutexLock mu(Thread::Current(), *Locks::classlinker_classes_lock_); |
Mathieu Chartier | c2e2062 | 2014-11-03 11:41:47 -0800 | [diff] [blame] | 9828 | // 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] | 9829 | return NumNonZygoteClasses(); |
Elliott Hughes | e27955c | 2011-08-26 15:21:24 -0700 | [diff] [blame] | 9830 | } |
| 9831 | |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 9832 | pid_t ClassLinker::GetClassesLockOwner() { |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 9833 | return Locks::classlinker_classes_lock_->GetExclusiveOwnerTid(); |
Brian Carlstrom | 47d237a | 2011-10-18 15:08:33 -0700 | [diff] [blame] | 9834 | } |
| 9835 | |
| 9836 | pid_t ClassLinker::GetDexLockOwner() { |
Andreas Gampe | cc1b535 | 2016-12-01 16:58:38 -0800 | [diff] [blame] | 9837 | return Locks::dex_lock_->GetExclusiveOwnerTid(); |
Brian Carlstrom | 24a3c2e | 2011-10-17 18:07:52 -0700 | [diff] [blame] | 9838 | } |
| 9839 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 9840 | void ClassLinker::SetClassRoot(ClassRoot class_root, ObjPtr<mirror::Class> klass) { |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 9841 | DCHECK(!init_done_); |
| 9842 | |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 9843 | DCHECK(klass != nullptr); |
| 9844 | DCHECK(klass->GetClassLoader() == nullptr); |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 9845 | |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 9846 | mirror::ObjectArray<mirror::Class>* class_roots = class_roots_.Read(); |
Andreas Gampe | 2ed8def | 2014-08-28 14:41:02 -0700 | [diff] [blame] | 9847 | DCHECK(class_roots != nullptr); |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 9848 | DCHECK_LT(static_cast<uint32_t>(class_root), static_cast<uint32_t>(ClassRoot::kMax)); |
| 9849 | int32_t index = static_cast<int32_t>(class_root); |
| 9850 | DCHECK(class_roots->Get(index) == nullptr); |
| 9851 | class_roots->Set<false>(index, klass); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 9852 | } |
| 9853 | |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 9854 | ObjPtr<mirror::ClassLoader> ClassLinker::CreateWellKnownClassLoader( |
| 9855 | Thread* self, |
| 9856 | const std::vector<const DexFile*>& dex_files, |
| 9857 | Handle<mirror::Class> loader_class, |
Nicolas Geoffray | e167273 | 2018-11-30 01:09:49 +0000 | [diff] [blame] | 9858 | Handle<mirror::ClassLoader> parent_loader, |
| 9859 | Handle<mirror::ObjectArray<mirror::ClassLoader>> shared_libraries) { |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 9860 | |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 9861 | StackHandleScope<5> hs(self); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9862 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 9863 | ArtField* dex_elements_field = |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 9864 | jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList_dexElements); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9865 | |
Vladimir Marko | 4098a7a | 2017-11-06 16:00:51 +0000 | [diff] [blame] | 9866 | Handle<mirror::Class> dex_elements_class(hs.NewHandle(dex_elements_field->ResolveType())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 9867 | DCHECK(dex_elements_class != nullptr); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9868 | DCHECK(dex_elements_class->IsArrayClass()); |
Mathieu Chartier | daaf326 | 2015-03-24 13:30:28 -0700 | [diff] [blame] | 9869 | Handle<mirror::ObjectArray<mirror::Object>> h_dex_elements(hs.NewHandle( |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 9870 | mirror::ObjectArray<mirror::Object>::Alloc(self, |
| 9871 | dex_elements_class.Get(), |
| 9872 | dex_files.size()))); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9873 | Handle<mirror::Class> h_dex_element_class = |
| 9874 | hs.NewHandle(dex_elements_class->GetComponentType()); |
| 9875 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 9876 | ArtField* element_file_field = |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 9877 | jni::DecodeArtField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 9878 | DCHECK_EQ(h_dex_element_class.Get(), element_file_field->GetDeclaringClass()); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9879 | |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 9880 | ArtField* cookie_field = jni::DecodeArtField(WellKnownClasses::dalvik_system_DexFile_cookie); |
Vladimir Marko | 208f670 | 2017-12-08 12:00:50 +0000 | [diff] [blame] | 9881 | DCHECK_EQ(cookie_field->GetDeclaringClass(), element_file_field->LookupResolvedType()); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9882 | |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 9883 | ArtField* file_name_field = jni::DecodeArtField(WellKnownClasses::dalvik_system_DexFile_fileName); |
Vladimir Marko | 208f670 | 2017-12-08 12:00:50 +0000 | [diff] [blame] | 9884 | DCHECK_EQ(file_name_field->GetDeclaringClass(), element_file_field->LookupResolvedType()); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 9885 | |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9886 | // Fill the elements array. |
| 9887 | int32_t index = 0; |
| 9888 | for (const DexFile* dex_file : dex_files) { |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 9889 | StackHandleScope<4> hs2(self); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9890 | |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 9891 | // CreateWellKnownClassLoader is only used by gtests and compiler. |
| 9892 | // 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] | 9893 | Handle<mirror::LongArray> h_long_array = hs2.NewHandle(mirror::LongArray::Alloc( |
| 9894 | self, |
| 9895 | kDexFileIndexStart + 1)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 9896 | DCHECK(h_long_array != nullptr); |
Vladimir Marko | 78baed5 | 2018-10-11 10:44:58 +0100 | [diff] [blame] | 9897 | h_long_array->Set(kDexFileIndexStart, reinterpret_cast64<int64_t>(dex_file)); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9898 | |
Mathieu Chartier | 3738e98 | 2017-05-12 16:07:28 -0700 | [diff] [blame] | 9899 | // Note that this creates a finalizable dalvik.system.DexFile object and a corresponding |
| 9900 | // FinalizerReference which will never get cleaned up without a started runtime. |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9901 | Handle<mirror::Object> h_dex_file = hs2.NewHandle( |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 9902 | cookie_field->GetDeclaringClass()->AllocObject(self)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 9903 | DCHECK(h_dex_file != nullptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 9904 | cookie_field->SetObject<false>(h_dex_file.Get(), h_long_array.Get()); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9905 | |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 9906 | Handle<mirror::String> h_file_name = hs2.NewHandle( |
| 9907 | mirror::String::AllocFromModifiedUtf8(self, dex_file->GetLocation().c_str())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 9908 | DCHECK(h_file_name != nullptr); |
Mathieu Chartier | fbc3108 | 2016-01-24 11:59:56 -0800 | [diff] [blame] | 9909 | file_name_field->SetObject<false>(h_dex_file.Get(), h_file_name.Get()); |
| 9910 | |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9911 | 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] | 9912 | DCHECK(h_element != nullptr); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 9913 | element_file_field->SetObject<false>(h_element.Get(), h_dex_file.Get()); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9914 | |
| 9915 | h_dex_elements->Set(index, h_element.Get()); |
| 9916 | index++; |
| 9917 | } |
| 9918 | DCHECK_EQ(index, h_dex_elements->GetLength()); |
| 9919 | |
| 9920 | // Create DexPathList. |
| 9921 | Handle<mirror::Object> h_dex_path_list = hs.NewHandle( |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 9922 | dex_elements_field->GetDeclaringClass()->AllocObject(self)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 9923 | DCHECK(h_dex_path_list != nullptr); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9924 | // Set elements. |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 9925 | 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] | 9926 | // Create an empty List for the "nativeLibraryDirectories," required for native tests. |
| 9927 | // Note: this code is uncommon(oatdump)/testing-only, so don't add further WellKnownClasses |
| 9928 | // elements. |
| 9929 | { |
| 9930 | ArtField* native_lib_dirs = dex_elements_field->GetDeclaringClass()-> |
| 9931 | FindDeclaredInstanceField("nativeLibraryDirectories", "Ljava/util/List;"); |
| 9932 | DCHECK(native_lib_dirs != nullptr); |
| 9933 | ObjPtr<mirror::Class> list_class = FindSystemClass(self, "Ljava/util/ArrayList;"); |
| 9934 | DCHECK(list_class != nullptr); |
| 9935 | { |
| 9936 | StackHandleScope<1> h_list_scope(self); |
| 9937 | Handle<mirror::Class> h_list_class(h_list_scope.NewHandle<mirror::Class>(list_class)); |
| 9938 | bool list_init = EnsureInitialized(self, h_list_class, true, true); |
| 9939 | DCHECK(list_init); |
| 9940 | list_class = h_list_class.Get(); |
| 9941 | } |
| 9942 | ObjPtr<mirror::Object> list_object = list_class->AllocObject(self); |
| 9943 | // Note: we leave the object uninitialized. This must never leak into any non-testing code, but |
| 9944 | // is fine for testing. While it violates a Java-code invariant (the elementData field is |
| 9945 | // normally never null), as long as one does not try to add elements, this will still |
| 9946 | // work. |
| 9947 | native_lib_dirs->SetObject<false>(h_dex_path_list.Get(), list_object); |
| 9948 | } |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9949 | |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 9950 | // Create the class loader.. |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 9951 | Handle<mirror::ClassLoader> h_class_loader = hs.NewHandle<mirror::ClassLoader>( |
| 9952 | ObjPtr<mirror::ClassLoader>::DownCast(loader_class->AllocObject(self))); |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 9953 | DCHECK(h_class_loader != nullptr); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 9954 | // Set DexPathList. |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 9955 | ArtField* path_list_field = |
Andreas Gampe | 08883de | 2016-11-08 13:20:52 -0800 | [diff] [blame] | 9956 | jni::DecodeArtField(WellKnownClasses::dalvik_system_BaseDexClassLoader_pathList); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 9957 | DCHECK(path_list_field != nullptr); |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 9958 | 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] | 9959 | |
| 9960 | // Make a pretend boot-classpath. |
| 9961 | // TODO: Should we scan the image? |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 9962 | ArtField* const parent_field = |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 9963 | mirror::Class::FindField(self, |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 9964 | h_class_loader->GetClass(), |
Vladimir Marko | 19a4d37 | 2016-12-08 14:41:46 +0000 | [diff] [blame] | 9965 | "parent", |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 9966 | "Ljava/lang/ClassLoader;"); |
Roland Levillain | f39c9eb | 2015-05-26 15:02:07 +0100 | [diff] [blame] | 9967 | DCHECK(parent_field != nullptr); |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 9968 | if (parent_loader.Get() == nullptr) { |
| 9969 | ScopedObjectAccessUnchecked soa(self); |
| 9970 | ObjPtr<mirror::Object> boot_loader(soa.Decode<mirror::Class>( |
| 9971 | WellKnownClasses::java_lang_BootClassLoader)->AllocObject(self)); |
| 9972 | parent_field->SetObject<false>(h_class_loader.Get(), boot_loader); |
| 9973 | } else { |
| 9974 | parent_field->SetObject<false>(h_class_loader.Get(), parent_loader.Get()); |
| 9975 | } |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 9976 | |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 9977 | ArtField* shared_libraries_field = |
| 9978 | jni::DecodeArtField(WellKnownClasses::dalvik_system_BaseDexClassLoader_sharedLibraryLoaders); |
| 9979 | DCHECK(shared_libraries_field != nullptr); |
| 9980 | shared_libraries_field->SetObject<false>(h_class_loader.Get(), shared_libraries.Get()); |
| 9981 | |
| 9982 | return h_class_loader.Get(); |
| 9983 | } |
| 9984 | |
| 9985 | jobject ClassLinker::CreateWellKnownClassLoader(Thread* self, |
| 9986 | const std::vector<const DexFile*>& dex_files, |
| 9987 | jclass loader_class, |
Nicolas Geoffray | e167273 | 2018-11-30 01:09:49 +0000 | [diff] [blame] | 9988 | jobject parent_loader, |
| 9989 | jobject shared_libraries) { |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 9990 | CHECK(self->GetJniEnv()->IsSameObject(loader_class, |
| 9991 | WellKnownClasses::dalvik_system_PathClassLoader) || |
| 9992 | self->GetJniEnv()->IsSameObject(loader_class, |
David Brazdil | 1a9ac53 | 2019-03-05 11:57:13 +0000 | [diff] [blame] | 9993 | WellKnownClasses::dalvik_system_DelegateLastClassLoader) || |
| 9994 | self->GetJniEnv()->IsSameObject(loader_class, |
| 9995 | WellKnownClasses::dalvik_system_InMemoryDexClassLoader)); |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 9996 | |
| 9997 | // SOAAlreadyRunnable is protected, and we need something to add a global reference. |
| 9998 | // We could move the jobject to the callers, but all call-sites do this... |
| 9999 | ScopedObjectAccessUnchecked soa(self); |
| 10000 | |
| 10001 | // For now, create a libcore-level DexFile for each ART DexFile. This "explodes" multidex. |
Nicolas Geoffray | e167273 | 2018-11-30 01:09:49 +0000 | [diff] [blame] | 10002 | StackHandleScope<4> hs(self); |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 10003 | |
| 10004 | Handle<mirror::Class> h_loader_class = |
| 10005 | hs.NewHandle<mirror::Class>(soa.Decode<mirror::Class>(loader_class)); |
Nicolas Geoffray | e167273 | 2018-11-30 01:09:49 +0000 | [diff] [blame] | 10006 | Handle<mirror::ClassLoader> h_parent = |
| 10007 | hs.NewHandle<mirror::ClassLoader>(soa.Decode<mirror::ClassLoader>(parent_loader)); |
| 10008 | Handle<mirror::ObjectArray<mirror::ClassLoader>> h_shared_libraries = |
| 10009 | hs.NewHandle(soa.Decode<mirror::ObjectArray<mirror::ClassLoader>>(shared_libraries)); |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 10010 | |
| 10011 | ObjPtr<mirror::ClassLoader> loader = CreateWellKnownClassLoader( |
| 10012 | self, |
| 10013 | dex_files, |
| 10014 | h_loader_class, |
Nicolas Geoffray | e167273 | 2018-11-30 01:09:49 +0000 | [diff] [blame] | 10015 | h_parent, |
| 10016 | h_shared_libraries); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 10017 | |
| 10018 | // Make it a global ref and return. |
| 10019 | ScopedLocalRef<jobject> local_ref( |
Nicolas Geoffray | 6b9fd8c | 2018-11-16 10:25:42 +0000 | [diff] [blame] | 10020 | soa.Env(), soa.Env()->AddLocalReference<jobject>(loader)); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 10021 | return soa.Env()->NewGlobalRef(local_ref.get()); |
| 10022 | } |
| 10023 | |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 10024 | jobject ClassLinker::CreatePathClassLoader(Thread* self, |
| 10025 | const std::vector<const DexFile*>& dex_files) { |
| 10026 | return CreateWellKnownClassLoader(self, |
| 10027 | dex_files, |
| 10028 | WellKnownClasses::dalvik_system_PathClassLoader, |
| 10029 | nullptr); |
| 10030 | } |
| 10031 | |
Andreas Gampe | 8ac7595 | 2015-06-02 21:01:45 -0700 | [diff] [blame] | 10032 | void ClassLinker::DropFindArrayClassCache() { |
| 10033 | std::fill_n(find_array_class_cache_, kFindArrayCacheSize, GcRoot<mirror::Class>(nullptr)); |
| 10034 | find_array_class_cache_next_victim_ = 0; |
| 10035 | } |
| 10036 | |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 10037 | void ClassLinker::VisitClassLoaders(ClassLoaderVisitor* visitor) const { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 10038 | Thread* const self = Thread::Current(); |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 10039 | for (const ClassLoaderData& data : class_loaders_) { |
Mathieu Chartier | 4843bd5 | 2015-10-01 17:08:44 -0700 | [diff] [blame] | 10040 | // 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] | 10041 | ObjPtr<mirror::ClassLoader> class_loader = ObjPtr<mirror::ClassLoader>::DownCast( |
| 10042 | self->DecodeJObject(data.weak_root)); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 10043 | if (class_loader != nullptr) { |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 10044 | visitor->Visit(class_loader); |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 10045 | } |
| 10046 | } |
| 10047 | } |
| 10048 | |
Alexey Grebenkin | 252a4e4 | 2018-04-02 18:18:01 +0300 | [diff] [blame] | 10049 | void ClassLinker::VisitAllocators(AllocatorVisitor* visitor) const { |
| 10050 | for (const ClassLoaderData& data : class_loaders_) { |
| 10051 | LinearAlloc* alloc = data.allocator; |
| 10052 | if (alloc != nullptr && !visitor->Visit(alloc)) { |
| 10053 | break; |
| 10054 | } |
| 10055 | } |
| 10056 | } |
| 10057 | |
Mathieu Chartier | bc5a795 | 2016-10-17 15:46:31 -0700 | [diff] [blame] | 10058 | void ClassLinker::InsertDexFileInToClassLoader(ObjPtr<mirror::Object> dex_file, |
| 10059 | ObjPtr<mirror::ClassLoader> class_loader) { |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 10060 | DCHECK(dex_file != nullptr); |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 10061 | Thread* const self = Thread::Current(); |
| 10062 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 10063 | ClassTable* const table = ClassTableForClassLoader(class_loader); |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 10064 | DCHECK(table != nullptr); |
Mathieu Chartier | c9dbb1d | 2016-06-03 17:47:32 -0700 | [diff] [blame] | 10065 | if (table->InsertStrongRoot(dex_file) && class_loader != nullptr) { |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 10066 | // It was not already inserted, perform the write barrier to let the GC know the class loader's |
| 10067 | // class table was modified. |
Mathieu Chartier | 88ea61e | 2018-06-20 17:45:41 -0700 | [diff] [blame] | 10068 | WriteBarrier::ForEveryFieldWrite(class_loader); |
Mathieu Chartier | 00310e0 | 2015-10-17 12:46:42 -0700 | [diff] [blame] | 10069 | } |
| 10070 | } |
| 10071 | |
Mathieu Chartier | 951ec2c | 2015-09-22 08:50:05 -0700 | [diff] [blame] | 10072 | void ClassLinker::CleanupClassLoaders() { |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 10073 | Thread* const self = Thread::Current(); |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 10074 | std::vector<ClassLoaderData> to_delete; |
| 10075 | // Do the delete outside the lock to avoid lock violation in jit code cache. |
| 10076 | { |
| 10077 | WriterMutexLock mu(self, *Locks::classlinker_classes_lock_); |
| 10078 | for (auto it = class_loaders_.begin(); it != class_loaders_.end(); ) { |
| 10079 | const ClassLoaderData& data = *it; |
| 10080 | // 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] | 10081 | ObjPtr<mirror::ClassLoader> class_loader = |
| 10082 | ObjPtr<mirror::ClassLoader>::DownCast(self->DecodeJObject(data.weak_root)); |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 10083 | if (class_loader != nullptr) { |
| 10084 | ++it; |
| 10085 | } else { |
| 10086 | VLOG(class_linker) << "Freeing class loader"; |
| 10087 | to_delete.push_back(data); |
| 10088 | it = class_loaders_.erase(it); |
| 10089 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 10090 | } |
| 10091 | } |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 10092 | for (ClassLoaderData& data : to_delete) { |
Alexey Grebenkin | be4c2bd | 2018-02-01 19:09:59 +0300 | [diff] [blame] | 10093 | // CHA unloading analysis and SingleImplementaion cleanups are required. |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 10094 | DeleteClassLoader(self, data, /*cleanup_cha=*/ true); |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 10095 | } |
Mathieu Chartier | 9b1c71e | 2015-09-02 18:51:54 -0700 | [diff] [blame] | 10096 | } |
| 10097 | |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 10098 | class ClassLinker::FindVirtualMethodHolderVisitor : public ClassVisitor { |
| 10099 | public: |
| 10100 | FindVirtualMethodHolderVisitor(const ArtMethod* method, PointerSize pointer_size) |
| 10101 | : method_(method), |
| 10102 | pointer_size_(pointer_size) {} |
| 10103 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 10104 | bool operator()(ObjPtr<mirror::Class> klass) REQUIRES_SHARED(Locks::mutator_lock_) override { |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 10105 | if (klass->GetVirtualMethodsSliceUnchecked(pointer_size_).Contains(method_)) { |
| 10106 | holder_ = klass; |
| 10107 | } |
| 10108 | // Return false to stop searching if holder_ is not null. |
| 10109 | return holder_ == nullptr; |
| 10110 | } |
| 10111 | |
Mathieu Chartier | 28357fa | 2016-10-18 16:27:40 -0700 | [diff] [blame] | 10112 | ObjPtr<mirror::Class> holder_ = nullptr; |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 10113 | const ArtMethod* const method_; |
| 10114 | const PointerSize pointer_size_; |
| 10115 | }; |
| 10116 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 10117 | ObjPtr<mirror::Class> ClassLinker::GetHoldingClassOfCopiedMethod(ArtMethod* method) { |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 10118 | ScopedTrace trace(__FUNCTION__); // Since this function is slow, have a trace to notify people. |
| 10119 | CHECK(method->IsCopied()); |
| 10120 | FindVirtualMethodHolderVisitor visitor(method, image_pointer_size_); |
| 10121 | VisitClasses(&visitor); |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 10122 | return visitor.holder_; |
Mathieu Chartier | 6597577 | 2016-08-05 10:46:36 -0700 | [diff] [blame] | 10123 | } |
| 10124 | |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 10125 | ObjPtr<mirror::IfTable> ClassLinker::AllocIfTable(Thread* self, size_t ifcount) { |
| 10126 | return ObjPtr<mirror::IfTable>::DownCast(ObjPtr<mirror::ObjectArray<mirror::Object>>( |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 10127 | mirror::IfTable::Alloc(self, |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 10128 | GetClassRoot<mirror::ObjectArray<mirror::Object>>(this), |
Vladimir Marko | a8bba7d | 2018-05-30 15:18:48 +0100 | [diff] [blame] | 10129 | ifcount * mirror::IfTable::kMax))); |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 10130 | } |
| 10131 | |
Vladimir Marko | d1f7351 | 2020-04-02 10:50:35 +0100 | [diff] [blame] | 10132 | bool ClassLinker::IsUpdatableBootClassPathDescriptor(const char* descriptor ATTRIBUTE_UNUSED) { |
| 10133 | // Should not be called on ClassLinker, only on AotClassLinker that overrides this. |
| 10134 | LOG(FATAL) << "UNREACHABLE"; |
| 10135 | UNREACHABLE(); |
| 10136 | } |
| 10137 | |
Calin Juravle | 3378768 | 2019-07-26 14:27:18 -0700 | [diff] [blame] | 10138 | bool ClassLinker::DenyAccessBasedOnPublicSdk(ArtMethod* art_method ATTRIBUTE_UNUSED) const |
| 10139 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 10140 | // Should not be called on ClassLinker, only on AotClassLinker that overrides this. |
| 10141 | LOG(FATAL) << "UNREACHABLE"; |
| 10142 | UNREACHABLE(); |
| 10143 | } |
| 10144 | |
| 10145 | bool ClassLinker::DenyAccessBasedOnPublicSdk(ArtField* art_field ATTRIBUTE_UNUSED) const |
| 10146 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 10147 | // Should not be called on ClassLinker, only on AotClassLinker that overrides this. |
| 10148 | LOG(FATAL) << "UNREACHABLE"; |
| 10149 | UNREACHABLE(); |
| 10150 | } |
| 10151 | |
| 10152 | bool ClassLinker::DenyAccessBasedOnPublicSdk(const char* type_descriptor ATTRIBUTE_UNUSED) const { |
| 10153 | // Should not be called on ClassLinker, only on AotClassLinker that overrides this. |
| 10154 | LOG(FATAL) << "UNREACHABLE"; |
| 10155 | UNREACHABLE(); |
| 10156 | } |
| 10157 | |
Calin Juravle | 2c2724c | 2021-01-14 19:54:23 -0800 | [diff] [blame] | 10158 | void ClassLinker::SetEnablePublicSdkChecks(bool enabled ATTRIBUTE_UNUSED) { |
| 10159 | // Should not be called on ClassLinker, only on AotClassLinker that overrides this. |
| 10160 | LOG(FATAL) << "UNREACHABLE"; |
| 10161 | UNREACHABLE(); |
| 10162 | } |
| 10163 | |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 10164 | // Instantiate ClassLinker::ResolveMethod. |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 10165 | template ArtMethod* ClassLinker::ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>( |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 10166 | uint32_t method_idx, |
| 10167 | Handle<mirror::DexCache> dex_cache, |
| 10168 | Handle<mirror::ClassLoader> class_loader, |
| 10169 | ArtMethod* referrer, |
| 10170 | InvokeType type); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 10171 | template ArtMethod* ClassLinker::ResolveMethod<ClassLinker::ResolveMode::kNoChecks>( |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 10172 | uint32_t method_idx, |
| 10173 | Handle<mirror::DexCache> dex_cache, |
| 10174 | Handle<mirror::ClassLoader> class_loader, |
| 10175 | ArtMethod* referrer, |
| 10176 | InvokeType type); |
| 10177 | |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 10178 | // Instantiate ClassLinker::AllocClass. |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 10179 | template ObjPtr<mirror::Class> ClassLinker::AllocClass</* kMovable= */ true>( |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 10180 | Thread* self, |
| 10181 | ObjPtr<mirror::Class> java_lang_Class, |
| 10182 | uint32_t class_size); |
Andreas Gampe | 98ea9d9 | 2018-10-19 14:06:15 -0700 | [diff] [blame] | 10183 | template ObjPtr<mirror::Class> ClassLinker::AllocClass</* kMovable= */ false>( |
Roland Levillain | 0e84027 | 2018-08-23 19:55:30 +0100 | [diff] [blame] | 10184 | Thread* self, |
| 10185 | ObjPtr<mirror::Class> java_lang_Class, |
| 10186 | uint32_t class_size); |
| 10187 | |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 10188 | } // namespace art |