835 // update all the static field offsets to included the size.
836 for (JavaFieldStream fs(InstanceKlass::cast(k)); !fs.done(); fs.next()) {
837 if (fs.access_flags().is_static()) {
838 int real_offset = fs.offset() + InstanceMirrorKlass::offset_of_static_fields();
839 fs.set_offset(real_offset);
840 }
841 }
842 }
843 }
844
845 if (k->is_shared() && k->has_raw_archived_mirror()) {
846 if (HeapShared::open_archive_heap_region_mapped()) {
847 bool present = restore_archived_mirror(k, Handle(), Handle(), Handle(), CHECK);
848 assert(present, "Missing archived mirror for %s", k->external_name());
849 return;
850 } else {
851 k->set_java_mirror_handle(NULL);
852 k->clear_has_raw_archived_mirror();
853 }
854 }
855 create_mirror(k, Handle(), Handle(), Handle(), CHECK);
856 }
857
858 void java_lang_Class::initialize_mirror_fields(Klass* k,
859 Handle mirror,
860 Handle protection_domain,
861 TRAPS) {
862 // Allocate a simple java object for a lock.
863 // This needs to be a java object because during class initialization
864 // it can be held across a java call.
865 typeArrayOop r = oopFactory::new_typeArray(T_INT, 0, CHECK);
866 set_init_lock(mirror(), r);
867
868 // Set protection domain also
869 set_protection_domain(mirror(), protection_domain());
870
871 // Initialize static fields
872 InstanceKlass::cast(k)->do_local_static_fields(&initialize_static_field, mirror, CHECK);
873 }
874
875 // Set the java.lang.Module module field in the java_lang_Class mirror
876 void java_lang_Class::set_mirror_module_field(Klass* k, Handle mirror, Handle module, TRAPS) {
877 if (module.is_null()) {
878 // During startup, the module may be NULL only if java.base has not been defined yet.
879 // Put the class on the fixup_module_list to patch later when the java.lang.Module
880 // for java.base is known. But note that since we captured the NULL module another
881 // thread may have completed that initialization.
882
883 bool javabase_was_defined = false;
884 {
885 MutexLocker m1(THREAD, Module_lock);
886 // Keep list of classes needing java.base module fixup
887 if (!ModuleEntryTable::javabase_defined()) {
888 assert(k->java_mirror() != NULL, "Class's mirror is null");
889 k->class_loader_data()->inc_keep_alive();
890 assert(fixup_module_field_list() != NULL, "fixup_module_field_list not initialized");
891 fixup_module_field_list()->push(k);
892 } else {
906 assert(Universe::is_module_initialized() ||
907 (ModuleEntryTable::javabase_defined() &&
908 (module() == ModuleEntryTable::javabase_moduleEntry()->module())),
909 "Incorrect java.lang.Module specification while creating mirror");
910 set_module(mirror(), module());
911 }
912 }
913
914 // Statically allocate fixup lists because they always get created.
915 void java_lang_Class::allocate_fixup_lists() {
916 GrowableArray<Klass*>* mirror_list =
917 new (ResourceObj::C_HEAP, mtClass) GrowableArray<Klass*>(40, true);
918 set_fixup_mirror_list(mirror_list);
919
920 GrowableArray<Klass*>* module_list =
921 new (ResourceObj::C_HEAP, mtModule) GrowableArray<Klass*>(500, true);
922 set_fixup_module_field_list(module_list);
923 }
924
925 void java_lang_Class::create_mirror(Klass* k, Handle class_loader,
926 Handle module, Handle protection_domain, TRAPS) {
927 assert(k != NULL, "Use create_basic_type_mirror for primitive types");
928 assert(k->java_mirror() == NULL, "should only assign mirror once");
929
930 // Use this moment of initialization to cache modifier_flags also,
931 // to support Class.getModifiers(). Instance classes recalculate
932 // the cached flags after the class file is parsed, but before the
933 // class is put into the system dictionary.
934 int computed_modifiers = k->compute_modifier_flags(CHECK);
935 k->set_modifier_flags(computed_modifiers);
936 // Class_klass has to be loaded because it is used to allocate
937 // the mirror.
938 if (SystemDictionary::Class_klass_loaded()) {
939 // Allocate mirror (java.lang.Class instance)
940 oop mirror_oop = InstanceMirrorKlass::cast(SystemDictionary::Class_klass())->allocate_instance(k, CHECK);
941 Handle mirror(THREAD, mirror_oop);
942 Handle comp_mirror;
943
944 // Setup indirection from mirror->klass
945 java_lang_Class::set_klass(mirror(), k);
946
953 if (k->is_array_klass()) {
954 if (k->is_typeArray_klass()) {
955 BasicType type = TypeArrayKlass::cast(k)->element_type();
956 comp_mirror = Handle(THREAD, Universe::java_mirror(type));
957 } else {
958 assert(k->is_objArray_klass(), "Must be");
959 Klass* element_klass = ObjArrayKlass::cast(k)->element_klass();
960 assert(element_klass != NULL, "Must have an element klass");
961 comp_mirror = Handle(THREAD, element_klass->java_mirror());
962 }
963 assert(comp_mirror() != NULL, "must have a mirror");
964
965 // Two-way link between the array klass and its component mirror:
966 // (array_klass) k -> mirror -> component_mirror -> array_klass -> k
967 set_component_mirror(mirror(), comp_mirror());
968 // See below for ordering dependencies between field array_klass in component mirror
969 // and java_mirror in this klass.
970 } else {
971 assert(k->is_instance_klass(), "Must be");
972
973 initialize_mirror_fields(k, mirror, protection_domain, THREAD);
974 if (HAS_PENDING_EXCEPTION) {
975 // If any of the fields throws an exception like OOM remove the klass field
976 // from the mirror so GC doesn't follow it after the klass has been deallocated.
977 // This mirror looks like a primitive type, which logically it is because it
978 // it represents no class.
979 java_lang_Class::set_klass(mirror(), NULL);
980 return;
981 }
982 }
983
984 // set the classLoader field in the java_lang_Class instance
985 assert(class_loader() == k->class_loader(), "should be same");
986 set_class_loader(mirror(), class_loader());
987
988 // Setup indirection from klass->mirror
989 // after any exceptions can happen during allocations.
990 k->set_java_mirror(mirror);
991
992 // Set the module field in the java_lang_Class instance. This must be done
993 // after the mirror is set.
1380 }
1381
1382 oop java_lang_Class::init_lock(oop java_class) {
1383 assert(_init_lock_offset != 0, "must be set");
1384 return java_class->obj_field(_init_lock_offset);
1385 }
1386 void java_lang_Class::set_init_lock(oop java_class, oop init_lock) {
1387 assert(_init_lock_offset != 0, "must be set");
1388 java_class->obj_field_put(_init_lock_offset, init_lock);
1389 }
1390
1391 objArrayOop java_lang_Class::signers(oop java_class) {
1392 assert(_signers_offset != 0, "must be set");
1393 return (objArrayOop)java_class->obj_field(_signers_offset);
1394 }
1395 void java_lang_Class::set_signers(oop java_class, objArrayOop signers) {
1396 assert(_signers_offset != 0, "must be set");
1397 java_class->obj_field_put(_signers_offset, (oop)signers);
1398 }
1399
1400
1401 void java_lang_Class::set_class_loader(oop java_class, oop loader) {
1402 assert(_class_loader_offset != 0, "offsets should have been initialized");
1403 java_class->obj_field_put(_class_loader_offset, loader);
1404 }
1405
1406 oop java_lang_Class::class_loader(oop java_class) {
1407 assert(_class_loader_offset != 0, "must be set");
1408 return java_class->obj_field(_class_loader_offset);
1409 }
1410
1411 oop java_lang_Class::module(oop java_class) {
1412 assert(_module_offset != 0, "must be set");
1413 return java_class->obj_field(_module_offset);
1414 }
1415
1416 void java_lang_Class::set_module(oop java_class, oop module) {
1417 assert(_module_offset != 0, "must be set");
1418 java_class->obj_field_put(_module_offset, module);
1419 }
1583 }
1584 }
1585
1586
1587 oop java_lang_Class::primitive_mirror(BasicType t) {
1588 oop mirror = Universe::java_mirror(t);
1589 assert(mirror != NULL && mirror->is_a(SystemDictionary::Class_klass()), "must be a Class");
1590 assert(java_lang_Class::is_primitive(mirror), "must be primitive");
1591 return mirror;
1592 }
1593
1594 bool java_lang_Class::offsets_computed = false;
1595 int java_lang_Class::classRedefinedCount_offset = -1;
1596
1597 #define CLASS_FIELDS_DO(macro) \
1598 macro(classRedefinedCount_offset, k, "classRedefinedCount", int_signature, false); \
1599 macro(_class_loader_offset, k, "classLoader", classloader_signature, false); \
1600 macro(_component_mirror_offset, k, "componentType", class_signature, false); \
1601 macro(_module_offset, k, "module", module_signature, false); \
1602 macro(_name_offset, k, "name", string_signature, false); \
1603
1604 void java_lang_Class::compute_offsets() {
1605 if (offsets_computed) {
1606 return;
1607 }
1608
1609 offsets_computed = true;
1610
1611 InstanceKlass* k = SystemDictionary::Class_klass();
1612 CLASS_FIELDS_DO(FIELD_COMPUTE_OFFSET);
1613
1614 // Init lock is a C union with component_mirror. Only instanceKlass mirrors have
1615 // init_lock and only ArrayKlass mirrors have component_mirror. Since both are oops
1616 // GC treats them the same.
1617 _init_lock_offset = _component_mirror_offset;
1618
1619 CLASS_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET);
1620 }
1621
1622 #if INCLUDE_CDS
4251 }
4252 };
4253
4254 void jdk_internal_misc_UnsafeConstants::set_unsafe_constants() {
4255 UnsafeConstantsFixup fixup;
4256 SystemDictionary::UnsafeConstants_klass()->do_local_static_fields(&fixup);
4257 }
4258
4259 int java_lang_Class::_klass_offset;
4260 int java_lang_Class::_array_klass_offset;
4261 int java_lang_Class::_oop_size_offset;
4262 int java_lang_Class::_static_oop_field_count_offset;
4263 int java_lang_Class::_class_loader_offset;
4264 int java_lang_Class::_module_offset;
4265 int java_lang_Class::_protection_domain_offset;
4266 int java_lang_Class::_component_mirror_offset;
4267 int java_lang_Class::_init_lock_offset;
4268 int java_lang_Class::_signers_offset;
4269 int java_lang_Class::_name_offset;
4270 int java_lang_Class::_source_file_offset;
4271 GrowableArray<Klass*>* java_lang_Class::_fixup_mirror_list = NULL;
4272 GrowableArray<Klass*>* java_lang_Class::_fixup_module_field_list = NULL;
4273 int java_lang_Throwable::backtrace_offset;
4274 int java_lang_Throwable::detailMessage_offset;
4275 int java_lang_Throwable::stackTrace_offset;
4276 int java_lang_Throwable::depth_offset;
4277 int java_lang_Throwable::static_unassigned_stacktrace_offset;
4278 int java_lang_reflect_AccessibleObject::override_offset;
4279 int java_lang_reflect_Method::clazz_offset;
4280 int java_lang_reflect_Method::name_offset;
4281 int java_lang_reflect_Method::returnType_offset;
4282 int java_lang_reflect_Method::parameterTypes_offset;
4283 int java_lang_reflect_Method::exceptionTypes_offset;
4284 int java_lang_reflect_Method::slot_offset;
4285 int java_lang_reflect_Method::modifiers_offset;
4286 int java_lang_reflect_Method::signature_offset;
4287 int java_lang_reflect_Method::annotations_offset;
4288 int java_lang_reflect_Method::parameter_annotations_offset;
4289 int java_lang_reflect_Method::annotation_default_offset;
4290 int java_lang_reflect_Constructor::clazz_offset;
|
835 // update all the static field offsets to included the size.
836 for (JavaFieldStream fs(InstanceKlass::cast(k)); !fs.done(); fs.next()) {
837 if (fs.access_flags().is_static()) {
838 int real_offset = fs.offset() + InstanceMirrorKlass::offset_of_static_fields();
839 fs.set_offset(real_offset);
840 }
841 }
842 }
843 }
844
845 if (k->is_shared() && k->has_raw_archived_mirror()) {
846 if (HeapShared::open_archive_heap_region_mapped()) {
847 bool present = restore_archived_mirror(k, Handle(), Handle(), Handle(), CHECK);
848 assert(present, "Missing archived mirror for %s", k->external_name());
849 return;
850 } else {
851 k->set_java_mirror_handle(NULL);
852 k->clear_has_raw_archived_mirror();
853 }
854 }
855 create_mirror(k, Handle(), Handle(), Handle(), Handle(), CHECK);
856 }
857
858 void java_lang_Class::initialize_mirror_fields(Klass* k,
859 Handle mirror,
860 Handle protection_domain,
861 Handle classData,
862 TRAPS) {
863 // Allocate a simple java object for a lock.
864 // This needs to be a java object because during class initialization
865 // it can be held across a java call.
866 typeArrayOop r = oopFactory::new_typeArray(T_INT, 0, CHECK);
867 set_init_lock(mirror(), r);
868
869 // Set protection domain also
870 set_protection_domain(mirror(), protection_domain());
871
872 // Initialize static fields
873 InstanceKlass::cast(k)->do_local_static_fields(&initialize_static_field, mirror, CHECK);
874
875 // Set classData
876 set_class_data(mirror(), classData());
877 }
878
879 // Set the java.lang.Module module field in the java_lang_Class mirror
880 void java_lang_Class::set_mirror_module_field(Klass* k, Handle mirror, Handle module, TRAPS) {
881 if (module.is_null()) {
882 // During startup, the module may be NULL only if java.base has not been defined yet.
883 // Put the class on the fixup_module_list to patch later when the java.lang.Module
884 // for java.base is known. But note that since we captured the NULL module another
885 // thread may have completed that initialization.
886
887 bool javabase_was_defined = false;
888 {
889 MutexLocker m1(THREAD, Module_lock);
890 // Keep list of classes needing java.base module fixup
891 if (!ModuleEntryTable::javabase_defined()) {
892 assert(k->java_mirror() != NULL, "Class's mirror is null");
893 k->class_loader_data()->inc_keep_alive();
894 assert(fixup_module_field_list() != NULL, "fixup_module_field_list not initialized");
895 fixup_module_field_list()->push(k);
896 } else {
910 assert(Universe::is_module_initialized() ||
911 (ModuleEntryTable::javabase_defined() &&
912 (module() == ModuleEntryTable::javabase_moduleEntry()->module())),
913 "Incorrect java.lang.Module specification while creating mirror");
914 set_module(mirror(), module());
915 }
916 }
917
918 // Statically allocate fixup lists because they always get created.
919 void java_lang_Class::allocate_fixup_lists() {
920 GrowableArray<Klass*>* mirror_list =
921 new (ResourceObj::C_HEAP, mtClass) GrowableArray<Klass*>(40, true);
922 set_fixup_mirror_list(mirror_list);
923
924 GrowableArray<Klass*>* module_list =
925 new (ResourceObj::C_HEAP, mtModule) GrowableArray<Klass*>(500, true);
926 set_fixup_module_field_list(module_list);
927 }
928
929 void java_lang_Class::create_mirror(Klass* k, Handle class_loader,
930 Handle module, Handle protection_domain,
931 Handle classData, TRAPS) {
932 assert(k != NULL, "Use create_basic_type_mirror for primitive types");
933 assert(k->java_mirror() == NULL, "should only assign mirror once");
934
935 // Use this moment of initialization to cache modifier_flags also,
936 // to support Class.getModifiers(). Instance classes recalculate
937 // the cached flags after the class file is parsed, but before the
938 // class is put into the system dictionary.
939 int computed_modifiers = k->compute_modifier_flags(CHECK);
940 k->set_modifier_flags(computed_modifiers);
941 // Class_klass has to be loaded because it is used to allocate
942 // the mirror.
943 if (SystemDictionary::Class_klass_loaded()) {
944 // Allocate mirror (java.lang.Class instance)
945 oop mirror_oop = InstanceMirrorKlass::cast(SystemDictionary::Class_klass())->allocate_instance(k, CHECK);
946 Handle mirror(THREAD, mirror_oop);
947 Handle comp_mirror;
948
949 // Setup indirection from mirror->klass
950 java_lang_Class::set_klass(mirror(), k);
951
958 if (k->is_array_klass()) {
959 if (k->is_typeArray_klass()) {
960 BasicType type = TypeArrayKlass::cast(k)->element_type();
961 comp_mirror = Handle(THREAD, Universe::java_mirror(type));
962 } else {
963 assert(k->is_objArray_klass(), "Must be");
964 Klass* element_klass = ObjArrayKlass::cast(k)->element_klass();
965 assert(element_klass != NULL, "Must have an element klass");
966 comp_mirror = Handle(THREAD, element_klass->java_mirror());
967 }
968 assert(comp_mirror() != NULL, "must have a mirror");
969
970 // Two-way link between the array klass and its component mirror:
971 // (array_klass) k -> mirror -> component_mirror -> array_klass -> k
972 set_component_mirror(mirror(), comp_mirror());
973 // See below for ordering dependencies between field array_klass in component mirror
974 // and java_mirror in this klass.
975 } else {
976 assert(k->is_instance_klass(), "Must be");
977
978 initialize_mirror_fields(k, mirror, protection_domain, classData, THREAD);
979 if (HAS_PENDING_EXCEPTION) {
980 // If any of the fields throws an exception like OOM remove the klass field
981 // from the mirror so GC doesn't follow it after the klass has been deallocated.
982 // This mirror looks like a primitive type, which logically it is because it
983 // it represents no class.
984 java_lang_Class::set_klass(mirror(), NULL);
985 return;
986 }
987 }
988
989 // set the classLoader field in the java_lang_Class instance
990 assert(class_loader() == k->class_loader(), "should be same");
991 set_class_loader(mirror(), class_loader());
992
993 // Setup indirection from klass->mirror
994 // after any exceptions can happen during allocations.
995 k->set_java_mirror(mirror);
996
997 // Set the module field in the java_lang_Class instance. This must be done
998 // after the mirror is set.
1385 }
1386
1387 oop java_lang_Class::init_lock(oop java_class) {
1388 assert(_init_lock_offset != 0, "must be set");
1389 return java_class->obj_field(_init_lock_offset);
1390 }
1391 void java_lang_Class::set_init_lock(oop java_class, oop init_lock) {
1392 assert(_init_lock_offset != 0, "must be set");
1393 java_class->obj_field_put(_init_lock_offset, init_lock);
1394 }
1395
1396 objArrayOop java_lang_Class::signers(oop java_class) {
1397 assert(_signers_offset != 0, "must be set");
1398 return (objArrayOop)java_class->obj_field(_signers_offset);
1399 }
1400 void java_lang_Class::set_signers(oop java_class, objArrayOop signers) {
1401 assert(_signers_offset != 0, "must be set");
1402 java_class->obj_field_put(_signers_offset, (oop)signers);
1403 }
1404
1405 oop java_lang_Class::class_data(oop java_class) {
1406 assert(_classData_offset != 0, "must be set");
1407 return java_class->obj_field(_classData_offset);
1408 }
1409 void java_lang_Class::set_class_data(oop java_class, oop class_data) {
1410 assert(_classData_offset != 0, "must be set");
1411 java_class->obj_field_put(_classData_offset, class_data);
1412 }
1413
1414 void java_lang_Class::set_class_loader(oop java_class, oop loader) {
1415 assert(_class_loader_offset != 0, "offsets should have been initialized");
1416 java_class->obj_field_put(_class_loader_offset, loader);
1417 }
1418
1419 oop java_lang_Class::class_loader(oop java_class) {
1420 assert(_class_loader_offset != 0, "must be set");
1421 return java_class->obj_field(_class_loader_offset);
1422 }
1423
1424 oop java_lang_Class::module(oop java_class) {
1425 assert(_module_offset != 0, "must be set");
1426 return java_class->obj_field(_module_offset);
1427 }
1428
1429 void java_lang_Class::set_module(oop java_class, oop module) {
1430 assert(_module_offset != 0, "must be set");
1431 java_class->obj_field_put(_module_offset, module);
1432 }
1596 }
1597 }
1598
1599
1600 oop java_lang_Class::primitive_mirror(BasicType t) {
1601 oop mirror = Universe::java_mirror(t);
1602 assert(mirror != NULL && mirror->is_a(SystemDictionary::Class_klass()), "must be a Class");
1603 assert(java_lang_Class::is_primitive(mirror), "must be primitive");
1604 return mirror;
1605 }
1606
1607 bool java_lang_Class::offsets_computed = false;
1608 int java_lang_Class::classRedefinedCount_offset = -1;
1609
1610 #define CLASS_FIELDS_DO(macro) \
1611 macro(classRedefinedCount_offset, k, "classRedefinedCount", int_signature, false); \
1612 macro(_class_loader_offset, k, "classLoader", classloader_signature, false); \
1613 macro(_component_mirror_offset, k, "componentType", class_signature, false); \
1614 macro(_module_offset, k, "module", module_signature, false); \
1615 macro(_name_offset, k, "name", string_signature, false); \
1616 macro(_classData_offset, k, "classData", object_signature, false); \
1617
1618 void java_lang_Class::compute_offsets() {
1619 if (offsets_computed) {
1620 return;
1621 }
1622
1623 offsets_computed = true;
1624
1625 InstanceKlass* k = SystemDictionary::Class_klass();
1626 CLASS_FIELDS_DO(FIELD_COMPUTE_OFFSET);
1627
1628 // Init lock is a C union with component_mirror. Only instanceKlass mirrors have
1629 // init_lock and only ArrayKlass mirrors have component_mirror. Since both are oops
1630 // GC treats them the same.
1631 _init_lock_offset = _component_mirror_offset;
1632
1633 CLASS_INJECTED_FIELDS(INJECTED_FIELD_COMPUTE_OFFSET);
1634 }
1635
1636 #if INCLUDE_CDS
4265 }
4266 };
4267
4268 void jdk_internal_misc_UnsafeConstants::set_unsafe_constants() {
4269 UnsafeConstantsFixup fixup;
4270 SystemDictionary::UnsafeConstants_klass()->do_local_static_fields(&fixup);
4271 }
4272
4273 int java_lang_Class::_klass_offset;
4274 int java_lang_Class::_array_klass_offset;
4275 int java_lang_Class::_oop_size_offset;
4276 int java_lang_Class::_static_oop_field_count_offset;
4277 int java_lang_Class::_class_loader_offset;
4278 int java_lang_Class::_module_offset;
4279 int java_lang_Class::_protection_domain_offset;
4280 int java_lang_Class::_component_mirror_offset;
4281 int java_lang_Class::_init_lock_offset;
4282 int java_lang_Class::_signers_offset;
4283 int java_lang_Class::_name_offset;
4284 int java_lang_Class::_source_file_offset;
4285 int java_lang_Class::_classData_offset;
4286 GrowableArray<Klass*>* java_lang_Class::_fixup_mirror_list = NULL;
4287 GrowableArray<Klass*>* java_lang_Class::_fixup_module_field_list = NULL;
4288 int java_lang_Throwable::backtrace_offset;
4289 int java_lang_Throwable::detailMessage_offset;
4290 int java_lang_Throwable::stackTrace_offset;
4291 int java_lang_Throwable::depth_offset;
4292 int java_lang_Throwable::static_unassigned_stacktrace_offset;
4293 int java_lang_reflect_AccessibleObject::override_offset;
4294 int java_lang_reflect_Method::clazz_offset;
4295 int java_lang_reflect_Method::name_offset;
4296 int java_lang_reflect_Method::returnType_offset;
4297 int java_lang_reflect_Method::parameterTypes_offset;
4298 int java_lang_reflect_Method::exceptionTypes_offset;
4299 int java_lang_reflect_Method::slot_offset;
4300 int java_lang_reflect_Method::modifiers_offset;
4301 int java_lang_reflect_Method::signature_offset;
4302 int java_lang_reflect_Method::annotations_offset;
4303 int java_lang_reflect_Method::parameter_annotations_offset;
4304 int java_lang_reflect_Method::annotation_default_offset;
4305 int java_lang_reflect_Constructor::clazz_offset;
|