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