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 jlong doit_time = _timer_rsc_phase1.milliseconds() + 245 _timer_rsc_phase2.milliseconds(); 246 jlong all_time = _timer_vm_op_prologue.milliseconds() + doit_time; 247 248 log_info(redefine, class, timer) 249 ("vm_op: all=" UINT64_FORMAT " prologue=" UINT64_FORMAT " doit=" UINT64_FORMAT, 250 all_time, _timer_vm_op_prologue.milliseconds(), doit_time); 251 log_info(redefine, class, timer) 252 ("redefine_single_class: phase1=" UINT64_FORMAT " phase2=" UINT64_FORMAT, 253 _timer_rsc_phase1.milliseconds(), _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 } 1829 } // end for each bytecode 1830 1831 // We also need to rewrite the parameter name indexes, if there is 1832 // method parameter data present 1833 if(method->has_method_parameters()) { 1834 const int len = method->method_parameters_length(); 1835 MethodParametersElement* elem = method->method_parameters_start(); 1836 1837 for (int i = 0; i < len; i++) { 1838 const u2 cp_index = elem[i].name_cp_index; 1839 const u2 new_cp_index = find_new_index(cp_index); 1840 if (new_cp_index != 0) { 1841 elem[i].name_cp_index = new_cp_index; 1842 } 1843 } 1844 } 1845 } // end rewrite_cp_refs_in_method() 1846 1847 1848 // Rewrite constant pool references in the class_annotations field. 1849 bool VM_RedefineClasses::rewrite_cp_refs_in_class_annotations( 1850 InstanceKlass* scratch_class, TRAPS) { 1851 1852 AnnotationArray* class_annotations = scratch_class->class_annotations(); 1853 if (class_annotations == NULL || class_annotations->length() == 0) { 1854 // no class_annotations so nothing to do 1855 return true; 1856 } 1857 1858 log_debug(redefine, class, annotation)("class_annotations length=%d", class_annotations->length()); 1859 1860 int byte_i = 0; // byte index into class_annotations 1861 return rewrite_cp_refs_in_annotations_typeArray(class_annotations, byte_i, 1862 THREAD); 1863 } 1864 1865 1866 // Rewrite constant pool references in an annotations typeArray. This 1867 // "structure" is adapted from the RuntimeVisibleAnnotations_attribute 1868 // that is described in section 4.8.15 of the 2nd-edition of the VM spec: 1869 // 1870 // annotations_typeArray { 1871 // u2 num_annotations; 1872 // annotation annotations[num_annotations]; 1873 // } 1874 // 1875 bool VM_RedefineClasses::rewrite_cp_refs_in_annotations_typeArray( 1876 AnnotationArray* annotations_typeArray, int &byte_i_ref, TRAPS) { 1877 1878 if ((byte_i_ref + 2) > annotations_typeArray->length()) { 1879 // not enough room for num_annotations field 1880 log_debug(redefine, class, annotation)("length() is too small for num_annotations field"); 1881 return false; 1882 } 1883 1884 u2 num_annotations = Bytes::get_Java_u2((address) 1885 annotations_typeArray->adr_at(byte_i_ref)); 1886 byte_i_ref += 2; 1887 1888 log_debug(redefine, class, annotation)("num_annotations=%d", num_annotations); 1889 1890 int calc_num_annotations = 0; 1891 for (; calc_num_annotations < num_annotations; calc_num_annotations++) { 1892 if (!rewrite_cp_refs_in_annotation_struct(annotations_typeArray, 1893 byte_i_ref, THREAD)) { 1894 log_debug(redefine, class, annotation)("bad annotation_struct at %d", calc_num_annotations); 1895 // propagate failure back to caller 1896 return false; 1897 } 1898 } 1899 assert(num_annotations == calc_num_annotations, "sanity check"); 1900 1901 return true; 1902 } // end rewrite_cp_refs_in_annotations_typeArray() 1903 1904 1905 // Rewrite constant pool references in the annotation struct portion of 1906 // an annotations_typeArray. This "structure" is from section 4.8.15 of 1907 // the 2nd-edition of the VM spec: 1908 // 1909 // struct annotation { 1910 // u2 type_index; 1911 // u2 num_element_value_pairs; 1912 // { 1913 // u2 element_name_index; 1914 // element_value value; 1915 // } element_value_pairs[num_element_value_pairs]; 1916 // } 1917 // 1918 bool VM_RedefineClasses::rewrite_cp_refs_in_annotation_struct( 1919 AnnotationArray* annotations_typeArray, int &byte_i_ref, TRAPS) { 1920 if ((byte_i_ref + 2 + 2) > annotations_typeArray->length()) { 1921 // not enough room for smallest annotation_struct 1922 log_debug(redefine, class, annotation)("length() is too small for annotation_struct"); 1923 return false; 1924 } 1925 1926 u2 type_index = rewrite_cp_ref_in_annotation_data(annotations_typeArray, 1927 byte_i_ref, "type_index", THREAD); 1928 1929 u2 num_element_value_pairs = Bytes::get_Java_u2((address) 1930 annotations_typeArray->adr_at(byte_i_ref)); 1931 byte_i_ref += 2; 1932 1933 log_debug(redefine, class, annotation) 1934 ("type_index=%d num_element_value_pairs=%d", type_index, num_element_value_pairs); 1935 1936 int calc_num_element_value_pairs = 0; 1937 for (; calc_num_element_value_pairs < num_element_value_pairs; 1938 calc_num_element_value_pairs++) { 1939 if ((byte_i_ref + 2) > annotations_typeArray->length()) { 1940 // not enough room for another element_name_index, let alone 1941 // the rest of another component 1942 log_debug(redefine, class, annotation)("length() is too small for element_name_index"); 1943 return false; 1944 } 1945 1946 u2 element_name_index = rewrite_cp_ref_in_annotation_data( 1947 annotations_typeArray, byte_i_ref, 1948 "element_name_index", THREAD); 1949 1950 log_debug(redefine, class, annotation)("element_name_index=%d", element_name_index); 1951 1952 if (!rewrite_cp_refs_in_element_value(annotations_typeArray, 1953 byte_i_ref, THREAD)) { 1954 log_debug(redefine, class, annotation)("bad element_value at %d", calc_num_element_value_pairs); 1955 // propagate failure back to caller 1956 return false; 1957 } 1958 } // end for each component 1959 assert(num_element_value_pairs == calc_num_element_value_pairs, 1960 "sanity check"); 1961 1962 return true; 1963 } // end rewrite_cp_refs_in_annotation_struct() 1964 1965 1966 // Rewrite a constant pool reference at the current position in 1967 // annotations_typeArray if needed. Returns the original constant 1968 // pool reference if a rewrite was not needed or the new constant 1969 // pool reference if a rewrite was needed. 1970 u2 VM_RedefineClasses::rewrite_cp_ref_in_annotation_data( 1971 AnnotationArray* annotations_typeArray, int &byte_i_ref, 1972 const char * trace_mesg, TRAPS) { 1973 1974 address cp_index_addr = (address) 1975 annotations_typeArray->adr_at(byte_i_ref); 1976 u2 old_cp_index = Bytes::get_Java_u2(cp_index_addr); 1977 u2 new_cp_index = find_new_index(old_cp_index); 1978 if (new_cp_index != 0) { 1979 log_debug(redefine, class, annotation)("mapped old %s=%d", trace_mesg, old_cp_index); 1980 Bytes::put_Java_u2(cp_index_addr, new_cp_index); 1981 old_cp_index = new_cp_index; 1982 } 1983 byte_i_ref += 2; 1984 return old_cp_index; 1985 } 1986 1987 1988 // Rewrite constant pool references in the element_value portion of an 1989 // annotations_typeArray. This "structure" is from section 4.8.15.1 of 1990 // the 2nd-edition of the VM spec: 1991 // 1992 // struct element_value { 1993 // u1 tag; 1994 // union { 1995 // u2 const_value_index; 1996 // { 1997 // u2 type_name_index; 1998 // u2 const_name_index; 1999 // } enum_const_value; 2000 // u2 class_info_index; 2001 // annotation annotation_value; 2002 // struct { 2003 // u2 num_values; 2004 // element_value values[num_values]; 2005 // } array_value; 2006 // } value; 2007 // } 2008 // 2009 bool VM_RedefineClasses::rewrite_cp_refs_in_element_value( 2010 AnnotationArray* annotations_typeArray, int &byte_i_ref, TRAPS) { 2011 2012 if ((byte_i_ref + 1) > annotations_typeArray->length()) { 2013 // not enough room for a tag let alone the rest of an element_value 2014 log_debug(redefine, class, annotation)("length() is too small for a tag"); 2015 return false; 2016 } 2017 2018 u1 tag = annotations_typeArray->at(byte_i_ref); 2019 byte_i_ref++; 2020 log_debug(redefine, class, annotation)("tag='%c'", tag); 2021 2022 switch (tag) { 2023 // These BaseType tag values are from Table 4.2 in VM spec: 2024 case 'B': // byte 2025 case 'C': // char 2026 case 'D': // double 2027 case 'F': // float 2028 case 'I': // int 2029 case 'J': // long 2030 case 'S': // short 2031 case 'Z': // boolean 2032 2033 // The remaining tag values are from Table 4.8 in the 2nd-edition of 2034 // the VM spec: 2035 case 's': 2036 { 2037 // For the above tag values (including the BaseType values), 2038 // value.const_value_index is right union field. 2039 2040 if ((byte_i_ref + 2) > annotations_typeArray->length()) { 2041 // not enough room for a const_value_index 2042 log_debug(redefine, class, annotation)("length() is too small for a const_value_index"); 2043 return false; 2044 } 2045 2046 u2 const_value_index = rewrite_cp_ref_in_annotation_data( 2047 annotations_typeArray, byte_i_ref, 2048 "const_value_index", THREAD); 2049 2050 log_debug(redefine, class, annotation)("const_value_index=%d", const_value_index); 2051 } break; 2052 2053 case 'e': 2054 { 2055 // for the above tag value, value.enum_const_value is right union field 2056 2057 if ((byte_i_ref + 4) > annotations_typeArray->length()) { 2058 // not enough room for a enum_const_value 2059 log_debug(redefine, class, annotation)("length() is too small for a enum_const_value"); 2060 return false; 2061 } 2062 2063 u2 type_name_index = rewrite_cp_ref_in_annotation_data( 2064 annotations_typeArray, byte_i_ref, 2065 "type_name_index", THREAD); 2066 2067 u2 const_name_index = rewrite_cp_ref_in_annotation_data( 2068 annotations_typeArray, byte_i_ref, 2069 "const_name_index", THREAD); 2070 2071 log_debug(redefine, class, annotation) 2072 ("type_name_index=%d const_name_index=%d", type_name_index, const_name_index); 2073 } break; 2074 2075 case 'c': 2076 { 2077 // for the above tag value, value.class_info_index is right union field 2078 2079 if ((byte_i_ref + 2) > annotations_typeArray->length()) { 2080 // not enough room for a class_info_index 2081 log_debug(redefine, class, annotation)("length() is too small for a class_info_index"); 2082 return false; 2083 } 2084 2085 u2 class_info_index = rewrite_cp_ref_in_annotation_data( 2086 annotations_typeArray, byte_i_ref, 2087 "class_info_index", THREAD); 2088 2089 log_debug(redefine, class, annotation)("class_info_index=%d", class_info_index); 2090 } break; 2091 2092 case '@': 2093 // For the above tag value, value.attr_value is the right union 2094 // field. This is a nested annotation. 2095 if (!rewrite_cp_refs_in_annotation_struct(annotations_typeArray, 2096 byte_i_ref, THREAD)) { 2097 // propagate failure back to caller 2098 return false; 2099 } 2100 break; 2101 2102 case '[': 2103 { 2104 if ((byte_i_ref + 2) > annotations_typeArray->length()) { 2105 // not enough room for a num_values field 2106 log_debug(redefine, class, annotation)("length() is too small for a num_values field"); 2107 return false; 2108 } 2109 2110 // For the above tag value, value.array_value is the right union 2111 // field. This is an array of nested element_value. 2112 u2 num_values = Bytes::get_Java_u2((address) 2113 annotations_typeArray->adr_at(byte_i_ref)); 2114 byte_i_ref += 2; 2115 log_debug(redefine, class, annotation)("num_values=%d", num_values); 2116 2117 int calc_num_values = 0; 2118 for (; calc_num_values < num_values; calc_num_values++) { 2119 if (!rewrite_cp_refs_in_element_value( 2120 annotations_typeArray, byte_i_ref, THREAD)) { 2121 log_debug(redefine, class, annotation)("bad nested element_value at %d", calc_num_values); 2122 // propagate failure back to caller 2123 return false; 2124 } 2125 } 2126 assert(num_values == calc_num_values, "sanity check"); 2127 } break; 2128 2129 default: 2130 log_debug(redefine, class, annotation)("bad tag=0x%x", tag); 2131 return false; 2132 } // end decode tag field 2133 2134 return true; 2135 } // end rewrite_cp_refs_in_element_value() 2136 2137 2138 // Rewrite constant pool references in a fields_annotations field. 2139 bool VM_RedefineClasses::rewrite_cp_refs_in_fields_annotations( 2140 InstanceKlass* scratch_class, TRAPS) { 2141 2142 Array<AnnotationArray*>* fields_annotations = scratch_class->fields_annotations(); 2143 2144 if (fields_annotations == NULL || fields_annotations->length() == 0) { 2145 // no fields_annotations so nothing to do 2146 return true; 2147 } 2148 2149 log_debug(redefine, class, annotation)("fields_annotations length=%d", fields_annotations->length()); 2150 2151 for (int i = 0; i < fields_annotations->length(); i++) { 2152 AnnotationArray* field_annotations = fields_annotations->at(i); 2153 if (field_annotations == NULL || field_annotations->length() == 0) { 2154 // this field does not have any annotations so skip it 2155 continue; 2156 } 2157 2158 int byte_i = 0; // byte index into field_annotations 2159 if (!rewrite_cp_refs_in_annotations_typeArray(field_annotations, byte_i, 2160 THREAD)) { 2161 log_debug(redefine, class, annotation)("bad field_annotations at %d", i); 2162 // propagate failure back to caller 2163 return false; 2164 } 2165 } 2166 2167 return true; 2168 } // end rewrite_cp_refs_in_fields_annotations() 2169 2170 2171 // Rewrite constant pool references in a methods_annotations field. 2172 bool VM_RedefineClasses::rewrite_cp_refs_in_methods_annotations( 2173 InstanceKlass* scratch_class, TRAPS) { 2174 2175 for (int i = 0; i < scratch_class->methods()->length(); i++) { 2176 Method* m = scratch_class->methods()->at(i); 2177 AnnotationArray* method_annotations = m->constMethod()->method_annotations(); 2178 2179 if (method_annotations == NULL || method_annotations->length() == 0) { 2180 // this method does not have any annotations so skip it 2181 continue; 2182 } 2183 2184 int byte_i = 0; // byte index into method_annotations 2185 if (!rewrite_cp_refs_in_annotations_typeArray(method_annotations, byte_i, 2186 THREAD)) { 2187 log_debug(redefine, class, annotation)("bad method_annotations at %d", i); 2188 // propagate failure back to caller 2189 return false; 2190 } 2191 } 2192 2193 return true; 2194 } // end rewrite_cp_refs_in_methods_annotations() 2195 2196 2197 // Rewrite constant pool references in a methods_parameter_annotations 2198 // field. This "structure" is adapted from the 2199 // RuntimeVisibleParameterAnnotations_attribute described in section 2200 // 4.8.17 of the 2nd-edition of the VM spec: 2201 // 2202 // methods_parameter_annotations_typeArray { 2203 // u1 num_parameters; 2204 // { 2205 // u2 num_annotations; 2206 // annotation annotations[num_annotations]; 2207 // } parameter_annotations[num_parameters]; 2208 // } 2209 // 2210 bool VM_RedefineClasses::rewrite_cp_refs_in_methods_parameter_annotations( 2211 InstanceKlass* scratch_class, TRAPS) { 2212 2213 for (int i = 0; i < scratch_class->methods()->length(); i++) { 2214 Method* m = scratch_class->methods()->at(i); 2215 AnnotationArray* method_parameter_annotations = m->constMethod()->parameter_annotations(); 2216 if (method_parameter_annotations == NULL 2217 || method_parameter_annotations->length() == 0) { 2218 // this method does not have any parameter annotations so skip it 2219 continue; 2220 } 2221 2222 if (method_parameter_annotations->length() < 1) { 2223 // not enough room for a num_parameters field 2224 log_debug(redefine, class, annotation)("length() is too small for a num_parameters field at %d", i); 2225 return false; 2226 } 2227 2228 int byte_i = 0; // byte index into method_parameter_annotations 2229 2230 u1 num_parameters = method_parameter_annotations->at(byte_i); 2231 byte_i++; 2232 2233 log_debug(redefine, class, annotation)("num_parameters=%d", num_parameters); 2234 2235 int calc_num_parameters = 0; 2236 for (; calc_num_parameters < num_parameters; calc_num_parameters++) { 2237 if (!rewrite_cp_refs_in_annotations_typeArray( 2238 method_parameter_annotations, byte_i, THREAD)) { 2239 log_debug(redefine, class, annotation)("bad method_parameter_annotations at %d", calc_num_parameters); 2240 // propagate failure back to caller 2241 return false; 2242 } 2243 } 2244 assert(num_parameters == calc_num_parameters, "sanity check"); 2245 } 2246 2247 return true; 2248 } // end rewrite_cp_refs_in_methods_parameter_annotations() 2249 2250 2251 // Rewrite constant pool references in a methods_default_annotations 2252 // field. This "structure" is adapted from the AnnotationDefault_attribute 2253 // that is described in section 4.8.19 of the 2nd-edition of the VM spec: 2254 // 2255 // methods_default_annotations_typeArray { 2256 // element_value default_value; 2257 // } 2258 // 2259 bool VM_RedefineClasses::rewrite_cp_refs_in_methods_default_annotations( 2260 InstanceKlass* scratch_class, TRAPS) { 2261 2262 for (int i = 0; i < scratch_class->methods()->length(); i++) { 2263 Method* m = scratch_class->methods()->at(i); 2264 AnnotationArray* method_default_annotations = m->constMethod()->default_annotations(); 2265 if (method_default_annotations == NULL 2266 || method_default_annotations->length() == 0) { 2267 // this method does not have any default annotations so skip it 2268 continue; 2269 } 2270 2271 int byte_i = 0; // byte index into method_default_annotations 2272 2273 if (!rewrite_cp_refs_in_element_value( 2274 method_default_annotations, byte_i, THREAD)) { 2275 log_debug(redefine, class, annotation)("bad default element_value at %d", i); 2276 // propagate failure back to caller 2277 return false; 2278 } 2279 } 2280 2281 return true; 2282 } // end rewrite_cp_refs_in_methods_default_annotations() 2283 2284 2285 // Rewrite constant pool references in a class_type_annotations field. 2286 bool VM_RedefineClasses::rewrite_cp_refs_in_class_type_annotations( 2287 InstanceKlass* scratch_class, TRAPS) { 2288 2289 AnnotationArray* class_type_annotations = scratch_class->class_type_annotations(); 2290 if (class_type_annotations == NULL || class_type_annotations->length() == 0) { 2291 // no class_type_annotations so nothing to do 2292 return true; 2293 } 2294 2295 log_debug(redefine, class, annotation)("class_type_annotations length=%d", class_type_annotations->length()); 2296 2297 int byte_i = 0; // byte index into class_type_annotations 2298 return rewrite_cp_refs_in_type_annotations_typeArray(class_type_annotations, 2299 byte_i, "ClassFile", THREAD); 2300 } // end rewrite_cp_refs_in_class_type_annotations() 2301 2302 2303 // Rewrite constant pool references in a fields_type_annotations field. 2304 bool VM_RedefineClasses::rewrite_cp_refs_in_fields_type_annotations( 2305 InstanceKlass* scratch_class, TRAPS) { 2306 2307 Array<AnnotationArray*>* fields_type_annotations = scratch_class->fields_type_annotations(); 2308 if (fields_type_annotations == NULL || fields_type_annotations->length() == 0) { 2309 // no fields_type_annotations so nothing to do 2310 return true; 2311 } 2312 2313 log_debug(redefine, class, annotation)("fields_type_annotations length=%d", fields_type_annotations->length()); 2314 2315 for (int i = 0; i < fields_type_annotations->length(); i++) { 2316 AnnotationArray* field_type_annotations = fields_type_annotations->at(i); 2317 if (field_type_annotations == NULL || field_type_annotations->length() == 0) { 2318 // this field does not have any annotations so skip it 2319 continue; 2320 } 2321 2322 int byte_i = 0; // byte index into field_type_annotations 2323 if (!rewrite_cp_refs_in_type_annotations_typeArray(field_type_annotations, 2324 byte_i, "field_info", THREAD)) { 2325 log_debug(redefine, class, annotation)("bad field_type_annotations at %d", i); 2326 // propagate failure back to caller 2327 return false; 2328 } 2329 } 2330 2331 return true; 2332 } // end rewrite_cp_refs_in_fields_type_annotations() 2333 2334 2335 // Rewrite constant pool references in a methods_type_annotations field. 2336 bool VM_RedefineClasses::rewrite_cp_refs_in_methods_type_annotations( 2337 InstanceKlass* scratch_class, TRAPS) { 2338 2339 for (int i = 0; i < scratch_class->methods()->length(); i++) { 2340 Method* m = scratch_class->methods()->at(i); 2341 AnnotationArray* method_type_annotations = m->constMethod()->type_annotations(); 2342 2343 if (method_type_annotations == NULL || method_type_annotations->length() == 0) { 2344 // this method does not have any annotations so skip it 2345 continue; 2346 } 2347 2348 log_debug(redefine, class, annotation)("methods type_annotations length=%d", method_type_annotations->length()); 2349 2350 int byte_i = 0; // byte index into method_type_annotations 2351 if (!rewrite_cp_refs_in_type_annotations_typeArray(method_type_annotations, 2352 byte_i, "method_info", THREAD)) { 2353 log_debug(redefine, class, annotation)("bad method_type_annotations at %d", i); 2354 // propagate failure back to caller 2355 return false; 2356 } 2357 } 2358 2359 return true; 2360 } // end rewrite_cp_refs_in_methods_type_annotations() 2361 2362 2363 // Rewrite constant pool references in a type_annotations 2364 // field. This "structure" is adapted from the 2365 // RuntimeVisibleTypeAnnotations_attribute described in 2366 // section 4.7.20 of the Java SE 8 Edition of the VM spec: 2367 // 2368 // type_annotations_typeArray { 2369 // u2 num_annotations; 2370 // type_annotation annotations[num_annotations]; 2371 // } 2372 // 2373 bool VM_RedefineClasses::rewrite_cp_refs_in_type_annotations_typeArray( 2374 AnnotationArray* type_annotations_typeArray, int &byte_i_ref, 2375 const char * location_mesg, TRAPS) { 2376 2377 if ((byte_i_ref + 2) > type_annotations_typeArray->length()) { 2378 // not enough room for num_annotations field 2379 log_debug(redefine, class, annotation)("length() is too small for num_annotations field"); 2380 return false; 2381 } 2382 2383 u2 num_annotations = Bytes::get_Java_u2((address) 2384 type_annotations_typeArray->adr_at(byte_i_ref)); 2385 byte_i_ref += 2; 2386 2387 log_debug(redefine, class, annotation)("num_type_annotations=%d", num_annotations); 2388 2389 int calc_num_annotations = 0; 2390 for (; calc_num_annotations < num_annotations; calc_num_annotations++) { 2391 if (!rewrite_cp_refs_in_type_annotation_struct(type_annotations_typeArray, 2392 byte_i_ref, location_mesg, THREAD)) { 2393 log_debug(redefine, class, annotation)("bad type_annotation_struct at %d", calc_num_annotations); 2394 // propagate failure back to caller 2395 return false; 2396 } 2397 } 2398 assert(num_annotations == calc_num_annotations, "sanity check"); 2399 2400 if (byte_i_ref != type_annotations_typeArray->length()) { 2401 log_debug(redefine, class, annotation) 2402 ("read wrong amount of bytes at end of processing type_annotations_typeArray (%d of %d bytes were read)", 2403 byte_i_ref, type_annotations_typeArray->length()); 2404 return false; 2405 } 2406 2407 return true; 2408 } // end rewrite_cp_refs_in_type_annotations_typeArray() 2409 2410 2411 // Rewrite constant pool references in a type_annotation 2412 // field. This "structure" is adapted from the 2413 // RuntimeVisibleTypeAnnotations_attribute described in 2414 // section 4.7.20 of the Java SE 8 Edition of the VM spec: 2415 // 2416 // type_annotation { 2417 // u1 target_type; 2418 // union { 2419 // type_parameter_target; 2420 // supertype_target; 2421 // type_parameter_bound_target; 2422 // empty_target; 2423 // method_formal_parameter_target; 2424 // throws_target; 2425 // localvar_target; 2426 // catch_target; 2427 // offset_target; 2428 // type_argument_target; 2429 // } target_info; 2430 // type_path target_path; 2431 // annotation anno; 2432 // } 2433 // 2434 bool VM_RedefineClasses::rewrite_cp_refs_in_type_annotation_struct( 2435 AnnotationArray* type_annotations_typeArray, int &byte_i_ref, 2436 const char * location_mesg, TRAPS) { 2437 2438 if (!skip_type_annotation_target(type_annotations_typeArray, 2439 byte_i_ref, location_mesg, THREAD)) { 2440 return false; 2441 } 2442 2443 if (!skip_type_annotation_type_path(type_annotations_typeArray, 2444 byte_i_ref, THREAD)) { 2445 return false; 2446 } 2447 2448 if (!rewrite_cp_refs_in_annotation_struct(type_annotations_typeArray, 2449 byte_i_ref, THREAD)) { 2450 return false; 2451 } 2452 2453 return true; 2454 } // end rewrite_cp_refs_in_type_annotation_struct() 2455 2456 2457 // Read, verify and skip over the target_type and target_info part 2458 // so that rewriting can continue in the later parts of the struct. 2459 // 2460 // u1 target_type; 2461 // union { 2462 // type_parameter_target; 2463 // supertype_target; 2464 // type_parameter_bound_target; 2465 // empty_target; 2466 // method_formal_parameter_target; 2467 // throws_target; 2468 // localvar_target; 2469 // catch_target; 2470 // offset_target; 2471 // type_argument_target; 2472 // } target_info; 2473 // 2474 bool VM_RedefineClasses::skip_type_annotation_target( 2475 AnnotationArray* type_annotations_typeArray, int &byte_i_ref, 2476 const char * location_mesg, TRAPS) { 2477 2478 if ((byte_i_ref + 1) > type_annotations_typeArray->length()) { 2479 // not enough room for a target_type let alone the rest of a type_annotation 2480 log_debug(redefine, class, annotation)("length() is too small for a target_type"); 2481 return false; 2482 } 2483 2484 u1 target_type = type_annotations_typeArray->at(byte_i_ref); 2485 byte_i_ref += 1; 2486 log_debug(redefine, class, annotation)("target_type=0x%.2x", target_type); 2487 log_debug(redefine, class, annotation)("location=%s", location_mesg); 2488 2489 // Skip over target_info 2490 switch (target_type) { 2491 case 0x00: 2492 // kind: type parameter declaration of generic class or interface 2493 // location: ClassFile 2494 case 0x01: 2495 // kind: type parameter declaration of generic method or constructor 2496 // location: method_info 2497 2498 { 2499 // struct: 2500 // type_parameter_target { 2501 // u1 type_parameter_index; 2502 // } 2503 // 2504 if ((byte_i_ref + 1) > type_annotations_typeArray->length()) { 2505 log_debug(redefine, class, annotation)("length() is too small for a type_parameter_target"); 2506 return false; 2507 } 2508 2509 u1 type_parameter_index = type_annotations_typeArray->at(byte_i_ref); 2510 byte_i_ref += 1; 2511 2512 log_debug(redefine, class, annotation)("type_parameter_target: type_parameter_index=%d", type_parameter_index); 2513 } break; 2514 2515 case 0x10: 2516 // kind: type in extends clause of class or interface declaration 2517 // (including the direct superclass of an anonymous class declaration), 2518 // or in implements clause of interface declaration 2519 // location: ClassFile 2520 2521 { 2522 // struct: 2523 // supertype_target { 2524 // u2 supertype_index; 2525 // } 2526 // 2527 if ((byte_i_ref + 2) > type_annotations_typeArray->length()) { 2528 log_debug(redefine, class, annotation)("length() is too small for a supertype_target"); 2529 return false; 2530 } 2531 2532 u2 supertype_index = Bytes::get_Java_u2((address) 2533 type_annotations_typeArray->adr_at(byte_i_ref)); 2534 byte_i_ref += 2; 2535 2536 log_debug(redefine, class, annotation)("supertype_target: supertype_index=%d", supertype_index); 2537 } break; 2538 2539 case 0x11: 2540 // kind: type in bound of type parameter declaration of generic class or interface 2541 // location: ClassFile 2542 case 0x12: 2543 // kind: type in bound of type parameter declaration of generic method or constructor 2544 // location: method_info 2545 2546 { 2547 // struct: 2548 // type_parameter_bound_target { 2549 // u1 type_parameter_index; 2550 // u1 bound_index; 2551 // } 2552 // 2553 if ((byte_i_ref + 2) > type_annotations_typeArray->length()) { 2554 log_debug(redefine, class, annotation)("length() is too small for a type_parameter_bound_target"); 2555 return false; 2556 } 2557 2558 u1 type_parameter_index = type_annotations_typeArray->at(byte_i_ref); 2559 byte_i_ref += 1; 2560 u1 bound_index = type_annotations_typeArray->at(byte_i_ref); 2561 byte_i_ref += 1; 2562 2563 log_debug(redefine, class, annotation) 2564 ("type_parameter_bound_target: type_parameter_index=%d, bound_index=%d", type_parameter_index, bound_index); 2565 } break; 2566 2567 case 0x13: 2568 // kind: type in field declaration 2569 // location: field_info 2570 case 0x14: 2571 // kind: return type of method, or type of newly constructed object 2572 // location: method_info 2573 case 0x15: 2574 // kind: receiver type of method or constructor 2575 // location: method_info 2576 2577 { 2578 // struct: 2579 // empty_target { 2580 // } 2581 // 2582 log_debug(redefine, class, annotation)("empty_target"); 2583 } break; 2584 2585 case 0x16: 2586 // kind: type in formal parameter declaration of method, constructor, or lambda expression 2587 // location: method_info 2588 2589 { 2590 // struct: 2591 // formal_parameter_target { 2592 // u1 formal_parameter_index; 2593 // } 2594 // 2595 if ((byte_i_ref + 1) > type_annotations_typeArray->length()) { 2596 log_debug(redefine, class, annotation)("length() is too small for a formal_parameter_target"); 2597 return false; 2598 } 2599 2600 u1 formal_parameter_index = type_annotations_typeArray->at(byte_i_ref); 2601 byte_i_ref += 1; 2602 2603 log_debug(redefine, class, annotation) 2604 ("formal_parameter_target: formal_parameter_index=%d", formal_parameter_index); 2605 } break; 2606 2607 case 0x17: 2608 // kind: type in throws clause of method or constructor 2609 // location: method_info 2610 2611 { 2612 // struct: 2613 // throws_target { 2614 // u2 throws_type_index 2615 // } 2616 // 2617 if ((byte_i_ref + 2) > type_annotations_typeArray->length()) { 2618 log_debug(redefine, class, annotation)("length() is too small for a throws_target"); 2619 return false; 2620 } 2621 2622 u2 throws_type_index = Bytes::get_Java_u2((address) 2623 type_annotations_typeArray->adr_at(byte_i_ref)); 2624 byte_i_ref += 2; 2625 2626 log_debug(redefine, class, annotation)("throws_target: throws_type_index=%d", throws_type_index); 2627 } break; 2628 2629 case 0x40: 2630 // kind: type in local variable declaration 2631 // location: Code 2632 case 0x41: 2633 // kind: type in resource variable declaration 2634 // location: Code 2635 2636 { 2637 // struct: 2638 // localvar_target { 2639 // u2 table_length; 2640 // struct { 2641 // u2 start_pc; 2642 // u2 length; 2643 // u2 index; 2644 // } table[table_length]; 2645 // } 2646 // 2647 if ((byte_i_ref + 2) > type_annotations_typeArray->length()) { 2648 // not enough room for a table_length let alone the rest of a localvar_target 2649 log_debug(redefine, class, annotation)("length() is too small for a localvar_target table_length"); 2650 return false; 2651 } 2652 2653 u2 table_length = Bytes::get_Java_u2((address) 2654 type_annotations_typeArray->adr_at(byte_i_ref)); 2655 byte_i_ref += 2; 2656 2657 log_debug(redefine, class, annotation)("localvar_target: table_length=%d", table_length); 2658 2659 int table_struct_size = 2 + 2 + 2; // 3 u2 variables per table entry 2660 int table_size = table_length * table_struct_size; 2661 2662 if ((byte_i_ref + table_size) > type_annotations_typeArray->length()) { 2663 // not enough room for a table 2664 log_debug(redefine, class, annotation)("length() is too small for a table array of length %d", table_length); 2665 return false; 2666 } 2667 2668 // Skip over table 2669 byte_i_ref += table_size; 2670 } break; 2671 2672 case 0x42: 2673 // kind: type in exception parameter declaration 2674 // location: Code 2675 2676 { 2677 // struct: 2678 // catch_target { 2679 // u2 exception_table_index; 2680 // } 2681 // 2682 if ((byte_i_ref + 2) > type_annotations_typeArray->length()) { 2683 log_debug(redefine, class, annotation)("length() is too small for a catch_target"); 2684 return false; 2685 } 2686 2687 u2 exception_table_index = Bytes::get_Java_u2((address) 2688 type_annotations_typeArray->adr_at(byte_i_ref)); 2689 byte_i_ref += 2; 2690 2691 log_debug(redefine, class, annotation)("catch_target: exception_table_index=%d", exception_table_index); 2692 } break; 2693 2694 case 0x43: 2695 // kind: type in instanceof expression 2696 // location: Code 2697 case 0x44: 2698 // kind: type in new expression 2699 // location: Code 2700 case 0x45: 2701 // kind: type in method reference expression using ::new 2702 // location: Code 2703 case 0x46: 2704 // kind: type in method reference expression using ::Identifier 2705 // location: Code 2706 2707 { 2708 // struct: 2709 // offset_target { 2710 // u2 offset; 2711 // } 2712 // 2713 if ((byte_i_ref + 2) > type_annotations_typeArray->length()) { 2714 log_debug(redefine, class, annotation)("length() is too small for a offset_target"); 2715 return false; 2716 } 2717 2718 u2 offset = Bytes::get_Java_u2((address) 2719 type_annotations_typeArray->adr_at(byte_i_ref)); 2720 byte_i_ref += 2; 2721 2722 log_debug(redefine, class, annotation)("offset_target: offset=%d", offset); 2723 } break; 2724 2725 case 0x47: 2726 // kind: type in cast expression 2727 // location: Code 2728 case 0x48: 2729 // kind: type argument for generic constructor in new expression or 2730 // explicit constructor invocation statement 2731 // location: Code 2732 case 0x49: 2733 // kind: type argument for generic method in method invocation expression 2734 // location: Code 2735 case 0x4A: 2736 // kind: type argument for generic constructor in method reference expression using ::new 2737 // location: Code 2738 case 0x4B: 2739 // kind: type argument for generic method in method reference expression using ::Identifier 2740 // location: Code 2741 2742 { 2743 // struct: 2744 // type_argument_target { 2745 // u2 offset; 2746 // u1 type_argument_index; 2747 // } 2748 // 2749 if ((byte_i_ref + 3) > type_annotations_typeArray->length()) { 2750 log_debug(redefine, class, annotation)("length() is too small for a type_argument_target"); 2751 return false; 2752 } 2753 2754 u2 offset = Bytes::get_Java_u2((address) 2755 type_annotations_typeArray->adr_at(byte_i_ref)); 2756 byte_i_ref += 2; 2757 u1 type_argument_index = type_annotations_typeArray->at(byte_i_ref); 2758 byte_i_ref += 1; 2759 2760 log_debug(redefine, class, annotation) 2761 ("type_argument_target: offset=%d, type_argument_index=%d", offset, type_argument_index); 2762 } break; 2763 2764 default: 2765 log_debug(redefine, class, annotation)("unknown target_type"); 2766 #ifdef ASSERT 2767 ShouldNotReachHere(); 2768 #endif 2769 return false; 2770 } 2771 2772 return true; 2773 } // end skip_type_annotation_target() 2774 2775 2776 // Read, verify and skip over the type_path part so that rewriting 2777 // can continue in the later parts of the struct. 2778 // 2779 // type_path { 2780 // u1 path_length; 2781 // { 2782 // u1 type_path_kind; 2783 // u1 type_argument_index; 2784 // } path[path_length]; 2785 // } 2786 // 2787 bool VM_RedefineClasses::skip_type_annotation_type_path( 2788 AnnotationArray* type_annotations_typeArray, int &byte_i_ref, TRAPS) { 2789 2790 if ((byte_i_ref + 1) > type_annotations_typeArray->length()) { 2791 // not enough room for a path_length let alone the rest of the type_path 2792 log_debug(redefine, class, annotation)("length() is too small for a type_path"); 2793 return false; 2794 } 2795 2796 u1 path_length = type_annotations_typeArray->at(byte_i_ref); 2797 byte_i_ref += 1; 2798 2799 log_debug(redefine, class, annotation)("type_path: path_length=%d", path_length); 2800 2801 int calc_path_length = 0; 2802 for (; calc_path_length < path_length; calc_path_length++) { 2803 if ((byte_i_ref + 1 + 1) > type_annotations_typeArray->length()) { 2804 // not enough room for a path 2805 log_debug(redefine, class, annotation) 2806 ("length() is too small for path entry %d of %d", calc_path_length, path_length); 2807 return false; 2808 } 2809 2810 u1 type_path_kind = type_annotations_typeArray->at(byte_i_ref); 2811 byte_i_ref += 1; 2812 u1 type_argument_index = type_annotations_typeArray->at(byte_i_ref); 2813 byte_i_ref += 1; 2814 2815 log_debug(redefine, class, annotation) 2816 ("type_path: path[%d]: type_path_kind=%d, type_argument_index=%d", 2817 calc_path_length, type_path_kind, type_argument_index); 2818 2819 if (type_path_kind > 3 || (type_path_kind != 3 && type_argument_index != 0)) { 2820 // not enough room for a path 2821 log_debug(redefine, class, annotation)("inconsistent type_path values"); 2822 return false; 2823 } 2824 } 2825 assert(path_length == calc_path_length, "sanity check"); 2826 2827 return true; 2828 } // end skip_type_annotation_type_path() 2829 2830 2831 // Rewrite constant pool references in the method's stackmap table. 2832 // These "structures" are adapted from the StackMapTable_attribute that 2833 // is described in section 4.8.4 of the 6.0 version of the VM spec 2834 // (dated 2005.10.26): 2835 // file:///net/quincunx.sfbay/export/gbracha/ClassFile-Java6.pdf 2836 // 2837 // stack_map { 2838 // u2 number_of_entries; 2839 // stack_map_frame entries[number_of_entries]; 2840 // } 2841 // 2842 void VM_RedefineClasses::rewrite_cp_refs_in_stack_map_table( 2843 const methodHandle& method, TRAPS) { 2844 2845 if (!method->has_stackmap_table()) { 2846 return; 2847 } 2848 2849 AnnotationArray* stackmap_data = method->stackmap_data(); 2850 address stackmap_p = (address)stackmap_data->adr_at(0); 2851 address stackmap_end = stackmap_p + stackmap_data->length(); 2852 2853 assert(stackmap_p + 2 <= stackmap_end, "no room for number_of_entries"); 2854 u2 number_of_entries = Bytes::get_Java_u2(stackmap_p); 2855 stackmap_p += 2; 2856 2857 log_debug(redefine, class, stackmap)("number_of_entries=%u", number_of_entries); 2858 2859 // walk through each stack_map_frame 2860 u2 calc_number_of_entries = 0; 2861 for (; calc_number_of_entries < number_of_entries; calc_number_of_entries++) { 2862 // The stack_map_frame structure is a u1 frame_type followed by 2863 // 0 or more bytes of data: 2864 // 2865 // union stack_map_frame { 2866 // same_frame; 2867 // same_locals_1_stack_item_frame; 2868 // same_locals_1_stack_item_frame_extended; 2869 // chop_frame; 2870 // same_frame_extended; 2871 // append_frame; 2872 // full_frame; 2873 // } 2874 2875 assert(stackmap_p + 1 <= stackmap_end, "no room for frame_type"); 2876 u1 frame_type = *stackmap_p; 2877 stackmap_p++; 2878 2879 // same_frame { 2880 // u1 frame_type = SAME; /* 0-63 */ 2881 // } 2882 if (frame_type <= 63) { 2883 // nothing more to do for same_frame 2884 } 2885 2886 // same_locals_1_stack_item_frame { 2887 // u1 frame_type = SAME_LOCALS_1_STACK_ITEM; /* 64-127 */ 2888 // verification_type_info stack[1]; 2889 // } 2890 else if (frame_type >= 64 && frame_type <= 127) { 2891 rewrite_cp_refs_in_verification_type_info(stackmap_p, stackmap_end, 2892 calc_number_of_entries, frame_type, THREAD); 2893 } 2894 2895 // reserved for future use 2896 else if (frame_type >= 128 && frame_type <= 246) { 2897 // nothing more to do for reserved frame_types 2898 } 2899 2900 // same_locals_1_stack_item_frame_extended { 2901 // u1 frame_type = SAME_LOCALS_1_STACK_ITEM_EXTENDED; /* 247 */ 2902 // u2 offset_delta; 2903 // verification_type_info stack[1]; 2904 // } 2905 else if (frame_type == 247) { 2906 stackmap_p += 2; 2907 rewrite_cp_refs_in_verification_type_info(stackmap_p, stackmap_end, 2908 calc_number_of_entries, frame_type, THREAD); 2909 } 2910 2911 // chop_frame { 2912 // u1 frame_type = CHOP; /* 248-250 */ 2913 // u2 offset_delta; 2914 // } 2915 else if (frame_type >= 248 && frame_type <= 250) { 2916 stackmap_p += 2; 2917 } 2918 2919 // same_frame_extended { 2920 // u1 frame_type = SAME_FRAME_EXTENDED; /* 251*/ 2921 // u2 offset_delta; 2922 // } 2923 else if (frame_type == 251) { 2924 stackmap_p += 2; 2925 } 2926 2927 // append_frame { 2928 // u1 frame_type = APPEND; /* 252-254 */ 2929 // u2 offset_delta; 2930 // verification_type_info locals[frame_type - 251]; 2931 // } 2932 else if (frame_type >= 252 && frame_type <= 254) { 2933 assert(stackmap_p + 2 <= stackmap_end, 2934 "no room for offset_delta"); 2935 stackmap_p += 2; 2936 u1 len = frame_type - 251; 2937 for (u1 i = 0; i < len; i++) { 2938 rewrite_cp_refs_in_verification_type_info(stackmap_p, stackmap_end, 2939 calc_number_of_entries, frame_type, THREAD); 2940 } 2941 } 2942 2943 // full_frame { 2944 // u1 frame_type = FULL_FRAME; /* 255 */ 2945 // u2 offset_delta; 2946 // u2 number_of_locals; 2947 // verification_type_info locals[number_of_locals]; 2948 // u2 number_of_stack_items; 2949 // verification_type_info stack[number_of_stack_items]; 2950 // } 2951 else if (frame_type == 255) { 2952 assert(stackmap_p + 2 + 2 <= stackmap_end, 2953 "no room for smallest full_frame"); 2954 stackmap_p += 2; 2955 2956 u2 number_of_locals = Bytes::get_Java_u2(stackmap_p); 2957 stackmap_p += 2; 2958 2959 for (u2 locals_i = 0; locals_i < number_of_locals; locals_i++) { 2960 rewrite_cp_refs_in_verification_type_info(stackmap_p, stackmap_end, 2961 calc_number_of_entries, frame_type, THREAD); 2962 } 2963 2964 // Use the largest size for the number_of_stack_items, but only get 2965 // the right number of bytes. 2966 u2 number_of_stack_items = Bytes::get_Java_u2(stackmap_p); 2967 stackmap_p += 2; 2968 2969 for (u2 stack_i = 0; stack_i < number_of_stack_items; stack_i++) { 2970 rewrite_cp_refs_in_verification_type_info(stackmap_p, stackmap_end, 2971 calc_number_of_entries, frame_type, THREAD); 2972 } 2973 } 2974 } // end while there is a stack_map_frame 2975 assert(number_of_entries == calc_number_of_entries, "sanity check"); 2976 } // end rewrite_cp_refs_in_stack_map_table() 2977 2978 2979 // Rewrite constant pool references in the verification type info 2980 // portion of the method's stackmap table. These "structures" are 2981 // adapted from the StackMapTable_attribute that is described in 2982 // section 4.8.4 of the 6.0 version of the VM spec (dated 2005.10.26): 2983 // file:///net/quincunx.sfbay/export/gbracha/ClassFile-Java6.pdf 2984 // 2985 // The verification_type_info structure is a u1 tag followed by 0 or 2986 // more bytes of data: 2987 // 2988 // union verification_type_info { 2989 // Top_variable_info; 2990 // Integer_variable_info; 2991 // Float_variable_info; 2992 // Long_variable_info; 2993 // Double_variable_info; 2994 // Null_variable_info; 2995 // UninitializedThis_variable_info; 2996 // Object_variable_info; 2997 // Uninitialized_variable_info; 2998 // } 2999 // 3000 void VM_RedefineClasses::rewrite_cp_refs_in_verification_type_info( 3001 address& stackmap_p_ref, address stackmap_end, u2 frame_i, 3002 u1 frame_type, TRAPS) { 3003 3004 assert(stackmap_p_ref + 1 <= stackmap_end, "no room for tag"); 3005 u1 tag = *stackmap_p_ref; 3006 stackmap_p_ref++; 3007 3008 switch (tag) { 3009 // Top_variable_info { 3010 // u1 tag = ITEM_Top; /* 0 */ 3011 // } 3012 // verificationType.hpp has zero as ITEM_Bogus instead of ITEM_Top 3013 case 0: // fall through 3014 3015 // Integer_variable_info { 3016 // u1 tag = ITEM_Integer; /* 1 */ 3017 // } 3018 case ITEM_Integer: // fall through 3019 3020 // Float_variable_info { 3021 // u1 tag = ITEM_Float; /* 2 */ 3022 // } 3023 case ITEM_Float: // fall through 3024 3025 // Double_variable_info { 3026 // u1 tag = ITEM_Double; /* 3 */ 3027 // } 3028 case ITEM_Double: // fall through 3029 3030 // Long_variable_info { 3031 // u1 tag = ITEM_Long; /* 4 */ 3032 // } 3033 case ITEM_Long: // fall through 3034 3035 // Null_variable_info { 3036 // u1 tag = ITEM_Null; /* 5 */ 3037 // } 3038 case ITEM_Null: // fall through 3039 3040 // UninitializedThis_variable_info { 3041 // u1 tag = ITEM_UninitializedThis; /* 6 */ 3042 // } 3043 case ITEM_UninitializedThis: 3044 // nothing more to do for the above tag types 3045 break; 3046 3047 // Object_variable_info { 3048 // u1 tag = ITEM_Object; /* 7 */ 3049 // u2 cpool_index; 3050 // } 3051 case ITEM_Object: 3052 { 3053 assert(stackmap_p_ref + 2 <= stackmap_end, "no room for cpool_index"); 3054 u2 cpool_index = Bytes::get_Java_u2(stackmap_p_ref); 3055 u2 new_cp_index = find_new_index(cpool_index); 3056 if (new_cp_index != 0) { 3057 log_debug(redefine, class, stackmap)("mapped old cpool_index=%d", cpool_index); 3058 Bytes::put_Java_u2(stackmap_p_ref, new_cp_index); 3059 cpool_index = new_cp_index; 3060 } 3061 stackmap_p_ref += 2; 3062 3063 log_debug(redefine, class, stackmap) 3064 ("frame_i=%u, frame_type=%u, cpool_index=%d", frame_i, frame_type, cpool_index); 3065 } break; 3066 3067 // Uninitialized_variable_info { 3068 // u1 tag = ITEM_Uninitialized; /* 8 */ 3069 // u2 offset; 3070 // } 3071 case ITEM_Uninitialized: 3072 assert(stackmap_p_ref + 2 <= stackmap_end, "no room for offset"); 3073 stackmap_p_ref += 2; 3074 break; 3075 3076 default: 3077 log_debug(redefine, class, stackmap)("frame_i=%u, frame_type=%u, bad tag=0x%x", frame_i, frame_type, tag); 3078 ShouldNotReachHere(); 3079 break; 3080 } // end switch (tag) 3081 } // end rewrite_cp_refs_in_verification_type_info() 3082 3083 3084 // Change the constant pool associated with klass scratch_class to 3085 // scratch_cp. If shrink is true, then scratch_cp_length elements 3086 // are copied from scratch_cp to a smaller constant pool and the 3087 // smaller constant pool is associated with scratch_class. 3088 void VM_RedefineClasses::set_new_constant_pool( 3089 ClassLoaderData* loader_data, 3090 InstanceKlass* scratch_class, constantPoolHandle scratch_cp, 3091 int scratch_cp_length, TRAPS) { 3092 assert(scratch_cp->length() >= scratch_cp_length, "sanity check"); 3093 3094 // scratch_cp is a merged constant pool and has enough space for a 3095 // worst case merge situation. We want to associate the minimum 3096 // sized constant pool with the klass to save space. 3097 ConstantPool* cp = ConstantPool::allocate(loader_data, scratch_cp_length, CHECK); 3098 constantPoolHandle smaller_cp(THREAD, cp); 3099 3100 // preserve version() value in the smaller copy 3101 int version = scratch_cp->version(); 3102 assert(version != 0, "sanity check"); 3103 smaller_cp->set_version(version); 3104 3105 // attach klass to new constant pool 3106 // reference to the cp holder is needed for copy_operands() 3107 smaller_cp->set_pool_holder(scratch_class); 3108 3109 scratch_cp->copy_cp_to(1, scratch_cp_length - 1, smaller_cp, 1, THREAD); 3110 if (HAS_PENDING_EXCEPTION) { 3111 // Exception is handled in the caller 3112 loader_data->add_to_deallocate_list(smaller_cp()); 3113 return; 3114 } 3115 scratch_cp = smaller_cp; 3116 3117 // attach new constant pool to klass 3118 scratch_class->set_constants(scratch_cp()); 3119 scratch_cp->initialize_unresolved_klasses(loader_data, CHECK); 3120 3121 int i; // for portability 3122 3123 // update each field in klass to use new constant pool indices as needed 3124 for (JavaFieldStream fs(scratch_class); !fs.done(); fs.next()) { 3125 jshort cur_index = fs.name_index(); 3126 jshort new_index = find_new_index(cur_index); 3127 if (new_index != 0) { 3128 log_trace(redefine, class, constantpool)("field-name_index change: %d to %d", cur_index, new_index); 3129 fs.set_name_index(new_index); 3130 } 3131 cur_index = fs.signature_index(); 3132 new_index = find_new_index(cur_index); 3133 if (new_index != 0) { 3134 log_trace(redefine, class, constantpool)("field-signature_index change: %d to %d", cur_index, new_index); 3135 fs.set_signature_index(new_index); 3136 } 3137 cur_index = fs.initval_index(); 3138 new_index = find_new_index(cur_index); 3139 if (new_index != 0) { 3140 log_trace(redefine, class, constantpool)("field-initval_index change: %d to %d", cur_index, new_index); 3141 fs.set_initval_index(new_index); 3142 } 3143 cur_index = fs.generic_signature_index(); 3144 new_index = find_new_index(cur_index); 3145 if (new_index != 0) { 3146 log_trace(redefine, class, constantpool)("field-generic_signature change: %d to %d", cur_index, new_index); 3147 fs.set_generic_signature_index(new_index); 3148 } 3149 } // end for each field 3150 3151 // Update constant pool indices in the inner classes info to use 3152 // new constant indices as needed. The inner classes info is a 3153 // quadruple: 3154 // (inner_class_info, outer_class_info, inner_name, inner_access_flags) 3155 InnerClassesIterator iter(scratch_class); 3156 for (; !iter.done(); iter.next()) { 3157 int cur_index = iter.inner_class_info_index(); 3158 if (cur_index == 0) { 3159 continue; // JVM spec. allows null inner class refs so skip it 3160 } 3161 int new_index = find_new_index(cur_index); 3162 if (new_index != 0) { 3163 log_trace(redefine, class, constantpool)("inner_class_info change: %d to %d", cur_index, new_index); 3164 iter.set_inner_class_info_index(new_index); 3165 } 3166 cur_index = iter.outer_class_info_index(); 3167 new_index = find_new_index(cur_index); 3168 if (new_index != 0) { 3169 log_trace(redefine, class, constantpool)("outer_class_info change: %d to %d", cur_index, new_index); 3170 iter.set_outer_class_info_index(new_index); 3171 } 3172 cur_index = iter.inner_name_index(); 3173 new_index = find_new_index(cur_index); 3174 if (new_index != 0) { 3175 log_trace(redefine, class, constantpool)("inner_name change: %d to %d", cur_index, new_index); 3176 iter.set_inner_name_index(new_index); 3177 } 3178 } // end for each inner class 3179 3180 // Attach each method in klass to the new constant pool and update 3181 // to use new constant pool indices as needed: 3182 Array<Method*>* methods = scratch_class->methods(); 3183 for (i = methods->length() - 1; i >= 0; i--) { 3184 methodHandle method(THREAD, methods->at(i)); 3185 method->set_constants(scratch_cp()); 3186 3187 int new_index = find_new_index(method->name_index()); 3188 if (new_index != 0) { 3189 log_trace(redefine, class, constantpool) 3190 ("method-name_index change: %d to %d", method->name_index(), new_index); 3191 method->set_name_index(new_index); 3192 } 3193 new_index = find_new_index(method->signature_index()); 3194 if (new_index != 0) { 3195 log_trace(redefine, class, constantpool) 3196 ("method-signature_index change: %d to %d", method->signature_index(), new_index); 3197 method->set_signature_index(new_index); 3198 } 3199 new_index = find_new_index(method->generic_signature_index()); 3200 if (new_index != 0) { 3201 log_trace(redefine, class, constantpool) 3202 ("method-generic_signature_index change: %d to %d", method->generic_signature_index(), new_index); 3203 method->set_generic_signature_index(new_index); 3204 } 3205 3206 // Update constant pool indices in the method's checked exception 3207 // table to use new constant indices as needed. 3208 int cext_length = method->checked_exceptions_length(); 3209 if (cext_length > 0) { 3210 CheckedExceptionElement * cext_table = 3211 method->checked_exceptions_start(); 3212 for (int j = 0; j < cext_length; j++) { 3213 int cur_index = cext_table[j].class_cp_index; 3214 int new_index = find_new_index(cur_index); 3215 if (new_index != 0) { 3216 log_trace(redefine, class, constantpool)("cext-class_cp_index change: %d to %d", cur_index, new_index); 3217 cext_table[j].class_cp_index = (u2)new_index; 3218 } 3219 } // end for each checked exception table entry 3220 } // end if there are checked exception table entries 3221 3222 // Update each catch type index in the method's exception table 3223 // to use new constant pool indices as needed. The exception table 3224 // holds quadruple entries of the form: 3225 // (beg_bci, end_bci, handler_bci, klass_index) 3226 3227 ExceptionTable ex_table(method()); 3228 int ext_length = ex_table.length(); 3229 3230 for (int j = 0; j < ext_length; j ++) { 3231 int cur_index = ex_table.catch_type_index(j); 3232 int new_index = find_new_index(cur_index); 3233 if (new_index != 0) { 3234 log_trace(redefine, class, constantpool)("ext-klass_index change: %d to %d", cur_index, new_index); 3235 ex_table.set_catch_type_index(j, new_index); 3236 } 3237 } // end for each exception table entry 3238 3239 // Update constant pool indices in the method's local variable 3240 // table to use new constant indices as needed. The local variable 3241 // table hold sextuple entries of the form: 3242 // (start_pc, length, name_index, descriptor_index, signature_index, slot) 3243 int lvt_length = method->localvariable_table_length(); 3244 if (lvt_length > 0) { 3245 LocalVariableTableElement * lv_table = 3246 method->localvariable_table_start(); 3247 for (int j = 0; j < lvt_length; j++) { 3248 int cur_index = lv_table[j].name_cp_index; 3249 int new_index = find_new_index(cur_index); 3250 if (new_index != 0) { 3251 log_trace(redefine, class, constantpool)("lvt-name_cp_index change: %d to %d", cur_index, new_index); 3252 lv_table[j].name_cp_index = (u2)new_index; 3253 } 3254 cur_index = lv_table[j].descriptor_cp_index; 3255 new_index = find_new_index(cur_index); 3256 if (new_index != 0) { 3257 log_trace(redefine, class, constantpool)("lvt-descriptor_cp_index change: %d to %d", cur_index, new_index); 3258 lv_table[j].descriptor_cp_index = (u2)new_index; 3259 } 3260 cur_index = lv_table[j].signature_cp_index; 3261 new_index = find_new_index(cur_index); 3262 if (new_index != 0) { 3263 log_trace(redefine, class, constantpool)("lvt-signature_cp_index change: %d to %d", cur_index, new_index); 3264 lv_table[j].signature_cp_index = (u2)new_index; 3265 } 3266 } // end for each local variable table entry 3267 } // end if there are local variable table entries 3268 3269 rewrite_cp_refs_in_stack_map_table(method, THREAD); 3270 } // end for each method 3271 } // end set_new_constant_pool() 3272 3273 3274 // Unevolving classes may point to methods of the_class directly 3275 // from their constant pool caches, itables, and/or vtables. We 3276 // use the ClassLoaderDataGraph::classes_do() facility and this helper 3277 // to fix up these pointers. 3278 3279 // Adjust cpools and vtables closure 3280 void VM_RedefineClasses::AdjustCpoolCacheAndVtable::do_klass(Klass* k) { 3281 3282 // This is a very busy routine. We don't want too much tracing 3283 // printed out. 3284 bool trace_name_printed = false; 3285 InstanceKlass *the_class = InstanceKlass::cast(_the_class); 3286 3287 // If the class being redefined is java.lang.Object, we need to fix all 3288 // array class vtables also 3289 if (k->is_array_klass() && _the_class == SystemDictionary::Object_klass()) { 3290 k->vtable().adjust_method_entries(the_class, &trace_name_printed); 3291 3292 } else if (k->is_instance_klass()) { 3293 HandleMark hm(_thread); 3294 InstanceKlass *ik = InstanceKlass::cast(k); 3295 3296 // HotSpot specific optimization! HotSpot does not currently 3297 // support delegation from the bootstrap class loader to a 3298 // user-defined class loader. This means that if the bootstrap 3299 // class loader is the initiating class loader, then it will also 3300 // be the defining class loader. This also means that classes 3301 // loaded by the bootstrap class loader cannot refer to classes 3302 // loaded by a user-defined class loader. Note: a user-defined 3303 // class loader can delegate to the bootstrap class loader. 3304 // 3305 // If the current class being redefined has a user-defined class 3306 // loader as its defining class loader, then we can skip all 3307 // classes loaded by the bootstrap class loader. 3308 bool is_user_defined = (_the_class->class_loader() != NULL); 3309 if (is_user_defined && ik->class_loader() == NULL) { 3310 return; 3311 } 3312 3313 // Fix the vtable embedded in the_class and subclasses of the_class, 3314 // if one exists. We discard scratch_class and we don't keep an 3315 // InstanceKlass around to hold obsolete methods so we don't have 3316 // any other InstanceKlass embedded vtables to update. The vtable 3317 // holds the Method*s for virtual (but not final) methods. 3318 // Default methods, or concrete methods in interfaces are stored 3319 // in the vtable, so if an interface changes we need to check 3320 // adjust_method_entries() for every InstanceKlass, which will also 3321 // adjust the default method vtable indices. 3322 // We also need to adjust any default method entries that are 3323 // not yet in the vtable, because the vtable setup is in progress. 3324 // This must be done after we adjust the default_methods and 3325 // default_vtable_indices for methods already in the vtable. 3326 // If redefining Unsafe, walk all the vtables looking for entries. 3327 if (ik->vtable_length() > 0 && (_the_class->is_interface() 3328 || _the_class == SystemDictionary::internal_Unsafe_klass() 3329 || ik->is_subtype_of(_the_class))) { 3330 // ik->vtable() creates a wrapper object; rm cleans it up 3331 ResourceMark rm(_thread); 3332 3333 ik->vtable().adjust_method_entries(the_class, &trace_name_printed); 3334 ik->adjust_default_methods(the_class, &trace_name_printed); 3335 } 3336 3337 // If the current class has an itable and we are either redefining an 3338 // interface or if the current class is a subclass of the_class, then 3339 // we potentially have to fix the itable. If we are redefining an 3340 // interface, then we have to call adjust_method_entries() for 3341 // every InstanceKlass that has an itable since there isn't a 3342 // subclass relationship between an interface and an InstanceKlass. 3343 // If redefining Unsafe, walk all the itables looking for entries. 3344 if (ik->itable_length() > 0 && (_the_class->is_interface() 3345 || _the_class == SystemDictionary::internal_Unsafe_klass() 3346 || ik->is_subclass_of(_the_class))) { 3347 ResourceMark rm(_thread); 3348 ik->itable().adjust_method_entries(the_class, &trace_name_printed); 3349 } 3350 3351 // The constant pools in other classes (other_cp) can refer to 3352 // methods in the_class. We have to update method information in 3353 // other_cp's cache. If other_cp has a previous version, then we 3354 // have to repeat the process for each previous version. The 3355 // constant pool cache holds the Method*s for non-virtual 3356 // methods and for virtual, final methods. 3357 // 3358 // Special case: if the current class is the_class, then new_cp 3359 // has already been attached to the_class and old_cp has already 3360 // been added as a previous version. The new_cp doesn't have any 3361 // cached references to old methods so it doesn't need to be 3362 // updated. We can simply start with the previous version(s) in 3363 // that case. 3364 constantPoolHandle other_cp; 3365 ConstantPoolCache* cp_cache; 3366 3367 if (ik != _the_class) { 3368 // this klass' constant pool cache may need adjustment 3369 other_cp = constantPoolHandle(ik->constants()); 3370 cp_cache = other_cp->cache(); 3371 if (cp_cache != NULL) { 3372 cp_cache->adjust_method_entries(the_class, &trace_name_printed); 3373 } 3374 } 3375 3376 // the previous versions' constant pool caches may need adjustment 3377 for (InstanceKlass* pv_node = ik->previous_versions(); 3378 pv_node != NULL; 3379 pv_node = pv_node->previous_versions()) { 3380 cp_cache = pv_node->constants()->cache(); 3381 if (cp_cache != NULL) { 3382 cp_cache->adjust_method_entries(pv_node, &trace_name_printed); 3383 } 3384 } 3385 } 3386 } 3387 3388 // Clean method data for this class 3389 void VM_RedefineClasses::MethodDataCleaner::do_klass(Klass* k) { 3390 if (k->is_instance_klass()) { 3391 InstanceKlass *ik = InstanceKlass::cast(k); 3392 // Clean MethodData of this class's methods so they don't refer to 3393 // old methods that are no longer running. 3394 Array<Method*>* methods = ik->methods(); 3395 int num_methods = methods->length(); 3396 for (int index = 0; index < num_methods; ++index) { 3397 if (methods->at(index)->method_data() != NULL) { 3398 methods->at(index)->method_data()->clean_weak_method_links(); 3399 } 3400 } 3401 } 3402 } 3403 3404 void VM_RedefineClasses::update_jmethod_ids() { 3405 for (int j = 0; j < _matching_methods_length; ++j) { 3406 Method* old_method = _matching_old_methods[j]; 3407 jmethodID jmid = old_method->find_jmethod_id_or_null(); 3408 if (jmid != NULL) { 3409 // There is a jmethodID, change it to point to the new method 3410 methodHandle new_method_h(_matching_new_methods[j]); 3411 Method::change_method_associated_with_jmethod_id(jmid, new_method_h()); 3412 assert(Method::resolve_jmethod_id(jmid) == _matching_new_methods[j], 3413 "should be replaced"); 3414 } 3415 } 3416 } 3417 3418 int VM_RedefineClasses::check_methods_and_mark_as_obsolete() { 3419 int emcp_method_count = 0; 3420 int obsolete_count = 0; 3421 int old_index = 0; 3422 for (int j = 0; j < _matching_methods_length; ++j, ++old_index) { 3423 Method* old_method = _matching_old_methods[j]; 3424 Method* new_method = _matching_new_methods[j]; 3425 Method* old_array_method; 3426 3427 // Maintain an old_index into the _old_methods array by skipping 3428 // deleted methods 3429 while ((old_array_method = _old_methods->at(old_index)) != old_method) { 3430 ++old_index; 3431 } 3432 3433 if (MethodComparator::methods_EMCP(old_method, new_method)) { 3434 // The EMCP definition from JSR-163 requires the bytecodes to be 3435 // the same with the exception of constant pool indices which may 3436 // differ. However, the constants referred to by those indices 3437 // must be the same. 3438 // 3439 // We use methods_EMCP() for comparison since constant pool 3440 // merging can remove duplicate constant pool entries that were 3441 // present in the old method and removed from the rewritten new 3442 // method. A faster binary comparison function would consider the 3443 // old and new methods to be different when they are actually 3444 // EMCP. 3445 // 3446 // The old and new methods are EMCP and you would think that we 3447 // could get rid of one of them here and now and save some space. 3448 // However, the concept of EMCP only considers the bytecodes and 3449 // the constant pool entries in the comparison. Other things, 3450 // e.g., the line number table (LNT) or the local variable table 3451 // (LVT) don't count in the comparison. So the new (and EMCP) 3452 // method can have a new LNT that we need so we can't just 3453 // overwrite the new method with the old method. 3454 // 3455 // When this routine is called, we have already attached the new 3456 // methods to the_class so the old methods are effectively 3457 // overwritten. However, if an old method is still executing, 3458 // then the old method cannot be collected until sometime after 3459 // the old method call has returned. So the overwriting of old 3460 // methods by new methods will save us space except for those 3461 // (hopefully few) old methods that are still executing. 3462 // 3463 // A method refers to a ConstMethod* and this presents another 3464 // possible avenue to space savings. The ConstMethod* in the 3465 // new method contains possibly new attributes (LNT, LVT, etc). 3466 // At first glance, it seems possible to save space by replacing 3467 // the ConstMethod* in the old method with the ConstMethod* 3468 // from the new method. The old and new methods would share the 3469 // same ConstMethod* and we would save the space occupied by 3470 // the old ConstMethod*. However, the ConstMethod* contains 3471 // a back reference to the containing method. Sharing the 3472 // ConstMethod* between two methods could lead to confusion in 3473 // the code that uses the back reference. This would lead to 3474 // brittle code that could be broken in non-obvious ways now or 3475 // in the future. 3476 // 3477 // Another possibility is to copy the ConstMethod* from the new 3478 // method to the old method and then overwrite the new method with 3479 // the old method. Since the ConstMethod* contains the bytecodes 3480 // for the method embedded in the oop, this option would change 3481 // the bytecodes out from under any threads executing the old 3482 // method and make the thread's bcp invalid. Since EMCP requires 3483 // that the bytecodes be the same modulo constant pool indices, it 3484 // is straight forward to compute the correct new bcp in the new 3485 // ConstMethod* from the old bcp in the old ConstMethod*. The 3486 // time consuming part would be searching all the frames in all 3487 // of the threads to find all of the calls to the old method. 3488 // 3489 // It looks like we will have to live with the limited savings 3490 // that we get from effectively overwriting the old methods 3491 // when the new methods are attached to the_class. 3492 3493 // Count number of methods that are EMCP. The method will be marked 3494 // old but not obsolete if it is EMCP. 3495 emcp_method_count++; 3496 3497 // An EMCP method is _not_ obsolete. An obsolete method has a 3498 // different jmethodID than the current method. An EMCP method 3499 // has the same jmethodID as the current method. Having the 3500 // same jmethodID for all EMCP versions of a method allows for 3501 // a consistent view of the EMCP methods regardless of which 3502 // EMCP method you happen to have in hand. For example, a 3503 // breakpoint set in one EMCP method will work for all EMCP 3504 // versions of the method including the current one. 3505 } else { 3506 // mark obsolete methods as such 3507 old_method->set_is_obsolete(); 3508 obsolete_count++; 3509 3510 // obsolete methods need a unique idnum so they become new entries in 3511 // the jmethodID cache in InstanceKlass 3512 assert(old_method->method_idnum() == new_method->method_idnum(), "must match"); 3513 u2 num = InstanceKlass::cast(_the_class)->next_method_idnum(); 3514 if (num != ConstMethod::UNSET_IDNUM) { 3515 old_method->set_method_idnum(num); 3516 } 3517 3518 // With tracing we try not to "yack" too much. The position of 3519 // this trace assumes there are fewer obsolete methods than 3520 // EMCP methods. 3521 if (log_is_enabled(Trace, redefine, class, obsolete, mark)) { 3522 ResourceMark rm; 3523 log_trace(redefine, class, obsolete, mark) 3524 ("mark %s(%s) as obsolete", old_method->name()->as_C_string(), old_method->signature()->as_C_string()); 3525 } 3526 } 3527 old_method->set_is_old(); 3528 } 3529 for (int i = 0; i < _deleted_methods_length; ++i) { 3530 Method* old_method = _deleted_methods[i]; 3531 3532 assert(!old_method->has_vtable_index(), 3533 "cannot delete methods with vtable entries");; 3534 3535 // Mark all deleted methods as old, obsolete and deleted 3536 old_method->set_is_deleted(); 3537 old_method->set_is_old(); 3538 old_method->set_is_obsolete(); 3539 ++obsolete_count; 3540 // With tracing we try not to "yack" too much. The position of 3541 // this trace assumes there are fewer obsolete methods than 3542 // EMCP methods. 3543 if (log_is_enabled(Trace, redefine, class, obsolete, mark)) { 3544 ResourceMark rm; 3545 log_trace(redefine, class, obsolete, mark) 3546 ("mark deleted %s(%s) as obsolete", old_method->name()->as_C_string(), old_method->signature()->as_C_string()); 3547 } 3548 } 3549 assert((emcp_method_count + obsolete_count) == _old_methods->length(), 3550 "sanity check"); 3551 log_trace(redefine, class, obsolete, mark)("EMCP_cnt=%d, obsolete_cnt=%d", emcp_method_count, obsolete_count); 3552 return emcp_method_count; 3553 } 3554 3555 // This internal class transfers the native function registration from old methods 3556 // to new methods. It is designed to handle both the simple case of unchanged 3557 // native methods and the complex cases of native method prefixes being added and/or 3558 // removed. 3559 // It expects only to be used during the VM_RedefineClasses op (a safepoint). 3560 // 3561 // This class is used after the new methods have been installed in "the_class". 3562 // 3563 // So, for example, the following must be handled. Where 'm' is a method and 3564 // a number followed by an underscore is a prefix. 3565 // 3566 // Old Name New Name 3567 // Simple transfer to new method m -> m 3568 // Add prefix m -> 1_m 3569 // Remove prefix 1_m -> m 3570 // Simultaneous add of prefixes m -> 3_2_1_m 3571 // Simultaneous removal of prefixes 3_2_1_m -> m 3572 // Simultaneous add and remove 1_m -> 2_m 3573 // Same, caused by prefix removal only 3_2_1_m -> 3_2_m 3574 // 3575 class TransferNativeFunctionRegistration { 3576 private: 3577 InstanceKlass* the_class; 3578 int prefix_count; 3579 char** prefixes; 3580 3581 // Recursively search the binary tree of possibly prefixed method names. 3582 // Iteration could be used if all agents were well behaved. Full tree walk is 3583 // more resilent to agents not cleaning up intermediate methods. 3584 // Branch at each depth in the binary tree is: 3585 // (1) without the prefix. 3586 // (2) with the prefix. 3587 // where 'prefix' is the prefix at that 'depth' (first prefix, second prefix,...) 3588 Method* search_prefix_name_space(int depth, char* name_str, size_t name_len, 3589 Symbol* signature) { 3590 TempNewSymbol name_symbol = SymbolTable::probe(name_str, (int)name_len); 3591 if (name_symbol != NULL) { 3592 Method* method = the_class->lookup_method(name_symbol, signature); 3593 if (method != NULL) { 3594 // Even if prefixed, intermediate methods must exist. 3595 if (method->is_native()) { 3596 // Wahoo, we found a (possibly prefixed) version of the method, return it. 3597 return method; 3598 } 3599 if (depth < prefix_count) { 3600 // Try applying further prefixes (other than this one). 3601 method = search_prefix_name_space(depth+1, name_str, name_len, signature); 3602 if (method != NULL) { 3603 return method; // found 3604 } 3605 3606 // Try adding this prefix to the method name and see if it matches 3607 // another method name. 3608 char* prefix = prefixes[depth]; 3609 size_t prefix_len = strlen(prefix); 3610 size_t trial_len = name_len + prefix_len; 3611 char* trial_name_str = NEW_RESOURCE_ARRAY(char, trial_len + 1); 3612 strcpy(trial_name_str, prefix); 3613 strcat(trial_name_str, name_str); 3614 method = search_prefix_name_space(depth+1, trial_name_str, trial_len, 3615 signature); 3616 if (method != NULL) { 3617 // If found along this branch, it was prefixed, mark as such 3618 method->set_is_prefixed_native(); 3619 return method; // found 3620 } 3621 } 3622 } 3623 } 3624 return NULL; // This whole branch bore nothing 3625 } 3626 3627 // Return the method name with old prefixes stripped away. 3628 char* method_name_without_prefixes(Method* method) { 3629 Symbol* name = method->name(); 3630 char* name_str = name->as_utf8(); 3631 3632 // Old prefixing may be defunct, strip prefixes, if any. 3633 for (int i = prefix_count-1; i >= 0; i--) { 3634 char* prefix = prefixes[i]; 3635 size_t prefix_len = strlen(prefix); 3636 if (strncmp(prefix, name_str, prefix_len) == 0) { 3637 name_str += prefix_len; 3638 } 3639 } 3640 return name_str; 3641 } 3642 3643 // Strip any prefixes off the old native method, then try to find a 3644 // (possibly prefixed) new native that matches it. 3645 Method* strip_and_search_for_new_native(Method* method) { 3646 ResourceMark rm; 3647 char* name_str = method_name_without_prefixes(method); 3648 return search_prefix_name_space(0, name_str, strlen(name_str), 3649 method->signature()); 3650 } 3651 3652 public: 3653 3654 // Construct a native method transfer processor for this class. 3655 TransferNativeFunctionRegistration(InstanceKlass* _the_class) { 3656 assert(SafepointSynchronize::is_at_safepoint(), "sanity check"); 3657 3658 the_class = _the_class; 3659 prefixes = JvmtiExport::get_all_native_method_prefixes(&prefix_count); 3660 } 3661 3662 // Attempt to transfer any of the old or deleted methods that are native 3663 void transfer_registrations(Method** old_methods, int methods_length) { 3664 for (int j = 0; j < methods_length; j++) { 3665 Method* old_method = old_methods[j]; 3666 3667 if (old_method->is_native() && old_method->has_native_function()) { 3668 Method* new_method = strip_and_search_for_new_native(old_method); 3669 if (new_method != NULL) { 3670 // Actually set the native function in the new method. 3671 // Redefine does not send events (except CFLH), certainly not this 3672 // behind the scenes re-registration. 3673 new_method->set_native_function(old_method->native_function(), 3674 !Method::native_bind_event_is_interesting); 3675 } 3676 } 3677 } 3678 } 3679 }; 3680 3681 // Don't lose the association between a native method and its JNI function. 3682 void VM_RedefineClasses::transfer_old_native_function_registrations(InstanceKlass* the_class) { 3683 TransferNativeFunctionRegistration transfer(the_class); 3684 transfer.transfer_registrations(_deleted_methods, _deleted_methods_length); 3685 transfer.transfer_registrations(_matching_old_methods, _matching_methods_length); 3686 } 3687 3688 // Deoptimize all compiled code that depends on this class. 3689 // 3690 // If the can_redefine_classes capability is obtained in the onload 3691 // phase then the compiler has recorded all dependencies from startup. 3692 // In that case we need only deoptimize and throw away all compiled code 3693 // that depends on the class. 3694 // 3695 // If can_redefine_classes is obtained sometime after the onload 3696 // phase then the dependency information may be incomplete. In that case 3697 // the first call to RedefineClasses causes all compiled code to be 3698 // thrown away. As can_redefine_classes has been obtained then 3699 // all future compilations will record dependencies so second and 3700 // subsequent calls to RedefineClasses need only throw away code 3701 // that depends on the class. 3702 // 3703 void VM_RedefineClasses::flush_dependent_code(InstanceKlass* ik, TRAPS) { 3704 assert_locked_or_safepoint(Compile_lock); 3705 3706 // All dependencies have been recorded from startup or this is a second or 3707 // subsequent use of RedefineClasses 3708 if (JvmtiExport::all_dependencies_are_recorded()) { 3709 CodeCache::flush_evol_dependents_on(ik); 3710 } else { 3711 CodeCache::mark_all_nmethods_for_deoptimization(); 3712 3713 ResourceMark rm(THREAD); 3714 DeoptimizationMarker dm; 3715 3716 // Deoptimize all activations depending on marked nmethods 3717 Deoptimization::deoptimize_dependents(); 3718 3719 // Make the dependent methods not entrant 3720 CodeCache::make_marked_nmethods_not_entrant(); 3721 3722 // From now on we know that the dependency information is complete 3723 JvmtiExport::set_all_dependencies_are_recorded(true); 3724 } 3725 } 3726 3727 void VM_RedefineClasses::compute_added_deleted_matching_methods() { 3728 Method* old_method; 3729 Method* new_method; 3730 3731 _matching_old_methods = NEW_RESOURCE_ARRAY(Method*, _old_methods->length()); 3732 _matching_new_methods = NEW_RESOURCE_ARRAY(Method*, _old_methods->length()); 3733 _added_methods = NEW_RESOURCE_ARRAY(Method*, _new_methods->length()); 3734 _deleted_methods = NEW_RESOURCE_ARRAY(Method*, _old_methods->length()); 3735 3736 _matching_methods_length = 0; 3737 _deleted_methods_length = 0; 3738 _added_methods_length = 0; 3739 3740 int nj = 0; 3741 int oj = 0; 3742 while (true) { 3743 if (oj >= _old_methods->length()) { 3744 if (nj >= _new_methods->length()) { 3745 break; // we've looked at everything, done 3746 } 3747 // New method at the end 3748 new_method = _new_methods->at(nj); 3749 _added_methods[_added_methods_length++] = new_method; 3750 ++nj; 3751 } else if (nj >= _new_methods->length()) { 3752 // Old method, at the end, is deleted 3753 old_method = _old_methods->at(oj); 3754 _deleted_methods[_deleted_methods_length++] = old_method; 3755 ++oj; 3756 } else { 3757 old_method = _old_methods->at(oj); 3758 new_method = _new_methods->at(nj); 3759 if (old_method->name() == new_method->name()) { 3760 if (old_method->signature() == new_method->signature()) { 3761 _matching_old_methods[_matching_methods_length ] = old_method; 3762 _matching_new_methods[_matching_methods_length++] = new_method; 3763 ++nj; 3764 ++oj; 3765 } else { 3766 // added overloaded have already been moved to the end, 3767 // so this is a deleted overloaded method 3768 _deleted_methods[_deleted_methods_length++] = old_method; 3769 ++oj; 3770 } 3771 } else { // names don't match 3772 if (old_method->name()->fast_compare(new_method->name()) > 0) { 3773 // new method 3774 _added_methods[_added_methods_length++] = new_method; 3775 ++nj; 3776 } else { 3777 // deleted method 3778 _deleted_methods[_deleted_methods_length++] = old_method; 3779 ++oj; 3780 } 3781 } 3782 } 3783 } 3784 assert(_matching_methods_length + _deleted_methods_length == _old_methods->length(), "sanity"); 3785 assert(_matching_methods_length + _added_methods_length == _new_methods->length(), "sanity"); 3786 } 3787 3788 3789 void VM_RedefineClasses::swap_annotations(InstanceKlass* the_class, 3790 InstanceKlass* scratch_class) { 3791 // Swap annotation fields values 3792 Annotations* old_annotations = the_class->annotations(); 3793 the_class->set_annotations(scratch_class->annotations()); 3794 scratch_class->set_annotations(old_annotations); 3795 } 3796 3797 3798 // Install the redefinition of a class: 3799 // - house keeping (flushing breakpoints and caches, deoptimizing 3800 // dependent compiled code) 3801 // - replacing parts in the_class with parts from scratch_class 3802 // - adding a weak reference to track the obsolete but interesting 3803 // parts of the_class 3804 // - adjusting constant pool caches and vtables in other classes 3805 // that refer to methods in the_class. These adjustments use the 3806 // ClassLoaderDataGraph::classes_do() facility which only allows 3807 // a helper method to be specified. The interesting parameters 3808 // that we would like to pass to the helper method are saved in 3809 // static global fields in the VM operation. 3810 void VM_RedefineClasses::redefine_single_class(jclass the_jclass, 3811 InstanceKlass* scratch_class, TRAPS) { 3812 3813 HandleMark hm(THREAD); // make sure handles from this call are freed 3814 3815 if (log_is_enabled(Info, redefine, class, timer)) { 3816 _timer_rsc_phase1.start(); 3817 } 3818 3819 InstanceKlass* the_class = get_ik(the_jclass); 3820 3821 // Remove all breakpoints in methods of this class 3822 JvmtiBreakpoints& jvmti_breakpoints = JvmtiCurrentBreakpoints::get_jvmti_breakpoints(); 3823 jvmti_breakpoints.clearall_in_class_at_safepoint(the_class); 3824 3825 // Deoptimize all compiled code that depends on this class 3826 flush_dependent_code(the_class, THREAD); 3827 3828 _old_methods = the_class->methods(); 3829 _new_methods = scratch_class->methods(); 3830 _the_class = the_class; 3831 compute_added_deleted_matching_methods(); 3832 update_jmethod_ids(); 3833 3834 _any_class_has_resolved_methods = the_class->has_resolved_methods() || _any_class_has_resolved_methods; 3835 3836 // Attach new constant pool to the original klass. The original 3837 // klass still refers to the old constant pool (for now). 3838 scratch_class->constants()->set_pool_holder(the_class); 3839 3840 #if 0 3841 // In theory, with constant pool merging in place we should be able 3842 // to save space by using the new, merged constant pool in place of 3843 // the old constant pool(s). By "pool(s)" I mean the constant pool in 3844 // the klass version we are replacing now and any constant pool(s) in 3845 // previous versions of klass. Nice theory, doesn't work in practice. 3846 // When this code is enabled, even simple programs throw NullPointer 3847 // exceptions. I'm guessing that this is caused by some constant pool 3848 // cache difference between the new, merged constant pool and the 3849 // constant pool that was just being used by the klass. I'm keeping 3850 // this code around to archive the idea, but the code has to remain 3851 // disabled for now. 3852 3853 // Attach each old method to the new constant pool. This can be 3854 // done here since we are past the bytecode verification and 3855 // constant pool optimization phases. 3856 for (int i = _old_methods->length() - 1; i >= 0; i--) { 3857 Method* method = _old_methods->at(i); 3858 method->set_constants(scratch_class->constants()); 3859 } 3860 3861 // NOTE: this doesn't work because you can redefine the same class in two 3862 // threads, each getting their own constant pool data appended to the 3863 // original constant pool. In order for the new methods to work when they 3864 // become old methods, they need to keep their updated copy of the constant pool. 3865 3866 { 3867 // walk all previous versions of the klass 3868 InstanceKlass *ik = the_class; 3869 PreviousVersionWalker pvw(ik); 3870 do { 3871 ik = pvw.next_previous_version(); 3872 if (ik != NULL) { 3873 3874 // attach previous version of klass to the new constant pool 3875 ik->set_constants(scratch_class->constants()); 3876 3877 // Attach each method in the previous version of klass to the 3878 // new constant pool 3879 Array<Method*>* prev_methods = ik->methods(); 3880 for (int i = prev_methods->length() - 1; i >= 0; i--) { 3881 Method* method = prev_methods->at(i); 3882 method->set_constants(scratch_class->constants()); 3883 } 3884 } 3885 } while (ik != NULL); 3886 } 3887 #endif 3888 3889 // Replace methods and constantpool 3890 the_class->set_methods(_new_methods); 3891 scratch_class->set_methods(_old_methods); // To prevent potential GCing of the old methods, 3892 // and to be able to undo operation easily. 3893 3894 Array<int>* old_ordering = the_class->method_ordering(); 3895 the_class->set_method_ordering(scratch_class->method_ordering()); 3896 scratch_class->set_method_ordering(old_ordering); 3897 3898 ConstantPool* old_constants = the_class->constants(); 3899 the_class->set_constants(scratch_class->constants()); 3900 scratch_class->set_constants(old_constants); // See the previous comment. 3901 #if 0 3902 // We are swapping the guts of "the new class" with the guts of "the 3903 // class". Since the old constant pool has just been attached to "the 3904 // new class", it seems logical to set the pool holder in the old 3905 // constant pool also. However, doing this will change the observable 3906 // class hierarchy for any old methods that are still executing. A 3907 // method can query the identity of its "holder" and this query uses 3908 // the method's constant pool link to find the holder. The change in 3909 // holding class from "the class" to "the new class" can confuse 3910 // things. 3911 // 3912 // Setting the old constant pool's holder will also cause 3913 // verification done during vtable initialization below to fail. 3914 // During vtable initialization, the vtable's class is verified to be 3915 // a subtype of the method's holder. The vtable's class is "the 3916 // class" and the method's holder is gotten from the constant pool 3917 // link in the method itself. For "the class"'s directly implemented 3918 // methods, the method holder is "the class" itself (as gotten from 3919 // the new constant pool). The check works fine in this case. The 3920 // check also works fine for methods inherited from super classes. 3921 // 3922 // Miranda methods are a little more complicated. A miranda method is 3923 // provided by an interface when the class implementing the interface 3924 // does not provide its own method. These interfaces are implemented 3925 // internally as an InstanceKlass. These special instanceKlasses 3926 // share the constant pool of the class that "implements" the 3927 // interface. By sharing the constant pool, the method holder of a 3928 // miranda method is the class that "implements" the interface. In a 3929 // non-redefine situation, the subtype check works fine. However, if 3930 // the old constant pool's pool holder is modified, then the check 3931 // fails because there is no class hierarchy relationship between the 3932 // vtable's class and "the new class". 3933 3934 old_constants->set_pool_holder(scratch_class()); 3935 #endif 3936 3937 // track number of methods that are EMCP for add_previous_version() call below 3938 int emcp_method_count = check_methods_and_mark_as_obsolete(); 3939 transfer_old_native_function_registrations(the_class); 3940 3941 // The class file bytes from before any retransformable agents mucked 3942 // with them was cached on the scratch class, move to the_class. 3943 // Note: we still want to do this if nothing needed caching since it 3944 // should get cleared in the_class too. 3945 if (the_class->get_cached_class_file_bytes() == 0) { 3946 // the_class doesn't have a cache yet so copy it 3947 the_class->set_cached_class_file(scratch_class->get_cached_class_file()); 3948 } 3949 else if (scratch_class->get_cached_class_file_bytes() != 3950 the_class->get_cached_class_file_bytes()) { 3951 // The same class can be present twice in the scratch classes list or there 3952 // are multiple concurrent RetransformClasses calls on different threads. 3953 // In such cases we have to deallocate scratch_class cached_class_file. 3954 os::free(scratch_class->get_cached_class_file()); 3955 } 3956 3957 // NULL out in scratch class to not delete twice. The class to be redefined 3958 // always owns these bytes. 3959 scratch_class->set_cached_class_file(NULL); 3960 3961 // Replace inner_classes 3962 Array<u2>* old_inner_classes = the_class->inner_classes(); 3963 the_class->set_inner_classes(scratch_class->inner_classes()); 3964 scratch_class->set_inner_classes(old_inner_classes); 3965 3966 // Initialize the vtable and interface table after 3967 // methods have been rewritten 3968 { 3969 ResourceMark rm(THREAD); 3970 // no exception should happen here since we explicitly 3971 // do not check loader constraints. 3972 // compare_and_normalize_class_versions has already checked: 3973 // - classloaders unchanged, signatures unchanged 3974 // - all instanceKlasses for redefined classes reused & contents updated 3975 the_class->vtable().initialize_vtable(false, THREAD); 3976 the_class->itable().initialize_itable(false, THREAD); 3977 assert(!HAS_PENDING_EXCEPTION || (THREAD->pending_exception()->is_a(SystemDictionary::ThreadDeath_klass())), "redefine exception"); 3978 } 3979 3980 // Leave arrays of jmethodIDs and itable index cache unchanged 3981 3982 // Copy the "source file name" attribute from new class version 3983 the_class->set_source_file_name_index( 3984 scratch_class->source_file_name_index()); 3985 3986 // Copy the "source debug extension" attribute from new class version 3987 the_class->set_source_debug_extension( 3988 scratch_class->source_debug_extension(), 3989 scratch_class->source_debug_extension() == NULL ? 0 : 3990 (int)strlen(scratch_class->source_debug_extension())); 3991 3992 // Use of javac -g could be different in the old and the new 3993 if (scratch_class->access_flags().has_localvariable_table() != 3994 the_class->access_flags().has_localvariable_table()) { 3995 3996 AccessFlags flags = the_class->access_flags(); 3997 if (scratch_class->access_flags().has_localvariable_table()) { 3998 flags.set_has_localvariable_table(); 3999 } else { 4000 flags.clear_has_localvariable_table(); 4001 } 4002 the_class->set_access_flags(flags); 4003 } 4004 4005 swap_annotations(the_class, scratch_class); 4006 4007 // Replace minor version number of class file 4008 u2 old_minor_version = the_class->minor_version(); 4009 the_class->set_minor_version(scratch_class->minor_version()); 4010 scratch_class->set_minor_version(old_minor_version); 4011 4012 // Replace major version number of class file 4013 u2 old_major_version = the_class->major_version(); 4014 the_class->set_major_version(scratch_class->major_version()); 4015 scratch_class->set_major_version(old_major_version); 4016 4017 // Replace CP indexes for class and name+type of enclosing method 4018 u2 old_class_idx = the_class->enclosing_method_class_index(); 4019 u2 old_method_idx = the_class->enclosing_method_method_index(); 4020 the_class->set_enclosing_method_indices( 4021 scratch_class->enclosing_method_class_index(), 4022 scratch_class->enclosing_method_method_index()); 4023 scratch_class->set_enclosing_method_indices(old_class_idx, old_method_idx); 4024 4025 // Replace fingerprint data 4026 the_class->set_has_passed_fingerprint_check(scratch_class->has_passed_fingerprint_check()); 4027 the_class->store_fingerprint(scratch_class->get_stored_fingerprint()); 4028 4029 the_class->set_has_been_redefined(); 4030 4031 if (!the_class->should_be_initialized()) { 4032 // Class was already initialized, so AOT has only seen the original version. 4033 // We need to let AOT look at it again. 4034 AOTLoader::load_for_klass(the_class, THREAD); 4035 } 4036 4037 // keep track of previous versions of this class 4038 the_class->add_previous_version(scratch_class, emcp_method_count); 4039 4040 _timer_rsc_phase1.stop(); 4041 if (log_is_enabled(Info, redefine, class, timer)) { 4042 _timer_rsc_phase2.start(); 4043 } 4044 4045 // Adjust constantpool caches and vtables for all classes 4046 // that reference methods of the evolved class. 4047 AdjustCpoolCacheAndVtable adjust_cpool_cache_and_vtable(THREAD); 4048 ClassLoaderDataGraph::classes_do(&adjust_cpool_cache_and_vtable); 4049 4050 if (the_class->oop_map_cache() != NULL) { 4051 // Flush references to any obsolete methods from the oop map cache 4052 // so that obsolete methods are not pinned. 4053 the_class->oop_map_cache()->flush_obsolete_entries(); 4054 } 4055 4056 increment_class_counter((InstanceKlass *)the_class, THREAD); 4057 { 4058 ResourceMark rm(THREAD); 4059 // increment the classRedefinedCount field in the_class and in any 4060 // direct and indirect subclasses of the_class 4061 log_info(redefine, class, load) 4062 ("redefined name=%s, count=%d (avail_mem=" UINT64_FORMAT "K)", 4063 the_class->external_name(), java_lang_Class::classRedefinedCount(the_class->java_mirror()), os::available_memory() >> 10); 4064 Events::log_redefinition(THREAD, "redefined class name=%s, count=%d", 4065 the_class->external_name(), 4066 java_lang_Class::classRedefinedCount(the_class->java_mirror())); 4067 4068 } 4069 _timer_rsc_phase2.stop(); 4070 } // end redefine_single_class() 4071 4072 4073 // Increment the classRedefinedCount field in the specific InstanceKlass 4074 // and in all direct and indirect subclasses. 4075 void VM_RedefineClasses::increment_class_counter(InstanceKlass *ik, TRAPS) { 4076 oop class_mirror = ik->java_mirror(); 4077 Klass* class_oop = java_lang_Class::as_Klass(class_mirror); 4078 int new_count = java_lang_Class::classRedefinedCount(class_mirror) + 1; 4079 java_lang_Class::set_classRedefinedCount(class_mirror, new_count); 4080 4081 if (class_oop != _the_class) { 4082 // _the_class count is printed at end of redefine_single_class() 4083 log_debug(redefine, class, subclass)("updated count in subclass=%s to %d", ik->external_name(), new_count); 4084 } 4085 4086 for (Klass *subk = ik->subklass(); subk != NULL; 4087 subk = subk->next_sibling()) { 4088 if (subk->is_instance_klass()) { 4089 // Only update instanceKlasses 4090 InstanceKlass *subik = InstanceKlass::cast(subk); 4091 // recursively do subclasses of the current subclass 4092 increment_class_counter(subik, THREAD); 4093 } 4094 } 4095 } 4096 4097 void VM_RedefineClasses::CheckClass::do_klass(Klass* k) { 4098 bool no_old_methods = true; // be optimistic 4099 4100 // Both array and instance classes have vtables. 4101 // a vtable should never contain old or obsolete methods 4102 ResourceMark rm(_thread); 4103 if (k->vtable_length() > 0 && 4104 !k->vtable().check_no_old_or_obsolete_entries()) { 4105 if (log_is_enabled(Trace, redefine, class, obsolete, metadata)) { 4106 log_trace(redefine, class, obsolete, metadata) 4107 ("klassVtable::check_no_old_or_obsolete_entries failure -- OLD or OBSOLETE method found -- class: %s", 4108 k->signature_name()); 4109 k->vtable().dump_vtable(); 4110 } 4111 no_old_methods = false; 4112 } 4113 4114 if (k->is_instance_klass()) { 4115 HandleMark hm(_thread); 4116 InstanceKlass *ik = InstanceKlass::cast(k); 4117 4118 // an itable should never contain old or obsolete methods 4119 if (ik->itable_length() > 0 && 4120 !ik->itable().check_no_old_or_obsolete_entries()) { 4121 if (log_is_enabled(Trace, redefine, class, obsolete, metadata)) { 4122 log_trace(redefine, class, obsolete, metadata) 4123 ("klassItable::check_no_old_or_obsolete_entries failure -- OLD or OBSOLETE method found -- class: %s", 4124 ik->signature_name()); 4125 ik->itable().dump_itable(); 4126 } 4127 no_old_methods = false; 4128 } 4129 4130 // the constant pool cache should never contain non-deleted old or obsolete methods 4131 if (ik->constants() != NULL && 4132 ik->constants()->cache() != NULL && 4133 !ik->constants()->cache()->check_no_old_or_obsolete_entries()) { 4134 if (log_is_enabled(Trace, redefine, class, obsolete, metadata)) { 4135 log_trace(redefine, class, obsolete, metadata) 4136 ("cp-cache::check_no_old_or_obsolete_entries failure -- OLD or OBSOLETE method found -- class: %s", 4137 ik->signature_name()); 4138 ik->constants()->cache()->dump_cache(); 4139 } 4140 no_old_methods = false; 4141 } 4142 } 4143 4144 // print and fail guarantee if old methods are found. 4145 if (!no_old_methods) { 4146 if (log_is_enabled(Trace, redefine, class, obsolete, metadata)) { 4147 dump_methods(); 4148 } else { 4149 log_trace(redefine, class)("Use the '-Xlog:redefine+class*:' option " 4150 "to see more info about the following guarantee() failure."); 4151 } 4152 guarantee(false, "OLD and/or OBSOLETE method(s) found"); 4153 } 4154 } 4155 4156 4157 void VM_RedefineClasses::dump_methods() { 4158 int j; 4159 log_trace(redefine, class, dump)("_old_methods --"); 4160 for (j = 0; j < _old_methods->length(); ++j) { 4161 LogStreamHandle(Trace, redefine, class, dump) log_stream; 4162 Method* m = _old_methods->at(j); 4163 log_stream.print("%4d (%5d) ", j, m->vtable_index()); 4164 m->access_flags().print_on(&log_stream); 4165 log_stream.print(" -- "); 4166 m->print_name(&log_stream); 4167 log_stream.cr(); 4168 } 4169 log_trace(redefine, class, dump)("_new_methods --"); 4170 for (j = 0; j < _new_methods->length(); ++j) { 4171 LogStreamHandle(Trace, redefine, class, dump) log_stream; 4172 Method* m = _new_methods->at(j); 4173 log_stream.print("%4d (%5d) ", j, m->vtable_index()); 4174 m->access_flags().print_on(&log_stream); 4175 log_stream.print(" -- "); 4176 m->print_name(&log_stream); 4177 log_stream.cr(); 4178 } 4179 log_trace(redefine, class, dump)("_matching_methods --"); 4180 for (j = 0; j < _matching_methods_length; ++j) { 4181 LogStreamHandle(Trace, redefine, class, dump) log_stream; 4182 Method* m = _matching_old_methods[j]; 4183 log_stream.print("%4d (%5d) ", j, m->vtable_index()); 4184 m->access_flags().print_on(&log_stream); 4185 log_stream.print(" -- "); 4186 m->print_name(); 4187 log_stream.cr(); 4188 4189 m = _matching_new_methods[j]; 4190 log_stream.print(" (%5d) ", m->vtable_index()); 4191 m->access_flags().print_on(&log_stream); 4192 log_stream.cr(); 4193 } 4194 log_trace(redefine, class, dump)("_deleted_methods --"); 4195 for (j = 0; j < _deleted_methods_length; ++j) { 4196 LogStreamHandle(Trace, redefine, class, dump) log_stream; 4197 Method* m = _deleted_methods[j]; 4198 log_stream.print("%4d (%5d) ", j, m->vtable_index()); 4199 m->access_flags().print_on(&log_stream); 4200 log_stream.print(" -- "); 4201 m->print_name(&log_stream); 4202 log_stream.cr(); 4203 } 4204 log_trace(redefine, class, dump)("_added_methods --"); 4205 for (j = 0; j < _added_methods_length; ++j) { 4206 LogStreamHandle(Trace, redefine, class, dump) log_stream; 4207 Method* m = _added_methods[j]; 4208 log_stream.print("%4d (%5d) ", j, m->vtable_index()); 4209 m->access_flags().print_on(&log_stream); 4210 log_stream.print(" -- "); 4211 m->print_name(&log_stream); 4212 log_stream.cr(); 4213 } 4214 } 4215 4216 void VM_RedefineClasses::print_on_error(outputStream* st) const { 4217 VM_Operation::print_on_error(st); 4218 if (_the_class != NULL) { 4219 ResourceMark rm; 4220 st->print_cr(", redefining class %s", _the_class->external_name()); 4221 } 4222 }