1 /*
   2  * Copyright (c) 2012, 2020, 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_METASPACESHARED_HPP
  26 #define SHARE_MEMORY_METASPACESHARED_HPP
  27 
  28 #include "classfile/compactHashtable.hpp"
  29 #include "memory/allocation.hpp"
  30 #include "memory/memRegion.hpp"
  31 #include "memory/virtualspace.hpp"
  32 #include "oops/oop.hpp"
  33 #include "utilities/exceptions.hpp"
  34 #include "utilities/macros.hpp"
  35 #include "utilities/resourceHash.hpp"
  36 
  37 #define MAX_SHARED_DELTA                (0x7FFFFFFF)
  38 
  39 class outputStream;
  40 class FileMapInfo;
  41 class CHeapBitMap;
  42 struct ArchiveHeapOopmapInfo;
  43 
  44 enum MapArchiveResult {
  45   MAP_ARCHIVE_SUCCESS,
  46   MAP_ARCHIVE_MMAP_FAILURE,
  47   MAP_ARCHIVE_OTHER_FAILURE
  48 };
  49 
  50 class MetaspaceSharedStats {
  51 public:
  52   MetaspaceSharedStats() {
  53     memset(this, 0, sizeof(*this));
  54   }
  55   CompactHashtableStats symbol;
  56   CompactHashtableStats string;
  57 };
  58 
  59 #if INCLUDE_CDS
  60 class DumpRegion {
  61 private:
  62   const char* _name;
  63   char* _base;
  64   char* _top;
  65   char* _end;
  66   bool _is_packed;
  67   ReservedSpace* _rs;
  68   VirtualSpace* _vs;
  69 
  70 public:
  71   DumpRegion(const char* name) : _name(name), _base(NULL), _top(NULL), _end(NULL), _is_packed(false) {}
  72 
  73   char* expand_top_to(char* newtop);
  74   char* allocate(size_t num_bytes, size_t alignment=BytesPerWord);
  75 
  76   void append_intptr_t(intptr_t n, bool need_to_mark = false);
  77 
  78   char* base()      const { return _base;        }
  79   char* top()       const { return _top;         }
  80   char* end()       const { return _end;         }
  81   size_t reserved() const { return _end - _base; }
  82   size_t used()     const { return _top - _base; }
  83   bool is_packed()  const { return _is_packed;   }
  84   bool is_allocatable() const {
  85     return !is_packed() && _base != NULL;
  86   }
  87 
  88   void print(size_t total_bytes) const;
  89   void print_out_of_space_msg(const char* failing_region, size_t needed_bytes);
  90 
  91   void init(ReservedSpace* rs, VirtualSpace* vs);
  92 
  93   void pack(DumpRegion* next = NULL);
  94 
  95   bool contains(char* p) {
  96     return base() <= p && p < top();
  97   }
  98 };
  99 
 100 // Closure for serializing initialization data out to a data area to be
 101 // written to the shared file.
 102 
 103 class WriteClosure : public SerializeClosure {
 104 private:
 105   DumpRegion* _dump_region;
 106 
 107 public:
 108   WriteClosure(DumpRegion* r) {
 109     _dump_region = r;
 110   }
 111 
 112   void do_ptr(void** p) {
 113     _dump_region->append_intptr_t((intptr_t)*p, true);
 114   }
 115 
 116   void do_u4(u4* p) {
 117     _dump_region->append_intptr_t((intptr_t)(*p));
 118   }
 119 
 120   void do_bool(bool *p) {
 121     _dump_region->append_intptr_t((intptr_t)(*p));
 122   }
 123 
 124   void do_tag(int tag) {
 125     _dump_region->append_intptr_t((intptr_t)tag);
 126   }
 127 
 128   void do_oop(oop* o);
 129 
 130   void do_region(u_char* start, size_t size);
 131 
 132   bool reading() const { return false; }
 133 };
 134 
 135 // Closure for serializing initialization data in from a data area
 136 // (ptr_array) read from the shared file.
 137 
 138 class ReadClosure : public SerializeClosure {
 139 private:
 140   intptr_t** _ptr_array;
 141 
 142   inline intptr_t nextPtr() {
 143     return *(*_ptr_array)++;
 144   }
 145 
 146 public:
 147   ReadClosure(intptr_t** ptr_array) { _ptr_array = ptr_array; }
 148 
 149   void do_ptr(void** p);
 150 
 151   void do_u4(u4* p);
 152 
 153   void do_bool(bool *p);
 154 
 155   void do_tag(int tag);
 156 
 157   void do_oop(oop *p);
 158 
 159   void do_region(u_char* start, size_t size);
 160 
 161   bool reading() const { return true; }
 162 };
 163 
 164 #endif // INCLUDE_CDS
 165 
 166 // Class Data Sharing Support
 167 class MetaspaceShared : AllStatic {
 168 
 169   // CDS support
 170 
 171   // Note: _shared_rs and _symbol_rs are only used at dump time.
 172   static ReservedSpace _shared_rs;
 173   static VirtualSpace _shared_vs;
 174   static ReservedSpace _symbol_rs;
 175   static VirtualSpace _symbol_vs;
 176   static int _max_alignment;
 177   static MetaspaceSharedStats _stats;
 178   static bool _has_error_classes;
 179   static bool _archive_loading_failed;
 180   static bool _remapped_readwrite;
 181   static address _i2i_entry_code_buffers;
 182   static size_t  _i2i_entry_code_buffers_size;
 183   static size_t  _core_spaces_size;
 184   static void* _shared_metaspace_static_top;
 185   static intx _relocation_delta;
 186   static char* _requested_base_address;
 187   static bool _use_optimized_module_handling;
 188   static bool _use_full_module_graph;
 189  public:
 190   enum {
 191     // core archive spaces
 192     mc = 0,  // miscellaneous code for method trampolines
 193     rw = 1,  // read-write shared space in the heap
 194     ro = 2,  // read-only shared space in the heap
 195     bm = 3,  // relocation bitmaps (freed after file mapping is finished)
 196     num_core_region = 3,
 197     num_non_heap_spaces = 4,
 198 
 199     // mapped java heap regions
 200     first_closed_archive_heap_region = bm + 1,
 201     max_closed_archive_heap_region = 2,
 202     last_closed_archive_heap_region = first_closed_archive_heap_region + max_closed_archive_heap_region - 1,
 203     first_open_archive_heap_region = last_closed_archive_heap_region + 1,
 204     max_open_archive_heap_region = 2,
 205     last_open_archive_heap_region = first_open_archive_heap_region + max_open_archive_heap_region - 1,
 206 
 207     last_valid_region = last_open_archive_heap_region,
 208     n_regions =  last_valid_region + 1 // total number of regions
 209   };
 210 
 211   static void prepare_for_dumping() NOT_CDS_RETURN;
 212   static void preload_and_dump(TRAPS) NOT_CDS_RETURN;
 213   static int preload_classes(const char * class_list_path,
 214                              TRAPS) NOT_CDS_RETURN_(0);
 215 
 216   static GrowableArray<Klass*>* collected_klasses();
 217   static GrowableArray<Symbol*>* collected_symbols();
 218   static void add_symbol(Symbol* sym) NOT_CDS_RETURN;
 219 
 220   static ReservedSpace* shared_rs() {
 221     CDS_ONLY(return &_shared_rs);
 222     NOT_CDS(return NULL);
 223   }
 224 
 225   static Symbol* symbol_rs_base() {
 226     return (Symbol*)_symbol_rs.base();
 227   }
 228 
 229   static void set_shared_rs(ReservedSpace rs) {
 230     CDS_ONLY(_shared_rs = rs);
 231   }
 232 
 233   static void commit_to(ReservedSpace* rs, VirtualSpace* vs, char* newtop) NOT_CDS_RETURN;
 234   static void initialize_dumptime_shared_and_meta_spaces() NOT_CDS_RETURN;
 235   static void initialize_runtime_shared_and_meta_spaces() NOT_CDS_RETURN;
 236   static void post_initialize(TRAPS) NOT_CDS_RETURN;
 237 
 238   static void print_on(outputStream* st);
 239 
 240   // Delta of this object from SharedBaseAddress
 241   static uintx object_delta_uintx(void* obj);
 242 
 243   static u4 object_delta_u4(void* obj) {
 244     // offset is guaranteed to be less than MAX_SHARED_DELTA in DumpRegion::expand_top_to()
 245     uintx deltax = object_delta_uintx(obj);
 246     guarantee(deltax <= MAX_SHARED_DELTA, "must be 32-bit offset");
 247     return (u4)deltax;
 248   }
 249 
 250   static void set_archive_loading_failed() {
 251     _archive_loading_failed = true;
 252   }
 253   static bool is_in_output_space(void* ptr) {
 254     assert(DumpSharedSpaces, "must be");
 255     return shared_rs()->contains(ptr);
 256   }
 257 
 258   static bool map_shared_spaces(FileMapInfo* mapinfo) NOT_CDS_RETURN_(false);
 259   static void initialize_shared_spaces() NOT_CDS_RETURN;
 260 
 261   // Return true if given address is in the shared metaspace regions (i.e., excluding any
 262   // mapped shared heap regions.)
 263   static bool is_in_shared_metaspace(const void* p) {
 264     return MetaspaceObj::is_shared((const MetaspaceObj*)p);
 265   }
 266 
 267   static address shared_metaspace_top() {
 268     return (address)MetaspaceObj::shared_metaspace_top();
 269   }
 270 
 271   static void set_shared_metaspace_range(void* base, void *static_top, void* top) NOT_CDS_RETURN;
 272 
 273   // Return true if given address is in the shared region corresponding to the idx
 274   static bool is_in_shared_region(const void* p, int idx) NOT_CDS_RETURN_(false);
 275 
 276   static bool is_in_trampoline_frame(address addr) NOT_CDS_RETURN_(false);
 277 
 278   static bool is_shared_dynamic(void* p) NOT_CDS_RETURN_(false);
 279 
 280   static char* allocate_cpp_vtable_clones();
 281   static void clone_cpp_vtables(intptr_t* p);
 282   static void zero_cpp_vtable_clones_for_writing();
 283   static void patch_cpp_vtable_pointers();
 284   static void serialize_cloned_cpp_vtptrs(SerializeClosure* sc);
 285 
 286   static bool is_valid_shared_method(const Method* m) NOT_CDS_RETURN_(false);
 287   static void serialize(SerializeClosure* sc) NOT_CDS_RETURN;
 288 
 289   static MetaspaceSharedStats* stats() {
 290     return &_stats;
 291   }
 292 
 293   static void report_out_of_space(const char* name, size_t needed_bytes);
 294 
 295   // JVM/TI RedefineClasses() support:
 296   // Remap the shared readonly space to shared readwrite, private if
 297   // sharing is enabled. Simply returns true if sharing is not enabled
 298   // or if the remapping has already been done by a prior call.
 299   static bool remap_shared_readonly_as_readwrite() NOT_CDS_RETURN_(true);
 300   static bool remapped_readwrite() {
 301     CDS_ONLY(return _remapped_readwrite);
 302     NOT_CDS(return false);
 303   }
 304 
 305   static bool try_link_class(InstanceKlass* ik, TRAPS);
 306   static void link_and_cleanup_shared_classes(TRAPS) NOT_CDS_RETURN;
 307 
 308 #if INCLUDE_CDS
 309   static size_t reserved_space_alignment();
 310   static void init_shared_dump_space(DumpRegion* first_space);
 311   static DumpRegion* misc_code_dump_space();
 312   static DumpRegion* read_write_dump_space();
 313   static DumpRegion* read_only_dump_space();
 314   static void pack_dump_space(DumpRegion* current, DumpRegion* next,
 315                               ReservedSpace* rs);
 316 
 317   static void rewrite_nofast_bytecodes_and_calculate_fingerprints(Thread* thread, InstanceKlass* ik);
 318 #endif
 319 
 320   // Allocate a block of memory from the temporary "symbol" region.
 321   static char* symbol_space_alloc(size_t num_bytes);
 322 
 323   // Allocate a block of memory from the "mc" or "ro" regions.
 324   static char* misc_code_space_alloc(size_t num_bytes);
 325   static char* read_only_space_alloc(size_t num_bytes);
 326   static char* read_write_space_alloc(size_t num_bytes);
 327 
 328   template <typename T>
 329   static Array<T>* new_ro_array(int length) {
 330     size_t byte_size = Array<T>::byte_sizeof(length, sizeof(T));
 331     Array<T>* array = (Array<T>*)read_only_space_alloc(byte_size);
 332     array->initialize(length);
 333     return array;
 334   }
 335 
 336   template <typename T>
 337   static Array<T>* new_rw_array(int length) {
 338     size_t byte_size = Array<T>::byte_sizeof(length, sizeof(T));
 339     Array<T>* array = (Array<T>*)read_write_space_alloc(byte_size);
 340     array->initialize(length);
 341     return array;
 342   }
 343 
 344   template <typename T>
 345   static size_t ro_array_bytesize(int length) {
 346     size_t byte_size = Array<T>::byte_sizeof(length, sizeof(T));
 347     return align_up(byte_size, BytesPerWord);
 348   }
 349 
 350   static address i2i_entry_code_buffers(size_t total_size);
 351 
 352   static address i2i_entry_code_buffers() {
 353     return _i2i_entry_code_buffers;
 354   }
 355   static size_t i2i_entry_code_buffers_size() {
 356     return _i2i_entry_code_buffers_size;
 357   }
 358   static void relocate_klass_ptr(oop o);
 359 
 360   static Klass* get_relocated_klass(Klass *k, bool is_final=false);
 361   static Symbol* get_relocated_symbol(Symbol* orig_symbol);
 362 
 363   static void allocate_cloned_cpp_vtptrs();
 364   static intptr_t* get_archived_cpp_vtable(MetaspaceObj::Type msotype, address obj);
 365   static void initialize_ptr_marker(CHeapBitMap* ptrmap);
 366 
 367   // This is the base address as specified by -XX:SharedBaseAddress during -Xshare:dump.
 368   // Both the base/top archives are written using this as their base address.
 369   static char* requested_base_address() {
 370     return _requested_base_address;
 371   }
 372 
 373   // Non-zero if the archive(s) need to be mapped a non-default location due to ASLR.
 374   static intx relocation_delta() { return _relocation_delta; }
 375   static intx final_delta();
 376   static bool use_windows_memory_mapping() {
 377     const bool is_windows = (NOT_WINDOWS(false) WINDOWS_ONLY(true));
 378     //const bool is_windows = true; // enable this to allow testing the windows mmap semantics on Linux, etc.
 379     return is_windows;
 380   }
 381 
 382   static void write_core_archive_regions(FileMapInfo* mapinfo,
 383                                          GrowableArray<ArchiveHeapOopmapInfo>* closed_oopmaps,
 384                                          GrowableArray<ArchiveHeapOopmapInfo>* open_oopmaps);
 385 
 386   // Can we skip some expensive operations related to modules?
 387   static bool use_optimized_module_handling() { return NOT_CDS(false) CDS_ONLY(_use_optimized_module_handling); }
 388   static void disable_optimized_module_handling() { _use_optimized_module_handling = false; }
 389 
 390   // Can we use the full archived modue graph?
 391   static bool use_full_module_graph() NOT_CDS_RETURN_(false);
 392   static void disable_full_module_graph() { _use_full_module_graph = false; }
 393 
 394 private:
 395 #if INCLUDE_CDS
 396   static void write_region(FileMapInfo* mapinfo, int region_idx, DumpRegion* dump_region,
 397                            bool read_only,  bool allow_exec);
 398 #endif
 399   static void read_extra_data(const char* filename, TRAPS) NOT_CDS_RETURN;
 400   static FileMapInfo* open_static_archive();
 401   static FileMapInfo* open_dynamic_archive();
 402   // use_requested_addr: If true (default), attempt to map at the address the
 403   static MapArchiveResult map_archives(FileMapInfo* static_mapinfo, FileMapInfo* dynamic_mapinfo,
 404                                        bool use_requested_addr);
 405   static char* reserve_address_space_for_archives(FileMapInfo* static_mapinfo,
 406                                                   FileMapInfo* dynamic_mapinfo,
 407                                                   bool use_archive_base_addr,
 408                                                   ReservedSpace& archive_space_rs,
 409                                                   ReservedSpace& class_space_rs);
 410   static void release_reserved_spaces(ReservedSpace& archive_space_rs,
 411                                       ReservedSpace& class_space_rs);
 412   static MapArchiveResult map_archive(FileMapInfo* mapinfo, char* mapped_base_address, ReservedSpace rs);
 413   static void unmap_archive(FileMapInfo* mapinfo);
 414 };
 415 #endif // SHARE_MEMORY_METASPACESHARED_HPP