1 /*
   2  * Copyright (c) 2003, 2019, 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_MEMORY_FILEMAP_HPP
  26 #define SHARE_MEMORY_FILEMAP_HPP
  27 
  28 #include "classfile/classLoader.hpp"
  29 #include "include/cds.h"
  30 #include "memory/metaspaceShared.hpp"
  31 #include "memory/metaspace.hpp"
  32 #include "oops/compressedOops.hpp"
  33 #include "utilities/align.hpp"
  34 
  35 // Layout of the file:
  36 //  header: dump of archive instance plus versioning info, datestamp, etc.
  37 //   [magic # = 0xF00BABA2]
  38 //  ... padding to align on page-boundary
  39 //  read-write space
  40 //  read-only space
  41 //  misc data (block offset table, string table, symbols, dictionary, etc.)
  42 //  tag(666)
  43 
  44 static const int JVM_IDENT_MAX = 256;
  45 
  46 class SharedClassPathEntry {
  47   enum {
  48     modules_image_entry,
  49     jar_entry,
  50     signed_jar_entry,
  51     dir_entry,
  52     non_existent_entry,
  53     unknown_entry
  54   };
  55 
  56   void set_name(const char* name, TRAPS);
  57 
  58   u1     _type;
  59   bool   _from_class_path_attr;
  60   time_t _timestamp;          // jar timestamp,  0 if is directory, modules image or other
  61   long   _filesize;           // jar/jimage file size, -1 if is directory, -2 if other
  62   Array<char>* _name;
  63   Array<u1>*   _manifest;
  64 
  65 public:
  66   void init(bool is_modules_image, ClassPathEntry* cpe, TRAPS);
  67   void init_as_non_existent(const char* path, TRAPS);
  68   void metaspace_pointers_do(MetaspaceClosure* it);
  69   bool validate(bool is_class_path = true) const;
  70 
  71   // The _timestamp only gets set for jar files.
  72   bool has_timestamp() const {
  73     return _timestamp != 0;
  74   }
  75   bool is_dir()           const { return _type == dir_entry; }
  76   bool is_modules_image() const { return _type == modules_image_entry; }
  77   bool is_jar()           const { return _type == jar_entry; }
  78   bool is_signed()        const { return _type == signed_jar_entry; }
  79   void set_is_signed() {
  80     _type = signed_jar_entry;
  81   }
  82   bool from_class_path_attr() { return _from_class_path_attr; }
  83   time_t timestamp() const { return _timestamp; }
  84   long   filesize()  const { return _filesize; }
  85   const char* name() const;
  86   const char* manifest() const {
  87     return (_manifest == NULL) ? NULL : (const char*)_manifest->data();
  88   }
  89   int manifest_size() const {
  90     return (_manifest == NULL) ? 0 : _manifest->length();
  91   }
  92   void set_manifest(Array<u1>* manifest) {
  93     _manifest = manifest;
  94   }
  95   bool check_non_existent() const;
  96 };
  97 
  98 struct ArchiveHeapOopmapInfo {
  99   address _oopmap;               // bitmap for relocating embedded oops
 100   size_t  _oopmap_size_in_bits;
 101 };
 102 
 103 class SharedPathTable {
 104   Array<u8>* _table;
 105   int _size;
 106 public:
 107   void dumptime_init(ClassLoaderData* loader_data, Thread* THREAD);
 108   void metaspace_pointers_do(MetaspaceClosure* it);
 109 
 110   int size() {
 111     return _size;
 112   }
 113   SharedClassPathEntry* path_at(int index) {
 114     if (index < 0) {
 115       return NULL;
 116     }
 117     assert(index < _size, "sanity");
 118     char* p = (char*)_table->data();
 119     p += sizeof(SharedClassPathEntry) * index;
 120     return (SharedClassPathEntry*)p;
 121   }
 122   Array<u8>* table() {return _table;}
 123   void set_table(Array<u8>* table) {_table = table;}
 124 };
 125 
 126 
 127 class FileMapRegion: private CDSFileMapRegion {
 128   void assert_is_heap_region() const {
 129     assert(_is_heap_region, "must be heap region");
 130   }
 131   void assert_is_not_heap_region() const {
 132     assert(!_is_heap_region, "must not be heap region");
 133   }
 134 
 135 public:
 136   static FileMapRegion* cast(CDSFileMapRegion* p) {
 137     return (FileMapRegion*)p;
 138   }
 139 
 140   // Accessors
 141   int crc()                      const { return _crc; }
 142   size_t file_offset()           const { return _file_offset; }
 143   char*  base()                  const { assert_is_not_heap_region(); return _addr._base;  }
 144   narrowOop offset()             const { assert_is_heap_region();     return (narrowOop)(_addr._offset); }
 145   size_t used()                  const { return _used; }
 146   bool read_only()               const { return _read_only != 0; }
 147   bool allow_exec()              const { return _allow_exec != 0; }
 148   void* oopmap()                 const { return _oopmap; }
 149   size_t oopmap_size_in_bits()   const { return _oopmap_size_in_bits; }
 150 
 151   void set_file_offset(size_t s) { _file_offset = s; }
 152   void set_read_only(bool v)     { _read_only = v; }
 153   void mark_invalid()            { _addr._base = NULL; }
 154 
 155   void init(bool is_heap_region, char* base, size_t size, bool read_only,
 156             bool allow_exec, int crc);
 157 
 158   void init_oopmap(void* map, size_t size_in_bits) {
 159     _oopmap = map;
 160     _oopmap_size_in_bits = size_in_bits;
 161   }
 162 };
 163 
 164 class FileMapHeader: private CDSFileMapHeaderBase {
 165   friend class CDSOffsets;
 166   friend class VMStructs;
 167 
 168   size_t _header_size;
 169 
 170   // The following fields record the states of the VM during dump time.
 171   // They are compared with the runtime states to see if the archive
 172   // can be used.
 173   size_t _alignment;                // how shared archive should be aligned
 174   int    _obj_alignment;            // value of ObjectAlignmentInBytes
 175   address _narrow_oop_base;         // compressed oop encoding base
 176   int    _narrow_oop_shift;         // compressed oop encoding shift
 177   bool   _compact_strings;          // value of CompactStrings
 178   uintx  _max_heap_size;            // java max heap size during dumping
 179   CompressedOops::Mode _narrow_oop_mode; // compressed oop encoding mode
 180   int     _narrow_klass_shift;      // save narrow klass base and shift
 181   address _narrow_klass_base;
 182   char*   _misc_data_patching_start;
 183   char*   _serialized_data_start;  // Data accessed using {ReadClosure,WriteClosure}::serialize()
 184   address _i2i_entry_code_buffers;
 185   size_t  _i2i_entry_code_buffers_size;
 186   size_t  _core_spaces_size;        // number of bytes allocated by the core spaces
 187                                     // (mc, md, ro, rw and od).
 188   address _heap_end;                // heap end at dump time.
 189   bool _base_archive_is_default;    // indicates if the base archive is the system default one
 190 
 191   // The following fields are all sanity checks for whether this archive
 192   // will function correctly with this JVM and the bootclasspath it's
 193   // invoked with.
 194   char  _jvm_ident[JVM_IDENT_MAX];  // identifier string of the jvm that created this dump
 195 
 196   // size of the base archive name including NULL terminator
 197   size_t _base_archive_name_size;
 198 
 199   // The following is a table of all the boot/app/module path entries that were used
 200   // during dumping. At run time, we validate these entries according to their
 201   // SharedClassPathEntry::_type. See:
 202   //      check_nonempty_dir_in_shared_path_table()
 203   //      validate_shared_path_table()
 204   //      validate_non_existent_class_paths()
 205   SharedPathTable _shared_path_table;
 206 
 207   jshort _app_class_paths_start_index;  // Index of first app classpath entry
 208   jshort _app_module_paths_start_index; // Index of first module path entry
 209   jshort _num_module_paths;             // number of module path entries
 210   jshort _max_used_path_index;          // max path index referenced during CDS dump
 211   bool   _verify_local;                 // BytecodeVerificationLocal setting
 212   bool   _verify_remote;                // BytecodeVerificationRemote setting
 213   bool   _has_platform_or_app_classes;  // Archive contains app classes
 214   size_t _shared_base_address;          // SharedBaseAddress used at dump time
 215   bool   _allow_archiving_with_java_agent; // setting of the AllowArchivingWithJavaAgent option
 216 
 217 public:
 218   // Accessors -- fields declared in CDSFileMapHeaderBase
 219   unsigned int magic() const {return _magic;}
 220   int crc()                               const { return _crc; }
 221   int version()                           const { return _version; }
 222 
 223   void set_crc(int crc_value)                   { _crc = crc_value; }
 224   void set_version(int v)                       { _version = v; }
 225 
 226   // Accessors -- fields declared in FileMapHeader
 227 
 228   size_t header_size()                     const { return _header_size; }
 229   size_t alignment()                       const { return _alignment; }
 230   int obj_alignment()                      const { return _obj_alignment; }
 231   address narrow_oop_base()                const { return _narrow_oop_base; }
 232   int narrow_oop_shift()                   const { return _narrow_oop_shift; }
 233   bool compact_strings()                   const { return _compact_strings; }
 234   uintx max_heap_size()                    const { return _max_heap_size; }
 235   CompressedOops::Mode narrow_oop_mode()   const { return _narrow_oop_mode; }
 236   int narrow_klass_shift()                 const { return _narrow_klass_shift; }
 237   address narrow_klass_base()              const { return _narrow_klass_base; }
 238   char* misc_data_patching_start()         const { return _misc_data_patching_start; }
 239   char* serialized_data_start()            const { return _serialized_data_start; }
 240   address i2i_entry_code_buffers()         const { return _i2i_entry_code_buffers; }
 241   size_t i2i_entry_code_buffers_size()     const { return _i2i_entry_code_buffers_size; }
 242   size_t core_spaces_size()                const { return _core_spaces_size; }
 243   address heap_end()                       const { return _heap_end; }
 244   bool base_archive_is_default()           const { return _base_archive_is_default; }
 245   const char* jvm_ident()                  const { return _jvm_ident; }
 246   size_t base_archive_name_size()          const { return _base_archive_name_size; }
 247   size_t shared_base_address()             const { return _shared_base_address; }
 248   bool has_platform_or_app_classes()       const { return _has_platform_or_app_classes; }
 249   SharedPathTable shared_path_table()      const { return _shared_path_table; }
 250 
 251   // FIXME: These should really return int
 252   jshort max_used_path_index()             const { return _max_used_path_index; }
 253   jshort app_module_paths_start_index()    const { return _app_module_paths_start_index; }
 254   jshort app_class_paths_start_index()     const { return _app_class_paths_start_index; }
 255   jshort num_module_paths()                const { return _num_module_paths; }
 256 
 257   void set_core_spaces_size(size_t s)            { _core_spaces_size = s; }
 258   void set_has_platform_or_app_classes(bool v)   { _has_platform_or_app_classes = v; }
 259   void set_misc_data_patching_start(char* p)     { _misc_data_patching_start = p; }
 260   void set_serialized_data_start(char* p)        { _serialized_data_start   = p; }
 261   void set_base_archive_name_size(size_t s)      { _base_archive_name_size = s; }
 262   void set_base_archive_is_default(bool b)       { _base_archive_is_default = b; }
 263   void set_header_size(size_t s)                 { _header_size = s; }
 264 
 265   void set_i2i_entry_code_buffers(address p, size_t s) {
 266     _i2i_entry_code_buffers = p;
 267     _i2i_entry_code_buffers_size = s;
 268   }
 269 
 270   void relocate_shared_path_table(Array<u8>* t) {
 271     assert(DynamicDumpSharedSpaces, "only");
 272     _shared_path_table.set_table(t);
 273   }
 274 
 275   void shared_path_table_metaspace_pointers_do(MetaspaceClosure* it) {
 276     assert(DynamicDumpSharedSpaces, "only");
 277     _shared_path_table.metaspace_pointers_do(it);
 278   }
 279 
 280   bool validate();
 281   int compute_crc();
 282 
 283   FileMapRegion* space_at(int i) {
 284     assert(is_valid_region(i), "invalid region");
 285     return FileMapRegion::cast(&_space[i]);
 286   }
 287 
 288   void populate(FileMapInfo* info, size_t alignment);
 289 
 290   static bool is_valid_region(int region) {
 291     return (0 <= region && region < NUM_CDS_REGIONS);
 292   }
 293 };
 294 
 295 class FileMapInfo : public CHeapObj<mtInternal> {
 296 private:
 297   friend class ManifestStream;
 298   friend class VMStructs;
 299   friend class CDSOffsets;
 300   friend class FileMapHeader;
 301 
 302   bool           _is_static;
 303   bool           _file_open;
 304   int            _fd;
 305   size_t         _file_offset;
 306   const char*    _full_path;
 307   const char*    _base_archive_name;
 308   FileMapHeader* _header;
 309 
 310   // TODO: Probably change the following to be non-static
 311   static SharedPathTable       _shared_path_table;
 312   static bool                  _validating_shared_path_table;
 313 
 314   // FileMapHeader describes the shared space data in the file to be
 315   // mapped.  This structure gets written to a file.  It is not a class, so
 316   // that the compilers don't add any compiler-private data to it.
 317 
 318   static FileMapInfo* _current_info;
 319   static FileMapInfo* _dynamic_archive_info;
 320   static bool _heap_pointers_need_patching;
 321   static bool _memory_mapping_failed;
 322   static GrowableArray<const char*>* _non_existent_class_paths;
 323 
 324   FileMapHeader *header() const       { return _header; }
 325 
 326 public:
 327   static bool get_base_archive_name_from_header(const char* archive_name,
 328                                                 int* size, char** base_archive_name);
 329   static bool check_archive(const char* archive_name, bool is_static);
 330   void restore_shared_path_table();
 331   bool init_from_file(int fd, bool is_static);
 332   static void metaspace_pointers_do(MetaspaceClosure* it);
 333 
 334   void log_paths(const char* msg, int start_idx, int end_idx);
 335 
 336   FileMapInfo(bool is_static);
 337   ~FileMapInfo();
 338 
 339   // Accessors
 340   int    compute_header_crc()  const { return header()->compute_crc(); }
 341   void   set_header_crc(int crc)     { header()->set_crc(crc); }
 342   int    space_crc(int i)      const { return space_at(i)->crc(); }
 343   void   populate_header(size_t alignment);
 344   bool   validate_header(bool is_static);
 345   void   invalidate();
 346   int    crc()                 const { return header()->crc(); }
 347   int    version()             const { return header()->version(); }
 348   size_t alignment()           const { return header()->alignment(); }
 349   address narrow_oop_base()    const { return header()->narrow_oop_base(); }
 350   int     narrow_oop_shift()   const { return header()->narrow_oop_shift(); }
 351   uintx   max_heap_size()      const { return header()->max_heap_size(); }
 352   address narrow_klass_base()  const { return header()->narrow_klass_base(); }
 353   int     narrow_klass_shift() const { return header()->narrow_klass_shift(); }
 354 
 355   CompressedOops::Mode narrow_oop_mode()      const { return header()->narrow_oop_mode(); }
 356   jshort app_module_paths_start_index()       const { return header()->app_module_paths_start_index(); }
 357   jshort app_class_paths_start_index()        const { return header()->app_class_paths_start_index(); }
 358 
 359   char* misc_data_patching_start()            const { return header()->misc_data_patching_start(); }
 360   void  set_misc_data_patching_start(char* p) const { header()->set_misc_data_patching_start(p); }
 361   char* serialized_data_start()               const { return header()->serialized_data_start(); }
 362   void  set_serialized_data_start(char* p)    const { header()->set_serialized_data_start(p); }
 363 
 364   bool  is_file_position_aligned() const;
 365   void  align_file_position();
 366 
 367   address i2i_entry_code_buffers()            const { return header()->i2i_entry_code_buffers();  }
 368   size_t i2i_entry_code_buffers_size()        const { return header()->i2i_entry_code_buffers_size(); }
 369   void set_i2i_entry_code_buffers(address addr, size_t s) const {
 370     header()->set_i2i_entry_code_buffers(addr, s);
 371   }
 372 
 373   void set_core_spaces_size(size_t s)         const { header()->set_core_spaces_size(s); }
 374   size_t core_spaces_size()                   const { return header()->core_spaces_size(); }
 375 
 376   class DynamicArchiveHeader* dynamic_header() const {
 377     assert(!_is_static, "must be");
 378     return (DynamicArchiveHeader*)header();
 379   }
 380 
 381   void set_has_platform_or_app_classes(bool v) {
 382     header()->set_has_platform_or_app_classes(v);
 383   }
 384   bool has_platform_or_app_classes() const {
 385     return header()->has_platform_or_app_classes();
 386   }
 387 
 388   static FileMapInfo* current_info() {
 389     CDS_ONLY(return _current_info;)
 390     NOT_CDS(return NULL;)
 391   }
 392 
 393   static void set_current_info(FileMapInfo* info) {
 394     CDS_ONLY(_current_info = info;)
 395   }
 396 
 397   static FileMapInfo* dynamic_info() {
 398     CDS_ONLY(return _dynamic_archive_info;)
 399     NOT_CDS(return NULL;)
 400   }
 401 
 402   static void assert_mark(bool check);
 403 
 404   // File manipulation.
 405   bool  initialize(bool is_static) NOT_CDS_RETURN_(false);
 406   bool  open_for_read(const char* path = NULL);
 407   void  open_for_write(const char* path = NULL);
 408   void  write_header();
 409   void  write_region(int region, char* base, size_t size,
 410                      bool read_only, bool allow_exec);
 411   size_t write_archive_heap_regions(GrowableArray<MemRegion> *heap_mem,
 412                                     GrowableArray<ArchiveHeapOopmapInfo> *oopmaps,
 413                                     int first_region_id, int max_num_regions);
 414   void  write_bytes(const void* buffer, size_t count);
 415   void  write_bytes_aligned(const void* buffer, size_t count);
 416   size_t  read_bytes(void* buffer, size_t count);
 417   char* map_regions(int regions[], char* saved_base[], size_t len);
 418   char* map_region(int i, char** top_ret);
 419   void  map_heap_regions_impl() NOT_CDS_JAVA_HEAP_RETURN;
 420   void  map_heap_regions() NOT_CDS_JAVA_HEAP_RETURN;
 421   void  fixup_mapped_heap_regions() NOT_CDS_JAVA_HEAP_RETURN;
 422   void  patch_archived_heap_embedded_pointers() NOT_CDS_JAVA_HEAP_RETURN;
 423   void  patch_archived_heap_embedded_pointers(MemRegion* ranges, int num_ranges,
 424                                               int first_region_idx) NOT_CDS_JAVA_HEAP_RETURN;
 425   bool  has_heap_regions()  NOT_CDS_JAVA_HEAP_RETURN_(false);
 426   MemRegion get_heap_regions_range_with_current_oop_encoding_mode() NOT_CDS_JAVA_HEAP_RETURN_(MemRegion());
 427   void  unmap_regions(int regions[], char* saved_base[], size_t len);
 428   void  unmap_region(int i);
 429   bool  verify_region_checksum(int i);
 430   void  close();
 431   bool  is_open() { return _file_open; }
 432   ReservedSpace reserve_shared_memory();
 433 
 434   // JVM/TI RedefineClasses() support:
 435   // Remap the shared readonly space to shared readwrite, private.
 436   bool  remap_shared_readonly_as_readwrite();
 437 
 438   // Errors.
 439   static void fail_stop(const char *msg, ...) ATTRIBUTE_PRINTF(1, 2);
 440   static void fail_continue(const char *msg, ...) ATTRIBUTE_PRINTF(1, 2);
 441   static bool memory_mapping_failed() {
 442     CDS_ONLY(return _memory_mapping_failed;)
 443     NOT_CDS(return false;)
 444   }
 445   bool is_in_shared_region(const void* p, int idx) NOT_CDS_RETURN_(false);
 446 
 447   // Stop CDS sharing and unmap CDS regions.
 448   static void stop_sharing_and_unmap(const char* msg);
 449 
 450   static void allocate_shared_path_table();
 451   static int add_shared_classpaths(int i, const char* which, ClassPathEntry *cpe, TRAPS);
 452   static void check_nonempty_dir_in_shared_path_table();
 453   bool validate_shared_path_table();
 454   void validate_non_existent_class_paths();
 455   static void update_jar_manifest(ClassPathEntry *cpe, SharedClassPathEntry* ent, TRAPS);
 456   static int num_non_existent_class_paths();
 457   static void record_non_existent_class_path_entry(const char* path);
 458 
 459 #if INCLUDE_JVMTI
 460   static ClassFileStream* open_stream_for_jvmti(InstanceKlass* ik, Handle class_loader, TRAPS);
 461 #endif
 462 
 463   static SharedClassPathEntry* shared_path(int index) {
 464     return _shared_path_table.path_at(index);
 465   }
 466 
 467   static const char* shared_path_name(int index) {
 468     assert(index >= 0, "Sanity");
 469     return shared_path(index)->name();
 470   }
 471 
 472   static int get_number_of_shared_paths() {
 473     return _shared_path_table.size();
 474   }
 475 
 476   char* region_addr(int idx);
 477 
 478  private:
 479   void  seek_to_position(size_t pos);
 480   char* skip_first_path_entry(const char* path) NOT_CDS_RETURN_(NULL);
 481   int   num_paths(const char* path) NOT_CDS_RETURN_(0);
 482   GrowableArray<const char*>* create_path_array(const char* path) NOT_CDS_RETURN_(NULL);
 483   bool  fail(const char* msg, const char* name) NOT_CDS_RETURN_(false);
 484   bool  check_paths(int shared_path_start_idx, int num_paths,
 485                     GrowableArray<const char*>* rp_array) NOT_CDS_RETURN_(false);
 486   bool  validate_boot_class_paths() NOT_CDS_RETURN_(false);
 487   bool  validate_app_class_paths(int shared_app_paths_len) NOT_CDS_RETURN_(false);
 488   bool  map_heap_data(MemRegion **heap_mem, int first, int max, int* num,
 489                       bool is_open = false) NOT_CDS_JAVA_HEAP_RETURN_(false);
 490   bool  region_crc_check(char* buf, size_t size, int expected_crc) NOT_CDS_RETURN_(false);
 491   void  dealloc_archive_heap_regions(MemRegion* regions, int num, bool is_open) NOT_CDS_JAVA_HEAP_RETURN;
 492 
 493   FileMapRegion* space_at(int i) const {
 494     return header()->space_at(i);
 495   }
 496 
 497   // The starting address of spc, as calculated with CompressedOop::decode_non_null()
 498   address start_address_as_decoded_with_current_oop_encoding_mode(FileMapRegion* spc) {
 499     return decode_start_address(spc, true);
 500   }
 501 
 502   // The starting address of spc, as calculated with HeapShared::decode_from_archive()
 503   address start_address_as_decoded_from_archive(FileMapRegion* spc) {
 504     return decode_start_address(spc, false);
 505   }
 506 
 507   address decode_start_address(FileMapRegion* spc, bool with_current_oop_encoding_mode);
 508 
 509 #if INCLUDE_JVMTI
 510   static ClassPathEntry** _classpath_entries_for_jvmti;
 511   static ClassPathEntry* get_classpath_entry_for_jvmti(int i, TRAPS);
 512 #endif
 513 };
 514 
 515 #endif // SHARE_MEMORY_FILEMAP_HPP