src/share/vm/prims/jvmtiRedefineClasses.cpp

Print this page




 764           method_was = deleted;
 765         }
 766       }
 767     }
 768 
 769     switch (method_was) {
 770     case matched:
 771       // methods match, be sure modifiers do too
 772       old_flags = (jushort) k_old_method->access_flags().get_flags();
 773       new_flags = (jushort) k_new_method->access_flags().get_flags();
 774       if ((old_flags ^ new_flags) & ~(JVM_ACC_NATIVE)) {
 775         return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_METHOD_MODIFIERS_CHANGED;
 776       }
 777       {
 778         u2 new_num = k_new_method->method_idnum();
 779         u2 old_num = k_old_method->method_idnum();
 780         if (new_num != old_num) {
 781           Method* idnum_owner = scratch_class->method_with_idnum(old_num);
 782           if (idnum_owner != NULL) {
 783             // There is already a method assigned this idnum -- switch them

 784             idnum_owner->set_method_idnum(new_num);

 785           }

 786           k_new_method->set_method_idnum(old_num);

 787           if (thread->has_pending_exception()) {
 788             return JVMTI_ERROR_OUT_OF_MEMORY;
 789           }
 790         }
 791       }
 792       RC_TRACE(0x00008000, ("Method matched: new: %s [%d] == old: %s [%d]",
 793                             k_new_method->name_and_sig_as_C_string(), ni,
 794                             k_old_method->name_and_sig_as_C_string(), oi));
 795       // advance to next pair of methods
 796       ++oi;
 797       ++ni;
 798       break;
 799     case added:
 800       // method added, see if it is OK
 801       new_flags = (jushort) k_new_method->access_flags().get_flags();
 802       if ((new_flags & JVM_ACC_PRIVATE) == 0
 803            // hack: private should be treated as final, but alas
 804           || (new_flags & (JVM_ACC_FINAL|JVM_ACC_STATIC)) == 0
 805          ) {
 806         // new methods must be private
 807         return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_METHOD_ADDED;
 808       }
 809       {
 810         u2 num = the_class->next_method_idnum();
 811         if (num == ConstMethod::UNSET_IDNUM) {
 812           // cannot add any more methods
 813           return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_METHOD_ADDED;
 814         }
 815         u2 new_num = k_new_method->method_idnum();
 816         Method* idnum_owner = scratch_class->method_with_idnum(num);
 817         if (idnum_owner != NULL) {
 818           // There is already a method assigned this idnum -- switch them

 819           idnum_owner->set_method_idnum(new_num);

 820         }
 821         k_new_method->set_method_idnum(num);

 822         if (thread->has_pending_exception()) {
 823           return JVMTI_ERROR_OUT_OF_MEMORY;
 824         }
 825       }
 826       RC_TRACE(0x00008000, ("Method added: new: %s [%d]",
 827                             k_new_method->name_and_sig_as_C_string(), ni));
 828       ++ni; // advance to next new method
 829       break;
 830     case deleted:
 831       // method deleted, see if it is OK
 832       old_flags = (jushort) k_old_method->access_flags().get_flags();
 833       if ((old_flags & JVM_ACC_PRIVATE) == 0
 834            // hack: private should be treated as final, but alas
 835           || (old_flags & (JVM_ACC_FINAL|JVM_ACC_STATIC)) == 0
 836          ) {
 837         // deleted methods must be private
 838         return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_METHOD_DELETED;
 839       }
 840       RC_TRACE(0x00008000, ("Method deleted: old: %s [%d]",
 841                             k_old_method->name_and_sig_as_C_string(), oi));


3309         }
3310       } // end for each local variable table entry
3311     } // end if there are local variable table entries
3312 
3313     rewrite_cp_refs_in_stack_map_table(method, THREAD);
3314   } // end for each method
3315 } // end set_new_constant_pool()
3316 
3317 
3318 // Unevolving classes may point to methods of the_class directly
3319 // from their constant pool caches, itables, and/or vtables. We
3320 // use the ClassLoaderDataGraph::classes_do() facility and this helper
3321 // to fix up these pointers.
3322 
3323 // Adjust cpools and vtables closure
3324 void VM_RedefineClasses::AdjustCpoolCacheAndVtable::do_klass(Klass* k) {
3325 
3326   // This is a very busy routine. We don't want too much tracing
3327   // printed out.
3328   bool trace_name_printed = false;

3329 
3330   // Very noisy: only enable this call if you are trying to determine
3331   // that a specific class gets found by this routine.
3332   // RC_TRACE macro has an embedded ResourceMark
3333   // RC_TRACE_WITH_THREAD(0x00100000, THREAD,
3334   //   ("adjust check: name=%s", k->external_name()));
3335   // trace_name_printed = true;
3336 
3337   // If the class being redefined is java.lang.Object, we need to fix all
3338   // array class vtables also
3339   if (k->oop_is_array() && _the_class_oop == SystemDictionary::Object_klass()) {
3340     k->vtable()->adjust_method_entries(_matching_old_methods,
3341                                        _matching_new_methods,
3342                                        _matching_methods_length,
3343                                        &trace_name_printed);
3344   } else if (k->oop_is_instance()) {
3345     HandleMark hm(_thread);
3346     InstanceKlass *ik = InstanceKlass::cast(k);
3347 
3348     // HotSpot specific optimization! HotSpot does not currently
3349     // support delegation from the bootstrap class loader to a
3350     // user-defined class loader. This means that if the bootstrap
3351     // class loader is the initiating class loader, then it will also
3352     // be the defining class loader. This also means that classes
3353     // loaded by the bootstrap class loader cannot refer to classes
3354     // loaded by a user-defined class loader. Note: a user-defined
3355     // class loader can delegate to the bootstrap class loader.
3356     //
3357     // If the current class being redefined has a user-defined class
3358     // loader as its defining class loader, then we can skip all
3359     // classes loaded by the bootstrap class loader.
3360     bool is_user_defined =
3361            InstanceKlass::cast(_the_class_oop)->class_loader() != NULL;
3362     if (is_user_defined && ik->class_loader() == NULL) {
3363       return;


3365 
3366     // Fix the vtable embedded in the_class and subclasses of the_class,
3367     // if one exists. We discard scratch_class and we don't keep an
3368     // InstanceKlass around to hold obsolete methods so we don't have
3369     // any other InstanceKlass embedded vtables to update. The vtable
3370     // holds the Method*s for virtual (but not final) methods.
3371     // Default methods, or concrete methods in interfaces are stored
3372     // in the vtable, so if an interface changes we need to check
3373     // adjust_method_entries() for every InstanceKlass, which will also
3374     // adjust the default method vtable indices.
3375     // We also need to adjust any default method entries that are
3376     // not yet in the vtable, because the vtable setup is in progress.
3377     // This must be done after we adjust the default_methods and
3378     // default_vtable_indices for methods already in the vtable.
3379     // If redefining Unsafe, walk all the vtables looking for entries.
3380     if (ik->vtable_length() > 0 && (_the_class_oop->is_interface()
3381         || _the_class_oop == SystemDictionary::misc_Unsafe_klass()
3382         || ik->is_subtype_of(_the_class_oop))) {
3383       // ik->vtable() creates a wrapper object; rm cleans it up
3384       ResourceMark rm(_thread);
3385       ik->vtable()->adjust_method_entries(_matching_old_methods,
3386                                           _matching_new_methods,
3387                                           _matching_methods_length,
3388                                           &trace_name_printed);
3389       ik->adjust_default_methods(_matching_old_methods,
3390                                  _matching_new_methods,
3391                                  _matching_methods_length,
3392                                  &trace_name_printed);
3393     }
3394 
3395     // If the current class has an itable and we are either redefining an
3396     // interface or if the current class is a subclass of the_class, then
3397     // we potentially have to fix the itable. If we are redefining an
3398     // interface, then we have to call adjust_method_entries() for
3399     // every InstanceKlass that has an itable since there isn't a
3400     // subclass relationship between an interface and an InstanceKlass.
3401     // If redefining Unsafe, walk all the itables looking for entries.
3402     if (ik->itable_length() > 0 && (_the_class_oop->is_interface()
3403         || _the_class_oop == SystemDictionary::misc_Unsafe_klass()
3404         || ik->is_subclass_of(_the_class_oop))) {
3405       // ik->itable() creates a wrapper object; rm cleans it up
3406       ResourceMark rm(_thread);
3407       ik->itable()->adjust_method_entries(_matching_old_methods,
3408                                           _matching_new_methods,
3409                                           _matching_methods_length,
3410                                           &trace_name_printed);
3411     }
3412 
3413     // The constant pools in other classes (other_cp) can refer to
3414     // methods in the_class. We have to update method information in
3415     // other_cp's cache. If other_cp has a previous version, then we
3416     // have to repeat the process for each previous version. The
3417     // constant pool cache holds the Method*s for non-virtual
3418     // methods and for virtual, final methods.
3419     //
3420     // Special case: if the current class is the_class, then new_cp
3421     // has already been attached to the_class and old_cp has already
3422     // been added as a previous version. The new_cp doesn't have any
3423     // cached references to old methods so it doesn't need to be
3424     // updated. We can simply start with the previous version(s) in
3425     // that case.
3426     constantPoolHandle other_cp;
3427     ConstantPoolCache* cp_cache;
3428 
3429     if (ik != _the_class_oop) {
3430       // this klass' constant pool cache may need adjustment
3431       other_cp = constantPoolHandle(ik->constants());
3432       cp_cache = other_cp->cache();
3433       if (cp_cache != NULL) {
3434         cp_cache->adjust_method_entries(_matching_old_methods,
3435                                         _matching_new_methods,
3436                                         _matching_methods_length,
3437                                         &trace_name_printed);
3438       }
3439     }
3440 
3441     // the previous versions' constant pool caches may need adjustment
3442     for (InstanceKlass* pv_node = ik->previous_versions();
3443          pv_node != NULL;
3444          pv_node = pv_node->previous_versions()) {
3445       cp_cache = pv_node->constants()->cache();
3446       if (cp_cache != NULL) {
3447         cp_cache->adjust_method_entries(_matching_old_methods,
3448                                         _matching_new_methods,
3449                                         _matching_methods_length,
3450                                         &trace_name_printed);
3451       }
3452     }
3453   }
3454 }
3455 
3456 // Clean method data for this class
3457 void VM_RedefineClasses::MethodDataCleaner::do_klass(Klass* k) {


3560 
3561       // Count number of methods that are EMCP.  The method will be marked
3562       // old but not obsolete if it is EMCP.
3563       emcp_method_count++;
3564 
3565       // An EMCP method is _not_ obsolete. An obsolete method has a
3566       // different jmethodID than the current method. An EMCP method
3567       // has the same jmethodID as the current method. Having the
3568       // same jmethodID for all EMCP versions of a method allows for
3569       // a consistent view of the EMCP methods regardless of which
3570       // EMCP method you happen to have in hand. For example, a
3571       // breakpoint set in one EMCP method will work for all EMCP
3572       // versions of the method including the current one.
3573     } else {
3574       // mark obsolete methods as such
3575       old_method->set_is_obsolete();
3576       obsolete_count++;
3577 
3578       // obsolete methods need a unique idnum so they become new entries in
3579       // the jmethodID cache in InstanceKlass

3580       u2 num = InstanceKlass::cast(_the_class_oop)->next_method_idnum();
3581       if (num != ConstMethod::UNSET_IDNUM) {
3582         old_method->set_method_idnum(num);
3583       }
3584 
3585       // With tracing we try not to "yack" too much. The position of
3586       // this trace assumes there are fewer obsolete methods than
3587       // EMCP methods.
3588       RC_TRACE(0x00000100, ("mark %s(%s) as obsolete",
3589         old_method->name()->as_C_string(),
3590         old_method->signature()->as_C_string()));
3591     }
3592     old_method->set_is_old();
3593   }
3594   for (int i = 0; i < _deleted_methods_length; ++i) {
3595     Method* old_method = _deleted_methods[i];
3596 
3597     assert(!old_method->has_vtable_index(),
3598            "cannot delete methods with vtable entries");;
3599 




 764           method_was = deleted;
 765         }
 766       }
 767     }
 768 
 769     switch (method_was) {
 770     case matched:
 771       // methods match, be sure modifiers do too
 772       old_flags = (jushort) k_old_method->access_flags().get_flags();
 773       new_flags = (jushort) k_new_method->access_flags().get_flags();
 774       if ((old_flags ^ new_flags) & ~(JVM_ACC_NATIVE)) {
 775         return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_METHOD_MODIFIERS_CHANGED;
 776       }
 777       {
 778         u2 new_num = k_new_method->method_idnum();
 779         u2 old_num = k_old_method->method_idnum();
 780         if (new_num != old_num) {
 781           Method* idnum_owner = scratch_class->method_with_idnum(old_num);
 782           if (idnum_owner != NULL) {
 783             // There is already a method assigned this idnum -- switch them
 784             // Take current and original idnum from the new_method
 785             idnum_owner->set_method_idnum(new_num);
 786             idnum_owner->set_orig_method_idnum(k_new_method->orig_method_idnum());
 787           }
 788           // Take current and original idnum from the old_method
 789           k_new_method->set_method_idnum(old_num);
 790           k_new_method->set_orig_method_idnum(k_old_method->orig_method_idnum());
 791           if (thread->has_pending_exception()) {
 792             return JVMTI_ERROR_OUT_OF_MEMORY;
 793           }
 794         }
 795       }
 796       RC_TRACE(0x00008000, ("Method matched: new: %s [%d] == old: %s [%d]",
 797                             k_new_method->name_and_sig_as_C_string(), ni,
 798                             k_old_method->name_and_sig_as_C_string(), oi));
 799       // advance to next pair of methods
 800       ++oi;
 801       ++ni;
 802       break;
 803     case added:
 804       // method added, see if it is OK
 805       new_flags = (jushort) k_new_method->access_flags().get_flags();
 806       if ((new_flags & JVM_ACC_PRIVATE) == 0
 807            // hack: private should be treated as final, but alas
 808           || (new_flags & (JVM_ACC_FINAL|JVM_ACC_STATIC)) == 0
 809          ) {
 810         // new methods must be private
 811         return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_METHOD_ADDED;
 812       }
 813       {
 814         u2 num = the_class->next_method_idnum();
 815         if (num == ConstMethod::UNSET_IDNUM) {
 816           // cannot add any more methods
 817           return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_METHOD_ADDED;
 818         }
 819         u2 new_num = k_new_method->method_idnum();
 820         Method* idnum_owner = scratch_class->method_with_idnum(num);
 821         if (idnum_owner != NULL) {
 822           // There is already a method assigned this idnum -- switch them
 823           // Take current and original idnum from the new_method
 824           idnum_owner->set_method_idnum(new_num);
 825           idnum_owner->set_orig_method_idnum(k_new_method->orig_method_idnum());
 826         }
 827         k_new_method->set_method_idnum(num);
 828         k_new_method->set_orig_method_idnum(num);
 829         if (thread->has_pending_exception()) {
 830           return JVMTI_ERROR_OUT_OF_MEMORY;
 831         }
 832       }
 833       RC_TRACE(0x00008000, ("Method added: new: %s [%d]",
 834                             k_new_method->name_and_sig_as_C_string(), ni));
 835       ++ni; // advance to next new method
 836       break;
 837     case deleted:
 838       // method deleted, see if it is OK
 839       old_flags = (jushort) k_old_method->access_flags().get_flags();
 840       if ((old_flags & JVM_ACC_PRIVATE) == 0
 841            // hack: private should be treated as final, but alas
 842           || (old_flags & (JVM_ACC_FINAL|JVM_ACC_STATIC)) == 0
 843          ) {
 844         // deleted methods must be private
 845         return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_METHOD_DELETED;
 846       }
 847       RC_TRACE(0x00008000, ("Method deleted: old: %s [%d]",
 848                             k_old_method->name_and_sig_as_C_string(), oi));


3316         }
3317       } // end for each local variable table entry
3318     } // end if there are local variable table entries
3319 
3320     rewrite_cp_refs_in_stack_map_table(method, THREAD);
3321   } // end for each method
3322 } // end set_new_constant_pool()
3323 
3324 
3325 // Unevolving classes may point to methods of the_class directly
3326 // from their constant pool caches, itables, and/or vtables. We
3327 // use the ClassLoaderDataGraph::classes_do() facility and this helper
3328 // to fix up these pointers.
3329 
3330 // Adjust cpools and vtables closure
3331 void VM_RedefineClasses::AdjustCpoolCacheAndVtable::do_klass(Klass* k) {
3332 
3333   // This is a very busy routine. We don't want too much tracing
3334   // printed out.
3335   bool trace_name_printed = false;
3336   InstanceKlass *the_class = InstanceKlass::cast(_the_class_oop);
3337 
3338   // Very noisy: only enable this call if you are trying to determine
3339   // that a specific class gets found by this routine.
3340   // RC_TRACE macro has an embedded ResourceMark
3341   // RC_TRACE_WITH_THREAD(0x00100000, THREAD,
3342   //   ("adjust check: name=%s", k->external_name()));
3343   // trace_name_printed = true;
3344 
3345   // If the class being redefined is java.lang.Object, we need to fix all
3346   // array class vtables also
3347   if (k->oop_is_array() && _the_class_oop == SystemDictionary::Object_klass()) {
3348     k->vtable()->adjust_method_entries(the_class, &trace_name_printed);
3349 


3350   } else if (k->oop_is_instance()) {
3351     HandleMark hm(_thread);
3352     InstanceKlass *ik = InstanceKlass::cast(k);
3353 
3354     // HotSpot specific optimization! HotSpot does not currently
3355     // support delegation from the bootstrap class loader to a
3356     // user-defined class loader. This means that if the bootstrap
3357     // class loader is the initiating class loader, then it will also
3358     // be the defining class loader. This also means that classes
3359     // loaded by the bootstrap class loader cannot refer to classes
3360     // loaded by a user-defined class loader. Note: a user-defined
3361     // class loader can delegate to the bootstrap class loader.
3362     //
3363     // If the current class being redefined has a user-defined class
3364     // loader as its defining class loader, then we can skip all
3365     // classes loaded by the bootstrap class loader.
3366     bool is_user_defined =
3367            InstanceKlass::cast(_the_class_oop)->class_loader() != NULL;
3368     if (is_user_defined && ik->class_loader() == NULL) {
3369       return;


3371 
3372     // Fix the vtable embedded in the_class and subclasses of the_class,
3373     // if one exists. We discard scratch_class and we don't keep an
3374     // InstanceKlass around to hold obsolete methods so we don't have
3375     // any other InstanceKlass embedded vtables to update. The vtable
3376     // holds the Method*s for virtual (but not final) methods.
3377     // Default methods, or concrete methods in interfaces are stored
3378     // in the vtable, so if an interface changes we need to check
3379     // adjust_method_entries() for every InstanceKlass, which will also
3380     // adjust the default method vtable indices.
3381     // We also need to adjust any default method entries that are
3382     // not yet in the vtable, because the vtable setup is in progress.
3383     // This must be done after we adjust the default_methods and
3384     // default_vtable_indices for methods already in the vtable.
3385     // If redefining Unsafe, walk all the vtables looking for entries.
3386     if (ik->vtable_length() > 0 && (_the_class_oop->is_interface()
3387         || _the_class_oop == SystemDictionary::misc_Unsafe_klass()
3388         || ik->is_subtype_of(_the_class_oop))) {
3389       // ik->vtable() creates a wrapper object; rm cleans it up
3390       ResourceMark rm(_thread);
3391 
3392       ik->vtable()->adjust_method_entries(the_class, &trace_name_printed);
3393       ik->adjust_default_methods(the_class, &trace_name_printed);





3394     }
3395 
3396     // If the current class has an itable and we are either redefining an
3397     // interface or if the current class is a subclass of the_class, then
3398     // we potentially have to fix the itable. If we are redefining an
3399     // interface, then we have to call adjust_method_entries() for
3400     // every InstanceKlass that has an itable since there isn't a
3401     // subclass relationship between an interface and an InstanceKlass.
3402     // If redefining Unsafe, walk all the itables looking for entries.
3403     if (ik->itable_length() > 0 && (_the_class_oop->is_interface()
3404         || _the_class_oop == SystemDictionary::misc_Unsafe_klass()
3405         || ik->is_subclass_of(_the_class_oop))) {
3406       // ik->itable() creates a wrapper object; rm cleans it up
3407       ResourceMark rm(_thread);
3408 
3409       ik->itable()->adjust_method_entries(the_class, &trace_name_printed);


3410     }
3411 
3412     // The constant pools in other classes (other_cp) can refer to
3413     // methods in the_class. We have to update method information in
3414     // other_cp's cache. If other_cp has a previous version, then we
3415     // have to repeat the process for each previous version. The
3416     // constant pool cache holds the Method*s for non-virtual
3417     // methods and for virtual, final methods.
3418     //
3419     // Special case: if the current class is the_class, then new_cp
3420     // has already been attached to the_class and old_cp has already
3421     // been added as a previous version. The new_cp doesn't have any
3422     // cached references to old methods so it doesn't need to be
3423     // updated. We can simply start with the previous version(s) in
3424     // that case.
3425     constantPoolHandle other_cp;
3426     ConstantPoolCache* cp_cache;
3427 
3428     if (ik != _the_class_oop) {
3429       // this klass' constant pool cache may need adjustment
3430       other_cp = constantPoolHandle(ik->constants());
3431       cp_cache = other_cp->cache();
3432       if (cp_cache != NULL) {
3433         cp_cache->adjust_method_entries(the_class, &trace_name_printed);



3434       }
3435     }
3436 
3437     // the previous versions' constant pool caches may need adjustment
3438     for (InstanceKlass* pv_node = ik->previous_versions();
3439          pv_node != NULL;
3440          pv_node = pv_node->previous_versions()) {
3441       cp_cache = pv_node->constants()->cache();
3442       if (cp_cache != NULL) {
3443         cp_cache->adjust_method_entries(_matching_old_methods,
3444                                         _matching_new_methods,
3445                                         _matching_methods_length,
3446                                         &trace_name_printed);
3447       }
3448     }
3449   }
3450 }
3451 
3452 // Clean method data for this class
3453 void VM_RedefineClasses::MethodDataCleaner::do_klass(Klass* k) {


3556 
3557       // Count number of methods that are EMCP.  The method will be marked
3558       // old but not obsolete if it is EMCP.
3559       emcp_method_count++;
3560 
3561       // An EMCP method is _not_ obsolete. An obsolete method has a
3562       // different jmethodID than the current method. An EMCP method
3563       // has the same jmethodID as the current method. Having the
3564       // same jmethodID for all EMCP versions of a method allows for
3565       // a consistent view of the EMCP methods regardless of which
3566       // EMCP method you happen to have in hand. For example, a
3567       // breakpoint set in one EMCP method will work for all EMCP
3568       // versions of the method including the current one.
3569     } else {
3570       // mark obsolete methods as such
3571       old_method->set_is_obsolete();
3572       obsolete_count++;
3573 
3574       // obsolete methods need a unique idnum so they become new entries in
3575       // the jmethodID cache in InstanceKlass
3576       assert(old_method->method_idnum() == new_method->method_idnum(), "must match");
3577       u2 num = InstanceKlass::cast(_the_class_oop)->next_method_idnum();
3578       if (num != ConstMethod::UNSET_IDNUM) {
3579         old_method->set_method_idnum(num);
3580       }
3581 
3582       // With tracing we try not to "yack" too much. The position of
3583       // this trace assumes there are fewer obsolete methods than
3584       // EMCP methods.
3585       RC_TRACE(0x00000100, ("mark %s(%s) as obsolete",
3586         old_method->name()->as_C_string(),
3587         old_method->signature()->as_C_string()));
3588     }
3589     old_method->set_is_old();
3590   }
3591   for (int i = 0; i < _deleted_methods_length; ++i) {
3592     Method* old_method = _deleted_methods[i];
3593 
3594     assert(!old_method->has_vtable_index(),
3595            "cannot delete methods with vtable entries");;
3596