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 #include "precompiled.hpp" 26 #include "classfile/classLoader.hpp" 27 #include "classfile/compactHashtable.inline.hpp" 28 #include "classfile/sharedClassUtil.hpp" 29 #include "classfile/symbolTable.hpp" 30 #include "classfile/systemDictionaryShared.hpp" 31 #include "classfile/altHashing.hpp" 32 #if INCLUDE_ALL_GCS 33 #include "gc/g1/g1CollectedHeap.hpp" 34 #endif 35 #include "logging/log.hpp" 36 #include "logging/logMessage.hpp" 37 #include "memory/filemap.hpp" 38 #include "memory/metadataFactory.hpp" 39 #include "memory/oopFactory.hpp" 40 #include "oops/objArrayOop.hpp" 41 #include "prims/jvmtiExport.hpp" 42 #include "runtime/arguments.hpp" 43 #include "runtime/java.hpp" 44 #include "runtime/os.hpp" 45 #include "runtime/vm_version.hpp" 46 #include "services/memTracker.hpp" 47 #include "utilities/defaultStream.hpp" 48 49 # include <sys/stat.h> 50 # include <errno.h> 51 52 #ifndef O_BINARY // if defined (Win32) use binary files. 53 #define O_BINARY 0 // otherwise do nothing. 54 #endif 55 56 extern address JVM_FunctionAtStart(); 57 extern address JVM_FunctionAtEnd(); 58 175 _alignment = alignment; 176 _obj_alignment = ObjectAlignmentInBytes; 177 _compact_strings = CompactStrings; 178 _narrow_oop_mode = Universe::narrow_oop_mode(); 179 _narrow_oop_shift = Universe::narrow_oop_shift(); 180 _max_heap_size = MaxHeapSize; 181 _narrow_klass_base = Universe::narrow_klass_base(); 182 _narrow_klass_shift = Universe::narrow_klass_shift(); 183 _classpath_entry_table_size = mapinfo->_classpath_entry_table_size; 184 _classpath_entry_table = mapinfo->_classpath_entry_table; 185 _classpath_entry_size = mapinfo->_classpath_entry_size; 186 187 // The following fields are for sanity checks for whether this archive 188 // will function correctly with this JVM and the bootclasspath it's 189 // invoked with. 190 191 // JVM version string ... changes on each build. 192 get_header_version(_jvm_ident); 193 } 194 195 void FileMapInfo::allocate_classpath_entry_table() { 196 int bytes = 0; 197 int count = 0; 198 char* strptr = NULL; 199 char* strptr_max = NULL; 200 Thread* THREAD = Thread::current(); 201 202 ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data(); 203 size_t entry_size = SharedClassUtil::shared_class_path_entry_size(); 204 205 for (int pass=0; pass<2; pass++) { 206 207 // Process the modular java runtime image first 208 ClassPathEntry* jrt_entry = ClassLoader::get_jrt_entry(); 209 assert(jrt_entry != NULL, 210 "No modular java runtime image present when allocating the CDS classpath entry table"); 211 const char *name = jrt_entry->name(); 212 int name_bytes = (int)(strlen(name) + 1); 213 if (pass == 0) { 214 count++; 215 bytes += (int)entry_size; 216 bytes += name_bytes; 217 log_info(class, path)("add main shared path for modular java runtime image %s", name); 218 } else { 219 // The java runtime image is always in slot 0 on the shared class path. 220 SharedClassPathEntry* ent = shared_classpath(0); 221 struct stat st; 222 if (os::stat(name, &st) == 0) { 223 ent->_timestamp = st.st_mtime; 224 ent->_filesize = st.st_size; 225 } 226 if (ent->_filesize == 0) { 227 // unknown 228 ent->_filesize = -2; 229 } 230 ent->_name = strptr; 231 assert(strptr + name_bytes <= strptr_max, "miscalculated buffer size"); 232 strncpy(strptr, name, (size_t)name_bytes); // name_bytes includes trailing 0. 233 strptr += name_bytes; 234 } 235 236 // Walk the appended entries, which includes the entries added for the classpath. 237 ClassPathEntry *cpe = ClassLoader::classpath_entry(1); 238 239 // Since the java runtime image is always in slot 0 on the shared class path, the 240 // appended entries are started at slot 1 immediately after. 241 for (int cur_entry = 1 ; cpe != NULL; cpe = cpe->next(), cur_entry++) { 242 const char *name = cpe->name(); 243 int name_bytes = (int)(strlen(name) + 1); 244 assert(!cpe->is_jrt(), "A modular java runtime image is present on the list of appended entries"); 245 246 if (pass == 0) { 247 count ++; 248 bytes += (int)entry_size; 249 bytes += name_bytes; 250 log_info(class, path)("add main shared path (%s) %s", (cpe->is_jar_file() ? "jar" : "dir"), name); 251 } else { 252 SharedClassPathEntry* ent = shared_classpath(cur_entry); 253 if (cpe->is_jar_file()) { 254 struct stat st; 255 if (os::stat(name, &st) != 0) { 256 // The file/dir must exist, or it would not have been added 257 // into ClassLoader::classpath_entry(). 258 // 259 // If we can't access a jar file in the boot path, then we can't 260 // make assumptions about where classes get loaded from. 261 FileMapInfo::fail_stop("Unable to open jar file %s.", name); 262 } 263 264 EXCEPTION_MARK; // The following call should never throw, but would exit VM on error. 265 SharedClassUtil::update_shared_classpath(cpe, ent, st.st_mtime, st.st_size, THREAD); 266 } else { 267 struct stat st; 268 if (os::stat(name, &st) == 0) { 269 if ((st.st_mode & S_IFMT) == S_IFDIR) { 270 if (!os::dir_is_empty(name)) { 271 ClassLoader::exit_with_path_failure( 272 "Cannot have non-empty directory in archived classpaths", name); 273 } 274 ent->_filesize = -1; 275 } 276 } 277 if (ent->_filesize == 0) { 278 // unknown 279 ent->_filesize = -2; 280 } 281 } 282 ent->_name = strptr; 283 if (strptr + name_bytes <= strptr_max) { 284 strncpy(strptr, name, (size_t)name_bytes); // name_bytes includes trailing 0. 285 strptr += name_bytes; 286 } else { 287 assert(0, "miscalculated buffer size"); 288 } 289 } 290 } 291 292 if (pass == 0) { 293 EXCEPTION_MARK; // The following call should never throw, but would exit VM on error. 294 Array<u8>* arr = MetadataFactory::new_array<u8>(loader_data, (bytes + 7)/8, THREAD); 295 strptr = (char*)(arr->data()); 296 strptr_max = strptr + bytes; 297 SharedClassPathEntry* table = (SharedClassPathEntry*)strptr; 298 strptr += entry_size * count; 299 300 _classpath_entry_table_size = count; 301 _classpath_entry_table = table; 302 _classpath_entry_size = entry_size; 303 } 304 } 305 } 306 307 bool FileMapInfo::validate_classpath_entry_table() { 308 _validating_classpath_entry_table = true; 309 310 int count = _header->_classpath_entry_table_size; 311 312 _classpath_entry_table = _header->_classpath_entry_table; 313 _classpath_entry_size = _header->_classpath_entry_size; 314 315 for (int i=0; i<count; i++) { 316 SharedClassPathEntry* ent = shared_classpath(i); 317 struct stat st; 318 const char* name = ent->_name; 319 bool ok = true; 320 log_info(class, path)("checking shared classpath entry: %s", name); 321 if (os::stat(name, &st) != 0) { 322 fail_continue("Required classpath entry does not exist: %s", name); 323 ok = false; 324 } else if (ent->is_dir()) { 325 if (!os::dir_is_empty(name)) { 326 fail_continue("directory is not empty: %s", name); 327 ok = false; 328 } 329 } else if (ent->is_jar_or_bootimage()) { 330 if (ent->_timestamp != st.st_mtime || 331 ent->_filesize != st.st_size) { 332 ok = false; 333 if (PrintSharedArchiveAndExit) { 334 fail_continue(ent->_timestamp != st.st_mtime ? 335 "Timestamp mismatch" : 336 "File size mismatch"); 337 } else { 338 fail_continue("A jar/jimage file is not the one used while building" 339 " the shared archive file: %s", name); 340 } 341 } 342 } 343 if (ok) { 344 log_info(class, path)("ok"); 345 } else if (!PrintSharedArchiveAndExit) { 346 _validating_classpath_entry_table = false; 347 return false; 348 } 349 } 350 351 _classpath_entry_table_size = _header->_classpath_entry_table_size; 352 _validating_classpath_entry_table = false; 353 return true; 354 } 355 356 357 // Read the FileMapInfo information from the file. 358 359 bool FileMapInfo::init_from_file(int fd) { 360 size_t sz = _header->data_size(); 361 char* addr = _header->data(); 362 size_t n = os::read(fd, addr, (unsigned int)sz); 363 if (n != sz) { 364 fail_continue("Unable to read the file header."); 365 return false; 366 } 367 if (_header->_version != current_version()) { 368 fail_continue("The shared archive file has the wrong version."); 369 return false; 370 } 371 _file_offset = (long)n; 372 373 size_t info_size = _header->_paths_misc_info_size; 374 _paths_misc_info = NEW_C_HEAP_ARRAY_RETURN_NULL(char, info_size, mtClass); 375 if (_paths_misc_info == NULL) { 376 fail_continue("Unable to read the file header."); 377 return false; 378 } 379 n = os::read(fd, _paths_misc_info, (unsigned int)info_size); 380 if (n != info_size) { 381 fail_continue("Unable to read the shared path info header."); 382 FREE_C_HEAP_ARRAY(char, _paths_misc_info); 383 _paths_misc_info = NULL; 384 return false; 385 } 386 387 size_t len = lseek(fd, 0, SEEK_END); 388 struct FileMapInfo::FileMapHeader::space_info* si = 389 &_header->_space[MetaspaceShared::mc]; 390 if (si->_file_offset >= len || len - si->_file_offset < si->_used) { 391 fail_continue("The shared archive file has been truncated."); 392 return false; 393 } 394 395 _file_offset += (long)n; 396 return true; 397 } 398 399 400 // Read the FileMapInfo information from the file. 401 bool FileMapInfo::open_for_read() { 402 _full_path = Arguments::GetSharedArchivePath(); 403 int fd = open(_full_path, O_RDONLY | O_BINARY, 0); 404 if (fd < 0) { 405 if (errno == ENOENT) { 406 // Not locating the shared archive is ok. 407 fail_continue("Specified shared archive not found."); 408 } else { 409 fail_continue("Failed to open shared archive file (%s).", 448 _file_open = true; 449 } 450 451 452 // Write the header to the file, seek to the next allocation boundary. 453 454 void FileMapInfo::write_header() { 455 int info_size = ClassLoader::get_shared_paths_misc_info_size(); 456 457 _header->_paths_misc_info_size = info_size; 458 459 align_file_position(); 460 size_t sz = _header->data_size(); 461 char* addr = _header->data(); 462 write_bytes(addr, (int)sz); // skip the C++ vtable 463 write_bytes(ClassLoader::get_shared_paths_misc_info(), info_size); 464 align_file_position(); 465 } 466 467 468 // Dump shared spaces to file. 469 470 void FileMapInfo::write_space(int i, Metaspace* space, bool read_only) { 471 align_file_position(); 472 size_t used = space->used_bytes_slow(Metaspace::NonClassType); 473 size_t capacity = space->capacity_bytes_slow(Metaspace::NonClassType); 474 struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[i]; 475 write_region(i, (char*)space->bottom(), used, capacity, read_only, false); 476 } 477 478 479 // Dump region to file. 480 481 void FileMapInfo::write_region(int region, char* base, size_t size, 482 size_t capacity, bool read_only, 483 bool allow_exec) { 484 struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[region]; 485 486 if (_file_open) { 487 guarantee(si->_file_offset == _file_offset, "file offset mismatch."); 488 log_info(cds)("Shared file region %d: " SIZE_FORMAT_HEX_W(6) 489 " bytes, addr " INTPTR_FORMAT " file offset " SIZE_FORMAT_HEX_W(6), 490 region, size, p2i(base), _file_offset); 491 } else { 492 si->_file_offset = _file_offset; 493 } 494 if (MetaspaceShared::is_string_region(region)) { 495 assert((base - (char*)Universe::narrow_oop_base()) % HeapWordSize == 0, "Sanity"); 496 if (base != NULL) { 497 si->_addr._offset = (intx)oopDesc::encode_heap_oop_not_null((oop)base); 498 } else { 499 si->_addr._offset = 0; 500 } 501 } else { 502 si->_addr._base = base; 503 } 504 si->_used = size; 505 si->_capacity = capacity; 506 si->_read_only = read_only; 507 si->_allow_exec = allow_exec; 508 si->_crc = ClassLoader::crc32(0, base, (jint)size); 509 write_bytes_aligned(base, (int)size); 510 } 511 512 // Write the string space. The string space contains one or multiple GC(G1) regions. 513 // When the total string space size is smaller than one GC region of the dump time, 514 // only one string region is used for shared strings. 515 // 516 // If the total string space size is bigger than one GC region, there would be more 517 // than one GC regions allocated for shared strings. The first/bottom GC region might 518 // be a partial GC region with the empty portion at the higher address within that region. 519 // The non-empty portion of the first region is written into the archive as one string 520 // region. The rest are consecutive full GC regions if they exist, which can be written 521 // out in one chunk as another string region. 522 void FileMapInfo::write_string_regions(GrowableArray<MemRegion> *regions) { 523 for (int i = MetaspaceShared::first_string; 524 i < MetaspaceShared::first_string + MetaspaceShared::max_strings; i++) { 525 char* start = NULL; 526 size_t size = 0; 527 if (regions->is_nonempty()) { 528 if (i == MetaspaceShared::first_string) { 529 MemRegion first = regions->first(); 530 start = (char*)first.start(); 531 size = first.byte_size(); 532 } else { 533 int len = regions->length(); 534 if (len > 1) { 535 start = (char*)regions->at(1).start(); 536 size = (char*)regions->at(len - 1).end() - start; 537 } 538 } 539 } 540 write_region(i, start, size, size, false, false); 541 } 542 } 543 544 545 // Dump bytes to file -- at the current file position. 546 547 void FileMapInfo::write_bytes(const void* buffer, int nbytes) { 548 if (_file_open) { 549 int n = ::write(_fd, buffer, nbytes); 550 if (n != nbytes) { 551 // It is dangerous to leave the corrupted shared archive file around, 552 // close and remove the file. See bug 6372906. 553 close(); 554 remove(_full_path); 555 fail_stop("Unable to write to shared archive file."); 556 } 557 } 558 _file_offset += nbytes; 559 } 560 588 align_file_position(); 589 } 590 591 592 // Close the shared archive file. This does NOT unmap mapped regions. 593 594 void FileMapInfo::close() { 595 if (_file_open) { 596 if (::close(_fd) < 0) { 597 fail_stop("Unable to close the shared archive file."); 598 } 599 _file_open = false; 600 _fd = -1; 601 } 602 } 603 604 605 // JVM/TI RedefineClasses() support: 606 // Remap the shared readonly space to shared readwrite, private. 607 bool FileMapInfo::remap_shared_readonly_as_readwrite() { 608 int idx = 0; 609 struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[idx]; 610 if (!si->_read_only) { 611 // the space is already readwrite so we are done 612 return true; 613 } 614 size_t used = si->_used; 615 size_t size = align_size_up(used, os::vm_allocation_granularity()); 616 if (!open_for_read()) { 617 return false; 618 } 619 char *addr = _header->region_addr(idx); 620 char *base = os::remap_memory(_fd, _full_path, si->_file_offset, 621 addr, size, false /* !read_only */, 622 si->_allow_exec); 623 close(); 624 if (base == NULL) { 625 fail_continue("Unable to remap shared readonly space (errno=%d).", errno); 626 return false; 627 } 628 if (base != addr) { 629 fail_continue("Unable to remap shared readonly space at required address."); 630 return false; 631 } 632 si->_read_only = false; 633 return true; 634 } 635 636 // Map the whole region at once, assumed to be allocated contiguously. 637 ReservedSpace FileMapInfo::reserve_shared_memory() { 638 struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[0]; 639 char* requested_addr = _header->region_addr(0); 640 641 size_t size = FileMapInfo::shared_spaces_size(); 642 643 // Reserve the space first, then map otherwise map will go right over some 644 // other reserved memory (like the code cache). 645 ReservedSpace rs(size, os::vm_allocation_granularity(), false, requested_addr); 646 if (!rs.is_reserved()) { 647 fail_continue("Unable to reserve shared space at required address " 648 INTPTR_FORMAT, p2i(requested_addr)); 649 return rs; 650 } 651 // the reserved virtual memory is for mapping class data sharing archive 652 MemTracker::record_virtual_memory_type((address)rs.base(), mtClassShared); 653 654 return rs; 655 } 656 657 // Memory map a region in the address space. 658 static const char* shared_region_name[] = { "ReadOnly", "ReadWrite", "MiscData", "MiscCode", 659 "String1", "String2", "OptionalData" }; 660 661 char* FileMapInfo::map_region(int i) { 841 fail_stop("Unable to unmap shared space."); 842 } 843 } 844 845 // dealloc the archived string region from java heap 846 void FileMapInfo::dealloc_string_regions() { 847 #if INCLUDE_ALL_GCS 848 if (num_ranges > 0) { 849 assert(string_ranges != NULL, "Null string_ranges array with non-zero count"); 850 G1CollectedHeap::heap()->dealloc_archive_regions(string_ranges, num_ranges); 851 } 852 #endif 853 } 854 855 void FileMapInfo::assert_mark(bool check) { 856 if (!check) { 857 fail_stop("Mark mismatch while restoring from shared file."); 858 } 859 } 860 861 862 FileMapInfo* FileMapInfo::_current_info = NULL; 863 SharedClassPathEntry* FileMapInfo::_classpath_entry_table = NULL; 864 int FileMapInfo::_classpath_entry_table_size = 0; 865 size_t FileMapInfo::_classpath_entry_size = 0x1234baad; 866 bool FileMapInfo::_validating_classpath_entry_table = false; 867 868 // Open the shared archive file, read and validate the header 869 // information (version, boot classpath, etc.). If initialization 870 // fails, shared spaces are disabled and the file is closed. [See 871 // fail_continue.] 872 // 873 // Validation of the archive is done in two steps: 874 // 875 // [1] validate_header() - done here. This checks the header, including _paths_misc_info. 876 // [2] validate_classpath_entry_table - this is done later, because the table is in the RW 877 // region of the archive, which is not mapped yet. 878 bool FileMapInfo::initialize() { 879 assert(UseSharedSpaces, "UseSharedSpaces expected."); 880 881 if (!open_for_read()) { 882 return false; 883 } 884 885 init_from_file(_fd); 886 if (!validate_header()) { 887 return false; 888 } 889 890 SharedReadOnlySize = _header->_space[0]._capacity; 891 SharedReadWriteSize = _header->_space[1]._capacity; 892 SharedMiscDataSize = _header->_space[2]._capacity; 893 SharedMiscCodeSize = _header->_space[3]._capacity; 894 return true; 895 } 896 897 char* FileMapInfo::FileMapHeader::region_addr(int idx) { 898 if (MetaspaceShared::is_string_region(idx)) { 899 return (char*)((void*)oopDesc::decode_heap_oop_not_null( 900 (narrowOop)_space[idx]._addr._offset)); 901 } else { 902 return _space[idx]._addr._base; 903 } 904 } 905 906 int FileMapInfo::FileMapHeader::compute_crc() { 907 char* header = data(); 908 // start computing from the field after _crc 909 char* buf = (char*)&_crc + sizeof(int); 910 size_t sz = data_size() - (buf - header); 911 int crc = ClassLoader::crc32(0, buf, (jint)sz); 912 return crc; 913 } 980 // falls in the mapped shared space. 981 // Param: 982 // p, The given pointer 983 // Return: 984 // True if the p is within the mapped shared space, otherwise, false. 985 bool FileMapInfo::is_in_shared_space(const void* p) { 986 for (int i = 0; i < MetaspaceShared::n_regions; i++) { 987 char *base; 988 if (MetaspaceShared::is_string_region(i) && _header->_space[i]._used == 0) { 989 continue; 990 } 991 base = _header->region_addr(i); 992 if (p >= base && p < base + _header->_space[i]._used) { 993 return true; 994 } 995 } 996 997 return false; 998 } 999 1000 // Check if a given address is within one of the shared regions (ro, rw, md, mc) 1001 bool FileMapInfo::is_in_shared_region(const void* p, int idx) { 1002 assert((idx >= MetaspaceShared::ro) && (idx <= MetaspaceShared::mc), "invalid region index"); 1003 char* base = _header->region_addr(idx); 1004 if (p >= base && p < base + _header->_space[idx]._used) { 1005 return true; 1006 } 1007 return false; 1008 } 1009 1010 void FileMapInfo::print_shared_spaces() { 1011 tty->print_cr("Shared Spaces:"); 1012 for (int i = 0; i < MetaspaceShared::n_regions; i++) { 1013 struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[i]; 1014 char *base = _header->region_addr(i); 1015 tty->print(" %s " INTPTR_FORMAT "-" INTPTR_FORMAT, 1016 shared_region_name[i], 1017 p2i(base), p2i(base + si->_used)); 1018 } 1019 } 1020 1021 // Unmap mapped regions of shared space. 1022 void FileMapInfo::stop_sharing_and_unmap(const char* msg) { | 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 #include "precompiled.hpp" 26 #include "classfile/classLoader.hpp" 27 #include "classfile/compactHashtable.inline.hpp" 28 #include "classfile/sharedClassUtil.hpp" 29 #include "classfile/symbolTable.hpp" 30 #include "classfile/systemDictionaryShared.hpp" 31 #include "classfile/altHashing.hpp" 32 #if INCLUDE_ALL_GCS 33 #include "gc/g1/g1CollectedHeap.hpp" 34 #endif 35 #include "logging/log.hpp" 36 #include "logging/logMessage.hpp" 37 #include "memory/filemap.hpp" 38 #include "memory/metadataFactory.hpp" 39 #include "memory/metaspaceClosure.hpp" 40 #include "memory/metaspaceShared.hpp" 41 #include "memory/oopFactory.hpp" 42 #include "oops/objArrayOop.hpp" 43 #include "prims/jvmtiExport.hpp" 44 #include "runtime/arguments.hpp" 45 #include "runtime/java.hpp" 46 #include "runtime/os.hpp" 47 #include "runtime/vm_version.hpp" 48 #include "services/memTracker.hpp" 49 #include "utilities/defaultStream.hpp" 50 51 # include <sys/stat.h> 52 # include <errno.h> 53 54 #ifndef O_BINARY // if defined (Win32) use binary files. 55 #define O_BINARY 0 // otherwise do nothing. 56 #endif 57 58 extern address JVM_FunctionAtStart(); 59 extern address JVM_FunctionAtEnd(); 60 177 _alignment = alignment; 178 _obj_alignment = ObjectAlignmentInBytes; 179 _compact_strings = CompactStrings; 180 _narrow_oop_mode = Universe::narrow_oop_mode(); 181 _narrow_oop_shift = Universe::narrow_oop_shift(); 182 _max_heap_size = MaxHeapSize; 183 _narrow_klass_base = Universe::narrow_klass_base(); 184 _narrow_klass_shift = Universe::narrow_klass_shift(); 185 _classpath_entry_table_size = mapinfo->_classpath_entry_table_size; 186 _classpath_entry_table = mapinfo->_classpath_entry_table; 187 _classpath_entry_size = mapinfo->_classpath_entry_size; 188 189 // The following fields are for sanity checks for whether this archive 190 // will function correctly with this JVM and the bootclasspath it's 191 // invoked with. 192 193 // JVM version string ... changes on each build. 194 get_header_version(_jvm_ident); 195 } 196 197 void SharedClassPathEntry::init(const char* name, TRAPS) { 198 _timestamp = 0; 199 _filesize = 0; 200 201 struct stat st; 202 if (os::stat(name, &st) == 0) { 203 if ((st.st_mode & S_IFMT) == S_IFDIR) { 204 if (!os::dir_is_empty(name)) { 205 ClassLoader::exit_with_path_failure( 206 "Cannot have non-empty directory in archived classpaths", name); 207 } 208 _is_dir = true; 209 } else { 210 _is_dir = false; 211 _timestamp = st.st_mtime; 212 _filesize = st.st_size; 213 } 214 } else { 215 // The file/dir must exist, or it would not have been added 216 // into ClassLoader::classpath_entry(). 217 // 218 // If we can't access a jar file in the boot path, then we can't 219 // make assumptions about where classes get loaded from. 220 FileMapInfo::fail_stop("Unable to open file %s.", name); 221 } 222 223 size_t len = strlen(name) + 1; 224 _name = MetadataFactory::new_array<char>(ClassLoaderData::the_null_class_loader_data(), (int)len, THREAD); 225 strcpy(_name->data(), name); 226 } 227 228 bool SharedClassPathEntry::validate() { 229 struct stat st; 230 const char* name = this->name(); 231 bool ok = true; 232 log_info(class, path)("checking shared classpath entry: %s", name); 233 if (os::stat(name, &st) != 0) { 234 FileMapInfo::fail_continue("Required classpath entry does not exist: %s", name); 235 ok = false; 236 } else if (is_dir()) { 237 if (!os::dir_is_empty(name)) { 238 FileMapInfo::fail_continue("directory is not empty: %s", name); 239 ok = false; 240 } 241 } else if (is_jar_or_bootimage()) { 242 if (_timestamp != st.st_mtime || 243 _filesize != st.st_size) { 244 ok = false; 245 if (PrintSharedArchiveAndExit) { 246 FileMapInfo::fail_continue(_timestamp != st.st_mtime ? 247 "Timestamp mismatch" : 248 "File size mismatch"); 249 } else { 250 FileMapInfo::fail_continue("A jar/jimage file is not the one used while building" 251 " the shared archive file: %s", name); 252 } 253 } 254 } 255 return ok; 256 } 257 258 void SharedClassPathEntry::metaspace_pointers_do(MetaspaceClosure* it) { 259 it->push(&_name); 260 it->push(&_manifest); 261 } 262 263 void FileMapInfo::allocate_classpath_entry_table() { 264 Thread* THREAD = Thread::current(); 265 ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data(); 266 size_t entry_size = SharedClassUtil::shared_class_path_entry_size(); // assert ( should be 8 byte aligned??) 267 int num_entries = ClassLoader::number_of_classpath_entries(); 268 size_t bytes = entry_size * num_entries; 269 270 _classpath_entry_table = MetadataFactory::new_array<u8>(loader_data, (int)(bytes + 7 / 8), THREAD); 271 _classpath_entry_table_size = num_entries; 272 _classpath_entry_size = entry_size; 273 274 assert(ClassLoader::get_jrt_entry() != NULL, 275 "No modular java runtime image present when allocating the CDS classpath entry table"); 276 277 for (int i=0; i<num_entries; i++) { 278 ClassPathEntry *cpe = ClassLoader::classpath_entry(i); 279 const char* type = ((i == 0) ? "jrt" : (cpe->is_jar_file() ? "jar" : "dir")); 280 281 log_info(class, path)("add main shared path (%s) %s", type, cpe->name()); 282 SharedClassPathEntry* ent = shared_classpath(i); 283 ent->init(cpe->name(), THREAD); 284 285 if (i > 0) { // No need to do jimage. 286 EXCEPTION_MARK; // The following call should never throw, but would exit VM on error. 287 SharedClassUtil::update_shared_classpath(cpe, ent, THREAD); 288 } 289 } 290 } 291 292 bool FileMapInfo::validate_classpath_entry_table() { 293 _validating_classpath_entry_table = true; 294 295 int count = _header->_classpath_entry_table_size; 296 297 _classpath_entry_table = _header->_classpath_entry_table; 298 _classpath_entry_size = _header->_classpath_entry_size; 299 _classpath_entry_table_size = _header->_classpath_entry_table_size; 300 301 for (int i=0; i<count; i++) { 302 if (shared_classpath(i)->validate()) { 303 log_info(class, path)("ok"); 304 } else if (!PrintSharedArchiveAndExit) { 305 _validating_classpath_entry_table = false; 306 _classpath_entry_table = NULL; 307 _classpath_entry_table_size = 0; 308 return false; 309 } 310 } 311 312 _validating_classpath_entry_table = false; 313 return true; 314 } 315 316 317 // Read the FileMapInfo information from the file. 318 319 bool FileMapInfo::init_from_file(int fd) { 320 size_t sz = _header->data_size(); 321 char* addr = _header->data(); 322 size_t n = os::read(fd, addr, (unsigned int)sz); 323 if (n != sz) { 324 fail_continue("Unable to read the file header."); 325 return false; 326 } 327 if (_header->_version != current_version()) { 328 fail_continue("The shared archive file has the wrong version."); 329 return false; 330 } 331 _file_offset = (long)n; 332 333 size_t info_size = _header->_paths_misc_info_size; 334 _paths_misc_info = NEW_C_HEAP_ARRAY_RETURN_NULL(char, info_size, mtClass); 335 if (_paths_misc_info == NULL) { 336 fail_continue("Unable to read the file header."); 337 return false; 338 } 339 n = os::read(fd, _paths_misc_info, (unsigned int)info_size); 340 if (n != info_size) { 341 fail_continue("Unable to read the shared path info header."); 342 FREE_C_HEAP_ARRAY(char, _paths_misc_info); 343 _paths_misc_info = NULL; 344 return false; 345 } 346 347 size_t len = lseek(fd, 0, SEEK_END); 348 struct FileMapInfo::FileMapHeader::space_info* si = 349 &_header->_space[MetaspaceShared::last_valid_region]; 350 if (si->_file_offset >= len || len - si->_file_offset < si->_used) { 351 fail_continue("The shared archive file has been truncated."); 352 return false; 353 } 354 355 _file_offset += (long)n; 356 return true; 357 } 358 359 360 // Read the FileMapInfo information from the file. 361 bool FileMapInfo::open_for_read() { 362 _full_path = Arguments::GetSharedArchivePath(); 363 int fd = open(_full_path, O_RDONLY | O_BINARY, 0); 364 if (fd < 0) { 365 if (errno == ENOENT) { 366 // Not locating the shared archive is ok. 367 fail_continue("Specified shared archive not found."); 368 } else { 369 fail_continue("Failed to open shared archive file (%s).", 408 _file_open = true; 409 } 410 411 412 // Write the header to the file, seek to the next allocation boundary. 413 414 void FileMapInfo::write_header() { 415 int info_size = ClassLoader::get_shared_paths_misc_info_size(); 416 417 _header->_paths_misc_info_size = info_size; 418 419 align_file_position(); 420 size_t sz = _header->data_size(); 421 char* addr = _header->data(); 422 write_bytes(addr, (int)sz); // skip the C++ vtable 423 write_bytes(ClassLoader::get_shared_paths_misc_info(), info_size); 424 align_file_position(); 425 } 426 427 428 // Dump region to file. 429 430 void FileMapInfo::write_region(int region, char* base, size_t size, 431 bool read_only, bool allow_exec) { 432 struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[region]; 433 434 if (_file_open) { 435 guarantee(si->_file_offset == _file_offset, "file offset mismatch."); 436 log_info(cds)("Shared file region %d: " SIZE_FORMAT_HEX_W(08) 437 " bytes, addr " INTPTR_FORMAT " file offset " SIZE_FORMAT_HEX_W(08), 438 region, size, p2i(base), _file_offset); 439 } else { 440 si->_file_offset = _file_offset; 441 } 442 if (MetaspaceShared::is_string_region(region)) { 443 assert((base - (char*)Universe::narrow_oop_base()) % HeapWordSize == 0, "Sanity"); 444 if (base != NULL) { 445 si->_addr._offset = (intx)oopDesc::encode_heap_oop_not_null((oop)base); 446 } else { 447 si->_addr._offset = 0; 448 } 449 } else { 450 si->_addr._base = base; 451 } 452 si->_used = size; 453 si->_read_only = read_only; 454 si->_allow_exec = allow_exec; 455 si->_crc = ClassLoader::crc32(0, base, (jint)size); 456 write_bytes_aligned(base, (int)size); 457 } 458 459 // Write the string space. The string space contains one or multiple GC(G1) regions. 460 // When the total string space size is smaller than one GC region of the dump time, 461 // only one string region is used for shared strings. 462 // 463 // If the total string space size is bigger than one GC region, there would be more 464 // than one GC regions allocated for shared strings. The first/bottom GC region might 465 // be a partial GC region with the empty portion at the higher address within that region. 466 // The non-empty portion of the first region is written into the archive as one string 467 // region. The rest are consecutive full GC regions if they exist, which can be written 468 // out in one chunk as another string region. 469 void FileMapInfo::write_string_regions(GrowableArray<MemRegion> *regions) { 470 for (int i = MetaspaceShared::first_string; 471 i < MetaspaceShared::first_string + MetaspaceShared::max_strings; i++) { 472 char* start = NULL; 473 size_t size = 0; 474 if (regions->is_nonempty()) { 475 if (i == MetaspaceShared::first_string) { 476 MemRegion first = regions->first(); 477 start = (char*)first.start(); 478 size = first.byte_size(); 479 } else { 480 int len = regions->length(); 481 if (len > 1) { 482 start = (char*)regions->at(1).start(); 483 size = (char*)regions->at(len - 1).end() - start; 484 } 485 } 486 } 487 write_region(i, start, size, false, false); 488 } 489 } 490 491 492 // Dump bytes to file -- at the current file position. 493 494 void FileMapInfo::write_bytes(const void* buffer, int nbytes) { 495 if (_file_open) { 496 int n = ::write(_fd, buffer, nbytes); 497 if (n != nbytes) { 498 // It is dangerous to leave the corrupted shared archive file around, 499 // close and remove the file. See bug 6372906. 500 close(); 501 remove(_full_path); 502 fail_stop("Unable to write to shared archive file."); 503 } 504 } 505 _file_offset += nbytes; 506 } 507 535 align_file_position(); 536 } 537 538 539 // Close the shared archive file. This does NOT unmap mapped regions. 540 541 void FileMapInfo::close() { 542 if (_file_open) { 543 if (::close(_fd) < 0) { 544 fail_stop("Unable to close the shared archive file."); 545 } 546 _file_open = false; 547 _fd = -1; 548 } 549 } 550 551 552 // JVM/TI RedefineClasses() support: 553 // Remap the shared readonly space to shared readwrite, private. 554 bool FileMapInfo::remap_shared_readonly_as_readwrite() { 555 int idx = MetaspaceShared::ro; 556 struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[idx]; 557 if (!si->_read_only) { 558 // the space is already readwrite so we are done 559 return true; 560 } 561 size_t used = si->_used; 562 size_t size = align_size_up(used, os::vm_allocation_granularity()); 563 if (!open_for_read()) { 564 return false; 565 } 566 char *addr = _header->region_addr(idx); 567 char *base = os::remap_memory(_fd, _full_path, si->_file_offset, 568 addr, size, false /* !read_only */, 569 si->_allow_exec); 570 close(); 571 if (base == NULL) { 572 fail_continue("Unable to remap shared readonly space (errno=%d).", errno); 573 return false; 574 } 575 if (base != addr) { 576 fail_continue("Unable to remap shared readonly space at required address."); 577 return false; 578 } 579 si->_read_only = false; 580 return true; 581 } 582 583 // Map the whole region at once, assumed to be allocated contiguously. 584 ReservedSpace FileMapInfo::reserve_shared_memory() { 585 char* requested_addr = _header->region_addr(0); 586 size_t size = FileMapInfo::core_spaces_size(); 587 588 // Reserve the space first, then map otherwise map will go right over some 589 // other reserved memory (like the code cache). 590 ReservedSpace rs(size, os::vm_allocation_granularity(), false, requested_addr); 591 if (!rs.is_reserved()) { 592 fail_continue("Unable to reserve shared space at required address " 593 INTPTR_FORMAT, p2i(requested_addr)); 594 return rs; 595 } 596 // the reserved virtual memory is for mapping class data sharing archive 597 MemTracker::record_virtual_memory_type((address)rs.base(), mtClassShared); 598 599 return rs; 600 } 601 602 // Memory map a region in the address space. 603 static const char* shared_region_name[] = { "ReadOnly", "ReadWrite", "MiscData", "MiscCode", 604 "String1", "String2", "OptionalData" }; 605 606 char* FileMapInfo::map_region(int i) { 786 fail_stop("Unable to unmap shared space."); 787 } 788 } 789 790 // dealloc the archived string region from java heap 791 void FileMapInfo::dealloc_string_regions() { 792 #if INCLUDE_ALL_GCS 793 if (num_ranges > 0) { 794 assert(string_ranges != NULL, "Null string_ranges array with non-zero count"); 795 G1CollectedHeap::heap()->dealloc_archive_regions(string_ranges, num_ranges); 796 } 797 #endif 798 } 799 800 void FileMapInfo::assert_mark(bool check) { 801 if (!check) { 802 fail_stop("Mark mismatch while restoring from shared file."); 803 } 804 } 805 806 void FileMapInfo::metaspace_pointers_do(MetaspaceClosure* it) { 807 it->push(&_classpath_entry_table); 808 for (int i=0; i<_classpath_entry_table_size; i++) { 809 shared_classpath(i)->metaspace_pointers_do(it); 810 } 811 } 812 813 814 FileMapInfo* FileMapInfo::_current_info = NULL; 815 Array<u8>* FileMapInfo::_classpath_entry_table = NULL; 816 int FileMapInfo::_classpath_entry_table_size = 0; 817 size_t FileMapInfo::_classpath_entry_size = 0x1234baad; 818 bool FileMapInfo::_validating_classpath_entry_table = false; 819 820 // Open the shared archive file, read and validate the header 821 // information (version, boot classpath, etc.). If initialization 822 // fails, shared spaces are disabled and the file is closed. [See 823 // fail_continue.] 824 // 825 // Validation of the archive is done in two steps: 826 // 827 // [1] validate_header() - done here. This checks the header, including _paths_misc_info. 828 // [2] validate_classpath_entry_table - this is done later, because the table is in the RW 829 // region of the archive, which is not mapped yet. 830 bool FileMapInfo::initialize() { 831 assert(UseSharedSpaces, "UseSharedSpaces expected."); 832 833 if (!open_for_read()) { 834 return false; 835 } 836 837 init_from_file(_fd); 838 if (!validate_header()) { 839 return false; 840 } 841 return true; 842 } 843 844 char* FileMapInfo::FileMapHeader::region_addr(int idx) { 845 if (MetaspaceShared::is_string_region(idx)) { 846 return (char*)((void*)oopDesc::decode_heap_oop_not_null( 847 (narrowOop)_space[idx]._addr._offset)); 848 } else { 849 return _space[idx]._addr._base; 850 } 851 } 852 853 int FileMapInfo::FileMapHeader::compute_crc() { 854 char* header = data(); 855 // start computing from the field after _crc 856 char* buf = (char*)&_crc + sizeof(int); 857 size_t sz = data_size() - (buf - header); 858 int crc = ClassLoader::crc32(0, buf, (jint)sz); 859 return crc; 860 } 927 // falls in the mapped shared space. 928 // Param: 929 // p, The given pointer 930 // Return: 931 // True if the p is within the mapped shared space, otherwise, false. 932 bool FileMapInfo::is_in_shared_space(const void* p) { 933 for (int i = 0; i < MetaspaceShared::n_regions; i++) { 934 char *base; 935 if (MetaspaceShared::is_string_region(i) && _header->_space[i]._used == 0) { 936 continue; 937 } 938 base = _header->region_addr(i); 939 if (p >= base && p < base + _header->_space[i]._used) { 940 return true; 941 } 942 } 943 944 return false; 945 } 946 947 // Check if a given address is within one of the shared regions ( ro, rw, mc or md) 948 bool FileMapInfo::is_in_shared_region(const void* p, int idx) { 949 assert(idx == MetaspaceShared::ro || 950 idx == MetaspaceShared::rw || 951 idx == MetaspaceShared::mc || 952 idx == MetaspaceShared::md, "invalid region index"); 953 char* base = _header->region_addr(idx); 954 if (p >= base && p < base + _header->_space[idx]._used) { 955 return true; 956 } 957 return false; 958 } 959 960 void FileMapInfo::print_shared_spaces() { 961 tty->print_cr("Shared Spaces:"); 962 for (int i = 0; i < MetaspaceShared::n_regions; i++) { 963 struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[i]; 964 char *base = _header->region_addr(i); 965 tty->print(" %s " INTPTR_FORMAT "-" INTPTR_FORMAT, 966 shared_region_name[i], 967 p2i(base), p2i(base + si->_used)); 968 } 969 } 970 971 // Unmap mapped regions of shared space. 972 void FileMapInfo::stop_sharing_and_unmap(const char* msg) { |