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