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