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