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