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