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