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