blob: a974dc177fbe703c267d7d43386f71a4566fffcd [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
35#include <string>
36
37#include <jni.h>
38
39#include "art_jvmti.h"
Vladimir Markoe1993c72017-06-14 17:01:38 +010040#include "base/array_ref.h"
David Sehr1979c642018-04-26 14:41:18 -070041#include "base/globals.h"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010042#include "jni/jni_env_ext-inl.h"
Alex Lighta01de592016-11-15 10:43:06 -080043#include "jvmti.h"
Alex Lighta01de592016-11-15 10:43:06 -080044#include "mirror/array.h"
Alex Lighta01de592016-11-15 10:43:06 -080045#include "mirror/class.h"
Alex Lighta01de592016-11-15 10:43:06 -080046#include "obj_ptr.h"
Alex Lighta01de592016-11-15 10:43:06 -080047
Andreas Gampe3f1dcd32018-12-28 09:39:56 -080048namespace art {
49namespace dex {
50struct ClassDef;
51} // namespace dex
52class DexFile;
53} // namespace art
54
Alex Lighta01de592016-11-15 10:43:06 -080055namespace openjdkjvmti {
56
Vladimir Marko5924a4a2018-05-29 17:40:41 +010057class ArtClassDefinition;
Alex Light0e692732017-01-10 15:00:05 -080058class RedefinitionDataHolder;
Alex Lightc5f5a6e2017-03-01 16:57:08 -080059class RedefinitionDataIter;
Alex Light0e692732017-01-10 15:00:05 -080060
Alex Lighta01de592016-11-15 10:43:06 -080061// Class that can redefine a single class's methods.
62class Redefiner {
63 public:
Alex Light0e692732017-01-10 15:00:05 -080064 // Redefine the given classes with the given dex data. Note this function does not take ownership
65 // 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 -080066 // The caller is responsible for freeing it. The runtime makes its own copy of the data. This
67 // function does not call the transformation events.
Alex Light6ac57502017-01-19 15:05:06 -080068 static jvmtiError RedefineClassesDirect(ArtJvmTiEnv* env,
69 art::Runtime* runtime,
70 art::Thread* self,
71 const std::vector<ArtClassDefinition>& definitions,
72 /*out*/std::string* error_msg);
73
74 // Redefine the given classes with the given dex data. Note this function does not take ownership
75 // 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 -080076 // The caller is responsible for freeing it. The runtime makes its own copy of the data.
Alex Light0e692732017-01-10 15:00:05 -080077 static jvmtiError RedefineClasses(ArtJvmTiEnv* env,
Andreas Gampede19eb92017-02-24 16:21:18 -080078 EventHandler* event_handler,
Alex Light0e692732017-01-10 15:00:05 -080079 art::Runtime* runtime,
80 art::Thread* self,
81 jint class_count,
82 const jvmtiClassDefinition* definitions,
Alex Light6ac57502017-01-19 15:05:06 -080083 /*out*/std::string* error_msg);
Alex Lighta01de592016-11-15 10:43:06 -080084
Alex Lighte4a88632017-01-10 07:41:24 -080085 static jvmtiError IsModifiableClass(jvmtiEnv* env, jclass klass, jboolean* is_redefinable);
86
Vladimir Markoc34bebf2018-08-16 16:12:49 +010087 static art::MemMap MoveDataToMemMap(const std::string& original_location,
88 art::ArrayRef<const unsigned char> data,
89 std::string* error_msg);
Alex Light440b5d92017-01-24 15:32:25 -080090
Alex Light9e7859c2018-04-05 13:49:43 -070091 // Helper for checking if redefinition/retransformation is allowed.
92 static jvmtiError GetClassRedefinitionError(jclass klass, /*out*/std::string* error_msg)
93 REQUIRES(!art::Locks::mutator_lock_);
94
Alex Lighta01de592016-11-15 10:43:06 -080095 private:
Alex Light0e692732017-01-10 15:00:05 -080096 class ClassRedefinition {
97 public:
98 ClassRedefinition(Redefiner* driver,
99 jclass klass,
100 const art::DexFile* redefined_dex_file,
Alex Lighta7e38d82017-01-19 14:57:28 -0800101 const char* class_sig,
Vladimir Markoe1993c72017-06-14 17:01:38 +0100102 art::ArrayRef<const unsigned char> orig_dex_file)
Alex Light0e692732017-01-10 15:00:05 -0800103 REQUIRES_SHARED(art::Locks::mutator_lock_);
104
105 // NO_THREAD_SAFETY_ANALYSIS so we can unlock the class in the destructor.
106 ~ClassRedefinition() NO_THREAD_SAFETY_ANALYSIS;
107
108 // Move constructor so we can put these into a vector.
109 ClassRedefinition(ClassRedefinition&& other)
110 : driver_(other.driver_),
111 klass_(other.klass_),
112 dex_file_(std::move(other.dex_file_)),
Alex Lighta7e38d82017-01-19 14:57:28 -0800113 class_sig_(std::move(other.class_sig_)),
114 original_dex_file_(other.original_dex_file_) {
Alex Light0e692732017-01-10 15:00:05 -0800115 other.driver_ = nullptr;
116 }
117
118 art::mirror::Class* GetMirrorClass() REQUIRES_SHARED(art::Locks::mutator_lock_);
119 art::mirror::ClassLoader* GetClassLoader() REQUIRES_SHARED(art::Locks::mutator_lock_);
120
Alex Light7916f202017-01-27 09:00:15 -0800121 const art::DexFile& GetDexFile() {
122 return *dex_file_;
123 }
124
Alex Light0e692732017-01-10 15:00:05 -0800125 art::mirror::DexCache* CreateNewDexCache(art::Handle<art::mirror::ClassLoader> loader)
126 REQUIRES_SHARED(art::Locks::mutator_lock_);
127
Alex Lighta7e38d82017-01-19 14:57:28 -0800128 // This may return nullptr with a OOME pending if allocation fails.
Alex Light2f814aa2017-03-24 15:21:34 +0000129 art::mirror::Object* AllocateOrGetOriginalDexFile()
Alex Lighta7e38d82017-01-19 14:57:28 -0800130 REQUIRES_SHARED(art::Locks::mutator_lock_);
131
Alex Light0e692732017-01-10 15:00:05 -0800132 void RecordFailure(jvmtiError e, const std::string& err) {
133 driver_->RecordFailure(e, class_sig_, err);
134 }
135
Alex Lightc5f5a6e2017-03-01 16:57:08 -0800136 bool FinishRemainingAllocations(/*out*/RedefinitionDataIter* cur_data)
Alex Light0e692732017-01-10 15:00:05 -0800137 REQUIRES_SHARED(art::Locks::mutator_lock_);
138
Alex Light1babae02017-02-01 15:35:34 -0800139 bool AllocateAndRememberNewDexFileCookie(
Alex Light1babae02017-02-01 15:35:34 -0800140 art::Handle<art::mirror::ClassLoader> source_class_loader,
141 art::Handle<art::mirror::Object> dex_file_obj,
Alex Lightc5f5a6e2017-03-01 16:57:08 -0800142 /*out*/RedefinitionDataIter* cur_data)
Alex Light1babae02017-02-01 15:35:34 -0800143 REQUIRES_SHARED(art::Locks::mutator_lock_);
144
Vladimir Markod93e3742018-07-18 10:58:13 +0100145 void FindAndAllocateObsoleteMethods(art::ObjPtr<art::mirror::Class> art_klass)
Alex Light0e692732017-01-10 15:00:05 -0800146 REQUIRES(art::Locks::mutator_lock_);
147
Alex Light0e692732017-01-10 15:00:05 -0800148 // Checks that the dex file contains only the single expected class and that the top-level class
149 // data has not been modified in an incompatible manner.
150 bool CheckClass() REQUIRES_SHARED(art::Locks::mutator_lock_);
151
Alex Light8c889d22017-02-06 13:58:27 -0800152 // Checks that the contained class can be successfully verified.
Alex Lightc5f5a6e2017-03-01 16:57:08 -0800153 bool CheckVerification(const RedefinitionDataIter& holder)
Alex Light8c889d22017-02-06 13:58:27 -0800154 REQUIRES_SHARED(art::Locks::mutator_lock_);
155
Alex Light0e692732017-01-10 15:00:05 -0800156 // Preallocates all needed allocations in klass so that we can pause execution safely.
Alex Light1e3926a2017-04-07 10:38:06 -0700157 bool EnsureClassAllocationsFinished(/*out*/RedefinitionDataIter* data)
158 REQUIRES_SHARED(art::Locks::mutator_lock_);
Alex Light0e692732017-01-10 15:00:05 -0800159
160 // This will check that no constraints are violated (more than 1 class in dex file, any changes
161 // in number/declaration of methods & fields, changes in access flags, etc.)
162 bool CheckRedefinitionIsValid() REQUIRES_SHARED(art::Locks::mutator_lock_);
163
164 // Checks that the class can even be redefined.
165 bool CheckRedefinable() REQUIRES_SHARED(art::Locks::mutator_lock_);
166
Alex Light6161f132017-01-25 10:30:20 -0800167 // Checks that the dex file does not add/remove methods, or change their modifiers or types.
168 bool CheckSameMethods() REQUIRES_SHARED(art::Locks::mutator_lock_);
Alex Light0e692732017-01-10 15:00:05 -0800169
Alex Light6161f132017-01-25 10:30:20 -0800170 // Checks that the dex file does not modify fields types or modifiers.
171 bool CheckSameFields() REQUIRES_SHARED(art::Locks::mutator_lock_);
Alex Light0e692732017-01-10 15:00:05 -0800172
173 void UpdateJavaDexFile(art::ObjPtr<art::mirror::Object> java_dex_file,
174 art::ObjPtr<art::mirror::LongArray> new_cookie)
175 REQUIRES(art::Locks::mutator_lock_);
176
177 void UpdateFields(art::ObjPtr<art::mirror::Class> mclass)
178 REQUIRES(art::Locks::mutator_lock_);
179
180 void UpdateMethods(art::ObjPtr<art::mirror::Class> mclass,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800181 const art::dex::ClassDef& class_def)
Alex Light0e692732017-01-10 15:00:05 -0800182 REQUIRES(art::Locks::mutator_lock_);
183
184 void UpdateClass(art::ObjPtr<art::mirror::Class> mclass,
Alex Lighta7e38d82017-01-19 14:57:28 -0800185 art::ObjPtr<art::mirror::DexCache> new_dex_cache,
Alex Light2f814aa2017-03-24 15:21:34 +0000186 art::ObjPtr<art::mirror::Object> original_dex_file)
Alex Light0e692732017-01-10 15:00:05 -0800187 REQUIRES(art::Locks::mutator_lock_);
188
Alex Light1e3926a2017-04-07 10:38:06 -0700189 void RestoreObsoleteMethodMapsIfUnneeded(const RedefinitionDataIter* cur_data)
190 REQUIRES(art::Locks::mutator_lock_);
191
Alex Light0e692732017-01-10 15:00:05 -0800192 void ReleaseDexFile() REQUIRES_SHARED(art::Locks::mutator_lock_);
193
Alex Light5643caf2017-02-08 11:39:07 -0800194 void UnregisterBreakpoints() REQUIRES_SHARED(art::Locks::mutator_lock_);
Alex Lighta26e3492017-06-27 17:55:37 -0700195 // This should be done with all threads suspended.
Alex Lighte34fe442018-02-21 17:35:55 -0800196 void UnregisterJvmtiBreakpoints() REQUIRES_SHARED(art::Locks::mutator_lock_);
Alex Light5643caf2017-02-08 11:39:07 -0800197
Alex Light0e692732017-01-10 15:00:05 -0800198 private:
199 Redefiner* driver_;
200 jclass klass_;
201 std::unique_ptr<const art::DexFile> dex_file_;
202 std::string class_sig_;
Vladimir Markoe1993c72017-06-14 17:01:38 +0100203 art::ArrayRef<const unsigned char> original_dex_file_;
Alex Light0e692732017-01-10 15:00:05 -0800204 };
205
Alex Lighta26e3492017-06-27 17:55:37 -0700206 ArtJvmTiEnv* env_;
Alex Lighta01de592016-11-15 10:43:06 -0800207 jvmtiError result_;
208 art::Runtime* runtime_;
209 art::Thread* self_;
Alex Light0e692732017-01-10 15:00:05 -0800210 std::vector<ClassRedefinition> redefinitions_;
Alex Lighta01de592016-11-15 10:43:06 -0800211 // Kept as a jclass since we have weird run-state changes that make keeping it around as a
212 // mirror::Class difficult and confusing.
Alex Lighta01de592016-11-15 10:43:06 -0800213 std::string* error_msg_;
Alex Lighta01de592016-11-15 10:43:06 -0800214
Alex Lighta26e3492017-06-27 17:55:37 -0700215 Redefiner(ArtJvmTiEnv* env,
216 art::Runtime* runtime,
Alex Lighta01de592016-11-15 10:43:06 -0800217 art::Thread* self,
Alex Lighta01de592016-11-15 10:43:06 -0800218 std::string* error_msg)
Alex Lighta26e3492017-06-27 17:55:37 -0700219 : env_(env),
220 result_(ERR(INTERNAL)),
Alex Lighta01de592016-11-15 10:43:06 -0800221 runtime_(runtime),
222 self_(self),
Alex Light0e692732017-01-10 15:00:05 -0800223 redefinitions_(),
224 error_msg_(error_msg) { }
225
Alex Light6ac57502017-01-19 15:05:06 -0800226 jvmtiError AddRedefinition(ArtJvmTiEnv* env, const ArtClassDefinition& def)
Alex Light0e692732017-01-10 15:00:05 -0800227 REQUIRES_SHARED(art::Locks::mutator_lock_);
Alex Lighta01de592016-11-15 10:43:06 -0800228
Alex Lighte4a88632017-01-10 07:41:24 -0800229 static jvmtiError GetClassRedefinitionError(art::Handle<art::mirror::Class> klass,
230 /*out*/std::string* error_msg)
231 REQUIRES_SHARED(art::Locks::mutator_lock_);
232
Alex Lighta01de592016-11-15 10:43:06 -0800233 jvmtiError Run() REQUIRES_SHARED(art::Locks::mutator_lock_);
234
Alex Light0e692732017-01-10 15:00:05 -0800235 bool CheckAllRedefinitionAreValid() REQUIRES_SHARED(art::Locks::mutator_lock_);
Alex Lightc5f5a6e2017-03-01 16:57:08 -0800236 bool CheckAllClassesAreVerified(RedefinitionDataHolder& holder)
Alex Light8c889d22017-02-06 13:58:27 -0800237 REQUIRES_SHARED(art::Locks::mutator_lock_);
Alex Light1e3926a2017-04-07 10:38:06 -0700238 bool EnsureAllClassAllocationsFinished(RedefinitionDataHolder& holder)
239 REQUIRES_SHARED(art::Locks::mutator_lock_);
Alex Light0e692732017-01-10 15:00:05 -0800240 bool FinishAllRemainingAllocations(RedefinitionDataHolder& holder)
Alex Lighta01de592016-11-15 10:43:06 -0800241 REQUIRES_SHARED(art::Locks::mutator_lock_);
Alex Light0e692732017-01-10 15:00:05 -0800242 void ReleaseAllDexFiles() REQUIRES_SHARED(art::Locks::mutator_lock_);
Alex Light5643caf2017-02-08 11:39:07 -0800243 void UnregisterAllBreakpoints() REQUIRES_SHARED(art::Locks::mutator_lock_);
Alex Light1e3926a2017-04-07 10:38:06 -0700244 // Restores the old obsolete methods maps if it turns out they weren't needed (ie there were no
245 // new obsolete methods).
246 void RestoreObsoleteMethodMapsIfUnneeded(RedefinitionDataHolder& holder)
247 REQUIRES(art::Locks::mutator_lock_);
Alex Lighta01de592016-11-15 10:43:06 -0800248
Alex Light0e692732017-01-10 15:00:05 -0800249 void RecordFailure(jvmtiError result, const std::string& class_sig, const std::string& error_msg);
250 void RecordFailure(jvmtiError result, const std::string& error_msg) {
251 RecordFailure(result, "NO CLASS", error_msg);
Alex Lighta01de592016-11-15 10:43:06 -0800252 }
253
Alex Light0e692732017-01-10 15:00:05 -0800254 friend struct CallbackCtx;
Alex Lightc5f5a6e2017-03-01 16:57:08 -0800255 friend class RedefinitionDataHolder;
256 friend class RedefinitionDataIter;
Alex Lighta01de592016-11-15 10:43:06 -0800257};
258
259} // namespace openjdkjvmti
260
Andreas Gampe06c42a52017-07-26 14:17:14 -0700261#endif // ART_OPENJDKJVMTI_TI_REDEFINE_H_