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 boot/app/module path entries that were used 157 // during dumping. At run time, we we validate these entries according to their 158 // SharedClassPathEntry::_type. See: 159 // check_nonempty_dir_in_shared_path_table() 160 // validate_shared_path_table() 161 // validate_non_existent_class_paths() 162 SharedPathTable _shared_path_table; 163 164 jshort _app_class_paths_start_index; // Index of first app classpath entry 165 jshort _app_module_paths_start_index; // Index of first module path entry 166 jshort _num_module_paths; // number of module path entries 167 jshort _max_used_path_index; // max path index referenced during CDS dump 168 bool _verify_local; // BytecodeVerificationLocal setting 169 bool _verify_remote; // BytecodeVerificationRemote setting 170 bool _has_platform_or_app_classes; // Archive contains app classes 171 size_t _shared_base_address; // SharedBaseAddress used at dump time 172 bool _allow_archiving_with_java_agent; // setting of the AllowArchivingWithJavaAgent option 173 174 void set_has_platform_or_app_classes(bool v) { 175 _has_platform_or_app_classes = v; 176 } 177 bool has_platform_or_app_classes() { return _has_platform_or_app_classes; } 178 jshort max_used_path_index() { return _max_used_path_index; } 179 jshort app_module_paths_start_index() { return _app_module_paths_start_index; } 180 181 bool validate(); 182 int compute_crc(); 183 184 CDSFileMapRegion* space_at(int i) { 185 assert(i >= 0 && i < NUM_CDS_REGIONS, "invalid region"); 186 return &_space[i]; 187 } 188 public: 189 void populate(FileMapInfo* info, size_t alignment); 190 }; 191 192 class FileMapInfo : public CHeapObj<mtInternal> { 193 private: 194 friend class ManifestStream; 195 friend class VMStructs; 196 friend struct FileMapHeader; 197 198 bool _is_static; 199 bool _file_open; 200 int _fd; 201 size_t _file_offset; 202 203 private: 204 // TODO: Probably change the following to be non-static 205 static SharedPathTable _shared_path_table; 206 static bool _validating_shared_path_table; 207 208 // FileMapHeader describes the shared space data in the file to be 209 // mapped. This structure gets written to a file. It is not a class, so 210 // that the compilers don't add any compiler-private data to it. 211 212 public: 213 struct FileMapHeaderBase : public CHeapObj<mtClass> { 214 // Need to put something here. Otherwise, in product build, because CHeapObj has no virtual 215 // methods, we would get sizeof(FileMapHeaderBase) == 1 with gcc. 216 intx _dummy; 217 }; 218 219 220 FileMapHeader * _header; 221 222 const char* _full_path; 223 char* _base_archive_name; 224 225 static FileMapInfo* _current_info; 226 static FileMapInfo* _dynamic_archive_info; 227 static bool _heap_pointers_need_patching; 228 static bool _memory_mapping_failed; 229 static GrowableArray<const char*>* _non_existent_class_paths; 230 231 static bool get_base_archive_name_from_header(const char* archive_name, 232 int* size, char** base_archive_name); 233 static bool check_archive(const char* archive_name, bool is_static); 234 void restore_shared_path_table(); 235 bool init_from_file(int fd, bool is_static); 236 static void metaspace_pointers_do(MetaspaceClosure* it); 237 238 void log_paths(const char* msg, int start_idx, int end_idx); 239 240 public: 241 FileMapInfo(bool is_static); 242 ~FileMapInfo(); 243 244 int compute_header_crc() { return _header->compute_crc(); } 245 void set_header_crc(int crc) { _header->_crc = crc; } 246 int space_crc(int i) { return space_at(i)->_crc; } 247 void populate_header(size_t alignment); 248 bool validate_header(bool is_static); 249 void invalidate(); 250 int crc() { return _header->_crc; } 251 int version() { return _header->_version; } 252 size_t alignment() { return _header->_alignment; } 253 CompressedOops::Mode narrow_oop_mode() { return _header->_narrow_oop_mode; } 254 address narrow_oop_base() const { return _header->_narrow_oop_base; } 255 int narrow_oop_shift() const { return _header->_narrow_oop_shift; } 256 uintx max_heap_size() const { return _header->_max_heap_size; } 257 address narrow_klass_base() const { return _header->_narrow_klass_base; } 258 int narrow_klass_shift() const { return _header->_narrow_klass_shift; } 259 struct FileMapHeader* header() { return _header; } 260 char* misc_data_patching_start() { return _header->_misc_data_patching_start; } 261 void set_misc_data_patching_start(char* p) { _header->_misc_data_patching_start = p; } 262 char* read_only_tables_start() { return _header->_read_only_tables_start; } 263 void set_read_only_tables_start(char* p) { _header->_read_only_tables_start = p; } 264 265 bool is_file_position_aligned() const; 266 void align_file_position(); 267 268 address cds_i2i_entry_code_buffers() { 269 return _header->_cds_i2i_entry_code_buffers; 270 } 271 void set_cds_i2i_entry_code_buffers(address addr) { 272 _header->_cds_i2i_entry_code_buffers = addr; 273 } 274 size_t cds_i2i_entry_code_buffers_size() { 275 return _header->_cds_i2i_entry_code_buffers_size; 276 } 277 void set_cds_i2i_entry_code_buffers_size(size_t s) { 278 _header->_cds_i2i_entry_code_buffers_size = s; 279 } 280 void set_core_spaces_size(size_t s) { _header->_core_spaces_size = s; } 281 size_t core_spaces_size() { return _header->_core_spaces_size; } 282 283 static FileMapInfo* current_info() { 284 CDS_ONLY(return _current_info;) 285 NOT_CDS(return NULL;) 286 } 287 288 static void set_current_info(FileMapInfo* info) { 289 CDS_ONLY(_current_info = info;) 290 } 291 292 static FileMapInfo* dynamic_info() { 293 CDS_ONLY(return _dynamic_archive_info;) 294 NOT_CDS(return NULL;) 295 } 296 297 static void assert_mark(bool check); 298 299 // File manipulation. 300 bool initialize(bool is_static) NOT_CDS_RETURN_(false); 301 bool open_for_read(const char* path = NULL); 302 void open_for_write(const char* path = NULL); 303 void write_header(); 304 void write_region(int region, char* base, size_t size, 305 bool read_only, bool allow_exec); 306 size_t write_archive_heap_regions(GrowableArray<MemRegion> *heap_mem, 307 GrowableArray<ArchiveHeapOopmapInfo> *oopmaps, 308 int first_region_id, int max_num_regions, 309 bool print_log); 310 void write_bytes(const void* buffer, size_t count); 311 void write_bytes_aligned(const void* buffer, size_t count); 312 size_t read_bytes(void* buffer, size_t count); 313 char* map_regions(int regions[], char* saved_base[], size_t len); 314 char* map_region(int i, char** top_ret); 315 void map_heap_regions_impl() NOT_CDS_JAVA_HEAP_RETURN; 316 void map_heap_regions() NOT_CDS_JAVA_HEAP_RETURN; 317 void fixup_mapped_heap_regions() NOT_CDS_JAVA_HEAP_RETURN; 318 void patch_archived_heap_embedded_pointers() NOT_CDS_JAVA_HEAP_RETURN; 319 void patch_archived_heap_embedded_pointers(MemRegion* ranges, int num_ranges, 320 int first_region_idx) NOT_CDS_JAVA_HEAP_RETURN; 321 bool has_heap_regions() NOT_CDS_JAVA_HEAP_RETURN_(false); 322 MemRegion get_heap_regions_range_with_current_oop_encoding_mode() NOT_CDS_JAVA_HEAP_RETURN_(MemRegion()); 323 void unmap_regions(int regions[], char* saved_base[], size_t len); 324 void unmap_region(int i); 325 bool verify_region_checksum(int i); 326 void close(); 327 bool is_open() { return _file_open; } 328 ReservedSpace reserve_shared_memory(); 329 330 // JVM/TI RedefineClasses() support: 331 // Remap the shared readonly space to shared readwrite, private. 332 bool remap_shared_readonly_as_readwrite(); 333 334 // Errors. 335 static void fail_stop(const char *msg, ...) ATTRIBUTE_PRINTF(1, 2); 336 static void fail_continue(const char *msg, ...) ATTRIBUTE_PRINTF(1, 2); 337 static bool memory_mapping_failed() { 338 CDS_ONLY(return _memory_mapping_failed;) 339 NOT_CDS(return false;) 340 } 341 bool is_in_shared_region(const void* p, int idx) NOT_CDS_RETURN_(false); 342 343 // Stop CDS sharing and unmap CDS regions. 344 static void stop_sharing_and_unmap(const char* msg); 345 346 static void allocate_shared_path_table(); 347 static int add_shared_classpaths(int i, const char* which, ClassPathEntry *cpe, TRAPS); 348 static void check_nonempty_dir_in_shared_path_table(); 349 bool validate_shared_path_table(); 350 void validate_non_existent_class_paths(); 351 static void update_jar_manifest(ClassPathEntry *cpe, SharedClassPathEntry* ent, TRAPS); 352 static int num_non_existent_class_paths(); 353 static void record_non_existent_class_path_entry(const char* path); 354 355 #if INCLUDE_JVMTI 356 static ClassFileStream* open_stream_for_jvmti(InstanceKlass* ik, Handle class_loader, TRAPS); 357 #endif 358 359 static SharedClassPathEntry* shared_path(int index) { 360 return _shared_path_table.path_at(index); 361 } 362 363 static const char* shared_path_name(int index) { 364 assert(index >= 0, "Sanity"); 365 return shared_path(index)->name(); 366 } 367 368 static int get_number_of_shared_paths() { 369 return _shared_path_table.size(); 370 } 371 372 char* region_addr(int idx); 373 374 private: 375 char* skip_first_path_entry(const char* path) NOT_CDS_RETURN_(NULL); 376 int num_paths(const char* path) NOT_CDS_RETURN_(0); 377 GrowableArray<const char*>* create_path_array(const char* path) NOT_CDS_RETURN_(NULL); 378 bool fail(const char* msg, const char* name) NOT_CDS_RETURN_(false); 379 bool check_paths(int shared_path_start_idx, int num_paths, 380 GrowableArray<const char*>* rp_array) NOT_CDS_RETURN_(false); 381 bool validate_boot_class_paths() NOT_CDS_RETURN_(false); 382 bool validate_app_class_paths(int shared_app_paths_len) NOT_CDS_RETURN_(false); 383 bool map_heap_data(MemRegion **heap_mem, int first, int max, int* num, 384 bool is_open = false) NOT_CDS_JAVA_HEAP_RETURN_(false); 385 bool region_crc_check(char* buf, size_t size, int expected_crc) NOT_CDS_RETURN_(false); 386 void dealloc_archive_heap_regions(MemRegion* regions, int num, bool is_open) NOT_CDS_JAVA_HEAP_RETURN; 387 388 CDSFileMapRegion* space_at(int i) { 389 return _header->space_at(i); 390 } 391 392 narrowOop offset_of_space(CDSFileMapRegion* spc) { 393 return (narrowOop)(spc->_addr._offset); 394 } 395 396 // The starting address of spc, as calculated with CompressedOop::decode_non_null() 397 address start_address_as_decoded_with_current_oop_encoding_mode(CDSFileMapRegion* spc) { 398 return decode_start_address(spc, true); 399 } 400 401 // The starting address of spc, as calculated with HeapShared::decode_from_archive() 402 address start_address_as_decoded_from_archive(CDSFileMapRegion* spc) { 403 return decode_start_address(spc, false); 404 } 405 406 address decode_start_address(CDSFileMapRegion* spc, bool with_current_oop_encoding_mode); 407 408 #if INCLUDE_JVMTI 409 static ClassPathEntry** _classpath_entries_for_jvmti; 410 static ClassPathEntry* get_classpath_entry_for_jvmti(int i, TRAPS); 411 #endif 412 }; 413 414 #endif // SHARE_MEMORY_FILEMAP_HPP