2809 // been added as a previous version. The new_cp doesn't have any 2810 // cached references to old methods so it doesn't need to be 2811 // updated. We can simply start with the previous version(s) in 2812 // that case. 2813 constantPoolHandle other_cp; 2814 ConstantPoolCache* cp_cache; 2815 2816 if (ik != _the_class_oop) { 2817 // this klass' constant pool cache may need adjustment 2818 other_cp = constantPoolHandle(ik->constants()); 2819 cp_cache = other_cp->cache(); 2820 if (cp_cache != NULL) { 2821 cp_cache->adjust_method_entries(_matching_old_methods, 2822 _matching_new_methods, 2823 _matching_methods_length, 2824 &trace_name_printed); 2825 } 2826 } 2827 2828 // the previous versions' constant pool caches may need adjustment 2829 PreviousVersionWalker pvw(_thread, ik); 2830 for (PreviousVersionNode * pv_node = pvw.next_previous_version(); 2831 pv_node != NULL; pv_node = pvw.next_previous_version()) { 2832 other_cp = pv_node->prev_constant_pool(); 2833 cp_cache = other_cp->cache(); 2834 if (cp_cache != NULL) { 2835 cp_cache->adjust_method_entries(_matching_old_methods, 2836 _matching_new_methods, 2837 _matching_methods_length, 2838 &trace_name_printed); 2839 } 2840 } 2841 } 2842 } 2843 2844 void VM_RedefineClasses::update_jmethod_ids() { 2845 for (int j = 0; j < _matching_methods_length; ++j) { 2846 Method* old_method = _matching_old_methods[j]; 2847 jmethodID jmid = old_method->find_jmethod_id_or_null(); 2848 if (jmid != NULL) { 2849 // There is a jmethodID, change it to point to the new method 2850 methodHandle new_method_h(_matching_new_methods[j]); 2851 Method::change_method_associated_with_jmethod_id(jmid, new_method_h()); 2852 assert(Method::resolve_jmethod_id(jmid) == _matching_new_methods[j], 2853 "should be replaced"); 2854 } 2855 } 2856 } 2857 2858 void VM_RedefineClasses::check_methods_and_mark_as_obsolete( 2859 BitMap *emcp_methods, int * emcp_method_count_p) { 2860 *emcp_method_count_p = 0; 2861 int obsolete_count = 0; 2862 int old_index = 0; 2863 for (int j = 0; j < _matching_methods_length; ++j, ++old_index) { 2864 Method* old_method = _matching_old_methods[j]; 2865 Method* new_method = _matching_new_methods[j]; 2866 Method* old_array_method; 2867 2868 // Maintain an old_index into the _old_methods array by skipping 2869 // deleted methods 2870 while ((old_array_method = _old_methods->at(old_index)) != old_method) { 2871 ++old_index; 2872 } 2873 2874 if (MethodComparator::methods_EMCP(old_method, new_method)) { 2875 // The EMCP definition from JSR-163 requires the bytecodes to be 2876 // the same with the exception of constant pool indices which may 2877 // differ. However, the constants referred to by those indices 2878 // must be the same. 2879 // 2880 // We use methods_EMCP() for comparison since constant pool 2914 // the code that uses the back reference. This would lead to 2915 // brittle code that could be broken in non-obvious ways now or 2916 // in the future. 2917 // 2918 // Another possibility is to copy the ConstMethod* from the new 2919 // method to the old method and then overwrite the new method with 2920 // the old method. Since the ConstMethod* contains the bytecodes 2921 // for the method embedded in the oop, this option would change 2922 // the bytecodes out from under any threads executing the old 2923 // method and make the thread's bcp invalid. Since EMCP requires 2924 // that the bytecodes be the same modulo constant pool indices, it 2925 // is straight forward to compute the correct new bcp in the new 2926 // ConstMethod* from the old bcp in the old ConstMethod*. The 2927 // time consuming part would be searching all the frames in all 2928 // of the threads to find all of the calls to the old method. 2929 // 2930 // It looks like we will have to live with the limited savings 2931 // that we get from effectively overwriting the old methods 2932 // when the new methods are attached to the_class. 2933 2934 // track which methods are EMCP for add_previous_version() call 2935 emcp_methods->set_bit(old_index); 2936 (*emcp_method_count_p)++; 2937 2938 // An EMCP method is _not_ obsolete. An obsolete method has a 2939 // different jmethodID than the current method. An EMCP method 2940 // has the same jmethodID as the current method. Having the 2941 // same jmethodID for all EMCP versions of a method allows for 2942 // a consistent view of the EMCP methods regardless of which 2943 // EMCP method you happen to have in hand. For example, a 2944 // breakpoint set in one EMCP method will work for all EMCP 2945 // versions of the method including the current one. 2946 } else { 2947 // mark obsolete methods as such 2948 old_method->set_is_obsolete(); 2949 obsolete_count++; 2950 2951 // obsolete methods need a unique idnum so they become new entries in 2952 // the jmethodID cache in InstanceKlass 2953 u2 num = InstanceKlass::cast(_the_class_oop)->next_method_idnum(); 2954 if (num != ConstMethod::UNSET_IDNUM) { 2955 old_method->set_method_idnum(num); 2956 } 2965 old_method->set_is_old(); 2966 } 2967 for (int i = 0; i < _deleted_methods_length; ++i) { 2968 Method* old_method = _deleted_methods[i]; 2969 2970 assert(!old_method->has_vtable_index(), 2971 "cannot delete methods with vtable entries");; 2972 2973 // Mark all deleted methods as old, obsolete and deleted 2974 old_method->set_is_deleted(); 2975 old_method->set_is_old(); 2976 old_method->set_is_obsolete(); 2977 ++obsolete_count; 2978 // With tracing we try not to "yack" too much. The position of 2979 // this trace assumes there are fewer obsolete methods than 2980 // EMCP methods. 2981 RC_TRACE(0x00000100, ("mark deleted %s(%s) as obsolete", 2982 old_method->name()->as_C_string(), 2983 old_method->signature()->as_C_string())); 2984 } 2985 assert((*emcp_method_count_p + obsolete_count) == _old_methods->length(), 2986 "sanity check"); 2987 RC_TRACE(0x00000100, ("EMCP_cnt=%d, obsolete_cnt=%d", *emcp_method_count_p, 2988 obsolete_count)); 2989 } 2990 2991 // This internal class transfers the native function registration from old methods 2992 // to new methods. It is designed to handle both the simple case of unchanged 2993 // native methods and the complex cases of native method prefixes being added and/or 2994 // removed. 2995 // It expects only to be used during the VM_RedefineClasses op (a safepoint). 2996 // 2997 // This class is used after the new methods have been installed in "the_class". 2998 // 2999 // So, for example, the following must be handled. Where 'm' is a method and 3000 // a number followed by an underscore is a prefix. 3001 // 3002 // Old Name New Name 3003 // Simple transfer to new method m -> m 3004 // Add prefix m -> 1_m 3005 // Remove prefix 1_m -> m 3006 // Simultaneous add of prefixes m -> 3_2_1_m 3007 // Simultaneous removal of prefixes 3_2_1_m -> m 3008 // Simultaneous add and remove 1_m -> 2_m 3362 // link in the method itself. For "the class"'s directly implemented 3363 // methods, the method holder is "the class" itself (as gotten from 3364 // the new constant pool). The check works fine in this case. The 3365 // check also works fine for methods inherited from super classes. 3366 // 3367 // Miranda methods are a little more complicated. A miranda method is 3368 // provided by an interface when the class implementing the interface 3369 // does not provide its own method. These interfaces are implemented 3370 // internally as an InstanceKlass. These special instanceKlasses 3371 // share the constant pool of the class that "implements" the 3372 // interface. By sharing the constant pool, the method holder of a 3373 // miranda method is the class that "implements" the interface. In a 3374 // non-redefine situation, the subtype check works fine. However, if 3375 // the old constant pool's pool holder is modified, then the check 3376 // fails because there is no class hierarchy relationship between the 3377 // vtable's class and "the new class". 3378 3379 old_constants->set_pool_holder(scratch_class()); 3380 #endif 3381 3382 // track which methods are EMCP for add_previous_version() call below 3383 BitMap emcp_methods(_old_methods->length()); 3384 int emcp_method_count = 0; 3385 emcp_methods.clear(); // clears 0..(length() - 1) 3386 check_methods_and_mark_as_obsolete(&emcp_methods, &emcp_method_count); 3387 transfer_old_native_function_registrations(the_class); 3388 3389 // The class file bytes from before any retransformable agents mucked 3390 // with them was cached on the scratch class, move to the_class. 3391 // Note: we still want to do this if nothing needed caching since it 3392 // should get cleared in the_class too. 3393 if (the_class->get_cached_class_file_bytes() == 0) { 3394 // the_class doesn't have a cache yet so copy it 3395 the_class->set_cached_class_file(scratch_class->get_cached_class_file()); 3396 } 3397 #ifndef PRODUCT 3398 else { 3399 assert(the_class->get_cached_class_file_bytes() == 3400 scratch_class->get_cached_class_file_bytes(), "cache ptrs must match"); 3401 assert(the_class->get_cached_class_file_len() == 3402 scratch_class->get_cached_class_file_len(), "cache lens must match"); 3403 } 3404 #endif 3405 3406 // NULL out in scratch class to not delete twice. The class to be redefined 3454 swap_annotations(the_class, scratch_class); 3455 3456 // Replace minor version number of class file 3457 u2 old_minor_version = the_class->minor_version(); 3458 the_class->set_minor_version(scratch_class->minor_version()); 3459 scratch_class->set_minor_version(old_minor_version); 3460 3461 // Replace major version number of class file 3462 u2 old_major_version = the_class->major_version(); 3463 the_class->set_major_version(scratch_class->major_version()); 3464 scratch_class->set_major_version(old_major_version); 3465 3466 // Replace CP indexes for class and name+type of enclosing method 3467 u2 old_class_idx = the_class->enclosing_method_class_index(); 3468 u2 old_method_idx = the_class->enclosing_method_method_index(); 3469 the_class->set_enclosing_method_indices( 3470 scratch_class->enclosing_method_class_index(), 3471 scratch_class->enclosing_method_method_index()); 3472 scratch_class->set_enclosing_method_indices(old_class_idx, old_method_idx); 3473 3474 // keep track of previous versions of this class 3475 the_class->add_previous_version(scratch_class, &emcp_methods, 3476 emcp_method_count); 3477 3478 RC_TIMER_STOP(_timer_rsc_phase1); 3479 RC_TIMER_START(_timer_rsc_phase2); 3480 3481 // Adjust constantpool caches and vtables for all classes 3482 // that reference methods of the evolved class. 3483 AdjustCpoolCacheAndVtable adjust_cpool_cache_and_vtable(THREAD); 3484 ClassLoaderDataGraph::classes_do(&adjust_cpool_cache_and_vtable); 3485 3486 // JSR-292 support 3487 MemberNameTable* mnt = the_class->member_names(); 3488 if (mnt != NULL) { 3489 bool trace_name_printed = false; 3490 mnt->adjust_method_entries(_matching_old_methods, 3491 _matching_new_methods, 3492 _matching_methods_length, 3493 &trace_name_printed); 3494 } 3495 3496 // Fix Resolution Error table also to remove old constant pools | 2809 // been added as a previous version. The new_cp doesn't have any 2810 // cached references to old methods so it doesn't need to be 2811 // updated. We can simply start with the previous version(s) in 2812 // that case. 2813 constantPoolHandle other_cp; 2814 ConstantPoolCache* cp_cache; 2815 2816 if (ik != _the_class_oop) { 2817 // this klass' constant pool cache may need adjustment 2818 other_cp = constantPoolHandle(ik->constants()); 2819 cp_cache = other_cp->cache(); 2820 if (cp_cache != NULL) { 2821 cp_cache->adjust_method_entries(_matching_old_methods, 2822 _matching_new_methods, 2823 _matching_methods_length, 2824 &trace_name_printed); 2825 } 2826 } 2827 2828 // the previous versions' constant pool caches may need adjustment 2829 for (InstanceKlass* pv_node = ik->previous_versions(); 2830 pv_node != NULL; 2831 pv_node = pv_node->previous_versions()) { 2832 cp_cache = pv_node->constants()->cache(); 2833 if (cp_cache != NULL) { 2834 cp_cache->adjust_method_entries(_matching_old_methods, 2835 _matching_new_methods, 2836 _matching_methods_length, 2837 &trace_name_printed); 2838 } 2839 } 2840 } 2841 } 2842 2843 void VM_RedefineClasses::update_jmethod_ids() { 2844 for (int j = 0; j < _matching_methods_length; ++j) { 2845 Method* old_method = _matching_old_methods[j]; 2846 jmethodID jmid = old_method->find_jmethod_id_or_null(); 2847 if (jmid != NULL) { 2848 // There is a jmethodID, change it to point to the new method 2849 methodHandle new_method_h(_matching_new_methods[j]); 2850 Method::change_method_associated_with_jmethod_id(jmid, new_method_h()); 2851 assert(Method::resolve_jmethod_id(jmid) == _matching_new_methods[j], 2852 "should be replaced"); 2853 } 2854 } 2855 } 2856 2857 int VM_RedefineClasses::check_methods_and_mark_as_obsolete() { 2858 int emcp_method_count = 0; 2859 int obsolete_count = 0; 2860 int old_index = 0; 2861 for (int j = 0; j < _matching_methods_length; ++j, ++old_index) { 2862 Method* old_method = _matching_old_methods[j]; 2863 Method* new_method = _matching_new_methods[j]; 2864 Method* old_array_method; 2865 2866 // Maintain an old_index into the _old_methods array by skipping 2867 // deleted methods 2868 while ((old_array_method = _old_methods->at(old_index)) != old_method) { 2869 ++old_index; 2870 } 2871 2872 if (MethodComparator::methods_EMCP(old_method, new_method)) { 2873 // The EMCP definition from JSR-163 requires the bytecodes to be 2874 // the same with the exception of constant pool indices which may 2875 // differ. However, the constants referred to by those indices 2876 // must be the same. 2877 // 2878 // We use methods_EMCP() for comparison since constant pool 2912 // the code that uses the back reference. This would lead to 2913 // brittle code that could be broken in non-obvious ways now or 2914 // in the future. 2915 // 2916 // Another possibility is to copy the ConstMethod* from the new 2917 // method to the old method and then overwrite the new method with 2918 // the old method. Since the ConstMethod* contains the bytecodes 2919 // for the method embedded in the oop, this option would change 2920 // the bytecodes out from under any threads executing the old 2921 // method and make the thread's bcp invalid. Since EMCP requires 2922 // that the bytecodes be the same modulo constant pool indices, it 2923 // is straight forward to compute the correct new bcp in the new 2924 // ConstMethod* from the old bcp in the old ConstMethod*. The 2925 // time consuming part would be searching all the frames in all 2926 // of the threads to find all of the calls to the old method. 2927 // 2928 // It looks like we will have to live with the limited savings 2929 // that we get from effectively overwriting the old methods 2930 // when the new methods are attached to the_class. 2931 2932 // Count number of methods that are EMCP. The method will be marked 2933 // old but not obsolete if it is EMCP. 2934 emcp_method_count++; 2935 2936 // An EMCP method is _not_ obsolete. An obsolete method has a 2937 // different jmethodID than the current method. An EMCP method 2938 // has the same jmethodID as the current method. Having the 2939 // same jmethodID for all EMCP versions of a method allows for 2940 // a consistent view of the EMCP methods regardless of which 2941 // EMCP method you happen to have in hand. For example, a 2942 // breakpoint set in one EMCP method will work for all EMCP 2943 // versions of the method including the current one. 2944 } else { 2945 // mark obsolete methods as such 2946 old_method->set_is_obsolete(); 2947 obsolete_count++; 2948 2949 // obsolete methods need a unique idnum so they become new entries in 2950 // the jmethodID cache in InstanceKlass 2951 u2 num = InstanceKlass::cast(_the_class_oop)->next_method_idnum(); 2952 if (num != ConstMethod::UNSET_IDNUM) { 2953 old_method->set_method_idnum(num); 2954 } 2963 old_method->set_is_old(); 2964 } 2965 for (int i = 0; i < _deleted_methods_length; ++i) { 2966 Method* old_method = _deleted_methods[i]; 2967 2968 assert(!old_method->has_vtable_index(), 2969 "cannot delete methods with vtable entries");; 2970 2971 // Mark all deleted methods as old, obsolete and deleted 2972 old_method->set_is_deleted(); 2973 old_method->set_is_old(); 2974 old_method->set_is_obsolete(); 2975 ++obsolete_count; 2976 // With tracing we try not to "yack" too much. The position of 2977 // this trace assumes there are fewer obsolete methods than 2978 // EMCP methods. 2979 RC_TRACE(0x00000100, ("mark deleted %s(%s) as obsolete", 2980 old_method->name()->as_C_string(), 2981 old_method->signature()->as_C_string())); 2982 } 2983 assert((emcp_method_count + obsolete_count) == _old_methods->length(), 2984 "sanity check"); 2985 RC_TRACE(0x00000100, ("EMCP_cnt=%d, obsolete_cnt=%d", emcp_method_count, 2986 obsolete_count)); 2987 return emcp_method_count; 2988 } 2989 2990 // This internal class transfers the native function registration from old methods 2991 // to new methods. It is designed to handle both the simple case of unchanged 2992 // native methods and the complex cases of native method prefixes being added and/or 2993 // removed. 2994 // It expects only to be used during the VM_RedefineClasses op (a safepoint). 2995 // 2996 // This class is used after the new methods have been installed in "the_class". 2997 // 2998 // So, for example, the following must be handled. Where 'm' is a method and 2999 // a number followed by an underscore is a prefix. 3000 // 3001 // Old Name New Name 3002 // Simple transfer to new method m -> m 3003 // Add prefix m -> 1_m 3004 // Remove prefix 1_m -> m 3005 // Simultaneous add of prefixes m -> 3_2_1_m 3006 // Simultaneous removal of prefixes 3_2_1_m -> m 3007 // Simultaneous add and remove 1_m -> 2_m 3361 // link in the method itself. For "the class"'s directly implemented 3362 // methods, the method holder is "the class" itself (as gotten from 3363 // the new constant pool). The check works fine in this case. The 3364 // check also works fine for methods inherited from super classes. 3365 // 3366 // Miranda methods are a little more complicated. A miranda method is 3367 // provided by an interface when the class implementing the interface 3368 // does not provide its own method. These interfaces are implemented 3369 // internally as an InstanceKlass. These special instanceKlasses 3370 // share the constant pool of the class that "implements" the 3371 // interface. By sharing the constant pool, the method holder of a 3372 // miranda method is the class that "implements" the interface. In a 3373 // non-redefine situation, the subtype check works fine. However, if 3374 // the old constant pool's pool holder is modified, then the check 3375 // fails because there is no class hierarchy relationship between the 3376 // vtable's class and "the new class". 3377 3378 old_constants->set_pool_holder(scratch_class()); 3379 #endif 3380 3381 // track number of methods are EMCP for add_previous_version() call below 3382 int emcp_method_count = check_methods_and_mark_as_obsolete(); 3383 transfer_old_native_function_registrations(the_class); 3384 3385 // The class file bytes from before any retransformable agents mucked 3386 // with them was cached on the scratch class, move to the_class. 3387 // Note: we still want to do this if nothing needed caching since it 3388 // should get cleared in the_class too. 3389 if (the_class->get_cached_class_file_bytes() == 0) { 3390 // the_class doesn't have a cache yet so copy it 3391 the_class->set_cached_class_file(scratch_class->get_cached_class_file()); 3392 } 3393 #ifndef PRODUCT 3394 else { 3395 assert(the_class->get_cached_class_file_bytes() == 3396 scratch_class->get_cached_class_file_bytes(), "cache ptrs must match"); 3397 assert(the_class->get_cached_class_file_len() == 3398 scratch_class->get_cached_class_file_len(), "cache lens must match"); 3399 } 3400 #endif 3401 3402 // NULL out in scratch class to not delete twice. The class to be redefined 3450 swap_annotations(the_class, scratch_class); 3451 3452 // Replace minor version number of class file 3453 u2 old_minor_version = the_class->minor_version(); 3454 the_class->set_minor_version(scratch_class->minor_version()); 3455 scratch_class->set_minor_version(old_minor_version); 3456 3457 // Replace major version number of class file 3458 u2 old_major_version = the_class->major_version(); 3459 the_class->set_major_version(scratch_class->major_version()); 3460 scratch_class->set_major_version(old_major_version); 3461 3462 // Replace CP indexes for class and name+type of enclosing method 3463 u2 old_class_idx = the_class->enclosing_method_class_index(); 3464 u2 old_method_idx = the_class->enclosing_method_method_index(); 3465 the_class->set_enclosing_method_indices( 3466 scratch_class->enclosing_method_class_index(), 3467 scratch_class->enclosing_method_method_index()); 3468 scratch_class->set_enclosing_method_indices(old_class_idx, old_method_idx); 3469 3470 the_class->set_has_been_redefined(); 3471 3472 // keep track of previous versions of this class 3473 the_class->add_previous_version(scratch_class, emcp_method_count); 3474 3475 RC_TIMER_STOP(_timer_rsc_phase1); 3476 RC_TIMER_START(_timer_rsc_phase2); 3477 3478 // Adjust constantpool caches and vtables for all classes 3479 // that reference methods of the evolved class. 3480 AdjustCpoolCacheAndVtable adjust_cpool_cache_and_vtable(THREAD); 3481 ClassLoaderDataGraph::classes_do(&adjust_cpool_cache_and_vtable); 3482 3483 // JSR-292 support 3484 MemberNameTable* mnt = the_class->member_names(); 3485 if (mnt != NULL) { 3486 bool trace_name_printed = false; 3487 mnt->adjust_method_entries(_matching_old_methods, 3488 _matching_new_methods, 3489 _matching_methods_length, 3490 &trace_name_printed); 3491 } 3492 3493 // Fix Resolution Error table also to remove old constant pools |