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