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