1 /* 2 * Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "cds/cdsConfig.hpp" 27 #include "cds/metaspaceShared.hpp" 28 #include "classfile/classFileStream.hpp" 29 #include "classfile/classLoaderDataGraph.hpp" 30 #include "classfile/classLoadInfo.hpp" 31 #include "classfile/javaClasses.inline.hpp" 32 #include "classfile/metadataOnStackMark.hpp" 33 #include "classfile/symbolTable.hpp" 34 #include "classfile/klassFactory.hpp" 35 #include "classfile/verifier.hpp" 36 #include "classfile/vmClasses.hpp" 37 #include "classfile/vmSymbols.hpp" 38 #include "code/codeCache.hpp" 39 #include "compiler/compileBroker.hpp" 40 #include "interpreter/oopMapCache.hpp" 41 #include "interpreter/rewriter.hpp" 42 #include "jfr/jfrEvents.hpp" 43 #include "logging/logStream.hpp" 44 #include "memory/metadataFactory.hpp" 45 #include "memory/resourceArea.hpp" 46 #include "memory/universe.hpp" 47 #include "oops/annotations.hpp" 48 #include "oops/constantPool.hpp" 49 #include "oops/fieldStreams.inline.hpp" 50 #include "oops/klass.inline.hpp" 51 #include "oops/klassVtable.hpp" 52 #include "oops/method.hpp" 53 #include "oops/oop.inline.hpp" 54 #include "oops/recordComponent.hpp" 55 #include "prims/jvmtiImpl.hpp" 56 #include "prims/jvmtiRedefineClasses.hpp" 57 #include "prims/jvmtiThreadState.inline.hpp" 58 #include "prims/resolvedMethodTable.hpp" 59 #include "prims/methodComparator.hpp" 60 #include "runtime/atomic.hpp" 61 #include "runtime/deoptimization.hpp" 62 #include "runtime/handles.inline.hpp" 63 #include "runtime/jniHandles.inline.hpp" 64 #include "runtime/relocator.hpp" 65 #include "runtime/safepointVerifiers.hpp" 66 #include "utilities/bitMap.inline.hpp" 67 #include "utilities/checkedCast.hpp" 68 #include "utilities/events.hpp" 69 #include "utilities/macros.hpp" 70 71 Array<Method*>* VM_RedefineClasses::_old_methods = nullptr; 72 Array<Method*>* VM_RedefineClasses::_new_methods = nullptr; 73 Method** VM_RedefineClasses::_matching_old_methods = nullptr; 74 Method** VM_RedefineClasses::_matching_new_methods = nullptr; 75 Method** VM_RedefineClasses::_deleted_methods = nullptr; 76 Method** VM_RedefineClasses::_added_methods = nullptr; 77 int VM_RedefineClasses::_matching_methods_length = 0; 78 int VM_RedefineClasses::_deleted_methods_length = 0; 79 int VM_RedefineClasses::_added_methods_length = 0; 80 81 // This flag is global as the constructor does not reset it: 82 bool VM_RedefineClasses::_has_redefined_Object = false; 83 u8 VM_RedefineClasses::_id_counter = 0; 84 85 VM_RedefineClasses::VM_RedefineClasses(jint class_count, 86 const jvmtiClassDefinition *class_defs, 87 JvmtiClassLoadKind class_load_kind) { 88 _class_count = class_count; 89 _class_defs = class_defs; 90 _class_load_kind = class_load_kind; 91 _any_class_has_resolved_methods = false; 92 _res = JVMTI_ERROR_NONE; 93 _the_class = nullptr; 94 _id = next_id(); 95 } 96 97 static inline InstanceKlass* get_ik(jclass def) { 98 oop mirror = JNIHandles::resolve_non_null(def); 99 return InstanceKlass::cast(java_lang_Class::as_Klass(mirror)); 100 } 101 102 // If any of the classes are being redefined, wait 103 // Parallel constant pool merging leads to indeterminate constant pools. 104 void VM_RedefineClasses::lock_classes() { 105 JvmtiThreadState *state = JvmtiThreadState::state_for(JavaThread::current()); 106 GrowableArray<Klass*>* redef_classes = state->get_classes_being_redefined(); 107 108 MonitorLocker ml(RedefineClasses_lock); 109 110 if (redef_classes == nullptr) { 111 redef_classes = new (mtClass) GrowableArray<Klass*>(1, mtClass); 112 state->set_classes_being_redefined(redef_classes); 113 } 114 115 bool has_redefined; 116 do { 117 has_redefined = false; 118 // Go through classes each time until none are being redefined. Skip 119 // the ones that are being redefined by this thread currently. Class file 120 // load hook event may trigger new class redefine when we are redefining 121 // a class (after lock_classes()). 122 for (int i = 0; i < _class_count; i++) { 123 InstanceKlass* ik = get_ik(_class_defs[i].klass); 124 // Check if we are currently redefining the class in this thread already. 125 if (redef_classes->contains(ik)) { 126 assert(ik->is_being_redefined(), "sanity"); 127 } else { 128 if (ik->is_being_redefined()) { 129 ml.wait(); 130 has_redefined = true; 131 break; // for loop 132 } 133 } 134 } 135 } while (has_redefined); 136 137 for (int i = 0; i < _class_count; i++) { 138 InstanceKlass* ik = get_ik(_class_defs[i].klass); 139 redef_classes->push(ik); // Add to the _classes_being_redefined list 140 ik->set_is_being_redefined(true); 141 } 142 ml.notify_all(); 143 } 144 145 void VM_RedefineClasses::unlock_classes() { 146 JvmtiThreadState *state = JvmtiThreadState::state_for(JavaThread::current()); 147 GrowableArray<Klass*>* redef_classes = state->get_classes_being_redefined(); 148 assert(redef_classes != nullptr, "_classes_being_redefined is not allocated"); 149 150 MonitorLocker ml(RedefineClasses_lock); 151 152 for (int i = _class_count - 1; i >= 0; i--) { 153 InstanceKlass* def_ik = get_ik(_class_defs[i].klass); 154 if (redef_classes->length() > 0) { 155 // Remove the class from _classes_being_redefined list 156 Klass* k = redef_classes->pop(); 157 assert(def_ik == k, "unlocking wrong class"); 158 } 159 assert(def_ik->is_being_redefined(), 160 "should be being redefined to get here"); 161 162 // Unlock after we finish all redefines for this class within 163 // the thread. Same class can be pushed to the list multiple 164 // times (not more than once by each recursive redefinition). 165 if (!redef_classes->contains(def_ik)) { 166 def_ik->set_is_being_redefined(false); 167 } 168 } 169 ml.notify_all(); 170 } 171 172 bool VM_RedefineClasses::doit_prologue() { 173 if (_class_count == 0) { 174 _res = JVMTI_ERROR_NONE; 175 return false; 176 } 177 if (_class_defs == nullptr) { 178 _res = JVMTI_ERROR_NULL_POINTER; 179 return false; 180 } 181 182 for (int i = 0; i < _class_count; i++) { 183 if (_class_defs[i].klass == nullptr) { 184 _res = JVMTI_ERROR_INVALID_CLASS; 185 return false; 186 } 187 if (_class_defs[i].class_byte_count == 0) { 188 _res = JVMTI_ERROR_INVALID_CLASS_FORMAT; 189 return false; 190 } 191 if (_class_defs[i].class_bytes == nullptr) { 192 _res = JVMTI_ERROR_NULL_POINTER; 193 return false; 194 } 195 196 oop mirror = JNIHandles::resolve_non_null(_class_defs[i].klass); 197 // classes for primitives, arrays, and hidden classes 198 // cannot be redefined. 199 if (!is_modifiable_class(mirror)) { 200 _res = JVMTI_ERROR_UNMODIFIABLE_CLASS; 201 return false; 202 } 203 } 204 205 // Start timer after all the sanity checks; not quite accurate, but 206 // better than adding a bunch of stop() calls. 207 if (log_is_enabled(Info, redefine, class, timer)) { 208 _timer_vm_op_prologue.start(); 209 } 210 211 lock_classes(); 212 // We first load new class versions in the prologue, because somewhere down the 213 // call chain it is required that the current thread is a Java thread. 214 _res = load_new_class_versions(); 215 if (_res != JVMTI_ERROR_NONE) { 216 // free any successfully created classes, since none are redefined 217 for (int i = 0; i < _class_count; i++) { 218 if (_scratch_classes[i] != nullptr) { 219 ClassLoaderData* cld = _scratch_classes[i]->class_loader_data(); 220 // Free the memory for this class at class unloading time. Not before 221 // because CMS might think this is still live. 222 InstanceKlass* ik = get_ik(_class_defs[i].klass); 223 if (ik->get_cached_class_file() == _scratch_classes[i]->get_cached_class_file()) { 224 // Don't double-free cached_class_file copied from the original class if error. 225 _scratch_classes[i]->set_cached_class_file(nullptr); 226 } 227 cld->add_to_deallocate_list(InstanceKlass::cast(_scratch_classes[i])); 228 } 229 } 230 // Free os::malloc allocated memory in load_new_class_version. 231 os::free(_scratch_classes); 232 _timer_vm_op_prologue.stop(); 233 unlock_classes(); 234 return false; 235 } 236 237 _timer_vm_op_prologue.stop(); 238 return true; 239 } 240 241 void VM_RedefineClasses::doit() { 242 Thread* current = Thread::current(); 243 244 if (log_is_enabled(Info, redefine, class, timer)) { 245 _timer_vm_op_doit.start(); 246 } 247 248 #if INCLUDE_CDS 249 if (CDSConfig::is_using_archive()) { 250 // Sharing is enabled so we remap the shared readonly space to 251 // shared readwrite, private just in case we need to redefine 252 // a shared class. We do the remap during the doit() phase of 253 // the safepoint to be safer. 254 if (!MetaspaceShared::remap_shared_readonly_as_readwrite()) { 255 log_info(redefine, class, load)("failed to remap shared readonly space to readwrite, private"); 256 _res = JVMTI_ERROR_INTERNAL; 257 _timer_vm_op_doit.stop(); 258 return; 259 } 260 } 261 #endif 262 263 // Mark methods seen on stack and everywhere else so old methods are not 264 // cleaned up if they're on the stack. 265 MetadataOnStackMark md_on_stack(/*walk_all_metadata*/true, /*redefinition_walk*/true); 266 HandleMark hm(current); // make sure any handles created are deleted 267 // before the stack walk again. 268 269 for (int i = 0; i < _class_count; i++) { 270 redefine_single_class(current, _class_defs[i].klass, _scratch_classes[i]); 271 } 272 273 // Flush all compiled code that depends on the classes redefined. 274 flush_dependent_code(); 275 276 // Adjust constantpool caches and vtables for all classes 277 // that reference methods of the evolved classes. 278 // Have to do this after all classes are redefined and all methods that 279 // are redefined are marked as old. 280 AdjustAndCleanMetadata adjust_and_clean_metadata(current); 281 ClassLoaderDataGraph::classes_do(&adjust_and_clean_metadata); 282 283 // JSR-292 support 284 if (_any_class_has_resolved_methods) { 285 bool trace_name_printed = false; 286 ResolvedMethodTable::adjust_method_entries(&trace_name_printed); 287 } 288 289 // Increment flag indicating that some invariants are no longer true. 290 // See jvmtiExport.hpp for detailed explanation. 291 JvmtiExport::increment_redefinition_count(); 292 293 // check_class() is optionally called for product bits, but is 294 // always called for non-product bits. 295 #ifdef PRODUCT 296 if (log_is_enabled(Trace, redefine, class, obsolete, metadata)) { 297 #endif 298 log_trace(redefine, class, obsolete, metadata)("calling check_class"); 299 CheckClass check_class(current); 300 ClassLoaderDataGraph::classes_do(&check_class); 301 #ifdef PRODUCT 302 } 303 #endif 304 305 // Clean up any metadata now unreferenced while MetadataOnStackMark is set. 306 ClassLoaderDataGraph::clean_deallocate_lists(false); 307 308 _timer_vm_op_doit.stop(); 309 } 310 311 void VM_RedefineClasses::doit_epilogue() { 312 unlock_classes(); 313 314 // Free os::malloc allocated memory. 315 os::free(_scratch_classes); 316 317 // Reset the_class to null for error printing. 318 _the_class = nullptr; 319 320 if (log_is_enabled(Info, redefine, class, timer)) { 321 // Used to have separate timers for "doit" and "all", but the timer 322 // overhead skewed the measurements. 323 julong doit_time = _timer_vm_op_doit.milliseconds(); 324 julong all_time = _timer_vm_op_prologue.milliseconds() + doit_time; 325 326 log_info(redefine, class, timer) 327 ("vm_op: all=" JULONG_FORMAT " prologue=" JULONG_FORMAT " doit=" JULONG_FORMAT, 328 all_time, (julong)_timer_vm_op_prologue.milliseconds(), doit_time); 329 log_info(redefine, class, timer) 330 ("redefine_single_class: phase1=" JULONG_FORMAT " phase2=" JULONG_FORMAT, 331 (julong)_timer_rsc_phase1.milliseconds(), (julong)_timer_rsc_phase2.milliseconds()); 332 } 333 } 334 335 bool VM_RedefineClasses::is_modifiable_class(oop klass_mirror) { 336 // classes for primitives cannot be redefined 337 if (java_lang_Class::is_primitive(klass_mirror)) { 338 return false; 339 } 340 Klass* k = java_lang_Class::as_Klass(klass_mirror); 341 // classes for arrays cannot be redefined 342 if (k == nullptr || !k->is_instance_klass()) { 343 return false; 344 } 345 346 // Cannot redefine or retransform a hidden class. 347 if (InstanceKlass::cast(k)->is_hidden()) { 348 return false; 349 } 350 if (InstanceKlass::cast(k) == vmClasses::Object_klass()) { 351 return false; 352 } 353 if (InstanceKlass::cast(k) == vmClasses::Continuation_klass()) { 354 // Don't redefine Continuation class. See 8302779. 355 return false; 356 } 357 return true; 358 } 359 360 // Append the current entry at scratch_i in scratch_cp to *merge_cp_p 361 // where the end of *merge_cp_p is specified by *merge_cp_length_p. For 362 // direct CP entries, there is just the current entry to append. For 363 // indirect and double-indirect CP entries, there are zero or more 364 // referenced CP entries along with the current entry to append. 365 // Indirect and double-indirect CP entries are handled by recursive 366 // calls to append_entry() as needed. The referenced CP entries are 367 // always appended to *merge_cp_p before the referee CP entry. These 368 // referenced CP entries may already exist in *merge_cp_p in which case 369 // there is nothing extra to append and only the current entry is 370 // appended. 371 void VM_RedefineClasses::append_entry(const constantPoolHandle& scratch_cp, 372 int scratch_i, constantPoolHandle *merge_cp_p, int *merge_cp_length_p) { 373 374 // append is different depending on entry tag type 375 switch (scratch_cp->tag_at(scratch_i).value()) { 376 377 // The old verifier is implemented outside the VM. It loads classes, 378 // but does not resolve constant pool entries directly so we never 379 // see Class entries here with the old verifier. Similarly the old 380 // verifier does not like Class entries in the input constant pool. 381 // The split-verifier is implemented in the VM so it can optionally 382 // and directly resolve constant pool entries to load classes. The 383 // split-verifier can accept either Class entries or UnresolvedClass 384 // entries in the input constant pool. We revert the appended copy 385 // back to UnresolvedClass so that either verifier will be happy 386 // with the constant pool entry. 387 // 388 // this is an indirect CP entry so it needs special handling 389 case JVM_CONSTANT_Class: 390 case JVM_CONSTANT_UnresolvedClass: 391 { 392 int name_i = scratch_cp->klass_name_index_at(scratch_i); 393 int new_name_i = find_or_append_indirect_entry(scratch_cp, name_i, merge_cp_p, 394 merge_cp_length_p); 395 396 if (new_name_i != name_i) { 397 log_trace(redefine, class, constantpool) 398 ("Class entry@%d name_index change: %d to %d", 399 *merge_cp_length_p, name_i, new_name_i); 400 } 401 402 (*merge_cp_p)->temp_unresolved_klass_at_put(*merge_cp_length_p, new_name_i); 403 if (scratch_i != *merge_cp_length_p) { 404 // The new entry in *merge_cp_p is at a different index than 405 // the new entry in scratch_cp so we need to map the index values. 406 map_index(scratch_cp, scratch_i, *merge_cp_length_p); 407 } 408 (*merge_cp_length_p)++; 409 } break; 410 411 // these are direct CP entries so they can be directly appended, 412 // but double and long take two constant pool entries 413 case JVM_CONSTANT_Double: // fall through 414 case JVM_CONSTANT_Long: 415 { 416 ConstantPool::copy_entry_to(scratch_cp, scratch_i, *merge_cp_p, *merge_cp_length_p); 417 418 if (scratch_i != *merge_cp_length_p) { 419 // The new entry in *merge_cp_p is at a different index than 420 // the new entry in scratch_cp so we need to map the index values. 421 map_index(scratch_cp, scratch_i, *merge_cp_length_p); 422 } 423 (*merge_cp_length_p) += 2; 424 } break; 425 426 // these are direct CP entries so they can be directly appended 427 case JVM_CONSTANT_Float: // fall through 428 case JVM_CONSTANT_Integer: // fall through 429 case JVM_CONSTANT_Utf8: // fall through 430 431 // This was an indirect CP entry, but it has been changed into 432 // Symbol*s so this entry can be directly appended. 433 case JVM_CONSTANT_String: // fall through 434 { 435 ConstantPool::copy_entry_to(scratch_cp, scratch_i, *merge_cp_p, *merge_cp_length_p); 436 437 if (scratch_i != *merge_cp_length_p) { 438 // The new entry in *merge_cp_p is at a different index than 439 // the new entry in scratch_cp so we need to map the index values. 440 map_index(scratch_cp, scratch_i, *merge_cp_length_p); 441 } 442 (*merge_cp_length_p)++; 443 } break; 444 445 // this is an indirect CP entry so it needs special handling 446 case JVM_CONSTANT_NameAndType: 447 { 448 int name_ref_i = scratch_cp->name_ref_index_at(scratch_i); 449 int new_name_ref_i = find_or_append_indirect_entry(scratch_cp, name_ref_i, merge_cp_p, 450 merge_cp_length_p); 451 452 int signature_ref_i = scratch_cp->signature_ref_index_at(scratch_i); 453 int new_signature_ref_i = find_or_append_indirect_entry(scratch_cp, signature_ref_i, 454 merge_cp_p, merge_cp_length_p); 455 456 // If the referenced entries already exist in *merge_cp_p, then 457 // both new_name_ref_i and new_signature_ref_i will both be 0. 458 // In that case, all we are appending is the current entry. 459 if (new_name_ref_i != name_ref_i) { 460 log_trace(redefine, class, constantpool) 461 ("NameAndType entry@%d name_ref_index change: %d to %d", 462 *merge_cp_length_p, name_ref_i, new_name_ref_i); 463 } 464 if (new_signature_ref_i != signature_ref_i) { 465 log_trace(redefine, class, constantpool) 466 ("NameAndType entry@%d signature_ref_index change: %d to %d", 467 *merge_cp_length_p, signature_ref_i, new_signature_ref_i); 468 } 469 470 (*merge_cp_p)->name_and_type_at_put(*merge_cp_length_p, 471 new_name_ref_i, new_signature_ref_i); 472 if (scratch_i != *merge_cp_length_p) { 473 // The new entry in *merge_cp_p is at a different index than 474 // the new entry in scratch_cp so we need to map the index values. 475 map_index(scratch_cp, scratch_i, *merge_cp_length_p); 476 } 477 (*merge_cp_length_p)++; 478 } break; 479 480 // this is a double-indirect CP entry so it needs special handling 481 case JVM_CONSTANT_Fieldref: // fall through 482 case JVM_CONSTANT_InterfaceMethodref: // fall through 483 case JVM_CONSTANT_Methodref: 484 { 485 int klass_ref_i = scratch_cp->uncached_klass_ref_index_at(scratch_i); 486 int new_klass_ref_i = find_or_append_indirect_entry(scratch_cp, klass_ref_i, 487 merge_cp_p, merge_cp_length_p); 488 489 int name_and_type_ref_i = scratch_cp->uncached_name_and_type_ref_index_at(scratch_i); 490 int new_name_and_type_ref_i = find_or_append_indirect_entry(scratch_cp, name_and_type_ref_i, 491 merge_cp_p, merge_cp_length_p); 492 493 const char *entry_name = nullptr; 494 switch (scratch_cp->tag_at(scratch_i).value()) { 495 case JVM_CONSTANT_Fieldref: 496 entry_name = "Fieldref"; 497 (*merge_cp_p)->field_at_put(*merge_cp_length_p, new_klass_ref_i, 498 new_name_and_type_ref_i); 499 break; 500 case JVM_CONSTANT_InterfaceMethodref: 501 entry_name = "IFMethodref"; 502 (*merge_cp_p)->interface_method_at_put(*merge_cp_length_p, 503 new_klass_ref_i, new_name_and_type_ref_i); 504 break; 505 case JVM_CONSTANT_Methodref: 506 entry_name = "Methodref"; 507 (*merge_cp_p)->method_at_put(*merge_cp_length_p, new_klass_ref_i, 508 new_name_and_type_ref_i); 509 break; 510 default: 511 guarantee(false, "bad switch"); 512 break; 513 } 514 515 if (klass_ref_i != new_klass_ref_i) { 516 log_trace(redefine, class, constantpool) 517 ("%s entry@%d class_index changed: %d to %d", entry_name, *merge_cp_length_p, klass_ref_i, new_klass_ref_i); 518 } 519 if (name_and_type_ref_i != new_name_and_type_ref_i) { 520 log_trace(redefine, class, constantpool) 521 ("%s entry@%d name_and_type_index changed: %d to %d", 522 entry_name, *merge_cp_length_p, name_and_type_ref_i, new_name_and_type_ref_i); 523 } 524 525 if (scratch_i != *merge_cp_length_p) { 526 // The new entry in *merge_cp_p is at a different index than 527 // the new entry in scratch_cp so we need to map the index values. 528 map_index(scratch_cp, scratch_i, *merge_cp_length_p); 529 } 530 (*merge_cp_length_p)++; 531 } break; 532 533 // this is an indirect CP entry so it needs special handling 534 case JVM_CONSTANT_MethodType: 535 { 536 int ref_i = scratch_cp->method_type_index_at(scratch_i); 537 int new_ref_i = find_or_append_indirect_entry(scratch_cp, ref_i, merge_cp_p, 538 merge_cp_length_p); 539 if (new_ref_i != ref_i) { 540 log_trace(redefine, class, constantpool) 541 ("MethodType entry@%d ref_index change: %d to %d", *merge_cp_length_p, ref_i, new_ref_i); 542 } 543 (*merge_cp_p)->method_type_index_at_put(*merge_cp_length_p, new_ref_i); 544 if (scratch_i != *merge_cp_length_p) { 545 // The new entry in *merge_cp_p is at a different index than 546 // the new entry in scratch_cp so we need to map the index values. 547 map_index(scratch_cp, scratch_i, *merge_cp_length_p); 548 } 549 (*merge_cp_length_p)++; 550 } break; 551 552 // this is an indirect CP entry so it needs special handling 553 case JVM_CONSTANT_MethodHandle: 554 { 555 int ref_kind = scratch_cp->method_handle_ref_kind_at(scratch_i); 556 int ref_i = scratch_cp->method_handle_index_at(scratch_i); 557 int new_ref_i = find_or_append_indirect_entry(scratch_cp, ref_i, merge_cp_p, 558 merge_cp_length_p); 559 if (new_ref_i != ref_i) { 560 log_trace(redefine, class, constantpool) 561 ("MethodHandle entry@%d ref_index change: %d to %d", *merge_cp_length_p, ref_i, new_ref_i); 562 } 563 (*merge_cp_p)->method_handle_index_at_put(*merge_cp_length_p, ref_kind, new_ref_i); 564 if (scratch_i != *merge_cp_length_p) { 565 // The new entry in *merge_cp_p is at a different index than 566 // the new entry in scratch_cp so we need to map the index values. 567 map_index(scratch_cp, scratch_i, *merge_cp_length_p); 568 } 569 (*merge_cp_length_p)++; 570 } break; 571 572 // this is an indirect CP entry so it needs special handling 573 case JVM_CONSTANT_Dynamic: // fall through 574 case JVM_CONSTANT_InvokeDynamic: 575 { 576 // Index of the bootstrap specifier in the operands array 577 int old_bs_i = scratch_cp->bootstrap_methods_attribute_index(scratch_i); 578 int new_bs_i = find_or_append_operand(scratch_cp, old_bs_i, merge_cp_p, 579 merge_cp_length_p); 580 // The bootstrap method NameAndType_info index 581 int old_ref_i = scratch_cp->bootstrap_name_and_type_ref_index_at(scratch_i); 582 int new_ref_i = find_or_append_indirect_entry(scratch_cp, old_ref_i, merge_cp_p, 583 merge_cp_length_p); 584 if (new_bs_i != old_bs_i) { 585 log_trace(redefine, class, constantpool) 586 ("Dynamic entry@%d bootstrap_method_attr_index change: %d to %d", 587 *merge_cp_length_p, old_bs_i, new_bs_i); 588 } 589 if (new_ref_i != old_ref_i) { 590 log_trace(redefine, class, constantpool) 591 ("Dynamic entry@%d name_and_type_index change: %d to %d", *merge_cp_length_p, old_ref_i, new_ref_i); 592 } 593 594 if (scratch_cp->tag_at(scratch_i).is_dynamic_constant()) 595 (*merge_cp_p)->dynamic_constant_at_put(*merge_cp_length_p, new_bs_i, new_ref_i); 596 else 597 (*merge_cp_p)->invoke_dynamic_at_put(*merge_cp_length_p, new_bs_i, new_ref_i); 598 if (scratch_i != *merge_cp_length_p) { 599 // The new entry in *merge_cp_p is at a different index than 600 // the new entry in scratch_cp so we need to map the index values. 601 map_index(scratch_cp, scratch_i, *merge_cp_length_p); 602 } 603 (*merge_cp_length_p)++; 604 } break; 605 606 // At this stage, Class or UnresolvedClass could be in scratch_cp, but not 607 // ClassIndex 608 case JVM_CONSTANT_ClassIndex: // fall through 609 610 // Invalid is used as the tag for the second constant pool entry 611 // occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should 612 // not be seen by itself. 613 case JVM_CONSTANT_Invalid: // fall through 614 615 // At this stage, String could be here, but not StringIndex 616 case JVM_CONSTANT_StringIndex: // fall through 617 618 // At this stage JVM_CONSTANT_UnresolvedClassInError should not be 619 // here 620 case JVM_CONSTANT_UnresolvedClassInError: // fall through 621 622 default: 623 { 624 // leave a breadcrumb 625 jbyte bad_value = scratch_cp->tag_at(scratch_i).value(); 626 ShouldNotReachHere(); 627 } break; 628 } // end switch tag value 629 } // end append_entry() 630 631 632 u2 VM_RedefineClasses::find_or_append_indirect_entry(const constantPoolHandle& scratch_cp, 633 int ref_i, constantPoolHandle *merge_cp_p, int *merge_cp_length_p) { 634 635 int new_ref_i = ref_i; 636 bool match = (ref_i < *merge_cp_length_p) && 637 scratch_cp->compare_entry_to(ref_i, *merge_cp_p, ref_i); 638 639 if (!match) { 640 // forward reference in *merge_cp_p or not a direct match 641 int found_i = scratch_cp->find_matching_entry(ref_i, *merge_cp_p); 642 if (found_i != 0) { 643 guarantee(found_i != ref_i, "compare_entry_to() and find_matching_entry() do not agree"); 644 // Found a matching entry somewhere else in *merge_cp_p so just need a mapping entry. 645 new_ref_i = found_i; 646 map_index(scratch_cp, ref_i, found_i); 647 } else { 648 // no match found so we have to append this entry to *merge_cp_p 649 append_entry(scratch_cp, ref_i, merge_cp_p, merge_cp_length_p); 650 // The above call to append_entry() can only append one entry 651 // so the post call query of *merge_cp_length_p is only for 652 // the sake of consistency. 653 new_ref_i = *merge_cp_length_p - 1; 654 } 655 } 656 657 // constant pool indices are u2, unless the merged constant pool overflows which 658 // we don't check for. 659 return checked_cast<u2>(new_ref_i); 660 } // end find_or_append_indirect_entry() 661 662 663 // Append a bootstrap specifier into the merge_cp operands that is semantically equal 664 // to the scratch_cp operands bootstrap specifier passed by the old_bs_i index. 665 // Recursively append new merge_cp entries referenced by the new bootstrap specifier. 666 void VM_RedefineClasses::append_operand(const constantPoolHandle& scratch_cp, int old_bs_i, 667 constantPoolHandle *merge_cp_p, int *merge_cp_length_p) { 668 669 u2 old_ref_i = scratch_cp->operand_bootstrap_method_ref_index_at(old_bs_i); 670 u2 new_ref_i = find_or_append_indirect_entry(scratch_cp, old_ref_i, merge_cp_p, 671 merge_cp_length_p); 672 if (new_ref_i != old_ref_i) { 673 log_trace(redefine, class, constantpool) 674 ("operands entry@%d bootstrap method ref_index change: %d to %d", _operands_cur_length, old_ref_i, new_ref_i); 675 } 676 677 Array<u2>* merge_ops = (*merge_cp_p)->operands(); 678 int new_bs_i = _operands_cur_length; 679 // We have _operands_cur_length == 0 when the merge_cp operands is empty yet. 680 // However, the operand_offset_at(0) was set in the extend_operands() call. 681 int new_base = (new_bs_i == 0) ? (*merge_cp_p)->operand_offset_at(0) 682 : (*merge_cp_p)->operand_next_offset_at(new_bs_i - 1); 683 u2 argc = scratch_cp->operand_argument_count_at(old_bs_i); 684 685 ConstantPool::operand_offset_at_put(merge_ops, _operands_cur_length, new_base); 686 merge_ops->at_put(new_base++, new_ref_i); 687 merge_ops->at_put(new_base++, argc); 688 689 for (int i = 0; i < argc; i++) { 690 u2 old_arg_ref_i = scratch_cp->operand_argument_index_at(old_bs_i, i); 691 u2 new_arg_ref_i = find_or_append_indirect_entry(scratch_cp, old_arg_ref_i, merge_cp_p, 692 merge_cp_length_p); 693 merge_ops->at_put(new_base++, new_arg_ref_i); 694 if (new_arg_ref_i != old_arg_ref_i) { 695 log_trace(redefine, class, constantpool) 696 ("operands entry@%d bootstrap method argument ref_index change: %d to %d", 697 _operands_cur_length, old_arg_ref_i, new_arg_ref_i); 698 } 699 } 700 if (old_bs_i != _operands_cur_length) { 701 // The bootstrap specifier in *merge_cp_p is at a different index than 702 // that in scratch_cp so we need to map the index values. 703 map_operand_index(old_bs_i, new_bs_i); 704 } 705 _operands_cur_length++; 706 } // end append_operand() 707 708 709 int VM_RedefineClasses::find_or_append_operand(const constantPoolHandle& scratch_cp, 710 int old_bs_i, constantPoolHandle *merge_cp_p, int *merge_cp_length_p) { 711 712 int new_bs_i = old_bs_i; // bootstrap specifier index 713 bool match = (old_bs_i < _operands_cur_length) && 714 scratch_cp->compare_operand_to(old_bs_i, *merge_cp_p, old_bs_i); 715 716 if (!match) { 717 // forward reference in *merge_cp_p or not a direct match 718 int found_i = scratch_cp->find_matching_operand(old_bs_i, *merge_cp_p, 719 _operands_cur_length); 720 if (found_i != -1) { 721 guarantee(found_i != old_bs_i, "compare_operand_to() and find_matching_operand() disagree"); 722 // found a matching operand somewhere else in *merge_cp_p so just need a mapping 723 new_bs_i = found_i; 724 map_operand_index(old_bs_i, found_i); 725 } else { 726 // no match found so we have to append this bootstrap specifier to *merge_cp_p 727 append_operand(scratch_cp, old_bs_i, merge_cp_p, merge_cp_length_p); 728 new_bs_i = _operands_cur_length - 1; 729 } 730 } 731 return new_bs_i; 732 } // end find_or_append_operand() 733 734 735 void VM_RedefineClasses::finalize_operands_merge(const constantPoolHandle& merge_cp, TRAPS) { 736 if (merge_cp->operands() == nullptr) { 737 return; 738 } 739 // Shrink the merge_cp operands 740 merge_cp->shrink_operands(_operands_cur_length, CHECK); 741 742 if (log_is_enabled(Trace, redefine, class, constantpool)) { 743 // don't want to loop unless we are tracing 744 int count = 0; 745 for (int i = 1; i < _operands_index_map_p->length(); i++) { 746 int value = _operands_index_map_p->at(i); 747 if (value != -1) { 748 log_trace(redefine, class, constantpool)("operands_index_map[%d]: old=%d new=%d", count, i, value); 749 count++; 750 } 751 } 752 } 753 // Clean-up 754 _operands_index_map_p = nullptr; 755 _operands_cur_length = 0; 756 _operands_index_map_count = 0; 757 } // end finalize_operands_merge() 758 759 // Symbol* comparator for qsort 760 // The caller must have an active ResourceMark. 761 static int symcmp(const void* a, const void* b) { 762 char* astr = (*(Symbol**)a)->as_C_string(); 763 char* bstr = (*(Symbol**)b)->as_C_string(); 764 return strcmp(astr, bstr); 765 } 766 767 // The caller must have an active ResourceMark. 768 static jvmtiError check_attribute_arrays(const char* attr_name, 769 InstanceKlass* the_class, InstanceKlass* scratch_class, 770 Array<u2>* the_array, Array<u2>* scr_array) { 771 bool the_array_exists = the_array != Universe::the_empty_short_array(); 772 bool scr_array_exists = scr_array != Universe::the_empty_short_array(); 773 774 int array_len = the_array->length(); 775 if (the_array_exists && scr_array_exists) { 776 if (array_len != scr_array->length()) { 777 log_trace(redefine, class) 778 ("redefined class %s attribute change error: %s len=%d changed to len=%d", 779 the_class->external_name(), attr_name, array_len, scr_array->length()); 780 return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_CLASS_ATTRIBUTE_CHANGED; 781 } 782 783 // The order of entries in the attribute array is not specified so we 784 // have to explicitly check for the same contents. We do this by copying 785 // the referenced symbols into their own arrays, sorting them and then 786 // comparing each element pair. 787 788 Symbol** the_syms = NEW_RESOURCE_ARRAY_RETURN_NULL(Symbol*, array_len); 789 Symbol** scr_syms = NEW_RESOURCE_ARRAY_RETURN_NULL(Symbol*, array_len); 790 791 if (the_syms == nullptr || scr_syms == nullptr) { 792 return JVMTI_ERROR_OUT_OF_MEMORY; 793 } 794 795 for (int i = 0; i < array_len; i++) { 796 int the_cp_index = the_array->at(i); 797 int scr_cp_index = scr_array->at(i); 798 the_syms[i] = the_class->constants()->klass_name_at(the_cp_index); 799 scr_syms[i] = scratch_class->constants()->klass_name_at(scr_cp_index); 800 } 801 802 qsort(the_syms, array_len, sizeof(Symbol*), symcmp); 803 qsort(scr_syms, array_len, sizeof(Symbol*), symcmp); 804 805 for (int i = 0; i < array_len; i++) { 806 if (the_syms[i] != scr_syms[i]) { 807 log_info(redefine, class) 808 ("redefined class %s attribute change error: %s[%d]: %s changed to %s", 809 the_class->external_name(), attr_name, i, 810 the_syms[i]->as_C_string(), scr_syms[i]->as_C_string()); 811 return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_CLASS_ATTRIBUTE_CHANGED; 812 } 813 } 814 } else if (the_array_exists ^ scr_array_exists) { 815 const char* action_str = (the_array_exists) ? "removed" : "added"; 816 log_info(redefine, class) 817 ("redefined class %s attribute change error: %s attribute %s", 818 the_class->external_name(), attr_name, action_str); 819 return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_CLASS_ATTRIBUTE_CHANGED; 820 } 821 return JVMTI_ERROR_NONE; 822 } 823 824 static jvmtiError check_nest_attributes(InstanceKlass* the_class, 825 InstanceKlass* scratch_class) { 826 // Check whether the class NestHost attribute has been changed. 827 Thread* thread = Thread::current(); 828 ResourceMark rm(thread); 829 u2 the_nest_host_idx = the_class->nest_host_index(); 830 u2 scr_nest_host_idx = scratch_class->nest_host_index(); 831 832 if (the_nest_host_idx != 0 && scr_nest_host_idx != 0) { 833 Symbol* the_sym = the_class->constants()->klass_name_at(the_nest_host_idx); 834 Symbol* scr_sym = scratch_class->constants()->klass_name_at(scr_nest_host_idx); 835 if (the_sym != scr_sym) { 836 log_info(redefine, class, nestmates) 837 ("redefined class %s attribute change error: NestHost class: %s replaced with: %s", 838 the_class->external_name(), the_sym->as_C_string(), scr_sym->as_C_string()); 839 return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_CLASS_ATTRIBUTE_CHANGED; 840 } 841 } else if ((the_nest_host_idx == 0) ^ (scr_nest_host_idx == 0)) { 842 const char* action_str = (the_nest_host_idx != 0) ? "removed" : "added"; 843 log_info(redefine, class, nestmates) 844 ("redefined class %s attribute change error: NestHost attribute %s", 845 the_class->external_name(), action_str); 846 return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_CLASS_ATTRIBUTE_CHANGED; 847 } 848 849 // Check whether the class NestMembers attribute has been changed. 850 return check_attribute_arrays("NestMembers", 851 the_class, scratch_class, 852 the_class->nest_members(), 853 scratch_class->nest_members()); 854 } 855 856 // Return an error status if the class Record attribute was changed. 857 static jvmtiError check_record_attribute(InstanceKlass* the_class, InstanceKlass* scratch_class) { 858 // Get lists of record components. 859 Array<RecordComponent*>* the_record = the_class->record_components(); 860 Array<RecordComponent*>* scr_record = scratch_class->record_components(); 861 bool the_record_exists = the_record != nullptr; 862 bool scr_record_exists = scr_record != nullptr; 863 864 if (the_record_exists && scr_record_exists) { 865 int the_num_components = the_record->length(); 866 int scr_num_components = scr_record->length(); 867 if (the_num_components != scr_num_components) { 868 log_info(redefine, class, record) 869 ("redefined class %s attribute change error: Record num_components=%d changed to num_components=%d", 870 the_class->external_name(), the_num_components, scr_num_components); 871 return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_CLASS_ATTRIBUTE_CHANGED; 872 } 873 874 // Compare each field in each record component. 875 ConstantPool* the_cp = the_class->constants(); 876 ConstantPool* scr_cp = scratch_class->constants(); 877 for (int x = 0; x < the_num_components; x++) { 878 RecordComponent* the_component = the_record->at(x); 879 RecordComponent* scr_component = scr_record->at(x); 880 const Symbol* const the_name = the_cp->symbol_at(the_component->name_index()); 881 const Symbol* const scr_name = scr_cp->symbol_at(scr_component->name_index()); 882 const Symbol* const the_descr = the_cp->symbol_at(the_component->descriptor_index()); 883 const Symbol* const scr_descr = scr_cp->symbol_at(scr_component->descriptor_index()); 884 if (the_name != scr_name || the_descr != scr_descr) { 885 log_info(redefine, class, record) 886 ("redefined class %s attribute change error: Record name_index, descriptor_index, and/or attributes_count changed", 887 the_class->external_name()); 888 return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_CLASS_ATTRIBUTE_CHANGED; 889 } 890 891 int the_gen_sig = the_component->generic_signature_index(); 892 int scr_gen_sig = scr_component->generic_signature_index(); 893 const Symbol* const the_gen_sig_sym = (the_gen_sig == 0 ? nullptr : 894 the_cp->symbol_at(the_component->generic_signature_index())); 895 const Symbol* const scr_gen_sig_sym = (scr_gen_sig == 0 ? nullptr : 896 scr_cp->symbol_at(scr_component->generic_signature_index())); 897 if (the_gen_sig_sym != scr_gen_sig_sym) { 898 log_info(redefine, class, record) 899 ("redefined class %s attribute change error: Record generic_signature attribute changed", 900 the_class->external_name()); 901 return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_CLASS_ATTRIBUTE_CHANGED; 902 } 903 904 // It's okay if a record component's annotations were changed. 905 } 906 907 } else if (the_record_exists ^ scr_record_exists) { 908 const char* action_str = (the_record_exists) ? "removed" : "added"; 909 log_info(redefine, class, record) 910 ("redefined class %s attribute change error: Record attribute %s", 911 the_class->external_name(), action_str); 912 return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_CLASS_ATTRIBUTE_CHANGED; 913 } 914 915 return JVMTI_ERROR_NONE; 916 } 917 918 919 static jvmtiError check_permitted_subclasses_attribute(InstanceKlass* the_class, 920 InstanceKlass* scratch_class) { 921 Thread* thread = Thread::current(); 922 ResourceMark rm(thread); 923 924 // Check whether the class PermittedSubclasses attribute has been changed. 925 return check_attribute_arrays("PermittedSubclasses", 926 the_class, scratch_class, 927 the_class->permitted_subclasses(), 928 scratch_class->permitted_subclasses()); 929 } 930 931 static bool can_add_or_delete(Method* m) { 932 // Compatibility mode 933 return (AllowRedefinitionToAddDeleteMethods && 934 (m->is_private() && (m->is_static() || m->is_final()))); 935 } 936 937 jvmtiError VM_RedefineClasses::compare_and_normalize_class_versions( 938 InstanceKlass* the_class, 939 InstanceKlass* scratch_class) { 940 int i; 941 942 // Check superclasses, or rather their names, since superclasses themselves can be 943 // requested to replace. 944 // Check for null superclass first since this might be java.lang.Object 945 if (the_class->super() != scratch_class->super() && 946 (the_class->super() == nullptr || scratch_class->super() == nullptr || 947 the_class->super()->name() != 948 scratch_class->super()->name())) { 949 log_info(redefine, class, normalize) 950 ("redefined class %s superclass change error: superclass changed from %s to %s.", 951 the_class->external_name(), 952 the_class->super() == nullptr ? "null" : the_class->super()->external_name(), 953 scratch_class->super() == nullptr ? "null" : scratch_class->super()->external_name()); 954 return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED; 955 } 956 957 // Check if the number, names and order of directly implemented interfaces are the same. 958 // I think in principle we should just check if the sets of names of directly implemented 959 // interfaces are the same, i.e. the order of declaration (which, however, if changed in the 960 // .java file, also changes in .class file) should not matter. However, comparing sets is 961 // technically a bit more difficult, and, more importantly, I am not sure at present that the 962 // order of interfaces does not matter on the implementation level, i.e. that the VM does not 963 // rely on it somewhere. 964 Array<InstanceKlass*>* k_interfaces = the_class->local_interfaces(); 965 Array<InstanceKlass*>* k_new_interfaces = scratch_class->local_interfaces(); 966 int n_intfs = k_interfaces->length(); 967 if (n_intfs != k_new_interfaces->length()) { 968 log_info(redefine, class, normalize) 969 ("redefined class %s interfaces change error: number of implemented interfaces changed from %d to %d.", 970 the_class->external_name(), n_intfs, k_new_interfaces->length()); 971 return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED; 972 } 973 for (i = 0; i < n_intfs; i++) { 974 if (k_interfaces->at(i)->name() != 975 k_new_interfaces->at(i)->name()) { 976 log_info(redefine, class, normalize) 977 ("redefined class %s interfaces change error: interface changed from %s to %s.", 978 the_class->external_name(), 979 k_interfaces->at(i)->external_name(), k_new_interfaces->at(i)->external_name()); 980 return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED; 981 } 982 } 983 984 // Check whether class is in the error init state. 985 if (the_class->is_in_error_state()) { 986 log_info(redefine, class, normalize) 987 ("redefined class %s is in error init state.", the_class->external_name()); 988 // TBD #5057930: special error code is needed in 1.6 989 return JVMTI_ERROR_INVALID_CLASS; 990 } 991 992 // Check whether the nest-related attributes have been changed. 993 jvmtiError err = check_nest_attributes(the_class, scratch_class); 994 if (err != JVMTI_ERROR_NONE) { 995 return err; 996 } 997 998 // Check whether the Record attribute has been changed. 999 err = check_record_attribute(the_class, scratch_class); 1000 if (err != JVMTI_ERROR_NONE) { 1001 return err; 1002 } 1003 1004 // Check whether the PermittedSubclasses attribute has been changed. 1005 err = check_permitted_subclasses_attribute(the_class, scratch_class); 1006 if (err != JVMTI_ERROR_NONE) { 1007 return err; 1008 } 1009 1010 // Check whether class modifiers are the same. 1011 jushort old_flags = (jushort) the_class->access_flags().get_flags(); 1012 jushort new_flags = (jushort) scratch_class->access_flags().get_flags(); 1013 if (old_flags != new_flags) { 1014 log_info(redefine, class, normalize) 1015 ("redefined class %s modifiers change error: modifiers changed from %d to %d.", 1016 the_class->external_name(), old_flags, new_flags); 1017 return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_CLASS_MODIFIERS_CHANGED; 1018 } 1019 1020 // Check if the number, names, types and order of fields declared in these classes 1021 // are the same. 1022 JavaFieldStream old_fs(the_class); 1023 JavaFieldStream new_fs(scratch_class); 1024 for (; !old_fs.done() && !new_fs.done(); old_fs.next(), new_fs.next()) { 1025 // name and signature 1026 Symbol* name_sym1 = the_class->constants()->symbol_at(old_fs.name_index()); 1027 Symbol* sig_sym1 = the_class->constants()->symbol_at(old_fs.signature_index()); 1028 Symbol* name_sym2 = scratch_class->constants()->symbol_at(new_fs.name_index()); 1029 Symbol* sig_sym2 = scratch_class->constants()->symbol_at(new_fs.signature_index()); 1030 if (name_sym1 != name_sym2 || sig_sym1 != sig_sym2) { 1031 log_info(redefine, class, normalize) 1032 ("redefined class %s fields change error: field %s %s changed to %s %s.", 1033 the_class->external_name(), 1034 sig_sym1->as_C_string(), name_sym1->as_C_string(), 1035 sig_sym2->as_C_string(), name_sym2->as_C_string()); 1036 return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_SCHEMA_CHANGED; 1037 } 1038 // offset 1039 if (old_fs.offset() != new_fs.offset()) { 1040 log_info(redefine, class, normalize) 1041 ("redefined class %s field %s change error: offset changed from %d to %d.", 1042 the_class->external_name(), name_sym2->as_C_string(), old_fs.offset(), new_fs.offset()); 1043 return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_SCHEMA_CHANGED; 1044 } 1045 // access 1046 old_flags = old_fs.access_flags().as_short(); 1047 new_flags = new_fs.access_flags().as_short(); 1048 if ((old_flags ^ new_flags) & JVM_RECOGNIZED_FIELD_MODIFIERS) { 1049 log_info(redefine, class, normalize) 1050 ("redefined class %s field %s change error: modifiers changed from %d to %d.", 1051 the_class->external_name(), name_sym2->as_C_string(), old_flags, new_flags); 1052 return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_SCHEMA_CHANGED; 1053 } 1054 } 1055 1056 // If both streams aren't done then we have a differing number of 1057 // fields. 1058 if (!old_fs.done() || !new_fs.done()) { 1059 const char* action = old_fs.done() ? "added" : "deleted"; 1060 log_info(redefine, class, normalize) 1061 ("redefined class %s fields change error: some fields were %s.", 1062 the_class->external_name(), action); 1063 return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_SCHEMA_CHANGED; 1064 } 1065 1066 // Do a parallel walk through the old and new methods. Detect 1067 // cases where they match (exist in both), have been added in 1068 // the new methods, or have been deleted (exist only in the 1069 // old methods). The class file parser places methods in order 1070 // by method name, but does not order overloaded methods by 1071 // signature. In order to determine what fate befell the methods, 1072 // this code places the overloaded new methods that have matching 1073 // old methods in the same order as the old methods and places 1074 // new overloaded methods at the end of overloaded methods of 1075 // that name. The code for this order normalization is adapted 1076 // from the algorithm used in InstanceKlass::find_method(). 1077 // Since we are swapping out of order entries as we find them, 1078 // we only have to search forward through the overloaded methods. 1079 // Methods which are added and have the same name as an existing 1080 // method (but different signature) will be put at the end of 1081 // the methods with that name, and the name mismatch code will 1082 // handle them. 1083 Array<Method*>* k_old_methods(the_class->methods()); 1084 Array<Method*>* k_new_methods(scratch_class->methods()); 1085 int n_old_methods = k_old_methods->length(); 1086 int n_new_methods = k_new_methods->length(); 1087 Thread* thread = Thread::current(); 1088 1089 int ni = 0; 1090 int oi = 0; 1091 while (true) { 1092 Method* k_old_method; 1093 Method* k_new_method; 1094 enum { matched, added, deleted, undetermined } method_was = undetermined; 1095 1096 if (oi >= n_old_methods) { 1097 if (ni >= n_new_methods) { 1098 break; // we've looked at everything, done 1099 } 1100 // New method at the end 1101 k_new_method = k_new_methods->at(ni); 1102 method_was = added; 1103 } else if (ni >= n_new_methods) { 1104 // Old method, at the end, is deleted 1105 k_old_method = k_old_methods->at(oi); 1106 method_was = deleted; 1107 } else { 1108 // There are more methods in both the old and new lists 1109 k_old_method = k_old_methods->at(oi); 1110 k_new_method = k_new_methods->at(ni); 1111 if (k_old_method->name() != k_new_method->name()) { 1112 // Methods are sorted by method name, so a mismatch means added 1113 // or deleted 1114 if (k_old_method->name()->fast_compare(k_new_method->name()) > 0) { 1115 method_was = added; 1116 } else { 1117 method_was = deleted; 1118 } 1119 } else if (k_old_method->signature() == k_new_method->signature()) { 1120 // Both the name and signature match 1121 method_was = matched; 1122 } else { 1123 // The name matches, but the signature doesn't, which means we have to 1124 // search forward through the new overloaded methods. 1125 int nj; // outside the loop for post-loop check 1126 for (nj = ni + 1; nj < n_new_methods; nj++) { 1127 Method* m = k_new_methods->at(nj); 1128 if (k_old_method->name() != m->name()) { 1129 // reached another method name so no more overloaded methods 1130 method_was = deleted; 1131 break; 1132 } 1133 if (k_old_method->signature() == m->signature()) { 1134 // found a match so swap the methods 1135 k_new_methods->at_put(ni, m); 1136 k_new_methods->at_put(nj, k_new_method); 1137 k_new_method = m; 1138 method_was = matched; 1139 break; 1140 } 1141 } 1142 1143 if (nj >= n_new_methods) { 1144 // reached the end without a match; so method was deleted 1145 method_was = deleted; 1146 } 1147 } 1148 } 1149 1150 switch (method_was) { 1151 case matched: 1152 // methods match, be sure modifiers do too 1153 old_flags = (jushort) k_old_method->access_flags().get_flags(); 1154 new_flags = (jushort) k_new_method->access_flags().get_flags(); 1155 if ((old_flags ^ new_flags) & ~(JVM_ACC_NATIVE)) { 1156 log_info(redefine, class, normalize) 1157 ("redefined class %s method %s modifiers error: modifiers changed from %d to %d", 1158 the_class->external_name(), k_old_method->name_and_sig_as_C_string(), old_flags, new_flags); 1159 return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_METHOD_MODIFIERS_CHANGED; 1160 } 1161 { 1162 u2 new_num = k_new_method->method_idnum(); 1163 u2 old_num = k_old_method->method_idnum(); 1164 if (new_num != old_num) { 1165 Method* idnum_owner = scratch_class->method_with_idnum(old_num); 1166 if (idnum_owner != nullptr) { 1167 // There is already a method assigned this idnum -- switch them 1168 // Take current and original idnum from the new_method 1169 idnum_owner->set_method_idnum(new_num); 1170 idnum_owner->set_orig_method_idnum(k_new_method->orig_method_idnum()); 1171 } 1172 // Take current and original idnum from the old_method 1173 k_new_method->set_method_idnum(old_num); 1174 k_new_method->set_orig_method_idnum(k_old_method->orig_method_idnum()); 1175 if (thread->has_pending_exception()) { 1176 return JVMTI_ERROR_OUT_OF_MEMORY; 1177 } 1178 } 1179 } 1180 JFR_ONLY(k_new_method->copy_trace_flags(*k_old_method->trace_flags_addr());) 1181 log_trace(redefine, class, normalize) 1182 ("Method matched: new: %s [%d] == old: %s [%d]", 1183 k_new_method->name_and_sig_as_C_string(), ni, k_old_method->name_and_sig_as_C_string(), oi); 1184 // advance to next pair of methods 1185 ++oi; 1186 ++ni; 1187 break; 1188 case added: 1189 // method added, see if it is OK 1190 if (!can_add_or_delete(k_new_method)) { 1191 log_info(redefine, class, normalize) 1192 ("redefined class %s methods error: added method: %s [%d]", 1193 the_class->external_name(), k_new_method->name_and_sig_as_C_string(), ni); 1194 return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_METHOD_ADDED; 1195 } 1196 { 1197 u2 num = the_class->next_method_idnum(); 1198 if (num == ConstMethod::UNSET_IDNUM) { 1199 // cannot add any more methods 1200 log_info(redefine, class, normalize) 1201 ("redefined class %s methods error: can't create ID for new method %s [%d]", 1202 the_class->external_name(), k_new_method->name_and_sig_as_C_string(), ni); 1203 return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_METHOD_ADDED; 1204 } 1205 u2 new_num = k_new_method->method_idnum(); 1206 Method* idnum_owner = scratch_class->method_with_idnum(num); 1207 if (idnum_owner != nullptr) { 1208 // There is already a method assigned this idnum -- switch them 1209 // Take current and original idnum from the new_method 1210 idnum_owner->set_method_idnum(new_num); 1211 idnum_owner->set_orig_method_idnum(k_new_method->orig_method_idnum()); 1212 } 1213 k_new_method->set_method_idnum(num); 1214 k_new_method->set_orig_method_idnum(num); 1215 if (thread->has_pending_exception()) { 1216 return JVMTI_ERROR_OUT_OF_MEMORY; 1217 } 1218 } 1219 log_trace(redefine, class, normalize) 1220 ("Method added: new: %s [%d]", k_new_method->name_and_sig_as_C_string(), ni); 1221 ++ni; // advance to next new method 1222 break; 1223 case deleted: 1224 // method deleted, see if it is OK 1225 if (!can_add_or_delete(k_old_method)) { 1226 log_info(redefine, class, normalize) 1227 ("redefined class %s methods error: deleted method %s [%d]", 1228 the_class->external_name(), k_old_method->name_and_sig_as_C_string(), oi); 1229 return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_METHOD_DELETED; 1230 } 1231 log_trace(redefine, class, normalize) 1232 ("Method deleted: old: %s [%d]", k_old_method->name_and_sig_as_C_string(), oi); 1233 ++oi; // advance to next old method 1234 break; 1235 default: 1236 ShouldNotReachHere(); 1237 } 1238 } 1239 1240 return JVMTI_ERROR_NONE; 1241 } 1242 1243 1244 // Find new constant pool index value for old constant pool index value 1245 // by searching the index map. Returns zero (0) if there is no mapped 1246 // value for the old constant pool index. 1247 u2 VM_RedefineClasses::find_new_index(int old_index) { 1248 if (_index_map_count == 0) { 1249 // map is empty so nothing can be found 1250 return 0; 1251 } 1252 1253 if (old_index < 1 || old_index >= _index_map_p->length()) { 1254 // The old_index is out of range so it is not mapped. This should 1255 // not happen in regular constant pool merging use, but it can 1256 // happen if a corrupt annotation is processed. 1257 return 0; 1258 } 1259 1260 int value = _index_map_p->at(old_index); 1261 if (value == -1) { 1262 // the old_index is not mapped 1263 return 0; 1264 } 1265 1266 // constant pool indices are u2, unless the merged constant pool overflows which 1267 // we don't check for. 1268 return checked_cast<u2>(value); 1269 } // end find_new_index() 1270 1271 1272 // Find new bootstrap specifier index value for old bootstrap specifier index 1273 // value by searching the index map. Returns unused index (-1) if there is 1274 // no mapped value for the old bootstrap specifier index. 1275 int VM_RedefineClasses::find_new_operand_index(int old_index) { 1276 if (_operands_index_map_count == 0) { 1277 // map is empty so nothing can be found 1278 return -1; 1279 } 1280 1281 if (old_index == -1 || old_index >= _operands_index_map_p->length()) { 1282 // The old_index is out of range so it is not mapped. 1283 // This should not happen in regular constant pool merging use. 1284 return -1; 1285 } 1286 1287 int value = _operands_index_map_p->at(old_index); 1288 if (value == -1) { 1289 // the old_index is not mapped 1290 return -1; 1291 } 1292 1293 return value; 1294 } // end find_new_operand_index() 1295 1296 1297 // The bug 6214132 caused the verification to fail. 1298 // 1. What's done in RedefineClasses() before verification: 1299 // a) A reference to the class being redefined (_the_class) and a 1300 // reference to new version of the class (_scratch_class) are 1301 // saved here for use during the bytecode verification phase of 1302 // RedefineClasses. 1303 // b) The _java_mirror field from _the_class is copied to the 1304 // _java_mirror field in _scratch_class. This means that a jclass 1305 // returned for _the_class or _scratch_class will refer to the 1306 // same Java mirror. The verifier will see the "one true mirror" 1307 // for the class being verified. 1308 // 2. See comments in JvmtiThreadState for what is done during verification. 1309 1310 class RedefineVerifyMark : public StackObj { 1311 private: 1312 JvmtiThreadState* _state; 1313 Klass* _scratch_class; 1314 OopHandle _scratch_mirror; 1315 1316 public: 1317 1318 RedefineVerifyMark(Klass* the_class, Klass* scratch_class, 1319 JvmtiThreadState* state) : _state(state), _scratch_class(scratch_class) 1320 { 1321 _state->set_class_versions_map(the_class, scratch_class); 1322 _scratch_mirror = the_class->java_mirror_handle(); // this is a copy that is swapped 1323 _scratch_class->swap_java_mirror_handle(_scratch_mirror); 1324 } 1325 1326 ~RedefineVerifyMark() { 1327 // Restore the scratch class's mirror, so when scratch_class is removed 1328 // the correct mirror pointing to it can be cleared. 1329 _scratch_class->swap_java_mirror_handle(_scratch_mirror); 1330 _state->clear_class_versions_map(); 1331 } 1332 }; 1333 1334 1335 jvmtiError VM_RedefineClasses::load_new_class_versions() { 1336 1337 // For consistency allocate memory using os::malloc wrapper. 1338 _scratch_classes = (InstanceKlass**) 1339 os::malloc(sizeof(InstanceKlass*) * _class_count, mtClass); 1340 if (_scratch_classes == nullptr) { 1341 return JVMTI_ERROR_OUT_OF_MEMORY; 1342 } 1343 // Zero initialize the _scratch_classes array. 1344 for (int i = 0; i < _class_count; i++) { 1345 _scratch_classes[i] = nullptr; 1346 } 1347 1348 JavaThread* current = JavaThread::current(); 1349 ResourceMark rm(current); 1350 1351 JvmtiThreadState *state = JvmtiThreadState::state_for(current); 1352 // state can only be null if the current thread is exiting which 1353 // should not happen since we're trying to do a RedefineClasses 1354 guarantee(state != nullptr, "exiting thread calling load_new_class_versions"); 1355 for (int i = 0; i < _class_count; i++) { 1356 // Create HandleMark so that any handles created while loading new class 1357 // versions are deleted. Constant pools are deallocated while merging 1358 // constant pools 1359 HandleMark hm(current); 1360 InstanceKlass* the_class = get_ik(_class_defs[i].klass); 1361 1362 log_debug(redefine, class, load) 1363 ("loading name=%s kind=%d (avail_mem=" UINT64_FORMAT "K)", 1364 the_class->external_name(), _class_load_kind, os::available_memory() >> 10); 1365 1366 ClassFileStream st((u1*)_class_defs[i].class_bytes, 1367 _class_defs[i].class_byte_count, 1368 "__VM_RedefineClasses__", 1369 ClassFileStream::verify); 1370 1371 // Set redefined class handle in JvmtiThreadState class. 1372 // This redefined class is sent to agent event handler for class file 1373 // load hook event. 1374 state->set_class_being_redefined(the_class, _class_load_kind); 1375 1376 JavaThread* THREAD = current; // For exception macros. 1377 ExceptionMark em(THREAD); 1378 Handle protection_domain(THREAD, the_class->protection_domain()); 1379 ClassLoadInfo cl_info(protection_domain); 1380 // Parse and create a class from the bytes, but this class isn't added 1381 // to the dictionary, so do not call resolve_from_stream. 1382 InstanceKlass* scratch_class = KlassFactory::create_from_stream(&st, 1383 the_class->name(), 1384 the_class->class_loader_data(), 1385 cl_info, 1386 THREAD); 1387 1388 // Clear class_being_redefined just to be sure. 1389 state->clear_class_being_redefined(); 1390 1391 // TODO: if this is retransform, and nothing changed we can skip it 1392 1393 // Need to clean up allocated InstanceKlass if there's an error so assign 1394 // the result here. Caller deallocates all the scratch classes in case of 1395 // an error. 1396 _scratch_classes[i] = scratch_class; 1397 1398 if (HAS_PENDING_EXCEPTION) { 1399 Symbol* ex_name = PENDING_EXCEPTION->klass()->name(); 1400 log_info(redefine, class, load, exceptions)("create_from_stream exception: '%s'", ex_name->as_C_string()); 1401 CLEAR_PENDING_EXCEPTION; 1402 1403 if (ex_name == vmSymbols::java_lang_UnsupportedClassVersionError()) { 1404 return JVMTI_ERROR_UNSUPPORTED_VERSION; 1405 } else if (ex_name == vmSymbols::java_lang_ClassFormatError()) { 1406 return JVMTI_ERROR_INVALID_CLASS_FORMAT; 1407 } else if (ex_name == vmSymbols::java_lang_ClassCircularityError()) { 1408 return JVMTI_ERROR_CIRCULAR_CLASS_DEFINITION; 1409 } else if (ex_name == vmSymbols::java_lang_NoClassDefFoundError()) { 1410 // The message will be "XXX (wrong name: YYY)" 1411 return JVMTI_ERROR_NAMES_DONT_MATCH; 1412 } else if (ex_name == vmSymbols::java_lang_OutOfMemoryError()) { 1413 return JVMTI_ERROR_OUT_OF_MEMORY; 1414 } else { // Just in case more exceptions can be thrown.. 1415 return JVMTI_ERROR_FAILS_VERIFICATION; 1416 } 1417 } 1418 1419 // Ensure class is linked before redefine 1420 if (!the_class->is_linked()) { 1421 the_class->link_class(THREAD); 1422 if (HAS_PENDING_EXCEPTION) { 1423 Symbol* ex_name = PENDING_EXCEPTION->klass()->name(); 1424 oop message = java_lang_Throwable::message(PENDING_EXCEPTION); 1425 if (message != nullptr) { 1426 char* ex_msg = java_lang_String::as_utf8_string(message); 1427 log_info(redefine, class, load, exceptions)("link_class exception: '%s %s'", 1428 ex_name->as_C_string(), ex_msg); 1429 } else { 1430 log_info(redefine, class, load, exceptions)("link_class exception: '%s'", 1431 ex_name->as_C_string()); 1432 } 1433 CLEAR_PENDING_EXCEPTION; 1434 if (ex_name == vmSymbols::java_lang_OutOfMemoryError()) { 1435 return JVMTI_ERROR_OUT_OF_MEMORY; 1436 } else if (ex_name == vmSymbols::java_lang_NoClassDefFoundError()) { 1437 return JVMTI_ERROR_INVALID_CLASS; 1438 } else { 1439 return JVMTI_ERROR_INTERNAL; 1440 } 1441 } 1442 } 1443 1444 // Do the validity checks in compare_and_normalize_class_versions() 1445 // before verifying the byte codes. By doing these checks first, we 1446 // limit the number of functions that require redirection from 1447 // the_class to scratch_class. In particular, we don't have to 1448 // modify JNI GetSuperclass() and thus won't change its performance. 1449 jvmtiError res = compare_and_normalize_class_versions(the_class, 1450 scratch_class); 1451 if (res != JVMTI_ERROR_NONE) { 1452 return res; 1453 } 1454 1455 // verify what the caller passed us 1456 { 1457 // The bug 6214132 caused the verification to fail. 1458 // Information about the_class and scratch_class is temporarily 1459 // recorded into jvmtiThreadState. This data is used to redirect 1460 // the_class to scratch_class in the JVM_* functions called by the 1461 // verifier. Please, refer to jvmtiThreadState.hpp for the detailed 1462 // description. 1463 RedefineVerifyMark rvm(the_class, scratch_class, state); 1464 Verifier::verify(scratch_class, true, THREAD); 1465 } 1466 1467 if (HAS_PENDING_EXCEPTION) { 1468 Symbol* ex_name = PENDING_EXCEPTION->klass()->name(); 1469 log_info(redefine, class, load, exceptions)("verify_byte_codes exception: '%s'", ex_name->as_C_string()); 1470 CLEAR_PENDING_EXCEPTION; 1471 if (ex_name == vmSymbols::java_lang_OutOfMemoryError()) { 1472 return JVMTI_ERROR_OUT_OF_MEMORY; 1473 } else { 1474 // tell the caller the bytecodes are bad 1475 return JVMTI_ERROR_FAILS_VERIFICATION; 1476 } 1477 } 1478 1479 res = merge_cp_and_rewrite(the_class, scratch_class, THREAD); 1480 if (HAS_PENDING_EXCEPTION) { 1481 Symbol* ex_name = PENDING_EXCEPTION->klass()->name(); 1482 log_info(redefine, class, load, exceptions)("merge_cp_and_rewrite exception: '%s'", ex_name->as_C_string()); 1483 CLEAR_PENDING_EXCEPTION; 1484 if (ex_name == vmSymbols::java_lang_OutOfMemoryError()) { 1485 return JVMTI_ERROR_OUT_OF_MEMORY; 1486 } else { 1487 return JVMTI_ERROR_INTERNAL; 1488 } 1489 } 1490 1491 #ifdef ASSERT 1492 { 1493 // verify what we have done during constant pool merging 1494 { 1495 RedefineVerifyMark rvm(the_class, scratch_class, state); 1496 Verifier::verify(scratch_class, true, THREAD); 1497 } 1498 1499 if (HAS_PENDING_EXCEPTION) { 1500 Symbol* ex_name = PENDING_EXCEPTION->klass()->name(); 1501 log_info(redefine, class, load, exceptions) 1502 ("verify_byte_codes post merge-CP exception: '%s'", ex_name->as_C_string()); 1503 CLEAR_PENDING_EXCEPTION; 1504 if (ex_name == vmSymbols::java_lang_OutOfMemoryError()) { 1505 return JVMTI_ERROR_OUT_OF_MEMORY; 1506 } else { 1507 // tell the caller that constant pool merging screwed up 1508 return JVMTI_ERROR_INTERNAL; 1509 } 1510 } 1511 } 1512 #endif // ASSERT 1513 1514 Rewriter::rewrite(scratch_class, THREAD); 1515 if (!HAS_PENDING_EXCEPTION) { 1516 scratch_class->link_methods(THREAD); 1517 } 1518 if (HAS_PENDING_EXCEPTION) { 1519 Symbol* ex_name = PENDING_EXCEPTION->klass()->name(); 1520 log_info(redefine, class, load, exceptions) 1521 ("Rewriter::rewrite or link_methods exception: '%s'", ex_name->as_C_string()); 1522 CLEAR_PENDING_EXCEPTION; 1523 if (ex_name == vmSymbols::java_lang_OutOfMemoryError()) { 1524 return JVMTI_ERROR_OUT_OF_MEMORY; 1525 } else { 1526 return JVMTI_ERROR_INTERNAL; 1527 } 1528 } 1529 1530 log_debug(redefine, class, load) 1531 ("loaded name=%s (avail_mem=" UINT64_FORMAT "K)", the_class->external_name(), os::available_memory() >> 10); 1532 } 1533 1534 return JVMTI_ERROR_NONE; 1535 } 1536 1537 1538 // Map old_index to new_index as needed. scratch_cp is only needed 1539 // for log calls. 1540 void VM_RedefineClasses::map_index(const constantPoolHandle& scratch_cp, 1541 int old_index, int new_index) { 1542 if (find_new_index(old_index) != 0) { 1543 // old_index is already mapped 1544 return; 1545 } 1546 1547 if (old_index == new_index) { 1548 // no mapping is needed 1549 return; 1550 } 1551 1552 _index_map_p->at_put(old_index, new_index); 1553 _index_map_count++; 1554 1555 log_trace(redefine, class, constantpool) 1556 ("mapped tag %d at index %d to %d", scratch_cp->tag_at(old_index).value(), old_index, new_index); 1557 } // end map_index() 1558 1559 1560 // Map old_index to new_index as needed. 1561 void VM_RedefineClasses::map_operand_index(int old_index, int new_index) { 1562 if (find_new_operand_index(old_index) != -1) { 1563 // old_index is already mapped 1564 return; 1565 } 1566 1567 if (old_index == new_index) { 1568 // no mapping is needed 1569 return; 1570 } 1571 1572 _operands_index_map_p->at_put(old_index, new_index); 1573 _operands_index_map_count++; 1574 1575 log_trace(redefine, class, constantpool)("mapped bootstrap specifier at index %d to %d", old_index, new_index); 1576 } // end map_index() 1577 1578 1579 // Merge old_cp and scratch_cp and return the results of the merge via 1580 // merge_cp_p. The number of entries in *merge_cp_p is returned via 1581 // merge_cp_length_p. The entries in old_cp occupy the same locations 1582 // in *merge_cp_p. Also creates a map of indices from entries in 1583 // scratch_cp to the corresponding entry in *merge_cp_p. Index map 1584 // entries are only created for entries in scratch_cp that occupy a 1585 // different location in *merged_cp_p. 1586 bool VM_RedefineClasses::merge_constant_pools(const constantPoolHandle& old_cp, 1587 const constantPoolHandle& scratch_cp, constantPoolHandle *merge_cp_p, 1588 int *merge_cp_length_p, TRAPS) { 1589 1590 if (merge_cp_p == nullptr) { 1591 assert(false, "caller must provide scratch constantPool"); 1592 return false; // robustness 1593 } 1594 if (merge_cp_length_p == nullptr) { 1595 assert(false, "caller must provide scratch CP length"); 1596 return false; // robustness 1597 } 1598 // Worst case we need old_cp->length() + scratch_cp()->length(), 1599 // but the caller might be smart so make sure we have at least 1600 // the minimum. 1601 if ((*merge_cp_p)->length() < old_cp->length()) { 1602 assert(false, "merge area too small"); 1603 return false; // robustness 1604 } 1605 1606 log_info(redefine, class, constantpool)("old_cp_len=%d, scratch_cp_len=%d", old_cp->length(), scratch_cp->length()); 1607 1608 { 1609 // Pass 0: 1610 // The old_cp is copied to *merge_cp_p; this means that any code 1611 // using old_cp does not have to change. This work looks like a 1612 // perfect fit for ConstantPool*::copy_cp_to(), but we need to 1613 // handle one special case: 1614 // - revert JVM_CONSTANT_Class to JVM_CONSTANT_UnresolvedClass 1615 // This will make verification happy. 1616 1617 int old_i; // index into old_cp 1618 1619 // index zero (0) is not used in constantPools 1620 for (old_i = 1; old_i < old_cp->length(); old_i++) { 1621 // leave debugging crumb 1622 jbyte old_tag = old_cp->tag_at(old_i).value(); 1623 switch (old_tag) { 1624 case JVM_CONSTANT_Class: 1625 case JVM_CONSTANT_UnresolvedClass: 1626 // revert the copy to JVM_CONSTANT_UnresolvedClass 1627 // May be resolving while calling this so do the same for 1628 // JVM_CONSTANT_UnresolvedClass (klass_name_at() deals with transition) 1629 (*merge_cp_p)->temp_unresolved_klass_at_put(old_i, 1630 old_cp->klass_name_index_at(old_i)); 1631 break; 1632 1633 case JVM_CONSTANT_Double: 1634 case JVM_CONSTANT_Long: 1635 // just copy the entry to *merge_cp_p, but double and long take 1636 // two constant pool entries 1637 ConstantPool::copy_entry_to(old_cp, old_i, *merge_cp_p, old_i); 1638 old_i++; 1639 break; 1640 1641 default: 1642 // just copy the entry to *merge_cp_p 1643 ConstantPool::copy_entry_to(old_cp, old_i, *merge_cp_p, old_i); 1644 break; 1645 } 1646 } // end for each old_cp entry 1647 1648 ConstantPool::copy_operands(old_cp, *merge_cp_p, CHECK_false); 1649 (*merge_cp_p)->extend_operands(scratch_cp, CHECK_false); 1650 1651 // We don't need to sanity check that *merge_cp_length_p is within 1652 // *merge_cp_p bounds since we have the minimum on-entry check above. 1653 (*merge_cp_length_p) = old_i; 1654 } 1655 1656 // merge_cp_len should be the same as old_cp->length() at this point 1657 // so this trace message is really a "warm-and-breathing" message. 1658 log_debug(redefine, class, constantpool)("after pass 0: merge_cp_len=%d", *merge_cp_length_p); 1659 1660 int scratch_i; // index into scratch_cp 1661 { 1662 // Pass 1a: 1663 // Compare scratch_cp entries to the old_cp entries that we have 1664 // already copied to *merge_cp_p. In this pass, we are eliminating 1665 // exact duplicates (matching entry at same index) so we only 1666 // compare entries in the common indice range. 1667 int increment = 1; 1668 int pass1a_length = MIN2(old_cp->length(), scratch_cp->length()); 1669 for (scratch_i = 1; scratch_i < pass1a_length; scratch_i += increment) { 1670 switch (scratch_cp->tag_at(scratch_i).value()) { 1671 case JVM_CONSTANT_Double: 1672 case JVM_CONSTANT_Long: 1673 // double and long take two constant pool entries 1674 increment = 2; 1675 break; 1676 1677 default: 1678 increment = 1; 1679 break; 1680 } 1681 1682 bool match = scratch_cp->compare_entry_to(scratch_i, *merge_cp_p, scratch_i); 1683 if (match) { 1684 // found a match at the same index so nothing more to do 1685 continue; 1686 } 1687 1688 int found_i = scratch_cp->find_matching_entry(scratch_i, *merge_cp_p); 1689 if (found_i != 0) { 1690 guarantee(found_i != scratch_i, 1691 "compare_entry_to() and find_matching_entry() do not agree"); 1692 1693 // Found a matching entry somewhere else in *merge_cp_p so 1694 // just need a mapping entry. 1695 map_index(scratch_cp, scratch_i, found_i); 1696 continue; 1697 } 1698 1699 // No match found so we have to append this entry and any unique 1700 // referenced entries to *merge_cp_p. 1701 append_entry(scratch_cp, scratch_i, merge_cp_p, merge_cp_length_p); 1702 } 1703 } 1704 1705 log_debug(redefine, class, constantpool) 1706 ("after pass 1a: merge_cp_len=%d, scratch_i=%d, index_map_len=%d", 1707 *merge_cp_length_p, scratch_i, _index_map_count); 1708 1709 if (scratch_i < scratch_cp->length()) { 1710 // Pass 1b: 1711 // old_cp is smaller than scratch_cp so there are entries in 1712 // scratch_cp that we have not yet processed. We take care of 1713 // those now. 1714 int increment = 1; 1715 for (; scratch_i < scratch_cp->length(); scratch_i += increment) { 1716 switch (scratch_cp->tag_at(scratch_i).value()) { 1717 case JVM_CONSTANT_Double: 1718 case JVM_CONSTANT_Long: 1719 // double and long take two constant pool entries 1720 increment = 2; 1721 break; 1722 1723 default: 1724 increment = 1; 1725 break; 1726 } 1727 1728 int found_i = 1729 scratch_cp->find_matching_entry(scratch_i, *merge_cp_p); 1730 if (found_i != 0) { 1731 // Found a matching entry somewhere else in *merge_cp_p so 1732 // just need a mapping entry. 1733 map_index(scratch_cp, scratch_i, found_i); 1734 continue; 1735 } 1736 1737 // No match found so we have to append this entry and any unique 1738 // referenced entries to *merge_cp_p. 1739 append_entry(scratch_cp, scratch_i, merge_cp_p, merge_cp_length_p); 1740 } 1741 1742 log_debug(redefine, class, constantpool) 1743 ("after pass 1b: merge_cp_len=%d, scratch_i=%d, index_map_len=%d", 1744 *merge_cp_length_p, scratch_i, _index_map_count); 1745 } 1746 finalize_operands_merge(*merge_cp_p, CHECK_false); 1747 1748 return true; 1749 } // end merge_constant_pools() 1750 1751 1752 // Scoped object to clean up the constant pool(s) created for merging 1753 class MergeCPCleaner { 1754 ClassLoaderData* _loader_data; 1755 ConstantPool* _cp; 1756 ConstantPool* _scratch_cp; 1757 public: 1758 MergeCPCleaner(ClassLoaderData* loader_data, ConstantPool* merge_cp) : 1759 _loader_data(loader_data), _cp(merge_cp), _scratch_cp(nullptr) {} 1760 ~MergeCPCleaner() { 1761 _loader_data->add_to_deallocate_list(_cp); 1762 if (_scratch_cp != nullptr) { 1763 _loader_data->add_to_deallocate_list(_scratch_cp); 1764 } 1765 } 1766 void add_scratch_cp(ConstantPool* scratch_cp) { _scratch_cp = scratch_cp; } 1767 }; 1768 1769 // Merge constant pools between the_class and scratch_class and 1770 // potentially rewrite bytecodes in scratch_class to use the merged 1771 // constant pool. 1772 jvmtiError VM_RedefineClasses::merge_cp_and_rewrite( 1773 InstanceKlass* the_class, InstanceKlass* scratch_class, 1774 TRAPS) { 1775 // worst case merged constant pool length is old and new combined 1776 int merge_cp_length = the_class->constants()->length() 1777 + scratch_class->constants()->length(); 1778 1779 // Constant pools are not easily reused so we allocate a new one 1780 // each time. 1781 // merge_cp is created unsafe for concurrent GC processing. It 1782 // should be marked safe before discarding it. Even though 1783 // garbage, if it crosses a card boundary, it may be scanned 1784 // in order to find the start of the first complete object on the card. 1785 ClassLoaderData* loader_data = the_class->class_loader_data(); 1786 ConstantPool* merge_cp_oop = 1787 ConstantPool::allocate(loader_data, 1788 merge_cp_length, 1789 CHECK_(JVMTI_ERROR_OUT_OF_MEMORY)); 1790 MergeCPCleaner cp_cleaner(loader_data, merge_cp_oop); 1791 1792 HandleMark hm(THREAD); // make sure handles are cleared before 1793 // MergeCPCleaner clears out merge_cp_oop 1794 constantPoolHandle merge_cp(THREAD, merge_cp_oop); 1795 1796 // Get constants() from the old class because it could have been rewritten 1797 // while we were at a safepoint allocating a new constant pool. 1798 constantPoolHandle old_cp(THREAD, the_class->constants()); 1799 constantPoolHandle scratch_cp(THREAD, scratch_class->constants()); 1800 1801 // If the length changed, the class was redefined out from under us. Return 1802 // an error. 1803 if (merge_cp_length != the_class->constants()->length() 1804 + scratch_class->constants()->length()) { 1805 return JVMTI_ERROR_INTERNAL; 1806 } 1807 1808 // Update the version number of the constant pools (may keep scratch_cp) 1809 merge_cp->increment_and_save_version(old_cp->version()); 1810 scratch_cp->increment_and_save_version(old_cp->version()); 1811 1812 ResourceMark rm(THREAD); 1813 _index_map_count = 0; 1814 _index_map_p = new intArray(scratch_cp->length(), scratch_cp->length(), -1); 1815 1816 _operands_cur_length = ConstantPool::operand_array_length(old_cp->operands()); 1817 _operands_index_map_count = 0; 1818 int operands_index_map_len = ConstantPool::operand_array_length(scratch_cp->operands()); 1819 _operands_index_map_p = new intArray(operands_index_map_len, operands_index_map_len, -1); 1820 1821 // reference to the cp holder is needed for copy_operands() 1822 merge_cp->set_pool_holder(scratch_class); 1823 bool result = merge_constant_pools(old_cp, scratch_cp, &merge_cp, 1824 &merge_cp_length, THREAD); 1825 merge_cp->set_pool_holder(nullptr); 1826 1827 if (!result) { 1828 // The merge can fail due to memory allocation failure or due 1829 // to robustness checks. 1830 return JVMTI_ERROR_INTERNAL; 1831 } 1832 1833 // ensure merged constant pool size does not overflow u2 1834 if (merge_cp_length > 0xFFFF) { 1835 log_warning(redefine, class, constantpool)("Merged constant pool overflow: %d entries", merge_cp_length); 1836 return JVMTI_ERROR_INTERNAL; 1837 } 1838 1839 // Set dynamic constants attribute from the original CP. 1840 if (old_cp->has_dynamic_constant()) { 1841 scratch_cp->set_has_dynamic_constant(); 1842 } 1843 1844 log_info(redefine, class, constantpool)("merge_cp_len=%d, index_map_len=%d", merge_cp_length, _index_map_count); 1845 1846 if (_index_map_count == 0) { 1847 // there is nothing to map between the new and merged constant pools 1848 1849 // Copy attributes from scratch_cp to merge_cp 1850 merge_cp->copy_fields(scratch_cp()); 1851 1852 if (old_cp->length() == scratch_cp->length()) { 1853 // The old and new constant pools are the same length and the 1854 // index map is empty. This means that the three constant pools 1855 // are equivalent (but not the same). Unfortunately, the new 1856 // constant pool has not gone through link resolution nor have 1857 // the new class bytecodes gone through constant pool cache 1858 // rewriting so we can't use the old constant pool with the new 1859 // class. 1860 1861 // toss the merged constant pool at return 1862 } else if (old_cp->length() < scratch_cp->length()) { 1863 // The old constant pool has fewer entries than the new constant 1864 // pool and the index map is empty. This means the new constant 1865 // pool is a superset of the old constant pool. However, the old 1866 // class bytecodes have already gone through constant pool cache 1867 // rewriting so we can't use the new constant pool with the old 1868 // class. 1869 1870 // toss the merged constant pool at return 1871 } else { 1872 // The old constant pool has more entries than the new constant 1873 // pool and the index map is empty. This means that both the old 1874 // and merged constant pools are supersets of the new constant 1875 // pool. 1876 1877 // Replace the new constant pool with a shrunken copy of the 1878 // merged constant pool 1879 set_new_constant_pool(loader_data, scratch_class, merge_cp, merge_cp_length, 1880 CHECK_(JVMTI_ERROR_OUT_OF_MEMORY)); 1881 // The new constant pool replaces scratch_cp so have cleaner clean it up. 1882 // It can't be cleaned up while there are handles to it. 1883 cp_cleaner.add_scratch_cp(scratch_cp()); 1884 } 1885 } else { 1886 if (log_is_enabled(Trace, redefine, class, constantpool)) { 1887 // don't want to loop unless we are tracing 1888 int count = 0; 1889 for (int i = 1; i < _index_map_p->length(); i++) { 1890 int value = _index_map_p->at(i); 1891 1892 if (value != -1) { 1893 log_trace(redefine, class, constantpool)("index_map[%d]: old=%d new=%d", count, i, value); 1894 count++; 1895 } 1896 } 1897 } 1898 1899 // We have entries mapped between the new and merged constant pools 1900 // so we have to rewrite some constant pool references. 1901 if (!rewrite_cp_refs(scratch_class)) { 1902 return JVMTI_ERROR_INTERNAL; 1903 } 1904 1905 // Copy attributes from scratch_cp to merge_cp (should be done after rewrite_cp_refs()) 1906 merge_cp->copy_fields(scratch_cp()); 1907 1908 // Replace the new constant pool with a shrunken copy of the 1909 // merged constant pool so now the rewritten bytecodes have 1910 // valid references; the previous new constant pool will get 1911 // GCed. 1912 set_new_constant_pool(loader_data, scratch_class, merge_cp, merge_cp_length, 1913 CHECK_(JVMTI_ERROR_OUT_OF_MEMORY)); 1914 // The new constant pool replaces scratch_cp so have cleaner clean it up. 1915 // It can't be cleaned up while there are handles to it. 1916 cp_cleaner.add_scratch_cp(scratch_cp()); 1917 } 1918 1919 return JVMTI_ERROR_NONE; 1920 } // end merge_cp_and_rewrite() 1921 1922 1923 // Rewrite constant pool references in klass scratch_class. 1924 bool VM_RedefineClasses::rewrite_cp_refs(InstanceKlass* scratch_class) { 1925 1926 // rewrite constant pool references in the nest attributes: 1927 if (!rewrite_cp_refs_in_nest_attributes(scratch_class)) { 1928 // propagate failure back to caller 1929 return false; 1930 } 1931 1932 // rewrite constant pool references in the Record attribute: 1933 if (!rewrite_cp_refs_in_record_attribute(scratch_class)) { 1934 // propagate failure back to caller 1935 return false; 1936 } 1937 1938 // rewrite constant pool references in the PermittedSubclasses attribute: 1939 if (!rewrite_cp_refs_in_permitted_subclasses_attribute(scratch_class)) { 1940 // propagate failure back to caller 1941 return false; 1942 } 1943 1944 // rewrite constant pool references in the methods: 1945 if (!rewrite_cp_refs_in_methods(scratch_class)) { 1946 // propagate failure back to caller 1947 return false; 1948 } 1949 1950 // rewrite constant pool references in the class_annotations: 1951 if (!rewrite_cp_refs_in_class_annotations(scratch_class)) { 1952 // propagate failure back to caller 1953 return false; 1954 } 1955 1956 // rewrite constant pool references in the fields_annotations: 1957 if (!rewrite_cp_refs_in_fields_annotations(scratch_class)) { 1958 // propagate failure back to caller 1959 return false; 1960 } 1961 1962 // rewrite constant pool references in the methods_annotations: 1963 if (!rewrite_cp_refs_in_methods_annotations(scratch_class)) { 1964 // propagate failure back to caller 1965 return false; 1966 } 1967 1968 // rewrite constant pool references in the methods_parameter_annotations: 1969 if (!rewrite_cp_refs_in_methods_parameter_annotations(scratch_class)) { 1970 // propagate failure back to caller 1971 return false; 1972 } 1973 1974 // rewrite constant pool references in the methods_default_annotations: 1975 if (!rewrite_cp_refs_in_methods_default_annotations(scratch_class)) { 1976 // propagate failure back to caller 1977 return false; 1978 } 1979 1980 // rewrite constant pool references in the class_type_annotations: 1981 if (!rewrite_cp_refs_in_class_type_annotations(scratch_class)) { 1982 // propagate failure back to caller 1983 return false; 1984 } 1985 1986 // rewrite constant pool references in the fields_type_annotations: 1987 if (!rewrite_cp_refs_in_fields_type_annotations(scratch_class)) { 1988 // propagate failure back to caller 1989 return false; 1990 } 1991 1992 // rewrite constant pool references in the methods_type_annotations: 1993 if (!rewrite_cp_refs_in_methods_type_annotations(scratch_class)) { 1994 // propagate failure back to caller 1995 return false; 1996 } 1997 1998 // There can be type annotations in the Code part of a method_info attribute. 1999 // These annotations are not accessible, even by reflection. 2000 // Currently they are not even parsed by the ClassFileParser. 2001 // If runtime access is added they will also need to be rewritten. 2002 2003 // rewrite source file name index: 2004 u2 source_file_name_idx = scratch_class->source_file_name_index(); 2005 if (source_file_name_idx != 0) { 2006 u2 new_source_file_name_idx = find_new_index(source_file_name_idx); 2007 if (new_source_file_name_idx != 0) { 2008 scratch_class->set_source_file_name_index(new_source_file_name_idx); 2009 } 2010 } 2011 2012 // rewrite class generic signature index: 2013 u2 generic_signature_index = scratch_class->generic_signature_index(); 2014 if (generic_signature_index != 0) { 2015 u2 new_generic_signature_index = find_new_index(generic_signature_index); 2016 if (new_generic_signature_index != 0) { 2017 scratch_class->set_generic_signature_index(new_generic_signature_index); 2018 } 2019 } 2020 2021 return true; 2022 } // end rewrite_cp_refs() 2023 2024 // Rewrite constant pool references in the NestHost and NestMembers attributes. 2025 bool VM_RedefineClasses::rewrite_cp_refs_in_nest_attributes( 2026 InstanceKlass* scratch_class) { 2027 2028 u2 cp_index = scratch_class->nest_host_index(); 2029 if (cp_index != 0) { 2030 scratch_class->set_nest_host_index(find_new_index(cp_index)); 2031 } 2032 Array<u2>* nest_members = scratch_class->nest_members(); 2033 for (int i = 0; i < nest_members->length(); i++) { 2034 u2 cp_index = nest_members->at(i); 2035 nest_members->at_put(i, find_new_index(cp_index)); 2036 } 2037 return true; 2038 } 2039 2040 // Rewrite constant pool references in the Record attribute. 2041 bool VM_RedefineClasses::rewrite_cp_refs_in_record_attribute(InstanceKlass* scratch_class) { 2042 Array<RecordComponent*>* components = scratch_class->record_components(); 2043 if (components != nullptr) { 2044 for (int i = 0; i < components->length(); i++) { 2045 RecordComponent* component = components->at(i); 2046 u2 cp_index = component->name_index(); 2047 component->set_name_index(find_new_index(cp_index)); 2048 cp_index = component->descriptor_index(); 2049 component->set_descriptor_index(find_new_index(cp_index)); 2050 cp_index = component->generic_signature_index(); 2051 if (cp_index != 0) { 2052 component->set_generic_signature_index(find_new_index(cp_index)); 2053 } 2054 2055 AnnotationArray* annotations = component->annotations(); 2056 if (annotations != nullptr && annotations->length() != 0) { 2057 int byte_i = 0; // byte index into annotations 2058 if (!rewrite_cp_refs_in_annotations_typeArray(annotations, byte_i)) { 2059 log_debug(redefine, class, annotation)("bad record_component_annotations at %d", i); 2060 // propagate failure back to caller 2061 return false; 2062 } 2063 } 2064 2065 AnnotationArray* type_annotations = component->type_annotations(); 2066 if (type_annotations != nullptr && type_annotations->length() != 0) { 2067 int byte_i = 0; // byte index into annotations 2068 if (!rewrite_cp_refs_in_annotations_typeArray(type_annotations, byte_i)) { 2069 log_debug(redefine, class, annotation)("bad record_component_type_annotations at %d", i); 2070 // propagate failure back to caller 2071 return false; 2072 } 2073 } 2074 } 2075 } 2076 return true; 2077 } 2078 2079 // Rewrite constant pool references in the PermittedSubclasses attribute. 2080 bool VM_RedefineClasses::rewrite_cp_refs_in_permitted_subclasses_attribute( 2081 InstanceKlass* scratch_class) { 2082 2083 Array<u2>* permitted_subclasses = scratch_class->permitted_subclasses(); 2084 assert(permitted_subclasses != nullptr, "unexpected null permitted_subclasses"); 2085 for (int i = 0; i < permitted_subclasses->length(); i++) { 2086 u2 cp_index = permitted_subclasses->at(i); 2087 permitted_subclasses->at_put(i, find_new_index(cp_index)); 2088 } 2089 return true; 2090 } 2091 2092 // Rewrite constant pool references in the methods. 2093 bool VM_RedefineClasses::rewrite_cp_refs_in_methods(InstanceKlass* scratch_class) { 2094 2095 Array<Method*>* methods = scratch_class->methods(); 2096 2097 if (methods == nullptr || methods->length() == 0) { 2098 // no methods so nothing to do 2099 return true; 2100 } 2101 2102 JavaThread* THREAD = JavaThread::current(); // For exception macros. 2103 ExceptionMark em(THREAD); 2104 2105 // rewrite constant pool references in the methods: 2106 for (int i = methods->length() - 1; i >= 0; i--) { 2107 methodHandle method(THREAD, methods->at(i)); 2108 methodHandle new_method; 2109 rewrite_cp_refs_in_method(method, &new_method, THREAD); 2110 if (!new_method.is_null()) { 2111 // the method has been replaced so save the new method version 2112 // even in the case of an exception. original method is on the 2113 // deallocation list. 2114 methods->at_put(i, new_method()); 2115 } 2116 if (HAS_PENDING_EXCEPTION) { 2117 Symbol* ex_name = PENDING_EXCEPTION->klass()->name(); 2118 log_info(redefine, class, load, exceptions)("rewrite_cp_refs_in_method exception: '%s'", ex_name->as_C_string()); 2119 // Need to clear pending exception here as the super caller sets 2120 // the JVMTI_ERROR_INTERNAL if the returned value is false. 2121 CLEAR_PENDING_EXCEPTION; 2122 return false; 2123 } 2124 } 2125 2126 return true; 2127 } 2128 2129 2130 // Rewrite constant pool references in the specific method. This code 2131 // was adapted from Rewriter::rewrite_method(). 2132 void VM_RedefineClasses::rewrite_cp_refs_in_method(methodHandle method, 2133 methodHandle *new_method_p, TRAPS) { 2134 2135 *new_method_p = methodHandle(); // default is no new method 2136 2137 // We cache a pointer to the bytecodes here in code_base. If GC 2138 // moves the Method*, then the bytecodes will also move which 2139 // will likely cause a crash. We create a NoSafepointVerifier 2140 // object to detect whether we pass a possible safepoint in this 2141 // code block. 2142 NoSafepointVerifier nsv; 2143 2144 // Bytecodes and their length 2145 address code_base = method->code_base(); 2146 int code_length = method->code_size(); 2147 2148 int bc_length; 2149 for (int bci = 0; bci < code_length; bci += bc_length) { 2150 address bcp = code_base + bci; 2151 Bytecodes::Code c = (Bytecodes::Code)(*bcp); 2152 2153 bc_length = Bytecodes::length_for(c); 2154 if (bc_length == 0) { 2155 // More complicated bytecodes report a length of zero so 2156 // we have to try again a slightly different way. 2157 bc_length = Bytecodes::length_at(method(), bcp); 2158 } 2159 2160 assert(bc_length != 0, "impossible bytecode length"); 2161 2162 switch (c) { 2163 case Bytecodes::_ldc: 2164 { 2165 u1 cp_index = *(bcp + 1); 2166 u2 new_index = find_new_index(cp_index); 2167 2168 if (StressLdcRewrite && new_index == 0) { 2169 // If we are stressing ldc -> ldc_w rewriting, then we 2170 // always need a new_index value. 2171 new_index = cp_index; 2172 } 2173 if (new_index != 0) { 2174 // the original index is mapped so we have more work to do 2175 if (!StressLdcRewrite && new_index <= max_jubyte) { 2176 // The new value can still use ldc instead of ldc_w 2177 // unless we are trying to stress ldc -> ldc_w rewriting 2178 log_trace(redefine, class, constantpool) 2179 ("%s@" INTPTR_FORMAT " old=%d, new=%d", Bytecodes::name(c), p2i(bcp), cp_index, new_index); 2180 // We checked that new_index fits in a u1 so this cast is safe 2181 *(bcp + 1) = (u1)new_index; 2182 } else { 2183 log_trace(redefine, class, constantpool) 2184 ("%s->ldc_w@" INTPTR_FORMAT " old=%d, new=%d", Bytecodes::name(c), p2i(bcp), cp_index, new_index); 2185 // the new value needs ldc_w instead of ldc 2186 u_char inst_buffer[4]; // max instruction size is 4 bytes 2187 bcp = (address)inst_buffer; 2188 // construct new instruction sequence 2189 *bcp = Bytecodes::_ldc_w; 2190 bcp++; 2191 // Rewriter::rewrite_method() does not rewrite ldc -> ldc_w. 2192 // See comment below for difference between put_Java_u2() 2193 // and put_native_u2(). 2194 Bytes::put_Java_u2(bcp, new_index); 2195 2196 Relocator rc(method, nullptr /* no RelocatorListener needed */); 2197 methodHandle m; 2198 { 2199 PauseNoSafepointVerifier pnsv(&nsv); 2200 2201 // ldc is 2 bytes and ldc_w is 3 bytes 2202 m = rc.insert_space_at(bci, 3, inst_buffer, CHECK); 2203 } 2204 2205 // return the new method so that the caller can update 2206 // the containing class 2207 *new_method_p = method = m; 2208 // switch our bytecode processing loop from the old method 2209 // to the new method 2210 code_base = method->code_base(); 2211 code_length = method->code_size(); 2212 bcp = code_base + bci; 2213 c = (Bytecodes::Code)(*bcp); 2214 bc_length = Bytecodes::length_for(c); 2215 assert(bc_length != 0, "sanity check"); 2216 } // end we need ldc_w instead of ldc 2217 } // end if there is a mapped index 2218 } break; 2219 2220 // these bytecodes have a two-byte constant pool index 2221 case Bytecodes::_anewarray : // fall through 2222 case Bytecodes::_checkcast : // fall through 2223 case Bytecodes::_getfield : // fall through 2224 case Bytecodes::_getstatic : // fall through 2225 case Bytecodes::_instanceof : // fall through 2226 case Bytecodes::_invokedynamic : // fall through 2227 case Bytecodes::_invokeinterface: // fall through 2228 case Bytecodes::_invokespecial : // fall through 2229 case Bytecodes::_invokestatic : // fall through 2230 case Bytecodes::_invokevirtual : // fall through 2231 case Bytecodes::_ldc_w : // fall through 2232 case Bytecodes::_ldc2_w : // fall through 2233 case Bytecodes::_multianewarray : // fall through 2234 case Bytecodes::_new : // fall through 2235 case Bytecodes::_putfield : // fall through 2236 case Bytecodes::_putstatic : 2237 { 2238 address p = bcp + 1; 2239 int cp_index = Bytes::get_Java_u2(p); 2240 u2 new_index = find_new_index(cp_index); 2241 if (new_index != 0) { 2242 // the original index is mapped so update w/ new value 2243 log_trace(redefine, class, constantpool) 2244 ("%s@" INTPTR_FORMAT " old=%d, new=%d", Bytecodes::name(c),p2i(bcp), cp_index, new_index); 2245 // Rewriter::rewrite_method() uses put_native_u2() in this 2246 // situation because it is reusing the constant pool index 2247 // location for a native index into the ConstantPoolCache. 2248 // Since we are updating the constant pool index prior to 2249 // verification and ConstantPoolCache initialization, we 2250 // need to keep the new index in Java byte order. 2251 Bytes::put_Java_u2(p, new_index); 2252 } 2253 } break; 2254 default: 2255 break; 2256 } 2257 } // end for each bytecode 2258 } // end rewrite_cp_refs_in_method() 2259 2260 2261 // Rewrite constant pool references in the class_annotations field. 2262 bool VM_RedefineClasses::rewrite_cp_refs_in_class_annotations(InstanceKlass* scratch_class) { 2263 2264 AnnotationArray* class_annotations = scratch_class->class_annotations(); 2265 if (class_annotations == nullptr || class_annotations->length() == 0) { 2266 // no class_annotations so nothing to do 2267 return true; 2268 } 2269 2270 log_debug(redefine, class, annotation)("class_annotations length=%d", class_annotations->length()); 2271 2272 int byte_i = 0; // byte index into class_annotations 2273 return rewrite_cp_refs_in_annotations_typeArray(class_annotations, byte_i); 2274 } 2275 2276 2277 // Rewrite constant pool references in an annotations typeArray. This 2278 // "structure" is adapted from the RuntimeVisibleAnnotations_attribute 2279 // that is described in section 4.8.15 of the 2nd-edition of the VM spec: 2280 // 2281 // annotations_typeArray { 2282 // u2 num_annotations; 2283 // annotation annotations[num_annotations]; 2284 // } 2285 // 2286 bool VM_RedefineClasses::rewrite_cp_refs_in_annotations_typeArray( 2287 AnnotationArray* annotations_typeArray, int &byte_i_ref) { 2288 2289 if ((byte_i_ref + 2) > annotations_typeArray->length()) { 2290 // not enough room for num_annotations field 2291 log_debug(redefine, class, annotation)("length() is too small for num_annotations field"); 2292 return false; 2293 } 2294 2295 u2 num_annotations = Bytes::get_Java_u2((address) 2296 annotations_typeArray->adr_at(byte_i_ref)); 2297 byte_i_ref += 2; 2298 2299 log_debug(redefine, class, annotation)("num_annotations=%d", num_annotations); 2300 2301 int calc_num_annotations = 0; 2302 for (; calc_num_annotations < num_annotations; calc_num_annotations++) { 2303 if (!rewrite_cp_refs_in_annotation_struct(annotations_typeArray, byte_i_ref)) { 2304 log_debug(redefine, class, annotation)("bad annotation_struct at %d", calc_num_annotations); 2305 // propagate failure back to caller 2306 return false; 2307 } 2308 } 2309 assert(num_annotations == calc_num_annotations, "sanity check"); 2310 2311 return true; 2312 } // end rewrite_cp_refs_in_annotations_typeArray() 2313 2314 2315 // Rewrite constant pool references in the annotation struct portion of 2316 // an annotations_typeArray. This "structure" is from section 4.8.15 of 2317 // the 2nd-edition of the VM spec: 2318 // 2319 // struct annotation { 2320 // u2 type_index; 2321 // u2 num_element_value_pairs; 2322 // { 2323 // u2 element_name_index; 2324 // element_value value; 2325 // } element_value_pairs[num_element_value_pairs]; 2326 // } 2327 // 2328 bool VM_RedefineClasses::rewrite_cp_refs_in_annotation_struct( 2329 AnnotationArray* annotations_typeArray, int &byte_i_ref) { 2330 if ((byte_i_ref + 2 + 2) > annotations_typeArray->length()) { 2331 // not enough room for smallest annotation_struct 2332 log_debug(redefine, class, annotation)("length() is too small for annotation_struct"); 2333 return false; 2334 } 2335 2336 u2 type_index = rewrite_cp_ref_in_annotation_data(annotations_typeArray, 2337 byte_i_ref, "type_index"); 2338 2339 u2 num_element_value_pairs = Bytes::get_Java_u2((address) 2340 annotations_typeArray->adr_at(byte_i_ref)); 2341 byte_i_ref += 2; 2342 2343 log_debug(redefine, class, annotation) 2344 ("type_index=%d num_element_value_pairs=%d", type_index, num_element_value_pairs); 2345 2346 int calc_num_element_value_pairs = 0; 2347 for (; calc_num_element_value_pairs < num_element_value_pairs; 2348 calc_num_element_value_pairs++) { 2349 if ((byte_i_ref + 2) > annotations_typeArray->length()) { 2350 // not enough room for another element_name_index, let alone 2351 // the rest of another component 2352 log_debug(redefine, class, annotation)("length() is too small for element_name_index"); 2353 return false; 2354 } 2355 2356 u2 element_name_index = rewrite_cp_ref_in_annotation_data( 2357 annotations_typeArray, byte_i_ref, 2358 "element_name_index"); 2359 2360 log_debug(redefine, class, annotation)("element_name_index=%d", element_name_index); 2361 2362 if (!rewrite_cp_refs_in_element_value(annotations_typeArray, byte_i_ref)) { 2363 log_debug(redefine, class, annotation)("bad element_value at %d", calc_num_element_value_pairs); 2364 // propagate failure back to caller 2365 return false; 2366 } 2367 } // end for each component 2368 assert(num_element_value_pairs == calc_num_element_value_pairs, 2369 "sanity check"); 2370 2371 return true; 2372 } // end rewrite_cp_refs_in_annotation_struct() 2373 2374 2375 // Rewrite a constant pool reference at the current position in 2376 // annotations_typeArray if needed. Returns the original constant 2377 // pool reference if a rewrite was not needed or the new constant 2378 // pool reference if a rewrite was needed. 2379 u2 VM_RedefineClasses::rewrite_cp_ref_in_annotation_data( 2380 AnnotationArray* annotations_typeArray, int &byte_i_ref, 2381 const char * trace_mesg) { 2382 2383 address cp_index_addr = (address) 2384 annotations_typeArray->adr_at(byte_i_ref); 2385 u2 old_cp_index = Bytes::get_Java_u2(cp_index_addr); 2386 u2 new_cp_index = find_new_index(old_cp_index); 2387 if (new_cp_index != 0) { 2388 log_debug(redefine, class, annotation)("mapped old %s=%d", trace_mesg, old_cp_index); 2389 Bytes::put_Java_u2(cp_index_addr, new_cp_index); 2390 old_cp_index = new_cp_index; 2391 } 2392 byte_i_ref += 2; 2393 return old_cp_index; 2394 } 2395 2396 2397 // Rewrite constant pool references in the element_value portion of an 2398 // annotations_typeArray. This "structure" is from section 4.8.15.1 of 2399 // the 2nd-edition of the VM spec: 2400 // 2401 // struct element_value { 2402 // u1 tag; 2403 // union { 2404 // u2 const_value_index; 2405 // { 2406 // u2 type_name_index; 2407 // u2 const_name_index; 2408 // } enum_const_value; 2409 // u2 class_info_index; 2410 // annotation annotation_value; 2411 // struct { 2412 // u2 num_values; 2413 // element_value values[num_values]; 2414 // } array_value; 2415 // } value; 2416 // } 2417 // 2418 bool VM_RedefineClasses::rewrite_cp_refs_in_element_value( 2419 AnnotationArray* annotations_typeArray, int &byte_i_ref) { 2420 2421 if ((byte_i_ref + 1) > annotations_typeArray->length()) { 2422 // not enough room for a tag let alone the rest of an element_value 2423 log_debug(redefine, class, annotation)("length() is too small for a tag"); 2424 return false; 2425 } 2426 2427 u1 tag = annotations_typeArray->at(byte_i_ref); 2428 byte_i_ref++; 2429 log_debug(redefine, class, annotation)("tag='%c'", tag); 2430 2431 switch (tag) { 2432 // These BaseType tag values are from Table 4.2 in VM spec: 2433 case JVM_SIGNATURE_BYTE: 2434 case JVM_SIGNATURE_CHAR: 2435 case JVM_SIGNATURE_DOUBLE: 2436 case JVM_SIGNATURE_FLOAT: 2437 case JVM_SIGNATURE_INT: 2438 case JVM_SIGNATURE_LONG: 2439 case JVM_SIGNATURE_SHORT: 2440 case JVM_SIGNATURE_BOOLEAN: 2441 2442 // The remaining tag values are from Table 4.8 in the 2nd-edition of 2443 // the VM spec: 2444 case 's': 2445 { 2446 // For the above tag values (including the BaseType values), 2447 // value.const_value_index is right union field. 2448 2449 if ((byte_i_ref + 2) > annotations_typeArray->length()) { 2450 // not enough room for a const_value_index 2451 log_debug(redefine, class, annotation)("length() is too small for a const_value_index"); 2452 return false; 2453 } 2454 2455 u2 const_value_index = rewrite_cp_ref_in_annotation_data( 2456 annotations_typeArray, byte_i_ref, 2457 "const_value_index"); 2458 2459 log_debug(redefine, class, annotation)("const_value_index=%d", const_value_index); 2460 } break; 2461 2462 case 'e': 2463 { 2464 // for the above tag value, value.enum_const_value is right union field 2465 2466 if ((byte_i_ref + 4) > annotations_typeArray->length()) { 2467 // not enough room for a enum_const_value 2468 log_debug(redefine, class, annotation)("length() is too small for a enum_const_value"); 2469 return false; 2470 } 2471 2472 u2 type_name_index = rewrite_cp_ref_in_annotation_data( 2473 annotations_typeArray, byte_i_ref, 2474 "type_name_index"); 2475 2476 u2 const_name_index = rewrite_cp_ref_in_annotation_data( 2477 annotations_typeArray, byte_i_ref, 2478 "const_name_index"); 2479 2480 log_debug(redefine, class, annotation) 2481 ("type_name_index=%d const_name_index=%d", type_name_index, const_name_index); 2482 } break; 2483 2484 case 'c': 2485 { 2486 // for the above tag value, value.class_info_index is right union field 2487 2488 if ((byte_i_ref + 2) > annotations_typeArray->length()) { 2489 // not enough room for a class_info_index 2490 log_debug(redefine, class, annotation)("length() is too small for a class_info_index"); 2491 return false; 2492 } 2493 2494 u2 class_info_index = rewrite_cp_ref_in_annotation_data( 2495 annotations_typeArray, byte_i_ref, 2496 "class_info_index"); 2497 2498 log_debug(redefine, class, annotation)("class_info_index=%d", class_info_index); 2499 } break; 2500 2501 case '@': 2502 // For the above tag value, value.attr_value is the right union 2503 // field. This is a nested annotation. 2504 if (!rewrite_cp_refs_in_annotation_struct(annotations_typeArray, byte_i_ref)) { 2505 // propagate failure back to caller 2506 return false; 2507 } 2508 break; 2509 2510 case JVM_SIGNATURE_ARRAY: 2511 { 2512 if ((byte_i_ref + 2) > annotations_typeArray->length()) { 2513 // not enough room for a num_values field 2514 log_debug(redefine, class, annotation)("length() is too small for a num_values field"); 2515 return false; 2516 } 2517 2518 // For the above tag value, value.array_value is the right union 2519 // field. This is an array of nested element_value. 2520 u2 num_values = Bytes::get_Java_u2((address) 2521 annotations_typeArray->adr_at(byte_i_ref)); 2522 byte_i_ref += 2; 2523 log_debug(redefine, class, annotation)("num_values=%d", num_values); 2524 2525 int calc_num_values = 0; 2526 for (; calc_num_values < num_values; calc_num_values++) { 2527 if (!rewrite_cp_refs_in_element_value(annotations_typeArray, byte_i_ref)) { 2528 log_debug(redefine, class, annotation)("bad nested element_value at %d", calc_num_values); 2529 // propagate failure back to caller 2530 return false; 2531 } 2532 } 2533 assert(num_values == calc_num_values, "sanity check"); 2534 } break; 2535 2536 default: 2537 log_debug(redefine, class, annotation)("bad tag=0x%x", tag); 2538 return false; 2539 } // end decode tag field 2540 2541 return true; 2542 } // end rewrite_cp_refs_in_element_value() 2543 2544 2545 // Rewrite constant pool references in a fields_annotations field. 2546 bool VM_RedefineClasses::rewrite_cp_refs_in_fields_annotations( 2547 InstanceKlass* scratch_class) { 2548 2549 Array<AnnotationArray*>* fields_annotations = scratch_class->fields_annotations(); 2550 2551 if (fields_annotations == nullptr || fields_annotations->length() == 0) { 2552 // no fields_annotations so nothing to do 2553 return true; 2554 } 2555 2556 log_debug(redefine, class, annotation)("fields_annotations length=%d", fields_annotations->length()); 2557 2558 for (int i = 0; i < fields_annotations->length(); i++) { 2559 AnnotationArray* field_annotations = fields_annotations->at(i); 2560 if (field_annotations == nullptr || field_annotations->length() == 0) { 2561 // this field does not have any annotations so skip it 2562 continue; 2563 } 2564 2565 int byte_i = 0; // byte index into field_annotations 2566 if (!rewrite_cp_refs_in_annotations_typeArray(field_annotations, byte_i)) { 2567 log_debug(redefine, class, annotation)("bad field_annotations at %d", i); 2568 // propagate failure back to caller 2569 return false; 2570 } 2571 } 2572 2573 return true; 2574 } // end rewrite_cp_refs_in_fields_annotations() 2575 2576 2577 // Rewrite constant pool references in a methods_annotations field. 2578 bool VM_RedefineClasses::rewrite_cp_refs_in_methods_annotations( 2579 InstanceKlass* scratch_class) { 2580 2581 for (int i = 0; i < scratch_class->methods()->length(); i++) { 2582 Method* m = scratch_class->methods()->at(i); 2583 AnnotationArray* method_annotations = m->constMethod()->method_annotations(); 2584 2585 if (method_annotations == nullptr || method_annotations->length() == 0) { 2586 // this method does not have any annotations so skip it 2587 continue; 2588 } 2589 2590 int byte_i = 0; // byte index into method_annotations 2591 if (!rewrite_cp_refs_in_annotations_typeArray(method_annotations, byte_i)) { 2592 log_debug(redefine, class, annotation)("bad method_annotations at %d", i); 2593 // propagate failure back to caller 2594 return false; 2595 } 2596 } 2597 2598 return true; 2599 } // end rewrite_cp_refs_in_methods_annotations() 2600 2601 2602 // Rewrite constant pool references in a methods_parameter_annotations 2603 // field. This "structure" is adapted from the 2604 // RuntimeVisibleParameterAnnotations_attribute described in section 2605 // 4.8.17 of the 2nd-edition of the VM spec: 2606 // 2607 // methods_parameter_annotations_typeArray { 2608 // u1 num_parameters; 2609 // { 2610 // u2 num_annotations; 2611 // annotation annotations[num_annotations]; 2612 // } parameter_annotations[num_parameters]; 2613 // } 2614 // 2615 bool VM_RedefineClasses::rewrite_cp_refs_in_methods_parameter_annotations( 2616 InstanceKlass* scratch_class) { 2617 2618 for (int i = 0; i < scratch_class->methods()->length(); i++) { 2619 Method* m = scratch_class->methods()->at(i); 2620 AnnotationArray* method_parameter_annotations = m->constMethod()->parameter_annotations(); 2621 if (method_parameter_annotations == nullptr 2622 || method_parameter_annotations->length() == 0) { 2623 // this method does not have any parameter annotations so skip it 2624 continue; 2625 } 2626 2627 if (method_parameter_annotations->length() < 1) { 2628 // not enough room for a num_parameters field 2629 log_debug(redefine, class, annotation)("length() is too small for a num_parameters field at %d", i); 2630 return false; 2631 } 2632 2633 int byte_i = 0; // byte index into method_parameter_annotations 2634 2635 u1 num_parameters = method_parameter_annotations->at(byte_i); 2636 byte_i++; 2637 2638 log_debug(redefine, class, annotation)("num_parameters=%d", num_parameters); 2639 2640 int calc_num_parameters = 0; 2641 for (; calc_num_parameters < num_parameters; calc_num_parameters++) { 2642 if (!rewrite_cp_refs_in_annotations_typeArray(method_parameter_annotations, byte_i)) { 2643 log_debug(redefine, class, annotation)("bad method_parameter_annotations at %d", calc_num_parameters); 2644 // propagate failure back to caller 2645 return false; 2646 } 2647 } 2648 assert(num_parameters == calc_num_parameters, "sanity check"); 2649 } 2650 2651 return true; 2652 } // end rewrite_cp_refs_in_methods_parameter_annotations() 2653 2654 2655 // Rewrite constant pool references in a methods_default_annotations 2656 // field. This "structure" is adapted from the AnnotationDefault_attribute 2657 // that is described in section 4.8.19 of the 2nd-edition of the VM spec: 2658 // 2659 // methods_default_annotations_typeArray { 2660 // element_value default_value; 2661 // } 2662 // 2663 bool VM_RedefineClasses::rewrite_cp_refs_in_methods_default_annotations( 2664 InstanceKlass* scratch_class) { 2665 2666 for (int i = 0; i < scratch_class->methods()->length(); i++) { 2667 Method* m = scratch_class->methods()->at(i); 2668 AnnotationArray* method_default_annotations = m->constMethod()->default_annotations(); 2669 if (method_default_annotations == nullptr 2670 || method_default_annotations->length() == 0) { 2671 // this method does not have any default annotations so skip it 2672 continue; 2673 } 2674 2675 int byte_i = 0; // byte index into method_default_annotations 2676 2677 if (!rewrite_cp_refs_in_element_value( 2678 method_default_annotations, byte_i)) { 2679 log_debug(redefine, class, annotation)("bad default element_value at %d", i); 2680 // propagate failure back to caller 2681 return false; 2682 } 2683 } 2684 2685 return true; 2686 } // end rewrite_cp_refs_in_methods_default_annotations() 2687 2688 2689 // Rewrite constant pool references in a class_type_annotations field. 2690 bool VM_RedefineClasses::rewrite_cp_refs_in_class_type_annotations( 2691 InstanceKlass* scratch_class) { 2692 2693 AnnotationArray* class_type_annotations = scratch_class->class_type_annotations(); 2694 if (class_type_annotations == nullptr || class_type_annotations->length() == 0) { 2695 // no class_type_annotations so nothing to do 2696 return true; 2697 } 2698 2699 log_debug(redefine, class, annotation)("class_type_annotations length=%d", class_type_annotations->length()); 2700 2701 int byte_i = 0; // byte index into class_type_annotations 2702 return rewrite_cp_refs_in_type_annotations_typeArray(class_type_annotations, 2703 byte_i, "ClassFile"); 2704 } // end rewrite_cp_refs_in_class_type_annotations() 2705 2706 2707 // Rewrite constant pool references in a fields_type_annotations field. 2708 bool VM_RedefineClasses::rewrite_cp_refs_in_fields_type_annotations(InstanceKlass* scratch_class) { 2709 2710 Array<AnnotationArray*>* fields_type_annotations = scratch_class->fields_type_annotations(); 2711 if (fields_type_annotations == nullptr || fields_type_annotations->length() == 0) { 2712 // no fields_type_annotations so nothing to do 2713 return true; 2714 } 2715 2716 log_debug(redefine, class, annotation)("fields_type_annotations length=%d", fields_type_annotations->length()); 2717 2718 for (int i = 0; i < fields_type_annotations->length(); i++) { 2719 AnnotationArray* field_type_annotations = fields_type_annotations->at(i); 2720 if (field_type_annotations == nullptr || field_type_annotations->length() == 0) { 2721 // this field does not have any annotations so skip it 2722 continue; 2723 } 2724 2725 int byte_i = 0; // byte index into field_type_annotations 2726 if (!rewrite_cp_refs_in_type_annotations_typeArray(field_type_annotations, 2727 byte_i, "field_info")) { 2728 log_debug(redefine, class, annotation)("bad field_type_annotations at %d", i); 2729 // propagate failure back to caller 2730 return false; 2731 } 2732 } 2733 2734 return true; 2735 } // end rewrite_cp_refs_in_fields_type_annotations() 2736 2737 2738 // Rewrite constant pool references in a methods_type_annotations field. 2739 bool VM_RedefineClasses::rewrite_cp_refs_in_methods_type_annotations( 2740 InstanceKlass* scratch_class) { 2741 2742 for (int i = 0; i < scratch_class->methods()->length(); i++) { 2743 Method* m = scratch_class->methods()->at(i); 2744 AnnotationArray* method_type_annotations = m->constMethod()->type_annotations(); 2745 2746 if (method_type_annotations == nullptr || method_type_annotations->length() == 0) { 2747 // this method does not have any annotations so skip it 2748 continue; 2749 } 2750 2751 log_debug(redefine, class, annotation)("methods type_annotations length=%d", method_type_annotations->length()); 2752 2753 int byte_i = 0; // byte index into method_type_annotations 2754 if (!rewrite_cp_refs_in_type_annotations_typeArray(method_type_annotations, 2755 byte_i, "method_info")) { 2756 log_debug(redefine, class, annotation)("bad method_type_annotations at %d", i); 2757 // propagate failure back to caller 2758 return false; 2759 } 2760 } 2761 2762 return true; 2763 } // end rewrite_cp_refs_in_methods_type_annotations() 2764 2765 2766 // Rewrite constant pool references in a type_annotations 2767 // field. This "structure" is adapted from the 2768 // RuntimeVisibleTypeAnnotations_attribute described in 2769 // section 4.7.20 of the Java SE 8 Edition of the VM spec: 2770 // 2771 // type_annotations_typeArray { 2772 // u2 num_annotations; 2773 // type_annotation annotations[num_annotations]; 2774 // } 2775 // 2776 bool VM_RedefineClasses::rewrite_cp_refs_in_type_annotations_typeArray( 2777 AnnotationArray* type_annotations_typeArray, int &byte_i_ref, 2778 const char * location_mesg) { 2779 2780 if ((byte_i_ref + 2) > type_annotations_typeArray->length()) { 2781 // not enough room for num_annotations field 2782 log_debug(redefine, class, annotation)("length() is too small for num_annotations field"); 2783 return false; 2784 } 2785 2786 u2 num_annotations = Bytes::get_Java_u2((address) 2787 type_annotations_typeArray->adr_at(byte_i_ref)); 2788 byte_i_ref += 2; 2789 2790 log_debug(redefine, class, annotation)("num_type_annotations=%d", num_annotations); 2791 2792 int calc_num_annotations = 0; 2793 for (; calc_num_annotations < num_annotations; calc_num_annotations++) { 2794 if (!rewrite_cp_refs_in_type_annotation_struct(type_annotations_typeArray, 2795 byte_i_ref, location_mesg)) { 2796 log_debug(redefine, class, annotation)("bad type_annotation_struct at %d", calc_num_annotations); 2797 // propagate failure back to caller 2798 return false; 2799 } 2800 } 2801 assert(num_annotations == calc_num_annotations, "sanity check"); 2802 2803 if (byte_i_ref != type_annotations_typeArray->length()) { 2804 log_debug(redefine, class, annotation) 2805 ("read wrong amount of bytes at end of processing type_annotations_typeArray (%d of %d bytes were read)", 2806 byte_i_ref, type_annotations_typeArray->length()); 2807 return false; 2808 } 2809 2810 return true; 2811 } // end rewrite_cp_refs_in_type_annotations_typeArray() 2812 2813 2814 // Rewrite constant pool references in a type_annotation 2815 // field. This "structure" is adapted from the 2816 // RuntimeVisibleTypeAnnotations_attribute described in 2817 // section 4.7.20 of the Java SE 8 Edition of the VM spec: 2818 // 2819 // type_annotation { 2820 // u1 target_type; 2821 // union { 2822 // type_parameter_target; 2823 // supertype_target; 2824 // type_parameter_bound_target; 2825 // empty_target; 2826 // method_formal_parameter_target; 2827 // throws_target; 2828 // localvar_target; 2829 // catch_target; 2830 // offset_target; 2831 // type_argument_target; 2832 // } target_info; 2833 // type_path target_path; 2834 // annotation anno; 2835 // } 2836 // 2837 bool VM_RedefineClasses::rewrite_cp_refs_in_type_annotation_struct( 2838 AnnotationArray* type_annotations_typeArray, int &byte_i_ref, 2839 const char * location_mesg) { 2840 2841 if (!skip_type_annotation_target(type_annotations_typeArray, 2842 byte_i_ref, location_mesg)) { 2843 return false; 2844 } 2845 2846 if (!skip_type_annotation_type_path(type_annotations_typeArray, byte_i_ref)) { 2847 return false; 2848 } 2849 2850 if (!rewrite_cp_refs_in_annotation_struct(type_annotations_typeArray, byte_i_ref)) { 2851 return false; 2852 } 2853 2854 return true; 2855 } // end rewrite_cp_refs_in_type_annotation_struct() 2856 2857 2858 // Read, verify and skip over the target_type and target_info part 2859 // so that rewriting can continue in the later parts of the struct. 2860 // 2861 // u1 target_type; 2862 // union { 2863 // type_parameter_target; 2864 // supertype_target; 2865 // type_parameter_bound_target; 2866 // empty_target; 2867 // method_formal_parameter_target; 2868 // throws_target; 2869 // localvar_target; 2870 // catch_target; 2871 // offset_target; 2872 // type_argument_target; 2873 // } target_info; 2874 // 2875 bool VM_RedefineClasses::skip_type_annotation_target( 2876 AnnotationArray* type_annotations_typeArray, int &byte_i_ref, 2877 const char * location_mesg) { 2878 2879 if ((byte_i_ref + 1) > type_annotations_typeArray->length()) { 2880 // not enough room for a target_type let alone the rest of a type_annotation 2881 log_debug(redefine, class, annotation)("length() is too small for a target_type"); 2882 return false; 2883 } 2884 2885 u1 target_type = type_annotations_typeArray->at(byte_i_ref); 2886 byte_i_ref += 1; 2887 log_debug(redefine, class, annotation)("target_type=0x%.2x", target_type); 2888 log_debug(redefine, class, annotation)("location=%s", location_mesg); 2889 2890 // Skip over target_info 2891 switch (target_type) { 2892 case 0x00: 2893 // kind: type parameter declaration of generic class or interface 2894 // location: ClassFile 2895 case 0x01: 2896 // kind: type parameter declaration of generic method or constructor 2897 // location: method_info 2898 2899 { 2900 // struct: 2901 // type_parameter_target { 2902 // u1 type_parameter_index; 2903 // } 2904 // 2905 if ((byte_i_ref + 1) > type_annotations_typeArray->length()) { 2906 log_debug(redefine, class, annotation)("length() is too small for a type_parameter_target"); 2907 return false; 2908 } 2909 2910 u1 type_parameter_index = type_annotations_typeArray->at(byte_i_ref); 2911 byte_i_ref += 1; 2912 2913 log_debug(redefine, class, annotation)("type_parameter_target: type_parameter_index=%d", type_parameter_index); 2914 } break; 2915 2916 case 0x10: 2917 // kind: type in extends clause of class or interface declaration 2918 // or in implements clause of interface declaration 2919 // location: ClassFile 2920 2921 { 2922 // struct: 2923 // supertype_target { 2924 // u2 supertype_index; 2925 // } 2926 // 2927 if ((byte_i_ref + 2) > type_annotations_typeArray->length()) { 2928 log_debug(redefine, class, annotation)("length() is too small for a supertype_target"); 2929 return false; 2930 } 2931 2932 u2 supertype_index = Bytes::get_Java_u2((address) 2933 type_annotations_typeArray->adr_at(byte_i_ref)); 2934 byte_i_ref += 2; 2935 2936 log_debug(redefine, class, annotation)("supertype_target: supertype_index=%d", supertype_index); 2937 } break; 2938 2939 case 0x11: 2940 // kind: type in bound of type parameter declaration of generic class or interface 2941 // location: ClassFile 2942 case 0x12: 2943 // kind: type in bound of type parameter declaration of generic method or constructor 2944 // location: method_info 2945 2946 { 2947 // struct: 2948 // type_parameter_bound_target { 2949 // u1 type_parameter_index; 2950 // u1 bound_index; 2951 // } 2952 // 2953 if ((byte_i_ref + 2) > type_annotations_typeArray->length()) { 2954 log_debug(redefine, class, annotation)("length() is too small for a type_parameter_bound_target"); 2955 return false; 2956 } 2957 2958 u1 type_parameter_index = type_annotations_typeArray->at(byte_i_ref); 2959 byte_i_ref += 1; 2960 u1 bound_index = type_annotations_typeArray->at(byte_i_ref); 2961 byte_i_ref += 1; 2962 2963 log_debug(redefine, class, annotation) 2964 ("type_parameter_bound_target: type_parameter_index=%d, bound_index=%d", type_parameter_index, bound_index); 2965 } break; 2966 2967 case 0x13: 2968 // kind: type in field declaration 2969 // location: field_info 2970 case 0x14: 2971 // kind: return type of method, or type of newly constructed object 2972 // location: method_info 2973 case 0x15: 2974 // kind: receiver type of method or constructor 2975 // location: method_info 2976 2977 { 2978 // struct: 2979 // empty_target { 2980 // } 2981 // 2982 log_debug(redefine, class, annotation)("empty_target"); 2983 } break; 2984 2985 case 0x16: 2986 // kind: type in formal parameter declaration of method, constructor, or lambda expression 2987 // location: method_info 2988 2989 { 2990 // struct: 2991 // formal_parameter_target { 2992 // u1 formal_parameter_index; 2993 // } 2994 // 2995 if ((byte_i_ref + 1) > type_annotations_typeArray->length()) { 2996 log_debug(redefine, class, annotation)("length() is too small for a formal_parameter_target"); 2997 return false; 2998 } 2999 3000 u1 formal_parameter_index = type_annotations_typeArray->at(byte_i_ref); 3001 byte_i_ref += 1; 3002 3003 log_debug(redefine, class, annotation) 3004 ("formal_parameter_target: formal_parameter_index=%d", formal_parameter_index); 3005 } break; 3006 3007 case 0x17: 3008 // kind: type in throws clause of method or constructor 3009 // location: method_info 3010 3011 { 3012 // struct: 3013 // throws_target { 3014 // u2 throws_type_index 3015 // } 3016 // 3017 if ((byte_i_ref + 2) > type_annotations_typeArray->length()) { 3018 log_debug(redefine, class, annotation)("length() is too small for a throws_target"); 3019 return false; 3020 } 3021 3022 u2 throws_type_index = Bytes::get_Java_u2((address) 3023 type_annotations_typeArray->adr_at(byte_i_ref)); 3024 byte_i_ref += 2; 3025 3026 log_debug(redefine, class, annotation)("throws_target: throws_type_index=%d", throws_type_index); 3027 } break; 3028 3029 case 0x40: 3030 // kind: type in local variable declaration 3031 // location: Code 3032 case 0x41: 3033 // kind: type in resource variable declaration 3034 // location: Code 3035 3036 { 3037 // struct: 3038 // localvar_target { 3039 // u2 table_length; 3040 // struct { 3041 // u2 start_pc; 3042 // u2 length; 3043 // u2 index; 3044 // } table[table_length]; 3045 // } 3046 // 3047 if ((byte_i_ref + 2) > type_annotations_typeArray->length()) { 3048 // not enough room for a table_length let alone the rest of a localvar_target 3049 log_debug(redefine, class, annotation)("length() is too small for a localvar_target table_length"); 3050 return false; 3051 } 3052 3053 u2 table_length = Bytes::get_Java_u2((address) 3054 type_annotations_typeArray->adr_at(byte_i_ref)); 3055 byte_i_ref += 2; 3056 3057 log_debug(redefine, class, annotation)("localvar_target: table_length=%d", table_length); 3058 3059 int table_struct_size = 2 + 2 + 2; // 3 u2 variables per table entry 3060 int table_size = table_length * table_struct_size; 3061 3062 if ((byte_i_ref + table_size) > type_annotations_typeArray->length()) { 3063 // not enough room for a table 3064 log_debug(redefine, class, annotation)("length() is too small for a table array of length %d", table_length); 3065 return false; 3066 } 3067 3068 // Skip over table 3069 byte_i_ref += table_size; 3070 } break; 3071 3072 case 0x42: 3073 // kind: type in exception parameter declaration 3074 // location: Code 3075 3076 { 3077 // struct: 3078 // catch_target { 3079 // u2 exception_table_index; 3080 // } 3081 // 3082 if ((byte_i_ref + 2) > type_annotations_typeArray->length()) { 3083 log_debug(redefine, class, annotation)("length() is too small for a catch_target"); 3084 return false; 3085 } 3086 3087 u2 exception_table_index = Bytes::get_Java_u2((address) 3088 type_annotations_typeArray->adr_at(byte_i_ref)); 3089 byte_i_ref += 2; 3090 3091 log_debug(redefine, class, annotation)("catch_target: exception_table_index=%d", exception_table_index); 3092 } break; 3093 3094 case 0x43: 3095 // kind: type in instanceof expression 3096 // location: Code 3097 case 0x44: 3098 // kind: type in new expression 3099 // location: Code 3100 case 0x45: 3101 // kind: type in method reference expression using ::new 3102 // location: Code 3103 case 0x46: 3104 // kind: type in method reference expression using ::Identifier 3105 // location: Code 3106 3107 { 3108 // struct: 3109 // offset_target { 3110 // u2 offset; 3111 // } 3112 // 3113 if ((byte_i_ref + 2) > type_annotations_typeArray->length()) { 3114 log_debug(redefine, class, annotation)("length() is too small for a offset_target"); 3115 return false; 3116 } 3117 3118 u2 offset = Bytes::get_Java_u2((address) 3119 type_annotations_typeArray->adr_at(byte_i_ref)); 3120 byte_i_ref += 2; 3121 3122 log_debug(redefine, class, annotation)("offset_target: offset=%d", offset); 3123 } break; 3124 3125 case 0x47: 3126 // kind: type in cast expression 3127 // location: Code 3128 case 0x48: 3129 // kind: type argument for generic constructor in new expression or 3130 // explicit constructor invocation statement 3131 // location: Code 3132 case 0x49: 3133 // kind: type argument for generic method in method invocation expression 3134 // location: Code 3135 case 0x4A: 3136 // kind: type argument for generic constructor in method reference expression using ::new 3137 // location: Code 3138 case 0x4B: 3139 // kind: type argument for generic method in method reference expression using ::Identifier 3140 // location: Code 3141 3142 { 3143 // struct: 3144 // type_argument_target { 3145 // u2 offset; 3146 // u1 type_argument_index; 3147 // } 3148 // 3149 if ((byte_i_ref + 3) > type_annotations_typeArray->length()) { 3150 log_debug(redefine, class, annotation)("length() is too small for a type_argument_target"); 3151 return false; 3152 } 3153 3154 u2 offset = Bytes::get_Java_u2((address) 3155 type_annotations_typeArray->adr_at(byte_i_ref)); 3156 byte_i_ref += 2; 3157 u1 type_argument_index = type_annotations_typeArray->at(byte_i_ref); 3158 byte_i_ref += 1; 3159 3160 log_debug(redefine, class, annotation) 3161 ("type_argument_target: offset=%d, type_argument_index=%d", offset, type_argument_index); 3162 } break; 3163 3164 default: 3165 log_debug(redefine, class, annotation)("unknown target_type"); 3166 #ifdef ASSERT 3167 ShouldNotReachHere(); 3168 #endif 3169 return false; 3170 } 3171 3172 return true; 3173 } // end skip_type_annotation_target() 3174 3175 3176 // Read, verify and skip over the type_path part so that rewriting 3177 // can continue in the later parts of the struct. 3178 // 3179 // type_path { 3180 // u1 path_length; 3181 // { 3182 // u1 type_path_kind; 3183 // u1 type_argument_index; 3184 // } path[path_length]; 3185 // } 3186 // 3187 bool VM_RedefineClasses::skip_type_annotation_type_path( 3188 AnnotationArray* type_annotations_typeArray, int &byte_i_ref) { 3189 3190 if ((byte_i_ref + 1) > type_annotations_typeArray->length()) { 3191 // not enough room for a path_length let alone the rest of the type_path 3192 log_debug(redefine, class, annotation)("length() is too small for a type_path"); 3193 return false; 3194 } 3195 3196 u1 path_length = type_annotations_typeArray->at(byte_i_ref); 3197 byte_i_ref += 1; 3198 3199 log_debug(redefine, class, annotation)("type_path: path_length=%d", path_length); 3200 3201 int calc_path_length = 0; 3202 for (; calc_path_length < path_length; calc_path_length++) { 3203 if ((byte_i_ref + 1 + 1) > type_annotations_typeArray->length()) { 3204 // not enough room for a path 3205 log_debug(redefine, class, annotation) 3206 ("length() is too small for path entry %d of %d", calc_path_length, path_length); 3207 return false; 3208 } 3209 3210 u1 type_path_kind = type_annotations_typeArray->at(byte_i_ref); 3211 byte_i_ref += 1; 3212 u1 type_argument_index = type_annotations_typeArray->at(byte_i_ref); 3213 byte_i_ref += 1; 3214 3215 log_debug(redefine, class, annotation) 3216 ("type_path: path[%d]: type_path_kind=%d, type_argument_index=%d", 3217 calc_path_length, type_path_kind, type_argument_index); 3218 3219 if (type_path_kind > 3 || (type_path_kind != 3 && type_argument_index != 0)) { 3220 // not enough room for a path 3221 log_debug(redefine, class, annotation)("inconsistent type_path values"); 3222 return false; 3223 } 3224 } 3225 assert(path_length == calc_path_length, "sanity check"); 3226 3227 return true; 3228 } // end skip_type_annotation_type_path() 3229 3230 3231 // Rewrite constant pool references in the method's stackmap table. 3232 // These "structures" are adapted from the StackMapTable_attribute that 3233 // is described in section 4.8.4 of the 6.0 version of the VM spec 3234 // (dated 2005.10.26): 3235 // file:///net/quincunx.sfbay/export/gbracha/ClassFile-Java6.pdf 3236 // 3237 // stack_map { 3238 // u2 number_of_entries; 3239 // stack_map_frame entries[number_of_entries]; 3240 // } 3241 // 3242 void VM_RedefineClasses::rewrite_cp_refs_in_stack_map_table( 3243 const methodHandle& method) { 3244 3245 if (!method->has_stackmap_table()) { 3246 return; 3247 } 3248 3249 AnnotationArray* stackmap_data = method->stackmap_data(); 3250 address stackmap_p = (address)stackmap_data->adr_at(0); 3251 address stackmap_end = stackmap_p + stackmap_data->length(); 3252 3253 assert(stackmap_p + 2 <= stackmap_end, "no room for number_of_entries"); 3254 u2 number_of_entries = Bytes::get_Java_u2(stackmap_p); 3255 stackmap_p += 2; 3256 3257 log_debug(redefine, class, stackmap)("number_of_entries=%u", number_of_entries); 3258 3259 // walk through each stack_map_frame 3260 u2 calc_number_of_entries = 0; 3261 for (; calc_number_of_entries < number_of_entries; calc_number_of_entries++) { 3262 // The stack_map_frame structure is a u1 frame_type followed by 3263 // 0 or more bytes of data: 3264 // 3265 // union stack_map_frame { 3266 // same_frame; 3267 // same_locals_1_stack_item_frame; 3268 // same_locals_1_stack_item_frame_extended; 3269 // chop_frame; 3270 // same_frame_extended; 3271 // append_frame; 3272 // full_frame; 3273 // } 3274 3275 assert(stackmap_p + 1 <= stackmap_end, "no room for frame_type"); 3276 u1 frame_type = *stackmap_p; 3277 stackmap_p++; 3278 3279 // same_frame { 3280 // u1 frame_type = SAME; /* 0-63 */ 3281 // } 3282 if (frame_type <= 63) { 3283 // nothing more to do for same_frame 3284 } 3285 3286 // same_locals_1_stack_item_frame { 3287 // u1 frame_type = SAME_LOCALS_1_STACK_ITEM; /* 64-127 */ 3288 // verification_type_info stack[1]; 3289 // } 3290 else if (frame_type >= 64 && frame_type <= 127) { 3291 rewrite_cp_refs_in_verification_type_info(stackmap_p, stackmap_end, 3292 calc_number_of_entries, frame_type); 3293 } 3294 3295 // reserved for future use 3296 else if (frame_type >= 128 && frame_type <= 246) { 3297 // nothing more to do for reserved frame_types 3298 } 3299 3300 // same_locals_1_stack_item_frame_extended { 3301 // u1 frame_type = SAME_LOCALS_1_STACK_ITEM_EXTENDED; /* 247 */ 3302 // u2 offset_delta; 3303 // verification_type_info stack[1]; 3304 // } 3305 else if (frame_type == 247) { 3306 stackmap_p += 2; 3307 rewrite_cp_refs_in_verification_type_info(stackmap_p, stackmap_end, 3308 calc_number_of_entries, frame_type); 3309 } 3310 3311 // chop_frame { 3312 // u1 frame_type = CHOP; /* 248-250 */ 3313 // u2 offset_delta; 3314 // } 3315 else if (frame_type >= 248 && frame_type <= 250) { 3316 stackmap_p += 2; 3317 } 3318 3319 // same_frame_extended { 3320 // u1 frame_type = SAME_FRAME_EXTENDED; /* 251*/ 3321 // u2 offset_delta; 3322 // } 3323 else if (frame_type == 251) { 3324 stackmap_p += 2; 3325 } 3326 3327 // append_frame { 3328 // u1 frame_type = APPEND; /* 252-254 */ 3329 // u2 offset_delta; 3330 // verification_type_info locals[frame_type - 251]; 3331 // } 3332 else if (frame_type >= 252 && frame_type <= 254) { 3333 assert(stackmap_p + 2 <= stackmap_end, 3334 "no room for offset_delta"); 3335 stackmap_p += 2; 3336 u1 len = frame_type - 251; 3337 for (u1 i = 0; i < len; i++) { 3338 rewrite_cp_refs_in_verification_type_info(stackmap_p, stackmap_end, 3339 calc_number_of_entries, frame_type); 3340 } 3341 } 3342 3343 // full_frame { 3344 // u1 frame_type = FULL_FRAME; /* 255 */ 3345 // u2 offset_delta; 3346 // u2 number_of_locals; 3347 // verification_type_info locals[number_of_locals]; 3348 // u2 number_of_stack_items; 3349 // verification_type_info stack[number_of_stack_items]; 3350 // } 3351 else if (frame_type == 255) { 3352 assert(stackmap_p + 2 + 2 <= stackmap_end, 3353 "no room for smallest full_frame"); 3354 stackmap_p += 2; 3355 3356 u2 number_of_locals = Bytes::get_Java_u2(stackmap_p); 3357 stackmap_p += 2; 3358 3359 for (u2 locals_i = 0; locals_i < number_of_locals; locals_i++) { 3360 rewrite_cp_refs_in_verification_type_info(stackmap_p, stackmap_end, 3361 calc_number_of_entries, frame_type); 3362 } 3363 3364 // Use the largest size for the number_of_stack_items, but only get 3365 // the right number of bytes. 3366 u2 number_of_stack_items = Bytes::get_Java_u2(stackmap_p); 3367 stackmap_p += 2; 3368 3369 for (u2 stack_i = 0; stack_i < number_of_stack_items; stack_i++) { 3370 rewrite_cp_refs_in_verification_type_info(stackmap_p, stackmap_end, 3371 calc_number_of_entries, frame_type); 3372 } 3373 } 3374 } // end while there is a stack_map_frame 3375 assert(number_of_entries == calc_number_of_entries, "sanity check"); 3376 } // end rewrite_cp_refs_in_stack_map_table() 3377 3378 3379 // Rewrite constant pool references in the verification type info 3380 // portion of the method's stackmap table. These "structures" are 3381 // adapted from the StackMapTable_attribute that is described in 3382 // section 4.8.4 of the 6.0 version of the VM spec (dated 2005.10.26): 3383 // file:///net/quincunx.sfbay/export/gbracha/ClassFile-Java6.pdf 3384 // 3385 // The verification_type_info structure is a u1 tag followed by 0 or 3386 // more bytes of data: 3387 // 3388 // union verification_type_info { 3389 // Top_variable_info; 3390 // Integer_variable_info; 3391 // Float_variable_info; 3392 // Long_variable_info; 3393 // Double_variable_info; 3394 // Null_variable_info; 3395 // UninitializedThis_variable_info; 3396 // Object_variable_info; 3397 // Uninitialized_variable_info; 3398 // } 3399 // 3400 void VM_RedefineClasses::rewrite_cp_refs_in_verification_type_info( 3401 address& stackmap_p_ref, address stackmap_end, u2 frame_i, 3402 u1 frame_type) { 3403 3404 assert(stackmap_p_ref + 1 <= stackmap_end, "no room for tag"); 3405 u1 tag = *stackmap_p_ref; 3406 stackmap_p_ref++; 3407 3408 switch (tag) { 3409 // Top_variable_info { 3410 // u1 tag = ITEM_Top; /* 0 */ 3411 // } 3412 // verificationType.hpp has zero as ITEM_Bogus instead of ITEM_Top 3413 case 0: // fall through 3414 3415 // Integer_variable_info { 3416 // u1 tag = ITEM_Integer; /* 1 */ 3417 // } 3418 case ITEM_Integer: // fall through 3419 3420 // Float_variable_info { 3421 // u1 tag = ITEM_Float; /* 2 */ 3422 // } 3423 case ITEM_Float: // fall through 3424 3425 // Double_variable_info { 3426 // u1 tag = ITEM_Double; /* 3 */ 3427 // } 3428 case ITEM_Double: // fall through 3429 3430 // Long_variable_info { 3431 // u1 tag = ITEM_Long; /* 4 */ 3432 // } 3433 case ITEM_Long: // fall through 3434 3435 // Null_variable_info { 3436 // u1 tag = ITEM_Null; /* 5 */ 3437 // } 3438 case ITEM_Null: // fall through 3439 3440 // UninitializedThis_variable_info { 3441 // u1 tag = ITEM_UninitializedThis; /* 6 */ 3442 // } 3443 case ITEM_UninitializedThis: 3444 // nothing more to do for the above tag types 3445 break; 3446 3447 // Object_variable_info { 3448 // u1 tag = ITEM_Object; /* 7 */ 3449 // u2 cpool_index; 3450 // } 3451 case ITEM_Object: 3452 { 3453 assert(stackmap_p_ref + 2 <= stackmap_end, "no room for cpool_index"); 3454 u2 cpool_index = Bytes::get_Java_u2(stackmap_p_ref); 3455 u2 new_cp_index = find_new_index(cpool_index); 3456 if (new_cp_index != 0) { 3457 log_debug(redefine, class, stackmap)("mapped old cpool_index=%d", cpool_index); 3458 Bytes::put_Java_u2(stackmap_p_ref, new_cp_index); 3459 cpool_index = new_cp_index; 3460 } 3461 stackmap_p_ref += 2; 3462 3463 log_debug(redefine, class, stackmap) 3464 ("frame_i=%u, frame_type=%u, cpool_index=%d", frame_i, frame_type, cpool_index); 3465 } break; 3466 3467 // Uninitialized_variable_info { 3468 // u1 tag = ITEM_Uninitialized; /* 8 */ 3469 // u2 offset; 3470 // } 3471 case ITEM_Uninitialized: 3472 assert(stackmap_p_ref + 2 <= stackmap_end, "no room for offset"); 3473 stackmap_p_ref += 2; 3474 break; 3475 3476 default: 3477 log_debug(redefine, class, stackmap)("frame_i=%u, frame_type=%u, bad tag=0x%x", frame_i, frame_type, tag); 3478 ShouldNotReachHere(); 3479 break; 3480 } // end switch (tag) 3481 } // end rewrite_cp_refs_in_verification_type_info() 3482 3483 3484 // Change the constant pool associated with klass scratch_class to scratch_cp. 3485 // scratch_cp_length elements are copied from scratch_cp to a smaller constant pool 3486 // and the smaller constant pool is associated with scratch_class. 3487 void VM_RedefineClasses::set_new_constant_pool( 3488 ClassLoaderData* loader_data, 3489 InstanceKlass* scratch_class, constantPoolHandle scratch_cp, 3490 int scratch_cp_length, TRAPS) { 3491 assert(scratch_cp->length() >= scratch_cp_length, "sanity check"); 3492 3493 // scratch_cp is a merged constant pool and has enough space for a 3494 // worst case merge situation. We want to associate the minimum 3495 // sized constant pool with the klass to save space. 3496 ConstantPool* cp = ConstantPool::allocate(loader_data, scratch_cp_length, CHECK); 3497 constantPoolHandle smaller_cp(THREAD, cp); 3498 3499 // preserve version() value in the smaller copy 3500 int version = scratch_cp->version(); 3501 assert(version != 0, "sanity check"); 3502 smaller_cp->set_version(version); 3503 3504 // attach klass to new constant pool 3505 // reference to the cp holder is needed for copy_operands() 3506 smaller_cp->set_pool_holder(scratch_class); 3507 3508 smaller_cp->copy_fields(scratch_cp()); 3509 3510 scratch_cp->copy_cp_to(1, scratch_cp_length - 1, smaller_cp, 1, THREAD); 3511 if (HAS_PENDING_EXCEPTION) { 3512 // Exception is handled in the caller 3513 loader_data->add_to_deallocate_list(smaller_cp()); 3514 return; 3515 } 3516 scratch_cp = smaller_cp; 3517 3518 // attach new constant pool to klass 3519 scratch_class->set_constants(scratch_cp()); 3520 scratch_cp->initialize_unresolved_klasses(loader_data, CHECK); 3521 3522 int i; // for portability 3523 3524 // update each field in klass to use new constant pool indices as needed 3525 int java_fields; 3526 int injected_fields; 3527 bool update_required = false; 3528 GrowableArray<FieldInfo>* fields = FieldInfoStream::create_FieldInfoArray(scratch_class->fieldinfo_stream(), &java_fields, &injected_fields); 3529 for (int i = 0; i < java_fields; i++) { 3530 FieldInfo* fi = fields->adr_at(i); 3531 jshort cur_index = fi->name_index(); 3532 jshort new_index = find_new_index(cur_index); 3533 if (new_index != 0) { 3534 log_trace(redefine, class, constantpool)("field-name_index change: %d to %d", cur_index, new_index); 3535 fi->set_name_index(new_index); 3536 update_required = true; 3537 } 3538 cur_index = fi->signature_index(); 3539 new_index = find_new_index(cur_index); 3540 if (new_index != 0) { 3541 log_trace(redefine, class, constantpool)("field-signature_index change: %d to %d", cur_index, new_index); 3542 fi->set_signature_index(new_index); 3543 update_required = true; 3544 } 3545 cur_index = fi->initializer_index(); 3546 new_index = find_new_index(cur_index); 3547 if (new_index != 0) { 3548 log_trace(redefine, class, constantpool)("field-initval_index change: %d to %d", cur_index, new_index); 3549 fi->set_initializer_index(new_index); 3550 update_required = true; 3551 } 3552 cur_index = fi->generic_signature_index(); 3553 new_index = find_new_index(cur_index); 3554 if (new_index != 0) { 3555 log_trace(redefine, class, constantpool)("field-generic_signature change: %d to %d", cur_index, new_index); 3556 fi->set_generic_signature_index(new_index); 3557 update_required = true; 3558 } 3559 } 3560 if (update_required) { 3561 Array<u1>* old_stream = scratch_class->fieldinfo_stream(); 3562 assert(fields->length() == (java_fields + injected_fields), "Must be"); 3563 Array<u1>* new_fis = FieldInfoStream::create_FieldInfoStream(fields, java_fields, injected_fields, scratch_class->class_loader_data(), CHECK); 3564 scratch_class->set_fieldinfo_stream(new_fis); 3565 MetadataFactory::free_array<u1>(scratch_class->class_loader_data(), old_stream); 3566 } 3567 3568 // Update constant pool indices in the inner classes info to use 3569 // new constant indices as needed. The inner classes info is a 3570 // quadruple: 3571 // (inner_class_info, outer_class_info, inner_name, inner_access_flags) 3572 InnerClassesIterator iter(scratch_class); 3573 for (; !iter.done(); iter.next()) { 3574 int cur_index = iter.inner_class_info_index(); 3575 if (cur_index == 0) { 3576 continue; // JVM spec. allows null inner class refs so skip it 3577 } 3578 u2 new_index = find_new_index(cur_index); 3579 if (new_index != 0) { 3580 log_trace(redefine, class, constantpool)("inner_class_info change: %d to %d", cur_index, new_index); 3581 iter.set_inner_class_info_index(new_index); 3582 } 3583 cur_index = iter.outer_class_info_index(); 3584 new_index = find_new_index(cur_index); 3585 if (new_index != 0) { 3586 log_trace(redefine, class, constantpool)("outer_class_info change: %d to %d", cur_index, new_index); 3587 iter.set_outer_class_info_index(new_index); 3588 } 3589 cur_index = iter.inner_name_index(); 3590 new_index = find_new_index(cur_index); 3591 if (new_index != 0) { 3592 log_trace(redefine, class, constantpool)("inner_name change: %d to %d", cur_index, new_index); 3593 iter.set_inner_name_index(new_index); 3594 } 3595 } // end for each inner class 3596 3597 // Attach each method in klass to the new constant pool and update 3598 // to use new constant pool indices as needed: 3599 Array<Method*>* methods = scratch_class->methods(); 3600 for (i = methods->length() - 1; i >= 0; i--) { 3601 methodHandle method(THREAD, methods->at(i)); 3602 method->set_constants(scratch_cp()); 3603 3604 u2 new_index = find_new_index(method->name_index()); 3605 if (new_index != 0) { 3606 log_trace(redefine, class, constantpool) 3607 ("method-name_index change: %d to %d", method->name_index(), new_index); 3608 method->set_name_index(new_index); 3609 } 3610 new_index = find_new_index(method->signature_index()); 3611 if (new_index != 0) { 3612 log_trace(redefine, class, constantpool) 3613 ("method-signature_index change: %d to %d", method->signature_index(), new_index); 3614 method->set_signature_index(new_index); 3615 } 3616 new_index = find_new_index(method->generic_signature_index()); 3617 if (new_index != 0) { 3618 log_trace(redefine, class, constantpool) 3619 ("method-generic_signature_index change: %d to %d", method->generic_signature_index(), new_index); 3620 method->constMethod()->set_generic_signature_index(new_index); 3621 } 3622 3623 // Update constant pool indices in the method's checked exception 3624 // table to use new constant indices as needed. 3625 int cext_length = method->checked_exceptions_length(); 3626 if (cext_length > 0) { 3627 CheckedExceptionElement * cext_table = 3628 method->checked_exceptions_start(); 3629 for (int j = 0; j < cext_length; j++) { 3630 int cur_index = cext_table[j].class_cp_index; 3631 int new_index = find_new_index(cur_index); 3632 if (new_index != 0) { 3633 log_trace(redefine, class, constantpool)("cext-class_cp_index change: %d to %d", cur_index, new_index); 3634 cext_table[j].class_cp_index = (u2)new_index; 3635 } 3636 } // end for each checked exception table entry 3637 } // end if there are checked exception table entries 3638 3639 // Update each catch type index in the method's exception table 3640 // to use new constant pool indices as needed. The exception table 3641 // holds quadruple entries of the form: 3642 // (beg_bci, end_bci, handler_bci, klass_index) 3643 3644 ExceptionTable ex_table(method()); 3645 int ext_length = ex_table.length(); 3646 3647 for (int j = 0; j < ext_length; j ++) { 3648 int cur_index = ex_table.catch_type_index(j); 3649 u2 new_index = find_new_index(cur_index); 3650 if (new_index != 0) { 3651 log_trace(redefine, class, constantpool)("ext-klass_index change: %d to %d", cur_index, new_index); 3652 ex_table.set_catch_type_index(j, new_index); 3653 } 3654 } // end for each exception table entry 3655 3656 // Update constant pool indices in the method's local variable 3657 // table to use new constant indices as needed. The local variable 3658 // table hold sextuple entries of the form: 3659 // (start_pc, length, name_index, descriptor_index, signature_index, slot) 3660 int lvt_length = method->localvariable_table_length(); 3661 if (lvt_length > 0) { 3662 LocalVariableTableElement * lv_table = 3663 method->localvariable_table_start(); 3664 for (int j = 0; j < lvt_length; j++) { 3665 int cur_index = lv_table[j].name_cp_index; 3666 int new_index = find_new_index(cur_index); 3667 if (new_index != 0) { 3668 log_trace(redefine, class, constantpool)("lvt-name_cp_index change: %d to %d", cur_index, new_index); 3669 lv_table[j].name_cp_index = (u2)new_index; 3670 } 3671 cur_index = lv_table[j].descriptor_cp_index; 3672 new_index = find_new_index(cur_index); 3673 if (new_index != 0) { 3674 log_trace(redefine, class, constantpool)("lvt-descriptor_cp_index change: %d to %d", cur_index, new_index); 3675 lv_table[j].descriptor_cp_index = (u2)new_index; 3676 } 3677 cur_index = lv_table[j].signature_cp_index; 3678 new_index = find_new_index(cur_index); 3679 if (new_index != 0) { 3680 log_trace(redefine, class, constantpool)("lvt-signature_cp_index change: %d to %d", cur_index, new_index); 3681 lv_table[j].signature_cp_index = (u2)new_index; 3682 } 3683 } // end for each local variable table entry 3684 } // end if there are local variable table entries 3685 3686 // Update constant pool indices in the method's method_parameters. 3687 int mp_length = method->method_parameters_length(); 3688 if (mp_length > 0) { 3689 MethodParametersElement* elem = method->method_parameters_start(); 3690 for (int j = 0; j < mp_length; j++) { 3691 const int cp_index = elem[j].name_cp_index; 3692 const int new_cp_index = find_new_index(cp_index); 3693 if (new_cp_index != 0) { 3694 elem[j].name_cp_index = (u2)new_cp_index; 3695 } 3696 } 3697 } 3698 3699 rewrite_cp_refs_in_stack_map_table(method); 3700 } // end for each method 3701 } // end set_new_constant_pool() 3702 3703 3704 // Unevolving classes may point to methods of the_class directly 3705 // from their constant pool caches, itables, and/or vtables. We 3706 // use the ClassLoaderDataGraph::classes_do() facility and this helper 3707 // to fix up these pointers. MethodData also points to old methods and 3708 // must be cleaned. 3709 3710 // Adjust cpools and vtables closure 3711 void VM_RedefineClasses::AdjustAndCleanMetadata::do_klass(Klass* k) { 3712 3713 // This is a very busy routine. We don't want too much tracing 3714 // printed out. 3715 bool trace_name_printed = false; 3716 3717 // If the class being redefined is java.lang.Object, we need to fix all 3718 // array class vtables also. The _has_redefined_Object flag is global. 3719 // Once the java.lang.Object has been redefined (by the current or one 3720 // of the previous VM_RedefineClasses operations) we have to always 3721 // adjust method entries for array classes. 3722 if (k->is_array_klass() && _has_redefined_Object) { 3723 k->vtable().adjust_method_entries(&trace_name_printed); 3724 3725 } else if (k->is_instance_klass()) { 3726 HandleMark hm(_thread); 3727 InstanceKlass *ik = InstanceKlass::cast(k); 3728 3729 // Clean MethodData of this class's methods so they don't refer to 3730 // old methods that are no longer running. 3731 Array<Method*>* methods = ik->methods(); 3732 int num_methods = methods->length(); 3733 for (int index = 0; index < num_methods; ++index) { 3734 if (methods->at(index)->method_data() != nullptr) { 3735 methods->at(index)->method_data()->clean_weak_method_links(); 3736 } 3737 } 3738 3739 // Adjust all vtables, default methods and itables, to clean out old methods. 3740 ResourceMark rm(_thread); 3741 if (ik->vtable_length() > 0) { 3742 ik->vtable().adjust_method_entries(&trace_name_printed); 3743 ik->adjust_default_methods(&trace_name_printed); 3744 } 3745 3746 if (ik->itable_length() > 0) { 3747 ik->itable().adjust_method_entries(&trace_name_printed); 3748 } 3749 3750 // The constant pools in other classes (other_cp) can refer to 3751 // old methods. We have to update method information in 3752 // other_cp's cache. If other_cp has a previous version, then we 3753 // have to repeat the process for each previous version. The 3754 // constant pool cache holds the Method*s for non-virtual 3755 // methods and for virtual, final methods. 3756 // 3757 // Special case: if the current class is being redefined by the current 3758 // VM_RedefineClasses operation, then new_cp has already been attached 3759 // to the_class and old_cp has already been added as a previous version. 3760 // The new_cp doesn't have any cached references to old methods so it 3761 // doesn't need to be updated and we could optimize by skipping it. 3762 // However, the current class can be marked as being redefined by another 3763 // VM_RedefineClasses operation which has already executed its doit_prologue 3764 // and needs cpcache method entries adjusted. For simplicity, the cpcache 3765 // update is done unconditionally. It should result in doing nothing for 3766 // classes being redefined by the current VM_RedefineClasses operation. 3767 // Method entries in the previous version(s) are adjusted as well. 3768 ConstantPoolCache* cp_cache; 3769 3770 // this klass' constant pool cache may need adjustment 3771 ConstantPool* other_cp = ik->constants(); 3772 cp_cache = other_cp->cache(); 3773 if (cp_cache != nullptr) { 3774 cp_cache->adjust_method_entries(&trace_name_printed); 3775 } 3776 3777 // the previous versions' constant pool caches may need adjustment 3778 for (InstanceKlass* pv_node = ik->previous_versions(); 3779 pv_node != nullptr; 3780 pv_node = pv_node->previous_versions()) { 3781 cp_cache = pv_node->constants()->cache(); 3782 if (cp_cache != nullptr) { 3783 cp_cache->adjust_method_entries(&trace_name_printed); 3784 } 3785 } 3786 } 3787 } 3788 3789 void VM_RedefineClasses::update_jmethod_ids() { 3790 for (int j = 0; j < _matching_methods_length; ++j) { 3791 Method* old_method = _matching_old_methods[j]; 3792 jmethodID jmid = old_method->find_jmethod_id_or_null(); 3793 if (jmid != nullptr) { 3794 // There is a jmethodID, change it to point to the new method 3795 Method* new_method = _matching_new_methods[j]; 3796 Method::change_method_associated_with_jmethod_id(jmid, new_method); 3797 assert(Method::resolve_jmethod_id(jmid) == _matching_new_methods[j], 3798 "should be replaced"); 3799 } 3800 } 3801 } 3802 3803 int VM_RedefineClasses::check_methods_and_mark_as_obsolete() { 3804 int emcp_method_count = 0; 3805 int obsolete_count = 0; 3806 int old_index = 0; 3807 for (int j = 0; j < _matching_methods_length; ++j, ++old_index) { 3808 Method* old_method = _matching_old_methods[j]; 3809 Method* new_method = _matching_new_methods[j]; 3810 Method* old_array_method; 3811 3812 // Maintain an old_index into the _old_methods array by skipping 3813 // deleted methods 3814 while ((old_array_method = _old_methods->at(old_index)) != old_method) { 3815 ++old_index; 3816 } 3817 3818 if (MethodComparator::methods_EMCP(old_method, new_method)) { 3819 // The EMCP definition from JSR-163 requires the bytecodes to be 3820 // the same with the exception of constant pool indices which may 3821 // differ. However, the constants referred to by those indices 3822 // must be the same. 3823 // 3824 // We use methods_EMCP() for comparison since constant pool 3825 // merging can remove duplicate constant pool entries that were 3826 // present in the old method and removed from the rewritten new 3827 // method. A faster binary comparison function would consider the 3828 // old and new methods to be different when they are actually 3829 // EMCP. 3830 // 3831 // The old and new methods are EMCP and you would think that we 3832 // could get rid of one of them here and now and save some space. 3833 // However, the concept of EMCP only considers the bytecodes and 3834 // the constant pool entries in the comparison. Other things, 3835 // e.g., the line number table (LNT) or the local variable table 3836 // (LVT) don't count in the comparison. So the new (and EMCP) 3837 // method can have a new LNT that we need so we can't just 3838 // overwrite the new method with the old method. 3839 // 3840 // When this routine is called, we have already attached the new 3841 // methods to the_class so the old methods are effectively 3842 // overwritten. However, if an old method is still executing, 3843 // then the old method cannot be collected until sometime after 3844 // the old method call has returned. So the overwriting of old 3845 // methods by new methods will save us space except for those 3846 // (hopefully few) old methods that are still executing. 3847 // 3848 // A method refers to a ConstMethod* and this presents another 3849 // possible avenue to space savings. The ConstMethod* in the 3850 // new method contains possibly new attributes (LNT, LVT, etc). 3851 // At first glance, it seems possible to save space by replacing 3852 // the ConstMethod* in the old method with the ConstMethod* 3853 // from the new method. The old and new methods would share the 3854 // same ConstMethod* and we would save the space occupied by 3855 // the old ConstMethod*. However, the ConstMethod* contains 3856 // a back reference to the containing method. Sharing the 3857 // ConstMethod* between two methods could lead to confusion in 3858 // the code that uses the back reference. This would lead to 3859 // brittle code that could be broken in non-obvious ways now or 3860 // in the future. 3861 // 3862 // Another possibility is to copy the ConstMethod* from the new 3863 // method to the old method and then overwrite the new method with 3864 // the old method. Since the ConstMethod* contains the bytecodes 3865 // for the method embedded in the oop, this option would change 3866 // the bytecodes out from under any threads executing the old 3867 // method and make the thread's bcp invalid. Since EMCP requires 3868 // that the bytecodes be the same modulo constant pool indices, it 3869 // is straight forward to compute the correct new bcp in the new 3870 // ConstMethod* from the old bcp in the old ConstMethod*. The 3871 // time consuming part would be searching all the frames in all 3872 // of the threads to find all of the calls to the old method. 3873 // 3874 // It looks like we will have to live with the limited savings 3875 // that we get from effectively overwriting the old methods 3876 // when the new methods are attached to the_class. 3877 3878 // Count number of methods that are EMCP. The method will be marked 3879 // old but not obsolete if it is EMCP. 3880 emcp_method_count++; 3881 3882 // An EMCP method is _not_ obsolete. An obsolete method has a 3883 // different jmethodID than the current method. An EMCP method 3884 // has the same jmethodID as the current method. Having the 3885 // same jmethodID for all EMCP versions of a method allows for 3886 // a consistent view of the EMCP methods regardless of which 3887 // EMCP method you happen to have in hand. For example, a 3888 // breakpoint set in one EMCP method will work for all EMCP 3889 // versions of the method including the current one. 3890 } else { 3891 // mark obsolete methods as such 3892 old_method->set_is_obsolete(); 3893 obsolete_count++; 3894 3895 // obsolete methods need a unique idnum so they become new entries in 3896 // the jmethodID cache in InstanceKlass 3897 assert(old_method->method_idnum() == new_method->method_idnum(), "must match"); 3898 u2 num = InstanceKlass::cast(_the_class)->next_method_idnum(); 3899 if (num != ConstMethod::UNSET_IDNUM) { 3900 old_method->set_method_idnum(num); 3901 } 3902 3903 // With tracing we try not to "yack" too much. The position of 3904 // this trace assumes there are fewer obsolete methods than 3905 // EMCP methods. 3906 if (log_is_enabled(Trace, redefine, class, obsolete, mark)) { 3907 ResourceMark rm; 3908 log_trace(redefine, class, obsolete, mark) 3909 ("mark %s(%s) as obsolete", old_method->name()->as_C_string(), old_method->signature()->as_C_string()); 3910 } 3911 } 3912 old_method->set_is_old(); 3913 } 3914 for (int i = 0; i < _deleted_methods_length; ++i) { 3915 Method* old_method = _deleted_methods[i]; 3916 3917 assert(!old_method->has_vtable_index(), 3918 "cannot delete methods with vtable entries");; 3919 3920 // Mark all deleted methods as old, obsolete and deleted 3921 old_method->set_is_deleted(); 3922 old_method->set_is_old(); 3923 old_method->set_is_obsolete(); 3924 ++obsolete_count; 3925 // With tracing we try not to "yack" too much. The position of 3926 // this trace assumes there are fewer obsolete methods than 3927 // EMCP methods. 3928 if (log_is_enabled(Trace, redefine, class, obsolete, mark)) { 3929 ResourceMark rm; 3930 log_trace(redefine, class, obsolete, mark) 3931 ("mark deleted %s(%s) as obsolete", old_method->name()->as_C_string(), old_method->signature()->as_C_string()); 3932 } 3933 } 3934 assert((emcp_method_count + obsolete_count) == _old_methods->length(), 3935 "sanity check"); 3936 log_trace(redefine, class, obsolete, mark)("EMCP_cnt=%d, obsolete_cnt=%d", emcp_method_count, obsolete_count); 3937 return emcp_method_count; 3938 } 3939 3940 // This internal class transfers the native function registration from old methods 3941 // to new methods. It is designed to handle both the simple case of unchanged 3942 // native methods and the complex cases of native method prefixes being added and/or 3943 // removed. 3944 // It expects only to be used during the VM_RedefineClasses op (a safepoint). 3945 // 3946 // This class is used after the new methods have been installed in "the_class". 3947 // 3948 // So, for example, the following must be handled. Where 'm' is a method and 3949 // a number followed by an underscore is a prefix. 3950 // 3951 // Old Name New Name 3952 // Simple transfer to new method m -> m 3953 // Add prefix m -> 1_m 3954 // Remove prefix 1_m -> m 3955 // Simultaneous add of prefixes m -> 3_2_1_m 3956 // Simultaneous removal of prefixes 3_2_1_m -> m 3957 // Simultaneous add and remove 1_m -> 2_m 3958 // Same, caused by prefix removal only 3_2_1_m -> 3_2_m 3959 // 3960 class TransferNativeFunctionRegistration { 3961 private: 3962 InstanceKlass* the_class; 3963 int prefix_count; 3964 char** prefixes; 3965 3966 // Recursively search the binary tree of possibly prefixed method names. 3967 // Iteration could be used if all agents were well behaved. Full tree walk is 3968 // more resilent to agents not cleaning up intermediate methods. 3969 // Branch at each depth in the binary tree is: 3970 // (1) without the prefix. 3971 // (2) with the prefix. 3972 // where 'prefix' is the prefix at that 'depth' (first prefix, second prefix,...) 3973 Method* search_prefix_name_space(int depth, char* name_str, size_t name_len, 3974 Symbol* signature) { 3975 TempNewSymbol name_symbol = SymbolTable::probe(name_str, (int)name_len); 3976 if (name_symbol != nullptr) { 3977 Method* method = the_class->lookup_method(name_symbol, signature); 3978 if (method != nullptr) { 3979 // Even if prefixed, intermediate methods must exist. 3980 if (method->is_native()) { 3981 // Wahoo, we found a (possibly prefixed) version of the method, return it. 3982 return method; 3983 } 3984 if (depth < prefix_count) { 3985 // Try applying further prefixes (other than this one). 3986 method = search_prefix_name_space(depth+1, name_str, name_len, signature); 3987 if (method != nullptr) { 3988 return method; // found 3989 } 3990 3991 // Try adding this prefix to the method name and see if it matches 3992 // another method name. 3993 char* prefix = prefixes[depth]; 3994 size_t prefix_len = strlen(prefix); 3995 size_t trial_len = name_len + prefix_len; 3996 char* trial_name_str = NEW_RESOURCE_ARRAY(char, trial_len + 1); 3997 strcpy(trial_name_str, prefix); 3998 strcat(trial_name_str, name_str); 3999 method = search_prefix_name_space(depth+1, trial_name_str, trial_len, 4000 signature); 4001 if (method != nullptr) { 4002 // If found along this branch, it was prefixed, mark as such 4003 method->set_is_prefixed_native(); 4004 return method; // found 4005 } 4006 } 4007 } 4008 } 4009 return nullptr; // This whole branch bore nothing 4010 } 4011 4012 // Return the method name with old prefixes stripped away. 4013 char* method_name_without_prefixes(Method* method) { 4014 Symbol* name = method->name(); 4015 char* name_str = name->as_utf8(); 4016 4017 // Old prefixing may be defunct, strip prefixes, if any. 4018 for (int i = prefix_count-1; i >= 0; i--) { 4019 char* prefix = prefixes[i]; 4020 size_t prefix_len = strlen(prefix); 4021 if (strncmp(prefix, name_str, prefix_len) == 0) { 4022 name_str += prefix_len; 4023 } 4024 } 4025 return name_str; 4026 } 4027 4028 // Strip any prefixes off the old native method, then try to find a 4029 // (possibly prefixed) new native that matches it. 4030 Method* strip_and_search_for_new_native(Method* method) { 4031 ResourceMark rm; 4032 char* name_str = method_name_without_prefixes(method); 4033 return search_prefix_name_space(0, name_str, strlen(name_str), 4034 method->signature()); 4035 } 4036 4037 public: 4038 4039 // Construct a native method transfer processor for this class. 4040 TransferNativeFunctionRegistration(InstanceKlass* _the_class) { 4041 assert(SafepointSynchronize::is_at_safepoint(), "sanity check"); 4042 4043 the_class = _the_class; 4044 prefixes = JvmtiExport::get_all_native_method_prefixes(&prefix_count); 4045 } 4046 4047 // Attempt to transfer any of the old or deleted methods that are native 4048 void transfer_registrations(Method** old_methods, int methods_length) { 4049 for (int j = 0; j < methods_length; j++) { 4050 Method* old_method = old_methods[j]; 4051 4052 if (old_method->is_native() && old_method->has_native_function()) { 4053 Method* new_method = strip_and_search_for_new_native(old_method); 4054 if (new_method != nullptr) { 4055 // Actually set the native function in the new method. 4056 // Redefine does not send events (except CFLH), certainly not this 4057 // behind the scenes re-registration. 4058 new_method->set_native_function(old_method->native_function(), 4059 !Method::native_bind_event_is_interesting); 4060 } 4061 } 4062 } 4063 } 4064 }; 4065 4066 // Don't lose the association between a native method and its JNI function. 4067 void VM_RedefineClasses::transfer_old_native_function_registrations(InstanceKlass* the_class) { 4068 TransferNativeFunctionRegistration transfer(the_class); 4069 transfer.transfer_registrations(_deleted_methods, _deleted_methods_length); 4070 transfer.transfer_registrations(_matching_old_methods, _matching_methods_length); 4071 } 4072 4073 // Deoptimize all compiled code that depends on the classes redefined. 4074 // 4075 // If the can_redefine_classes capability is obtained in the onload 4076 // phase or 'AlwaysRecordEvolDependencies' is true, then the compiler has 4077 // recorded all dependencies from startup. In that case we need only 4078 // deoptimize and throw away all compiled code that depends on the class. 4079 // 4080 // If can_redefine_classes is obtained sometime after the onload phase 4081 // (and 'AlwaysRecordEvolDependencies' is false) then the dependency 4082 // information may be incomplete. In that case the first call to 4083 // RedefineClasses causes all compiled code to be thrown away. As 4084 // can_redefine_classes has been obtained then all future compilations will 4085 // record dependencies so second and subsequent calls to RedefineClasses 4086 // need only throw away code that depends on the class. 4087 // 4088 4089 void VM_RedefineClasses::flush_dependent_code() { 4090 assert(SafepointSynchronize::is_at_safepoint(), "sanity check"); 4091 assert(JvmtiExport::all_dependencies_are_recorded() || !AlwaysRecordEvolDependencies, "sanity check"); 4092 4093 DeoptimizationScope deopt_scope; 4094 4095 // This is the first redefinition, mark all the nmethods for deoptimization 4096 if (!JvmtiExport::all_dependencies_are_recorded()) { 4097 CodeCache::mark_all_nmethods_for_evol_deoptimization(&deopt_scope); 4098 log_debug(redefine, class, nmethod)("Marked all nmethods for deopt"); 4099 } else { 4100 CodeCache::mark_dependents_for_evol_deoptimization(&deopt_scope); 4101 log_debug(redefine, class, nmethod)("Marked dependent nmethods for deopt"); 4102 } 4103 4104 deopt_scope.deoptimize_marked(); 4105 4106 // From now on we know that the dependency information is complete 4107 JvmtiExport::set_all_dependencies_are_recorded(true); 4108 } 4109 4110 void VM_RedefineClasses::compute_added_deleted_matching_methods() { 4111 Method* old_method; 4112 Method* new_method; 4113 4114 _matching_old_methods = NEW_RESOURCE_ARRAY(Method*, _old_methods->length()); 4115 _matching_new_methods = NEW_RESOURCE_ARRAY(Method*, _old_methods->length()); 4116 _added_methods = NEW_RESOURCE_ARRAY(Method*, _new_methods->length()); 4117 _deleted_methods = NEW_RESOURCE_ARRAY(Method*, _old_methods->length()); 4118 4119 _matching_methods_length = 0; 4120 _deleted_methods_length = 0; 4121 _added_methods_length = 0; 4122 4123 int nj = 0; 4124 int oj = 0; 4125 while (true) { 4126 if (oj >= _old_methods->length()) { 4127 if (nj >= _new_methods->length()) { 4128 break; // we've looked at everything, done 4129 } 4130 // New method at the end 4131 new_method = _new_methods->at(nj); 4132 _added_methods[_added_methods_length++] = new_method; 4133 ++nj; 4134 } else if (nj >= _new_methods->length()) { 4135 // Old method, at the end, is deleted 4136 old_method = _old_methods->at(oj); 4137 _deleted_methods[_deleted_methods_length++] = old_method; 4138 ++oj; 4139 } else { 4140 old_method = _old_methods->at(oj); 4141 new_method = _new_methods->at(nj); 4142 if (old_method->name() == new_method->name()) { 4143 if (old_method->signature() == new_method->signature()) { 4144 _matching_old_methods[_matching_methods_length ] = old_method; 4145 _matching_new_methods[_matching_methods_length++] = new_method; 4146 ++nj; 4147 ++oj; 4148 } else { 4149 // added overloaded have already been moved to the end, 4150 // so this is a deleted overloaded method 4151 _deleted_methods[_deleted_methods_length++] = old_method; 4152 ++oj; 4153 } 4154 } else { // names don't match 4155 if (old_method->name()->fast_compare(new_method->name()) > 0) { 4156 // new method 4157 _added_methods[_added_methods_length++] = new_method; 4158 ++nj; 4159 } else { 4160 // deleted method 4161 _deleted_methods[_deleted_methods_length++] = old_method; 4162 ++oj; 4163 } 4164 } 4165 } 4166 } 4167 assert(_matching_methods_length + _deleted_methods_length == _old_methods->length(), "sanity"); 4168 assert(_matching_methods_length + _added_methods_length == _new_methods->length(), "sanity"); 4169 } 4170 4171 4172 void VM_RedefineClasses::swap_annotations(InstanceKlass* the_class, 4173 InstanceKlass* scratch_class) { 4174 // Swap annotation fields values 4175 Annotations* old_annotations = the_class->annotations(); 4176 the_class->set_annotations(scratch_class->annotations()); 4177 scratch_class->set_annotations(old_annotations); 4178 } 4179 4180 4181 // Install the redefinition of a class: 4182 // - house keeping (flushing breakpoints and caches, deoptimizing 4183 // dependent compiled code) 4184 // - replacing parts in the_class with parts from scratch_class 4185 // - adding a weak reference to track the obsolete but interesting 4186 // parts of the_class 4187 // - adjusting constant pool caches and vtables in other classes 4188 // that refer to methods in the_class. These adjustments use the 4189 // ClassLoaderDataGraph::classes_do() facility which only allows 4190 // a helper method to be specified. The interesting parameters 4191 // that we would like to pass to the helper method are saved in 4192 // static global fields in the VM operation. 4193 void VM_RedefineClasses::redefine_single_class(Thread* current, jclass the_jclass, 4194 InstanceKlass* scratch_class) { 4195 4196 HandleMark hm(current); // make sure handles from this call are freed 4197 4198 if (log_is_enabled(Info, redefine, class, timer)) { 4199 _timer_rsc_phase1.start(); 4200 } 4201 4202 InstanceKlass* the_class = get_ik(the_jclass); 4203 4204 // Set a flag to control and optimize adjusting method entries 4205 _has_redefined_Object |= the_class == vmClasses::Object_klass(); 4206 4207 // Remove all breakpoints in methods of this class 4208 JvmtiBreakpoints& jvmti_breakpoints = JvmtiCurrentBreakpoints::get_jvmti_breakpoints(); 4209 jvmti_breakpoints.clearall_in_class_at_safepoint(the_class); 4210 4211 _old_methods = the_class->methods(); 4212 _new_methods = scratch_class->methods(); 4213 _the_class = the_class; 4214 compute_added_deleted_matching_methods(); 4215 update_jmethod_ids(); 4216 4217 _any_class_has_resolved_methods = the_class->has_resolved_methods() || _any_class_has_resolved_methods; 4218 4219 // Attach new constant pool to the original klass. The original 4220 // klass still refers to the old constant pool (for now). 4221 scratch_class->constants()->set_pool_holder(the_class); 4222 4223 #if 0 4224 // In theory, with constant pool merging in place we should be able 4225 // to save space by using the new, merged constant pool in place of 4226 // the old constant pool(s). By "pool(s)" I mean the constant pool in 4227 // the klass version we are replacing now and any constant pool(s) in 4228 // previous versions of klass. Nice theory, doesn't work in practice. 4229 // When this code is enabled, even simple programs throw NullPointer 4230 // exceptions. I'm guessing that this is caused by some constant pool 4231 // cache difference between the new, merged constant pool and the 4232 // constant pool that was just being used by the klass. I'm keeping 4233 // this code around to archive the idea, but the code has to remain 4234 // disabled for now. 4235 4236 // Attach each old method to the new constant pool. This can be 4237 // done here since we are past the bytecode verification and 4238 // constant pool optimization phases. 4239 for (int i = _old_methods->length() - 1; i >= 0; i--) { 4240 Method* method = _old_methods->at(i); 4241 method->set_constants(scratch_class->constants()); 4242 } 4243 4244 // NOTE: this doesn't work because you can redefine the same class in two 4245 // threads, each getting their own constant pool data appended to the 4246 // original constant pool. In order for the new methods to work when they 4247 // become old methods, they need to keep their updated copy of the constant pool. 4248 4249 { 4250 // walk all previous versions of the klass 4251 InstanceKlass *ik = the_class; 4252 PreviousVersionWalker pvw(ik); 4253 do { 4254 ik = pvw.next_previous_version(); 4255 if (ik != nullptr) { 4256 4257 // attach previous version of klass to the new constant pool 4258 ik->set_constants(scratch_class->constants()); 4259 4260 // Attach each method in the previous version of klass to the 4261 // new constant pool 4262 Array<Method*>* prev_methods = ik->methods(); 4263 for (int i = prev_methods->length() - 1; i >= 0; i--) { 4264 Method* method = prev_methods->at(i); 4265 method->set_constants(scratch_class->constants()); 4266 } 4267 } 4268 } while (ik != nullptr); 4269 } 4270 #endif 4271 4272 // Replace methods and constantpool 4273 the_class->set_methods(_new_methods); 4274 scratch_class->set_methods(_old_methods); // To prevent potential GCing of the old methods, 4275 // and to be able to undo operation easily. 4276 4277 Array<int>* old_ordering = the_class->method_ordering(); 4278 the_class->set_method_ordering(scratch_class->method_ordering()); 4279 scratch_class->set_method_ordering(old_ordering); 4280 4281 ConstantPool* old_constants = the_class->constants(); 4282 the_class->set_constants(scratch_class->constants()); 4283 scratch_class->set_constants(old_constants); // See the previous comment. 4284 #if 0 4285 // We are swapping the guts of "the new class" with the guts of "the 4286 // class". Since the old constant pool has just been attached to "the 4287 // new class", it seems logical to set the pool holder in the old 4288 // constant pool also. However, doing this will change the observable 4289 // class hierarchy for any old methods that are still executing. A 4290 // method can query the identity of its "holder" and this query uses 4291 // the method's constant pool link to find the holder. The change in 4292 // holding class from "the class" to "the new class" can confuse 4293 // things. 4294 // 4295 // Setting the old constant pool's holder will also cause 4296 // verification done during vtable initialization below to fail. 4297 // During vtable initialization, the vtable's class is verified to be 4298 // a subtype of the method's holder. The vtable's class is "the 4299 // class" and the method's holder is gotten from the constant pool 4300 // link in the method itself. For "the class"'s directly implemented 4301 // methods, the method holder is "the class" itself (as gotten from 4302 // the new constant pool). The check works fine in this case. The 4303 // check also works fine for methods inherited from super classes. 4304 // 4305 // Miranda methods are a little more complicated. A miranda method is 4306 // provided by an interface when the class implementing the interface 4307 // does not provide its own method. These interfaces are implemented 4308 // internally as an InstanceKlass. These special instanceKlasses 4309 // share the constant pool of the class that "implements" the 4310 // interface. By sharing the constant pool, the method holder of a 4311 // miranda method is the class that "implements" the interface. In a 4312 // non-redefine situation, the subtype check works fine. However, if 4313 // the old constant pool's pool holder is modified, then the check 4314 // fails because there is no class hierarchy relationship between the 4315 // vtable's class and "the new class". 4316 4317 old_constants->set_pool_holder(scratch_class()); 4318 #endif 4319 4320 // track number of methods that are EMCP for add_previous_version() call below 4321 int emcp_method_count = check_methods_and_mark_as_obsolete(); 4322 transfer_old_native_function_registrations(the_class); 4323 4324 if (scratch_class->get_cached_class_file() != the_class->get_cached_class_file()) { 4325 // 1. the_class doesn't have a cache yet, scratch_class does have a cache. 4326 // 2. The same class can be present twice in the scratch classes list or there 4327 // are multiple concurrent RetransformClasses calls on different threads. 4328 // the_class and scratch_class have the same cached bytes, but different buffers. 4329 // In such cases we need to deallocate one of the buffers. 4330 // 3. RedefineClasses and the_class has cached bytes from a previous transformation. 4331 // In the case we need to use class bytes from scratch_class. 4332 if (the_class->get_cached_class_file() != nullptr) { 4333 os::free(the_class->get_cached_class_file()); 4334 } 4335 the_class->set_cached_class_file(scratch_class->get_cached_class_file()); 4336 } 4337 4338 // null out in scratch class to not delete twice. The class to be redefined 4339 // always owns these bytes. 4340 scratch_class->set_cached_class_file(nullptr); 4341 4342 // Replace inner_classes 4343 Array<u2>* old_inner_classes = the_class->inner_classes(); 4344 the_class->set_inner_classes(scratch_class->inner_classes()); 4345 scratch_class->set_inner_classes(old_inner_classes); 4346 4347 // Initialize the vtable and interface table after 4348 // methods have been rewritten 4349 // no exception should happen here since we explicitly 4350 // do not check loader constraints. 4351 // compare_and_normalize_class_versions has already checked: 4352 // - classloaders unchanged, signatures unchanged 4353 // - all instanceKlasses for redefined classes reused & contents updated 4354 the_class->vtable().initialize_vtable(); 4355 the_class->itable().initialize_itable(); 4356 4357 // Update jmethodID cache if present. 4358 the_class->update_methods_jmethod_cache(); 4359 4360 // Copy the "source debug extension" attribute from new class version 4361 the_class->set_source_debug_extension( 4362 scratch_class->source_debug_extension(), 4363 scratch_class->source_debug_extension() == nullptr ? 0 : 4364 (int)strlen(scratch_class->source_debug_extension())); 4365 4366 // Use of javac -g could be different in the old and the new 4367 if (scratch_class->has_localvariable_table() != 4368 the_class->has_localvariable_table()) { 4369 the_class->set_has_localvariable_table(scratch_class->has_localvariable_table()); 4370 } 4371 4372 swap_annotations(the_class, scratch_class); 4373 4374 // Replace minor version number of class file 4375 u2 old_minor_version = the_class->constants()->minor_version(); 4376 the_class->constants()->set_minor_version(scratch_class->constants()->minor_version()); 4377 scratch_class->constants()->set_minor_version(old_minor_version); 4378 4379 // Replace major version number of class file 4380 u2 old_major_version = the_class->constants()->major_version(); 4381 the_class->constants()->set_major_version(scratch_class->constants()->major_version()); 4382 scratch_class->constants()->set_major_version(old_major_version); 4383 4384 // Replace CP indexes for class and name+type of enclosing method 4385 u2 old_class_idx = the_class->enclosing_method_class_index(); 4386 u2 old_method_idx = the_class->enclosing_method_method_index(); 4387 the_class->set_enclosing_method_indices( 4388 scratch_class->enclosing_method_class_index(), 4389 scratch_class->enclosing_method_method_index()); 4390 scratch_class->set_enclosing_method_indices(old_class_idx, old_method_idx); 4391 4392 if (!the_class->has_been_redefined()) { 4393 the_class->set_has_been_redefined(); 4394 } 4395 4396 // Scratch class is unloaded but still needs cleaning, and skipping for CDS. 4397 scratch_class->set_is_scratch_class(); 4398 4399 // keep track of previous versions of this class 4400 the_class->add_previous_version(scratch_class, emcp_method_count); 4401 4402 _timer_rsc_phase1.stop(); 4403 if (log_is_enabled(Info, redefine, class, timer)) { 4404 _timer_rsc_phase2.start(); 4405 } 4406 4407 if (the_class->oop_map_cache() != nullptr) { 4408 // Flush references to any obsolete methods from the oop map cache 4409 // so that obsolete methods are not pinned. 4410 the_class->oop_map_cache()->flush_obsolete_entries(); 4411 } 4412 4413 increment_class_counter(the_class); 4414 4415 if (EventClassRedefinition::is_enabled()) { 4416 EventClassRedefinition event; 4417 event.set_classModificationCount(java_lang_Class::classRedefinedCount(the_class->java_mirror())); 4418 event.set_redefinedClass(the_class); 4419 event.set_redefinitionId(_id); 4420 event.commit(); 4421 } 4422 4423 { 4424 ResourceMark rm(current); 4425 // increment the classRedefinedCount field in the_class and in any 4426 // direct and indirect subclasses of the_class 4427 log_info(redefine, class, load) 4428 ("redefined name=%s, count=%d (avail_mem=" UINT64_FORMAT "K)", 4429 the_class->external_name(), java_lang_Class::classRedefinedCount(the_class->java_mirror()), os::available_memory() >> 10); 4430 Events::log_redefinition(current, "redefined class name=%s, count=%d", 4431 the_class->external_name(), 4432 java_lang_Class::classRedefinedCount(the_class->java_mirror())); 4433 4434 } 4435 _timer_rsc_phase2.stop(); 4436 4437 } // end redefine_single_class() 4438 4439 4440 // Increment the classRedefinedCount field in the specific InstanceKlass 4441 // and in all direct and indirect subclasses. 4442 void VM_RedefineClasses::increment_class_counter(InstanceKlass* ik) { 4443 for (ClassHierarchyIterator iter(ik); !iter.done(); iter.next()) { 4444 // Only update instanceKlasses 4445 Klass* sub = iter.klass(); 4446 if (sub->is_instance_klass()) { 4447 oop class_mirror = InstanceKlass::cast(sub)->java_mirror(); 4448 Klass* class_oop = java_lang_Class::as_Klass(class_mirror); 4449 int new_count = java_lang_Class::classRedefinedCount(class_mirror) + 1; 4450 java_lang_Class::set_classRedefinedCount(class_mirror, new_count); 4451 4452 if (class_oop != _the_class) { 4453 // _the_class count is printed at end of redefine_single_class() 4454 log_debug(redefine, class, subclass)("updated count in subclass=%s to %d", ik->external_name(), new_count); 4455 } 4456 } 4457 } 4458 } 4459 4460 void VM_RedefineClasses::CheckClass::do_klass(Klass* k) { 4461 bool no_old_methods = true; // be optimistic 4462 4463 // Both array and instance classes have vtables. 4464 // a vtable should never contain old or obsolete methods 4465 ResourceMark rm(_thread); 4466 if (k->vtable_length() > 0 && 4467 !k->vtable().check_no_old_or_obsolete_entries()) { 4468 if (log_is_enabled(Trace, redefine, class, obsolete, metadata)) { 4469 log_trace(redefine, class, obsolete, metadata) 4470 ("klassVtable::check_no_old_or_obsolete_entries failure -- OLD or OBSOLETE method found -- class: %s", 4471 k->signature_name()); 4472 k->vtable().dump_vtable(); 4473 } 4474 no_old_methods = false; 4475 } 4476 4477 if (k->is_instance_klass()) { 4478 HandleMark hm(_thread); 4479 InstanceKlass *ik = InstanceKlass::cast(k); 4480 4481 // an itable should never contain old or obsolete methods 4482 if (ik->itable_length() > 0 && 4483 !ik->itable().check_no_old_or_obsolete_entries()) { 4484 if (log_is_enabled(Trace, redefine, class, obsolete, metadata)) { 4485 log_trace(redefine, class, obsolete, metadata) 4486 ("klassItable::check_no_old_or_obsolete_entries failure -- OLD or OBSOLETE method found -- class: %s", 4487 ik->signature_name()); 4488 ik->itable().dump_itable(); 4489 } 4490 no_old_methods = false; 4491 } 4492 4493 // the constant pool cache should never contain non-deleted old or obsolete methods 4494 if (ik->constants() != nullptr && 4495 ik->constants()->cache() != nullptr && 4496 !ik->constants()->cache()->check_no_old_or_obsolete_entries()) { 4497 if (log_is_enabled(Trace, redefine, class, obsolete, metadata)) { 4498 log_trace(redefine, class, obsolete, metadata) 4499 ("cp-cache::check_no_old_or_obsolete_entries failure -- OLD or OBSOLETE method found -- class: %s", 4500 ik->signature_name()); 4501 ik->constants()->cache()->dump_cache(); 4502 } 4503 no_old_methods = false; 4504 } 4505 } 4506 4507 // print and fail guarantee if old methods are found. 4508 if (!no_old_methods) { 4509 if (log_is_enabled(Trace, redefine, class, obsolete, metadata)) { 4510 dump_methods(); 4511 } else { 4512 log_trace(redefine, class)("Use the '-Xlog:redefine+class*:' option " 4513 "to see more info about the following guarantee() failure."); 4514 } 4515 guarantee(false, "OLD and/or OBSOLETE method(s) found"); 4516 } 4517 } 4518 4519 u8 VM_RedefineClasses::next_id() { 4520 while (true) { 4521 u8 id = _id_counter; 4522 u8 next_id = id + 1; 4523 u8 result = Atomic::cmpxchg(&_id_counter, id, next_id); 4524 if (result == id) { 4525 return next_id; 4526 } 4527 } 4528 } 4529 4530 void VM_RedefineClasses::dump_methods() { 4531 int j; 4532 log_trace(redefine, class, dump)("_old_methods --"); 4533 for (j = 0; j < _old_methods->length(); ++j) { 4534 LogStreamHandle(Trace, redefine, class, dump) log_stream; 4535 Method* m = _old_methods->at(j); 4536 log_stream.print("%4d (%5d) ", j, m->vtable_index()); 4537 m->access_flags().print_on(&log_stream); 4538 log_stream.print(" -- "); 4539 m->print_name(&log_stream); 4540 log_stream.cr(); 4541 } 4542 log_trace(redefine, class, dump)("_new_methods --"); 4543 for (j = 0; j < _new_methods->length(); ++j) { 4544 LogStreamHandle(Trace, redefine, class, dump) log_stream; 4545 Method* m = _new_methods->at(j); 4546 log_stream.print("%4d (%5d) ", j, m->vtable_index()); 4547 m->access_flags().print_on(&log_stream); 4548 log_stream.print(" -- "); 4549 m->print_name(&log_stream); 4550 log_stream.cr(); 4551 } 4552 log_trace(redefine, class, dump)("_matching_methods --"); 4553 for (j = 0; j < _matching_methods_length; ++j) { 4554 LogStreamHandle(Trace, redefine, class, dump) log_stream; 4555 Method* m = _matching_old_methods[j]; 4556 log_stream.print("%4d (%5d) ", j, m->vtable_index()); 4557 m->access_flags().print_on(&log_stream); 4558 log_stream.print(" -- "); 4559 m->print_name(); 4560 log_stream.cr(); 4561 4562 m = _matching_new_methods[j]; 4563 log_stream.print(" (%5d) ", m->vtable_index()); 4564 m->access_flags().print_on(&log_stream); 4565 log_stream.cr(); 4566 } 4567 log_trace(redefine, class, dump)("_deleted_methods --"); 4568 for (j = 0; j < _deleted_methods_length; ++j) { 4569 LogStreamHandle(Trace, redefine, class, dump) log_stream; 4570 Method* m = _deleted_methods[j]; 4571 log_stream.print("%4d (%5d) ", j, m->vtable_index()); 4572 m->access_flags().print_on(&log_stream); 4573 log_stream.print(" -- "); 4574 m->print_name(&log_stream); 4575 log_stream.cr(); 4576 } 4577 log_trace(redefine, class, dump)("_added_methods --"); 4578 for (j = 0; j < _added_methods_length; ++j) { 4579 LogStreamHandle(Trace, redefine, class, dump) log_stream; 4580 Method* m = _added_methods[j]; 4581 log_stream.print("%4d (%5d) ", j, m->vtable_index()); 4582 m->access_flags().print_on(&log_stream); 4583 log_stream.print(" -- "); 4584 m->print_name(&log_stream); 4585 log_stream.cr(); 4586 } 4587 } 4588 4589 void VM_RedefineClasses::print_on_error(outputStream* st) const { 4590 VM_Operation::print_on_error(st); 4591 if (_the_class != nullptr) { 4592 ResourceMark rm; 4593 st->print_cr(", redefining class %s", _the_class->external_name()); 4594 } 4595 }