1 /*
  2  * Copyright (c) 1997, 2021, 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 
 47 // A helper class for caching a Method* when the user of the cache
 48 // only cares about the latest version of the Method*.  This cache safely
 49 // interacts with the RedefineClasses API.
 50 
 51 class LatestMethodCache : public CHeapObj<mtClass> {
 52   // We save the Klass* and the idnum of Method* in order to get
 53   // the current cached Method*.
 54  private:
 55   Klass*                _klass;
 56   int                   _method_idnum;
 57 
 58  public:
 59   LatestMethodCache()   { _klass = NULL; _method_idnum = -1; }
 60   ~LatestMethodCache()  { _klass = NULL; _method_idnum = -1; }
 61 
 62   void   init(Klass* k, Method* m);
 63   Klass* klass() const           { return _klass; }
 64   int    method_idnum() const    { return _method_idnum; }
 65 
 66   Method* get_method();
 67 
 68   // CDS support.  Replace the klass in this with the archive version
 69   // could use this for Enhanced Class Redefinition also.
 70   void serialize(SerializeClosure* f) {
 71     f->do_ptr((void**)&_klass);
 72   }
 73   void metaspace_pointers_do(MetaspaceClosure* it);
 74 };
 75 
 76 class Universe: AllStatic {
 77   // Ugh.  Universe is much too friendly.
 78   friend class MarkSweep;
 79   friend class oopDesc;
 80   friend class ClassLoader;
 81   friend class SystemDictionary;
 82   friend class ReservedHeapSpace;
 83   friend class VMStructs;
 84   friend class VM_PopulateDumpSharedSpace;
 85   friend class Metaspace;
 86   friend class MetaspaceShared;
 87   friend class vmClasses;
 88 
 89   friend jint  universe_init();
 90   friend void  universe2_init();
 91   friend bool  universe_post_init();
 92   friend void  universe_post_module_init();
 93 
 94  private:
 95   // Known classes in the VM
 96   static Klass* _typeArrayKlassObjs[T_LONG+1];
 97   static Klass* _objectArrayKlassObj;
 98 
 99   // Known objects in the VM
100   static OopHandle    _main_thread_group;             // Reference to the main thread group object
101   static OopHandle    _system_thread_group;           // Reference to the system thread group object
102 
103   static OopHandle    _the_empty_class_array;         // Canonicalized obj array of type java.lang.Class
104   static OopHandle    _the_null_string;               // A cache of "null" as a Java string
105   static OopHandle    _the_min_jint_string;           // A cache of "-2147483648" as a Java string
106 
107   static OopHandle    _the_null_sentinel;             // A unique object pointer unused except as a sentinel for null.
108 
109   // preallocated error objects (no backtrace)
110   static OopHandle    _out_of_memory_errors;
111 
112   // preallocated cause message for delayed StackOverflowError
113   static OopHandle    _delayed_stack_overflow_error_message;
114 
115   static LatestMethodCache* _finalizer_register_cache; // static method for registering finalizable objects
116   static LatestMethodCache* _loader_addClass_cache;    // method for registering loaded classes in class loader vector
117   static LatestMethodCache* _throw_illegal_access_error_cache; // Unsafe.throwIllegalAccessError() method
118   static LatestMethodCache* _throw_no_such_method_error_cache; // Unsafe.throwNoSuchMethodError() method
119   static LatestMethodCache* _do_stack_walk_cache;      // method for stack walker callback
120 
121   static Array<int>*            _the_empty_int_array;            // Canonicalized int array
122   static Array<u2>*             _the_empty_short_array;          // Canonicalized short array
123   static Array<Klass*>*         _the_empty_klass_array;          // Canonicalized klass array
124   static Array<InstanceKlass*>* _the_empty_instance_klass_array; // Canonicalized instance klass array
125   static Array<Method*>*        _the_empty_method_array;         // Canonicalized method array
126 
127   static Array<Klass*>*  _the_array_interfaces_array;
128 
129   // array of preallocated error objects with backtrace
130   static OopHandle     _preallocated_out_of_memory_error_array;
131 
132   // number of preallocated error objects available for use
133   static volatile jint _preallocated_out_of_memory_error_avail_count;
134 
135   static OopHandle    _null_ptr_exception_instance;   // preallocated exception object
136   static OopHandle    _arithmetic_exception_instance; // preallocated exception object
137   static OopHandle    _virtual_machine_error_instance; // preallocated exception object
138 
139   // References waiting to be transferred to the ReferenceHandler
140   static OopHandle    _reference_pending_list;
141 
142   // The particular choice of collected heap.
143   static CollectedHeap* _collectedHeap;
144 
145   static intptr_t _non_oop_bits;
146 
147   // array of dummy objects used with +FullGCAlot
148   debug_only(static OopHandle   _fullgc_alot_dummy_array;)
149   debug_only(static int         _fullgc_alot_dummy_next;)
150 
151   // Compiler/dispatch support
152   static int  _base_vtable_size;                      // Java vtbl size of klass Object (in words)
153 
154   // Initialization
155   static bool _bootstrapping;                         // true during genesis
156   static bool _module_initialized;                    // true after call_initPhase2 called
157   static bool _fully_initialized;                     // true after universe_init and initialize_vtables called
158 
159   // the array of preallocated errors with backtraces
160   static objArrayOop  preallocated_out_of_memory_errors();
161 
162   static objArrayOop out_of_memory_errors();
163   // generate an out of memory error; if possible using an error with preallocated backtrace;
164   // otherwise return the given default error.
165   static oop        gen_out_of_memory_error(oop default_err);
166 
167   static OopStorage* _vm_weak;
168   static OopStorage* _vm_global;
169 
170   static jint initialize_heap();
171   static void initialize_tlab();
172   static void initialize_basic_type_mirrors(TRAPS);
173   static void fixup_mirrors(TRAPS);
174 
175   static void compute_base_vtable_size();             // compute vtable size of class Object
176 
177   static void genesis(TRAPS);                         // Create the initial world
178 
179   // Mirrors for primitive classes (created eagerly)
180   static oop check_mirror(oop m) {
181     assert(m != NULL, "mirror not initialized");
182     return m;
183   }
184 
185   // Debugging
186   static int _verify_count;                           // number of verifies done
187   static long verify_flags;
188 
189   static uintptr_t _verify_oop_mask;
190   static uintptr_t _verify_oop_bits;
191 
192  public:
193   static void calculate_verify_data(HeapWord* low_boundary, HeapWord* high_boundary) PRODUCT_RETURN;
194 
195   // Known classes in the VM
196   static Klass* boolArrayKlassObj()                 { return typeArrayKlassObj(T_BOOLEAN); }
197   static Klass* byteArrayKlassObj()                 { return typeArrayKlassObj(T_BYTE); }
198   static Klass* charArrayKlassObj()                 { return typeArrayKlassObj(T_CHAR); }
199   static Klass* intArrayKlassObj()                  { return typeArrayKlassObj(T_INT); }
200   static Klass* shortArrayKlassObj()                { return typeArrayKlassObj(T_SHORT); }
201   static Klass* longArrayKlassObj()                 { return typeArrayKlassObj(T_LONG); }
202   static Klass* floatArrayKlassObj()                { return typeArrayKlassObj(T_FLOAT); }
203   static Klass* doubleArrayKlassObj()               { return typeArrayKlassObj(T_DOUBLE); }
204 
205   static Klass* objectArrayKlassObj()               { return _objectArrayKlassObj; }
206 
207   static Klass* typeArrayKlassObj(BasicType t) {
208     assert((uint)t >= T_BOOLEAN, "range check for type: %s", type2name(t));
209     assert((uint)t < T_LONG+1,   "range check for type: %s", type2name(t));
210     assert(_typeArrayKlassObjs[t] != NULL, "domain check");
211     return _typeArrayKlassObjs[t];
212   }
213 
214   // Known objects in the VM
215   static oop int_mirror();
216   static oop float_mirror();
217   static oop double_mirror();
218   static oop byte_mirror();
219   static oop bool_mirror();
220   static oop char_mirror();
221   static oop long_mirror();
222   static oop short_mirror();
223   static oop void_mirror();
224 
225   // Table of primitive type mirrors, excluding T_OBJECT and T_ARRAY
226   // but including T_VOID, hence the index including T_VOID
227   static OopHandle _mirrors[T_VOID+1];
228 
229   static oop java_mirror(BasicType t);
230   static void replace_mirror(BasicType t, oop obj);
231 
232   static oop      main_thread_group();
233   static void set_main_thread_group(oop group);
234 
235   static oop      system_thread_group();
236   static void set_system_thread_group(oop group);
237 
238   static objArrayOop  the_empty_class_array ();
239 
240   static oop          the_null_string();
241   static oop          the_min_jint_string();
242 
243   static oop          null_ptr_exception_instance();
244   static oop          arithmetic_exception_instance();
245   static oop          virtual_machine_error_instance();
246   static oop          vm_exception()                  { return virtual_machine_error_instance(); }
247 
248   static Array<Klass*>* the_array_interfaces_array()  { return _the_array_interfaces_array;   }
249   static Method*      finalizer_register_method()     { return _finalizer_register_cache->get_method(); }
250   static Method*      loader_addClass_method()        { return _loader_addClass_cache->get_method(); }
251 
252   static Method*      throw_illegal_access_error()    { return _throw_illegal_access_error_cache->get_method(); }
253   static Method*      throw_no_such_method_error()    { return _throw_no_such_method_error_cache->get_method(); }
254 
255   static Method*      do_stack_walk_method()          { return _do_stack_walk_cache->get_method(); }
256 
257   static oop          the_null_sentinel();
258   static address      the_null_sentinel_addr()        { return (address) &_the_null_sentinel;  }
259 
260   // Function to initialize these
261   static void initialize_known_methods(TRAPS);
262 
263   static void create_preallocated_out_of_memory_errors(TRAPS);
264 
265   // Reference pending list manipulation.  Access is protected by
266   // Heap_lock.  The getter, setter and predicate require the caller
267   // owns the lock.  Swap is used by parallel non-concurrent reference
268   // processing threads, where some higher level controller owns
269   // Heap_lock, so requires the lock is locked, but not necessarily by
270   // the current thread.
271   static oop          reference_pending_list();
272   static void         clear_reference_pending_list();
273   static bool         has_reference_pending_list();
274   static oop          swap_reference_pending_list(oop list);
275 
276   static Array<int>*             the_empty_int_array()    { return _the_empty_int_array; }
277   static Array<u2>*              the_empty_short_array()  { return _the_empty_short_array; }
278   static Array<Method*>*         the_empty_method_array() { return _the_empty_method_array; }
279   static Array<Klass*>*          the_empty_klass_array()  { return _the_empty_klass_array; }
280   static Array<InstanceKlass*>*  the_empty_instance_klass_array() { return _the_empty_instance_klass_array; }
281 
282   // OutOfMemoryError support. Returns an error with the required message. The returned error
283   // may or may not have a backtrace. If error has a backtrace then the stack trace is already
284   // filled in.
285   static oop out_of_memory_error_java_heap();
286   static oop out_of_memory_error_c_heap();
287   static oop out_of_memory_error_metaspace();
288   static oop out_of_memory_error_class_metaspace();
289   static oop out_of_memory_error_array_size();
290   static oop out_of_memory_error_gc_overhead_limit();
291   static oop out_of_memory_error_realloc_objects();
292 
293   // Throw default _out_of_memory_error_retry object as it will never propagate out of the VM
294   static oop out_of_memory_error_retry();
295   static oop delayed_stack_overflow_error_message();
296 
297   // The particular choice of collected heap.
298   static CollectedHeap* heap() { return _collectedHeap; }
299 
300   DEBUG_ONLY(static bool is_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 == NULL || 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 "f" to all klasses for basic types (classes not present in
325   // SystemDictionary).
326   static void basic_type_classes_do(void f(Klass*));
327   static void basic_type_classes_do(KlassClosure* closure);
328   static void metaspace_pointers_do(MetaspaceClosure* it);
329 
330   // Debugging
331   enum VERIFY_FLAGS {
332     Verify_Threads = 1,
333     Verify_Heap = 2,
334     Verify_SymbolTable = 4,
335     Verify_StringTable = 8,
336     Verify_CodeCache = 16,
337     Verify_SystemDictionary = 32,
338     Verify_ClassLoaderDataGraph = 64,
339     Verify_MetaspaceUtils = 128,
340     Verify_JNIHandles = 256,
341     Verify_CodeCacheOops = 512,
342     Verify_ResolvedMethodTable = 1024,
343     Verify_StringDedup = 2048,
344     Verify_All = -1
345   };
346   static void initialize_verify_flags();
347   static bool should_verify_subset(uint subset);
348   static void verify(VerifyOption option, const char* prefix);
349   static void verify(const char* prefix) {
350     verify(VerifyOption_Default, prefix);
351   }
352   static void verify() {
353     verify("");
354   }
355 
356   static int  verify_count()       { return _verify_count; }
357   static void print_on(outputStream* st);
358   static void print_heap_at_SIGBREAK();
359 
360   // Change the number of dummy objects kept reachable by the full gc dummy
361   // array; this should trigger relocation in a sliding compaction collector.
362   debug_only(static bool release_fullgc_alot_dummy();)
363   // The non-oop pattern (see compiledIC.hpp, etc)
364   static void*         non_oop_word();
365   static bool contains_non_oop_word(void* p);
366 
367   // Oop verification (see MacroAssembler::verify_oop)
368   static uintptr_t verify_oop_mask()          PRODUCT_RETURN0;
369   static uintptr_t verify_oop_bits()          PRODUCT_RETURN0;
370   static uintptr_t verify_mark_bits()         PRODUCT_RETURN0;
371   static uintptr_t verify_mark_mask()         PRODUCT_RETURN0;
372 
373   // Compiler support
374   static int base_vtable_size()               { return _base_vtable_size; }
375 };
376 
377 #endif // SHARE_MEMORY_UNIVERSE_HPP