1 /*
   2  * Copyright (c) 2012, 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_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 FileMapInfo;
  40 
  41 class MetaspaceSharedStats {
  42 public:
  43   MetaspaceSharedStats() {
  44     memset(this, 0, sizeof(*this));
  45   }
  46   CompactHashtableStats symbol;
  47   CompactHashtableStats string;
  48 };
  49 
  50 #if INCLUDE_CDS
  51 class DumpRegion {
  52 private:
  53   const char* _name;
  54   char* _base;
  55   char* _top;
  56   char* _end;
  57   bool _is_packed;
  58 
  59 public:
  60   DumpRegion(const char* name) : _name(name), _base(NULL), _top(NULL), _end(NULL), _is_packed(false) {}
  61 
  62   char* expand_top_to(char* newtop);
  63   char* allocate(size_t num_bytes, size_t alignment=BytesPerWord);
  64 
  65   void append_intptr_t(intptr_t n) {
  66     assert(is_aligned(_top, sizeof(intptr_t)), "bad alignment");
  67     intptr_t *p = (intptr_t*)_top;
  68     char* newtop = _top + sizeof(intptr_t);
  69     expand_top_to(newtop);
  70     *p = n;
  71   }
  72 
  73   char* base()      const { return _base;        }
  74   char* top()       const { return _top;         }
  75   char* end()       const { return _end;         }
  76   size_t reserved() const { return _end - _base; }
  77   size_t used()     const { return _top - _base; }
  78   bool is_packed()  const { return _is_packed;   }
  79   bool is_allocatable() const {
  80     return !is_packed() && _base != NULL;
  81   }
  82 
  83   void print(size_t total_bytes) const;
  84   void print_out_of_space_msg(const char* failing_region, size_t needed_bytes);
  85 
  86   void init(const ReservedSpace* rs, char* base) {
  87     if (base == NULL) {
  88       base = rs->base();
  89     }
  90     assert(rs->contains(base), "must be");
  91     _base = _top = base;
  92     _end = rs->end();
  93   }
  94   void init(char* b, char* t, char* e) {
  95     _base = b;
  96     _top = t;
  97     _end = e;
  98   }
  99 
 100   void pack(DumpRegion* next = NULL);
 101 
 102   bool contains(char* p) {
 103     return base() <= p && p < top();
 104   }
 105 };
 106 
 107 // Closure for serializing initialization data out to a data area to be
 108 // written to the shared file.
 109 
 110 class WriteClosure : public SerializeClosure {
 111 private:
 112   DumpRegion* _dump_region;
 113 
 114 public:
 115   WriteClosure(DumpRegion* r) {
 116     _dump_region = r;
 117   }
 118 
 119   void do_ptr(void** p) {
 120     _dump_region->append_intptr_t((intptr_t)*p);
 121   }
 122 
 123   void do_u4(u4* p) {
 124     void* ptr = (void*)(uintx(*p));
 125     do_ptr(&ptr);
 126   }
 127 
 128   void do_tag(int tag) {
 129     _dump_region->append_intptr_t((intptr_t)tag);
 130   }
 131 
 132   void do_oop(oop* o);
 133 
 134   void do_region(u_char* start, size_t size);
 135 
 136   bool reading() const { return false; }
 137 };
 138 
 139 // Closure for serializing initialization data in from a data area
 140 // (ptr_array) read from the shared file.
 141 
 142 class ReadClosure : public SerializeClosure {
 143 private:
 144   intptr_t** _ptr_array;
 145 
 146   inline intptr_t nextPtr() {
 147     return *(*_ptr_array)++;
 148   }
 149 
 150 public:
 151   ReadClosure(intptr_t** ptr_array) { _ptr_array = ptr_array; }
 152 
 153   void do_ptr(void** p);
 154 
 155   void do_u4(u4* p);
 156 
 157   void do_tag(int tag);
 158 
 159   void do_oop(oop *p);
 160 
 161   void do_region(u_char* start, size_t size);
 162 
 163   bool reading() const { return true; }
 164 };
 165 
 166 #endif
 167 
 168 // Class Data Sharing Support
 169 class MetaspaceShared : AllStatic {
 170 
 171   // CDS support
 172   static ReservedSpace _shared_rs;
 173   static VirtualSpace _shared_vs;
 174   static int _max_alignment;
 175   static MetaspaceSharedStats _stats;
 176   static bool _has_error_classes;
 177   static bool _archive_loading_failed;
 178   static bool _remapped_readwrite;
 179   static address _cds_i2i_entry_code_buffers;
 180   static size_t  _cds_i2i_entry_code_buffers_size;
 181   static size_t  _core_spaces_size;
 182   static void* _shared_metaspace_static_top;
 183  public:
 184   enum {
 185     // core archive spaces
 186     mc = 0,  // miscellaneous code for method trampolines
 187     rw = 1,  // read-write shared space in the heap
 188     ro = 2,  // read-only shared space in the heap
 189     md = 3,  // miscellaneous data for initializing tables, etc.
 190     num_core_spaces = 4, // number of non-string regions
 191     num_non_heap_spaces = 4,
 192 
 193     // mapped java heap regions
 194     first_closed_archive_heap_region = md + 1,
 195     max_closed_archive_heap_region = 2,
 196     last_closed_archive_heap_region = first_closed_archive_heap_region + max_closed_archive_heap_region - 1,
 197     first_open_archive_heap_region = last_closed_archive_heap_region + 1,
 198     max_open_archive_heap_region = 2,
 199     last_open_archive_heap_region = first_open_archive_heap_region + max_open_archive_heap_region - 1,
 200 
 201     last_valid_region = last_open_archive_heap_region,
 202     n_regions =  last_valid_region + 1 // total number of regions
 203   };
 204 
 205   static void prepare_for_dumping() NOT_CDS_RETURN;
 206   static void preload_and_dump(TRAPS) NOT_CDS_RETURN;
 207   static int preload_classes(const char * class_list_path,
 208                              TRAPS) NOT_CDS_RETURN_(0);
 209 
 210   static GrowableArray<Klass*>* collected_klasses();
 211 
 212   static ReservedSpace* shared_rs() {
 213     CDS_ONLY(return &_shared_rs);
 214     NOT_CDS(return NULL);
 215   }
 216   static void commit_shared_space_to(char* newtop) NOT_CDS_RETURN;
 217   static size_t core_spaces_size() {
 218     assert(DumpSharedSpaces || UseSharedSpaces, "sanity");
 219     assert(_core_spaces_size != 0, "sanity");
 220     return _core_spaces_size;
 221   }
 222   static void initialize_dumptime_shared_and_meta_spaces() NOT_CDS_RETURN;
 223   static void initialize_runtime_shared_and_meta_spaces() NOT_CDS_RETURN;
 224   static char* initialize_dynamic_runtime_shared_spaces(
 225                      char* static_start, char* static_end) NOT_CDS_RETURN_(NULL);
 226   static void post_initialize(TRAPS) NOT_CDS_RETURN;
 227 
 228   // Delta of this object from SharedBaseAddress
 229   static uintx object_delta_uintx(void* obj);
 230 
 231   static u4 object_delta_u4(void* obj) {
 232     // offset is guaranteed to be less than MAX_SHARED_DELTA in DumpRegion::expand_top_to()
 233     uintx deltax = object_delta_uintx(obj);
 234     guarantee(deltax <= MAX_SHARED_DELTA, "must be 32-bit offset");
 235     return (u4)deltax;
 236   }
 237 
 238   static void set_archive_loading_failed() {
 239     _archive_loading_failed = true;
 240   }
 241   static bool map_shared_spaces(FileMapInfo* mapinfo) NOT_CDS_RETURN_(false);
 242   static void initialize_shared_spaces() NOT_CDS_RETURN;
 243 
 244   // Return true if given address is in the shared metaspace regions (i.e., excluding any
 245   // mapped shared heap regions.)
 246   static bool is_in_shared_metaspace(const void* p) {
 247     // If no shared metaspace regions are mapped, MetaspceObj::_shared_metaspace_{base,top} will
 248     // both be NULL and all values of p will be rejected quickly.
 249     return (p < MetaspaceObj::shared_metaspace_top() && p >= MetaspaceObj::shared_metaspace_base());
 250   }
 251 
 252   static address shared_metaspace_top() {
 253     return (address)MetaspaceObj::shared_metaspace_top();
 254   }
 255 
 256   static void set_shared_metaspace_range(void* base, void* top) NOT_CDS_RETURN;
 257 
 258   // Return true if given address is in the shared region corresponding to the idx
 259   static bool is_in_shared_region(const void* p, int idx) NOT_CDS_RETURN_(false);
 260 
 261   static bool is_in_trampoline_frame(address addr) NOT_CDS_RETURN_(false);
 262 
 263   static bool is_shared_dynamic(void* p) NOT_CDS_RETURN_(false);
 264 
 265   static void allocate_cpp_vtable_clones();
 266   static intptr_t* clone_cpp_vtables(intptr_t* p);
 267   static void zero_cpp_vtable_clones_for_writing();
 268   static void patch_cpp_vtable_pointers();
 269   static void serialize_cloned_cpp_vtptrs(SerializeClosure* sc);
 270 
 271   static bool is_valid_shared_method(const Method* m) NOT_CDS_RETURN_(false);
 272   static void serialize(SerializeClosure* sc) NOT_CDS_RETURN;
 273 
 274   static MetaspaceSharedStats* stats() {
 275     return &_stats;
 276   }
 277 
 278   static void report_out_of_space(const char* name, size_t needed_bytes);
 279 
 280   // JVM/TI RedefineClasses() support:
 281   // Remap the shared readonly space to shared readwrite, private if
 282   // sharing is enabled. Simply returns true if sharing is not enabled
 283   // or if the remapping has already been done by a prior call.
 284   static bool remap_shared_readonly_as_readwrite() NOT_CDS_RETURN_(true);
 285   static bool remapped_readwrite() {
 286     CDS_ONLY(return _remapped_readwrite);
 287     NOT_CDS(return false);
 288   }
 289 
 290   static bool try_link_class(InstanceKlass* ik, TRAPS);
 291   static void link_and_cleanup_shared_classes(TRAPS);
 292 
 293 #if INCLUDE_CDS
 294   static ReservedSpace* reserve_shared_rs(size_t size, size_t alignment,
 295                                           bool large, char* requested_address);
 296   static void init_shared_dump_space(DumpRegion* first_space, address first_space_bottom = NULL);
 297   static DumpRegion* misc_code_dump_space();
 298   static DumpRegion* read_write_dump_space();
 299   static DumpRegion* read_only_dump_space();
 300   static DumpRegion* optional_data_dump_space();
 301   static void pack_dump_space(DumpRegion* current, DumpRegion* next,
 302                               ReservedSpace* rs);
 303 
 304   static void rewrite_nofast_bytecodes_and_calculate_fingerprints(InstanceKlass* ik);
 305 #endif
 306 
 307   // Allocate a block of memory from the "mc", "ro", or "rw" regions.
 308   static char* misc_code_space_alloc(size_t num_bytes);
 309   static char* read_only_space_alloc(size_t num_bytes);
 310 
 311   template <typename T>
 312   static Array<T>* new_ro_array(int length) {
 313 #if INCLUDE_CDS
 314     size_t byte_size = Array<T>::byte_sizeof(length, sizeof(T));
 315     Array<T>* array = (Array<T>*)read_only_space_alloc(byte_size);
 316     array->initialize(length);
 317     return array;
 318 #else
 319     return NULL;
 320 #endif
 321   }
 322 
 323   template <typename T>
 324   static size_t ro_array_bytesize(int length) {
 325     size_t byte_size = Array<T>::byte_sizeof(length, sizeof(T));
 326     return align_up(byte_size, BytesPerWord);
 327   }
 328 
 329   static address cds_i2i_entry_code_buffers(size_t total_size);
 330 
 331   static address cds_i2i_entry_code_buffers() {
 332     return _cds_i2i_entry_code_buffers;
 333   }
 334   static size_t cds_i2i_entry_code_buffers_size() {
 335     return _cds_i2i_entry_code_buffers_size;
 336   }
 337   static void relocate_klass_ptr(oop o);
 338 
 339   static Klass* get_relocated_klass(Klass *k);
 340 
 341   static intptr_t* fix_cpp_vtable_for_dynamic_archive(MetaspaceObj::Type msotype, address obj);
 342 
 343 private:
 344   static void read_extra_data(const char* filename, TRAPS) NOT_CDS_RETURN;
 345 };
 346 #endif // SHARE_MEMORY_METASPACESHARED_HPP