src/share/vm/prims/jvmtiRedefineClasses.cpp
Index Unified diffs Context diffs Sdiffs Wdiffs Patch New Old Previous File Next File hotspot Sdiff src/share/vm/prims

src/share/vm/prims/jvmtiRedefineClasses.cpp

Print this page




 594       }
 595     }
 596   }
 597   // Clean-up
 598   _operands_index_map_p = NULL;
 599   _operands_cur_length = 0;
 600   _operands_index_map_count = 0;
 601 } // end finalize_operands_merge()
 602 
 603 
 604 jvmtiError VM_RedefineClasses::compare_and_normalize_class_versions(
 605              instanceKlassHandle the_class,
 606              instanceKlassHandle scratch_class) {
 607   int i;
 608 
 609   // Check superclasses, or rather their names, since superclasses themselves can be
 610   // requested to replace.
 611   // Check for NULL superclass first since this might be java.lang.Object
 612   if (the_class->super() != scratch_class->super() &&
 613       (the_class->super() == NULL || scratch_class->super() == NULL ||
 614        the_class->super()->name() !=
 615        scratch_class->super()->name())) {
 616     return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED;
 617   }
 618 
 619   // Check if the number, names and order of directly implemented interfaces are the same.
 620   // I think in principle we should just check if the sets of names of directly implemented
 621   // interfaces are the same, i.e. the order of declaration (which, however, if changed in the
 622   // .java file, also changes in .class file) should not matter. However, comparing sets is
 623   // technically a bit more difficult, and, more importantly, I am not sure at present that the
 624   // order of interfaces does not matter on the implementation level, i.e. that the VM does not
 625   // rely on it somewhere.
 626   Array<Klass*>* k_interfaces = the_class->local_interfaces();
 627   Array<Klass*>* k_new_interfaces = scratch_class->local_interfaces();
 628   int n_intfs = k_interfaces->length();
 629   if (n_intfs != k_new_interfaces->length()) {
 630     return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED;
 631   }
 632   for (i = 0; i < n_intfs; i++) {
 633     if (k_interfaces->at(i)->name() !=
 634         k_new_interfaces->at(i)->name()) {
 635       return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED;
 636     }
 637   }
 638 
 639   // Check whether class is in the error init state.
 640   if (the_class->is_in_error_state()) {
 641     // TBD #5057930: special error code is needed in 1.6
 642     return JVMTI_ERROR_INVALID_CLASS;
 643   }
 644 
 645   // Check whether class modifiers are the same.
 646   jushort old_flags = (jushort) the_class->access_flags().get_flags();
 647   jushort new_flags = (jushort) scratch_class->access_flags().get_flags();
 648   if (old_flags != new_flags) {
 649     return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_CLASS_MODIFIERS_CHANGED;
 650   }
 651 
 652   // Check if the number, names, types and order of fields declared in these classes
 653   // are the same.
 654   JavaFieldStream old_fs(the_class);
 655   JavaFieldStream new_fs(scratch_class);
 656   for (; !old_fs.done() && !new_fs.done(); old_fs.next(), new_fs.next()) {
 657     // access
 658     old_flags = old_fs.access_flags().as_short();
 659     new_flags = new_fs.access_flags().as_short();
 660     if ((old_flags ^ new_flags) & JVM_RECOGNIZED_FIELD_MODIFIERS) {
 661       return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_SCHEMA_CHANGED;
 662     }
 663     // offset
 664     if (old_fs.offset() != new_fs.offset()) {
 665       return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_SCHEMA_CHANGED;
 666     }
 667     // name and signature
 668     Symbol* name_sym1 = the_class->constants()->symbol_at(old_fs.name_index());
 669     Symbol* sig_sym1 = the_class->constants()->symbol_at(old_fs.signature_index());
 670     Symbol* name_sym2 = scratch_class->constants()->symbol_at(new_fs.name_index());
 671     Symbol* sig_sym2 = scratch_class->constants()->symbol_at(new_fs.signature_index());
 672     if (name_sym1 != name_sym2 || sig_sym1 != sig_sym2) {
 673       return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_SCHEMA_CHANGED;
 674     }
 675   }
 676 
 677   // If both streams aren't done then we have a differing number of
 678   // fields.
 679   if (!old_fs.done() || !new_fs.done()) {
 680     return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_SCHEMA_CHANGED;
 681   }
 682 
 683   // Do a parallel walk through the old and new methods. Detect
 684   // cases where they match (exist in both), have been added in
 685   // the new methods, or have been deleted (exist only in the
 686   // old methods).  The class file parser places methods in order
 687   // by method name, but does not order overloaded methods by
 688   // signature.  In order to determine what fate befell the methods,
 689   // this code places the overloaded new methods that have matching
 690   // old methods in the same order as the old methods and places
 691   // new overloaded methods at the end of overloaded methods of
 692   // that name. The code for this order normalization is adapted


 708   while (true) {
 709     Method* k_old_method;
 710     Method* k_new_method;
 711     enum { matched, added, deleted, undetermined } method_was = undetermined;
 712 
 713     if (oi >= n_old_methods) {
 714       if (ni >= n_new_methods) {
 715         break; // we've looked at everything, done
 716       }
 717       // New method at the end
 718       k_new_method = k_new_methods->at(ni);
 719       method_was = added;
 720     } else if (ni >= n_new_methods) {
 721       // Old method, at the end, is deleted
 722       k_old_method = k_old_methods->at(oi);
 723       method_was = deleted;
 724     } else {
 725       // There are more methods in both the old and new lists
 726       k_old_method = k_old_methods->at(oi);
 727       k_new_method = k_new_methods->at(ni);
 728       if (k_old_method->name() != k_new_method->name()) {
 729         // Methods are sorted by method name, so a mismatch means added
 730         // or deleted
 731         if (k_old_method->name()->fast_compare(k_new_method->name()) > 0) {
 732           method_was = added;
 733         } else {
 734           method_was = deleted;
 735         }
 736       } else if (k_old_method->signature() == k_new_method->signature()) {
 737         // Both the name and signature match
 738         method_was = matched;
 739       } else {
 740         // The name matches, but the signature doesn't, which means we have to
 741         // search forward through the new overloaded methods.
 742         int nj;  // outside the loop for post-loop check
 743         for (nj = ni + 1; nj < n_new_methods; nj++) {
 744           Method* m = k_new_methods->at(nj);
 745           if (k_old_method->name() != m->name()) {
 746             // reached another method name so no more overloaded methods
 747             method_was = deleted;
 748             break;
 749           }
 750           if (k_old_method->signature() == m->signature()) {
 751             // found a match so swap the methods
 752             k_new_methods->at_put(ni, m);
 753             k_new_methods->at_put(nj, k_new_method);
 754             k_new_method = m;
 755             method_was = matched;
 756             break;
 757           }
 758         }
 759 
 760         if (nj >= n_new_methods) {
 761           // reached the end without a match; so method was deleted
 762           method_was = deleted;
 763         }
 764       }
 765     }
 766 
 767     switch (method_was) {
 768     case matched:
 769       // methods match, be sure modifiers do too
 770       old_flags = (jushort) k_old_method->access_flags().get_flags();


 993                                                 THREAD);
 994     // Clear class_being_redefined just to be sure.
 995     state->clear_class_being_redefined();
 996 
 997     // TODO: if this is retransform, and nothing changed we can skip it
 998 
 999     instanceKlassHandle scratch_class (THREAD, k);
1000 
1001     // Need to clean up allocated InstanceKlass if there's an error so assign
1002     // the result here. Caller deallocates all the scratch classes in case of
1003     // an error.
1004     _scratch_classes[i] = k;
1005 
1006     if (HAS_PENDING_EXCEPTION) {
1007       Symbol* ex_name = PENDING_EXCEPTION->klass()->name();
1008       // RC_TRACE_WITH_THREAD macro has an embedded ResourceMark
1009       RC_TRACE_WITH_THREAD(0x00000002, THREAD, ("parse_stream exception: '%s'",
1010         ex_name->as_C_string()));
1011       CLEAR_PENDING_EXCEPTION;
1012 
1013       if (ex_name == vmSymbols::java_lang_UnsupportedClassVersionError()) {
1014         return JVMTI_ERROR_UNSUPPORTED_VERSION;
1015       } else if (ex_name == vmSymbols::java_lang_ClassFormatError()) {
1016         return JVMTI_ERROR_INVALID_CLASS_FORMAT;
1017       } else if (ex_name == vmSymbols::java_lang_ClassCircularityError()) {
1018         return JVMTI_ERROR_CIRCULAR_CLASS_DEFINITION;
1019       } else if (ex_name == vmSymbols::java_lang_NoClassDefFoundError()) {
1020         // The message will be "XXX (wrong name: YYY)"
1021         return JVMTI_ERROR_NAMES_DONT_MATCH;
1022       } else if (ex_name == vmSymbols::java_lang_OutOfMemoryError()) {
1023         return JVMTI_ERROR_OUT_OF_MEMORY;
1024       } else {  // Just in case more exceptions can be thrown..
1025         return JVMTI_ERROR_FAILS_VERIFICATION;
1026       }
1027     }
1028 
1029     // Ensure class is linked before redefine
1030     if (!the_class->is_linked()) {
1031       the_class->link_class(THREAD);
1032       if (HAS_PENDING_EXCEPTION) {
1033         Symbol* ex_name = PENDING_EXCEPTION->klass()->name();
1034         // RC_TRACE_WITH_THREAD macro has an embedded ResourceMark
1035         RC_TRACE_WITH_THREAD(0x00000002, THREAD, ("link_class exception: '%s'",
1036           ex_name->as_C_string()));
1037         CLEAR_PENDING_EXCEPTION;
1038         if (ex_name == vmSymbols::java_lang_OutOfMemoryError()) {
1039           return JVMTI_ERROR_OUT_OF_MEMORY;
1040         } else {
1041           return JVMTI_ERROR_INTERNAL;
1042         }
1043       }
1044     }
1045 
1046     // Do the validity checks in compare_and_normalize_class_versions()
1047     // before verifying the byte codes. By doing these checks first, we
1048     // limit the number of functions that require redirection from
1049     // the_class to scratch_class. In particular, we don't have to
1050     // modify JNI GetSuperclass() and thus won't change its performance.
1051     jvmtiError res = compare_and_normalize_class_versions(the_class,
1052                        scratch_class);
1053     if (res != JVMTI_ERROR_NONE) {
1054       return res;
1055     }
1056 
1057     // verify what the caller passed us
1058     {
1059       // The bug 6214132 caused the verification to fail.
1060       // Information about the_class and scratch_class is temporarily
1061       // recorded into jvmtiThreadState. This data is used to redirect
1062       // the_class to scratch_class in the JVM_* functions called by the
1063       // verifier. Please, refer to jvmtiThreadState.hpp for the detailed
1064       // description.
1065       RedefineVerifyMark rvm(&the_class, &scratch_class, state);
1066       Verifier::verify(
1067         scratch_class, Verifier::ThrowException, true, THREAD);
1068     }
1069 
1070     if (HAS_PENDING_EXCEPTION) {
1071       Symbol* ex_name = PENDING_EXCEPTION->klass()->name();
1072       // RC_TRACE_WITH_THREAD macro has an embedded ResourceMark
1073       RC_TRACE_WITH_THREAD(0x00000002, THREAD,
1074         ("verify_byte_codes exception: '%s'", ex_name->as_C_string()));
1075       CLEAR_PENDING_EXCEPTION;
1076       if (ex_name == vmSymbols::java_lang_OutOfMemoryError()) {
1077         return JVMTI_ERROR_OUT_OF_MEMORY;
1078       } else {
1079         // tell the caller the bytecodes are bad
1080         return JVMTI_ERROR_FAILS_VERIFICATION;
1081       }
1082     }
1083 
1084     res = merge_cp_and_rewrite(the_class, scratch_class, THREAD);
1085     if (HAS_PENDING_EXCEPTION) {
1086       Symbol* ex_name = PENDING_EXCEPTION->klass()->name();
1087       // RC_TRACE_WITH_THREAD macro has an embedded ResourceMark
1088       RC_TRACE_WITH_THREAD(0x00000002, THREAD,
1089         ("merge_cp_and_rewrite exception: '%s'", ex_name->as_C_string()));
1090       CLEAR_PENDING_EXCEPTION;
1091       if (ex_name == vmSymbols::java_lang_OutOfMemoryError()) {
1092         return JVMTI_ERROR_OUT_OF_MEMORY;
1093       } else {
1094         return JVMTI_ERROR_INTERNAL;
1095       }
1096     }
1097 
1098     if (VerifyMergedCPBytecodes) {
1099       // verify what we have done during constant pool merging
1100       {
1101         RedefineVerifyMark rvm(&the_class, &scratch_class, state);
1102         Verifier::verify(scratch_class, Verifier::ThrowException, true, THREAD);
1103       }
1104 
1105       if (HAS_PENDING_EXCEPTION) {
1106         Symbol* ex_name = PENDING_EXCEPTION->klass()->name();
1107         // RC_TRACE_WITH_THREAD macro has an embedded ResourceMark
1108         RC_TRACE_WITH_THREAD(0x00000002, THREAD,
1109           ("verify_byte_codes post merge-CP exception: '%s'",
1110           ex_name->as_C_string()));
1111         CLEAR_PENDING_EXCEPTION;
1112         if (ex_name == vmSymbols::java_lang_OutOfMemoryError()) {
1113           return JVMTI_ERROR_OUT_OF_MEMORY;
1114         } else {
1115           // tell the caller that constant pool merging screwed up
1116           return JVMTI_ERROR_INTERNAL;
1117         }
1118       }
1119     }
1120 
1121     Rewriter::rewrite(scratch_class, THREAD);
1122     if (!HAS_PENDING_EXCEPTION) {
1123       scratch_class->link_methods(THREAD);
1124     }
1125     if (HAS_PENDING_EXCEPTION) {
1126       Symbol* ex_name = PENDING_EXCEPTION->klass()->name();
1127       // RC_TRACE_WITH_THREAD macro has an embedded ResourceMark
1128       RC_TRACE_WITH_THREAD(0x00000002, THREAD,
1129         ("Rewriter::rewrite or link_methods exception: '%s'", ex_name->as_C_string()));
1130       CLEAR_PENDING_EXCEPTION;
1131       if (ex_name == vmSymbols::java_lang_OutOfMemoryError()) {
1132         return JVMTI_ERROR_OUT_OF_MEMORY;
1133       } else {
1134         return JVMTI_ERROR_INTERNAL;
1135       }
1136     }
1137 
1138     // RC_TRACE_WITH_THREAD macro has an embedded ResourceMark
1139     RC_TRACE_WITH_THREAD(0x00000001, THREAD,
1140       ("loaded name=%s (avail_mem=" UINT64_FORMAT "K)",
1141       the_class->external_name(), os::available_memory() >> 10));
1142   }
1143 
1144   return JVMTI_ERROR_NONE;
1145 }
1146 
1147 
1148 // Map old_index to new_index as needed. scratch_cp is only needed
1149 // for RC_TRACE() calls.
1150 void VM_RedefineClasses::map_index(constantPoolHandle scratch_cp,
1151        int old_index, int new_index) {


3793 
3794   int nj = 0;
3795   int oj = 0;
3796   while (true) {
3797     if (oj >= _old_methods->length()) {
3798       if (nj >= _new_methods->length()) {
3799         break; // we've looked at everything, done
3800       }
3801       // New method at the end
3802       new_method = _new_methods->at(nj);
3803       _added_methods[_added_methods_length++] = new_method;
3804       ++nj;
3805     } else if (nj >= _new_methods->length()) {
3806       // Old method, at the end, is deleted
3807       old_method = _old_methods->at(oj);
3808       _deleted_methods[_deleted_methods_length++] = old_method;
3809       ++oj;
3810     } else {
3811       old_method = _old_methods->at(oj);
3812       new_method = _new_methods->at(nj);
3813       if (old_method->name() == new_method->name()) {
3814         if (old_method->signature() == new_method->signature()) {
3815           _matching_old_methods[_matching_methods_length  ] = old_method;
3816           _matching_new_methods[_matching_methods_length++] = new_method;
3817           ++nj;
3818           ++oj;
3819         } else {
3820           // added overloaded have already been moved to the end,
3821           // so this is a deleted overloaded method
3822           _deleted_methods[_deleted_methods_length++] = old_method;
3823           ++oj;
3824         }
3825       } else { // names don't match
3826         if (old_method->name()->fast_compare(new_method->name()) > 0) {
3827           // new method
3828           _added_methods[_added_methods_length++] = new_method;
3829           ++nj;
3830         } else {
3831           // deleted method
3832           _deleted_methods[_deleted_methods_length++] = old_method;
3833           ++oj;
3834         }




 594       }
 595     }
 596   }
 597   // Clean-up
 598   _operands_index_map_p = NULL;
 599   _operands_cur_length = 0;
 600   _operands_index_map_count = 0;
 601 } // end finalize_operands_merge()
 602 
 603 
 604 jvmtiError VM_RedefineClasses::compare_and_normalize_class_versions(
 605              instanceKlassHandle the_class,
 606              instanceKlassHandle scratch_class) {
 607   int i;
 608 
 609   // Check superclasses, or rather their names, since superclasses themselves can be
 610   // requested to replace.
 611   // Check for NULL superclass first since this might be java.lang.Object
 612   if (the_class->super() != scratch_class->super() &&
 613       (the_class->super() == NULL || scratch_class->super() == NULL ||
 614        the_class->super()->name()->not_equals(
 615        scratch_class->super()->name()))) {
 616     return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED;
 617   }
 618 
 619   // Check if the number, names and order of directly implemented interfaces are the same.
 620   // I think in principle we should just check if the sets of names of directly implemented
 621   // interfaces are the same, i.e. the order of declaration (which, however, if changed in the
 622   // .java file, also changes in .class file) should not matter. However, comparing sets is
 623   // technically a bit more difficult, and, more importantly, I am not sure at present that the
 624   // order of interfaces does not matter on the implementation level, i.e. that the VM does not
 625   // rely on it somewhere.
 626   Array<Klass*>* k_interfaces = the_class->local_interfaces();
 627   Array<Klass*>* k_new_interfaces = scratch_class->local_interfaces();
 628   int n_intfs = k_interfaces->length();
 629   if (n_intfs != k_new_interfaces->length()) {
 630     return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED;
 631   }
 632   for (i = 0; i < n_intfs; i++) {
 633     if (k_interfaces->at(i)->name()->not_equals(
 634         k_new_interfaces->at(i)->name())) {
 635       return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED;
 636     }
 637   }
 638 
 639   // Check whether class is in the error init state.
 640   if (the_class->is_in_error_state()) {
 641     // TBD #5057930: special error code is needed in 1.6
 642     return JVMTI_ERROR_INVALID_CLASS;
 643   }
 644 
 645   // Check whether class modifiers are the same.
 646   jushort old_flags = (jushort) the_class->access_flags().get_flags();
 647   jushort new_flags = (jushort) scratch_class->access_flags().get_flags();
 648   if (old_flags != new_flags) {
 649     return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_CLASS_MODIFIERS_CHANGED;
 650   }
 651 
 652   // Check if the number, names, types and order of fields declared in these classes
 653   // are the same.
 654   JavaFieldStream old_fs(the_class);
 655   JavaFieldStream new_fs(scratch_class);
 656   for (; !old_fs.done() && !new_fs.done(); old_fs.next(), new_fs.next()) {
 657     // access
 658     old_flags = old_fs.access_flags().as_short();
 659     new_flags = new_fs.access_flags().as_short();
 660     if ((old_flags ^ new_flags) & JVM_RECOGNIZED_FIELD_MODIFIERS) {
 661       return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_SCHEMA_CHANGED;
 662     }
 663     // offset
 664     if (old_fs.offset() != new_fs.offset()) {
 665       return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_SCHEMA_CHANGED;
 666     }
 667     // name and signature
 668     Symbol* name_sym1 = the_class->constants()->symbol_at(old_fs.name_index());
 669     Symbol* sig_sym1 = the_class->constants()->symbol_at(old_fs.signature_index());
 670     Symbol* name_sym2 = scratch_class->constants()->symbol_at(new_fs.name_index());
 671     Symbol* sig_sym2 = scratch_class->constants()->symbol_at(new_fs.signature_index());
 672     if (name_sym1->not_equals(name_sym2) || sig_sym1->not_equals(sig_sym2)) {
 673       return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_SCHEMA_CHANGED;
 674     }
 675   }
 676 
 677   // If both streams aren't done then we have a differing number of
 678   // fields.
 679   if (!old_fs.done() || !new_fs.done()) {
 680     return JVMTI_ERROR_UNSUPPORTED_REDEFINITION_SCHEMA_CHANGED;
 681   }
 682 
 683   // Do a parallel walk through the old and new methods. Detect
 684   // cases where they match (exist in both), have been added in
 685   // the new methods, or have been deleted (exist only in the
 686   // old methods).  The class file parser places methods in order
 687   // by method name, but does not order overloaded methods by
 688   // signature.  In order to determine what fate befell the methods,
 689   // this code places the overloaded new methods that have matching
 690   // old methods in the same order as the old methods and places
 691   // new overloaded methods at the end of overloaded methods of
 692   // that name. The code for this order normalization is adapted


 708   while (true) {
 709     Method* k_old_method;
 710     Method* k_new_method;
 711     enum { matched, added, deleted, undetermined } method_was = undetermined;
 712 
 713     if (oi >= n_old_methods) {
 714       if (ni >= n_new_methods) {
 715         break; // we've looked at everything, done
 716       }
 717       // New method at the end
 718       k_new_method = k_new_methods->at(ni);
 719       method_was = added;
 720     } else if (ni >= n_new_methods) {
 721       // Old method, at the end, is deleted
 722       k_old_method = k_old_methods->at(oi);
 723       method_was = deleted;
 724     } else {
 725       // There are more methods in both the old and new lists
 726       k_old_method = k_old_methods->at(oi);
 727       k_new_method = k_new_methods->at(ni);
 728       if (k_old_method->name()->not_equals(k_new_method->name())) {
 729         // Methods are sorted by method name, so a mismatch means added
 730         // or deleted
 731         if (k_old_method->name()->fast_compare(k_new_method->name()) > 0) {
 732           method_was = added;
 733         } else {
 734           method_was = deleted;
 735         }
 736       } else if (k_old_method->signature()->equals(k_new_method->signature())) {
 737         // Both the name and signature match
 738         method_was = matched;
 739       } else {
 740         // The name matches, but the signature doesn't, which means we have to
 741         // search forward through the new overloaded methods.
 742         int nj;  // outside the loop for post-loop check
 743         for (nj = ni + 1; nj < n_new_methods; nj++) {
 744           Method* m = k_new_methods->at(nj);
 745           if (k_old_method->name()->not_equals(m->name())) {
 746             // reached another method name so no more overloaded methods
 747             method_was = deleted;
 748             break;
 749           }
 750           if (k_old_method->signature()->equals(m->signature())) {
 751             // found a match so swap the methods
 752             k_new_methods->at_put(ni, m);
 753             k_new_methods->at_put(nj, k_new_method);
 754             k_new_method = m;
 755             method_was = matched;
 756             break;
 757           }
 758         }
 759 
 760         if (nj >= n_new_methods) {
 761           // reached the end without a match; so method was deleted
 762           method_was = deleted;
 763         }
 764       }
 765     }
 766 
 767     switch (method_was) {
 768     case matched:
 769       // methods match, be sure modifiers do too
 770       old_flags = (jushort) k_old_method->access_flags().get_flags();


 993                                                 THREAD);
 994     // Clear class_being_redefined just to be sure.
 995     state->clear_class_being_redefined();
 996 
 997     // TODO: if this is retransform, and nothing changed we can skip it
 998 
 999     instanceKlassHandle scratch_class (THREAD, k);
1000 
1001     // Need to clean up allocated InstanceKlass if there's an error so assign
1002     // the result here. Caller deallocates all the scratch classes in case of
1003     // an error.
1004     _scratch_classes[i] = k;
1005 
1006     if (HAS_PENDING_EXCEPTION) {
1007       Symbol* ex_name = PENDING_EXCEPTION->klass()->name();
1008       // RC_TRACE_WITH_THREAD macro has an embedded ResourceMark
1009       RC_TRACE_WITH_THREAD(0x00000002, THREAD, ("parse_stream exception: '%s'",
1010         ex_name->as_C_string()));
1011       CLEAR_PENDING_EXCEPTION;
1012 
1013       if (ex_name->equals(vmSymbols::java_lang_UnsupportedClassVersionError())) {
1014         return JVMTI_ERROR_UNSUPPORTED_VERSION;
1015       } else if (ex_name->equals(vmSymbols::java_lang_ClassFormatError())) {
1016         return JVMTI_ERROR_INVALID_CLASS_FORMAT;
1017       } else if (ex_name->equals(vmSymbols::java_lang_ClassCircularityError())) {
1018         return JVMTI_ERROR_CIRCULAR_CLASS_DEFINITION;
1019       } else if (ex_name->equals(vmSymbols::java_lang_NoClassDefFoundError())) {
1020         // The message will be "XXX (wrong name: YYY)"
1021         return JVMTI_ERROR_NAMES_DONT_MATCH;
1022       } else if (ex_name->equals(vmSymbols::java_lang_OutOfMemoryError())) {
1023         return JVMTI_ERROR_OUT_OF_MEMORY;
1024       } else {  // Just in case more exceptions can be thrown..
1025         return JVMTI_ERROR_FAILS_VERIFICATION;
1026       }
1027     }
1028 
1029     // Ensure class is linked before redefine
1030     if (!the_class->is_linked()) {
1031       the_class->link_class(THREAD);
1032       if (HAS_PENDING_EXCEPTION) {
1033         Symbol* ex_name = PENDING_EXCEPTION->klass()->name();
1034         // RC_TRACE_WITH_THREAD macro has an embedded ResourceMark
1035         RC_TRACE_WITH_THREAD(0x00000002, THREAD, ("link_class exception: '%s'",
1036           ex_name->as_C_string()));
1037         CLEAR_PENDING_EXCEPTION;
1038         if (ex_name->equals(vmSymbols::java_lang_OutOfMemoryError())) {
1039           return JVMTI_ERROR_OUT_OF_MEMORY;
1040         } else {
1041           return JVMTI_ERROR_INTERNAL;
1042         }
1043       }
1044     }
1045 
1046     // Do the validity checks in compare_and_normalize_class_versions()
1047     // before verifying the byte codes. By doing these checks first, we
1048     // limit the number of functions that require redirection from
1049     // the_class to scratch_class. In particular, we don't have to
1050     // modify JNI GetSuperclass() and thus won't change its performance.
1051     jvmtiError res = compare_and_normalize_class_versions(the_class,
1052                        scratch_class);
1053     if (res != JVMTI_ERROR_NONE) {
1054       return res;
1055     }
1056 
1057     // verify what the caller passed us
1058     {
1059       // The bug 6214132 caused the verification to fail.
1060       // Information about the_class and scratch_class is temporarily
1061       // recorded into jvmtiThreadState. This data is used to redirect
1062       // the_class to scratch_class in the JVM_* functions called by the
1063       // verifier. Please, refer to jvmtiThreadState.hpp for the detailed
1064       // description.
1065       RedefineVerifyMark rvm(&the_class, &scratch_class, state);
1066       Verifier::verify(
1067         scratch_class, Verifier::ThrowException, true, THREAD);
1068     }
1069 
1070     if (HAS_PENDING_EXCEPTION) {
1071       Symbol* ex_name = PENDING_EXCEPTION->klass()->name();
1072       // RC_TRACE_WITH_THREAD macro has an embedded ResourceMark
1073       RC_TRACE_WITH_THREAD(0x00000002, THREAD,
1074         ("verify_byte_codes exception: '%s'", ex_name->as_C_string()));
1075       CLEAR_PENDING_EXCEPTION;
1076       if (ex_name->equals(vmSymbols::java_lang_OutOfMemoryError())) {
1077         return JVMTI_ERROR_OUT_OF_MEMORY;
1078       } else {
1079         // tell the caller the bytecodes are bad
1080         return JVMTI_ERROR_FAILS_VERIFICATION;
1081       }
1082     }
1083 
1084     res = merge_cp_and_rewrite(the_class, scratch_class, THREAD);
1085     if (HAS_PENDING_EXCEPTION) {
1086       Symbol* ex_name = PENDING_EXCEPTION->klass()->name();
1087       // RC_TRACE_WITH_THREAD macro has an embedded ResourceMark
1088       RC_TRACE_WITH_THREAD(0x00000002, THREAD,
1089         ("merge_cp_and_rewrite exception: '%s'", ex_name->as_C_string()));
1090       CLEAR_PENDING_EXCEPTION;
1091       if (ex_name->equals(vmSymbols::java_lang_OutOfMemoryError())) {
1092         return JVMTI_ERROR_OUT_OF_MEMORY;
1093       } else {
1094         return JVMTI_ERROR_INTERNAL;
1095       }
1096     }
1097 
1098     if (VerifyMergedCPBytecodes) {
1099       // verify what we have done during constant pool merging
1100       {
1101         RedefineVerifyMark rvm(&the_class, &scratch_class, state);
1102         Verifier::verify(scratch_class, Verifier::ThrowException, true, THREAD);
1103       }
1104 
1105       if (HAS_PENDING_EXCEPTION) {
1106         Symbol* ex_name = PENDING_EXCEPTION->klass()->name();
1107         // RC_TRACE_WITH_THREAD macro has an embedded ResourceMark
1108         RC_TRACE_WITH_THREAD(0x00000002, THREAD,
1109           ("verify_byte_codes post merge-CP exception: '%s'",
1110           ex_name->as_C_string()));
1111         CLEAR_PENDING_EXCEPTION;
1112         if (ex_name->equals(vmSymbols::java_lang_OutOfMemoryError())) {
1113           return JVMTI_ERROR_OUT_OF_MEMORY;
1114         } else {
1115           // tell the caller that constant pool merging screwed up
1116           return JVMTI_ERROR_INTERNAL;
1117         }
1118       }
1119     }
1120 
1121     Rewriter::rewrite(scratch_class, THREAD);
1122     if (!HAS_PENDING_EXCEPTION) {
1123       scratch_class->link_methods(THREAD);
1124     }
1125     if (HAS_PENDING_EXCEPTION) {
1126       Symbol* ex_name = PENDING_EXCEPTION->klass()->name();
1127       // RC_TRACE_WITH_THREAD macro has an embedded ResourceMark
1128       RC_TRACE_WITH_THREAD(0x00000002, THREAD,
1129         ("Rewriter::rewrite or link_methods exception: '%s'", ex_name->as_C_string()));
1130       CLEAR_PENDING_EXCEPTION;
1131       if (ex_name->equals(vmSymbols::java_lang_OutOfMemoryError())) {
1132         return JVMTI_ERROR_OUT_OF_MEMORY;
1133       } else {
1134         return JVMTI_ERROR_INTERNAL;
1135       }
1136     }
1137 
1138     // RC_TRACE_WITH_THREAD macro has an embedded ResourceMark
1139     RC_TRACE_WITH_THREAD(0x00000001, THREAD,
1140       ("loaded name=%s (avail_mem=" UINT64_FORMAT "K)",
1141       the_class->external_name(), os::available_memory() >> 10));
1142   }
1143 
1144   return JVMTI_ERROR_NONE;
1145 }
1146 
1147 
1148 // Map old_index to new_index as needed. scratch_cp is only needed
1149 // for RC_TRACE() calls.
1150 void VM_RedefineClasses::map_index(constantPoolHandle scratch_cp,
1151        int old_index, int new_index) {


3793 
3794   int nj = 0;
3795   int oj = 0;
3796   while (true) {
3797     if (oj >= _old_methods->length()) {
3798       if (nj >= _new_methods->length()) {
3799         break; // we've looked at everything, done
3800       }
3801       // New method at the end
3802       new_method = _new_methods->at(nj);
3803       _added_methods[_added_methods_length++] = new_method;
3804       ++nj;
3805     } else if (nj >= _new_methods->length()) {
3806       // Old method, at the end, is deleted
3807       old_method = _old_methods->at(oj);
3808       _deleted_methods[_deleted_methods_length++] = old_method;
3809       ++oj;
3810     } else {
3811       old_method = _old_methods->at(oj);
3812       new_method = _new_methods->at(nj);
3813       if (old_method->name()->equals(new_method->name())) {
3814         if (old_method->signature()->equals(new_method->signature())) {
3815           _matching_old_methods[_matching_methods_length  ] = old_method;
3816           _matching_new_methods[_matching_methods_length++] = new_method;
3817           ++nj;
3818           ++oj;
3819         } else {
3820           // added overloaded have already been moved to the end,
3821           // so this is a deleted overloaded method
3822           _deleted_methods[_deleted_methods_length++] = old_method;
3823           ++oj;
3824         }
3825       } else { // names don't match
3826         if (old_method->name()->fast_compare(new_method->name()) > 0) {
3827           // new method
3828           _added_methods[_added_methods_length++] = new_method;
3829           ++nj;
3830         } else {
3831           // deleted method
3832           _deleted_methods[_deleted_methods_length++] = old_method;
3833           ++oj;
3834         }


src/share/vm/prims/jvmtiRedefineClasses.cpp
Index Unified diffs Context diffs Sdiffs Wdiffs Patch New Old Previous File Next File