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