< prev index next >

src/hotspot/share/prims/jvmtiRedefineClasses.cpp

Print this page

  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "cds/cdsConfig.hpp"
  26 #include "cds/metaspaceShared.hpp"
  27 #include "classfile/classFileStream.hpp"
  28 #include "classfile/classLoaderDataGraph.hpp"
  29 #include "classfile/classLoadInfo.hpp"

  30 #include "classfile/javaClasses.inline.hpp"
  31 #include "classfile/metadataOnStackMark.hpp"
  32 #include "classfile/stackMapTable.hpp"
  33 #include "classfile/symbolTable.hpp"
  34 #include "classfile/klassFactory.hpp"
  35 #include "classfile/verifier.hpp"
  36 #include "classfile/vmClasses.hpp"
  37 #include "classfile/vmSymbols.hpp"
  38 #include "code/codeCache.hpp"
  39 #include "compiler/compileBroker.hpp"
  40 #include "interpreter/oopMapCache.hpp"
  41 #include "interpreter/rewriter.hpp"
  42 #include "jfr/jfrEvents.hpp"
  43 #include "logging/logStream.hpp"
  44 #include "memory/metadataFactory.hpp"
  45 #include "memory/resourceArea.hpp"
  46 #include "memory/universe.hpp"
  47 #include "oops/annotations.hpp"
  48 #include "oops/constantPool.hpp"
  49 #include "oops/fieldStreams.inline.hpp"
  50 #include "oops/klass.inline.hpp"
  51 #include "oops/klassVtable.hpp"
  52 #include "oops/method.hpp"
  53 #include "oops/oop.inline.hpp"
  54 #include "oops/recordComponent.hpp"

 595       if (scratch_i != *merge_cp_length_p) {
 596         // The new entry in *merge_cp_p is at a different index than
 597         // the new entry in scratch_cp so we need to map the index values.
 598         map_index(scratch_cp, scratch_i, *merge_cp_length_p);
 599       }
 600       (*merge_cp_length_p)++;
 601     } break;
 602 
 603     // At this stage, Class or UnresolvedClass could be in scratch_cp, but not
 604     // ClassIndex
 605     case JVM_CONSTANT_ClassIndex: // fall through
 606 
 607     // Invalid is used as the tag for the second constant pool entry
 608     // occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should
 609     // not be seen by itself.
 610     case JVM_CONSTANT_Invalid: // fall through
 611 
 612     // At this stage, String could be here, but not StringIndex
 613     case JVM_CONSTANT_StringIndex: // fall through
 614 
 615     // At this stage JVM_CONSTANT_UnresolvedClassInError should not be
 616     // here
 617     case JVM_CONSTANT_UnresolvedClassInError: // fall through
 618 
 619     default:
 620     {
 621       // leave a breadcrumb
 622       jbyte bad_value = scratch_cp->tag_at(scratch_i).value();
 623       ShouldNotReachHere();
 624     } break;
 625   } // end switch tag value
 626 } // end append_entry()
 627 
 628 
 629 u2 VM_RedefineClasses::find_or_append_indirect_entry(const constantPoolHandle& scratch_cp,
 630       int ref_i, constantPoolHandle *merge_cp_p, int *merge_cp_length_p) {
 631 
 632   int new_ref_i = ref_i;
 633   bool match = (ref_i < *merge_cp_length_p) &&
 634                scratch_cp->compare_entry_to(ref_i, *merge_cp_p, ref_i);
 635 
 636   if (!match) {

1913 bool VM_RedefineClasses::rewrite_cp_refs(InstanceKlass* scratch_class) {
1914 
1915   // rewrite constant pool references in the nest attributes:
1916   if (!rewrite_cp_refs_in_nest_attributes(scratch_class)) {
1917     // propagate failure back to caller
1918     return false;
1919   }
1920 
1921   // rewrite constant pool references in the Record attribute:
1922   if (!rewrite_cp_refs_in_record_attribute(scratch_class)) {
1923     // propagate failure back to caller
1924     return false;
1925   }
1926 
1927   // rewrite constant pool references in the PermittedSubclasses attribute:
1928   if (!rewrite_cp_refs_in_permitted_subclasses_attribute(scratch_class)) {
1929     // propagate failure back to caller
1930     return false;
1931   }
1932 






1933   // rewrite constant pool references in the methods:
1934   if (!rewrite_cp_refs_in_methods(scratch_class)) {
1935     // propagate failure back to caller
1936     return false;
1937   }
1938 
1939   // rewrite constant pool references in the class_annotations:
1940   if (!rewrite_cp_refs_in_class_annotations(scratch_class)) {
1941     // propagate failure back to caller
1942     return false;
1943   }
1944 
1945   // rewrite constant pool references in the fields_annotations:
1946   if (!rewrite_cp_refs_in_fields_annotations(scratch_class)) {
1947     // propagate failure back to caller
1948     return false;
1949   }
1950 
1951   // rewrite constant pool references in the methods_annotations:
1952   if (!rewrite_cp_refs_in_methods_annotations(scratch_class)) {

2061         }
2062       }
2063     }
2064   }
2065   return true;
2066 }
2067 
2068 // Rewrite constant pool references in the PermittedSubclasses attribute.
2069 bool VM_RedefineClasses::rewrite_cp_refs_in_permitted_subclasses_attribute(
2070        InstanceKlass* scratch_class) {
2071 
2072   Array<u2>* permitted_subclasses = scratch_class->permitted_subclasses();
2073   assert(permitted_subclasses != nullptr, "unexpected null permitted_subclasses");
2074   for (int i = 0; i < permitted_subclasses->length(); i++) {
2075     u2 cp_index = permitted_subclasses->at(i);
2076     permitted_subclasses->at_put(i, find_new_index(cp_index));
2077   }
2078   return true;
2079 }
2080 













