< prev index next >

src/share/vm/prims/jvmtiRedefineClasses.cpp

Print this page




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     PreviousVersionWalker pvw(_thread, ik);
3439     for (PreviousVersionNode * pv_node = pvw.next_previous_version();
3440          pv_node != NULL; pv_node = pvw.next_previous_version()) {
3441       other_cp = pv_node->prev_constant_pool();
3442       cp_cache = other_cp->cache();
3443       if (cp_cache != NULL) {
3444         cp_cache->adjust_method_entries(other_cp->pool_holder(), &trace_name_printed);
3445       }
3446     }
3447   }
3448 }
3449 
3450 void VM_RedefineClasses::update_jmethod_ids() {
3451   for (int j = 0; j < _matching_methods_length; ++j) {
3452     Method* old_method = _matching_old_methods[j];
3453     jmethodID jmid = old_method->find_jmethod_id_or_null();
3454     if (jmid != NULL) {
3455       // There is a jmethodID, change it to point to the new method
3456       methodHandle new_method_h(_matching_new_methods[j]);
3457       Method::change_method_associated_with_jmethod_id(jmid, new_method_h());
3458       assert(Method::resolve_jmethod_id(jmid) == _matching_new_methods[j],
3459              "should be replaced");
3460     }
3461   }
3462 }
3463 
3464 void VM_RedefineClasses::check_methods_and_mark_as_obsolete(
3465        BitMap *emcp_methods, int * emcp_method_count_p) {
3466   *emcp_method_count_p = 0;
3467   int obsolete_count = 0;
3468   int old_index = 0;
3469   for (int j = 0; j < _matching_methods_length; ++j, ++old_index) {
3470     Method* old_method = _matching_old_methods[j];
3471     Method* new_method = _matching_new_methods[j];
3472     Method* old_array_method;
3473 
3474     // Maintain an old_index into the _old_methods array by skipping
3475     // deleted methods
3476     while ((old_array_method = _old_methods->at(old_index)) != old_method) {
3477       ++old_index;
3478     }
3479 
3480     if (MethodComparator::methods_EMCP(old_method, new_method)) {
3481       // The EMCP definition from JSR-163 requires the bytecodes to be
3482       // the same with the exception of constant pool indices which may
3483       // differ. However, the constants referred to by those indices
3484       // must be the same.
3485       //
3486       // We use methods_EMCP() for comparison since constant pool


3520       // the code that uses the back reference. This would lead to
3521       // brittle code that could be broken in non-obvious ways now or
3522       // in the future.
3523       //
3524       // Another possibility is to copy the ConstMethod* from the new
3525       // method to the old method and then overwrite the new method with
3526       // the old method. Since the ConstMethod* contains the bytecodes
3527       // for the method embedded in the oop, this option would change
3528       // the bytecodes out from under any threads executing the old
3529       // method and make the thread's bcp invalid. Since EMCP requires
3530       // that the bytecodes be the same modulo constant pool indices, it
3531       // is straight forward to compute the correct new bcp in the new
3532       // ConstMethod* from the old bcp in the old ConstMethod*. The
3533       // time consuming part would be searching all the frames in all
3534       // of the threads to find all of the calls to the old method.
3535       //
3536       // It looks like we will have to live with the limited savings
3537       // that we get from effectively overwriting the old methods
3538       // when the new methods are attached to the_class.
3539 
3540       // track which methods are EMCP for add_previous_version() call
3541       emcp_methods->set_bit(old_index);
3542       (*emcp_method_count_p)++;
3543 
3544       // An EMCP method is _not_ obsolete. An obsolete method has a
3545       // different jmethodID than the current method. An EMCP method
3546       // has the same jmethodID as the current method. Having the
3547       // same jmethodID for all EMCP versions of a method allows for
3548       // a consistent view of the EMCP methods regardless of which
3549       // EMCP method you happen to have in hand. For example, a
3550       // breakpoint set in one EMCP method will work for all EMCP
3551       // versions of the method including the current one.
3552     } else {
3553       // mark obsolete methods as such
3554       old_method->set_is_obsolete();
3555       obsolete_count++;
3556 
3557       // obsolete methods need a unique idnum so they become new entries in
3558       // the jmethodID cache in InstanceKlass
3559       assert(old_method->method_idnum() == new_method->method_idnum(), "must match");
3560       u2 num = InstanceKlass::cast(_the_class_oop)->next_method_idnum();
3561       if (num != ConstMethod::UNSET_IDNUM) {
3562         old_method->set_method_idnum(num);


3572     old_method->set_is_old();
3573   }
3574   for (int i = 0; i < _deleted_methods_length; ++i) {
3575     Method* old_method = _deleted_methods[i];
3576 
3577     assert(!old_method->has_vtable_index(),
3578            "cannot delete methods with vtable entries");;
3579 
3580     // Mark all deleted methods as old, obsolete and deleted
3581     old_method->set_is_deleted();
3582     old_method->set_is_old();
3583     old_method->set_is_obsolete();
3584     ++obsolete_count;
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 deleted %s(%s) as obsolete",
3589                           old_method->name()->as_C_string(),
3590                           old_method->signature()->as_C_string()));
3591   }
3592   assert((*emcp_method_count_p + obsolete_count) == _old_methods->length(),
3593     "sanity check");
3594   RC_TRACE(0x00000100, ("EMCP_cnt=%d, obsolete_cnt=%d", *emcp_method_count_p,
3595     obsolete_count));

3596 }
3597 
3598 // This internal class transfers the native function registration from old methods
3599 // to new methods.  It is designed to handle both the simple case of unchanged
3600 // native methods and the complex cases of native method prefixes being added and/or
3601 // removed.
3602 // It expects only to be used during the VM_RedefineClasses op (a safepoint).
3603 //
3604 // This class is used after the new methods have been installed in "the_class".
3605 //
3606 // So, for example, the following must be handled.  Where 'm' is a method and
3607 // a number followed by an underscore is a prefix.
3608 //
3609 //                                      Old Name    New Name
3610 // Simple transfer to new method        m       ->  m
3611 // Add prefix                           m       ->  1_m
3612 // Remove prefix                        1_m     ->  m
3613 // Simultaneous add of prefixes         m       ->  3_2_1_m
3614 // Simultaneous removal of prefixes     3_2_1_m ->  m
3615 // Simultaneous add and remove          1_m     ->  2_m


