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 class SharedClassPathEntry { 45 enum { 46 modules_image_entry, 47 jar_entry, 48 signed_jar_entry, 49 dir_entry, 50 unknown_entry 51 }; 52 protected: 53 u1 _type; 54 bool _from_class_path_attr; 55 time_t _timestamp; // jar timestamp, 0 if is directory, modules image or other 56 long _filesize; // jar/jimage file size, -1 if is directory, -2 if other 57 Array<char>* _name; 58 Array<u1>* _manifest; 59 60 public: 61 void init(bool is_modules_image, ClassPathEntry* cpe, TRAPS); 62 void metaspace_pointers_do(MetaspaceClosure* it); 63 bool validate(bool is_class_path = true); 64 65 // The _timestamp only gets set for jar files. 66 bool has_timestamp() { 67 return _timestamp != 0; 68 } 69 bool is_dir() { return _type == dir_entry; } 70 bool is_modules_image() { return _type == modules_image_entry; } 71 bool is_jar() { return _type == jar_entry; } 72 bool is_signed() { return _type == signed_jar_entry; } 73 void set_is_signed() { 74 _type = signed_jar_entry; 75 } 76 bool from_class_path_attr() { return _from_class_path_attr; } 77 time_t timestamp() const { return _timestamp; } 78 long filesize() const { return _filesize; } 79 const char* name() const { return _name->data(); } 80 const char* manifest() const { 81 return (_manifest == NULL) ? NULL : (const char*)_manifest->data(); 82 } 83 int manifest_size() const { 84 return (_manifest == NULL) ? 0 : _manifest->length(); 85 } 86 void set_manifest(Array<u1>* manifest) { 87 _manifest = manifest; 88 } 89 }; 90 91 struct ArchiveHeapOopmapInfo { 92 address _oopmap; // bitmap for relocating embedded oops 93 size_t _oopmap_size_in_bits; 94 }; 95 96 class SharedPathTable { 97 Array<u8>* _table; 98 int _size; 99 public: 100 void dumptime_init(ClassLoaderData* loader_data, Thread* THREAD); 101 void metaspace_pointers_do(MetaspaceClosure* it); 102 103 int size() { 104 return _size; 105 } 106 SharedClassPathEntry* path_at(int index) { 107 if (index < 0) { 108 return NULL; 109 } 110 assert(index < _size, "sanity"); 111 char* p = (char*)_table->data(); 112 p += sizeof(SharedClassPathEntry) * index; 113 return (SharedClassPathEntry*)p; 114 } 115 Array<u8>* table() {return _table;} 116 void set_table(Array<u8>* table) {_table = table;} 117 118 }; 119 120 struct FileMapHeader : public CDSFileMapHeaderBase { 121 size_t _header_size; 122 size_t _alignment; // how shared archive should be aligned 123 int _obj_alignment; // value of ObjectAlignmentInBytes 124 address _narrow_oop_base; // compressed oop encoding base 125 int _narrow_oop_shift; // compressed oop encoding shift 126 bool _compact_strings; // value of CompactStrings 127 uintx _max_heap_size; // java max heap size during dumping 128 CompressedOops::Mode _narrow_oop_mode; // compressed oop encoding mode 129 int _narrow_klass_shift; // save narrow klass base and shift 130 address _narrow_klass_base; 131 char* _misc_data_patching_start; 132 char* _read_only_tables_start; 133 address _cds_i2i_entry_code_buffers; 134 size_t _cds_i2i_entry_code_buffers_size; 135 size_t _core_spaces_size; // number of bytes allocated by the core spaces 136 // (mc, md, ro, rw and od). 137 MemRegion _heap_reserved; // reserved region for the entire heap at dump time. 138 bool _base_archive_is_default; // indicates if the base archive is the system default one 139 140 // size of the base archive name including NULL terminator 141 int _base_archive_name_size; 142 143 // The _paths_misc_info is a variable-size structure that records "miscellaneous" 144 // information during dumping. It is generated and validated by the 145 // SharedPathsMiscInfo class. See SharedPathsMiscInfo.hpp for 146 // detailed description. 147 // 148 // The _paths_misc_info data is stored as a byte array in the archive file header, 149 // immediately after the _header field. This information is used only when 150 // checking the validity of the archive and is deallocated after the archive is loaded. 151 // 152 // Note that the _paths_misc_info does NOT include information for JAR files 153 // that existed during dump time. Their information is stored in _shared_path_table. 154 int _paths_misc_info_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 bool get_base_archive_name_from_header(const char* archive_name, 236 int* size, char** base_archive_name); 237 static bool check_archive(const char* archive_name, bool is_static); 238 void restore_shared_path_table(); 239 bool init_from_file(int fd, bool is_static); 240 static void metaspace_pointers_do(MetaspaceClosure* it); 241 242 public: 243 FileMapInfo(bool is_static); 244 ~FileMapInfo(); 245 246 int compute_header_crc() { return _header->compute_crc(); } 247 void set_header_crc(int crc) { _header->_crc = crc; } 248 int space_crc(int i) { return space_at(i)->_crc; } 249 void populate_header(size_t alignment); 250 bool validate_header(bool is_static); 251 void invalidate(); 252 int crc() { return _header->_crc; } 253 int version() { return _header->_version; } 254 size_t alignment() { return _header->_alignment; } 255 CompressedOops::Mode narrow_oop_mode() { return _header->_narrow_oop_mode; } 256 address narrow_oop_base() const { return _header->_narrow_oop_base; } 257 int narrow_oop_shift() const { return _header->_narrow_oop_shift; } 258 uintx max_heap_size() const { return _header->_max_heap_size; } 259 address narrow_klass_base() const { return _header->_narrow_klass_base; } 260 int narrow_klass_shift() const { return _header->_narrow_klass_shift; } 261 struct FileMapHeader* header() { return _header; } 262 char* misc_data_patching_start() { return _header->_misc_data_patching_start; } 263 void set_misc_data_patching_start(char* p) { _header->_misc_data_patching_start = p; } 264 char* read_only_tables_start() { return _header->_read_only_tables_start; } 265 void set_read_only_tables_start(char* p) { _header->_read_only_tables_start = p; } 266 267 bool is_file_position_aligned() const; 268 void align_file_position(); 269 270 address cds_i2i_entry_code_buffers() { 271 return _header->_cds_i2i_entry_code_buffers; 272 } 273 void set_cds_i2i_entry_code_buffers(address addr) { 274 _header->_cds_i2i_entry_code_buffers = addr; 275 } 276 size_t cds_i2i_entry_code_buffers_size() { 277 return _header->_cds_i2i_entry_code_buffers_size; 278 } 279 void set_cds_i2i_entry_code_buffers_size(size_t s) { 280 _header->_cds_i2i_entry_code_buffers_size = s; 281 } 282 void set_core_spaces_size(size_t s) { _header->_core_spaces_size = s; } 283 size_t core_spaces_size() { return _header->_core_spaces_size; } 284 285 static FileMapInfo* current_info() { 286 CDS_ONLY(return _current_info;) 287 NOT_CDS(return NULL;) 288 } 289 290 static void set_current_info(FileMapInfo* info) { 291 CDS_ONLY(_current_info = info;) 292 } 293 294 static FileMapInfo* dynamic_info() { 295 CDS_ONLY(return _dynamic_archive_info;) 296 NOT_CDS(return NULL;) 297 } 298 299 static void assert_mark(bool check); 300 301 // File manipulation. 302 bool initialize(bool is_static) NOT_CDS_RETURN_(false); 303 bool open_for_read(const char* path = NULL); 304 void open_for_write(const char* path = NULL); 305 void write_header(); 306 void write_region(int region, char* base, size_t size, 307 bool read_only, bool allow_exec); 308 size_t write_archive_heap_regions(GrowableArray<MemRegion> *heap_mem, 309 GrowableArray<ArchiveHeapOopmapInfo> *oopmaps, 310 int first_region_id, int max_num_regions, 311 bool print_log); 312 void write_bytes(const void* buffer, size_t count); 313 void write_bytes_aligned(const void* buffer, size_t count); 314 size_t read_bytes(void* buffer, size_t count); 315 char* map_regions(int regions[], char* saved_base[], size_t len); 316 char* map_region(int i, char** top_ret); 317 void map_heap_regions_impl() NOT_CDS_JAVA_HEAP_RETURN; 318 void map_heap_regions() NOT_CDS_JAVA_HEAP_RETURN; 319 void fixup_mapped_heap_regions() NOT_CDS_JAVA_HEAP_RETURN; 320 void patch_archived_heap_embedded_pointers() NOT_CDS_JAVA_HEAP_RETURN; 321 void patch_archived_heap_embedded_pointers(MemRegion* ranges, int num_ranges, 322 int first_region_idx) NOT_CDS_JAVA_HEAP_RETURN; 323 bool has_heap_regions() NOT_CDS_JAVA_HEAP_RETURN_(false); 324 MemRegion get_heap_regions_range_with_current_oop_encoding_mode() NOT_CDS_JAVA_HEAP_RETURN_(MemRegion()); 325 void unmap_regions(int regions[], char* saved_base[], size_t len); 326 void unmap_region(int i); 327 bool verify_region_checksum(int i); 328 void close(); 329 bool is_open() { return _file_open; } 330 ReservedSpace reserve_shared_memory(); 331 332 // JVM/TI RedefineClasses() support: 333 // Remap the shared readonly space to shared readwrite, private. 334 bool remap_shared_readonly_as_readwrite(); 335 336 // Errors. 337 static void fail_stop(const char *msg, ...) ATTRIBUTE_PRINTF(1, 2); 338 static void fail_continue(const char *msg, ...) ATTRIBUTE_PRINTF(1, 2); 339 static bool memory_mapping_failed() { 340 CDS_ONLY(return _memory_mapping_failed;) 341 NOT_CDS(return false;) 342 } 343 bool is_in_shared_region(const void* p, int idx) NOT_CDS_RETURN_(false); 344 345 // Stop CDS sharing and unmap CDS regions. 346 static void stop_sharing_and_unmap(const char* msg); 347 348 static void allocate_shared_path_table(); 349 static void check_nonempty_dir_in_shared_path_table(); 350 bool validate_shared_path_table(); 351 static void update_shared_classpath(ClassPathEntry *cpe, SharedClassPathEntry* ent, TRAPS); 352 353 #if INCLUDE_JVMTI 354 static ClassFileStream* open_stream_for_jvmti(InstanceKlass* ik, Handle class_loader, TRAPS); 355 #endif 356 357 static SharedClassPathEntry* shared_path(int index) { 358 return _shared_path_table.path_at(index); 359 } 360 361 static const char* shared_path_name(int index) { 362 assert(index >= 0, "Sanity"); 363 return shared_path(index)->name(); 364 } 365 366 static int get_number_of_shared_paths() { 367 return _shared_path_table.size(); 368 } 369 370 char* region_addr(int idx); 371 372 private: 373 char* skip_first_path_entry(const char* path) NOT_CDS_RETURN_(NULL); 374 int num_paths(const char* path) NOT_CDS_RETURN_(0); 375 GrowableArray<char*>* create_path_array(const char* path) NOT_CDS_RETURN_(NULL); 376 bool fail(const char* msg, const char* name) NOT_CDS_RETURN_(false); 377 bool check_paths(int shared_path_start_idx, 378 int num_paths, 379 GrowableArray<char*>* rp_array) NOT_CDS_RETURN_(false); 380 bool validate_boot_class_paths() NOT_CDS_RETURN_(false); 381 bool validate_app_class_paths(int shared_app_paths_len) NOT_CDS_RETURN_(false); 382 bool map_heap_data(MemRegion **heap_mem, int first, int max, int* num, 383 bool is_open = false) NOT_CDS_JAVA_HEAP_RETURN_(false); 384 bool region_crc_check(char* buf, size_t size, int expected_crc) NOT_CDS_RETURN_(false); 385 void dealloc_archive_heap_regions(MemRegion* regions, int num, bool is_open) NOT_CDS_JAVA_HEAP_RETURN; 386 387 CDSFileMapRegion* space_at(int i) { 388 return _header->space_at(i); 389 } 390 391 narrowOop offset_of_space(CDSFileMapRegion* spc) { 392 return (narrowOop)(spc->_addr._offset); 393 } 394 395 // The starting address of spc, as calculated with CompressedOop::decode_non_null() 396 address start_address_as_decoded_with_current_oop_encoding_mode(CDSFileMapRegion* spc) { 397 return decode_start_address(spc, true); 398 } 399 400 // The starting address of spc, as calculated with HeapShared::decode_from_archive() 401 address start_address_as_decoded_from_archive(CDSFileMapRegion* spc) { 402 return decode_start_address(spc, false); 403 } 404 405 address decode_start_address(CDSFileMapRegion* spc, bool with_current_oop_encoding_mode); 406 407 #if INCLUDE_JVMTI 408 static ClassPathEntry** _classpath_entries_for_jvmti; 409 static ClassPathEntry* get_classpath_entry_for_jvmti(int i, TRAPS); 410 #endif 411 }; 412 413 #endif // SHARE_MEMORY_FILEMAP_HPP