2081 // Rewrite constant pool references in the methods.
2082 bool VM_RedefineClasses::rewrite_cp_refs_in_methods(InstanceKlass* scratch_class) {
2083 
2084   Array<Method*>* methods = scratch_class->methods();
2085 
2086   if (methods == nullptr || methods->length() == 0) {
2087     // no methods so nothing to do
2088     return true;
2089   }
2090 
2091   JavaThread* THREAD = JavaThread::current(); // For exception macros.
2092   ExceptionMark em(THREAD);
2093 
2094   // rewrite constant pool references in the methods:
2095   for (int i = methods->length() - 1; i >= 0; i--) {
2096     methodHandle method(THREAD, methods->at(i));
2097     methodHandle new_method;
2098     rewrite_cp_refs_in_method(method, &new_method, THREAD);
2099     if (!new_method.is_null()) {
2100       // the method has been replaced so save the new method version

3248   // walk through each stack_map_frame
3249   u2 calc_number_of_entries = 0;
3250   for (; calc_number_of_entries < number_of_entries; calc_number_of_entries++) {
3251     // The stack_map_frame structure is a u1 frame_type followed by
3252     // 0 or more bytes of data:
3253     //
3254     // union stack_map_frame {
3255     //   same_frame;
3256     //   same_locals_1_stack_item_frame;
3257     //   same_locals_1_stack_item_frame_extended;
3258     //   chop_frame;
3259     //   same_frame_extended;
3260     //   append_frame;
3261     //   full_frame;
3262     // }
3263 
3264     assert(stackmap_p + 1 <= stackmap_end, "no room for frame_type");
3265     u1 frame_type = *stackmap_p;
3266     stackmap_p++;
3267 








3268     // same_frame {
3269     //   u1 frame_type = SAME; /* 0-63 */
3270     // }
3271     if (frame_type <= StackMapReader::SAME_FRAME_END) {
3272       // nothing more to do for same_frame
3273     }
3274 
3275     // same_locals_1_stack_item_frame {
3276     //   u1 frame_type = SAME_LOCALS_1_STACK_ITEM; /* 64-127 */
3277     //   verification_type_info stack[1];
3278     // }
3279     else if (frame_type >= StackMapReader::SAME_LOCALS_1_STACK_ITEM_FRAME_START &&
3280              frame_type <= StackMapReader::SAME_LOCALS_1_STACK_ITEM_FRAME_END) {
3281       rewrite_cp_refs_in_verification_type_info(stackmap_p, stackmap_end,
3282         calc_number_of_entries, frame_type);
3283     }
3284 
3285     // reserved for future use
3286     else if (frame_type >= StackMapReader::RESERVED_START &&
3287              frame_type <= StackMapReader::RESERVED_END) {

3457       ("frame_i=%u, frame_type=%u, cpool_index=%d", frame_i, frame_type, cpool_index);
3458   } break;
3459 
3460   // Uninitialized_variable_info {
3461   //   u1 tag = ITEM_Uninitialized; /* 8 */
3462   //   u2 offset;
3463   // }
3464   case ITEM_Uninitialized:
3465     assert(stackmap_p_ref + 2 <= stackmap_end, "no room for offset");
3466     stackmap_p_ref += 2;
3467     break;
3468 
3469   default:
3470     log_debug(redefine, class, stackmap)("frame_i=%u, frame_type=%u, bad tag=0x%x", frame_i, frame_type, tag);
3471     ShouldNotReachHere();
3472     break;
3473   } // end switch (tag)
3474 } // end rewrite_cp_refs_in_verification_type_info()
3475 
3476 























3477 // Change the constant pool associated with klass scratch_class to scratch_cp.
3478 // scratch_cp_length elements are copied from scratch_cp to a smaller constant pool
3479 // and the smaller constant pool is associated with scratch_class.
3480 void VM_RedefineClasses::set_new_constant_pool(
3481        ClassLoaderData* loader_data,
3482        InstanceKlass* scratch_class, constantPoolHandle scratch_cp,
3483        int scratch_cp_length, TRAPS) {
3484   assert(scratch_cp->length() >= scratch_cp_length, "sanity check");
3485 
3486   // scratch_cp is a merged constant pool and has enough space for a
3487   // worst case merge situation. We want to associate the minimum
3488   // sized constant pool with the klass to save space.
3489   ConstantPool* cp = ConstantPool::allocate(loader_data, scratch_cp_length, CHECK);
3490   constantPoolHandle smaller_cp(THREAD, cp);
3491 
3492   // preserve version() value in the smaller copy
3493   int version = scratch_cp->version();
3494   assert(version != 0, "sanity check");
3495   smaller_cp->set_version(version);
3496 

  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "cds/cdsConfig.hpp"
  26 #include "cds/metaspaceShared.hpp"
  27 #include "classfile/classFileStream.hpp"
  28 #include "classfile/classLoaderDataGraph.hpp"
  29 #include "classfile/classLoadInfo.hpp"
  30 #include "classfile/klassFactory.hpp"
  31 #include "classfile/javaClasses.inline.hpp"
  32 #include "classfile/metadataOnStackMark.hpp"
  33 #include "classfile/stackMapTable.hpp"
  34 #include "classfile/symbolTable.hpp"

  35 #include "classfile/verifier.hpp"
  36 #include "classfile/vmClasses.hpp"
  37 #include "classfile/vmSymbols.hpp"
  38 #include "code/codeCache.hpp"
  39 #include "compiler/compileBroker.hpp"
  40 #include "interpreter/oopMapCache.hpp"
  41 #include "interpreter/rewriter.hpp"
  42 #include "jfr/jfrEvents.hpp"
  43 #include "logging/logStream.hpp"
  44 #include "memory/metadataFactory.hpp"
  45 #include "memory/resourceArea.hpp"
  46 #include "memory/universe.hpp"
  47 #include "oops/annotations.hpp"
  48 #include "oops/constantPool.hpp"
  49 #include "oops/fieldStreams.inline.hpp"
  50 #include "oops/klass.inline.hpp"
  51 #include "oops/klassVtable.hpp"
  52 #include "oops/method.hpp"
  53 #include "oops/oop.inline.hpp"
  54 #include "oops/recordComponent.hpp"

 595       if (scratch_i != *merge_cp_length_p) {
 596         // The new entry in *merge_cp_p is at a different index than
 597         // the new entry in scratch_cp so we need to map the index values.
 598         map_index(scratch_cp, scratch_i, *merge_cp_length_p);
 599       }
 600       (*merge_cp_length_p)++;
 601     } break;
 602 
 603     // At this stage, Class or UnresolvedClass could be in scratch_cp, but not
 604     // ClassIndex
 605     case JVM_CONSTANT_ClassIndex: // fall through
 606 
 607     // Invalid is used as the tag for the second constant pool entry
 608     // occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should
 609     // not be seen by itself.
 610     case JVM_CONSTANT_Invalid: // fall through
 611 
 612     // At this stage, String could be here, but not StringIndex
 613     case JVM_CONSTANT_StringIndex: // fall through
 614 
 615     // At this stage JVM_CONSTANT_UnresolvedClassInError should not be here

 616     case JVM_CONSTANT_UnresolvedClassInError: // fall through
 617 
 618     default:
 619     {
 620       // leave a breadcrumb
 621       jbyte bad_value = scratch_cp->tag_at(scratch_i).value();
 622       ShouldNotReachHere();
 623     } break;
 624   } // end switch tag value
 625 } // end append_entry()
 626 
 627 
 628 u2 VM_RedefineClasses::find_or_append_indirect_entry(const constantPoolHandle& scratch_cp,
 629       int ref_i, constantPoolHandle *merge_cp_p, int *merge_cp_length_p) {
 630 
 631   int new_ref_i = ref_i;
 632   bool match = (ref_i < *merge_cp_length_p) &&
 633                scratch_cp->compare_entry_to(ref_i, *merge_cp_p, ref_i);
 634 
 635   if (!match) {

1912 bool VM_RedefineClasses::rewrite_cp_refs(InstanceKlass* scratch_class) {
1913 
1914   // rewrite constant pool references in the nest attributes:
1915   if (!rewrite_cp_refs_in_nest_attributes(scratch_class)) {
1916     // propagate failure back to caller
1917     return false;
1918   }
1919 
1920   // rewrite constant pool references in the Record attribute:
1921   if (!rewrite_cp_refs_in_record_attribute(scratch_class)) {
1922     // propagate failure back to caller
1923     return false;
1924   }
1925 
1926   // rewrite constant pool references in the PermittedSubclasses attribute:
1927   if (!rewrite_cp_refs_in_permitted_subclasses_attribute(scratch_class)) {
1928     // propagate failure back to caller
1929     return false;
1930   }
1931 
1932   // rewrite constant pool references in the LoadableDescriptors attribute:
1933   if (!rewrite_cp_refs_in_loadable_descriptors_attribute(scratch_class)) {
1934     // propagate failure back to caller
1935     return false;
1936   }
1937 
1938   // rewrite constant pool references in the methods:
1939   if (!rewrite_cp_refs_in_methods(scratch_class)) {
1940     // propagate failure back to caller
1941     return false;
1942   }
1943 
1944   // rewrite constant pool references in the class_annotations:
1945   if (!rewrite_cp_refs_in_class_annotations(scratch_class)) {
1946     // propagate failure back to caller
1947     return false;
1948   }
1949 
1950   // rewrite constant pool references in the fields_annotations:
1951   if (!rewrite_cp_refs_in_fields_annotations(scratch_class)) {
1952     // propagate failure back to caller
1953     return false;
1954   }
1955 
1956   // rewrite constant pool references in the methods_annotations:
1957   if (!rewrite_cp_refs_in_methods_annotations(scratch_class)) {

2066         }
2067       }
2068     }
2069   }
2070   return true;
2071 }
2072 
2073 // Rewrite constant pool references in the PermittedSubclasses attribute.
2074 bool VM_RedefineClasses::rewrite_cp_refs_in_permitted_subclasses_attribute(
2075        InstanceKlass* scratch_class) {
2076 
2077   Array<u2>* permitted_subclasses = scratch_class->permitted_subclasses();
2078   assert(permitted_subclasses != nullptr, "unexpected null permitted_subclasses");
2079   for (int i = 0; i < permitted_subclasses->length(); i++) {
2080     u2 cp_index = permitted_subclasses->at(i);
2081     permitted_subclasses->at_put(i, find_new_index(cp_index));
2082   }
2083   return true;
2084 }
2085 
2086 // Rewrite constant pool references in the LoadableDescriptors attribute.
2087 bool VM_RedefineClasses::rewrite_cp_refs_in_loadable_descriptors_attribute(
2088        InstanceKlass* scratch_class) {
2089 
2090   Array<u2>* loadable_descriptors = scratch_class->loadable_descriptors();
2091   assert(loadable_descriptors != nullptr, "unexpected null loadable_descriptors");
2092   for (int i = 0; i < loadable_descriptors->length(); i++) {
2093     u2 cp_index = loadable_descriptors->at(i);
2094     loadable_descriptors->at_put(i, find_new_index(cp_index));
2095   }
2096   return true;
2097 }
2098 
2099 // Rewrite constant pool references in the methods.
2100 bool VM_RedefineClasses::rewrite_cp_refs_in_methods(InstanceKlass* scratch_class) {
2101 
2102   Array<Method*>* methods = scratch_class->methods();
2103 
2104   if (methods == nullptr || methods->length() == 0) {
2105     // no methods so nothing to do
2106     return true;
2107   }
2108 
2109   JavaThread* THREAD = JavaThread::current(); // For exception macros.
2110   ExceptionMark em(THREAD);
2111 
2112   // rewrite constant pool references in the methods:
2113   for (int i = methods->length() - 1; i >= 0; i--) {
2114     methodHandle method(THREAD, methods->at(i));
2115     methodHandle new_method;
2116     rewrite_cp_refs_in_method(method, &new_method, THREAD);
2117     if (!new_method.is_null()) {
2118       // the method has been replaced so save the new method version

3266   // walk through each stack_map_frame
3267   u2 calc_number_of_entries = 0;
3268   for (; calc_number_of_entries < number_of_entries; calc_number_of_entries++) {
3269     // The stack_map_frame structure is a u1 frame_type followed by
3270     // 0 or more bytes of data:
3271     //
3272     // union stack_map_frame {
3273     //   same_frame;
3274     //   same_locals_1_stack_item_frame;
3275     //   same_locals_1_stack_item_frame_extended;
3276     //   chop_frame;
3277     //   same_frame_extended;
3278     //   append_frame;
3279     //   full_frame;
3280     // }
3281 
3282     assert(stackmap_p + 1 <= stackmap_end, "no room for frame_type");
3283     u1 frame_type = *stackmap_p;
3284     stackmap_p++;
3285 
3286    if (frame_type == 246) {  // EARLY_LARVAL
3287      // rewrite_cp_refs in  unset fields and fall through.
3288      rewrite_cp_refs_in_early_larval_stackmaps(stackmap_p, stackmap_end, calc_number_of_entries, frame_type);
3289      // The larval frames point to the next frame, so advance to the next frame and fall through.
3290      frame_type = *stackmap_p;
3291      stackmap_p++;
3292    }
3293 
3294     // same_frame {
3295     //   u1 frame_type = SAME; /* 0-63 */
3296     // }
3297     if (frame_type <= StackMapReader::SAME_FRAME_END) {
3298       // nothing more to do for same_frame
3299     }
3300 
3301     // same_locals_1_stack_item_frame {
3302     //   u1 frame_type = SAME_LOCALS_1_STACK_ITEM; /* 64-127 */
3303     //   verification_type_info stack[1];
3304     // }
3305     else if (frame_type >= StackMapReader::SAME_LOCALS_1_STACK_ITEM_FRAME_START &&
3306              frame_type <= StackMapReader::SAME_LOCALS_1_STACK_ITEM_FRAME_END) {
3307       rewrite_cp_refs_in_verification_type_info(stackmap_p, stackmap_end,
3308         calc_number_of_entries, frame_type);
3309     }
3310 
3311     // reserved for future use
3312     else if (frame_type >= StackMapReader::RESERVED_START &&
3313              frame_type <= StackMapReader::RESERVED_END) {

3483       ("frame_i=%u, frame_type=%u, cpool_index=%d", frame_i, frame_type, cpool_index);
3484   } break;
3485 
3486   // Uninitialized_variable_info {
3487   //   u1 tag = ITEM_Uninitialized; /* 8 */
3488   //   u2 offset;
3489   // }
3490   case ITEM_Uninitialized:
3491     assert(stackmap_p_ref + 2 <= stackmap_end, "no room for offset");
3492     stackmap_p_ref += 2;
3493     break;
3494 
3495   default:
3496     log_debug(redefine, class, stackmap)("frame_i=%u, frame_type=%u, bad tag=0x%x", frame_i, frame_type, tag);
3497     ShouldNotReachHere();
3498     break;
3499   } // end switch (tag)
3500 } // end rewrite_cp_refs_in_verification_type_info()
3501 
3502 
3503 void VM_RedefineClasses::rewrite_cp_refs_in_early_larval_stackmaps(
3504        address& stackmap_p_ref, address stackmap_end, u2 frame_i,
3505        u1 frame_type) {
3506 
3507     u2 num_early_larval_stackmaps = Bytes::get_Java_u2(stackmap_p_ref);
3508     stackmap_p_ref += 2;
3509 
3510     for (u2 i = 0; i < num_early_larval_stackmaps; i++) {
3511 
3512       u2 name_and_ref_index = Bytes::get_Java_u2(stackmap_p_ref);
3513       u2 new_cp_index = find_new_index(name_and_ref_index);
3514       if (new_cp_index != 0) {
3515         log_debug(redefine, class, stackmap)("mapped old name_and_ref_index=%d", name_and_ref_index);
3516         Bytes::put_Java_u2(stackmap_p_ref, new_cp_index);
3517         name_and_ref_index = new_cp_index;
3518       }
3519       log_debug(redefine, class, stackmap)
3520         ("frame_i=%u, frame_type=%u, name_and_ref_index=%d", frame_i, frame_type, name_and_ref_index);
3521 
3522       stackmap_p_ref += 2;
3523     }
3524 } // rewrite_cp_refs_in_early_larval_stackmaps
3525 
3526 // Change the constant pool associated with klass scratch_class to scratch_cp.
3527 // scratch_cp_length elements are copied from scratch_cp to a smaller constant pool
3528 // and the smaller constant pool is associated with scratch_class.
3529 void VM_RedefineClasses::set_new_constant_pool(
3530        ClassLoaderData* loader_data,
3531        InstanceKlass* scratch_class, constantPoolHandle scratch_cp,
3532        int scratch_cp_length, TRAPS) {
3533   assert(scratch_cp->length() >= scratch_cp_length, "sanity check");
3534 
3535   // scratch_cp is a merged constant pool and has enough space for a
3536   // worst case merge situation. We want to associate the minimum
3537   // sized constant pool with the klass to save space.
3538   ConstantPool* cp = ConstantPool::allocate(loader_data, scratch_cp_length, CHECK);
3539   constantPoolHandle smaller_cp(THREAD, cp);
3540 
3541   // preserve version() value in the smaller copy
3542   int version = scratch_cp->version();
3543   assert(version != 0, "sanity check");
3544   smaller_cp->set_version(version);
3545 
< prev index next >