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