src/share/vm/prims/jvmtiRedefineClasses.cpp

Print this page




 398       int ref_i = scratch_cp->method_handle_index_at(scratch_i);
 399       int new_ref_i = find_or_append_indirect_entry(scratch_cp, ref_i, merge_cp_p,
 400                                                     merge_cp_length_p, THREAD);
 401       if (new_ref_i != ref_i) {
 402         RC_TRACE(0x00080000,
 403                  ("MethodHandle entry@%d ref_index change: %d to %d",
 404                   *merge_cp_length_p, ref_i, new_ref_i));
 405       }
 406       (*merge_cp_p)->method_handle_index_at_put(*merge_cp_length_p, ref_kind, new_ref_i);
 407       if (scratch_i != *merge_cp_length_p) {
 408         // The new entry in *merge_cp_p is at a different index than
 409         // the new entry in scratch_cp so we need to map the index values.
 410         map_index(scratch_cp, scratch_i, *merge_cp_length_p);
 411       }
 412       (*merge_cp_length_p)++;
 413     } break;
 414 
 415     // this is an indirect CP entry so it needs special handling
 416     case JVM_CONSTANT_InvokeDynamic:
 417     {
 418       // TBD: cross-checks and possible extra appends into CP and bsm operands
 419       // are needed as well. This issue is tracked by a separate bug 8007037.
 420       int bss_idx = scratch_cp->invoke_dynamic_bootstrap_specifier_index(scratch_i);
 421 
 422       int ref_i = scratch_cp->invoke_dynamic_name_and_type_ref_index_at(scratch_i);
 423       int new_ref_i = find_or_append_indirect_entry(scratch_cp, ref_i, merge_cp_p,
 424                                                     merge_cp_length_p, THREAD);
 425       if (new_ref_i != ref_i) {




 426         RC_TRACE(0x00080000,
 427                  ("InvokeDynamic entry@%d name_and_type ref_index change: %d to %d",
 428                   *merge_cp_length_p, ref_i, new_ref_i));
 429       }





 430 
 431       (*merge_cp_p)->invoke_dynamic_at_put(*merge_cp_length_p, bss_idx, new_ref_i);
 432       if (scratch_i != *merge_cp_length_p) {
 433         // The new entry in *merge_cp_p is at a different index than
 434         // the new entry in scratch_cp so we need to map the index values.
 435         map_index(scratch_cp, scratch_i, *merge_cp_length_p);
 436       }
 437       (*merge_cp_length_p)++;
 438     } break;
 439 
 440     // At this stage, Class or UnresolvedClass could be here, but not
 441     // ClassIndex
 442     case JVM_CONSTANT_ClassIndex: // fall through
 443 
 444     // Invalid is used as the tag for the second constant pool entry
 445     // occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should
 446     // not be seen by itself.
 447     case JVM_CONSTANT_Invalid: // fall through
 448 
 449     // At this stage, String could be here, but not StringIndex
 450     case JVM_CONSTANT_StringIndex: // fall through
 451 


 475     int found_i = scratch_cp->find_matching_entry(ref_i, *merge_cp_p, THREAD);
 476     if (found_i != 0) {
 477       guarantee(found_i != ref_i, "compare_entry_to() and find_matching_entry() do not agree");
 478       // Found a matching entry somewhere else in *merge_cp_p so just need a mapping entry.
 479       new_ref_i = found_i;
 480       map_index(scratch_cp, ref_i, found_i);
 481     } else {
 482       // no match found so we have to append this entry to *merge_cp_p
 483       append_entry(scratch_cp, ref_i, merge_cp_p, merge_cp_length_p, THREAD);
 484       // The above call to append_entry() can only append one entry
 485       // so the post call query of *merge_cp_length_p is only for
 486       // the sake of consistency.
 487       new_ref_i = *merge_cp_length_p - 1;
 488     }
 489   }
 490 
 491   return new_ref_i;
 492 } // end find_or_append_indirect_entry()
 493 
 494 



































































































 495 jvmtiError VM_RedefineClasses::compare_and_normalize_class_versions(
 496              instanceKlassHandle the_class,
 497              instanceKlassHandle scratch_class) {
 498   int i;
 499 
 500   // Check superclasses, or rather their names, since superclasses themselves can be
 501   // requested to replace.
 502   // Check for NULL superclass first since this might be java.lang.Object
 503   if (the_class->super() != scratch_class->super() &&
 504       (the_class->super() == NULL || scratch_class->super() == NULL ||
 505        the_class->super()->name() !=
 506        scratch_class->super()->name())) {
 507     return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED;
 508   }
 509 
 510   // Check if the number, names and order of directly implemented interfaces are the same.
 511   // I think in principle we should just check if the sets of names of directly implemented
 512   // interfaces are the same, i.e. the order of declaration (which, however, if changed in the
 513   // .java file, also changes in .class file) should not matter. However, comparing sets is
 514   // technically a bit more difficult, and, more importantly, I am not sure at present that the


 748     return 0;
 749   }
 750 
 751   if (old_index < 1 || old_index >= _index_map_p->length()) {
 752     // The old_index is out of range so it is not mapped. This should
 753     // not happen in regular constant pool merging use, but it can
 754     // happen if a corrupt annotation is processed.
 755     return 0;
 756   }
 757 
 758   int value = _index_map_p->at(old_index);
 759   if (value == -1) {
 760     // the old_index is not mapped
 761     return 0;
 762   }
 763 
 764   return value;
 765 } // end find_new_index()
 766 
 767 

























 768 // Returns true if the current mismatch is due to a resolved/unresolved
 769 // class pair. Otherwise, returns false.
 770 bool VM_RedefineClasses::is_unresolved_class_mismatch(constantPoolHandle cp1,
 771        int index1, constantPoolHandle cp2, int index2) {
 772 
 773   jbyte t1 = cp1->tag_at(index1).value();
 774   if (t1 != JVM_CONSTANT_Class && t1 != JVM_CONSTANT_UnresolvedClass) {
 775     return false;  // wrong entry type; not our special case
 776   }
 777 
 778   jbyte t2 = cp2->tag_at(index2).value();
 779   if (t2 != JVM_CONSTANT_Class && t2 != JVM_CONSTANT_UnresolvedClass) {
 780     return false;  // wrong entry type; not our special case
 781   }
 782 
 783   if (t1 == t2) {
 784     return false;  // not a mismatch; not our special case
 785   }
 786 
 787   char *s1 = cp1->klass_name_at(index1)->as_C_string();


 997 void VM_RedefineClasses::map_index(constantPoolHandle scratch_cp,
 998        int old_index, int new_index) {
 999   if (find_new_index(old_index) != 0) {
1000     // old_index is already mapped
1001     return;
1002   }
1003 
1004   if (old_index == new_index) {
1005     // no mapping is needed
1006     return;
1007   }
1008 
1009   _index_map_p->at_put(old_index, new_index);
1010   _index_map_count++;
1011 
1012   RC_TRACE(0x00040000, ("mapped tag %d at index %d to %d",
1013     scratch_cp->tag_at(old_index).value(), old_index, new_index));
1014 } // end map_index()
1015 
1016 



















1017 // Merge old_cp and scratch_cp and return the results of the merge via
1018 // merge_cp_p. The number of entries in *merge_cp_p is returned via
1019 // merge_cp_length_p. The entries in old_cp occupy the same locations
1020 // in *merge_cp_p. Also creates a map of indices from entries in
1021 // scratch_cp to the corresponding entry in *merge_cp_p. Index map
1022 // entries are only created for entries in scratch_cp that occupy a
1023 // different location in *merged_cp_p.
1024 bool VM_RedefineClasses::merge_constant_pools(constantPoolHandle old_cp,
1025        constantPoolHandle scratch_cp, constantPoolHandle *merge_cp_p,
1026        int *merge_cp_length_p, TRAPS) {
1027 
1028   if (merge_cp_p == NULL) {
1029     assert(false, "caller must provide scratch constantPool");
1030     return false; // robustness
1031   }
1032   if (merge_cp_length_p == NULL) {
1033     assert(false, "caller must provide scratch CP length");
1034     return false; // robustness
1035   }
1036   // Worst case we need old_cp->length() + scratch_cp()->length(),


1069         (*merge_cp_p)->unresolved_klass_at_put(old_i,
1070           old_cp->klass_name_at(old_i));
1071         break;
1072 
1073       case JVM_CONSTANT_Double:
1074       case JVM_CONSTANT_Long:
1075         // just copy the entry to *merge_cp_p, but double and long take
1076         // two constant pool entries
1077         ConstantPool::copy_entry_to(old_cp, old_i, *merge_cp_p, old_i, CHECK_0);
1078         old_i++;
1079         break;
1080 
1081       default:
1082         // just copy the entry to *merge_cp_p
1083         ConstantPool::copy_entry_to(old_cp, old_i, *merge_cp_p, old_i, CHECK_0);
1084         break;
1085       }
1086     } // end for each old_cp entry
1087 
1088     ConstantPool::copy_operands(old_cp, *merge_cp_p, CHECK_0);

