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 | |
Andreas Gampe | 06c42a5 | 2017-07-26 14:17:14 -0700 | [diff] [blame] | 32 | #ifndef ART_OPENJDKJVMTI_TI_REDEFINE_H_ |
| 33 | #define ART_OPENJDKJVMTI_TI_REDEFINE_H_ |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 34 | |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 35 | #include <functional> |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 36 | #include <string> |
| 37 | |
| 38 | #include <jni.h> |
| 39 | |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 40 | #include "art_field.h" |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 41 | #include "art_jvmti.h" |
Vladimir Marko | e1993c7 | 2017-06-14 17:01:38 +0100 | [diff] [blame] | 42 | #include "base/array_ref.h" |
David Sehr | 1979c64 | 2018-04-26 14:41:18 -0700 | [diff] [blame] | 43 | #include "base/globals.h" |
Alex Light | 986914b | 2019-11-19 01:12:25 +0000 | [diff] [blame] | 44 | #include "dex/class_accessor.h" |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 45 | #include "dex/dex_file.h" |
| 46 | #include "dex/dex_file_structs.h" |
Vladimir Marko | a3ad0cd | 2018-05-04 10:06:38 +0100 | [diff] [blame] | 47 | #include "jni/jni_env_ext-inl.h" |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 48 | #include "jvmti.h" |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 49 | #include "mirror/array.h" |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 50 | #include "mirror/class.h" |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 51 | #include "mirror/dex_cache.h" |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 52 | #include "obj_ptr.h" |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 53 | |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 54 | namespace art { |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 55 | class ClassAccessor; |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 56 | namespace dex { |
| 57 | struct ClassDef; |
| 58 | } // namespace dex |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 59 | } // namespace art |
| 60 | |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 61 | namespace openjdkjvmti { |
| 62 | |
Vladimir Marko | 5924a4a | 2018-05-29 17:40:41 +0100 | [diff] [blame] | 63 | class ArtClassDefinition; |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 64 | class RedefinitionDataHolder; |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 65 | class RedefinitionDataIter; |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 66 | |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 67 | enum class RedefinitionType { |
| 68 | kStructural, |
| 69 | kNormal, |
| 70 | }; |
| 71 | |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 72 | // Class that can redefine a single class's methods. |
| 73 | class Redefiner { |
| 74 | public: |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 75 | // Redefine the given classes with the given dex data. Note this function does not take ownership |
| 76 | // of the dex_data pointers. It is not used after this call however and may be freed if desired. |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 77 | // The caller is responsible for freeing it. The runtime makes its own copy of the data. This |
| 78 | // function does not call the transformation events. |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 79 | static jvmtiError RedefineClassesDirect(ArtJvmTiEnv* env, |
| 80 | art::Runtime* runtime, |
| 81 | art::Thread* self, |
| 82 | const std::vector<ArtClassDefinition>& definitions, |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 83 | RedefinitionType type, |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 84 | /*out*/std::string* error_msg); |
| 85 | |
| 86 | // Redefine the given classes with the given dex data. Note this function does not take ownership |
| 87 | // of the dex_data pointers. It is not used after this call however and may be freed if desired. |
Alex Light | 0b77257 | 2016-12-02 17:27:31 -0800 | [diff] [blame] | 88 | // The caller is responsible for freeing it. The runtime makes its own copy of the data. |
Alex Light | 3732beb | 2019-10-04 13:35:34 -0700 | [diff] [blame] | 89 | static jvmtiError RedefineClasses(jvmtiEnv* env, |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 90 | jint class_count, |
Alex Light | 3732beb | 2019-10-04 13:35:34 -0700 | [diff] [blame] | 91 | const jvmtiClassDefinition* definitions); |
Alex Light | d55b844 | 2019-10-15 15:46:07 -0700 | [diff] [blame] | 92 | static jvmtiError StructurallyRedefineClasses(jvmtiEnv* env, |
| 93 | jint class_count, |
| 94 | const jvmtiClassDefinition* definitions); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 95 | |
Alex Light | e4a8863 | 2017-01-10 07:41:24 -0800 | [diff] [blame] | 96 | static jvmtiError IsModifiableClass(jvmtiEnv* env, jclass klass, jboolean* is_redefinable); |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 97 | static jvmtiError IsStructurallyModifiableClass(jvmtiEnv* env, |
| 98 | jclass klass, |
| 99 | jboolean* is_redefinable); |
Alex Light | e4a8863 | 2017-01-10 07:41:24 -0800 | [diff] [blame] | 100 | |
Vladimir Marko | c34bebf | 2018-08-16 16:12:49 +0100 | [diff] [blame] | 101 | static art::MemMap MoveDataToMemMap(const std::string& original_location, |
| 102 | art::ArrayRef<const unsigned char> data, |
| 103 | std::string* error_msg); |
Alex Light | 440b5d9 | 2017-01-24 15:32:25 -0800 | [diff] [blame] | 104 | |
Alex Light | 9e7859c | 2018-04-05 13:49:43 -0700 | [diff] [blame] | 105 | // Helper for checking if redefinition/retransformation is allowed. |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 106 | template<RedefinitionType kType = RedefinitionType::kNormal> |
Alex Light | 9e7859c | 2018-04-05 13:49:43 -0700 | [diff] [blame] | 107 | static jvmtiError GetClassRedefinitionError(jclass klass, /*out*/std::string* error_msg) |
| 108 | REQUIRES(!art::Locks::mutator_lock_); |
| 109 | |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 110 | static jvmtiError StructurallyRedefineClassDirect(jvmtiEnv* env, |
| 111 | jclass klass, |
| 112 | const unsigned char* data, |
| 113 | jint data_size); |
| 114 | |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 115 | private: |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 116 | class ClassRedefinition { |
| 117 | public: |
| 118 | ClassRedefinition(Redefiner* driver, |
| 119 | jclass klass, |
| 120 | const art::DexFile* redefined_dex_file, |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 121 | const char* class_sig, |
Vladimir Marko | e1993c7 | 2017-06-14 17:01:38 +0100 | [diff] [blame] | 122 | art::ArrayRef<const unsigned char> orig_dex_file) |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 123 | REQUIRES_SHARED(art::Locks::mutator_lock_); |
| 124 | |
| 125 | // NO_THREAD_SAFETY_ANALYSIS so we can unlock the class in the destructor. |
| 126 | ~ClassRedefinition() NO_THREAD_SAFETY_ANALYSIS; |
| 127 | |
Alex Light | fb53708 | 2019-12-10 14:38:34 -0800 | [diff] [blame] | 128 | // Move assignment so we can sort these in a vector. |
| 129 | ClassRedefinition& operator=(ClassRedefinition&& other) { |
| 130 | driver_ = other.driver_; |
| 131 | klass_ = other.klass_; |
| 132 | dex_file_ = std::move(other.dex_file_); |
| 133 | class_sig_ = std::move(other.class_sig_); |
| 134 | original_dex_file_ = other.original_dex_file_; |
ShuJieWang | b087bb2 | 2021-11-24 11:46:16 +0800 | [diff] [blame] | 135 | lock_acquired_ = other.lock_acquired_; |
Alex Light | fb53708 | 2019-12-10 14:38:34 -0800 | [diff] [blame] | 136 | other.driver_ = nullptr; |
| 137 | return *this; |
| 138 | } |
| 139 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 140 | // Move constructor so we can put these into a vector. |
| 141 | ClassRedefinition(ClassRedefinition&& other) |
| 142 | : driver_(other.driver_), |
| 143 | klass_(other.klass_), |
| 144 | dex_file_(std::move(other.dex_file_)), |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 145 | class_sig_(std::move(other.class_sig_)), |
ShuJieWang | b087bb2 | 2021-11-24 11:46:16 +0800 | [diff] [blame] | 146 | original_dex_file_(other.original_dex_file_), |
| 147 | lock_acquired_(other.lock_acquired_) { |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 148 | other.driver_ = nullptr; |
| 149 | } |
| 150 | |
Alex Light | fb53708 | 2019-12-10 14:38:34 -0800 | [diff] [blame] | 151 | // No copy! |
| 152 | ClassRedefinition(ClassRedefinition&) = delete; |
| 153 | ClassRedefinition& operator=(ClassRedefinition&) = delete; |
| 154 | |
Vladimir Marko | 4617d58 | 2019-03-28 13:48:31 +0000 | [diff] [blame] | 155 | art::ObjPtr<art::mirror::Class> GetMirrorClass() REQUIRES_SHARED(art::Locks::mutator_lock_); |
Vladimir Marko | c524e9e | 2019-03-26 10:54:50 +0000 | [diff] [blame] | 156 | art::ObjPtr<art::mirror::ClassLoader> GetClassLoader() |
| 157 | REQUIRES_SHARED(art::Locks::mutator_lock_); |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 158 | |
Alex Light | 7916f20 | 2017-01-27 09:00:15 -0800 | [diff] [blame] | 159 | const art::DexFile& GetDexFile() { |
| 160 | return *dex_file_; |
| 161 | } |
| 162 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 163 | art::mirror::DexCache* CreateNewDexCache(art::Handle<art::mirror::ClassLoader> loader) |
| 164 | REQUIRES_SHARED(art::Locks::mutator_lock_); |
| 165 | |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 166 | // This may return nullptr with a OOME pending if allocation fails. |
Alex Light | 2f814aa | 2017-03-24 15:21:34 +0000 | [diff] [blame] | 167 | art::mirror::Object* AllocateOrGetOriginalDexFile() |
Alex Light | a7e38d8 | 2017-01-19 14:57:28 -0800 | [diff] [blame] | 168 | REQUIRES_SHARED(art::Locks::mutator_lock_); |
| 169 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 170 | void RecordFailure(jvmtiError e, const std::string& err) { |
| 171 | driver_->RecordFailure(e, class_sig_, err); |
| 172 | } |
| 173 | |
Alex Light | fb53708 | 2019-12-10 14:38:34 -0800 | [diff] [blame] | 174 | bool FinishRemainingCommonAllocations(/*out*/RedefinitionDataIter* cur_data) |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 175 | REQUIRES_SHARED(art::Locks::mutator_lock_); |
| 176 | |
Alex Light | fb53708 | 2019-12-10 14:38:34 -0800 | [diff] [blame] | 177 | bool FinishNewClassAllocations(RedefinitionDataHolder& holder, |
| 178 | /*out*/RedefinitionDataIter* cur_data) |
| 179 | REQUIRES_SHARED(art::Locks::mutator_lock_); |
Alex Light | 986914b | 2019-11-19 01:12:25 +0000 | [diff] [blame] | 180 | bool CollectAndCreateNewInstances(/*out*/RedefinitionDataIter* cur_data) |
| 181 | REQUIRES_SHARED(art::Locks::mutator_lock_); |
| 182 | |
Alex Light | 1babae0 | 2017-02-01 15:35:34 -0800 | [diff] [blame] | 183 | bool AllocateAndRememberNewDexFileCookie( |
Alex Light | 1babae0 | 2017-02-01 15:35:34 -0800 | [diff] [blame] | 184 | art::Handle<art::mirror::ClassLoader> source_class_loader, |
| 185 | art::Handle<art::mirror::Object> dex_file_obj, |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 186 | /*out*/RedefinitionDataIter* cur_data) |
Alex Light | 1babae0 | 2017-02-01 15:35:34 -0800 | [diff] [blame] | 187 | REQUIRES_SHARED(art::Locks::mutator_lock_); |
| 188 | |
Vladimir Marko | d93e374 | 2018-07-18 10:58:13 +0100 | [diff] [blame] | 189 | void FindAndAllocateObsoleteMethods(art::ObjPtr<art::mirror::Class> art_klass) |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 190 | REQUIRES(art::Locks::mutator_lock_); |
| 191 | |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 192 | art::ObjPtr<art::mirror::Class> AllocateNewClassObject( |
| 193 | art::Handle<art::mirror::Class> old_class, |
| 194 | art::Handle<art::mirror::Class> super_class, |
| 195 | art::Handle<art::mirror::DexCache> cache, |
| 196 | uint16_t dex_class_def_index) REQUIRES_SHARED(art::Locks::mutator_lock_); |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 197 | art::ObjPtr<art::mirror::Class> AllocateNewClassObject(art::Handle<art::mirror::DexCache> cache) |
| 198 | REQUIRES_SHARED(art::Locks::mutator_lock_); |
| 199 | |
Alex Light | 270db1c | 2019-12-03 12:20:01 +0000 | [diff] [blame] | 200 | uint32_t GetNewClassSize(art::ClassAccessor& accessor) |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 201 | REQUIRES_SHARED(art::Locks::mutator_lock_); |
| 202 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 203 | // Checks that the dex file contains only the single expected class and that the top-level class |
| 204 | // data has not been modified in an incompatible manner. |
| 205 | bool CheckClass() REQUIRES_SHARED(art::Locks::mutator_lock_); |
| 206 | |
Alex Light | 8c889d2 | 2017-02-06 13:58:27 -0800 | [diff] [blame] | 207 | // Checks that the contained class can be successfully verified. |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 208 | bool CheckVerification(const RedefinitionDataIter& holder) |
Alex Light | 8c889d2 | 2017-02-06 13:58:27 -0800 | [diff] [blame] | 209 | REQUIRES_SHARED(art::Locks::mutator_lock_); |
| 210 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 211 | // Preallocates all needed allocations in klass so that we can pause execution safely. |
Alex Light | 1e3926a | 2017-04-07 10:38:06 -0700 | [diff] [blame] | 212 | bool EnsureClassAllocationsFinished(/*out*/RedefinitionDataIter* data) |
| 213 | REQUIRES_SHARED(art::Locks::mutator_lock_); |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 214 | |
| 215 | // This will check that no constraints are violated (more than 1 class in dex file, any changes |
| 216 | // in number/declaration of methods & fields, changes in access flags, etc.) |
| 217 | bool CheckRedefinitionIsValid() REQUIRES_SHARED(art::Locks::mutator_lock_); |
| 218 | |
| 219 | // Checks that the class can even be redefined. |
| 220 | bool CheckRedefinable() REQUIRES_SHARED(art::Locks::mutator_lock_); |
| 221 | |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 222 | // Checks that the dex file does not add/remove methods, or change their modifiers or types in |
| 223 | // illegal ways. |
| 224 | bool CheckMethods() REQUIRES_SHARED(art::Locks::mutator_lock_); |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 225 | |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 226 | // Checks that the dex file does not modify fields types or modifiers in illegal ways. |
| 227 | bool CheckFields() REQUIRES_SHARED(art::Locks::mutator_lock_); |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 228 | |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 229 | // Temporary check that a class undergoing structural redefinition has no instances. This |
| 230 | // requirement will be removed in time. |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 231 | void UpdateJavaDexFile(art::ObjPtr<art::mirror::Object> java_dex_file, |
| 232 | art::ObjPtr<art::mirror::LongArray> new_cookie) |
| 233 | REQUIRES(art::Locks::mutator_lock_); |
| 234 | |
| 235 | void UpdateFields(art::ObjPtr<art::mirror::Class> mclass) |
| 236 | REQUIRES(art::Locks::mutator_lock_); |
| 237 | |
| 238 | void UpdateMethods(art::ObjPtr<art::mirror::Class> mclass, |
Andreas Gampe | 3f1dcd3 | 2018-12-28 09:39:56 -0800 | [diff] [blame] | 239 | const art::dex::ClassDef& class_def) |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 240 | REQUIRES(art::Locks::mutator_lock_); |
| 241 | |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 242 | void UpdateClass(const RedefinitionDataIter& cur_data) |
| 243 | REQUIRES(art::Locks::mutator_lock_); |
| 244 | |
| 245 | void CollectNewFieldAndMethodMappings(const RedefinitionDataIter& data, |
| 246 | std::map<art::ArtMethod*, art::ArtMethod*>* method_map, |
| 247 | std::map<art::ArtField*, art::ArtField*>* field_map) |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 248 | REQUIRES(art::Locks::mutator_lock_); |
| 249 | |
Alex Light | 1e3926a | 2017-04-07 10:38:06 -0700 | [diff] [blame] | 250 | void RestoreObsoleteMethodMapsIfUnneeded(const RedefinitionDataIter* cur_data) |
| 251 | REQUIRES(art::Locks::mutator_lock_); |
| 252 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 253 | void ReleaseDexFile() REQUIRES_SHARED(art::Locks::mutator_lock_); |
| 254 | |
Alex Light | a26e349 | 2017-06-27 17:55:37 -0700 | [diff] [blame] | 255 | // This should be done with all threads suspended. |
Alex Light | e34fe44 | 2018-02-21 17:35:55 -0800 | [diff] [blame] | 256 | void UnregisterJvmtiBreakpoints() REQUIRES_SHARED(art::Locks::mutator_lock_); |
Alex Light | 5643caf | 2017-02-08 11:39:07 -0800 | [diff] [blame] | 257 | |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 258 | void RecordNewMethodAdded(); |
| 259 | void RecordNewFieldAdded(); |
Alex Light | 986914b | 2019-11-19 01:12:25 +0000 | [diff] [blame] | 260 | void RecordHasVirtualMembers() { |
| 261 | has_virtuals_ = true; |
| 262 | } |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 263 | |
Alex Light | 986914b | 2019-11-19 01:12:25 +0000 | [diff] [blame] | 264 | bool HasVirtualMembers() const { |
| 265 | return has_virtuals_; |
| 266 | } |
| 267 | |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 268 | bool IsStructuralRedefinition() const { |
| 269 | DCHECK(!(added_fields_ || added_methods_) || driver_->IsStructuralRedefinition()) |
| 270 | << "added_fields_: " << added_fields_ << " added_methods_: " << added_methods_ |
| 271 | << " driver_->IsStructuralRedefinition(): " << driver_->IsStructuralRedefinition(); |
| 272 | return driver_->IsStructuralRedefinition() && (added_fields_ || added_methods_); |
| 273 | } |
| 274 | |
Alex Light | 986914b | 2019-11-19 01:12:25 +0000 | [diff] [blame] | 275 | private: |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 276 | void UpdateClassStructurally(const RedefinitionDataIter& cur_data) |
| 277 | REQUIRES(art::Locks::mutator_lock_); |
| 278 | |
| 279 | void UpdateClassInPlace(const RedefinitionDataIter& cur_data) |
| 280 | REQUIRES(art::Locks::mutator_lock_); |
| 281 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 282 | Redefiner* driver_; |
| 283 | jclass klass_; |
| 284 | std::unique_ptr<const art::DexFile> dex_file_; |
| 285 | std::string class_sig_; |
Vladimir Marko | e1993c7 | 2017-06-14 17:01:38 +0100 | [diff] [blame] | 286 | art::ArrayRef<const unsigned char> original_dex_file_; |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 287 | |
| 288 | bool added_fields_ = false; |
| 289 | bool added_methods_ = false; |
Alex Light | 986914b | 2019-11-19 01:12:25 +0000 | [diff] [blame] | 290 | bool has_virtuals_ = false; |
ShuJieWang | b087bb2 | 2021-11-24 11:46:16 +0800 | [diff] [blame] | 291 | bool lock_acquired_ = false; |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 292 | }; |
| 293 | |
Alex Light | a26e349 | 2017-06-27 17:55:37 -0700 | [diff] [blame] | 294 | ArtJvmTiEnv* env_; |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 295 | jvmtiError result_; |
| 296 | art::Runtime* runtime_; |
| 297 | art::Thread* self_; |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 298 | RedefinitionType type_; |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 299 | std::vector<ClassRedefinition> redefinitions_; |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 300 | // Kept as a jclass since we have weird run-state changes that make keeping it around as a |
| 301 | // mirror::Class difficult and confusing. |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 302 | std::string* error_msg_; |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 303 | |
Alex Light | a26e349 | 2017-06-27 17:55:37 -0700 | [diff] [blame] | 304 | Redefiner(ArtJvmTiEnv* env, |
| 305 | art::Runtime* runtime, |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 306 | art::Thread* self, |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 307 | RedefinitionType type, |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 308 | std::string* error_msg) |
Alex Light | a26e349 | 2017-06-27 17:55:37 -0700 | [diff] [blame] | 309 | : env_(env), |
| 310 | result_(ERR(INTERNAL)), |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 311 | runtime_(runtime), |
| 312 | self_(self), |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 313 | type_(type), |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 314 | redefinitions_(), |
| 315 | error_msg_(error_msg) { } |
| 316 | |
Alex Light | 6ac5750 | 2017-01-19 15:05:06 -0800 | [diff] [blame] | 317 | jvmtiError AddRedefinition(ArtJvmTiEnv* env, const ArtClassDefinition& def) |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 318 | REQUIRES_SHARED(art::Locks::mutator_lock_); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 319 | |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 320 | template<RedefinitionType kType = RedefinitionType::kNormal> |
Alex Light | d55b844 | 2019-10-15 15:46:07 -0700 | [diff] [blame] | 321 | static jvmtiError RedefineClassesGeneric(jvmtiEnv* env, |
| 322 | jint class_count, |
| 323 | const jvmtiClassDefinition* definitions); |
| 324 | |
| 325 | template<RedefinitionType kType = RedefinitionType::kNormal> |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 326 | static jvmtiError IsModifiableClassGeneric(jvmtiEnv* env, jclass klass, jboolean* is_redefinable); |
| 327 | |
| 328 | template<RedefinitionType kType = RedefinitionType::kNormal> |
Alex Light | e4a8863 | 2017-01-10 07:41:24 -0800 | [diff] [blame] | 329 | static jvmtiError GetClassRedefinitionError(art::Handle<art::mirror::Class> klass, |
| 330 | /*out*/std::string* error_msg) |
| 331 | REQUIRES_SHARED(art::Locks::mutator_lock_); |
| 332 | |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 333 | jvmtiError Run() REQUIRES_SHARED(art::Locks::mutator_lock_); |
| 334 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 335 | bool CheckAllRedefinitionAreValid() REQUIRES_SHARED(art::Locks::mutator_lock_); |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 336 | bool CheckAllClassesAreVerified(RedefinitionDataHolder& holder) |
Alex Light | 8c889d2 | 2017-02-06 13:58:27 -0800 | [diff] [blame] | 337 | REQUIRES_SHARED(art::Locks::mutator_lock_); |
Alex Light | fb53708 | 2019-12-10 14:38:34 -0800 | [diff] [blame] | 338 | void MarkStructuralChanges(RedefinitionDataHolder& holder) |
| 339 | REQUIRES_SHARED(art::Locks::mutator_lock_); |
Alex Light | 1e3926a | 2017-04-07 10:38:06 -0700 | [diff] [blame] | 340 | bool EnsureAllClassAllocationsFinished(RedefinitionDataHolder& holder) |
| 341 | REQUIRES_SHARED(art::Locks::mutator_lock_); |
Alex Light | fb53708 | 2019-12-10 14:38:34 -0800 | [diff] [blame] | 342 | bool FinishAllRemainingCommonAllocations(RedefinitionDataHolder& holder) |
| 343 | REQUIRES_SHARED(art::Locks::mutator_lock_); |
| 344 | bool FinishAllNewClassAllocations(RedefinitionDataHolder& holder) |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 345 | REQUIRES_SHARED(art::Locks::mutator_lock_); |
Alex Light | 986914b | 2019-11-19 01:12:25 +0000 | [diff] [blame] | 346 | bool CollectAndCreateNewInstances(RedefinitionDataHolder& holder) |
| 347 | REQUIRES_SHARED(art::Locks::mutator_lock_); |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 348 | void ReleaseAllDexFiles() REQUIRES_SHARED(art::Locks::mutator_lock_); |
Alex Light | c2d0c96 | 2019-10-23 14:14:25 -0700 | [diff] [blame] | 349 | void ReverifyClasses(RedefinitionDataHolder& holder) REQUIRES_SHARED(art::Locks::mutator_lock_); |
Alex Light | 5643caf | 2017-02-08 11:39:07 -0800 | [diff] [blame] | 350 | void UnregisterAllBreakpoints() REQUIRES_SHARED(art::Locks::mutator_lock_); |
Alex Light | 1e3926a | 2017-04-07 10:38:06 -0700 | [diff] [blame] | 351 | // Restores the old obsolete methods maps if it turns out they weren't needed (ie there were no |
| 352 | // new obsolete methods). |
| 353 | void RestoreObsoleteMethodMapsIfUnneeded(RedefinitionDataHolder& holder) |
| 354 | REQUIRES(art::Locks::mutator_lock_); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 355 | |
Nicolas Geoffray | 4ac0e15 | 2019-09-18 06:14:50 +0000 | [diff] [blame] | 356 | bool IsStructuralRedefinition() const { |
| 357 | return type_ == RedefinitionType::kStructural; |
| 358 | } |
| 359 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 360 | void RecordFailure(jvmtiError result, const std::string& class_sig, const std::string& error_msg); |
| 361 | void RecordFailure(jvmtiError result, const std::string& error_msg) { |
| 362 | RecordFailure(result, "NO CLASS", error_msg); |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 363 | } |
| 364 | |
Alex Light | 0e69273 | 2017-01-10 15:00:05 -0800 | [diff] [blame] | 365 | friend struct CallbackCtx; |
Alex Light | c5f5a6e | 2017-03-01 16:57:08 -0800 | [diff] [blame] | 366 | friend class RedefinitionDataHolder; |
| 367 | friend class RedefinitionDataIter; |
Alex Light | a01de59 | 2016-11-15 10:43:06 -0800 | [diff] [blame] | 368 | }; |
| 369 | |
| 370 | } // namespace openjdkjvmti |
| 371 | |
Andreas Gampe | 06c42a5 | 2017-07-26 14:17:14 -0700 | [diff] [blame] | 372 | #endif // ART_OPENJDKJVMTI_TI_REDEFINE_H_ |