1 /* 2 * Copyright (c) 2003, 2025, 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_CDS_FILEMAP_HPP 26 #define SHARE_CDS_FILEMAP_HPP 27 28 #include "cds/archiveUtils.hpp" 29 #include "cds/metaspaceShared.hpp" 30 #include "include/cds.h" 31 #include "logging/logLevel.hpp" 32 #include "memory/allocation.hpp" 33 #include "oops/array.hpp" 34 #include "oops/compressedOops.hpp" 35 #include "runtime/globals.hpp" 36 #include "utilities/align.hpp" 37 38 // To understand the layout of the CDS archive file: 39 // 40 // java -Xlog:cds+map=info:file=cds.map:none:filesize=0 41 // java -Xlog:cds+map=debug:file=cds.map:none:filesize=0 42 // java -Xlog:cds+map=trace:file=cds.map:none:filesize=0 43 44 static const int JVM_IDENT_MAX = 256; 45 46 class AOTClassLocationConfig; 47 class ArchiveHeapInfo; 48 class BitMapView; 49 class CHeapBitMap; 50 class ClassFileStream; 51 class ClassLoaderData; 52 class ClassPathEntry; 53 class outputStream; 54 class ReservedSpace; 55 56 class FileMapRegion: private CDSFileMapRegion { 57 public: 58 void assert_is_heap_region() const { 59 assert(_is_heap_region, "must be heap region"); 60 } 61 void assert_is_not_heap_region() const { 62 assert(!_is_heap_region, "must not be heap region"); 63 } 64 65 static FileMapRegion* cast(CDSFileMapRegion* p) { 66 return (FileMapRegion*)p; 67 } 68 69 // Accessors 70 int crc() const { return _crc; } 71 size_t file_offset() const { return _file_offset; } 72 size_t mapping_offset() const { return _mapping_offset; } 73 size_t mapping_end_offset() const { return _mapping_offset + used_aligned(); } 74 size_t used() const { return _used; } 75 size_t used_aligned() const; // aligned up to MetaspaceShared::core_region_alignment() 76 char* mapped_base() const { return _mapped_base; } 77 char* mapped_end() const { return mapped_base() + used_aligned(); } 78 bool read_only() const { return _read_only != 0; } 79 bool allow_exec() const { return _allow_exec != 0; } 80 bool mapped_from_file() const { return _mapped_from_file != 0; } 81 size_t oopmap_offset() const { assert_is_heap_region(); return _oopmap_offset; } 82 size_t oopmap_size_in_bits() const { assert_is_heap_region(); return _oopmap_size_in_bits; } 83 size_t ptrmap_offset() const { return _ptrmap_offset; } 84 size_t ptrmap_size_in_bits() const { return _ptrmap_size_in_bits; } 85 bool in_reserved_space() const { return _in_reserved_space; } 86 87 void set_file_offset(size_t s) { _file_offset = s; } 88 void set_read_only(bool v) { _read_only = v; } 89 void set_mapped_base(char* p) { _mapped_base = p; } 90 void set_mapped_from_file(bool v) { _mapped_from_file = v; } 91 void set_in_reserved_space(bool is_reserved) { _in_reserved_space = is_reserved; } 92 void init(int region_index, size_t mapping_offset, size_t size, bool read_only, 93 bool allow_exec, int crc); 94 void init_oopmap(size_t offset, size_t size_in_bits); 95 void init_ptrmap(size_t offset, size_t size_in_bits); 96 bool has_ptrmap() { return _ptrmap_size_in_bits != 0; } 97 98 bool check_region_crc(char* base) const; 99 void print(outputStream* st, int region_index); 100 }; 101 102 #define CDS_MUST_MATCH_FLAGS_DO(f) \ 103 f(EnableValhalla) \ 104 f(UseArrayFlattening) \ 105 f(UseFieldFlattening) \ 106 f(InlineTypePassFieldsAsArgs) \ 107 f(InlineTypeReturnedAsFields) \ 108 f(UseNonAtomicValueFlattening) \ 109 f(UseAtomicValueFlattening) \ 110 f(UseNullableValueFlattening) 111 112 113 class CDSMustMatchFlags { 114 private: 115 size_t _max_name_width; 116 #define DECLARE_CDS_MUST_MATCH_FLAG(n) \ 117 decltype(n) _v_##n; 118 CDS_MUST_MATCH_FLAGS_DO(DECLARE_CDS_MUST_MATCH_FLAG); 119 #undef DECLARE_CDS_MUST_MATCH_FLAG 120 121 inline static void do_print(outputStream* st, bool v); 122 inline static void do_print(outputStream* st, intx v); 123 inline static void do_print(outputStream* st, uintx v); 124 inline static void do_print(outputStream* st, double v); 125 void print_info() const; 126 127 public: 128 void init(); 129 bool runtime_check() const; 130 void print(outputStream* st) const; 131 }; 132 133 class FileMapHeader: private CDSFileMapHeaderBase { 134 friend class CDSConstants; 135 friend class VMStructs; 136 137 private: 138 // The following fields record the states of the VM during dump time. 139 // They are compared with the runtime states to see if the archive 140 // can be used. 141 size_t _core_region_alignment; // how shared archive should be aligned 142 int _obj_alignment; // value of ObjectAlignmentInBytes 143 address _narrow_oop_base; // compressed oop encoding base 144 int _narrow_oop_shift; // compressed oop encoding shift 145 bool _compact_strings; // value of CompactStrings 146 bool _compact_headers; // value of UseCompactObjectHeaders 147 uintx _max_heap_size; // java max heap size during dumping 148 CompressedOops::Mode _narrow_oop_mode; // compressed oop encoding mode 149 bool _compressed_oops; // save the flag UseCompressedOops 150 bool _compressed_class_ptrs; // save the flag UseCompressedClassPointers 151 int _narrow_klass_pointer_bits; // save number of bits in narrowKlass 152 int _narrow_klass_shift; // save shift width used to pre-compute narrowKlass IDs in archived heap objects 153 size_t _cloned_vtables_offset; // The address of the first cloned vtable 154 size_t _early_serialized_data_offset; // Data accessed using {ReadClosure,WriteClosure}::serialize() 155 size_t _serialized_data_offset; // Data accessed using {ReadClosure,WriteClosure}::serialize() 156 157 // The following fields are all sanity checks for whether this archive 158 // will function correctly with this JVM and the bootclasspath it's 159 // invoked with. 160 char _jvm_ident[JVM_IDENT_MAX]; // identifier string of the jvm that created this dump 161 162 size_t _class_location_config_offset; 163 164 bool _verify_local; // BytecodeVerificationLocal setting 165 bool _verify_remote; // BytecodeVerificationRemote setting 166 bool _has_platform_or_app_classes; // Archive contains app or platform classes 167 char* _requested_base_address; // Archive relocation is not necessary if we map with this base address. 168 char* _mapped_base_address; // Actual base address where archive is mapped. 169 170 bool _allow_archiving_with_java_agent; // setting of the AllowArchivingWithJavaAgent option 171 bool _use_optimized_module_handling;// No module-relation VM options were specified, so we can skip 172 // some expensive operations. 173 bool _has_aot_linked_classes; // Was the CDS archive created with -XX:+AOTClassLinking 174 bool _has_full_module_graph; // Does this CDS archive contain the full archived module graph? 175 bool _has_valhalla_patched_classes; // Is this archived dumped with --enable-preview -XX:+EnableValhalla? 176 CDSMustMatchFlags _must_match; // These flags must be the same between dumptime and runtime 177 HeapRootSegments _heap_root_segments; // Heap root segments info 178 size_t _heap_oopmap_start_pos; // The first bit in the oopmap corresponds to this position in the heap. 179 size_t _heap_ptrmap_start_pos; // The first bit in the ptrmap corresponds to this position in the heap. 180 size_t _rw_ptrmap_start_pos; // The first bit in the ptrmap corresponds to this position in the rw region 181 size_t _ro_ptrmap_start_pos; // The first bit in the ptrmap corresponds to this position in the ro region 182 183 // The following are parameters that affect MethodData layout. 184 u1 _compiler_type; 185 uint _type_profile_level; 186 int _type_profile_args_limit; 187 int _type_profile_parms_limit; 188 intx _type_profile_width; 189 intx _bci_profile_width; 190 bool _profile_traps; 191 bool _type_profile_casts; 192 int _spec_trap_limit_extra_entries; 193 194 template <typename T> T from_mapped_offset(size_t offset) const { 195 return (T)(mapped_base_address() + offset); 196 } 197 void set_as_offset(char* p, size_t *offset); 198 template <typename T> void set_as_offset(T p, size_t *offset) { 199 set_as_offset((char*)p, offset); 200 } 201 202 public: 203 // Accessors -- fields declared in GenericCDSFileMapHeader 204 unsigned int magic() const { return _generic_header._magic; } 205 int crc() const { return _generic_header._crc; } 206 int version() const { return _generic_header._version; } 207 unsigned int header_size() const { return _generic_header._header_size; } 208 unsigned int base_archive_name_offset() const { return _generic_header._base_archive_name_offset; } 209 unsigned int base_archive_name_size() const { return _generic_header._base_archive_name_size; } 210 211 void set_magic(unsigned int m) { _generic_header._magic = m; } 212 void set_crc(int crc_value) { _generic_header._crc = crc_value; } 213 void set_version(int v) { _generic_header._version = v; } 214 void set_header_size(unsigned int s) { _generic_header._header_size = s; } 215 void set_base_archive_name_offset(unsigned int s) { _generic_header._base_archive_name_offset = s; } 216 void set_base_archive_name_size(unsigned int s) { _generic_header._base_archive_name_size = s; } 217 218 bool is_static() const { return magic() == CDS_ARCHIVE_MAGIC; } 219 size_t core_region_alignment() const { return _core_region_alignment; } 220 int obj_alignment() const { return _obj_alignment; } 221 address narrow_oop_base() const { return _narrow_oop_base; } 222 int narrow_oop_shift() const { return _narrow_oop_shift; } 223 bool compact_strings() const { return _compact_strings; } 224 bool compact_headers() const { return _compact_headers; } 225 uintx max_heap_size() const { return _max_heap_size; } 226 CompressedOops::Mode narrow_oop_mode() const { return _narrow_oop_mode; } 227 char* cloned_vtables() const { return from_mapped_offset<char*>(_cloned_vtables_offset); } 228 char* early_serialized_data() const { return from_mapped_offset<char*>(_early_serialized_data_offset); } 229 char* serialized_data() const { return from_mapped_offset<char*>(_serialized_data_offset); } 230 const char* jvm_ident() const { return _jvm_ident; } 231 char* requested_base_address() const { return _requested_base_address; } 232 char* mapped_base_address() const { return _mapped_base_address; } 233 bool has_platform_or_app_classes() const { return _has_platform_or_app_classes; } 234 bool has_aot_linked_classes() const { return _has_aot_linked_classes; } 235 bool compressed_oops() const { return _compressed_oops; } 236 bool compressed_class_pointers() const { return _compressed_class_ptrs; } 237 int narrow_klass_pointer_bits() const { return _narrow_klass_pointer_bits; } 238 int narrow_klass_shift() const { return _narrow_klass_shift; } 239 HeapRootSegments heap_root_segments() const { return _heap_root_segments; } 240 bool has_full_module_graph() const { return _has_full_module_graph; } 241 size_t heap_oopmap_start_pos() const { return _heap_oopmap_start_pos; } 242 size_t heap_ptrmap_start_pos() const { return _heap_ptrmap_start_pos; } 243 size_t rw_ptrmap_start_pos() const { return _rw_ptrmap_start_pos; } 244 size_t ro_ptrmap_start_pos() const { return _ro_ptrmap_start_pos; } 245 246 void set_has_platform_or_app_classes(bool v) { _has_platform_or_app_classes = v; } 247 void set_cloned_vtables(char* p) { set_as_offset(p, &_cloned_vtables_offset); } 248 void set_early_serialized_data(char* p) { set_as_offset(p, &_early_serialized_data_offset); } 249 void set_serialized_data(char* p) { set_as_offset(p, &_serialized_data_offset); } 250 void set_mapped_base_address(char* p) { _mapped_base_address = p; } 251 void set_heap_root_segments(HeapRootSegments segments) { _heap_root_segments = segments; } 252 void set_heap_oopmap_start_pos(size_t n) { _heap_oopmap_start_pos = n; } 253 void set_heap_ptrmap_start_pos(size_t n) { _heap_ptrmap_start_pos = n; } 254 void set_rw_ptrmap_start_pos(size_t n) { _rw_ptrmap_start_pos = n; } 255 void set_ro_ptrmap_start_pos(size_t n) { _ro_ptrmap_start_pos = n; } 256 void copy_base_archive_name(const char* name); 257 258 void set_class_location_config(AOTClassLocationConfig* table) { 259 set_as_offset(table, &_class_location_config_offset); 260 } 261 262 AOTClassLocationConfig* class_location_config() { 263 return from_mapped_offset<AOTClassLocationConfig*>(_class_location_config_offset); 264 } 265 266 void set_requested_base(char* b) { 267 _requested_base_address = b; 268 _mapped_base_address = nullptr; 269 } 270 271 bool validate(); 272 int compute_crc(); 273 274 FileMapRegion* region_at(int i) { 275 assert(is_valid_region(i), "invalid region"); 276 return FileMapRegion::cast(&_regions[i]); 277 } 278 279 void populate(FileMapInfo *info, size_t core_region_alignment, size_t header_size, 280 size_t base_archive_name_size, size_t base_archive_name_offset); 281 static bool is_valid_region(int region) { 282 return (0 <= region && region < NUM_CDS_REGIONS); 283 } 284 285 bool check_must_match_flags() const { 286 return _must_match.runtime_check(); 287 } 288 289 void print(outputStream* st); 290 }; 291 292 class FileMapInfo : public CHeapObj<mtInternal> { 293 private: 294 friend class ManifestStream; 295 friend class VMStructs; 296 friend class ArchiveBuilder; 297 friend class CDSOffsets; 298 friend class FileMapHeader; 299 300 bool _is_static; 301 bool _file_open; 302 bool _is_mapped; 303 int _fd; 304 size_t _file_offset; 305 const char* _full_path; 306 const char* _base_archive_name; 307 FileMapHeader* _header; 308 309 // FileMapHeader describes the shared space data in the file to be 310 // mapped. This structure gets written to a file. It is not a class, so 311 // that the compilers don't add any compiler-private data to it. 312 313 static FileMapInfo* _current_info; 314 static FileMapInfo* _dynamic_archive_info; 315 static bool _heap_pointers_need_patching; 316 static bool _memory_mapping_failed; 317 318 public: 319 FileMapHeader *header() const { return _header; } 320 static bool get_base_archive_name_from_header(const char* archive_name, 321 const char** base_archive_name); 322 static bool is_preimage_static_archive(const char* file); 323 324 bool init_from_file(int fd); 325 326 void log_paths(const char* msg, int start_idx, int end_idx); 327 328 FileMapInfo(const char* full_apth, bool is_static); 329 ~FileMapInfo(); 330 static void free_current_info(); 331 332 // Accessors 333 int compute_header_crc() const { return header()->compute_crc(); } 334 void set_header_crc(int crc) { header()->set_crc(crc); } 335 int region_crc(int i) const { return region_at(i)->crc(); } 336 void populate_header(size_t core_region_alignment); 337 bool validate_header(); 338 void invalidate(); 339 int crc() const { return header()->crc(); } 340 int version() const { return header()->version(); } 341 unsigned int magic() const { return header()->magic(); } 342 address narrow_oop_base() const { return header()->narrow_oop_base(); } 343 int narrow_oop_shift() const { return header()->narrow_oop_shift(); } 344 uintx max_heap_size() const { return header()->max_heap_size(); } 345 HeapRootSegments heap_root_segments() const { return header()->heap_root_segments(); } 346 size_t core_region_alignment() const { return header()->core_region_alignment(); } 347 size_t heap_oopmap_start_pos() const { return header()->heap_oopmap_start_pos(); } 348 size_t heap_ptrmap_start_pos() const { return header()->heap_ptrmap_start_pos(); } 349 350 CompressedOops::Mode narrow_oop_mode() const { return header()->narrow_oop_mode(); } 351 352 char* cloned_vtables() const { return header()->cloned_vtables(); } 353 void set_cloned_vtables(char* p) const { header()->set_cloned_vtables(p); } 354 char* early_serialized_data() const { return header()->early_serialized_data(); } 355 void set_early_serialized_data(char* p) const { header()->set_early_serialized_data(p); } 356 char* serialized_data() const { return header()->serialized_data(); } 357 void set_serialized_data(char* p) const { header()->set_serialized_data(p); } 358 359 bool is_file_position_aligned() const; 360 void align_file_position(); 361 362 bool is_static() const { return _is_static; } 363 bool is_mapped() const { return _is_mapped; } 364 void set_is_mapped(bool v) { _is_mapped = v; } 365 const char* full_path() const { return _full_path; } 366 367 void set_requested_base(char* b) { header()->set_requested_base(b); } 368 char* requested_base_address() const { return header()->requested_base_address(); } 369 370 class DynamicArchiveHeader* dynamic_header() const { 371 assert(!is_static(), "must be"); 372 return (DynamicArchiveHeader*)header(); 373 } 374 375 void set_has_platform_or_app_classes(bool v) { 376 header()->set_has_platform_or_app_classes(v); 377 } 378 bool has_platform_or_app_classes() const { 379 return header()->has_platform_or_app_classes(); 380 } 381 382 static FileMapInfo* current_info() { 383 CDS_ONLY(return _current_info;) 384 NOT_CDS(return nullptr;) 385 } 386 387 static void set_current_info(FileMapInfo* info) { 388 CDS_ONLY(_current_info = info;) 389 } 390 391 static FileMapInfo* dynamic_info() { 392 CDS_ONLY(return _dynamic_archive_info;) 393 NOT_CDS(return nullptr;) 394 } 395 396 static void assert_mark(bool check); 397 398 // File manipulation. 399 bool open_as_input() NOT_CDS_RETURN_(false); 400 void open_as_output(); 401 void write_header(); 402 void write_region(int region, char* base, size_t size, 403 bool read_only, bool allow_exec); 404 size_t remove_bitmap_zeros(CHeapBitMap* map); 405 char* write_bitmap_region(CHeapBitMap* rw_ptrmap, CHeapBitMap* ro_ptrmap, ArchiveHeapInfo* heap_info, 406 size_t &size_in_bytes); 407 size_t write_heap_region(ArchiveHeapInfo* heap_info); 408 void write_bytes(const void* buffer, size_t count); 409 void write_bytes_aligned(const void* buffer, size_t count); 410 size_t read_bytes(void* buffer, size_t count); 411 static size_t readonly_total(); 412 MapArchiveResult map_regions(int regions[], int num_regions, char* mapped_base_address, ReservedSpace rs); 413 void unmap_regions(int regions[], int num_regions); 414 void map_or_load_heap_region() NOT_CDS_JAVA_HEAP_RETURN; 415 void fixup_mapped_heap_region() NOT_CDS_JAVA_HEAP_RETURN; 416 void patch_heap_embedded_pointers() NOT_CDS_JAVA_HEAP_RETURN; 417 bool has_heap_region() NOT_CDS_JAVA_HEAP_RETURN_(false); 418 MemRegion get_heap_region_requested_range() NOT_CDS_JAVA_HEAP_RETURN_(MemRegion()); 419 bool read_region(int i, char* base, size_t size, bool do_commit); 420 char* map_bitmap_region(); 421 bool map_aot_code_region(ReservedSpace rs); 422 void unmap_region(int i); 423 void close(); 424 bool is_open() { return _file_open; } 425 426 // JVM/TI RedefineClasses() support: 427 // Remap the shared readonly space to shared readwrite, private. 428 bool remap_shared_readonly_as_readwrite(); 429 430 static bool memory_mapping_failed() { 431 CDS_ONLY(return _memory_mapping_failed;) 432 NOT_CDS(return false;) 433 } 434 435 bool validate_class_location(); 436 bool validate_aot_class_linking(); 437 438 #if INCLUDE_JVMTI 439 // Caller needs a ResourceMark because parts of the returned cfs are resource-allocated. 440 static ClassFileStream* open_stream_for_jvmti(InstanceKlass* ik, Handle class_loader, TRAPS); 441 static ClassFileStream* get_stream_from_class_loader(Handle class_loader, 442 ClassPathEntry* cpe, 443 const char* file_name, 444 TRAPS); 445 #endif 446 447 // The offset of the first core region in the archive, relative to SharedBaseAddress 448 size_t mapping_base_offset() const { return first_core_region()->mapping_offset(); } 449 // The offset of the (exclusive) end of the last core region in this archive, relative to SharedBaseAddress 450 size_t mapping_end_offset() const { return last_core_region()->mapping_end_offset(); } 451 452 char* mapped_base() const { return header()->mapped_base_address(); } 453 char* mapped_end() const { return last_core_region()->mapped_end(); } 454 455 // Non-zero if the archive needs to be mapped a non-default location due to ASLR. 456 intx relocation_delta() const { 457 return header()->mapped_base_address() - header()->requested_base_address(); 458 } 459 460 FileMapRegion* first_core_region() const; 461 FileMapRegion* last_core_region() const; 462 463 FileMapRegion* region_at(int i) const { 464 return header()->region_at(i); 465 } 466 467 BitMapView bitmap_view(int region_index, bool is_oopmap); 468 BitMapView oopmap_view(int region_index); 469 BitMapView ptrmap_view(int region_index); 470 471 void print(outputStream* st) const; 472 473 const char* vm_version() { 474 return header()->jvm_ident(); 475 } 476 477 private: 478 bool open_for_read(); 479 void seek_to_position(size_t pos); 480 bool map_heap_region_impl() NOT_CDS_JAVA_HEAP_RETURN_(false); 481 void dealloc_heap_region() NOT_CDS_JAVA_HEAP_RETURN; 482 bool can_use_heap_region(); 483 bool load_heap_region() NOT_CDS_JAVA_HEAP_RETURN_(false); 484 bool map_heap_region() NOT_CDS_JAVA_HEAP_RETURN_(false); 485 void init_heap_region_relocation(); 486 MapArchiveResult map_region(int i, intx addr_delta, char* mapped_base_address, ReservedSpace rs); 487 bool relocate_pointers_in_core_regions(intx addr_delta); 488 489 static MemRegion _mapped_heap_memregion; 490 491 public: 492 address heap_region_dumptime_address() NOT_CDS_JAVA_HEAP_RETURN_(nullptr); 493 address heap_region_requested_address() NOT_CDS_JAVA_HEAP_RETURN_(nullptr); 494 narrowOop encoded_heap_region_dumptime_address(); 495 496 private: 497 498 #if INCLUDE_JVMTI 499 static ClassPathEntry** _classpath_entries_for_jvmti; 500 static ClassPathEntry* get_classpath_entry_for_jvmti(int i, TRAPS); 501 #endif 502 }; 503 504 #endif // SHARE_CDS_FILEMAP_HPP