1 /*
   2  * Copyright (c) 2003, 2017, 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 "memory/metaspaceShared.hpp"
  29 #include "memory/metaspace.hpp"
  30 #include "utilities/align.hpp"
  31 
  32 // Layout of the file:
  33 //  header: dump of archive instance plus versioning info, datestamp, etc.
  34 //   [magic # = 0xF00BABA2]
  35 //  ... padding to align on page-boundary
  36 //  read-write space
  37 //  read-only space
  38 //  misc data (block offset table, string table, symbols, dictionary, etc.)
  39 //  tag(666)
  40 
  41 static const int JVM_IDENT_MAX = 256;
  42 
  43 class SharedClassPathEntry VALUE_OBJ_CLASS_SPEC {
  44 protected:
  45   bool   _is_dir;
  46   time_t _timestamp;          // jar/jimage timestamp,  0 if is directory or other
  47   long   _filesize;           // jar/jimage file size, -1 if is directory, -2 if other
  48   Array<char>* _name;
  49   Array<u1>*   _manifest;
  50 
  51 public:
  52   void init(const char* name, TRAPS);
  53   void metaspace_pointers_do(MetaspaceClosure* it);
  54   bool validate();
  55 
  56   // The _timestamp only gets set for jar files and "modules" jimage.
  57   bool is_jar_or_bootimage() {
  58     return _timestamp != 0;
  59   }
  60   bool is_dir() { return _is_dir; }
  61   bool is_jrt() { return ClassLoader::is_jrt(name()); }
  62   time_t timestamp() const { return _timestamp; }
  63   long   filesize()  const { return _filesize; }
  64   const char* name() const { return _name->data(); }
  65   const char* manifest() const {
  66     return (_manifest == NULL) ? NULL : (const char*)_manifest->data();
  67   }
  68   int manifest_size() const {
  69     return (_manifest == NULL) ? 0 : _manifest->length();
  70   }
  71 };
  72 
  73 class FileMapInfo : public CHeapObj<mtInternal> {
  74 private:
  75   friend class ManifestStream;
  76   enum {
  77     _invalid_version = -1,
  78     _current_version = 3
  79   };
  80 
  81   bool  _file_open;
  82   int   _fd;
  83   size_t  _file_offset;
  84 
  85 private:
  86   static Array<u8>*            _classpath_entry_table;
  87   static int                   _classpath_entry_table_size;
  88   static size_t                _classpath_entry_size;
  89   static bool                  _validating_classpath_entry_table;
  90 
  91   // FileMapHeader describes the shared space data in the file to be
  92   // mapped.  This structure gets written to a file.  It is not a class, so
  93   // that the compilers don't add any compiler-private data to it.
  94 
  95 public:
  96   struct FileMapHeaderBase : public CHeapObj<mtClass> {
  97     virtual bool validate() = 0;
  98     virtual void populate(FileMapInfo* info, size_t alignment) = 0;
  99   };
 100   struct FileMapHeader : FileMapHeaderBase {
 101     // Use data() and data_size() to memcopy to/from the FileMapHeader. We need to
 102     // avoid read/writing the C++ vtable pointer.
 103     static size_t data_size();
 104     char* data() {
 105       return ((char*)this) + sizeof(FileMapHeaderBase);
 106     }
 107 
 108     int    _magic;                    // identify file type.
 109     int    _crc;                      // header crc checksum.
 110     int    _version;                  // (from enum, above.)
 111     size_t _alignment;                // how shared archive should be aligned
 112     int    _obj_alignment;            // value of ObjectAlignmentInBytes
 113     int    _narrow_oop_shift;         // compressed oop encoding shift
 114     bool   _compact_strings;          // value of CompactStrings
 115     uintx  _max_heap_size;            // java max heap size during dumping
 116     Universe::NARROW_OOP_MODE _narrow_oop_mode; // compressed oop encoding mode
 117     int     _narrow_klass_shift;      // save narrow klass base and shift
 118     address _narrow_klass_base;
 119     char*   _misc_data_patching_start;
 120     char*   _read_only_tables_start;
 121     address _cds_i2i_entry_code_buffers;
 122     size_t  _cds_i2i_entry_code_buffers_size;
 123     size_t  _core_spaces_size;        // number of bytes allocated by the core spaces
 124                                       // (mc, md, ro, rw and od).
 125 
 126     struct space_info {
 127       int    _crc;           // crc checksum of the current space
 128       size_t _file_offset;   // sizeof(this) rounded to vm page size
 129       union {
 130         char*  _base;        // copy-on-write base address
 131         intx   _offset;      // offset from the compressed oop encoding base, only used
 132                              // by string space
 133       } _addr;
 134       size_t _used;          // for setting space top on read
 135       bool   _read_only;     // read only space?
 136       bool   _allow_exec;    // executable code in space?
 137     } _space[MetaspaceShared::n_regions];
 138 
 139     // The following fields are all sanity checks for whether this archive
 140     // will function correctly with this JVM and the bootclasspath it's
 141     // invoked with.
 142     char  _jvm_ident[JVM_IDENT_MAX];      // identifier for jvm
 143 
 144     // The _paths_misc_info is a variable-size structure that records "miscellaneous"
 145     // information during dumping. It is generated and validated by the
 146     // SharedPathsMiscInfo class. See SharedPathsMiscInfo.hpp and sharedClassUtil.hpp for
 147     // detailed description.
 148     //
 149     // The _paths_misc_info data is stored as a byte array in the archive file header,
 150     // immediately after the _header field. This information is used only when
 151     // checking the validity of the archive and is deallocated after the archive is loaded.
 152     //
 153     // Note that the _paths_misc_info does NOT include information for JAR files
 154     // that existed during dump time. Their information is stored in _classpath_entry_table.
 155     int _paths_misc_info_size;
 156 
 157     // The following is a table of all the class path entries that were used
 158     // during dumping. At run time, we require these files to exist and have the same
 159     // size/modification time, or else the archive will refuse to load.
 160     //
 161     // All of these entries must be JAR files. The dumping process would fail if a non-empty
 162     // directory was specified in the classpaths. If an empty directory was specified
 163     // it is checked by the _paths_misc_info as described above.
 164     //
 165     // FIXME -- if JAR files in the tail of the list were specified but not used during dumping,
 166     // they should be removed from this table, to save space and to avoid spurious
 167     // loading failures during runtime.
 168     int _classpath_entry_table_size;
 169     size_t _classpath_entry_size;
 170     Array<u8>* _classpath_entry_table;
 171 
 172     char* region_addr(int idx);
 173 
 174     virtual bool validate();
 175     virtual void populate(FileMapInfo* info, size_t alignment);
 176     int compute_crc();
 177   };
 178 
 179   FileMapHeader * _header;
 180 
 181   const char* _full_path;
 182   char* _paths_misc_info;
 183 
 184   static FileMapInfo* _current_info;
 185 
 186   bool  init_from_file(int fd);
 187   void  align_file_position();
 188   bool  validate_header_impl();
 189   static void metaspace_pointers_do(MetaspaceClosure* it);
 190 
 191 public:
 192   FileMapInfo();
 193   ~FileMapInfo();
 194 
 195   static int current_version()        { return _current_version; }
 196   int    compute_header_crc()         { return _header->compute_crc(); }
 197   void   set_header_crc(int crc)      { _header->_crc = crc; }
 198   void   populate_header(size_t alignment);
 199   bool   validate_header();
 200   void   invalidate();
 201   int    version()                    { return _header->_version; }
 202   size_t alignment()                  { return _header->_alignment; }
 203   Universe::NARROW_OOP_MODE narrow_oop_mode() { return _header->_narrow_oop_mode; }
 204   int    narrow_oop_shift()           { return _header->_narrow_oop_shift; }
 205   uintx  max_heap_size()              { return _header->_max_heap_size; }
 206   address narrow_klass_base() const   { return _header->_narrow_klass_base; }
 207   int     narrow_klass_shift() const  { return _header->_narrow_klass_shift; }
 208   struct FileMapHeader* header()      { return _header; }
 209   char* misc_data_patching_start()            { return _header->_misc_data_patching_start; }
 210   void set_misc_data_patching_start(char* p)  { _header->_misc_data_patching_start = p; }
 211   char* read_only_tables_start()              { return _header->_read_only_tables_start; }
 212   void set_read_only_tables_start(char* p)    { _header->_read_only_tables_start = p; }
 213 
 214   address cds_i2i_entry_code_buffers() {
 215     return _header->_cds_i2i_entry_code_buffers;
 216   }
 217   void set_cds_i2i_entry_code_buffers(address addr) {
 218     _header->_cds_i2i_entry_code_buffers = addr;
 219   }
 220   size_t cds_i2i_entry_code_buffers_size() {
 221     return _header->_cds_i2i_entry_code_buffers_size;
 222   }
 223   void set_cds_i2i_entry_code_buffers_size(size_t s) {
 224     _header->_cds_i2i_entry_code_buffers_size = s;
 225   }
 226   void set_core_spaces_size(size_t s)    {  _header->_core_spaces_size = s; }
 227   size_t core_spaces_size()              { return _header->_core_spaces_size; }
 228 
 229   static FileMapInfo* current_info() {
 230     CDS_ONLY(return _current_info;)
 231     NOT_CDS(return NULL;)
 232   }
 233 
 234   static void assert_mark(bool check);
 235 
 236   // File manipulation.
 237   bool  initialize() NOT_CDS_RETURN_(false);
 238   bool  open_for_read();
 239   void  open_for_write();
 240   void  write_header();
 241   void  write_region(int region, char* base, size_t size,
 242                      bool read_only, bool allow_exec);
 243   void  write_string_regions(GrowableArray<MemRegion> *regions,
 244                              char** s0_start, char** s0_top, char** s0_end,
 245                              char** s1_start, char** s1_top, char** s1_end);
 246   void  write_bytes(const void* buffer, int count);
 247   void  write_bytes_aligned(const void* buffer, int count);
 248   char* map_region(int i);
 249   bool  map_string_regions();
 250   bool  verify_string_regions();
 251   void  fixup_string_regions();
 252   void  unmap_region(int i);
 253   void  dealloc_string_regions();
 254   bool  verify_region_checksum(int i);
 255   void  close();
 256   bool  is_open() { return _file_open; }
 257   ReservedSpace reserve_shared_memory();
 258 
 259   // JVM/TI RedefineClasses() support:
 260   // Remap the shared readonly space to shared readwrite, private.
 261   bool  remap_shared_readonly_as_readwrite();
 262 
 263   // Errors.
 264   static void fail_stop(const char *msg, ...) ATTRIBUTE_PRINTF(1, 2);
 265   static void fail_continue(const char *msg, ...) ATTRIBUTE_PRINTF(1, 2);
 266 
 267   // Return true if given address is in the mapped shared space.
 268   bool is_in_shared_space(const void* p) NOT_CDS_RETURN_(false);
 269   bool is_in_shared_region(const void* p, int idx) NOT_CDS_RETURN_(false);
 270   void print_shared_spaces() NOT_CDS_RETURN;
 271 
 272   // Stop CDS sharing and unmap CDS regions.
 273   static void stop_sharing_and_unmap(const char* msg);
 274 
 275   static void allocate_classpath_entry_table();
 276   bool validate_classpath_entry_table();
 277 
 278   static SharedClassPathEntry* shared_classpath(int index) {
 279     if (index < 0) {
 280       return NULL;
 281     }
 282     assert(index < _classpath_entry_table_size, "sanity");
 283     char* p = (char*)_classpath_entry_table->data();
 284     p += _classpath_entry_size * index;
 285     return (SharedClassPathEntry*)p;
 286   }
 287   static const char* shared_classpath_name(int index) {
 288     assert(index >= 0, "Sanity");
 289     return shared_classpath(index)->name();
 290   }
 291 
 292   static int get_number_of_share_classpaths() {
 293     return _classpath_entry_table_size;
 294   }
 295 };
 296 
 297 #endif // SHARE_VM_MEMORY_FILEMAP_HPP