1 /*
   2  * Copyright (c) 1997, 2015, 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_VM_MEMORY_UNIVERSE_HPP
  26 #define SHARE_VM_MEMORY_UNIVERSE_HPP
  27 
  28 #include "runtime/handles.hpp"
  29 #include "utilities/array.hpp"
  30 #include "utilities/growableArray.hpp"
  31 
  32 // Universe is a name space holding known system classes and objects in the VM.
  33 //
  34 // Loaded classes are accessible through the SystemDictionary.
  35 //
  36 // The object heap is allocated and accessed through Universe, and various allocation
  37 // support is provided. Allocation by the interpreter and compiled code is done inline
  38 // and bails out to Scavenge::invoke_and_allocate.
  39 
  40 class CollectedHeap;
  41 class DeferredObjAllocEvent;
  42 
  43 
  44 // A helper class for caching a Method* when the user of the cache
  45 // only cares about the latest version of the Method*.  This cache safely
  46 // interacts with the RedefineClasses API.
  47 
  48 class LatestMethodCache : public CHeapObj<mtClass> {
  49   // We save the Klass* and the idnum of Method* in order to get
  50   // the current cached Method*.
  51  private:
  52   Klass*                _klass;
  53   int                   _method_idnum;
  54 
  55  public:
  56   LatestMethodCache()   { _klass = NULL; _method_idnum = -1; }
  57   ~LatestMethodCache()  { _klass = NULL; _method_idnum = -1; }
  58 
  59   void   init(Klass* k, Method* m);
  60   Klass* klass() const           { return _klass; }
  61   int    method_idnum() const    { return _method_idnum; }
  62 
  63   Method* get_method();
  64 
  65   // Enhanced Class Redefinition support
  66   void classes_do(void f(Klass*)) {
  67     f(_klass);
  68   }
  69 
  70   // CDS support.  Replace the klass in this with the archive version
  71   // could use this for Enhanced Class Redefinition also.
  72   void serialize(SerializeClosure* f) {
  73     f->do_ptr((void**)&_klass);
  74   }
  75 };
  76 
  77 
  78 // For UseCompressedOops.
  79 struct NarrowPtrStruct {
  80   // Base address for oop-within-java-object materialization.
  81   // NULL if using wide oops or zero based narrow oops.
  82   address _base;
  83   // Number of shift bits for encoding/decoding narrow ptrs.
  84   // 0 if using wide ptrs or zero based unscaled narrow ptrs,
  85   // LogMinObjAlignmentInBytes/LogKlassAlignmentInBytes otherwise.
  86   int     _shift;
  87   // Generate code with implicit null checks for narrow ptrs.
  88   bool    _use_implicit_null_checks;
  89 };
  90 
  91 enum VerifyOption {
  92       VerifyOption_Default = 0,
  93 
  94       // G1
  95       VerifyOption_G1UsePrevMarking = VerifyOption_Default,
  96       VerifyOption_G1UseNextMarking = VerifyOption_G1UsePrevMarking + 1,
  97       VerifyOption_G1UseMarkWord    = VerifyOption_G1UseNextMarking + 1
  98 };
  99 
 100 class Universe: AllStatic {
 101   // Ugh.  Universe is much too friendly.
 102   friend class MarkSweep;
 103   friend class oopDesc;
 104   friend class ClassLoader;
 105   friend class Arguments;
 106   friend class SystemDictionary;
 107   friend class VMStructs;
 108   friend class VM_PopulateDumpSharedSpace;
 109   friend class Metaspace;
 110 
 111   friend jint  universe_init();
 112   friend void  universe2_init();
 113   friend bool  universe_post_init();
 114 
 115  private:
 116   // Known classes in the VM
 117   static Klass* _boolArrayKlassObj;
 118   static Klass* _byteArrayKlassObj;
 119   static Klass* _charArrayKlassObj;
 120   static Klass* _intArrayKlassObj;
 121   static Klass* _shortArrayKlassObj;
 122   static Klass* _longArrayKlassObj;
 123   static Klass* _singleArrayKlassObj;
 124   static Klass* _doubleArrayKlassObj;
 125   static Klass* _typeArrayKlassObjs[T_VOID+1];
 126 
 127   static Klass* _objectArrayKlassObj;
 128 
 129   // Known objects in the VM
 130 
 131   // Primitive objects
 132   static oop _int_mirror;
 133   static oop _float_mirror;
 134   static oop _double_mirror;
 135   static oop _byte_mirror;
 136   static oop _bool_mirror;
 137   static oop _char_mirror;
 138   static oop _long_mirror;
 139   static oop _short_mirror;
 140   static oop _void_mirror;
 141 
 142   static oop          _main_thread_group;             // Reference to the main thread group object
 143   static oop          _system_thread_group;           // Reference to the system thread group object
 144 
 145   static objArrayOop  _the_empty_class_klass_array;   // Canonicalized obj array of type java.lang.Class
 146   static oop          _the_null_string;               // A cache of "null" as a Java string
 147   static oop          _the_min_jint_string;          // A cache of "-2147483648" as a Java string
 148   static LatestMethodCache* _finalizer_register_cache; // static method for registering finalizable objects
 149   static LatestMethodCache* _loader_addClass_cache;    // method for registering loaded classes in class loader vector
 150   static LatestMethodCache* _pd_implies_cache;         // method for checking protection domain attributes
 151   static LatestMethodCache* _throw_illegal_access_error_cache; // Unsafe.throwIllegalAccessError() method
 152 
 153   // preallocated error objects (no backtrace)
 154   static oop          _out_of_memory_error_java_heap;
 155   static oop          _out_of_memory_error_metaspace;
 156   static oop          _out_of_memory_error_class_metaspace;
 157   static oop          _out_of_memory_error_array_size;
 158   static oop          _out_of_memory_error_gc_overhead_limit;
 159   static oop          _out_of_memory_error_realloc_objects;
 160 
 161   static Array<int>*       _the_empty_int_array;    // Canonicalized int array
 162   static Array<u2>*        _the_empty_short_array;  // Canonicalized short array
 163   static Array<Klass*>*  _the_empty_klass_array;  // Canonicalized klass obj array
 164   static Array<Method*>* _the_empty_method_array; // Canonicalized method obj array
 165 
 166   static Array<Klass*>*  _the_array_interfaces_array;
 167 
 168   // array of preallocated error objects with backtrace
 169   static objArrayOop   _preallocated_out_of_memory_error_array;
 170 
 171   // number of preallocated error objects available for use
 172   static volatile jint _preallocated_out_of_memory_error_avail_count;
 173 
 174   static oop          _null_ptr_exception_instance;   // preallocated exception object
 175   static oop          _arithmetic_exception_instance; // preallocated exception object
 176   static oop          _virtual_machine_error_instance; // preallocated exception object
 177   // The object used as an exception dummy when exceptions are thrown for
 178   // the vm thread.
 179   static oop          _vm_exception;
 180 
 181   static oop          _allocation_context_notification_obj;
 182 
 183   // The particular choice of collected heap.
 184   static CollectedHeap* _collectedHeap;
 185 
 186   static intptr_t _non_oop_bits;
 187 
 188   // For UseCompressedOops.
 189   static struct NarrowPtrStruct _narrow_oop;
 190   // For UseCompressedClassPointers.
 191   static struct NarrowPtrStruct _narrow_klass;
 192   static address _narrow_ptrs_base;
 193 
 194   // array of dummy objects used with +FullGCAlot
 195   debug_only(static objArrayOop _fullgc_alot_dummy_array;)
 196   // index of next entry to clear
 197   debug_only(static int         _fullgc_alot_dummy_next;)
 198 
 199   // Compiler/dispatch support
 200   static int  _base_vtable_size;                      // Java vtbl size of klass Object (in words)
 201 
 202   // Initialization
 203   static bool _bootstrapping;                         // true during genesis
 204   static bool _fully_initialized;                     // true after universe_init and initialize_vtables called
 205 
 206   // the array of preallocated errors with backtraces
 207   static objArrayOop  preallocated_out_of_memory_errors()     { return _preallocated_out_of_memory_error_array; }
 208 
 209   // generate an out of memory error; if possible using an error with preallocated backtrace;
 210   // otherwise return the given default error.
 211   static oop        gen_out_of_memory_error(oop default_err);
 212 
 213   // Historic gc information
 214   static size_t _heap_capacity_at_last_gc;
 215   static size_t _heap_used_at_last_gc;
 216 
 217   static jint initialize_heap();
 218   static void initialize_basic_type_mirrors(TRAPS);
 219   static void fixup_mirrors(TRAPS);
 220 
 221   static void reinitialize_vtable_of(KlassHandle h_k, TRAPS);
 222   static void reinitialize_itables(TRAPS);
 223   static void compute_base_vtable_size();             // compute vtable size of class Object
 224 
 225   static void genesis(TRAPS);                         // Create the initial world
 226 
 227   // Mirrors for primitive classes (created eagerly)
 228   static oop check_mirror(oop m) {
 229     assert(m != NULL, "mirror not initialized");
 230     return m;
 231   }
 232 
 233   static void     set_narrow_oop_base(address base) {
 234     assert(UseCompressedOops, "no compressed oops?");
 235     _narrow_oop._base    = base;
 236   }
 237   static void     set_narrow_klass_base(address base) {
 238     assert(UseCompressedClassPointers, "no compressed klass ptrs?");
 239     _narrow_klass._base   = base;
 240   }
 241   static void     set_narrow_oop_use_implicit_null_checks(bool use) {
 242     assert(UseCompressedOops, "no compressed ptrs?");
 243     _narrow_oop._use_implicit_null_checks   = use;
 244   }
 245 
 246   // Debugging
 247   static int _verify_count;                           // number of verifies done
 248   // True during call to verify().  Should only be set/cleared in verify().
 249   static bool _verify_in_progress;
 250 
 251   static void compute_verify_oop_data();
 252 
 253  public:
 254   // Known classes in the VM
 255   static Klass* boolArrayKlassObj()                 { return _boolArrayKlassObj;   }
 256   static Klass* byteArrayKlassObj()                 { return _byteArrayKlassObj;   }
 257   static Klass* charArrayKlassObj()                 { return _charArrayKlassObj;   }
 258   static Klass* intArrayKlassObj()                  { return _intArrayKlassObj;    }
 259   static Klass* shortArrayKlassObj()                { return _shortArrayKlassObj;  }
 260   static Klass* longArrayKlassObj()                 { return _longArrayKlassObj;   }
 261   static Klass* singleArrayKlassObj()               { return _singleArrayKlassObj; }
 262   static Klass* doubleArrayKlassObj()               { return _doubleArrayKlassObj; }
 263 
 264   static Klass* objectArrayKlassObj() {
 265     return _objectArrayKlassObj;
 266   }
 267 
 268   static Klass* typeArrayKlassObj(BasicType t) {
 269     assert((uint)t < T_VOID+1, err_msg("range check for type: %s", type2name(t)));
 270     assert(_typeArrayKlassObjs[t] != NULL, "domain check");
 271     return _typeArrayKlassObjs[t];
 272   }
 273 
 274   // Known objects in the VM
 275   static oop int_mirror()                   { return check_mirror(_int_mirror); }
 276   static oop float_mirror()                 { return check_mirror(_float_mirror); }
 277   static oop double_mirror()                { return check_mirror(_double_mirror); }
 278   static oop byte_mirror()                  { return check_mirror(_byte_mirror); }
 279   static oop bool_mirror()                  { return check_mirror(_bool_mirror); }
 280   static oop char_mirror()                  { return check_mirror(_char_mirror); }
 281   static oop long_mirror()                  { return check_mirror(_long_mirror); }
 282   static oop short_mirror()                 { return check_mirror(_short_mirror); }
 283   static oop void_mirror()                  { return check_mirror(_void_mirror); }
 284 
 285   // table of same
 286   static oop _mirrors[T_VOID+1];
 287 
 288   static oop java_mirror(BasicType t) {
 289     assert((uint)t < T_VOID+1, "range check");
 290     return check_mirror(_mirrors[t]);
 291   }
 292   static oop      main_thread_group()                 { return _main_thread_group; }
 293   static void set_main_thread_group(oop group)        { _main_thread_group = group;}
 294 
 295   static oop      system_thread_group()               { return _system_thread_group; }
 296   static void set_system_thread_group(oop group)      { _system_thread_group = group;}
 297 
 298   static objArrayOop  the_empty_class_klass_array ()  { return _the_empty_class_klass_array;   }
 299   static Array<Klass*>* the_array_interfaces_array() { return _the_array_interfaces_array;   }
 300   static oop          the_null_string()               { return _the_null_string;               }
 301   static oop          the_min_jint_string()          { return _the_min_jint_string;          }
 302 
 303   static Method*      finalizer_register_method()     { return _finalizer_register_cache->get_method(); }
 304   static Method*      loader_addClass_method()        { return _loader_addClass_cache->get_method(); }
 305 
 306   static Method*      protection_domain_implies_method() { return _pd_implies_cache->get_method(); }
 307   static Method*      throw_illegal_access_error()    { return _throw_illegal_access_error_cache->get_method(); }
 308 
 309   static oop          null_ptr_exception_instance()   { return _null_ptr_exception_instance;   }
 310   static oop          arithmetic_exception_instance() { return _arithmetic_exception_instance; }
 311   static oop          virtual_machine_error_instance() { return _virtual_machine_error_instance; }
 312   static oop          vm_exception()                  { return _vm_exception; }
 313 
 314   static inline oop   allocation_context_notification_obj();
 315   static inline void  set_allocation_context_notification_obj(oop obj);
 316 
 317   static Array<int>*       the_empty_int_array()    { return _the_empty_int_array; }
 318   static Array<u2>*        the_empty_short_array()  { return _the_empty_short_array; }
 319   static Array<Method*>* the_empty_method_array() { return _the_empty_method_array; }
 320   static Array<Klass*>*  the_empty_klass_array()  { return _the_empty_klass_array; }
 321 
 322   // OutOfMemoryError support. Returns an error with the required message. The returned error
 323   // may or may not have a backtrace. If error has a backtrace then the stack trace is already
 324   // filled in.
 325   static oop out_of_memory_error_java_heap()          { return gen_out_of_memory_error(_out_of_memory_error_java_heap);  }
 326   static oop out_of_memory_error_metaspace()          { return gen_out_of_memory_error(_out_of_memory_error_metaspace);   }
 327   static oop out_of_memory_error_class_metaspace()    { return gen_out_of_memory_error(_out_of_memory_error_class_metaspace);   }
 328   static oop out_of_memory_error_array_size()         { return gen_out_of_memory_error(_out_of_memory_error_array_size); }
 329   static oop out_of_memory_error_gc_overhead_limit()  { return gen_out_of_memory_error(_out_of_memory_error_gc_overhead_limit);  }
 330   static oop out_of_memory_error_realloc_objects()    { return gen_out_of_memory_error(_out_of_memory_error_realloc_objects);  }
 331 
 332   // Accessors needed for fast allocation
 333   static Klass** boolArrayKlassObj_addr()           { return &_boolArrayKlassObj;   }
 334   static Klass** byteArrayKlassObj_addr()           { return &_byteArrayKlassObj;   }
 335   static Klass** charArrayKlassObj_addr()           { return &_charArrayKlassObj;   }
 336   static Klass** intArrayKlassObj_addr()            { return &_intArrayKlassObj;    }
 337   static Klass** shortArrayKlassObj_addr()          { return &_shortArrayKlassObj;  }
 338   static Klass** longArrayKlassObj_addr()           { return &_longArrayKlassObj;   }
 339   static Klass** singleArrayKlassObj_addr()         { return &_singleArrayKlassObj; }
 340   static Klass** doubleArrayKlassObj_addr()         { return &_doubleArrayKlassObj; }
 341   static Klass** objectArrayKlassObj_addr()         { return &_objectArrayKlassObj; }
 342 
 343   // The particular choice of collected heap.
 344   static CollectedHeap* heap() { return _collectedHeap; }
 345 
 346   // For UseCompressedOops
 347   // Narrow Oop encoding mode:
 348   // 0 - Use 32-bits oops without encoding when
 349   //     NarrowOopHeapBaseMin + heap_size < 4Gb
 350   // 1 - Use zero based compressed oops with encoding when
 351   //     NarrowOopHeapBaseMin + heap_size < 32Gb
 352   // 2 - Use compressed oops with heap base + encoding.
 353   enum NARROW_OOP_MODE {
 354     UnscaledNarrowOop  = 0,
 355     ZeroBasedNarrowOop = 1,
 356     HeapBasedNarrowOop = 2
 357   };
 358   static NARROW_OOP_MODE narrow_oop_mode();
 359   static const char* narrow_oop_mode_to_string(NARROW_OOP_MODE mode);
 360   static char*    preferred_heap_base(size_t heap_size, size_t alignment, NARROW_OOP_MODE mode);
 361   static char*    preferred_metaspace_base(size_t heap_size, NARROW_OOP_MODE mode);
 362   static address  narrow_oop_base()                       { return  _narrow_oop._base; }
 363   static bool  is_narrow_oop_base(void* addr)             { return (narrow_oop_base() == (address)addr); }
 364   static int      narrow_oop_shift()                      { return  _narrow_oop._shift; }
 365   static bool     narrow_oop_use_implicit_null_checks()   { return  _narrow_oop._use_implicit_null_checks; }
 366 
 367   // For UseCompressedClassPointers
 368   static address  narrow_klass_base()                     { return  _narrow_klass._base; }
 369   static bool  is_narrow_klass_base(void* addr)           { return (narrow_klass_base() == (address)addr); }
 370   static int      narrow_klass_shift()                    { return  _narrow_klass._shift; }
 371   static bool     narrow_klass_use_implicit_null_checks() { return  _narrow_klass._use_implicit_null_checks; }
 372 
 373   static address* narrow_ptrs_base_addr()                 { return &_narrow_ptrs_base; }
 374   static void     set_narrow_ptrs_base(address a)         { _narrow_ptrs_base = a; }
 375   static address  narrow_ptrs_base()                      { return _narrow_ptrs_base; }
 376 
 377   static void     print_compressed_oops_mode();
 378 
 379   // this is set in vm_version on sparc (and then reset in universe afaict)
 380   static void     set_narrow_oop_shift(int shift)         {
 381     _narrow_oop._shift   = shift;
 382   }
 383 
 384   static void     set_narrow_klass_shift(int shift)       {
 385     assert(shift == 0 || shift == LogKlassAlignmentInBytes, "invalid shift for klass ptrs");
 386     _narrow_klass._shift   = shift;
 387   }
 388 
 389   // Reserve Java heap and determine CompressedOops mode
 390   static ReservedSpace reserve_heap(size_t heap_size, size_t alignment);
 391 
 392   // Historic gc information
 393   static size_t get_heap_capacity_at_last_gc()         { return _heap_capacity_at_last_gc; }
 394   static size_t get_heap_free_at_last_gc()             { return _heap_capacity_at_last_gc - _heap_used_at_last_gc; }
 395   static size_t get_heap_used_at_last_gc()             { return _heap_used_at_last_gc; }
 396   static void update_heap_info_at_gc();
 397 
 398   // Testers
 399   static bool is_bootstrapping()                      { return _bootstrapping; }
 400   static bool is_fully_initialized()                  { return _fully_initialized; }
 401 
 402   static inline bool element_type_should_be_aligned(BasicType type);
 403   static inline bool field_type_should_be_aligned(BasicType type);
 404   static bool        on_page_boundary(void* addr);
 405   static bool        should_fill_in_stack_trace(Handle throwable);
 406   static void check_alignment(uintx size, uintx alignment, const char* name);
 407 
 408   // Finalizer support.
 409   static void run_finalizers_on_exit();
 410 
 411   // Iteration
 412 
 413   // Apply "f" to the addresses of all the direct heap pointers maintained
 414   // as static fields of "Universe".
 415   static void oops_do(OopClosure* f, bool do_all = false);
 416 
 417   // CDS support
 418   static void serialize(SerializeClosure* f, bool do_all = false);
 419 
 420   // Apply "f" to all klasses for basic types (classes not present in
 421   // SystemDictionary).
 422   static void basic_type_classes_do(void f(Klass*));
 423 
 424   // For sharing -- fill in a list of known vtable pointers.
 425   static void init_self_patching_vtbl_list(void** list, int count);
 426 
 427   // Debugging
 428   static bool verify_in_progress() { return _verify_in_progress; }
 429   static void verify(VerifyOption option, const char* prefix, bool silent = VerifySilently);
 430   static void verify(const char* prefix, bool silent = VerifySilently) {
 431     verify(VerifyOption_Default, prefix, silent);
 432   }
 433   static void verify(bool silent = VerifySilently) {
 434     verify("", silent);
 435   }
 436 
 437   static int  verify_count()       { return _verify_count; }
 438   // The default behavior is to call print_on() on gclog_or_tty.
 439   static void print();
 440   // The extended parameter determines which method on the heap will
 441   // be called: print_on() (extended == false) or print_extended_on()
 442   // (extended == true).
 443   static void print_on(outputStream* st, bool extended = false);
 444   static void print_heap_at_SIGBREAK();
 445   static void print_heap_before_gc() { print_heap_before_gc(gclog_or_tty); }
 446   static void print_heap_after_gc()  { print_heap_after_gc(gclog_or_tty); }
 447   static void print_heap_before_gc(outputStream* st, bool ignore_extended = false);
 448   static void print_heap_after_gc(outputStream* st, bool ignore_extended = false);
 449 
 450   // Change the number of dummy objects kept reachable by the full gc dummy
 451   // array; this should trigger relocation in a sliding compaction collector.
 452   debug_only(static bool release_fullgc_alot_dummy();)
 453   // The non-oop pattern (see compiledIC.hpp, etc)
 454   static void*   non_oop_word();
 455 
 456   // Oop verification (see MacroAssembler::verify_oop)
 457   static uintptr_t verify_oop_mask()          PRODUCT_RETURN0;
 458   static uintptr_t verify_oop_bits()          PRODUCT_RETURN0;
 459   static uintptr_t verify_mark_bits()         PRODUCT_RETURN0;
 460   static uintptr_t verify_mark_mask()         PRODUCT_RETURN0;
 461 
 462   // Flushing and deoptimization
 463   static void flush_dependents_on(instanceKlassHandle dependee);
 464   static void flush_dependents_on(Handle call_site, Handle method_handle);
 465 #ifdef HOTSWAP
 466   // Flushing and deoptimization in case of evolution
 467   static void flush_evol_dependents_on(instanceKlassHandle dependee);
 468 #endif // HOTSWAP
 469   // Support for fullspeed debugging
 470   static void flush_dependents_on_method(methodHandle dependee);
 471 
 472   // Compiler support
 473   static int base_vtable_size()               { return _base_vtable_size; }
 474 };
 475 
 476 class DeferredObjAllocEvent : public CHeapObj<mtInternal> {
 477   private:
 478     oop    _oop;
 479     size_t _bytesize;
 480     jint   _arena_id;
 481 
 482   public:
 483     DeferredObjAllocEvent(const oop o, const size_t s, const jint id) {
 484       _oop      = o;
 485       _bytesize = s;
 486       _arena_id = id;
 487     }
 488 
 489     ~DeferredObjAllocEvent() {
 490     }
 491 
 492     jint   arena_id() { return _arena_id; }
 493     size_t bytesize() { return _bytesize; }
 494     oop    get_oop()  { return _oop; }
 495 };
 496 
 497 #endif // SHARE_VM_MEMORY_UNIVERSE_HPP