blob: 4313a9476e307101e5165b4e9d6c961d59d78307 [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
32#ifndef ART_RUNTIME_OPENJDKJVMTI_TI_REDEFINE_H_
33#define ART_RUNTIME_OPENJDKJVMTI_TI_REDEFINE_H_
34
35#include <string>
36
37#include <jni.h>
38
39#include "art_jvmti.h"
40#include "art_method.h"
Alex Lighta7e38d82017-01-19 14:57:28 -080041#include "base/array_slice.h"
Alex Lighta01de592016-11-15 10:43:06 -080042#include "class_linker.h"
43#include "dex_file.h"
44#include "gc_root-inl.h"
45#include "globals.h"
46#include "jni_env_ext-inl.h"
47#include "jvmti.h"
48#include "linear_alloc.h"
49#include "mem_map.h"
50#include "mirror/array-inl.h"
51#include "mirror/array.h"
52#include "mirror/class-inl.h"
53#include "mirror/class.h"
54#include "mirror/class_loader-inl.h"
55#include "mirror/string-inl.h"
56#include "oat_file.h"
57#include "obj_ptr.h"
58#include "scoped_thread_state_change-inl.h"
59#include "stack.h"
Alex Lighta7e38d82017-01-19 14:57:28 -080060#include "ti_class_definition.h"
Alex Lighta01de592016-11-15 10:43:06 -080061#include "thread_list.h"
62#include "transform.h"
63#include "utf.h"
64#include "utils/dex_cache_arrays_layout-inl.h"
65
66namespace openjdkjvmti {
67
Alex Light0e692732017-01-10 15:00:05 -080068class RedefinitionDataHolder;
Alex Lightc5f5a6e2017-03-01 16:57:08 -080069class RedefinitionDataIter;
Alex Light0e692732017-01-10 15:00:05 -080070
Alex Lighta01de592016-11-15 10:43:06 -080071// Class that can redefine a single class's methods.
Alex Lightdba61482016-12-21 08:20:29 -080072// TODO We should really make this be driven by an outside class so we can do multiple classes at
73// the same time and have less required cleanup.
Alex Lighta01de592016-11-15 10:43:06 -080074class Redefiner {
75 public:
Alex Light0e692732017-01-10 15:00:05 -080076 // Redefine the given classes with the given dex data. Note this function does not take ownership
77 // 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 -080078 // The caller is responsible for freeing it. The runtime makes its own copy of the data. This
79 // function does not call the transformation events.
80 // TODO Check modified flag of the definitions.
81 static jvmtiError RedefineClassesDirect(ArtJvmTiEnv* env,
82 art::Runtime* runtime,
83 art::Thread* self,
84 const std::vector<ArtClassDefinition>& definitions,
85 /*out*/std::string* error_msg);
86
87 // Redefine the given classes with the given dex data. Note this function does not take ownership
88 // 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 -080089 // The caller is responsible for freeing it. The runtime makes its own copy of the data.
Alex Light6ac57502017-01-19 15:05:06 -080090 // TODO This function should call the transformation events.
Alex Light0e692732017-01-10 15:00:05 -080091 static jvmtiError RedefineClasses(ArtJvmTiEnv* env,
Andreas Gampede19eb92017-02-24 16:21:18 -080092 EventHandler* event_handler,
Alex Light0e692732017-01-10 15:00:05 -080093 art::Runtime* runtime,
94 art::Thread* self,
95 jint class_count,
96 const jvmtiClassDefinition* definitions,
Alex Light6ac57502017-01-19 15:05:06 -080097 /*out*/std::string* error_msg);
Alex Lighta01de592016-11-15 10:43:06 -080098
Alex Lighte4a88632017-01-10 07:41:24 -080099 static jvmtiError IsModifiableClass(jvmtiEnv* env, jclass klass, jboolean* is_redefinable);
100
Alex Light440b5d92017-01-24 15:32:25 -0800101 static std::unique_ptr<art::MemMap> MoveDataToMemMap(const std::string& original_location,
102 jint data_len,
103 const unsigned char* dex_data,
104 std::string* error_msg);
105
Alex Lighta01de592016-11-15 10:43:06 -0800106 private:
Alex Light0e692732017-01-10 15:00:05 -0800107 class ClassRedefinition {
108 public:
109 ClassRedefinition(Redefiner* driver,
110 jclass klass,
111 const art::DexFile* redefined_dex_file,
Alex Lighta7e38d82017-01-19 14:57:28 -0800112 const char* class_sig,
113 art::ArraySlice<const unsigned char> orig_dex_file)
Alex Light0e692732017-01-10 15:00:05 -0800114 REQUIRES_SHARED(art::Locks::mutator_lock_);
115
116 // NO_THREAD_SAFETY_ANALYSIS so we can unlock the class in the destructor.
117 ~ClassRedefinition() NO_THREAD_SAFETY_ANALYSIS;
118
119 // Move constructor so we can put these into a vector.
120 ClassRedefinition(ClassRedefinition&& other)
121 : driver_(other.driver_),
122 klass_(other.klass_),
123 dex_file_(std::move(other.dex_file_)),
Alex Lighta7e38d82017-01-19 14:57:28 -0800124 class_sig_(std::move(other.class_sig_)),
125 original_dex_file_(other.original_dex_file_) {
Alex Light0e692732017-01-10 15:00:05 -0800126 other.driver_ = nullptr;
127 }
128
129 art::mirror::Class* GetMirrorClass() REQUIRES_SHARED(art::Locks::mutator_lock_);
130 art::mirror::ClassLoader* GetClassLoader() REQUIRES_SHARED(art::Locks::mutator_lock_);
131
Alex Light7916f202017-01-27 09:00:15 -0800132 const art::DexFile& GetDexFile() {
133 return *dex_file_;
134 }
135
Alex Light0e692732017-01-10 15:00:05 -0800136 art::mirror::DexCache* CreateNewDexCache(art::Handle<art::mirror::ClassLoader> loader)
137 REQUIRES_SHARED(art::Locks::mutator_lock_);
138
Alex Lighta7e38d82017-01-19 14:57:28 -0800139 // This may return nullptr with a OOME pending if allocation fails.
140 art::mirror::ByteArray* AllocateOrGetOriginalDexFileBytes()
141 REQUIRES_SHARED(art::Locks::mutator_lock_);
142
Alex Light0e692732017-01-10 15:00:05 -0800143 void RecordFailure(jvmtiError e, const std::string& err) {
144 driver_->RecordFailure(e, class_sig_, err);
145 }
146
Alex Lightc5f5a6e2017-03-01 16:57:08 -0800147 bool FinishRemainingAllocations(/*out*/RedefinitionDataIter* cur_data)
Alex Light0e692732017-01-10 15:00:05 -0800148 REQUIRES_SHARED(art::Locks::mutator_lock_);
149
Alex Light1babae02017-02-01 15:35:34 -0800150 bool AllocateAndRememberNewDexFileCookie(
Alex Light1babae02017-02-01 15:35:34 -0800151 art::Handle<art::mirror::ClassLoader> source_class_loader,
152 art::Handle<art::mirror::Object> dex_file_obj,
Alex Lightc5f5a6e2017-03-01 16:57:08 -0800153 /*out*/RedefinitionDataIter* cur_data)
Alex Light1babae02017-02-01 15:35:34 -0800154 REQUIRES_SHARED(art::Locks::mutator_lock_);
155
Alex Light0e692732017-01-10 15:00:05 -0800156 void FindAndAllocateObsoleteMethods(art::mirror::Class* art_klass)
157 REQUIRES(art::Locks::mutator_lock_);
158
Alex Light0e692732017-01-10 15:00:05 -0800159 // Checks that the dex file contains only the single expected class and that the top-level class
160 // data has not been modified in an incompatible manner.
161 bool CheckClass() REQUIRES_SHARED(art::Locks::mutator_lock_);
162
Alex Light8c889d22017-02-06 13:58:27 -0800163 // Checks that the contained class can be successfully verified.
Alex Lightc5f5a6e2017-03-01 16:57:08 -0800164 bool CheckVerification(const RedefinitionDataIter& holder)
Alex Light8c889d22017-02-06 13:58:27 -0800165 REQUIRES_SHARED(art::Locks::mutator_lock_);
166
Alex Light0e692732017-01-10 15:00:05 -0800167 // Preallocates all needed allocations in klass so that we can pause execution safely.
168 // TODO We should be able to free the arrays if they end up not being used. Investigate doing
169 // this in the future. For now we will just take the memory hit.
170 bool EnsureClassAllocationsFinished() REQUIRES_SHARED(art::Locks::mutator_lock_);
171
172 // This will check that no constraints are violated (more than 1 class in dex file, any changes
173 // in number/declaration of methods & fields, changes in access flags, etc.)
174 bool CheckRedefinitionIsValid() REQUIRES_SHARED(art::Locks::mutator_lock_);
175
176 // Checks that the class can even be redefined.
177 bool CheckRedefinable() REQUIRES_SHARED(art::Locks::mutator_lock_);
178
Alex Light6161f132017-01-25 10:30:20 -0800179 // Checks that the dex file does not add/remove methods, or change their modifiers or types.
180 bool CheckSameMethods() REQUIRES_SHARED(art::Locks::mutator_lock_);
Alex Light0e692732017-01-10 15:00:05 -0800181
Alex Light6161f132017-01-25 10:30:20 -0800182 // Checks that the dex file does not modify fields types or modifiers.
183 bool CheckSameFields() REQUIRES_SHARED(art::Locks::mutator_lock_);
Alex Light0e692732017-01-10 15:00:05 -0800184
185 void UpdateJavaDexFile(art::ObjPtr<art::mirror::Object> java_dex_file,
186 art::ObjPtr<art::mirror::LongArray> new_cookie)
187 REQUIRES(art::Locks::mutator_lock_);
188
189 void UpdateFields(art::ObjPtr<art::mirror::Class> mclass)
190 REQUIRES(art::Locks::mutator_lock_);
191
192 void UpdateMethods(art::ObjPtr<art::mirror::Class> mclass,
193 art::ObjPtr<art::mirror::DexCache> new_dex_cache,
194 const art::DexFile::ClassDef& class_def)
195 REQUIRES(art::Locks::mutator_lock_);
196
197 void UpdateClass(art::ObjPtr<art::mirror::Class> mclass,
Alex Lighta7e38d82017-01-19 14:57:28 -0800198 art::ObjPtr<art::mirror::DexCache> new_dex_cache,
199 art::ObjPtr<art::mirror::ByteArray> original_dex_file)
Alex Light0e692732017-01-10 15:00:05 -0800200 REQUIRES(art::Locks::mutator_lock_);
201
202 void ReleaseDexFile() REQUIRES_SHARED(art::Locks::mutator_lock_);
203
Alex Light5643caf2017-02-08 11:39:07 -0800204 void UnregisterBreakpoints() REQUIRES_SHARED(art::Locks::mutator_lock_);
205
Alex Light0e692732017-01-10 15:00:05 -0800206 private:
207 Redefiner* driver_;
208 jclass klass_;
209 std::unique_ptr<const art::DexFile> dex_file_;
210 std::string class_sig_;
Alex Lighta7e38d82017-01-19 14:57:28 -0800211 art::ArraySlice<const unsigned char> original_dex_file_;
Alex Light0e692732017-01-10 15:00:05 -0800212 };
213
Alex Lighta01de592016-11-15 10:43:06 -0800214 jvmtiError result_;
215 art::Runtime* runtime_;
216 art::Thread* self_;
Alex Light0e692732017-01-10 15:00:05 -0800217 std::vector<ClassRedefinition> redefinitions_;
Alex Lighta01de592016-11-15 10:43:06 -0800218 // Kept as a jclass since we have weird run-state changes that make keeping it around as a
219 // mirror::Class difficult and confusing.
Alex Lighta01de592016-11-15 10:43:06 -0800220 std::string* error_msg_;
Alex Lighta01de592016-11-15 10:43:06 -0800221
222 // TODO Maybe change jclass to a mirror::Class
223 Redefiner(art::Runtime* runtime,
224 art::Thread* self,
Alex Lighta01de592016-11-15 10:43:06 -0800225 std::string* error_msg)
226 : result_(ERR(INTERNAL)),
227 runtime_(runtime),
228 self_(self),
Alex Light0e692732017-01-10 15:00:05 -0800229 redefinitions_(),
230 error_msg_(error_msg) { }
231
Alex Light6ac57502017-01-19 15:05:06 -0800232 jvmtiError AddRedefinition(ArtJvmTiEnv* env, const ArtClassDefinition& def)
Alex Light0e692732017-01-10 15:00:05 -0800233 REQUIRES_SHARED(art::Locks::mutator_lock_);
Alex Lighta01de592016-11-15 10:43:06 -0800234
Alex Lighte4a88632017-01-10 07:41:24 -0800235 static jvmtiError GetClassRedefinitionError(art::Handle<art::mirror::Class> klass,
236 /*out*/std::string* error_msg)
237 REQUIRES_SHARED(art::Locks::mutator_lock_);
238
Alex Lighta01de592016-11-15 10:43:06 -0800239 // TODO Put on all the lock qualifiers.
240 jvmtiError Run() REQUIRES_SHARED(art::Locks::mutator_lock_);
241
Alex Light0e692732017-01-10 15:00:05 -0800242 bool CheckAllRedefinitionAreValid() REQUIRES_SHARED(art::Locks::mutator_lock_);
Alex Lightc5f5a6e2017-03-01 16:57:08 -0800243 bool CheckAllClassesAreVerified(RedefinitionDataHolder& holder)
Alex Light8c889d22017-02-06 13:58:27 -0800244 REQUIRES_SHARED(art::Locks::mutator_lock_);
Alex Light0e692732017-01-10 15:00:05 -0800245 bool EnsureAllClassAllocationsFinished() REQUIRES_SHARED(art::Locks::mutator_lock_);
246 bool FinishAllRemainingAllocations(RedefinitionDataHolder& holder)
Alex Lighta01de592016-11-15 10:43:06 -0800247 REQUIRES_SHARED(art::Locks::mutator_lock_);
Alex Light0e692732017-01-10 15:00:05 -0800248 void ReleaseAllDexFiles() REQUIRES_SHARED(art::Locks::mutator_lock_);
Alex Light5643caf2017-02-08 11:39:07 -0800249 void UnregisterAllBreakpoints() REQUIRES_SHARED(art::Locks::mutator_lock_);
Alex Lighta01de592016-11-15 10:43:06 -0800250
Alex Light0e692732017-01-10 15:00:05 -0800251 void RecordFailure(jvmtiError result, const std::string& class_sig, const std::string& error_msg);
252 void RecordFailure(jvmtiError result, const std::string& error_msg) {
253 RecordFailure(result, "NO CLASS", error_msg);
Alex Lighta01de592016-11-15 10:43:06 -0800254 }
255
Alex Light0e692732017-01-10 15:00:05 -0800256 friend struct CallbackCtx;
Alex Lightc5f5a6e2017-03-01 16:57:08 -0800257 friend class RedefinitionDataHolder;
258 friend class RedefinitionDataIter;
Alex Lighta01de592016-11-15 10:43:06 -0800259};
260
261} // namespace openjdkjvmti
262
263#endif // ART_RUNTIME_OPENJDKJVMTI_TI_REDEFINE_H_