3956   // link in the method itself. For "the class"'s directly implemented
3957   // methods, the method holder is "the class" itself (as gotten from
3958   // the new constant pool). The check works fine in this case. The
3959   // check also works fine for methods inherited from super classes.
3960   //
3961   // Miranda methods are a little more complicated. A miranda method is
3962   // provided by an interface when the class implementing the interface
3963   // does not provide its own method.  These interfaces are implemented
3964   // internally as an InstanceKlass. These special instanceKlasses
3965   // share the constant pool of the class that "implements" the
3966   // interface. By sharing the constant pool, the method holder of a
3967   // miranda method is the class that "implements" the interface. In a
3968   // non-redefine situation, the subtype check works fine. However, if
3969   // the old constant pool's pool holder is modified, then the check
3970   // fails because there is no class hierarchy relationship between the
3971   // vtable's class and "the new class".
3972 
3973   old_constants->set_pool_holder(scratch_class());
3974 #endif
3975 
3976   // track which methods are EMCP for add_previous_version() call below
3977   BitMap emcp_methods(_old_methods->length());
3978   int emcp_method_count = 0;
3979   emcp_methods.clear();  // clears 0..(length() - 1)
3980   check_methods_and_mark_as_obsolete(&emcp_methods, &emcp_method_count);
3981   transfer_old_native_function_registrations(the_class);
3982 
3983   // The class file bytes from before any retransformable agents mucked
3984   // with them was cached on the scratch class, move to the_class.
3985   // Note: we still want to do this if nothing needed caching since it
3986   // should get cleared in the_class too.
3987   if (the_class->get_cached_class_file_bytes() == 0) {
3988     // the_class doesn't have a cache yet so copy it
3989     the_class->set_cached_class_file(scratch_class->get_cached_class_file());
3990   }
3991   else if (scratch_class->get_cached_class_file_bytes() !=
3992            the_class->get_cached_class_file_bytes()) {
3993     // The same class can be present twice in the scratch classes list or there
3994     // are multiple concurrent RetransformClasses calls on different threads.
3995     // In such cases we have to deallocate scratch_class cached_class_file.
3996     os::free(scratch_class->get_cached_class_file());
3997   }
3998 
3999   // NULL out in scratch class to not delete twice.  The class to be redefined
4000   // always owns these bytes.


