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