587 if (scratch_i != *merge_cp_length_p) {
588 // The new entry in *merge_cp_p is at a different index than
589 // the new entry in scratch_cp so we need to map the index values.
590 map_index(scratch_cp, scratch_i, *merge_cp_length_p);
591 }
592 (*merge_cp_length_p)++;
593 } break;
594
595 // At this stage, Class or UnresolvedClass could be in scratch_cp, but not
596 // ClassIndex
597 case JVM_CONSTANT_ClassIndex: // fall through
598
599 // Invalid is used as the tag for the second constant pool entry
600 // occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should
601 // not be seen by itself.
602 case JVM_CONSTANT_Invalid: // fall through
603
604 // At this stage, String could be here, but not StringIndex
605 case JVM_CONSTANT_StringIndex: // fall through
606
607 // At this stage JVM_CONSTANT_UnresolvedClassInError should not be
608 // here
609 case JVM_CONSTANT_UnresolvedClassInError: // fall through
610
611 default:
612 {
613 // leave a breadcrumb
614 jbyte bad_value = scratch_cp->tag_at(scratch_i).value();
615 ShouldNotReachHere();
616 } break;
617 } // end switch tag value
618 } // end append_entry()
619
620
621 int VM_RedefineClasses::find_or_append_indirect_entry(const constantPoolHandle& scratch_cp,
622 int ref_i, constantPoolHandle *merge_cp_p, int *merge_cp_length_p) {
623
624 int new_ref_i = ref_i;
625 bool match = (ref_i < *merge_cp_length_p) &&
626 scratch_cp->compare_entry_to(ref_i, *merge_cp_p, ref_i);
627
628 if (!match) {
898 the_class->external_name(), action_str);
899 return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_CLASS_ATTRIBUTE_CHANGED;
900 }
901
902 return JVMTI_ERROR_NONE;
903 }
904
905
906 static jvmtiError check_permitted_subclasses_attribute(InstanceKlass* the_class,
907 InstanceKlass* scratch_class) {
908 Thread* thread = Thread::current();
909 ResourceMark rm(thread);
910
911 // Check whether the class PermittedSubclasses attribute has been changed.
912 return check_attribute_arrays("PermittedSubclasses",
913 the_class, scratch_class,
914 the_class->permitted_subclasses(),
915 scratch_class->permitted_subclasses());
916 }
917
918 static bool can_add_or_delete(Method* m) {
919 // Compatibility mode
920 return (AllowRedefinitionToAddDeleteMethods &&
921 (m->is_private() && (m->is_static() || m->is_final())));
922 }
923
924 jvmtiError VM_RedefineClasses::compare_and_normalize_class_versions(
925 InstanceKlass* the_class,
926 InstanceKlass* scratch_class) {
927 int i;
928
929 // Check superclasses, or rather their names, since superclasses themselves can be
930 // requested to replace.
931 // Check for NULL superclass first since this might be java.lang.Object
932 if (the_class->super() != scratch_class->super() &&
933 (the_class->super() == NULL || scratch_class->super() == NULL ||
934 the_class->super()->name() !=
935 scratch_class->super()->name())) {
936 log_info(redefine, class, normalize)
937 ("redefined class %s superclass change error: superclass changed from %s to %s.",
977 }
978
979 // Check whether the nest-related attributes have been changed.
980 jvmtiError err = check_nest_attributes(the_class, scratch_class);
981 if (err != JVMTI_ERROR_NONE) {
982 return err;
983 }
984
985 // Check whether the Record attribute has been changed.
986 err = check_record_attribute(the_class, scratch_class);
987 if (err != JVMTI_ERROR_NONE) {
988 return err;
989 }
990
991 // Check whether the PermittedSubclasses attribute has been changed.
992 err = check_permitted_subclasses_attribute(the_class, scratch_class);
993 if (err != JVMTI_ERROR_NONE) {
994 return err;
995 }
996
997 // Check whether class modifiers are the same.
998 jushort old_flags = (jushort) the_class->access_flags().get_flags();
999 jushort new_flags = (jushort) scratch_class->access_flags().get_flags();
1000 if (old_flags != new_flags) {
1001 log_info(redefine, class, normalize)
1002 ("redefined class %s modifiers change error: modifiers changed from %d to %d.",
1003 the_class->external_name(), old_flags, new_flags);
1004 return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_CLASS_MODIFIERS_CHANGED;
1005 }
1006
1007 // Check if the number, names, types and order of fields declared in these classes
1008 // are the same.
1009 JavaFieldStream old_fs(the_class);
1010 JavaFieldStream new_fs(scratch_class);
1011 for (; !old_fs.done() && !new_fs.done(); old_fs.next(), new_fs.next()) {
1012 // name and signature
1013 Symbol* name_sym1 = the_class->constants()->symbol_at(old_fs.name_index());
1014 Symbol* sig_sym1 = the_class->constants()->symbol_at(old_fs.signature_index());
1015 Symbol* name_sym2 = scratch_class->constants()->symbol_at(new_fs.name_index());
1016 Symbol* sig_sym2 = scratch_class->constants()->symbol_at(new_fs.signature_index());
1946 bool VM_RedefineClasses::rewrite_cp_refs(InstanceKlass* scratch_class) {
1947
1948 // rewrite constant pool references in the nest attributes:
1949 if (!rewrite_cp_refs_in_nest_attributes(scratch_class)) {
1950 // propagate failure back to caller
1951 return false;
1952 }
1953
1954 // rewrite constant pool references in the Record attribute:
1955 if (!rewrite_cp_refs_in_record_attribute(scratch_class)) {
1956 // propagate failure back to caller
1957 return false;
1958 }
1959
1960 // rewrite constant pool references in the PermittedSubclasses attribute:
1961 if (!rewrite_cp_refs_in_permitted_subclasses_attribute(scratch_class)) {
1962 // propagate failure back to caller
1963 return false;
1964 }
1965
1966 // rewrite constant pool references in the methods:
1967 if (!rewrite_cp_refs_in_methods(scratch_class)) {
1968 // propagate failure back to caller
1969 return false;
1970 }
1971
1972 // rewrite constant pool references in the class_annotations:
1973 if (!rewrite_cp_refs_in_class_annotations(scratch_class)) {
1974 // propagate failure back to caller
1975 return false;
1976 }
1977
1978 // rewrite constant pool references in the fields_annotations:
1979 if (!rewrite_cp_refs_in_fields_annotations(scratch_class)) {
1980 // propagate failure back to caller
1981 return false;
1982 }
1983
1984 // rewrite constant pool references in the methods_annotations:
1985 if (!rewrite_cp_refs_in_methods_annotations(scratch_class)) {
2094 }
2095 }
2096 }
2097 }
2098 return true;
2099 }
2100
2101 // Rewrite constant pool references in the PermittedSubclasses attribute.
2102 bool VM_RedefineClasses::rewrite_cp_refs_in_permitted_subclasses_attribute(
2103 InstanceKlass* scratch_class) {
2104
2105 Array<u2>* permitted_subclasses = scratch_class->permitted_subclasses();
2106 assert(permitted_subclasses != NULL, "unexpected null permitted_subclasses");
2107 for (int i = 0; i < permitted_subclasses->length(); i++) {
2108 u2 cp_index = permitted_subclasses->at(i);
2109 permitted_subclasses->at_put(i, find_new_index(cp_index));
2110 }
2111 return true;
2112 }
2113
2114 // Rewrite constant pool references in the methods.
2115 bool VM_RedefineClasses::rewrite_cp_refs_in_methods(InstanceKlass* scratch_class) {
2116
2117 Array<Method*>* methods = scratch_class->methods();
2118
2119 if (methods == NULL || methods->length() == 0) {
2120 // no methods so nothing to do
2121 return true;
2122 }
2123
2124 JavaThread* THREAD = JavaThread::current(); // For exception macros.
2125 ExceptionMark em(THREAD);
2126
2127 // rewrite constant pool references in the methods:
2128 for (int i = methods->length() - 1; i >= 0; i--) {
2129 methodHandle method(THREAD, methods->at(i));
2130 methodHandle new_method;
2131 rewrite_cp_refs_in_method(method, &new_method, THREAD);
2132 if (!new_method.is_null()) {
2133 // the method has been replaced so save the new method version
2226 // return the new method so that the caller can update
2227 // the containing class
2228 *new_method_p = method = m;
2229 // switch our bytecode processing loop from the old method
2230 // to the new method
2231 code_base = method->code_base();
2232 code_length = method->code_size();
2233 bcp = code_base + bci;
2234 c = (Bytecodes::Code)(*bcp);
2235 bc_length = Bytecodes::length_for(c);
2236 assert(bc_length != 0, "sanity check");
2237 } // end we need ldc_w instead of ldc
2238 } // end if there is a mapped index
2239 } break;
2240
2241 // these bytecodes have a two-byte constant pool index
2242 case Bytecodes::_anewarray : // fall through
2243 case Bytecodes::_checkcast : // fall through
2244 case Bytecodes::_getfield : // fall through
2245 case Bytecodes::_getstatic : // fall through
2246 case Bytecodes::_instanceof : // fall through
2247 case Bytecodes::_invokedynamic : // fall through
2248 case Bytecodes::_invokeinterface: // fall through
2249 case Bytecodes::_invokespecial : // fall through
2250 case Bytecodes::_invokestatic : // fall through
2251 case Bytecodes::_invokevirtual : // fall through
2252 case Bytecodes::_ldc_w : // fall through
2253 case Bytecodes::_ldc2_w : // fall through
2254 case Bytecodes::_multianewarray : // fall through
2255 case Bytecodes::_new : // fall through
2256 case Bytecodes::_putfield : // fall through
2257 case Bytecodes::_putstatic :
2258 {
2259 address p = bcp + 1;
2260 int cp_index = Bytes::get_Java_u2(p);
2261 int new_index = find_new_index(cp_index);
2262 if (new_index != 0) {
2263 // the original index is mapped so update w/ new value
2264 log_trace(redefine, class, constantpool)
2265 ("%s@" INTPTR_FORMAT " old=%d, new=%d", Bytecodes::name(c),p2i(bcp), cp_index, new_index);
|
587 if (scratch_i != *merge_cp_length_p) {
588 // The new entry in *merge_cp_p is at a different index than
589 // the new entry in scratch_cp so we need to map the index values.
590 map_index(scratch_cp, scratch_i, *merge_cp_length_p);
591 }
592 (*merge_cp_length_p)++;
593 } break;
594
595 // At this stage, Class or UnresolvedClass could be in scratch_cp, but not
596 // ClassIndex
597 case JVM_CONSTANT_ClassIndex: // fall through
598
599 // Invalid is used as the tag for the second constant pool entry
600 // occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should
601 // not be seen by itself.
602 case JVM_CONSTANT_Invalid: // fall through
603
604 // At this stage, String could be here, but not StringIndex
605 case JVM_CONSTANT_StringIndex: // fall through
606
607 // At this stage JVM_CONSTANT_UnresolvedClassInError should not be here
608 case JVM_CONSTANT_UnresolvedClassInError: // fall through
609
610 default:
611 {
612 // leave a breadcrumb
613 jbyte bad_value = scratch_cp->tag_at(scratch_i).value();
614 ShouldNotReachHere();
615 } break;
616 } // end switch tag value
617 } // end append_entry()
618
619
620 int VM_RedefineClasses::find_or_append_indirect_entry(const constantPoolHandle& scratch_cp,
621 int ref_i, constantPoolHandle *merge_cp_p, int *merge_cp_length_p) {
622
623 int new_ref_i = ref_i;
624 bool match = (ref_i < *merge_cp_length_p) &&
625 scratch_cp->compare_entry_to(ref_i, *merge_cp_p, ref_i);
626
627 if (!match) {
897 the_class->external_name(), action_str);
898 return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_CLASS_ATTRIBUTE_CHANGED;
899 }
900
901 return JVMTI_ERROR_NONE;
902 }
903
904
905 static jvmtiError check_permitted_subclasses_attribute(InstanceKlass* the_class,
906 InstanceKlass* scratch_class) {
907 Thread* thread = Thread::current();
908 ResourceMark rm(thread);
909
910 // Check whether the class PermittedSubclasses attribute has been changed.
911 return check_attribute_arrays("PermittedSubclasses",
912 the_class, scratch_class,
913 the_class->permitted_subclasses(),
914 scratch_class->permitted_subclasses());
915 }
916
917 static jvmtiError check_preload_attribute(InstanceKlass* the_class,
918 InstanceKlass* scratch_class) {
919 Thread* thread = Thread::current();
920 ResourceMark rm(thread);
921
922 // Check whether the class Preload attribute has been changed.
923 return check_attribute_arrays("Preload",
924 the_class, scratch_class,
925 the_class->preload_classes(),
926 scratch_class->preload_classes());
927 }
928
929 static bool can_add_or_delete(Method* m) {
930 // Compatibility mode
931 return (AllowRedefinitionToAddDeleteMethods &&
932 (m->is_private() && (m->is_static() || m->is_final())));
933 }
934
935 jvmtiError VM_RedefineClasses::compare_and_normalize_class_versions(
936 InstanceKlass* the_class,
937 InstanceKlass* scratch_class) {
938 int i;
939
940 // Check superclasses, or rather their names, since superclasses themselves can be
941 // requested to replace.
942 // Check for NULL superclass first since this might be java.lang.Object
943 if (the_class->super() != scratch_class->super() &&
944 (the_class->super() == NULL || scratch_class->super() == NULL ||
945 the_class->super()->name() !=
946 scratch_class->super()->name())) {
947 log_info(redefine, class, normalize)
948 ("redefined class %s superclass change error: superclass changed from %s to %s.",
988 }
989
990 // Check whether the nest-related attributes have been changed.
991 jvmtiError err = check_nest_attributes(the_class, scratch_class);
992 if (err != JVMTI_ERROR_NONE) {
993 return err;
994 }
995
996 // Check whether the Record attribute has been changed.
997 err = check_record_attribute(the_class, scratch_class);
998 if (err != JVMTI_ERROR_NONE) {
999 return err;
1000 }
1001
1002 // Check whether the PermittedSubclasses attribute has been changed.
1003 err = check_permitted_subclasses_attribute(the_class, scratch_class);
1004 if (err != JVMTI_ERROR_NONE) {
1005 return err;
1006 }
1007
1008 // Check whether the Preload attribute has been changed.
1009 err = check_preload_attribute(the_class, scratch_class);
1010 if (err != JVMTI_ERROR_NONE) {
1011 return err;
1012 }
1013
1014 // Check whether class modifiers are the same.
1015 jushort old_flags = (jushort) the_class->access_flags().get_flags();
1016 jushort new_flags = (jushort) scratch_class->access_flags().get_flags();
1017 if (old_flags != new_flags) {
1018 log_info(redefine, class, normalize)
1019 ("redefined class %s modifiers change error: modifiers changed from %d to %d.",
1020 the_class->external_name(), old_flags, new_flags);
1021 return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_CLASS_MODIFIERS_CHANGED;
1022 }
1023
1024 // Check if the number, names, types and order of fields declared in these classes
1025 // are the same.
1026 JavaFieldStream old_fs(the_class);
1027 JavaFieldStream new_fs(scratch_class);
1028 for (; !old_fs.done() && !new_fs.done(); old_fs.next(), new_fs.next()) {
1029 // name and signature
1030 Symbol* name_sym1 = the_class->constants()->symbol_at(old_fs.name_index());
1031 Symbol* sig_sym1 = the_class->constants()->symbol_at(old_fs.signature_index());
1032 Symbol* name_sym2 = scratch_class->constants()->symbol_at(new_fs.name_index());
1033 Symbol* sig_sym2 = scratch_class->constants()->symbol_at(new_fs.signature_index());
1963 bool VM_RedefineClasses::rewrite_cp_refs(InstanceKlass* scratch_class) {
1964
1965 // rewrite constant pool references in the nest attributes:
1966 if (!rewrite_cp_refs_in_nest_attributes(scratch_class)) {
1967 // propagate failure back to caller
1968 return false;
1969 }
1970
1971 // rewrite constant pool references in the Record attribute:
1972 if (!rewrite_cp_refs_in_record_attribute(scratch_class)) {
1973 // propagate failure back to caller
1974 return false;
1975 }
1976
1977 // rewrite constant pool references in the PermittedSubclasses attribute:
1978 if (!rewrite_cp_refs_in_permitted_subclasses_attribute(scratch_class)) {
1979 // propagate failure back to caller
1980 return false;
1981 }
1982
1983 // rewrite constant pool references in the Preload attribute:
1984 if (!rewrite_cp_refs_in_preload_attribute(scratch_class)) {
1985 // propagate failure back to caller
1986 return false;
1987 }
1988
1989 // rewrite constant pool references in the methods:
1990 if (!rewrite_cp_refs_in_methods(scratch_class)) {
1991 // propagate failure back to caller
1992 return false;
1993 }
1994
1995 // rewrite constant pool references in the class_annotations:
1996 if (!rewrite_cp_refs_in_class_annotations(scratch_class)) {
1997 // propagate failure back to caller
1998 return false;
1999 }
2000
2001 // rewrite constant pool references in the fields_annotations:
2002 if (!rewrite_cp_refs_in_fields_annotations(scratch_class)) {
2003 // propagate failure back to caller
2004 return false;
2005 }
2006
2007 // rewrite constant pool references in the methods_annotations:
2008 if (!rewrite_cp_refs_in_methods_annotations(scratch_class)) {
2117 }
2118 }
2119 }
2120 }
2121 return true;
2122 }
2123
2124 // Rewrite constant pool references in the PermittedSubclasses attribute.
2125 bool VM_RedefineClasses::rewrite_cp_refs_in_permitted_subclasses_attribute(
2126 InstanceKlass* scratch_class) {
2127
2128 Array<u2>* permitted_subclasses = scratch_class->permitted_subclasses();
2129 assert(permitted_subclasses != NULL, "unexpected null permitted_subclasses");
2130 for (int i = 0; i < permitted_subclasses->length(); i++) {
2131 u2 cp_index = permitted_subclasses->at(i);
2132 permitted_subclasses->at_put(i, find_new_index(cp_index));
2133 }
2134 return true;
2135 }
2136
2137 // Rewrite constant pool references in the Preload attribute.
2138 bool VM_RedefineClasses::rewrite_cp_refs_in_preload_attribute(
2139 InstanceKlass* scratch_class) {
2140
2141 Array<u2>* preload_classes = scratch_class->preload_classes();
2142 assert(preload_classes != NULL, "unexpected null preload_classes");
2143 for (int i = 0; i < preload_classes->length(); i++) {
2144 u2 cp_index = preload_classes->at(i);
2145 preload_classes->at_put(i, find_new_index(cp_index));
2146 }
2147 return true;
2148 }
2149
2150 // Rewrite constant pool references in the methods.
2151 bool VM_RedefineClasses::rewrite_cp_refs_in_methods(InstanceKlass* scratch_class) {
2152
2153 Array<Method*>* methods = scratch_class->methods();
2154
2155 if (methods == NULL || methods->length() == 0) {
2156 // no methods so nothing to do
2157 return true;
2158 }
2159
2160 JavaThread* THREAD = JavaThread::current(); // For exception macros.
2161 ExceptionMark em(THREAD);
2162
2163 // rewrite constant pool references in the methods:
2164 for (int i = methods->length() - 1; i >= 0; i--) {
2165 methodHandle method(THREAD, methods->at(i));
2166 methodHandle new_method;
2167 rewrite_cp_refs_in_method(method, &new_method, THREAD);
2168 if (!new_method.is_null()) {
2169 // the method has been replaced so save the new method version
2262 // return the new method so that the caller can update
2263 // the containing class
2264 *new_method_p = method = m;
2265 // switch our bytecode processing loop from the old method
2266 // to the new method
2267 code_base = method->code_base();
2268 code_length = method->code_size();
2269 bcp = code_base + bci;
2270 c = (Bytecodes::Code)(*bcp);
2271 bc_length = Bytecodes::length_for(c);
2272 assert(bc_length != 0, "sanity check");
2273 } // end we need ldc_w instead of ldc
2274 } // end if there is a mapped index
2275 } break;
2276
2277 // these bytecodes have a two-byte constant pool index
2278 case Bytecodes::_anewarray : // fall through
2279 case Bytecodes::_checkcast : // fall through
2280 case Bytecodes::_getfield : // fall through
2281 case Bytecodes::_getstatic : // fall through
2282 case Bytecodes::_aconst_init : // fall through
2283 case Bytecodes::_withfield : // fall through
2284 case Bytecodes::_instanceof : // fall through
2285 case Bytecodes::_invokedynamic : // fall through
2286 case Bytecodes::_invokeinterface: // fall through
2287 case Bytecodes::_invokespecial : // fall through
2288 case Bytecodes::_invokestatic : // fall through
2289 case Bytecodes::_invokevirtual : // fall through
2290 case Bytecodes::_ldc_w : // fall through
2291 case Bytecodes::_ldc2_w : // fall through
2292 case Bytecodes::_multianewarray : // fall through
2293 case Bytecodes::_new : // fall through
2294 case Bytecodes::_putfield : // fall through
2295 case Bytecodes::_putstatic :
2296 {
2297 address p = bcp + 1;
2298 int cp_index = Bytes::get_Java_u2(p);
2299 int new_index = find_new_index(cp_index);
2300 if (new_index != 0) {
2301 // the original index is mapped so update w/ new value
2302 log_trace(redefine, class, constantpool)
2303 ("%s@" INTPTR_FORMAT " old=%d, new=%d", Bytecodes::name(c),p2i(bcp), cp_index, new_index);
|