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