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_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" 41 #include "base/array_slice.h" 42 #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" 60 #include "ti_class_definition.h" 61 #include "thread_list.h" 62 #include "transform.h" 63 #include "utf.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::ArraySlice<const unsigned char> data, 99 std::string* error_msg); 100 101 private: 102 class ClassRedefinition { 103 public: 104 ClassRedefinition(Redefiner* driver, 105 jclass klass, 106 const art::DexFile* redefined_dex_file, 107 const char* class_sig, 108 art::ArraySlice<const unsigned char> orig_dex_file) 109 REQUIRES_SHARED(art::Locks::mutator_lock_); 110 111 // NO_THREAD_SAFETY_ANALYSIS so we can unlock the class in the destructor. 112 ~ClassRedefinition() NO_THREAD_SAFETY_ANALYSIS; 113 114 // Move constructor so we can put these into a vector. 115 ClassRedefinition(ClassRedefinition&& other) 116 : driver_(other.driver_), 117 klass_(other.klass_), 118 dex_file_(std::move(other.dex_file_)), 119 class_sig_(std::move(other.class_sig_)), 120 original_dex_file_(other.original_dex_file_) { 121 other.driver_ = nullptr; 122 } 123 124 art::mirror::Class* GetMirrorClass() REQUIRES_SHARED(art::Locks::mutator_lock_); 125 art::mirror::ClassLoader* GetClassLoader() REQUIRES_SHARED(art::Locks::mutator_lock_); 126 127 const art::DexFile& GetDexFile() { 128 return *dex_file_; 129 } 130 131 art::mirror::DexCache* CreateNewDexCache(art::Handle<art::mirror::ClassLoader> loader) 132 REQUIRES_SHARED(art::Locks::mutator_lock_); 133 134 // This may return nullptr with a OOME pending if allocation fails. 135 art::mirror::Object* AllocateOrGetOriginalDexFile() 136 REQUIRES_SHARED(art::Locks::mutator_lock_); 137 138 void RecordFailure(jvmtiError e, const std::string& err) { 139 driver_->RecordFailure(e, class_sig_, err); 140 } 141 142 bool FinishRemainingAllocations(/*out*/RedefinitionDataIter* cur_data) 143 REQUIRES_SHARED(art::Locks::mutator_lock_); 144 145 bool AllocateAndRememberNewDexFileCookie( 146 art::Handle<art::mirror::ClassLoader> source_class_loader, 147 art::Handle<art::mirror::Object> dex_file_obj, 148 /*out*/RedefinitionDataIter* cur_data) 149 REQUIRES_SHARED(art::Locks::mutator_lock_); 150 151 void FindAndAllocateObsoleteMethods(art::mirror::Class* art_klass) 152 REQUIRES(art::Locks::mutator_lock_); 153 154 // Checks that the dex file contains only the single expected class and that the top-level class 155 // data has not been modified in an incompatible manner. 156 bool CheckClass() REQUIRES_SHARED(art::Locks::mutator_lock_); 157 158 // Checks that the contained class can be successfully verified. 159 bool CheckVerification(const RedefinitionDataIter& holder) 160 REQUIRES_SHARED(art::Locks::mutator_lock_); 161 162 // Preallocates all needed allocations in klass so that we can pause execution safely. 163 bool EnsureClassAllocationsFinished(/*out*/RedefinitionDataIter* data) 164 REQUIRES_SHARED(art::Locks::mutator_lock_); 165 166 // This will check that no constraints are violated (more than 1 class in dex file, any changes 167 // in number/declaration of methods & fields, changes in access flags, etc.) 168 bool CheckRedefinitionIsValid() REQUIRES_SHARED(art::Locks::mutator_lock_); 169 170 // Checks that the class can even be redefined. 171 bool CheckRedefinable() REQUIRES_SHARED(art::Locks::mutator_lock_); 172 173 // Checks that the dex file does not add/remove methods, or change their modifiers or types. 174 bool CheckSameMethods() REQUIRES_SHARED(art::Locks::mutator_lock_); 175 176 // Checks that the dex file does not modify fields types or modifiers. 177 bool CheckSameFields() REQUIRES_SHARED(art::Locks::mutator_lock_); 178 179 void UpdateJavaDexFile(art::ObjPtr<art::mirror::Object> java_dex_file, 180 art::ObjPtr<art::mirror::LongArray> new_cookie) 181 REQUIRES(art::Locks::mutator_lock_); 182 183 void UpdateFields(art::ObjPtr<art::mirror::Class> mclass) 184 REQUIRES(art::Locks::mutator_lock_); 185 186 void UpdateMethods(art::ObjPtr<art::mirror::Class> mclass, 187 art::ObjPtr<art::mirror::DexCache> new_dex_cache, 188 const art::DexFile::ClassDef& class_def) 189 REQUIRES(art::Locks::mutator_lock_); 190 191 void UpdateClass(art::ObjPtr<art::mirror::Class> mclass, 192 art::ObjPtr<art::mirror::DexCache> new_dex_cache, 193 art::ObjPtr<art::mirror::Object> original_dex_file) 194 REQUIRES(art::Locks::mutator_lock_); 195 196 void RestoreObsoleteMethodMapsIfUnneeded(const RedefinitionDataIter* cur_data) 197 REQUIRES(art::Locks::mutator_lock_); 198 199 void ReleaseDexFile() REQUIRES_SHARED(art::Locks::mutator_lock_); 200 201 void UnregisterBreakpoints() REQUIRES_SHARED(art::Locks::mutator_lock_); 202 203 private: 204 Redefiner* driver_; 205 jclass klass_; 206 std::unique_ptr<const art::DexFile> dex_file_; 207 std::string class_sig_; 208 art::ArraySlice<const unsigned char> original_dex_file_; 209 }; 210 211 jvmtiError result_; 212 art::Runtime* runtime_; 213 art::Thread* self_; 214 std::vector<ClassRedefinition> redefinitions_; 215 // Kept as a jclass since we have weird run-state changes that make keeping it around as a 216 // mirror::Class difficult and confusing. 217 std::string* error_msg_; 218 219 Redefiner(art::Runtime* runtime, 220 art::Thread* self, 221 std::string* error_msg) 222 : result_(ERR(INTERNAL)), 223 runtime_(runtime), 224 self_(self), 225 redefinitions_(), 226 error_msg_(error_msg) { } 227 228 jvmtiError AddRedefinition(ArtJvmTiEnv* env, const ArtClassDefinition& def) 229 REQUIRES_SHARED(art::Locks::mutator_lock_); 230 231 static jvmtiError GetClassRedefinitionError(art::Handle<art::mirror::Class> klass, 232 /*out*/std::string* error_msg) 233 REQUIRES_SHARED(art::Locks::mutator_lock_); 234 235 jvmtiError Run() REQUIRES_SHARED(art::Locks::mutator_lock_); 236 237 bool CheckAllRedefinitionAreValid() REQUIRES_SHARED(art::Locks::mutator_lock_); 238 bool CheckAllClassesAreVerified(RedefinitionDataHolder& holder) 239 REQUIRES_SHARED(art::Locks::mutator_lock_); 240 bool EnsureAllClassAllocationsFinished(RedefinitionDataHolder& holder) 241 REQUIRES_SHARED(art::Locks::mutator_lock_); 242 bool FinishAllRemainingAllocations(RedefinitionDataHolder& holder) 243 REQUIRES_SHARED(art::Locks::mutator_lock_); 244 void ReleaseAllDexFiles() REQUIRES_SHARED(art::Locks::mutator_lock_); 245 void UnregisterAllBreakpoints() REQUIRES_SHARED(art::Locks::mutator_lock_); 246 // Restores the old obsolete methods maps if it turns out they weren't needed (ie there were no 247 // new obsolete methods). 248 void RestoreObsoleteMethodMapsIfUnneeded(RedefinitionDataHolder& holder) 249 REQUIRES(art::Locks::mutator_lock_); 250 251 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); 254 } 255 256 friend struct CallbackCtx; 257 friend class RedefinitionDataHolder; 258 friend class RedefinitionDataIter; 259 }; 260 261 } // namespace openjdkjvmti 262 263 #endif // ART_RUNTIME_OPENJDKJVMTI_TI_REDEFINE_H_ 264