• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <string>
36 
37 #include <jni.h>
38 
39 #include "art_jvmti.h"
40 #include "base/array_ref.h"
41 #include "base/globals.h"
42 #include "jni/jni_env_ext-inl.h"
43 #include "jvmti.h"
44 #include "mirror/array.h"
45 #include "mirror/class.h"
46 #include "obj_ptr.h"
47 
48 namespace art {
49 namespace dex {
50 struct ClassDef;
51 }  // namespace dex
52 class DexFile;
53 }  // namespace art
54 
55 namespace openjdkjvmti {
56 
57 class ArtClassDefinition;
58 class RedefinitionDataHolder;
59 class RedefinitionDataIter;
60 
61 // Class that can redefine a single class's methods.
62 class Redefiner {
63  public:
64   // 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.
66   // 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.
68   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.
76   // The caller is responsible for freeing it. The runtime makes its own copy of the data.
77   static jvmtiError RedefineClasses(ArtJvmTiEnv* env,
78                                     EventHandler* event_handler,
79                                     art::Runtime* runtime,
80                                     art::Thread* self,
81                                     jint class_count,
82                                     const jvmtiClassDefinition* definitions,
83                                     /*out*/std::string* error_msg);
84 
85   static jvmtiError IsModifiableClass(jvmtiEnv* env, jclass klass, jboolean* is_redefinable);
86 
87   static art::MemMap MoveDataToMemMap(const std::string& original_location,
88                                       art::ArrayRef<const unsigned char> data,
89                                       std::string* error_msg);
90 
91   // 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 
95  private:
96   class ClassRedefinition {
97    public:
98     ClassRedefinition(Redefiner* driver,
99                       jclass klass,
100                       const art::DexFile* redefined_dex_file,
101                       const char* class_sig,
102                       art::ArrayRef<const unsigned char> orig_dex_file)
103       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.
ClassRedefinition(ClassRedefinition && other)109     ClassRedefinition(ClassRedefinition&& other)
110         : driver_(other.driver_),
111           klass_(other.klass_),
112           dex_file_(std::move(other.dex_file_)),
113           class_sig_(std::move(other.class_sig_)),
114           original_dex_file_(other.original_dex_file_) {
115       other.driver_ = nullptr;
116     }
117 
118     art::ObjPtr<art::mirror::Class> GetMirrorClass() REQUIRES_SHARED(art::Locks::mutator_lock_);
119     art::ObjPtr<art::mirror::ClassLoader> GetClassLoader()
120         REQUIRES_SHARED(art::Locks::mutator_lock_);
121 
GetDexFile()122     const art::DexFile& GetDexFile() {
123       return *dex_file_;
124     }
125 
126     art::mirror::DexCache* CreateNewDexCache(art::Handle<art::mirror::ClassLoader> loader)
127         REQUIRES_SHARED(art::Locks::mutator_lock_);
128 
129     // This may return nullptr with a OOME pending if allocation fails.
130     art::mirror::Object* AllocateOrGetOriginalDexFile()
131         REQUIRES_SHARED(art::Locks::mutator_lock_);
132 
RecordFailure(jvmtiError e,const std::string & err)133     void RecordFailure(jvmtiError e, const std::string& err) {
134       driver_->RecordFailure(e, class_sig_, err);
135     }
136 
137     bool FinishRemainingAllocations(/*out*/RedefinitionDataIter* cur_data)
138         REQUIRES_SHARED(art::Locks::mutator_lock_);
139 
140     bool AllocateAndRememberNewDexFileCookie(
141         art::Handle<art::mirror::ClassLoader> source_class_loader,
142         art::Handle<art::mirror::Object> dex_file_obj,
143         /*out*/RedefinitionDataIter* cur_data)
144           REQUIRES_SHARED(art::Locks::mutator_lock_);
145 
146     void FindAndAllocateObsoleteMethods(art::ObjPtr<art::mirror::Class> art_klass)
147         REQUIRES(art::Locks::mutator_lock_);
148 
149     // Checks that the dex file contains only the single expected class and that the top-level class
150     // data has not been modified in an incompatible manner.
151     bool CheckClass() REQUIRES_SHARED(art::Locks::mutator_lock_);
152 
153     // Checks that the contained class can be successfully verified.
154     bool CheckVerification(const RedefinitionDataIter& holder)
155         REQUIRES_SHARED(art::Locks::mutator_lock_);
156 
157     // Preallocates all needed allocations in klass so that we can pause execution safely.
158     bool EnsureClassAllocationsFinished(/*out*/RedefinitionDataIter* data)
159         REQUIRES_SHARED(art::Locks::mutator_lock_);
160 
161     // This will check that no constraints are violated (more than 1 class in dex file, any changes
162     // in number/declaration of methods & fields, changes in access flags, etc.)
163     bool CheckRedefinitionIsValid() REQUIRES_SHARED(art::Locks::mutator_lock_);
164 
165     // Checks that the class can even be redefined.
166     bool CheckRedefinable() REQUIRES_SHARED(art::Locks::mutator_lock_);
167 
168     // Checks that the dex file does not add/remove methods, or change their modifiers or types.
169     bool CheckSameMethods() REQUIRES_SHARED(art::Locks::mutator_lock_);
170 
171     // Checks that the dex file does not modify fields types or modifiers.
172     bool CheckSameFields() REQUIRES_SHARED(art::Locks::mutator_lock_);
173 
174     void UpdateJavaDexFile(art::ObjPtr<art::mirror::Object> java_dex_file,
175                            art::ObjPtr<art::mirror::LongArray> new_cookie)
176         REQUIRES(art::Locks::mutator_lock_);
177 
178     void UpdateFields(art::ObjPtr<art::mirror::Class> mclass)
179         REQUIRES(art::Locks::mutator_lock_);
180 
181     void UpdateMethods(art::ObjPtr<art::mirror::Class> mclass,
182                        const art::dex::ClassDef& class_def)
183         REQUIRES(art::Locks::mutator_lock_);
184 
185     void UpdateClass(art::ObjPtr<art::mirror::Class> mclass,
186                      art::ObjPtr<art::mirror::DexCache> new_dex_cache,
187                      art::ObjPtr<art::mirror::Object> original_dex_file)
188         REQUIRES(art::Locks::mutator_lock_);
189 
190     void RestoreObsoleteMethodMapsIfUnneeded(const RedefinitionDataIter* cur_data)
191         REQUIRES(art::Locks::mutator_lock_);
192 
193     void ReleaseDexFile() REQUIRES_SHARED(art::Locks::mutator_lock_);
194 
195     void UnregisterBreakpoints() REQUIRES_SHARED(art::Locks::mutator_lock_);
196     // This should be done with all threads suspended.
197     void UnregisterJvmtiBreakpoints() REQUIRES_SHARED(art::Locks::mutator_lock_);
198 
199    private:
200     Redefiner* driver_;
201     jclass klass_;
202     std::unique_ptr<const art::DexFile> dex_file_;
203     std::string class_sig_;
204     art::ArrayRef<const unsigned char> original_dex_file_;
205   };
206 
207   ArtJvmTiEnv* env_;
208   jvmtiError result_;
209   art::Runtime* runtime_;
210   art::Thread* self_;
211   std::vector<ClassRedefinition> redefinitions_;
212   // Kept as a jclass since we have weird run-state changes that make keeping it around as a
213   // mirror::Class difficult and confusing.
214   std::string* error_msg_;
215 
Redefiner(ArtJvmTiEnv * env,art::Runtime * runtime,art::Thread * self,std::string * error_msg)216   Redefiner(ArtJvmTiEnv* env,
217             art::Runtime* runtime,
218             art::Thread* self,
219             std::string* error_msg)
220       : env_(env),
221         result_(ERR(INTERNAL)),
222         runtime_(runtime),
223         self_(self),
224         redefinitions_(),
225         error_msg_(error_msg) { }
226 
227   jvmtiError AddRedefinition(ArtJvmTiEnv* env, const ArtClassDefinition& def)
228       REQUIRES_SHARED(art::Locks::mutator_lock_);
229 
230   static jvmtiError GetClassRedefinitionError(art::Handle<art::mirror::Class> klass,
231                                               /*out*/std::string* error_msg)
232       REQUIRES_SHARED(art::Locks::mutator_lock_);
233 
234   jvmtiError Run() REQUIRES_SHARED(art::Locks::mutator_lock_);
235 
236   bool CheckAllRedefinitionAreValid() REQUIRES_SHARED(art::Locks::mutator_lock_);
237   bool CheckAllClassesAreVerified(RedefinitionDataHolder& holder)
238       REQUIRES_SHARED(art::Locks::mutator_lock_);
239   bool EnsureAllClassAllocationsFinished(RedefinitionDataHolder& holder)
240       REQUIRES_SHARED(art::Locks::mutator_lock_);
241   bool FinishAllRemainingAllocations(RedefinitionDataHolder& holder)
242       REQUIRES_SHARED(art::Locks::mutator_lock_);
243   void ReleaseAllDexFiles() REQUIRES_SHARED(art::Locks::mutator_lock_);
244   void UnregisterAllBreakpoints() REQUIRES_SHARED(art::Locks::mutator_lock_);
245   // Restores the old obsolete methods maps if it turns out they weren't needed (ie there were no
246   // new obsolete methods).
247   void RestoreObsoleteMethodMapsIfUnneeded(RedefinitionDataHolder& holder)
248       REQUIRES(art::Locks::mutator_lock_);
249 
250   void RecordFailure(jvmtiError result, const std::string& class_sig, const std::string& error_msg);
RecordFailure(jvmtiError result,const std::string & error_msg)251   void RecordFailure(jvmtiError result, const std::string& error_msg) {
252     RecordFailure(result, "NO CLASS", error_msg);
253   }
254 
255   friend struct CallbackCtx;
256   friend class RedefinitionDataHolder;
257   friend class RedefinitionDataIter;
258 };
259 
260 }  // namespace openjdkjvmti
261 
262 #endif  // ART_OPENJDKJVMTI_TI_REDEFINE_H_
263