blob: dad085de59e8c6ce49bbebd4bf1f2d9513d9b0db [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 Light3732beb2019-10-04 13:35:34 -070087 static jvmtiError RedefineClasses(jvmtiEnv* env,
Alex Light0e692732017-01-10 15:00:05 -080088 jint class_count,
Alex Light3732beb2019-10-04 13:35:34 -070089 const jvmtiClassDefinition* definitions);
Alex Lighta01de592016-11-15 10:43:06 -080090
Alex Lighte4a88632017-01-10 07:41:24 -080091 static jvmtiError IsModifiableClass(jvmtiEnv* env, jclass klass, jboolean* is_redefinable);
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +000092 static jvmtiError IsStructurallyModifiableClass(jvmtiEnv* env,
93 jclass klass,
94 jboolean* is_redefinable);
Alex Lighte4a88632017-01-10 07:41:24 -080095
Vladimir Markoc34bebf2018-08-16 16:12:49 +010096 static art::MemMap MoveDataToMemMap(const std::string& original_location,
97 art::ArrayRef<const unsigned char> data,
98 std::string* error_msg);
Alex Light440b5d92017-01-24 15:32:25 -080099
Alex Light9e7859c2018-04-05 13:49:43 -0700100 // Helper for checking if redefinition/retransformation is allowed.
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +0000101 template<RedefinitionType kType = RedefinitionType::kNormal>
Alex Light9e7859c2018-04-05 13:49:43 -0700102 static jvmtiError GetClassRedefinitionError(jclass klass, /*out*/std::string* error_msg)
103 REQUIRES(!art::Locks::mutator_lock_);
104
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +0000105 static jvmtiError StructurallyRedefineClassDirect(jvmtiEnv* env,
106 jclass klass,
107 const unsigned char* data,
108 jint data_size);
109
Alex Lighta01de592016-11-15 10:43:06 -0800110 private:
Alex Light0e692732017-01-10 15:00:05 -0800111 class ClassRedefinition {
112 public:
113 ClassRedefinition(Redefiner* driver,
114 jclass klass,
115 const art::DexFile* redefined_dex_file,
Alex Lighta7e38d82017-01-19 14:57:28 -0800116 const char* class_sig,
Vladimir Markoe1993c72017-06-14 17:01:38 +0100117 art::ArrayRef<const unsigned char> orig_dex_file)
Alex Light0e692732017-01-10 15:00:05 -0800118 REQUIRES_SHARED(art::Locks::mutator_lock_);
119
120 // NO_THREAD_SAFETY_ANALYSIS so we can unlock the class in the destructor.
121 ~ClassRedefinition() NO_THREAD_SAFETY_ANALYSIS;
122
123 // Move constructor so we can put these into a vector.
124 ClassRedefinition(ClassRedefinition&& other)
125 : driver_(other.driver_),
126 klass_(other.klass_),
127 dex_file_(std::move(other.dex_file_)),
Alex Lighta7e38d82017-01-19 14:57:28 -0800128 class_sig_(std::move(other.class_sig_)),
129 original_dex_file_(other.original_dex_file_) {
Alex Light0e692732017-01-10 15:00:05 -0800130 other.driver_ = nullptr;
131 }
132
Vladimir Marko4617d582019-03-28 13:48:31 +0000133 art::ObjPtr<art::mirror::Class> GetMirrorClass() REQUIRES_SHARED(art::Locks::mutator_lock_);
Vladimir Markoc524e9e2019-03-26 10:54:50 +0000134 art::ObjPtr<art::mirror::ClassLoader> GetClassLoader()
135 REQUIRES_SHARED(art::Locks::mutator_lock_);
Alex Light0e692732017-01-10 15:00:05 -0800136
Alex Light7916f202017-01-27 09:00:15 -0800137 const art::DexFile& GetDexFile() {
138 return *dex_file_;
139 }
140
Alex Light0e692732017-01-10 15:00:05 -0800141 art::mirror::DexCache* CreateNewDexCache(art::Handle<art::mirror::ClassLoader> loader)
142 REQUIRES_SHARED(art::Locks::mutator_lock_);
143
Alex Lighta7e38d82017-01-19 14:57:28 -0800144 // This may return nullptr with a OOME pending if allocation fails.
Alex Light2f814aa2017-03-24 15:21:34 +0000145 art::mirror::Object* AllocateOrGetOriginalDexFile()
Alex Lighta7e38d82017-01-19 14:57:28 -0800146 REQUIRES_SHARED(art::Locks::mutator_lock_);
147
Alex Light0e692732017-01-10 15:00:05 -0800148 void RecordFailure(jvmtiError e, const std::string& err) {
149 driver_->RecordFailure(e, class_sig_, err);
150 }
151
Alex Lightc5f5a6e2017-03-01 16:57:08 -0800152 bool FinishRemainingAllocations(/*out*/RedefinitionDataIter* cur_data)
Alex Light0e692732017-01-10 15:00:05 -0800153 REQUIRES_SHARED(art::Locks::mutator_lock_);
154
Alex Light1babae02017-02-01 15:35:34 -0800155 bool AllocateAndRememberNewDexFileCookie(
Alex Light1babae02017-02-01 15:35:34 -0800156 art::Handle<art::mirror::ClassLoader> source_class_loader,
157 art::Handle<art::mirror::Object> dex_file_obj,
Alex Lightc5f5a6e2017-03-01 16:57:08 -0800158 /*out*/RedefinitionDataIter* cur_data)
Alex Light1babae02017-02-01 15:35:34 -0800159 REQUIRES_SHARED(art::Locks::mutator_lock_);
160
Vladimir Markod93e3742018-07-18 10:58:13 +0100161 void FindAndAllocateObsoleteMethods(art::ObjPtr<art::mirror::Class> art_klass)
Alex Light0e692732017-01-10 15:00:05 -0800162 REQUIRES(art::Locks::mutator_lock_);
163
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +0000164 art::ObjPtr<art::mirror::Class> AllocateNewClassObject(art::Handle<art::mirror::DexCache> cache)
165 REQUIRES_SHARED(art::Locks::mutator_lock_);
166
167 uint32_t GetNewClassSize(bool with_embedded_tables, art::Handle<art::mirror::Class> old_class)
168 REQUIRES_SHARED(art::Locks::mutator_lock_);
169
Alex Light0e692732017-01-10 15:00:05 -0800170 // Checks that the dex file contains only the single expected class and that the top-level class
171 // data has not been modified in an incompatible manner.
172 bool CheckClass() REQUIRES_SHARED(art::Locks::mutator_lock_);
173
Alex Light8c889d22017-02-06 13:58:27 -0800174 // Checks that the contained class can be successfully verified.
Alex Lightc5f5a6e2017-03-01 16:57:08 -0800175 bool CheckVerification(const RedefinitionDataIter& holder)
Alex Light8c889d22017-02-06 13:58:27 -0800176 REQUIRES_SHARED(art::Locks::mutator_lock_);
177
Alex Light0e692732017-01-10 15:00:05 -0800178 // Preallocates all needed allocations in klass so that we can pause execution safely.
Alex Light1e3926a2017-04-07 10:38:06 -0700179 bool EnsureClassAllocationsFinished(/*out*/RedefinitionDataIter* data)
180 REQUIRES_SHARED(art::Locks::mutator_lock_);
Alex Light0e692732017-01-10 15:00:05 -0800181
182 // This will check that no constraints are violated (more than 1 class in dex file, any changes
183 // in number/declaration of methods & fields, changes in access flags, etc.)
184 bool CheckRedefinitionIsValid() REQUIRES_SHARED(art::Locks::mutator_lock_);
185
186 // Checks that the class can even be redefined.
187 bool CheckRedefinable() REQUIRES_SHARED(art::Locks::mutator_lock_);
188
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +0000189 // Checks that the dex file does not add/remove methods, or change their modifiers or types in
190 // illegal ways.
191 bool CheckMethods() REQUIRES_SHARED(art::Locks::mutator_lock_);
Alex Light0e692732017-01-10 15:00:05 -0800192
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +0000193 // Checks that the dex file does not modify fields types or modifiers in illegal ways.
194 bool CheckFields() REQUIRES_SHARED(art::Locks::mutator_lock_);
Alex Light0e692732017-01-10 15:00:05 -0800195
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +0000196 // Temporary check that a class undergoing structural redefinition has no instances. This
197 // requirement will be removed in time.
Alex Light0e692732017-01-10 15:00:05 -0800198 void UpdateJavaDexFile(art::ObjPtr<art::mirror::Object> java_dex_file,
199 art::ObjPtr<art::mirror::LongArray> new_cookie)
200 REQUIRES(art::Locks::mutator_lock_);
201
202 void UpdateFields(art::ObjPtr<art::mirror::Class> mclass)
203 REQUIRES(art::Locks::mutator_lock_);
204
205 void UpdateMethods(art::ObjPtr<art::mirror::Class> mclass,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800206 const art::dex::ClassDef& class_def)
Alex Light0e692732017-01-10 15:00:05 -0800207 REQUIRES(art::Locks::mutator_lock_);
208
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +0000209 void UpdateClass(const RedefinitionDataIter& cur_data)
210 REQUIRES(art::Locks::mutator_lock_);
211
Alex Lightb1eebde2019-10-22 16:30:47 +0000212 void ReverifyClass(const RedefinitionDataIter& cur_data)
213 REQUIRES(art::Locks::mutator_lock_);
214
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +0000215 void CollectNewFieldAndMethodMappings(const RedefinitionDataIter& data,
216 std::map<art::ArtMethod*, art::ArtMethod*>* method_map,
217 std::map<art::ArtField*, art::ArtField*>* field_map)
Alex Light0e692732017-01-10 15:00:05 -0800218 REQUIRES(art::Locks::mutator_lock_);
219
Alex Light1e3926a2017-04-07 10:38:06 -0700220 void RestoreObsoleteMethodMapsIfUnneeded(const RedefinitionDataIter* cur_data)
221 REQUIRES(art::Locks::mutator_lock_);
222
Alex Light0e692732017-01-10 15:00:05 -0800223 void ReleaseDexFile() REQUIRES_SHARED(art::Locks::mutator_lock_);
224
Alex Light5643caf2017-02-08 11:39:07 -0800225 void UnregisterBreakpoints() REQUIRES_SHARED(art::Locks::mutator_lock_);
Alex Lighta26e3492017-06-27 17:55:37 -0700226 // This should be done with all threads suspended.
Alex Lighte34fe442018-02-21 17:35:55 -0800227 void UnregisterJvmtiBreakpoints() REQUIRES_SHARED(art::Locks::mutator_lock_);
Alex Light5643caf2017-02-08 11:39:07 -0800228
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +0000229 void RecordNewMethodAdded();
230 void RecordNewFieldAdded();
231
Alex Light0e692732017-01-10 15:00:05 -0800232 private:
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +0000233 bool IsStructuralRedefinition() const {
234 DCHECK(!(added_fields_ || added_methods_) || driver_->IsStructuralRedefinition())
235 << "added_fields_: " << added_fields_ << " added_methods_: " << added_methods_
236 << " driver_->IsStructuralRedefinition(): " << driver_->IsStructuralRedefinition();
237 return driver_->IsStructuralRedefinition() && (added_fields_ || added_methods_);
238 }
239
240 void UpdateClassStructurally(const RedefinitionDataIter& cur_data)
241 REQUIRES(art::Locks::mutator_lock_);
242
243 void UpdateClassInPlace(const RedefinitionDataIter& cur_data)
244 REQUIRES(art::Locks::mutator_lock_);
245
Alex Light0e692732017-01-10 15:00:05 -0800246 Redefiner* driver_;
247 jclass klass_;
248 std::unique_ptr<const art::DexFile> dex_file_;
249 std::string class_sig_;
Vladimir Markoe1993c72017-06-14 17:01:38 +0100250 art::ArrayRef<const unsigned char> original_dex_file_;
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +0000251
252 bool added_fields_ = false;
253 bool added_methods_ = false;
Alex Lightb1eebde2019-10-22 16:30:47 +0000254
255 // Does the class need to be reverified due to verification soft-fails possibly forcing
256 // interpreter or lock-counting?
257 bool needs_reverify_ = false;
Alex Light0e692732017-01-10 15:00:05 -0800258 };
259
Alex Lighta26e3492017-06-27 17:55:37 -0700260 ArtJvmTiEnv* env_;
Alex Lighta01de592016-11-15 10:43:06 -0800261 jvmtiError result_;
262 art::Runtime* runtime_;
263 art::Thread* self_;
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +0000264 RedefinitionType type_;
Alex Light0e692732017-01-10 15:00:05 -0800265 std::vector<ClassRedefinition> redefinitions_;
Alex Lighta01de592016-11-15 10:43:06 -0800266 // Kept as a jclass since we have weird run-state changes that make keeping it around as a
267 // mirror::Class difficult and confusing.
Alex Lighta01de592016-11-15 10:43:06 -0800268 std::string* error_msg_;
Alex Lighta01de592016-11-15 10:43:06 -0800269
Alex Lighta26e3492017-06-27 17:55:37 -0700270 Redefiner(ArtJvmTiEnv* env,
271 art::Runtime* runtime,
Alex Lighta01de592016-11-15 10:43:06 -0800272 art::Thread* self,
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +0000273 RedefinitionType type,
Alex Lighta01de592016-11-15 10:43:06 -0800274 std::string* error_msg)
Alex Lighta26e3492017-06-27 17:55:37 -0700275 : env_(env),
276 result_(ERR(INTERNAL)),
Alex Lighta01de592016-11-15 10:43:06 -0800277 runtime_(runtime),
278 self_(self),
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +0000279 type_(type),
Alex Light0e692732017-01-10 15:00:05 -0800280 redefinitions_(),
281 error_msg_(error_msg) { }
282
Alex Light6ac57502017-01-19 15:05:06 -0800283 jvmtiError AddRedefinition(ArtJvmTiEnv* env, const ArtClassDefinition& def)
Alex Light0e692732017-01-10 15:00:05 -0800284 REQUIRES_SHARED(art::Locks::mutator_lock_);
Alex Lighta01de592016-11-15 10:43:06 -0800285
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +0000286 template<RedefinitionType kType = RedefinitionType::kNormal>
287 static jvmtiError IsModifiableClassGeneric(jvmtiEnv* env, jclass klass, jboolean* is_redefinable);
288
289 template<RedefinitionType kType = RedefinitionType::kNormal>
Alex Lighte4a88632017-01-10 07:41:24 -0800290 static jvmtiError GetClassRedefinitionError(art::Handle<art::mirror::Class> klass,
291 /*out*/std::string* error_msg)
292 REQUIRES_SHARED(art::Locks::mutator_lock_);
293
Alex Lighta01de592016-11-15 10:43:06 -0800294 jvmtiError Run() REQUIRES_SHARED(art::Locks::mutator_lock_);
295
Alex Light0e692732017-01-10 15:00:05 -0800296 bool CheckAllRedefinitionAreValid() REQUIRES_SHARED(art::Locks::mutator_lock_);
Alex Lightc5f5a6e2017-03-01 16:57:08 -0800297 bool CheckAllClassesAreVerified(RedefinitionDataHolder& holder)
Alex Light8c889d22017-02-06 13:58:27 -0800298 REQUIRES_SHARED(art::Locks::mutator_lock_);
Alex Light1e3926a2017-04-07 10:38:06 -0700299 bool EnsureAllClassAllocationsFinished(RedefinitionDataHolder& holder)
300 REQUIRES_SHARED(art::Locks::mutator_lock_);
Alex Light0e692732017-01-10 15:00:05 -0800301 bool FinishAllRemainingAllocations(RedefinitionDataHolder& holder)
Alex Lighta01de592016-11-15 10:43:06 -0800302 REQUIRES_SHARED(art::Locks::mutator_lock_);
Alex Light0e692732017-01-10 15:00:05 -0800303 void ReleaseAllDexFiles() REQUIRES_SHARED(art::Locks::mutator_lock_);
Alex Lightb1eebde2019-10-22 16:30:47 +0000304 void ReverifyClasses(RedefinitionDataHolder& holder) REQUIRES(art::Locks::mutator_lock_);
Alex Light5643caf2017-02-08 11:39:07 -0800305 void UnregisterAllBreakpoints() REQUIRES_SHARED(art::Locks::mutator_lock_);
Alex Light1e3926a2017-04-07 10:38:06 -0700306 // Restores the old obsolete methods maps if it turns out they weren't needed (ie there were no
307 // new obsolete methods).
308 void RestoreObsoleteMethodMapsIfUnneeded(RedefinitionDataHolder& holder)
309 REQUIRES(art::Locks::mutator_lock_);
Alex Lighta01de592016-11-15 10:43:06 -0800310
Nicolas Geoffray4ac0e152019-09-18 06:14:50 +0000311 bool IsStructuralRedefinition() const {
312 return type_ == RedefinitionType::kStructural;
313 }
314
Alex Light0e692732017-01-10 15:00:05 -0800315 void RecordFailure(jvmtiError result, const std::string& class_sig, const std::string& error_msg);
316 void RecordFailure(jvmtiError result, const std::string& error_msg) {
317 RecordFailure(result, "NO CLASS", error_msg);
Alex Lighta01de592016-11-15 10:43:06 -0800318 }
319
Alex Light0e692732017-01-10 15:00:05 -0800320 friend struct CallbackCtx;
Alex Lightc5f5a6e2017-03-01 16:57:08 -0800321 friend class RedefinitionDataHolder;
322 friend class RedefinitionDataIter;
Alex Lighta01de592016-11-15 10:43:06 -0800323};
324
325} // namespace openjdkjvmti
326
Andreas Gampe06c42a52017-07-26 14:17:14 -0700327#endif // ART_OPENJDKJVMTI_TI_REDEFINE_H_