1 /*
2 * Copyright (c) 1997, 2025, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
49 #include "gc/shared/tlab_globals.hpp"
50 #include "logging/log.hpp"
51 #include "logging/logStream.hpp"
52 #include "memory/memoryReserver.hpp"
53 #include "memory/metadataFactory.hpp"
54 #include "memory/metaspaceClosure.hpp"
55 #include "memory/metaspaceCounters.hpp"
56 #include "memory/metaspaceUtils.hpp"
57 #include "memory/oopFactory.hpp"
58 #include "memory/resourceArea.hpp"
59 #include "memory/universe.hpp"
60 #include "oops/compressedOops.hpp"
61 #include "oops/instanceKlass.hpp"
62 #include "oops/instanceMirrorKlass.hpp"
63 #include "oops/jmethodIDTable.hpp"
64 #include "oops/klass.inline.hpp"
65 #include "oops/objArrayOop.inline.hpp"
66 #include "oops/objLayout.hpp"
67 #include "oops/oop.inline.hpp"
68 #include "oops/oopHandle.inline.hpp"
69 #include "oops/typeArrayKlass.hpp"
70 #include "prims/resolvedMethodTable.hpp"
71 #include "runtime/arguments.hpp"
72 #include "runtime/atomicAccess.hpp"
73 #include "runtime/cpuTimeCounters.hpp"
74 #include "runtime/flags/jvmFlagLimit.hpp"
75 #include "runtime/handles.inline.hpp"
76 #include "runtime/init.hpp"
77 #include "runtime/java.hpp"
78 #include "runtime/javaThread.hpp"
79 #include "runtime/jniHandles.hpp"
80 #include "runtime/threads.hpp"
81 #include "runtime/timerTrace.hpp"
82 #include "sanitizers/leak.hpp"
83 #include "services/cpuTimeUsage.hpp"
84 #include "services/memoryService.hpp"
85 #include "utilities/align.hpp"
86 #include "utilities/autoRestore.hpp"
87 #include "utilities/debug.hpp"
88 #include "utilities/formatBuffer.hpp"
96 // interacts with the RedefineClasses API.
97 class LatestMethodCache {
98 // We save the InstanceKlass* and the idnum of Method* in order to get
99 // the current Method*.
100 InstanceKlass* _klass;
101 int _method_idnum;
102
103 public:
104 LatestMethodCache() { _klass = nullptr; _method_idnum = -1; }
105
106 void init(JavaThread* current, InstanceKlass* ik, const char* method,
107 Symbol* signature, bool is_static);
108 Method* get_method();
109 };
110
111 static LatestMethodCache _finalizer_register_cache; // Finalizer.register()
112 static LatestMethodCache _loader_addClass_cache; // ClassLoader.addClass()
113 static LatestMethodCache _throw_illegal_access_error_cache; // Unsafe.throwIllegalAccessError()
114 static LatestMethodCache _throw_no_such_method_error_cache; // Unsafe.throwNoSuchMethodError()
115 static LatestMethodCache _do_stack_walk_cache; // AbstractStackWalker.doStackWalk()
116
117 // Known objects
118 TypeArrayKlass* Universe::_typeArrayKlasses[T_LONG+1] = { nullptr /*, nullptr...*/ };
119 ObjArrayKlass* Universe::_objectArrayKlass = nullptr;
120 Klass* Universe::_fillerArrayKlass = nullptr;
121 OopHandle Universe::_basic_type_mirrors[T_VOID+1];
122 #if INCLUDE_CDS_JAVA_HEAP
123 int Universe::_archived_basic_type_mirror_indices[T_VOID+1];
124 #endif
125
126 OopHandle Universe::_main_thread_group;
127 OopHandle Universe::_system_thread_group;
128 OopHandle Universe::_the_empty_class_array;
129 OopHandle Universe::_the_null_string;
130 OopHandle Universe::_the_min_jint_string;
131
132 OopHandle Universe::_the_null_sentinel;
133
134 // _out_of_memory_errors is an objArray
135 enum OutOfMemoryInstance { _oom_java_heap,
492 Handle tns = java_lang_String::create_from_str("<null_sentinel>", CHECK);
493 _the_null_sentinel = OopHandle(vm_global(), tns());
494 }
495
496 // Create a handle for reference_pending_list
497 _reference_pending_list = OopHandle(vm_global(), nullptr);
498
499 // Maybe this could be lifted up now that object array can be initialized
500 // during the bootstrapping.
501
502 // OLD
503 // Initialize _objectArrayKlass after core bootstraping to make
504 // sure the super class is set up properly for _objectArrayKlass.
505 // ---
506 // NEW
507 // Since some of the old system object arrays have been converted to
508 // ordinary object arrays, _objectArrayKlass will be loaded when
509 // SystemDictionary::initialize(CHECK); is run. See the extra check
510 // for Object_klass_is_loaded in ObjArrayKlass::allocate_objArray_klass.
511 {
512 Klass* oak = vmClasses::Object_klass()->array_klass(CHECK);
513 _objectArrayKlass = ObjArrayKlass::cast(oak);
514 }
515 // OLD
516 // Add the class to the class hierarchy manually to make sure that
517 // its vtable is initialized after core bootstrapping is completed.
518 // ---
519 // New
520 // Have already been initialized.
521 _objectArrayKlass->append_to_sibling_list();
522
523 #ifdef ASSERT
524 if (FullGCALot) {
525 // Allocate an array of dummy objects.
526 // We'd like these to be at the bottom of the old generation,
527 // so that when we free one and then collect,
528 // (almost) the whole heap moves
529 // and we find out if we actually update all the oops correctly.
530 // But we can't allocate directly in the old generation,
531 // so we allocate wherever, and hope that the first collection
532 // moves these objects to the bottom of the old generation.
533 int size = FullGCALotDummies * 2;
534
535 objArrayOop naked_array = oopFactory::new_objArray(vmClasses::Object_klass(), size, CHECK);
536 objArrayHandle dummy_array(THREAD, naked_array);
537 int i = 0;
538 while (i < size) {
539 // Allocate dummy in old generation
540 oop dummy = vmClasses::Object_klass()->allocate_instance(CHECK);
541 dummy_array->obj_at_put(i++, dummy);
636 assert_pll_ownership();
637 return _reference_pending_list.peek() != nullptr;
638 }
639
640 oop Universe::swap_reference_pending_list(oop list) {
641 assert_pll_locked(is_locked);
642 return _reference_pending_list.xchg(list);
643 }
644
645 #undef assert_pll_locked
646 #undef assert_pll_ownership
647
648 static void reinitialize_vtables() {
649 // The vtables are initialized by starting at java.lang.Object and
650 // initializing through the subclass links, so that the super
651 // classes are always initialized first.
652 for (ClassHierarchyIterator iter(vmClasses::Object_klass()); !iter.done(); iter.next()) {
653 Klass* sub = iter.klass();
654 sub->vtable().initialize_vtable();
655 }
656 }
657
658 static void reinitialize_itables() {
659
660 class ReinitTableClosure : public KlassClosure {
661 public:
662 void do_klass(Klass* k) {
663 if (k->is_instance_klass()) {
664 InstanceKlass::cast(k)->itable().initialize_itable();
665 }
666 }
667 };
668
669 MutexLocker mcld(ClassLoaderDataGraph_lock);
670 ReinitTableClosure cl;
671 ClassLoaderDataGraph::classes_do(&cl);
672 }
673
674 bool Universe::on_page_boundary(void* addr) {
675 return is_aligned(addr, os::vm_page_size());
1060 // <init> function before java_lang_Class is linked. Print error and exit.
1061 vm_exit_during_initialization(err_msg("Unable to link/verify %s.%s method",
1062 ik->name()->as_C_string(), method));
1063 }
1064
1065 _klass = ik;
1066 _method_idnum = m->method_idnum();
1067 assert(_method_idnum >= 0, "sanity check");
1068 }
1069
1070 Method* LatestMethodCache::get_method() {
1071 if (_klass == nullptr) {
1072 return nullptr;
1073 } else {
1074 Method* m = _klass->method_with_idnum(_method_idnum);
1075 assert(m != nullptr, "sanity check");
1076 return m;
1077 }
1078 }
1079
1080 Method* Universe::finalizer_register_method() { return _finalizer_register_cache.get_method(); }
1081 Method* Universe::loader_addClass_method() { return _loader_addClass_cache.get_method(); }
1082 Method* Universe::throw_illegal_access_error() { return _throw_illegal_access_error_cache.get_method(); }
1083 Method* Universe::throw_no_such_method_error() { return _throw_no_such_method_error_cache.get_method(); }
1084 Method* Universe::do_stack_walk_method() { return _do_stack_walk_cache.get_method(); }
1085
1086 void Universe::initialize_known_methods(JavaThread* current) {
1087 // Set up static method for registering finalizers
1088 _finalizer_register_cache.init(current,
1089 vmClasses::Finalizer_klass(),
1090 "register",
1091 vmSymbols::object_void_signature(), true);
1092
1093 _throw_illegal_access_error_cache.init(current,
1094 vmClasses::internal_Unsafe_klass(),
1095 "throwIllegalAccessError",
1096 vmSymbols::void_method_signature(), true);
1097
1098 _throw_no_such_method_error_cache.init(current,
1099 vmClasses::internal_Unsafe_klass(),
1100 "throwNoSuchMethodError",
1101 vmSymbols::void_method_signature(), true);
1102
1103 // Set up method for registering loaded classes in class loader vector
1104 _loader_addClass_cache.init(current,
1105 vmClasses::ClassLoader_klass(),
1106 "addClass",
1107 vmSymbols::class_void_signature(), false);
1108
1109 // Set up method for stack walking
1110 _do_stack_walk_cache.init(current,
1111 vmClasses::AbstractStackWalker_klass(),
1112 "doStackWalk",
1113 vmSymbols::doStackWalk_signature(), false);
1114 }
1115
1116 void universe2_init() {
1117 EXCEPTION_MARK;
1118 Universe::genesis(CATCH);
1119 }
1120
1121 // Set after initialization of the module runtime, call_initModuleRuntime
1122 void universe_post_module_init() {
1123 Universe::_module_initialized = true;
1124 }
1125
1126 bool universe_post_init() {
1127 assert(!is_init_completed(), "Error: initialization not yet completed!");
1128 Universe::_fully_initialized = true;
1129 EXCEPTION_MARK;
1130 if (!CDSConfig::is_using_archive()) {
1131 reinitialize_vtables();
1132 reinitialize_itables();
1133 }
|
1 /*
2 * Copyright (c) 1997, 2026, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
49 #include "gc/shared/tlab_globals.hpp"
50 #include "logging/log.hpp"
51 #include "logging/logStream.hpp"
52 #include "memory/memoryReserver.hpp"
53 #include "memory/metadataFactory.hpp"
54 #include "memory/metaspaceClosure.hpp"
55 #include "memory/metaspaceCounters.hpp"
56 #include "memory/metaspaceUtils.hpp"
57 #include "memory/oopFactory.hpp"
58 #include "memory/resourceArea.hpp"
59 #include "memory/universe.hpp"
60 #include "oops/compressedOops.hpp"
61 #include "oops/instanceKlass.hpp"
62 #include "oops/instanceMirrorKlass.hpp"
63 #include "oops/jmethodIDTable.hpp"
64 #include "oops/klass.inline.hpp"
65 #include "oops/objArrayOop.inline.hpp"
66 #include "oops/objLayout.hpp"
67 #include "oops/oop.inline.hpp"
68 #include "oops/oopHandle.inline.hpp"
69 #include "oops/refArrayKlass.hpp"
70 #include "oops/typeArrayKlass.hpp"
71 #include "prims/resolvedMethodTable.hpp"
72 #include "runtime/arguments.hpp"
73 #include "runtime/atomicAccess.hpp"
74 #include "runtime/cpuTimeCounters.hpp"
75 #include "runtime/flags/jvmFlagLimit.hpp"
76 #include "runtime/handles.inline.hpp"
77 #include "runtime/init.hpp"
78 #include "runtime/java.hpp"
79 #include "runtime/javaThread.hpp"
80 #include "runtime/jniHandles.hpp"
81 #include "runtime/threads.hpp"
82 #include "runtime/timerTrace.hpp"
83 #include "sanitizers/leak.hpp"
84 #include "services/cpuTimeUsage.hpp"
85 #include "services/memoryService.hpp"
86 #include "utilities/align.hpp"
87 #include "utilities/autoRestore.hpp"
88 #include "utilities/debug.hpp"
89 #include "utilities/formatBuffer.hpp"
97 // interacts with the RedefineClasses API.
98 class LatestMethodCache {
99 // We save the InstanceKlass* and the idnum of Method* in order to get
100 // the current Method*.
101 InstanceKlass* _klass;
102 int _method_idnum;
103
104 public:
105 LatestMethodCache() { _klass = nullptr; _method_idnum = -1; }
106
107 void init(JavaThread* current, InstanceKlass* ik, const char* method,
108 Symbol* signature, bool is_static);
109 Method* get_method();
110 };
111
112 static LatestMethodCache _finalizer_register_cache; // Finalizer.register()
113 static LatestMethodCache _loader_addClass_cache; // ClassLoader.addClass()
114 static LatestMethodCache _throw_illegal_access_error_cache; // Unsafe.throwIllegalAccessError()
115 static LatestMethodCache _throw_no_such_method_error_cache; // Unsafe.throwNoSuchMethodError()
116 static LatestMethodCache _do_stack_walk_cache; // AbstractStackWalker.doStackWalk()
117 static LatestMethodCache _is_substitutable_cache; // ValueObjectMethods.isSubstitutable()
118 static LatestMethodCache _value_object_hash_code_cache; // ValueObjectMethods.valueObjectHashCode()
119
120 // Known objects
121 TypeArrayKlass* Universe::_typeArrayKlasses[T_LONG+1] = { nullptr /*, nullptr...*/ };
122 ObjArrayKlass* Universe::_objectArrayKlass = nullptr;
123 Klass* Universe::_fillerArrayKlass = nullptr;
124 OopHandle Universe::_basic_type_mirrors[T_VOID+1];
125 #if INCLUDE_CDS_JAVA_HEAP
126 int Universe::_archived_basic_type_mirror_indices[T_VOID+1];
127 #endif
128
129 OopHandle Universe::_main_thread_group;
130 OopHandle Universe::_system_thread_group;
131 OopHandle Universe::_the_empty_class_array;
132 OopHandle Universe::_the_null_string;
133 OopHandle Universe::_the_min_jint_string;
134
135 OopHandle Universe::_the_null_sentinel;
136
137 // _out_of_memory_errors is an objArray
138 enum OutOfMemoryInstance { _oom_java_heap,
495 Handle tns = java_lang_String::create_from_str("<null_sentinel>", CHECK);
496 _the_null_sentinel = OopHandle(vm_global(), tns());
497 }
498
499 // Create a handle for reference_pending_list
500 _reference_pending_list = OopHandle(vm_global(), nullptr);
501
502 // Maybe this could be lifted up now that object array can be initialized
503 // during the bootstrapping.
504
505 // OLD
506 // Initialize _objectArrayKlass after core bootstraping to make
507 // sure the super class is set up properly for _objectArrayKlass.
508 // ---
509 // NEW
510 // Since some of the old system object arrays have been converted to
511 // ordinary object arrays, _objectArrayKlass will be loaded when
512 // SystemDictionary::initialize(CHECK); is run. See the extra check
513 // for Object_klass_is_loaded in ObjArrayKlass::allocate_objArray_klass.
514 {
515 ArrayKlass* oak = vmClasses::Object_klass()->array_klass(CHECK);
516 oak->append_to_sibling_list();
517
518 // Create a RefArrayKlass (which is the default) and initialize.
519 ObjArrayKlass* rak = ObjArrayKlass::cast(oak)->klass_with_properties(ArrayKlass::ArrayProperties::DEFAULT, THREAD);
520 _objectArrayKlass = rak;
521 }
522
523 #ifdef ASSERT
524 if (FullGCALot) {
525 // Allocate an array of dummy objects.
526 // We'd like these to be at the bottom of the old generation,
527 // so that when we free one and then collect,
528 // (almost) the whole heap moves
529 // and we find out if we actually update all the oops correctly.
530 // But we can't allocate directly in the old generation,
531 // so we allocate wherever, and hope that the first collection
532 // moves these objects to the bottom of the old generation.
533 int size = FullGCALotDummies * 2;
534
535 objArrayOop naked_array = oopFactory::new_objArray(vmClasses::Object_klass(), size, CHECK);
536 objArrayHandle dummy_array(THREAD, naked_array);
537 int i = 0;
538 while (i < size) {
539 // Allocate dummy in old generation
540 oop dummy = vmClasses::Object_klass()->allocate_instance(CHECK);
541 dummy_array->obj_at_put(i++, dummy);
636 assert_pll_ownership();
637 return _reference_pending_list.peek() != nullptr;
638 }
639
640 oop Universe::swap_reference_pending_list(oop list) {
641 assert_pll_locked(is_locked);
642 return _reference_pending_list.xchg(list);
643 }
644
645 #undef assert_pll_locked
646 #undef assert_pll_ownership
647
648 static void reinitialize_vtables() {
649 // The vtables are initialized by starting at java.lang.Object and
650 // initializing through the subclass links, so that the super
651 // classes are always initialized first.
652 for (ClassHierarchyIterator iter(vmClasses::Object_klass()); !iter.done(); iter.next()) {
653 Klass* sub = iter.klass();
654 sub->vtable().initialize_vtable();
655 }
656
657 // This isn't added to the subclass list, so need to reinitialize vtables directly.
658 Universe::objectArrayKlass()->vtable().initialize_vtable();
659 }
660
661 static void reinitialize_itables() {
662
663 class ReinitTableClosure : public KlassClosure {
664 public:
665 void do_klass(Klass* k) {
666 if (k->is_instance_klass()) {
667 InstanceKlass::cast(k)->itable().initialize_itable();
668 }
669 }
670 };
671
672 MutexLocker mcld(ClassLoaderDataGraph_lock);
673 ReinitTableClosure cl;
674 ClassLoaderDataGraph::classes_do(&cl);
675 }
676
677 bool Universe::on_page_boundary(void* addr) {
678 return is_aligned(addr, os::vm_page_size());
1063 // <init> function before java_lang_Class is linked. Print error and exit.
1064 vm_exit_during_initialization(err_msg("Unable to link/verify %s.%s method",
1065 ik->name()->as_C_string(), method));
1066 }
1067
1068 _klass = ik;
1069 _method_idnum = m->method_idnum();
1070 assert(_method_idnum >= 0, "sanity check");
1071 }
1072
1073 Method* LatestMethodCache::get_method() {
1074 if (_klass == nullptr) {
1075 return nullptr;
1076 } else {
1077 Method* m = _klass->method_with_idnum(_method_idnum);
1078 assert(m != nullptr, "sanity check");
1079 return m;
1080 }
1081 }
1082
1083 Method* Universe::finalizer_register_method() { return _finalizer_register_cache.get_method(); }
1084 Method* Universe::loader_addClass_method() { return _loader_addClass_cache.get_method(); }
1085 Method* Universe::throw_illegal_access_error() { return _throw_illegal_access_error_cache.get_method(); }
1086 Method* Universe::throw_no_such_method_error() { return _throw_no_such_method_error_cache.get_method(); }
1087 Method* Universe::do_stack_walk_method() { return _do_stack_walk_cache.get_method(); }
1088 Method* Universe::is_substitutable_method() { return _is_substitutable_cache.get_method(); }
1089 Method* Universe::value_object_hash_code_method() { return _value_object_hash_code_cache.get_method(); }
1090
1091 void Universe::initialize_known_methods(JavaThread* current) {
1092 // Set up static method for registering finalizers
1093 _finalizer_register_cache.init(current,
1094 vmClasses::Finalizer_klass(),
1095 "register",
1096 vmSymbols::object_void_signature(), true);
1097
1098 _throw_illegal_access_error_cache.init(current,
1099 vmClasses::internal_Unsafe_klass(),
1100 "throwIllegalAccessError",
1101 vmSymbols::void_method_signature(), true);
1102
1103 _throw_no_such_method_error_cache.init(current,
1104 vmClasses::internal_Unsafe_klass(),
1105 "throwNoSuchMethodError",
1106 vmSymbols::void_method_signature(), true);
1107
1108 // Set up method for registering loaded classes in class loader vector
1109 _loader_addClass_cache.init(current,
1110 vmClasses::ClassLoader_klass(),
1111 "addClass",
1112 vmSymbols::class_void_signature(), false);
1113
1114 // Set up method for stack walking
1115 _do_stack_walk_cache.init(current,
1116 vmClasses::AbstractStackWalker_klass(),
1117 "doStackWalk",
1118 vmSymbols::doStackWalk_signature(), false);
1119
1120 // Set up substitutability testing
1121 ResourceMark rm(current);
1122 _is_substitutable_cache.init(current,
1123 vmClasses::ValueObjectMethods_klass(),
1124 vmSymbols::isSubstitutable_name()->as_C_string(),
1125 vmSymbols::object_object_boolean_signature(), true);
1126 _value_object_hash_code_cache.init(current,
1127 vmClasses::ValueObjectMethods_klass(),
1128 vmSymbols::valueObjectHashCode_name()->as_C_string(),
1129 vmSymbols::object_int_signature(), true);
1130 }
1131
1132 void universe2_init() {
1133 EXCEPTION_MARK;
1134 Universe::genesis(CATCH);
1135 }
1136
1137 // Set after initialization of the module runtime, call_initModuleRuntime
1138 void universe_post_module_init() {
1139 Universe::_module_initialized = true;
1140 }
1141
1142 bool universe_post_init() {
1143 assert(!is_init_completed(), "Error: initialization not yet completed!");
1144 Universe::_fully_initialized = true;
1145 EXCEPTION_MARK;
1146 if (!CDSConfig::is_using_archive()) {
1147 reinitialize_vtables();
1148 reinitialize_itables();
1149 }
|