36 #include "classfile/moduleEntry.hpp"
37 #include "classfile/stringTable.hpp"
38 #include "classfile/symbolTable.hpp"
39 #include "classfile/systemDictionary.hpp"
40 #include "classfile/vmClasses.hpp"
41 #include "classfile/vmSymbols.hpp"
42 #include "code/debugInfo.hpp"
43 #include "code/dependencyContext.hpp"
44 #include "code/pcDesc.hpp"
45 #include "gc/shared/collectedHeap.inline.hpp"
46 #include "interpreter/interpreter.hpp"
47 #include "interpreter/linkResolver.hpp"
48 #include "jvm.h"
49 #include "logging/log.hpp"
50 #include "logging/logStream.hpp"
51 #include "memory/oopFactory.hpp"
52 #include "memory/resourceArea.hpp"
53 #include "memory/universe.hpp"
54 #include "oops/fieldInfo.hpp"
55 #include "oops/fieldStreams.inline.hpp"
56 #include "oops/instanceKlass.inline.hpp"
57 #include "oops/instanceMirrorKlass.hpp"
58 #include "oops/klass.inline.hpp"
59 #include "oops/method.inline.hpp"
60 #include "oops/objArrayKlass.hpp"
61 #include "oops/objArrayOop.inline.hpp"
62 #include "oops/oop.inline.hpp"
63 #include "oops/oopCast.inline.hpp"
64 #include "oops/recordComponent.hpp"
65 #include "oops/symbol.hpp"
66 #include "oops/typeArrayOop.inline.hpp"
67 #include "prims/jvmtiExport.hpp"
68 #include "prims/methodHandles.hpp"
69 #include "prims/resolvedMethodTable.hpp"
70 #include "runtime/continuationEntry.inline.hpp"
71 #include "runtime/continuationJavaClasses.inline.hpp"
72 #include "runtime/fieldDescriptor.inline.hpp"
73 #include "runtime/frame.inline.hpp"
74 #include "runtime/handles.inline.hpp"
75 #include "runtime/handshake.hpp"
76 #include "runtime/init.hpp"
77 #include "runtime/interfaceSupport.inline.hpp"
1068 mirror = Handle(THREAD, mirror_oop);
1069
1070 // Setup indirection from mirror->klass
1071 set_klass(mirror(), k);
1072
1073 // Set the modifiers flag.
1074 u2 computed_modifiers = k->compute_modifier_flags();
1075 set_modifiers(mirror(), computed_modifiers);
1076 // Set the raw access_flags, this is used by reflection instead of modifier flags.
1077 // The Java code for array classes gets the access flags from the element type.
1078 assert(!k->is_array_klass() || k->access_flags().as_unsigned_short() == 0, "access flags are not set for arrays");
1079 set_raw_access_flags(mirror(), k->access_flags().as_unsigned_short());
1080
1081 InstanceMirrorKlass* mk = InstanceMirrorKlass::cast(mirror->klass());
1082 assert(oop_size(mirror()) == mk->instance_size(k), "should have been set");
1083
1084 set_static_oop_field_count(mirror(), mk->compute_static_oop_field_count(mirror()));
1085
1086 // It might also have a component mirror. This mirror must already exist.
1087 if (k->is_array_klass()) {
1088 if (k->is_typeArray_klass()) {
1089 BasicType type = TypeArrayKlass::cast(k)->element_type();
1090 if (is_scratch) {
1091 comp_mirror = Handle(THREAD, HeapShared::scratch_java_mirror(type));
1092 } else {
1093 comp_mirror = Handle(THREAD, Universe::java_mirror(type));
1094 }
1095 } else {
1096 assert(k->is_objArray_klass(), "Must be");
1097 Klass* element_klass = ObjArrayKlass::cast(k)->element_klass();
1098 assert(element_klass != nullptr, "Must have an element klass");
1099 if (is_scratch) {
1100 comp_mirror = Handle(THREAD, HeapShared::scratch_java_mirror(element_klass));
1101 } else {
1102 comp_mirror = Handle(THREAD, element_klass->java_mirror());
1103 }
1104 }
1105 assert(comp_mirror() != nullptr, "must have a mirror");
1106
1107 // Two-way link between the array klass and its component mirror:
1108 // (array_klass) k -> mirror -> component_mirror -> array_klass -> k
1109 set_component_mirror(mirror(), comp_mirror());
1110 // See below for ordering dependencies between field array_klass in component mirror
1111 // and java_mirror in this klass.
1112 } else {
1113 assert(k->is_instance_klass(), "Must be");
1114
1115 initialize_mirror_fields(k, mirror, protection_domain, classData, THREAD);
1116 if (HAS_PENDING_EXCEPTION) {
1117 // If any of the fields throws an exception like OOM remove the klass field
1118 // from the mirror so GC doesn't follow it after the klass has been deallocated.
1119 // This mirror looks like a primitive type, which logically it is because it
1120 // it represents no class.
1121 set_klass(mirror(), nullptr);
1122 return;
1123 }
1124 }
1125 }
1126
1127 void java_lang_Class::create_mirror(Klass* k, Handle class_loader,
1128 Handle module, Handle protection_domain,
1129 Handle classData, TRAPS) {
1130 assert(k != nullptr, "Use create_basic_type_mirror for primitive types");
1131 assert(k->java_mirror() == nullptr, "should only assign mirror once");
1132
1133 // Class_klass has to be loaded because it is used to allocate
1134 // the mirror.
1135 if (vmClasses::Class_klass_loaded()) {
1136 Handle mirror;
1137 Handle comp_mirror;
1138
1139 allocate_mirror(k, /*is_scratch=*/false, protection_domain, classData, mirror, comp_mirror, CHECK);
1140
1141 // set the classLoader field in the java_lang_Class instance
1142 assert(class_loader() == k->class_loader(), "should be same");
1143 set_class_loader(mirror(), class_loader());
1144
1145 // Setup indirection from klass->mirror
1146 // after any exceptions can happen during allocations.
1147 k->set_java_mirror(mirror);
1148
1149 // Set the module field in the java_lang_Class instance. This must be done
1150 // after the mirror is set.
1151 set_mirror_module_field(THREAD, k, mirror, module);
1152
1153 if (comp_mirror() != nullptr) {
1154 // Set after k->java_mirror() is published, because compiled code running
1155 // concurrently doesn't expect a k to have a null java_mirror.
1156 release_set_array_klass(comp_mirror(), k);
1157 }
1158 if (CDSConfig::is_dumping_heap()) {
1159 create_scratch_mirror(k, CHECK);
1160 }
1161 } else {
1162 assert(fixup_mirror_list() != nullptr, "fixup_mirror_list not initialized");
1163 fixup_mirror_list()->push(k);
1164 }
1165 }
1166
1167 #if INCLUDE_CDS_JAVA_HEAP
1168 // The "scratch mirror" stores the states of the mirror object that can be
1169 // decided at dump time (such as the initial values of the static fields, the
1170 // component mirror, etc). At runtime, more information is added to it by
1171 // java_lang_Class::restore_archived_mirror().
1172 //
1173 // Essentially, /*dumptime*/create_scratch_mirror() + /*runtime*/restore_archived_mirror()
1174 // produces the same result as /*runtime*/create_mirror().
1175 //
1176 // Note: we archive the "scratch mirror" instead of k->java_mirror(), because the
1177 // latter may contain dumptime-specific information that cannot be archived
1178 // (e.g., ClassLoaderData*, or static fields that are modified by Java code execution).
1179 void java_lang_Class::create_scratch_mirror(Klass* k, TRAPS) {
1180 if (k->class_loader() != nullptr &&
1181 k->class_loader() != SystemDictionary::java_platform_loader() &&
1182 k->class_loader() != SystemDictionary::java_system_loader()) {
1183 // We only archive the mirrors of classes loaded by the built-in loaders
1184 return;
1185 }
1186
1187 Handle protection_domain, classData; // set to null. Will be reinitialized at runtime
1188 Handle mirror;
1189 Handle comp_mirror;
1190 allocate_mirror(k, /*is_scratch=*/true, protection_domain, classData, mirror, comp_mirror, CHECK);
1191
1192 if (comp_mirror() != nullptr) {
1193 release_set_array_klass(comp_mirror(), k);
1194 }
1195
1196 HeapShared::set_scratch_java_mirror(k, mirror());
1197 }
1198
1199 // Returns true if the mirror is updated, false if no archived mirror
1200 // data is present. After the archived mirror object is restored, the
1201 // shared klass' _has_raw_archived_mirror flag is cleared.
1202 bool java_lang_Class::restore_archived_mirror(Klass *k,
1203 Handle class_loader, Handle module,
1204 Handle protection_domain, TRAPS) {
1205 // Postpone restoring archived mirror until java.lang.Class is loaded. Please
1206 // see more details in vmClasses::resolve_all().
1207 if (!vmClasses::Class_klass_loaded()) {
1208 assert(fixup_mirror_list() != nullptr, "fixup_mirror_list not initialized");
1209 fixup_mirror_list()->push(k);
1210 return true;
1211 }
1212
1213 oop m = k->archived_java_mirror();
1214 assert(m != nullptr, "must have stored non-null archived mirror");
1215
1216 // Sanity: clear it now to prevent re-initialization if any of the following fails
1217 k->clear_archived_mirror_index();
1218
1219 // mirror is archived, restore
1220 log_debug(aot, mirror)("Archived mirror is: " PTR_FORMAT, p2i(m));
1221 assert(as_Klass(m) == k, "must be");
1222 Handle mirror(THREAD, m);
1223
1224 if (!k->is_array_klass()) {
1225 // - local static final fields with initial values were initialized at dump time
1226
1227 // create the init_lock
1228 typeArrayOop r = oopFactory::new_typeArray(T_INT, 0, CHECK_(false));
1229 set_init_lock(mirror(), r);
1230
1231 if (protection_domain.not_null()) {
1232 set_protection_domain(mirror(), protection_domain());
1233 }
1234 }
1235
1236 assert(class_loader() == k->class_loader(), "should be same");
1237 if (class_loader.not_null()) {
1238 set_class_loader(mirror(), class_loader());
1239 }
1240
1241 k->set_java_mirror(mirror);
1242
1243 set_mirror_module_field(THREAD, k, mirror, module);
1244
1245 if (log_is_enabled(Trace, aot, heap, mirror)) {
1246 ResourceMark rm(THREAD);
1247 log_trace(aot, heap, mirror)(
1248 "Restored %s archived mirror " PTR_FORMAT, k->external_name(), p2i(mirror()));
1249 }
1250
1251 return true;
1252 }
1253 #endif // INCLUDE_CDS_JAVA_HEAP
1392 assert(is_instance(java_class), "must be a Class object");
1393 java_class->metadata_field_put(_klass_offset, klass);
1394 }
1395
1396
1397 void java_lang_Class::print_signature(oop java_class, outputStream* st) {
1398 assert(is_instance(java_class), "must be a Class object");
1399 Symbol* name = nullptr;
1400 bool is_instance = false;
1401 if (is_primitive(java_class)) {
1402 name = vmSymbols::type_signature(primitive_type(java_class));
1403 } else {
1404 Klass* k = as_Klass(java_class);
1405 is_instance = k->is_instance_klass();
1406 name = k->name();
1407 }
1408 if (name == nullptr) {
1409 st->print("<null>");
1410 return;
1411 }
1412 if (is_instance) st->print("L");
1413 st->write((char*) name->base(), (int) name->utf8_length());
1414 if (is_instance) st->print(";");
1415 }
1416
1417 Symbol* java_lang_Class::as_signature(oop java_class, bool intern_if_not_found) {
1418 assert(is_instance(java_class), "must be a Class object");
1419 Symbol* name;
1420 if (is_primitive(java_class)) {
1421 name = vmSymbols::type_signature(primitive_type(java_class));
1422 // Because this can create a new symbol, the caller has to decrement
1423 // the refcount, so make adjustment here and below for symbols returned
1424 // that are not created or incremented due to a successful lookup.
1425 name->increment_refcount();
1426 } else {
1427 Klass* k = as_Klass(java_class);
1428 if (!k->is_instance_klass()) {
1429 name = k->name();
1430 name->increment_refcount();
1431 } else {
1432 ResourceMark rm;
1451 if (is_primitive(java_class)) {
1452 name = type2name(primitive_type(java_class));
1453 } else {
1454 name = as_Klass(java_class)->external_name();
1455 }
1456 if (name == nullptr) {
1457 name = "<null>";
1458 }
1459 return name;
1460 }
1461
1462 Klass* java_lang_Class::array_klass_acquire(oop java_class) {
1463 Klass* k = ((Klass*)java_class->metadata_field_acquire(_array_klass_offset));
1464 assert(k == nullptr || (k->is_klass() && k->is_array_klass()), "should be array klass");
1465 return k;
1466 }
1467
1468
1469 void java_lang_Class::release_set_array_klass(oop java_class, Klass* klass) {
1470 assert(klass->is_klass() && klass->is_array_klass(), "should be array klass");
1471 java_class->release_metadata_field_put(_array_klass_offset, klass);
1472 }
1473
1474
1475 BasicType java_lang_Class::primitive_type(oop java_class) {
1476 assert(is_primitive(java_class), "just checking");
1477 Klass* ak = ((Klass*)java_class->metadata_field(_array_klass_offset));
1478 BasicType type = T_VOID;
1479 if (ak != nullptr) {
1480 // Note: create_basic_type_mirror above initializes ak to a non-null value.
1481 type = ArrayKlass::cast(ak)->element_type();
1482 } else {
1483 assert(java_class == Universe::void_mirror(), "only valid non-array primitive");
1484 }
1485 #ifdef ASSERT
1486 if (CDSConfig::is_dumping_heap()) {
1487 oop mirror = Universe::java_mirror(type);
1488 oop scratch_mirror = HeapShared::scratch_java_mirror(type);
1489 assert(java_class == mirror || java_class == scratch_mirror, "must be consistent");
1490 } else {
2845
2846 // the format of the stacktrace will be:
2847 // - 1 or more fillInStackTrace frames for the exception class (skipped)
2848 // - 0 or more <init> methods for the exception class (skipped)
2849 // - rest of the stack
2850
2851 if (!skip_fillInStackTrace_check) {
2852 if (method->name() == vmSymbols::fillInStackTrace_name() &&
2853 throwable->is_a(method->method_holder())) {
2854 continue;
2855 }
2856 else {
2857 skip_fillInStackTrace_check = true; // gone past them all
2858 }
2859 }
2860 if (!skip_throwableInit_check) {
2861 assert(skip_fillInStackTrace_check, "logic error in backtrace filtering");
2862
2863 // skip <init> methods of the exception class and superclasses
2864 // This is similar to classic VM.
2865 if (method->name() == vmSymbols::object_initializer_name() &&
2866 throwable->is_a(method->method_holder())) {
2867 continue;
2868 } else {
2869 // there are none or we've seen them all - either way stop checking
2870 skip_throwableInit_check = true;
2871 }
2872 }
2873 if (method->is_hidden() || method->is_continuation_enter_intrinsic()) {
2874 if (skip_hidden) {
2875 if (total_count == 0) {
2876 // The top frame will be hidden from the stack trace.
2877 bt.set_has_hidden_top_frame();
2878 }
2879 continue;
2880 }
2881 }
2882
2883 bt.push(method, bci, CHECK);
2884 total_count++;
2885 }
3204 int java_lang_ClassFrameInfo::_classOrMemberName_offset;
3205 int java_lang_ClassFrameInfo::_flags_offset;
3206
3207 #define CLASSFRAMEINFO_FIELDS_DO(macro) \
3208 macro(_classOrMemberName_offset, k, "classOrMemberName", object_signature, false); \
3209 macro(_flags_offset, k, vmSymbols::flags_name(), int_signature, false)
3210
3211 void java_lang_ClassFrameInfo::compute_offsets() {
3212 InstanceKlass* k = vmClasses::ClassFrameInfo_klass();
3213 CLASSFRAMEINFO_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3214 }
3215
3216 #if INCLUDE_CDS
3217 void java_lang_ClassFrameInfo::serialize_offsets(SerializeClosure* f) {
3218 CLASSFRAMEINFO_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3219 }
3220 #endif
3221
3222 static int get_flags(const methodHandle& m) {
3223 int flags = m->access_flags().as_method_flags();
3224 if (m->is_object_initializer()) {
3225 flags |= java_lang_invoke_MemberName::MN_IS_CONSTRUCTOR;
3226 } else {
3227 // Note: Static initializers can be here. Record them as plain methods.
3228 flags |= java_lang_invoke_MemberName::MN_IS_METHOD;
3229 }
3230 if (m->caller_sensitive()) {
3231 flags |= java_lang_invoke_MemberName::MN_CALLER_SENSITIVE;
3232 }
3233 if (m->is_hidden()) {
3234 flags |= java_lang_invoke_MemberName::MN_HIDDEN_MEMBER;
3235 }
3236 assert((flags & 0xFF000000) == 0, "unexpected flags");
3237 return flags;
3238 }
3239
3240 oop java_lang_ClassFrameInfo::classOrMemberName(oop obj) {
3241 return obj->obj_field(_classOrMemberName_offset);
3242 }
3243
3244 int java_lang_ClassFrameInfo::flags(oop obj) {
3245 return obj->int_field(_flags_offset);
3596 constructor->int_field_put(_modifiers_offset, value);
3597 }
3598
3599 void java_lang_reflect_Constructor::set_signature(oop constructor, oop value) {
3600 constructor->obj_field_put(_signature_offset, value);
3601 }
3602
3603 void java_lang_reflect_Constructor::set_annotations(oop constructor, oop value) {
3604 constructor->obj_field_put(_annotations_offset, value);
3605 }
3606
3607 void java_lang_reflect_Constructor::set_parameter_annotations(oop method, oop value) {
3608 method->obj_field_put(_parameter_annotations_offset, value);
3609 }
3610
3611 int java_lang_reflect_Field::_clazz_offset;
3612 int java_lang_reflect_Field::_name_offset;
3613 int java_lang_reflect_Field::_type_offset;
3614 int java_lang_reflect_Field::_slot_offset;
3615 int java_lang_reflect_Field::_modifiers_offset;
3616 int java_lang_reflect_Field::_trusted_final_offset;
3617 int java_lang_reflect_Field::_signature_offset;
3618 int java_lang_reflect_Field::_annotations_offset;
3619
3620 #define FIELD_FIELDS_DO(macro) \
3621 macro(_clazz_offset, k, vmSymbols::clazz_name(), class_signature, false); \
3622 macro(_name_offset, k, vmSymbols::name_name(), string_signature, false); \
3623 macro(_type_offset, k, vmSymbols::type_name(), class_signature, false); \
3624 macro(_slot_offset, k, vmSymbols::slot_name(), int_signature, false); \
3625 macro(_modifiers_offset, k, vmSymbols::modifiers_name(), int_signature, false); \
3626 macro(_trusted_final_offset, k, vmSymbols::trusted_final_name(), bool_signature, false); \
3627 macro(_signature_offset, k, vmSymbols::signature_name(), string_signature, false); \
3628 macro(_annotations_offset, k, vmSymbols::annotations_name(), byte_array_signature, false);
3629
3630 void java_lang_reflect_Field::compute_offsets() {
3631 InstanceKlass* k = vmClasses::reflect_Field_klass();
3632 FIELD_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3633 }
3634
3635 #if INCLUDE_CDS
3636 void java_lang_reflect_Field::serialize_offsets(SerializeClosure* f) {
3637 FIELD_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3638 }
3639 #endif
3640
3641 Handle java_lang_reflect_Field::create(TRAPS) {
3642 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3643 Symbol* name = vmSymbols::java_lang_reflect_Field();
3644 Klass* k = SystemDictionary::resolve_or_fail(name, true, CHECK_NH);
3645 InstanceKlass* ik = InstanceKlass::cast(k);
3646 // Ensure it is initialized
3671 void java_lang_reflect_Field::set_type(oop field, oop value) {
3672 field->obj_field_put(_type_offset, value);
3673 }
3674
3675 int java_lang_reflect_Field::slot(oop reflect) {
3676 return reflect->int_field(_slot_offset);
3677 }
3678
3679 void java_lang_reflect_Field::set_slot(oop reflect, int value) {
3680 reflect->int_field_put(_slot_offset, value);
3681 }
3682
3683 int java_lang_reflect_Field::modifiers(oop field) {
3684 return field->int_field(_modifiers_offset);
3685 }
3686
3687 void java_lang_reflect_Field::set_modifiers(oop field, int value) {
3688 field->int_field_put(_modifiers_offset, value);
3689 }
3690
3691 void java_lang_reflect_Field::set_trusted_final(oop field) {
3692 field->bool_field_put(_trusted_final_offset, true);
3693 }
3694
3695 void java_lang_reflect_Field::set_signature(oop field, oop value) {
3696 field->obj_field_put(_signature_offset, value);
3697 }
3698
3699 void java_lang_reflect_Field::set_annotations(oop field, oop value) {
3700 field->obj_field_put(_annotations_offset, value);
3701 }
3702
3703 oop java_lang_reflect_RecordComponent::create(InstanceKlass* holder, RecordComponent* component, TRAPS) {
3704 // Allocate java.lang.reflect.RecordComponent instance
3705 HandleMark hm(THREAD);
3706 InstanceKlass* ik = vmClasses::RecordComponent_klass();
3707 assert(ik != nullptr, "must be loaded");
3708 ik->initialize(CHECK_NULL);
3709
3710 Handle element = ik->allocate_instance_handle(CHECK_NULL);
3711
3712 Handle decl_class(THREAD, holder->java_mirror());
3975 }
3976 #endif
3977
3978 bool java_lang_ref_Reference::is_referent_field(oop obj, ptrdiff_t offset) {
3979 assert(obj != nullptr, "sanity");
3980 if (offset != _referent_offset) {
3981 return false;
3982 }
3983
3984 Klass* k = obj->klass();
3985 if (!k->is_instance_klass()) {
3986 return false;
3987 }
3988
3989 InstanceKlass* ik = InstanceKlass::cast(obj->klass());
3990 bool is_reference = ik->reference_type() != REF_NONE;
3991 assert(!is_reference || ik->is_subclass_of(vmClasses::Reference_klass()), "sanity");
3992 return is_reference;
3993 }
3994
3995 int java_lang_boxing_object::_value_offset;
3996 int java_lang_boxing_object::_long_value_offset;
3997
3998 #define BOXING_FIELDS_DO(macro) \
3999 macro(_value_offset, integerKlass, "value", int_signature, false); \
4000 macro(_long_value_offset, longKlass, "value", long_signature, false);
4001
4002 void java_lang_boxing_object::compute_offsets() {
4003 InstanceKlass* integerKlass = vmClasses::Integer_klass();
4004 InstanceKlass* longKlass = vmClasses::Long_klass();
4005 BOXING_FIELDS_DO(FIELD_COMPUTE_OFFSET);
4006 }
4007
4008 #if INCLUDE_CDS
4009 void java_lang_boxing_object::serialize_offsets(SerializeClosure* f) {
4010 BOXING_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
4011 }
4012 #endif
4013
4014 oop java_lang_boxing_object::initialize_and_allocate(BasicType type, TRAPS) {
4015 Klass* k = vmClasses::box_klass(type);
4016 if (k == nullptr) return nullptr;
4017 InstanceKlass* ik = InstanceKlass::cast(k);
4018 if (!ik->is_initialized()) {
4019 ik->initialize(CHECK_NULL);
4020 }
4021 return ik->allocate_instance(THREAD);
4022 }
4023
4024
4025 oop java_lang_boxing_object::create(BasicType type, jvalue* value, TRAPS) {
4026 oop box = initialize_and_allocate(type, CHECK_NULL);
4027 if (box == nullptr) return nullptr;
4028 switch (type) {
4029 case T_BOOLEAN:
4030 box->bool_field_put(_value_offset, value->z);
4031 break;
4032 case T_CHAR:
4033 box->char_field_put(_value_offset, value->c);
4034 break;
4035 case T_FLOAT:
4036 box->float_field_put(_value_offset, value->f);
4037 break;
4038 case T_DOUBLE:
4039 box->double_field_put(_long_value_offset, value->d);
4040 break;
4041 case T_BYTE:
4042 box->byte_field_put(_value_offset, value->b);
4043 break;
4044 case T_SHORT:
4045 box->short_field_put(_value_offset, value->s);
4046 break;
4047 case T_INT:
4048 box->int_field_put(_value_offset, value->i);
4049 break;
4050 case T_LONG:
4051 box->long_field_put(_long_value_offset, value->j);
4052 break;
4053 default:
4054 return nullptr;
4055 }
4056 return box;
4057 }
4058
4059
4060 BasicType java_lang_boxing_object::basic_type(oop box) {
4061 if (box == nullptr) return T_ILLEGAL;
4062 BasicType type = vmClasses::box_klass_type(box->klass());
4063 if (type == T_OBJECT) // 'unknown' value returned by SD::bkt
4064 return T_ILLEGAL;
4065 return type;
4066 }
4067
4068
4069 BasicType java_lang_boxing_object::get_value(oop box, jvalue* value) {
4070 BasicType type = vmClasses::box_klass_type(box->klass());
4071 switch (type) {
4072 case T_BOOLEAN:
4073 value->z = box->bool_field(_value_offset);
4074 break;
4075 case T_CHAR:
4076 value->c = box->char_field(_value_offset);
4077 break;
4078 case T_FLOAT:
4079 value->f = box->float_field(_value_offset);
4080 break;
4081 case T_DOUBLE:
4082 value->d = box->double_field(_long_value_offset);
4083 break;
4084 case T_BYTE:
4085 value->b = box->byte_field(_value_offset);
4086 break;
4087 case T_SHORT:
4088 value->s = box->short_field(_value_offset);
4089 break;
4090 case T_INT:
4091 value->i = box->int_field(_value_offset);
4092 break;
4093 case T_LONG:
4094 value->j = box->long_field(_long_value_offset);
4095 break;
4096 default:
4097 return T_ILLEGAL;
4098 } // end switch
4099 return type;
4100 }
4101
4102
4103 BasicType java_lang_boxing_object::set_value(oop box, jvalue* value) {
4104 BasicType type = vmClasses::box_klass_type(box->klass());
4105 switch (type) {
4106 case T_BOOLEAN:
4107 box->bool_field_put(_value_offset, value->z);
4108 break;
4109 case T_CHAR:
4110 box->char_field_put(_value_offset, value->c);
4111 break;
4112 case T_FLOAT:
4113 box->float_field_put(_value_offset, value->f);
4114 break;
4115 case T_DOUBLE:
4116 box->double_field_put(_long_value_offset, value->d);
4117 break;
4118 case T_BYTE:
4119 box->byte_field_put(_value_offset, value->b);
4120 break;
4121 case T_SHORT:
4122 box->short_field_put(_value_offset, value->s);
4123 break;
4124 case T_INT:
4125 box->int_field_put(_value_offset, value->i);
4126 break;
4127 case T_LONG:
4128 box->long_field_put(_long_value_offset, value->j);
4129 break;
4130 default:
4131 return T_ILLEGAL;
4132 } // end switch
4133 return type;
4134 }
4135
4136
4137 void java_lang_boxing_object::print(BasicType type, jvalue* value, outputStream* st) {
4138 switch (type) {
4139 case T_BOOLEAN: st->print("%s", value->z ? "true" : "false"); break;
4140 case T_CHAR: st->print("%d", value->c); break;
4141 case T_BYTE: st->print("%d", value->b); break;
4142 case T_SHORT: st->print("%d", value->s); break;
4143 case T_INT: st->print("%d", value->i); break;
4144 case T_LONG: st->print(JLONG_FORMAT, value->j); break;
4145 case T_FLOAT: st->print("%f", value->f); break;
4146 case T_DOUBLE: st->print("%lf", value->d); break;
4147 default: st->print("type %d?", type); break;
4148 }
4504 oop java_lang_invoke_MemberName::type(oop mname) {
4505 assert(is_instance(mname), "wrong type");
4506 return mname->obj_field(_type_offset);
4507 }
4508
4509 void java_lang_invoke_MemberName::set_type(oop mname, oop type) {
4510 assert(is_instance(mname), "wrong type");
4511 mname->obj_field_put(_type_offset, type);
4512 }
4513
4514 int java_lang_invoke_MemberName::flags(oop mname) {
4515 assert(is_instance(mname), "wrong type");
4516 return mname->int_field(_flags_offset);
4517 }
4518
4519 void java_lang_invoke_MemberName::set_flags(oop mname, int flags) {
4520 assert(is_instance(mname), "wrong type");
4521 mname->int_field_put(_flags_offset, flags);
4522 }
4523
4524
4525 // Return vmtarget from ResolvedMethodName method field through indirection
4526 Method* java_lang_invoke_MemberName::vmtarget(oop mname) {
4527 assert(is_instance(mname), "wrong type");
4528 oop method = mname->obj_field(_method_offset);
4529 return method == nullptr ? nullptr : java_lang_invoke_ResolvedMethodName::vmtarget(method);
4530 }
4531
4532 bool java_lang_invoke_MemberName::is_method(oop mname) {
4533 assert(is_instance(mname), "must be MemberName");
4534 return (flags(mname) & (MN_IS_METHOD | MN_IS_CONSTRUCTOR)) > 0;
4535 }
4536
4537 void java_lang_invoke_MemberName::set_method(oop mname, oop resolved_method) {
4538 assert(is_instance(mname), "wrong type");
4539 mname->obj_field_put(_method_offset, resolved_method);
4540 }
4541
4542 intptr_t java_lang_invoke_MemberName::vmindex(oop mname) {
4543 assert(is_instance(mname), "wrong type");
4544 return (intptr_t) mname->address_field(_vmindex_offset);
4545 }
4546
4547 void java_lang_invoke_MemberName::set_vmindex(oop mname, intptr_t index) {
4548 assert(is_instance(mname), "wrong type");
4549 mname->address_field_put(_vmindex_offset, (address) index);
4550 }
4551
4552
4553 Method* java_lang_invoke_ResolvedMethodName::vmtarget(oop resolved_method) {
4554 assert(is_instance(resolved_method), "wrong type");
5507 if (!ik->find_local_field(f_name, f_sig, &fd)) {
5508 tty->print_cr("Nonstatic field %s.%s not found", klass_name, field_name);
5509 return false;
5510 }
5511 if (fd.is_static()) {
5512 tty->print_cr("Nonstatic field %s.%s appears to be static", klass_name, field_name);
5513 return false;
5514 }
5515 if (fd.offset() == deserialized_offset ) {
5516 return true;
5517 } else {
5518 tty->print_cr("Offset of nonstatic field %s.%s is deserialized as %d but should really be %d.",
5519 klass_name, field_name, deserialized_offset, fd.offset());
5520 return false;
5521 }
5522 }
5523
5524 void JavaClasses::check_offsets() {
5525 bool valid = true;
5526
5527 #define CHECK_OFFSET(klass_name, cpp_klass_name, field_name, field_sig) \
5528 valid &= check_offset(klass_name, cpp_klass_name :: _##field_name ## _offset, #field_name, field_sig)
5529
5530 #define CHECK_LONG_OFFSET(klass_name, cpp_klass_name, field_name, field_sig) \
5531 valid &= check_offset(klass_name, cpp_klass_name :: _##long_ ## field_name ## _offset, #field_name, field_sig)
5532
5533 // Boxed primitive objects (java_lang_boxing_object)
5534
5535 CHECK_OFFSET("java/lang/Boolean", java_lang_boxing_object, value, "Z");
5536 CHECK_OFFSET("java/lang/Character", java_lang_boxing_object, value, "C");
5537 CHECK_OFFSET("java/lang/Float", java_lang_boxing_object, value, "F");
5538 CHECK_LONG_OFFSET("java/lang/Double", java_lang_boxing_object, value, "D");
5539 CHECK_OFFSET("java/lang/Byte", java_lang_boxing_object, value, "B");
5540 CHECK_OFFSET("java/lang/Short", java_lang_boxing_object, value, "S");
5541 CHECK_OFFSET("java/lang/Integer", java_lang_boxing_object, value, "I");
5542 CHECK_LONG_OFFSET("java/lang/Long", java_lang_boxing_object, value, "J");
5543
5544 if (!valid) vm_exit_during_initialization("Field offset verification failed");
5545 }
5546
5547 #endif // PRODUCT
5548
5549 int InjectedField::compute_offset() {
5550 InstanceKlass* ik = InstanceKlass::cast(klass());
5551 for (AllFieldStream fs(ik); !fs.done(); fs.next()) {
5552 if (!may_be_java && !fs.field_flags().is_injected()) {
5553 // Only look at injected fields
5554 continue;
5555 }
5556 if (fs.name() == name() && fs.signature() == signature()) {
5557 return fs.offset();
5558 }
5559 }
5560 ResourceMark rm;
5561 tty->print_cr("Invalid layout of %s at %s/%s%s", ik->external_name(), name()->as_C_string(), signature()->as_C_string(), may_be_java ? " (may_be_java)" : "");
5562 #ifndef PRODUCT
|
36 #include "classfile/moduleEntry.hpp"
37 #include "classfile/stringTable.hpp"
38 #include "classfile/symbolTable.hpp"
39 #include "classfile/systemDictionary.hpp"
40 #include "classfile/vmClasses.hpp"
41 #include "classfile/vmSymbols.hpp"
42 #include "code/debugInfo.hpp"
43 #include "code/dependencyContext.hpp"
44 #include "code/pcDesc.hpp"
45 #include "gc/shared/collectedHeap.inline.hpp"
46 #include "interpreter/interpreter.hpp"
47 #include "interpreter/linkResolver.hpp"
48 #include "jvm.h"
49 #include "logging/log.hpp"
50 #include "logging/logStream.hpp"
51 #include "memory/oopFactory.hpp"
52 #include "memory/resourceArea.hpp"
53 #include "memory/universe.hpp"
54 #include "oops/fieldInfo.hpp"
55 #include "oops/fieldStreams.inline.hpp"
56 #include "oops/flatArrayKlass.hpp"
57 #include "oops/inlineKlass.inline.hpp"
58 #include "oops/instanceKlass.inline.hpp"
59 #include "oops/instanceMirrorKlass.inline.hpp"
60 #include "oops/klass.inline.hpp"
61 #include "oops/method.inline.hpp"
62 #include "oops/objArrayKlass.hpp"
63 #include "oops/objArrayOop.inline.hpp"
64 #include "oops/oop.inline.hpp"
65 #include "oops/oopCast.inline.hpp"
66 #include "oops/recordComponent.hpp"
67 #include "oops/symbol.hpp"
68 #include "oops/typeArrayOop.inline.hpp"
69 #include "prims/jvmtiExport.hpp"
70 #include "prims/methodHandles.hpp"
71 #include "prims/resolvedMethodTable.hpp"
72 #include "runtime/continuationEntry.inline.hpp"
73 #include "runtime/continuationJavaClasses.inline.hpp"
74 #include "runtime/fieldDescriptor.inline.hpp"
75 #include "runtime/frame.inline.hpp"
76 #include "runtime/handles.inline.hpp"
77 #include "runtime/handshake.hpp"
78 #include "runtime/init.hpp"
79 #include "runtime/interfaceSupport.inline.hpp"
1070 mirror = Handle(THREAD, mirror_oop);
1071
1072 // Setup indirection from mirror->klass
1073 set_klass(mirror(), k);
1074
1075 // Set the modifiers flag.
1076 u2 computed_modifiers = k->compute_modifier_flags();
1077 set_modifiers(mirror(), computed_modifiers);
1078 // Set the raw access_flags, this is used by reflection instead of modifier flags.
1079 // The Java code for array classes gets the access flags from the element type.
1080 assert(!k->is_array_klass() || k->access_flags().as_unsigned_short() == 0, "access flags are not set for arrays");
1081 set_raw_access_flags(mirror(), k->access_flags().as_unsigned_short());
1082
1083 InstanceMirrorKlass* mk = InstanceMirrorKlass::cast(mirror->klass());
1084 assert(oop_size(mirror()) == mk->instance_size(k), "should have been set");
1085
1086 set_static_oop_field_count(mirror(), mk->compute_static_oop_field_count(mirror()));
1087
1088 // It might also have a component mirror. This mirror must already exist.
1089 if (k->is_array_klass()) {
1090 if (k->is_flatArray_klass()) {
1091 Klass* element_klass = (Klass*) FlatArrayKlass::cast(k)->element_klass();
1092 assert(element_klass->is_inline_klass(), "Must be inline type component");
1093 if (is_scratch) {
1094 comp_mirror = Handle(THREAD, HeapShared::scratch_java_mirror(element_klass));
1095 } else {
1096 comp_mirror = Handle(THREAD, element_klass->java_mirror());
1097 }
1098 } else if (k->is_typeArray_klass()) {
1099 BasicType type = TypeArrayKlass::cast(k)->element_type();
1100 if (is_scratch) {
1101 comp_mirror = Handle(THREAD, HeapShared::scratch_java_mirror(type));
1102 } else {
1103 comp_mirror = Handle(THREAD, Universe::java_mirror(type));
1104 }
1105 } else {
1106 assert(k->is_objArray_klass(), "Must be");
1107 assert(!k->is_refArray_klass() || !k->is_flatArray_klass(), "Must not have mirror");
1108 Klass* element_klass = ObjArrayKlass::cast(k)->element_klass();
1109 assert(element_klass != nullptr, "Must have an element klass");
1110 oop comp_oop = element_klass->java_mirror();
1111 if (is_scratch) {
1112 comp_mirror = Handle(THREAD, HeapShared::scratch_java_mirror(element_klass));
1113 } else {
1114 comp_mirror = Handle(THREAD, comp_oop);
1115 }
1116 }
1117 assert(comp_mirror() != nullptr, "must have a mirror");
1118
1119 // Two-way link between the array klass and its component mirror:
1120 // (array_klass) k -> mirror -> component_mirror -> array_klass -> k
1121 set_component_mirror(mirror(), comp_mirror());
1122 // See below for ordering dependencies between field array_klass in component mirror
1123 // and java_mirror in this klass.
1124 } else {
1125 assert(k->is_instance_klass(), "Must be");
1126
1127 initialize_mirror_fields(k, mirror, protection_domain, classData, THREAD);
1128 if (HAS_PENDING_EXCEPTION) {
1129 // If any of the fields throws an exception like OOM remove the klass field
1130 // from the mirror so GC doesn't follow it after the klass has been deallocated.
1131 // This mirror looks like a primitive type, which logically it is because it
1132 // it represents no class.
1133 set_klass(mirror(), nullptr);
1134 return;
1135 }
1136 }
1137 }
1138
1139 void java_lang_Class::create_mirror(Klass* k, Handle class_loader,
1140 Handle module, Handle protection_domain,
1141 Handle classData, TRAPS) {
1142 assert(k != nullptr, "Use create_basic_type_mirror for primitive types");
1143 assert(k->java_mirror() == nullptr, "should only assign mirror once");
1144 // Class_klass has to be loaded because it is used to allocate
1145 // the mirror.
1146 if (vmClasses::Class_klass_loaded()) {
1147
1148 if (k->is_refined_objArray_klass()) {
1149 Klass* super_klass = k->super();
1150 assert(super_klass != nullptr, "Must be");
1151 Handle mirror(THREAD, super_klass->java_mirror());
1152 k->set_java_mirror(mirror);
1153 return;
1154 }
1155
1156 Handle mirror;
1157 Handle comp_mirror;
1158
1159 allocate_mirror(k, /*is_scratch=*/false, protection_domain, classData, mirror, comp_mirror, CHECK);
1160
1161 // set the classLoader field in the java_lang_Class instance
1162 assert(class_loader() == k->class_loader(), "should be same");
1163 set_class_loader(mirror(), class_loader());
1164
1165 // Setup indirection from klass->mirror
1166 // after any exceptions can happen during allocations.
1167 k->set_java_mirror(mirror);
1168
1169 // Set the module field in the java_lang_Class instance. This must be done
1170 // after the mirror is set.
1171 set_mirror_module_field(THREAD, k, mirror, module);
1172
1173 if (comp_mirror() != nullptr) {
1174 // Set after k->java_mirror() is published, because compiled code running
1175 // concurrently doesn't expect a k to have a null java_mirror.
1176 release_set_array_klass(comp_mirror(), k);
1177 }
1178
1179 if (CDSConfig::is_dumping_heap()) {
1180 create_scratch_mirror(k, CHECK);
1181 }
1182 } else {
1183 assert(fixup_mirror_list() != nullptr, "fixup_mirror_list not initialized");
1184 fixup_mirror_list()->push(k);
1185 }
1186 }
1187
1188 #if INCLUDE_CDS_JAVA_HEAP
1189 // The "scratch mirror" stores the states of the mirror object that can be
1190 // decided at dump time (such as the initial values of the static fields, the
1191 // component mirror, etc). At runtime, more information is added to it by
1192 // java_lang_Class::restore_archived_mirror().
1193 //
1194 // Essentially, /*dumptime*/create_scratch_mirror() + /*runtime*/restore_archived_mirror()
1195 // produces the same result as /*runtime*/create_mirror().
1196 //
1197 // Note: we archive the "scratch mirror" instead of k->java_mirror(), because the
1198 // latter may contain dumptime-specific information that cannot be archived
1199 // (e.g., ClassLoaderData*, or static fields that are modified by Java code execution).
1200 void java_lang_Class::create_scratch_mirror(Klass* k, TRAPS) {
1201 if ((k->class_loader() != nullptr &&
1202 k->class_loader() != SystemDictionary::java_platform_loader() &&
1203 k->class_loader() != SystemDictionary::java_system_loader())) {
1204 // We only archive the mirrors of classes loaded by the built-in loaders
1205 return;
1206 }
1207
1208 Handle protection_domain, classData; // set to null. Will be reinitialized at runtime
1209 Handle mirror;
1210 Handle comp_mirror;
1211 allocate_mirror(k, /*is_scratch=*/true, protection_domain, classData, mirror, comp_mirror, CHECK);
1212
1213 if (comp_mirror() != nullptr) {
1214 release_set_array_klass(comp_mirror(), k);
1215 }
1216
1217 HeapShared::set_scratch_java_mirror(k, mirror());
1218 }
1219
1220 // Returns true if the mirror is updated, false if no archived mirror
1221 // data is present. After the archived mirror object is restored, the
1222 // shared klass' _has_raw_archived_mirror flag is cleared.
1223 bool java_lang_Class::restore_archived_mirror(Klass *k,
1224 Handle class_loader, Handle module,
1225 Handle protection_domain, TRAPS) {
1226 // Postpone restoring archived mirror until java.lang.Class is loaded. Please
1227 // see more details in vmClasses::resolve_all().
1228 if (!vmClasses::Class_klass_loaded()) {
1229 assert(fixup_mirror_list() != nullptr, "fixup_mirror_list not initialized");
1230 fixup_mirror_list()->push(k);
1231 return true;
1232 }
1233
1234 oop m = k->archived_java_mirror();
1235 assert(m != nullptr, "must have stored non-null archived mirror");
1236
1237 // Sanity: clear it now to prevent re-initialization if any of the following fails
1238 k->clear_archived_mirror_index();
1239
1240 // mirror is archived, restore
1241 log_debug(aot, mirror)("Archived mirror is: " PTR_FORMAT, p2i(m));
1242 Handle mirror(THREAD, m);
1243
1244 if (!k->is_array_klass()) {
1245 assert(as_Klass(m) == k, "must be");
1246 // - local static final fields with initial values were initialized at dump time
1247
1248 // create the init_lock
1249 typeArrayOop r = oopFactory::new_typeArray(T_INT, 0, CHECK_(false));
1250 set_init_lock(mirror(), r);
1251
1252 if (protection_domain.not_null()) {
1253 set_protection_domain(mirror(), protection_domain());
1254 }
1255 } else {
1256 ObjArrayKlass* objarray_k = (ObjArrayKlass*)as_Klass(m);
1257 // Mirror should be restored for an ObjArrayKlass or one of its refined array klasses
1258 assert(objarray_k == k || objarray_k->next_refined_array_klass() == k, "must be");
1259 }
1260
1261 assert(class_loader() == k->class_loader(), "should be same");
1262 if (class_loader.not_null()) {
1263 set_class_loader(mirror(), class_loader());
1264 }
1265
1266 k->set_java_mirror(mirror);
1267
1268 set_mirror_module_field(THREAD, k, mirror, module);
1269
1270 if (log_is_enabled(Trace, aot, heap, mirror)) {
1271 ResourceMark rm(THREAD);
1272 log_trace(aot, heap, mirror)(
1273 "Restored %s archived mirror " PTR_FORMAT, k->external_name(), p2i(mirror()));
1274 }
1275
1276 return true;
1277 }
1278 #endif // INCLUDE_CDS_JAVA_HEAP
1417 assert(is_instance(java_class), "must be a Class object");
1418 java_class->metadata_field_put(_klass_offset, klass);
1419 }
1420
1421
1422 void java_lang_Class::print_signature(oop java_class, outputStream* st) {
1423 assert(is_instance(java_class), "must be a Class object");
1424 Symbol* name = nullptr;
1425 bool is_instance = false;
1426 if (is_primitive(java_class)) {
1427 name = vmSymbols::type_signature(primitive_type(java_class));
1428 } else {
1429 Klass* k = as_Klass(java_class);
1430 is_instance = k->is_instance_klass();
1431 name = k->name();
1432 }
1433 if (name == nullptr) {
1434 st->print("<null>");
1435 return;
1436 }
1437 if (is_instance) {
1438 st->print("L");
1439 }
1440 st->write((char*) name->base(), (int) name->utf8_length());
1441 if (is_instance) st->print(";");
1442 }
1443
1444 Symbol* java_lang_Class::as_signature(oop java_class, bool intern_if_not_found) {
1445 assert(is_instance(java_class), "must be a Class object");
1446 Symbol* name;
1447 if (is_primitive(java_class)) {
1448 name = vmSymbols::type_signature(primitive_type(java_class));
1449 // Because this can create a new symbol, the caller has to decrement
1450 // the refcount, so make adjustment here and below for symbols returned
1451 // that are not created or incremented due to a successful lookup.
1452 name->increment_refcount();
1453 } else {
1454 Klass* k = as_Klass(java_class);
1455 if (!k->is_instance_klass()) {
1456 name = k->name();
1457 name->increment_refcount();
1458 } else {
1459 ResourceMark rm;
1478 if (is_primitive(java_class)) {
1479 name = type2name(primitive_type(java_class));
1480 } else {
1481 name = as_Klass(java_class)->external_name();
1482 }
1483 if (name == nullptr) {
1484 name = "<null>";
1485 }
1486 return name;
1487 }
1488
1489 Klass* java_lang_Class::array_klass_acquire(oop java_class) {
1490 Klass* k = ((Klass*)java_class->metadata_field_acquire(_array_klass_offset));
1491 assert(k == nullptr || (k->is_klass() && k->is_array_klass()), "should be array klass");
1492 return k;
1493 }
1494
1495
1496 void java_lang_Class::release_set_array_klass(oop java_class, Klass* klass) {
1497 assert(klass->is_klass() && klass->is_array_klass(), "should be array klass");
1498 assert(!klass->is_refined_objArray_klass(), "should not be ref or flat array klass");
1499 java_class->release_metadata_field_put(_array_klass_offset, klass);
1500 }
1501
1502
1503 BasicType java_lang_Class::primitive_type(oop java_class) {
1504 assert(is_primitive(java_class), "just checking");
1505 Klass* ak = ((Klass*)java_class->metadata_field(_array_klass_offset));
1506 BasicType type = T_VOID;
1507 if (ak != nullptr) {
1508 // Note: create_basic_type_mirror above initializes ak to a non-null value.
1509 type = ArrayKlass::cast(ak)->element_type();
1510 } else {
1511 assert(java_class == Universe::void_mirror(), "only valid non-array primitive");
1512 }
1513 #ifdef ASSERT
1514 if (CDSConfig::is_dumping_heap()) {
1515 oop mirror = Universe::java_mirror(type);
1516 oop scratch_mirror = HeapShared::scratch_java_mirror(type);
1517 assert(java_class == mirror || java_class == scratch_mirror, "must be consistent");
1518 } else {
2873
2874 // the format of the stacktrace will be:
2875 // - 1 or more fillInStackTrace frames for the exception class (skipped)
2876 // - 0 or more <init> methods for the exception class (skipped)
2877 // - rest of the stack
2878
2879 if (!skip_fillInStackTrace_check) {
2880 if (method->name() == vmSymbols::fillInStackTrace_name() &&
2881 throwable->is_a(method->method_holder())) {
2882 continue;
2883 }
2884 else {
2885 skip_fillInStackTrace_check = true; // gone past them all
2886 }
2887 }
2888 if (!skip_throwableInit_check) {
2889 assert(skip_fillInStackTrace_check, "logic error in backtrace filtering");
2890
2891 // skip <init> methods of the exception class and superclasses
2892 // This is similar to classic VM.
2893 if (method->is_object_constructor() &&
2894 throwable->is_a(method->method_holder())) {
2895 continue;
2896 } else {
2897 // there are none or we've seen them all - either way stop checking
2898 skip_throwableInit_check = true;
2899 }
2900 }
2901 if (method->is_hidden() || method->is_continuation_enter_intrinsic()) {
2902 if (skip_hidden) {
2903 if (total_count == 0) {
2904 // The top frame will be hidden from the stack trace.
2905 bt.set_has_hidden_top_frame();
2906 }
2907 continue;
2908 }
2909 }
2910
2911 bt.push(method, bci, CHECK);
2912 total_count++;
2913 }
3232 int java_lang_ClassFrameInfo::_classOrMemberName_offset;
3233 int java_lang_ClassFrameInfo::_flags_offset;
3234
3235 #define CLASSFRAMEINFO_FIELDS_DO(macro) \
3236 macro(_classOrMemberName_offset, k, "classOrMemberName", object_signature, false); \
3237 macro(_flags_offset, k, vmSymbols::flags_name(), int_signature, false)
3238
3239 void java_lang_ClassFrameInfo::compute_offsets() {
3240 InstanceKlass* k = vmClasses::ClassFrameInfo_klass();
3241 CLASSFRAMEINFO_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3242 }
3243
3244 #if INCLUDE_CDS
3245 void java_lang_ClassFrameInfo::serialize_offsets(SerializeClosure* f) {
3246 CLASSFRAMEINFO_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3247 }
3248 #endif
3249
3250 static int get_flags(const methodHandle& m) {
3251 int flags = m->access_flags().as_method_flags();
3252 if (m->is_object_constructor()) {
3253 flags |= java_lang_invoke_MemberName::MN_IS_OBJECT_CONSTRUCTOR;
3254 } else {
3255 // Note: Static initializers can be here. Record them as plain methods.
3256 flags |= java_lang_invoke_MemberName::MN_IS_METHOD;
3257 }
3258 if (m->caller_sensitive()) {
3259 flags |= java_lang_invoke_MemberName::MN_CALLER_SENSITIVE;
3260 }
3261 if (m->is_hidden()) {
3262 flags |= java_lang_invoke_MemberName::MN_HIDDEN_MEMBER;
3263 }
3264 assert((flags & 0xFF000000) == 0, "unexpected flags");
3265 return flags;
3266 }
3267
3268 oop java_lang_ClassFrameInfo::classOrMemberName(oop obj) {
3269 return obj->obj_field(_classOrMemberName_offset);
3270 }
3271
3272 int java_lang_ClassFrameInfo::flags(oop obj) {
3273 return obj->int_field(_flags_offset);
3624 constructor->int_field_put(_modifiers_offset, value);
3625 }
3626
3627 void java_lang_reflect_Constructor::set_signature(oop constructor, oop value) {
3628 constructor->obj_field_put(_signature_offset, value);
3629 }
3630
3631 void java_lang_reflect_Constructor::set_annotations(oop constructor, oop value) {
3632 constructor->obj_field_put(_annotations_offset, value);
3633 }
3634
3635 void java_lang_reflect_Constructor::set_parameter_annotations(oop method, oop value) {
3636 method->obj_field_put(_parameter_annotations_offset, value);
3637 }
3638
3639 int java_lang_reflect_Field::_clazz_offset;
3640 int java_lang_reflect_Field::_name_offset;
3641 int java_lang_reflect_Field::_type_offset;
3642 int java_lang_reflect_Field::_slot_offset;
3643 int java_lang_reflect_Field::_modifiers_offset;
3644 int java_lang_reflect_Field::_flags_offset;
3645 int java_lang_reflect_Field::_signature_offset;
3646 int java_lang_reflect_Field::_annotations_offset;
3647
3648 #define FIELD_FIELDS_DO(macro) \
3649 macro(_clazz_offset, k, vmSymbols::clazz_name(), class_signature, false); \
3650 macro(_name_offset, k, vmSymbols::name_name(), string_signature, false); \
3651 macro(_type_offset, k, vmSymbols::type_name(), class_signature, false); \
3652 macro(_slot_offset, k, vmSymbols::slot_name(), int_signature, false); \
3653 macro(_modifiers_offset, k, vmSymbols::modifiers_name(), int_signature, false); \
3654 macro(_flags_offset, k, vmSymbols::flags_name(), int_signature, false); \
3655 macro(_signature_offset, k, vmSymbols::signature_name(), string_signature, false); \
3656 macro(_annotations_offset, k, vmSymbols::annotations_name(), byte_array_signature, false);
3657
3658 void java_lang_reflect_Field::compute_offsets() {
3659 InstanceKlass* k = vmClasses::reflect_Field_klass();
3660 FIELD_FIELDS_DO(FIELD_COMPUTE_OFFSET);
3661 }
3662
3663 #if INCLUDE_CDS
3664 void java_lang_reflect_Field::serialize_offsets(SerializeClosure* f) {
3665 FIELD_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
3666 }
3667 #endif
3668
3669 Handle java_lang_reflect_Field::create(TRAPS) {
3670 assert(Universe::is_fully_initialized(), "Need to find another solution to the reflection problem");
3671 Symbol* name = vmSymbols::java_lang_reflect_Field();
3672 Klass* k = SystemDictionary::resolve_or_fail(name, true, CHECK_NH);
3673 InstanceKlass* ik = InstanceKlass::cast(k);
3674 // Ensure it is initialized
3699 void java_lang_reflect_Field::set_type(oop field, oop value) {
3700 field->obj_field_put(_type_offset, value);
3701 }
3702
3703 int java_lang_reflect_Field::slot(oop reflect) {
3704 return reflect->int_field(_slot_offset);
3705 }
3706
3707 void java_lang_reflect_Field::set_slot(oop reflect, int value) {
3708 reflect->int_field_put(_slot_offset, value);
3709 }
3710
3711 int java_lang_reflect_Field::modifiers(oop field) {
3712 return field->int_field(_modifiers_offset);
3713 }
3714
3715 void java_lang_reflect_Field::set_modifiers(oop field, int value) {
3716 field->int_field_put(_modifiers_offset, value);
3717 }
3718
3719 void java_lang_reflect_Field::set_flags(oop field, int value) {
3720 field->int_field_put(_flags_offset, value);
3721 }
3722
3723 void java_lang_reflect_Field::set_signature(oop field, oop value) {
3724 field->obj_field_put(_signature_offset, value);
3725 }
3726
3727 void java_lang_reflect_Field::set_annotations(oop field, oop value) {
3728 field->obj_field_put(_annotations_offset, value);
3729 }
3730
3731 oop java_lang_reflect_RecordComponent::create(InstanceKlass* holder, RecordComponent* component, TRAPS) {
3732 // Allocate java.lang.reflect.RecordComponent instance
3733 HandleMark hm(THREAD);
3734 InstanceKlass* ik = vmClasses::RecordComponent_klass();
3735 assert(ik != nullptr, "must be loaded");
3736 ik->initialize(CHECK_NULL);
3737
3738 Handle element = ik->allocate_instance_handle(CHECK_NULL);
3739
3740 Handle decl_class(THREAD, holder->java_mirror());
4003 }
4004 #endif
4005
4006 bool java_lang_ref_Reference::is_referent_field(oop obj, ptrdiff_t offset) {
4007 assert(obj != nullptr, "sanity");
4008 if (offset != _referent_offset) {
4009 return false;
4010 }
4011
4012 Klass* k = obj->klass();
4013 if (!k->is_instance_klass()) {
4014 return false;
4015 }
4016
4017 InstanceKlass* ik = InstanceKlass::cast(obj->klass());
4018 bool is_reference = ik->reference_type() != REF_NONE;
4019 assert(!is_reference || ik->is_subclass_of(vmClasses::Reference_klass()), "sanity");
4020 return is_reference;
4021 }
4022
4023 int* java_lang_boxing_object::_offsets;
4024
4025 #define BOXING_FIELDS_DO(macro) \
4026 macro(java_lang_boxing_object::_offsets[T_BOOLEAN - T_BOOLEAN], vmClasses::Boolean_klass(), "value", bool_signature, false); \
4027 macro(java_lang_boxing_object::_offsets[T_CHAR - T_BOOLEAN], vmClasses::Character_klass(), "value", char_signature, false); \
4028 macro(java_lang_boxing_object::_offsets[T_FLOAT - T_BOOLEAN], vmClasses::Float_klass(), "value", float_signature, false); \
4029 macro(java_lang_boxing_object::_offsets[T_DOUBLE - T_BOOLEAN], vmClasses::Double_klass(), "value", double_signature, false); \
4030 macro(java_lang_boxing_object::_offsets[T_BYTE - T_BOOLEAN], vmClasses::Byte_klass(), "value", byte_signature, false); \
4031 macro(java_lang_boxing_object::_offsets[T_SHORT - T_BOOLEAN], vmClasses::Short_klass(), "value", short_signature, false); \
4032 macro(java_lang_boxing_object::_offsets[T_INT - T_BOOLEAN], vmClasses::Integer_klass(), "value", int_signature, false); \
4033 macro(java_lang_boxing_object::_offsets[T_LONG - T_BOOLEAN], vmClasses::Long_klass(), "value", long_signature, false);
4034
4035 void java_lang_boxing_object::compute_offsets() {
4036 assert(T_LONG - T_BOOLEAN == 7, "Sanity check");
4037 java_lang_boxing_object::_offsets = NEW_C_HEAP_ARRAY(int, 8, mtInternal);
4038 BOXING_FIELDS_DO(FIELD_COMPUTE_OFFSET);
4039 }
4040
4041 #if INCLUDE_CDS
4042 void java_lang_boxing_object::serialize_offsets(SerializeClosure* f) {
4043 if (f->reading()) {
4044 assert(T_LONG - T_BOOLEAN == 7, "Sanity check");
4045 java_lang_boxing_object::_offsets = NEW_C_HEAP_ARRAY(int, 8, mtInternal);
4046 }
4047 BOXING_FIELDS_DO(FIELD_SERIALIZE_OFFSET);
4048 }
4049 #endif
4050
4051 oop java_lang_boxing_object::initialize_and_allocate(BasicType type, TRAPS) {
4052 Klass* k = vmClasses::box_klass(type);
4053 if (k == nullptr) return nullptr;
4054 InstanceKlass* ik = InstanceKlass::cast(k);
4055 if (!ik->is_initialized()) {
4056 ik->initialize(CHECK_NULL);
4057 }
4058 return ik->allocate_instance(THREAD);
4059 }
4060
4061
4062 oop java_lang_boxing_object::create(BasicType type, jvalue* value, TRAPS) {
4063 oop box = initialize_and_allocate(type, CHECK_NULL);
4064 if (box == nullptr) return nullptr;
4065 switch (type) {
4066 case T_BOOLEAN:
4067 box->bool_field_put(value_offset(type), value->z);
4068 break;
4069 case T_CHAR:
4070 box->char_field_put(value_offset(type), value->c);
4071 break;
4072 case T_FLOAT:
4073 box->float_field_put(value_offset(type), value->f);
4074 break;
4075 case T_DOUBLE:
4076 box->double_field_put(value_offset(type), value->d);
4077 break;
4078 case T_BYTE:
4079 box->byte_field_put(value_offset(type), value->b);
4080 break;
4081 case T_SHORT:
4082 box->short_field_put(value_offset(type), value->s);
4083 break;
4084 case T_INT:
4085 box->int_field_put(value_offset(type), value->i);
4086 break;
4087 case T_LONG:
4088 box->long_field_put(value_offset(type), value->j);
4089 break;
4090 default:
4091 return nullptr;
4092 }
4093 return box;
4094 }
4095
4096
4097 BasicType java_lang_boxing_object::basic_type(oop box) {
4098 if (box == nullptr) return T_ILLEGAL;
4099 BasicType type = vmClasses::box_klass_type(box->klass());
4100 if (type == T_OBJECT) // 'unknown' value returned by SD::bkt
4101 return T_ILLEGAL;
4102 return type;
4103 }
4104
4105
4106 BasicType java_lang_boxing_object::get_value(oop box, jvalue* value) {
4107 BasicType type = vmClasses::box_klass_type(box->klass());
4108 switch (type) {
4109 case T_BOOLEAN:
4110 value->z = box->bool_field(value_offset(type));
4111 break;
4112 case T_CHAR:
4113 value->c = box->char_field(value_offset(type));
4114 break;
4115 case T_FLOAT:
4116 value->f = box->float_field(value_offset(type));
4117 break;
4118 case T_DOUBLE:
4119 value->d = box->double_field(value_offset(type));
4120 break;
4121 case T_BYTE:
4122 value->b = box->byte_field(value_offset(type));
4123 break;
4124 case T_SHORT:
4125 value->s = box->short_field(value_offset(type));
4126 break;
4127 case T_INT:
4128 value->i = box->int_field(value_offset(type));
4129 break;
4130 case T_LONG:
4131 value->j = box->long_field(value_offset(type));
4132 break;
4133 default:
4134 return T_ILLEGAL;
4135 } // end switch
4136 return type;
4137 }
4138
4139
4140 BasicType java_lang_boxing_object::set_value(oop box, jvalue* value) {
4141 BasicType type = vmClasses::box_klass_type(box->klass());
4142 switch (type) {
4143 case T_BOOLEAN:
4144 box->bool_field_put(value_offset(type), value->z);
4145 break;
4146 case T_CHAR:
4147 box->char_field_put(value_offset(type), value->c);
4148 break;
4149 case T_FLOAT:
4150 box->float_field_put(value_offset(type), value->f);
4151 break;
4152 case T_DOUBLE:
4153 box->double_field_put(value_offset(type), value->d);
4154 break;
4155 case T_BYTE:
4156 box->byte_field_put(value_offset(type), value->b);
4157 break;
4158 case T_SHORT:
4159 box->short_field_put(value_offset(type), value->s);
4160 break;
4161 case T_INT:
4162 box->int_field_put(value_offset(type), value->i);
4163 break;
4164 case T_LONG:
4165 box->long_field_put(value_offset(type), value->j);
4166 break;
4167 default:
4168 return T_ILLEGAL;
4169 } // end switch
4170 return type;
4171 }
4172
4173
4174 void java_lang_boxing_object::print(BasicType type, jvalue* value, outputStream* st) {
4175 switch (type) {
4176 case T_BOOLEAN: st->print("%s", value->z ? "true" : "false"); break;
4177 case T_CHAR: st->print("%d", value->c); break;
4178 case T_BYTE: st->print("%d", value->b); break;
4179 case T_SHORT: st->print("%d", value->s); break;
4180 case T_INT: st->print("%d", value->i); break;
4181 case T_LONG: st->print(JLONG_FORMAT, value->j); break;
4182 case T_FLOAT: st->print("%f", value->f); break;
4183 case T_DOUBLE: st->print("%lf", value->d); break;
4184 default: st->print("type %d?", type); break;
4185 }
4541 oop java_lang_invoke_MemberName::type(oop mname) {
4542 assert(is_instance(mname), "wrong type");
4543 return mname->obj_field(_type_offset);
4544 }
4545
4546 void java_lang_invoke_MemberName::set_type(oop mname, oop type) {
4547 assert(is_instance(mname), "wrong type");
4548 mname->obj_field_put(_type_offset, type);
4549 }
4550
4551 int java_lang_invoke_MemberName::flags(oop mname) {
4552 assert(is_instance(mname), "wrong type");
4553 return mname->int_field(_flags_offset);
4554 }
4555
4556 void java_lang_invoke_MemberName::set_flags(oop mname, int flags) {
4557 assert(is_instance(mname), "wrong type");
4558 mname->int_field_put(_flags_offset, flags);
4559 }
4560
4561 // Return vmtarget from ResolvedMethodName method field through indirection
4562 Method* java_lang_invoke_MemberName::vmtarget(oop mname) {
4563 assert(is_instance(mname), "wrong type");
4564 oop method = mname->obj_field(_method_offset);
4565 return method == nullptr ? nullptr : java_lang_invoke_ResolvedMethodName::vmtarget(method);
4566 }
4567
4568 bool java_lang_invoke_MemberName::is_method(oop mname) {
4569 assert(is_instance(mname), "must be MemberName");
4570 return (flags(mname) & (MN_IS_METHOD | MN_IS_OBJECT_CONSTRUCTOR)) > 0;
4571 }
4572
4573 void java_lang_invoke_MemberName::set_method(oop mname, oop resolved_method) {
4574 assert(is_instance(mname), "wrong type");
4575 mname->obj_field_put(_method_offset, resolved_method);
4576 }
4577
4578 intptr_t java_lang_invoke_MemberName::vmindex(oop mname) {
4579 assert(is_instance(mname), "wrong type");
4580 return (intptr_t) mname->address_field(_vmindex_offset);
4581 }
4582
4583 void java_lang_invoke_MemberName::set_vmindex(oop mname, intptr_t index) {
4584 assert(is_instance(mname), "wrong type");
4585 mname->address_field_put(_vmindex_offset, (address) index);
4586 }
4587
4588
4589 Method* java_lang_invoke_ResolvedMethodName::vmtarget(oop resolved_method) {
4590 assert(is_instance(resolved_method), "wrong type");
5543 if (!ik->find_local_field(f_name, f_sig, &fd)) {
5544 tty->print_cr("Nonstatic field %s.%s not found", klass_name, field_name);
5545 return false;
5546 }
5547 if (fd.is_static()) {
5548 tty->print_cr("Nonstatic field %s.%s appears to be static", klass_name, field_name);
5549 return false;
5550 }
5551 if (fd.offset() == deserialized_offset ) {
5552 return true;
5553 } else {
5554 tty->print_cr("Offset of nonstatic field %s.%s is deserialized as %d but should really be %d.",
5555 klass_name, field_name, deserialized_offset, fd.offset());
5556 return false;
5557 }
5558 }
5559
5560 void JavaClasses::check_offsets() {
5561 bool valid = true;
5562
5563 #define CHECK_OFFSET(klass_name, type, field_sig) \
5564 valid &= check_offset(klass_name, java_lang_boxing_object::value_offset(type), "value", field_sig)
5565
5566 CHECK_OFFSET("java/lang/Boolean", T_BOOLEAN, "Z");
5567 CHECK_OFFSET("java/lang/Character", T_CHAR, "C");
5568 CHECK_OFFSET("java/lang/Float", T_FLOAT, "F");
5569 CHECK_OFFSET("java/lang/Double", T_DOUBLE, "D");
5570 CHECK_OFFSET("java/lang/Byte", T_BYTE, "B");
5571 CHECK_OFFSET("java/lang/Short", T_SHORT, "S");
5572 CHECK_OFFSET("java/lang/Integer", T_INT, "I");
5573 CHECK_OFFSET("java/lang/Long", T_LONG, "J");
5574
5575 if (!valid) vm_exit_during_initialization("Field offset verification failed");
5576 }
5577
5578 #endif // PRODUCT
5579
5580 int InjectedField::compute_offset() {
5581 InstanceKlass* ik = InstanceKlass::cast(klass());
5582 for (AllFieldStream fs(ik); !fs.done(); fs.next()) {
5583 if (!may_be_java && !fs.field_flags().is_injected()) {
5584 // Only look at injected fields
5585 continue;
5586 }
5587 if (fs.name() == name() && fs.signature() == signature()) {
5588 return fs.offset();
5589 }
5590 }
5591 ResourceMark rm;
5592 tty->print_cr("Invalid layout of %s at %s/%s%s", ik->external_name(), name()->as_C_string(), signature()->as_C_string(), may_be_java ? " (may_be_java)" : "");
5593 #ifndef PRODUCT
|