blob: 732d3e450d3b87761671038da119aeb6c6a5d9cc [file] [log] [blame]
Alex Lighta01de592016-11-15 10:43:06 -08001/* 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 Gampe06c42a52017-07-26 14:17:14 -070032#ifndef ART_OPENJDKJVMTI_TI_REDEFINE_H_
33#define ART_OPENJDKJVMTI_TI_REDEFINE_H_
Alex Lighta01de592016-11-15 10:43:06 -080034
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +000035#include <functional>
Alex Lighta01de592016-11-15 10:43:06 -080036#include <string>
37
38#include <jni.h>
39
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +000040#include "art_field.h"
Alex Lighta01de592016-11-15 10:43:06 -080041#include "art_jvmti.h"
Vladimir Markoe1993c72017-06-14 17:01:38 +010042#include "base/array_ref.h"
David Sehr1979c642018-04-26 14:41:18 -070043#include "base/globals.h"
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +000044#include "dex/dex_file.h"
45#include "dex/dex_file_structs.h"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010046#include "jni/jni_env_ext-inl.h"
Alex Lighta01de592016-11-15 10:43:06 -080047#include "jvmti.h"
Alex Lighta01de592016-11-15 10:43:06 -080048#include "mirror/array.h"
Alex Lighta01de592016-11-15 10:43:06 -080049#include "mirror/class.h"
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +000050#include "mirror/dex_cache.h"
Alex Lighta01de592016-11-15 10:43:06 -080051#include "obj_ptr.h"
Alex Lighta01de592016-11-15 10:43:06 -080052
Andreas Gampe3f1dcd32018-12-28 09:39:56 -080053namespace art {
54namespace dex {
55struct ClassDef;
56} // namespace dex
Andreas Gampe3f1dcd32018-12-28 09:39:56 -080057} // namespace art
58
Alex Lighta01de592016-11-15 10:43:06 -080059namespace openjdkjvmti {
60
Vladimir Marko5924a4a2018-05-29 17:40:41 +010061class ArtClassDefinition;
Alex Light0e692732017-01-10 15:00:05 -080062class RedefinitionDataHolder;
Alex Lightc5f5a6e2017-03-01 16:57:08 -080063class RedefinitionDataIter;
Alex Light0e692732017-01-10 15:00:05 -080064
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +000065enum class RedefinitionType {
66 kStructural,
67 kNormal,
68};
69
Alex Lighta01de592016-11-15 10:43:06 -080070// Class that can redefine a single class's methods.
71class Redefiner {
72 public:
Alex Light0e692732017-01-10 15:00:05 -080073 // Redefine the given classes with the given dex data. Note this function does not take ownership
74 // of the dex_data pointers. It is not used after this call however and may be freed if desired.
Alex Light6ac57502017-01-19 15:05:06 -080075 // The caller is responsible for freeing it. The runtime makes its own copy of the data. This
76 // function does not call the transformation events.
Alex Light6ac57502017-01-19 15:05:06 -080077 static jvmtiError RedefineClassesDirect(ArtJvmTiEnv* env,
78 art::Runtime* runtime,
79 art::Thread* self,
80 const std::vector<ArtClassDefinition>& definitions,
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +000081 RedefinitionType type,
Alex Light6ac57502017-01-19 15:05:06 -080082 /*out*/std::string* error_msg);
83
84 // Redefine the given classes with the given dex data. Note this function does not take ownership
85 // of the dex_data pointers. It is not used after this call however and may be freed if desired.
Alex Light0b772572016-12-02 17:27:31 -080086 // The caller is responsible for freeing it. The runtime makes its own copy of the data.
Alex Light0e692732017-01-10 15:00:05 -080087 static jvmtiError RedefineClasses(ArtJvmTiEnv* env,
Andreas Gampede19eb92017-02-24 16:21:18 -080088 EventHandler* event_handler,
Alex Light0e692732017-01-10 15:00:05 -080089 art::Runtime* runtime,
90 art::Thread* self,
91 jint class_count,
92 const jvmtiClassDefinition* definitions,
Alex Light6ac57502017-01-19 15:05:06 -080093 /*out*/std::string* error_msg);
Alex Lighta01de592016-11-15 10:43:06 -080094
Alex Lighte4a88632017-01-10 07:41:24 -080095 static jvmtiError IsModifiableClass(jvmtiEnv* env, jclass klass, jboolean* is_redefinable);
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +000096 static jvmtiError IsStructurallyModifiableClass(jvmtiEnv* env,
97 jclass klass,
98 jboolean* is_redefinable);
Alex Lighte4a88632017-01-10 07:41:24 -080099
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100100 static art::MemMap MoveDataToMemMap(const std::string& original_location,
101 art::ArrayRef<const unsigned char> data,
102 std::string* error_msg);
Alex Light440b5d92017-01-24 15:32:25 -0800103
Alex Light9e7859c2018-04-05 13:49:43 -0700104 // Helper for checking if redefinition/retransformation is allowed.
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +0000105 template<RedefinitionType kType = RedefinitionType::kNormal>
Alex Light9e7859c2018-04-05 13:49:43 -0700106 static jvmtiError GetClassRedefinitionError(jclass klass, /*out*/std::string* error_msg)
107 REQUIRES(!art::Locks::mutator_lock_);
108
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +0000109 static jvmtiError StructurallyRedefineClassDirect(jvmtiEnv* env,
110 jclass klass,
111 const unsigned char* data,
112 jint data_size);
113
Alex Lighta01de592016-11-15 10:43:06 -0800114 private:
Alex Light0e692732017-01-10 15:00:05 -0800115 class ClassRedefinition {
116 public:
117 ClassRedefinition(Redefiner* driver,
118 jclass klass,
119 const art::DexFile* redefined_dex_file,
Alex Lighta7e38d82017-01-19 14:57:28 -0800120 const char* class_sig,
Vladimir Markoe1993c72017-06-14 17:01:38 +0100121 art::ArrayRef<const unsigned char> orig_dex_file)
Alex Light0e692732017-01-10 15:00:05 -0800122 REQUIRES_SHARED(art::Locks::mutator_lock_);
123
124 // NO_THREAD_SAFETY_ANALYSIS so we can unlock the class in the destructor.
125 ~ClassRedefinition() NO_THREAD_SAFETY_ANALYSIS;
126
127 // Move constructor so we can put these into a vector.
128 ClassRedefinition(ClassRedefinition&& other)
129 : driver_(other.driver_),
130 klass_(other.klass_),
131 dex_file_(std::move(other.dex_file_)),
Alex Lighta7e38d82017-01-19 14:57:28 -0800132 class_sig_(std::move(other.class_sig_)),
133 original_dex_file_(other.original_dex_file_) {
Alex Light0e692732017-01-10 15:00:05 -0800134 other.driver_ = nullptr;
135 }
136
Vladimir Marko4617d582019-03-28 13:48:31 +0000137 art::ObjPtr<art::mirror::Class> GetMirrorClass() REQUIRES_SHARED(art::Locks::mutator_lock_);
Vladimir Markoc524e9e2019-03-26 10:54:50 +0000138 art::ObjPtr<art::mirror::ClassLoader> GetClassLoader()
139 REQUIRES_SHARED(art::Locks::mutator_lock_);
Alex Light0e692732017-01-10 15:00:05 -0800140
Alex Light7916f202017-01-27 09:00:15 -0800141 const art::DexFile& GetDexFile() {
142 return *dex_file_;
143 }
144
Alex Light0e692732017-01-10 15:00:05 -0800145 art::mirror::DexCache* CreateNewDexCache(art::Handle<art::mirror::ClassLoader> loader)
146 REQUIRES_SHARED(art::Locks::mutator_lock_);
147
Alex Lighta7e38d82017-01-19 14:57:28 -0800148 // This may return nullptr with a OOME pending if allocation fails.
Alex Light2f814aa2017-03-24 15:21:34 +0000149 art::mirror::Object* AllocateOrGetOriginalDexFile()
Alex Lighta7e38d82017-01-19 14:57:28 -0800150 REQUIRES_SHARED(art::Locks::mutator_lock_);
151
Alex Light0e692732017-01-10 15:00:05 -0800152 void RecordFailure(jvmtiError e, const std::string& err) {
153 driver_->RecordFailure(e, class_sig_, err);
154 }
155
Alex Lightc5f5a6e2017-03-01 16:57:08 -0800156 bool FinishRemainingAllocations(/*out*/RedefinitionDataIter* cur_data)
Alex Light0e692732017-01-10 15:00:05 -0800157 REQUIRES_SHARED(art::Locks::mutator_lock_);
158
Alex Light1babae02017-02-01 15:35:34 -0800159 bool AllocateAndRememberNewDexFileCookie(
Alex Light1babae02017-02-01 15:35:34 -0800160 art::Handle<art::mirror::ClassLoader> source_class_loader,
161 art::Handle<art::mirror::Object> dex_file_obj,
Alex Lightc5f5a6e2017-03-01 16:57:08 -0800162 /*out*/RedefinitionDataIter* cur_data)
Alex Light1babae02017-02-01 15:35:34 -0800163 REQUIRES_SHARED(art::Locks::mutator_lock_);
164
Vladimir Markod93e3742018-07-18 10:58:13 +0100165 void FindAndAllocateObsoleteMethods(art::ObjPtr<art::mirror::Class> art_klass)
Alex Light0e692732017-01-10 15:00:05 -0800166 REQUIRES(art::Locks::mutator_lock_);
167
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +0000168 art::ObjPtr<art::mirror::Class> AllocateNewClassObject(art::Handle<art::mirror::DexCache> cache)
169 REQUIRES_SHARED(art::Locks::mutator_lock_);
170
171 uint32_t GetNewClassSize(bool with_embedded_tables, art::Handle<art::mirror::Class> old_class)
172 REQUIRES_SHARED(art::Locks::mutator_lock_);
173
Alex Light0e692732017-01-10 15:00:05 -0800174 // Checks that the dex file contains only the single expected class and that the top-level class
175 // data has not been modified in an incompatible manner.
176 bool CheckClass() REQUIRES_SHARED(art::Locks::mutator_lock_);
177
Alex Light8c889d22017-02-06 13:58:27 -0800178 // Checks that the contained class can be successfully verified.
Alex Lightc5f5a6e2017-03-01 16:57:08 -0800179 bool CheckVerification(const RedefinitionDataIter& holder)
Alex Light8c889d22017-02-06 13:58:27 -0800180 REQUIRES_SHARED(art::Locks::mutator_lock_);
181
Alex Light0e692732017-01-10 15:00:05 -0800182 // Preallocates all needed allocations in klass so that we can pause execution safely.
Alex Light1e3926a2017-04-07 10:38:06 -0700183 bool EnsureClassAllocationsFinished(/*out*/RedefinitionDataIter* data)
184 REQUIRES_SHARED(art::Locks::mutator_lock_);
Alex Light0e692732017-01-10 15:00:05 -0800185
186 // This will check that no constraints are violated (more than 1 class in dex file, any changes
187 // in number/declaration of methods & fields, changes in access flags, etc.)
188 bool CheckRedefinitionIsValid() REQUIRES_SHARED(art::Locks::mutator_lock_);
189
190 // Checks that the class can even be redefined.
191 bool CheckRedefinable() REQUIRES_SHARED(art::Locks::mutator_lock_);
192
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +0000193 // Checks that the dex file does not add/remove methods, or change their modifiers or types in
194 // illegal ways.
195 bool CheckMethods() REQUIRES_SHARED(art::Locks::mutator_lock_);
Alex Light0e692732017-01-10 15:00:05 -0800196
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +0000197 // Checks that the dex file does not modify fields types or modifiers in illegal ways.
198 bool CheckFields() REQUIRES_SHARED(art::Locks::mutator_lock_);
Alex Light0e692732017-01-10 15:00:05 -0800199
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +0000200 // Temporary check that a class undergoing structural redefinition has no instances. This
201 // requirement will be removed in time.
Alex Light0e692732017-01-10 15:00:05 -0800202 void UpdateJavaDexFile(art::ObjPtr<art::mirror::Object> java_dex_file,
203 art::ObjPtr<art::mirror::LongArray> new_cookie)
204 REQUIRES(art::Locks::mutator_lock_);
205
206 void UpdateFields(art::ObjPtr<art::mirror::Class> mclass)
207 REQUIRES(art::Locks::mutator_lock_);
208
209 void UpdateMethods(art::ObjPtr<art::mirror::Class> mclass,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800210 const art::dex::ClassDef& class_def)
Alex Light0e692732017-01-10 15:00:05 -0800211 REQUIRES(art::Locks::mutator_lock_);
212
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +0000213 void UpdateClass(const RedefinitionDataIter& cur_data)
214 REQUIRES(art::Locks::mutator_lock_);
215
216 void CollectNewFieldAndMethodMappings(const RedefinitionDataIter& data,
217 std::map<art::ArtMethod*, art::ArtMethod*>* method_map,
218 std::map<art::ArtField*, art::ArtField*>* field_map)
Alex Light0e692732017-01-10 15:00:05 -0800219 REQUIRES(art::Locks::mutator_lock_);
220
Alex Light1e3926a2017-04-07 10:38:06 -0700221 void RestoreObsoleteMethodMapsIfUnneeded(const RedefinitionDataIter* cur_data)
222 REQUIRES(art::Locks::mutator_lock_);
223
Alex Light0e692732017-01-10 15:00:05 -0800224 void ReleaseDexFile() REQUIRES_SHARED(art::Locks::mutator_lock_);
225
Alex Light5643caf2017-02-08 11:39:07 -0800226 void UnregisterBreakpoints() REQUIRES_SHARED(art::Locks::mutator_lock_);
Alex Lighta26e3492017-06-27 17:55:37 -0700227 // This should be done with all threads suspended.
Alex Lighte34fe442018-02-21 17:35:55 -0800228 void UnregisterJvmtiBreakpoints() REQUIRES_SHARED(art::Locks::mutator_lock_);
Alex Light5643caf2017-02-08 11:39:07 -0800229
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +0000230 void RecordNewMethodAdded();
231 void RecordNewFieldAdded();
232
Alex Light0e692732017-01-10 15:00:05 -0800233 private:
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +0000234 bool IsStructuralRedefinition() const {
235 DCHECK(!(added_fields_ || added_methods_) || driver_->IsStructuralRedefinition())
236 << "added_fields_: " << added_fields_ << " added_methods_: " << added_methods_
237 << " driver_->IsStructuralRedefinition(): " << driver_->IsStructuralRedefinition();
238 return driver_->IsStructuralRedefinition() && (added_fields_ || added_methods_);
239 }
240
241 void UpdateClassStructurally(const RedefinitionDataIter& cur_data)
242 REQUIRES(art::Locks::mutator_lock_);
243
244 void UpdateClassInPlace(const RedefinitionDataIter& cur_data)
245 REQUIRES(art::Locks::mutator_lock_);
246
Alex Light0e692732017-01-10 15:00:05 -0800247 Redefiner* driver_;
248 jclass klass_;
249 std::unique_ptr<const art::DexFile> dex_file_;
250 std::string class_sig_;
Vladimir Markoe1993c72017-06-14 17:01:38 +0100251 art::ArrayRef<const unsigned char> original_dex_file_;
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +0000252
253 bool added_fields_ = false;
254 bool added_methods_ = false;
Alex Light0e692732017-01-10 15:00:05 -0800255 };
256
Alex Lighta26e3492017-06-27 17:55:37 -0700257 ArtJvmTiEnv* env_;
Alex Lighta01de592016-11-15 10:43:06 -0800258 jvmtiError result_;
259 art::Runtime* runtime_;
260 art::Thread* self_;
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +0000261 RedefinitionType type_;
Alex Light0e692732017-01-10 15:00:05 -0800262 std::vector<ClassRedefinition> redefinitions_;
Alex Lighta01de592016-11-15 10:43:06 -0800263 // Kept as a jclass since we have weird run-state changes that make keeping it around as a
264 // mirror::Class difficult and confusing.
Alex Lighta01de592016-11-15 10:43:06 -0800265 std::string* error_msg_;
Alex Lighta01de592016-11-15 10:43:06 -0800266
Alex Lighta26e3492017-06-27 17:55:37 -0700267 Redefiner(ArtJvmTiEnv* env,
268 art::Runtime* runtime,
Alex Lighta01de592016-11-15 10:43:06 -0800269 art::Thread* self,
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +0000270 RedefinitionType type,
Alex Lighta01de592016-11-15 10:43:06 -0800271 std::string* error_msg)
Alex Lighta26e3492017-06-27 17:55:37 -0700272 : env_(env),
273 result_(ERR(INTERNAL)),
Alex Lighta01de592016-11-15 10:43:06 -0800274 runtime_(runtime),
275 self_(self),
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +0000276 type_(type),
Alex Light0e692732017-01-10 15:00:05 -0800277 redefinitions_(),
278 error_msg_(error_msg) { }
279
Alex Light6ac57502017-01-19 15:05:06 -0800280 jvmtiError AddRedefinition(ArtJvmTiEnv* env, const ArtClassDefinition& def)
Alex Light0e692732017-01-10 15:00:05 -0800281 REQUIRES_SHARED(art::Locks::mutator_lock_);
Alex Lighta01de592016-11-15 10:43:06 -0800282
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +0000283 template<RedefinitionType kType = RedefinitionType::kNormal>
284 static jvmtiError IsModifiableClassGeneric(jvmtiEnv* env, jclass klass, jboolean* is_redefinable);
285
286 template<RedefinitionType kType = RedefinitionType::kNormal>
Alex Lighte4a88632017-01-10 07:41:24 -0800287 static jvmtiError GetClassRedefinitionError(art::Handle<art::mirror::Class> klass,
288 /*out*/std::string* error_msg)
289 REQUIRES_SHARED(art::Locks::mutator_lock_);
290
Alex Lighta01de592016-11-15 10:43:06 -0800291 jvmtiError Run() REQUIRES_SHARED(art::Locks::mutator_lock_);
292
Alex Light0e692732017-01-10 15:00:05 -0800293 bool CheckAllRedefinitionAreValid() REQUIRES_SHARED(art::Locks::mutator_lock_);
Alex Lightc5f5a6e2017-03-01 16:57:08 -0800294 bool CheckAllClassesAreVerified(RedefinitionDataHolder& holder)
Alex Light8c889d22017-02-06 13:58:27 -0800295 REQUIRES_SHARED(art::Locks::mutator_lock_);
Alex Light1e3926a2017-04-07 10:38:06 -0700296 bool EnsureAllClassAllocationsFinished(RedefinitionDataHolder& holder)
297 REQUIRES_SHARED(art::Locks::mutator_lock_);
Alex Light0e692732017-01-10 15:00:05 -0800298 bool FinishAllRemainingAllocations(RedefinitionDataHolder& holder)
Alex Lighta01de592016-11-15 10:43:06 -0800299 REQUIRES_SHARED(art::Locks::mutator_lock_);
Alex Light0e692732017-01-10 15:00:05 -0800300 void ReleaseAllDexFiles() REQUIRES_SHARED(art::Locks::mutator_lock_);
Alex Light5643caf2017-02-08 11:39:07 -0800301 void UnregisterAllBreakpoints() REQUIRES_SHARED(art::Locks::mutator_lock_);
Alex Light1e3926a2017-04-07 10:38:06 -0700302 // Restores the old obsolete methods maps if it turns out they weren't needed (ie there were no
303 // new obsolete methods).
304 void RestoreObsoleteMethodMapsIfUnneeded(RedefinitionDataHolder& holder)
305 REQUIRES(art::Locks::mutator_lock_);
Alex Lighta01de592016-11-15 10:43:06 -0800306
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +0000307 bool IsStructuralRedefinition() const {
308 return type_ == RedefinitionType::kStructural;
309 }
310
Alex Light0e692732017-01-10 15:00:05 -0800311 void RecordFailure(jvmtiError result, const std::string& class_sig, const std::string& error_msg);
312 void RecordFailure(jvmtiError result, const std::string& error_msg) {
313 RecordFailure(result, "NO CLASS", error_msg);
Alex Lighta01de592016-11-15 10:43:06 -0800314 }
315
Alex Light0e692732017-01-10 15:00:05 -0800316 friend struct CallbackCtx;
Alex Lightc5f5a6e2017-03-01 16:57:08 -0800317 friend class RedefinitionDataHolder;
318 friend class RedefinitionDataIter;
Alex Lighta01de592016-11-15 10:43:06 -0800319};
320
321} // namespace openjdkjvmti
322
Andreas Gampe06c42a52017-07-26 14:17:14 -0700323#endif // ART_OPENJDKJVMTI_TI_REDEFINE_H_