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