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 
32 #ifndef ART_OPENJDKJVMTI_TI_REDEFINE_H_
33 #define ART_OPENJDKJVMTI_TI_REDEFINE_H_
34 
35 #include <functional>
36 #include <string>
37 
38 #include <jni.h>
39 
40 #include "art_field.h"
41 #include "art_jvmti.h"
42 #include "base/array_ref.h"
43 #include "base/globals.h"
44 #include "dex/class_accessor.h"
45 #include "dex/dex_file.h"
46 #include "dex/dex_file_structs.h"
47 #include "jni/jni_env_ext-inl.h"
48 #include "jvmti.h"
49 #include "mirror/array.h"
50 #include "mirror/class.h"
51 #include "mirror/dex_cache.h"
52 #include "obj_ptr.h"
53 
54 namespace art {
55 class ClassAccessor;
56 namespace dex {
57 struct ClassDef;
58 }  // namespace dex
59 }  // namespace art
60 
61 namespace openjdkjvmti {
62 
63 class ArtClassDefinition;
64 class RedefinitionDataHolder;
65 class RedefinitionDataIter;
66 
67 enum class RedefinitionType {
68   kStructural,
69   kNormal,
70 };
71 
72 // Class that can redefine a single class's methods.
73 class Redefiner {
74  public:
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.
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.
79   static jvmtiError RedefineClassesDirect(ArtJvmTiEnv* env,
80                                           art::Runtime* runtime,
81                                           art::Thread* self,
82                                           const std::vector<ArtClassDefinition>& definitions,
83                                           RedefinitionType type,
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.
88   // The caller is responsible for freeing it. The runtime makes its own copy of the data.
89   static jvmtiError RedefineClasses(jvmtiEnv* env,
90                                     jint class_count,
91                                     const jvmtiClassDefinition* definitions);
92   static jvmtiError StructurallyRedefineClasses(jvmtiEnv* env,
93                                                 jint class_count,
94                                                 const jvmtiClassDefinition* definitions);
95 
96   static jvmtiError IsModifiableClass(jvmtiEnv* env, jclass klass, jboolean* is_redefinable);
97   static jvmtiError IsStructurallyModifiableClass(jvmtiEnv* env,
98                                                   jclass klass,
99                                                   jboolean* is_redefinable);
100 
101   static art::MemMap MoveDataToMemMap(const std::string& original_location,
102                                       art::ArrayRef<const unsigned char> data,
103                                       std::string* error_msg);
104 
105   // Helper for checking if redefinition/retransformation is allowed.
106   template<RedefinitionType kType = RedefinitionType::kNormal>
107   static jvmtiError GetClassRedefinitionError(jclass klass, /*out*/std::string* error_msg)
108       REQUIRES(!art::Locks::mutator_lock_);
109 
110   static jvmtiError StructurallyRedefineClassDirect(jvmtiEnv* env,
111                                                     jclass klass,
112                                                     const unsigned char* data,
113                                                     jint data_size);
114 
115  private:
116   class ClassRedefinition {
117    public:
118     ClassRedefinition(Redefiner* driver,
119                       jclass klass,
120                       const art::DexFile* redefined_dex_file,
121                       const char* class_sig,
122                       art::ArrayRef<const unsigned char> orig_dex_file)
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 
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_;
135       other.driver_ = nullptr;
136       return *this;
137     }
138 
139     // Move constructor so we can put these into a vector.
ClassRedefinition(ClassRedefinition && other)140     ClassRedefinition(ClassRedefinition&& other)
141         : driver_(other.driver_),
142           klass_(other.klass_),
143           dex_file_(std::move(other.dex_file_)),
144           class_sig_(std::move(other.class_sig_)),
145           original_dex_file_(other.original_dex_file_) {
146       other.driver_ = nullptr;
147     }
148 
149     // No copy!
150     ClassRedefinition(ClassRedefinition&) = delete;
151     ClassRedefinition& operator=(ClassRedefinition&) = delete;
152 
153     art::ObjPtr<art::mirror::Class> GetMirrorClass() REQUIRES_SHARED(art::Locks::mutator_lock_);
154     art::ObjPtr<art::mirror::ClassLoader> GetClassLoader()
155         REQUIRES_SHARED(art::Locks::mutator_lock_);
156 
GetDexFile()157     const art::DexFile& GetDexFile() {
158       return *dex_file_;
159     }
160 
161     art::mirror::DexCache* CreateNewDexCache(art::Handle<art::mirror::ClassLoader> loader)
162         REQUIRES_SHARED(art::Locks::mutator_lock_);
163 
164     // This may return nullptr with a OOME pending if allocation fails.
165     art::mirror::Object* AllocateOrGetOriginalDexFile()
166         REQUIRES_SHARED(art::Locks::mutator_lock_);
167 
RecordFailure(jvmtiError e,const std::string & err)168     void RecordFailure(jvmtiError e, const std::string& err) {
169       driver_->RecordFailure(e, class_sig_, err);
170     }
171 
172     bool FinishRemainingCommonAllocations(/*out*/RedefinitionDataIter* cur_data)
173         REQUIRES_SHARED(art::Locks::mutator_lock_);
174 
175     bool FinishNewClassAllocations(RedefinitionDataHolder& holder,
176                                    /*out*/RedefinitionDataIter* cur_data)
177         REQUIRES_SHARED(art::Locks::mutator_lock_);
178     bool CollectAndCreateNewInstances(/*out*/RedefinitionDataIter* cur_data)
179         REQUIRES_SHARED(art::Locks::mutator_lock_);
180 
181     bool AllocateAndRememberNewDexFileCookie(
182         art::Handle<art::mirror::ClassLoader> source_class_loader,
183         art::Handle<art::mirror::Object> dex_file_obj,
184         /*out*/RedefinitionDataIter* cur_data)
185           REQUIRES_SHARED(art::Locks::mutator_lock_);
186 
187     void FindAndAllocateObsoleteMethods(art::ObjPtr<art::mirror::Class> art_klass)
188         REQUIRES(art::Locks::mutator_lock_);
189 
190     art::ObjPtr<art::mirror::Class> AllocateNewClassObject(
191         art::Handle<art::mirror::Class> old_class,
192         art::Handle<art::mirror::Class> super_class,
193         art::Handle<art::mirror::DexCache> cache,
194         uint16_t dex_class_def_index) REQUIRES_SHARED(art::Locks::mutator_lock_);
195     art::ObjPtr<art::mirror::Class> AllocateNewClassObject(art::Handle<art::mirror::DexCache> cache)
196         REQUIRES_SHARED(art::Locks::mutator_lock_);
197 
198     uint32_t GetNewClassSize(art::ClassAccessor& accessor)
199         REQUIRES_SHARED(art::Locks::mutator_lock_);
200 
201     // Checks that the dex file contains only the single expected class and that the top-level class
202     // data has not been modified in an incompatible manner.
203     bool CheckClass() REQUIRES_SHARED(art::Locks::mutator_lock_);
204 
205     // Checks that the contained class can be successfully verified.
206     bool CheckVerification(const RedefinitionDataIter& holder)
207         REQUIRES_SHARED(art::Locks::mutator_lock_);
208 
209     // Preallocates all needed allocations in klass so that we can pause execution safely.
210     bool EnsureClassAllocationsFinished(/*out*/RedefinitionDataIter* data)
211         REQUIRES_SHARED(art::Locks::mutator_lock_);
212 
213     // This will check that no constraints are violated (more than 1 class in dex file, any changes
214     // in number/declaration of methods & fields, changes in access flags, etc.)
215     bool CheckRedefinitionIsValid() REQUIRES_SHARED(art::Locks::mutator_lock_);
216 
217     // Checks that the class can even be redefined.
218     bool CheckRedefinable() REQUIRES_SHARED(art::Locks::mutator_lock_);
219 
220     // Checks that the dex file does not add/remove methods, or change their modifiers or types in
221     // illegal ways.
222     bool CheckMethods() REQUIRES_SHARED(art::Locks::mutator_lock_);
223 
224     // Checks that the dex file does not modify fields types or modifiers in illegal ways.
225     bool CheckFields() REQUIRES_SHARED(art::Locks::mutator_lock_);
226 
227     // Temporary check that a class undergoing structural redefinition has no instances. This
228     // requirement will be removed in time.
229     void UpdateJavaDexFile(art::ObjPtr<art::mirror::Object> java_dex_file,
230                            art::ObjPtr<art::mirror::LongArray> new_cookie)
231         REQUIRES(art::Locks::mutator_lock_);
232 
233     void UpdateFields(art::ObjPtr<art::mirror::Class> mclass)
234         REQUIRES(art::Locks::mutator_lock_);
235 
236     void UpdateMethods(art::ObjPtr<art::mirror::Class> mclass,
237                        const art::dex::ClassDef& class_def)
238         REQUIRES(art::Locks::mutator_lock_);
239 
240     void UpdateClass(const RedefinitionDataIter& cur_data)
241         REQUIRES(art::Locks::mutator_lock_);
242 
243     void UpdateClassCommon(const RedefinitionDataIter& cur_data)
244         REQUIRES(art::Locks::mutator_lock_);
245 
246     void ReverifyClass(const RedefinitionDataIter& cur_data)
247         REQUIRES_SHARED(art::Locks::mutator_lock_);
248 
249     void CollectNewFieldAndMethodMappings(const RedefinitionDataIter& data,
250                                           std::map<art::ArtMethod*, art::ArtMethod*>* method_map,
251                                           std::map<art::ArtField*, art::ArtField*>* field_map)
252         REQUIRES(art::Locks::mutator_lock_);
253 
254     void RestoreObsoleteMethodMapsIfUnneeded(const RedefinitionDataIter* cur_data)
255         REQUIRES(art::Locks::mutator_lock_);
256 
257     void ReleaseDexFile() REQUIRES_SHARED(art::Locks::mutator_lock_);
258 
259     // This should be done with all threads suspended.
260     void UnregisterJvmtiBreakpoints() REQUIRES_SHARED(art::Locks::mutator_lock_);
261 
262     void RecordNewMethodAdded();
263     void RecordNewFieldAdded();
RecordHasVirtualMembers()264     void RecordHasVirtualMembers() {
265       has_virtuals_ = true;
266     }
267 
HasVirtualMembers()268     bool HasVirtualMembers() const {
269       return has_virtuals_;
270     }
271 
IsStructuralRedefinition()272     bool IsStructuralRedefinition() const {
273       DCHECK(!(added_fields_ || added_methods_) || driver_->IsStructuralRedefinition())
274           << "added_fields_: " << added_fields_ << " added_methods_: " << added_methods_
275           << " driver_->IsStructuralRedefinition(): " << driver_->IsStructuralRedefinition();
276       return driver_->IsStructuralRedefinition() && (added_fields_ || added_methods_);
277     }
278 
279    private:
280     void UpdateClassStructurally(const RedefinitionDataIter& cur_data)
281         REQUIRES(art::Locks::mutator_lock_);
282 
283     void UpdateClassInPlace(const RedefinitionDataIter& cur_data)
284         REQUIRES(art::Locks::mutator_lock_);
285 
286     Redefiner* driver_;
287     jclass klass_;
288     std::unique_ptr<const art::DexFile> dex_file_;
289     std::string class_sig_;
290     art::ArrayRef<const unsigned char> original_dex_file_;
291 
292     bool added_fields_ = false;
293     bool added_methods_ = false;
294     bool has_virtuals_ = false;
295 
296     // Does the class need to be reverified due to verification soft-fails possibly forcing
297     // interpreter or lock-counting?
298     bool needs_reverify_ = false;
299   };
300 
301   ArtJvmTiEnv* env_;
302   jvmtiError result_;
303   art::Runtime* runtime_;
304   art::Thread* self_;
305   RedefinitionType type_;
306   std::vector<ClassRedefinition> redefinitions_;
307   // Kept as a jclass since we have weird run-state changes that make keeping it around as a
308   // mirror::Class difficult and confusing.
309   std::string* error_msg_;
310 
Redefiner(ArtJvmTiEnv * env,art::Runtime * runtime,art::Thread * self,RedefinitionType type,std::string * error_msg)311   Redefiner(ArtJvmTiEnv* env,
312             art::Runtime* runtime,
313             art::Thread* self,
314             RedefinitionType type,
315             std::string* error_msg)
316       : env_(env),
317         result_(ERR(INTERNAL)),
318         runtime_(runtime),
319         self_(self),
320         type_(type),
321         redefinitions_(),
322         error_msg_(error_msg) { }
323 
324   jvmtiError AddRedefinition(ArtJvmTiEnv* env, const ArtClassDefinition& def)
325       REQUIRES_SHARED(art::Locks::mutator_lock_);
326 
327   template<RedefinitionType kType = RedefinitionType::kNormal>
328   static jvmtiError RedefineClassesGeneric(jvmtiEnv* env,
329                                            jint class_count,
330                                            const jvmtiClassDefinition* definitions);
331 
332   template<RedefinitionType kType = RedefinitionType::kNormal>
333   static jvmtiError IsModifiableClassGeneric(jvmtiEnv* env, jclass klass, jboolean* is_redefinable);
334 
335   template<RedefinitionType kType = RedefinitionType::kNormal>
336   static jvmtiError GetClassRedefinitionError(art::Handle<art::mirror::Class> klass,
337                                               /*out*/std::string* error_msg)
338       REQUIRES_SHARED(art::Locks::mutator_lock_);
339 
340   jvmtiError Run() REQUIRES_SHARED(art::Locks::mutator_lock_);
341 
342   bool CheckAllRedefinitionAreValid() REQUIRES_SHARED(art::Locks::mutator_lock_);
343   bool CheckAllClassesAreVerified(RedefinitionDataHolder& holder)
344       REQUIRES_SHARED(art::Locks::mutator_lock_);
345   void MarkStructuralChanges(RedefinitionDataHolder& holder)
346       REQUIRES_SHARED(art::Locks::mutator_lock_);
347   bool EnsureAllClassAllocationsFinished(RedefinitionDataHolder& holder)
348       REQUIRES_SHARED(art::Locks::mutator_lock_);
349   bool FinishAllRemainingCommonAllocations(RedefinitionDataHolder& holder)
350       REQUIRES_SHARED(art::Locks::mutator_lock_);
351   bool FinishAllNewClassAllocations(RedefinitionDataHolder& holder)
352       REQUIRES_SHARED(art::Locks::mutator_lock_);
353   bool CollectAndCreateNewInstances(RedefinitionDataHolder& holder)
354       REQUIRES_SHARED(art::Locks::mutator_lock_);
355   void ReleaseAllDexFiles() REQUIRES_SHARED(art::Locks::mutator_lock_);
356   void ReverifyClasses(RedefinitionDataHolder& holder) REQUIRES_SHARED(art::Locks::mutator_lock_);
357   void UnregisterAllBreakpoints() REQUIRES_SHARED(art::Locks::mutator_lock_);
358   // Restores the old obsolete methods maps if it turns out they weren't needed (ie there were no
359   // new obsolete methods).
360   void RestoreObsoleteMethodMapsIfUnneeded(RedefinitionDataHolder& holder)
361       REQUIRES(art::Locks::mutator_lock_);
362 
IsStructuralRedefinition()363   bool IsStructuralRedefinition() const {
364     return type_ == RedefinitionType::kStructural;
365   }
366 
367   void RecordFailure(jvmtiError result, const std::string& class_sig, const std::string& error_msg);
RecordFailure(jvmtiError result,const std::string & error_msg)368   void RecordFailure(jvmtiError result, const std::string& error_msg) {
369     RecordFailure(result, "NO CLASS", error_msg);
370   }
371 
372   friend struct CallbackCtx;
373   friend class RedefinitionDataHolder;
374   friend class RedefinitionDataIter;
375 };
376 
377 }  // namespace openjdkjvmti
378 
379 #endif  // ART_OPENJDKJVMTI_TI_REDEFINE_H_
380