197 set_magic(CDSConfig::is_dumping_dynamic_archive() ? CDS_DYNAMIC_ARCHIVE_MAGIC : CDS_ARCHIVE_MAGIC);
198 set_version(CURRENT_CDS_ARCHIVE_VERSION);
199
200 if (!info->is_static() && base_archive_name_size != 0) {
201 // copy base archive name
202 copy_base_archive_name(CDSConfig::static_archive_path());
203 }
204 _core_region_alignment = core_region_alignment;
205 _obj_alignment = ObjectAlignmentInBytes;
206 _compact_strings = CompactStrings;
207 if (CDSConfig::is_dumping_heap()) {
208 _narrow_oop_mode = CompressedOops::mode();
209 _narrow_oop_base = CompressedOops::base();
210 _narrow_oop_shift = CompressedOops::shift();
211 }
212 _compressed_oops = UseCompressedOops;
213 _compressed_class_ptrs = UseCompressedClassPointers;
214 _use_secondary_supers_table = UseSecondarySupersTable;
215 _max_heap_size = MaxHeapSize;
216 _use_optimized_module_handling = CDSConfig::is_using_optimized_module_handling();
217 _has_full_module_graph = CDSConfig::is_dumping_full_module_graph();
218
219 // The following fields are for sanity checks for whether this archive
220 // will function correctly with this JVM and the bootclasspath it's
221 // invoked with.
222
223 // JVM version string ... changes on each build.
224 get_header_version(_jvm_ident);
225
226 _app_class_paths_start_index = ClassLoaderExt::app_class_paths_start_index();
227 _app_module_paths_start_index = ClassLoaderExt::app_module_paths_start_index();
228 _max_used_path_index = ClassLoaderExt::max_used_path_index();
229 _num_module_paths = ClassLoader::num_module_path_entries();
230
231 _verify_local = BytecodeVerificationLocal;
232 _verify_remote = BytecodeVerificationRemote;
233 _has_platform_or_app_classes = ClassLoaderExt::has_platform_or_app_classes();
234 _has_non_jar_in_classpath = ClassLoaderExt::has_non_jar_in_classpath();
235 _requested_base_address = (char*)SharedBaseAddress;
236 _mapped_base_address = (char*)SharedBaseAddress;
237 _allow_archiving_with_java_agent = AllowArchivingWithJavaAgent;
284 st->print_cr("- num_module_paths: %d", _num_module_paths);
285 st->print_cr("- max_used_path_index: %d", _max_used_path_index);
286 st->print_cr("- verify_local: %d", _verify_local);
287 st->print_cr("- verify_remote: %d", _verify_remote);
288 st->print_cr("- has_platform_or_app_classes: %d", _has_platform_or_app_classes);
289 st->print_cr("- has_non_jar_in_classpath: %d", _has_non_jar_in_classpath);
290 st->print_cr("- requested_base_address: " INTPTR_FORMAT, p2i(_requested_base_address));
291 st->print_cr("- mapped_base_address: " INTPTR_FORMAT, p2i(_mapped_base_address));
292 st->print_cr("- heap_root_segments.roots_count: %d" , _heap_root_segments.roots_count());
293 st->print_cr("- heap_root_segments.base_offset: " SIZE_FORMAT_X, _heap_root_segments.base_offset());
294 st->print_cr("- heap_root_segments.count: " SIZE_FORMAT, _heap_root_segments.count());
295 st->print_cr("- heap_root_segments.max_size_elems: %d", _heap_root_segments.max_size_in_elems());
296 st->print_cr("- heap_root_segments.max_size_bytes: %d", _heap_root_segments.max_size_in_bytes());
297 st->print_cr("- _heap_oopmap_start_pos: " SIZE_FORMAT, _heap_oopmap_start_pos);
298 st->print_cr("- _heap_ptrmap_start_pos: " SIZE_FORMAT, _heap_ptrmap_start_pos);
299 st->print_cr("- _rw_ptrmap_start_pos: " SIZE_FORMAT, _rw_ptrmap_start_pos);
300 st->print_cr("- _ro_ptrmap_start_pos: " SIZE_FORMAT, _ro_ptrmap_start_pos);
301 st->print_cr("- allow_archiving_with_java_agent:%d", _allow_archiving_with_java_agent);
302 st->print_cr("- use_optimized_module_handling: %d", _use_optimized_module_handling);
303 st->print_cr("- has_full_module_graph %d", _has_full_module_graph);
304 }
305
306 void SharedClassPathEntry::init_as_non_existent(const char* path, TRAPS) {
307 _type = non_existent_entry;
308 set_name(path, CHECK);
309 }
310
311 void SharedClassPathEntry::init(bool is_modules_image,
312 bool is_module_path,
313 ClassPathEntry* cpe, TRAPS) {
314 assert(CDSConfig::is_dumping_archive(), "sanity");
315 _timestamp = 0;
316 _filesize = 0;
317 _from_class_path_attr = false;
318
319 struct stat st;
320 if (os::stat(cpe->name(), &st) == 0) {
321 if ((st.st_mode & S_IFMT) == S_IFDIR) {
322 _type = dir_entry;
323 } else {
548
549 void FileMapInfo::record_non_existent_class_path_entry(const char* path) {
550 assert(CDSConfig::is_dumping_archive(), "sanity");
551 log_info(class, path)("non-existent Class-Path entry %s", path);
552 if (_non_existent_class_paths == nullptr) {
553 _non_existent_class_paths = new (mtClass) GrowableArray<const char*>(10, mtClass);
554 }
555 _non_existent_class_paths->append(os::strdup(path));
556 }
557
558 int FileMapInfo::num_non_existent_class_paths() {
559 assert(CDSConfig::is_dumping_archive(), "sanity");
560 if (_non_existent_class_paths != nullptr) {
561 return _non_existent_class_paths->length();
562 } else {
563 return 0;
564 }
565 }
566
567 int FileMapInfo::get_module_shared_path_index(Symbol* location) {
568 if (location->starts_with("jrt:", 4) && get_number_of_shared_paths() > 0) {
569 assert(shared_path(0)->is_modules_image(), "first shared_path must be the modules image");
570 return 0;
571 }
572
573 if (ClassLoaderExt::app_module_paths_start_index() >= get_number_of_shared_paths()) {
574 // The archive(s) were created without --module-path option
575 return -1;
576 }
577
578 if (!location->starts_with("file:", 5)) {
579 return -1;
580 }
581
582 // skip_uri_protocol was also called during dump time -- see ClassLoaderExt::process_module_table()
583 ResourceMark rm;
584 const char* file = ClassLoader::uri_to_path(location->as_C_string());
585 for (int i = ClassLoaderExt::app_module_paths_start_index(); i < get_number_of_shared_paths(); i++) {
586 SharedClassPathEntry* ent = shared_path(i);
587 if (!ent->is_non_existent()) {
1031 }
1032
1033 if (header()->max_used_path_index() == 0) {
1034 // default archive only contains the module image in the bootclasspath
1035 assert(shared_path(0)->is_modules_image(), "first shared_path must be the modules image");
1036 } else {
1037 if (!validate_boot_class_paths() || !validate_app_class_paths(shared_app_paths_len)) {
1038 const char* mismatch_msg = "shared class paths mismatch";
1039 const char* hint_msg = log_is_enabled(Info, class, path) ?
1040 "" : " (hint: enable -Xlog:class+path=info to diagnose the failure)";
1041 if (RequireSharedSpaces) {
1042 log_error(cds)("%s%s", mismatch_msg, hint_msg);
1043 MetaspaceShared::unrecoverable_loading_error();
1044 } else {
1045 log_warning(cds)("%s%s", mismatch_msg, hint_msg);
1046 }
1047 return false;
1048 }
1049 }
1050
1051 validate_non_existent_class_paths();
1052
1053 _validating_shared_path_table = false;
1054
1055 #if INCLUDE_JVMTI
1056 if (_classpath_entries_for_jvmti != nullptr) {
1057 os::free(_classpath_entries_for_jvmti);
1058 }
1059 size_t sz = sizeof(ClassPathEntry*) * get_number_of_shared_paths();
1060 _classpath_entries_for_jvmti = (ClassPathEntry**)os::malloc(sz, mtClass);
1061 memset((void*)_classpath_entries_for_jvmti, 0, sz);
1062 #endif
1063
1064 return true;
1065 }
1066
1067 void FileMapInfo::validate_non_existent_class_paths() {
1068 // All of the recorded non-existent paths came from the Class-Path: attribute from the JAR
1069 // files on the app classpath. If any of these are found to exist during runtime,
1070 // it will change how classes are loading for the app loader. For safety, disable
1071 // loading of archived platform/app classes (currently there's no way to disable just the
1072 // app classes).
1073
1074 assert(CDSConfig::is_using_archive(), "runtime only");
1075 for (int i = header()->app_module_paths_start_index() + header()->num_module_paths();
1076 i < get_number_of_shared_paths();
1077 i++) {
1078 SharedClassPathEntry* ent = shared_path(i);
1079 if (!ent->check_non_existent()) {
1080 log_warning(cds)("Archived non-system classes are disabled because the "
1081 "file %s exists", ent->name());
1082 header()->set_has_platform_or_app_classes(false);
1083 }
1084 }
1085 }
1086
1087 // A utility class for reading/validating the GenericCDSFileMapHeader portion of
1088 // a CDS archive's header. The file header of all CDS archives with versions from
1089 // CDS_GENERIC_HEADER_SUPPORTED_MIN_VERSION (12) are guaranteed to always start
1090 // with GenericCDSFileMapHeader. This makes it possible to read important information
1091 // from a CDS archive created by a different version of HotSpot, so that we can
1092 // automatically regenerate the archive as necessary (JDK-8261455).
1093 class FileHeaderHelper {
1094 int _fd;
1095 bool _is_valid;
1096 bool _is_static;
1097 GenericCDSFileMapHeader* _header;
1098 const char* _archive_name;
1099 const char* _base_archive_name;
1100
1101 public:
1102 FileHeaderHelper(const char* archive_name, bool is_static) {
1103 _fd = -1;
1104 _is_valid = false;
1493 bool FileMapRegion::check_region_crc(char* base) const {
1494 // This function should be called after the region has been properly
1495 // loaded into memory via FileMapInfo::map_region() or FileMapInfo::read_region().
1496 // I.e., this->mapped_base() must be valid.
1497 size_t sz = used();
1498 if (sz == 0) {
1499 return true;
1500 }
1501
1502 assert(base != nullptr, "must be initialized");
1503 int crc = ClassLoader::crc32(0, base, (jint)sz);
1504 if (crc != this->crc()) {
1505 log_warning(cds)("Checksum verification failed.");
1506 return false;
1507 }
1508 return true;
1509 }
1510
1511 static const char* region_name(int region_index) {
1512 static const char* names[] = {
1513 "rw", "ro", "bm", "hp"
1514 };
1515 const int num_regions = sizeof(names)/sizeof(names[0]);
1516 assert(0 <= region_index && region_index < num_regions, "sanity");
1517
1518 return names[region_index];
1519 }
1520
1521 BitMapView FileMapInfo::bitmap_view(int region_index, bool is_oopmap) {
1522 FileMapRegion* r = region_at(region_index);
1523 char* bitmap_base = is_static() ? FileMapInfo::current_info()->map_bitmap_region() : FileMapInfo::dynamic_info()->map_bitmap_region();
1524 bitmap_base += is_oopmap ? r->oopmap_offset() : r->ptrmap_offset();
1525 size_t size_in_bits = is_oopmap ? r->oopmap_size_in_bits() : r->ptrmap_size_in_bits();
1526
1527 log_debug(cds, reloc)("mapped %s relocation %smap @ " INTPTR_FORMAT " (" SIZE_FORMAT " bits)",
1528 region_name(region_index), is_oopmap ? "oop" : "ptr",
1529 p2i(bitmap_base), size_in_bits);
1530
1531 return BitMapView((BitMap::bm_word_t*)(bitmap_base), size_in_bits);
1532 }
1533
1579 mapping_offset = (size_t)((address)requested_base - CompressedOops::base());
1580 assert((mapping_offset >> CompressedOops::shift()) << CompressedOops::shift() == mapping_offset, "must be");
1581 } else {
1582 mapping_offset = 0; // not used with !UseCompressedOops
1583 }
1584 #endif // INCLUDE_CDS_JAVA_HEAP
1585 } else {
1586 char* requested_SharedBaseAddress = (char*)MetaspaceShared::requested_base_address();
1587 requested_base = ArchiveBuilder::current()->to_requested(base);
1588 assert(requested_base >= requested_SharedBaseAddress, "must be");
1589 mapping_offset = requested_base - requested_SharedBaseAddress;
1590 }
1591
1592 r->set_file_offset(_file_offset);
1593 int crc = ClassLoader::crc32(0, base, (jint)size);
1594 if (size > 0) {
1595 log_info(cds)("Shared file region (%s) %d: " SIZE_FORMAT_W(8)
1596 " bytes, addr " INTPTR_FORMAT " file offset 0x%08" PRIxPTR
1597 " crc 0x%08x",
1598 region_name(region), region, size, p2i(requested_base), _file_offset, crc);
1599 }
1600
1601 r->init(region, mapping_offset, size, read_only, allow_exec, crc);
1602
1603 if (base != nullptr) {
1604 write_bytes_aligned(base, size);
1605 }
1606 }
1607
1608 static size_t write_bitmap(const CHeapBitMap* map, char* output, size_t offset) {
1609 size_t size_in_bytes = map->size_in_bytes();
1610 map->write_to((BitMap::bm_word_t*)(output + offset), size_in_bytes);
1611 return offset + size_in_bytes;
1612 }
1613
1614 // The sorting code groups the objects with non-null oop/ptrs together.
1615 // Relevant bitmaps then have lots of leading and trailing zeros, which
1616 // we do not have to store.
1617 size_t FileMapInfo::remove_bitmap_zeros(CHeapBitMap* map) {
1618 BitMap::idx_t first_set = map->find_first_set_bit(0);
1619 BitMap::idx_t last_set = map->find_last_set_bit(0);
1620 size_t old_size = map->size();
1621
1622 // Slice and resize bitmap
1623 map->truncate(first_set, last_set + 1);
1624
1625 assert(map->at(0), "First bit should be set");
1626 assert(map->at(map->size() - 1), "Last bit should be set");
1627 assert(map->size() <= old_size, "sanity");
1628
1629 return first_set;
1630 }
1631
1632 char* FileMapInfo::write_bitmap_region(CHeapBitMap* rw_ptrmap, CHeapBitMap* ro_ptrmap, ArchiveHeapInfo* heap_info,
1633 size_t &size_in_bytes) {
1634 size_t removed_rw_leading_zeros = remove_bitmap_zeros(rw_ptrmap);
1635 size_t removed_ro_leading_zeros = remove_bitmap_zeros(ro_ptrmap);
1636 header()->set_rw_ptrmap_start_pos(removed_rw_leading_zeros);
1637 header()->set_ro_ptrmap_start_pos(removed_ro_leading_zeros);
1638 size_in_bytes = rw_ptrmap->size_in_bytes() + ro_ptrmap->size_in_bytes();
1639
1640 if (heap_info->is_used()) {
1641 // Remove leading and trailing zeros
1642 size_t removed_oop_leading_zeros = remove_bitmap_zeros(heap_info->oopmap());
1643 size_t removed_ptr_leading_zeros = remove_bitmap_zeros(heap_info->ptrmap());
1644 header()->set_heap_oopmap_start_pos(removed_oop_leading_zeros);
1645 header()->set_heap_ptrmap_start_pos(removed_ptr_leading_zeros);
1646
1647 size_in_bytes += heap_info->oopmap()->size_in_bytes();
1648 size_in_bytes += heap_info->ptrmap()->size_in_bytes();
1649 }
1650
1651 // The bitmap region contains up to 4 parts:
1652 // rw_ptrmap: metaspace pointers inside the read-write region
1653 // ro_ptrmap: metaspace pointers inside the read-only region
1654 // heap_info->oopmap(): Java oop pointers in the heap region
1655 // heap_info->ptrmap(): metaspace pointers in the heap region
1656 char* buffer = NEW_C_HEAP_ARRAY(char, size_in_bytes, mtClassShared);
1657 size_t written = 0;
1658
1659 region_at(MetaspaceShared::rw)->init_ptrmap(0, rw_ptrmap->size());
1660 written = write_bitmap(rw_ptrmap, buffer, written);
1661
1662 region_at(MetaspaceShared::ro)->init_ptrmap(written, ro_ptrmap->size());
1663 written = write_bitmap(ro_ptrmap, buffer, written);
1664
1665 if (heap_info->is_used()) {
1666 FileMapRegion* r = region_at(MetaspaceShared::hp);
1667
1668 r->init_oopmap(written, heap_info->oopmap()->size());
1669 written = write_bitmap(heap_info->oopmap(), buffer, written);
1670
1671 r->init_ptrmap(written, heap_info->ptrmap()->size());
1672 written = write_bitmap(heap_info->ptrmap(), buffer, written);
1673 }
1674
1675 write_region(MetaspaceShared::bm, (char*)buffer, size_in_bytes, /*read_only=*/true, /*allow_exec=*/false);
1676 return buffer;
1677 }
1678
1679 size_t FileMapInfo::write_heap_region(ArchiveHeapInfo* heap_info) {
1680 char* buffer_start = heap_info->buffer_start();
1681 size_t buffer_size = heap_info->buffer_byte_size();
1682 write_region(MetaspaceShared::hp, buffer_start, buffer_size, false, false);
1683 header()->set_heap_root_segments(heap_info->heap_root_segments());
1684 return buffer_size;
1773 assert(WINDOWS_ONLY(false) NOT_WINDOWS(true), "Don't call on Windows");
1774 // Replace old mapping with new one that is writable.
1775 char *base = os::map_memory(_fd, _full_path, r->file_offset(),
1776 addr, size, false /* !read_only */,
1777 r->allow_exec());
1778 close();
1779 // These have to be errors because the shared region is now unmapped.
1780 if (base == nullptr) {
1781 log_error(cds)("Unable to remap shared readonly space (errno=%d).", errno);
1782 vm_exit(1);
1783 }
1784 if (base != addr) {
1785 log_error(cds)("Unable to remap shared readonly space (errno=%d).", errno);
1786 vm_exit(1);
1787 }
1788 r->set_read_only(false);
1789 return true;
1790 }
1791
1792 // Memory map a region in the address space.
1793 static const char* shared_region_name[] = { "ReadWrite", "ReadOnly", "Bitmap", "Heap" };
1794
1795 MapArchiveResult FileMapInfo::map_regions(int regions[], int num_regions, char* mapped_base_address, ReservedSpace rs) {
1796 DEBUG_ONLY(FileMapRegion* last_region = nullptr);
1797 intx addr_delta = mapped_base_address - header()->requested_base_address();
1798
1799 // Make sure we don't attempt to use header()->mapped_base_address() unless
1800 // it's been successfully mapped.
1801 DEBUG_ONLY(header()->set_mapped_base_address((char*)(uintptr_t)0xdeadbeef);)
1802
1803 for (int i = 0; i < num_regions; i++) {
1804 int idx = regions[i];
1805 MapArchiveResult result = map_region(idx, addr_delta, mapped_base_address, rs);
1806 if (result != MAP_ARCHIVE_SUCCESS) {
1807 return result;
1808 }
1809 FileMapRegion* r = region_at(idx);
1810 DEBUG_ONLY(if (last_region != nullptr) {
1811 // Ensure that the OS won't be able to allocate new memory spaces between any mapped
1812 // regions, or else it would mess up the simple comparison in MetaspaceObj::is_shared().
1813 assert(r->mapped_base() == last_region->mapped_end(), "must have no gaps");
1876 r->set_read_only(false); // Need to patch the pointers
1877 }
1878
1879 if (MetaspaceShared::use_windows_memory_mapping() && rs.is_reserved()) {
1880 // This is the second time we try to map the archive(s). We have already created a ReservedSpace
1881 // that covers all the FileMapRegions to ensure all regions can be mapped. However, Windows
1882 // can't mmap into a ReservedSpace, so we just ::read() the data. We're going to patch all the
1883 // regions anyway, so there's no benefit for mmap anyway.
1884 if (!read_region(i, requested_addr, size, /* do_commit = */ true)) {
1885 log_info(cds)("Failed to read %s shared space into reserved space at " INTPTR_FORMAT,
1886 shared_region_name[i], p2i(requested_addr));
1887 return MAP_ARCHIVE_OTHER_FAILURE; // oom or I/O error.
1888 } else {
1889 assert(r->mapped_base() != nullptr, "must be initialized");
1890 return MAP_ARCHIVE_SUCCESS;
1891 }
1892 } else {
1893 // Note that this may either be a "fresh" mapping into unreserved address
1894 // space (Windows, first mapping attempt), or a mapping into pre-reserved
1895 // space (Posix). See also comment in MetaspaceShared::map_archives().
1896 char* base = map_memory(_fd, _full_path, r->file_offset(),
1897 requested_addr, size, r->read_only(),
1898 r->allow_exec(), mtClassShared);
1899 if (base != requested_addr) {
1900 log_info(cds)("Unable to map %s shared space at " INTPTR_FORMAT,
1901 shared_region_name[i], p2i(requested_addr));
1902 _memory_mapping_failed = true;
1903 return MAP_ARCHIVE_MMAP_FAILURE;
1904 }
1905
1906 if (VerifySharedSpaces && !r->check_region_crc(requested_addr)) {
1907 return MAP_ARCHIVE_OTHER_FAILURE;
1908 }
1909
1910 r->set_mapped_from_file(true);
1911 r->set_mapped_base(requested_addr);
1912
1913 return MAP_ARCHIVE_SUCCESS;
1914 }
1915 }
1916
1917 // The return value is the location of the archive relocation bitmap.
1929 return nullptr;
1930 }
1931
1932 if (VerifySharedSpaces && !r->check_region_crc(bitmap_base)) {
1933 log_error(cds)("relocation bitmap CRC error");
1934 if (!os::unmap_memory(bitmap_base, r->used_aligned())) {
1935 fatal("os::unmap_memory of relocation bitmap failed");
1936 }
1937 return nullptr;
1938 }
1939
1940 r->set_mapped_from_file(true);
1941 r->set_mapped_base(bitmap_base);
1942 log_info(cds)("Mapped %s region #%d at base " INTPTR_FORMAT " top " INTPTR_FORMAT " (%s)",
1943 is_static() ? "static " : "dynamic",
1944 MetaspaceShared::bm, p2i(r->mapped_base()), p2i(r->mapped_end()),
1945 shared_region_name[MetaspaceShared::bm]);
1946 return bitmap_base;
1947 }
1948
1949 // This is called when we cannot map the archive at the requested[ base address (usually 0x800000000).
1950 // We relocate all pointers in the 2 core regions (ro, rw).
1951 bool FileMapInfo::relocate_pointers_in_core_regions(intx addr_delta) {
1952 log_debug(cds, reloc)("runtime archive relocation start");
1953 char* bitmap_base = map_bitmap_region();
1954
1955 if (bitmap_base == nullptr) {
1956 return false; // OOM, or CRC check failure
1957 } else {
1958 BitMapView rw_ptrmap = ptrmap_view(MetaspaceShared::rw);
1959 BitMapView ro_ptrmap = ptrmap_view(MetaspaceShared::ro);
1960
1961 FileMapRegion* rw_region = first_core_region();
1962 FileMapRegion* ro_region = last_core_region();
1963
1964 // Patch all pointers inside the RW region
1965 address rw_patch_base = (address)rw_region->mapped_base();
1966 address rw_patch_end = (address)rw_region->mapped_end();
1967
1968 // Patch all pointers inside the RO region
2046
2047 void FileMapInfo::map_or_load_heap_region() {
2048 bool success = false;
2049
2050 if (can_use_heap_region()) {
2051 if (ArchiveHeapLoader::can_map()) {
2052 success = map_heap_region();
2053 } else if (ArchiveHeapLoader::can_load()) {
2054 success = ArchiveHeapLoader::load_heap_region(this);
2055 } else {
2056 if (!UseCompressedOops && !ArchiveHeapLoader::can_map()) {
2057 // TODO - remove implicit knowledge of G1
2058 log_info(cds)("Cannot use CDS heap data. UseG1GC is required for -XX:-UseCompressedOops");
2059 } else {
2060 log_info(cds)("Cannot use CDS heap data. UseEpsilonGC, UseG1GC, UseSerialGC, UseParallelGC, or UseShenandoahGC are required.");
2061 }
2062 }
2063 }
2064
2065 if (!success) {
2066 CDSConfig::stop_using_full_module_graph();
2067 }
2068 }
2069
2070 bool FileMapInfo::can_use_heap_region() {
2071 if (!has_heap_region()) {
2072 return false;
2073 }
2074 if (JvmtiExport::should_post_class_file_load_hook() && JvmtiExport::has_early_class_hook_env()) {
2075 ShouldNotReachHere(); // CDS should have been disabled.
2076 // The archived objects are mapped at JVM start-up, but we don't know if
2077 // j.l.String or j.l.Class might be replaced by the ClassFileLoadHook,
2078 // which would make the archived String or mirror objects invalid. Let's be safe and not
2079 // use the archived objects. These 2 classes are loaded during the JVMTI "early" stage.
2080 //
2081 // If JvmtiExport::has_early_class_hook_env() is false, the classes of some objects
2082 // in the archived subgraphs may be replaced by the ClassFileLoadHook. But that's OK
2083 // because we won't install an archived object subgraph if the klass of any of the
2084 // referenced objects are replaced. See HeapShared::initialize_from_archived_subgraph().
2085 }
2086
2375 return false;
2376 }
2377
2378 if (!open_for_read() || !init_from_file(_fd) || !validate_header()) {
2379 if (_is_static) {
2380 log_info(cds)("Initialize static archive failed.");
2381 return false;
2382 } else {
2383 log_info(cds)("Initialize dynamic archive failed.");
2384 if (AutoCreateSharedArchive) {
2385 CDSConfig::enable_dumping_dynamic_archive();
2386 ArchiveClassesAtExit = CDSConfig::dynamic_archive_path();
2387 }
2388 return false;
2389 }
2390 }
2391
2392 return true;
2393 }
2394
2395 // The 2 core spaces are RW->RO
2396 FileMapRegion* FileMapInfo::first_core_region() const {
2397 return region_at(MetaspaceShared::rw);
2398 }
2399
2400 FileMapRegion* FileMapInfo::last_core_region() const {
2401 return region_at(MetaspaceShared::ro);
2402 }
2403
2404 void FileMapInfo::print(outputStream* st) const {
2405 header()->print(st);
2406 if (!is_static()) {
2407 dynamic_header()->print(st);
2408 }
2409 }
2410
2411 void FileMapHeader::set_as_offset(char* p, size_t *offset) {
2412 *offset = ArchiveBuilder::current()->any_to_offset((address)p);
2413 }
2414
2480 }
2481
2482 if (_allow_archiving_with_java_agent) {
2483 log_warning(cds)("This archive was created with AllowArchivingWithJavaAgent. It should be used "
2484 "for testing purposes only and should not be used in a production environment");
2485 }
2486
2487 log_info(cds)("Archive was created with UseCompressedOops = %d, UseCompressedClassPointers = %d",
2488 compressed_oops(), compressed_class_pointers());
2489 if (compressed_oops() != UseCompressedOops || compressed_class_pointers() != UseCompressedClassPointers) {
2490 log_info(cds)("Unable to use shared archive.\nThe saved state of UseCompressedOops and UseCompressedClassPointers is "
2491 "different from runtime, CDS will be disabled.");
2492 return false;
2493 }
2494
2495 if (! _use_secondary_supers_table && UseSecondarySupersTable) {
2496 log_warning(cds)("The shared archive was created without UseSecondarySupersTable.");
2497 return false;
2498 }
2499
2500 if (!_use_optimized_module_handling) {
2501 CDSConfig::stop_using_optimized_module_handling();
2502 log_info(cds)("optimized module handling: disabled because archive was created without optimized module handling");
2503 }
2504
2505 if (is_static() && !_has_full_module_graph) {
2506 // Only the static archive can contain the full module graph.
2507 CDSConfig::stop_using_full_module_graph("archive was created without full module graph");
2508 }
2509
2510 return true;
2511 }
2512
2513 bool FileMapInfo::validate_header() {
2514 if (!header()->validate()) {
2515 return false;
2516 }
2517 if (_is_static) {
2518 return true;
2519 } else {
2520 return DynamicArchive::validate(this);
2521 }
2522 }
2523
2524 #if INCLUDE_JVMTI
2525 ClassPathEntry** FileMapInfo::_classpath_entries_for_jvmti = nullptr;
2526
2527 ClassPathEntry* FileMapInfo::get_classpath_entry_for_jvmti(int i, TRAPS) {
|
197 set_magic(CDSConfig::is_dumping_dynamic_archive() ? CDS_DYNAMIC_ARCHIVE_MAGIC : CDS_ARCHIVE_MAGIC);
198 set_version(CURRENT_CDS_ARCHIVE_VERSION);
199
200 if (!info->is_static() && base_archive_name_size != 0) {
201 // copy base archive name
202 copy_base_archive_name(CDSConfig::static_archive_path());
203 }
204 _core_region_alignment = core_region_alignment;
205 _obj_alignment = ObjectAlignmentInBytes;
206 _compact_strings = CompactStrings;
207 if (CDSConfig::is_dumping_heap()) {
208 _narrow_oop_mode = CompressedOops::mode();
209 _narrow_oop_base = CompressedOops::base();
210 _narrow_oop_shift = CompressedOops::shift();
211 }
212 _compressed_oops = UseCompressedOops;
213 _compressed_class_ptrs = UseCompressedClassPointers;
214 _use_secondary_supers_table = UseSecondarySupersTable;
215 _max_heap_size = MaxHeapSize;
216 _use_optimized_module_handling = CDSConfig::is_using_optimized_module_handling();
217 _has_aot_linked_classes = CDSConfig::is_dumping_aot_linked_classes();
218 _has_full_module_graph = CDSConfig::is_dumping_full_module_graph();
219 _has_archived_invokedynamic = CDSConfig::is_dumping_invokedynamic();
220 _has_archived_packages = CDSConfig::is_dumping_packages();
221 _has_archived_protection_domains = CDSConfig::is_dumping_protection_domains();
222 _gc_kind = (int)Universe::heap()->kind();
223 jio_snprintf(_gc_name, sizeof(_gc_name), Universe::heap()->name());
224
225 // The following fields are for sanity checks for whether this archive
226 // will function correctly with this JVM and the bootclasspath it's
227 // invoked with.
228
229 // JVM version string ... changes on each build.
230 get_header_version(_jvm_ident);
231
232 _app_class_paths_start_index = ClassLoaderExt::app_class_paths_start_index();
233 _app_module_paths_start_index = ClassLoaderExt::app_module_paths_start_index();
234 _max_used_path_index = ClassLoaderExt::max_used_path_index();
235 _num_module_paths = ClassLoader::num_module_path_entries();
236
237 _verify_local = BytecodeVerificationLocal;
238 _verify_remote = BytecodeVerificationRemote;
239 _has_platform_or_app_classes = ClassLoaderExt::has_platform_or_app_classes();
240 _has_non_jar_in_classpath = ClassLoaderExt::has_non_jar_in_classpath();
241 _requested_base_address = (char*)SharedBaseAddress;
242 _mapped_base_address = (char*)SharedBaseAddress;
243 _allow_archiving_with_java_agent = AllowArchivingWithJavaAgent;
290 st->print_cr("- num_module_paths: %d", _num_module_paths);
291 st->print_cr("- max_used_path_index: %d", _max_used_path_index);
292 st->print_cr("- verify_local: %d", _verify_local);
293 st->print_cr("- verify_remote: %d", _verify_remote);
294 st->print_cr("- has_platform_or_app_classes: %d", _has_platform_or_app_classes);
295 st->print_cr("- has_non_jar_in_classpath: %d", _has_non_jar_in_classpath);
296 st->print_cr("- requested_base_address: " INTPTR_FORMAT, p2i(_requested_base_address));
297 st->print_cr("- mapped_base_address: " INTPTR_FORMAT, p2i(_mapped_base_address));
298 st->print_cr("- heap_root_segments.roots_count: %d" , _heap_root_segments.roots_count());
299 st->print_cr("- heap_root_segments.base_offset: " SIZE_FORMAT_X, _heap_root_segments.base_offset());
300 st->print_cr("- heap_root_segments.count: " SIZE_FORMAT, _heap_root_segments.count());
301 st->print_cr("- heap_root_segments.max_size_elems: %d", _heap_root_segments.max_size_in_elems());
302 st->print_cr("- heap_root_segments.max_size_bytes: %d", _heap_root_segments.max_size_in_bytes());
303 st->print_cr("- _heap_oopmap_start_pos: " SIZE_FORMAT, _heap_oopmap_start_pos);
304 st->print_cr("- _heap_ptrmap_start_pos: " SIZE_FORMAT, _heap_ptrmap_start_pos);
305 st->print_cr("- _rw_ptrmap_start_pos: " SIZE_FORMAT, _rw_ptrmap_start_pos);
306 st->print_cr("- _ro_ptrmap_start_pos: " SIZE_FORMAT, _ro_ptrmap_start_pos);
307 st->print_cr("- allow_archiving_with_java_agent:%d", _allow_archiving_with_java_agent);
308 st->print_cr("- use_optimized_module_handling: %d", _use_optimized_module_handling);
309 st->print_cr("- has_full_module_graph %d", _has_full_module_graph);
310 st->print_cr("- has_aot_linked_classes %d", _has_aot_linked_classes);
311 st->print_cr("- has_archived_invokedynamic %d", _has_archived_invokedynamic);
312 st->print_cr("- has_archived_packages %d", _has_archived_packages);
313 st->print_cr("- has_archived_protection_domains %d", _has_archived_protection_domains);
314 st->print_cr("- ptrmap_size_in_bits: " SIZE_FORMAT, _ptrmap_size_in_bits);
315 }
316
317 void SharedClassPathEntry::init_as_non_existent(const char* path, TRAPS) {
318 _type = non_existent_entry;
319 set_name(path, CHECK);
320 }
321
322 void SharedClassPathEntry::init(bool is_modules_image,
323 bool is_module_path,
324 ClassPathEntry* cpe, TRAPS) {
325 assert(CDSConfig::is_dumping_archive(), "sanity");
326 _timestamp = 0;
327 _filesize = 0;
328 _from_class_path_attr = false;
329
330 struct stat st;
331 if (os::stat(cpe->name(), &st) == 0) {
332 if ((st.st_mode & S_IFMT) == S_IFDIR) {
333 _type = dir_entry;
334 } else {
559
560 void FileMapInfo::record_non_existent_class_path_entry(const char* path) {
561 assert(CDSConfig::is_dumping_archive(), "sanity");
562 log_info(class, path)("non-existent Class-Path entry %s", path);
563 if (_non_existent_class_paths == nullptr) {
564 _non_existent_class_paths = new (mtClass) GrowableArray<const char*>(10, mtClass);
565 }
566 _non_existent_class_paths->append(os::strdup(path));
567 }
568
569 int FileMapInfo::num_non_existent_class_paths() {
570 assert(CDSConfig::is_dumping_archive(), "sanity");
571 if (_non_existent_class_paths != nullptr) {
572 return _non_existent_class_paths->length();
573 } else {
574 return 0;
575 }
576 }
577
578 int FileMapInfo::get_module_shared_path_index(Symbol* location) {
579 if (location == nullptr) {
580 return 0; // Used by java/lang/reflect/Proxy$ProxyBuilder
581 }
582 if (location->starts_with("jrt:", 4) && get_number_of_shared_paths() > 0) {
583 assert(shared_path(0)->is_modules_image(), "first shared_path must be the modules image");
584 return 0;
585 }
586
587 if (ClassLoaderExt::app_module_paths_start_index() >= get_number_of_shared_paths()) {
588 // The archive(s) were created without --module-path option
589 return -1;
590 }
591
592 if (!location->starts_with("file:", 5)) {
593 return -1;
594 }
595
596 // skip_uri_protocol was also called during dump time -- see ClassLoaderExt::process_module_table()
597 ResourceMark rm;
598 const char* file = ClassLoader::uri_to_path(location->as_C_string());
599 for (int i = ClassLoaderExt::app_module_paths_start_index(); i < get_number_of_shared_paths(); i++) {
600 SharedClassPathEntry* ent = shared_path(i);
601 if (!ent->is_non_existent()) {
1045 }
1046
1047 if (header()->max_used_path_index() == 0) {
1048 // default archive only contains the module image in the bootclasspath
1049 assert(shared_path(0)->is_modules_image(), "first shared_path must be the modules image");
1050 } else {
1051 if (!validate_boot_class_paths() || !validate_app_class_paths(shared_app_paths_len)) {
1052 const char* mismatch_msg = "shared class paths mismatch";
1053 const char* hint_msg = log_is_enabled(Info, class, path) ?
1054 "" : " (hint: enable -Xlog:class+path=info to diagnose the failure)";
1055 if (RequireSharedSpaces) {
1056 log_error(cds)("%s%s", mismatch_msg, hint_msg);
1057 MetaspaceShared::unrecoverable_loading_error();
1058 } else {
1059 log_warning(cds)("%s%s", mismatch_msg, hint_msg);
1060 }
1061 return false;
1062 }
1063 }
1064
1065 if (!validate_non_existent_class_paths()) {
1066 return false;
1067 }
1068
1069 _validating_shared_path_table = false;
1070
1071 #if INCLUDE_JVMTI
1072 if (_classpath_entries_for_jvmti != nullptr) {
1073 os::free(_classpath_entries_for_jvmti);
1074 }
1075 size_t sz = sizeof(ClassPathEntry*) * get_number_of_shared_paths();
1076 _classpath_entries_for_jvmti = (ClassPathEntry**)os::malloc(sz, mtClass);
1077 memset((void*)_classpath_entries_for_jvmti, 0, sz);
1078 #endif
1079
1080 return true;
1081 }
1082
1083 bool FileMapInfo::validate_non_existent_class_paths() {
1084 // All of the recorded non-existent paths came from the Class-Path: attribute from the JAR
1085 // files on the app classpath. If any of these are found to exist during runtime,
1086 // it will change how classes are loading for the app loader. For safety, disable
1087 // loading of archived platform/app classes (currently there's no way to disable just the
1088 // app classes).
1089
1090 assert(CDSConfig::is_using_archive(), "runtime only");
1091 for (int i = header()->app_module_paths_start_index() + header()->num_module_paths();
1092 i < get_number_of_shared_paths();
1093 i++) {
1094 SharedClassPathEntry* ent = shared_path(i);
1095 if (!ent->check_non_existent()) {
1096 if (header()->has_aot_linked_classes()) {
1097 log_error(cds)("CDS archive has aot-linked classes. It cannot be used because the "
1098 "file %s exists", ent->name());
1099 return false;
1100 } else {
1101 log_warning(cds)("Archived non-system classes are disabled because the "
1102 "file %s exists", ent->name());
1103 header()->set_has_platform_or_app_classes(false);
1104 }
1105 }
1106 }
1107
1108 return true;
1109 }
1110
1111 // A utility class for reading/validating the GenericCDSFileMapHeader portion of
1112 // a CDS archive's header. The file header of all CDS archives with versions from
1113 // CDS_GENERIC_HEADER_SUPPORTED_MIN_VERSION (12) are guaranteed to always start
1114 // with GenericCDSFileMapHeader. This makes it possible to read important information
1115 // from a CDS archive created by a different version of HotSpot, so that we can
1116 // automatically regenerate the archive as necessary (JDK-8261455).
1117 class FileHeaderHelper {
1118 int _fd;
1119 bool _is_valid;
1120 bool _is_static;
1121 GenericCDSFileMapHeader* _header;
1122 const char* _archive_name;
1123 const char* _base_archive_name;
1124
1125 public:
1126 FileHeaderHelper(const char* archive_name, bool is_static) {
1127 _fd = -1;
1128 _is_valid = false;
1517 bool FileMapRegion::check_region_crc(char* base) const {
1518 // This function should be called after the region has been properly
1519 // loaded into memory via FileMapInfo::map_region() or FileMapInfo::read_region().
1520 // I.e., this->mapped_base() must be valid.
1521 size_t sz = used();
1522 if (sz == 0) {
1523 return true;
1524 }
1525
1526 assert(base != nullptr, "must be initialized");
1527 int crc = ClassLoader::crc32(0, base, (jint)sz);
1528 if (crc != this->crc()) {
1529 log_warning(cds)("Checksum verification failed.");
1530 return false;
1531 }
1532 return true;
1533 }
1534
1535 static const char* region_name(int region_index) {
1536 static const char* names[] = {
1537 "rw", "ro", "bm", "hp", "cc",
1538 };
1539 const int num_regions = sizeof(names)/sizeof(names[0]);
1540 assert(0 <= region_index && region_index < num_regions, "sanity");
1541
1542 return names[region_index];
1543 }
1544
1545 BitMapView FileMapInfo::bitmap_view(int region_index, bool is_oopmap) {
1546 FileMapRegion* r = region_at(region_index);
1547 char* bitmap_base = is_static() ? FileMapInfo::current_info()->map_bitmap_region() : FileMapInfo::dynamic_info()->map_bitmap_region();
1548 bitmap_base += is_oopmap ? r->oopmap_offset() : r->ptrmap_offset();
1549 size_t size_in_bits = is_oopmap ? r->oopmap_size_in_bits() : r->ptrmap_size_in_bits();
1550
1551 log_debug(cds, reloc)("mapped %s relocation %smap @ " INTPTR_FORMAT " (" SIZE_FORMAT " bits)",
1552 region_name(region_index), is_oopmap ? "oop" : "ptr",
1553 p2i(bitmap_base), size_in_bits);
1554
1555 return BitMapView((BitMap::bm_word_t*)(bitmap_base), size_in_bits);
1556 }
1557
1603 mapping_offset = (size_t)((address)requested_base - CompressedOops::base());
1604 assert((mapping_offset >> CompressedOops::shift()) << CompressedOops::shift() == mapping_offset, "must be");
1605 } else {
1606 mapping_offset = 0; // not used with !UseCompressedOops
1607 }
1608 #endif // INCLUDE_CDS_JAVA_HEAP
1609 } else {
1610 char* requested_SharedBaseAddress = (char*)MetaspaceShared::requested_base_address();
1611 requested_base = ArchiveBuilder::current()->to_requested(base);
1612 assert(requested_base >= requested_SharedBaseAddress, "must be");
1613 mapping_offset = requested_base - requested_SharedBaseAddress;
1614 }
1615
1616 r->set_file_offset(_file_offset);
1617 int crc = ClassLoader::crc32(0, base, (jint)size);
1618 if (size > 0) {
1619 log_info(cds)("Shared file region (%s) %d: " SIZE_FORMAT_W(8)
1620 " bytes, addr " INTPTR_FORMAT " file offset 0x%08" PRIxPTR
1621 " crc 0x%08x",
1622 region_name(region), region, size, p2i(requested_base), _file_offset, crc);
1623 } else {
1624 log_info(cds)("Shared file region (%s) %d: " SIZE_FORMAT_W(8)
1625 " bytes", region_name(region), region, size);
1626 }
1627
1628 r->init(region, mapping_offset, size, read_only, allow_exec, crc);
1629
1630 if (base != nullptr) {
1631 write_bytes_aligned(base, size);
1632 }
1633 }
1634
1635 static size_t write_bitmap(const CHeapBitMap* map, char* output, size_t offset) {
1636 size_t size_in_bytes = map->size_in_bytes();
1637 map->write_to((BitMap::bm_word_t*)(output + offset), size_in_bytes);
1638 return offset + size_in_bytes;
1639 }
1640
1641 // The sorting code groups the objects with non-null oop/ptrs together.
1642 // Relevant bitmaps then have lots of leading and trailing zeros, which
1643 // we do not have to store.
1644 size_t FileMapInfo::remove_bitmap_zeros(CHeapBitMap* map) {
1645 BitMap::idx_t first_set = map->find_first_set_bit(0);
1646 BitMap::idx_t last_set = map->find_last_set_bit(0);
1647 size_t old_size = map->size();
1648
1649 // Slice and resize bitmap
1650 map->truncate(first_set, last_set + 1);
1651
1652 assert(map->at(0), "First bit should be set");
1653 assert(map->at(map->size() - 1), "Last bit should be set");
1654 assert(map->size() <= old_size, "sanity");
1655
1656 return first_set;
1657 }
1658
1659 char* FileMapInfo::write_bitmap_region(CHeapBitMap* rw_ptrmap, CHeapBitMap* ro_ptrmap,
1660 CHeapBitMap* cc_ptrmap,
1661 ArchiveHeapInfo* heap_info,
1662 size_t &size_in_bytes) {
1663 size_t removed_rw_leading_zeros = remove_bitmap_zeros(rw_ptrmap);
1664 size_t removed_ro_leading_zeros = remove_bitmap_zeros(ro_ptrmap);
1665 header()->set_rw_ptrmap_start_pos(removed_rw_leading_zeros);
1666 header()->set_ro_ptrmap_start_pos(removed_ro_leading_zeros);
1667 size_in_bytes = rw_ptrmap->size_in_bytes() + ro_ptrmap->size_in_bytes() + cc_ptrmap->size_in_bytes();
1668
1669 if (heap_info->is_used()) {
1670 // Remove leading and trailing zeros
1671 size_t removed_oop_leading_zeros = remove_bitmap_zeros(heap_info->oopmap());
1672 size_t removed_ptr_leading_zeros = remove_bitmap_zeros(heap_info->ptrmap());
1673 header()->set_heap_oopmap_start_pos(removed_oop_leading_zeros);
1674 header()->set_heap_ptrmap_start_pos(removed_ptr_leading_zeros);
1675
1676 size_in_bytes += heap_info->oopmap()->size_in_bytes();
1677 size_in_bytes += heap_info->ptrmap()->size_in_bytes();
1678 }
1679
1680 // The bitmap region contains up to 4 parts:
1681 // rw_ptrmap: metaspace pointers inside the read-write region
1682 // ro_ptrmap: metaspace pointers inside the read-only region
1683 // heap_info->oopmap(): Java oop pointers in the heap region
1684 // heap_info->ptrmap(): metaspace pointers in the heap region
1685 char* buffer = NEW_C_HEAP_ARRAY(char, size_in_bytes, mtClassShared);
1686 size_t written = 0;
1687
1688 region_at(MetaspaceShared::rw)->init_ptrmap(0, rw_ptrmap->size());
1689 written = write_bitmap(rw_ptrmap, buffer, written);
1690
1691 region_at(MetaspaceShared::ro)->init_ptrmap(written, ro_ptrmap->size());
1692 written = write_bitmap(ro_ptrmap, buffer, written);
1693
1694 region_at(MetaspaceShared::cc)->init_ptrmap(written, cc_ptrmap->size());
1695 written = write_bitmap(cc_ptrmap, buffer, written);
1696
1697 if (heap_info->is_used()) {
1698 FileMapRegion* r = region_at(MetaspaceShared::hp);
1699
1700 r->init_oopmap(written, heap_info->oopmap()->size());
1701 written = write_bitmap(heap_info->oopmap(), buffer, written);
1702
1703 r->init_ptrmap(written, heap_info->ptrmap()->size());
1704 written = write_bitmap(heap_info->ptrmap(), buffer, written);
1705 }
1706
1707 write_region(MetaspaceShared::bm, (char*)buffer, size_in_bytes, /*read_only=*/true, /*allow_exec=*/false);
1708 return buffer;
1709 }
1710
1711 size_t FileMapInfo::write_heap_region(ArchiveHeapInfo* heap_info) {
1712 char* buffer_start = heap_info->buffer_start();
1713 size_t buffer_size = heap_info->buffer_byte_size();
1714 write_region(MetaspaceShared::hp, buffer_start, buffer_size, false, false);
1715 header()->set_heap_root_segments(heap_info->heap_root_segments());
1716 return buffer_size;
1805 assert(WINDOWS_ONLY(false) NOT_WINDOWS(true), "Don't call on Windows");
1806 // Replace old mapping with new one that is writable.
1807 char *base = os::map_memory(_fd, _full_path, r->file_offset(),
1808 addr, size, false /* !read_only */,
1809 r->allow_exec());
1810 close();
1811 // These have to be errors because the shared region is now unmapped.
1812 if (base == nullptr) {
1813 log_error(cds)("Unable to remap shared readonly space (errno=%d).", errno);
1814 vm_exit(1);
1815 }
1816 if (base != addr) {
1817 log_error(cds)("Unable to remap shared readonly space (errno=%d).", errno);
1818 vm_exit(1);
1819 }
1820 r->set_read_only(false);
1821 return true;
1822 }
1823
1824 // Memory map a region in the address space.
1825 static const char* shared_region_name[] = { "ReadWrite", "ReadOnly", "Bitmap", "Heap", "Code" };
1826
1827 MapArchiveResult FileMapInfo::map_regions(int regions[], int num_regions, char* mapped_base_address, ReservedSpace rs) {
1828 DEBUG_ONLY(FileMapRegion* last_region = nullptr);
1829 intx addr_delta = mapped_base_address - header()->requested_base_address();
1830
1831 // Make sure we don't attempt to use header()->mapped_base_address() unless
1832 // it's been successfully mapped.
1833 DEBUG_ONLY(header()->set_mapped_base_address((char*)(uintptr_t)0xdeadbeef);)
1834
1835 for (int i = 0; i < num_regions; i++) {
1836 int idx = regions[i];
1837 MapArchiveResult result = map_region(idx, addr_delta, mapped_base_address, rs);
1838 if (result != MAP_ARCHIVE_SUCCESS) {
1839 return result;
1840 }
1841 FileMapRegion* r = region_at(idx);
1842 DEBUG_ONLY(if (last_region != nullptr) {
1843 // Ensure that the OS won't be able to allocate new memory spaces between any mapped
1844 // regions, or else it would mess up the simple comparison in MetaspaceObj::is_shared().
1845 assert(r->mapped_base() == last_region->mapped_end(), "must have no gaps");
1908 r->set_read_only(false); // Need to patch the pointers
1909 }
1910
1911 if (MetaspaceShared::use_windows_memory_mapping() && rs.is_reserved()) {
1912 // This is the second time we try to map the archive(s). We have already created a ReservedSpace
1913 // that covers all the FileMapRegions to ensure all regions can be mapped. However, Windows
1914 // can't mmap into a ReservedSpace, so we just ::read() the data. We're going to patch all the
1915 // regions anyway, so there's no benefit for mmap anyway.
1916 if (!read_region(i, requested_addr, size, /* do_commit = */ true)) {
1917 log_info(cds)("Failed to read %s shared space into reserved space at " INTPTR_FORMAT,
1918 shared_region_name[i], p2i(requested_addr));
1919 return MAP_ARCHIVE_OTHER_FAILURE; // oom or I/O error.
1920 } else {
1921 assert(r->mapped_base() != nullptr, "must be initialized");
1922 return MAP_ARCHIVE_SUCCESS;
1923 }
1924 } else {
1925 // Note that this may either be a "fresh" mapping into unreserved address
1926 // space (Windows, first mapping attempt), or a mapping into pre-reserved
1927 // space (Posix). See also comment in MetaspaceShared::map_archives().
1928 bool read_only = r->read_only() && !CDSConfig::is_dumping_final_static_archive();
1929 char* base = map_memory(_fd, _full_path, r->file_offset(),
1930 requested_addr, size, read_only,
1931 r->allow_exec(), mtClassShared);
1932 if (base != requested_addr) {
1933 log_info(cds)("Unable to map %s shared space at " INTPTR_FORMAT,
1934 shared_region_name[i], p2i(requested_addr));
1935 _memory_mapping_failed = true;
1936 return MAP_ARCHIVE_MMAP_FAILURE;
1937 }
1938
1939 if (VerifySharedSpaces && !r->check_region_crc(requested_addr)) {
1940 return MAP_ARCHIVE_OTHER_FAILURE;
1941 }
1942
1943 r->set_mapped_from_file(true);
1944 r->set_mapped_base(requested_addr);
1945
1946 return MAP_ARCHIVE_SUCCESS;
1947 }
1948 }
1949
1950 // The return value is the location of the archive relocation bitmap.
1962 return nullptr;
1963 }
1964
1965 if (VerifySharedSpaces && !r->check_region_crc(bitmap_base)) {
1966 log_error(cds)("relocation bitmap CRC error");
1967 if (!os::unmap_memory(bitmap_base, r->used_aligned())) {
1968 fatal("os::unmap_memory of relocation bitmap failed");
1969 }
1970 return nullptr;
1971 }
1972
1973 r->set_mapped_from_file(true);
1974 r->set_mapped_base(bitmap_base);
1975 log_info(cds)("Mapped %s region #%d at base " INTPTR_FORMAT " top " INTPTR_FORMAT " (%s)",
1976 is_static() ? "static " : "dynamic",
1977 MetaspaceShared::bm, p2i(r->mapped_base()), p2i(r->mapped_end()),
1978 shared_region_name[MetaspaceShared::bm]);
1979 return bitmap_base;
1980 }
1981
1982 bool FileMapInfo::map_cached_code_region(ReservedSpace rs) {
1983 FileMapRegion* r = region_at(MetaspaceShared::cc);
1984 assert(r->used() > 0 && r->used_aligned() == rs.size(), "must be");
1985
1986 char* requested_base = rs.base();
1987 assert(requested_base != nullptr, "should be inside code cache");
1988
1989 char* mapped_base;
1990 if (MetaspaceShared::use_windows_memory_mapping()) {
1991 if (!read_region(MetaspaceShared::cc, requested_base, r->used_aligned(), /* do_commit = */ true)) {
1992 log_info(cds)("Failed to read cc shared space into reserved space at " INTPTR_FORMAT,
1993 p2i(requested_base));
1994 return false;
1995 }
1996 mapped_base = requested_base;
1997 } else {
1998 bool read_only = false, allow_exec = false;
1999 mapped_base = map_memory(_fd, _full_path, r->file_offset(),
2000 requested_base, r->used_aligned(), read_only, allow_exec, mtClassShared);
2001 }
2002 if (mapped_base == nullptr) {
2003 log_info(cds)("failed to map cached code region");
2004 return false;
2005 } else {
2006 assert(mapped_base == requested_base, "must be");
2007 r->set_mapped_from_file(true);
2008 r->set_mapped_base(mapped_base);
2009 relocate_pointers_in_cached_code_region();
2010 log_info(cds)("Mapped static region #%d at base " INTPTR_FORMAT " top " INTPTR_FORMAT " (%s)",
2011 MetaspaceShared::cc, p2i(r->mapped_base()), p2i(r->mapped_end()),
2012 shared_region_name[MetaspaceShared::cc]);
2013 return true;
2014 }
2015 }
2016
2017 class CachedCodeRelocator: public BitMapClosure {
2018 address _code_requested_base;
2019 address* _patch_base;
2020 intx _code_delta;
2021 intx _metadata_delta;
2022
2023 public:
2024 CachedCodeRelocator(address code_requested_base, address code_mapped_base,
2025 intx metadata_delta) {
2026 _code_requested_base = code_requested_base;
2027 _patch_base = (address*)code_mapped_base;
2028 _code_delta = code_mapped_base - code_requested_base;
2029 _metadata_delta = metadata_delta;
2030 }
2031
2032 bool do_bit(size_t offset) {
2033 address* p = _patch_base + offset;
2034 address requested_ptr = *p;
2035 if (requested_ptr < _code_requested_base) {
2036 *p = requested_ptr + _metadata_delta;
2037 } else {
2038 *p = requested_ptr + _code_delta;
2039 }
2040 return true; // keep iterating
2041 }
2042 };
2043
2044 void FileMapInfo::relocate_pointers_in_cached_code_region() {
2045 FileMapRegion* r = region_at(MetaspaceShared::cc);
2046 char* bitmap_base = map_bitmap_region();
2047
2048 BitMapView cc_ptrmap = ptrmap_view(MetaspaceShared::cc);
2049 if (cc_ptrmap.size() == 0) {
2050 return;
2051 }
2052
2053 address core_regions_requested_base = (address)header()->requested_base_address();
2054 address core_regions_mapped_base = (address)header()->mapped_base_address();
2055 address cc_region_requested_base = core_regions_requested_base + r->mapping_offset();
2056 address cc_region_mapped_base = (address)r->mapped_base();
2057
2058 size_t max_bits_for_core_regions = pointer_delta(mapped_end(), mapped_base(), // FIXME - renamed to core_regions_mapped_base(), etc
2059 sizeof(address));
2060
2061 CachedCodeRelocator patcher(cc_region_requested_base, cc_region_mapped_base,
2062 core_regions_mapped_base - core_regions_requested_base);
2063 cc_ptrmap.iterate(&patcher);
2064 }
2065
2066 // This is called when we cannot map the archive at the requested[ base address (usually 0x800000000).
2067 // We relocate all pointers in the 2 core regions (ro, rw).
2068 bool FileMapInfo::relocate_pointers_in_core_regions(intx addr_delta) {
2069 log_debug(cds, reloc)("runtime archive relocation start");
2070 char* bitmap_base = map_bitmap_region();
2071
2072 if (bitmap_base == nullptr) {
2073 return false; // OOM, or CRC check failure
2074 } else {
2075 BitMapView rw_ptrmap = ptrmap_view(MetaspaceShared::rw);
2076 BitMapView ro_ptrmap = ptrmap_view(MetaspaceShared::ro);
2077
2078 FileMapRegion* rw_region = first_core_region();
2079 FileMapRegion* ro_region = last_core_region();
2080
2081 // Patch all pointers inside the RW region
2082 address rw_patch_base = (address)rw_region->mapped_base();
2083 address rw_patch_end = (address)rw_region->mapped_end();
2084
2085 // Patch all pointers inside the RO region
2163
2164 void FileMapInfo::map_or_load_heap_region() {
2165 bool success = false;
2166
2167 if (can_use_heap_region()) {
2168 if (ArchiveHeapLoader::can_map()) {
2169 success = map_heap_region();
2170 } else if (ArchiveHeapLoader::can_load()) {
2171 success = ArchiveHeapLoader::load_heap_region(this);
2172 } else {
2173 if (!UseCompressedOops && !ArchiveHeapLoader::can_map()) {
2174 // TODO - remove implicit knowledge of G1
2175 log_info(cds)("Cannot use CDS heap data. UseG1GC is required for -XX:-UseCompressedOops");
2176 } else {
2177 log_info(cds)("Cannot use CDS heap data. UseEpsilonGC, UseG1GC, UseSerialGC, UseParallelGC, or UseShenandoahGC are required.");
2178 }
2179 }
2180 }
2181
2182 if (!success) {
2183 if (CDSConfig::is_using_aot_linked_classes() && !CDSConfig::is_dumping_final_static_archive()) {
2184 // It's too later to recover -- we have already committed to use the archived metaspace objects, but
2185 // the archived heap objects cannot be loaded, so we don't have the archived FMG to guarantee that
2186 // all AOT-linked classes are visible.
2187 //
2188 // We get here because the heap is too small. The app will fail anyway. So let's quit.
2189 MetaspaceShared::unrecoverable_loading_error("CDS archive has aot-linked classes but the archived "
2190 "heap objects cannot be loaded. Try increasing your heap size.");
2191 }
2192 CDSConfig::stop_using_full_module_graph("archive heap loading failed");
2193 }
2194 }
2195
2196 bool FileMapInfo::can_use_heap_region() {
2197 if (!has_heap_region()) {
2198 return false;
2199 }
2200 if (JvmtiExport::should_post_class_file_load_hook() && JvmtiExport::has_early_class_hook_env()) {
2201 ShouldNotReachHere(); // CDS should have been disabled.
2202 // The archived objects are mapped at JVM start-up, but we don't know if
2203 // j.l.String or j.l.Class might be replaced by the ClassFileLoadHook,
2204 // which would make the archived String or mirror objects invalid. Let's be safe and not
2205 // use the archived objects. These 2 classes are loaded during the JVMTI "early" stage.
2206 //
2207 // If JvmtiExport::has_early_class_hook_env() is false, the classes of some objects
2208 // in the archived subgraphs may be replaced by the ClassFileLoadHook. But that's OK
2209 // because we won't install an archived object subgraph if the klass of any of the
2210 // referenced objects are replaced. See HeapShared::initialize_from_archived_subgraph().
2211 }
2212
2501 return false;
2502 }
2503
2504 if (!open_for_read() || !init_from_file(_fd) || !validate_header()) {
2505 if (_is_static) {
2506 log_info(cds)("Initialize static archive failed.");
2507 return false;
2508 } else {
2509 log_info(cds)("Initialize dynamic archive failed.");
2510 if (AutoCreateSharedArchive) {
2511 CDSConfig::enable_dumping_dynamic_archive();
2512 ArchiveClassesAtExit = CDSConfig::dynamic_archive_path();
2513 }
2514 return false;
2515 }
2516 }
2517
2518 return true;
2519 }
2520
2521 bool FileMapInfo::validate_aot_class_linking() {
2522 // These checks need to be done after FileMapInfo::initialize(), which gets called before Universe::heap()
2523 // is available.
2524 if (header()->has_aot_linked_classes()) {
2525 CDSConfig::set_has_aot_linked_classes(is_static(), true);
2526 if (JvmtiExport::should_post_class_file_load_hook()) {
2527 log_error(cds)("CDS archive has aot-linked classes. It cannot be used when JVMTI ClassFileLoadHook is in use.");
2528 return false;
2529 }
2530 if (JvmtiExport::has_early_vmstart_env()) {
2531 log_error(cds)("CDS archive has aot-linked classes. It cannot be used when JVMTI early vm start is in use.");
2532 return false;
2533 }
2534 if (!CDSConfig::is_using_full_module_graph() && !CDSConfig::is_dumping_final_static_archive()) {
2535 log_error(cds)("CDS archive has aot-linked classes. It cannot be used when archived full module graph is not used.");
2536 return false;
2537 }
2538 if (header()->gc_kind() != (int)Universe::heap()->kind()) {
2539 log_error(cds)("CDS archive has aot-linked classes. It cannot be used because GC used during dump time (%s) is not the same as runtime (%s)",
2540 header()->gc_name(), Universe::heap()->name());
2541 return false;
2542 }
2543 }
2544
2545 return true;
2546 }
2547
2548 // The 2 core spaces are RW->RO
2549 FileMapRegion* FileMapInfo::first_core_region() const {
2550 return region_at(MetaspaceShared::rw);
2551 }
2552
2553 FileMapRegion* FileMapInfo::last_core_region() const {
2554 return region_at(MetaspaceShared::ro);
2555 }
2556
2557 void FileMapInfo::print(outputStream* st) const {
2558 header()->print(st);
2559 if (!is_static()) {
2560 dynamic_header()->print(st);
2561 }
2562 }
2563
2564 void FileMapHeader::set_as_offset(char* p, size_t *offset) {
2565 *offset = ArchiveBuilder::current()->any_to_offset((address)p);
2566 }
2567
2633 }
2634
2635 if (_allow_archiving_with_java_agent) {
2636 log_warning(cds)("This archive was created with AllowArchivingWithJavaAgent. It should be used "
2637 "for testing purposes only and should not be used in a production environment");
2638 }
2639
2640 log_info(cds)("Archive was created with UseCompressedOops = %d, UseCompressedClassPointers = %d",
2641 compressed_oops(), compressed_class_pointers());
2642 if (compressed_oops() != UseCompressedOops || compressed_class_pointers() != UseCompressedClassPointers) {
2643 log_info(cds)("Unable to use shared archive.\nThe saved state of UseCompressedOops and UseCompressedClassPointers is "
2644 "different from runtime, CDS will be disabled.");
2645 return false;
2646 }
2647
2648 if (! _use_secondary_supers_table && UseSecondarySupersTable) {
2649 log_warning(cds)("The shared archive was created without UseSecondarySupersTable.");
2650 return false;
2651 }
2652
2653 if (!_use_optimized_module_handling && !CDSConfig::is_dumping_final_static_archive()) {
2654 CDSConfig::stop_using_optimized_module_handling();
2655 log_info(cds)("optimized module handling: disabled because archive was created without optimized module handling");
2656 }
2657
2658 if (is_static()) {
2659 // Only the static archive can contain the full module graph.
2660 if (!_has_full_module_graph) {
2661 CDSConfig::stop_using_full_module_graph("archive was created without full module graph");
2662 }
2663
2664 if (_has_archived_invokedynamic) {
2665 CDSConfig::set_has_archived_invokedynamic();
2666 }
2667 if (_has_archived_packages) {
2668 CDSConfig::set_is_loading_packages();
2669 }
2670 if (_has_archived_protection_domains) {
2671 CDSConfig::set_is_loading_protection_domains();
2672 }
2673 }
2674
2675 return true;
2676 }
2677
2678 bool FileMapInfo::validate_header() {
2679 if (!header()->validate()) {
2680 return false;
2681 }
2682 if (_is_static) {
2683 return true;
2684 } else {
2685 return DynamicArchive::validate(this);
2686 }
2687 }
2688
2689 #if INCLUDE_JVMTI
2690 ClassPathEntry** FileMapInfo::_classpath_entries_for_jvmti = nullptr;
2691
2692 ClassPathEntry* FileMapInfo::get_classpath_entry_for_jvmti(int i, TRAPS) {
|