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