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