hotspot/src/share/vm/prims/jvmtiRedefineClasses.cpp
changeset 13728 882756847a04
parent 13286 b8b0898d5f3a
child 13952 e3cf184080bc
equal deleted inserted replaced
13727:caf5eb7dd4a7 13728:882756847a04
    24 
    24 
    25 #include "precompiled.hpp"
    25 #include "precompiled.hpp"
    26 #include "classfile/systemDictionary.hpp"
    26 #include "classfile/systemDictionary.hpp"
    27 #include "classfile/verifier.hpp"
    27 #include "classfile/verifier.hpp"
    28 #include "code/codeCache.hpp"
    28 #include "code/codeCache.hpp"
       
    29 #include "compiler/compileBroker.hpp"
    29 #include "interpreter/oopMapCache.hpp"
    30 #include "interpreter/oopMapCache.hpp"
    30 #include "interpreter/rewriter.hpp"
    31 #include "interpreter/rewriter.hpp"
    31 #include "memory/gcLocker.hpp"
    32 #include "memory/gcLocker.hpp"
       
    33 #include "memory/metadataFactory.hpp"
       
    34 #include "memory/metaspaceShared.hpp"
    32 #include "memory/universe.inline.hpp"
    35 #include "memory/universe.inline.hpp"
    33 #include "oops/fieldStreams.hpp"
    36 #include "oops/fieldStreams.hpp"
    34 #include "oops/klassVtable.hpp"
    37 #include "oops/klassVtable.hpp"
    35 #include "prims/jvmtiImpl.hpp"
    38 #include "prims/jvmtiImpl.hpp"
    36 #include "prims/jvmtiRedefineClasses.hpp"
    39 #include "prims/jvmtiRedefineClasses.hpp"
    38 #include "runtime/deoptimization.hpp"
    41 #include "runtime/deoptimization.hpp"
    39 #include "runtime/relocator.hpp"
    42 #include "runtime/relocator.hpp"
    40 #include "utilities/bitMap.inline.hpp"
    43 #include "utilities/bitMap.inline.hpp"
    41 
    44 
    42 
    45 
    43 objArrayOop VM_RedefineClasses::_old_methods = NULL;
    46 Array<Method*>* VM_RedefineClasses::_old_methods = NULL;
    44 objArrayOop VM_RedefineClasses::_new_methods = NULL;
    47 Array<Method*>* VM_RedefineClasses::_new_methods = NULL;
    45 methodOop*  VM_RedefineClasses::_matching_old_methods = NULL;
    48 Method**  VM_RedefineClasses::_matching_old_methods = NULL;
    46 methodOop*  VM_RedefineClasses::_matching_new_methods = NULL;
    49 Method**  VM_RedefineClasses::_matching_new_methods = NULL;
    47 methodOop*  VM_RedefineClasses::_deleted_methods      = NULL;
    50 Method**  VM_RedefineClasses::_deleted_methods      = NULL;
    48 methodOop*  VM_RedefineClasses::_added_methods        = NULL;
    51 Method**  VM_RedefineClasses::_added_methods        = NULL;
    49 int         VM_RedefineClasses::_matching_methods_length = 0;
    52 int         VM_RedefineClasses::_matching_methods_length = 0;
    50 int         VM_RedefineClasses::_deleted_methods_length  = 0;
    53 int         VM_RedefineClasses::_deleted_methods_length  = 0;
    51 int         VM_RedefineClasses::_added_methods_length    = 0;
    54 int         VM_RedefineClasses::_added_methods_length    = 0;
    52 klassOop    VM_RedefineClasses::_the_class_oop = NULL;
    55 Klass*      VM_RedefineClasses::_the_class_oop = NULL;
    53 
    56 
    54 
    57 
    55 VM_RedefineClasses::VM_RedefineClasses(jint class_count,
    58 VM_RedefineClasses::VM_RedefineClasses(jint class_count,
    56                                        const jvmtiClassDefinition *class_defs,
    59                                        const jvmtiClassDefinition *class_defs,
    57                                        JvmtiClassLoadKind class_load_kind) {
    60                                        JvmtiClassLoadKind class_load_kind) {
    91 
    94 
    92   // We first load new class versions in the prologue, because somewhere down the
    95   // We first load new class versions in the prologue, because somewhere down the
    93   // call chain it is required that the current thread is a Java thread.
    96   // call chain it is required that the current thread is a Java thread.
    94   _res = load_new_class_versions(Thread::current());
    97   _res = load_new_class_versions(Thread::current());
    95   if (_res != JVMTI_ERROR_NONE) {
    98   if (_res != JVMTI_ERROR_NONE) {
       
    99     // free any successfully created classes, since none are redefined
       
   100     for (int i = 0; i < _class_count; i++) {
       
   101       if (_scratch_classes[i] != NULL) {
       
   102         ClassLoaderData* cld = _scratch_classes[i]->class_loader_data();
       
   103         // Free the memory for this class at class unloading time.  Not before
       
   104         // because CMS might think this is still live.
       
   105         cld->add_to_deallocate_list((InstanceKlass*)_scratch_classes[i]);
       
   106       }
       
   107     }
    96     // Free os::malloc allocated memory in load_new_class_version.
   108     // Free os::malloc allocated memory in load_new_class_version.
    97     os::free(_scratch_classes);
   109     os::free(_scratch_classes);
    98     RC_TIMER_STOP(_timer_vm_op_prologue);
   110     RC_TIMER_STOP(_timer_vm_op_prologue);
    99     return false;
   111     return false;
   100   }
   112   }
   101 
   113 
   102   RC_TIMER_STOP(_timer_vm_op_prologue);
   114   RC_TIMER_STOP(_timer_vm_op_prologue);
   103   return true;
   115   return true;
   104 }
   116 }
       
   117 
       
   118 // Keep track of marked on-stack metadata so it can be cleared.
       
   119 GrowableArray<Metadata*>* _marked_objects = NULL;
       
   120 NOT_PRODUCT(bool MetadataOnStackMark::_is_active = false;)
       
   121 
       
   122 // Walk metadata on the stack and mark it so that redefinition doesn't delete
       
   123 // it.  Class unloading also walks the previous versions and might try to
       
   124 // delete it, so this class is used by class unloading also.
       
   125 MetadataOnStackMark::MetadataOnStackMark() {
       
   126   assert(SafepointSynchronize::is_at_safepoint(), "sanity check");
       
   127   NOT_PRODUCT(_is_active = true;)
       
   128   if (_marked_objects == NULL) {
       
   129     _marked_objects = new (ResourceObj::C_HEAP, mtClass) GrowableArray<Metadata*>(1000, true);
       
   130   }
       
   131   Threads::metadata_do(Metadata::mark_on_stack);
       
   132   CodeCache::alive_nmethods_do(nmethod::mark_on_stack);
       
   133   CompileBroker::mark_on_stack();
       
   134 }
       
   135 
       
   136 MetadataOnStackMark::~MetadataOnStackMark() {
       
   137   assert(SafepointSynchronize::is_at_safepoint(), "sanity check");
       
   138   // Unmark everything that was marked.   Can't do the same walk because
       
   139   // redefine classes messes up the code cache so the set of methods
       
   140   // might not be the same.
       
   141   for (int i = 0; i< _marked_objects->length(); i++) {
       
   142     _marked_objects->at(i)->set_on_stack(false);
       
   143   }
       
   144   _marked_objects->clear();   // reuse growable array for next time.
       
   145   NOT_PRODUCT(_is_active = false;)
       
   146 }
       
   147 
       
   148 // Record which objects are marked so we can unmark the same objects.
       
   149 void MetadataOnStackMark::record(Metadata* m) {
       
   150   assert(_is_active, "metadata on stack marking is active");
       
   151   _marked_objects->push(m);
       
   152 }
       
   153 
   105 
   154 
   106 void VM_RedefineClasses::doit() {
   155 void VM_RedefineClasses::doit() {
   107   Thread *thread = Thread::current();
   156   Thread *thread = Thread::current();
   108 
   157 
   109   if (UseSharedSpaces) {
   158   if (UseSharedSpaces) {
   110     // Sharing is enabled so we remap the shared readonly space to
   159     // Sharing is enabled so we remap the shared readonly space to
   111     // shared readwrite, private just in case we need to redefine
   160     // shared readwrite, private just in case we need to redefine
   112     // a shared class. We do the remap during the doit() phase of
   161     // a shared class. We do the remap during the doit() phase of
   113     // the safepoint to be safer.
   162     // the safepoint to be safer.
   114     if (!CompactingPermGenGen::remap_shared_readonly_as_readwrite()) {
   163     if (!MetaspaceShared::remap_shared_readonly_as_readwrite()) {
   115       RC_TRACE_WITH_THREAD(0x00000001, thread,
   164       RC_TRACE_WITH_THREAD(0x00000001, thread,
   116         ("failed to remap shared readonly space to readwrite, private"));
   165         ("failed to remap shared readonly space to readwrite, private"));
   117       _res = JVMTI_ERROR_INTERNAL;
   166       _res = JVMTI_ERROR_INTERNAL;
   118       return;
   167       return;
   119     }
   168     }
   120   }
   169   }
   121 
   170 
       
   171   // Mark methods seen on stack and everywhere else so old methods are not
       
   172   // cleaned up if they're on the stack.
       
   173   MetadataOnStackMark md_on_stack;
       
   174   HandleMark hm(thread);   // make sure any handles created are deleted
       
   175                            // before the stack walk again.
       
   176 
   122   for (int i = 0; i < _class_count; i++) {
   177   for (int i = 0; i < _class_count; i++) {
   123     redefine_single_class(_class_defs[i].klass, _scratch_classes[i], thread);
   178     redefine_single_class(_class_defs[i].klass, _scratch_classes[i], thread);
   124   }
   179     ClassLoaderData* cld = _scratch_classes[i]->class_loader_data();
       
   180     // Free the memory for this class at class unloading time.  Not before
       
   181     // because CMS might think this is still live.
       
   182     cld->add_to_deallocate_list((InstanceKlass*)_scratch_classes[i]);
       
   183     _scratch_classes[i] = NULL;
       
   184   }
       
   185 
   125   // Disable any dependent concurrent compilations
   186   // Disable any dependent concurrent compilations
   126   SystemDictionary::notice_modification();
   187   SystemDictionary::notice_modification();
   127 
   188 
   128   // Set flag indicating that some invariants are no longer true.
   189   // Set flag indicating that some invariants are no longer true.
   129   // See jvmtiExport.hpp for detailed explanation.
   190   // See jvmtiExport.hpp for detailed explanation.
   134 #endif
   195 #endif
   135 }
   196 }
   136 
   197 
   137 void VM_RedefineClasses::doit_epilogue() {
   198 void VM_RedefineClasses::doit_epilogue() {
   138   // Free os::malloc allocated memory.
   199   // Free os::malloc allocated memory.
   139   // The memory allocated in redefine will be free'ed in next VM operation.
       
   140   os::free(_scratch_classes);
   200   os::free(_scratch_classes);
   141 
   201 
   142   if (RC_TRACE_ENABLED(0x00000004)) {
   202   if (RC_TRACE_ENABLED(0x00000004)) {
   143     // Used to have separate timers for "doit" and "all", but the timer
   203     // Used to have separate timers for "doit" and "all", but the timer
   144     // overhead skewed the measurements.
   204     // overhead skewed the measurements.
   158 bool VM_RedefineClasses::is_modifiable_class(oop klass_mirror) {
   218 bool VM_RedefineClasses::is_modifiable_class(oop klass_mirror) {
   159   // classes for primitives cannot be redefined
   219   // classes for primitives cannot be redefined
   160   if (java_lang_Class::is_primitive(klass_mirror)) {
   220   if (java_lang_Class::is_primitive(klass_mirror)) {
   161     return false;
   221     return false;
   162   }
   222   }
   163   klassOop the_class_oop = java_lang_Class::as_klassOop(klass_mirror);
   223   Klass* the_class_oop = java_lang_Class::as_Klass(klass_mirror);
   164   // classes for arrays cannot be redefined
   224   // classes for arrays cannot be redefined
   165   if (the_class_oop == NULL || !Klass::cast(the_class_oop)->oop_is_instance()) {
   225   if (the_class_oop == NULL || !Klass::cast(the_class_oop)->oop_is_instance()) {
   166     return false;
   226     return false;
   167   }
   227   }
   168   return true;
   228   return true;
   213     // these are direct CP entries so they can be directly appended,
   273     // these are direct CP entries so they can be directly appended,
   214     // but double and long take two constant pool entries
   274     // but double and long take two constant pool entries
   215     case JVM_CONSTANT_Double:  // fall through
   275     case JVM_CONSTANT_Double:  // fall through
   216     case JVM_CONSTANT_Long:
   276     case JVM_CONSTANT_Long:
   217     {
   277     {
   218       constantPoolOopDesc::copy_entry_to(scratch_cp, scratch_i, *merge_cp_p, *merge_cp_length_p,
   278       ConstantPool::copy_entry_to(scratch_cp, scratch_i, *merge_cp_p, *merge_cp_length_p,
   219         THREAD);
   279         THREAD);
   220 
   280 
   221       if (scratch_i != *merge_cp_length_p) {
   281       if (scratch_i != *merge_cp_length_p) {
   222         // The new entry in *merge_cp_p is at a different index than
   282         // The new entry in *merge_cp_p is at a different index than
   223         // the new entry in scratch_cp so we need to map the index values.
   283         // the new entry in scratch_cp so we need to map the index values.
   230     case JVM_CONSTANT_Float:   // fall through
   290     case JVM_CONSTANT_Float:   // fall through
   231     case JVM_CONSTANT_Integer: // fall through
   291     case JVM_CONSTANT_Integer: // fall through
   232     case JVM_CONSTANT_Utf8:    // fall through
   292     case JVM_CONSTANT_Utf8:    // fall through
   233 
   293 
   234     // This was an indirect CP entry, but it has been changed into
   294     // This was an indirect CP entry, but it has been changed into
   235     // an interned string so this entry can be directly appended.
   295     // Symbol*s so this entry can be directly appended.
   236     case JVM_CONSTANT_String:      // fall through
   296     case JVM_CONSTANT_String:      // fall through
   237 
   297 
   238     // These were indirect CP entries, but they have been changed into
   298     // These were indirect CP entries, but they have been changed into
   239     // Symbol*s so these entries can be directly appended.
   299     // Symbol*s so these entries can be directly appended.
   240     case JVM_CONSTANT_UnresolvedClass:  // fall through
   300     case JVM_CONSTANT_UnresolvedClass:  // fall through
   241     case JVM_CONSTANT_UnresolvedString:
       
   242     {
   301     {
   243       constantPoolOopDesc::copy_entry_to(scratch_cp, scratch_i, *merge_cp_p, *merge_cp_length_p,
   302       ConstantPool::copy_entry_to(scratch_cp, scratch_i, *merge_cp_p, *merge_cp_length_p,
   244         THREAD);
   303         THREAD);
   245 
   304 
   246       if (scratch_i != *merge_cp_length_p) {
   305       if (scratch_i != *merge_cp_length_p) {
   247         // The new entry in *merge_cp_p is at a different index than
   306         // The new entry in *merge_cp_p is at a different index than
   248         // the new entry in scratch_cp so we need to map the index values.
   307         // the new entry in scratch_cp so we need to map the index values.
   465     // Invalid is used as the tag for the second constant pool entry
   524     // Invalid is used as the tag for the second constant pool entry
   466     // occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should
   525     // occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should
   467     // not be seen by itself.
   526     // not be seen by itself.
   468     case JVM_CONSTANT_Invalid: // fall through
   527     case JVM_CONSTANT_Invalid: // fall through
   469 
   528 
   470     // At this stage, String or UnresolvedString could be here, but not
   529     // At this stage, String could be here, but not StringIndex
   471     // StringIndex
       
   472     case JVM_CONSTANT_StringIndex: // fall through
   530     case JVM_CONSTANT_StringIndex: // fall through
   473 
   531 
   474     // At this stage JVM_CONSTANT_UnresolvedClassInError should not be
   532     // At this stage JVM_CONSTANT_UnresolvedClassInError should not be
   475     // here
   533     // here
   476     case JVM_CONSTANT_UnresolvedClassInError: // fall through
   534     case JVM_CONSTANT_UnresolvedClassInError: // fall through
   483     } break;
   541     } break;
   484   } // end switch tag value
   542   } // end switch tag value
   485 } // end append_entry()
   543 } // end append_entry()
   486 
   544 
   487 
   545 
   488 void VM_RedefineClasses::swap_all_method_annotations(int i, int j, instanceKlassHandle scratch_class) {
   546 void VM_RedefineClasses::swap_all_method_annotations(int i, int j, instanceKlassHandle scratch_class, TRAPS) {
   489   typeArrayOop save;
   547   AnnotationArray* save;
   490 
   548 
   491   save = scratch_class->get_method_annotations_of(i);
   549   Annotations* sca = scratch_class->annotations();
   492   scratch_class->set_method_annotations_of(i, scratch_class->get_method_annotations_of(j));
   550   if (sca == NULL) return;
   493   scratch_class->set_method_annotations_of(j, save);
   551 
   494 
   552   save = sca->get_method_annotations_of(i);
   495   save = scratch_class->get_method_parameter_annotations_of(i);
   553   sca->set_method_annotations_of(scratch_class, i, sca->get_method_annotations_of(j), CHECK);
   496   scratch_class->set_method_parameter_annotations_of(i, scratch_class->get_method_parameter_annotations_of(j));
   554   sca->set_method_annotations_of(scratch_class, j, save, CHECK);
   497   scratch_class->set_method_parameter_annotations_of(j, save);
   555 
   498 
   556   save = sca->get_method_parameter_annotations_of(i);
   499   save = scratch_class->get_method_default_annotations_of(i);
   557   sca->set_method_parameter_annotations_of(scratch_class, i, sca->get_method_parameter_annotations_of(j), CHECK);
   500   scratch_class->set_method_default_annotations_of(i, scratch_class->get_method_default_annotations_of(j));
   558   sca->set_method_parameter_annotations_of(scratch_class, j, save, CHECK);
   501   scratch_class->set_method_default_annotations_of(j, save);
   559 
       
   560   save = sca->get_method_default_annotations_of(i);
       
   561   sca->set_method_default_annotations_of(scratch_class, i, sca->get_method_default_annotations_of(j), CHECK);
       
   562   sca->set_method_default_annotations_of(scratch_class, j, save, CHECK);
   502 }
   563 }
   503 
   564 
   504 
   565 
   505 jvmtiError VM_RedefineClasses::compare_and_normalize_class_versions(
   566 jvmtiError VM_RedefineClasses::compare_and_normalize_class_versions(
   506              instanceKlassHandle the_class,
   567              instanceKlassHandle the_class,
   522   // interfaces are the same, i.e. the order of declaration (which, however, if changed in the
   583   // interfaces are the same, i.e. the order of declaration (which, however, if changed in the
   523   // .java file, also changes in .class file) should not matter. However, comparing sets is
   584   // .java file, also changes in .class file) should not matter. However, comparing sets is
   524   // technically a bit more difficult, and, more importantly, I am not sure at present that the
   585   // technically a bit more difficult, and, more importantly, I am not sure at present that the
   525   // order of interfaces does not matter on the implementation level, i.e. that the VM does not
   586   // order of interfaces does not matter on the implementation level, i.e. that the VM does not
   526   // rely on it somewhere.
   587   // rely on it somewhere.
   527   objArrayOop k_interfaces = the_class->local_interfaces();
   588   Array<Klass*>* k_interfaces = the_class->local_interfaces();
   528   objArrayOop k_new_interfaces = scratch_class->local_interfaces();
   589   Array<Klass*>* k_new_interfaces = scratch_class->local_interfaces();
   529   int n_intfs = k_interfaces->length();
   590   int n_intfs = k_interfaces->length();
   530   if (n_intfs != k_new_interfaces->length()) {
   591   if (n_intfs != k_new_interfaces->length()) {
   531     return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED;
   592     return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED;
   532   }
   593   }
   533   for (i = 0; i < n_intfs; i++) {
   594   for (i = 0; i < n_intfs; i++) {
   534     if (Klass::cast((klassOop) k_interfaces->obj_at(i))->name() !=
   595     if (Klass::cast(k_interfaces->at(i))->name() !=
   535         Klass::cast((klassOop) k_new_interfaces->obj_at(i))->name()) {
   596         Klass::cast(k_new_interfaces->at(i))->name()) {
   536       return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED;
   597       return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED;
   537     }
   598     }
   538   }
   599   }
   539 
   600 
   540   // Check whether class is in the error init state.
   601   // Check whether class is in the error init state.
   589   // signature.  In order to determine what fate befell the methods,
   650   // signature.  In order to determine what fate befell the methods,
   590   // this code places the overloaded new methods that have matching
   651   // this code places the overloaded new methods that have matching
   591   // old methods in the same order as the old methods and places
   652   // old methods in the same order as the old methods and places
   592   // new overloaded methods at the end of overloaded methods of
   653   // new overloaded methods at the end of overloaded methods of
   593   // that name. The code for this order normalization is adapted
   654   // that name. The code for this order normalization is adapted
   594   // from the algorithm used in instanceKlass::find_method().
   655   // from the algorithm used in InstanceKlass::find_method().
   595   // Since we are swapping out of order entries as we find them,
   656   // Since we are swapping out of order entries as we find them,
   596   // we only have to search forward through the overloaded methods.
   657   // we only have to search forward through the overloaded methods.
   597   // Methods which are added and have the same name as an existing
   658   // Methods which are added and have the same name as an existing
   598   // method (but different signature) will be put at the end of
   659   // method (but different signature) will be put at the end of
   599   // the methods with that name, and the name mismatch code will
   660   // the methods with that name, and the name mismatch code will
   600   // handle them.
   661   // handle them.
   601   objArrayHandle k_old_methods(the_class->methods());
   662   Array<Method*>* k_old_methods(the_class->methods());
   602   objArrayHandle k_new_methods(scratch_class->methods());
   663   Array<Method*>* k_new_methods(scratch_class->methods());
   603   int n_old_methods = k_old_methods->length();
   664   int n_old_methods = k_old_methods->length();
   604   int n_new_methods = k_new_methods->length();
   665   int n_new_methods = k_new_methods->length();
       
   666   Thread* thread = Thread::current();
   605 
   667 
   606   int ni = 0;
   668   int ni = 0;
   607   int oi = 0;
   669   int oi = 0;
   608   while (true) {
   670   while (true) {
   609     methodOop k_old_method;
   671     Method* k_old_method;
   610     methodOop k_new_method;
   672     Method* k_new_method;
   611     enum { matched, added, deleted, undetermined } method_was = undetermined;
   673     enum { matched, added, deleted, undetermined } method_was = undetermined;
   612 
   674 
   613     if (oi >= n_old_methods) {
   675     if (oi >= n_old_methods) {
   614       if (ni >= n_new_methods) {
   676       if (ni >= n_new_methods) {
   615         break; // we've looked at everything, done
   677         break; // we've looked at everything, done
   616       }
   678       }
   617       // New method at the end
   679       // New method at the end
   618       k_new_method = (methodOop) k_new_methods->obj_at(ni);
   680       k_new_method = k_new_methods->at(ni);
   619       method_was = added;
   681       method_was = added;
   620     } else if (ni >= n_new_methods) {
   682     } else if (ni >= n_new_methods) {
   621       // Old method, at the end, is deleted
   683       // Old method, at the end, is deleted
   622       k_old_method = (methodOop) k_old_methods->obj_at(oi);
   684       k_old_method = k_old_methods->at(oi);
   623       method_was = deleted;
   685       method_was = deleted;
   624     } else {
   686     } else {
   625       // There are more methods in both the old and new lists
   687       // There are more methods in both the old and new lists
   626       k_old_method = (methodOop) k_old_methods->obj_at(oi);
   688       k_old_method = k_old_methods->at(oi);
   627       k_new_method = (methodOop) k_new_methods->obj_at(ni);
   689       k_new_method = k_new_methods->at(ni);
   628       if (k_old_method->name() != k_new_method->name()) {
   690       if (k_old_method->name() != k_new_method->name()) {
   629         // Methods are sorted by method name, so a mismatch means added
   691         // Methods are sorted by method name, so a mismatch means added
   630         // or deleted
   692         // or deleted
   631         if (k_old_method->name()->fast_compare(k_new_method->name()) > 0) {
   693         if (k_old_method->name()->fast_compare(k_new_method->name()) > 0) {
   632           method_was = added;
   694           method_was = added;
   639       } else {
   701       } else {
   640         // The name matches, but the signature doesn't, which means we have to
   702         // The name matches, but the signature doesn't, which means we have to
   641         // search forward through the new overloaded methods.
   703         // search forward through the new overloaded methods.
   642         int nj;  // outside the loop for post-loop check
   704         int nj;  // outside the loop for post-loop check
   643         for (nj = ni + 1; nj < n_new_methods; nj++) {
   705         for (nj = ni + 1; nj < n_new_methods; nj++) {
   644           methodOop m = (methodOop)k_new_methods->obj_at(nj);
   706           Method* m = k_new_methods->at(nj);
   645           if (k_old_method->name() != m->name()) {
   707           if (k_old_method->name() != m->name()) {
   646             // reached another method name so no more overloaded methods
   708             // reached another method name so no more overloaded methods
   647             method_was = deleted;
   709             method_was = deleted;
   648             break;
   710             break;
   649           }
   711           }
   650           if (k_old_method->signature() == m->signature()) {
   712           if (k_old_method->signature() == m->signature()) {
   651             // found a match so swap the methods
   713             // found a match so swap the methods
   652             k_new_methods->obj_at_put(ni, m);
   714             k_new_methods->at_put(ni, m);
   653             k_new_methods->obj_at_put(nj, k_new_method);
   715             k_new_methods->at_put(nj, k_new_method);
   654             k_new_method = m;
   716             k_new_method = m;
   655             method_was = matched;
   717             method_was = matched;
   656             break;
   718             break;
   657           }
   719           }
   658         }
   720         }
   674       }
   736       }
   675       {
   737       {
   676         u2 new_num = k_new_method->method_idnum();
   738         u2 new_num = k_new_method->method_idnum();
   677         u2 old_num = k_old_method->method_idnum();
   739         u2 old_num = k_old_method->method_idnum();
   678         if (new_num != old_num) {
   740         if (new_num != old_num) {
   679           methodOop idnum_owner = scratch_class->method_with_idnum(old_num);
   741           Method* idnum_owner = scratch_class->method_with_idnum(old_num);
   680           if (idnum_owner != NULL) {
   742           if (idnum_owner != NULL) {
   681             // There is already a method assigned this idnum -- switch them
   743             // There is already a method assigned this idnum -- switch them
   682             idnum_owner->set_method_idnum(new_num);
   744             idnum_owner->set_method_idnum(new_num);
   683           }
   745           }
   684           k_new_method->set_method_idnum(old_num);
   746           k_new_method->set_method_idnum(old_num);
   685           swap_all_method_annotations(old_num, new_num, scratch_class);
   747           swap_all_method_annotations(old_num, new_num, scratch_class, thread);
       
   748            if (thread->has_pending_exception()) {
       
   749              return JVMTI_ERROR_OUT_OF_MEMORY;
       
   750            }
   686         }
   751         }
   687       }
   752       }
   688       RC_TRACE(0x00008000, ("Method matched: new: %s [%d] == old: %s [%d]",
   753       RC_TRACE(0x00008000, ("Method matched: new: %s [%d] == old: %s [%d]",
   689                             k_new_method->name_and_sig_as_C_string(), ni,
   754                             k_new_method->name_and_sig_as_C_string(), ni,
   690                             k_old_method->name_and_sig_as_C_string(), oi));
   755                             k_old_method->name_and_sig_as_C_string(), oi));
   702         // new methods must be private
   767         // new methods must be private
   703         return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_METHOD_ADDED;
   768         return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_METHOD_ADDED;
   704       }
   769       }
   705       {
   770       {
   706         u2 num = the_class->next_method_idnum();
   771         u2 num = the_class->next_method_idnum();
   707         if (num == constMethodOopDesc::UNSET_IDNUM) {
   772         if (num == ConstMethod::UNSET_IDNUM) {
   708           // cannot add any more methods
   773           // cannot add any more methods
   709           return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_METHOD_ADDED;
   774           return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_METHOD_ADDED;
   710         }
   775         }
   711         u2 new_num = k_new_method->method_idnum();
   776         u2 new_num = k_new_method->method_idnum();
   712         methodOop idnum_owner = scratch_class->method_with_idnum(num);
   777         Method* idnum_owner = scratch_class->method_with_idnum(num);
   713         if (idnum_owner != NULL) {
   778         if (idnum_owner != NULL) {
   714           // There is already a method assigned this idnum -- switch them
   779           // There is already a method assigned this idnum -- switch them
   715           idnum_owner->set_method_idnum(new_num);
   780           idnum_owner->set_method_idnum(new_num);
   716         }
   781         }
   717         k_new_method->set_method_idnum(num);
   782         k_new_method->set_method_idnum(num);
   718         swap_all_method_annotations(new_num, num, scratch_class);
   783         swap_all_method_annotations(new_num, num, scratch_class, thread);
       
   784         if (thread->has_pending_exception()) {
       
   785           return JVMTI_ERROR_OUT_OF_MEMORY;
       
   786         }
   719       }
   787       }
   720       RC_TRACE(0x00008000, ("Method added: new: %s [%d]",
   788       RC_TRACE(0x00008000, ("Method added: new: %s [%d]",
   721                             k_new_method->name_and_sig_as_C_string(), ni));
   789                             k_new_method->name_and_sig_as_C_string(), ni));
   722       ++ni; // advance to next new method
   790       ++ni; // advance to next new method
   723       break;
   791       break;
   797 
   865 
   798   return true;  // made it through the gauntlet; this is our special case
   866   return true;  // made it through the gauntlet; this is our special case
   799 } // end is_unresolved_class_mismatch()
   867 } // end is_unresolved_class_mismatch()
   800 
   868 
   801 
   869 
   802 // Returns true if the current mismatch is due to a resolved/unresolved
       
   803 // string pair. Otherwise, returns false.
       
   804 bool VM_RedefineClasses::is_unresolved_string_mismatch(constantPoolHandle cp1,
       
   805        int index1, constantPoolHandle cp2, int index2) {
       
   806 
       
   807   jbyte t1 = cp1->tag_at(index1).value();
       
   808   if (t1 != JVM_CONSTANT_String && t1 != JVM_CONSTANT_UnresolvedString) {
       
   809     return false;  // wrong entry type; not our special case
       
   810   }
       
   811 
       
   812   jbyte t2 = cp2->tag_at(index2).value();
       
   813   if (t2 != JVM_CONSTANT_String && t2 != JVM_CONSTANT_UnresolvedString) {
       
   814     return false;  // wrong entry type; not our special case
       
   815   }
       
   816 
       
   817   if (t1 == t2) {
       
   818     return false;  // not a mismatch; not our special case
       
   819   }
       
   820 
       
   821   char *s1 = cp1->string_at_noresolve(index1);
       
   822   char *s2 = cp2->string_at_noresolve(index2);
       
   823   if (strcmp(s1, s2) != 0) {
       
   824     return false;  // strings don't match; not our special case
       
   825   }
       
   826 
       
   827   return true;  // made it through the gauntlet; this is our special case
       
   828 } // end is_unresolved_string_mismatch()
       
   829 
       
   830 
       
   831 jvmtiError VM_RedefineClasses::load_new_class_versions(TRAPS) {
   870 jvmtiError VM_RedefineClasses::load_new_class_versions(TRAPS) {
       
   871 
   832   // For consistency allocate memory using os::malloc wrapper.
   872   // For consistency allocate memory using os::malloc wrapper.
   833   _scratch_classes = (instanceKlassHandle *)
   873   _scratch_classes = (Klass**)
   834     os::malloc(sizeof(instanceKlassHandle) * _class_count, mtInternal);
   874     os::malloc(sizeof(Klass*) * _class_count, mtClass);
   835   if (_scratch_classes == NULL) {
   875   if (_scratch_classes == NULL) {
   836     return JVMTI_ERROR_OUT_OF_MEMORY;
   876     return JVMTI_ERROR_OUT_OF_MEMORY;
       
   877   }
       
   878   // Zero initialize the _scratch_classes array.
       
   879   for (int i = 0; i < _class_count; i++) {
       
   880     _scratch_classes[i] = NULL;
   837   }
   881   }
   838 
   882 
   839   ResourceMark rm(THREAD);
   883   ResourceMark rm(THREAD);
   840 
   884 
   841   JvmtiThreadState *state = JvmtiThreadState::state_for(JavaThread::current());
   885   JvmtiThreadState *state = JvmtiThreadState::state_for(JavaThread::current());
   842   // state can only be NULL if the current thread is exiting which
   886   // state can only be NULL if the current thread is exiting which
   843   // should not happen since we're trying to do a RedefineClasses
   887   // should not happen since we're trying to do a RedefineClasses
   844   guarantee(state != NULL, "exiting thread calling load_new_class_versions");
   888   guarantee(state != NULL, "exiting thread calling load_new_class_versions");
   845   for (int i = 0; i < _class_count; i++) {
   889   for (int i = 0; i < _class_count; i++) {
       
   890     // Create HandleMark so that any handles created while loading new class
       
   891     // versions are deleted. Constant pools are deallocated while merging
       
   892     // constant pools
       
   893     HandleMark hm(THREAD);
       
   894 
   846     oop mirror = JNIHandles::resolve_non_null(_class_defs[i].klass);
   895     oop mirror = JNIHandles::resolve_non_null(_class_defs[i].klass);
   847     // classes for primitives cannot be redefined
   896     // classes for primitives cannot be redefined
   848     if (!is_modifiable_class(mirror)) {
   897     if (!is_modifiable_class(mirror)) {
   849       return JVMTI_ERROR_UNMODIFIABLE_CLASS;
   898       return JVMTI_ERROR_UNMODIFIABLE_CLASS;
   850     }
   899     }
   851     klassOop the_class_oop = java_lang_Class::as_klassOop(mirror);
   900     Klass* the_class_oop = java_lang_Class::as_Klass(mirror);
   852     instanceKlassHandle the_class = instanceKlassHandle(THREAD, the_class_oop);
   901     instanceKlassHandle the_class = instanceKlassHandle(THREAD, the_class_oop);
   853     Symbol*  the_class_sym = the_class->name();
   902     Symbol*  the_class_sym = the_class->name();
   854 
   903 
   855     // RC_TRACE_WITH_THREAD macro has an embedded ResourceMark
   904     // RC_TRACE_WITH_THREAD macro has an embedded ResourceMark
   856     RC_TRACE_WITH_THREAD(0x00000001, THREAD,
   905     RC_TRACE_WITH_THREAD(0x00000001, THREAD,
   867     // Set redefined class handle in JvmtiThreadState class.
   916     // Set redefined class handle in JvmtiThreadState class.
   868     // This redefined class is sent to agent event handler for class file
   917     // This redefined class is sent to agent event handler for class file
   869     // load hook event.
   918     // load hook event.
   870     state->set_class_being_redefined(&the_class, _class_load_kind);
   919     state->set_class_being_redefined(&the_class, _class_load_kind);
   871 
   920 
   872     klassOop k = SystemDictionary::parse_stream(the_class_sym,
   921     Klass* k = SystemDictionary::parse_stream(the_class_sym,
   873                                                 the_class_loader,
   922                                                 the_class_loader,
   874                                                 protection_domain,
   923                                                 protection_domain,
   875                                                 &st,
   924                                                 &st,
   876                                                 THREAD);
   925                                                 THREAD);
   877     // Clear class_being_redefined just to be sure.
   926     // Clear class_being_redefined just to be sure.
   879 
   928 
   880     // TODO: if this is retransform, and nothing changed we can skip it
   929     // TODO: if this is retransform, and nothing changed we can skip it
   881 
   930 
   882     instanceKlassHandle scratch_class (THREAD, k);
   931     instanceKlassHandle scratch_class (THREAD, k);
   883 
   932 
       
   933     // Need to clean up allocated InstanceKlass if there's an error so assign
       
   934     // the result here. Caller deallocates all the scratch classes in case of
       
   935     // an error.
       
   936     _scratch_classes[i] = k;
       
   937 
   884     if (HAS_PENDING_EXCEPTION) {
   938     if (HAS_PENDING_EXCEPTION) {
   885       Symbol* ex_name = PENDING_EXCEPTION->klass()->klass_part()->name();
   939       Symbol* ex_name = PENDING_EXCEPTION->klass()->name();
   886       // RC_TRACE_WITH_THREAD macro has an embedded ResourceMark
   940       // RC_TRACE_WITH_THREAD macro has an embedded ResourceMark
   887       RC_TRACE_WITH_THREAD(0x00000002, THREAD, ("parse_stream exception: '%s'",
   941       RC_TRACE_WITH_THREAD(0x00000002, THREAD, ("parse_stream exception: '%s'",
   888         ex_name->as_C_string()));
   942         ex_name->as_C_string()));
   889       CLEAR_PENDING_EXCEPTION;
   943       CLEAR_PENDING_EXCEPTION;
   890 
   944 
   906 
   960 
   907     // Ensure class is linked before redefine
   961     // Ensure class is linked before redefine
   908     if (!the_class->is_linked()) {
   962     if (!the_class->is_linked()) {
   909       the_class->link_class(THREAD);
   963       the_class->link_class(THREAD);
   910       if (HAS_PENDING_EXCEPTION) {
   964       if (HAS_PENDING_EXCEPTION) {
   911         Symbol* ex_name = PENDING_EXCEPTION->klass()->klass_part()->name();
   965         Symbol* ex_name = PENDING_EXCEPTION->klass()->name();
   912         // RC_TRACE_WITH_THREAD macro has an embedded ResourceMark
   966         // RC_TRACE_WITH_THREAD macro has an embedded ResourceMark
   913         RC_TRACE_WITH_THREAD(0x00000002, THREAD, ("link_class exception: '%s'",
   967         RC_TRACE_WITH_THREAD(0x00000002, THREAD, ("link_class exception: '%s'",
   914           ex_name->as_C_string()));
   968           ex_name->as_C_string()));
   915         CLEAR_PENDING_EXCEPTION;
   969         CLEAR_PENDING_EXCEPTION;
   916         if (ex_name == vmSymbols::java_lang_OutOfMemoryError()) {
   970         if (ex_name == vmSymbols::java_lang_OutOfMemoryError()) {
   944       Verifier::verify(
   998       Verifier::verify(
   945         scratch_class, Verifier::ThrowException, true, THREAD);
   999         scratch_class, Verifier::ThrowException, true, THREAD);
   946     }
  1000     }
   947 
  1001 
   948     if (HAS_PENDING_EXCEPTION) {
  1002     if (HAS_PENDING_EXCEPTION) {
   949       Symbol* ex_name = PENDING_EXCEPTION->klass()->klass_part()->name();
  1003       Symbol* ex_name = PENDING_EXCEPTION->klass()->name();
   950       // RC_TRACE_WITH_THREAD macro has an embedded ResourceMark
  1004       // RC_TRACE_WITH_THREAD macro has an embedded ResourceMark
   951       RC_TRACE_WITH_THREAD(0x00000002, THREAD,
  1005       RC_TRACE_WITH_THREAD(0x00000002, THREAD,
   952         ("verify_byte_codes exception: '%s'", ex_name->as_C_string()));
  1006         ("verify_byte_codes exception: '%s'", ex_name->as_C_string()));
   953       CLEAR_PENDING_EXCEPTION;
  1007       CLEAR_PENDING_EXCEPTION;
   954       if (ex_name == vmSymbols::java_lang_OutOfMemoryError()) {
  1008       if (ex_name == vmSymbols::java_lang_OutOfMemoryError()) {
   970         RedefineVerifyMark rvm(&the_class, &scratch_class, state);
  1024         RedefineVerifyMark rvm(&the_class, &scratch_class, state);
   971         Verifier::verify(scratch_class, Verifier::ThrowException, true, THREAD);
  1025         Verifier::verify(scratch_class, Verifier::ThrowException, true, THREAD);
   972       }
  1026       }
   973 
  1027 
   974       if (HAS_PENDING_EXCEPTION) {
  1028       if (HAS_PENDING_EXCEPTION) {
   975         Symbol* ex_name = PENDING_EXCEPTION->klass()->klass_part()->name();
  1029         Symbol* ex_name = PENDING_EXCEPTION->klass()->name();
   976         // RC_TRACE_WITH_THREAD macro has an embedded ResourceMark
  1030         // RC_TRACE_WITH_THREAD macro has an embedded ResourceMark
   977         RC_TRACE_WITH_THREAD(0x00000002, THREAD,
  1031         RC_TRACE_WITH_THREAD(0x00000002, THREAD,
   978           ("verify_byte_codes post merge-CP exception: '%s'",
  1032           ("verify_byte_codes post merge-CP exception: '%s'",
   979           ex_name->as_C_string()));
  1033           ex_name->as_C_string()));
   980         CLEAR_PENDING_EXCEPTION;
  1034         CLEAR_PENDING_EXCEPTION;
   990     Rewriter::rewrite(scratch_class, THREAD);
  1044     Rewriter::rewrite(scratch_class, THREAD);
   991     if (!HAS_PENDING_EXCEPTION) {
  1045     if (!HAS_PENDING_EXCEPTION) {
   992       Rewriter::relocate_and_link(scratch_class, THREAD);
  1046       Rewriter::relocate_and_link(scratch_class, THREAD);
   993     }
  1047     }
   994     if (HAS_PENDING_EXCEPTION) {
  1048     if (HAS_PENDING_EXCEPTION) {
   995       Symbol* ex_name = PENDING_EXCEPTION->klass()->klass_part()->name();
  1049       Symbol* ex_name = PENDING_EXCEPTION->klass()->name();
   996       CLEAR_PENDING_EXCEPTION;
  1050       CLEAR_PENDING_EXCEPTION;
   997       if (ex_name == vmSymbols::java_lang_OutOfMemoryError()) {
  1051       if (ex_name == vmSymbols::java_lang_OutOfMemoryError()) {
   998         return JVMTI_ERROR_OUT_OF_MEMORY;
  1052         return JVMTI_ERROR_OUT_OF_MEMORY;
   999       } else {
  1053       } else {
  1000         return JVMTI_ERROR_INTERNAL;
  1054         return JVMTI_ERROR_INTERNAL;
  1001       }
  1055       }
  1002     }
  1056     }
  1003 
       
  1004     _scratch_classes[i] = scratch_class;
       
  1005 
  1057 
  1006     // RC_TRACE_WITH_THREAD macro has an embedded ResourceMark
  1058     // RC_TRACE_WITH_THREAD macro has an embedded ResourceMark
  1007     RC_TRACE_WITH_THREAD(0x00000001, THREAD,
  1059     RC_TRACE_WITH_THREAD(0x00000001, THREAD,
  1008       ("loaded name=%s (avail_mem=" UINT64_FORMAT "K)",
  1060       ("loaded name=%s (avail_mem=" UINT64_FORMAT "K)",
  1009       the_class->external_name(), os::available_memory() >> 10));
  1061       the_class->external_name(), os::available_memory() >> 10));
  1045 bool VM_RedefineClasses::merge_constant_pools(constantPoolHandle old_cp,
  1097 bool VM_RedefineClasses::merge_constant_pools(constantPoolHandle old_cp,
  1046        constantPoolHandle scratch_cp, constantPoolHandle *merge_cp_p,
  1098        constantPoolHandle scratch_cp, constantPoolHandle *merge_cp_p,
  1047        int *merge_cp_length_p, TRAPS) {
  1099        int *merge_cp_length_p, TRAPS) {
  1048 
  1100 
  1049   if (merge_cp_p == NULL) {
  1101   if (merge_cp_p == NULL) {
  1050     assert(false, "caller must provide scatch constantPool");
  1102     assert(false, "caller must provide scratch constantPool");
  1051     return false; // robustness
  1103     return false; // robustness
  1052   }
  1104   }
  1053   if (merge_cp_length_p == NULL) {
  1105   if (merge_cp_length_p == NULL) {
  1054     assert(false, "caller must provide scatch CP length");
  1106     assert(false, "caller must provide scratch CP length");
  1055     return false; // robustness
  1107     return false; // robustness
  1056   }
  1108   }
  1057   // Worst case we need old_cp->length() + scratch_cp()->length(),
  1109   // Worst case we need old_cp->length() + scratch_cp()->length(),
  1058   // but the caller might be smart so make sure we have at least
  1110   // but the caller might be smart so make sure we have at least
  1059   // the minimum.
  1111   // the minimum.
  1068 
  1120 
  1069   {
  1121   {
  1070     // Pass 0:
  1122     // Pass 0:
  1071     // The old_cp is copied to *merge_cp_p; this means that any code
  1123     // The old_cp is copied to *merge_cp_p; this means that any code
  1072     // using old_cp does not have to change. This work looks like a
  1124     // using old_cp does not have to change. This work looks like a
  1073     // perfect fit for constantPoolOop::copy_cp_to(), but we need to
  1125     // perfect fit for ConstantPool*::copy_cp_to(), but we need to
  1074     // handle one special case:
  1126     // handle one special case:
  1075     // - revert JVM_CONSTANT_Class to JVM_CONSTANT_UnresolvedClass
  1127     // - revert JVM_CONSTANT_Class to JVM_CONSTANT_UnresolvedClass
  1076     // This will make verification happy.
  1128     // This will make verification happy.
  1077 
  1129 
  1078     int old_i;  // index into old_cp
  1130     int old_i;  // index into old_cp
  1093 
  1145 
  1094       case JVM_CONSTANT_Double:
  1146       case JVM_CONSTANT_Double:
  1095       case JVM_CONSTANT_Long:
  1147       case JVM_CONSTANT_Long:
  1096         // just copy the entry to *merge_cp_p, but double and long take
  1148         // just copy the entry to *merge_cp_p, but double and long take
  1097         // two constant pool entries
  1149         // two constant pool entries
  1098         constantPoolOopDesc::copy_entry_to(old_cp, old_i, *merge_cp_p, old_i, CHECK_0);
  1150         ConstantPool::copy_entry_to(old_cp, old_i, *merge_cp_p, old_i, CHECK_0);
  1099         old_i++;
  1151         old_i++;
  1100         break;
  1152         break;
  1101 
  1153 
  1102       default:
  1154       default:
  1103         // just copy the entry to *merge_cp_p
  1155         // just copy the entry to *merge_cp_p
  1104         constantPoolOopDesc::copy_entry_to(old_cp, old_i, *merge_cp_p, old_i, CHECK_0);
  1156         ConstantPool::copy_entry_to(old_cp, old_i, *merge_cp_p, old_i, CHECK_0);
  1105         break;
  1157         break;
  1106       }
  1158       }
  1107     } // end for each old_cp entry
  1159     } // end for each old_cp entry
  1108 
  1160 
  1109     // We don't need to sanity check that *merge_cp_length_p is within
  1161     // We don't need to sanity check that *merge_cp_length_p is within
  1149         // resolved versus unresolved class entry at the same index
  1201         // resolved versus unresolved class entry at the same index
  1150         // with the same string value. Since Pass 0 reverted any
  1202         // with the same string value. Since Pass 0 reverted any
  1151         // class entries to unresolved class entries in *merge_cp_p,
  1203         // class entries to unresolved class entries in *merge_cp_p,
  1152         // we go with the unresolved class entry.
  1204         // we go with the unresolved class entry.
  1153         continue;
  1205         continue;
  1154       } else if (is_unresolved_string_mismatch(scratch_cp, scratch_i,
       
  1155                                                *merge_cp_p, scratch_i)) {
       
  1156         // The mismatch in compare_entry_to() above is because of a
       
  1157         // resolved versus unresolved string entry at the same index
       
  1158         // with the same string value. We can live with whichever
       
  1159         // happens to be at scratch_i in *merge_cp_p.
       
  1160         continue;
       
  1161       }
  1206       }
  1162 
  1207 
  1163       int found_i = scratch_cp->find_matching_entry(scratch_i, *merge_cp_p,
  1208       int found_i = scratch_cp->find_matching_entry(scratch_i, *merge_cp_p,
  1164         CHECK_0);
  1209         CHECK_0);
  1165       if (found_i != 0) {
  1210       if (found_i != 0) {
  1231 
  1276 
  1232   return true;
  1277   return true;
  1233 } // end merge_constant_pools()
  1278 } // end merge_constant_pools()
  1234 
  1279 
  1235 
  1280 
       
  1281 // Scoped object to clean up the constant pool(s) created for merging
       
  1282 class MergeCPCleaner {
       
  1283   ClassLoaderData*   _loader_data;
       
  1284   ConstantPool*      _cp;
       
  1285   ConstantPool*      _scratch_cp;
       
  1286  public:
       
  1287   MergeCPCleaner(ClassLoaderData* loader_data, ConstantPool* merge_cp) :
       
  1288                  _loader_data(loader_data), _cp(merge_cp), _scratch_cp(NULL) {}
       
  1289   ~MergeCPCleaner() {
       
  1290     _loader_data->add_to_deallocate_list(_cp);
       
  1291     if (_scratch_cp != NULL) {
       
  1292       _loader_data->add_to_deallocate_list(_scratch_cp);
       
  1293     }
       
  1294   }
       
  1295   void add_scratch_cp(ConstantPool* scratch_cp) { _scratch_cp = scratch_cp; }
       
  1296 };
       
  1297 
  1236 // Merge constant pools between the_class and scratch_class and
  1298 // Merge constant pools between the_class and scratch_class and
  1237 // potentially rewrite bytecodes in scratch_class to use the merged
  1299 // potentially rewrite bytecodes in scratch_class to use the merged
  1238 // constant pool.
  1300 // constant pool.
  1239 jvmtiError VM_RedefineClasses::merge_cp_and_rewrite(
  1301 jvmtiError VM_RedefineClasses::merge_cp_and_rewrite(
  1240              instanceKlassHandle the_class, instanceKlassHandle scratch_class,
  1302              instanceKlassHandle the_class, instanceKlassHandle scratch_class,
  1241              TRAPS) {
  1303              TRAPS) {
  1242   // worst case merged constant pool length is old and new combined
  1304   // worst case merged constant pool length is old and new combined
  1243   int merge_cp_length = the_class->constants()->length()
  1305   int merge_cp_length = the_class->constants()->length()
  1244         + scratch_class->constants()->length();
  1306         + scratch_class->constants()->length();
  1245 
  1307 
  1246   constantPoolHandle old_cp(THREAD, the_class->constants());
       
  1247   constantPoolHandle scratch_cp(THREAD, scratch_class->constants());
       
  1248 
       
  1249   // Constant pools are not easily reused so we allocate a new one
  1308   // Constant pools are not easily reused so we allocate a new one
  1250   // each time.
  1309   // each time.
  1251   // merge_cp is created unsafe for concurrent GC processing.  It
  1310   // merge_cp is created unsafe for concurrent GC processing.  It
  1252   // should be marked safe before discarding it. Even though
  1311   // should be marked safe before discarding it. Even though
  1253   // garbage,  if it crosses a card boundary, it may be scanned
  1312   // garbage,  if it crosses a card boundary, it may be scanned
  1254   // in order to find the start of the first complete object on the card.
  1313   // in order to find the start of the first complete object on the card.
  1255   constantPoolHandle merge_cp(THREAD,
  1314   ClassLoaderData* loader_data = the_class->class_loader_data();
  1256     oopFactory::new_constantPool(merge_cp_length,
  1315   ConstantPool* merge_cp_oop =
  1257                                  oopDesc::IsUnsafeConc,
  1316     ConstantPool::allocate(loader_data,
  1258                                  THREAD));
  1317                                   merge_cp_length,
       
  1318                                   THREAD);
       
  1319   MergeCPCleaner cp_cleaner(loader_data, merge_cp_oop);
       
  1320 
       
  1321   HandleMark hm(THREAD);  // make sure handles are cleared before
       
  1322                           // MergeCPCleaner clears out merge_cp_oop
       
  1323   constantPoolHandle merge_cp(THREAD, merge_cp_oop);
       
  1324 
       
  1325   // Get constants() from the old class because it could have been rewritten
       
  1326   // while we were at a safepoint allocating a new constant pool.
       
  1327   constantPoolHandle old_cp(THREAD, the_class->constants());
       
  1328   constantPoolHandle scratch_cp(THREAD, scratch_class->constants());
       
  1329 
       
  1330   // If the length changed, the class was redefined out from under us. Return
       
  1331   // an error.
       
  1332   if (merge_cp_length != the_class->constants()->length()
       
  1333          + scratch_class->constants()->length()) {
       
  1334     return JVMTI_ERROR_INTERNAL;
       
  1335   }
       
  1336 
  1259   int orig_length = old_cp->orig_length();
  1337   int orig_length = old_cp->orig_length();
  1260   if (orig_length == 0) {
  1338   if (orig_length == 0) {
  1261     // This old_cp is an actual original constant pool. We save
  1339     // This old_cp is an actual original constant pool. We save
  1262     // the original length in the merged constant pool so that
  1340     // the original length in the merged constant pool so that
  1263     // merge_constant_pools() can be more efficient. If a constant
  1341     // merge_constant_pools() can be more efficient. If a constant
  1296       // constant pool has not gone through link resolution nor have
  1374       // constant pool has not gone through link resolution nor have
  1297       // the new class bytecodes gone through constant pool cache
  1375       // the new class bytecodes gone through constant pool cache
  1298       // rewriting so we can't use the old constant pool with the new
  1376       // rewriting so we can't use the old constant pool with the new
  1299       // class.
  1377       // class.
  1300 
  1378 
  1301       merge_cp()->set_is_conc_safe(true);
  1379       // toss the merged constant pool at return
  1302       merge_cp = constantPoolHandle();  // toss the merged constant pool
       
  1303     } else if (old_cp->length() < scratch_cp->length()) {
  1380     } else if (old_cp->length() < scratch_cp->length()) {
  1304       // The old constant pool has fewer entries than the new constant
  1381       // The old constant pool has fewer entries than the new constant
  1305       // pool and the index map is empty. This means the new constant
  1382       // pool and the index map is empty. This means the new constant
  1306       // pool is a superset of the old constant pool. However, the old
  1383       // pool is a superset of the old constant pool. However, the old
  1307       // class bytecodes have already gone through constant pool cache
  1384       // class bytecodes have already gone through constant pool cache
  1308       // rewriting so we can't use the new constant pool with the old
  1385       // rewriting so we can't use the new constant pool with the old
  1309       // class.
  1386       // class.
  1310 
  1387 
  1311       merge_cp()->set_is_conc_safe(true);
  1388       // toss the merged constant pool at return
  1312       merge_cp = constantPoolHandle();  // toss the merged constant pool
       
  1313     } else {
  1389     } else {
  1314       // The old constant pool has more entries than the new constant
  1390       // The old constant pool has more entries than the new constant
  1315       // pool and the index map is empty. This means that both the old
  1391       // pool and the index map is empty. This means that both the old
  1316       // and merged constant pools are supersets of the new constant
  1392       // and merged constant pools are supersets of the new constant
  1317       // pool.
  1393       // pool.
  1318 
  1394 
  1319       // Replace the new constant pool with a shrunken copy of the
  1395       // Replace the new constant pool with a shrunken copy of the
  1320       // merged constant pool; the previous new constant pool will
  1396       // merged constant pool
  1321       // get GCed.
  1397       set_new_constant_pool(loader_data, scratch_class, merge_cp, merge_cp_length, THREAD);
  1322       set_new_constant_pool(scratch_class, merge_cp, merge_cp_length, true,
  1398       // The new constant pool replaces scratch_cp so have cleaner clean it up.
  1323         THREAD);
  1399       // It can't be cleaned up while there are handles to it.
  1324       // drop local ref to the merged constant pool
  1400       cp_cleaner.add_scratch_cp(scratch_cp());
  1325       merge_cp()->set_is_conc_safe(true);
       
  1326       merge_cp = constantPoolHandle();
       
  1327     }
  1401     }
  1328   } else {
  1402   } else {
  1329     if (RC_TRACE_ENABLED(0x00040000)) {
  1403     if (RC_TRACE_ENABLED(0x00040000)) {
  1330       // don't want to loop unless we are tracing
  1404       // don't want to loop unless we are tracing
  1331       int count = 0;
  1405       int count = 0;
  1348 
  1422 
  1349     // Replace the new constant pool with a shrunken copy of the
  1423     // Replace the new constant pool with a shrunken copy of the
  1350     // merged constant pool so now the rewritten bytecodes have
  1424     // merged constant pool so now the rewritten bytecodes have
  1351     // valid references; the previous new constant pool will get
  1425     // valid references; the previous new constant pool will get
  1352     // GCed.
  1426     // GCed.
  1353     set_new_constant_pool(scratch_class, merge_cp, merge_cp_length, true,
  1427     set_new_constant_pool(loader_data, scratch_class, merge_cp, merge_cp_length, THREAD);
  1354       THREAD);
  1428     // The new constant pool replaces scratch_cp so have cleaner clean it up.
  1355     merge_cp()->set_is_conc_safe(true);
  1429     // It can't be cleaned up while there are handles to it.
  1356   }
  1430     cp_cleaner.add_scratch_cp(scratch_cp());
  1357   assert(old_cp()->is_conc_safe(), "Just checking");
  1431   }
  1358   assert(scratch_cp()->is_conc_safe(), "Just checking");
       
  1359 
  1432 
  1360   return JVMTI_ERROR_NONE;
  1433   return JVMTI_ERROR_NONE;
  1361 } // end merge_cp_and_rewrite()
  1434 } // end merge_cp_and_rewrite()
  1362 
  1435 
  1363 
  1436 
  1409 
  1482 
  1410 // Rewrite constant pool references in the methods.
  1483 // Rewrite constant pool references in the methods.
  1411 bool VM_RedefineClasses::rewrite_cp_refs_in_methods(
  1484 bool VM_RedefineClasses::rewrite_cp_refs_in_methods(
  1412        instanceKlassHandle scratch_class, TRAPS) {
  1485        instanceKlassHandle scratch_class, TRAPS) {
  1413 
  1486 
  1414   objArrayHandle methods(THREAD, scratch_class->methods());
  1487   Array<Method*>* methods = scratch_class->methods();
  1415 
  1488 
  1416   if (methods.is_null() || methods->length() == 0) {
  1489   if (methods == NULL || methods->length() == 0) {
  1417     // no methods so nothing to do
  1490     // no methods so nothing to do
  1418     return true;
  1491     return true;
  1419   }
  1492   }
  1420 
  1493 
  1421   // rewrite constant pool references in the methods:
  1494   // rewrite constant pool references in the methods:
  1422   for (int i = methods->length() - 1; i >= 0; i--) {
  1495   for (int i = methods->length() - 1; i >= 0; i--) {
  1423     methodHandle method(THREAD, (methodOop)methods->obj_at(i));
  1496     methodHandle method(THREAD, methods->at(i));
  1424     methodHandle new_method;
  1497     methodHandle new_method;
  1425     rewrite_cp_refs_in_method(method, &new_method, CHECK_false);
  1498     rewrite_cp_refs_in_method(method, &new_method, CHECK_false);
  1426     if (!new_method.is_null()) {
  1499     if (!new_method.is_null()) {
  1427       // the method has been replaced so save the new method version
  1500       // the method has been replaced so save the new method version
  1428       methods->obj_at_put(i, new_method());
  1501       methods->at_put(i, new_method());
  1429     }
  1502     }
  1430   }
  1503   }
  1431 
  1504 
  1432   return true;
  1505   return true;
  1433 }
  1506 }
  1439        methodHandle *new_method_p, TRAPS) {
  1512        methodHandle *new_method_p, TRAPS) {
  1440 
  1513 
  1441   *new_method_p = methodHandle();  // default is no new method
  1514   *new_method_p = methodHandle();  // default is no new method
  1442 
  1515 
  1443   // We cache a pointer to the bytecodes here in code_base. If GC
  1516   // We cache a pointer to the bytecodes here in code_base. If GC
  1444   // moves the methodOop, then the bytecodes will also move which
  1517   // moves the Method*, then the bytecodes will also move which
  1445   // will likely cause a crash. We create a No_Safepoint_Verifier
  1518   // will likely cause a crash. We create a No_Safepoint_Verifier
  1446   // object to detect whether we pass a possible safepoint in this
  1519   // object to detect whether we pass a possible safepoint in this
  1447   // code block.
  1520   // code block.
  1448   No_Safepoint_Verifier nsv;
  1521   No_Safepoint_Verifier nsv;
  1449 
  1522 
  1568 
  1641 
  1569 // Rewrite constant pool references in the class_annotations field.
  1642 // Rewrite constant pool references in the class_annotations field.
  1570 bool VM_RedefineClasses::rewrite_cp_refs_in_class_annotations(
  1643 bool VM_RedefineClasses::rewrite_cp_refs_in_class_annotations(
  1571        instanceKlassHandle scratch_class, TRAPS) {
  1644        instanceKlassHandle scratch_class, TRAPS) {
  1572 
  1645 
  1573   typeArrayHandle class_annotations(THREAD,
  1646   AnnotationArray* class_annotations = scratch_class->class_annotations();
  1574     scratch_class->class_annotations());
  1647   if (class_annotations == NULL || class_annotations->length() == 0) {
  1575   if (class_annotations.is_null() || class_annotations->length() == 0) {
       
  1576     // no class_annotations so nothing to do
  1648     // no class_annotations so nothing to do
  1577     return true;
  1649     return true;
  1578   }
  1650   }
  1579 
  1651 
  1580   RC_TRACE_WITH_THREAD(0x02000000, THREAD,
  1652   RC_TRACE_WITH_THREAD(0x02000000, THREAD,
  1594 //   u2 num_annotations;
  1666 //   u2 num_annotations;
  1595 //   annotation annotations[num_annotations];
  1667 //   annotation annotations[num_annotations];
  1596 // }
  1668 // }
  1597 //
  1669 //
  1598 bool VM_RedefineClasses::rewrite_cp_refs_in_annotations_typeArray(
  1670 bool VM_RedefineClasses::rewrite_cp_refs_in_annotations_typeArray(
  1599        typeArrayHandle annotations_typeArray, int &byte_i_ref, TRAPS) {
  1671        AnnotationArray* annotations_typeArray, int &byte_i_ref, TRAPS) {
  1600 
  1672 
  1601   if ((byte_i_ref + 2) > annotations_typeArray->length()) {
  1673   if ((byte_i_ref + 2) > annotations_typeArray->length()) {
  1602     // not enough room for num_annotations field
  1674     // not enough room for num_annotations field
  1603     RC_TRACE_WITH_THREAD(0x02000000, THREAD,
  1675     RC_TRACE_WITH_THREAD(0x02000000, THREAD,
  1604       ("length() is too small for num_annotations field"));
  1676       ("length() is too small for num_annotations field"));
  1605     return false;
  1677     return false;
  1606   }
  1678   }
  1607 
  1679 
  1608   u2 num_annotations = Bytes::get_Java_u2((address)
  1680   u2 num_annotations = Bytes::get_Java_u2((address)
  1609                          annotations_typeArray->byte_at_addr(byte_i_ref));
  1681                          annotations_typeArray->adr_at(byte_i_ref));
  1610   byte_i_ref += 2;
  1682   byte_i_ref += 2;
  1611 
  1683 
  1612   RC_TRACE_WITH_THREAD(0x02000000, THREAD,
  1684   RC_TRACE_WITH_THREAD(0x02000000, THREAD,
  1613     ("num_annotations=%d", num_annotations));
  1685     ("num_annotations=%d", num_annotations));
  1614 
  1686 
  1640 //     element_value value;
  1712 //     element_value value;
  1641 //   } element_value_pairs[num_element_value_pairs];
  1713 //   } element_value_pairs[num_element_value_pairs];
  1642 // }
  1714 // }
  1643 //
  1715 //
  1644 bool VM_RedefineClasses::rewrite_cp_refs_in_annotation_struct(
  1716 bool VM_RedefineClasses::rewrite_cp_refs_in_annotation_struct(
  1645        typeArrayHandle annotations_typeArray, int &byte_i_ref, TRAPS) {
  1717        AnnotationArray* annotations_typeArray, int &byte_i_ref, TRAPS) {
  1646   if ((byte_i_ref + 2 + 2) > annotations_typeArray->length()) {
  1718   if ((byte_i_ref + 2 + 2) > annotations_typeArray->length()) {
  1647     // not enough room for smallest annotation_struct
  1719     // not enough room for smallest annotation_struct
  1648     RC_TRACE_WITH_THREAD(0x02000000, THREAD,
  1720     RC_TRACE_WITH_THREAD(0x02000000, THREAD,
  1649       ("length() is too small for annotation_struct"));
  1721       ("length() is too small for annotation_struct"));
  1650     return false;
  1722     return false;
  1652 
  1724 
  1653   u2 type_index = rewrite_cp_ref_in_annotation_data(annotations_typeArray,
  1725   u2 type_index = rewrite_cp_ref_in_annotation_data(annotations_typeArray,
  1654                     byte_i_ref, "mapped old type_index=%d", THREAD);
  1726                     byte_i_ref, "mapped old type_index=%d", THREAD);
  1655 
  1727 
  1656   u2 num_element_value_pairs = Bytes::get_Java_u2((address)
  1728   u2 num_element_value_pairs = Bytes::get_Java_u2((address)
  1657                                  annotations_typeArray->byte_at_addr(
  1729                                  annotations_typeArray->adr_at(byte_i_ref));
  1658                                  byte_i_ref));
       
  1659   byte_i_ref += 2;
  1730   byte_i_ref += 2;
  1660 
  1731 
  1661   RC_TRACE_WITH_THREAD(0x02000000, THREAD,
  1732   RC_TRACE_WITH_THREAD(0x02000000, THREAD,
  1662     ("type_index=%d  num_element_value_pairs=%d", type_index,
  1733     ("type_index=%d  num_element_value_pairs=%d", type_index,
  1663     num_element_value_pairs));
  1734     num_element_value_pairs));
  1698 // Rewrite a constant pool reference at the current position in
  1769 // Rewrite a constant pool reference at the current position in
  1699 // annotations_typeArray if needed. Returns the original constant
  1770 // annotations_typeArray if needed. Returns the original constant
  1700 // pool reference if a rewrite was not needed or the new constant
  1771 // pool reference if a rewrite was not needed or the new constant
  1701 // pool reference if a rewrite was needed.
  1772 // pool reference if a rewrite was needed.
  1702 u2 VM_RedefineClasses::rewrite_cp_ref_in_annotation_data(
  1773 u2 VM_RedefineClasses::rewrite_cp_ref_in_annotation_data(
  1703      typeArrayHandle annotations_typeArray, int &byte_i_ref,
  1774      AnnotationArray* annotations_typeArray, int &byte_i_ref,
  1704      const char * trace_mesg, TRAPS) {
  1775      const char * trace_mesg, TRAPS) {
  1705 
  1776 
  1706   address cp_index_addr = (address)
  1777   address cp_index_addr = (address)
  1707     annotations_typeArray->byte_at_addr(byte_i_ref);
  1778     annotations_typeArray->adr_at(byte_i_ref);
  1708   u2 old_cp_index = Bytes::get_Java_u2(cp_index_addr);
  1779   u2 old_cp_index = Bytes::get_Java_u2(cp_index_addr);
  1709   u2 new_cp_index = find_new_index(old_cp_index);
  1780   u2 new_cp_index = find_new_index(old_cp_index);
  1710   if (new_cp_index != 0) {
  1781   if (new_cp_index != 0) {
  1711     RC_TRACE_WITH_THREAD(0x02000000, THREAD, (trace_mesg, old_cp_index));
  1782     RC_TRACE_WITH_THREAD(0x02000000, THREAD, (trace_mesg, old_cp_index));
  1712     Bytes::put_Java_u2(cp_index_addr, new_cp_index);
  1783     Bytes::put_Java_u2(cp_index_addr, new_cp_index);
  1737 //     } array_value;
  1808 //     } array_value;
  1738 //   } value;
  1809 //   } value;
  1739 // }
  1810 // }
  1740 //
  1811 //
  1741 bool VM_RedefineClasses::rewrite_cp_refs_in_element_value(
  1812 bool VM_RedefineClasses::rewrite_cp_refs_in_element_value(
  1742        typeArrayHandle annotations_typeArray, int &byte_i_ref, TRAPS) {
  1813        AnnotationArray* annotations_typeArray, int &byte_i_ref, TRAPS) {
  1743 
  1814 
  1744   if ((byte_i_ref + 1) > annotations_typeArray->length()) {
  1815   if ((byte_i_ref + 1) > annotations_typeArray->length()) {
  1745     // not enough room for a tag let alone the rest of an element_value
  1816     // not enough room for a tag let alone the rest of an element_value
  1746     RC_TRACE_WITH_THREAD(0x02000000, THREAD,
  1817     RC_TRACE_WITH_THREAD(0x02000000, THREAD,
  1747       ("length() is too small for a tag"));
  1818       ("length() is too small for a tag"));
  1748     return false;
  1819     return false;
  1749   }
  1820   }
  1750 
  1821 
  1751   u1 tag = annotations_typeArray->byte_at(byte_i_ref);
  1822   u1 tag = annotations_typeArray->at(byte_i_ref);
  1752   byte_i_ref++;
  1823   byte_i_ref++;
  1753   RC_TRACE_WITH_THREAD(0x02000000, THREAD, ("tag='%c'", tag));
  1824   RC_TRACE_WITH_THREAD(0x02000000, THREAD, ("tag='%c'", tag));
  1754 
  1825 
  1755   switch (tag) {
  1826   switch (tag) {
  1756     // These BaseType tag values are from Table 4.2 in VM spec:
  1827     // These BaseType tag values are from Table 4.2 in VM spec:
  1848       }
  1919       }
  1849 
  1920 
  1850       // For the above tag value, value.array_value is the right union
  1921       // For the above tag value, value.array_value is the right union
  1851       // field. This is an array of nested element_value.
  1922       // field. This is an array of nested element_value.
  1852       u2 num_values = Bytes::get_Java_u2((address)
  1923       u2 num_values = Bytes::get_Java_u2((address)
  1853                         annotations_typeArray->byte_at_addr(byte_i_ref));
  1924                         annotations_typeArray->adr_at(byte_i_ref));
  1854       byte_i_ref += 2;
  1925       byte_i_ref += 2;
  1855       RC_TRACE_WITH_THREAD(0x02000000, THREAD, ("num_values=%d", num_values));
  1926       RC_TRACE_WITH_THREAD(0x02000000, THREAD, ("num_values=%d", num_values));
  1856 
  1927 
  1857       int calc_num_values = 0;
  1928       int calc_num_values = 0;
  1858       for (; calc_num_values < num_values; calc_num_values++) {
  1929       for (; calc_num_values < num_values; calc_num_values++) {
  1878 
  1949 
  1879 // Rewrite constant pool references in a fields_annotations field.
  1950 // Rewrite constant pool references in a fields_annotations field.
  1880 bool VM_RedefineClasses::rewrite_cp_refs_in_fields_annotations(
  1951 bool VM_RedefineClasses::rewrite_cp_refs_in_fields_annotations(
  1881        instanceKlassHandle scratch_class, TRAPS) {
  1952        instanceKlassHandle scratch_class, TRAPS) {
  1882 
  1953 
  1883   objArrayHandle fields_annotations(THREAD,
  1954   Annotations* sca = scratch_class->annotations();
  1884     scratch_class->fields_annotations());
  1955   if (sca == NULL) return true;
  1885 
  1956 
  1886   if (fields_annotations.is_null() || fields_annotations->length() == 0) {
  1957   Array<AnnotationArray*>* fields_annotations = sca->fields_annotations();
       
  1958 
       
  1959   if (fields_annotations == NULL || fields_annotations->length() == 0) {
  1887     // no fields_annotations so nothing to do
  1960     // no fields_annotations so nothing to do
  1888     return true;
  1961     return true;
  1889   }
  1962   }
  1890 
  1963 
  1891   RC_TRACE_WITH_THREAD(0x02000000, THREAD,
  1964   RC_TRACE_WITH_THREAD(0x02000000, THREAD,
  1892     ("fields_annotations length=%d", fields_annotations->length()));
  1965     ("fields_annotations length=%d", fields_annotations->length()));
  1893 
  1966 
  1894   for (int i = 0; i < fields_annotations->length(); i++) {
  1967   for (int i = 0; i < fields_annotations->length(); i++) {
  1895     typeArrayHandle field_annotations(THREAD,
  1968     AnnotationArray* field_annotations = fields_annotations->at(i);
  1896       (typeArrayOop)fields_annotations->obj_at(i));
  1969     if (field_annotations == NULL || field_annotations->length() == 0) {
  1897     if (field_annotations.is_null() || field_annotations->length() == 0) {
       
  1898       // this field does not have any annotations so skip it
  1970       // this field does not have any annotations so skip it
  1899       continue;
  1971       continue;
  1900     }
  1972     }
  1901 
  1973 
  1902     int byte_i = 0;  // byte index into field_annotations
  1974     int byte_i = 0;  // byte index into field_annotations
  1915 
  1987 
  1916 // Rewrite constant pool references in a methods_annotations field.
  1988 // Rewrite constant pool references in a methods_annotations field.
  1917 bool VM_RedefineClasses::rewrite_cp_refs_in_methods_annotations(
  1989 bool VM_RedefineClasses::rewrite_cp_refs_in_methods_annotations(
  1918        instanceKlassHandle scratch_class, TRAPS) {
  1990        instanceKlassHandle scratch_class, TRAPS) {
  1919 
  1991 
  1920   objArrayHandle methods_annotations(THREAD,
  1992   Annotations* sca = scratch_class->annotations();
  1921     scratch_class->methods_annotations());
  1993   if (sca == NULL) return true;
  1922 
  1994 
  1923   if (methods_annotations.is_null() || methods_annotations->length() == 0) {
  1995   Array<AnnotationArray*>* methods_annotations = sca->methods_annotations();
       
  1996 
       
  1997   if (methods_annotations == NULL || methods_annotations->length() == 0) {
  1924     // no methods_annotations so nothing to do
  1998     // no methods_annotations so nothing to do
  1925     return true;
  1999     return true;
  1926   }
  2000   }
  1927 
  2001 
  1928   RC_TRACE_WITH_THREAD(0x02000000, THREAD,
  2002   RC_TRACE_WITH_THREAD(0x02000000, THREAD,
  1929     ("methods_annotations length=%d", methods_annotations->length()));
  2003     ("methods_annotations length=%d", methods_annotations->length()));
  1930 
  2004 
  1931   for (int i = 0; i < methods_annotations->length(); i++) {
  2005   for (int i = 0; i < methods_annotations->length(); i++) {
  1932     typeArrayHandle method_annotations(THREAD,
  2006     AnnotationArray* method_annotations = methods_annotations->at(i);
  1933       (typeArrayOop)methods_annotations->obj_at(i));
  2007     if (method_annotations == NULL || method_annotations->length() == 0) {
  1934     if (method_annotations.is_null() || method_annotations->length() == 0) {
       
  1935       // this method does not have any annotations so skip it
  2008       // this method does not have any annotations so skip it
  1936       continue;
  2009       continue;
  1937     }
  2010     }
  1938 
  2011 
  1939     int byte_i = 0;  // byte index into method_annotations
  2012     int byte_i = 0;  // byte index into method_annotations
  1964 // }
  2037 // }
  1965 //
  2038 //
  1966 bool VM_RedefineClasses::rewrite_cp_refs_in_methods_parameter_annotations(
  2039 bool VM_RedefineClasses::rewrite_cp_refs_in_methods_parameter_annotations(
  1967        instanceKlassHandle scratch_class, TRAPS) {
  2040        instanceKlassHandle scratch_class, TRAPS) {
  1968 
  2041 
  1969   objArrayHandle methods_parameter_annotations(THREAD,
  2042   Annotations* sca = scratch_class->annotations();
  1970     scratch_class->methods_parameter_annotations());
  2043   if (sca == NULL) return true;
  1971 
  2044 
  1972   if (methods_parameter_annotations.is_null()
  2045   Array<AnnotationArray*>* methods_parameter_annotations =
       
  2046     sca->methods_parameter_annotations();
       
  2047 
       
  2048   if (methods_parameter_annotations == NULL
  1973       || methods_parameter_annotations->length() == 0) {
  2049       || methods_parameter_annotations->length() == 0) {
  1974     // no methods_parameter_annotations so nothing to do
  2050     // no methods_parameter_annotations so nothing to do
  1975     return true;
  2051     return true;
  1976   }
  2052   }
  1977 
  2053 
  1978   RC_TRACE_WITH_THREAD(0x02000000, THREAD,
  2054   RC_TRACE_WITH_THREAD(0x02000000, THREAD,
  1979     ("methods_parameter_annotations length=%d",
  2055     ("methods_parameter_annotations length=%d",
  1980     methods_parameter_annotations->length()));
  2056     methods_parameter_annotations->length()));
  1981 
  2057 
  1982   for (int i = 0; i < methods_parameter_annotations->length(); i++) {
  2058   for (int i = 0; i < methods_parameter_annotations->length(); i++) {
  1983     typeArrayHandle method_parameter_annotations(THREAD,
  2059     AnnotationArray* method_parameter_annotations = methods_parameter_annotations->at(i);
  1984       (typeArrayOop)methods_parameter_annotations->obj_at(i));
  2060     if (method_parameter_annotations == NULL
  1985     if (method_parameter_annotations.is_null()
       
  1986         || method_parameter_annotations->length() == 0) {
  2061         || method_parameter_annotations->length() == 0) {
  1987       // this method does not have any parameter annotations so skip it
  2062       // this method does not have any parameter annotations so skip it
  1988       continue;
  2063       continue;
  1989     }
  2064     }
  1990 
  2065 
  1995       return false;
  2070       return false;
  1996     }
  2071     }
  1997 
  2072 
  1998     int byte_i = 0;  // byte index into method_parameter_annotations
  2073     int byte_i = 0;  // byte index into method_parameter_annotations
  1999 
  2074 
  2000     u1 num_parameters = method_parameter_annotations->byte_at(byte_i);
  2075     u1 num_parameters = method_parameter_annotations->at(byte_i);
  2001     byte_i++;
  2076     byte_i++;
  2002 
  2077 
  2003     RC_TRACE_WITH_THREAD(0x02000000, THREAD,
  2078     RC_TRACE_WITH_THREAD(0x02000000, THREAD,
  2004       ("num_parameters=%d", num_parameters));
  2079       ("num_parameters=%d", num_parameters));
  2005 
  2080 
  2029 // }
  2104 // }
  2030 //
  2105 //
  2031 bool VM_RedefineClasses::rewrite_cp_refs_in_methods_default_annotations(
  2106 bool VM_RedefineClasses::rewrite_cp_refs_in_methods_default_annotations(
  2032        instanceKlassHandle scratch_class, TRAPS) {
  2107        instanceKlassHandle scratch_class, TRAPS) {
  2033 
  2108 
  2034   objArrayHandle methods_default_annotations(THREAD,
  2109   Annotations* sca = scratch_class->annotations();
  2035     scratch_class->methods_default_annotations());
  2110   if (sca == NULL) return true;
  2036 
  2111 
  2037   if (methods_default_annotations.is_null()
  2112   Array<AnnotationArray*>* methods_default_annotations =
       
  2113     sca->methods_default_annotations();
       
  2114 
       
  2115   if (methods_default_annotations == NULL
  2038       || methods_default_annotations->length() == 0) {
  2116       || methods_default_annotations->length() == 0) {
  2039     // no methods_default_annotations so nothing to do
  2117     // no methods_default_annotations so nothing to do
  2040     return true;
  2118     return true;
  2041   }
  2119   }
  2042 
  2120 
  2043   RC_TRACE_WITH_THREAD(0x02000000, THREAD,
  2121   RC_TRACE_WITH_THREAD(0x02000000, THREAD,
  2044     ("methods_default_annotations length=%d",
  2122     ("methods_default_annotations length=%d",
  2045     methods_default_annotations->length()));
  2123     methods_default_annotations->length()));
  2046 
  2124 
  2047   for (int i = 0; i < methods_default_annotations->length(); i++) {
  2125   for (int i = 0; i < methods_default_annotations->length(); i++) {
  2048     typeArrayHandle method_default_annotations(THREAD,
  2126     AnnotationArray* method_default_annotations = methods_default_annotations->at(i);
  2049       (typeArrayOop)methods_default_annotations->obj_at(i));
  2127     if (method_default_annotations == NULL
  2050     if (method_default_annotations.is_null()
       
  2051         || method_default_annotations->length() == 0) {
  2128         || method_default_annotations->length() == 0) {
  2052       // this method does not have any default annotations so skip it
  2129       // this method does not have any default annotations so skip it
  2053       continue;
  2130       continue;
  2054     }
  2131     }
  2055 
  2132 
  2084 
  2161 
  2085   if (!method->has_stackmap_table()) {
  2162   if (!method->has_stackmap_table()) {
  2086     return;
  2163     return;
  2087   }
  2164   }
  2088 
  2165 
  2089   typeArrayOop stackmap_data = method->stackmap_data();
  2166   AnnotationArray* stackmap_data = method->stackmap_data();
  2090   address stackmap_p = (address)stackmap_data->byte_at_addr(0);
  2167   address stackmap_p = (address)stackmap_data->adr_at(0);
  2091   address stackmap_end = stackmap_p + stackmap_data->length();
  2168   address stackmap_end = stackmap_p + stackmap_data->length();
  2092 
  2169 
  2093   assert(stackmap_p + 2 <= stackmap_end, "no room for number_of_entries");
  2170   assert(stackmap_p + 2 <= stackmap_end, "no room for number_of_entries");
  2094   u2 number_of_entries = Bytes::get_Java_u2(stackmap_p);
  2171   u2 number_of_entries = Bytes::get_Java_u2(stackmap_p);
  2095   stackmap_p += 2;
  2172   stackmap_p += 2;
  2333 // Change the constant pool associated with klass scratch_class to
  2410 // Change the constant pool associated with klass scratch_class to
  2334 // scratch_cp. If shrink is true, then scratch_cp_length elements
  2411 // scratch_cp. If shrink is true, then scratch_cp_length elements
  2335 // are copied from scratch_cp to a smaller constant pool and the
  2412 // are copied from scratch_cp to a smaller constant pool and the
  2336 // smaller constant pool is associated with scratch_class.
  2413 // smaller constant pool is associated with scratch_class.
  2337 void VM_RedefineClasses::set_new_constant_pool(
  2414 void VM_RedefineClasses::set_new_constant_pool(
       
  2415        ClassLoaderData* loader_data,
  2338        instanceKlassHandle scratch_class, constantPoolHandle scratch_cp,
  2416        instanceKlassHandle scratch_class, constantPoolHandle scratch_cp,
  2339        int scratch_cp_length, bool shrink, TRAPS) {
  2417        int scratch_cp_length, TRAPS) {
  2340   assert(!shrink || scratch_cp->length() >= scratch_cp_length, "sanity check");
  2418   assert(scratch_cp->length() >= scratch_cp_length, "sanity check");
  2341 
  2419 
  2342   if (shrink) {
       
  2343     // scratch_cp is a merged constant pool and has enough space for a
  2420     // scratch_cp is a merged constant pool and has enough space for a
  2344     // worst case merge situation. We want to associate the minimum
  2421     // worst case merge situation. We want to associate the minimum
  2345     // sized constant pool with the klass to save space.
  2422     // sized constant pool with the klass to save space.
  2346     constantPoolHandle smaller_cp(THREAD,
  2423     constantPoolHandle smaller_cp(THREAD,
  2347       oopFactory::new_constantPool(scratch_cp_length,
  2424     ConstantPool::allocate(loader_data, scratch_cp_length,
  2348                                    oopDesc::IsUnsafeConc,
       
  2349                                    THREAD));
  2425                                    THREAD));
  2350     // preserve orig_length() value in the smaller copy
  2426     // preserve orig_length() value in the smaller copy
  2351     int orig_length = scratch_cp->orig_length();
  2427     int orig_length = scratch_cp->orig_length();
  2352     assert(orig_length != 0, "sanity check");
  2428     assert(orig_length != 0, "sanity check");
  2353     smaller_cp->set_orig_length(orig_length);
  2429     smaller_cp->set_orig_length(orig_length);
  2354     scratch_cp->copy_cp_to(1, scratch_cp_length - 1, smaller_cp, 1, THREAD);
  2430     scratch_cp->copy_cp_to(1, scratch_cp_length - 1, smaller_cp, 1, THREAD);
  2355     scratch_cp = smaller_cp;
  2431     scratch_cp = smaller_cp;
  2356     smaller_cp()->set_is_conc_safe(true);
       
  2357   }
       
  2358 
  2432 
  2359   // attach new constant pool to klass
  2433   // attach new constant pool to klass
  2360   scratch_cp->set_pool_holder(scratch_class());
  2434   scratch_cp->set_pool_holder(scratch_class());
  2361 
  2435 
  2362   // attach klass to new constant pool
  2436   // attach klass to new constant pool
  2428     }
  2502     }
  2429   } // end for each inner class
  2503   } // end for each inner class
  2430 
  2504 
  2431   // Attach each method in klass to the new constant pool and update
  2505   // Attach each method in klass to the new constant pool and update
  2432   // to use new constant pool indices as needed:
  2506   // to use new constant pool indices as needed:
  2433   objArrayHandle methods(THREAD, scratch_class->methods());
  2507   Array<Method*>* methods = scratch_class->methods();
  2434   for (i = methods->length() - 1; i >= 0; i--) {
  2508   for (i = methods->length() - 1; i >= 0; i--) {
  2435     methodHandle method(THREAD, (methodOop)methods->obj_at(i));
  2509     methodHandle method(THREAD, methods->at(i));
  2436     method->set_constants(scratch_cp());
  2510     method->set_constants(scratch_cp());
  2437 
  2511 
  2438     int new_index = find_new_index(method->name_index());
  2512     int new_index = find_new_index(method->name_index());
  2439     if (new_index != 0) {
  2513     if (new_index != 0) {
  2440       RC_TRACE_WITH_THREAD(0x00080000, THREAD,
  2514       RC_TRACE_WITH_THREAD(0x00080000, THREAD,
  2526       } // end for each local variable table entry
  2600       } // end for each local variable table entry
  2527     } // end if there are local variable table entries
  2601     } // end if there are local variable table entries
  2528 
  2602 
  2529     rewrite_cp_refs_in_stack_map_table(method, THREAD);
  2603     rewrite_cp_refs_in_stack_map_table(method, THREAD);
  2530   } // end for each method
  2604   } // end for each method
  2531   assert(scratch_cp()->is_conc_safe(), "Just checking");
       
  2532 } // end set_new_constant_pool()
  2605 } // end set_new_constant_pool()
  2533 
  2606 
       
  2607 
       
  2608 void VM_RedefineClasses::adjust_array_vtable(Klass* k_oop) {
       
  2609   arrayKlass* ak = arrayKlass::cast(k_oop);
       
  2610   bool trace_name_printed = false;
       
  2611   ak->vtable()->adjust_method_entries(_matching_old_methods,
       
  2612                                       _matching_new_methods,
       
  2613                                       _matching_methods_length,
       
  2614                                       &trace_name_printed);
       
  2615 }
  2534 
  2616 
  2535 // Unevolving classes may point to methods of the_class directly
  2617 // Unevolving classes may point to methods of the_class directly
  2536 // from their constant pool caches, itables, and/or vtables. We
  2618 // from their constant pool caches, itables, and/or vtables. We
  2537 // use the SystemDictionary::classes_do() facility and this helper
  2619 // use the SystemDictionary::classes_do() facility and this helper
  2538 // to fix up these pointers.
  2620 // to fix up these pointers.
  2539 //
  2621 //
  2540 // Note: We currently don't support updating the vtable in
  2622 // Note: We currently don't support updating the vtable in
  2541 // arrayKlassOops. See Open Issues in jvmtiRedefineClasses.hpp.
  2623 // arrayKlassOops. See Open Issues in jvmtiRedefineClasses.hpp.
  2542 void VM_RedefineClasses::adjust_cpool_cache_and_vtable(klassOop k_oop,
  2624 void VM_RedefineClasses::adjust_cpool_cache_and_vtable(Klass* k_oop,
  2543        oop initiating_loader, TRAPS) {
  2625        ClassLoaderData* initiating_loader,
  2544   Klass *k = k_oop->klass_part();
  2626        TRAPS) {
       
  2627   Klass *k = k_oop;
  2545   if (k->oop_is_instance()) {
  2628   if (k->oop_is_instance()) {
  2546     HandleMark hm(THREAD);
  2629     HandleMark hm(THREAD);
  2547     instanceKlass *ik = (instanceKlass *) k;
  2630     InstanceKlass *ik = (InstanceKlass *) k;
  2548 
  2631 
  2549     // HotSpot specific optimization! HotSpot does not currently
  2632     // HotSpot specific optimization! HotSpot does not currently
  2550     // support delegation from the bootstrap class loader to a
  2633     // support delegation from the bootstrap class loader to a
  2551     // user-defined class loader. This means that if the bootstrap
  2634     // user-defined class loader. This means that if the bootstrap
  2552     // class loader is the initiating class loader, then it will also
  2635     // class loader is the initiating class loader, then it will also
  2557     //
  2640     //
  2558     // If the current class being redefined has a user-defined class
  2641     // If the current class being redefined has a user-defined class
  2559     // loader as its defining class loader, then we can skip all
  2642     // loader as its defining class loader, then we can skip all
  2560     // classes loaded by the bootstrap class loader.
  2643     // classes loaded by the bootstrap class loader.
  2561     bool is_user_defined =
  2644     bool is_user_defined =
  2562            instanceKlass::cast(_the_class_oop)->class_loader() != NULL;
  2645            InstanceKlass::cast(_the_class_oop)->class_loader() != NULL;
  2563     if (is_user_defined && ik->class_loader() == NULL) {
  2646     if (is_user_defined && ik->class_loader() == NULL) {
  2564       return;
  2647       return;
       
  2648     }
       
  2649 
       
  2650     // If the class being redefined is java.lang.Object, we need to fix all
       
  2651     // array class vtables also
       
  2652     if (_the_class_oop == SystemDictionary::Object_klass()) {
       
  2653       ik->array_klasses_do(adjust_array_vtable);
  2565     }
  2654     }
  2566 
  2655 
  2567     // This is a very busy routine. We don't want too much tracing
  2656     // This is a very busy routine. We don't want too much tracing
  2568     // printed out.
  2657     // printed out.
  2569     bool trace_name_printed = false;
  2658     bool trace_name_printed = false;
  2575     //   ("adjust check: name=%s", ik->external_name()));
  2664     //   ("adjust check: name=%s", ik->external_name()));
  2576     // trace_name_printed = true;
  2665     // trace_name_printed = true;
  2577 
  2666 
  2578     // Fix the vtable embedded in the_class and subclasses of the_class,
  2667     // Fix the vtable embedded in the_class and subclasses of the_class,
  2579     // if one exists. We discard scratch_class and we don't keep an
  2668     // if one exists. We discard scratch_class and we don't keep an
  2580     // instanceKlass around to hold obsolete methods so we don't have
  2669     // InstanceKlass around to hold obsolete methods so we don't have
  2581     // any other instanceKlass embedded vtables to update. The vtable
  2670     // any other InstanceKlass embedded vtables to update. The vtable
  2582     // holds the methodOops for virtual (but not final) methods.
  2671     // holds the Method*s for virtual (but not final) methods.
  2583     if (ik->vtable_length() > 0 && ik->is_subtype_of(_the_class_oop)) {
  2672     if (ik->vtable_length() > 0 && ik->is_subtype_of(_the_class_oop)) {
  2584       // ik->vtable() creates a wrapper object; rm cleans it up
  2673       // ik->vtable() creates a wrapper object; rm cleans it up
  2585       ResourceMark rm(THREAD);
  2674       ResourceMark rm(THREAD);
  2586       ik->vtable()->adjust_method_entries(_matching_old_methods,
  2675       ik->vtable()->adjust_method_entries(_matching_old_methods,
  2587                                           _matching_new_methods,
  2676                                           _matching_new_methods,
  2591 
  2680 
  2592     // If the current class has an itable and we are either redefining an
  2681     // If the current class has an itable and we are either redefining an
  2593     // interface or if the current class is a subclass of the_class, then
  2682     // interface or if the current class is a subclass of the_class, then
  2594     // we potentially have to fix the itable. If we are redefining an
  2683     // we potentially have to fix the itable. If we are redefining an
  2595     // interface, then we have to call adjust_method_entries() for
  2684     // interface, then we have to call adjust_method_entries() for
  2596     // every instanceKlass that has an itable since there isn't a
  2685     // every InstanceKlass that has an itable since there isn't a
  2597     // subclass relationship between an interface and an instanceKlass.
  2686     // subclass relationship between an interface and an InstanceKlass.
  2598     if (ik->itable_length() > 0 && (Klass::cast(_the_class_oop)->is_interface()
  2687     if (ik->itable_length() > 0 && (Klass::cast(_the_class_oop)->is_interface()
  2599         || ik->is_subclass_of(_the_class_oop))) {
  2688         || ik->is_subclass_of(_the_class_oop))) {
  2600       // ik->itable() creates a wrapper object; rm cleans it up
  2689       // ik->itable() creates a wrapper object; rm cleans it up
  2601       ResourceMark rm(THREAD);
  2690       ResourceMark rm(THREAD);
  2602       ik->itable()->adjust_method_entries(_matching_old_methods,
  2691       ik->itable()->adjust_method_entries(_matching_old_methods,
  2607 
  2696 
  2608     // The constant pools in other classes (other_cp) can refer to
  2697     // The constant pools in other classes (other_cp) can refer to
  2609     // methods in the_class. We have to update method information in
  2698     // methods in the_class. We have to update method information in
  2610     // other_cp's cache. If other_cp has a previous version, then we
  2699     // other_cp's cache. If other_cp has a previous version, then we
  2611     // have to repeat the process for each previous version. The
  2700     // have to repeat the process for each previous version. The
  2612     // constant pool cache holds the methodOops for non-virtual
  2701     // constant pool cache holds the Method*s for non-virtual
  2613     // methods and for virtual, final methods.
  2702     // methods and for virtual, final methods.
  2614     //
  2703     //
  2615     // Special case: if the current class is the_class, then new_cp
  2704     // Special case: if the current class is the_class, then new_cp
  2616     // has already been attached to the_class and old_cp has already
  2705     // has already been attached to the_class and old_cp has already
  2617     // been added as a previous version. The new_cp doesn't have any
  2706     // been added as a previous version. The new_cp doesn't have any
  2618     // cached references to old methods so it doesn't need to be
  2707     // cached references to old methods so it doesn't need to be
  2619     // updated. We can simply start with the previous version(s) in
  2708     // updated. We can simply start with the previous version(s) in
  2620     // that case.
  2709     // that case.
  2621     constantPoolHandle other_cp;
  2710     constantPoolHandle other_cp;
  2622     constantPoolCacheOop cp_cache;
  2711     ConstantPoolCache* cp_cache;
  2623 
  2712 
  2624     if (k_oop != _the_class_oop) {
  2713     if (k_oop != _the_class_oop) {
  2625       // this klass' constant pool cache may need adjustment
  2714       // this klass' constant pool cache may need adjustment
  2626       other_cp = constantPoolHandle(ik->constants());
  2715       other_cp = constantPoolHandle(ik->constants());
  2627       cp_cache = other_cp->cache();
  2716       cp_cache = other_cp->cache();
  2657   }
  2746   }
  2658 }
  2747 }
  2659 
  2748 
  2660 void VM_RedefineClasses::update_jmethod_ids() {
  2749 void VM_RedefineClasses::update_jmethod_ids() {
  2661   for (int j = 0; j < _matching_methods_length; ++j) {
  2750   for (int j = 0; j < _matching_methods_length; ++j) {
  2662     methodOop old_method = _matching_old_methods[j];
  2751     Method* old_method = _matching_old_methods[j];
  2663     jmethodID jmid = old_method->find_jmethod_id_or_null();
  2752     jmethodID jmid = old_method->find_jmethod_id_or_null();
  2664     if (jmid != NULL) {
  2753     if (jmid != NULL) {
  2665       // There is a jmethodID, change it to point to the new method
  2754       // There is a jmethodID, change it to point to the new method
  2666       methodHandle new_method_h(_matching_new_methods[j]);
  2755       methodHandle new_method_h(_matching_new_methods[j]);
  2667       JNIHandles::change_method_associated_with_jmethod_id(jmid, new_method_h);
  2756       Method::change_method_associated_with_jmethod_id(jmid, new_method_h());
  2668       assert(JNIHandles::resolve_jmethod_id(jmid) == _matching_new_methods[j],
  2757       assert(Method::resolve_jmethod_id(jmid) == _matching_new_methods[j],
  2669              "should be replaced");
  2758              "should be replaced");
  2670     }
  2759     }
  2671   }
  2760   }
  2672 }
  2761 }
  2673 
  2762 
  2675        BitMap *emcp_methods, int * emcp_method_count_p) {
  2764        BitMap *emcp_methods, int * emcp_method_count_p) {
  2676   *emcp_method_count_p = 0;
  2765   *emcp_method_count_p = 0;
  2677   int obsolete_count = 0;
  2766   int obsolete_count = 0;
  2678   int old_index = 0;
  2767   int old_index = 0;
  2679   for (int j = 0; j < _matching_methods_length; ++j, ++old_index) {
  2768   for (int j = 0; j < _matching_methods_length; ++j, ++old_index) {
  2680     methodOop old_method = _matching_old_methods[j];
  2769     Method* old_method = _matching_old_methods[j];
  2681     methodOop new_method = _matching_new_methods[j];
  2770     Method* new_method = _matching_new_methods[j];
  2682     methodOop old_array_method;
  2771     Method* old_array_method;
  2683 
  2772 
  2684     // Maintain an old_index into the _old_methods array by skipping
  2773     // Maintain an old_index into the _old_methods array by skipping
  2685     // deleted methods
  2774     // deleted methods
  2686     while ((old_array_method = (methodOop) _old_methods->obj_at(old_index))
  2775     while ((old_array_method = _old_methods->at(old_index)) != old_method) {
  2687                                                             != old_method) {
       
  2688       ++old_index;
  2776       ++old_index;
  2689     }
  2777     }
  2690 
  2778 
  2691     if (MethodComparator::methods_EMCP(old_method, new_method)) {
  2779     if (MethodComparator::methods_EMCP(old_method, new_method)) {
  2692       // The EMCP definition from JSR-163 requires the bytecodes to be
  2780       // The EMCP definition from JSR-163 requires the bytecodes to be
  2716       // then the old method cannot be collected until sometime after
  2804       // then the old method cannot be collected until sometime after
  2717       // the old method call has returned. So the overwriting of old
  2805       // the old method call has returned. So the overwriting of old
  2718       // methods by new methods will save us space except for those
  2806       // methods by new methods will save us space except for those
  2719       // (hopefully few) old methods that are still executing.
  2807       // (hopefully few) old methods that are still executing.
  2720       //
  2808       //
  2721       // A method refers to a constMethodOop and this presents another
  2809       // A method refers to a ConstMethod* and this presents another
  2722       // possible avenue to space savings. The constMethodOop in the
  2810       // possible avenue to space savings. The ConstMethod* in the
  2723       // new method contains possibly new attributes (LNT, LVT, etc).
  2811       // new method contains possibly new attributes (LNT, LVT, etc).
  2724       // At first glance, it seems possible to save space by replacing
  2812       // At first glance, it seems possible to save space by replacing
  2725       // the constMethodOop in the old method with the constMethodOop
  2813       // the ConstMethod* in the old method with the ConstMethod*
  2726       // from the new method. The old and new methods would share the
  2814       // from the new method. The old and new methods would share the
  2727       // same constMethodOop and we would save the space occupied by
  2815       // same ConstMethod* and we would save the space occupied by
  2728       // the old constMethodOop. However, the constMethodOop contains
  2816       // the old ConstMethod*. However, the ConstMethod* contains
  2729       // a back reference to the containing method. Sharing the
  2817       // a back reference to the containing method. Sharing the
  2730       // constMethodOop between two methods could lead to confusion in
  2818       // ConstMethod* between two methods could lead to confusion in
  2731       // the code that uses the back reference. This would lead to
  2819       // the code that uses the back reference. This would lead to
  2732       // brittle code that could be broken in non-obvious ways now or
  2820       // brittle code that could be broken in non-obvious ways now or
  2733       // in the future.
  2821       // in the future.
  2734       //
  2822       //
  2735       // Another possibility is to copy the constMethodOop from the new
  2823       // Another possibility is to copy the ConstMethod* from the new
  2736       // method to the old method and then overwrite the new method with
  2824       // method to the old method and then overwrite the new method with
  2737       // the old method. Since the constMethodOop contains the bytecodes
  2825       // the old method. Since the ConstMethod* contains the bytecodes
  2738       // for the method embedded in the oop, this option would change
  2826       // for the method embedded in the oop, this option would change
  2739       // the bytecodes out from under any threads executing the old
  2827       // the bytecodes out from under any threads executing the old
  2740       // method and make the thread's bcp invalid. Since EMCP requires
  2828       // method and make the thread's bcp invalid. Since EMCP requires
  2741       // that the bytecodes be the same modulo constant pool indices, it
  2829       // that the bytecodes be the same modulo constant pool indices, it
  2742       // is straight forward to compute the correct new bcp in the new
  2830       // is straight forward to compute the correct new bcp in the new
  2743       // constMethodOop from the old bcp in the old constMethodOop. The
  2831       // ConstMethod* from the old bcp in the old ConstMethod*. The
  2744       // time consuming part would be searching all the frames in all
  2832       // time consuming part would be searching all the frames in all
  2745       // of the threads to find all of the calls to the old method.
  2833       // of the threads to find all of the calls to the old method.
  2746       //
  2834       //
  2747       // It looks like we will have to live with the limited savings
  2835       // It looks like we will have to live with the limited savings
  2748       // that we get from effectively overwriting the old methods
  2836       // that we get from effectively overwriting the old methods
  2764       // mark obsolete methods as such
  2852       // mark obsolete methods as such
  2765       old_method->set_is_obsolete();
  2853       old_method->set_is_obsolete();
  2766       obsolete_count++;
  2854       obsolete_count++;
  2767 
  2855 
  2768       // obsolete methods need a unique idnum
  2856       // obsolete methods need a unique idnum
  2769       u2 num = instanceKlass::cast(_the_class_oop)->next_method_idnum();
  2857       u2 num = InstanceKlass::cast(_the_class_oop)->next_method_idnum();
  2770       if (num != constMethodOopDesc::UNSET_IDNUM) {
  2858       if (num != ConstMethod::UNSET_IDNUM) {
  2771 //      u2 old_num = old_method->method_idnum();
  2859 //      u2 old_num = old_method->method_idnum();
  2772         old_method->set_method_idnum(num);
  2860         old_method->set_method_idnum(num);
  2773 // TO DO: attach obsolete annotations to obsolete method's new idnum
  2861 // TO DO: attach obsolete annotations to obsolete method's new idnum
  2774       }
  2862       }
  2775       // With tracing we try not to "yack" too much. The position of
  2863       // With tracing we try not to "yack" too much. The position of
  2780         old_method->signature()->as_C_string()));
  2868         old_method->signature()->as_C_string()));
  2781     }
  2869     }
  2782     old_method->set_is_old();
  2870     old_method->set_is_old();
  2783   }
  2871   }
  2784   for (int i = 0; i < _deleted_methods_length; ++i) {
  2872   for (int i = 0; i < _deleted_methods_length; ++i) {
  2785     methodOop old_method = _deleted_methods[i];
  2873     Method* old_method = _deleted_methods[i];
  2786 
  2874 
  2787     assert(old_method->vtable_index() < 0,
  2875     assert(old_method->vtable_index() < 0,
  2788            "cannot delete methods with vtable entries");;
  2876            "cannot delete methods with vtable entries");;
  2789 
  2877 
  2790     // Mark all deleted methods as old and obsolete
  2878     // Mark all deleted methods as old and obsolete
  2835   // more resilent to agents not cleaning up intermediate methods.
  2923   // more resilent to agents not cleaning up intermediate methods.
  2836   // Branch at each depth in the binary tree is:
  2924   // Branch at each depth in the binary tree is:
  2837   //    (1) without the prefix.
  2925   //    (1) without the prefix.
  2838   //    (2) with the prefix.
  2926   //    (2) with the prefix.
  2839   // where 'prefix' is the prefix at that 'depth' (first prefix, second prefix,...)
  2927   // where 'prefix' is the prefix at that 'depth' (first prefix, second prefix,...)
  2840   methodOop search_prefix_name_space(int depth, char* name_str, size_t name_len,
  2928   Method* search_prefix_name_space(int depth, char* name_str, size_t name_len,
  2841                                      Symbol* signature) {
  2929                                      Symbol* signature) {
  2842     TempNewSymbol name_symbol = SymbolTable::probe(name_str, (int)name_len);
  2930     TempNewSymbol name_symbol = SymbolTable::probe(name_str, (int)name_len);
  2843     if (name_symbol != NULL) {
  2931     if (name_symbol != NULL) {
  2844       methodOop method = Klass::cast(the_class())->lookup_method(name_symbol, signature);
  2932       Method* method = Klass::cast(the_class())->lookup_method(name_symbol, signature);
  2845       if (method != NULL) {
  2933       if (method != NULL) {
  2846         // Even if prefixed, intermediate methods must exist.
  2934         // Even if prefixed, intermediate methods must exist.
  2847         if (method->is_native()) {
  2935         if (method->is_native()) {
  2848           // Wahoo, we found a (possibly prefixed) version of the method, return it.
  2936           // Wahoo, we found a (possibly prefixed) version of the method, return it.
  2849           return method;
  2937           return method;
  2875     }
  2963     }
  2876     return NULL;  // This whole branch bore nothing
  2964     return NULL;  // This whole branch bore nothing
  2877   }
  2965   }
  2878 
  2966 
  2879   // Return the method name with old prefixes stripped away.
  2967   // Return the method name with old prefixes stripped away.
  2880   char* method_name_without_prefixes(methodOop method) {
  2968   char* method_name_without_prefixes(Method* method) {
  2881     Symbol* name = method->name();
  2969     Symbol* name = method->name();
  2882     char* name_str = name->as_utf8();
  2970     char* name_str = name->as_utf8();
  2883 
  2971 
  2884     // Old prefixing may be defunct, strip prefixes, if any.
  2972     // Old prefixing may be defunct, strip prefixes, if any.
  2885     for (int i = prefix_count-1; i >= 0; i--) {
  2973     for (int i = prefix_count-1; i >= 0; i--) {
  2892     return name_str;
  2980     return name_str;
  2893   }
  2981   }
  2894 
  2982 
  2895   // Strip any prefixes off the old native method, then try to find a
  2983   // Strip any prefixes off the old native method, then try to find a
  2896   // (possibly prefixed) new native that matches it.
  2984   // (possibly prefixed) new native that matches it.
  2897   methodOop strip_and_search_for_new_native(methodOop method) {
  2985   Method* strip_and_search_for_new_native(Method* method) {
  2898     ResourceMark rm;
  2986     ResourceMark rm;
  2899     char* name_str = method_name_without_prefixes(method);
  2987     char* name_str = method_name_without_prefixes(method);
  2900     return search_prefix_name_space(0, name_str, strlen(name_str),
  2988     return search_prefix_name_space(0, name_str, strlen(name_str),
  2901                                     method->signature());
  2989                                     method->signature());
  2902   }
  2990   }
  2910     the_class = _the_class;
  2998     the_class = _the_class;
  2911     prefixes = JvmtiExport::get_all_native_method_prefixes(&prefix_count);
  2999     prefixes = JvmtiExport::get_all_native_method_prefixes(&prefix_count);
  2912   }
  3000   }
  2913 
  3001 
  2914   // Attempt to transfer any of the old or deleted methods that are native
  3002   // Attempt to transfer any of the old or deleted methods that are native
  2915   void transfer_registrations(methodOop* old_methods, int methods_length) {
  3003   void transfer_registrations(Method** old_methods, int methods_length) {
  2916     for (int j = 0; j < methods_length; j++) {
  3004     for (int j = 0; j < methods_length; j++) {
  2917       methodOop old_method = old_methods[j];
  3005       Method* old_method = old_methods[j];
  2918 
  3006 
  2919       if (old_method->is_native() && old_method->has_native_function()) {
  3007       if (old_method->is_native() && old_method->has_native_function()) {
  2920         methodOop new_method = strip_and_search_for_new_native(old_method);
  3008         Method* new_method = strip_and_search_for_new_native(old_method);
  2921         if (new_method != NULL) {
  3009         if (new_method != NULL) {
  2922           // Actually set the native function in the new method.
  3010           // Actually set the native function in the new method.
  2923           // Redefine does not send events (except CFLH), certainly not this
  3011           // Redefine does not send events (except CFLH), certainly not this
  2924           // behind the scenes re-registration.
  3012           // behind the scenes re-registration.
  2925           new_method->set_native_function(old_method->native_function(),
  3013           new_method->set_native_function(old_method->native_function(),
  2926                               !methodOopDesc::native_bind_event_is_interesting);
  3014                               !Method::native_bind_event_is_interesting);
  2927         }
  3015         }
  2928       }
  3016       }
  2929     }
  3017     }
  2930   }
  3018   }
  2931 };
  3019 };
  2975     JvmtiExport::set_all_dependencies_are_recorded(true);
  3063     JvmtiExport::set_all_dependencies_are_recorded(true);
  2976   }
  3064   }
  2977 }
  3065 }
  2978 
  3066 
  2979 void VM_RedefineClasses::compute_added_deleted_matching_methods() {
  3067 void VM_RedefineClasses::compute_added_deleted_matching_methods() {
  2980   methodOop old_method;
  3068   Method* old_method;
  2981   methodOop new_method;
  3069   Method* new_method;
  2982 
  3070 
  2983   _matching_old_methods = NEW_RESOURCE_ARRAY(methodOop, _old_methods->length());
  3071   _matching_old_methods = NEW_RESOURCE_ARRAY(Method*, _old_methods->length());
  2984   _matching_new_methods = NEW_RESOURCE_ARRAY(methodOop, _old_methods->length());
  3072   _matching_new_methods = NEW_RESOURCE_ARRAY(Method*, _old_methods->length());
  2985   _added_methods        = NEW_RESOURCE_ARRAY(methodOop, _new_methods->length());
  3073   _added_methods        = NEW_RESOURCE_ARRAY(Method*, _new_methods->length());
  2986   _deleted_methods      = NEW_RESOURCE_ARRAY(methodOop, _old_methods->length());
  3074   _deleted_methods      = NEW_RESOURCE_ARRAY(Method*, _old_methods->length());
  2987 
  3075 
  2988   _matching_methods_length = 0;
  3076   _matching_methods_length = 0;
  2989   _deleted_methods_length  = 0;
  3077   _deleted_methods_length  = 0;
  2990   _added_methods_length    = 0;
  3078   _added_methods_length    = 0;
  2991 
  3079 
  2995     if (oj >= _old_methods->length()) {
  3083     if (oj >= _old_methods->length()) {
  2996       if (nj >= _new_methods->length()) {
  3084       if (nj >= _new_methods->length()) {
  2997         break; // we've looked at everything, done
  3085         break; // we've looked at everything, done
  2998       }
  3086       }
  2999       // New method at the end
  3087       // New method at the end
  3000       new_method = (methodOop) _new_methods->obj_at(nj);
  3088       new_method = _new_methods->at(nj);
  3001       _added_methods[_added_methods_length++] = new_method;
  3089       _added_methods[_added_methods_length++] = new_method;
  3002       ++nj;
  3090       ++nj;
  3003     } else if (nj >= _new_methods->length()) {
  3091     } else if (nj >= _new_methods->length()) {
  3004       // Old method, at the end, is deleted
  3092       // Old method, at the end, is deleted
  3005       old_method = (methodOop) _old_methods->obj_at(oj);
  3093       old_method = _old_methods->at(oj);
  3006       _deleted_methods[_deleted_methods_length++] = old_method;
  3094       _deleted_methods[_deleted_methods_length++] = old_method;
  3007       ++oj;
  3095       ++oj;
  3008     } else {
  3096     } else {
  3009       old_method = (methodOop) _old_methods->obj_at(oj);
  3097       old_method = _old_methods->at(oj);
  3010       new_method = (methodOop) _new_methods->obj_at(nj);
  3098       new_method = _new_methods->at(nj);
  3011       if (old_method->name() == new_method->name()) {
  3099       if (old_method->name() == new_method->name()) {
  3012         if (old_method->signature() == new_method->signature()) {
  3100         if (old_method->signature() == new_method->signature()) {
  3013           _matching_old_methods[_matching_methods_length  ] = old_method;
  3101           _matching_old_methods[_matching_methods_length  ] = old_method;
  3014           _matching_new_methods[_matching_methods_length++] = new_method;
  3102           _matching_new_methods[_matching_methods_length++] = new_method;
  3015           ++nj;
  3103           ++nj;
  3050 //      SystemDictionary::classes_do() facility which only allows
  3138 //      SystemDictionary::classes_do() facility which only allows
  3051 //      a helper method to be specified. The interesting parameters
  3139 //      a helper method to be specified. The interesting parameters
  3052 //      that we would like to pass to the helper method are saved in
  3140 //      that we would like to pass to the helper method are saved in
  3053 //      static global fields in the VM operation.
  3141 //      static global fields in the VM operation.
  3054 void VM_RedefineClasses::redefine_single_class(jclass the_jclass,
  3142 void VM_RedefineClasses::redefine_single_class(jclass the_jclass,
  3055        instanceKlassHandle scratch_class, TRAPS) {
  3143        Klass* scratch_class_oop, TRAPS) {
  3056 
  3144 
       
  3145   HandleMark hm(THREAD);   // make sure handles from this call are freed
  3057   RC_TIMER_START(_timer_rsc_phase1);
  3146   RC_TIMER_START(_timer_rsc_phase1);
  3058 
  3147 
       
  3148   instanceKlassHandle scratch_class(scratch_class_oop);
       
  3149 
  3059   oop the_class_mirror = JNIHandles::resolve_non_null(the_jclass);
  3150   oop the_class_mirror = JNIHandles::resolve_non_null(the_jclass);
  3060   klassOop the_class_oop = java_lang_Class::as_klassOop(the_class_mirror);
  3151   Klass* the_class_oop = java_lang_Class::as_Klass(the_class_mirror);
  3061   instanceKlassHandle the_class = instanceKlassHandle(THREAD, the_class_oop);
  3152   instanceKlassHandle the_class = instanceKlassHandle(THREAD, the_class_oop);
  3062 
  3153 
  3063 #ifndef JVMTI_KERNEL
  3154 #ifndef JVMTI_KERNEL
  3064   // Remove all breakpoints in methods of this class
  3155   // Remove all breakpoints in methods of this class
  3065   JvmtiBreakpoints& jvmti_breakpoints = JvmtiCurrentBreakpoints::get_jvmti_breakpoints();
  3156   JvmtiBreakpoints& jvmti_breakpoints = JvmtiCurrentBreakpoints::get_jvmti_breakpoints();
  3103 
  3194 
  3104   // Attach each old method to the new constant pool. This can be
  3195   // Attach each old method to the new constant pool. This can be
  3105   // done here since we are past the bytecode verification and
  3196   // done here since we are past the bytecode verification and
  3106   // constant pool optimization phases.
  3197   // constant pool optimization phases.
  3107   for (int i = _old_methods->length() - 1; i >= 0; i--) {
  3198   for (int i = _old_methods->length() - 1; i >= 0; i--) {
  3108     methodOop method = (methodOop)_old_methods->obj_at(i);
  3199     Method* method = _old_methods->at(i);
  3109     method->set_constants(scratch_class->constants());
  3200     method->set_constants(scratch_class->constants());
  3110   }
  3201   }
  3111 
  3202 
  3112   {
  3203   {
  3113     // walk all previous versions of the klass
  3204     // walk all previous versions of the klass
  3114     instanceKlass *ik = (instanceKlass *)the_class()->klass_part();
  3205     InstanceKlass *ik = (InstanceKlass *)the_class();
  3115     PreviousVersionWalker pvw(ik);
  3206     PreviousVersionWalker pvw(ik);
  3116     instanceKlassHandle ikh;
  3207     instanceKlassHandle ikh;
  3117     do {
  3208     do {
  3118       ikh = pvw.next_previous_version();
  3209       ikh = pvw.next_previous_version();
  3119       if (!ikh.is_null()) {
  3210       if (!ikh.is_null()) {
  3122         // attach previous version of klass to the new constant pool
  3213         // attach previous version of klass to the new constant pool
  3123         ik->set_constants(scratch_class->constants());
  3214         ik->set_constants(scratch_class->constants());
  3124 
  3215 
  3125         // Attach each method in the previous version of klass to the
  3216         // Attach each method in the previous version of klass to the
  3126         // new constant pool
  3217         // new constant pool
  3127         objArrayOop prev_methods = ik->methods();
  3218         Array<Method*>* prev_methods = ik->methods();
  3128         for (int i = prev_methods->length() - 1; i >= 0; i--) {
  3219         for (int i = prev_methods->length() - 1; i >= 0; i--) {
  3129           methodOop method = (methodOop)prev_methods->obj_at(i);
  3220           Method* method = prev_methods->at(i);
  3130           method->set_constants(scratch_class->constants());
  3221           method->set_constants(scratch_class->constants());
  3131         }
  3222         }
  3132       }
  3223       }
  3133     } while (!ikh.is_null());
  3224     } while (!ikh.is_null());
  3134   }
  3225   }
  3137   // Replace methods and constantpool
  3228   // Replace methods and constantpool
  3138   the_class->set_methods(_new_methods);
  3229   the_class->set_methods(_new_methods);
  3139   scratch_class->set_methods(_old_methods);     // To prevent potential GCing of the old methods,
  3230   scratch_class->set_methods(_old_methods);     // To prevent potential GCing of the old methods,
  3140                                           // and to be able to undo operation easily.
  3231                                           // and to be able to undo operation easily.
  3141 
  3232 
  3142   constantPoolOop old_constants = the_class->constants();
  3233   ConstantPool* old_constants = the_class->constants();
  3143   the_class->set_constants(scratch_class->constants());
  3234   the_class->set_constants(scratch_class->constants());
  3144   scratch_class->set_constants(old_constants);  // See the previous comment.
  3235   scratch_class->set_constants(old_constants);  // See the previous comment.
  3145 #if 0
  3236 #if 0
  3146   // We are swapping the guts of "the new class" with the guts of "the
  3237   // We are swapping the guts of "the new class" with the guts of "the
  3147   // class". Since the old constant pool has just been attached to "the
  3238   // class". Since the old constant pool has just been attached to "the
  3164   // check also works fine for methods inherited from super classes.
  3255   // check also works fine for methods inherited from super classes.
  3165   //
  3256   //
  3166   // Miranda methods are a little more complicated. A miranda method is
  3257   // Miranda methods are a little more complicated. A miranda method is
  3167   // provided by an interface when the class implementing the interface
  3258   // provided by an interface when the class implementing the interface
  3168   // does not provide its own method.  These interfaces are implemented
  3259   // does not provide its own method.  These interfaces are implemented
  3169   // internally as an instanceKlass. These special instanceKlasses
  3260   // internally as an InstanceKlass. These special instanceKlasses
  3170   // share the constant pool of the class that "implements" the
  3261   // share the constant pool of the class that "implements" the
  3171   // interface. By sharing the constant pool, the method holder of a
  3262   // interface. By sharing the constant pool, the method holder of a
  3172   // miranda method is the class that "implements" the interface. In a
  3263   // miranda method is the class that "implements" the interface. In a
  3173   // non-redefine situation, the subtype check works fine. However, if
  3264   // non-redefine situation, the subtype check works fine. However, if
  3174   // the old constant pool's pool holder is modified, then the check
  3265   // the old constant pool's pool holder is modified, then the check
  3203       scratch_class->get_cached_class_file_len(), "cache lens must match");
  3294       scratch_class->get_cached_class_file_len(), "cache lens must match");
  3204   }
  3295   }
  3205 #endif
  3296 #endif
  3206 
  3297 
  3207   // Replace inner_classes
  3298   // Replace inner_classes
  3208   typeArrayOop old_inner_classes = the_class->inner_classes();
  3299   Array<u2>* old_inner_classes = the_class->inner_classes();
  3209   the_class->set_inner_classes(scratch_class->inner_classes());
  3300   the_class->set_inner_classes(scratch_class->inner_classes());
  3210   scratch_class->set_inner_classes(old_inner_classes);
  3301   scratch_class->set_inner_classes(old_inner_classes);
  3211 
  3302 
  3212   // Initialize the vtable and interface table after
  3303   // Initialize the vtable and interface table after
  3213   // methods have been rewritten
  3304   // methods have been rewritten
  3245       flags.clear_has_localvariable_table();
  3336       flags.clear_has_localvariable_table();
  3246     }
  3337     }
  3247     the_class->set_access_flags(flags);
  3338     the_class->set_access_flags(flags);
  3248   }
  3339   }
  3249 
  3340 
  3250   // Replace class annotation fields values
  3341   // Replace annotation fields value
  3251   typeArrayOop old_class_annotations = the_class->class_annotations();
  3342   Annotations* old_annotations = the_class->annotations();
  3252   the_class->set_class_annotations(scratch_class->class_annotations());
  3343   the_class->set_annotations(scratch_class->annotations());
  3253   scratch_class->set_class_annotations(old_class_annotations);
  3344   scratch_class->set_annotations(old_annotations);
  3254 
       
  3255   // Replace fields annotation fields values
       
  3256   objArrayOop old_fields_annotations = the_class->fields_annotations();
       
  3257   the_class->set_fields_annotations(scratch_class->fields_annotations());
       
  3258   scratch_class->set_fields_annotations(old_fields_annotations);
       
  3259 
       
  3260   // Replace methods annotation fields values
       
  3261   objArrayOop old_methods_annotations = the_class->methods_annotations();
       
  3262   the_class->set_methods_annotations(scratch_class->methods_annotations());
       
  3263   scratch_class->set_methods_annotations(old_methods_annotations);
       
  3264 
       
  3265   // Replace methods parameter annotation fields values
       
  3266   objArrayOop old_methods_parameter_annotations =
       
  3267     the_class->methods_parameter_annotations();
       
  3268   the_class->set_methods_parameter_annotations(
       
  3269     scratch_class->methods_parameter_annotations());
       
  3270   scratch_class->set_methods_parameter_annotations(old_methods_parameter_annotations);
       
  3271 
       
  3272   // Replace methods default annotation fields values
       
  3273   objArrayOop old_methods_default_annotations =
       
  3274     the_class->methods_default_annotations();
       
  3275   the_class->set_methods_default_annotations(
       
  3276     scratch_class->methods_default_annotations());
       
  3277   scratch_class->set_methods_default_annotations(old_methods_default_annotations);
       
  3278 
  3345 
  3279   // Replace minor version number of class file
  3346   // Replace minor version number of class file
  3280   u2 old_minor_version = the_class->minor_version();
  3347   u2 old_minor_version = the_class->minor_version();
  3281   the_class->set_minor_version(scratch_class->minor_version());
  3348   the_class->set_minor_version(scratch_class->minor_version());
  3282   scratch_class->set_minor_version(old_minor_version);
  3349   scratch_class->set_minor_version(old_minor_version);
  3303 
  3370 
  3304   // Adjust constantpool caches and vtables for all classes
  3371   // Adjust constantpool caches and vtables for all classes
  3305   // that reference methods of the evolved class.
  3372   // that reference methods of the evolved class.
  3306   SystemDictionary::classes_do(adjust_cpool_cache_and_vtable, THREAD);
  3373   SystemDictionary::classes_do(adjust_cpool_cache_and_vtable, THREAD);
  3307 
  3374 
       
  3375   // Fix Resolution Error table also to remove old constant pools
       
  3376   SystemDictionary::delete_resolution_error(old_constants);
       
  3377 
  3308   if (the_class->oop_map_cache() != NULL) {
  3378   if (the_class->oop_map_cache() != NULL) {
  3309     // Flush references to any obsolete methods from the oop map cache
  3379     // Flush references to any obsolete methods from the oop map cache
  3310     // so that obsolete methods are not pinned.
  3380     // so that obsolete methods are not pinned.
  3311     the_class->oop_map_cache()->flush_obsolete_entries();
  3381     the_class->oop_map_cache()->flush_obsolete_entries();
  3312   }
  3382   }
  3313 
  3383 
  3314   // increment the classRedefinedCount field in the_class and in any
  3384   // increment the classRedefinedCount field in the_class and in any
  3315   // direct and indirect subclasses of the_class
  3385   // direct and indirect subclasses of the_class
  3316   increment_class_counter((instanceKlass *)the_class()->klass_part(), THREAD);
  3386   increment_class_counter((InstanceKlass *)the_class(), THREAD);
  3317 
  3387 
  3318   // RC_TRACE macro has an embedded ResourceMark
  3388   // RC_TRACE macro has an embedded ResourceMark
  3319   RC_TRACE_WITH_THREAD(0x00000001, THREAD,
  3389   RC_TRACE_WITH_THREAD(0x00000001, THREAD,
  3320     ("redefined name=%s, count=%d (avail_mem=" UINT64_FORMAT "K)",
  3390     ("redefined name=%s, count=%d (avail_mem=" UINT64_FORMAT "K)",
  3321     the_class->external_name(),
  3391     the_class->external_name(),
  3324 
  3394 
  3325   RC_TIMER_STOP(_timer_rsc_phase2);
  3395   RC_TIMER_STOP(_timer_rsc_phase2);
  3326 } // end redefine_single_class()
  3396 } // end redefine_single_class()
  3327 
  3397 
  3328 
  3398 
  3329 // Increment the classRedefinedCount field in the specific instanceKlass
  3399 // Increment the classRedefinedCount field in the specific InstanceKlass
  3330 // and in all direct and indirect subclasses.
  3400 // and in all direct and indirect subclasses.
  3331 void VM_RedefineClasses::increment_class_counter(instanceKlass *ik, TRAPS) {
  3401 void VM_RedefineClasses::increment_class_counter(InstanceKlass *ik, TRAPS) {
  3332   oop class_mirror = ik->java_mirror();
  3402   oop class_mirror = ik->java_mirror();
  3333   klassOop class_oop = java_lang_Class::as_klassOop(class_mirror);
  3403   Klass* class_oop = java_lang_Class::as_Klass(class_mirror);
  3334   int new_count = java_lang_Class::classRedefinedCount(class_mirror) + 1;
  3404   int new_count = java_lang_Class::classRedefinedCount(class_mirror) + 1;
  3335   java_lang_Class::set_classRedefinedCount(class_mirror, new_count);
  3405   java_lang_Class::set_classRedefinedCount(class_mirror, new_count);
  3336 
  3406 
  3337   if (class_oop != _the_class_oop) {
  3407   if (class_oop != _the_class_oop) {
  3338     // _the_class_oop count is printed at end of redefine_single_class()
  3408     // _the_class_oop count is printed at end of redefine_single_class()
  3342 
  3412 
  3343   for (Klass *subk = ik->subklass(); subk != NULL;
  3413   for (Klass *subk = ik->subklass(); subk != NULL;
  3344        subk = subk->next_sibling()) {
  3414        subk = subk->next_sibling()) {
  3345     if (subk->oop_is_instance()) {
  3415     if (subk->oop_is_instance()) {
  3346       // Only update instanceKlasses
  3416       // Only update instanceKlasses
  3347       instanceKlass *subik = (instanceKlass*)subk;
  3417       InstanceKlass *subik = (InstanceKlass*)subk;
  3348       // recursively do subclasses of the current subclass
  3418       // recursively do subclasses of the current subclass
  3349       increment_class_counter(subik, THREAD);
  3419       increment_class_counter(subik, THREAD);
  3350     }
  3420     }
  3351   }
  3421   }
  3352 }
  3422 }
  3353 
  3423 
  3354 #ifndef PRODUCT
  3424 #ifndef PRODUCT
  3355 void VM_RedefineClasses::check_class(klassOop k_oop,
  3425 void VM_RedefineClasses::check_class(Klass* k_oop,
  3356        oop initiating_loader, TRAPS) {
  3426                                      ClassLoaderData* initiating_loader,
  3357   Klass *k = k_oop->klass_part();
  3427                                      TRAPS) {
       
  3428   Klass *k = k_oop;
  3358   if (k->oop_is_instance()) {
  3429   if (k->oop_is_instance()) {
  3359     HandleMark hm(THREAD);
  3430     HandleMark hm(THREAD);
  3360     instanceKlass *ik = (instanceKlass *) k;
  3431     InstanceKlass *ik = (InstanceKlass *) k;
  3361 
  3432 
  3362     if (ik->vtable_length() > 0) {
  3433     if (ik->vtable_length() > 0) {
  3363       ResourceMark rm(THREAD);
  3434       ResourceMark rm(THREAD);
  3364       if (!ik->vtable()->check_no_old_entries()) {
  3435       if (!ik->vtable()->check_no_old_entries()) {
  3365         tty->print_cr("klassVtable::check_no_old_entries failure -- OLD method found -- class: %s", ik->signature_name());
  3436         tty->print_cr("klassVtable::check_no_old_entries failure -- OLD method found -- class: %s", ik->signature_name());
  3366         ik->vtable()->dump_vtable();
  3437         ik->vtable()->dump_vtable();
  3367         dump_methods();
       
  3368         assert(false, "OLD method found");
  3438         assert(false, "OLD method found");
  3369       }
  3439       }
       
  3440     }
       
  3441     if (ik->itable_length() > 0) {
       
  3442       ResourceMark rm(THREAD);
       
  3443       if (!ik->itable()->check_no_old_entries()) {
       
  3444         tty->print_cr("klassItable::check_no_old_entries failure -- OLD method found -- class: %s", ik->signature_name());
       
  3445         assert(false, "OLD method found");
       
  3446       }
       
  3447     }
       
  3448     // Check that the constant pool cache has no deleted entries.
       
  3449     if (ik->constants() != NULL &&
       
  3450         ik->constants()->cache() != NULL &&
       
  3451        !ik->constants()->cache()->check_no_old_entries()) {
       
  3452       tty->print_cr("klassVtable::check_no_old_entries failure -- OLD method found -- class: %s", ik->signature_name());
       
  3453       assert(false, "OLD method found");
  3370     }
  3454     }
  3371   }
  3455   }
  3372 }
  3456 }
  3373 
  3457 
  3374 void VM_RedefineClasses::dump_methods() {
  3458 void VM_RedefineClasses::dump_methods() {
  3375         int j;
  3459         int j;
  3376         tty->print_cr("_old_methods --");
  3460         tty->print_cr("_old_methods --");
  3377         for (j = 0; j < _old_methods->length(); ++j) {
  3461         for (j = 0; j < _old_methods->length(); ++j) {
  3378           methodOop m = (methodOop) _old_methods->obj_at(j);
  3462           Method* m = _old_methods->at(j);
  3379           tty->print("%4d  (%5d)  ", j, m->vtable_index());
  3463           tty->print("%4d  (%5d)  ", j, m->vtable_index());
  3380           m->access_flags().print_on(tty);
  3464           m->access_flags().print_on(tty);
  3381           tty->print(" --  ");
  3465           tty->print(" --  ");
  3382           m->print_name(tty);
  3466           m->print_name(tty);
  3383           tty->cr();
  3467           tty->cr();
  3384         }
  3468         }
  3385         tty->print_cr("_new_methods --");
  3469         tty->print_cr("_new_methods --");
  3386         for (j = 0; j < _new_methods->length(); ++j) {
  3470         for (j = 0; j < _new_methods->length(); ++j) {
  3387           methodOop m = (methodOop) _new_methods->obj_at(j);
  3471           Method* m = _new_methods->at(j);
  3388           tty->print("%4d  (%5d)  ", j, m->vtable_index());
  3472           tty->print("%4d  (%5d)  ", j, m->vtable_index());
  3389           m->access_flags().print_on(tty);
  3473           m->access_flags().print_on(tty);
  3390           tty->print(" --  ");
  3474           tty->print(" --  ");
  3391           m->print_name(tty);
  3475           m->print_name(tty);
  3392           tty->cr();
  3476           tty->cr();
  3393         }
  3477         }
  3394         tty->print_cr("_matching_(old/new)_methods --");
  3478         tty->print_cr("_matching_(old/new)_methods --");
  3395         for (j = 0; j < _matching_methods_length; ++j) {
  3479         for (j = 0; j < _matching_methods_length; ++j) {
  3396           methodOop m = _matching_old_methods[j];
  3480           Method* m = _matching_old_methods[j];
  3397           tty->print("%4d  (%5d)  ", j, m->vtable_index());
  3481           tty->print("%4d  (%5d)  ", j, m->vtable_index());
  3398           m->access_flags().print_on(tty);
  3482           m->access_flags().print_on(tty);
  3399           tty->print(" --  ");
  3483           tty->print(" --  ");
  3400           m->print_name(tty);
  3484           m->print_name(tty);
  3401           tty->cr();
  3485           tty->cr();
  3404           m->access_flags().print_on(tty);
  3488           m->access_flags().print_on(tty);
  3405           tty->cr();
  3489           tty->cr();
  3406         }
  3490         }
  3407         tty->print_cr("_deleted_methods --");
  3491         tty->print_cr("_deleted_methods --");
  3408         for (j = 0; j < _deleted_methods_length; ++j) {
  3492         for (j = 0; j < _deleted_methods_length; ++j) {
  3409           methodOop m = _deleted_methods[j];
  3493           Method* m = _deleted_methods[j];
  3410           tty->print("%4d  (%5d)  ", j, m->vtable_index());
  3494           tty->print("%4d  (%5d)  ", j, m->vtable_index());
  3411           m->access_flags().print_on(tty);
  3495           m->access_flags().print_on(tty);
  3412           tty->print(" --  ");
  3496           tty->print(" --  ");
  3413           m->print_name(tty);
  3497           m->print_name(tty);
  3414           tty->cr();
  3498           tty->cr();
  3415         }
  3499         }
  3416         tty->print_cr("_added_methods --");
  3500         tty->print_cr("_added_methods --");
  3417         for (j = 0; j < _added_methods_length; ++j) {
  3501         for (j = 0; j < _added_methods_length; ++j) {
  3418           methodOop m = _added_methods[j];
  3502           Method* m = _added_methods[j];
  3419           tty->print("%4d  (%5d)  ", j, m->vtable_index());
  3503           tty->print("%4d  (%5d)  ", j, m->vtable_index());
  3420           m->access_flags().print_on(tty);
  3504           m->access_flags().print_on(tty);
  3421           tty->print(" --  ");
  3505           tty->print(" --  ");
  3422           m->print_name(tty);
  3506           m->print_name(tty);
  3423           tty->cr();
  3507           tty->cr();