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/symbolTable.hpp"
33 #include "classfile/klassFactory.hpp"
34 #include "classfile/verifier.hpp"
35 #include "classfile/vmClasses.hpp"
36 #include "classfile/vmSymbols.hpp"
37 #include "code/codeCache.hpp"
38 #include "compiler/compileBroker.hpp"
39 #include "interpreter/oopMapCache.hpp"
40 #include "interpreter/rewriter.hpp"
41 #include "jfr/jfrEvents.hpp"
42 #include "logging/logStream.hpp"
43 #include "memory/metadataFactory.hpp"
44 #include "memory/resourceArea.hpp"
45 #include "memory/universe.hpp"
46 #include "oops/annotations.hpp"
47 #include "oops/constantPool.hpp"
48 #include "oops/fieldStreams.inline.hpp"
49 #include "oops/klass.inline.hpp"
50 #include "oops/klassVtable.hpp"
51 #include "oops/method.hpp"
52 #include "oops/oop.inline.hpp"
53 #include "oops/recordComponent.hpp"
594 if (scratch_i != *merge_cp_length_p) {
595 // The new entry in *merge_cp_p is at a different index than
596 // the new entry in scratch_cp so we need to map the index values.
597 map_index(scratch_cp, scratch_i, *merge_cp_length_p);
598 }
599 (*merge_cp_length_p)++;
600 } break;
601
602 // At this stage, Class or UnresolvedClass could be in scratch_cp, but not
603 // ClassIndex
604 case JVM_CONSTANT_ClassIndex: // fall through
605
606 // Invalid is used as the tag for the second constant pool entry
607 // occupied by JVM_CONSTANT_Double or JVM_CONSTANT_Long. It should
608 // not be seen by itself.
609 case JVM_CONSTANT_Invalid: // fall through
610
611 // At this stage, String could be here, but not StringIndex
612 case JVM_CONSTANT_StringIndex: // fall through
613
614 // At this stage JVM_CONSTANT_UnresolvedClassInError should not be
615 // 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) {
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 <= 63) {
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 >= 64 && frame_type <= 127) {
3278 rewrite_cp_refs_in_verification_type_info(stackmap_p, stackmap_end,
3279 calc_number_of_entries, frame_type);
3280 }
3281
3282 // reserved for future use
3283 else if (frame_type >= 128 && frame_type <= 246) {
3284 // nothing more to do for reserved frame_types
3285 }
3286
3287 // same_locals_1_stack_item_frame_extended {
3288 // u1 frame_type = SAME_LOCALS_1_STACK_ITEM_EXTENDED; /* 247 */
3289 // u2 offset_delta;
3290 // verification_type_info stack[1];
3291 // }
3292 else if (frame_type == 247) {
3293 stackmap_p += 2;
3294 rewrite_cp_refs_in_verification_type_info(stackmap_p, stackmap_end,
3295 calc_number_of_entries, frame_type);
3296 }
3297
3298 // chop_frame {
3299 // u1 frame_type = CHOP; /* 248-250 */
3300 // u2 offset_delta;
3301 // }
3302 else if (frame_type >= 248 && frame_type <= 250) {
3303 stackmap_p += 2;
3451 ("frame_i=%u, frame_type=%u, cpool_index=%d", frame_i, frame_type, cpool_index);
3452 } break;
3453
3454 // Uninitialized_variable_info {
3455 // u1 tag = ITEM_Uninitialized; /* 8 */
3456 // u2 offset;
3457 // }
3458 case ITEM_Uninitialized:
3459 assert(stackmap_p_ref + 2 <= stackmap_end, "no room for offset");
3460 stackmap_p_ref += 2;
3461 break;
3462
3463 default:
3464 log_debug(redefine, class, stackmap)("frame_i=%u, frame_type=%u, bad tag=0x%x", frame_i, frame_type, tag);
3465 ShouldNotReachHere();
3466 break;
3467 } // end switch (tag)
3468 } // end rewrite_cp_refs_in_verification_type_info()
3469
3470
3471 // Change the constant pool associated with klass scratch_class to scratch_cp.
3472 // scratch_cp_length elements are copied from scratch_cp to a smaller constant pool
3473 // and the smaller constant pool is associated with scratch_class.
3474 void VM_RedefineClasses::set_new_constant_pool(
3475 ClassLoaderData* loader_data,
3476 InstanceKlass* scratch_class, constantPoolHandle scratch_cp,
3477 int scratch_cp_length, TRAPS) {
3478 assert(scratch_cp->length() >= scratch_cp_length, "sanity check");
3479
3480 // scratch_cp is a merged constant pool and has enough space for a
3481 // worst case merge situation. We want to associate the minimum
3482 // sized constant pool with the klass to save space.
3483 ConstantPool* cp = ConstantPool::allocate(loader_data, scratch_cp_length, CHECK);
3484 constantPoolHandle smaller_cp(THREAD, cp);
3485
3486 // preserve version() value in the smaller copy
3487 int version = scratch_cp->version();
3488 assert(version != 0, "sanity check");
3489 smaller_cp->set_version(version);
3490
|
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/klassFactory.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) {
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 LoadableDescriptors attribute:
1932 if (!rewrite_cp_refs_in_loadable_descriptors_attribute(scratch_class)) {
1933 // propagate failure back to caller
1934 return false;
1935 }
1936
1937 // rewrite constant pool references in the methods:
1938 if (!rewrite_cp_refs_in_methods(scratch_class)) {
1939 // propagate failure back to caller
1940 return false;
1941 }
1942
1943 // rewrite constant pool references in the class_annotations:
1944 if (!rewrite_cp_refs_in_class_annotations(scratch_class)) {
1945 // propagate failure back to caller
1946 return false;
1947 }
1948
1949 // rewrite constant pool references in the fields_annotations:
1950 if (!rewrite_cp_refs_in_fields_annotations(scratch_class)) {
1951 // propagate failure back to caller
1952 return false;
1953 }
1954
1955 // rewrite constant pool references in the methods_annotations:
1956 if (!rewrite_cp_refs_in_methods_annotations(scratch_class)) {
2065 }
2066 }
2067 }
2068 }
2069 return true;
2070 }
2071
2072 // Rewrite constant pool references in the PermittedSubclasses attribute.
2073 bool VM_RedefineClasses::rewrite_cp_refs_in_permitted_subclasses_attribute(
2074 InstanceKlass* scratch_class) {
2075
2076 Array<u2>* permitted_subclasses = scratch_class->permitted_subclasses();
2077 assert(permitted_subclasses != nullptr, "unexpected null permitted_subclasses");
2078 for (int i = 0; i < permitted_subclasses->length(); i++) {
2079 u2 cp_index = permitted_subclasses->at(i);
2080 permitted_subclasses->at_put(i, find_new_index(cp_index));
2081 }
2082 return true;
2083 }
2084
2085 // Rewrite constant pool references in the LoadableDescriptors attribute.
2086 bool VM_RedefineClasses::rewrite_cp_refs_in_loadable_descriptors_attribute(
2087 InstanceKlass* scratch_class) {
2088
2089 Array<u2>* loadable_descriptors = scratch_class->loadable_descriptors();
2090 assert(loadable_descriptors != nullptr, "unexpected null loadable_descriptors");
2091 for (int i = 0; i < loadable_descriptors->length(); i++) {
2092 u2 cp_index = loadable_descriptors->at(i);
2093 loadable_descriptors->at_put(i, find_new_index(cp_index));
2094 }
2095 return true;
2096 }
2097
2098 // Rewrite constant pool references in the methods.
2099 bool VM_RedefineClasses::rewrite_cp_refs_in_methods(InstanceKlass* scratch_class) {
2100
2101 Array<Method*>* methods = scratch_class->methods();
2102
2103 if (methods == nullptr || methods->length() == 0) {
2104 // no methods so nothing to do
2105 return true;
2106 }
2107
2108 JavaThread* THREAD = JavaThread::current(); // For exception macros.
2109 ExceptionMark em(THREAD);
2110
2111 // rewrite constant pool references in the methods:
2112 for (int i = methods->length() - 1; i >= 0; i--) {
2113 methodHandle method(THREAD, methods->at(i));
2114 methodHandle new_method;
2115 rewrite_cp_refs_in_method(method, &new_method, THREAD);
2116 if (!new_method.is_null()) {
2117 // the method has been replaced so save the new method version
3265 // walk through each stack_map_frame
3266 u2 calc_number_of_entries = 0;
3267 for (; calc_number_of_entries < number_of_entries; calc_number_of_entries++) {
3268 // The stack_map_frame structure is a u1 frame_type followed by
3269 // 0 or more bytes of data:
3270 //
3271 // union stack_map_frame {
3272 // same_frame;
3273 // same_locals_1_stack_item_frame;
3274 // same_locals_1_stack_item_frame_extended;
3275 // chop_frame;
3276 // same_frame_extended;
3277 // append_frame;
3278 // full_frame;
3279 // }
3280
3281 assert(stackmap_p + 1 <= stackmap_end, "no room for frame_type");
3282 u1 frame_type = *stackmap_p;
3283 stackmap_p++;
3284
3285 if (frame_type == 246) { // EARLY_LARVAL
3286 // rewrite_cp_refs in unset fields and fall through.
3287 rewrite_cp_refs_in_early_larval_stackmaps(stackmap_p, stackmap_end, calc_number_of_entries, frame_type);
3288 // The larval frames point to the next frame, so advance to the next frame and fall through.
3289 frame_type = *stackmap_p;
3290 stackmap_p++;
3291 }
3292
3293 // same_frame {
3294 // u1 frame_type = SAME; /* 0-63 */
3295 // }
3296 if (frame_type <= 63) {
3297 // nothing more to do for same_frame
3298 }
3299
3300 // same_locals_1_stack_item_frame {
3301 // u1 frame_type = SAME_LOCALS_1_STACK_ITEM; /* 64-127 */
3302 // verification_type_info stack[1];
3303 // }
3304 else if (frame_type >= 64 && frame_type <= 127) {
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 >= 128 && frame_type <= 245) {
3311 // nothing more to do for reserved frame_types
3312 }
3313
3314 // same_locals_1_stack_item_frame_extended {
3315 // u1 frame_type = SAME_LOCALS_1_STACK_ITEM_EXTENDED; /* 247 */
3316 // u2 offset_delta;
3317 // verification_type_info stack[1];
3318 // }
3319 else if (frame_type == 247) {
3320 stackmap_p += 2;
3321 rewrite_cp_refs_in_verification_type_info(stackmap_p, stackmap_end,
3322 calc_number_of_entries, frame_type);
3323 }
3324
3325 // chop_frame {
3326 // u1 frame_type = CHOP; /* 248-250 */
3327 // u2 offset_delta;
3328 // }
3329 else if (frame_type >= 248 && frame_type <= 250) {
3330 stackmap_p += 2;
3478 ("frame_i=%u, frame_type=%u, cpool_index=%d", frame_i, frame_type, cpool_index);
3479 } break;
3480
3481 // Uninitialized_variable_info {
3482 // u1 tag = ITEM_Uninitialized; /* 8 */
3483 // u2 offset;
3484 // }
3485 case ITEM_Uninitialized:
3486 assert(stackmap_p_ref + 2 <= stackmap_end, "no room for offset");
3487 stackmap_p_ref += 2;
3488 break;
3489
3490 default:
3491 log_debug(redefine, class, stackmap)("frame_i=%u, frame_type=%u, bad tag=0x%x", frame_i, frame_type, tag);
3492 ShouldNotReachHere();
3493 break;
3494 } // end switch (tag)
3495 } // end rewrite_cp_refs_in_verification_type_info()
3496
3497
3498 void VM_RedefineClasses::rewrite_cp_refs_in_early_larval_stackmaps(
3499 address& stackmap_p_ref, address stackmap_end, u2 frame_i,
3500 u1 frame_type) {
3501
3502 u2 num_early_larval_stackmaps = Bytes::get_Java_u2(stackmap_p_ref);
3503 stackmap_p_ref += 2;
3504
3505 for (u2 i = 0; i < num_early_larval_stackmaps; i++) {
3506
3507 u2 name_and_ref_index = Bytes::get_Java_u2(stackmap_p_ref);
3508 u2 new_cp_index = find_new_index(name_and_ref_index);
3509 if (new_cp_index != 0) {
3510 log_debug(redefine, class, stackmap)("mapped old name_and_ref_index=%d", name_and_ref_index);
3511 Bytes::put_Java_u2(stackmap_p_ref, new_cp_index);
3512 name_and_ref_index = new_cp_index;
3513 }
3514 log_debug(redefine, class, stackmap)
3515 ("frame_i=%u, frame_type=%u, name_and_ref_index=%d", frame_i, frame_type, name_and_ref_index);
3516
3517 stackmap_p_ref += 2;
3518 }
3519 } // rewrite_cp_refs_in_early_larval_stackmaps
3520
3521 // Change the constant pool associated with klass scratch_class to scratch_cp.
3522 // scratch_cp_length elements are copied from scratch_cp to a smaller constant pool
3523 // and the smaller constant pool is associated with scratch_class.
3524 void VM_RedefineClasses::set_new_constant_pool(
3525 ClassLoaderData* loader_data,
3526 InstanceKlass* scratch_class, constantPoolHandle scratch_cp,
3527 int scratch_cp_length, TRAPS) {
3528 assert(scratch_cp->length() >= scratch_cp_length, "sanity check");
3529
3530 // scratch_cp is a merged constant pool and has enough space for a
3531 // worst case merge situation. We want to associate the minimum
3532 // sized constant pool with the klass to save space.
3533 ConstantPool* cp = ConstantPool::allocate(loader_data, scratch_cp_length, CHECK);
3534 constantPoolHandle smaller_cp(THREAD, cp);
3535
3536 // preserve version() value in the smaller copy
3537 int version = scratch_cp->version();
3538 assert(version != 0, "sanity check");
3539 smaller_cp->set_version(version);
3540
|