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 CanRedefineClass(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) noexcept { 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) noexcept 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 CollectNewFieldAndMethodMappings(const RedefinitionDataIter& data, 244 std::map<art::ArtMethod*, art::ArtMethod*>* method_map, 245 std::map<art::ArtField*, art::ArtField*>* field_map) 246 REQUIRES(art::Locks::mutator_lock_); 247 248 void RestoreObsoleteMethodMapsIfUnneeded(const RedefinitionDataIter* cur_data) 249 REQUIRES(art::Locks::mutator_lock_); 250 251 void ReleaseDexFile() REQUIRES_SHARED(art::Locks::mutator_lock_); 252 253 // This should be done with all threads suspended. 254 void UnregisterJvmtiBreakpoints() REQUIRES_SHARED(art::Locks::mutator_lock_); 255 256 void RecordNewMethodAdded(); 257 void RecordNewFieldAdded(); RecordHasVirtualMembers()258 void RecordHasVirtualMembers() { 259 has_virtuals_ = true; 260 } 261 HasVirtualMembers()262 bool HasVirtualMembers() const { 263 return has_virtuals_; 264 } 265 IsStructuralRedefinition()266 bool IsStructuralRedefinition() const { 267 DCHECK(!(added_fields_ || added_methods_) || driver_->IsStructuralRedefinition()) 268 << "added_fields_: " << added_fields_ << " added_methods_: " << added_methods_ 269 << " driver_->IsStructuralRedefinition(): " << driver_->IsStructuralRedefinition(); 270 return driver_->IsStructuralRedefinition() && (added_fields_ || added_methods_); 271 } 272 273 private: 274 void UpdateClassStructurally(const RedefinitionDataIter& cur_data) 275 REQUIRES(art::Locks::mutator_lock_); 276 277 void UpdateClassInPlace(const RedefinitionDataIter& cur_data) 278 REQUIRES(art::Locks::mutator_lock_); 279 280 Redefiner* driver_; 281 jclass klass_; 282 std::unique_ptr<const art::DexFile> dex_file_; 283 std::string class_sig_; 284 art::ArrayRef<const unsigned char> original_dex_file_; 285 286 bool added_fields_ = false; 287 bool added_methods_ = false; 288 bool has_virtuals_ = false; 289 }; 290 291 ArtJvmTiEnv* env_; 292 jvmtiError result_; 293 art::Runtime* runtime_; 294 art::Thread* self_; 295 RedefinitionType type_; 296 std::vector<ClassRedefinition> redefinitions_; 297 // Kept as a jclass since we have weird run-state changes that make keeping it around as a 298 // mirror::Class difficult and confusing. 299 std::string* error_msg_; 300 Redefiner(ArtJvmTiEnv * env,art::Runtime * runtime,art::Thread * self,RedefinitionType type,std::string * error_msg)301 Redefiner(ArtJvmTiEnv* env, 302 art::Runtime* runtime, 303 art::Thread* self, 304 RedefinitionType type, 305 std::string* error_msg) 306 : env_(env), 307 result_(ERR(INTERNAL)), 308 runtime_(runtime), 309 self_(self), 310 type_(type), 311 redefinitions_(), 312 error_msg_(error_msg) { } 313 314 jvmtiError AddRedefinition(ArtJvmTiEnv* env, const ArtClassDefinition& def) 315 REQUIRES_SHARED(art::Locks::mutator_lock_); 316 317 template<RedefinitionType kType = RedefinitionType::kNormal> 318 static jvmtiError RedefineClassesGeneric(jvmtiEnv* env, 319 jint class_count, 320 const jvmtiClassDefinition* definitions); 321 322 template<RedefinitionType kType = RedefinitionType::kNormal> 323 static jvmtiError IsModifiableClassGeneric(jvmtiEnv* env, jclass klass, jboolean* is_redefinable); 324 325 template <RedefinitionType kType = RedefinitionType::kNormal> 326 static jvmtiError CanRedefineClass(art::Handle<art::mirror::Class> klass, 327 /*out*/ std::string* error_msg) 328 REQUIRES_SHARED(art::Locks::mutator_lock_); 329 330 jvmtiError Run() REQUIRES_SHARED(art::Locks::mutator_lock_); 331 332 bool CheckAllRedefinitionAreValid() REQUIRES_SHARED(art::Locks::mutator_lock_); 333 bool CheckAllClassesAreVerified(RedefinitionDataHolder& holder) 334 REQUIRES_SHARED(art::Locks::mutator_lock_); 335 void MarkStructuralChanges(RedefinitionDataHolder& holder) 336 REQUIRES_SHARED(art::Locks::mutator_lock_); 337 bool EnsureAllClassAllocationsFinished(RedefinitionDataHolder& holder) 338 REQUIRES_SHARED(art::Locks::mutator_lock_); 339 bool FinishAllRemainingCommonAllocations(RedefinitionDataHolder& holder) 340 REQUIRES_SHARED(art::Locks::mutator_lock_); 341 bool FinishAllNewClassAllocations(RedefinitionDataHolder& holder) 342 REQUIRES_SHARED(art::Locks::mutator_lock_); 343 bool CollectAndCreateNewInstances(RedefinitionDataHolder& holder) 344 REQUIRES_SHARED(art::Locks::mutator_lock_); 345 void ReleaseAllDexFiles() REQUIRES_SHARED(art::Locks::mutator_lock_); 346 void ReverifyClasses(RedefinitionDataHolder& holder) REQUIRES_SHARED(art::Locks::mutator_lock_); 347 void UnregisterAllBreakpoints() REQUIRES_SHARED(art::Locks::mutator_lock_); 348 // Restores the old obsolete methods maps if it turns out they weren't needed (ie there were no 349 // new obsolete methods). 350 void RestoreObsoleteMethodMapsIfUnneeded(RedefinitionDataHolder& holder) 351 REQUIRES(art::Locks::mutator_lock_); 352 IsStructuralRedefinition()353 bool IsStructuralRedefinition() const { 354 return type_ == RedefinitionType::kStructural; 355 } 356 357 void RecordFailure(jvmtiError result, const std::string& class_sig, const std::string& error_msg); RecordFailure(jvmtiError result,const std::string & error_msg)358 void RecordFailure(jvmtiError result, const std::string& error_msg) { 359 RecordFailure(result, "NO CLASS", error_msg); 360 } 361 362 friend struct CallbackCtx; 363 friend class RedefinitionDataHolder; 364 friend class RedefinitionDataIter; 365 }; 366 367 } // namespace openjdkjvmti 368 369 #endif // ART_OPENJDKJVMTI_TI_REDEFINE_H_ 370