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 "utilities/align.hpp"
 36 
 37 // To understand the layout of the CDS archive file:
 38 //
 39 // java -Xlog:cds+map=info:file=cds.map:none:filesize=0
 40 // java -Xlog:cds+map=debug:file=cds.map:none:filesize=0
 41 // java -Xlog:cds+map=trace:file=cds.map:none:filesize=0
 42 
 43 static const int JVM_IDENT_MAX = 256;
 44 
 45 class ArchiveHeapInfo;
 46 class BitMapView;
 47 class CHeapBitMap;
 48 class ClassFileStream;
 49 class ClassLoaderData;
 50 class ClassPathEntry;
 51 class outputStream;
 52 class ReservedSpace;
 53 
 54 class SharedClassPathEntry : public MetaspaceObj {
 55   enum {
 56     modules_image_entry,
 57     jar_entry,
 58     dir_entry,
 59     non_existent_entry,
 60     unknown_entry
 61   };
 62 
 63   void set_name(const char* name, TRAPS);
 64 
 65   u1     _type;
 66   bool   _is_module_path;
 67   bool   _from_class_path_attr;
 68   bool   _is_multi_release;
 69   time_t _timestamp;          // jar timestamp,  0 if is directory, modules image or other
 70   int64_t      _filesize;     // jar/jimage file size, -1 if is directory, -2 if other
 71   Array<char>* _name;
 72   Array<u1>*   _manifest;
 73 
 74 public:
 75   SharedClassPathEntry() : _type(0), _is_module_path(false),
 76                            _from_class_path_attr(false), _is_multi_release(false), _timestamp(0),
 77                            _filesize(0), _name(nullptr), _manifest(nullptr) {}
 78   static int size() {
 79     static_assert(is_aligned(sizeof(SharedClassPathEntry), wordSize), "must be");
 80     return (int)(sizeof(SharedClassPathEntry) / wordSize);
 81   }
 82   void init(bool is_modules_image, bool is_module_path, ClassPathEntry* cpe, TRAPS);
 83   void init_as_non_existent(const char* path, TRAPS);
 84   void metaspace_pointers_do(MetaspaceClosure* it);
 85   MetaspaceObj::Type type() const { return SharedClassPathEntryType; }
 86   bool validate(bool is_class_path = true) const;
 87 
 88   // The _timestamp only gets set for jar files.
 89   bool has_timestamp() const {
 90     return _timestamp != 0;
 91   }
 92   bool is_dir()           const { return _type == dir_entry; }
 93   bool is_modules_image() const { return _type == modules_image_entry; }
 94   bool is_jar()           const { return _type == jar_entry; }
 95   bool is_non_existent()  const { return _type == non_existent_entry; }
 96   bool from_class_path_attr() { return _from_class_path_attr; }
 97   bool is_multi_release()     { return _is_multi_release; }
 98   time_t timestamp() const { return _timestamp; }
 99   const char* name() const;
100   const char* manifest() const {
101     return (_manifest == nullptr) ? nullptr : (const char*)_manifest->data();
102   }
103   int manifest_size() const {
104     return (_manifest == nullptr) ? 0 : _manifest->length();
105   }
106   void set_manifest(Array<u1>* manifest) {
107     _manifest = manifest;
108   }
109   bool check_non_existent() const;
110   void copy_from(SharedClassPathEntry* ent, ClassLoaderData* loader_data, TRAPS);
111   bool in_named_module() {
112     return is_modules_image() || // modules image doesn't contain unnamed modules
113            _is_module_path;      // module path doesn't contain unnamed modules
114   }
115 };
116 
117 class SharedPathTable {
118   Array<SharedClassPathEntry*>* _entries;
119 public:
120   SharedPathTable() : _entries(nullptr) {}
121   SharedPathTable(Array<SharedClassPathEntry*>* entries) : _entries(entries) {}
122 
123   void dumptime_init(ClassLoaderData* loader_data, TRAPS);
124   void metaspace_pointers_do(MetaspaceClosure* it);
125 
126   int size() {
127     return _entries == nullptr ? 0 : _entries->length();
128   }
129   SharedClassPathEntry* path_at(int index) {
130     return _entries->at(index);
131   }
132   Array<SharedClassPathEntry*>* table() {return _entries;}
133   void set_table(Array<SharedClassPathEntry*>* table) {_entries = table;}
134 };
135 
136 
137 class FileMapRegion: private CDSFileMapRegion {
138 public:
139   void assert_is_heap_region() const {
140     assert(_is_heap_region, "must be heap region");
141   }
142   void assert_is_not_heap_region() const {
143     assert(!_is_heap_region, "must not be heap region");
144   }
145 
146   static FileMapRegion* cast(CDSFileMapRegion* p) {
147     return (FileMapRegion*)p;
148   }
149 
150   // Accessors
151   int crc()                         const { return _crc; }
152   size_t file_offset()              const { return _file_offset; }
153   size_t mapping_offset()           const { return _mapping_offset; }
154   size_t mapping_end_offset()       const { return _mapping_offset + used_aligned(); }
155   size_t used()                     const { return _used; }
156   size_t used_aligned()             const; // aligned up to MetaspaceShared::core_region_alignment()
157   char*  mapped_base()              const { return _mapped_base; }
158   char*  mapped_end()               const { return mapped_base()        + used_aligned(); }
159   bool   read_only()                const { return _read_only != 0; }
160   bool   allow_exec()               const { return _allow_exec != 0; }
161   bool   mapped_from_file()         const { return _mapped_from_file != 0; }
162   size_t oopmap_offset()            const { assert_is_heap_region();     return _oopmap_offset; }
163   size_t oopmap_size_in_bits()      const { assert_is_heap_region();     return _oopmap_size_in_bits; }
164   size_t ptrmap_offset()            const { return _ptrmap_offset; }
165   size_t ptrmap_size_in_bits()      const { return _ptrmap_size_in_bits; }
166   bool   in_reserved_space()        const { return _in_reserved_space; }
167 
168   void set_file_offset(size_t s)     { _file_offset = s; }
169   void set_read_only(bool v)         { _read_only = v; }
170   void set_mapped_base(char* p)      { _mapped_base = p; }
171   void set_mapped_from_file(bool v)  { _mapped_from_file = v; }
172   void set_in_reserved_space(bool is_reserved) { _in_reserved_space = is_reserved; }
173   void init(int region_index, size_t mapping_offset, size_t size, bool read_only,
174             bool allow_exec, int crc);
175   void init_oopmap(size_t offset, size_t size_in_bits);
176   void init_ptrmap(size_t offset, size_t size_in_bits);
177   bool has_ptrmap()                  { return _ptrmap_size_in_bits != 0; }
178 
179   bool check_region_crc(char* base) const;
180   void print(outputStream* st, int region_index);
181 };
182 
183 class FileMapHeader: private CDSFileMapHeaderBase {
184   friend class CDSConstants;
185   friend class VMStructs;
186 
187 private:
188   // The following fields record the states of the VM during dump time.
189   // They are compared with the runtime states to see if the archive
190   // can be used.
191   size_t _core_region_alignment;                  // how shared archive should be aligned
192   int    _obj_alignment;                          // value of ObjectAlignmentInBytes
193   address _narrow_oop_base;                       // compressed oop encoding base
194   int    _narrow_oop_shift;                       // compressed oop encoding shift
195   bool   _compact_strings;                        // value of CompactStrings
196   bool   _compact_headers;                        // value of UseCompactObjectHeaders
197   uintx  _max_heap_size;                          // java max heap size during dumping
198   CompressedOops::Mode _narrow_oop_mode;          // compressed oop encoding mode
199   bool    _compressed_oops;                       // save the flag UseCompressedOops
200   bool    _compressed_class_ptrs;                 // save the flag UseCompressedClassPointers
201   int     _narrow_klass_pointer_bits;             // save number of bits in narrowKlass
202   int     _narrow_klass_shift;                    // save shift width used to pre-compute narrowKlass IDs in archived heap objects
203   size_t  _cloned_vtables_offset;                 // The address of the first cloned vtable
204   size_t  _early_serialized_data_offset;          // Data accessed using {ReadClosure,WriteClosure}::serialize()
205   size_t  _serialized_data_offset;                // Data accessed using {ReadClosure,WriteClosure}::serialize()
206   bool _has_non_jar_in_classpath;                 // non-jar file entry exists in classpath
207   unsigned int _common_app_classpath_prefix_size; // size of the common prefix of app class paths
208                                                   //    0 if no common prefix exists
209 
210   // The following fields are all sanity checks for whether this archive
211   // will function correctly with this JVM and the bootclasspath it's
212   // invoked with.
213   char  _jvm_ident[JVM_IDENT_MAX];  // identifier string of the jvm that created this dump
214 
215   // The following is a table of all the boot/app/module path entries that were used
216   // during dumping. At run time, we validate these entries according to their
217   // SharedClassPathEntry::_type. See:
218   //      check_nonempty_dir_in_shared_path_table()
219   //      validate_shared_path_table()
220   //      validate_non_existent_class_paths()
221   size_t _shared_path_table_offset;
222 
223   jshort _app_class_paths_start_index;  // Index of first app classpath entry
224   jshort _app_module_paths_start_index; // Index of first module path entry
225   jshort _max_used_path_index;          // max path index referenced during CDS dump
226   int    _num_module_paths;             // number of module path entries
227   bool   _verify_local;                 // BytecodeVerificationLocal setting
228   bool   _verify_remote;                // BytecodeVerificationRemote setting
229   bool   _has_platform_or_app_classes;  // Archive contains app classes
230   char*  _requested_base_address;       // Archive relocation is not necessary if we map with this base address.
231   char*  _mapped_base_address;          // Actual base address where archive is mapped.
232 
233   bool   _allow_archiving_with_java_agent; // setting of the AllowArchivingWithJavaAgent option
234   bool   _use_optimized_module_handling;// No module-relation VM options were specified, so we can skip
235                                         // some expensive operations.
236   bool   _has_aot_linked_classes;       // Was the CDS archive created with -XX:+AOTClassLinking
237   bool   _has_full_module_graph;        // Does this CDS archive contain the full archived module graph?
238   bool   _has_archived_invokedynamic;   // Does the archive have aot-linked invokedynamic CP entries?
239   bool   _has_archived_packages;
240   bool   _has_archived_protection_domains;
241   int    _gc_kind;                      // Universe::heap()->kind();
242   char   _gc_name[32];                  // Universe::heap()->name();
243   size_t _ptrmap_size_in_bits;          // Size of pointer relocation bitmap
244   HeapRootSegments _heap_root_segments; // Heap root segments info
245   size_t _heap_oopmap_start_pos;        // The first bit in the oopmap corresponds to this position in the heap.
246   size_t _heap_ptrmap_start_pos;        // The first bit in the ptrmap corresponds to this position in the heap.
247   size_t _rw_ptrmap_start_pos;          // The first bit in the ptrmap corresponds to this position in the rw region
248   size_t _ro_ptrmap_start_pos;          // The first bit in the ptrmap corresponds to this position in the ro region
249   char* from_mapped_offset(size_t offset) const {
250     return mapped_base_address() + offset;
251   }
252   void set_as_offset(char* p, size_t *offset);
253 public:
254   // Accessors -- fields declared in GenericCDSFileMapHeader
255   unsigned int magic()                            const { return _generic_header._magic;                    }
256   int crc()                                       const { return _generic_header._crc;                      }
257   int version()                                   const { return _generic_header._version;                  }
258   unsigned int header_size()                      const { return _generic_header._header_size;              }
259   unsigned int base_archive_name_offset()         const { return _generic_header._base_archive_name_offset; }
260   unsigned int base_archive_name_size()           const { return _generic_header._base_archive_name_size;   }
261   unsigned int common_app_classpath_prefix_size() const { return _common_app_classpath_prefix_size;         }
262 
263   void set_magic(unsigned int m)                            { _generic_header._magic = m;                    }
264   void set_crc(int crc_value)                               { _generic_header._crc = crc_value;              }
265   void set_version(int v)                                   { _generic_header._version = v;                  }
266   void set_header_size(unsigned int s)                      { _generic_header._header_size = s;              }
267   void set_base_archive_name_offset(unsigned int s)         { _generic_header._base_archive_name_offset = s; }
268   void set_base_archive_name_size(unsigned int s)           { _generic_header._base_archive_name_size = s;   }
269   void set_common_app_classpath_prefix_size(unsigned int s) { _common_app_classpath_prefix_size = s;         }
270 
271   bool is_static()                         const { return magic() == CDS_ARCHIVE_MAGIC; }
272   size_t core_region_alignment()           const { return _core_region_alignment; }
273   int obj_alignment()                      const { return _obj_alignment; }
274   address narrow_oop_base()                const { return _narrow_oop_base; }
275   int narrow_oop_shift()                   const { return _narrow_oop_shift; }
276   bool compact_strings()                   const { return _compact_strings; }
277   bool compact_headers()                   const { return _compact_headers; }
278   uintx max_heap_size()                    const { return _max_heap_size; }
279   CompressedOops::Mode narrow_oop_mode()   const { return _narrow_oop_mode; }
280   char* cloned_vtables()                   const { return from_mapped_offset(_cloned_vtables_offset); }
281   char* early_serialized_data()            const { return from_mapped_offset(_early_serialized_data_offset); }
282   char* serialized_data()                  const { return from_mapped_offset(_serialized_data_offset); }
283   const char* jvm_ident()                  const { return _jvm_ident; }
284   char* requested_base_address()           const { return _requested_base_address; }
285   char* mapped_base_address()              const { return _mapped_base_address; }
286   bool has_platform_or_app_classes()       const { return _has_platform_or_app_classes; }
287   bool has_non_jar_in_classpath()          const { return _has_non_jar_in_classpath; }
288   bool has_aot_linked_classes()            const { return _has_aot_linked_classes; }
289   int gc_kind()                            const { return _gc_kind; }
290   const char* gc_name()                    const { return _gc_name; }
291   size_t ptrmap_size_in_bits()             const { return _ptrmap_size_in_bits; }
292   bool compressed_oops()                   const { return _compressed_oops; }
293   bool compressed_class_pointers()         const { return _compressed_class_ptrs; }
294   int narrow_klass_pointer_bits()          const { return _narrow_klass_pointer_bits; }
295   int narrow_klass_shift()                 const { return _narrow_klass_shift; }
296   HeapRootSegments heap_root_segments()    const { return _heap_root_segments; }
297   bool has_full_module_graph()             const { return _has_full_module_graph; }
298   size_t heap_oopmap_start_pos()           const { return _heap_oopmap_start_pos; }
299   size_t heap_ptrmap_start_pos()           const { return _heap_ptrmap_start_pos; }
300   size_t rw_ptrmap_start_pos()             const { return _rw_ptrmap_start_pos; }
301   size_t ro_ptrmap_start_pos()             const { return _ro_ptrmap_start_pos; }
302   // FIXME: These should really return int
303   jshort max_used_path_index()             const { return _max_used_path_index; }
304   jshort app_module_paths_start_index()    const { return _app_module_paths_start_index; }
305   jshort app_class_paths_start_index()     const { return _app_class_paths_start_index; }
306   int    num_module_paths()                const { return _num_module_paths; }
307 
308   void set_has_platform_or_app_classes(bool v)   { _has_platform_or_app_classes = v; }
309   void set_cloned_vtables(char* p)               { set_as_offset(p, &_cloned_vtables_offset); }
310   void set_early_serialized_data(char* p)        { set_as_offset(p, &_early_serialized_data_offset); }
311   void set_serialized_data(char* p)              { set_as_offset(p, &_serialized_data_offset); }
312   void set_mapped_base_address(char* p)          { _mapped_base_address = p; }
313   void set_heap_root_segments(HeapRootSegments segments) { _heap_root_segments = segments; }
314   void set_heap_oopmap_start_pos(size_t n)       { _heap_oopmap_start_pos = n; }
315   void set_heap_ptrmap_start_pos(size_t n)       { _heap_ptrmap_start_pos = n; }
316   void set_rw_ptrmap_start_pos(size_t n)         { _rw_ptrmap_start_pos = n; }
317   void set_ro_ptrmap_start_pos(size_t n)         { _ro_ptrmap_start_pos = n; }
318   void copy_base_archive_name(const char* name);
319 
320   void set_shared_path_table(SharedPathTable table) {
321     set_as_offset((char*)table.table(), &_shared_path_table_offset);
322   }
323 
324   void set_requested_base(char* b) {
325     _requested_base_address = b;
326     _mapped_base_address = nullptr;
327   }
328 
329   SharedPathTable shared_path_table() const {
330     return SharedPathTable((Array<SharedClassPathEntry*>*)
331                            from_mapped_offset(_shared_path_table_offset));
332   }
333 
334   bool validate();
335   int compute_crc();
336 
337   FileMapRegion* region_at(int i) {
338     assert(is_valid_region(i), "invalid region");
339     return FileMapRegion::cast(&_regions[i]);
340   }
341 
342   void populate(FileMapInfo *info, size_t core_region_alignment, size_t header_size,
343                 size_t base_archive_name_size, size_t base_archive_name_offset,
344                 size_t common_app_classpath_size);
345   static bool is_valid_region(int region) {
346     return (0 <= region && region < NUM_CDS_REGIONS);
347   }
348 
349   void print(outputStream* st);
350 };
351 
352 class FileMapInfo : public CHeapObj<mtInternal> {
353 private:
354   friend class ManifestStream;
355   friend class VMStructs;
356   friend class ArchiveBuilder;
357   friend class CDSOffsets;
358   friend class FileMapHeader;
359 
360   bool           _is_static;
361   bool           _file_open;
362   bool           _is_mapped;
363   int            _fd;
364   size_t         _file_offset;
365   const char*    _full_path;
366   const char*    _base_archive_name;
367   FileMapHeader* _header;
368 
369   static SharedPathTable       _shared_path_table;
370   static bool                  _validating_shared_path_table;
371 
372   // FileMapHeader describes the shared space data in the file to be
373   // mapped.  This structure gets written to a file.  It is not a class, so
374   // that the compilers don't add any compiler-private data to it.
375 
376   static FileMapInfo* _current_info;
377   static FileMapInfo* _dynamic_archive_info;
378   static bool _heap_pointers_need_patching;
379   static bool _memory_mapping_failed;
380   static GrowableArray<const char*>* _non_existent_class_paths;
381 
382 public:
383   FileMapHeader *header() const       { return _header; }
384   static bool get_base_archive_name_from_header(const char* archive_name,
385                                                 char** base_archive_name);
386   static SharedPathTable shared_path_table() {
387     return _shared_path_table;
388   }
389 
390   bool init_from_file(int fd);
391   static void metaspace_pointers_do(MetaspaceClosure* it) {
392     _shared_path_table.metaspace_pointers_do(it);
393   }
394 
395   void log_paths(const char* msg, int start_idx, int end_idx);
396 
397   FileMapInfo(const char* full_apth, bool is_static);
398   ~FileMapInfo();
399 
400   // Accessors
401   int    compute_header_crc()  const { return header()->compute_crc(); }
402   void   set_header_crc(int crc)     { header()->set_crc(crc); }
403   int    region_crc(int i)     const { return region_at(i)->crc(); }
404   void   populate_header(size_t core_region_alignment);
405   bool   validate_header();
406   void   invalidate();
407   int    crc()                 const { return header()->crc(); }
408   int    version()             const { return header()->version(); }
409   unsigned int magic()         const { return header()->magic(); }
410   address narrow_oop_base()    const { return header()->narrow_oop_base(); }
411   int     narrow_oop_shift()   const { return header()->narrow_oop_shift(); }
412   uintx   max_heap_size()      const { return header()->max_heap_size(); }
413   HeapRootSegments heap_root_segments() const { return header()->heap_root_segments(); }
414   size_t  core_region_alignment() const { return header()->core_region_alignment(); }
415   size_t  heap_oopmap_start_pos() const { return header()->heap_oopmap_start_pos(); }
416   size_t  heap_ptrmap_start_pos() const { return header()->heap_ptrmap_start_pos(); }
417 
418   CompressedOops::Mode narrow_oop_mode()      const { return header()->narrow_oop_mode(); }
419   jshort app_module_paths_start_index()       const { return header()->app_module_paths_start_index(); }
420   jshort app_class_paths_start_index()        const { return header()->app_class_paths_start_index(); }
421 
422   char* cloned_vtables()                      const { return header()->cloned_vtables(); }
423   void  set_cloned_vtables(char* p)           const { header()->set_cloned_vtables(p); }
424   char* early_serialized_data()               const { return header()->early_serialized_data(); }
425   void  set_early_serialized_data(char* p)    const { header()->set_early_serialized_data(p); }
426   char* serialized_data()                     const { return header()->serialized_data(); }
427   void  set_serialized_data(char* p)          const { header()->set_serialized_data(p); }
428 
429   bool  is_file_position_aligned() const;
430   void  align_file_position();
431 
432   bool is_static()                            const { return _is_static; }
433   bool is_mapped()                            const { return _is_mapped; }
434   void set_is_mapped(bool v)                        { _is_mapped = v; }
435   const char* full_path()                     const { return _full_path; }
436 
437   void set_requested_base(char* b)                  { header()->set_requested_base(b); }
438   char* requested_base_address()           const    { return header()->requested_base_address(); }
439 
440   class DynamicArchiveHeader* dynamic_header() const {
441     assert(!is_static(), "must be");
442     return (DynamicArchiveHeader*)header();
443   }
444 
445   void set_has_platform_or_app_classes(bool v) {
446     header()->set_has_platform_or_app_classes(v);
447   }
448   bool has_platform_or_app_classes() const {
449     return header()->has_platform_or_app_classes();
450   }
451 
452   static FileMapInfo* current_info() {
453     CDS_ONLY(return _current_info;)
454     NOT_CDS(return nullptr;)
455   }
456 
457   static void set_current_info(FileMapInfo* info) {
458     CDS_ONLY(_current_info = info;)
459   }
460 
461   static FileMapInfo* dynamic_info() {
462     CDS_ONLY(return _dynamic_archive_info;)
463     NOT_CDS(return nullptr;)
464   }
465 
466   static void assert_mark(bool check);
467 
468   // File manipulation.
469   bool  initialize() NOT_CDS_RETURN_(false);
470   bool  open_for_read();
471   void  open_for_write();
472   void  write_header();
473   void  write_region(int region, char* base, size_t size,
474                      bool read_only, bool allow_exec);
475   size_t remove_bitmap_zeros(CHeapBitMap* map);
476   char* write_bitmap_region(CHeapBitMap* rw_ptrmap, CHeapBitMap* ro_ptrmap,
477                             CHeapBitMap* cc_ptrmap, ArchiveHeapInfo* heap_info,
478                             size_t &size_in_bytes);
479   size_t write_heap_region(ArchiveHeapInfo* heap_info);
480   void  write_bytes(const void* buffer, size_t count);
481   void  write_bytes_aligned(const void* buffer, size_t count);
482   size_t  read_bytes(void* buffer, size_t count);
483   static size_t readonly_total();
484   MapArchiveResult map_regions(int regions[], int num_regions, char* mapped_base_address, ReservedSpace rs);
485   void  unmap_regions(int regions[], int num_regions);
486   void  map_or_load_heap_region() NOT_CDS_JAVA_HEAP_RETURN;
487   void  fixup_mapped_heap_region() NOT_CDS_JAVA_HEAP_RETURN;
488   void  patch_heap_embedded_pointers() NOT_CDS_JAVA_HEAP_RETURN;
489   bool  has_heap_region()  NOT_CDS_JAVA_HEAP_RETURN_(false);
490   MemRegion get_heap_region_requested_range() NOT_CDS_JAVA_HEAP_RETURN_(MemRegion());
491   bool  read_region(int i, char* base, size_t size, bool do_commit);
492   char* map_bitmap_region();
493   bool map_cached_code_region(ReservedSpace rs);
494   void  unmap_region(int i);
495   void  close();
496   bool  is_open() { return _file_open; }
497 
498   // JVM/TI RedefineClasses() support:
499   // Remap the shared readonly space to shared readwrite, private.
500   bool  remap_shared_readonly_as_readwrite();
501 
502   static bool memory_mapping_failed() {
503     CDS_ONLY(return _memory_mapping_failed;)
504     NOT_CDS(return false;)
505   }
506 
507   static void allocate_shared_path_table(TRAPS);
508   static int add_shared_classpaths(int i, const char* which, ClassPathEntry *cpe, TRAPS);
509   static void check_nonempty_dir_in_shared_path_table();
510   bool check_module_paths();
511   bool validate_shared_path_table();
512   bool validate_non_existent_class_paths();
513   bool validate_aot_class_linking();
514   static void set_shared_path_table(FileMapInfo* info) {
515     _shared_path_table = info->header()->shared_path_table();
516   }
517   static void update_jar_manifest(ClassPathEntry *cpe, SharedClassPathEntry* ent, TRAPS);
518   static int num_non_existent_class_paths();
519   static void record_non_existent_class_path_entry(const char* path);
520 
521 #if INCLUDE_JVMTI
522   // Caller needs a ResourceMark because parts of the returned cfs are resource-allocated.
523   static ClassFileStream* open_stream_for_jvmti(InstanceKlass* ik, Handle class_loader, TRAPS);
524   static ClassFileStream* get_stream_from_class_loader(Handle class_loader,
525                                                        ClassPathEntry* cpe,
526                                                        const char* file_name,
527                                                        TRAPS);
528 #endif
529 
530   static SharedClassPathEntry* shared_path(int index) {
531     return _shared_path_table.path_at(index);
532   }
533 
534   static const char* shared_path_name(int index) {
535     assert(index >= 0, "Sanity");
536     return shared_path(index)->name();
537   }
538 
539   static int get_number_of_shared_paths() {
540     return _shared_path_table.size();
541   }
542 
543   static int get_module_shared_path_index(Symbol* location) NOT_CDS_RETURN_(-1);
544 
545   // The offset of the first core region in the archive, relative to SharedBaseAddress
546   size_t mapping_base_offset() const { return first_core_region()->mapping_offset();    }
547   // The offset of the (exclusive) end of the last core region in this archive, relative to SharedBaseAddress
548   size_t mapping_end_offset()  const { return last_core_region()->mapping_end_offset(); }
549 
550   char* mapped_base()    const { return first_core_region()->mapped_base(); }
551   char* mapped_end()     const { return last_core_region()->mapped_end();   }
552 
553   // Non-zero if the archive needs to be mapped a non-default location due to ASLR.
554   intx relocation_delta() const {
555     return header()->mapped_base_address() - header()->requested_base_address();
556   }
557 
558   FileMapRegion* first_core_region() const;
559   FileMapRegion* last_core_region()  const;
560 
561   FileMapRegion* region_at(int i) const {
562     return header()->region_at(i);
563   }
564 
565   BitMapView bitmap_view(int region_index, bool is_oopmap);
566   BitMapView oopmap_view(int region_index);
567   BitMapView ptrmap_view(int region_index);
568 
569   void print(outputStream* st) const;
570 
571   const char* vm_version() {
572     return header()->jvm_ident();
573   }
574 
575  private:
576   void  seek_to_position(size_t pos);
577   char* skip_first_path_entry(const char* path) NOT_CDS_RETURN_(nullptr);
578   int   num_paths(const char* path) NOT_CDS_RETURN_(0);
579   bool  check_paths_existence(const char* paths) NOT_CDS_RETURN_(false);
580   GrowableArray<const char*>* create_dumptime_app_classpath_array() NOT_CDS_RETURN_(nullptr);
581   GrowableArray<const char*>* create_path_array(const char* path) NOT_CDS_RETURN_(nullptr);
582   bool  classpath_failure(const char* msg, const char* name) NOT_CDS_RETURN_(false);
583   unsigned int longest_common_app_classpath_prefix_len(int num_paths,
584                                                        GrowableArray<const char*>* rp_array)
585                                                        NOT_CDS_RETURN_(0);
586   bool  check_paths(int shared_path_start_idx, int num_paths,
587                     GrowableArray<const char*>* rp_array,
588                     unsigned int dumptime_prefix_len,
589                     unsigned int runtime_prefix_len) NOT_CDS_RETURN_(false);
590   void  extract_module_paths(const char* runtime_path, GrowableArray<const char*>* module_paths);
591   bool  validate_boot_class_paths() NOT_CDS_RETURN_(false);
592   bool  validate_app_class_paths(int shared_app_paths_len) NOT_CDS_RETURN_(false);
593   bool  map_heap_region_impl() NOT_CDS_JAVA_HEAP_RETURN_(false);
594   void  dealloc_heap_region() NOT_CDS_JAVA_HEAP_RETURN;
595   bool  can_use_heap_region();
596   bool  load_heap_region() NOT_CDS_JAVA_HEAP_RETURN_(false);
597   bool  map_heap_region() NOT_CDS_JAVA_HEAP_RETURN_(false);
598   void  init_heap_region_relocation();
599   MapArchiveResult map_region(int i, intx addr_delta, char* mapped_base_address, ReservedSpace rs);
600   bool  relocate_pointers_in_core_regions(intx addr_delta);
601   void  relocate_pointers_in_cached_code_region();
602   static MemRegion _mapped_heap_memregion;
603 
604 public:
605   address heap_region_dumptime_address() NOT_CDS_JAVA_HEAP_RETURN_(nullptr);
606   address heap_region_requested_address() NOT_CDS_JAVA_HEAP_RETURN_(nullptr);
607   narrowOop encoded_heap_region_dumptime_address();
608 
609 private:
610 
611 #if INCLUDE_JVMTI
612   static ClassPathEntry** _classpath_entries_for_jvmti;
613   static ClassPathEntry* get_classpath_entry_for_jvmti(int i, TRAPS);
614 #endif
615 };
616 
617 #endif // SHARE_CDS_FILEMAP_HPP