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