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