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