1089 
1090     // We don't need to sanity check that *merge_cp_length_p is within
1091     // *merge_cp_p bounds since we have the minimum on-entry check above.
1092     (*merge_cp_length_p) = old_i;
1093   }
1094 
1095   // merge_cp_len should be the same as old_cp->length() at this point
1096   // so this trace message is really a "warm-and-breathing" message.
1097   RC_TRACE_WITH_THREAD(0x00020000, THREAD,
1098     ("after pass 0: merge_cp_len=%d", *merge_cp_length_p));
1099 
1100   int scratch_i;  // index into scratch_cp
1101   {
1102     // Pass 1a:
1103     // Compare scratch_cp entries to the old_cp entries that we have
1104     // already copied to *merge_cp_p. In this pass, we are eliminating
1105     // exact duplicates (matching entry at same index) so we only
1106     // compare entries in the common indice range.
1107     int increment = 1;
1108     int pass1a_length = MIN2(old_cp->length(), scratch_cp->length());


1181       default:
1182         increment = 1;
1183         break;
1184       }
1185 
1186       int found_i =
1187         scratch_cp->find_matching_entry(scratch_i, *merge_cp_p, CHECK_0);
1188       if (found_i != 0) {
1189         // Found a matching entry somewhere else in *merge_cp_p so
1190         // just need a mapping entry.
1191         map_index(scratch_cp, scratch_i, found_i);
1192         continue;
1193       }
1194 
1195       // No match found so we have to append this entry and any unique
1196       // referenced entries to *merge_cp_p.
1197       append_entry(scratch_cp, scratch_i, merge_cp_p, merge_cp_length_p,
1198         CHECK_0);
1199     }
1200 


