Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 1 | /* Copyright (C) 2016 The Android Open Source Project |
| 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 3 | * |
| 4 | * This file implements interfaces from the file jvmti.h. This implementation |
| 5 | * is licensed under the same terms as the file jvmti.h. The |
| 6 | * copyright and license information for the file jvmti.h follows. |
| 7 | * |
| 8 | * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved. |
| 9 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 10 | * |
| 11 | * This code is free software; you can redistribute it and/or modify it |
| 12 | * under the terms of the GNU General Public License version 2 only, as |
| 13 | * published by the Free Software Foundation. Oracle designates this |
| 14 | * particular file as subject to the "Classpath" exception as provided |
| 15 | * by Oracle in the LICENSE file that accompanied this code. |
| 16 | * |
| 17 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 18 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 19 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 20 | * version 2 for more details (a copy is included in the LICENSE file that |
| 21 | * accompanied this code). |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License version |
| 24 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 25 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 26 | * |
| 27 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 28 | * or visit www.oracle.com if you need additional information or have any |
| 29 | * questions. |
| 30 | */ |
| 31 | |
| 32 | #include "ti_redefine.h" |
| 33 | |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 34 | #include <algorithm> |
| 35 | #include <atomic> |
Alex Light | e7a3354 | 2019-04-10 14:22:49 -0700 | [diff] [blame] | 36 | #include <iterator> |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 37 | #include <limits> |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 38 | #include <sstream> |
Vladimir Marko | eb37ba5 | 2019-02-05 14:10:38 +0000 | [diff] [blame] | 39 | #include <string_view> |
Alex Light | e7a3354 | 2019-04-10 14:22:49 -0700 | [diff] [blame] | 40 | #include <unordered_map> |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 41 | |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 42 | #include <android-base/logging.h> |
| 43 | #include <android-base/stringprintf.h> |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 44 | |
Alex Light | 986914b | 2019-11-19 01:12:25 +0000 | [diff] [blame] | 45 | #include "alloc_manager.h" |
| 46 | #include "android-base/macros.h" |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 47 | #include "android-base/thread_annotations.h" |
Andreas Gampe | a1d2f95 | 2017-04-20 22:53:58 -0700 | [diff] [blame] | 48 | #include "art_field-inl.h" |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 49 | #include "art_field.h" |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 50 | #include "art_jvmti.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 51 | #include "art_method-inl.h" |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 52 | #include "art_method.h" |
Vladimir Marko | e1993c7 | 2017-06-14 17:01:38 +0100 | [diff] [blame] | 53 | #include "base/array_ref.h" |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 54 | #include "base/casts.h" |
| 55 | #include "base/enums.h" |
| 56 | #include "base/globals.h" |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 57 | #include "base/iteration_range.h" |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 58 | #include "base/length_prefixed_array.h" |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 59 | #include "base/locks.h" |
| 60 | #include "base/stl_util.h" |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 61 | #include "base/utils.h" |
Andreas Gampe | c6ea7d0 | 2017-02-01 16:46:28 -0800 | [diff] [blame] | 62 | #include "class_linker-inl.h" |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 63 | #include "class_linker.h" |
Vladimir Marko | 5868ada | 2020-05-12 11:50:34 +0100 | [diff] [blame] | 64 | #include "class_root-inl.h" |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 65 | #include "class_status.h" |
Alex Light | 5643caf | 2017-02-08 11:39:07 -0800 | [diff] [blame] | 66 | #include "debugger.h" |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 67 | #include "dex/art_dex_file_loader.h" |
Mathieu Chartier | 4ac9ade | 2018-07-24 10:27:21 -0700 | [diff] [blame] | 68 | #include "dex/class_accessor-inl.h" |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 69 | #include "dex/class_accessor.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 70 | #include "dex/dex_file.h" |
| 71 | #include "dex/dex_file_loader.h" |
| 72 | #include "dex/dex_file_types.h" |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 73 | #include "dex/primitive.h" |
Andreas Gampe | ad1aa63 | 2019-01-02 10:30:54 -0800 | [diff] [blame] | 74 | #include "dex/signature-inl.h" |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 75 | #include "dex/signature.h" |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 76 | #include "events-inl.h" |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 77 | #include "events.h" |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 78 | #include "gc/allocation_listener.h" |
Alex Light | 6abd539 | 2017-01-05 17:53:00 -0800 | [diff] [blame] | 79 | #include "gc/heap.h" |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 80 | #include "gc/heap-inl.h" |
| 81 | #include "gc/heap-visit-objects-inl.h" |
| 82 | #include "handle.h" |
| 83 | #include "handle_scope.h" |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 84 | #include "instrumentation.h" |
Alex Light | 07f0621 | 2017-06-01 14:01:43 -0700 | [diff] [blame] | 85 | #include "intern_table.h" |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 86 | #include "jit/jit.h" |
| 87 | #include "jit/jit_code_cache.h" |
Vladimir Marko | a3ad0cd | 2018-05-04 10:06:38 +0100 | [diff] [blame] | 88 | #include "jni/jni_env_ext-inl.h" |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 89 | #include "jni/jni_id_manager.h" |
| 90 | #include "jvmti.h" |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 91 | #include "jvmti_allocator.h" |
Vladimir Marko | 5924a4a | 2018-05-29 17:40:41 +0100 | [diff] [blame] | 92 | #include "linear_alloc.h" |
Andreas Gampe | 8e0f043 | 2018-10-24 13:38:03 -0700 | [diff] [blame] | 93 | #include "mirror/array-alloc-inl.h" |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 94 | #include "mirror/array.h" |
Andreas Gampe | 70f5fd0 | 2018-10-24 19:58:37 -0700 | [diff] [blame] | 95 | #include "mirror/class-alloc-inl.h" |
Alex Light | 6161f13 | 2017-01-25 10:30:20 -0800 | [diff] [blame] | 96 | #include "mirror/class-inl.h" |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 97 | #include "mirror/class-refvisitor-inl.h" |
| 98 | #include "mirror/class.h" |
Vladimir Marko | bb206de | 2019-03-28 10:30:32 +0000 | [diff] [blame] | 99 | #include "mirror/class_ext-inl.h" |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 100 | #include "mirror/dex_cache-inl.h" |
| 101 | #include "mirror/dex_cache.h" |
| 102 | #include "mirror/executable-inl.h" |
| 103 | #include "mirror/field-inl.h" |
Alex Light | 163652e | 2020-01-08 15:18:25 -0800 | [diff] [blame] | 104 | #include "mirror/field.h" |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 105 | #include "mirror/method.h" |
| 106 | #include "mirror/method_handle_impl-inl.h" |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 107 | #include "mirror/object.h" |
Andreas Gampe | 52ecb65 | 2018-10-24 15:18:21 -0700 | [diff] [blame] | 108 | #include "mirror/object_array-alloc-inl.h" |
| 109 | #include "mirror/object_array-inl.h" |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 110 | #include "mirror/object_array.h" |
| 111 | #include "mirror/string.h" |
Alex Light | c18eba3 | 2019-09-24 14:36:27 -0700 | [diff] [blame] | 112 | #include "mirror/var_handle.h" |
Andreas Gampe | 373a9b5 | 2017-10-18 09:01:57 -0700 | [diff] [blame] | 113 | #include "nativehelper/scoped_local_ref.h" |
Alex Light | e77b48b | 2017-02-22 11:08:06 -0800 | [diff] [blame] | 114 | #include "non_debuggable_classes.h" |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 115 | #include "obj_ptr.h" |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 116 | #include "object_lock.h" |
Alex Light | 2462789 | 2019-11-06 10:28:21 -0800 | [diff] [blame] | 117 | #include "reflective_value_visitor.h" |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 118 | #include "runtime.h" |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 119 | #include "runtime_globals.h" |
Alex Light | 986914b | 2019-11-19 01:12:25 +0000 | [diff] [blame] | 120 | #include "scoped_thread_state_change.h" |
Vladimir Marko | 5924a4a | 2018-05-29 17:40:41 +0100 | [diff] [blame] | 121 | #include "stack.h" |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 122 | #include "thread.h" |
Vladimir Marko | 5924a4a | 2018-05-29 17:40:41 +0100 | [diff] [blame] | 123 | #include "thread_list.h" |
Alex Light | a26e349 | 2017-06-27 17:55:37 -0700 | [diff] [blame] | 124 | #include "ti_breakpoint.h" |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 125 | #include "ti_class_definition.h" |
Alex Light | eb98b08 | 2017-01-25 13:02:32 -0800 | [diff] [blame] | 126 | #include "ti_class_loader.h" |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 127 | #include "ti_heap.h" |
| 128 | #include "ti_logging.h" |
| 129 | #include "ti_thread.h" |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 130 | #include "transform.h" |
Andreas Gampe | a43ba3d | 2019-03-13 15:49:20 -0700 | [diff] [blame] | 131 | #include "verifier/class_verifier.h" |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 132 | #include "verifier/verifier_enums.h" |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 133 | #include "well_known_classes.h" |
| 134 | #include "write_barrier.h" |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 135 | |
| 136 | namespace openjdkjvmti { |
| 137 | |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 138 | // Debug check to force us to directly check we saw all methods and fields exactly once directly. |
| 139 | // Normally we don't need to do this since if any are missing the count will be different |
| 140 | constexpr bool kCheckAllMethodsSeenOnce = art::kIsDebugBuild; |
| 141 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 142 | using android::base::StringPrintf; |
| 143 | |
Alex Light | eee0bd4 | 2017-02-14 15:31:45 +0000 | [diff] [blame] | 144 | // A helper that fills in a classes obsolete_methods_ and obsolete_dex_caches_ classExt fields as |
| 145 | // they are created. This ensures that we can always call any method of an obsolete ArtMethod object |
| 146 | // almost as soon as they are created since the GetObsoleteDexCache method will succeed. |
| 147 | class ObsoleteMap { |
| 148 | public: |
Alex Light | e7a3354 | 2019-04-10 14:22:49 -0700 | [diff] [blame] | 149 | art::ArtMethod* FindObsoleteVersion(art::ArtMethod* original) const |
Alex Light | eee0bd4 | 2017-02-14 15:31:45 +0000 | [diff] [blame] | 150 | REQUIRES(art::Locks::mutator_lock_, art::Roles::uninterruptible_) { |
| 151 | auto method_pair = id_map_.find(original); |
| 152 | if (method_pair != id_map_.end()) { |
| 153 | art::ArtMethod* res = obsolete_methods_->GetElementPtrSize<art::ArtMethod*>( |
| 154 | method_pair->second, art::kRuntimePointerSize); |
| 155 | DCHECK(res != nullptr); |
Alex Light | eee0bd4 | 2017-02-14 15:31:45 +0000 | [diff] [blame] | 156 | return res; |
| 157 | } else { |
| 158 | return nullptr; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | void RecordObsolete(art::ArtMethod* original, art::ArtMethod* obsolete) |
| 163 | REQUIRES(art::Locks::mutator_lock_, art::Roles::uninterruptible_) { |
| 164 | DCHECK(original != nullptr); |
| 165 | DCHECK(obsolete != nullptr); |
| 166 | int32_t slot = next_free_slot_++; |
| 167 | DCHECK_LT(slot, obsolete_methods_->GetLength()); |
| 168 | DCHECK(nullptr == |
| 169 | obsolete_methods_->GetElementPtrSize<art::ArtMethod*>(slot, art::kRuntimePointerSize)); |
| 170 | DCHECK(nullptr == obsolete_dex_caches_->Get(slot)); |
| 171 | obsolete_methods_->SetElementPtrSize(slot, obsolete, art::kRuntimePointerSize); |
| 172 | obsolete_dex_caches_->Set(slot, original_dex_cache_); |
| 173 | id_map_.insert({original, slot}); |
| 174 | } |
| 175 | |
| 176 | ObsoleteMap(art::ObjPtr<art::mirror::PointerArray> obsolete_methods, |
| 177 | art::ObjPtr<art::mirror::ObjectArray<art::mirror::DexCache>> obsolete_dex_caches, |
| 178 | art::ObjPtr<art::mirror::DexCache> original_dex_cache) |
| 179 | : next_free_slot_(0), |
| 180 | obsolete_methods_(obsolete_methods), |
| 181 | obsolete_dex_caches_(obsolete_dex_caches), |
| 182 | original_dex_cache_(original_dex_cache) { |
| 183 | // Figure out where the first unused slot in the obsolete_methods_ array is. |
| 184 | while (obsolete_methods_->GetElementPtrSize<art::ArtMethod*>( |
| 185 | next_free_slot_, art::kRuntimePointerSize) != nullptr) { |
| 186 | DCHECK(obsolete_dex_caches_->Get(next_free_slot_) != nullptr); |
| 187 | next_free_slot_++; |
| 188 | } |
Orion Hodson | aacf977 | 2020-07-22 21:51:00 +0100 | [diff] [blame] | 189 | // Check that the same slot in obsolete_dex_caches_ is free. |
Alex Light | eee0bd4 | 2017-02-14 15:31:45 +0000 | [diff] [blame] | 190 | DCHECK(obsolete_dex_caches_->Get(next_free_slot_) == nullptr); |
| 191 | } |
| 192 | |
Alex Light | e7a3354 | 2019-04-10 14:22:49 -0700 | [diff] [blame] | 193 | struct ObsoleteMethodPair { |
| 194 | art::ArtMethod* old_method; |
| 195 | art::ArtMethod* obsolete_method; |
| 196 | }; |
| 197 | |
Vladimir Marko | eab0248 | 2019-05-09 10:28:17 +0100 | [diff] [blame] | 198 | class ObsoleteMapIter { |
Alex Light | e7a3354 | 2019-04-10 14:22:49 -0700 | [diff] [blame] | 199 | public: |
Vladimir Marko | eab0248 | 2019-05-09 10:28:17 +0100 | [diff] [blame] | 200 | using iterator_category = std::forward_iterator_tag; |
| 201 | using value_type = ObsoleteMethodPair; |
| 202 | using difference_type = ptrdiff_t; |
| 203 | using pointer = void; // Unsupported. |
| 204 | using reference = void; // Unsupported. |
| 205 | |
Alex Light | e7a3354 | 2019-04-10 14:22:49 -0700 | [diff] [blame] | 206 | ObsoleteMethodPair operator*() const |
| 207 | REQUIRES(art::Locks::mutator_lock_, art::Roles::uninterruptible_) { |
Vladimir Marko | eab0248 | 2019-05-09 10:28:17 +0100 | [diff] [blame] | 208 | art::ArtMethod* obsolete = map_->obsolete_methods_->GetElementPtrSize<art::ArtMethod*>( |
| 209 | iter_->second, art::kRuntimePointerSize); |
Alex Light | e7a3354 | 2019-04-10 14:22:49 -0700 | [diff] [blame] | 210 | DCHECK(obsolete != nullptr); |
| 211 | return { iter_->first, obsolete }; |
| 212 | } |
| 213 | |
| 214 | bool operator==(ObsoleteMapIter other) const { |
| 215 | return map_ == other.map_ && iter_ == other.iter_; |
| 216 | } |
| 217 | |
| 218 | bool operator!=(ObsoleteMapIter other) const { |
| 219 | return !(*this == other); |
| 220 | } |
| 221 | |
Vladimir Marko | eab0248 | 2019-05-09 10:28:17 +0100 | [diff] [blame] | 222 | ObsoleteMapIter operator++(int) { |
Alex Light | e7a3354 | 2019-04-10 14:22:49 -0700 | [diff] [blame] | 223 | ObsoleteMapIter retval = *this; |
| 224 | ++(*this); |
| 225 | return retval; |
| 226 | } |
| 227 | |
Vladimir Marko | eab0248 | 2019-05-09 10:28:17 +0100 | [diff] [blame] | 228 | ObsoleteMapIter operator++() { |
Alex Light | e7a3354 | 2019-04-10 14:22:49 -0700 | [diff] [blame] | 229 | ++iter_; |
| 230 | return *this; |
| 231 | } |
| 232 | |
| 233 | private: |
| 234 | ObsoleteMapIter(const ObsoleteMap* map, |
| 235 | std::unordered_map<art::ArtMethod*, int32_t>::const_iterator iter) |
| 236 | : map_(map), iter_(iter) {} |
| 237 | |
| 238 | const ObsoleteMap* map_; |
Vladimir Marko | eab0248 | 2019-05-09 10:28:17 +0100 | [diff] [blame] | 239 | std::unordered_map<art::ArtMethod*, int32_t>::const_iterator iter_; |
Alex Light | e7a3354 | 2019-04-10 14:22:49 -0700 | [diff] [blame] | 240 | |
| 241 | friend class ObsoleteMap; |
| 242 | }; |
| 243 | |
| 244 | ObsoleteMapIter end() const { |
| 245 | return ObsoleteMapIter(this, id_map_.cend()); |
| 246 | } |
| 247 | |
| 248 | ObsoleteMapIter begin() const { |
| 249 | return ObsoleteMapIter(this, id_map_.cbegin()); |
| 250 | } |
| 251 | |
Alex Light | eee0bd4 | 2017-02-14 15:31:45 +0000 | [diff] [blame] | 252 | private: |
| 253 | int32_t next_free_slot_; |
| 254 | std::unordered_map<art::ArtMethod*, int32_t> id_map_; |
| 255 | // Pointers to the fields in mirror::ClassExt. These can be held as ObjPtr since this is only used |
| 256 | // when we have an exclusive mutator_lock_ (i.e. all threads are suspended). |
| 257 | art::ObjPtr<art::mirror::PointerArray> obsolete_methods_; |
| 258 | art::ObjPtr<art::mirror::ObjectArray<art::mirror::DexCache>> obsolete_dex_caches_; |
| 259 | art::ObjPtr<art::mirror::DexCache> original_dex_cache_; |
| 260 | }; |
| 261 | |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 262 | // This visitor walks thread stacks and allocates and sets up the obsolete methods. It also does |
Orion Hodson | aacf977 | 2020-07-22 21:51:00 +0100 | [diff] [blame] | 263 | // some basic soundness checks that the obsolete method is valid. |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 264 | class ObsoleteMethodStackVisitor : public art::StackVisitor { |
| 265 | protected: |
| 266 | ObsoleteMethodStackVisitor( |
| 267 | art::Thread* thread, |
| 268 | art::LinearAlloc* allocator, |
| 269 | const std::unordered_set<art::ArtMethod*>& obsoleted_methods, |
Alex Light | eee0bd4 | 2017-02-14 15:31:45 +0000 | [diff] [blame] | 270 | ObsoleteMap* obsolete_maps) |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 271 | : StackVisitor(thread, |
Andreas Gampe | 6e89776 | 2018-10-16 13:09:32 -0700 | [diff] [blame] | 272 | /*context=*/nullptr, |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 273 | StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 274 | allocator_(allocator), |
| 275 | obsoleted_methods_(obsoleted_methods), |
Alex Light | 4ba388a | 2017-01-27 10:26:49 -0800 | [diff] [blame] | 276 | obsolete_maps_(obsolete_maps) { } |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 277 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 278 | ~ObsoleteMethodStackVisitor() override {} |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 279 | |
| 280 | public: |
| 281 | // Returns true if we successfully installed obsolete methods on this thread, filling |
| 282 | // obsolete_maps_ with the translations if needed. Returns false and fills error_msg if we fail. |
| 283 | // The stack is cleaned up when we fail. |
Alex Light | 007ada2 | 2017-01-10 13:33:56 -0800 | [diff] [blame] | 284 | static void UpdateObsoleteFrames( |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 285 | art::Thread* thread, |
| 286 | art::LinearAlloc* allocator, |
| 287 | const std::unordered_set<art::ArtMethod*>& obsoleted_methods, |
Alex Light | eee0bd4 | 2017-02-14 15:31:45 +0000 | [diff] [blame] | 288 | ObsoleteMap* obsolete_maps) |
Alex Light | 007ada2 | 2017-01-10 13:33:56 -0800 | [diff] [blame] | 289 | REQUIRES(art::Locks::mutator_lock_) { |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 290 | ObsoleteMethodStackVisitor visitor(thread, |
| 291 | allocator, |
| 292 | obsoleted_methods, |
Alex Light | 007ada2 | 2017-01-10 13:33:56 -0800 | [diff] [blame] | 293 | obsolete_maps); |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 294 | visitor.WalkStack(); |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 295 | } |
| 296 | |
Roland Levillain | bbc6e7e | 2018-08-24 16:58:47 +0100 | [diff] [blame] | 297 | bool VisitFrame() override REQUIRES(art::Locks::mutator_lock_) { |
Alex Light | eee0bd4 | 2017-02-14 15:31:45 +0000 | [diff] [blame] | 298 | art::ScopedAssertNoThreadSuspension snts("Fixing up the stack for obsolete methods."); |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 299 | art::ArtMethod* old_method = GetMethod(); |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 300 | if (obsoleted_methods_.find(old_method) != obsoleted_methods_.end()) { |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 301 | // We cannot ensure that the right dex file is used in inlined frames so we don't support |
| 302 | // redefining them. |
Nicolas Geoffray | 226805d | 2018-12-14 10:59:02 +0000 | [diff] [blame] | 303 | DCHECK(!IsInInlinedFrame()) << "Inlined frames are not supported when using redefinition: " |
| 304 | << old_method->PrettyMethod() << " is inlined into " |
| 305 | << GetOuterMethod()->PrettyMethod(); |
Alex Light | eee0bd4 | 2017-02-14 15:31:45 +0000 | [diff] [blame] | 306 | art::ArtMethod* new_obsolete_method = obsolete_maps_->FindObsoleteVersion(old_method); |
| 307 | if (new_obsolete_method == nullptr) { |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 308 | // Create a new Obsolete Method and put it in the list. |
| 309 | art::Runtime* runtime = art::Runtime::Current(); |
| 310 | art::ClassLinker* cl = runtime->GetClassLinker(); |
| 311 | auto ptr_size = cl->GetImagePointerSize(); |
| 312 | const size_t method_size = art::ArtMethod::Size(ptr_size); |
Alex Light | 5c11a79 | 2017-03-10 14:29:22 -0800 | [diff] [blame] | 313 | auto* method_storage = allocator_->Alloc(art::Thread::Current(), method_size); |
Alex Light | 007ada2 | 2017-01-10 13:33:56 -0800 | [diff] [blame] | 314 | CHECK(method_storage != nullptr) << "Unable to allocate storage for obsolete version of '" |
| 315 | << old_method->PrettyMethod() << "'"; |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 316 | new_obsolete_method = new (method_storage) art::ArtMethod(); |
| 317 | new_obsolete_method->CopyFrom(old_method, ptr_size); |
| 318 | DCHECK_EQ(new_obsolete_method->GetDeclaringClass(), old_method->GetDeclaringClass()); |
| 319 | new_obsolete_method->SetIsObsolete(); |
Alex Light | fcbafb3 | 2017-02-02 15:09:54 -0800 | [diff] [blame] | 320 | new_obsolete_method->SetDontCompile(); |
Alex Light | db01a09 | 2017-04-03 15:39:55 -0700 | [diff] [blame] | 321 | cl->SetEntryPointsForObsoleteMethod(new_obsolete_method); |
Alex Light | eee0bd4 | 2017-02-14 15:31:45 +0000 | [diff] [blame] | 322 | obsolete_maps_->RecordObsolete(old_method, new_obsolete_method); |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 323 | } |
| 324 | DCHECK(new_obsolete_method != nullptr); |
| 325 | SetMethod(new_obsolete_method); |
| 326 | } |
| 327 | return true; |
| 328 | } |
| 329 | |
| 330 | private: |
| 331 | // The linear allocator we should use to make new methods. |
| 332 | art::LinearAlloc* allocator_; |
| 333 | // The set of all methods which could be obsoleted. |
| 334 | const std::unordered_set<art::ArtMethod*>& obsoleted_methods_; |
| 335 | // A map from the original to the newly allocated obsolete method for frames on this thread. The |
Alex Light | eee0bd4 | 2017-02-14 15:31:45 +0000 | [diff] [blame] | 336 | // values in this map are added to the obsolete_methods_ (and obsolete_dex_caches_) fields of |
| 337 | // the redefined classes ClassExt as it is filled. |
| 338 | ObsoleteMap* obsolete_maps_; |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 339 | }; |
| 340 | |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 341 | template <RedefinitionType kType> |
| 342 | jvmtiError |
| 343 | Redefiner::IsModifiableClassGeneric(jvmtiEnv* env, jclass klass, jboolean* is_redefinable) { |
| 344 | if (env == nullptr) { |
| 345 | return ERR(INVALID_ENVIRONMENT); |
| 346 | } |
Alex Light | e4a8863 | 2017-01-10 07:41:24 -0800 | [diff] [blame] | 347 | art::Thread* self = art::Thread::Current(); |
| 348 | art::ScopedObjectAccess soa(self); |
| 349 | art::StackHandleScope<1> hs(self); |
| 350 | art::ObjPtr<art::mirror::Object> obj(self->DecodeJObject(klass)); |
Alex Light | 413a8ad | 2019-02-14 10:19:44 -0800 | [diff] [blame] | 351 | if (obj.IsNull() || !obj->IsClass()) { |
Alex Light | e4a8863 | 2017-01-10 07:41:24 -0800 | [diff] [blame] | 352 | return ERR(INVALID_CLASS); |
| 353 | } |
| 354 | art::Handle<art::mirror::Class> h_klass(hs.NewHandle(obj->AsClass())); |
| 355 | std::string err_unused; |
| 356 | *is_redefinable = |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 357 | Redefiner::GetClassRedefinitionError<kType>(h_klass, &err_unused) != ERR(UNMODIFIABLE_CLASS) |
| 358 | ? JNI_TRUE |
| 359 | : JNI_FALSE; |
Alex Light | e4a8863 | 2017-01-10 07:41:24 -0800 | [diff] [blame] | 360 | return OK; |
| 361 | } |
| 362 | |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 363 | jvmtiError |
| 364 | Redefiner::IsStructurallyModifiableClass(jvmtiEnv* env, jclass klass, jboolean* is_redefinable) { |
| 365 | return Redefiner::IsModifiableClassGeneric<RedefinitionType::kStructural>( |
| 366 | env, klass, is_redefinable); |
| 367 | } |
| 368 | |
| 369 | jvmtiError Redefiner::IsModifiableClass(jvmtiEnv* env, jclass klass, jboolean* is_redefinable) { |
| 370 | return Redefiner::IsModifiableClassGeneric<RedefinitionType::kNormal>(env, klass, is_redefinable); |
| 371 | } |
| 372 | |
| 373 | template <RedefinitionType kType> |
| 374 | jvmtiError Redefiner::GetClassRedefinitionError(jclass klass, /*out*/ std::string* error_msg) { |
Alex Light | 9e7859c | 2018-04-05 13:49:43 -0700 | [diff] [blame] | 375 | art::Thread* self = art::Thread::Current(); |
| 376 | art::ScopedObjectAccess soa(self); |
| 377 | art::StackHandleScope<1> hs(self); |
| 378 | art::ObjPtr<art::mirror::Object> obj(self->DecodeJObject(klass)); |
Alex Light | 413a8ad | 2019-02-14 10:19:44 -0800 | [diff] [blame] | 379 | if (obj.IsNull() || !obj->IsClass()) { |
Alex Light | 9e7859c | 2018-04-05 13:49:43 -0700 | [diff] [blame] | 380 | return ERR(INVALID_CLASS); |
| 381 | } |
| 382 | art::Handle<art::mirror::Class> h_klass(hs.NewHandle(obj->AsClass())); |
Alex Light | d55b844 | 2019-10-15 15:46:07 -0700 | [diff] [blame] | 383 | return Redefiner::GetClassRedefinitionError<kType>(h_klass, error_msg); |
Alex Light | 9e7859c | 2018-04-05 13:49:43 -0700 | [diff] [blame] | 384 | } |
| 385 | |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 386 | template <RedefinitionType kType> |
Alex Light | e4a8863 | 2017-01-10 07:41:24 -0800 | [diff] [blame] | 387 | jvmtiError Redefiner::GetClassRedefinitionError(art::Handle<art::mirror::Class> klass, |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 388 | /*out*/ std::string* error_msg) { |
| 389 | art::Thread* self = art::Thread::Current(); |
Alex Light | 9e7859c | 2018-04-05 13:49:43 -0700 | [diff] [blame] | 390 | if (!klass->IsResolved()) { |
| 391 | // It's only a problem to try to retransform/redefine a unprepared class if it's happening on |
| 392 | // the same thread as the class-linking process. If it's on another thread we will be able to |
| 393 | // wait for the preparation to finish and continue from there. |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 394 | if (klass->GetLockOwnerThreadId() == self->GetThreadId()) { |
Alex Light | 9e7859c | 2018-04-05 13:49:43 -0700 | [diff] [blame] | 395 | *error_msg = "Modification of class " + klass->PrettyClass() + |
| 396 | " from within the classes ClassLoad callback is not supported to prevent deadlocks." + |
| 397 | " Please use ClassFileLoadHook directly instead."; |
| 398 | return ERR(INTERNAL); |
| 399 | } else { |
| 400 | LOG(WARNING) << klass->PrettyClass() << " is not yet resolved. Attempting to transform " |
| 401 | << "it could cause arbitrary length waits as the class is being resolved."; |
| 402 | } |
| 403 | } |
Alex Light | e4a8863 | 2017-01-10 07:41:24 -0800 | [diff] [blame] | 404 | if (klass->IsPrimitive()) { |
| 405 | *error_msg = "Modification of primitive classes is not supported"; |
| 406 | return ERR(UNMODIFIABLE_CLASS); |
| 407 | } else if (klass->IsInterface()) { |
| 408 | *error_msg = "Modification of Interface classes is currently not supported"; |
| 409 | return ERR(UNMODIFIABLE_CLASS); |
Alex Light | 09f274f | 2017-02-21 15:00:48 -0800 | [diff] [blame] | 410 | } else if (klass->IsStringClass()) { |
| 411 | *error_msg = "Modification of String class is not supported"; |
| 412 | return ERR(UNMODIFIABLE_CLASS); |
Alex Light | e4a8863 | 2017-01-10 07:41:24 -0800 | [diff] [blame] | 413 | } else if (klass->IsArrayClass()) { |
| 414 | *error_msg = "Modification of Array classes is not supported"; |
| 415 | return ERR(UNMODIFIABLE_CLASS); |
| 416 | } else if (klass->IsProxyClass()) { |
| 417 | *error_msg = "Modification of proxy classes is not supported"; |
| 418 | return ERR(UNMODIFIABLE_CLASS); |
| 419 | } |
| 420 | |
Alex Light | e77b48b | 2017-02-22 11:08:06 -0800 | [diff] [blame] | 421 | for (jclass c : art::NonDebuggableClasses::GetNonDebuggableClasses()) { |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 422 | if (klass.Get() == self->DecodeJObject(c)->AsClass()) { |
Alex Light | e77b48b | 2017-02-22 11:08:06 -0800 | [diff] [blame] | 423 | *error_msg = "Class might have stack frames that cannot be made obsolete"; |
| 424 | return ERR(UNMODIFIABLE_CLASS); |
| 425 | } |
| 426 | } |
| 427 | |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 428 | if (kType == RedefinitionType::kStructural) { |
Alex Light | fb11957 | 2019-09-18 15:04:53 -0700 | [diff] [blame] | 429 | // Class initialization interacts really badly with structural redefinition since we need to |
| 430 | // make the old class obsolete. We currently just blanket don't allow it. |
| 431 | // TODO It might be nice to allow this at some point. |
| 432 | if (klass->IsInitializing() && |
| 433 | !klass->IsInitialized() && |
| 434 | klass->GetClinitThreadId() == self->GetTid()) { |
| 435 | // We are in the class-init running on this thread. |
| 436 | *error_msg = "Modification of class " + klass->PrettyClass() + " during class" + |
| 437 | " initialization is not allowed."; |
| 438 | return ERR(INTERNAL); |
| 439 | } |
| 440 | if (!art::Runtime::Current()->GetClassLinker()->EnsureInitialized( |
| 441 | self, klass, /*can_init_fields=*/true, /*can_init_parents=*/true)) { |
| 442 | self->AssertPendingException(); |
| 443 | *error_msg = "Class " + klass->PrettyClass() + " failed initialization. Structural" + |
| 444 | " redefinition of erroneous classes is not allowed. Failure was: " + |
| 445 | self->GetException()->Dump(); |
| 446 | self->ClearException(); |
| 447 | return ERR(INVALID_CLASS); |
| 448 | } |
Alex Light | f1b18fa | 2019-11-11 14:22:00 -0800 | [diff] [blame] | 449 | if (klass->IsMirrored()) { |
| 450 | std::string pc(klass->PrettyClass()); |
| 451 | *error_msg = StringPrintf("Class %s is a mirror class and cannot be structurally redefined.", |
| 452 | pc.c_str()); |
| 453 | return ERR(UNMODIFIABLE_CLASS); |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 454 | } |
| 455 | // Check Thread specifically since it's not a root but too many things reach into it with Unsafe |
| 456 | // too allow structural redefinition. |
| 457 | if (klass->IsAssignableFrom( |
| 458 | self->DecodeJObject(art::WellKnownClasses::java_lang_Thread)->AsClass())) { |
| 459 | *error_msg = |
| 460 | "java.lang.Thread has fields accessed using sun.misc.unsafe directly. It is not " |
| 461 | "safe to structurally redefine it."; |
| 462 | return ERR(UNMODIFIABLE_CLASS); |
| 463 | } |
Alex Light | 3f0dca1 | 2020-01-27 17:20:28 -0800 | [diff] [blame] | 464 | auto has_pointer_marker = |
| 465 | [](art::ObjPtr<art::mirror::Class> k) REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 466 | // Check for fields/methods which were returned before moving to index jni id type. |
| 467 | // TODO We might want to rework how this is done. Once full redefinition is implemented we |
| 468 | // will need to check any subtypes too. |
| 469 | art::ObjPtr<art::mirror::ClassExt> ext(k->GetExtData()); |
| 470 | if (!ext.IsNull()) { |
| 471 | if (ext->HasInstanceFieldPointerIdMarker() || ext->HasMethodPointerIdMarker() || |
| 472 | ext->HasStaticFieldPointerIdMarker()) { |
| 473 | return true; |
| 474 | } |
| 475 | } |
| 476 | return false; |
| 477 | }; |
| 478 | if (has_pointer_marker(klass.Get())) { |
| 479 | *error_msg = |
| 480 | StringPrintf("%s has active pointer jni-ids and cannot be redefined structurally", |
| 481 | klass->PrettyClass().c_str()); |
| 482 | return ERR(UNMODIFIABLE_CLASS); |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 483 | } |
Alex Light | 3f0dca1 | 2020-01-27 17:20:28 -0800 | [diff] [blame] | 484 | jvmtiError res = OK; |
| 485 | art::ClassFuncVisitor cfv( |
| 486 | [&](art::ObjPtr<art::mirror::Class> k) REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 487 | // if there is any class 'K' that is a subtype (i.e. extends) klass and has pointer-jni-ids |
| 488 | // we cannot structurally redefine the class 'k' since we would structurally redefine the |
| 489 | // subtype. |
| 490 | if (k->IsLoaded() && klass->IsAssignableFrom(k) && has_pointer_marker(k)) { |
| 491 | *error_msg = StringPrintf( |
| 492 | "%s has active pointer jni-ids from subtype %s and cannot be redefined structurally", |
| 493 | klass->PrettyClass().c_str(), |
| 494 | k->PrettyClass().c_str()); |
| 495 | res = ERR(UNMODIFIABLE_CLASS); |
| 496 | return false; |
| 497 | } |
| 498 | return true; |
| 499 | }); |
| 500 | art::Runtime::Current()->GetClassLinker()->VisitClasses(&cfv); |
| 501 | return res; |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 502 | } |
Alex Light | e4a8863 | 2017-01-10 07:41:24 -0800 | [diff] [blame] | 503 | return OK; |
| 504 | } |
| 505 | |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 506 | template jvmtiError Redefiner::GetClassRedefinitionError<RedefinitionType::kNormal>( |
| 507 | art::Handle<art::mirror::Class> klass, /*out*/ std::string* error_msg); |
| 508 | template jvmtiError Redefiner::GetClassRedefinitionError<RedefinitionType::kStructural>( |
| 509 | art::Handle<art::mirror::Class> klass, /*out*/ std::string* error_msg); |
| 510 | |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 511 | // Moves dex data to an anonymous, read-only mmap'd region. |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 512 | art::MemMap Redefiner::MoveDataToMemMap(const std::string& original_location, |
| 513 | art::ArrayRef<const unsigned char> data, |
| 514 | std::string* error_msg) { |
| 515 | art::MemMap map = art::MemMap::MapAnonymous( |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 516 | StringPrintf("%s-transformed", original_location.c_str()).c_str(), |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 517 | data.size(), |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 518 | PROT_READ|PROT_WRITE, |
Andreas Gampe | 6e89776 | 2018-10-16 13:09:32 -0700 | [diff] [blame] | 519 | /*low_4gb=*/ false, |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 520 | error_msg); |
| 521 | if (LIKELY(map.IsValid())) { |
| 522 | memcpy(map.Begin(), data.data(), data.size()); |
| 523 | // Make the dex files mmap read only. This matches how other DexFiles are mmaped and prevents |
| 524 | // programs from corrupting it. |
| 525 | map.Protect(PROT_READ); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 526 | } |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 527 | return map; |
| 528 | } |
| 529 | |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 530 | Redefiner::ClassRedefinition::ClassRedefinition( |
| 531 | Redefiner* driver, |
| 532 | jclass klass, |
| 533 | const art::DexFile* redefined_dex_file, |
| 534 | const char* class_sig, |
Vladimir Marko | e1993c7 | 2017-06-14 17:01:38 +0100 | [diff] [blame] | 535 | art::ArrayRef<const unsigned char> orig_dex_file) : |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 536 | driver_(driver), |
| 537 | klass_(klass), |
| 538 | dex_file_(redefined_dex_file), |
| 539 | class_sig_(class_sig), |
| 540 | original_dex_file_(orig_dex_file) { |
ShuJieWang | b087bb2 | 2021-11-24 11:46:16 +0800 | [diff] [blame] | 541 | lock_acquired_ = GetMirrorClass()->MonitorTryEnter(driver_->self_) != nullptr; |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | Redefiner::ClassRedefinition::~ClassRedefinition() { |
ShuJieWang | b087bb2 | 2021-11-24 11:46:16 +0800 | [diff] [blame] | 545 | if (driver_ != nullptr && lock_acquired_) { |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 546 | GetMirrorClass()->MonitorExit(driver_->self_); |
| 547 | } |
| 548 | } |
| 549 | |
Alex Light | d55b844 | 2019-10-15 15:46:07 -0700 | [diff] [blame] | 550 | template<RedefinitionType kType> |
| 551 | jvmtiError Redefiner::RedefineClassesGeneric(jvmtiEnv* jenv, |
| 552 | jint class_count, |
| 553 | const jvmtiClassDefinition* definitions) { |
Alex Light | 3732beb | 2019-10-04 13:35:34 -0700 | [diff] [blame] | 554 | art::Runtime* runtime = art::Runtime::Current(); |
| 555 | art::Thread* self = art::Thread::Current(); |
| 556 | ArtJvmTiEnv* env = ArtJvmTiEnv::AsArtJvmTiEnv(jenv); |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 557 | if (env == nullptr) { |
Alex Light | 3732beb | 2019-10-04 13:35:34 -0700 | [diff] [blame] | 558 | JVMTI_LOG(WARNING, env) << "FAILURE TO REDEFINE env was null!"; |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 559 | return ERR(INVALID_ENVIRONMENT); |
| 560 | } else if (class_count < 0) { |
Alex Light | 3732beb | 2019-10-04 13:35:34 -0700 | [diff] [blame] | 561 | JVMTI_LOG(WARNING, env) << "FAILURE TO REDEFINE class_count was less then 0"; |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 562 | return ERR(ILLEGAL_ARGUMENT); |
| 563 | } else if (class_count == 0) { |
| 564 | // We don't actually need to do anything. Just return OK. |
| 565 | return OK; |
| 566 | } else if (definitions == nullptr) { |
Alex Light | 3732beb | 2019-10-04 13:35:34 -0700 | [diff] [blame] | 567 | JVMTI_LOG(WARNING, env) << "FAILURE TO REDEFINE null definitions!"; |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 568 | return ERR(NULL_POINTER); |
| 569 | } |
Alex Light | 3732beb | 2019-10-04 13:35:34 -0700 | [diff] [blame] | 570 | std::string error_msg; |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 571 | std::vector<ArtClassDefinition> def_vector; |
| 572 | def_vector.reserve(class_count); |
| 573 | for (jint i = 0; i < class_count; i++) { |
Alex Light | d55b844 | 2019-10-15 15:46:07 -0700 | [diff] [blame] | 574 | jvmtiError res = Redefiner::GetClassRedefinitionError<RedefinitionType::kNormal>( |
| 575 | definitions[i].klass, &error_msg); |
Alex Light | ce6ee70 | 2017-03-06 15:46:43 -0800 | [diff] [blame] | 576 | if (res != OK) { |
Alex Light | 3732beb | 2019-10-04 13:35:34 -0700 | [diff] [blame] | 577 | JVMTI_LOG(WARNING, env) << "FAILURE TO REDEFINE " << error_msg; |
Alex Light | ce6ee70 | 2017-03-06 15:46:43 -0800 | [diff] [blame] | 578 | return res; |
Alex Light | ce6ee70 | 2017-03-06 15:46:43 -0800 | [diff] [blame] | 579 | } |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 580 | ArtClassDefinition def; |
Alex Light | 64e4c14 | 2018-01-30 13:46:37 -0800 | [diff] [blame] | 581 | res = def.Init(self, definitions[i]); |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 582 | if (res != OK) { |
Alex Light | 3732beb | 2019-10-04 13:35:34 -0700 | [diff] [blame] | 583 | JVMTI_LOG(WARNING, env) << "FAILURE TO REDEFINE bad definition " << i; |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 584 | return res; |
| 585 | } |
| 586 | def_vector.push_back(std::move(def)); |
| 587 | } |
| 588 | // Call all the transformation events. |
Alex Light | d55b844 | 2019-10-15 15:46:07 -0700 | [diff] [blame] | 589 | Transformer::RetransformClassesDirect<kType>(self, &def_vector); |
| 590 | if (kType == RedefinitionType::kStructural) { |
| 591 | Transformer::RetransformClassesDirect<RedefinitionType::kNormal>(self, &def_vector); |
| 592 | } |
| 593 | jvmtiError res = RedefineClassesDirect(env, runtime, self, def_vector, kType, &error_msg); |
Alex Light | 3732beb | 2019-10-04 13:35:34 -0700 | [diff] [blame] | 594 | if (res != OK) { |
| 595 | JVMTI_LOG(WARNING, env) << "FAILURE TO REDEFINE " << error_msg; |
| 596 | } |
| 597 | return res; |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 598 | } |
| 599 | |
Alex Light | d55b844 | 2019-10-15 15:46:07 -0700 | [diff] [blame] | 600 | jvmtiError Redefiner::StructurallyRedefineClasses(jvmtiEnv* jenv, |
| 601 | jint class_count, |
| 602 | const jvmtiClassDefinition* definitions) { |
| 603 | ArtJvmTiEnv* art_env = ArtJvmTiEnv::AsArtJvmTiEnv(jenv); |
| 604 | if (art_env == nullptr) { |
| 605 | return ERR(INVALID_ENVIRONMENT); |
| 606 | } else if (art_env->capabilities.can_redefine_classes != 1) { |
| 607 | return ERR(MUST_POSSESS_CAPABILITY); |
| 608 | } |
| 609 | return RedefineClassesGeneric<RedefinitionType::kStructural>(jenv, class_count, definitions); |
| 610 | } |
| 611 | |
| 612 | jvmtiError Redefiner::RedefineClasses(jvmtiEnv* jenv, |
| 613 | jint class_count, |
| 614 | const jvmtiClassDefinition* definitions) { |
| 615 | return RedefineClassesGeneric<RedefinitionType::kNormal>(jenv, class_count, definitions); |
| 616 | } |
| 617 | |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 618 | jvmtiError Redefiner::StructurallyRedefineClassDirect(jvmtiEnv* env, |
| 619 | jclass klass, |
| 620 | const unsigned char* data, |
| 621 | jint data_size) { |
| 622 | if (env == nullptr) { |
| 623 | return ERR(INVALID_ENVIRONMENT); |
| 624 | } else if (ArtJvmTiEnv::AsArtJvmTiEnv(env)->capabilities.can_redefine_classes != 1) { |
| 625 | JVMTI_LOG(INFO, env) << "Does not have can_redefine_classes cap!"; |
| 626 | return ERR(MUST_POSSESS_CAPABILITY); |
| 627 | } |
| 628 | std::vector<ArtClassDefinition> acds; |
| 629 | ArtClassDefinition acd; |
| 630 | jvmtiError err = acd.Init( |
| 631 | art::Thread::Current(), |
| 632 | jvmtiClassDefinition{ .klass = klass, .class_byte_count = data_size, .class_bytes = data }); |
| 633 | if (err != OK) { |
| 634 | return err; |
| 635 | } |
| 636 | acds.push_back(std::move(acd)); |
| 637 | std::string err_msg; |
| 638 | err = RedefineClassesDirect(ArtJvmTiEnv::AsArtJvmTiEnv(env), |
| 639 | art::Runtime::Current(), |
| 640 | art::Thread::Current(), |
| 641 | acds, |
| 642 | RedefinitionType::kStructural, |
| 643 | &err_msg); |
| 644 | if (err != OK) { |
| 645 | JVMTI_LOG(WARNING, env) << "Failed structural redefinition: " << err_msg; |
| 646 | } |
| 647 | return err; |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | jvmtiError Redefiner::RedefineClassesDirect(ArtJvmTiEnv* env, |
| 651 | art::Runtime* runtime, |
| 652 | art::Thread* self, |
| 653 | const std::vector<ArtClassDefinition>& definitions, |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 654 | RedefinitionType type, |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 655 | std::string* error_msg) { |
| 656 | DCHECK(env != nullptr); |
| 657 | if (definitions.size() == 0) { |
| 658 | // We don't actually need to do anything. Just return OK. |
| 659 | return OK; |
| 660 | } |
Alex Light | c2d0c96 | 2019-10-23 14:14:25 -0700 | [diff] [blame] | 661 | // We need to fiddle with the verification class flags. To do this we need to make sure there are |
| 662 | // no concurrent redefinitions of the same class at the same time. For simplicity and because |
| 663 | // this is not expected to be a common occurrence we will just wrap the whole thing in a TOP-level |
| 664 | // lock. |
| 665 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 666 | // Stop JIT for the duration of this redefine since the JIT might concurrently compile a method we |
| 667 | // are going to redefine. |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 668 | // TODO We should prevent user-code suspensions to make sure this isn't held for too long. |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 669 | art::jit::ScopedJitSuspend suspend_jit; |
| 670 | // Get shared mutator lock so we can lock all the classes. |
| 671 | art::ScopedObjectAccess soa(self); |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 672 | Redefiner r(env, runtime, self, type, error_msg); |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 673 | for (const ArtClassDefinition& def : definitions) { |
| 674 | // Only try to transform classes that have been modified. |
Alex Light | 4052847 | 2017-03-28 09:07:36 -0700 | [diff] [blame] | 675 | if (def.IsModified()) { |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 676 | jvmtiError res = r.AddRedefinition(env, def); |
| 677 | if (res != OK) { |
| 678 | return res; |
| 679 | } |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 680 | } |
| 681 | } |
| 682 | return r.Run(); |
| 683 | } |
| 684 | |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 685 | jvmtiError Redefiner::AddRedefinition(ArtJvmTiEnv* env, const ArtClassDefinition& def) { |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 686 | std::string original_dex_location; |
| 687 | jvmtiError ret = OK; |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 688 | if ((ret = GetClassLocation(env, def.GetClass(), &original_dex_location))) { |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 689 | *error_msg_ = "Unable to get original dex file location!"; |
| 690 | return ret; |
| 691 | } |
Alex Light | 52a2db5 | 2017-01-19 23:00:21 +0000 | [diff] [blame] | 692 | char* generic_ptr_unused = nullptr; |
| 693 | char* signature_ptr = nullptr; |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 694 | if ((ret = env->GetClassSignature(def.GetClass(), &signature_ptr, &generic_ptr_unused)) != OK) { |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 695 | *error_msg_ = "Unable to get class signature!"; |
| 696 | return ret; |
Alex Light | 52a2db5 | 2017-01-19 23:00:21 +0000 | [diff] [blame] | 697 | } |
Andreas Gampe | 5471141 | 2017-02-21 12:41:43 -0800 | [diff] [blame] | 698 | JvmtiUniquePtr<char> generic_unique_ptr(MakeJvmtiUniquePtr(env, generic_ptr_unused)); |
| 699 | JvmtiUniquePtr<char> signature_unique_ptr(MakeJvmtiUniquePtr(env, signature_ptr)); |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 700 | art::MemMap map = MoveDataToMemMap(original_dex_location, def.GetDexData(), error_msg_); |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 701 | std::ostringstream os; |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 702 | if (!map.IsValid()) { |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 703 | os << "Failed to create anonymous mmap for modified dex file of class " << def.GetName() |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 704 | << "in dex file " << original_dex_location << " because: " << *error_msg_; |
| 705 | *error_msg_ = os.str(); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 706 | return ERR(OUT_OF_MEMORY); |
| 707 | } |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 708 | if (map.Size() < sizeof(art::DexFile::Header)) { |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 709 | *error_msg_ = "Could not read dex file header because dex_data was too short"; |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 710 | return ERR(INVALID_CLASS_FORMAT); |
| 711 | } |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 712 | std::string name = map.GetName(); |
| 713 | uint32_t checksum = reinterpret_cast<const art::DexFile::Header*>(map.Begin())->checksum_; |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 714 | const art::ArtDexFileLoader dex_file_loader; |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 715 | std::unique_ptr<const art::DexFile> dex_file(dex_file_loader.Open(name, |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 716 | checksum, |
| 717 | std::move(map), |
Andreas Gampe | 6e89776 | 2018-10-16 13:09:32 -0700 | [diff] [blame] | 718 | /*verify=*/true, |
| 719 | /*verify_checksum=*/true, |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 720 | error_msg_)); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 721 | if (dex_file.get() == nullptr) { |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 722 | os << "Unable to load modified dex file for " << def.GetName() << ": " << *error_msg_; |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 723 | *error_msg_ = os.str(); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 724 | return ERR(INVALID_CLASS_FORMAT); |
| 725 | } |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 726 | redefinitions_.push_back( |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 727 | Redefiner::ClassRedefinition(this, |
Alex Light | b7354d5 | 2017-03-30 15:17:01 -0700 | [diff] [blame] | 728 | def.GetClass(), |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 729 | dex_file.release(), |
| 730 | signature_ptr, |
Alex Light | 4052847 | 2017-03-28 09:07:36 -0700 | [diff] [blame] | 731 | def.GetNewOriginalDexFile())); |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 732 | return OK; |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 733 | } |
| 734 | |
Vladimir Marko | 4617d58 | 2019-03-28 13:48:31 +0000 | [diff] [blame] | 735 | art::ObjPtr<art::mirror::Class> Redefiner::ClassRedefinition::GetMirrorClass() { |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 736 | return driver_->self_->DecodeJObject(klass_)->AsClass(); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 737 | } |
| 738 | |
Vladimir Marko | c524e9e | 2019-03-26 10:54:50 +0000 | [diff] [blame] | 739 | art::ObjPtr<art::mirror::ClassLoader> Redefiner::ClassRedefinition::GetClassLoader() { |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 740 | return GetMirrorClass()->GetClassLoader(); |
| 741 | } |
| 742 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 743 | art::mirror::DexCache* Redefiner::ClassRedefinition::CreateNewDexCache( |
| 744 | art::Handle<art::mirror::ClassLoader> loader) { |
Alex Light | 07f0621 | 2017-06-01 14:01:43 -0700 | [diff] [blame] | 745 | art::StackHandleScope<2> hs(driver_->self_); |
| 746 | art::ClassLinker* cl = driver_->runtime_->GetClassLinker(); |
| 747 | art::Handle<art::mirror::DexCache> cache(hs.NewHandle( |
| 748 | art::ObjPtr<art::mirror::DexCache>::DownCast( |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 749 | art::GetClassRoot<art::mirror::DexCache>(cl)->AllocObject(driver_->self_)))); |
Alex Light | 07f0621 | 2017-06-01 14:01:43 -0700 | [diff] [blame] | 750 | if (cache.IsNull()) { |
| 751 | driver_->self_->AssertPendingOOMException(); |
| 752 | return nullptr; |
| 753 | } |
| 754 | art::Handle<art::mirror::String> location(hs.NewHandle( |
| 755 | cl->GetInternTable()->InternStrong(dex_file_->GetLocation().c_str()))); |
| 756 | if (location.IsNull()) { |
| 757 | driver_->self_->AssertPendingOOMException(); |
| 758 | return nullptr; |
| 759 | } |
| 760 | art::WriterMutexLock mu(driver_->self_, *art::Locks::dex_lock_); |
David Srbecky | 86d6cd5 | 2020-12-02 18:13:10 +0000 | [diff] [blame] | 761 | cache->SetLocation(location.Get()); |
David Srbecky | 33df0e3 | 2021-09-30 14:36:32 +0000 | [diff] [blame] | 762 | cache->Initialize(dex_file_.get(), loader.Get()); |
Alex Light | 07f0621 | 2017-06-01 14:01:43 -0700 | [diff] [blame] | 763 | return cache.Get(); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 764 | } |
| 765 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 766 | void Redefiner::RecordFailure(jvmtiError result, |
| 767 | const std::string& class_sig, |
| 768 | const std::string& error_msg) { |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 769 | *error_msg_ = StringPrintf("Unable to perform redefinition of '%s': %s", |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 770 | class_sig.c_str(), |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 771 | error_msg.c_str()); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 772 | result_ = result; |
| 773 | } |
| 774 | |
Alex Light | 2f814aa | 2017-03-24 15:21:34 +0000 | [diff] [blame] | 775 | art::mirror::Object* Redefiner::ClassRedefinition::AllocateOrGetOriginalDexFile() { |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 776 | // If we have been specifically given a new set of bytes use that |
| 777 | if (original_dex_file_.size() != 0) { |
Alex Light | 440b5d9 | 2017-01-24 15:32:25 -0800 | [diff] [blame] | 778 | return art::mirror::ByteArray::AllocateAndFill( |
| 779 | driver_->self_, |
Vladimir Marko | e1993c7 | 2017-06-14 17:01:38 +0100 | [diff] [blame] | 780 | reinterpret_cast<const signed char*>(original_dex_file_.data()), |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 781 | original_dex_file_.size()).Ptr(); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 782 | } |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 783 | |
| 784 | // See if we already have one set. |
| 785 | art::ObjPtr<art::mirror::ClassExt> ext(GetMirrorClass()->GetExtData()); |
| 786 | if (!ext.IsNull()) { |
Alex Light | 2f814aa | 2017-03-24 15:21:34 +0000 | [diff] [blame] | 787 | art::ObjPtr<art::mirror::Object> old_original_dex_file(ext->GetOriginalDexFile()); |
| 788 | if (!old_original_dex_file.IsNull()) { |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 789 | // We do. Use it. |
Alex Light | 2f814aa | 2017-03-24 15:21:34 +0000 | [diff] [blame] | 790 | return old_original_dex_file.Ptr(); |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 791 | } |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 792 | } |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 793 | |
Alex Light | 2f814aa | 2017-03-24 15:21:34 +0000 | [diff] [blame] | 794 | // return the current dex_cache which has the dex file in it. |
| 795 | art::ObjPtr<art::mirror::DexCache> current_dex_cache(GetMirrorClass()->GetDexCache()); |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 796 | // TODO Handle this or make it so it cannot happen. |
Alex Light | 2f814aa | 2017-03-24 15:21:34 +0000 | [diff] [blame] | 797 | if (current_dex_cache->GetDexFile()->NumClassDefs() != 1) { |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 798 | LOG(WARNING) << "Current dex file has more than one class in it. Calling RetransformClasses " |
| 799 | << "on this class might fail if no transformations are applied to it!"; |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 800 | } |
Alex Light | 2f814aa | 2017-03-24 15:21:34 +0000 | [diff] [blame] | 801 | return current_dex_cache.Ptr(); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 802 | } |
| 803 | |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 804 | struct CallbackCtx { |
Alex Light | eee0bd4 | 2017-02-14 15:31:45 +0000 | [diff] [blame] | 805 | ObsoleteMap* obsolete_map; |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 806 | art::LinearAlloc* allocator; |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 807 | std::unordered_set<art::ArtMethod*> obsolete_methods; |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 808 | |
Alex Light | eee0bd4 | 2017-02-14 15:31:45 +0000 | [diff] [blame] | 809 | explicit CallbackCtx(ObsoleteMap* map, art::LinearAlloc* alloc) |
| 810 | : obsolete_map(map), allocator(alloc) {} |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 811 | }; |
| 812 | |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 813 | void DoAllocateObsoleteMethodsCallback(art::Thread* t, void* vdata) NO_THREAD_SAFETY_ANALYSIS { |
| 814 | CallbackCtx* data = reinterpret_cast<CallbackCtx*>(vdata); |
Alex Light | 007ada2 | 2017-01-10 13:33:56 -0800 | [diff] [blame] | 815 | ObsoleteMethodStackVisitor::UpdateObsoleteFrames(t, |
| 816 | data->allocator, |
| 817 | data->obsolete_methods, |
Alex Light | eee0bd4 | 2017-02-14 15:31:45 +0000 | [diff] [blame] | 818 | data->obsolete_map); |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 819 | } |
| 820 | |
| 821 | // This creates any ArtMethod* structures needed for obsolete methods and ensures that the stack is |
| 822 | // updated so they will be run. |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 823 | // TODO Rewrite so we can do this only once regardless of how many redefinitions there are. |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 824 | void Redefiner::ClassRedefinition::FindAndAllocateObsoleteMethods( |
| 825 | art::ObjPtr<art::mirror::Class> art_klass) { |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 826 | DCHECK(!IsStructuralRedefinition()); |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 827 | art::ScopedAssertNoThreadSuspension ns("No thread suspension during thread stack walking"); |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 828 | art::ObjPtr<art::mirror::ClassExt> ext = art_klass->GetExtData(); |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 829 | CHECK(ext->GetObsoleteMethods() != nullptr); |
Alex Light | 7916f20 | 2017-01-27 09:00:15 -0800 | [diff] [blame] | 830 | art::ClassLinker* linker = driver_->runtime_->GetClassLinker(); |
Alex Light | eee0bd4 | 2017-02-14 15:31:45 +0000 | [diff] [blame] | 831 | // This holds pointers to the obsolete methods map fields which are updated as needed. |
| 832 | ObsoleteMap map(ext->GetObsoleteMethods(), ext->GetObsoleteDexCaches(), art_klass->GetDexCache()); |
| 833 | CallbackCtx ctx(&map, linker->GetAllocatorForClassLoader(art_klass->GetClassLoader())); |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 834 | // Add all the declared methods to the map |
| 835 | for (auto& m : art_klass->GetDeclaredMethods(art::kRuntimePointerSize)) { |
Alex Light | 7532d58 | 2017-02-13 16:36:06 -0800 | [diff] [blame] | 836 | if (m.IsIntrinsic()) { |
| 837 | LOG(WARNING) << "Redefining intrinsic method " << m.PrettyMethod() << ". This may cause the " |
| 838 | << "unexpected use of the original definition of " << m.PrettyMethod() << "in " |
| 839 | << "methods that have already been compiled."; |
| 840 | } |
Alex Light | eee0bd4 | 2017-02-14 15:31:45 +0000 | [diff] [blame] | 841 | // It is possible to simply filter out some methods where they cannot really become obsolete, |
| 842 | // such as native methods and keep their original (possibly optimized) implementations. We don't |
| 843 | // do this, however, since we would need to mark these functions (still in the classes |
| 844 | // declared_methods array) as obsolete so we will find the correct dex file to get meta-data |
| 845 | // from (for example about stack-frame size). Furthermore we would be unable to get some useful |
| 846 | // error checking from the interpreter which ensure we don't try to start executing obsolete |
| 847 | // methods. |
Nicolas Geoffray | 7558d27 | 2017-02-10 10:01:47 +0000 | [diff] [blame] | 848 | ctx.obsolete_methods.insert(&m); |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 849 | } |
| 850 | { |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 851 | art::MutexLock mu(driver_->self_, *art::Locks::thread_list_lock_); |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 852 | art::ThreadList* list = art::Runtime::Current()->GetThreadList(); |
| 853 | list->ForEach(DoAllocateObsoleteMethodsCallback, static_cast<void*>(&ctx)); |
Vladimir Marko | eab0248 | 2019-05-09 10:28:17 +0100 | [diff] [blame] | 854 | // After we've done walking all threads' stacks and updating method pointers on them, |
| 855 | // update JIT data structures (used by the stack walk above) to point to the new methods. |
Alex Light | e7a3354 | 2019-04-10 14:22:49 -0700 | [diff] [blame] | 856 | art::jit::Jit* jit = art::Runtime::Current()->GetJit(); |
| 857 | if (jit != nullptr) { |
| 858 | for (const ObsoleteMap::ObsoleteMethodPair& it : *ctx.obsolete_map) { |
| 859 | // Notify the JIT we are making this obsolete method. It will update the jit's internal |
| 860 | // structures to keep track of the new obsolete method. |
| 861 | jit->GetCodeCache()->MoveObsoleteMethod(it.old_method, it.obsolete_method); |
| 862 | } |
| 863 | } |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 864 | } |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 865 | } |
| 866 | |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 867 | namespace { |
| 868 | template <typename T> struct SignatureType {}; |
| 869 | template <> struct SignatureType<art::ArtField> { using type = std::string_view; }; |
| 870 | template <> struct SignatureType<art::ArtMethod> { using type = art::Signature; }; |
| 871 | |
| 872 | template <typename T> struct NameAndSignature { |
| 873 | public: |
| 874 | using SigType = typename SignatureType<T>::type; |
| 875 | |
| 876 | NameAndSignature(const art::DexFile* dex_file, uint32_t id); |
| 877 | |
| 878 | NameAndSignature(const std::string_view& name, const SigType& sig) : name_(name), sig_(sig) {} |
| 879 | |
| 880 | bool operator==(const NameAndSignature<T>& o) { |
| 881 | return name_ == o.name_ && sig_ == o.sig_; |
Alex Light | 6161f13 | 2017-01-25 10:30:20 -0800 | [diff] [blame] | 882 | } |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 883 | |
| 884 | std::ostream& dump(std::ostream& os) const { |
| 885 | return os << "'" << name_ << "' (sig: " << sig_ << ")"; |
| 886 | } |
| 887 | |
| 888 | std::string ToString() const { |
| 889 | std::ostringstream os; |
| 890 | os << *this; |
| 891 | return os.str(); |
| 892 | } |
| 893 | |
| 894 | std::string_view name_; |
| 895 | SigType sig_; |
| 896 | }; |
| 897 | |
| 898 | template <typename T> |
| 899 | std::ostream& operator<<(std::ostream& os, const NameAndSignature<T>& nas) { |
| 900 | return nas.dump(os); |
Alex Light | 6161f13 | 2017-01-25 10:30:20 -0800 | [diff] [blame] | 901 | } |
| 902 | |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 903 | using FieldNameAndSignature = NameAndSignature<art::ArtField>; |
| 904 | template <> |
| 905 | FieldNameAndSignature::NameAndSignature(const art::DexFile* dex_file, uint32_t id) |
| 906 | : FieldNameAndSignature(dex_file->GetFieldName(dex_file->GetFieldId(id)), |
| 907 | dex_file->GetFieldTypeDescriptor(dex_file->GetFieldId(id))) {} |
| 908 | |
| 909 | using MethodNameAndSignature = NameAndSignature<art::ArtMethod>; |
| 910 | template <> |
| 911 | MethodNameAndSignature::NameAndSignature(const art::DexFile* dex_file, uint32_t id) |
| 912 | : MethodNameAndSignature(dex_file->GetMethodName(dex_file->GetMethodId(id)), |
| 913 | dex_file->GetMethodSignature(dex_file->GetMethodId(id))) {} |
| 914 | |
| 915 | } // namespace |
| 916 | |
| 917 | void Redefiner::ClassRedefinition::RecordNewMethodAdded() { |
| 918 | DCHECK(driver_->IsStructuralRedefinition()); |
| 919 | added_methods_ = true; |
| 920 | } |
| 921 | void Redefiner::ClassRedefinition::RecordNewFieldAdded() { |
| 922 | DCHECK(driver_->IsStructuralRedefinition()); |
| 923 | added_fields_ = true; |
| 924 | } |
| 925 | |
| 926 | bool Redefiner::ClassRedefinition::CheckMethods() { |
Alex Light | 6161f13 | 2017-01-25 10:30:20 -0800 | [diff] [blame] | 927 | art::StackHandleScope<1> hs(driver_->self_); |
| 928 | art::Handle<art::mirror::Class> h_klass(hs.NewHandle(GetMirrorClass())); |
| 929 | DCHECK_EQ(dex_file_->NumClassDefs(), 1u); |
| 930 | |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 931 | // Make sure we have the same number of methods (or the same or greater if we're structural). |
Mathieu Chartier | 4ac9ade | 2018-07-24 10:27:21 -0700 | [diff] [blame] | 932 | art::ClassAccessor accessor(*dex_file_, dex_file_->GetClassDef(0)); |
| 933 | uint32_t num_new_method = accessor.NumMethods(); |
Alex Light | 6161f13 | 2017-01-25 10:30:20 -0800 | [diff] [blame] | 934 | uint32_t num_old_method = h_klass->GetDeclaredMethodsSlice(art::kRuntimePointerSize).size(); |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 935 | const bool is_structural = driver_->IsStructuralRedefinition(); |
| 936 | if (!is_structural && num_new_method != num_old_method) { |
Alex Light | 6161f13 | 2017-01-25 10:30:20 -0800 | [diff] [blame] | 937 | bool bigger = num_new_method > num_old_method; |
| 938 | RecordFailure(bigger ? ERR(UNSUPPORTED_REDEFINITION_METHOD_ADDED) |
| 939 | : ERR(UNSUPPORTED_REDEFINITION_METHOD_DELETED), |
| 940 | StringPrintf("Total number of declared methods changed from %d to %d", |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 941 | num_old_method, |
| 942 | num_new_method)); |
Alex Light | 6161f13 | 2017-01-25 10:30:20 -0800 | [diff] [blame] | 943 | return false; |
| 944 | } |
| 945 | |
| 946 | // Skip all of the fields. We should have already checked this. |
Alex Light | 6161f13 | 2017-01-25 10:30:20 -0800 | [diff] [blame] | 947 | // Check each of the methods. NB we don't need to specifically check for removals since the 2 dex |
| 948 | // files have the same number of methods, which means there must be an equal amount of additions |
Mathieu Chartier | 4ac9ade | 2018-07-24 10:27:21 -0700 | [diff] [blame] | 949 | // and removals. We should have already checked the fields. |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 950 | const art::DexFile& old_dex_file = h_klass->GetDexFile(); |
| 951 | art::ClassAccessor old_accessor(old_dex_file, *h_klass->GetClassDef()); |
| 952 | // We need this to check for methods going missing in structural cases. |
| 953 | std::vector<bool> seen_old_methods( |
| 954 | (kCheckAllMethodsSeenOnce || is_structural) ? old_accessor.NumMethods() : 0, false); |
| 955 | const auto old_methods = old_accessor.GetMethods(); |
| 956 | for (const art::ClassAccessor::Method& new_method : accessor.GetMethods()) { |
Alex Light | 6161f13 | 2017-01-25 10:30:20 -0800 | [diff] [blame] | 957 | // Get the data on the method we are searching for |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 958 | MethodNameAndSignature new_method_id(dex_file_.get(), new_method.GetIndex()); |
| 959 | const auto old_iter = |
| 960 | std::find_if(old_methods.cbegin(), old_methods.cend(), [&](const auto& current_old_method) { |
| 961 | MethodNameAndSignature old_method_id(&old_dex_file, current_old_method.GetIndex()); |
| 962 | return old_method_id == new_method_id; |
| 963 | }); |
| 964 | |
Alex Light | 986914b | 2019-11-19 01:12:25 +0000 | [diff] [blame] | 965 | if (!new_method.IsStaticOrDirect()) { |
| 966 | RecordHasVirtualMembers(); |
| 967 | } |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 968 | if (old_iter == old_methods.cend()) { |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 969 | if (is_structural) { |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 970 | RecordNewMethodAdded(); |
| 971 | } else { |
| 972 | RecordFailure( |
| 973 | ERR(UNSUPPORTED_REDEFINITION_METHOD_ADDED), |
| 974 | StringPrintf("Unknown virtual method %s was added!", new_method_id.ToString().c_str())); |
| 975 | return false; |
| 976 | } |
| 977 | } else if (new_method.GetAccessFlags() != old_iter->GetAccessFlags()) { |
| 978 | RecordFailure( |
| 979 | ERR(UNSUPPORTED_REDEFINITION_METHOD_MODIFIERS_CHANGED), |
| 980 | StringPrintf("method %s had different access flags", new_method_id.ToString().c_str())); |
Alex Light | 6161f13 | 2017-01-25 10:30:20 -0800 | [diff] [blame] | 981 | return false; |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 982 | } else if (kCheckAllMethodsSeenOnce || is_structural) { |
| 983 | // We only need this if we are structural. |
| 984 | size_t off = std::distance(old_methods.cbegin(), old_iter); |
| 985 | DCHECK(!seen_old_methods[off]) |
| 986 | << "field at " << off << "(" |
| 987 | << MethodNameAndSignature(&old_dex_file, old_iter->GetIndex()) << ") already seen?"; |
| 988 | seen_old_methods[off] = true; |
Alex Light | 6161f13 | 2017-01-25 10:30:20 -0800 | [diff] [blame] | 989 | } |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 990 | } |
| 991 | if ((kCheckAllMethodsSeenOnce || is_structural) && |
| 992 | !std::all_of(seen_old_methods.cbegin(), seen_old_methods.cend(), [](auto x) { return x; })) { |
| 993 | DCHECK(is_structural) << "We should have hit an earlier failure before getting here!"; |
| 994 | auto first_fail = |
| 995 | std::find_if(seen_old_methods.cbegin(), seen_old_methods.cend(), [](auto x) { return !x; }); |
| 996 | auto off = std::distance(seen_old_methods.cbegin(), first_fail); |
| 997 | auto fail = old_methods.cbegin(); |
| 998 | std::advance(fail, off); |
| 999 | RecordFailure( |
| 1000 | ERR(UNSUPPORTED_REDEFINITION_METHOD_DELETED), |
| 1001 | StringPrintf("Method %s missing!", |
Alex Light | daad67f | 2019-12-16 13:01:46 -0800 | [diff] [blame] | 1002 | MethodNameAndSignature(&old_dex_file, fail->GetIndex()).ToString().c_str())); |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 1003 | return false; |
Alex Light | 6161f13 | 2017-01-25 10:30:20 -0800 | [diff] [blame] | 1004 | } |
| 1005 | return true; |
| 1006 | } |
| 1007 | |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 1008 | bool Redefiner::ClassRedefinition::CheckFields() { |
Alex Light | 6161f13 | 2017-01-25 10:30:20 -0800 | [diff] [blame] | 1009 | art::StackHandleScope<1> hs(driver_->self_); |
| 1010 | art::Handle<art::mirror::Class> h_klass(hs.NewHandle(GetMirrorClass())); |
| 1011 | DCHECK_EQ(dex_file_->NumClassDefs(), 1u); |
Mathieu Chartier | 4ac9ade | 2018-07-24 10:27:21 -0700 | [diff] [blame] | 1012 | art::ClassAccessor new_accessor(*dex_file_, dex_file_->GetClassDef(0)); |
| 1013 | |
Alex Light | 6161f13 | 2017-01-25 10:30:20 -0800 | [diff] [blame] | 1014 | const art::DexFile& old_dex_file = h_klass->GetDexFile(); |
Mathieu Chartier | 4ac9ade | 2018-07-24 10:27:21 -0700 | [diff] [blame] | 1015 | art::ClassAccessor old_accessor(old_dex_file, *h_klass->GetClassDef()); |
Alex Light | 6161f13 | 2017-01-25 10:30:20 -0800 | [diff] [blame] | 1016 | // Instance and static fields can be differentiated by their flags so no need to check them |
| 1017 | // separately. |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 1018 | std::vector<bool> seen_old_fields(old_accessor.NumFields(), false); |
| 1019 | const auto old_fields = old_accessor.GetFields(); |
Mathieu Chartier | 4ac9ade | 2018-07-24 10:27:21 -0700 | [diff] [blame] | 1020 | for (const art::ClassAccessor::Field& new_field : new_accessor.GetFields()) { |
Alex Light | 6161f13 | 2017-01-25 10:30:20 -0800 | [diff] [blame] | 1021 | // Get the data on the method we are searching for |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 1022 | FieldNameAndSignature new_field_id(dex_file_.get(), new_field.GetIndex()); |
| 1023 | const auto old_iter = |
| 1024 | std::find_if(old_fields.cbegin(), old_fields.cend(), [&](const auto& old_iter) { |
| 1025 | FieldNameAndSignature old_field_id(&old_dex_file, old_iter.GetIndex()); |
| 1026 | return old_field_id == new_field_id; |
| 1027 | }); |
Alex Light | 986914b | 2019-11-19 01:12:25 +0000 | [diff] [blame] | 1028 | if (!new_field.IsStatic()) { |
| 1029 | RecordHasVirtualMembers(); |
| 1030 | } |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 1031 | if (old_iter == old_fields.cend()) { |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 1032 | if (driver_->IsStructuralRedefinition()) { |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 1033 | RecordNewFieldAdded(); |
| 1034 | } else { |
| 1035 | RecordFailure(ERR(UNSUPPORTED_REDEFINITION_SCHEMA_CHANGED), |
| 1036 | StringPrintf("Unknown field %s added!", new_field_id.ToString().c_str())); |
| 1037 | return false; |
| 1038 | } |
| 1039 | } else if (new_field.GetAccessFlags() != old_iter->GetAccessFlags()) { |
| 1040 | RecordFailure( |
| 1041 | ERR(UNSUPPORTED_REDEFINITION_SCHEMA_CHANGED), |
| 1042 | StringPrintf("Field %s had different access flags", new_field_id.ToString().c_str())); |
Alex Light | 6161f13 | 2017-01-25 10:30:20 -0800 | [diff] [blame] | 1043 | return false; |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 1044 | } else { |
| 1045 | size_t off = std::distance(old_fields.cbegin(), old_iter); |
| 1046 | DCHECK(!seen_old_fields[off]) |
| 1047 | << "field at " << off << "(" << FieldNameAndSignature(&old_dex_file, old_iter->GetIndex()) |
| 1048 | << ") already seen?"; |
| 1049 | seen_old_fields[off] = true; |
Alex Light | 6161f13 | 2017-01-25 10:30:20 -0800 | [diff] [blame] | 1050 | } |
Alex Light | 6161f13 | 2017-01-25 10:30:20 -0800 | [diff] [blame] | 1051 | } |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 1052 | if (!std::all_of(seen_old_fields.cbegin(), seen_old_fields.cend(), [](auto x) { return x; })) { |
| 1053 | auto first_fail = |
| 1054 | std::find_if(seen_old_fields.cbegin(), seen_old_fields.cend(), [](auto x) { return !x; }); |
| 1055 | auto off = std::distance(seen_old_fields.cbegin(), first_fail); |
| 1056 | auto fail = old_fields.cbegin(); |
| 1057 | std::advance(fail, off); |
| 1058 | RecordFailure( |
| 1059 | ERR(UNSUPPORTED_REDEFINITION_SCHEMA_CHANGED), |
| 1060 | StringPrintf("Field %s is missing!", |
| 1061 | FieldNameAndSignature(&old_dex_file, fail->GetIndex()).ToString().c_str())); |
Alex Light | 6161f13 | 2017-01-25 10:30:20 -0800 | [diff] [blame] | 1062 | return false; |
| 1063 | } |
| 1064 | return true; |
| 1065 | } |
| 1066 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 1067 | bool Redefiner::ClassRedefinition::CheckClass() { |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 1068 | art::StackHandleScope<1> hs(driver_->self_); |
Alex Light | 460d1b4 | 2017-01-10 15:37:17 +0000 | [diff] [blame] | 1069 | // Easy check that only 1 class def is present. |
| 1070 | if (dex_file_->NumClassDefs() != 1) { |
| 1071 | RecordFailure(ERR(ILLEGAL_ARGUMENT), |
| 1072 | StringPrintf("Expected 1 class def in dex file but found %d", |
| 1073 | dex_file_->NumClassDefs())); |
| 1074 | return false; |
| 1075 | } |
| 1076 | // Get the ClassDef from the new DexFile. |
| 1077 | // Since the dex file has only a single class def the index is always 0. |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 1078 | const art::dex::ClassDef& def = dex_file_->GetClassDef(0); |
Alex Light | 460d1b4 | 2017-01-10 15:37:17 +0000 | [diff] [blame] | 1079 | // Get the class as it is now. |
| 1080 | art::Handle<art::mirror::Class> current_class(hs.NewHandle(GetMirrorClass())); |
| 1081 | |
ShuJieWang | b087bb2 | 2021-11-24 11:46:16 +0800 | [diff] [blame] | 1082 | // Check whether the class object has been successfully acquired. |
| 1083 | if (!lock_acquired_) { |
| 1084 | std::string storage; |
| 1085 | RecordFailure(ERR(INTERNAL), |
| 1086 | StringPrintf("Failed to lock class object '%s'", |
| 1087 | current_class->GetDescriptor(&storage))); |
| 1088 | return false; |
| 1089 | } |
| 1090 | |
Alex Light | 460d1b4 | 2017-01-10 15:37:17 +0000 | [diff] [blame] | 1091 | // Check the access flags didn't change. |
| 1092 | if (def.GetJavaAccessFlags() != (current_class->GetAccessFlags() & art::kAccValidClassFlags)) { |
| 1093 | RecordFailure(ERR(UNSUPPORTED_REDEFINITION_CLASS_MODIFIERS_CHANGED), |
| 1094 | "Cannot change modifiers of class by redefinition"); |
| 1095 | return false; |
| 1096 | } |
| 1097 | |
| 1098 | // Check class name. |
| 1099 | // These should have been checked by the dexfile verifier on load. |
| 1100 | DCHECK_NE(def.class_idx_, art::dex::TypeIndex::Invalid()) << "Invalid type index"; |
| 1101 | const char* descriptor = dex_file_->StringByTypeIdx(def.class_idx_); |
| 1102 | DCHECK(descriptor != nullptr) << "Invalid dex file structure!"; |
| 1103 | if (!current_class->DescriptorEquals(descriptor)) { |
| 1104 | std::string storage; |
| 1105 | RecordFailure(ERR(NAMES_DONT_MATCH), |
| 1106 | StringPrintf("expected file to contain class called '%s' but found '%s'!", |
| 1107 | current_class->GetDescriptor(&storage), |
| 1108 | descriptor)); |
| 1109 | return false; |
| 1110 | } |
| 1111 | if (current_class->IsObjectClass()) { |
| 1112 | if (def.superclass_idx_ != art::dex::TypeIndex::Invalid()) { |
| 1113 | RecordFailure(ERR(UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED), "Superclass added!"); |
| 1114 | return false; |
| 1115 | } |
| 1116 | } else { |
| 1117 | const char* super_descriptor = dex_file_->StringByTypeIdx(def.superclass_idx_); |
| 1118 | DCHECK(descriptor != nullptr) << "Invalid dex file structure!"; |
| 1119 | if (!current_class->GetSuperClass()->DescriptorEquals(super_descriptor)) { |
| 1120 | RecordFailure(ERR(UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED), "Superclass changed"); |
| 1121 | return false; |
| 1122 | } |
| 1123 | } |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 1124 | const art::dex::TypeList* interfaces = dex_file_->GetInterfacesList(def); |
Alex Light | 460d1b4 | 2017-01-10 15:37:17 +0000 | [diff] [blame] | 1125 | if (interfaces == nullptr) { |
| 1126 | if (current_class->NumDirectInterfaces() != 0) { |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 1127 | // TODO Support this for kStructural. |
Alex Light | 460d1b4 | 2017-01-10 15:37:17 +0000 | [diff] [blame] | 1128 | RecordFailure(ERR(UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED), "Interfaces added"); |
| 1129 | return false; |
| 1130 | } |
| 1131 | } else { |
| 1132 | DCHECK(!current_class->IsProxyClass()); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 1133 | const art::dex::TypeList* current_interfaces = current_class->GetInterfaceTypeList(); |
Alex Light | 460d1b4 | 2017-01-10 15:37:17 +0000 | [diff] [blame] | 1134 | if (current_interfaces == nullptr || current_interfaces->Size() != interfaces->Size()) { |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 1135 | // TODO Support this for kStructural. |
Alex Light | 460d1b4 | 2017-01-10 15:37:17 +0000 | [diff] [blame] | 1136 | RecordFailure(ERR(UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED), "Interfaces added or removed"); |
| 1137 | return false; |
| 1138 | } |
| 1139 | // The order of interfaces is (barely) meaningful so we error if it changes. |
| 1140 | const art::DexFile& orig_dex_file = current_class->GetDexFile(); |
| 1141 | for (uint32_t i = 0; i < interfaces->Size(); i++) { |
| 1142 | if (strcmp( |
| 1143 | dex_file_->StringByTypeIdx(interfaces->GetTypeItem(i).type_idx_), |
| 1144 | orig_dex_file.StringByTypeIdx(current_interfaces->GetTypeItem(i).type_idx_)) != 0) { |
| 1145 | RecordFailure(ERR(UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED), |
| 1146 | "Interfaces changed or re-ordered"); |
| 1147 | return false; |
| 1148 | } |
| 1149 | } |
| 1150 | } |
Alex Light | 460d1b4 | 2017-01-10 15:37:17 +0000 | [diff] [blame] | 1151 | return true; |
| 1152 | } |
| 1153 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 1154 | bool Redefiner::ClassRedefinition::CheckRedefinable() { |
Alex Light | e4a8863 | 2017-01-10 07:41:24 -0800 | [diff] [blame] | 1155 | std::string err; |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 1156 | art::StackHandleScope<1> hs(driver_->self_); |
Alex Light | 460d1b4 | 2017-01-10 15:37:17 +0000 | [diff] [blame] | 1157 | |
Alex Light | e4a8863 | 2017-01-10 07:41:24 -0800 | [diff] [blame] | 1158 | art::Handle<art::mirror::Class> h_klass(hs.NewHandle(GetMirrorClass())); |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 1159 | jvmtiError res; |
Alex Light | d55b844 | 2019-10-15 15:46:07 -0700 | [diff] [blame] | 1160 | if (driver_->type_ == RedefinitionType::kStructural && this->IsStructuralRedefinition()) { |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 1161 | res = Redefiner::GetClassRedefinitionError<RedefinitionType::kStructural>(h_klass, &err); |
Alex Light | d55b844 | 2019-10-15 15:46:07 -0700 | [diff] [blame] | 1162 | } else { |
| 1163 | res = Redefiner::GetClassRedefinitionError<RedefinitionType::kNormal>(h_klass, &err); |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 1164 | } |
Alex Light | e4a8863 | 2017-01-10 07:41:24 -0800 | [diff] [blame] | 1165 | if (res != OK) { |
| 1166 | RecordFailure(res, err); |
| 1167 | return false; |
| 1168 | } else { |
| 1169 | return true; |
| 1170 | } |
Alex Light | 460d1b4 | 2017-01-10 15:37:17 +0000 | [diff] [blame] | 1171 | } |
| 1172 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 1173 | bool Redefiner::ClassRedefinition::CheckRedefinitionIsValid() { |
Alex Light | d55b844 | 2019-10-15 15:46:07 -0700 | [diff] [blame] | 1174 | return CheckClass() && CheckFields() && CheckMethods() && CheckRedefinable(); |
Alex Light | 460d1b4 | 2017-01-10 15:37:17 +0000 | [diff] [blame] | 1175 | } |
| 1176 | |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1177 | class RedefinitionDataIter; |
| 1178 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 1179 | // A wrapper that lets us hold onto the arbitrary sized data needed for redefinitions in a |
Orion Hodson | aacf977 | 2020-07-22 21:51:00 +0100 | [diff] [blame] | 1180 | // reasonable way. This adds no fields to the normal ObjectArray. By doing this we can avoid |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 1181 | // having to deal with the fact that we need to hold an arbitrary number of references live. |
| 1182 | class RedefinitionDataHolder { |
| 1183 | public: |
| 1184 | enum DataSlot : int32_t { |
| 1185 | kSlotSourceClassLoader = 0, |
| 1186 | kSlotJavaDexFile = 1, |
| 1187 | kSlotNewDexFileCookie = 2, |
| 1188 | kSlotNewDexCache = 3, |
| 1189 | kSlotMirrorClass = 4, |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 1190 | kSlotOrigDexFile = 5, |
Alex Light | 1e3926a | 2017-04-07 10:38:06 -0700 | [diff] [blame] | 1191 | kSlotOldObsoleteMethods = 6, |
| 1192 | kSlotOldDexCaches = 7, |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 1193 | kSlotNewClassObject = 8, |
Alex Light | 986914b | 2019-11-19 01:12:25 +0000 | [diff] [blame] | 1194 | kSlotOldInstanceObjects = 9, |
| 1195 | kSlotNewInstanceObjects = 10, |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 1196 | kSlotOldClasses = 11, |
| 1197 | kSlotNewClasses = 12, |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 1198 | |
| 1199 | // Must be last one. |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 1200 | kNumSlots = 13, |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 1201 | }; |
| 1202 | |
| 1203 | // This needs to have a HandleScope passed in that is capable of creating a new Handle without |
| 1204 | // overflowing. Only one handle will be created. This object has a lifetime identical to that of |
| 1205 | // the passed in handle-scope. |
| 1206 | RedefinitionDataHolder(art::StackHandleScope<1>* hs, |
| 1207 | art::Runtime* runtime, |
| 1208 | art::Thread* self, |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1209 | std::vector<Redefiner::ClassRedefinition>* redefinitions) |
| 1210 | REQUIRES_SHARED(art::Locks::mutator_lock_) : |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 1211 | arr_(hs->NewHandle(art::mirror::ObjectArray<art::mirror::Object>::Alloc( |
| 1212 | self, |
| 1213 | art::GetClassRoot<art::mirror::ObjectArray<art::mirror::Object>>(runtime->GetClassLinker()), |
| 1214 | redefinitions->size() * kNumSlots))), |
Alex Light | fb53708 | 2019-12-10 14:38:34 -0800 | [diff] [blame] | 1215 | redefinitions_(redefinitions), |
| 1216 | initialized_(redefinitions_->size(), false), |
| 1217 | actually_structural_(redefinitions_->size(), false), |
| 1218 | initial_structural_(redefinitions_->size(), false) {} |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 1219 | |
| 1220 | bool IsNull() const REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1221 | return arr_.IsNull(); |
| 1222 | } |
| 1223 | |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 1224 | art::ObjPtr<art::mirror::ClassLoader> GetSourceClassLoader(jint klass_index) const |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 1225 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 1226 | return art::ObjPtr<art::mirror::ClassLoader>::DownCast( |
| 1227 | GetSlot(klass_index, kSlotSourceClassLoader)); |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 1228 | } |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 1229 | art::ObjPtr<art::mirror::Object> GetJavaDexFile(jint klass_index) const |
Alex Light | 8c889d2 | 2017-02-06 13:58:27 -0800 | [diff] [blame] | 1230 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 1231 | return GetSlot(klass_index, kSlotJavaDexFile); |
| 1232 | } |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 1233 | art::ObjPtr<art::mirror::LongArray> GetNewDexFileCookie(jint klass_index) const |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 1234 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 1235 | return art::ObjPtr<art::mirror::LongArray>::DownCast( |
| 1236 | GetSlot(klass_index, kSlotNewDexFileCookie)); |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 1237 | } |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 1238 | art::ObjPtr<art::mirror::DexCache> GetNewDexCache(jint klass_index) const |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 1239 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 1240 | return art::ObjPtr<art::mirror::DexCache>::DownCast(GetSlot(klass_index, kSlotNewDexCache)); |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 1241 | } |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 1242 | art::ObjPtr<art::mirror::Class> GetMirrorClass(jint klass_index) const |
Alex Light | 8c889d2 | 2017-02-06 13:58:27 -0800 | [diff] [blame] | 1243 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 1244 | return art::ObjPtr<art::mirror::Class>::DownCast(GetSlot(klass_index, kSlotMirrorClass)); |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 1245 | } |
| 1246 | |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 1247 | art::ObjPtr<art::mirror::Object> GetOriginalDexFile(jint klass_index) const |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 1248 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 1249 | return art::ObjPtr<art::mirror::Object>::DownCast(GetSlot(klass_index, kSlotOrigDexFile)); |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 1250 | } |
| 1251 | |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 1252 | art::ObjPtr<art::mirror::PointerArray> GetOldObsoleteMethods(jint klass_index) const |
Alex Light | 1e3926a | 2017-04-07 10:38:06 -0700 | [diff] [blame] | 1253 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 1254 | return art::ObjPtr<art::mirror::PointerArray>::DownCast( |
Alex Light | 1e3926a | 2017-04-07 10:38:06 -0700 | [diff] [blame] | 1255 | GetSlot(klass_index, kSlotOldObsoleteMethods)); |
| 1256 | } |
| 1257 | |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 1258 | art::ObjPtr<art::mirror::ObjectArray<art::mirror::DexCache>> GetOldDexCaches( |
| 1259 | jint klass_index) const REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1260 | return art::ObjPtr<art::mirror::ObjectArray<art::mirror::DexCache>>::DownCast( |
Alex Light | 1e3926a | 2017-04-07 10:38:06 -0700 | [diff] [blame] | 1261 | GetSlot(klass_index, kSlotOldDexCaches)); |
| 1262 | } |
| 1263 | |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 1264 | art::ObjPtr<art::mirror::Class> GetNewClassObject(jint klass_index) const |
| 1265 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1266 | return art::ObjPtr<art::mirror::Class>::DownCast(GetSlot(klass_index, kSlotNewClassObject)); |
| 1267 | } |
| 1268 | |
Alex Light | 986914b | 2019-11-19 01:12:25 +0000 | [diff] [blame] | 1269 | art::ObjPtr<art::mirror::ObjectArray<art::mirror::Object>> GetOldInstanceObjects( |
| 1270 | jint klass_index) const REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1271 | return art::ObjPtr<art::mirror::ObjectArray<art::mirror::Object>>::DownCast( |
| 1272 | GetSlot(klass_index, kSlotOldInstanceObjects)); |
| 1273 | } |
| 1274 | |
| 1275 | art::ObjPtr<art::mirror::ObjectArray<art::mirror::Object>> GetNewInstanceObjects( |
| 1276 | jint klass_index) const REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1277 | return art::ObjPtr<art::mirror::ObjectArray<art::mirror::Object>>::DownCast( |
| 1278 | GetSlot(klass_index, kSlotNewInstanceObjects)); |
| 1279 | } |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 1280 | art::ObjPtr<art::mirror::ObjectArray<art::mirror::Class>> GetOldClasses(jint klass_index) const |
| 1281 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1282 | return art::ObjPtr<art::mirror::ObjectArray<art::mirror::Class>>::DownCast( |
| 1283 | GetSlot(klass_index, kSlotOldClasses)); |
| 1284 | } |
| 1285 | art::ObjPtr<art::mirror::ObjectArray<art::mirror::Class>> GetNewClasses(jint klass_index) const |
| 1286 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1287 | return art::ObjPtr<art::mirror::ObjectArray<art::mirror::Class>>::DownCast( |
| 1288 | GetSlot(klass_index, kSlotNewClasses)); |
| 1289 | } |
Alex Light | fb53708 | 2019-12-10 14:38:34 -0800 | [diff] [blame] | 1290 | bool IsInitialized(jint klass_index) REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1291 | return initialized_[klass_index]; |
| 1292 | } |
| 1293 | bool IsActuallyStructural(jint klass_index) REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1294 | return actually_structural_[klass_index]; |
| 1295 | } |
| 1296 | |
| 1297 | bool IsInitialStructural(jint klass_index) REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1298 | return initial_structural_[klass_index]; |
| 1299 | } |
Alex Light | 986914b | 2019-11-19 01:12:25 +0000 | [diff] [blame] | 1300 | |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 1301 | void SetSourceClassLoader(jint klass_index, art::ObjPtr<art::mirror::ClassLoader> loader) |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 1302 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1303 | SetSlot(klass_index, kSlotSourceClassLoader, loader); |
| 1304 | } |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 1305 | void SetJavaDexFile(jint klass_index, art::ObjPtr<art::mirror::Object> dexfile) |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 1306 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1307 | SetSlot(klass_index, kSlotJavaDexFile, dexfile); |
| 1308 | } |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 1309 | void SetNewDexFileCookie(jint klass_index, art::ObjPtr<art::mirror::LongArray> cookie) |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 1310 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1311 | SetSlot(klass_index, kSlotNewDexFileCookie, cookie); |
| 1312 | } |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 1313 | void SetNewDexCache(jint klass_index, art::ObjPtr<art::mirror::DexCache> cache) |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 1314 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1315 | SetSlot(klass_index, kSlotNewDexCache, cache); |
| 1316 | } |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 1317 | void SetMirrorClass(jint klass_index, art::ObjPtr<art::mirror::Class> klass) |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 1318 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1319 | SetSlot(klass_index, kSlotMirrorClass, klass); |
| 1320 | } |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 1321 | void SetOriginalDexFile(jint klass_index, art::ObjPtr<art::mirror::Object> bytes) |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 1322 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1323 | SetSlot(klass_index, kSlotOrigDexFile, bytes); |
| 1324 | } |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 1325 | void SetOldObsoleteMethods(jint klass_index, art::ObjPtr<art::mirror::PointerArray> methods) |
Alex Light | 1e3926a | 2017-04-07 10:38:06 -0700 | [diff] [blame] | 1326 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1327 | SetSlot(klass_index, kSlotOldObsoleteMethods, methods); |
| 1328 | } |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 1329 | void SetOldDexCaches(jint klass_index, |
| 1330 | art::ObjPtr<art::mirror::ObjectArray<art::mirror::DexCache>> caches) |
Alex Light | 1e3926a | 2017-04-07 10:38:06 -0700 | [diff] [blame] | 1331 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1332 | SetSlot(klass_index, kSlotOldDexCaches, caches); |
| 1333 | } |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 1334 | |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 1335 | void SetNewClassObject(jint klass_index, art::ObjPtr<art::mirror::Class> klass) |
| 1336 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1337 | SetSlot(klass_index, kSlotNewClassObject, klass); |
| 1338 | } |
| 1339 | |
Alex Light | 986914b | 2019-11-19 01:12:25 +0000 | [diff] [blame] | 1340 | void SetOldInstanceObjects(jint klass_index, |
| 1341 | art::ObjPtr<art::mirror::ObjectArray<art::mirror::Object>> objs) |
| 1342 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1343 | SetSlot(klass_index, kSlotOldInstanceObjects, objs); |
| 1344 | } |
| 1345 | void SetNewInstanceObjects(jint klass_index, |
| 1346 | art::ObjPtr<art::mirror::ObjectArray<art::mirror::Object>> objs) |
| 1347 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1348 | SetSlot(klass_index, kSlotNewInstanceObjects, objs); |
| 1349 | } |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 1350 | void SetOldClasses(jint klass_index, |
| 1351 | art::ObjPtr<art::mirror::ObjectArray<art::mirror::Class>> klasses) |
| 1352 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1353 | SetSlot(klass_index, kSlotOldClasses, klasses); |
| 1354 | } |
| 1355 | void SetNewClasses(jint klass_index, |
| 1356 | art::ObjPtr<art::mirror::ObjectArray<art::mirror::Class>> klasses) |
| 1357 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1358 | SetSlot(klass_index, kSlotNewClasses, klasses); |
| 1359 | } |
Alex Light | fb53708 | 2019-12-10 14:38:34 -0800 | [diff] [blame] | 1360 | void SetInitialized(jint klass_index) REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1361 | initialized_[klass_index] = true; |
| 1362 | } |
| 1363 | void SetActuallyStructural(jint klass_index) REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1364 | actually_structural_[klass_index] = true; |
| 1365 | } |
| 1366 | void SetInitialStructural(jint klass_index) REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1367 | initial_structural_[klass_index] = true; |
| 1368 | } |
Alex Light | 8c889d2 | 2017-02-06 13:58:27 -0800 | [diff] [blame] | 1369 | int32_t Length() const REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 1370 | return arr_->GetLength() / kNumSlots; |
| 1371 | } |
| 1372 | |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1373 | std::vector<Redefiner::ClassRedefinition>* GetRedefinitions() |
| 1374 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1375 | return redefinitions_; |
| 1376 | } |
| 1377 | |
| 1378 | bool operator==(const RedefinitionDataHolder& other) const |
| 1379 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1380 | return arr_.Get() == other.arr_.Get(); |
| 1381 | } |
| 1382 | |
| 1383 | bool operator!=(const RedefinitionDataHolder& other) const |
| 1384 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1385 | return !(*this == other); |
| 1386 | } |
| 1387 | |
| 1388 | RedefinitionDataIter begin() REQUIRES_SHARED(art::Locks::mutator_lock_); |
| 1389 | RedefinitionDataIter end() REQUIRES_SHARED(art::Locks::mutator_lock_); |
| 1390 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 1391 | private: |
Alex Light | 8c889d2 | 2017-02-06 13:58:27 -0800 | [diff] [blame] | 1392 | mutable art::Handle<art::mirror::ObjectArray<art::mirror::Object>> arr_; |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1393 | std::vector<Redefiner::ClassRedefinition>* redefinitions_; |
Alex Light | fb53708 | 2019-12-10 14:38:34 -0800 | [diff] [blame] | 1394 | // Used to mark a particular redefinition as fully initialized. |
| 1395 | std::vector<bool> initialized_; |
| 1396 | // Used to mark a redefinition as 'actually' structural. That is either the redefinition is |
| 1397 | // structural or a superclass is. |
| 1398 | std::vector<bool> actually_structural_; |
| 1399 | // Used to mark a redefinition as the initial structural redefinition. This redefinition will take |
| 1400 | // care of updating all of its subtypes. |
| 1401 | std::vector<bool> initial_structural_; |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 1402 | |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 1403 | art::ObjPtr<art::mirror::Object> GetSlot(jint klass_index, DataSlot slot) const |
| 1404 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 1405 | DCHECK_LT(klass_index, Length()); |
| 1406 | return arr_->Get((kNumSlots * klass_index) + slot); |
| 1407 | } |
| 1408 | |
| 1409 | void SetSlot(jint klass_index, |
| 1410 | DataSlot slot, |
| 1411 | art::ObjPtr<art::mirror::Object> obj) REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1412 | DCHECK(!art::Runtime::Current()->IsActiveTransaction()); |
| 1413 | DCHECK_LT(klass_index, Length()); |
| 1414 | arr_->Set<false>((kNumSlots * klass_index) + slot, obj); |
| 1415 | } |
| 1416 | |
| 1417 | DISALLOW_COPY_AND_ASSIGN(RedefinitionDataHolder); |
| 1418 | }; |
| 1419 | |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1420 | class RedefinitionDataIter { |
| 1421 | public: |
| 1422 | RedefinitionDataIter(int32_t idx, RedefinitionDataHolder& holder) : idx_(idx), holder_(holder) {} |
| 1423 | |
| 1424 | RedefinitionDataIter(const RedefinitionDataIter&) = default; |
| 1425 | RedefinitionDataIter(RedefinitionDataIter&&) = default; |
| 1426 | RedefinitionDataIter& operator=(const RedefinitionDataIter&) = default; |
| 1427 | RedefinitionDataIter& operator=(RedefinitionDataIter&&) = default; |
| 1428 | |
| 1429 | bool operator==(const RedefinitionDataIter& other) const |
| 1430 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1431 | return idx_ == other.idx_ && holder_ == other.holder_; |
| 1432 | } |
| 1433 | |
| 1434 | bool operator!=(const RedefinitionDataIter& other) const |
| 1435 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1436 | return !(*this == other); |
| 1437 | } |
| 1438 | |
| 1439 | RedefinitionDataIter operator++() { // Value after modification. |
| 1440 | idx_++; |
| 1441 | return *this; |
| 1442 | } |
| 1443 | |
| 1444 | RedefinitionDataIter operator++(int) { |
| 1445 | RedefinitionDataIter temp = *this; |
| 1446 | idx_++; |
| 1447 | return temp; |
| 1448 | } |
| 1449 | |
| 1450 | RedefinitionDataIter operator+(ssize_t delta) const { |
| 1451 | RedefinitionDataIter temp = *this; |
| 1452 | temp += delta; |
| 1453 | return temp; |
| 1454 | } |
| 1455 | |
| 1456 | RedefinitionDataIter& operator+=(ssize_t delta) { |
| 1457 | idx_ += delta; |
| 1458 | return *this; |
| 1459 | } |
| 1460 | |
Alex Light | 986914b | 2019-11-19 01:12:25 +0000 | [diff] [blame] | 1461 | // Compat for STL iterators. |
| 1462 | RedefinitionDataIter& operator*() { |
| 1463 | return *this; |
| 1464 | } |
| 1465 | |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1466 | Redefiner::ClassRedefinition& GetRedefinition() REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1467 | return (*holder_.GetRedefinitions())[idx_]; |
| 1468 | } |
| 1469 | |
| 1470 | RedefinitionDataHolder& GetHolder() { |
| 1471 | return holder_; |
| 1472 | } |
| 1473 | |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 1474 | art::ObjPtr<art::mirror::ClassLoader> GetSourceClassLoader() const |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1475 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1476 | return holder_.GetSourceClassLoader(idx_); |
| 1477 | } |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 1478 | art::ObjPtr<art::mirror::Object> GetJavaDexFile() const |
| 1479 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1480 | return holder_.GetJavaDexFile(idx_); |
| 1481 | } |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 1482 | art::ObjPtr<art::mirror::LongArray> GetNewDexFileCookie() const |
| 1483 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1484 | return holder_.GetNewDexFileCookie(idx_); |
| 1485 | } |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 1486 | art::ObjPtr<art::mirror::DexCache> GetNewDexCache() const |
| 1487 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1488 | return holder_.GetNewDexCache(idx_); |
| 1489 | } |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 1490 | art::ObjPtr<art::mirror::Class> GetMirrorClass() const |
| 1491 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1492 | return holder_.GetMirrorClass(idx_); |
| 1493 | } |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 1494 | art::ObjPtr<art::mirror::Object> GetOriginalDexFile() const |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1495 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Alex Light | 2f814aa | 2017-03-24 15:21:34 +0000 | [diff] [blame] | 1496 | return holder_.GetOriginalDexFile(idx_); |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1497 | } |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 1498 | art::ObjPtr<art::mirror::PointerArray> GetOldObsoleteMethods() const |
Alex Light | 1e3926a | 2017-04-07 10:38:06 -0700 | [diff] [blame] | 1499 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1500 | return holder_.GetOldObsoleteMethods(idx_); |
| 1501 | } |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 1502 | art::ObjPtr<art::mirror::ObjectArray<art::mirror::DexCache>> GetOldDexCaches() const |
Alex Light | 1e3926a | 2017-04-07 10:38:06 -0700 | [diff] [blame] | 1503 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1504 | return holder_.GetOldDexCaches(idx_); |
| 1505 | } |
| 1506 | |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 1507 | art::ObjPtr<art::mirror::Class> GetNewClassObject() const |
| 1508 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1509 | return holder_.GetNewClassObject(idx_); |
| 1510 | } |
| 1511 | |
Alex Light | 986914b | 2019-11-19 01:12:25 +0000 | [diff] [blame] | 1512 | art::ObjPtr<art::mirror::ObjectArray<art::mirror::Object>> GetOldInstanceObjects() const |
| 1513 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1514 | return holder_.GetOldInstanceObjects(idx_); |
| 1515 | } |
| 1516 | art::ObjPtr<art::mirror::ObjectArray<art::mirror::Object>> GetNewInstanceObjects() const |
| 1517 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1518 | return holder_.GetNewInstanceObjects(idx_); |
| 1519 | } |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 1520 | art::ObjPtr<art::mirror::ObjectArray<art::mirror::Class>> GetOldClasses() const |
| 1521 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1522 | return holder_.GetOldClasses(idx_); |
| 1523 | } |
| 1524 | art::ObjPtr<art::mirror::ObjectArray<art::mirror::Class>> GetNewClasses() const |
| 1525 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1526 | return holder_.GetNewClasses(idx_); |
| 1527 | } |
Alex Light | fb53708 | 2019-12-10 14:38:34 -0800 | [diff] [blame] | 1528 | bool IsInitialized() const REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1529 | return holder_.IsInitialized(idx_); |
| 1530 | } |
| 1531 | bool IsActuallyStructural() const REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1532 | return holder_.IsActuallyStructural(idx_); |
| 1533 | } |
| 1534 | bool IsInitialStructural() const REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1535 | return holder_.IsInitialStructural(idx_); |
| 1536 | } |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1537 | int32_t GetIndex() const { |
| 1538 | return idx_; |
| 1539 | } |
| 1540 | |
| 1541 | void SetSourceClassLoader(art::mirror::ClassLoader* loader) |
| 1542 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1543 | holder_.SetSourceClassLoader(idx_, loader); |
| 1544 | } |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 1545 | void SetJavaDexFile(art::ObjPtr<art::mirror::Object> dexfile) |
| 1546 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1547 | holder_.SetJavaDexFile(idx_, dexfile); |
| 1548 | } |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 1549 | void SetNewDexFileCookie(art::ObjPtr<art::mirror::LongArray> cookie) |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1550 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1551 | holder_.SetNewDexFileCookie(idx_, cookie); |
| 1552 | } |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 1553 | void SetNewDexCache(art::ObjPtr<art::mirror::DexCache> cache) |
| 1554 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1555 | holder_.SetNewDexCache(idx_, cache); |
| 1556 | } |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 1557 | void SetMirrorClass(art::ObjPtr<art::mirror::Class> klass) |
| 1558 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1559 | holder_.SetMirrorClass(idx_, klass); |
| 1560 | } |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 1561 | void SetOriginalDexFile(art::ObjPtr<art::mirror::Object> bytes) |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1562 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Alex Light | 2f814aa | 2017-03-24 15:21:34 +0000 | [diff] [blame] | 1563 | holder_.SetOriginalDexFile(idx_, bytes); |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1564 | } |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 1565 | void SetOldObsoleteMethods(art::ObjPtr<art::mirror::PointerArray> methods) |
Alex Light | 1e3926a | 2017-04-07 10:38:06 -0700 | [diff] [blame] | 1566 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1567 | holder_.SetOldObsoleteMethods(idx_, methods); |
| 1568 | } |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 1569 | void SetOldDexCaches(art::ObjPtr<art::mirror::ObjectArray<art::mirror::DexCache>> caches) |
Alex Light | 1e3926a | 2017-04-07 10:38:06 -0700 | [diff] [blame] | 1570 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1571 | holder_.SetOldDexCaches(idx_, caches); |
| 1572 | } |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 1573 | void SetNewClassObject(art::ObjPtr<art::mirror::Class> klass) |
| 1574 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1575 | holder_.SetNewClassObject(idx_, klass); |
| 1576 | } |
Alex Light | 986914b | 2019-11-19 01:12:25 +0000 | [diff] [blame] | 1577 | void SetOldInstanceObjects(art::ObjPtr<art::mirror::ObjectArray<art::mirror::Object>> objs) |
| 1578 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1579 | holder_.SetOldInstanceObjects(idx_, objs); |
| 1580 | } |
| 1581 | void SetNewInstanceObjects(art::ObjPtr<art::mirror::ObjectArray<art::mirror::Object>> objs) |
| 1582 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1583 | holder_.SetNewInstanceObjects(idx_, objs); |
| 1584 | } |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 1585 | void SetOldClasses(art::ObjPtr<art::mirror::ObjectArray<art::mirror::Class>> klasses) |
| 1586 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1587 | holder_.SetOldClasses(idx_, klasses); |
| 1588 | } |
| 1589 | void SetNewClasses(art::ObjPtr<art::mirror::ObjectArray<art::mirror::Class>> klasses) |
| 1590 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1591 | holder_.SetNewClasses(idx_, klasses); |
| 1592 | } |
Alex Light | fb53708 | 2019-12-10 14:38:34 -0800 | [diff] [blame] | 1593 | void SetInitialized() REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1594 | holder_.SetInitialized(idx_); |
| 1595 | } |
| 1596 | void SetActuallyStructural() REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1597 | holder_.SetActuallyStructural(idx_); |
| 1598 | } |
| 1599 | void SetInitialStructural() REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1600 | holder_.SetInitialStructural(idx_); |
| 1601 | } |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1602 | |
| 1603 | private: |
| 1604 | int32_t idx_; |
| 1605 | RedefinitionDataHolder& holder_; |
| 1606 | }; |
| 1607 | |
| 1608 | RedefinitionDataIter RedefinitionDataHolder::begin() { |
| 1609 | return RedefinitionDataIter(0, *this); |
| 1610 | } |
| 1611 | |
| 1612 | RedefinitionDataIter RedefinitionDataHolder::end() { |
| 1613 | return RedefinitionDataIter(Length(), *this); |
| 1614 | } |
| 1615 | |
| 1616 | bool Redefiner::ClassRedefinition::CheckVerification(const RedefinitionDataIter& iter) { |
Alex Light | 8c889d2 | 2017-02-06 13:58:27 -0800 | [diff] [blame] | 1617 | DCHECK_EQ(dex_file_->NumClassDefs(), 1u); |
Nicolas Geoffray | 7744b69 | 2021-07-06 16:19:32 +0100 | [diff] [blame] | 1618 | art::StackHandleScope<3> hs(driver_->self_); |
Alex Light | 8c889d2 | 2017-02-06 13:58:27 -0800 | [diff] [blame] | 1619 | std::string error; |
| 1620 | // TODO Make verification log level lower |
Andreas Gampe | 6d7abbd | 2017-04-24 13:19:09 -0700 | [diff] [blame] | 1621 | art::verifier::FailureKind failure = |
Andreas Gampe | a43ba3d | 2019-03-13 15:49:20 -0700 | [diff] [blame] | 1622 | art::verifier::ClassVerifier::VerifyClass(driver_->self_, |
Nicolas Geoffray | 5b0b2e1 | 2021-03-19 14:48:40 +0000 | [diff] [blame] | 1623 | /*verifier_deps=*/nullptr, |
Andreas Gampe | a43ba3d | 2019-03-13 15:49:20 -0700 | [diff] [blame] | 1624 | dex_file_.get(), |
Nicolas Geoffray | 7744b69 | 2021-07-06 16:19:32 +0100 | [diff] [blame] | 1625 | hs.NewHandle(iter.GetNewClassObject() != nullptr |
| 1626 | ? iter.GetNewClassObject() |
| 1627 | : iter.GetMirrorClass()), |
Andreas Gampe | a43ba3d | 2019-03-13 15:49:20 -0700 | [diff] [blame] | 1628 | hs.NewHandle(iter.GetNewDexCache()), |
| 1629 | hs.NewHandle(GetClassLoader()), |
| 1630 | /*class_def=*/ dex_file_->GetClassDef(0), |
| 1631 | /*callbacks=*/ nullptr, |
Andreas Gampe | a43ba3d | 2019-03-13 15:49:20 -0700 | [diff] [blame] | 1632 | /*log_level=*/ |
| 1633 | art::verifier::HardFailLogMode::kLogWarning, |
| 1634 | art::Runtime::Current()->GetTargetSdkVersion(), |
| 1635 | &error); |
Nicolas Geoffray | 7744b69 | 2021-07-06 16:19:32 +0100 | [diff] [blame] | 1636 | if (failure == art::verifier::FailureKind::kHardFailure) { |
| 1637 | RecordFailure(ERR(FAILS_VERIFICATION), "Failed to verify class. Error was: " + error); |
| 1638 | return false; |
Alex Light | 8c889d2 | 2017-02-06 13:58:27 -0800 | [diff] [blame] | 1639 | } |
Nicolas Geoffray | 7744b69 | 2021-07-06 16:19:32 +0100 | [diff] [blame] | 1640 | return true; |
Alex Light | 8c889d2 | 2017-02-06 13:58:27 -0800 | [diff] [blame] | 1641 | } |
| 1642 | |
Alex Light | 1babae0 | 2017-02-01 15:35:34 -0800 | [diff] [blame] | 1643 | // Looks through the previously allocated cookies to see if we need to update them with another new |
| 1644 | // dexfile. This is so that even if multiple classes with the same classloader are redefined at |
| 1645 | // once they are all added to the classloader. |
| 1646 | bool Redefiner::ClassRedefinition::AllocateAndRememberNewDexFileCookie( |
Alex Light | 1babae0 | 2017-02-01 15:35:34 -0800 | [diff] [blame] | 1647 | art::Handle<art::mirror::ClassLoader> source_class_loader, |
| 1648 | art::Handle<art::mirror::Object> dex_file_obj, |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1649 | /*out*/RedefinitionDataIter* cur_data) { |
Alex Light | 1babae0 | 2017-02-01 15:35:34 -0800 | [diff] [blame] | 1650 | art::StackHandleScope<2> hs(driver_->self_); |
| 1651 | art::MutableHandle<art::mirror::LongArray> old_cookie( |
| 1652 | hs.NewHandle<art::mirror::LongArray>(nullptr)); |
| 1653 | bool has_older_cookie = false; |
| 1654 | // See if we already have a cookie that a previous redefinition got from the same classloader. |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1655 | for (auto old_data = cur_data->GetHolder().begin(); old_data != *cur_data; ++old_data) { |
| 1656 | if (old_data.GetSourceClassLoader() == source_class_loader.Get()) { |
Alex Light | 1babae0 | 2017-02-01 15:35:34 -0800 | [diff] [blame] | 1657 | // Since every instance of this classloader should have the same cookie associated with it we |
| 1658 | // can stop looking here. |
| 1659 | has_older_cookie = true; |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1660 | old_cookie.Assign(old_data.GetNewDexFileCookie()); |
Alex Light | 1babae0 | 2017-02-01 15:35:34 -0800 | [diff] [blame] | 1661 | break; |
| 1662 | } |
| 1663 | } |
| 1664 | if (old_cookie.IsNull()) { |
| 1665 | // No older cookie. Get it directly from the dex_file_obj |
| 1666 | // We should not have seen this classloader elsewhere. |
| 1667 | CHECK(!has_older_cookie); |
| 1668 | old_cookie.Assign(ClassLoaderHelper::GetDexFileCookie(dex_file_obj)); |
| 1669 | } |
| 1670 | // Use the old cookie to generate the new one with the new DexFile* added in. |
| 1671 | art::Handle<art::mirror::LongArray> |
| 1672 | new_cookie(hs.NewHandle(ClassLoaderHelper::AllocateNewDexFileCookie(driver_->self_, |
| 1673 | old_cookie, |
| 1674 | dex_file_.get()))); |
| 1675 | // Make sure the allocation worked. |
| 1676 | if (new_cookie.IsNull()) { |
| 1677 | return false; |
| 1678 | } |
| 1679 | |
| 1680 | // Save the cookie. |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1681 | cur_data->SetNewDexFileCookie(new_cookie.Get()); |
Alex Light | 1babae0 | 2017-02-01 15:35:34 -0800 | [diff] [blame] | 1682 | // If there are other copies of this same classloader we need to make sure that we all have the |
| 1683 | // same cookie. |
| 1684 | if (has_older_cookie) { |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1685 | for (auto old_data = cur_data->GetHolder().begin(); old_data != *cur_data; ++old_data) { |
Alex Light | 1babae0 | 2017-02-01 15:35:34 -0800 | [diff] [blame] | 1686 | // We will let the GC take care of the cookie we allocated for this one. |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1687 | if (old_data.GetSourceClassLoader() == source_class_loader.Get()) { |
| 1688 | old_data.SetNewDexFileCookie(new_cookie.Get()); |
Alex Light | 1babae0 | 2017-02-01 15:35:34 -0800 | [diff] [blame] | 1689 | } |
| 1690 | } |
| 1691 | } |
| 1692 | |
| 1693 | return true; |
| 1694 | } |
| 1695 | |
Alex Light | fb53708 | 2019-12-10 14:38:34 -0800 | [diff] [blame] | 1696 | bool CompareClasses(art::ObjPtr<art::mirror::Class> l, art::ObjPtr<art::mirror::Class> r) |
| 1697 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1698 | auto parents = [](art::ObjPtr<art::mirror::Class> c) REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1699 | uint32_t res = 0; |
| 1700 | while (!c->IsObjectClass()) { |
| 1701 | res++; |
| 1702 | c = c->GetSuperClass(); |
| 1703 | } |
| 1704 | return res; |
| 1705 | }; |
| 1706 | return parents(l.Ptr()) < parents(r.Ptr()); |
| 1707 | } |
| 1708 | |
Alex Light | 986914b | 2019-11-19 01:12:25 +0000 | [diff] [blame] | 1709 | bool Redefiner::ClassRedefinition::CollectAndCreateNewInstances( |
| 1710 | /*out*/ RedefinitionDataIter* cur_data) { |
Alex Light | fb53708 | 2019-12-10 14:38:34 -0800 | [diff] [blame] | 1711 | if (!cur_data->IsInitialStructural()) { |
| 1712 | // An earlier structural redefinition already remade all the instances. |
Alex Light | 986914b | 2019-11-19 01:12:25 +0000 | [diff] [blame] | 1713 | return true; |
| 1714 | } |
Alex Light | fb53708 | 2019-12-10 14:38:34 -0800 | [diff] [blame] | 1715 | art::gc::Heap* heap = driver_->runtime_->GetHeap(); |
Alex Light | 986914b | 2019-11-19 01:12:25 +0000 | [diff] [blame] | 1716 | art::VariableSizedHandleScope hs(driver_->self_); |
| 1717 | art::Handle<art::mirror::Class> old_klass(hs.NewHandle(cur_data->GetMirrorClass())); |
| 1718 | std::vector<art::Handle<art::mirror::Object>> old_instances; |
Alex Light | 986914b | 2019-11-19 01:12:25 +0000 | [diff] [blame] | 1719 | auto is_instance = [&](art::mirror::Object* obj) REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 1720 | return obj->InstanceOf(old_klass.Get()); |
Alex Light | 986914b | 2019-11-19 01:12:25 +0000 | [diff] [blame] | 1721 | }; |
| 1722 | heap->VisitObjects([&](art::mirror::Object* obj) REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1723 | if (is_instance(obj)) { |
Alex Light | 986914b | 2019-11-19 01:12:25 +0000 | [diff] [blame] | 1724 | old_instances.push_back(hs.NewHandle(obj)); |
| 1725 | } |
| 1726 | }); |
| 1727 | VLOG(plugin) << "Collected " << old_instances.size() << " instances to recreate!"; |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 1728 | art::Handle<art::mirror::ObjectArray<art::mirror::Class>> old_classes_arr( |
Alex Light | fb53708 | 2019-12-10 14:38:34 -0800 | [diff] [blame] | 1729 | hs.NewHandle(cur_data->GetOldClasses())); |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 1730 | art::Handle<art::mirror::ObjectArray<art::mirror::Class>> new_classes_arr( |
Alex Light | fb53708 | 2019-12-10 14:38:34 -0800 | [diff] [blame] | 1731 | hs.NewHandle(cur_data->GetNewClasses())); |
Alex Light | 3f4041b | 2020-01-09 12:59:22 -0800 | [diff] [blame] | 1732 | DCHECK_EQ(old_classes_arr->GetLength(), new_classes_arr->GetLength()); |
Alex Light | baf938f | 2020-01-10 10:40:46 -0800 | [diff] [blame] | 1733 | DCHECK_GT(old_classes_arr->GetLength(), 0); |
Alex Light | 986914b | 2019-11-19 01:12:25 +0000 | [diff] [blame] | 1734 | art::Handle<art::mirror::Class> obj_array_class( |
| 1735 | hs.NewHandle(art::GetClassRoot<art::mirror::ObjectArray<art::mirror::Object>>( |
| 1736 | driver_->runtime_->GetClassLinker()))); |
| 1737 | art::Handle<art::mirror::ObjectArray<art::mirror::Object>> old_instances_arr( |
| 1738 | hs.NewHandle(art::mirror::ObjectArray<art::mirror::Object>::Alloc( |
| 1739 | driver_->self_, obj_array_class.Get(), old_instances.size()))); |
| 1740 | if (old_instances_arr.IsNull()) { |
| 1741 | driver_->self_->AssertPendingOOMException(); |
| 1742 | driver_->self_->ClearException(); |
| 1743 | RecordFailure(ERR(OUT_OF_MEMORY), "Could not allocate old_instance arrays!"); |
| 1744 | return false; |
| 1745 | } |
| 1746 | for (uint32_t i = 0; i < old_instances.size(); ++i) { |
| 1747 | old_instances_arr->Set(i, old_instances[i].Get()); |
| 1748 | } |
| 1749 | cur_data->SetOldInstanceObjects(old_instances_arr.Get()); |
| 1750 | |
| 1751 | art::Handle<art::mirror::ObjectArray<art::mirror::Object>> new_instances_arr( |
| 1752 | hs.NewHandle(art::mirror::ObjectArray<art::mirror::Object>::Alloc( |
| 1753 | driver_->self_, obj_array_class.Get(), old_instances.size()))); |
| 1754 | if (new_instances_arr.IsNull()) { |
| 1755 | driver_->self_->AssertPendingOOMException(); |
| 1756 | driver_->self_->ClearException(); |
| 1757 | RecordFailure(ERR(OUT_OF_MEMORY), "Could not allocate new_instance arrays!"); |
| 1758 | return false; |
| 1759 | } |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 1760 | for (auto pair : art::ZipCount(art::IterationRange(old_instances.begin(), old_instances.end()))) { |
| 1761 | art::Handle<art::mirror::Object> hinstance(pair.first); |
| 1762 | int32_t i = pair.second; |
| 1763 | auto iterator = art::ZipLeft(old_classes_arr.Iterate<art::mirror::Class>(), |
| 1764 | new_classes_arr.Iterate<art::mirror::Class>()); |
Alex Light | baf938f | 2020-01-10 10:40:46 -0800 | [diff] [blame] | 1765 | auto it = std::find_if(iterator.begin(), |
| 1766 | iterator.end(), |
| 1767 | [&](auto class_pair) REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1768 | return class_pair.first == hinstance->GetClass(); |
| 1769 | }); |
| 1770 | DCHECK(it != iterator.end()) << "Unable to find class pair for " |
| 1771 | << hinstance->GetClass()->PrettyClass() << " (instance " << i |
| 1772 | << ")"; |
| 1773 | auto [_, new_type] = *it; |
Alex Light | 2f49428 | 2020-01-07 22:53:09 +0000 | [diff] [blame] | 1774 | // Make sure when allocating the new instance we don't add it's finalizer since we will directly |
| 1775 | // replace the old object in the finalizer reference. If we added it here to we would call |
| 1776 | // finalize twice. |
| 1777 | // NB If a type is changed from being non-finalizable to finalizable the finalizers on any |
| 1778 | // objects created before the redefine will never be called. This is (sort of) allowable by |
| 1779 | // the spec and greatly simplifies implementation. |
| 1780 | // TODO Make it so we will always call all finalizers, even if the object when it was created |
| 1781 | // wasn't finalizable. To do this we need to be careful of handling failure correctly and making |
| 1782 | // sure that objects aren't finalized multiple times and that instances of failed redefinitions |
| 1783 | // aren't finalized. |
| 1784 | art::ObjPtr<art::mirror::Object> new_instance( |
| 1785 | new_type->Alloc</*kIsInstrumented=*/true, |
| 1786 | art::mirror::Class::AddFinalizer::kNoAddFinalizer, |
| 1787 | /*kCheckAddFinalizer=*/false>( |
| 1788 | driver_->self_, driver_->runtime_->GetHeap()->GetCurrentAllocator())); |
Alex Light | 986914b | 2019-11-19 01:12:25 +0000 | [diff] [blame] | 1789 | if (new_instance.IsNull()) { |
| 1790 | driver_->self_->AssertPendingOOMException(); |
| 1791 | driver_->self_->ClearException(); |
| 1792 | std::string msg( |
| 1793 | StringPrintf("Could not allocate instance %d of %zu", i, old_instances.size())); |
| 1794 | RecordFailure(ERR(OUT_OF_MEMORY), msg); |
| 1795 | return false; |
| 1796 | } |
| 1797 | new_instances_arr->Set(i, new_instance); |
| 1798 | } |
| 1799 | cur_data->SetNewInstanceObjects(new_instances_arr.Get()); |
| 1800 | return true; |
| 1801 | } |
| 1802 | |
Alex Light | fb53708 | 2019-12-10 14:38:34 -0800 | [diff] [blame] | 1803 | bool Redefiner::ClassRedefinition::FinishRemainingCommonAllocations( |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1804 | /*out*/RedefinitionDataIter* cur_data) { |
Alex Light | 7916f20 | 2017-01-27 09:00:15 -0800 | [diff] [blame] | 1805 | art::ScopedObjectAccessUnchecked soa(driver_->self_); |
Alex Light | fb53708 | 2019-12-10 14:38:34 -0800 | [diff] [blame] | 1806 | art::StackHandleScope<2> hs(driver_->self_); |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1807 | cur_data->SetMirrorClass(GetMirrorClass()); |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 1808 | // This shouldn't allocate |
| 1809 | art::Handle<art::mirror::ClassLoader> loader(hs.NewHandle(GetClassLoader())); |
Alex Light | 7916f20 | 2017-01-27 09:00:15 -0800 | [diff] [blame] | 1810 | // The bootclasspath is handled specially so it doesn't have a j.l.DexFile. |
| 1811 | if (!art::ClassLinker::IsBootClassLoader(soa, loader.Get())) { |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1812 | cur_data->SetSourceClassLoader(loader.Get()); |
Alex Light | 7916f20 | 2017-01-27 09:00:15 -0800 | [diff] [blame] | 1813 | art::Handle<art::mirror::Object> dex_file_obj(hs.NewHandle( |
| 1814 | ClassLoaderHelper::FindSourceDexFileObject(driver_->self_, loader))); |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1815 | cur_data->SetJavaDexFile(dex_file_obj.Get()); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 1816 | if (dex_file_obj == nullptr) { |
Alex Light | 7916f20 | 2017-01-27 09:00:15 -0800 | [diff] [blame] | 1817 | RecordFailure(ERR(INTERNAL), "Unable to find dex file!"); |
| 1818 | return false; |
| 1819 | } |
Alex Light | 1babae0 | 2017-02-01 15:35:34 -0800 | [diff] [blame] | 1820 | // Allocate the new dex file cookie. |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1821 | if (!AllocateAndRememberNewDexFileCookie(loader, dex_file_obj, cur_data)) { |
Alex Light | 7916f20 | 2017-01-27 09:00:15 -0800 | [diff] [blame] | 1822 | driver_->self_->AssertPendingOOMException(); |
| 1823 | driver_->self_->ClearException(); |
| 1824 | RecordFailure(ERR(OUT_OF_MEMORY), "Unable to allocate dex file array for class loader"); |
| 1825 | return false; |
| 1826 | } |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 1827 | } |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 1828 | cur_data->SetNewDexCache(CreateNewDexCache(loader)); |
| 1829 | if (cur_data->GetNewDexCache() == nullptr) { |
Vladimir Marko | cd556b0 | 2017-02-03 11:47:34 +0000 | [diff] [blame] | 1830 | driver_->self_->AssertPendingException(); |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 1831 | driver_->self_->ClearException(); |
| 1832 | RecordFailure(ERR(OUT_OF_MEMORY), "Unable to allocate DexCache"); |
| 1833 | return false; |
| 1834 | } |
| 1835 | |
| 1836 | // We won't always need to set this field. |
Alex Light | 2f814aa | 2017-03-24 15:21:34 +0000 | [diff] [blame] | 1837 | cur_data->SetOriginalDexFile(AllocateOrGetOriginalDexFile()); |
| 1838 | if (cur_data->GetOriginalDexFile() == nullptr) { |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 1839 | driver_->self_->AssertPendingOOMException(); |
| 1840 | driver_->self_->ClearException(); |
| 1841 | RecordFailure(ERR(OUT_OF_MEMORY), "Unable to allocate array for original dex file"); |
| 1842 | return false; |
| 1843 | } |
Alex Light | fb53708 | 2019-12-10 14:38:34 -0800 | [diff] [blame] | 1844 | return true; |
| 1845 | } |
| 1846 | |
| 1847 | bool Redefiner::ClassRedefinition::FinishNewClassAllocations(RedefinitionDataHolder &holder, |
| 1848 | RedefinitionDataIter *cur_data) { |
| 1849 | if (cur_data->IsInitialized() || !cur_data->IsActuallyStructural()) { |
| 1850 | cur_data->SetInitialized(); |
| 1851 | return true; |
| 1852 | } |
| 1853 | |
| 1854 | art::VariableSizedHandleScope hs(driver_->self_); |
| 1855 | // If we weren't the lowest structural redef the superclass would have already initialized us. |
| 1856 | CHECK(IsStructuralRedefinition()); |
| 1857 | CHECK(cur_data->IsInitialStructural()) << "Should have already been initialized by supertype"; |
| 1858 | auto setup_single_redefinition = |
| 1859 | [this](RedefinitionDataIter* data, art::Handle<art::mirror::Class> super_class) |
| 1860 | REQUIRES_SHARED(art::Locks::mutator_lock_) -> art::ObjPtr<art::mirror::Class> { |
| 1861 | art::StackHandleScope<3> chs(driver_->self_); |
| 1862 | art::Handle<art::mirror::Class> nc( |
| 1863 | chs.NewHandle(AllocateNewClassObject(chs.NewHandle(data->GetMirrorClass()), |
| 1864 | super_class, |
| 1865 | chs.NewHandle(data->GetNewDexCache()), |
| 1866 | /*dex_class_def_index*/ 0))); |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 1867 | if (nc.IsNull()) { |
Alex Light | fb53708 | 2019-12-10 14:38:34 -0800 | [diff] [blame] | 1868 | return nullptr; |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 1869 | } |
| 1870 | |
Alex Light | fb53708 | 2019-12-10 14:38:34 -0800 | [diff] [blame] | 1871 | data->SetNewClassObject(nc.Get()); |
Alex Light | fb53708 | 2019-12-10 14:38:34 -0800 | [diff] [blame] | 1872 | data->SetInitialized(); |
| 1873 | return nc.Get(); |
| 1874 | }; |
| 1875 | |
| 1876 | std::vector<art::Handle<art::mirror::Class>> old_types; |
| 1877 | { |
| 1878 | art::gc::Heap* heap = driver_->runtime_->GetHeap(); |
| 1879 | art::Handle<art::mirror::Class> |
| 1880 | old_klass(hs.NewHandle(cur_data->GetMirrorClass())); |
| 1881 | if (setup_single_redefinition(cur_data, hs.NewHandle(old_klass->GetSuperClass())).IsNull()) { |
| 1882 | return false; |
| 1883 | } |
| 1884 | auto is_subtype = [&](art::mirror::Object* obj) REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1885 | // We've already waited for class defines to be finished and paused them. All classes should be |
| 1886 | // either resolved or error. We don't need to do anything with error classes, since they cannot |
| 1887 | // be accessed in any observable way. |
| 1888 | return obj->IsClass() && obj->AsClass()->IsResolved() && |
| 1889 | old_klass->IsAssignableFrom(obj->AsClass()); |
| 1890 | }; |
| 1891 | heap->VisitObjects([&](art::mirror::Object* obj) REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1892 | if (is_subtype(obj)) { |
| 1893 | old_types.push_back(hs.NewHandle(obj->AsClass())); |
| 1894 | } |
| 1895 | }); |
Alex Light | baf938f | 2020-01-10 10:40:46 -0800 | [diff] [blame] | 1896 | DCHECK_GT(old_types.size(), 0u) << "Expected to find at least old_klass!"; |
Alex Light | fb53708 | 2019-12-10 14:38:34 -0800 | [diff] [blame] | 1897 | VLOG(plugin) << "Found " << old_types.size() << " types that are/are subtypes of " |
| 1898 | << old_klass->PrettyClass(); |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 1899 | } |
Alex Light | fb53708 | 2019-12-10 14:38:34 -0800 | [diff] [blame] | 1900 | |
| 1901 | art::Handle<art::mirror::Class> cls_array_class( |
| 1902 | hs.NewHandle(art::GetClassRoot<art::mirror::ObjectArray<art::mirror::Class>>( |
| 1903 | driver_->runtime_->GetClassLinker()))); |
| 1904 | art::Handle<art::mirror::ObjectArray<art::mirror::Class>> old_classes_arr( |
| 1905 | hs.NewHandle(art::mirror::ObjectArray<art::mirror::Class>::Alloc( |
| 1906 | driver_->self_, cls_array_class.Get(), old_types.size()))); |
| 1907 | if (old_classes_arr.IsNull()) { |
| 1908 | driver_->self_->AssertPendingOOMException(); |
| 1909 | driver_->self_->ClearException(); |
| 1910 | RecordFailure(ERR(OUT_OF_MEMORY), "Could not allocate old_classes arrays!"); |
| 1911 | return false; |
| 1912 | } |
| 1913 | // Sort the old_types topologically. |
| 1914 | { |
| 1915 | art::ScopedAssertNoThreadSuspension sants("Sort classes"); |
| 1916 | // Sort them by the distance to the base-class. This ensures that any class occurs before any of |
| 1917 | // its subtypes. |
| 1918 | std::sort(old_types.begin(), |
| 1919 | old_types.end(), |
| 1920 | [](auto& l, auto& r) REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1921 | return CompareClasses(l.Get(), r.Get()); |
| 1922 | }); |
| 1923 | } |
| 1924 | for (uint32_t i = 0; i < old_types.size(); ++i) { |
Alex Light | baf938f | 2020-01-10 10:40:46 -0800 | [diff] [blame] | 1925 | DCHECK(!old_types[i].IsNull()) << i; |
Alex Light | fb53708 | 2019-12-10 14:38:34 -0800 | [diff] [blame] | 1926 | old_classes_arr->Set(i, old_types[i].Get()); |
| 1927 | } |
| 1928 | cur_data->SetOldClasses(old_classes_arr.Get()); |
Alex Light | baf938f | 2020-01-10 10:40:46 -0800 | [diff] [blame] | 1929 | DCHECK_GT(old_classes_arr->GetLength(), 0); |
Alex Light | fb53708 | 2019-12-10 14:38:34 -0800 | [diff] [blame] | 1930 | |
| 1931 | art::Handle<art::mirror::ObjectArray<art::mirror::Class>> new_classes_arr( |
| 1932 | hs.NewHandle(art::mirror::ObjectArray<art::mirror::Class>::Alloc( |
| 1933 | driver_->self_, cls_array_class.Get(), old_types.size()))); |
| 1934 | if (new_classes_arr.IsNull()) { |
| 1935 | driver_->self_->AssertPendingOOMException(); |
| 1936 | driver_->self_->ClearException(); |
| 1937 | RecordFailure(ERR(OUT_OF_MEMORY), "Could not allocate new_classes arrays!"); |
| 1938 | return false; |
| 1939 | } |
| 1940 | |
| 1941 | art::MutableHandle<art::mirror::DexCache> dch(hs.NewHandle<art::mirror::DexCache>(nullptr)); |
| 1942 | art::MutableHandle<art::mirror::Class> superclass(hs.NewHandle<art::mirror::Class>(nullptr)); |
| 1943 | for (size_t i = 0; i < old_types.size(); i++) { |
| 1944 | art::Handle<art::mirror::Class>& old_type = old_types[i]; |
| 1945 | if (old_type.Get() == cur_data->GetMirrorClass()) { |
| 1946 | CHECK_EQ(i, 0u) << "original class not at index 0. Bad sort!"; |
| 1947 | new_classes_arr->Set(i, cur_data->GetNewClassObject()); |
| 1948 | continue; |
| 1949 | } else { |
| 1950 | auto old_super = std::find_if(old_types.begin(), |
| 1951 | old_types.begin() + i, |
| 1952 | [&](art::Handle<art::mirror::Class>& v) |
| 1953 | REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1954 | return v.Get() == old_type->GetSuperClass(); |
| 1955 | }); |
| 1956 | // Only the GetMirrorClass should not be in this list. |
| 1957 | CHECK(old_super != old_types.begin() + i) |
| 1958 | << "from first " << i << " could not find super of " << old_type->PrettyClass() |
| 1959 | << " expected to find " << old_type->GetSuperClass()->PrettyClass(); |
| 1960 | superclass.Assign(new_classes_arr->Get(std::distance(old_types.begin(), old_super))); |
| 1961 | auto new_redef = std::find_if( |
| 1962 | *cur_data + 1, holder.end(), [&](auto it) REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 1963 | return it.GetMirrorClass() == old_type.Get(); |
| 1964 | }); |
| 1965 | art::ObjPtr<art::mirror::Class> new_type; |
| 1966 | if (new_redef == holder.end()) { |
| 1967 | // We aren't also redefining this subclass. Just allocate a new class and continue. |
| 1968 | dch.Assign(old_type->GetDexCache()); |
| 1969 | new_type = |
| 1970 | AllocateNewClassObject(old_type, superclass, dch, old_type->GetDexClassDefIndex()); |
| 1971 | } else { |
| 1972 | // This subclass is also being redefined. We need to use its new dex-file to load the new |
| 1973 | // class. |
| 1974 | CHECK(new_redef.IsActuallyStructural()); |
| 1975 | CHECK(!new_redef.IsInitialStructural()); |
| 1976 | new_type = setup_single_redefinition(&new_redef, superclass); |
| 1977 | } |
| 1978 | if (new_type == nullptr) { |
| 1979 | VLOG(plugin) << "Failed to load new version of class " << old_type->PrettyClass() |
| 1980 | << " for structural redefinition!"; |
| 1981 | return false; |
| 1982 | } |
| 1983 | new_classes_arr->Set(i, new_type); |
| 1984 | } |
| 1985 | } |
| 1986 | cur_data->SetNewClasses(new_classes_arr.Get()); |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 1987 | return true; |
| 1988 | } |
| 1989 | |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 1990 | uint32_t Redefiner::ClassRedefinition::GetNewClassSize(art::ClassAccessor& accessor) { |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 1991 | uint32_t num_8bit_static_fields = 0; |
| 1992 | uint32_t num_16bit_static_fields = 0; |
| 1993 | uint32_t num_32bit_static_fields = 0; |
| 1994 | uint32_t num_64bit_static_fields = 0; |
| 1995 | uint32_t num_ref_static_fields = 0; |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 1996 | for (const art::ClassAccessor::Field& f : accessor.GetStaticFields()) { |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 1997 | std::string_view desc(accessor.GetDexFile().GetFieldTypeDescriptor( |
| 1998 | accessor.GetDexFile().GetFieldId(f.GetIndex()))); |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 1999 | if (desc[0] == 'L' || desc[0] == '[') { |
| 2000 | num_ref_static_fields++; |
| 2001 | } else if (desc == "Z" || desc == "B") { |
| 2002 | num_8bit_static_fields++; |
| 2003 | } else if (desc == "C" || desc == "S") { |
| 2004 | num_16bit_static_fields++; |
| 2005 | } else if (desc == "I" || desc == "F") { |
| 2006 | num_32bit_static_fields++; |
| 2007 | } else if (desc == "J" || desc == "D") { |
| 2008 | num_64bit_static_fields++; |
| 2009 | } else { |
| 2010 | LOG(FATAL) << "Unknown type descriptor! " << desc; |
| 2011 | } |
| 2012 | } |
| 2013 | |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 2014 | return art::mirror::Class::ComputeClassSize(/*has_embedded_vtable=*/ false, |
| 2015 | /*num_vtable_entries=*/ 0, |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 2016 | num_8bit_static_fields, |
| 2017 | num_16bit_static_fields, |
| 2018 | num_32bit_static_fields, |
| 2019 | num_64bit_static_fields, |
| 2020 | num_ref_static_fields, |
| 2021 | art::kRuntimePointerSize); |
| 2022 | } |
| 2023 | |
| 2024 | art::ObjPtr<art::mirror::Class> |
| 2025 | Redefiner::ClassRedefinition::AllocateNewClassObject(art::Handle<art::mirror::DexCache> cache) { |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 2026 | art::StackHandleScope<2> hs(driver_->self_); |
| 2027 | art::Handle<art::mirror::Class> old_class(hs.NewHandle(GetMirrorClass())); |
| 2028 | art::Handle<art::mirror::Class> super_class(hs.NewHandle(old_class->GetSuperClass())); |
| 2029 | return AllocateNewClassObject(old_class, super_class, cache, /*dex_class_def_index*/0); |
| 2030 | } |
| 2031 | |
| 2032 | art::ObjPtr<art::mirror::Class> Redefiner::ClassRedefinition::AllocateNewClassObject( |
| 2033 | art::Handle<art::mirror::Class> old_class, |
| 2034 | art::Handle<art::mirror::Class> super_class, |
| 2035 | art::Handle<art::mirror::DexCache> cache, |
| 2036 | uint16_t dex_class_def_index) { |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 2037 | // This is a stripped down DefineClass. We don't want to use DefineClass directly because it needs |
| 2038 | // to perform a lot of extra steps to tell the ClassTable and the jit and everything about a new |
| 2039 | // class. For now we will need to rely on our tests catching any issues caused by changes in how |
| 2040 | // class_linker sets up classes. |
| 2041 | // TODO Unify/move this into ClassLinker maybe. |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 2042 | art::StackHandleScope<3> hs(driver_->self_); |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 2043 | art::ClassLinker* linker = driver_->runtime_->GetClassLinker(); |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 2044 | const art::DexFile* dex_file = cache->GetDexFile(); |
| 2045 | art::ClassAccessor accessor(*dex_file, dex_class_def_index); |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 2046 | art::Handle<art::mirror::Class> new_class(hs.NewHandle(linker->AllocClass( |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 2047 | driver_->self_, GetNewClassSize(accessor)))); |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 2048 | if (new_class.IsNull()) { |
| 2049 | driver_->self_->AssertPendingOOMException(); |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 2050 | RecordFailure( |
| 2051 | ERR(OUT_OF_MEMORY), |
| 2052 | "Unable to allocate class object for redefinition of " + old_class->PrettyClass()); |
| 2053 | driver_->self_->ClearException(); |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 2054 | return nullptr; |
| 2055 | } |
| 2056 | new_class->SetDexCache(cache.Get()); |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 2057 | linker->SetupClass(*dex_file, |
| 2058 | dex_file->GetClassDef(dex_class_def_index), |
| 2059 | new_class, |
| 2060 | old_class->GetClassLoader()); |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 2061 | |
| 2062 | // Make sure we are ready for linking. The lock isn't really needed since this isn't visible to |
| 2063 | // other threads but the linker expects it. |
| 2064 | art::ObjectLock<art::mirror::Class> lock(driver_->self_, new_class); |
| 2065 | new_class->SetClinitThreadId(driver_->self_->GetTid()); |
| 2066 | // Make sure we have a valid empty iftable even if there are errors. |
| 2067 | new_class->SetIfTable(art::GetClassRoot<art::mirror::Object>(linker)->GetIfTable()); |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 2068 | linker->LoadClass( |
| 2069 | driver_->self_, *dex_file, dex_file->GetClassDef(dex_class_def_index), new_class); |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 2070 | // NB. We know the interfaces and supers didn't change! :) |
| 2071 | art::MutableHandle<art::mirror::Class> linked_class(hs.NewHandle<art::mirror::Class>(nullptr)); |
| 2072 | art::Handle<art::mirror::ObjectArray<art::mirror::Class>> proxy_ifaces( |
| 2073 | hs.NewHandle<art::mirror::ObjectArray<art::mirror::Class>>(nullptr)); |
| 2074 | // No changing hierarchy so everything is loaded. |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 2075 | new_class->SetSuperClass(super_class.Get()); |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 2076 | art::mirror::Class::SetStatus(new_class, art::ClassStatus::kLoaded, nullptr); |
| 2077 | if (!linker->LinkClass(driver_->self_, nullptr, new_class, proxy_ifaces, &linked_class)) { |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 2078 | std::ostringstream oss; |
| 2079 | oss << "failed to link class due to " |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 2080 | << (driver_->self_->IsExceptionPending() ? driver_->self_->GetException()->Dump() |
| 2081 | : " unknown"); |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 2082 | RecordFailure(ERR(INTERNAL), oss.str()); |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 2083 | driver_->self_->ClearException(); |
| 2084 | return nullptr; |
| 2085 | } |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 2086 | // Everything is already resolved. |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 2087 | art::ObjectLock<art::mirror::Class> objlock(driver_->self_, linked_class); |
Alex Light | fb11957 | 2019-09-18 15:04:53 -0700 | [diff] [blame] | 2088 | // Mark the class as initialized. |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 2089 | CHECK(old_class->IsResolved()) |
| 2090 | << "Attempting to redefine an unresolved class " << old_class->PrettyClass() |
Alex Light | fb11957 | 2019-09-18 15:04:53 -0700 | [diff] [blame] | 2091 | << " status=" << old_class->GetStatus(); |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 2092 | CHECK(linked_class->IsResolved()); |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 2093 | if (old_class->ShouldSkipHiddenApiChecks()) { |
| 2094 | // Match skip hiddenapi flag |
| 2095 | linked_class->SetSkipHiddenApiChecks(); |
| 2096 | } |
| 2097 | if (old_class->IsInitialized()) { |
| 2098 | // We already verified the class earlier. No need to do it again. |
| 2099 | linker->ForceClassInitialized(driver_->self_, linked_class); |
| 2100 | } else if (old_class->GetStatus() > linked_class->GetStatus()) { |
| 2101 | // We want to match the old status. |
| 2102 | art::mirror::Class::SetStatus(linked_class, old_class->GetStatus(), driver_->self_); |
| 2103 | } |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 2104 | // Make sure we have ext-data space for method & field ids. We won't know if we need them until |
| 2105 | // it's too late to create them. |
| 2106 | // TODO We might want to remove these arrays if they're not needed. |
Alex Light | bc19b75 | 2019-12-02 18:54:13 +0000 | [diff] [blame] | 2107 | if (!art::mirror::Class::EnsureInstanceFieldIds(linked_class) || |
| 2108 | !art::mirror::Class::EnsureStaticFieldIds(linked_class) || |
| 2109 | !art::mirror::Class::EnsureMethodIds(linked_class)) { |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 2110 | driver_->self_->AssertPendingOOMException(); |
| 2111 | driver_->self_->ClearException(); |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 2112 | RecordFailure( |
| 2113 | ERR(OUT_OF_MEMORY), |
| 2114 | "Unable to allocate jni-id arrays for redefinition of " + old_class->PrettyClass()); |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 2115 | return nullptr; |
| 2116 | } |
| 2117 | // Finish setting up methods. |
| 2118 | linked_class->VisitMethods([&](art::ArtMethod* m) REQUIRES_SHARED(art::Locks::mutator_lock_) { |
Nicolas Geoffray | c8a694d | 2022-01-17 17:12:38 +0000 | [diff] [blame] | 2119 | driver_->runtime_->GetInstrumentation()->InitializeMethodsCode(m, /* aot_code= */ nullptr); |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 2120 | m->SetNotIntrinsic(); |
| 2121 | DCHECK(m->IsCopied() || m->GetDeclaringClass() == linked_class.Get()) |
| 2122 | << m->PrettyMethod() |
| 2123 | << " m->GetDeclaringClass(): " << m->GetDeclaringClass()->PrettyClass() |
| 2124 | << " != linked_class.Get(): " << linked_class->PrettyClass(); |
| 2125 | }, art::kRuntimePointerSize); |
| 2126 | if (art::kIsDebugBuild) { |
| 2127 | linked_class->VisitFields([&](art::ArtField* f) REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 2128 | DCHECK_EQ(f->GetDeclaringClass(), linked_class.Get()); |
| 2129 | }); |
| 2130 | } |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 2131 | // Reset ClinitThreadId back to the thread that loaded the old class. This is needed if we are in |
| 2132 | // the middle of initializing a class. |
| 2133 | linked_class->SetClinitThreadId(old_class->GetClinitThreadId()); |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 2134 | return linked_class.Get(); |
| 2135 | } |
| 2136 | |
Alex Light | a26e349 | 2017-06-27 17:55:37 -0700 | [diff] [blame] | 2137 | void Redefiner::ClassRedefinition::UnregisterJvmtiBreakpoints() { |
Vladimir Marko | 4617d58 | 2019-03-28 13:48:31 +0000 | [diff] [blame] | 2138 | BreakpointUtil::RemoveBreakpointsInClass(driver_->env_, GetMirrorClass().Ptr()); |
Alex Light | a26e349 | 2017-06-27 17:55:37 -0700 | [diff] [blame] | 2139 | } |
| 2140 | |
Alex Light | 5643caf | 2017-02-08 11:39:07 -0800 | [diff] [blame] | 2141 | void Redefiner::UnregisterAllBreakpoints() { |
Alex Light | 5643caf | 2017-02-08 11:39:07 -0800 | [diff] [blame] | 2142 | for (Redefiner::ClassRedefinition& redef : redefinitions_) { |
Alex Light | e34fe44 | 2018-02-21 17:35:55 -0800 | [diff] [blame] | 2143 | redef.UnregisterJvmtiBreakpoints(); |
Alex Light | 5643caf | 2017-02-08 11:39:07 -0800 | [diff] [blame] | 2144 | } |
| 2145 | } |
| 2146 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 2147 | bool Redefiner::CheckAllRedefinitionAreValid() { |
| 2148 | for (Redefiner::ClassRedefinition& redef : redefinitions_) { |
| 2149 | if (!redef.CheckRedefinitionIsValid()) { |
| 2150 | return false; |
| 2151 | } |
| 2152 | } |
| 2153 | return true; |
| 2154 | } |
| 2155 | |
Alex Light | 1e3926a | 2017-04-07 10:38:06 -0700 | [diff] [blame] | 2156 | void Redefiner::RestoreObsoleteMethodMapsIfUnneeded(RedefinitionDataHolder& holder) { |
| 2157 | for (RedefinitionDataIter data = holder.begin(); data != holder.end(); ++data) { |
| 2158 | data.GetRedefinition().RestoreObsoleteMethodMapsIfUnneeded(&data); |
| 2159 | } |
| 2160 | } |
| 2161 | |
Alex Light | fb53708 | 2019-12-10 14:38:34 -0800 | [diff] [blame] | 2162 | void Redefiner::MarkStructuralChanges(RedefinitionDataHolder& holder) { |
| 2163 | for (RedefinitionDataIter data = holder.begin(); data != holder.end(); ++data) { |
| 2164 | if (data.IsActuallyStructural()) { |
| 2165 | // A superclass was structural and it marked all subclasses already. No need to do anything. |
| 2166 | CHECK(!data.IsInitialStructural()); |
| 2167 | } else if (data.GetRedefinition().IsStructuralRedefinition()) { |
| 2168 | data.SetActuallyStructural(); |
| 2169 | data.SetInitialStructural(); |
| 2170 | // Go over all potential subtypes and mark any that are actually subclasses as structural. |
| 2171 | for (RedefinitionDataIter sub_data = data + 1; sub_data != holder.end(); ++sub_data) { |
| 2172 | if (sub_data.GetRedefinition().GetMirrorClass()->IsSubClass( |
| 2173 | data.GetRedefinition().GetMirrorClass())) { |
| 2174 | sub_data.SetActuallyStructural(); |
| 2175 | } |
| 2176 | } |
| 2177 | } |
| 2178 | } |
| 2179 | } |
| 2180 | |
Alex Light | 1e3926a | 2017-04-07 10:38:06 -0700 | [diff] [blame] | 2181 | bool Redefiner::EnsureAllClassAllocationsFinished(RedefinitionDataHolder& holder) { |
| 2182 | for (RedefinitionDataIter data = holder.begin(); data != holder.end(); ++data) { |
| 2183 | if (!data.GetRedefinition().EnsureClassAllocationsFinished(&data)) { |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 2184 | return false; |
| 2185 | } |
| 2186 | } |
| 2187 | return true; |
| 2188 | } |
| 2189 | |
Alex Light | 986914b | 2019-11-19 01:12:25 +0000 | [diff] [blame] | 2190 | bool Redefiner::CollectAndCreateNewInstances(RedefinitionDataHolder& holder) { |
| 2191 | for (RedefinitionDataIter data = holder.begin(); data != holder.end(); ++data) { |
| 2192 | // Allocate the data this redefinition requires. |
| 2193 | if (!data.GetRedefinition().CollectAndCreateNewInstances(&data)) { |
| 2194 | return false; |
| 2195 | } |
| 2196 | } |
| 2197 | return true; |
| 2198 | } |
| 2199 | |
Alex Light | fb53708 | 2019-12-10 14:38:34 -0800 | [diff] [blame] | 2200 | bool Redefiner::FinishAllNewClassAllocations(RedefinitionDataHolder& holder) { |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 2201 | for (RedefinitionDataIter data = holder.begin(); data != holder.end(); ++data) { |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 2202 | // Allocate the data this redefinition requires. |
Alex Light | fb53708 | 2019-12-10 14:38:34 -0800 | [diff] [blame] | 2203 | if (!data.GetRedefinition().FinishNewClassAllocations(holder, &data)) { |
| 2204 | return false; |
| 2205 | } |
| 2206 | } |
| 2207 | return true; |
| 2208 | } |
| 2209 | |
| 2210 | bool Redefiner::FinishAllRemainingCommonAllocations(RedefinitionDataHolder& holder) { |
| 2211 | for (RedefinitionDataIter data = holder.begin(); data != holder.end(); ++data) { |
| 2212 | // Allocate the data this redefinition requires. |
| 2213 | if (!data.GetRedefinition().FinishRemainingCommonAllocations(&data)) { |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 2214 | return false; |
| 2215 | } |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 2216 | } |
| 2217 | return true; |
| 2218 | } |
| 2219 | |
| 2220 | void Redefiner::ClassRedefinition::ReleaseDexFile() { |
Andreas Gampe | afaf7f8 | 2018-10-16 11:32:38 -0700 | [diff] [blame] | 2221 | dex_file_.release(); // NOLINT b/117926937 |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 2222 | } |
| 2223 | |
| 2224 | void Redefiner::ReleaseAllDexFiles() { |
| 2225 | for (Redefiner::ClassRedefinition& redef : redefinitions_) { |
| 2226 | redef.ReleaseDexFile(); |
| 2227 | } |
| 2228 | } |
| 2229 | |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 2230 | bool Redefiner::CheckAllClassesAreVerified(RedefinitionDataHolder& holder) { |
| 2231 | for (RedefinitionDataIter data = holder.begin(); data != holder.end(); ++data) { |
| 2232 | if (!data.GetRedefinition().CheckVerification(data)) { |
Alex Light | 8c889d2 | 2017-02-06 13:58:27 -0800 | [diff] [blame] | 2233 | return false; |
| 2234 | } |
Alex Light | 8c889d2 | 2017-02-06 13:58:27 -0800 | [diff] [blame] | 2235 | } |
| 2236 | return true; |
| 2237 | } |
| 2238 | |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 2239 | class ScopedDisableConcurrentAndMovingGc { |
| 2240 | public: |
| 2241 | ScopedDisableConcurrentAndMovingGc(art::gc::Heap* heap, art::Thread* self) |
| 2242 | : heap_(heap), self_(self) { |
| 2243 | if (heap_->IsGcConcurrentAndMoving()) { |
| 2244 | heap_->IncrementDisableMovingGC(self_); |
| 2245 | } |
| 2246 | } |
| 2247 | |
| 2248 | ~ScopedDisableConcurrentAndMovingGc() { |
| 2249 | if (heap_->IsGcConcurrentAndMoving()) { |
| 2250 | heap_->DecrementDisableMovingGC(self_); |
| 2251 | } |
| 2252 | } |
| 2253 | private: |
| 2254 | art::gc::Heap* heap_; |
| 2255 | art::Thread* self_; |
| 2256 | }; |
| 2257 | |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 2258 | class ClassDefinitionPauser : public art::ClassLoadCallback { |
| 2259 | public: |
| 2260 | explicit ClassDefinitionPauser(art::Thread* self) REQUIRES_SHARED(art::Locks::mutator_lock_) |
| 2261 | : self_(self), |
| 2262 | is_running_(false), |
| 2263 | barrier_(0), |
| 2264 | release_mu_("SuspendClassDefinition lock", art::kGenericBottomLock), |
| 2265 | release_barrier_(0), |
| 2266 | release_cond_("SuspendClassDefinition condvar", release_mu_), |
| 2267 | count_(0), |
| 2268 | release_(false) { |
| 2269 | art::Locks::mutator_lock_->AssertSharedHeld(self_); |
| 2270 | } |
| 2271 | ~ClassDefinitionPauser() REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 2272 | art::Locks::mutator_lock_->AssertSharedHeld(self_); |
Alex Light | f6c69e5 | 2020-01-17 15:30:40 -0800 | [diff] [blame] | 2273 | CHECK(release_) << "Must call Release()"; |
| 2274 | } |
| 2275 | void Release() REQUIRES(art::Locks::mutator_lock_) { |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 2276 | if (is_running_) { |
Alex Light | f6c69e5 | 2020-01-17 15:30:40 -0800 | [diff] [blame] | 2277 | art::Locks::mutator_lock_->AssertExclusiveHeld(self_); |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 2278 | uint32_t count; |
| 2279 | // Wake up everything. |
| 2280 | { |
| 2281 | art::MutexLock mu(self_, release_mu_); |
| 2282 | release_ = true; |
Alex Light | f6c69e5 | 2020-01-17 15:30:40 -0800 | [diff] [blame] | 2283 | // We have an exclusive mutator so all threads must be suspended and therefore they've |
| 2284 | // either already incremented this count_ or they are stuck somewhere before it. |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 2285 | count = count_; |
| 2286 | release_cond_.Broadcast(self_); |
| 2287 | } |
| 2288 | // Wait for all threads to leave this structs code. |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 2289 | VLOG(plugin) << "Resuming " << count << " threads paused before class-allocation!"; |
Alex Light | f6c69e5 | 2020-01-17 15:30:40 -0800 | [diff] [blame] | 2290 | release_barrier_.Increment</*locks=*/art::Barrier::kAllowHoldingLocks>(self_, count); |
| 2291 | } else { |
| 2292 | release_ = true; |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 2293 | } |
| 2294 | } |
| 2295 | void BeginDefineClass() override REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 2296 | art::Thread* this_thread = art::Thread::Current(); |
| 2297 | if (this_thread == self_) { |
| 2298 | // Allow the redefining thread to do whatever. |
| 2299 | return; |
| 2300 | } |
| 2301 | if (this_thread->GetDefineClassCount() != 0) { |
| 2302 | // We are in the middle of a recursive define-class. Don't suspend now allow it to finish. |
| 2303 | VLOG(plugin) << "Recursive DefineClass in " << *this_thread |
| 2304 | << " allowed to proceed despite class-def pause initiated by " << *self_; |
| 2305 | return; |
| 2306 | } |
Alex Light | f6c69e5 | 2020-01-17 15:30:40 -0800 | [diff] [blame] | 2307 | // If we are suspended (no mutator-lock) then the pausing thread could do everything before the |
| 2308 | // count_++ including destroying this object, causing UAF/deadlock. |
| 2309 | art::Locks::mutator_lock_->AssertSharedHeld(this_thread); |
| 2310 | ++count_; |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 2311 | art::ScopedThreadSuspension sts(this_thread, art::ThreadState::kSuspended); |
| 2312 | { |
| 2313 | art::MutexLock mu(this_thread, release_mu_); |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 2314 | VLOG(plugin) << "Suspending " << *this_thread << " due to class definition. class-def pause " |
| 2315 | << "initiated by " << *self_; |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 2316 | while (!release_) { |
| 2317 | release_cond_.Wait(this_thread); |
| 2318 | } |
| 2319 | } |
| 2320 | release_barrier_.Pass(this_thread); |
| 2321 | } |
Alex Light | f6c69e5 | 2020-01-17 15:30:40 -0800 | [diff] [blame] | 2322 | |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 2323 | void EndDefineClass() override REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 2324 | art::Thread* this_thread = art::Thread::Current(); |
| 2325 | if (this_thread == self_) { |
| 2326 | // Allow the redefining thread to do whatever. |
| 2327 | return; |
| 2328 | } |
| 2329 | if (this_thread->GetDefineClassCount() == 0) { |
| 2330 | // We are done with defining classes. |
| 2331 | barrier_.Pass(this_thread); |
| 2332 | } |
| 2333 | } |
| 2334 | |
| 2335 | void ClassLoad(art::Handle<art::mirror::Class> klass ATTRIBUTE_UNUSED) override {} |
| 2336 | void ClassPrepare(art::Handle<art::mirror::Class> klass1 ATTRIBUTE_UNUSED, |
| 2337 | art::Handle<art::mirror::Class> klass2 ATTRIBUTE_UNUSED) override {} |
| 2338 | |
| 2339 | void SetRunning() { |
| 2340 | is_running_ = true; |
| 2341 | } |
| 2342 | void WaitFor(uint32_t t) REQUIRES(!art::Locks::mutator_lock_) { |
| 2343 | barrier_.Increment(self_, t); |
| 2344 | } |
| 2345 | |
| 2346 | private: |
| 2347 | art::Thread* self_; |
| 2348 | bool is_running_; |
| 2349 | art::Barrier barrier_; |
| 2350 | art::Mutex release_mu_; |
| 2351 | art::Barrier release_barrier_; |
| 2352 | art::ConditionVariable release_cond_; |
Alex Light | f6c69e5 | 2020-01-17 15:30:40 -0800 | [diff] [blame] | 2353 | std::atomic<uint32_t> count_; |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 2354 | bool release_; |
| 2355 | }; |
| 2356 | |
| 2357 | class ScopedSuspendClassLoading { |
| 2358 | public: |
| 2359 | ScopedSuspendClassLoading(art::Thread* self, art::Runtime* runtime, RedefinitionDataHolder& h) |
| 2360 | REQUIRES_SHARED(art::Locks::mutator_lock_) |
Alex Light | f6c69e5 | 2020-01-17 15:30:40 -0800 | [diff] [blame] | 2361 | : self_(self), runtime_(runtime), pauser_() { |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 2362 | if (std::any_of(h.begin(), h.end(), [](auto r) REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 2363 | return r.GetRedefinition().IsStructuralRedefinition(); |
| 2364 | })) { |
| 2365 | VLOG(plugin) << "Pausing Class loading for structural redefinition."; |
Alex Light | f6c69e5 | 2020-01-17 15:30:40 -0800 | [diff] [blame] | 2366 | pauser_.emplace(self); |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 2367 | { |
| 2368 | art::ScopedThreadSuspension sts(self_, art::ThreadState::kNative); |
| 2369 | uint32_t in_progress_defines = 0; |
| 2370 | { |
| 2371 | art::ScopedSuspendAll ssa(__FUNCTION__); |
Alex Light | f6c69e5 | 2020-01-17 15:30:40 -0800 | [diff] [blame] | 2372 | pauser_->SetRunning(); |
| 2373 | runtime_->GetRuntimeCallbacks()->AddClassLoadCallback(&pauser_.value()); |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 2374 | art::MutexLock mu(self_, *art::Locks::thread_list_lock_); |
| 2375 | runtime_->GetThreadList()->ForEach([&](art::Thread* t) { |
| 2376 | if (t != self_ && t->GetDefineClassCount() != 0) { |
| 2377 | in_progress_defines++; |
| 2378 | } |
| 2379 | }); |
Alex Light | f6c69e5 | 2020-01-17 15:30:40 -0800 | [diff] [blame] | 2380 | VLOG(plugin) << "Waiting for " << in_progress_defines |
| 2381 | << " in progress class-loads to finish"; |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 2382 | } |
Alex Light | f6c69e5 | 2020-01-17 15:30:40 -0800 | [diff] [blame] | 2383 | pauser_->WaitFor(in_progress_defines); |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 2384 | } |
| 2385 | } |
| 2386 | } |
| 2387 | ~ScopedSuspendClassLoading() { |
Alex Light | f6c69e5 | 2020-01-17 15:30:40 -0800 | [diff] [blame] | 2388 | if (pauser_.has_value()) { |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 2389 | art::ScopedThreadSuspension sts(self_, art::ThreadState::kNative); |
| 2390 | art::ScopedSuspendAll ssa(__FUNCTION__); |
Alex Light | f6c69e5 | 2020-01-17 15:30:40 -0800 | [diff] [blame] | 2391 | pauser_->Release(); |
| 2392 | runtime_->GetRuntimeCallbacks()->RemoveClassLoadCallback(&pauser_.value()); |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 2393 | } |
| 2394 | } |
| 2395 | |
| 2396 | private: |
| 2397 | art::Thread* self_; |
| 2398 | art::Runtime* runtime_; |
Alex Light | f6c69e5 | 2020-01-17 15:30:40 -0800 | [diff] [blame] | 2399 | std::optional<ClassDefinitionPauser> pauser_; |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 2400 | }; |
| 2401 | |
Alex Light | 986914b | 2019-11-19 01:12:25 +0000 | [diff] [blame] | 2402 | class ScopedSuspendAllocations { |
| 2403 | public: |
| 2404 | ScopedSuspendAllocations(art::Runtime* runtime, RedefinitionDataHolder& h) |
| 2405 | REQUIRES_SHARED(art::Locks::mutator_lock_) |
| 2406 | : paused_(false) { |
| 2407 | if (std::any_of(h.begin(), |
| 2408 | h.end(), |
| 2409 | [](auto r) REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 2410 | return r.GetRedefinition().IsStructuralRedefinition(); |
| 2411 | })) { |
| 2412 | VLOG(plugin) << "Pausing allocations for structural redefinition."; |
| 2413 | paused_ = true; |
| 2414 | AllocationManager::Get()->PauseAllocations(art::Thread::Current()); |
| 2415 | // Collect garbage so we don't need to recreate as much. |
| 2416 | runtime->GetHeap()->CollectGarbage(/*clear_soft_references=*/false); |
| 2417 | } |
| 2418 | } |
| 2419 | |
| 2420 | ~ScopedSuspendAllocations() REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 2421 | if (paused_) { |
| 2422 | AllocationManager::Get()->ResumeAllocations(art::Thread::Current()); |
| 2423 | } |
| 2424 | } |
| 2425 | |
| 2426 | private: |
| 2427 | bool paused_; |
| 2428 | |
| 2429 | DISALLOW_COPY_AND_ASSIGN(ScopedSuspendAllocations); |
| 2430 | }; |
| 2431 | |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 2432 | jvmtiError Redefiner::Run() { |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 2433 | art::StackHandleScope<1> hs(self_); |
Alex Light | fb53708 | 2019-12-10 14:38:34 -0800 | [diff] [blame] | 2434 | // Sort the redefinitions_ array topologically by class. This makes later steps easier since we |
| 2435 | // know that every class precedes all of its supertypes. |
| 2436 | std::sort(redefinitions_.begin(), |
| 2437 | redefinitions_.end(), |
| 2438 | [&](auto& l, auto& r) REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 2439 | return CompareClasses(l.GetMirrorClass(), r.GetMirrorClass()); |
| 2440 | }); |
| 2441 | // Allocate an array to hold onto all java temporary objects associated with this |
| 2442 | // redefinition. We will let this be collected after the end of this function. |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 2443 | RedefinitionDataHolder holder(&hs, runtime_, self_, &redefinitions_); |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 2444 | if (holder.IsNull()) { |
| 2445 | self_->AssertPendingOOMException(); |
| 2446 | self_->ClearException(); |
| 2447 | RecordFailure(ERR(OUT_OF_MEMORY), "Could not allocate storage for temporaries"); |
| 2448 | return result_; |
| 2449 | } |
| 2450 | |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 2451 | // First we just allocate the ClassExt and its fields that we need. These can be updated |
Alex Light | fb53708 | 2019-12-10 14:38:34 -0800 | [diff] [blame] | 2452 | // atomically without any issues (since we allocate the map arrays as empty). |
| 2453 | if (!CheckAllRedefinitionAreValid()) { |
| 2454 | return result_; |
| 2455 | } |
| 2456 | // Mark structural changes. |
| 2457 | MarkStructuralChanges(holder); |
| 2458 | // Now we pause class loading. If we are doing a structural redefinition we will need to get an |
| 2459 | // accurate picture of the classes loaded and having loads in the middle would make that |
| 2460 | // impossible. This only pauses class-loading if we actually have at least one structural |
| 2461 | // redefinition. |
| 2462 | ScopedSuspendClassLoading suspend_class_load(self_, runtime_, holder); |
| 2463 | if (!EnsureAllClassAllocationsFinished(holder) || |
| 2464 | !FinishAllRemainingCommonAllocations(holder) || |
| 2465 | !FinishAllNewClassAllocations(holder) || |
Alex Light | 8c889d2 | 2017-02-06 13:58:27 -0800 | [diff] [blame] | 2466 | !CheckAllClassesAreVerified(holder)) { |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 2467 | return result_; |
| 2468 | } |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 2469 | |
Alex Light | 986914b | 2019-11-19 01:12:25 +0000 | [diff] [blame] | 2470 | ScopedSuspendAllocations suspend_alloc(runtime_, holder); |
| 2471 | if (!CollectAndCreateNewInstances(holder)) { |
| 2472 | return result_; |
| 2473 | } |
| 2474 | |
Alex Light | 5643caf | 2017-02-08 11:39:07 -0800 | [diff] [blame] | 2475 | // At this point we can no longer fail without corrupting the runtime state. |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 2476 | for (RedefinitionDataIter data = holder.begin(); data != holder.end(); ++data) { |
Alex Light | 07f0621 | 2017-06-01 14:01:43 -0700 | [diff] [blame] | 2477 | art::ClassLinker* cl = runtime_->GetClassLinker(); |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 2478 | if (data.GetSourceClassLoader() == nullptr) { |
David Srbecky | 11943fc | 2021-02-23 22:45:33 +0000 | [diff] [blame] | 2479 | // AppendToBootClassPath includes dex file registration. |
Nicolas Geoffray | 7913cf3 | 2022-02-16 09:28:21 +0000 | [diff] [blame] | 2480 | cl->AppendToBootClassPath(&data.GetRedefinition().GetDexFile(), data.GetNewDexCache()); |
David Srbecky | 11943fc | 2021-02-23 22:45:33 +0000 | [diff] [blame] | 2481 | } else { |
| 2482 | cl->RegisterExistingDexCache(data.GetNewDexCache(), data.GetSourceClassLoader()); |
Alex Light | 7916f20 | 2017-01-27 09:00:15 -0800 | [diff] [blame] | 2483 | } |
Nicolas Geoffray | 7913cf3 | 2022-02-16 09:28:21 +0000 | [diff] [blame] | 2484 | DCHECK_EQ(cl->FindDexCache(self_, data.GetRedefinition().GetDexFile()), data.GetNewDexCache()); |
Alex Light | 7916f20 | 2017-01-27 09:00:15 -0800 | [diff] [blame] | 2485 | } |
Alex Light | 5643caf | 2017-02-08 11:39:07 -0800 | [diff] [blame] | 2486 | UnregisterAllBreakpoints(); |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 2487 | |
Alex Light | c2d0c96 | 2019-10-23 14:14:25 -0700 | [diff] [blame] | 2488 | { |
| 2489 | // Disable GC and wait for it to be done if we are a moving GC. This is fine since we are done |
| 2490 | // allocating so no deadlocks. |
| 2491 | ScopedDisableConcurrentAndMovingGc sdcamgc(runtime_->GetHeap(), self_); |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 2492 | |
Alex Light | c2d0c96 | 2019-10-23 14:14:25 -0700 | [diff] [blame] | 2493 | // Do transition to final suspension |
| 2494 | // TODO We might want to give this its own suspended state! |
| 2495 | // TODO This isn't right. We need to change state without any chance of suspend ideally! |
| 2496 | art::ScopedThreadSuspension sts(self_, art::ThreadState::kNative); |
| 2497 | art::ScopedSuspendAll ssa("Final installation of redefined Classes!", /*long_suspend=*/true); |
| 2498 | for (RedefinitionDataIter data = holder.begin(); data != holder.end(); ++data) { |
| 2499 | art::ScopedAssertNoThreadSuspension nts("Updating runtime objects for redefinition"); |
| 2500 | ClassRedefinition& redef = data.GetRedefinition(); |
| 2501 | if (data.GetSourceClassLoader() != nullptr) { |
| 2502 | ClassLoaderHelper::UpdateJavaDexFile(data.GetJavaDexFile(), data.GetNewDexFileCookie()); |
| 2503 | } |
| 2504 | redef.UpdateClass(data); |
Alex Light | 7916f20 | 2017-01-27 09:00:15 -0800 | [diff] [blame] | 2505 | } |
Alex Light | c2d0c96 | 2019-10-23 14:14:25 -0700 | [diff] [blame] | 2506 | RestoreObsoleteMethodMapsIfUnneeded(holder); |
| 2507 | // TODO We should check for if any of the redefined methods are intrinsic methods here and, if |
| 2508 | // any are, force a full-world deoptimization before finishing redefinition. If we don't do this |
| 2509 | // then methods that have been jitted prior to the current redefinition being applied might |
| 2510 | // continue to use the old versions of the intrinsics! |
| 2511 | // TODO Do the dex_file release at a more reasonable place. This works but it muddles who really |
| 2512 | // owns the DexFile and when ownership is transferred. |
| 2513 | ReleaseAllDexFiles(); |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 2514 | } |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 2515 | return OK; |
| 2516 | } |
| 2517 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 2518 | void Redefiner::ClassRedefinition::UpdateMethods(art::ObjPtr<art::mirror::Class> mclass, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 2519 | const art::dex::ClassDef& class_def) { |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 2520 | art::ClassLinker* linker = driver_->runtime_->GetClassLinker(); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 2521 | art::PointerSize image_pointer_size = linker->GetImagePointerSize(); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 2522 | const art::dex::TypeId& declaring_class_id = dex_file_->GetTypeId(class_def.class_idx_); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 2523 | const art::DexFile& old_dex_file = mclass->GetDexFile(); |
Alex Light | 200b9d7 | 2016-12-15 11:34:13 -0800 | [diff] [blame] | 2524 | // Update methods. |
Alex Light | 00e475c | 2017-07-19 16:36:23 -0700 | [diff] [blame] | 2525 | for (art::ArtMethod& method : mclass->GetDeclaredMethods(image_pointer_size)) { |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 2526 | const art::dex::StringId* new_name_id = dex_file_->FindStringId(method.GetName()); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 2527 | art::dex::TypeIndex method_return_idx = |
| 2528 | dex_file_->GetIndexForTypeId(*dex_file_->FindTypeId(method.GetReturnTypeDescriptor())); |
| 2529 | const auto* old_type_list = method.GetParameterTypeList(); |
| 2530 | std::vector<art::dex::TypeIndex> new_type_list; |
| 2531 | for (uint32_t i = 0; old_type_list != nullptr && i < old_type_list->Size(); i++) { |
| 2532 | new_type_list.push_back( |
| 2533 | dex_file_->GetIndexForTypeId( |
| 2534 | *dex_file_->FindTypeId( |
| 2535 | old_dex_file.GetTypeDescriptor( |
| 2536 | old_dex_file.GetTypeId( |
| 2537 | old_type_list->GetTypeItem(i).type_idx_))))); |
| 2538 | } |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 2539 | const art::dex::ProtoId* proto_id = dex_file_->FindProtoId(method_return_idx, new_type_list); |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 2540 | CHECK(proto_id != nullptr || old_type_list == nullptr); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 2541 | const art::dex::MethodId* method_id = dex_file_->FindMethodId(declaring_class_id, |
| 2542 | *new_name_id, |
| 2543 | *proto_id); |
Alex Light | dba6148 | 2016-12-21 08:20:29 -0800 | [diff] [blame] | 2544 | CHECK(method_id != nullptr); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 2545 | uint32_t dex_method_idx = dex_file_->GetIndexForMethodId(*method_id); |
| 2546 | method.SetDexMethodIndex(dex_method_idx); |
Nicolas Geoffray | c8a694d | 2022-01-17 17:12:38 +0000 | [diff] [blame] | 2547 | driver_->runtime_->GetInstrumentation()->InitializeMethodsCode(&method, /*aot_code=*/ nullptr); |
Nicolas Geoffray | 4717175 | 2020-08-31 15:03:20 +0100 | [diff] [blame] | 2548 | if (method.HasCodeItem()) { |
| 2549 | method.SetCodeItem( |
zhaoxuyang | 7156ea2 | 2022-01-10 13:58:11 +0800 | [diff] [blame] | 2550 | dex_file_->GetCodeItem(dex_file_->FindCodeItemOffset(class_def, dex_method_idx)), |
| 2551 | dex_file_->IsCompactDexFile()); |
Nicolas Geoffray | 4717175 | 2020-08-31 15:03:20 +0100 | [diff] [blame] | 2552 | } |
Alex Light | 7532d58 | 2017-02-13 16:36:06 -0800 | [diff] [blame] | 2553 | // Clear all the intrinsics related flags. |
Orion Hodson | cfcc9cf | 2017-09-29 15:07:27 +0100 | [diff] [blame] | 2554 | method.SetNotIntrinsic(); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 2555 | } |
Alex Light | 200b9d7 | 2016-12-15 11:34:13 -0800 | [diff] [blame] | 2556 | } |
| 2557 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 2558 | void Redefiner::ClassRedefinition::UpdateFields(art::ObjPtr<art::mirror::Class> mclass) { |
Alex Light | 200b9d7 | 2016-12-15 11:34:13 -0800 | [diff] [blame] | 2559 | // TODO The IFields & SFields pointers should be combined like the methods_ arrays were. |
| 2560 | for (auto fields_iter : {mclass->GetIFields(), mclass->GetSFields()}) { |
| 2561 | for (art::ArtField& field : fields_iter) { |
| 2562 | std::string declaring_class_name; |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 2563 | const art::dex::TypeId* new_declaring_id = |
Alex Light | 200b9d7 | 2016-12-15 11:34:13 -0800 | [diff] [blame] | 2564 | dex_file_->FindTypeId(field.GetDeclaringClass()->GetDescriptor(&declaring_class_name)); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 2565 | const art::dex::StringId* new_name_id = dex_file_->FindStringId(field.GetName()); |
| 2566 | const art::dex::TypeId* new_type_id = dex_file_->FindTypeId(field.GetTypeDescriptor()); |
Alex Light | 200b9d7 | 2016-12-15 11:34:13 -0800 | [diff] [blame] | 2567 | CHECK(new_name_id != nullptr && new_type_id != nullptr && new_declaring_id != nullptr); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 2568 | const art::dex::FieldId* new_field_id = |
Alex Light | 200b9d7 | 2016-12-15 11:34:13 -0800 | [diff] [blame] | 2569 | dex_file_->FindFieldId(*new_declaring_id, *new_name_id, *new_type_id); |
| 2570 | CHECK(new_field_id != nullptr); |
Alex Light | 163652e | 2020-01-08 15:18:25 -0800 | [diff] [blame] | 2571 | uint32_t new_field_index = dex_file_->GetIndexForFieldId(*new_field_id); |
Alex Light | 200b9d7 | 2016-12-15 11:34:13 -0800 | [diff] [blame] | 2572 | // We only need to update the index since the other data in the ArtField cannot be updated. |
Alex Light | 163652e | 2020-01-08 15:18:25 -0800 | [diff] [blame] | 2573 | field.SetDexFieldIndex(new_field_index); |
Alex Light | 200b9d7 | 2016-12-15 11:34:13 -0800 | [diff] [blame] | 2574 | } |
| 2575 | } |
Alex Light | 200b9d7 | 2016-12-15 11:34:13 -0800 | [diff] [blame] | 2576 | } |
| 2577 | |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 2578 | void Redefiner::ClassRedefinition::CollectNewFieldAndMethodMappings( |
| 2579 | const RedefinitionDataIter& data, |
| 2580 | std::map<art::ArtMethod*, art::ArtMethod*>* method_map, |
| 2581 | std::map<art::ArtField*, art::ArtField*>* field_map) { |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 2582 | for (auto [new_cls, old_cls] : |
| 2583 | art::ZipLeft(data.GetNewClasses()->Iterate(), data.GetOldClasses()->Iterate())) { |
| 2584 | for (art::ArtField& f : old_cls->GetSFields()) { |
| 2585 | (*field_map)[&f] = new_cls->FindDeclaredStaticField(f.GetName(), f.GetTypeDescriptor()); |
| 2586 | } |
| 2587 | for (art::ArtField& f : old_cls->GetIFields()) { |
| 2588 | (*field_map)[&f] = new_cls->FindDeclaredInstanceField(f.GetName(), f.GetTypeDescriptor()); |
| 2589 | } |
| 2590 | auto new_methods = new_cls->GetMethods(art::kRuntimePointerSize); |
| 2591 | for (art::ArtMethod& m : old_cls->GetMethods(art::kRuntimePointerSize)) { |
| 2592 | // No support for finding methods in this way since it's generally not needed. Just do it the |
| 2593 | // easy way. |
| 2594 | auto nm_iter = std::find_if( |
| 2595 | new_methods.begin(), |
| 2596 | new_methods.end(), |
| 2597 | [&](art::ArtMethod& cand) REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 2598 | return cand.GetNameView() == m.GetNameView() && cand.GetSignature() == m.GetSignature(); |
| 2599 | }); |
| 2600 | CHECK(nm_iter != new_methods.end()) |
| 2601 | << "Could not find redefined version of " << m.PrettyMethod(); |
| 2602 | (*method_map)[&m] = &(*nm_iter); |
| 2603 | } |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 2604 | } |
| 2605 | } |
| 2606 | |
Alex Light | 986914b | 2019-11-19 01:12:25 +0000 | [diff] [blame] | 2607 | static void CopyField(art::ObjPtr<art::mirror::Object> target, |
| 2608 | art::ArtField* new_field, |
| 2609 | art::ObjPtr<art::mirror::Object> source, |
| 2610 | art::ArtField& old_field) REQUIRES(art::Locks::mutator_lock_) { |
| 2611 | art::Primitive::Type ftype = old_field.GetTypeAsPrimitiveType(); |
| 2612 | CHECK_EQ(ftype, new_field->GetTypeAsPrimitiveType()) |
| 2613 | << old_field.PrettyField() << " vs " << new_field->PrettyField(); |
| 2614 | if (ftype == art::Primitive::kPrimNot) { |
| 2615 | new_field->SetObject<false>(target, old_field.GetObject(source)); |
| 2616 | } else { |
| 2617 | switch (ftype) { |
| 2618 | #define UPDATE_FIELD(TYPE) \ |
| 2619 | case art::Primitive::kPrim##TYPE: \ |
| 2620 | new_field->Set##TYPE<false>(target, old_field.Get##TYPE(source)); \ |
| 2621 | break |
| 2622 | UPDATE_FIELD(Int); |
| 2623 | UPDATE_FIELD(Float); |
| 2624 | UPDATE_FIELD(Long); |
| 2625 | UPDATE_FIELD(Double); |
| 2626 | UPDATE_FIELD(Short); |
| 2627 | UPDATE_FIELD(Char); |
| 2628 | UPDATE_FIELD(Byte); |
| 2629 | UPDATE_FIELD(Boolean); |
| 2630 | case art::Primitive::kPrimNot: |
| 2631 | case art::Primitive::kPrimVoid: |
| 2632 | LOG(FATAL) << "Unexpected field with type " << ftype << " found!"; |
| 2633 | UNREACHABLE(); |
| 2634 | #undef UPDATE_FIELD |
| 2635 | } |
| 2636 | } |
| 2637 | } |
| 2638 | |
| 2639 | static void CopyFields(bool is_static, |
| 2640 | art::ObjPtr<art::mirror::Object> target, |
| 2641 | art::ObjPtr<art::mirror::Class> target_class, |
| 2642 | art::ObjPtr<art::mirror::Object> source, |
| 2643 | art::ObjPtr<art::mirror::Class> source_class) |
| 2644 | REQUIRES(art::Locks::mutator_lock_) { |
| 2645 | DCHECK(!source_class->IsObjectClass() && !target_class->IsObjectClass()) |
| 2646 | << "Should not be overriding object class fields. Target: " << target_class->PrettyClass() |
| 2647 | << " Source: " << source_class->PrettyClass(); |
| 2648 | for (art::ArtField& f : (is_static ? source_class->GetSFields() : source_class->GetIFields())) { |
| 2649 | art::ArtField* new_field = |
| 2650 | (is_static ? target_class->FindDeclaredStaticField(f.GetName(), f.GetTypeDescriptor()) |
| 2651 | : target_class->FindDeclaredInstanceField(f.GetName(), f.GetTypeDescriptor())); |
| 2652 | CHECK(new_field != nullptr) << "could not find new version of " << f.PrettyField(); |
| 2653 | CopyField(target, new_field, source, f); |
| 2654 | } |
| 2655 | if (!is_static && !target_class->GetSuperClass()->IsObjectClass()) { |
| 2656 | CopyFields( |
| 2657 | is_static, target, target_class->GetSuperClass(), source, source_class->GetSuperClass()); |
| 2658 | } |
| 2659 | } |
| 2660 | |
| 2661 | static void ClearField(art::ObjPtr<art::mirror::Object> target, art::ArtField& field) |
| 2662 | REQUIRES(art::Locks::mutator_lock_) { |
| 2663 | art::Primitive::Type ftype = field.GetTypeAsPrimitiveType(); |
| 2664 | if (ftype == art::Primitive::kPrimNot) { |
| 2665 | field.SetObject<false>(target, nullptr); |
| 2666 | } else { |
| 2667 | switch (ftype) { |
| 2668 | #define UPDATE_FIELD(TYPE) \ |
| 2669 | case art::Primitive::kPrim##TYPE: \ |
| 2670 | field.Set##TYPE<false>(target, 0); \ |
| 2671 | break |
| 2672 | UPDATE_FIELD(Int); |
| 2673 | UPDATE_FIELD(Float); |
| 2674 | UPDATE_FIELD(Long); |
| 2675 | UPDATE_FIELD(Double); |
| 2676 | UPDATE_FIELD(Short); |
| 2677 | UPDATE_FIELD(Char); |
| 2678 | UPDATE_FIELD(Byte); |
| 2679 | UPDATE_FIELD(Boolean); |
| 2680 | case art::Primitive::kPrimNot: |
| 2681 | case art::Primitive::kPrimVoid: |
| 2682 | LOG(FATAL) << "Unexpected field with type " << ftype << " found!"; |
| 2683 | UNREACHABLE(); |
| 2684 | #undef UPDATE_FIELD |
| 2685 | } |
| 2686 | } |
| 2687 | } |
| 2688 | |
| 2689 | static void ClearFields(bool is_static, |
| 2690 | art::ObjPtr<art::mirror::Object> target, |
| 2691 | art::ObjPtr<art::mirror::Class> target_class) |
| 2692 | REQUIRES(art::Locks::mutator_lock_) { |
| 2693 | DCHECK(!target_class->IsObjectClass()); |
| 2694 | for (art::ArtField& f : (is_static ? target_class->GetSFields() : target_class->GetIFields())) { |
| 2695 | ClearField(target, f); |
| 2696 | } |
| 2697 | if (!is_static && !target_class->GetSuperClass()->IsObjectClass()) { |
| 2698 | ClearFields(is_static, target, target_class->GetSuperClass()); |
| 2699 | } |
| 2700 | } |
| 2701 | |
| 2702 | static void CopyAndClearFields(bool is_static, |
| 2703 | art::ObjPtr<art::mirror::Object> target, |
| 2704 | art::ObjPtr<art::mirror::Class> target_class, |
| 2705 | art::ObjPtr<art::mirror::Object> source, |
| 2706 | art::ObjPtr<art::mirror::Class> source_class) |
| 2707 | REQUIRES(art::Locks::mutator_lock_) { |
| 2708 | // Copy all non-j.l.Object fields |
| 2709 | CopyFields(is_static, target, target_class, source, source_class); |
| 2710 | // Copy the lock-word. |
| 2711 | target->SetLockWord(source->GetLockWord(false), false); |
| 2712 | // Clear (reset) the old one. |
| 2713 | source->SetLockWord(art::LockWord::Default(), false); |
| 2714 | art::WriteBarrier::ForEveryFieldWrite(target); |
| 2715 | |
| 2716 | // Clear the fields from the old class. We don't need it anymore. |
| 2717 | ClearFields(is_static, source, source_class); |
| 2718 | art::WriteBarrier::ForEveryFieldWrite(source); |
| 2719 | } |
| 2720 | |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 2721 | void Redefiner::ClassRedefinition::UpdateClassStructurally(const RedefinitionDataIter& holder) { |
Alex Light | fb53708 | 2019-12-10 14:38:34 -0800 | [diff] [blame] | 2722 | DCHECK(holder.IsActuallyStructural()); |
| 2723 | DCHECK(holder.IsInitialStructural()); |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 2724 | // LETS GO. We've got all new class structures so no need to do all the updating of the stacks. |
| 2725 | // Instead we need to update everything else. |
| 2726 | // Just replace the class and be done with it. |
| 2727 | art::Locks::mutator_lock_->AssertExclusiveHeld(driver_->self_); |
Alex Light | 16527e8 | 2020-06-08 09:21:12 -0700 | [diff] [blame] | 2728 | art::ClassLinker* cl = driver_->runtime_->GetClassLinker(); |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 2729 | art::ScopedAssertNoThreadSuspension sants(__FUNCTION__); |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 2730 | art::ObjPtr<art::mirror::ObjectArray<art::mirror::Class>> new_classes(holder.GetNewClasses()); |
| 2731 | art::ObjPtr<art::mirror::ObjectArray<art::mirror::Class>> old_classes(holder.GetOldClasses()); |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 2732 | // Collect mappings from old to new fields/methods |
| 2733 | std::map<art::ArtMethod*, art::ArtMethod*> method_map; |
| 2734 | std::map<art::ArtField*, art::ArtField*> field_map; |
| 2735 | CollectNewFieldAndMethodMappings(holder, &method_map, &field_map); |
Alex Light | 986914b | 2019-11-19 01:12:25 +0000 | [diff] [blame] | 2736 | art::ObjPtr<art::mirror::ObjectArray<art::mirror::Object>> new_instances( |
| 2737 | holder.GetNewInstanceObjects()); |
| 2738 | art::ObjPtr<art::mirror::ObjectArray<art::mirror::Object>> old_instances( |
| 2739 | holder.GetOldInstanceObjects()); |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 2740 | // Once we do the ReplaceReferences old_classes will have the new_classes in it. We want to keep |
| 2741 | // ahold of the old classes so copy them now. |
| 2742 | std::vector<art::ObjPtr<art::mirror::Class>> old_classes_vec(old_classes->Iterate().begin(), |
| 2743 | old_classes->Iterate().end()); |
Alex Light | 986914b | 2019-11-19 01:12:25 +0000 | [diff] [blame] | 2744 | // Copy over the static fields of the class and all the instance fields. |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 2745 | for (auto [new_class, old_class] : art::ZipLeft(new_classes->Iterate(), old_classes->Iterate())) { |
| 2746 | CHECK(!new_class.IsNull()); |
| 2747 | CHECK(!old_class.IsNull()); |
| 2748 | CHECK(!old_class->IsErroneous()); |
| 2749 | if (old_class->GetStatus() > new_class->GetStatus()) { |
| 2750 | // Some verification/initialization step happened during interval between |
| 2751 | // creating the new class and now. Just copy the new status. |
| 2752 | new_class->SetStatusLocked(old_class->GetStatus()); |
| 2753 | } |
| 2754 | CopyAndClearFields(true, new_class, new_class, old_class, old_class); |
| 2755 | } |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 2756 | |
Alex Light | 986914b | 2019-11-19 01:12:25 +0000 | [diff] [blame] | 2757 | // Copy and clear the fields of the old-instances. |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 2758 | for (auto [new_instance, old_instance] : |
| 2759 | art::ZipLeft(new_instances->Iterate(), old_instances->Iterate())) { |
Alex Light | 986914b | 2019-11-19 01:12:25 +0000 | [diff] [blame] | 2760 | CopyAndClearFields(/*is_static=*/false, |
| 2761 | new_instance, |
| 2762 | new_instance->GetClass(), |
| 2763 | old_instance, |
| 2764 | old_instance->GetClass()); |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 2765 | } |
Alex Light | 16527e8 | 2020-06-08 09:21:12 -0700 | [diff] [blame] | 2766 | // Mark old class and methods obsolete. Copy over any native implementation as well. |
| 2767 | for (auto [old_class, new_class] : art::ZipLeft(old_classes->Iterate(), new_classes->Iterate())) { |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 2768 | old_class->SetObsoleteObject(); |
Alex Light | 16527e8 | 2020-06-08 09:21:12 -0700 | [diff] [blame] | 2769 | // Mark methods obsolete and copy native implementation. We need to wait |
| 2770 | // until later to actually clear the jit data. We copy the native |
| 2771 | // implementation here since we don't want to race with any threads doing |
| 2772 | // RegisterNatives. |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 2773 | for (art::ArtMethod& m : old_class->GetMethods(art::kRuntimePointerSize)) { |
Alex Light | 16527e8 | 2020-06-08 09:21:12 -0700 | [diff] [blame] | 2774 | if (m.IsNative()) { |
| 2775 | art::ArtMethod* new_method = |
| 2776 | new_class->FindClassMethod(m.GetNameView(), m.GetSignature(), art::kRuntimePointerSize); |
| 2777 | DCHECK(new_class->GetMethodsSlice(art::kRuntimePointerSize).Contains(new_method)) |
| 2778 | << "Could not find method " << m.PrettyMethod() << " declared in new class!"; |
| 2779 | DCHECK(new_method->IsNative()); |
| 2780 | new_method->SetEntryPointFromJni(m.GetEntryPointFromJni()); |
| 2781 | } |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 2782 | m.SetIsObsolete(); |
Alex Light | 16527e8 | 2020-06-08 09:21:12 -0700 | [diff] [blame] | 2783 | cl->SetEntryPointsForObsoleteMethod(&m); |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 2784 | if (m.IsInvokable()) { |
| 2785 | m.SetDontCompile(); |
| 2786 | } |
| 2787 | } |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 2788 | } |
Alex Light | c18eba3 | 2019-09-24 14:36:27 -0700 | [diff] [blame] | 2789 | // Update live pointers in ART code. |
Alex Light | 2462789 | 2019-11-06 10:28:21 -0800 | [diff] [blame] | 2790 | auto could_change_resolution_of = [&](auto* field_or_method, |
| 2791 | const auto& info) REQUIRES(art::Locks::mutator_lock_) { |
| 2792 | constexpr bool is_method = std::is_same_v<art::ArtMethod*, decltype(field_or_method)>; |
| 2793 | static_assert(is_method || std::is_same_v<art::ArtField*, decltype(field_or_method)>, |
| 2794 | "Input is not field or method!"); |
| 2795 | // Only dex-cache is used for resolution |
| 2796 | if (LIKELY(info.GetType() != art::ReflectionSourceType::kSourceDexCacheResolvedField && |
| 2797 | info.GetType() != art::ReflectionSourceType::kSourceDexCacheResolvedMethod)) { |
| 2798 | return false; |
| 2799 | } |
| 2800 | if constexpr (is_method) { |
| 2801 | // Only direct methods are used without further indirection through a vtable/IFTable. |
| 2802 | // Constructors cannot be shadowed. |
| 2803 | if (LIKELY(!field_or_method->IsDirect() || field_or_method->IsConstructor())) { |
| 2804 | return false; |
| 2805 | } |
| 2806 | } else { |
| 2807 | // Only non-private fields can be shadowed in a manner that's visible. |
| 2808 | if (LIKELY(field_or_method->IsPrivate())) { |
| 2809 | return false; |
| 2810 | } |
| 2811 | } |
| 2812 | // We can only shadow things from our superclasses |
Alex Light | bf6498e | 2020-07-21 17:03:26 -0700 | [diff] [blame] | 2813 | auto orig_classes_iter = old_classes->Iterate(); |
| 2814 | auto replacement_classes_iter = new_classes->Iterate(); |
| 2815 | art::ObjPtr<art::mirror::Class> f_or_m_class = field_or_method->GetDeclaringClass(); |
| 2816 | if (LIKELY(!f_or_m_class->IsAssignableFrom(holder.GetMirrorClass()) && |
| 2817 | std::find(orig_classes_iter.begin(), orig_classes_iter.end(), f_or_m_class) == |
| 2818 | orig_classes_iter.end())) { |
Alex Light | 2462789 | 2019-11-06 10:28:21 -0800 | [diff] [blame] | 2819 | return false; |
| 2820 | } |
| 2821 | if constexpr (is_method) { |
Alex Light | bf6498e | 2020-07-21 17:03:26 -0700 | [diff] [blame] | 2822 | return std::any_of( |
| 2823 | replacement_classes_iter.begin(), |
| 2824 | replacement_classes_iter.end(), |
| 2825 | [&](art::ObjPtr<art::mirror::Class> cand) REQUIRES(art::Locks::mutator_lock_) { |
| 2826 | auto direct_methods = cand->GetDirectMethods(art::kRuntimePointerSize); |
| 2827 | return std::find_if(direct_methods.begin(), |
| 2828 | direct_methods.end(), |
| 2829 | [&](art::ArtMethod& m) REQUIRES(art::Locks::mutator_lock_) { |
| 2830 | return UNLIKELY(m.HasSameNameAndSignature(field_or_method)); |
| 2831 | }) != direct_methods.end(); |
| 2832 | }); |
Alex Light | 2462789 | 2019-11-06 10:28:21 -0800 | [diff] [blame] | 2833 | } else { |
| 2834 | auto pred = [&](art::ArtField& f) REQUIRES(art::Locks::mutator_lock_) { |
| 2835 | return std::string_view(f.GetName()) == std::string_view(field_or_method->GetName()) && |
| 2836 | std::string_view(f.GetTypeDescriptor()) == |
| 2837 | std::string_view(field_or_method->GetTypeDescriptor()); |
| 2838 | }; |
| 2839 | if (field_or_method->IsStatic()) { |
Alex Light | bf6498e | 2020-07-21 17:03:26 -0700 | [diff] [blame] | 2840 | return std::any_of( |
| 2841 | replacement_classes_iter.begin(), |
| 2842 | replacement_classes_iter.end(), |
| 2843 | [&](art::ObjPtr<art::mirror::Class> cand) REQUIRES(art::Locks::mutator_lock_) { |
| 2844 | auto sfields = cand->GetSFields(); |
| 2845 | return std::find_if(sfields.begin(), sfields.end(), pred) != sfields.end(); |
| 2846 | }); |
Alex Light | 2462789 | 2019-11-06 10:28:21 -0800 | [diff] [blame] | 2847 | } else { |
Alex Light | bf6498e | 2020-07-21 17:03:26 -0700 | [diff] [blame] | 2848 | return std::any_of( |
| 2849 | replacement_classes_iter.begin(), |
| 2850 | replacement_classes_iter.end(), |
| 2851 | [&](art::ObjPtr<art::mirror::Class> cand) REQUIRES(art::Locks::mutator_lock_) { |
| 2852 | auto ifields = cand->GetIFields(); |
| 2853 | return std::find_if(ifields.begin(), ifields.end(), pred) != ifields.end(); |
| 2854 | }); |
Alex Light | 2462789 | 2019-11-06 10:28:21 -0800 | [diff] [blame] | 2855 | } |
| 2856 | } |
| 2857 | }; |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 2858 | // TODO Performing 2 stack-walks back to back isn't the greatest. We might want to try to combine |
| 2859 | // it with the one ReplaceReferences does. Doing so would be rather complicated though. |
Alex Light | c18eba3 | 2019-09-24 14:36:27 -0700 | [diff] [blame] | 2860 | driver_->runtime_->VisitReflectiveTargets( |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 2861 | [&](art::ArtField* f, const auto& info) REQUIRES(art::Locks::mutator_lock_) { |
Alex Light | 28b6efe | 2019-11-06 12:49:41 -0800 | [diff] [blame] | 2862 | DCHECK(f != nullptr) << info; |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 2863 | auto it = field_map.find(f); |
Alex Light | bf6498e | 2020-07-21 17:03:26 -0700 | [diff] [blame] | 2864 | if (UNLIKELY(could_change_resolution_of(f, info))) { |
| 2865 | // Dex-cache Resolution might change. Just clear the resolved value. |
| 2866 | VLOG(plugin) << "Clearing resolution " << info << " for (field) " << f->PrettyField(); |
| 2867 | return static_cast<art::ArtField*>(nullptr); |
| 2868 | } else if (it != field_map.end()) { |
Alex Light | 2462789 | 2019-11-06 10:28:21 -0800 | [diff] [blame] | 2869 | VLOG(plugin) << "Updating " << info << " object for (field) " |
| 2870 | << it->second->PrettyField(); |
| 2871 | return it->second; |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 2872 | } |
Alex Light | 2462789 | 2019-11-06 10:28:21 -0800 | [diff] [blame] | 2873 | return f; |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 2874 | }, |
| 2875 | [&](art::ArtMethod* m, const auto& info) REQUIRES(art::Locks::mutator_lock_) { |
Alex Light | 28b6efe | 2019-11-06 12:49:41 -0800 | [diff] [blame] | 2876 | DCHECK(m != nullptr) << info; |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 2877 | auto it = method_map.find(m); |
Alex Light | bf6498e | 2020-07-21 17:03:26 -0700 | [diff] [blame] | 2878 | if (UNLIKELY(could_change_resolution_of(m, info))) { |
| 2879 | // Dex-cache Resolution might change. Just clear the resolved value. |
| 2880 | VLOG(plugin) << "Clearing resolution " << info << " for (method) " << m->PrettyMethod(); |
| 2881 | return static_cast<art::ArtMethod*>(nullptr); |
| 2882 | } else if (it != method_map.end()) { |
Alex Light | 2462789 | 2019-11-06 10:28:21 -0800 | [diff] [blame] | 2883 | VLOG(plugin) << "Updating " << info << " object for (method) " |
| 2884 | << it->second->PrettyMethod(); |
| 2885 | return it->second; |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 2886 | } |
Alex Light | 2462789 | 2019-11-06 10:28:21 -0800 | [diff] [blame] | 2887 | return m; |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 2888 | }); |
| 2889 | |
| 2890 | // Force every frame of every thread to deoptimize (any frame might have eg offsets compiled in). |
| 2891 | driver_->runtime_->GetInstrumentation()->DeoptimizeAllThreadFrames(); |
| 2892 | |
Alex Light | 986914b | 2019-11-19 01:12:25 +0000 | [diff] [blame] | 2893 | std::unordered_map<art::ObjPtr<art::mirror::Object>, |
| 2894 | art::ObjPtr<art::mirror::Object>, |
| 2895 | art::HashObjPtr> map; |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 2896 | for (auto [new_class, old_class] : art::ZipLeft(new_classes->Iterate(), old_classes->Iterate())) { |
| 2897 | map.emplace(old_class, new_class); |
| 2898 | } |
| 2899 | for (auto [new_instance, old_instance] : |
| 2900 | art::ZipLeft(new_instances->Iterate(), old_instances->Iterate())) { |
| 2901 | map.emplace(old_instance, new_instance); |
| 2902 | // Bare-bones check that the mapping is correct. |
| 2903 | CHECK(new_instance->GetClass() == map[old_instance->GetClass()]->AsClass()) |
| 2904 | << new_instance->GetClass()->PrettyClass() << " vs " |
| 2905 | << map[old_instance->GetClass()]->AsClass()->PrettyClass(); |
Alex Light | 986914b | 2019-11-19 01:12:25 +0000 | [diff] [blame] | 2906 | } |
| 2907 | |
| 2908 | // Actually perform the general replacement. This doesn't affect ArtMethod/ArtFields. It does |
| 2909 | // affect the declaring_class field of all the obsolete objects, which is unfortunate and needs to |
| 2910 | // be undone. This replaces the mirror::Class in 'holder' as well. It's magic! |
| 2911 | HeapExtensions::ReplaceReferences(driver_->self_, map); |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 2912 | |
| 2913 | // Save the old class so that the JIT gc doesn't get confused by it being collected before the |
| 2914 | // jit code. This is also needed to keep the dex-caches of any obsolete methods live. |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 2915 | for (auto [new_class, old_class] : |
| 2916 | art::ZipLeft(new_classes->Iterate(), art::MakeIterationRange(old_classes_vec))) { |
| 2917 | new_class->GetExtData()->SetObsoleteClass(old_class); |
| 2918 | } |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 2919 | |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 2920 | art::jit::Jit* jit = driver_->runtime_->GetJit(); |
| 2921 | if (jit != nullptr) { |
| 2922 | // Clear jit. |
| 2923 | // TODO We might want to have some way to tell the JIT not to wait the kJitSamplesBatchSize |
| 2924 | // invokes to start compiling things again. |
| 2925 | jit->GetCodeCache()->InvalidateAllCompiledCode(); |
| 2926 | } |
| 2927 | |
| 2928 | // Clear thread caches |
| 2929 | { |
| 2930 | // TODO We might be able to avoid doing this but given the rather unstructured nature of the |
| 2931 | // interpreter cache it's probably not worth the effort. |
| 2932 | art::MutexLock mu(driver_->self_, *art::Locks::thread_list_lock_); |
| 2933 | driver_->runtime_->GetThreadList()->ForEach( |
Hans Boehm | 3d2f148 | 2022-01-17 01:32:55 +0000 | [diff] [blame] | 2934 | [](art::Thread* t) { t->GetInterpreterCache()->Clear(t); }); |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 2935 | } |
| 2936 | |
| 2937 | if (art::kIsDebugBuild) { |
| 2938 | // Just make sure we didn't screw up any of the now obsolete methods or fields. We need their |
| 2939 | // declaring-class to still be the obolete class |
Alex Light | bf6498e | 2020-07-21 17:03:26 -0700 | [diff] [blame] | 2940 | std::for_each( |
| 2941 | old_classes_vec.cbegin(), |
| 2942 | old_classes_vec.cend(), |
| 2943 | [](art::ObjPtr<art::mirror::Class> orig) REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 2944 | orig->VisitMethods( |
| 2945 | [&](art::ArtMethod* method) REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 2946 | if (method->IsCopied()) { |
| 2947 | // Copied methods have interfaces as their declaring class. |
| 2948 | return; |
| 2949 | } |
| 2950 | DCHECK_EQ(method->GetDeclaringClass(), orig) |
| 2951 | << method->GetDeclaringClass()->PrettyClass() << " vs " << orig->PrettyClass(); |
| 2952 | }, |
| 2953 | art::kRuntimePointerSize); |
| 2954 | orig->VisitFields([&](art::ArtField* field) REQUIRES_SHARED(art::Locks::mutator_lock_) { |
| 2955 | DCHECK_EQ(field->GetDeclaringClass(), orig) |
| 2956 | << field->GetDeclaringClass()->PrettyClass() << " vs " << orig->PrettyClass(); |
| 2957 | }); |
| 2958 | }); |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 2959 | } |
| 2960 | } |
| 2961 | |
| 2962 | // Redefines the class in place |
| 2963 | void Redefiner::ClassRedefinition::UpdateClassInPlace(const RedefinitionDataIter& holder) { |
| 2964 | art::ObjPtr<art::mirror::Class> mclass(holder.GetMirrorClass()); |
| 2965 | // TODO Rewrite so we don't do a stack walk for each and every class. |
| 2966 | FindAndAllocateObsoleteMethods(mclass); |
| 2967 | art::ObjPtr<art::mirror::DexCache> new_dex_cache(holder.GetNewDexCache()); |
| 2968 | art::ObjPtr<art::mirror::Object> original_dex_file(holder.GetOriginalDexFile()); |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 2969 | DCHECK_EQ(dex_file_->NumClassDefs(), 1u); |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 2970 | const art::dex::ClassDef& class_def = dex_file_->GetClassDef(0); |
Vladimir Marko | 5122e6b | 2017-08-17 16:10:09 +0100 | [diff] [blame] | 2971 | UpdateMethods(mclass, class_def); |
Alex Light | 007ada2 | 2017-01-10 13:33:56 -0800 | [diff] [blame] | 2972 | UpdateFields(mclass); |
Alex Light | 200b9d7 | 2016-12-15 11:34:13 -0800 | [diff] [blame] | 2973 | |
David Brazdil | 1a65863 | 2018-12-01 17:54:26 +0000 | [diff] [blame] | 2974 | art::ObjPtr<art::mirror::ClassExt> ext(mclass->GetExtData()); |
| 2975 | CHECK(!ext.IsNull()); |
| 2976 | ext->SetOriginalDexFile(original_dex_file); |
| 2977 | |
| 2978 | // If this is the first time the class is being redefined, store |
| 2979 | // the native DexFile pointer and initial ClassDef index in ClassExt. |
| 2980 | // This preserves the pointer for hiddenapi access checks which need |
| 2981 | // to read access flags from the initial DexFile. |
| 2982 | if (ext->GetPreRedefineDexFile() == nullptr) { |
| 2983 | ext->SetPreRedefineDexFile(&mclass->GetDexFile()); |
| 2984 | ext->SetPreRedefineClassDefIndex(mclass->GetDexClassDefIndex()); |
| 2985 | } |
| 2986 | |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 2987 | // Update the class fields. |
| 2988 | // Need to update class last since the ArtMethod gets its DexFile from the class (which is needed |
| 2989 | // to call GetReturnTypeDescriptor and GetParameterTypeList above). |
| 2990 | mclass->SetDexCache(new_dex_cache.Ptr()); |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 2991 | mclass->SetDexClassDefIndex(dex_file_->GetIndexForClassDef(class_def)); |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 2992 | mclass->SetDexTypeIndex(dex_file_->GetIndexForTypeId(*dex_file_->FindTypeId(class_sig_.c_str()))); |
Alex Light | 035105f | 2018-03-05 17:48:48 -0800 | [diff] [blame] | 2993 | |
| 2994 | // Notify the jit that all the methods in this class were redefined. Need to do this last since |
| 2995 | // the jit relies on the dex_file_ being correct (for native methods at least) to find the method |
| 2996 | // meta-data. |
| 2997 | art::jit::Jit* jit = driver_->runtime_->GetJit(); |
| 2998 | if (jit != nullptr) { |
| 2999 | art::PointerSize image_pointer_size = |
| 3000 | driver_->runtime_->GetClassLinker()->GetImagePointerSize(); |
| 3001 | auto code_cache = jit->GetCodeCache(); |
| 3002 | // Non-invokable methods don't have any JIT data associated with them so we don't need to tell |
| 3003 | // the jit about them. |
| 3004 | for (art::ArtMethod& method : mclass->GetDeclaredMethods(image_pointer_size)) { |
| 3005 | if (method.IsInvokable()) { |
| 3006 | code_cache->NotifyMethodRedefined(&method); |
| 3007 | } |
| 3008 | } |
| 3009 | } |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 3010 | } |
| 3011 | |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 3012 | // Performs final updates to class for redefinition. |
| 3013 | void Redefiner::ClassRedefinition::UpdateClass(const RedefinitionDataIter& holder) { |
Alex Light | fb53708 | 2019-12-10 14:38:34 -0800 | [diff] [blame] | 3014 | CHECK(holder.IsInitialized()); |
| 3015 | if (holder.IsInitialStructural()) { |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 3016 | UpdateClassStructurally(holder); |
Alex Light | fb53708 | 2019-12-10 14:38:34 -0800 | [diff] [blame] | 3017 | } else if (!holder.IsActuallyStructural()) { |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 3018 | UpdateClassInPlace(holder); |
| 3019 | } |
| 3020 | } |
| 3021 | |
Alex Light | 1e3926a | 2017-04-07 10:38:06 -0700 | [diff] [blame] | 3022 | // Restores the old obsolete methods maps if it turns out they weren't needed (ie there were no new |
| 3023 | // obsolete methods). |
| 3024 | void Redefiner::ClassRedefinition::RestoreObsoleteMethodMapsIfUnneeded( |
| 3025 | const RedefinitionDataIter* cur_data) { |
Alex Light | fb53708 | 2019-12-10 14:38:34 -0800 | [diff] [blame] | 3026 | if (cur_data->IsActuallyStructural()) { |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 3027 | // We didn't touch these in this case. |
| 3028 | return; |
| 3029 | } |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 3030 | art::ObjPtr<art::mirror::Class> klass = GetMirrorClass(); |
| 3031 | art::ObjPtr<art::mirror::ClassExt> ext = klass->GetExtData(); |
| 3032 | art::ObjPtr<art::mirror::PointerArray> methods = ext->GetObsoleteMethods(); |
| 3033 | art::ObjPtr<art::mirror::PointerArray> old_methods = cur_data->GetOldObsoleteMethods(); |
Alex Light | 70713df | 2017-04-18 13:03:31 -0700 | [diff] [blame] | 3034 | int32_t old_length = old_methods == nullptr ? 0 : old_methods->GetLength(); |
Alex Light | 1e3926a | 2017-04-07 10:38:06 -0700 | [diff] [blame] | 3035 | int32_t expected_length = |
| 3036 | old_length + klass->NumDirectMethods() + klass->NumDeclaredVirtualMethods(); |
| 3037 | // Check to make sure we are only undoing this one. |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 3038 | if (methods.IsNull()) { |
| 3039 | // No new obsolete methods! We can get rid of the maps. |
| 3040 | ext->SetObsoleteArrays(cur_data->GetOldObsoleteMethods(), cur_data->GetOldDexCaches()); |
| 3041 | } else if (expected_length == methods->GetLength()) { |
Alex Light | 70713df | 2017-04-18 13:03:31 -0700 | [diff] [blame] | 3042 | for (int32_t i = 0; i < expected_length; i++) { |
| 3043 | art::ArtMethod* expected = nullptr; |
| 3044 | if (i < old_length) { |
| 3045 | expected = old_methods->GetElementPtrSize<art::ArtMethod*>(i, art::kRuntimePointerSize); |
| 3046 | } |
| 3047 | if (methods->GetElementPtrSize<art::ArtMethod*>(i, art::kRuntimePointerSize) != expected) { |
Alex Light | 1e3926a | 2017-04-07 10:38:06 -0700 | [diff] [blame] | 3048 | // We actually have some new obsolete methods. Just abort since we cannot safely shrink the |
| 3049 | // obsolete methods array. |
| 3050 | return; |
| 3051 | } |
| 3052 | } |
| 3053 | // No new obsolete methods! We can get rid of the maps. |
| 3054 | ext->SetObsoleteArrays(cur_data->GetOldObsoleteMethods(), cur_data->GetOldDexCaches()); |
| 3055 | } |
| 3056 | } |
| 3057 | |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 3058 | // This function does all (java) allocations we need to do for the Class being redefined. |
| 3059 | // TODO Change this name maybe? |
Alex Light | 1e3926a | 2017-04-07 10:38:06 -0700 | [diff] [blame] | 3060 | bool Redefiner::ClassRedefinition::EnsureClassAllocationsFinished( |
| 3061 | /*out*/RedefinitionDataIter* cur_data) { |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 3062 | art::StackHandleScope<2> hs(driver_->self_); |
| 3063 | art::Handle<art::mirror::Class> klass(hs.NewHandle( |
| 3064 | driver_->self_->DecodeJObject(klass_)->AsClass())); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3065 | if (klass == nullptr) { |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 3066 | RecordFailure(ERR(INVALID_CLASS), "Unable to decode class argument!"); |
| 3067 | return false; |
| 3068 | } |
| 3069 | // Allocate the classExt |
Vladimir Marko | 3068d58 | 2019-05-28 16:39:29 +0100 | [diff] [blame] | 3070 | art::Handle<art::mirror::ClassExt> ext = |
| 3071 | hs.NewHandle(art::mirror::Class::EnsureExtDataPresent(klass, driver_->self_)); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 3072 | if (ext == nullptr) { |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 3073 | // No memory. Clear exception (it's not useful) and return error. |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 3074 | driver_->self_->AssertPendingOOMException(); |
| 3075 | driver_->self_->ClearException(); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 3076 | RecordFailure(ERR(OUT_OF_MEMORY), "Could not allocate ClassExt"); |
| 3077 | return false; |
| 3078 | } |
Alex Light | fb53708 | 2019-12-10 14:38:34 -0800 | [diff] [blame] | 3079 | if (!cur_data->IsActuallyStructural()) { |
| 3080 | CHECK(!IsStructuralRedefinition()); |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 3081 | // First save the old values of the 2 arrays that make up the obsolete methods maps. Then |
| 3082 | // allocate the 2 arrays that make up the obsolete methods map. Since the contents of the arrays |
| 3083 | // are only modified when all threads (other than the modifying one) are suspended we don't need |
| 3084 | // to worry about missing the unsyncronized writes to the array. We do synchronize when setting |
| 3085 | // it however, since that can happen at any time. |
| 3086 | cur_data->SetOldObsoleteMethods(ext->GetObsoleteMethods()); |
| 3087 | cur_data->SetOldDexCaches(ext->GetObsoleteDexCaches()); |
| 3088 | if (!art::mirror::ClassExt::ExtendObsoleteArrays( |
| 3089 | ext, driver_->self_, klass->GetDeclaredMethodsSlice(art::kRuntimePointerSize).size())) { |
| 3090 | // OOM. Clear exception and return error. |
| 3091 | driver_->self_->AssertPendingOOMException(); |
| 3092 | driver_->self_->ClearException(); |
| 3093 | RecordFailure(ERR(OUT_OF_MEMORY), "Unable to allocate/extend obsolete methods map"); |
| 3094 | return false; |
| 3095 | } |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 3096 | } |
| 3097 | return true; |
| 3098 | } |
| 3099 | |
| 3100 | } // namespace openjdkjvmti |