4047   swap_annotations(the_class, scratch_class);
4048 
4049   // Replace minor version number of class file
4050   u2 old_minor_version = the_class->minor_version();
4051   the_class->set_minor_version(scratch_class->minor_version());
4052   scratch_class->set_minor_version(old_minor_version);
4053 
4054   // Replace major version number of class file
4055   u2 old_major_version = the_class->major_version();
4056   the_class->set_major_version(scratch_class->major_version());
4057   scratch_class->set_major_version(old_major_version);
4058 
4059   // Replace CP indexes for class and name+type of enclosing method
4060   u2 old_class_idx  = the_class->enclosing_method_class_index();
4061   u2 old_method_idx = the_class->enclosing_method_method_index();
4062   the_class->set_enclosing_method_indices(
4063     scratch_class->enclosing_method_class_index(),
4064     scratch_class->enclosing_method_method_index());
4065   scratch_class->set_enclosing_method_indices(old_class_idx, old_method_idx);
4066 


4067   // keep track of previous versions of this class
4068   the_class->add_previous_version(scratch_class, &emcp_methods,
4069     emcp_method_count);
4070 
4071   RC_TIMER_STOP(_timer_rsc_phase1);
4072   RC_TIMER_START(_timer_rsc_phase2);
4073 
4074   // Adjust constantpool caches and vtables for all classes
4075   // that reference methods of the evolved class.
4076   AdjustCpoolCacheAndVtable adjust_cpool_cache_and_vtable(THREAD);
4077   ClassLoaderDataGraph::classes_do(&adjust_cpool_cache_and_vtable);
4078 
4079   // JSR-292 support
4080   MemberNameTable* mnt = the_class->member_names();
4081   if (mnt != NULL) {
4082     bool trace_name_printed = false;
4083     mnt->adjust_method_entries(the_class(), &trace_name_printed);
4084   }
4085 
4086   if (the_class->oop_map_cache() != NULL) {
4087     // Flush references to any obsolete methods from the oop map cache
4088     // so that obsolete methods are not pinned.
4089     the_class->oop_map_cache()->flush_obsolete_entries();




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(pv_node, &trace_name_printed);
3444       }
3445     }
3446   }
3447 }
3448 
3449 void VM_RedefineClasses::update_jmethod_ids() {
3450   for (int j = 0; j < _matching_methods_length; ++j) {
3451     Method* old_method = _matching_old_methods[j];
3452     jmethodID jmid = old_method->find_jmethod_id_or_null();
3453     if (jmid != NULL) {
3454       // There is a jmethodID, change it to point to the new method
3455       methodHandle new_method_h(_matching_new_methods[j]);
3456       Method::change_method_associated_with_jmethod_id(jmid, new_method_h());
3457       assert(Method::resolve_jmethod_id(jmid) == _matching_new_methods[j],
3458              "should be replaced");
3459     }
3460   }
3461 }
3462 
3463 int VM_RedefineClasses::check_methods_and_mark_as_obsolete() {
3464   int emcp_method_count = 0;

3465   int obsolete_count = 0;
3466   int old_index = 0;
3467   for (int j = 0; j < _matching_methods_length; ++j, ++old_index) {
3468     Method* old_method = _matching_old_methods[j];
3469     Method* new_method = _matching_new_methods[j];
3470     Method* old_array_method;
3471 
3472     // Maintain an old_index into the _old_methods array by skipping
3473     // deleted methods
3474     while ((old_array_method = _old_methods->at(old_index)) != old_method) {
3475       ++old_index;
3476     }
3477 
3478     if (MethodComparator::methods_EMCP(old_method, new_method)) {
3479       // The EMCP definition from JSR-163 requires the bytecodes to be
3480       // the same with the exception of constant pool indices which may
3481       // differ. However, the constants referred to by those indices
3482       // must be the same.
3483       //
3484       // We use methods_EMCP() for comparison since constant pool


3518       // the code that uses the back reference. This would lead to
3519       // brittle code that could be broken in non-obvious ways now or
3520       // in the future.
3521       //
3522       // Another possibility is to copy the ConstMethod* from the new
3523       // method to the old method and then overwrite the new method with
3524       // the old method. Since the ConstMethod* contains the bytecodes
3525       // for the method embedded in the oop, this option would change
3526       // the bytecodes out from under any threads executing the old
3527       // method and make the thread's bcp invalid. Since EMCP requires
3528       // that the bytecodes be the same modulo constant pool indices, it
3529       // is straight forward to compute the correct new bcp in the new
3530       // ConstMethod* from the old bcp in the old ConstMethod*. The
3531       // time consuming part would be searching all the frames in all
3532       // of the threads to find all of the calls to the old method.
3533       //
3534       // It looks like we will have to live with the limited savings
3535       // that we get from effectively overwriting the old methods
3536       // when the new methods are attached to the_class.
3537 
3538       // Count number of methods that are EMCP.  The method will be marked
3539       // old but not obsolete if it is EMCP.
3540       emcp_method_count++;
3541 
3542       // An EMCP method is _not_ obsolete. An obsolete method has a
3543       // different jmethodID than the current method. An EMCP method
3544       // has the same jmethodID as the current method. Having the
3545       // same jmethodID for all EMCP versions of a method allows for
3546       // a consistent view of the EMCP methods regardless of which
3547       // EMCP method you happen to have in hand. For example, a
3548       // breakpoint set in one EMCP method will work for all EMCP
3549       // versions of the method including the current one.
3550     } else {
3551       // mark obsolete methods as such
3552       old_method->set_is_obsolete();
3553       obsolete_count++;
3554 
3555       // obsolete methods need a unique idnum so they become new entries in
3556       // the jmethodID cache in InstanceKlass
3557       assert(old_method->method_idnum() == new_method->method_idnum(), "must match");
3558       u2 num = InstanceKlass::cast(_the_class_oop)->next_method_idnum();
3559       if (num != ConstMethod::UNSET_IDNUM) {
3560         old_method->set_method_idnum(num);


3570     old_method->set_is_old();
3571   }
3572   for (int i = 0; i < _deleted_methods_length; ++i) {
3573     Method* old_method = _deleted_methods[i];
3574 
3575     assert(!old_method->has_vtable_index(),
3576            "cannot delete methods with vtable entries");;
3577 
3578     // Mark all deleted methods as old, obsolete and deleted
3579     old_method->set_is_deleted();
3580     old_method->set_is_old();
3581     old_method->set_is_obsolete();
3582     ++obsolete_count;
3583     // With tracing we try not to "yack" too much. The position of
3584     // this trace assumes there are fewer obsolete methods than
3585     // EMCP methods.
3586     RC_TRACE(0x00000100, ("mark deleted %s(%s) as obsolete",
3587                           old_method->name()->as_C_string(),
3588                           old_method->signature()->as_C_string()));
3589   }
3590   assert((emcp_method_count + obsolete_count) == _old_methods->length(),
3591     "sanity check");
3592   RC_TRACE(0x00000100, ("EMCP_cnt=%d, obsolete_cnt=%d", emcp_method_count,
3593     obsolete_count));
3594   return emcp_method_count;
3595 }
3596 
3597 // This internal class transfers the native function registration from old methods
3598 // to new methods.  It is designed to handle both the simple case of unchanged
3599 // native methods and the complex cases of native method prefixes being added and/or
3600 // removed.
3601 // It expects only to be used during the VM_RedefineClasses op (a safepoint).
3602 //
3603 // This class is used after the new methods have been installed in "the_class".
3604 //
3605 // So, for example, the following must be handled.  Where 'm' is a method and
3606 // a number followed by an underscore is a prefix.
3607 //
3608 //                                      Old Name    New Name
3609 // Simple transfer to new method        m       ->  m
3610 // Add prefix                           m       ->  1_m
3611 // Remove prefix                        1_m     ->  m
3612 // Simultaneous add of prefixes         m       ->  3_2_1_m
3613 // Simultaneous removal of prefixes     3_2_1_m ->  m
3614 // Simultaneous add and remove          1_m     ->  2_m


3955   // link in the method itself. For "the class"'s directly implemented
3956   // methods, the method holder is "the class" itself (as gotten from
3957   // the new constant pool). The check works fine in this case. The
3958   // check also works fine for methods inherited from super classes.
3959   //
3960   // Miranda methods are a little more complicated. A miranda method is
3961   // provided by an interface when the class implementing the interface
3962   // does not provide its own method.  These interfaces are implemented
3963   // internally as an InstanceKlass. These special instanceKlasses
3964   // share the constant pool of the class that "implements" the
3965   // interface. By sharing the constant pool, the method holder of a
3966   // miranda method is the class that "implements" the interface. In a
3967   // non-redefine situation, the subtype check works fine. However, if
3968   // the old constant pool's pool holder is modified, then the check
3969   // fails because there is no class hierarchy relationship between the
3970   // vtable's class and "the new class".
3971 
3972   old_constants->set_pool_holder(scratch_class());
3973 #endif
3974 
3975   // track number of methods that are EMCP for add_previous_version() call below
3976   int emcp_method_count = check_methods_and_mark_as_obsolete();



3977   transfer_old_native_function_registrations(the_class);
3978 
3979   // The class file bytes from before any retransformable agents mucked
3980   // with them was cached on the scratch class, move to the_class.
3981   // Note: we still want to do this if nothing needed caching since it
3982   // should get cleared in the_class too.
3983   if (the_class->get_cached_class_file_bytes() == 0) {
3984     // the_class doesn't have a cache yet so copy it
3985     the_class->set_cached_class_file(scratch_class->get_cached_class_file());
3986   }
3987   else if (scratch_class->get_cached_class_file_bytes() !=
3988            the_class->get_cached_class_file_bytes()) {
3989     // The same class can be present twice in the scratch classes list or there
3990     // are multiple concurrent RetransformClasses calls on different threads.
3991     // In such cases we have to deallocate scratch_class cached_class_file.
3992     os::free(scratch_class->get_cached_class_file());
3993   }
3994 
3995   // NULL out in scratch class to not delete twice.  The class to be redefined
3996   // always owns these bytes.


4043   swap_annotations(the_class, scratch_class);
4044 
4045   // Replace minor version number of class file
4046   u2 old_minor_version = the_class->minor_version();
4047   the_class->set_minor_version(scratch_class->minor_version());
4048   scratch_class->set_minor_version(old_minor_version);
4049 
4050   // Replace major version number of class file
4051   u2 old_major_version = the_class->major_version();
4052   the_class->set_major_version(scratch_class->major_version());
4053   scratch_class->set_major_version(old_major_version);
4054 
4055   // Replace CP indexes for class and name+type of enclosing method
4056   u2 old_class_idx  = the_class->enclosing_method_class_index();
4057   u2 old_method_idx = the_class->enclosing_method_method_index();
4058   the_class->set_enclosing_method_indices(
4059     scratch_class->enclosing_method_class_index(),
4060     scratch_class->enclosing_method_method_index());
4061   scratch_class->set_enclosing_method_indices(old_class_idx, old_method_idx);
4062 
4063   the_class->set_has_been_redefined();
4064 
4065   // keep track of previous versions of this class
4066   the_class->add_previous_version(scratch_class, emcp_method_count);

4067 
4068   RC_TIMER_STOP(_timer_rsc_phase1);
4069   RC_TIMER_START(_timer_rsc_phase2);
4070 
4071   // Adjust constantpool caches and vtables for all classes
4072   // that reference methods of the evolved class.
4073   AdjustCpoolCacheAndVtable adjust_cpool_cache_and_vtable(THREAD);
4074   ClassLoaderDataGraph::classes_do(&adjust_cpool_cache_and_vtable);
4075 
4076   // JSR-292 support
4077   MemberNameTable* mnt = the_class->member_names();
4078   if (mnt != NULL) {
4079     bool trace_name_printed = false;
4080     mnt->adjust_method_entries(the_class(), &trace_name_printed);
4081   }
4082 
4083   if (the_class->oop_map_cache() != NULL) {
4084     // Flush references to any obsolete methods from the oop map cache
4085     // so that obsolete methods are not pinned.
4086     the_class->oop_map_cache()->flush_obsolete_entries();


< prev index next >