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