1201     RC_TRACE_WITH_THREAD(0x00020000, THREAD,
1202       ("after pass 1b: merge_cp_len=%d, scratch_i=%d, index_map_len=%d",
1203       *merge_cp_length_p, scratch_i, _index_map_count));
1204   }
1205 
1206   return true;
1207 } // end merge_constant_pools()
1208 
1209 
1210 // Scoped object to clean up the constant pool(s) created for merging
1211 class MergeCPCleaner {
1212   ClassLoaderData*   _loader_data;
1213   ConstantPool*      _cp;
1214   ConstantPool*      _scratch_cp;
1215  public:
1216   MergeCPCleaner(ClassLoaderData* loader_data, ConstantPool* merge_cp) :
1217                  _loader_data(loader_data), _cp(merge_cp), _scratch_cp(NULL) {}
1218   ~MergeCPCleaner() {
1219     _loader_data->add_to_deallocate_list(_cp);
1220     if (_scratch_cp != NULL) {


1253 
1254   // Get constants() from the old class because it could have been rewritten
1255   // while we were at a safepoint allocating a new constant pool.
1256   constantPoolHandle old_cp(THREAD, the_class->constants());
1257   constantPoolHandle scratch_cp(THREAD, scratch_class->constants());
1258 
1259   // If the length changed, the class was redefined out from under us. Return
1260   // an error.
1261   if (merge_cp_length != the_class->constants()->length()
1262          + scratch_class->constants()->length()) {
1263     return JVMTI_ERROR_INTERNAL;
1264   }
1265 
1266   // Update the version number of the constant pool
1267   merge_cp->increment_and_save_version(old_cp->version());
1268 
1269   ResourceMark rm(THREAD);
1270   _index_map_count = 0;
1271   _index_map_p = new intArray(scratch_cp->length(), -1);
1272 





1273   // reference to the cp holder is needed for copy_operands()
1274   merge_cp->set_pool_holder(scratch_class());
1275   bool result = merge_constant_pools(old_cp, scratch_cp, &merge_cp,
1276                   &merge_cp_length, THREAD);
1277   merge_cp->set_pool_holder(NULL);
1278 
1279   if (!result) {
1280     // The merge can fail due to memory allocation failure or due
1281     // to robustness checks.
1282     return JVMTI_ERROR_INTERNAL;
1283   }
1284 
1285   RC_TRACE_WITH_THREAD(0x00010000, THREAD,
1286     ("merge_cp_len=%d, index_map_len=%d", merge_cp_length, _index_map_count));
1287 
1288   if (_index_map_count == 0) {
1289     // there is nothing to map between the new and merged constant pools
1290 
1291     if (old_cp->length() == scratch_cp->length()) {
1292       // The old and new constant pools are the same length and the


1383     return false;
1384   }
1385 
1386   // rewrite constant pool references in the methods_parameter_annotations:
1387   if (!rewrite_cp_refs_in_methods_parameter_annotations(scratch_class,
1388          THREAD)) {
1389     // propagate failure back to caller
1390     return false;
1391   }
1392 
1393   // rewrite constant pool references in the methods_default_annotations:
1394   if (!rewrite_cp_refs_in_methods_default_annotations(scratch_class,
1395          THREAD)) {
1396     // propagate failure back to caller
1397     return false;
1398   }
1399 
1400   return true;
1401 } // end rewrite_cp_refs()
1402 
1403 
1404 // Rewrite constant pool references in the methods.
1405 bool VM_RedefineClasses::rewrite_cp_refs_in_methods(
1406        instanceKlassHandle scratch_class, TRAPS) {
1407 
1408   Array<Method*>* methods = scratch_class->methods();
1409 
1410   if (methods == NULL || methods->length() == 0) {
1411     // no methods so nothing to do
1412     return true;
1413   }
1414 
1415   // rewrite constant pool references in the methods:
1416   for (int i = methods->length() - 1; i >= 0; i--) {
1417     methodHandle method(THREAD, methods->at(i));
1418     methodHandle new_method;
1419     rewrite_cp_refs_in_method(method, &new_method, CHECK_false);
1420     if (!new_method.is_null()) {
1421       // the method has been replaced so save the new method version
1422       methods->at_put(i, new_method());
1423     }




 398       int ref_i = scratch_cp->method_handle_index_at(scratch_i);
 399       int new_ref_i = find_or_append_indirect_entry(scratch_cp, ref_i, merge_cp_p,
 400                                                     merge_cp_length_p, THREAD);
 401       if (new_ref_i != ref_i) {
 402         RC_TRACE(0x00080000,
 403                  ("MethodHandle entry@%d ref_index change: %d to %d",
 404                   *merge_cp_length_p, ref_i, new_ref_i));
 405       }
 406       (*merge_cp_p)->method_handle_index_at_put(*merge_cp_length_p, ref_kind, new_ref_i);
 407       if (scratch_i != *merge_cp_length_p) {
 408         // The new entry in *merge_cp_p is at a different index than
 409         // the new entry in scratch_cp so we need to map the index values.
 410         map_index(scratch_cp, scratch_i, *merge_cp_length_p);
 411       }
 412       (*merge_cp_length_p)++;
 413     } break;
 414 
 415     // this is an indirect CP entry so it needs special handling
 416     case JVM_CONSTANT_InvokeDynamic:
 417     {
 418       // Index of the bootstrap specifier in the operands array
 419       int old_bs_i = scratch_cp->invoke_dynamic_bootstrap_specifier_index(scratch_i);
 420       int new_bs_i = find_or_append_operand(scratch_cp, old_bs_i, merge_cp_p,



 421                                             merge_cp_length_p, THREAD);
 422       // The bootstrap method NameAndType_info index
 423       int old_ref_i = scratch_cp->invoke_dynamic_name_and_type_ref_index_at(scratch_i);
 424       int new_ref_i = find_or_append_indirect_entry(scratch_cp, old_ref_i, merge_cp_p,
 425                                                     merge_cp_length_p, THREAD);
 426       if (new_bs_i != old_bs_i) {
 427         RC_TRACE(0x00080000,
 428                  ("InvokeDynamic entry@%d bootstrap_method_attr_index change: %d to %d",
 429                   *merge_cp_length_p, old_bs_i, new_bs_i));
 430       }
 431       if (new_ref_i != old_ref_i) {
 432         RC_TRACE(0x00080000,
 433                  ("InvokeDynamic entry@%d name_and_type_index change: %d to %d",
 434                   *merge_cp_length_p, old_ref_i, new_ref_i));
 435       }
 436 
 437       (*merge_cp_p)->invoke_dynamic_at_put(*merge_cp_length_p, new_bs_i, new_ref_i);
 438       if (scratch_i != *merge_cp_length_p) {
 439         // The new entry in *merge_cp_p is at a different index than
 440         // the new entry in scratch_cp so we need to map the index values.
 441         map_index(scratch_cp, scratch_i, *merge_cp_length_p);
 442       }
 443       (*merge_cp_length_p)++;
 444     } break;
 445 
 446     // At this stage, Class or UnresolvedClass could be here, but not
 447     // ClassIndex
 448     case JVM_CONSTANT_ClassIndex: // fall through
 449 
 450     // Invalid is used as the tag for the second constant pool entry
 451     // occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should
 452     // not be seen by itself.
 453     case JVM_CONSTANT_Invalid: // fall through
 454 
 455     // At this stage, String could be here, but not StringIndex
 456     case JVM_CONSTANT_StringIndex: // fall through
 457 


 481     int found_i = scratch_cp->find_matching_entry(ref_i, *merge_cp_p, THREAD);
 482     if (found_i != 0) {
 483       guarantee(found_i != ref_i, "compare_entry_to() and find_matching_entry() do not agree");
 484       // Found a matching entry somewhere else in *merge_cp_p so just need a mapping entry.
 485       new_ref_i = found_i;
 486       map_index(scratch_cp, ref_i, found_i);
 487     } else {
 488       // no match found so we have to append this entry to *merge_cp_p
 489       append_entry(scratch_cp, ref_i, merge_cp_p, merge_cp_length_p, THREAD);
 490       // The above call to append_entry() can only append one entry
 491       // so the post call query of *merge_cp_length_p is only for
 492       // the sake of consistency.
 493       new_ref_i = *merge_cp_length_p - 1;
 494     }
 495   }
 496 
 497   return new_ref_i;
 498 } // end find_or_append_indirect_entry()
 499 
 500 
 501 // Append a bootstrap specifier into the merge_cp operands that is semantically equal
 502 // to the scratch_cp operands bootstrap specifier passed by the old_bs_i index.
 503 // Recursively append new merge_cp entries referenced by the new bootstrap specifier.
 504 void VM_RedefineClasses::append_operand(constantPoolHandle scratch_cp, int old_bs_i,
 505        constantPoolHandle *merge_cp_p, int *merge_cp_length_p, TRAPS) {
 506 
 507   int old_ref_i = scratch_cp->operand_bootstrap_method_ref_index_at(old_bs_i);
 508   int new_ref_i = find_or_append_indirect_entry(scratch_cp, old_ref_i, merge_cp_p,
 509                                                 merge_cp_length_p, THREAD);
 510   if (new_ref_i != old_ref_i) {
 511     RC_TRACE(0x00080000,
 512              ("operands entry@%d bootstrap method ref_index change: %d to %d",
 513               _operands_cur_length, old_ref_i, new_ref_i));
 514   }
 515 
 516   Array<u2>* merge_ops = (*merge_cp_p)->operands();
 517   int new_bs_i = _operands_cur_length;
 518   // We have _operands_cur_length == 0 when the merge_cp operands is empty yet.
 519   // However, the operand_offset_at(0) was set in the extend_operands() call.
 520   int new_base = (new_bs_i == 0) ? (*merge_cp_p)->operand_offset_at(0)
 521                                  : (*merge_cp_p)->operand_next_offset_at(new_bs_i - 1);
 522   int argc     = scratch_cp->operand_argument_count_at(old_bs_i);
 523 
 524   ConstantPool::operand_offset_at_put(merge_ops, _operands_cur_length, new_base);
 525   merge_ops->at_put(new_base++, new_ref_i);
 526   merge_ops->at_put(new_base++, argc);
 527 
 528   for (int i = 0; i < argc; i++) {
 529     int old_arg_ref_i = scratch_cp->operand_argument_index_at(old_bs_i, i);
 530     int new_arg_ref_i = find_or_append_indirect_entry(scratch_cp, old_arg_ref_i, merge_cp_p,
 531                                                       merge_cp_length_p, THREAD);
 532     merge_ops->at_put(new_base++, new_arg_ref_i);
 533     if (new_arg_ref_i != old_arg_ref_i) {
 534       RC_TRACE(0x00080000,
 535                ("operands entry@%d bootstrap method argument ref_index change: %d to %d",
 536                 _operands_cur_length, old_arg_ref_i, new_arg_ref_i));
 537     }
 538   }
 539   if (old_bs_i != _operands_cur_length) {
 540     // The bootstrap specifier in *merge_cp_p is at a different index than
 541     // that in scratch_cp so we need to map the index values.
 542     map_operand_index(old_bs_i, new_bs_i);
 543   }
 544   _operands_cur_length++;
 545 } // end append_operand()
 546 
 547 
 548 int VM_RedefineClasses::find_or_append_operand(constantPoolHandle scratch_cp,
 549       int old_bs_i, constantPoolHandle *merge_cp_p, int *merge_cp_length_p, TRAPS) {
 550 
 551   int new_bs_i = old_bs_i; // bootstrap specifier index
 552   bool match = (old_bs_i < _operands_cur_length) &&
 553                scratch_cp->compare_operand_to(old_bs_i, *merge_cp_p, old_bs_i, THREAD);
 554 
 555   if (!match) {
 556     // forward reference in *merge_cp_p or not a direct match
 557     int found_i = scratch_cp->find_matching_operand(old_bs_i, *merge_cp_p,
 558                                                     _operands_cur_length, THREAD);
 559     if (found_i != -1) {
 560       guarantee(found_i != old_bs_i, "compare_operand_to() and find_matching_operand() disagree");
 561       // found a matching operand somewhere else in *merge_cp_p so just need a mapping
 562       new_bs_i = found_i;
 563       map_operand_index(old_bs_i, found_i);
 564     } else {
 565       // no match found so we have to append this bootstrap specifier to *merge_cp_p
 566       append_operand(scratch_cp, old_bs_i, merge_cp_p, merge_cp_length_p, THREAD);
 567       new_bs_i = _operands_cur_length - 1;
 568     }
 569   }
 570   return new_bs_i;
 571 } // end find_or_append_operand()
 572 
 573 
 574 void VM_RedefineClasses::finalize_operands_merge(constantPoolHandle merge_cp, TRAPS) {
 575   if (merge_cp->operands() == NULL) {
 576     return;
 577   }
 578   // Shrink the merge_cp operands
 579   merge_cp->shrink_operands(_operands_cur_length, CHECK);
 580 
 581   if (RC_TRACE_ENABLED(0x00040000)) {
 582     // don't want to loop unless we are tracing
 583     int count = 0;
 584     for (int i = 1; i < _operands_index_map_p->length(); i++) {
 585       int value = _operands_index_map_p->at(i);
 586       if (value != -1) {
 587         RC_TRACE_WITH_THREAD(0x00040000, THREAD,
 588           ("operands_index_map[%d]: old=%d new=%d", count, i, value));
 589         count++;
 590       }
 591     }
 592   }
 593   // Clean-up
 594   _operands_index_map_p = NULL;
 595   _operands_cur_length = 0;
 596   _operands_index_map_count = 0;
 597 } // end finalize_operands_merge()
 598 
 599 
 600 jvmtiError VM_RedefineClasses::compare_and_normalize_class_versions(
 601              instanceKlassHandle the_class,
 602              instanceKlassHandle scratch_class) {
 603   int i;
 604 
 605   // Check superclasses, or rather their names, since superclasses themselves can be
 606   // requested to replace.
 607   // Check for NULL superclass first since this might be java.lang.Object
 608   if (the_class->super() != scratch_class->super() &&
 609       (the_class->super() == NULL || scratch_class->super() == NULL ||
 610        the_class->super()->name() !=
 611        scratch_class->super()->name())) {
 612     return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED;
 613   }
 614 
 615   // Check if the number, names and order of directly implemented interfaces are the same.
 616   // I think in principle we should just check if the sets of names of directly implemented
 617   // interfaces are the same, i.e. the order of declaration (which, however, if changed in the
 618   // .java file, also changes in .class file) should not matter. However, comparing sets is
 619   // technically a bit more difficult, and, more importantly, I am not sure at present that the


 853     return 0;
 854   }
 855 
 856   if (old_index < 1 || old_index >= _index_map_p->length()) {
 857     // The old_index is out of range so it is not mapped. This should
 858     // not happen in regular constant pool merging use, but it can
 859     // happen if a corrupt annotation is processed.
 860     return 0;
 861   }
 862 
 863   int value = _index_map_p->at(old_index);
 864   if (value == -1) {
 865     // the old_index is not mapped
 866     return 0;
 867   }
 868 
 869   return value;
 870 } // end find_new_index()
 871 
 872 
 873 // Find new bootstrap specifier index value for old bootstrap specifier index
 874 // value by seaching the index map. Returns zero (-1) if there is no mapped
 875 // value for the old bootstrap specifier index.
 876 int VM_RedefineClasses::find_new_operand_index(int old_index) {
 877   if (_operands_index_map_count == 0) {
 878     // map is empty so nothing can be found
 879     return -1;
 880   }
 881 
 882   if (old_index == -1 || old_index >= _operands_index_map_p->length()) {
 883     // The old_index is out of range so it is not mapped.
 884     // This should not happen in regular constant pool merging use.
 885     return -1;
 886   }
 887 
 888   int value = _operands_index_map_p->at(old_index);
 889   if (value == -1) {
 890     // the old_index is not mapped
 891     return -1;
 892   }
 893 
 894   return value;
 895 } // end find_new_operand_index()
 896 
 897 
 898 // Returns true if the current mismatch is due to a resolved/unresolved
 899 // class pair. Otherwise, returns false.
 900 bool VM_RedefineClasses::is_unresolved_class_mismatch(constantPoolHandle cp1,
 901        int index1, constantPoolHandle cp2, int index2) {
 902 
 903   jbyte t1 = cp1->tag_at(index1).value();
 904   if (t1 != JVM_CONSTANT_Class && t1 != JVM_CONSTANT_UnresolvedClass) {
 905     return false;  // wrong entry type; not our special case
 906   }
 907 
 908   jbyte t2 = cp2->tag_at(index2).value();
 909   if (t2 != JVM_CONSTANT_Class && t2 != JVM_CONSTANT_UnresolvedClass) {
 910     return false;  // wrong entry type; not our special case
 911   }
 912 
 913   if (t1 == t2) {
 914     return false;  // not a mismatch; not our special case
 915   }
 916 
 917   char *s1 = cp1->klass_name_at(index1)->as_C_string();


1127 void VM_RedefineClasses::map_index(constantPoolHandle scratch_cp,
1128        int old_index, int new_index) {
1129   if (find_new_index(old_index) != 0) {
1130     // old_index is already mapped
1131     return;
1132   }
1133 
1134   if (old_index == new_index) {
1135     // no mapping is needed
1136     return;
1137   }
1138 
1139   _index_map_p->at_put(old_index, new_index);
1140   _index_map_count++;
1141 
1142   RC_TRACE(0x00040000, ("mapped tag %d at index %d to %d",
1143     scratch_cp->tag_at(old_index).value(), old_index, new_index));
1144 } // end map_index()
1145 
1146 
1147 // Map old_index to new_index as needed.
1148 void VM_RedefineClasses::map_operand_index(int old_index, int new_index) {
1149   if (find_new_operand_index(old_index) != -1) {
1150     // old_index is already mapped
1151     return;
1152   }
1153 
1154   if (old_index == new_index) {
1155     // no mapping is needed
1156     return;
1157   }
1158 
1159   _operands_index_map_p->at_put(old_index, new_index);
1160   _operands_index_map_count++;
1161 
1162   RC_TRACE(0x00040000, ("mapped bootstrap specifier at index %d to %d", old_index, new_index));
1163 } // end map_index()
1164 
1165 
1166 // Merge old_cp and scratch_cp and return the results of the merge via
1167 // merge_cp_p. The number of entries in *merge_cp_p is returned via
1168 // merge_cp_length_p. The entries in old_cp occupy the same locations
1169 // in *merge_cp_p. Also creates a map of indices from entries in
1170 // scratch_cp to the corresponding entry in *merge_cp_p. Index map
1171 // entries are only created for entries in scratch_cp that occupy a
1172 // different location in *merged_cp_p.
1173 bool VM_RedefineClasses::merge_constant_pools(constantPoolHandle old_cp,
1174        constantPoolHandle scratch_cp, constantPoolHandle *merge_cp_p,
1175        int *merge_cp_length_p, TRAPS) {
1176 
1177   if (merge_cp_p == NULL) {
1178     assert(false, "caller must provide scratch constantPool");
1179     return false; // robustness
1180   }
1181   if (merge_cp_length_p == NULL) {
1182     assert(false, "caller must provide scratch CP length");
1183     return false; // robustness
1184   }
1185   // Worst case we need old_cp->length() + scratch_cp()->length(),


1218         (*merge_cp_p)->unresolved_klass_at_put(old_i,
1219           old_cp->klass_name_at(old_i));
1220         break;
1221 
1222       case JVM_CONSTANT_Double:
1223       case JVM_CONSTANT_Long:
1224         // just copy the entry to *merge_cp_p, but double and long take
1225         // two constant pool entries
1226         ConstantPool::copy_entry_to(old_cp, old_i, *merge_cp_p, old_i, CHECK_0);
1227         old_i++;
1228         break;
1229 
1230       default:
1231         // just copy the entry to *merge_cp_p
1232         ConstantPool::copy_entry_to(old_cp, old_i, *merge_cp_p, old_i, CHECK_0);
1233         break;
1234       }
1235     } // end for each old_cp entry
1236 
1237     ConstantPool::copy_operands(old_cp, *merge_cp_p, CHECK_0);
1238     (*merge_cp_p)->extend_operands(scratch_cp, CHECK_0);
1239 
1240     // We don't need to sanity check that *merge_cp_length_p is within
1241     // *merge_cp_p bounds since we have the minimum on-entry check above.
1242     (*merge_cp_length_p) = old_i;
1243   }
1244 
1245   // merge_cp_len should be the same as old_cp->length() at this point
1246   // so this trace message is really a "warm-and-breathing" message.
1247   RC_TRACE_WITH_THREAD(0x00020000, THREAD,
1248     ("after pass 0: merge_cp_len=%d", *merge_cp_length_p));
1249 
1250   int scratch_i;  // index into scratch_cp
1251   {
1252     // Pass 1a:
1253     // Compare scratch_cp entries to the old_cp entries that we have
1254     // already copied to *merge_cp_p. In this pass, we are eliminating
1255     // exact duplicates (matching entry at same index) so we only
1256     // compare entries in the common indice range.
1257     int increment = 1;
1258     int pass1a_length = MIN2(old_cp->length(), scratch_cp->length());


1331       default:
1332         increment = 1;
1333         break;
1334       }
1335 
1336       int found_i =
1337         scratch_cp->find_matching_entry(scratch_i, *merge_cp_p, CHECK_0);
1338       if (found_i != 0) {
1339         // Found a matching entry somewhere else in *merge_cp_p so
1340         // just need a mapping entry.
1341         map_index(scratch_cp, scratch_i, found_i);
1342         continue;
1343       }
1344 
1345       // No match found so we have to append this entry and any unique
1346       // referenced entries to *merge_cp_p.
1347       append_entry(scratch_cp, scratch_i, merge_cp_p, merge_cp_length_p,
1348         CHECK_0);
1349     }
1350 
1351     finalize_operands_merge(*merge_cp_p, THREAD);
1352 
1353     RC_TRACE_WITH_THREAD(0x00020000, THREAD,
1354       ("after pass 1b: merge_cp_len=%d, scratch_i=%d, index_map_len=%d",
1355       *merge_cp_length_p, scratch_i, _index_map_count));
1356   }
1357 
1358   return true;
1359 } // end merge_constant_pools()
1360 
1361 
1362 // Scoped object to clean up the constant pool(s) created for merging
1363 class MergeCPCleaner {
1364   ClassLoaderData*   _loader_data;
1365   ConstantPool*      _cp;
1366   ConstantPool*      _scratch_cp;
1367  public:
1368   MergeCPCleaner(ClassLoaderData* loader_data, ConstantPool* merge_cp) :
1369                  _loader_data(loader_data), _cp(merge_cp), _scratch_cp(NULL) {}
1370   ~MergeCPCleaner() {
1371     _loader_data->add_to_deallocate_list(_cp);
1372     if (_scratch_cp != NULL) {


1405 
1406   // Get constants() from the old class because it could have been rewritten
1407   // while we were at a safepoint allocating a new constant pool.
1408   constantPoolHandle old_cp(THREAD, the_class->constants());
1409   constantPoolHandle scratch_cp(THREAD, scratch_class->constants());
1410 
1411   // If the length changed, the class was redefined out from under us. Return
1412   // an error.
1413   if (merge_cp_length != the_class->constants()->length()
1414          + scratch_class->constants()->length()) {
1415     return JVMTI_ERROR_INTERNAL;
1416   }
1417 
1418   // Update the version number of the constant pool
1419   merge_cp->increment_and_save_version(old_cp->version());
1420 
1421   ResourceMark rm(THREAD);
1422   _index_map_count = 0;
1423   _index_map_p = new intArray(scratch_cp->length(), -1);
1424 
1425   _operands_cur_length = ConstantPool::operand_array_length(old_cp->operands());
1426   _operands_index_map_count = 0;
1427   _operands_index_map_p = new intArray(
1428     ConstantPool::operand_array_length(scratch_cp->operands()), -1);
1429 
1430   // reference to the cp holder is needed for copy_operands()
1431   merge_cp->set_pool_holder(scratch_class());
1432   bool result = merge_constant_pools(old_cp, scratch_cp, &merge_cp,
1433                   &merge_cp_length, THREAD);
1434   merge_cp->set_pool_holder(NULL);
1435 
1436   if (!result) {
1437     // The merge can fail due to memory allocation failure or due
1438     // to robustness checks.
1439     return JVMTI_ERROR_INTERNAL;
1440   }
1441 
1442   RC_TRACE_WITH_THREAD(0x00010000, THREAD,
1443     ("merge_cp_len=%d, index_map_len=%d", merge_cp_length, _index_map_count));
1444 
1445   if (_index_map_count == 0) {
1446     // there is nothing to map between the new and merged constant pools
1447 
1448     if (old_cp->length() == scratch_cp->length()) {
1449       // The old and new constant pools are the same length and the


1540     return false;
1541   }
1542 
1543   // rewrite constant pool references in the methods_parameter_annotations:
1544   if (!rewrite_cp_refs_in_methods_parameter_annotations(scratch_class,
1545          THREAD)) {
1546     // propagate failure back to caller
1547     return false;
1548   }
1549 
1550   // rewrite constant pool references in the methods_default_annotations:
1551   if (!rewrite_cp_refs_in_methods_default_annotations(scratch_class,
1552          THREAD)) {
1553     // propagate failure back to caller
1554     return false;
1555   }
1556 
1557   return true;
1558 } // end rewrite_cp_refs()
1559 

1560 // Rewrite constant pool references in the methods.
1561 bool VM_RedefineClasses::rewrite_cp_refs_in_methods(
1562        instanceKlassHandle scratch_class, TRAPS) {
1563 
1564   Array<Method*>* methods = scratch_class->methods();
1565 
1566   if (methods == NULL || methods->length() == 0) {
1567     // no methods so nothing to do
1568     return true;
1569   }
1570 
1571   // rewrite constant pool references in the methods:
1572   for (int i = methods->length() - 1; i >= 0; i--) {
1573     methodHandle method(THREAD, methods->at(i));
1574     methodHandle new_method;
1575     rewrite_cp_refs_in_method(method, &new_method, CHECK_false);
1576     if (!new_method.is_null()) {
1577       // the method has been replaced so save the new method version
1578       methods->at_put(i, new_method());
1579     }