1 /*
   2  * Copyright (c) 2003, 2018, 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_VM_MEMORY_FILEMAP_HPP
  26 #define SHARE_VM_MEMORY_FILEMAP_HPP
  27 
  28 #include "classfile/classLoader.hpp"
  29 #include "memory/metaspaceShared.hpp"
  30 #include "memory/metaspace.hpp"
  31 #include "memory/universe.hpp"
  32 #include "utilities/align.hpp"
  33 
  34 // Layout of the file:
  35 //  header: dump of archive instance plus versioning info, datestamp, etc.
  36 //   [magic # = 0xF00BABA2]
  37 //  ... padding to align on page-boundary
  38 //  read-write space
  39 //  read-only space
  40 //  misc data (block offset table, string table, symbols, dictionary, etc.)
  41 //  tag(666)
  42 
  43 static const int JVM_IDENT_MAX = 256;
  44 
  45 class SharedClassPathEntry {
  46   enum {
  47     modules_image_entry,
  48     jar_entry,
  49     signed_jar_entry,
  50     dir_entry,
  51     unknown_entry
  52   };
  53 protected:
  54   u1     _type;
  55   time_t _timestamp;          // jar timestamp,  0 if is directory, modules image or other
  56   long   _filesize;           // jar/jimage file size, -1 if is directory, -2 if other
  57   Array<char>* _name;
  58   Array<u1>*   _manifest;
  59 
  60 public:
  61   void init(const char* name, bool is_modules_image, TRAPS);
  62   void metaspace_pointers_do(MetaspaceClosure* it);
  63   bool validate(bool is_class_path = true);
  64 
  65   // The _timestamp only gets set for jar files.
  66   bool has_timestamp() {
  67     return _timestamp != 0;
  68   }
  69   bool is_dir()            { return _type == dir_entry; }
  70   bool is_modules_image()  { return _type == modules_image_entry; }
  71   bool is_jar()            { return _type == jar_entry; }
  72   bool is_signed()         { return _type == signed_jar_entry; }
  73   void set_is_signed()     {
  74     _type = signed_jar_entry;
  75   }
  76   time_t timestamp() const { return _timestamp; }
  77   long   filesize()  const { return _filesize; }
  78   const char* name() const { return _name->data(); }
  79   const char* manifest() const {
  80     return (_manifest == NULL) ? NULL : (const char*)_manifest->data();
  81   }
  82   int manifest_size() const {
  83     return (_manifest == NULL) ? 0 : _manifest->length();
  84   }
  85   void set_manifest(Array<u1>* manifest) {
  86     _manifest = manifest;
  87   }
  88 };
  89 
  90 struct ArchiveHeapOopmapInfo {
  91   address _oopmap;               // bitmap for relocating embedded oops
  92   size_t  _oopmap_size_in_bits;
  93 };
  94 
  95 class FileMapInfo : public CHeapObj<mtInternal> {
  96 private:
  97   friend class ManifestStream;
  98   friend class VMStructs;
  99   enum {
 100     _invalid_version = -1,
 101     _current_version = 4
 102   };
 103 
 104   bool  _file_open;
 105   int   _fd;
 106   size_t  _file_offset;
 107 
 108 private:
 109   static Array<u8>*            _shared_path_table;
 110   static int                   _shared_path_table_size;
 111   static size_t                _shared_path_entry_size;
 112   static bool                  _validating_shared_path_table;
 113 
 114   // FileMapHeader describes the shared space data in the file to be
 115   // mapped.  This structure gets written to a file.  It is not a class, so
 116   // that the compilers don't add any compiler-private data to it.
 117 
 118 public:
 119   struct FileMapHeaderBase : public CHeapObj<mtClass> {
 120     // Need to put something here. Otherwise, in product build, because CHeapObj has no virtual
 121     // methods, we would get sizeof(FileMapHeaderBase) == 1 with gcc.
 122     intx _dummy;
 123   };
 124 
 125   struct FileMapHeader : FileMapHeaderBase {
 126     // Use data() and data_size() to memcopy to/from the FileMapHeader. We need to
 127     // avoid read/writing the C++ vtable pointer.
 128     static size_t data_size() {
 129       return sizeof(FileMapHeader) - sizeof(FileMapInfo::FileMapHeaderBase);
 130     }
 131     char* data() {
 132       return ((char*)this) + sizeof(FileMapHeaderBase);
 133     }
 134     FileMapHeader() {
 135       memset(data(), 0, data_size());
 136     }
 137     int    _magic;                    // identify file type.
 138     int    _crc;                      // header crc checksum.
 139     int    _version;                  // (from enum, above.)
 140     size_t _alignment;                // how shared archive should be aligned
 141     int    _obj_alignment;            // value of ObjectAlignmentInBytes
 142     address _narrow_oop_base;         // compressed oop encoding base
 143     int    _narrow_oop_shift;         // compressed oop encoding shift
 144     bool    _compact_strings;         // value of CompactStrings
 145     uintx  _max_heap_size;            // java max heap size during dumping
 146     Universe::NARROW_OOP_MODE _narrow_oop_mode; // compressed oop encoding mode
 147     int     _narrow_klass_shift;      // save narrow klass base and shift
 148     address _narrow_klass_base;
 149     char*   _misc_data_patching_start;
 150     char*   _read_only_tables_start;
 151     address _cds_i2i_entry_code_buffers;
 152     size_t  _cds_i2i_entry_code_buffers_size;
 153     size_t  _core_spaces_size;        // number of bytes allocated by the core spaces
 154                                       // (mc, md, ro, rw and od).
 155     MemRegion _heap_reserved;         // reserved region for the entire heap at dump time.
 156     struct space_info {
 157       int    _crc;           // crc checksum of the current space
 158       size_t _file_offset;   // sizeof(this) rounded to vm page size
 159       union {
 160         char*  _base;        // copy-on-write base address
 161         intx   _offset;      // offset from the compressed oop encoding base, only used
 162                              // by archive heap space
 163       } _addr;
 164       size_t _used;          // for setting space top on read
 165       bool   _read_only;     // read only space?
 166       bool   _allow_exec;    // executable code in space?
 167       address _oopmap;       // bitmap for relocating embedded oops
 168       size_t  _oopmap_size_in_bits;
 169       narrowOop offset() {
 170         return (narrowOop)_addr._offset;
 171       }
 172 
 173       // The starting address, as calculated with CompressedOop::decode_non_null()
 174       address start_address_with_current_oop_encoding_mode() {
 175         return decode_start_address(true);
 176       }
 177 
 178       // The starting address, as calculated with HeapShared::decode_with_archived_oop_encoding_mode()
 179       address start_address_with_archived_oop_encoding_mode() {
 180         return decode_start_address(false);
 181       }
 182     private:
 183       address decode_start_address(bool with_current_oop_encoding_mode);
 184     } _space[MetaspaceShared::n_regions];
 185 
 186     // The following fields are all sanity checks for whether this archive
 187     // will function correctly with this JVM and the bootclasspath it's
 188     // invoked with.
 189     char  _jvm_ident[JVM_IDENT_MAX];      // identifier for jvm
 190 
 191     // The _paths_misc_info is a variable-size structure that records "miscellaneous"
 192     // information during dumping. It is generated and validated by the
 193     // SharedPathsMiscInfo class. See SharedPathsMiscInfo.hpp for
 194     // detailed description.
 195     //
 196     // The _paths_misc_info data is stored as a byte array in the archive file header,
 197     // immediately after the _header field. This information is used only when
 198     // checking the validity of the archive and is deallocated after the archive is loaded.
 199     //
 200     // Note that the _paths_misc_info does NOT include information for JAR files
 201     // that existed during dump time. Their information is stored in _shared_path_table.
 202     int _paths_misc_info_size;
 203 
 204     // The following is a table of all the class path entries that were used
 205     // during dumping. At run time, we require these files to exist and have the same
 206     // size/modification time, or else the archive will refuse to load.
 207     //
 208     // All of these entries must be JAR files. The dumping process would fail if a non-empty
 209     // directory was specified in the classpaths. If an empty directory was specified
 210     // it is checked by the _paths_misc_info as described above.
 211     //
 212     // FIXME -- if JAR files in the tail of the list were specified but not used during dumping,
 213     // they should be removed from this table, to save space and to avoid spurious
 214     // loading failures during runtime.
 215     int _shared_path_table_size;
 216     size_t _shared_path_entry_size;
 217     Array<u8>* _shared_path_table;
 218 
 219     jshort _app_class_paths_start_index;  // Index of first app classpath entry
 220     jshort _app_module_paths_start_index; // Index of first module path entry
 221     jshort _max_used_path_index;          // max path index referenced during CDS dump
 222     bool   _verify_local;                 // BytecodeVerificationLocal setting
 223     bool   _verify_remote;                // BytecodeVerificationRemote setting
 224     bool   _has_platform_or_app_classes;  // Archive contains app classes
 225 
 226     void set_has_platform_or_app_classes(bool v) {
 227       _has_platform_or_app_classes = v;
 228     }
 229     bool has_platform_or_app_classes() { return _has_platform_or_app_classes; }
 230     jshort max_used_path_index()       { return _max_used_path_index; }
 231     jshort app_module_paths_start_index() { return _app_module_paths_start_index; }
 232 
 233     char* region_addr(int idx);
 234 
 235     bool validate();
 236     void populate(FileMapInfo* info, size_t alignment);
 237     int compute_crc();
 238   };
 239 
 240   FileMapHeader * _header;
 241 
 242   const char* _full_path;
 243   char* _paths_misc_info;
 244 
 245   static FileMapInfo* _current_info;
 246   static bool _heap_pointers_need_patching;
 247 
 248   bool  init_from_file(int fd);
 249   void  align_file_position();
 250   bool  validate_header_impl();
 251   static void metaspace_pointers_do(MetaspaceClosure* it);
 252 
 253 public:
 254   FileMapInfo();
 255   ~FileMapInfo();
 256 
 257   static int current_version()        { return _current_version; }
 258   int    compute_header_crc()         { return _header->compute_crc(); }
 259   void   set_header_crc(int crc)      { _header->_crc = crc; }
 260   void   populate_header(size_t alignment);
 261   bool   validate_header();
 262   void   invalidate();
 263   int    version()                    { return _header->_version; }
 264   size_t alignment()                  { return _header->_alignment; }
 265   Universe::NARROW_OOP_MODE narrow_oop_mode() { return _header->_narrow_oop_mode; }
 266   address narrow_oop_base()    const  { return _header->_narrow_oop_base; }
 267   int     narrow_oop_shift()   const  { return _header->_narrow_oop_shift; }
 268   uintx   max_heap_size()      const  { return _header->_max_heap_size; }
 269   address narrow_klass_base()  const  { return _header->_narrow_klass_base; }
 270   int     narrow_klass_shift() const  { return _header->_narrow_klass_shift; }
 271   struct  FileMapHeader* header()     { return _header; }
 272   char*   misc_data_patching_start()          { return _header->_misc_data_patching_start; }
 273   void set_misc_data_patching_start(char* p)  { _header->_misc_data_patching_start = p; }
 274   char* read_only_tables_start()              { return _header->_read_only_tables_start; }
 275   void set_read_only_tables_start(char* p)    { _header->_read_only_tables_start = p; }
 276 
 277   address cds_i2i_entry_code_buffers() {
 278     return _header->_cds_i2i_entry_code_buffers;
 279   }
 280   void set_cds_i2i_entry_code_buffers(address addr) {
 281     _header->_cds_i2i_entry_code_buffers = addr;
 282   }
 283   size_t cds_i2i_entry_code_buffers_size() {
 284     return _header->_cds_i2i_entry_code_buffers_size;
 285   }
 286   void set_cds_i2i_entry_code_buffers_size(size_t s) {
 287     _header->_cds_i2i_entry_code_buffers_size = s;
 288   }
 289   void set_core_spaces_size(size_t s)    {  _header->_core_spaces_size = s; }
 290   size_t core_spaces_size()              { return _header->_core_spaces_size; }
 291 
 292   static FileMapInfo* current_info() {
 293     CDS_ONLY(return _current_info;)
 294     NOT_CDS(return NULL;)
 295   }
 296 
 297   static void assert_mark(bool check);
 298 
 299   // File manipulation.
 300   bool  initialize() NOT_CDS_RETURN_(false);
 301   bool  open_for_read();
 302   void  open_for_write();
 303   void  write_header();
 304   void  write_region(int region, char* base, size_t size,
 305                      bool read_only, bool allow_exec);
 306   size_t write_archive_heap_regions(GrowableArray<MemRegion> *heap_mem,
 307                                     GrowableArray<ArchiveHeapOopmapInfo> *oopmaps,
 308                                     int first_region_id, int max_num_regions);
 309   void  write_bytes(const void* buffer, int count);
 310   void  write_bytes_aligned(const void* buffer, int count);
 311   char* map_region(int i, char** top_ret);
 312   void  map_heap_regions_impl() NOT_CDS_JAVA_HEAP_RETURN;
 313   void  map_heap_regions() NOT_CDS_JAVA_HEAP_RETURN;
 314   void  fixup_mapped_heap_regions() NOT_CDS_JAVA_HEAP_RETURN;
 315   void  patch_archived_heap_embedded_pointers() NOT_CDS_JAVA_HEAP_RETURN;
 316   void  patch_archived_heap_embedded_pointers(MemRegion* ranges, int num_ranges,
 317                                               int first_region_idx) NOT_CDS_JAVA_HEAP_RETURN;
 318   bool  has_heap_regions()  NOT_CDS_JAVA_HEAP_RETURN_(false);
 319   MemRegion get_heap_regions_range_with_current_oop_encoding_mode() NOT_CDS_JAVA_HEAP_RETURN_(MemRegion());
 320   void  unmap_region(int i);
 321   bool  verify_region_checksum(int i);
 322   void  close();
 323   bool  is_open() { return _file_open; }
 324   ReservedSpace reserve_shared_memory();
 325 
 326   // JVM/TI RedefineClasses() support:
 327   // Remap the shared readonly space to shared readwrite, private.
 328   bool  remap_shared_readonly_as_readwrite();
 329 
 330   // Errors.
 331   static void fail_stop(const char *msg, ...) ATTRIBUTE_PRINTF(1, 2);
 332   static void fail_continue(const char *msg, ...) ATTRIBUTE_PRINTF(1, 2);
 333 
 334   bool is_in_shared_region(const void* p, int idx) NOT_CDS_RETURN_(false);
 335 
 336   // Stop CDS sharing and unmap CDS regions.
 337   static void stop_sharing_and_unmap(const char* msg);
 338 
 339   static void allocate_shared_path_table();
 340   static void check_nonempty_dir_in_shared_path_table();
 341   bool validate_shared_path_table();
 342   static void update_shared_classpath(ClassPathEntry *cpe, SharedClassPathEntry* ent, TRAPS);
 343 
 344   static SharedClassPathEntry* shared_path(int index) {
 345     if (index < 0) {
 346       return NULL;
 347     }
 348     assert(index < _shared_path_table_size, "sanity");
 349     char* p = (char*)_shared_path_table->data();
 350     p += _shared_path_entry_size * index;
 351     return (SharedClassPathEntry*)p;
 352   }
 353 
 354   static const char* shared_path_name(int index) {
 355     assert(index >= 0, "Sanity");
 356     return shared_path(index)->name();
 357   }
 358 
 359   static int get_number_of_shared_paths() {
 360     return _shared_path_table_size;
 361   }
 362 
 363  private:
 364   bool  map_heap_data(MemRegion **heap_mem, int first, int max, int* num,
 365                       bool is_open = false) NOT_CDS_JAVA_HEAP_RETURN_(false);
 366   bool  verify_mapped_heap_regions(int first, int num) NOT_CDS_JAVA_HEAP_RETURN_(false);
 367   void  dealloc_archive_heap_regions(MemRegion* regions, int num) NOT_CDS_JAVA_HEAP_RETURN;
 368 
 369   FileMapHeader::space_info* space_at(int i) {
 370     assert(i >= 0 && i <= MetaspaceShared::last_valid_region, "invalid region");
 371     return &_header->_space[i];
 372   }
 373 };
 374 
 375 #endif // SHARE_VM_MEMORY_FILEMAP_HPP