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     unknown_entry
  53   };
  54 protected:
  55   u1     _type;
  56   bool   _from_class_path_attr;
  57   time_t _timestamp;          // jar timestamp,  0 if is directory, modules image or other
  58   long   _filesize;           // jar/jimage file size, -1 if is directory, -2 if other
  59   Array<char>* _name;
  60   Array<u1>*   _manifest;
  61 
  62 public:
  63   void init(bool is_modules_image, ClassPathEntry* cpe, TRAPS);
  64   void metaspace_pointers_do(MetaspaceClosure* it);
  65   bool validate(bool is_class_path = true);
  66 
  67   // The _timestamp only gets set for jar files.
  68   bool has_timestamp() {
  69     return _timestamp != 0;
  70   }
  71   bool is_dir()            { return _type == dir_entry; }
  72   bool is_modules_image()  { return _type == modules_image_entry; }
  73   bool is_jar()            { return _type == jar_entry; }
  74   bool is_signed()         { return _type == signed_jar_entry; }
  75   void set_is_signed()     {
  76     _type = signed_jar_entry;
  77   }
  78   bool from_class_path_attr() { return _from_class_path_attr; }
  79   time_t timestamp() const { return _timestamp; }
  80   long   filesize()  const { return _filesize; }
  81   const char* name() const { return _name->data(); }
  82   const char* manifest() const {
  83     return (_manifest == NULL) ? NULL : (const char*)_manifest->data();
  84   }
  85   int manifest_size() const {
  86     return (_manifest == NULL) ? 0 : _manifest->length();
  87   }
  88   void set_manifest(Array<u1>* manifest) {
  89     _manifest = manifest;
  90   }
  91 };
  92 
  93 struct ArchiveHeapOopmapInfo {
  94   address _oopmap;               // bitmap for relocating embedded oops
  95   size_t  _oopmap_size_in_bits;
  96 };
  97 
  98 class SharedPathTable {
  99   Array<u8>* _table;
 100   int _size;
 101 public:
 102   void dumptime_init(ClassLoaderData* loader_data, Thread* THREAD);
 103   void metaspace_pointers_do(MetaspaceClosure* it);
 104 
 105   int size() {
 106     return _size;
 107   }
 108   SharedClassPathEntry* path_at(int index) {
 109     if (index < 0) {
 110       return NULL;
 111     }
 112     assert(index < _size, "sanity");
 113     char* p = (char*)_table->data();
 114     p += sizeof(SharedClassPathEntry) * index;
 115     return (SharedClassPathEntry*)p;
 116   }
 117   Array<u8>* table() {return _table;}
 118   void set_table(Array<u8>* table) {_table = table;}
 119 
 120 };
 121 
 122 struct FileMapHeader : public CDSFileMapHeaderBase {
 123   size_t _header_size;
 124   size_t _alignment;                // how shared archive should be aligned
 125   int    _obj_alignment;            // value of ObjectAlignmentInBytes
 126   address _narrow_oop_base;         // compressed oop encoding base
 127   int    _narrow_oop_shift;         // compressed oop encoding shift
 128   bool    _compact_strings;         // value of CompactStrings
 129   uintx  _max_heap_size;            // java max heap size during dumping
 130   CompressedOops::Mode _narrow_oop_mode; // compressed oop encoding mode
 131   int     _narrow_klass_shift;      // save narrow klass base and shift
 132   address _narrow_klass_base;
 133   char*   _misc_data_patching_start;
 134   char*   _read_only_tables_start;
 135   address _cds_i2i_entry_code_buffers;
 136   size_t  _cds_i2i_entry_code_buffers_size;
 137   size_t  _core_spaces_size;        // number of bytes allocated by the core spaces
 138                                     // (mc, md, ro, rw and od).
 139   MemRegion _heap_reserved;         // reserved region for the entire heap at dump time.
 140   bool _base_archive_is_default;    // indicates if the base archive is the system default one
 141 
 142   // The following fields are all sanity checks for whether this archive
 143   // will function correctly with this JVM and the bootclasspath it's
 144   // invoked with.
 145   char  _jvm_ident[JVM_IDENT_MAX];      // identifier for jvm
 146 
 147   // size of the base archive name including NULL terminator
 148   int _base_archive_name_size;
 149 
 150   // The _paths_misc_info is a variable-size structure that records "miscellaneous"
 151   // information during dumping. It is generated and validated by the
 152   // SharedPathsMiscInfo class. See SharedPathsMiscInfo.hpp for
 153   // detailed description.
 154   //
 155   // The _paths_misc_info data is stored as a byte array in the archive file header,
 156   // immediately after the _header field. This information is used only when
 157   // checking the validity of the archive and is deallocated after the archive is loaded.
 158   //
 159   // Note that the _paths_misc_info does NOT include information for JAR files
 160   // that existed during dump time. Their information is stored in _shared_path_table.
 161   int _paths_misc_info_size;
 162 
 163   // The following is a table of all the class path entries that were used
 164   // during dumping. At run time, we require these files to exist and have the same
 165   // size/modification time, or else the archive will refuse to load.
 166   //
 167   // All of these entries must be JAR files. The dumping process would fail if a non-empty
 168   // directory was specified in the classpaths. If an empty directory was specified
 169   // it is checked by the _paths_misc_info as described above.
 170   //
 171   // FIXME -- if JAR files in the tail of the list were specified but not used during dumping,
 172   // they should be removed from this table, to save space and to avoid spurious
 173   // loading failures during runtime.
 174   SharedPathTable _shared_path_table;
 175 
 176   jshort _app_class_paths_start_index;  // Index of first app classpath entry
 177   jshort _app_module_paths_start_index; // Index of first module path entry
 178   jshort _num_module_paths;             // number of module path entries
 179   jshort _max_used_path_index;          // max path index referenced during CDS dump
 180   bool   _verify_local;                 // BytecodeVerificationLocal setting
 181   bool   _verify_remote;                // BytecodeVerificationRemote setting
 182   bool   _has_platform_or_app_classes;  // Archive contains app classes
 183   size_t _shared_base_address;          // SharedBaseAddress used at dump time
 184   bool   _allow_archiving_with_java_agent; // setting of the AllowArchivingWithJavaAgent option
 185 
 186   void set_has_platform_or_app_classes(bool v) {
 187     _has_platform_or_app_classes = v;
 188   }
 189   bool has_platform_or_app_classes() { return _has_platform_or_app_classes; }
 190   jshort max_used_path_index()       { return _max_used_path_index; }
 191   jshort app_module_paths_start_index() { return _app_module_paths_start_index; }
 192 
 193   bool validate();
 194   int compute_crc();
 195 
 196   CDSFileMapRegion* space_at(int i) {
 197     assert(i >= 0 && i < NUM_CDS_REGIONS, "invalid region");
 198     return &_space[i];
 199   }
 200 public:
 201   void populate(FileMapInfo* info, size_t alignment);
 202 };
 203 
 204 class FileMapInfo : public CHeapObj<mtInternal> {
 205 private:
 206   friend class ManifestStream;
 207   friend class VMStructs;
 208   friend struct FileMapHeader;
 209 
 210   bool    _is_static;
 211   bool    _file_open;
 212   int     _fd;
 213   size_t  _file_offset;
 214 
 215 private:
 216   // TODO: Probably change the following to be non-static
 217   static SharedPathTable       _shared_path_table;
 218   static bool                  _validating_shared_path_table;
 219 
 220   // FileMapHeader describes the shared space data in the file to be
 221   // mapped.  This structure gets written to a file.  It is not a class, so
 222   // that the compilers don't add any compiler-private data to it.
 223 
 224 public:
 225   struct FileMapHeaderBase : public CHeapObj<mtClass> {
 226     // Need to put something here. Otherwise, in product build, because CHeapObj has no virtual
 227     // methods, we would get sizeof(FileMapHeaderBase) == 1 with gcc.
 228     intx _dummy;
 229   };
 230 
 231 
 232   FileMapHeader * _header;
 233 
 234   const char* _full_path;
 235   char* _paths_misc_info;
 236   char* _base_archive_name;
 237 
 238   static FileMapInfo* _current_info;
 239   static FileMapInfo* _dynamic_archive_info;
 240   static bool _heap_pointers_need_patching;
 241   static bool _memory_mapping_failed;
 242   static bool get_base_archive_name_from_header(const char* archive_name,
 243                                                 int* size, char** base_archive_name);
 244   static bool check_archive(const char* archive_name, bool is_static);
 245   void restore_shared_path_table();
 246   bool  init_from_file(int fd, bool is_static);
 247   static void metaspace_pointers_do(MetaspaceClosure* it);
 248 
 249 public:
 250   FileMapInfo(bool is_static);
 251   ~FileMapInfo();
 252 
 253   int    compute_header_crc()         { return _header->compute_crc(); }
 254   void   set_header_crc(int crc)      { _header->_crc = crc; }
 255   int    space_crc(int i)             { return space_at(i)->_crc; }
 256   void   populate_header(size_t alignment);
 257   bool   validate_header(bool is_static);
 258   void   invalidate();
 259   int    crc()                        { return _header->_crc; }
 260   int    version()                    { return _header->_version; }
 261   size_t alignment()                  { return _header->_alignment; }
 262   CompressedOops::Mode narrow_oop_mode() { return _header->_narrow_oop_mode; }
 263   address narrow_oop_base()    const  { return _header->_narrow_oop_base; }
 264   int     narrow_oop_shift()   const  { return _header->_narrow_oop_shift; }
 265   uintx   max_heap_size()      const  { return _header->_max_heap_size; }
 266   address narrow_klass_base()  const  { return _header->_narrow_klass_base; }
 267   int     narrow_klass_shift() const  { return _header->_narrow_klass_shift; }
 268   struct  FileMapHeader* header()     { return _header; }
 269   char*   misc_data_patching_start()          { return _header->_misc_data_patching_start; }
 270   void set_misc_data_patching_start(char* p)  { _header->_misc_data_patching_start = p; }
 271   char* read_only_tables_start()              { return _header->_read_only_tables_start; }
 272   void set_read_only_tables_start(char* p)    { _header->_read_only_tables_start = p; }
 273 
 274   bool  is_file_position_aligned() const;
 275   void  align_file_position();
 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 set_current_info(FileMapInfo* info) {
 298     CDS_ONLY(_current_info = info;)
 299   }
 300 
 301   static FileMapInfo* dynamic_info() {
 302     CDS_ONLY(return _dynamic_archive_info;)
 303     NOT_CDS(return NULL;)
 304   }
 305 
 306   static void assert_mark(bool check);
 307 
 308   // File manipulation.
 309   bool  initialize(bool is_static) NOT_CDS_RETURN_(false);
 310   bool  open_for_read(const char* path = NULL);
 311   void  open_for_write(const char* path = NULL);
 312   void  write_header();
 313   void  write_region(int region, char* base, size_t size,
 314                      bool read_only, bool allow_exec);
 315   size_t write_archive_heap_regions(GrowableArray<MemRegion> *heap_mem,
 316                                     GrowableArray<ArchiveHeapOopmapInfo> *oopmaps,
 317                                     int first_region_id, int max_num_regions,
 318                                     bool print_log);
 319   void  write_bytes(const void* buffer, size_t count);
 320   void  write_bytes_aligned(const void* buffer, size_t count);
 321   size_t  read_bytes(void* buffer, size_t count);
 322   char* map_regions(int regions[], char* saved_base[], size_t len);
 323   char* map_region(int i, char** top_ret);
 324   void  map_heap_regions_impl() NOT_CDS_JAVA_HEAP_RETURN;
 325   void  map_heap_regions() NOT_CDS_JAVA_HEAP_RETURN;
 326   void  fixup_mapped_heap_regions() NOT_CDS_JAVA_HEAP_RETURN;
 327   void  patch_archived_heap_embedded_pointers() NOT_CDS_JAVA_HEAP_RETURN;
 328   void  patch_archived_heap_embedded_pointers(MemRegion* ranges, int num_ranges,
 329                                               int first_region_idx) NOT_CDS_JAVA_HEAP_RETURN;
 330   bool  has_heap_regions()  NOT_CDS_JAVA_HEAP_RETURN_(false);
 331   MemRegion get_heap_regions_range_with_current_oop_encoding_mode() NOT_CDS_JAVA_HEAP_RETURN_(MemRegion());
 332   void  unmap_regions(int regions[], char* saved_base[], size_t len);
 333   void  unmap_region(int i);
 334   bool  verify_region_checksum(int i);
 335   void  close();
 336   bool  is_open() { return _file_open; }
 337   ReservedSpace reserve_shared_memory();
 338 
 339   // JVM/TI RedefineClasses() support:
 340   // Remap the shared readonly space to shared readwrite, private.
 341   bool  remap_shared_readonly_as_readwrite();
 342 
 343   // Errors.
 344   static void fail_stop(const char *msg, ...) ATTRIBUTE_PRINTF(1, 2);
 345   static void fail_continue(const char *msg, ...) ATTRIBUTE_PRINTF(1, 2);
 346   static bool memory_mapping_failed() {
 347     CDS_ONLY(return _memory_mapping_failed;)
 348     NOT_CDS(return false;)
 349   }
 350   bool is_in_shared_region(const void* p, int idx) NOT_CDS_RETURN_(false);
 351 
 352   // Stop CDS sharing and unmap CDS regions.
 353   static void stop_sharing_and_unmap(const char* msg);
 354 
 355   static void allocate_shared_path_table();
 356   static void check_nonempty_dir_in_shared_path_table();
 357   bool validate_shared_path_table();
 358   static void update_shared_classpath(ClassPathEntry *cpe, SharedClassPathEntry* ent, TRAPS);
 359 
 360 #if INCLUDE_JVMTI
 361   static ClassFileStream* open_stream_for_jvmti(InstanceKlass* ik, Handle class_loader, TRAPS);
 362 #endif
 363 
 364   static SharedClassPathEntry* shared_path(int index) {
 365     return _shared_path_table.path_at(index);
 366   }
 367 
 368   static const char* shared_path_name(int index) {
 369     assert(index >= 0, "Sanity");
 370     return shared_path(index)->name();
 371   }
 372 
 373   static int get_number_of_shared_paths() {
 374     return _shared_path_table.size();
 375   }
 376 
 377   char* region_addr(int idx);
 378 
 379  private:
 380   char* skip_first_path_entry(const char* path) NOT_CDS_RETURN_(NULL);
 381   int   num_paths(const char* path) NOT_CDS_RETURN_(0);
 382   GrowableArray<char*>* create_path_array(const char* path) NOT_CDS_RETURN_(NULL);
 383   bool  fail(const char* msg, const char* name) NOT_CDS_RETURN_(false);
 384   bool  check_paths(int shared_path_start_idx,
 385                     int num_paths,
 386                     GrowableArray<char*>* rp_array) NOT_CDS_RETURN_(false);
 387   bool  validate_boot_class_paths() NOT_CDS_RETURN_(false);
 388   bool  validate_app_class_paths(int shared_app_paths_len) NOT_CDS_RETURN_(false);
 389   bool  map_heap_data(MemRegion **heap_mem, int first, int max, int* num,
 390                       bool is_open = false) NOT_CDS_JAVA_HEAP_RETURN_(false);
 391   bool  region_crc_check(char* buf, size_t size, int expected_crc) NOT_CDS_RETURN_(false);
 392   void  dealloc_archive_heap_regions(MemRegion* regions, int num, bool is_open) NOT_CDS_JAVA_HEAP_RETURN;
 393 
 394   CDSFileMapRegion* space_at(int i) {
 395     return _header->space_at(i);
 396   }
 397 
 398   narrowOop offset_of_space(CDSFileMapRegion* spc) {
 399     return (narrowOop)(spc->_addr._offset);
 400   }
 401 
 402   // The starting address of spc, as calculated with CompressedOop::decode_non_null()
 403   address start_address_as_decoded_with_current_oop_encoding_mode(CDSFileMapRegion* spc) {
 404     return decode_start_address(spc, true);
 405   }
 406 
 407   // The starting address of spc, as calculated with HeapShared::decode_from_archive()
 408   address start_address_as_decoded_from_archive(CDSFileMapRegion* spc) {
 409     return decode_start_address(spc, false);
 410   }
 411 
 412   address decode_start_address(CDSFileMapRegion* spc, bool with_current_oop_encoding_mode);
 413 
 414 #if INCLUDE_JVMTI
 415   static ClassPathEntry** _classpath_entries_for_jvmti;
 416   static ClassPathEntry* get_classpath_entry_for_jvmti(int i, TRAPS);
 417 #endif
 418 };
 419 
 420 #endif // SHARE_MEMORY_FILEMAP_HPP