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