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