< prev index next >

src/hotspot/share/prims/jvmtiRedefineClasses.cpp

Print this page

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

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






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

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













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

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








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

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























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

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

 626     case JVM_CONSTANT_UnresolvedClassInError: // fall through
 627 
 628     default:
 629     {
 630       // leave a breadcrumb
 631       jbyte bad_value = scratch_cp->tag_at(scratch_i).value();
 632       ShouldNotReachHere();
 633     } break;
 634   } // end switch tag value
 635 } // end append_entry()
 636 
 637 
 638 u2 VM_RedefineClasses::find_or_append_indirect_entry(const constantPoolHandle& scratch_cp,
 639       int ref_i, constantPoolHandle *merge_cp_p, int *merge_cp_length_p) {
 640 
 641   int new_ref_i = ref_i;
 642   bool match = (ref_i < *merge_cp_length_p) &&
 643                scratch_cp->compare_entry_to(ref_i, *merge_cp_p, ref_i);
 644 
 645   if (!match) {

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

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

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

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