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 *
23 */
24
25 #ifndef SHARE_MEMORY_UNIVERSE_HPP
26 #define SHARE_MEMORY_UNIVERSE_HPP
27
28 #include "gc/shared/verifyOption.hpp"
29 #include "memory/reservedSpace.hpp"
30 #include "oops/array.hpp"
31 #include "oops/oopHandle.hpp"
32 #include "runtime/handles.hpp"
33 #include "utilities/growableArray.hpp"
34
35 // Universe is a name space holding known system classes and objects in the VM.
36 //
37 // Loaded classes are accessible through the SystemDictionary.
38 //
39 // The object heap is allocated and accessed through Universe, and various allocation
40 // support is provided. Allocation by the interpreter and compiled code is done inline
41 // and bails out to Scavenge::invoke_and_allocate.
42
43 class CollectedHeap;
44 class DeferredObjAllocEvent;
45 class OopStorage;
46 class SerializeClosure;
47
48 class Universe: AllStatic {
49 // Ugh. Universe is much too friendly.
50 friend class SerialFullGC;
51 friend class oopDesc;
52 friend class ClassLoader;
53 friend class SystemDictionary;
54 friend class VMStructs;
55 friend class VM_PopulateDumpSharedSpace;
56 friend class Metaspace;
57 friend class AOTMetaspace;
58 friend class vmClasses;
59
60 friend jint universe_init();
61 friend void universe2_init();
62 friend bool universe_post_init();
63 friend void universe_post_module_init();
64
65 private:
66 // Known classes in the VM
67 static TypeArrayKlass* _typeArrayKlasses[T_LONG+1];
68 static ObjArrayKlass* _objectArrayKlass;
69 // Special int-Array that represents filler objects that are used by GC to overwrite
70 // dead objects. References to them are generally an error.
71 static Klass* _fillerArrayKlass;
72
73 // Known objects in the VM
74 static OopHandle _main_thread_group; // Reference to the main thread group object
75 static OopHandle _system_thread_group; // Reference to the system thread group object
76
77 static OopHandle _the_empty_class_array; // Canonicalized obj array of type java.lang.Class
78 static OopHandle _the_null_string; // A cache of "null" as a Java string
79 static OopHandle _the_min_jint_string; // A cache of "-2147483648" as a Java string
80
81 static OopHandle _the_null_sentinel; // A unique object pointer unused except as a sentinel for null.
82
83 // preallocated error objects (no backtrace)
84 static OopHandle _out_of_memory_errors;
85 static OopHandle _class_init_stack_overflow_error;
86
87 // preallocated cause message for delayed StackOverflowError
88 static OopHandle _delayed_stack_overflow_error_message;
89
90 static Array<int>* _the_empty_int_array; // Canonicalized int array
91 static Array<u2>* _the_empty_short_array; // Canonicalized short array
92 static Array<Klass*>* _the_empty_klass_array; // Canonicalized klass array
93 static Array<InstanceKlass*>* _the_empty_instance_klass_array; // Canonicalized instance klass array
94 static Array<Method*>* _the_empty_method_array; // Canonicalized method array
95
96 static Array<Klass*>* _the_array_interfaces_array;
97
98 static uintx _the_array_interfaces_bitmap;
99 static uintx _the_empty_klass_bitmap;
100
101 // array of preallocated error objects with backtrace
102 static OopHandle _preallocated_out_of_memory_error_array;
103
104 // number of preallocated error objects available for use
105 static volatile jint _preallocated_out_of_memory_error_avail_count;
106
107 // preallocated message detail strings for error objects
108 static OopHandle _msg_metaspace;
109 static OopHandle _msg_class_metaspace;
110
111 // References waiting to be transferred to the ReferenceHandler
112 static OopHandle _reference_pending_list;
113
114 // The particular choice of collected heap.
115 static CollectedHeap* _collectedHeap;
116
117 static intptr_t _non_oop_bits;
118
119 // array of dummy objects used with +FullGCAlot
120 DEBUG_ONLY(static OopHandle _fullgc_alot_dummy_array;)
121 DEBUG_ONLY(static int _fullgc_alot_dummy_next;)
122
123 // Compiler/dispatch support
124 static int _base_vtable_size; // Java vtbl size of klass Object (in words)
125
126 // Initialization
127 static bool _bootstrapping; // true during genesis
128 static bool _module_initialized; // true after call_initPhase2 called
129 static bool _fully_initialized; // true after universe_init and initialize_vtables called
130
131 // Shutdown
132 static volatile bool _is_shutting_down;
133
134 // the array of preallocated errors with backtraces
135 static objArrayOop preallocated_out_of_memory_errors();
136
137 static objArrayOop out_of_memory_errors();
138 // generate an out of memory error; if possible using an error with preallocated backtrace;
139 // otherwise return the given default error.
140 static oop gen_out_of_memory_error(oop default_err);
141
142 static OopStorage* _vm_weak;
143 static OopStorage* _vm_global;
144
145 static jint initialize_heap();
146 static void initialize_tlab();
147 static void initialize_basic_type_mirrors(TRAPS);
148 static void fixup_mirrors(TRAPS);
149
150 static void compute_base_vtable_size(); // compute vtable size of class Object
151
152 static void genesis(TRAPS); // Create the initial world
153
154 // Mirrors for primitive classes (created eagerly)
155 static oop check_mirror(oop m) {
156 assert(m != nullptr, "mirror not initialized");
157 return m;
158 }
159
160 // Debugging
161 static int _verify_count; // number of verifies done
162 static long verify_flags;
163
164 static uintptr_t _verify_oop_mask;
165 static uintptr_t _verify_oop_bits;
166
167 // Table of primitive type mirrors, excluding T_OBJECT and T_ARRAY
168 // but including T_VOID, hence the index including T_VOID
169 static OopHandle _basic_type_mirrors[T_VOID+1];
170
171 #if INCLUDE_CDS_JAVA_HEAP
172 // Each slot i stores an index that can be used to restore _basic_type_mirrors[i]
173 // from the archive heap using HeapShared::get_root(int)
174 static int _archived_basic_type_mirror_indices[T_VOID+1];
175 #endif
176
177 public:
178 static void calculate_verify_data(HeapWord* low_boundary, HeapWord* high_boundary) PRODUCT_RETURN;
179 static void set_verify_data(uintptr_t mask, uintptr_t bits) PRODUCT_RETURN;
180
181 // Known classes in the VM
182 static TypeArrayKlass* boolArrayKlass() { return typeArrayKlass(T_BOOLEAN); }
183 static TypeArrayKlass* byteArrayKlass() { return typeArrayKlass(T_BYTE); }
184 static TypeArrayKlass* charArrayKlass() { return typeArrayKlass(T_CHAR); }
185 static TypeArrayKlass* intArrayKlass() { return typeArrayKlass(T_INT); }
186 static TypeArrayKlass* shortArrayKlass() { return typeArrayKlass(T_SHORT); }
187 static TypeArrayKlass* longArrayKlass() { return typeArrayKlass(T_LONG); }
188 static TypeArrayKlass* floatArrayKlass() { return typeArrayKlass(T_FLOAT); }
189 static TypeArrayKlass* doubleArrayKlass() { return typeArrayKlass(T_DOUBLE); }
190
191 static ObjArrayKlass* objectArrayKlass() {
192 ObjArrayKlass* k = _objectArrayKlass;
193 assert(k != nullptr, "Object array klass should be initialized; too early?");
194 return k;
195 }
196
197 static Klass* fillerArrayKlass() {
198 Klass* k = _fillerArrayKlass;
199 assert(k != nullptr, "Filler array class should be initialized; too early?");
200 return k;
201 }
202
203 static TypeArrayKlass* typeArrayKlass(BasicType t) {
204 assert((uint)t >= T_BOOLEAN, "range check for type: %s", type2name(t));
205 assert((uint)t < T_LONG+1, "range check for type: %s", type2name(t));
206 TypeArrayKlass* k = _typeArrayKlasses[t];
207 assert(k != nullptr, "Type array class should be initialized; too early?");
208 return k;
209 }
210
211 // Known objects in the VM
212 static oop int_mirror();
213 static oop float_mirror();
214 static oop double_mirror();
215 static oop byte_mirror();
216 static oop bool_mirror();
217 static oop char_mirror();
218 static oop long_mirror();
219 static oop short_mirror();
220 static oop void_mirror();
221
222 static oop java_mirror(BasicType t);
223
224 static void load_archived_object_instances() NOT_CDS_JAVA_HEAP_RETURN;
225 #if INCLUDE_CDS_JAVA_HEAP
226 static void set_archived_basic_type_mirror_index(BasicType t, int index);
227 static void archive_exception_instances();
228 #endif
229
230 static oop main_thread_group();
231 static void set_main_thread_group(oop group);
232
233 static oop system_thread_group();
234 static void set_system_thread_group(oop group);
235
236 static objArrayOop the_empty_class_array ();
237
238 static oop the_null_string();
239 static oop the_min_jint_string();
240
241 static oop null_ptr_exception_instance();
242 static oop arithmetic_exception_instance();
243 static oop internal_error_instance();
244 static oop array_index_out_of_bounds_exception_instance();
245 static oop array_store_exception_instance();
246 static oop class_cast_exception_instance();
247 static oop vm_exception() { return internal_error_instance(); }
248
249 static Array<Klass*>* the_array_interfaces_array() { return _the_array_interfaces_array; }
250 static uintx the_array_interfaces_bitmap() { return _the_array_interfaces_bitmap; }
251
252 static Method* finalizer_register_method();
253 static Method* loader_addClass_method();
254 static Method* throw_illegal_access_error();
255 static Method* throw_no_such_method_error();
256 static Method* do_stack_walk_method();
257
258 static oop the_null_sentinel();
259 static address the_null_sentinel_addr() { return (address) &_the_null_sentinel; }
260
261 // Function to initialize these
262 static void initialize_known_methods(JavaThread* current);
263
264 static void create_preallocated_out_of_memory_errors(TRAPS);
265
266 // Reference pending list manipulation. Access is protected by
267 // Heap_lock. The getter, setter and predicate require the caller
268 // owns the lock. Swap is used by parallel non-concurrent reference
269 // processing threads, where some higher level controller owns
270 // Heap_lock, so requires the lock is locked, but not necessarily by
271 // the current thread.
272 static oop reference_pending_list();
273 static void clear_reference_pending_list();
274 static bool has_reference_pending_list();
275 static oop swap_reference_pending_list(oop list);
276
277 static Array<int>* the_empty_int_array() { return _the_empty_int_array; }
278 static Array<u2>* the_empty_short_array() { return _the_empty_short_array; }
279 static Array<Method*>* the_empty_method_array() { return _the_empty_method_array; }
280 static Array<Klass*>* the_empty_klass_array() { return _the_empty_klass_array; }
281 static Array<InstanceKlass*>* the_empty_instance_klass_array() { return _the_empty_instance_klass_array; }
282
283 static uintx the_empty_klass_bitmap() { return _the_empty_klass_bitmap; }
284
285 // OutOfMemoryError support. Returns an error with the required message. The returned error
286 // may or may not have a backtrace. If error has a backtrace then the stack trace is already
287 // filled in.
288 static oop out_of_memory_error_java_heap();
289 static oop out_of_memory_error_java_heap_without_backtrace();
290 static oop out_of_memory_error_c_heap();
291 static oop out_of_memory_error_metaspace();
292 static oop out_of_memory_error_class_metaspace();
293 static oop out_of_memory_error_array_size();
294 static oop out_of_memory_error_gc_overhead_limit();
295 static oop out_of_memory_error_realloc_objects();
296
297 static oop delayed_stack_overflow_error_message();
298
299 // Saved StackOverflowError and OutOfMemoryError for use when
300 // class initialization can't create ExceptionInInitializerError.
301 static oop class_init_stack_overflow_error();
302 static oop class_init_out_of_memory_error();
303
304 // If it's a certain type of OOME object
305 static bool is_out_of_memory_error_metaspace(oop ex_obj);
306 static bool is_out_of_memory_error_class_metaspace(oop ex_obj);
307
308 // The particular choice of collected heap.
309 static CollectedHeap* heap() { return _collectedHeap; }
310
311 static void before_exit();
312
313 DEBUG_ONLY(static bool is_stw_gc_active();)
314 DEBUG_ONLY(static bool is_in_heap(const void* p);)
315 DEBUG_ONLY(static bool is_in_heap_or_null(const void* p) { return p == nullptr || is_in_heap(p); })
316
317 // Reserve Java heap and determine CompressedOops mode
318 static ReservedHeapSpace reserve_heap(size_t heap_size, size_t alignment, size_t desired_page_size = 0);
319
320 // Global OopStorages
321 static OopStorage* vm_weak();
322 static OopStorage* vm_global();
323 static void oopstorage_init();
324
325 // Testers
326 static bool is_bootstrapping() { return _bootstrapping; }
327 static bool is_module_initialized() { return _module_initialized; }
328 static bool is_fully_initialized() { return _fully_initialized; }
329
330 static bool is_shutting_down() { return AtomicAccess::load_acquire(&_is_shutting_down); }
331
332 static bool on_page_boundary(void* addr);
333 static bool should_fill_in_stack_trace(Handle throwable);
334 static void check_alignment(uintx size, uintx alignment, const char* name);
335
336 // CDS support
337 static void serialize(SerializeClosure* f);
338
339 // Apply the closure to all klasses for basic types (classes not present in
340 // SystemDictionary).
341 static void basic_type_classes_do(KlassClosure* closure);
342 static void metaspace_pointers_do(MetaspaceClosure* it);
343
344 // Debugging
345 enum VERIFY_FLAGS {
346 Verify_Threads = 1,
347 Verify_Heap = 2,
348 Verify_SymbolTable = 4,
349 Verify_StringTable = 8,
350 Verify_CodeCache = 16,
351 Verify_SystemDictionary = 32,
352 Verify_ClassLoaderDataGraph = 64,
353 Verify_MetaspaceUtils = 128,
354 Verify_JNIHandles = 256,
355 Verify_CodeCacheOops = 512,
356 Verify_ResolvedMethodTable = 1024,
357 Verify_StringDedup = 2048,
358 Verify_All = -1
359 };
360 static void initialize_verify_flags();
361 static bool should_verify_subset(uint subset);
362 static void verify(VerifyOption option, const char* prefix);
363 static void verify(const char* prefix) {
364 verify(VerifyOption::Default, prefix);
365 }
366 static void verify() {
367 verify("");
368 }
369
370 static int verify_count() { return _verify_count; }
371 static void print_on(outputStream* st);
372 static void print_heap_at_SIGBREAK();
373
374 // Change the number of dummy objects kept reachable by the full gc dummy
375 // array; this should trigger relocation in a sliding compaction collector.
376 DEBUG_ONLY(static bool release_fullgc_alot_dummy();)
377 // The non-oop pattern (see compiledIC.hpp, etc)
378 static void* non_oop_word();
379 static bool contains_non_oop_word(void* p);
380
381 // Oop verification (see MacroAssembler::verify_oop)
382 static uintptr_t verify_oop_mask() PRODUCT_RETURN0;
383 static uintptr_t verify_oop_bits() PRODUCT_RETURN0;
384 static uintptr_t verify_mark_bits() PRODUCT_RETURN0;
385 static uintptr_t verify_mark_mask() PRODUCT_RETURN0;
386
387 // Compiler support
388 static int base_vtable_size() { return _base_vtable_size; }
389 };
390
391 #endif // SHARE_MEMORY_UNIVERSE_HPP