< prev index next >

src/hotspot/share/cds/filemap.cpp

Print this page

 209   if (CDSConfig::is_dumping_heap()) {
 210     _narrow_oop_mode = CompressedOops::mode();
 211     _narrow_oop_base = CompressedOops::base();
 212     _narrow_oop_shift = CompressedOops::shift();
 213   }
 214   _compressed_oops = UseCompressedOops;
 215   _compressed_class_ptrs = UseCompressedClassPointers;
 216   if (UseCompressedClassPointers) {
 217 #ifdef _LP64
 218     _narrow_klass_pointer_bits = CompressedKlassPointers::narrow_klass_pointer_bits();
 219     _narrow_klass_shift = ArchiveBuilder::precomputed_narrow_klass_shift();
 220 #endif
 221   } else {
 222     _narrow_klass_pointer_bits = _narrow_klass_shift = -1;
 223   }
 224   _max_heap_size = MaxHeapSize;
 225   _use_optimized_module_handling = CDSConfig::is_using_optimized_module_handling();
 226   _has_aot_linked_classes = CDSConfig::is_dumping_aot_linked_classes();
 227   _has_full_module_graph = CDSConfig::is_dumping_full_module_graph();
 228   _has_archived_invokedynamic = CDSConfig::is_dumping_invokedynamic();




 229 
 230   // The following fields are for sanity checks for whether this archive
 231   // will function correctly with this JVM and the bootclasspath it's
 232   // invoked with.
 233 
 234   // JVM version string ... changes on each build.
 235   get_header_version(_jvm_ident);
 236 
 237   _app_class_paths_start_index = ClassLoaderExt::app_class_paths_start_index();
 238   _app_module_paths_start_index = ClassLoaderExt::app_module_paths_start_index();
 239   _max_used_path_index = ClassLoaderExt::max_used_path_index();
 240   _num_module_paths = ClassLoader::num_module_path_entries();
 241 
 242   _verify_local = BytecodeVerificationLocal;
 243   _verify_remote = BytecodeVerificationRemote;
 244   _has_platform_or_app_classes = ClassLoaderExt::has_platform_or_app_classes();
 245   _has_non_jar_in_classpath = ClassLoaderExt::has_non_jar_in_classpath();
 246   _requested_base_address = (char*)SharedBaseAddress;
 247   _mapped_base_address = (char*)SharedBaseAddress;
 248   _allow_archiving_with_java_agent = AllowArchivingWithJavaAgent;

 300   st->print_cr("- verify_local:                   %d", _verify_local);
 301   st->print_cr("- verify_remote:                  %d", _verify_remote);
 302   st->print_cr("- has_platform_or_app_classes:    %d", _has_platform_or_app_classes);
 303   st->print_cr("- has_non_jar_in_classpath:       %d", _has_non_jar_in_classpath);
 304   st->print_cr("- requested_base_address:         " INTPTR_FORMAT, p2i(_requested_base_address));
 305   st->print_cr("- mapped_base_address:            " INTPTR_FORMAT, p2i(_mapped_base_address));
 306   st->print_cr("- heap_root_segments.roots_count: %d" , _heap_root_segments.roots_count());
 307   st->print_cr("- heap_root_segments.base_offset: " SIZE_FORMAT_X, _heap_root_segments.base_offset());
 308   st->print_cr("- heap_root_segments.count:       " SIZE_FORMAT, _heap_root_segments.count());
 309   st->print_cr("- heap_root_segments.max_size_elems: %d", _heap_root_segments.max_size_in_elems());
 310   st->print_cr("- heap_root_segments.max_size_bytes: %d", _heap_root_segments.max_size_in_bytes());
 311   st->print_cr("- _heap_oopmap_start_pos:         " SIZE_FORMAT, _heap_oopmap_start_pos);
 312   st->print_cr("- _heap_ptrmap_start_pos:         " SIZE_FORMAT, _heap_ptrmap_start_pos);
 313   st->print_cr("- _rw_ptrmap_start_pos:           " SIZE_FORMAT, _rw_ptrmap_start_pos);
 314   st->print_cr("- _ro_ptrmap_start_pos:           " SIZE_FORMAT, _ro_ptrmap_start_pos);
 315   st->print_cr("- allow_archiving_with_java_agent:%d", _allow_archiving_with_java_agent);
 316   st->print_cr("- use_optimized_module_handling:  %d", _use_optimized_module_handling);
 317   st->print_cr("- has_full_module_graph           %d", _has_full_module_graph);
 318   st->print_cr("- has_aot_linked_classes          %d", _has_aot_linked_classes);
 319   st->print_cr("- has_archived_invokedynamic      %d", _has_archived_invokedynamic);



 320 }
 321 
 322 void SharedClassPathEntry::init_as_non_existent(const char* path, TRAPS) {
 323   _type = non_existent_entry;
 324   set_name(path, CHECK);
 325 }
 326 
 327 void SharedClassPathEntry::init(bool is_modules_image,
 328                                 bool is_module_path,
 329                                 ClassPathEntry* cpe, TRAPS) {
 330   assert(CDSConfig::is_dumping_archive(), "sanity");
 331   _timestamp = 0;
 332   _filesize  = 0;
 333   _from_class_path_attr = false;
 334 
 335   struct stat st;
 336   if (os::stat(cpe->name(), &st) == 0) {
 337     if ((st.st_mode & S_IFMT) == S_IFDIR) {
 338       _type = dir_entry;
 339     } else {

 564 
 565 void FileMapInfo::record_non_existent_class_path_entry(const char* path) {
 566   assert(CDSConfig::is_dumping_archive(), "sanity");
 567   log_info(class, path)("non-existent Class-Path entry %s", path);
 568   if (_non_existent_class_paths == nullptr) {
 569     _non_existent_class_paths = new (mtClass) GrowableArray<const char*>(10, mtClass);
 570   }
 571   _non_existent_class_paths->append(os::strdup(path));
 572 }
 573 
 574 int FileMapInfo::num_non_existent_class_paths() {
 575   assert(CDSConfig::is_dumping_archive(), "sanity");
 576   if (_non_existent_class_paths != nullptr) {
 577     return _non_existent_class_paths->length();
 578   } else {
 579     return 0;
 580   }
 581 }
 582 
 583 int FileMapInfo::get_module_shared_path_index(Symbol* location) {



 584   if (location->starts_with("jrt:", 4) && get_number_of_shared_paths() > 0) {
 585     assert(shared_path(0)->is_modules_image(), "first shared_path must be the modules image");
 586     return 0;
 587   }
 588 
 589   if (ClassLoaderExt::app_module_paths_start_index() >= get_number_of_shared_paths()) {
 590     // The archive(s) were created without --module-path option
 591     return -1;
 592   }
 593 
 594   if (!location->starts_with("file:", 5)) {
 595     return -1;
 596   }
 597 
 598   // skip_uri_protocol was also called during dump time -- see ClassLoaderExt::process_module_table()
 599   ResourceMark rm;
 600   const char* file = ClassLoader::uri_to_path(location->as_C_string());
 601   for (int i = ClassLoaderExt::app_module_paths_start_index(); i < get_number_of_shared_paths(); i++) {
 602     SharedClassPathEntry* ent = shared_path(i);
 603     if (!ent->is_non_existent()) {

1519 bool FileMapRegion::check_region_crc(char* base) const {
1520   // This function should be called after the region has been properly
1521   // loaded into memory via FileMapInfo::map_region() or FileMapInfo::read_region().
1522   // I.e., this->mapped_base() must be valid.
1523   size_t sz = used();
1524   if (sz == 0) {
1525     return true;
1526   }
1527 
1528   assert(base != nullptr, "must be initialized");
1529   int crc = ClassLoader::crc32(0, base, (jint)sz);
1530   if (crc != this->crc()) {
1531     log_warning(cds)("Checksum verification failed.");
1532     return false;
1533   }
1534   return true;
1535 }
1536 
1537 static const char* region_name(int region_index) {
1538   static const char* names[] = {
1539     "rw", "ro", "bm", "hp"
1540   };
1541   const int num_regions = sizeof(names)/sizeof(names[0]);
1542   assert(0 <= region_index && region_index < num_regions, "sanity");
1543 
1544   return names[region_index];
1545 }
1546 
1547 BitMapView FileMapInfo::bitmap_view(int region_index, bool is_oopmap) {
1548   FileMapRegion* r = region_at(region_index);
1549   char* bitmap_base = is_static() ? FileMapInfo::current_info()->map_bitmap_region() : FileMapInfo::dynamic_info()->map_bitmap_region();
1550   bitmap_base += is_oopmap ? r->oopmap_offset() : r->ptrmap_offset();
1551   size_t size_in_bits = is_oopmap ? r->oopmap_size_in_bits() : r->ptrmap_size_in_bits();
1552 
1553   log_debug(cds, reloc)("mapped %s relocation %smap @ " INTPTR_FORMAT " (" SIZE_FORMAT " bits)",
1554                         region_name(region_index), is_oopmap ? "oop" : "ptr",
1555                         p2i(bitmap_base), size_in_bits);
1556 
1557   return BitMapView((BitMap::bm_word_t*)(bitmap_base), size_in_bits);
1558 }
1559 

1605       mapping_offset = (size_t)((address)requested_base - CompressedOops::base());
1606       assert((mapping_offset >> CompressedOops::shift()) << CompressedOops::shift() == mapping_offset, "must be");
1607     } else {
1608       mapping_offset = 0; // not used with !UseCompressedOops
1609     }
1610 #endif // INCLUDE_CDS_JAVA_HEAP
1611   } else {
1612     char* requested_SharedBaseAddress = (char*)MetaspaceShared::requested_base_address();
1613     requested_base = ArchiveBuilder::current()->to_requested(base);
1614     assert(requested_base >= requested_SharedBaseAddress, "must be");
1615     mapping_offset = requested_base - requested_SharedBaseAddress;
1616   }
1617 
1618   r->set_file_offset(_file_offset);
1619   int crc = ClassLoader::crc32(0, base, (jint)size);
1620   if (size > 0) {
1621     log_info(cds)("Shared file region (%s) %d: " SIZE_FORMAT_W(8)
1622                    " bytes, addr " INTPTR_FORMAT " file offset 0x%08" PRIxPTR
1623                    " crc 0x%08x",
1624                    region_name(region), region, size, p2i(requested_base), _file_offset, crc);



1625   }
1626 
1627   r->init(region, mapping_offset, size, read_only, allow_exec, crc);
1628 
1629   if (base != nullptr) {
1630     write_bytes_aligned(base, size);
1631   }
1632 }
1633 
1634 static size_t write_bitmap(const CHeapBitMap* map, char* output, size_t offset) {
1635   size_t size_in_bytes = map->size_in_bytes();
1636   map->write_to((BitMap::bm_word_t*)(output + offset), size_in_bytes);
1637   return offset + size_in_bytes;
1638 }
1639 
1640 // The sorting code groups the objects with non-null oop/ptrs together.
1641 // Relevant bitmaps then have lots of leading and trailing zeros, which
1642 // we do not have to store.
1643 size_t FileMapInfo::remove_bitmap_zeros(CHeapBitMap* map) {
1644   BitMap::idx_t first_set = map->find_first_set_bit(0);
1645   BitMap::idx_t last_set  = map->find_last_set_bit(0);
1646   size_t old_size = map->size();
1647 
1648   // Slice and resize bitmap
1649   map->truncate(first_set, last_set + 1);
1650 
1651   assert(map->at(0), "First bit should be set");
1652   assert(map->at(map->size() - 1), "Last bit should be set");
1653   assert(map->size() <= old_size, "sanity");
1654 
1655   return first_set;
1656 }
1657 
1658 char* FileMapInfo::write_bitmap_region(CHeapBitMap* rw_ptrmap, CHeapBitMap* ro_ptrmap, ArchiveHeapInfo* heap_info,


1659                                        size_t &size_in_bytes) {
1660   size_t removed_rw_leading_zeros = remove_bitmap_zeros(rw_ptrmap);
1661   size_t removed_ro_leading_zeros = remove_bitmap_zeros(ro_ptrmap);
1662   header()->set_rw_ptrmap_start_pos(removed_rw_leading_zeros);
1663   header()->set_ro_ptrmap_start_pos(removed_ro_leading_zeros);
1664   size_in_bytes = rw_ptrmap->size_in_bytes() + ro_ptrmap->size_in_bytes();
1665 
1666   if (heap_info->is_used()) {
1667     // Remove leading and trailing zeros
1668     size_t removed_oop_leading_zeros = remove_bitmap_zeros(heap_info->oopmap());
1669     size_t removed_ptr_leading_zeros = remove_bitmap_zeros(heap_info->ptrmap());
1670     header()->set_heap_oopmap_start_pos(removed_oop_leading_zeros);
1671     header()->set_heap_ptrmap_start_pos(removed_ptr_leading_zeros);
1672 
1673     size_in_bytes += heap_info->oopmap()->size_in_bytes();
1674     size_in_bytes += heap_info->ptrmap()->size_in_bytes();
1675   }
1676 
1677   // The bitmap region contains up to 4 parts:
1678   // rw_ptrmap:           metaspace pointers inside the read-write region
1679   // ro_ptrmap:           metaspace pointers inside the read-only region
1680   // heap_info->oopmap(): Java oop pointers in the heap region
1681   // heap_info->ptrmap(): metaspace pointers in the heap region
1682   char* buffer = NEW_C_HEAP_ARRAY(char, size_in_bytes, mtClassShared);
1683   size_t written = 0;
1684 
1685   region_at(MetaspaceShared::rw)->init_ptrmap(0, rw_ptrmap->size());
1686   written = write_bitmap(rw_ptrmap, buffer, written);
1687 
1688   region_at(MetaspaceShared::ro)->init_ptrmap(written, ro_ptrmap->size());
1689   written = write_bitmap(ro_ptrmap, buffer, written);
1690 



1691   if (heap_info->is_used()) {
1692     FileMapRegion* r = region_at(MetaspaceShared::hp);
1693 
1694     r->init_oopmap(written, heap_info->oopmap()->size());
1695     written = write_bitmap(heap_info->oopmap(), buffer, written);
1696 
1697     r->init_ptrmap(written, heap_info->ptrmap()->size());
1698     written = write_bitmap(heap_info->ptrmap(), buffer, written);
1699   }
1700 
1701   write_region(MetaspaceShared::bm, (char*)buffer, size_in_bytes, /*read_only=*/true, /*allow_exec=*/false);
1702   return buffer;
1703 }
1704 
1705 size_t FileMapInfo::write_heap_region(ArchiveHeapInfo* heap_info) {
1706   char* buffer_start = heap_info->buffer_start();
1707   size_t buffer_size = heap_info->buffer_byte_size();
1708   write_region(MetaspaceShared::hp, buffer_start, buffer_size, false, false);
1709   header()->set_heap_root_segments(heap_info->heap_root_segments());
1710   return buffer_size;

1799   assert(WINDOWS_ONLY(false) NOT_WINDOWS(true), "Don't call on Windows");
1800   // Replace old mapping with new one that is writable.
1801   char *base = os::map_memory(_fd, _full_path, r->file_offset(),
1802                               addr, size, false /* !read_only */,
1803                               r->allow_exec());
1804   close();
1805   // These have to be errors because the shared region is now unmapped.
1806   if (base == nullptr) {
1807     log_error(cds)("Unable to remap shared readonly space (errno=%d).", errno);
1808     vm_exit(1);
1809   }
1810   if (base != addr) {
1811     log_error(cds)("Unable to remap shared readonly space (errno=%d).", errno);
1812     vm_exit(1);
1813   }
1814   r->set_read_only(false);
1815   return true;
1816 }
1817 
1818 // Memory map a region in the address space.
1819 static const char* shared_region_name[] = { "ReadWrite", "ReadOnly", "Bitmap", "Heap" };
1820 
1821 MapArchiveResult FileMapInfo::map_regions(int regions[], int num_regions, char* mapped_base_address, ReservedSpace rs) {
1822   DEBUG_ONLY(FileMapRegion* last_region = nullptr);
1823   intx addr_delta = mapped_base_address - header()->requested_base_address();
1824 
1825   // Make sure we don't attempt to use header()->mapped_base_address() unless
1826   // it's been successfully mapped.
1827   DEBUG_ONLY(header()->set_mapped_base_address((char*)(uintptr_t)0xdeadbeef);)
1828 
1829   for (int i = 0; i < num_regions; i++) {
1830     int idx = regions[i];
1831     MapArchiveResult result = map_region(idx, addr_delta, mapped_base_address, rs);
1832     if (result != MAP_ARCHIVE_SUCCESS) {
1833       return result;
1834     }
1835     FileMapRegion* r = region_at(idx);
1836     DEBUG_ONLY(if (last_region != nullptr) {
1837         // Ensure that the OS won't be able to allocate new memory spaces between any mapped
1838         // regions, or else it would mess up the simple comparison in MetaspaceObj::is_shared().
1839         assert(r->mapped_base() == last_region->mapped_end(), "must have no gaps");

1902     r->set_read_only(false); // Need to patch the pointers
1903   }
1904 
1905   if (MetaspaceShared::use_windows_memory_mapping() && rs.is_reserved()) {
1906     // This is the second time we try to map the archive(s). We have already created a ReservedSpace
1907     // that covers all the FileMapRegions to ensure all regions can be mapped. However, Windows
1908     // can't mmap into a ReservedSpace, so we just ::read() the data. We're going to patch all the
1909     // regions anyway, so there's no benefit for mmap anyway.
1910     if (!read_region(i, requested_addr, size, /* do_commit = */ true)) {
1911       log_info(cds)("Failed to read %s shared space into reserved space at " INTPTR_FORMAT,
1912                     shared_region_name[i], p2i(requested_addr));
1913       return MAP_ARCHIVE_OTHER_FAILURE; // oom or I/O error.
1914     } else {
1915       assert(r->mapped_base() != nullptr, "must be initialized");
1916       return MAP_ARCHIVE_SUCCESS;
1917     }
1918   } else {
1919     // Note that this may either be a "fresh" mapping into unreserved address
1920     // space (Windows, first mapping attempt), or a mapping into pre-reserved
1921     // space (Posix). See also comment in MetaspaceShared::map_archives().

1922     char* base = map_memory(_fd, _full_path, r->file_offset(),
1923                             requested_addr, size, r->read_only(),
1924                             r->allow_exec(), mtClassShared);
1925     if (base != requested_addr) {
1926       log_info(cds)("Unable to map %s shared space at " INTPTR_FORMAT,
1927                     shared_region_name[i], p2i(requested_addr));
1928       _memory_mapping_failed = true;
1929       return MAP_ARCHIVE_MMAP_FAILURE;
1930     }
1931 
1932     if (VerifySharedSpaces && !r->check_region_crc(requested_addr)) {
1933       return MAP_ARCHIVE_OTHER_FAILURE;
1934     }
1935 
1936     r->set_mapped_from_file(true);
1937     r->set_mapped_base(requested_addr);
1938 
1939     return MAP_ARCHIVE_SUCCESS;
1940   }
1941 }
1942 
1943 // The return value is the location of the archive relocation bitmap.

1955     return nullptr;
1956   }
1957 
1958   if (VerifySharedSpaces && !r->check_region_crc(bitmap_base)) {
1959     log_error(cds)("relocation bitmap CRC error");
1960     if (!os::unmap_memory(bitmap_base, r->used_aligned())) {
1961       fatal("os::unmap_memory of relocation bitmap failed");
1962     }
1963     return nullptr;
1964   }
1965 
1966   r->set_mapped_from_file(true);
1967   r->set_mapped_base(bitmap_base);
1968   log_info(cds)("Mapped %s region #%d at base " INTPTR_FORMAT " top " INTPTR_FORMAT " (%s)",
1969                 is_static() ? "static " : "dynamic",
1970                 MetaspaceShared::bm, p2i(r->mapped_base()), p2i(r->mapped_end()),
1971                 shared_region_name[MetaspaceShared::bm]);
1972   return bitmap_base;
1973 }
1974 




















































































1975 // This is called when we cannot map the archive at the requested[ base address (usually 0x800000000).
1976 // We relocate all pointers in the 2 core regions (ro, rw).
1977 bool FileMapInfo::relocate_pointers_in_core_regions(intx addr_delta) {
1978   log_debug(cds, reloc)("runtime archive relocation start");
1979   char* bitmap_base = map_bitmap_region();
1980 
1981   if (bitmap_base == nullptr) {
1982     return false; // OOM, or CRC check failure
1983   } else {
1984     BitMapView rw_ptrmap = ptrmap_view(MetaspaceShared::rw);
1985     BitMapView ro_ptrmap = ptrmap_view(MetaspaceShared::ro);
1986 
1987     FileMapRegion* rw_region = first_core_region();
1988     FileMapRegion* ro_region = last_core_region();
1989 
1990     // Patch all pointers inside the RW region
1991     address rw_patch_base = (address)rw_region->mapped_base();
1992     address rw_patch_end  = (address)rw_region->mapped_end();
1993 
1994     // Patch all pointers inside the RO region

2071 }
2072 
2073 void FileMapInfo::map_or_load_heap_region() {
2074   bool success = false;
2075 
2076   if (can_use_heap_region()) {
2077     if (ArchiveHeapLoader::can_map()) {
2078       success = map_heap_region();
2079     } else if (ArchiveHeapLoader::can_load()) {
2080       success = ArchiveHeapLoader::load_heap_region(this);
2081     } else {
2082       if (!UseCompressedOops && !ArchiveHeapLoader::can_map()) {
2083         log_info(cds)("Cannot use CDS heap data. Selected GC not compatible -XX:-UseCompressedOops");
2084       } else {
2085         log_info(cds)("Cannot use CDS heap data. UseEpsilonGC, UseG1GC, UseSerialGC, UseParallelGC, or UseShenandoahGC are required.");
2086       }
2087     }
2088   }
2089 
2090   if (!success) {
2091     if (CDSConfig::is_using_aot_linked_classes()) {
2092       // It's too late to recover -- we have already committed to use the archived metaspace objects, but
2093       // the archived heap objects cannot be loaded, so we don't have the archived FMG to guarantee that
2094       // all AOT-linked classes are visible.
2095       //
2096       // We get here because the heap is too small. The app will fail anyway. So let's quit.
2097       MetaspaceShared::unrecoverable_loading_error("CDS archive has aot-linked classes but the archived "
2098                                                    "heap objects cannot be loaded. Try increasing your heap size.");
2099     }
2100     CDSConfig::stop_using_full_module_graph("archive heap loading failed");
2101   }
2102 }
2103 
2104 bool FileMapInfo::can_use_heap_region() {
2105   if (!has_heap_region()) {
2106     return false;
2107   }
2108   if (JvmtiExport::should_post_class_file_load_hook() && JvmtiExport::has_early_class_hook_env()) {
2109     ShouldNotReachHere(); // CDS should have been disabled.
2110     // The archived objects are mapped at JVM start-up, but we don't know if
2111     // j.l.String or j.l.Class might be replaced by the ClassFileLoadHook,

2448       return false;
2449     }
2450   }
2451 
2452   return true;
2453 }
2454 
2455 bool FileMapInfo::validate_aot_class_linking() {
2456   // These checks need to be done after FileMapInfo::initialize(), which gets called before Universe::heap()
2457   // is available.
2458   if (header()->has_aot_linked_classes()) {
2459     CDSConfig::set_has_aot_linked_classes(true);
2460     if (JvmtiExport::should_post_class_file_load_hook()) {
2461       log_error(cds)("CDS archive has aot-linked classes. It cannot be used when JVMTI ClassFileLoadHook is in use.");
2462       return false;
2463     }
2464     if (JvmtiExport::has_early_vmstart_env()) {
2465       log_error(cds)("CDS archive has aot-linked classes. It cannot be used when JVMTI early vm start is in use.");
2466       return false;
2467     }
2468     if (!CDSConfig::is_using_full_module_graph()) {
2469       log_error(cds)("CDS archive has aot-linked classes. It cannot be used when archived full module graph is not used.");
2470       return false;
2471     }
2472 
2473     const char* prop = Arguments::get_property("java.security.manager");
2474     if (prop != nullptr && strcmp(prop, "disallow") != 0) {
2475       log_error(cds)("CDS archive has aot-linked classes. It cannot be used with -Djava.security.manager=%s.", prop);
2476       return false;
2477     }





2478   }
2479 
2480   return true;
2481 }
2482 
2483 // The 2 core spaces are RW->RO
2484 FileMapRegion* FileMapInfo::first_core_region() const {
2485   return region_at(MetaspaceShared::rw);
2486 }
2487 
2488 FileMapRegion* FileMapInfo::last_core_region() const {
2489   return region_at(MetaspaceShared::ro);
2490 }
2491 
2492 void FileMapInfo::print(outputStream* st) const {
2493   header()->print(st);
2494   if (!is_static()) {
2495     dynamic_header()->print(st);
2496   }
2497 }

2576     log_warning(cds)("This archive was created with AllowArchivingWithJavaAgent. It should be used "
2577             "for testing purposes only and should not be used in a production environment");
2578   }
2579 
2580   log_info(cds)("Archive was created with UseCompressedOops = %d, UseCompressedClassPointers = %d, UseCompactObjectHeaders = %d",
2581                           compressed_oops(), compressed_class_pointers(), compact_headers());
2582   if (compressed_oops() != UseCompressedOops || compressed_class_pointers() != UseCompressedClassPointers) {
2583     log_warning(cds)("Unable to use shared archive.\nThe saved state of UseCompressedOops and UseCompressedClassPointers is "
2584                                "different from runtime, CDS will be disabled.");
2585     return false;
2586   }
2587 
2588   if (compact_headers() != UseCompactObjectHeaders) {
2589     log_warning(cds)("Unable to use shared archive.\nThe shared archive file's UseCompactObjectHeaders setting (%s)"
2590                      " does not equal the current UseCompactObjectHeaders setting (%s).",
2591                      _compact_headers          ? "enabled" : "disabled",
2592                      UseCompactObjectHeaders   ? "enabled" : "disabled");
2593     return false;
2594   }
2595 
2596   if (!_use_optimized_module_handling) {
2597     CDSConfig::stop_using_optimized_module_handling();
2598     log_info(cds)("optimized module handling: disabled because archive was created without optimized module handling");
2599   }
2600 
2601   if (is_static()) {
2602     // Only the static archive can contain the full module graph.
2603     if (!_has_full_module_graph) {
2604       CDSConfig::stop_using_full_module_graph("archive was created without full module graph");
2605     }
2606 
2607     if (_has_archived_invokedynamic) {
2608       CDSConfig::set_has_archived_invokedynamic();
2609     }






2610   }
2611 
2612   return true;
2613 }
2614 
2615 bool FileMapInfo::validate_header() {
2616   if (!header()->validate()) {
2617     return false;
2618   }
2619   if (_is_static) {
2620     return true;
2621   } else {
2622     return DynamicArchive::validate(this);
2623   }
2624 }
2625 
2626 #if INCLUDE_JVMTI
2627 ClassPathEntry** FileMapInfo::_classpath_entries_for_jvmti = nullptr;
2628 
2629 ClassPathEntry* FileMapInfo::get_classpath_entry_for_jvmti(int i, TRAPS) {

 209   if (CDSConfig::is_dumping_heap()) {
 210     _narrow_oop_mode = CompressedOops::mode();
 211     _narrow_oop_base = CompressedOops::base();
 212     _narrow_oop_shift = CompressedOops::shift();
 213   }
 214   _compressed_oops = UseCompressedOops;
 215   _compressed_class_ptrs = UseCompressedClassPointers;
 216   if (UseCompressedClassPointers) {
 217 #ifdef _LP64
 218     _narrow_klass_pointer_bits = CompressedKlassPointers::narrow_klass_pointer_bits();
 219     _narrow_klass_shift = ArchiveBuilder::precomputed_narrow_klass_shift();
 220 #endif
 221   } else {
 222     _narrow_klass_pointer_bits = _narrow_klass_shift = -1;
 223   }
 224   _max_heap_size = MaxHeapSize;
 225   _use_optimized_module_handling = CDSConfig::is_using_optimized_module_handling();
 226   _has_aot_linked_classes = CDSConfig::is_dumping_aot_linked_classes();
 227   _has_full_module_graph = CDSConfig::is_dumping_full_module_graph();
 228   _has_archived_invokedynamic = CDSConfig::is_dumping_invokedynamic();
 229   _has_archived_packages = CDSConfig::is_dumping_packages();
 230   _has_archived_protection_domains = CDSConfig::is_dumping_protection_domains();
 231   _gc_kind = (int)Universe::heap()->kind();
 232   jio_snprintf(_gc_name, sizeof(_gc_name), Universe::heap()->name());
 233 
 234   // The following fields are for sanity checks for whether this archive
 235   // will function correctly with this JVM and the bootclasspath it's
 236   // invoked with.
 237 
 238   // JVM version string ... changes on each build.
 239   get_header_version(_jvm_ident);
 240 
 241   _app_class_paths_start_index = ClassLoaderExt::app_class_paths_start_index();
 242   _app_module_paths_start_index = ClassLoaderExt::app_module_paths_start_index();
 243   _max_used_path_index = ClassLoaderExt::max_used_path_index();
 244   _num_module_paths = ClassLoader::num_module_path_entries();
 245 
 246   _verify_local = BytecodeVerificationLocal;
 247   _verify_remote = BytecodeVerificationRemote;
 248   _has_platform_or_app_classes = ClassLoaderExt::has_platform_or_app_classes();
 249   _has_non_jar_in_classpath = ClassLoaderExt::has_non_jar_in_classpath();
 250   _requested_base_address = (char*)SharedBaseAddress;
 251   _mapped_base_address = (char*)SharedBaseAddress;
 252   _allow_archiving_with_java_agent = AllowArchivingWithJavaAgent;

 304   st->print_cr("- verify_local:                   %d", _verify_local);
 305   st->print_cr("- verify_remote:                  %d", _verify_remote);
 306   st->print_cr("- has_platform_or_app_classes:    %d", _has_platform_or_app_classes);
 307   st->print_cr("- has_non_jar_in_classpath:       %d", _has_non_jar_in_classpath);
 308   st->print_cr("- requested_base_address:         " INTPTR_FORMAT, p2i(_requested_base_address));
 309   st->print_cr("- mapped_base_address:            " INTPTR_FORMAT, p2i(_mapped_base_address));
 310   st->print_cr("- heap_root_segments.roots_count: %d" , _heap_root_segments.roots_count());
 311   st->print_cr("- heap_root_segments.base_offset: " SIZE_FORMAT_X, _heap_root_segments.base_offset());
 312   st->print_cr("- heap_root_segments.count:       " SIZE_FORMAT, _heap_root_segments.count());
 313   st->print_cr("- heap_root_segments.max_size_elems: %d", _heap_root_segments.max_size_in_elems());
 314   st->print_cr("- heap_root_segments.max_size_bytes: %d", _heap_root_segments.max_size_in_bytes());
 315   st->print_cr("- _heap_oopmap_start_pos:         " SIZE_FORMAT, _heap_oopmap_start_pos);
 316   st->print_cr("- _heap_ptrmap_start_pos:         " SIZE_FORMAT, _heap_ptrmap_start_pos);
 317   st->print_cr("- _rw_ptrmap_start_pos:           " SIZE_FORMAT, _rw_ptrmap_start_pos);
 318   st->print_cr("- _ro_ptrmap_start_pos:           " SIZE_FORMAT, _ro_ptrmap_start_pos);
 319   st->print_cr("- allow_archiving_with_java_agent:%d", _allow_archiving_with_java_agent);
 320   st->print_cr("- use_optimized_module_handling:  %d", _use_optimized_module_handling);
 321   st->print_cr("- has_full_module_graph           %d", _has_full_module_graph);
 322   st->print_cr("- has_aot_linked_classes          %d", _has_aot_linked_classes);
 323   st->print_cr("- has_archived_invokedynamic      %d", _has_archived_invokedynamic);
 324   st->print_cr("- has_archived_packages           %d", _has_archived_packages);
 325   st->print_cr("- has_archived_protection_domains %d", _has_archived_protection_domains);
 326   st->print_cr("- ptrmap_size_in_bits:            " SIZE_FORMAT, _ptrmap_size_in_bits);
 327 }
 328 
 329 void SharedClassPathEntry::init_as_non_existent(const char* path, TRAPS) {
 330   _type = non_existent_entry;
 331   set_name(path, CHECK);
 332 }
 333 
 334 void SharedClassPathEntry::init(bool is_modules_image,
 335                                 bool is_module_path,
 336                                 ClassPathEntry* cpe, TRAPS) {
 337   assert(CDSConfig::is_dumping_archive(), "sanity");
 338   _timestamp = 0;
 339   _filesize  = 0;
 340   _from_class_path_attr = false;
 341 
 342   struct stat st;
 343   if (os::stat(cpe->name(), &st) == 0) {
 344     if ((st.st_mode & S_IFMT) == S_IFDIR) {
 345       _type = dir_entry;
 346     } else {

 571 
 572 void FileMapInfo::record_non_existent_class_path_entry(const char* path) {
 573   assert(CDSConfig::is_dumping_archive(), "sanity");
 574   log_info(class, path)("non-existent Class-Path entry %s", path);
 575   if (_non_existent_class_paths == nullptr) {
 576     _non_existent_class_paths = new (mtClass) GrowableArray<const char*>(10, mtClass);
 577   }
 578   _non_existent_class_paths->append(os::strdup(path));
 579 }
 580 
 581 int FileMapInfo::num_non_existent_class_paths() {
 582   assert(CDSConfig::is_dumping_archive(), "sanity");
 583   if (_non_existent_class_paths != nullptr) {
 584     return _non_existent_class_paths->length();
 585   } else {
 586     return 0;
 587   }
 588 }
 589 
 590 int FileMapInfo::get_module_shared_path_index(Symbol* location) {
 591   if (location == nullptr) {
 592     return 0; // Used by java/lang/reflect/Proxy$ProxyBuilder
 593   }
 594   if (location->starts_with("jrt:", 4) && get_number_of_shared_paths() > 0) {
 595     assert(shared_path(0)->is_modules_image(), "first shared_path must be the modules image");
 596     return 0;
 597   }
 598 
 599   if (ClassLoaderExt::app_module_paths_start_index() >= get_number_of_shared_paths()) {
 600     // The archive(s) were created without --module-path option
 601     return -1;
 602   }
 603 
 604   if (!location->starts_with("file:", 5)) {
 605     return -1;
 606   }
 607 
 608   // skip_uri_protocol was also called during dump time -- see ClassLoaderExt::process_module_table()
 609   ResourceMark rm;
 610   const char* file = ClassLoader::uri_to_path(location->as_C_string());
 611   for (int i = ClassLoaderExt::app_module_paths_start_index(); i < get_number_of_shared_paths(); i++) {
 612     SharedClassPathEntry* ent = shared_path(i);
 613     if (!ent->is_non_existent()) {

1529 bool FileMapRegion::check_region_crc(char* base) const {
1530   // This function should be called after the region has been properly
1531   // loaded into memory via FileMapInfo::map_region() or FileMapInfo::read_region().
1532   // I.e., this->mapped_base() must be valid.
1533   size_t sz = used();
1534   if (sz == 0) {
1535     return true;
1536   }
1537 
1538   assert(base != nullptr, "must be initialized");
1539   int crc = ClassLoader::crc32(0, base, (jint)sz);
1540   if (crc != this->crc()) {
1541     log_warning(cds)("Checksum verification failed.");
1542     return false;
1543   }
1544   return true;
1545 }
1546 
1547 static const char* region_name(int region_index) {
1548   static const char* names[] = {
1549     "rw", "ro", "bm", "hp", "cc",
1550   };
1551   const int num_regions = sizeof(names)/sizeof(names[0]);
1552   assert(0 <= region_index && region_index < num_regions, "sanity");
1553 
1554   return names[region_index];
1555 }
1556 
1557 BitMapView FileMapInfo::bitmap_view(int region_index, bool is_oopmap) {
1558   FileMapRegion* r = region_at(region_index);
1559   char* bitmap_base = is_static() ? FileMapInfo::current_info()->map_bitmap_region() : FileMapInfo::dynamic_info()->map_bitmap_region();
1560   bitmap_base += is_oopmap ? r->oopmap_offset() : r->ptrmap_offset();
1561   size_t size_in_bits = is_oopmap ? r->oopmap_size_in_bits() : r->ptrmap_size_in_bits();
1562 
1563   log_debug(cds, reloc)("mapped %s relocation %smap @ " INTPTR_FORMAT " (" SIZE_FORMAT " bits)",
1564                         region_name(region_index), is_oopmap ? "oop" : "ptr",
1565                         p2i(bitmap_base), size_in_bits);
1566 
1567   return BitMapView((BitMap::bm_word_t*)(bitmap_base), size_in_bits);
1568 }
1569 

1615       mapping_offset = (size_t)((address)requested_base - CompressedOops::base());
1616       assert((mapping_offset >> CompressedOops::shift()) << CompressedOops::shift() == mapping_offset, "must be");
1617     } else {
1618       mapping_offset = 0; // not used with !UseCompressedOops
1619     }
1620 #endif // INCLUDE_CDS_JAVA_HEAP
1621   } else {
1622     char* requested_SharedBaseAddress = (char*)MetaspaceShared::requested_base_address();
1623     requested_base = ArchiveBuilder::current()->to_requested(base);
1624     assert(requested_base >= requested_SharedBaseAddress, "must be");
1625     mapping_offset = requested_base - requested_SharedBaseAddress;
1626   }
1627 
1628   r->set_file_offset(_file_offset);
1629   int crc = ClassLoader::crc32(0, base, (jint)size);
1630   if (size > 0) {
1631     log_info(cds)("Shared file region (%s) %d: " SIZE_FORMAT_W(8)
1632                    " bytes, addr " INTPTR_FORMAT " file offset 0x%08" PRIxPTR
1633                    " crc 0x%08x",
1634                    region_name(region), region, size, p2i(requested_base), _file_offset, crc);
1635   } else {
1636     log_info(cds)("Shared file region (%s) %d: " SIZE_FORMAT_W(8)
1637                   " bytes", region_name(region), region, size);
1638   }
1639 
1640   r->init(region, mapping_offset, size, read_only, allow_exec, crc);
1641 
1642   if (base != nullptr) {
1643     write_bytes_aligned(base, size);
1644   }
1645 }
1646 
1647 static size_t write_bitmap(const CHeapBitMap* map, char* output, size_t offset) {
1648   size_t size_in_bytes = map->size_in_bytes();
1649   map->write_to((BitMap::bm_word_t*)(output + offset), size_in_bytes);
1650   return offset + size_in_bytes;
1651 }
1652 
1653 // The sorting code groups the objects with non-null oop/ptrs together.
1654 // Relevant bitmaps then have lots of leading and trailing zeros, which
1655 // we do not have to store.
1656 size_t FileMapInfo::remove_bitmap_zeros(CHeapBitMap* map) {
1657   BitMap::idx_t first_set = map->find_first_set_bit(0);
1658   BitMap::idx_t last_set  = map->find_last_set_bit(0);
1659   size_t old_size = map->size();
1660 
1661   // Slice and resize bitmap
1662   map->truncate(first_set, last_set + 1);
1663 
1664   assert(map->at(0), "First bit should be set");
1665   assert(map->at(map->size() - 1), "Last bit should be set");
1666   assert(map->size() <= old_size, "sanity");
1667 
1668   return first_set;
1669 }
1670 
1671 char* FileMapInfo::write_bitmap_region(CHeapBitMap* rw_ptrmap, CHeapBitMap* ro_ptrmap,
1672                                        CHeapBitMap* cc_ptrmap,
1673                                        ArchiveHeapInfo* heap_info,
1674                                        size_t &size_in_bytes) {
1675   size_t removed_rw_leading_zeros = remove_bitmap_zeros(rw_ptrmap);
1676   size_t removed_ro_leading_zeros = remove_bitmap_zeros(ro_ptrmap);
1677   header()->set_rw_ptrmap_start_pos(removed_rw_leading_zeros);
1678   header()->set_ro_ptrmap_start_pos(removed_ro_leading_zeros);
1679   size_in_bytes = rw_ptrmap->size_in_bytes() + ro_ptrmap->size_in_bytes() + cc_ptrmap->size_in_bytes();
1680 
1681   if (heap_info->is_used()) {
1682     // Remove leading and trailing zeros
1683     size_t removed_oop_leading_zeros = remove_bitmap_zeros(heap_info->oopmap());
1684     size_t removed_ptr_leading_zeros = remove_bitmap_zeros(heap_info->ptrmap());
1685     header()->set_heap_oopmap_start_pos(removed_oop_leading_zeros);
1686     header()->set_heap_ptrmap_start_pos(removed_ptr_leading_zeros);
1687 
1688     size_in_bytes += heap_info->oopmap()->size_in_bytes();
1689     size_in_bytes += heap_info->ptrmap()->size_in_bytes();
1690   }
1691 
1692   // The bitmap region contains up to 4 parts:
1693   // rw_ptrmap:           metaspace pointers inside the read-write region
1694   // ro_ptrmap:           metaspace pointers inside the read-only region
1695   // heap_info->oopmap(): Java oop pointers in the heap region
1696   // heap_info->ptrmap(): metaspace pointers in the heap region
1697   char* buffer = NEW_C_HEAP_ARRAY(char, size_in_bytes, mtClassShared);
1698   size_t written = 0;
1699 
1700   region_at(MetaspaceShared::rw)->init_ptrmap(0, rw_ptrmap->size());
1701   written = write_bitmap(rw_ptrmap, buffer, written);
1702 
1703   region_at(MetaspaceShared::ro)->init_ptrmap(written, ro_ptrmap->size());
1704   written = write_bitmap(ro_ptrmap, buffer, written);
1705 
1706   region_at(MetaspaceShared::cc)->init_ptrmap(written, cc_ptrmap->size());
1707   written = write_bitmap(cc_ptrmap, buffer, written);
1708 
1709   if (heap_info->is_used()) {
1710     FileMapRegion* r = region_at(MetaspaceShared::hp);
1711 
1712     r->init_oopmap(written, heap_info->oopmap()->size());
1713     written = write_bitmap(heap_info->oopmap(), buffer, written);
1714 
1715     r->init_ptrmap(written, heap_info->ptrmap()->size());
1716     written = write_bitmap(heap_info->ptrmap(), buffer, written);
1717   }
1718 
1719   write_region(MetaspaceShared::bm, (char*)buffer, size_in_bytes, /*read_only=*/true, /*allow_exec=*/false);
1720   return buffer;
1721 }
1722 
1723 size_t FileMapInfo::write_heap_region(ArchiveHeapInfo* heap_info) {
1724   char* buffer_start = heap_info->buffer_start();
1725   size_t buffer_size = heap_info->buffer_byte_size();
1726   write_region(MetaspaceShared::hp, buffer_start, buffer_size, false, false);
1727   header()->set_heap_root_segments(heap_info->heap_root_segments());
1728   return buffer_size;

1817   assert(WINDOWS_ONLY(false) NOT_WINDOWS(true), "Don't call on Windows");
1818   // Replace old mapping with new one that is writable.
1819   char *base = os::map_memory(_fd, _full_path, r->file_offset(),
1820                               addr, size, false /* !read_only */,
1821                               r->allow_exec());
1822   close();
1823   // These have to be errors because the shared region is now unmapped.
1824   if (base == nullptr) {
1825     log_error(cds)("Unable to remap shared readonly space (errno=%d).", errno);
1826     vm_exit(1);
1827   }
1828   if (base != addr) {
1829     log_error(cds)("Unable to remap shared readonly space (errno=%d).", errno);
1830     vm_exit(1);
1831   }
1832   r->set_read_only(false);
1833   return true;
1834 }
1835 
1836 // Memory map a region in the address space.
1837 static const char* shared_region_name[] = { "ReadWrite", "ReadOnly", "Bitmap", "Heap", "Code" };
1838 
1839 MapArchiveResult FileMapInfo::map_regions(int regions[], int num_regions, char* mapped_base_address, ReservedSpace rs) {
1840   DEBUG_ONLY(FileMapRegion* last_region = nullptr);
1841   intx addr_delta = mapped_base_address - header()->requested_base_address();
1842 
1843   // Make sure we don't attempt to use header()->mapped_base_address() unless
1844   // it's been successfully mapped.
1845   DEBUG_ONLY(header()->set_mapped_base_address((char*)(uintptr_t)0xdeadbeef);)
1846 
1847   for (int i = 0; i < num_regions; i++) {
1848     int idx = regions[i];
1849     MapArchiveResult result = map_region(idx, addr_delta, mapped_base_address, rs);
1850     if (result != MAP_ARCHIVE_SUCCESS) {
1851       return result;
1852     }
1853     FileMapRegion* r = region_at(idx);
1854     DEBUG_ONLY(if (last_region != nullptr) {
1855         // Ensure that the OS won't be able to allocate new memory spaces between any mapped
1856         // regions, or else it would mess up the simple comparison in MetaspaceObj::is_shared().
1857         assert(r->mapped_base() == last_region->mapped_end(), "must have no gaps");

1920     r->set_read_only(false); // Need to patch the pointers
1921   }
1922 
1923   if (MetaspaceShared::use_windows_memory_mapping() && rs.is_reserved()) {
1924     // This is the second time we try to map the archive(s). We have already created a ReservedSpace
1925     // that covers all the FileMapRegions to ensure all regions can be mapped. However, Windows
1926     // can't mmap into a ReservedSpace, so we just ::read() the data. We're going to patch all the
1927     // regions anyway, so there's no benefit for mmap anyway.
1928     if (!read_region(i, requested_addr, size, /* do_commit = */ true)) {
1929       log_info(cds)("Failed to read %s shared space into reserved space at " INTPTR_FORMAT,
1930                     shared_region_name[i], p2i(requested_addr));
1931       return MAP_ARCHIVE_OTHER_FAILURE; // oom or I/O error.
1932     } else {
1933       assert(r->mapped_base() != nullptr, "must be initialized");
1934       return MAP_ARCHIVE_SUCCESS;
1935     }
1936   } else {
1937     // Note that this may either be a "fresh" mapping into unreserved address
1938     // space (Windows, first mapping attempt), or a mapping into pre-reserved
1939     // space (Posix). See also comment in MetaspaceShared::map_archives().
1940     bool read_only = r->read_only() && !CDSConfig::is_dumping_final_static_archive();
1941     char* base = map_memory(_fd, _full_path, r->file_offset(),
1942                             requested_addr, size, read_only,
1943                             r->allow_exec(), mtClassShared);
1944     if (base != requested_addr) {
1945       log_info(cds)("Unable to map %s shared space at " INTPTR_FORMAT,
1946                     shared_region_name[i], p2i(requested_addr));
1947       _memory_mapping_failed = true;
1948       return MAP_ARCHIVE_MMAP_FAILURE;
1949     }
1950 
1951     if (VerifySharedSpaces && !r->check_region_crc(requested_addr)) {
1952       return MAP_ARCHIVE_OTHER_FAILURE;
1953     }
1954 
1955     r->set_mapped_from_file(true);
1956     r->set_mapped_base(requested_addr);
1957 
1958     return MAP_ARCHIVE_SUCCESS;
1959   }
1960 }
1961 
1962 // The return value is the location of the archive relocation bitmap.

1974     return nullptr;
1975   }
1976 
1977   if (VerifySharedSpaces && !r->check_region_crc(bitmap_base)) {
1978     log_error(cds)("relocation bitmap CRC error");
1979     if (!os::unmap_memory(bitmap_base, r->used_aligned())) {
1980       fatal("os::unmap_memory of relocation bitmap failed");
1981     }
1982     return nullptr;
1983   }
1984 
1985   r->set_mapped_from_file(true);
1986   r->set_mapped_base(bitmap_base);
1987   log_info(cds)("Mapped %s region #%d at base " INTPTR_FORMAT " top " INTPTR_FORMAT " (%s)",
1988                 is_static() ? "static " : "dynamic",
1989                 MetaspaceShared::bm, p2i(r->mapped_base()), p2i(r->mapped_end()),
1990                 shared_region_name[MetaspaceShared::bm]);
1991   return bitmap_base;
1992 }
1993 
1994 bool FileMapInfo::map_cached_code_region(ReservedSpace rs) {
1995   FileMapRegion* r = region_at(MetaspaceShared::cc);
1996   assert(r->used() > 0 && r->used_aligned() == rs.size(), "must be");
1997 
1998   char* requested_base = rs.base();
1999   assert(requested_base != nullptr, "should be inside code cache");
2000 
2001   char* mapped_base;
2002   if (MetaspaceShared::use_windows_memory_mapping()) {
2003     if (!read_region(MetaspaceShared::cc, requested_base, r->used_aligned(), /* do_commit = */ true)) {
2004       log_info(cds)("Failed to read cc shared space into reserved space at " INTPTR_FORMAT,
2005                     p2i(requested_base));
2006       return false;
2007     }
2008     mapped_base = requested_base;
2009   } else {
2010     bool read_only = false, allow_exec = false;
2011     mapped_base = map_memory(_fd, _full_path, r->file_offset(),
2012                              requested_base, r->used_aligned(), read_only, allow_exec, mtClassShared);
2013   }
2014   if (mapped_base == nullptr) {
2015     log_info(cds)("failed to map cached code region");
2016     return false;
2017   } else {
2018     assert(mapped_base == requested_base, "must be");
2019     r->set_mapped_from_file(true);
2020     r->set_mapped_base(mapped_base);
2021     relocate_pointers_in_cached_code_region();
2022     log_info(cds)("Mapped static  region #%d at base " INTPTR_FORMAT " top " INTPTR_FORMAT " (%s)",
2023                   MetaspaceShared::cc, p2i(r->mapped_base()), p2i(r->mapped_end()),
2024                   shared_region_name[MetaspaceShared::cc]);
2025     return true;
2026   }
2027 }
2028 
2029 class CachedCodeRelocator: public BitMapClosure {
2030   address _code_requested_base;
2031   address* _patch_base;
2032   intx _code_delta;
2033   intx _metadata_delta;
2034 
2035 public:
2036   CachedCodeRelocator(address code_requested_base, address code_mapped_base,
2037                       intx metadata_delta) {
2038     _code_requested_base = code_requested_base;
2039     _patch_base = (address*)code_mapped_base;
2040     _code_delta = code_mapped_base - code_requested_base;
2041     _metadata_delta = metadata_delta;
2042   }
2043   
2044   bool do_bit(size_t offset) {
2045     address* p = _patch_base + offset;
2046     address requested_ptr = *p;
2047     if (requested_ptr < _code_requested_base) {
2048       *p = requested_ptr + _metadata_delta;
2049     } else {
2050       *p = requested_ptr + _code_delta;
2051     }
2052     return true; // keep iterating
2053   }
2054 };
2055 
2056 void FileMapInfo::relocate_pointers_in_cached_code_region() {
2057   FileMapRegion* r = region_at(MetaspaceShared::cc);
2058   char* bitmap_base = map_bitmap_region();
2059 
2060   BitMapView cc_ptrmap = ptrmap_view(MetaspaceShared::cc);
2061   if (cc_ptrmap.size() == 0) {
2062     return;
2063   }
2064 
2065   address core_regions_requested_base = (address)header()->requested_base_address();
2066   address core_regions_mapped_base = (address)header()->mapped_base_address();
2067   address cc_region_requested_base = core_regions_requested_base + r->mapping_offset();
2068   address cc_region_mapped_base = (address)r->mapped_base();
2069 
2070   size_t max_bits_for_core_regions = pointer_delta(mapped_end(), mapped_base(), // FIXME - renamed to core_regions_mapped_base(), etc
2071                                                    sizeof(address));
2072 
2073   CachedCodeRelocator patcher(cc_region_requested_base, cc_region_mapped_base,
2074                               core_regions_mapped_base - core_regions_requested_base);
2075   cc_ptrmap.iterate(&patcher);
2076 }
2077 
2078 // This is called when we cannot map the archive at the requested[ base address (usually 0x800000000).
2079 // We relocate all pointers in the 2 core regions (ro, rw).
2080 bool FileMapInfo::relocate_pointers_in_core_regions(intx addr_delta) {
2081   log_debug(cds, reloc)("runtime archive relocation start");
2082   char* bitmap_base = map_bitmap_region();
2083 
2084   if (bitmap_base == nullptr) {
2085     return false; // OOM, or CRC check failure
2086   } else {
2087     BitMapView rw_ptrmap = ptrmap_view(MetaspaceShared::rw);
2088     BitMapView ro_ptrmap = ptrmap_view(MetaspaceShared::ro);
2089 
2090     FileMapRegion* rw_region = first_core_region();
2091     FileMapRegion* ro_region = last_core_region();
2092 
2093     // Patch all pointers inside the RW region
2094     address rw_patch_base = (address)rw_region->mapped_base();
2095     address rw_patch_end  = (address)rw_region->mapped_end();
2096 
2097     // Patch all pointers inside the RO region

2174 }
2175 
2176 void FileMapInfo::map_or_load_heap_region() {
2177   bool success = false;
2178 
2179   if (can_use_heap_region()) {
2180     if (ArchiveHeapLoader::can_map()) {
2181       success = map_heap_region();
2182     } else if (ArchiveHeapLoader::can_load()) {
2183       success = ArchiveHeapLoader::load_heap_region(this);
2184     } else {
2185       if (!UseCompressedOops && !ArchiveHeapLoader::can_map()) {
2186         log_info(cds)("Cannot use CDS heap data. Selected GC not compatible -XX:-UseCompressedOops");
2187       } else {
2188         log_info(cds)("Cannot use CDS heap data. UseEpsilonGC, UseG1GC, UseSerialGC, UseParallelGC, or UseShenandoahGC are required.");
2189       }
2190     }
2191   }
2192 
2193   if (!success) {
2194     if (CDSConfig::is_using_aot_linked_classes() && !CDSConfig::is_dumping_final_static_archive()) {
2195       // It's too late to recover -- we have already committed to use the archived metaspace objects, but
2196       // the archived heap objects cannot be loaded, so we don't have the archived FMG to guarantee that
2197       // all AOT-linked classes are visible.
2198       //
2199       // We get here because the heap is too small. The app will fail anyway. So let's quit.
2200       MetaspaceShared::unrecoverable_loading_error("CDS archive has aot-linked classes but the archived "
2201                                                    "heap objects cannot be loaded. Try increasing your heap size.");
2202     }
2203     CDSConfig::stop_using_full_module_graph("archive heap loading failed");
2204   }
2205 }
2206 
2207 bool FileMapInfo::can_use_heap_region() {
2208   if (!has_heap_region()) {
2209     return false;
2210   }
2211   if (JvmtiExport::should_post_class_file_load_hook() && JvmtiExport::has_early_class_hook_env()) {
2212     ShouldNotReachHere(); // CDS should have been disabled.
2213     // The archived objects are mapped at JVM start-up, but we don't know if
2214     // j.l.String or j.l.Class might be replaced by the ClassFileLoadHook,

2551       return false;
2552     }
2553   }
2554 
2555   return true;
2556 }
2557 
2558 bool FileMapInfo::validate_aot_class_linking() {
2559   // These checks need to be done after FileMapInfo::initialize(), which gets called before Universe::heap()
2560   // is available.
2561   if (header()->has_aot_linked_classes()) {
2562     CDSConfig::set_has_aot_linked_classes(true);
2563     if (JvmtiExport::should_post_class_file_load_hook()) {
2564       log_error(cds)("CDS archive has aot-linked classes. It cannot be used when JVMTI ClassFileLoadHook is in use.");
2565       return false;
2566     }
2567     if (JvmtiExport::has_early_vmstart_env()) {
2568       log_error(cds)("CDS archive has aot-linked classes. It cannot be used when JVMTI early vm start is in use.");
2569       return false;
2570     }
2571     if (!CDSConfig::is_using_full_module_graph() && !CDSConfig::is_dumping_final_static_archive()) {
2572       log_error(cds)("CDS archive has aot-linked classes. It cannot be used when archived full module graph is not used.");
2573       return false;
2574     }
2575 
2576     const char* prop = Arguments::get_property("java.security.manager");
2577     if (prop != nullptr && strcmp(prop, "disallow") != 0) {
2578       log_error(cds)("CDS archive has aot-linked classes. It cannot be used with -Djava.security.manager=%s.", prop);
2579       return false;
2580     }
2581     if (header()->gc_kind() != (int)Universe::heap()->kind()) {
2582       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)",
2583                      header()->gc_name(), Universe::heap()->name());
2584       return false;
2585     }
2586   }
2587 
2588   return true;
2589 }
2590 
2591 // The 2 core spaces are RW->RO
2592 FileMapRegion* FileMapInfo::first_core_region() const {
2593   return region_at(MetaspaceShared::rw);
2594 }
2595 
2596 FileMapRegion* FileMapInfo::last_core_region() const {
2597   return region_at(MetaspaceShared::ro);
2598 }
2599 
2600 void FileMapInfo::print(outputStream* st) const {
2601   header()->print(st);
2602   if (!is_static()) {
2603     dynamic_header()->print(st);
2604   }
2605 }

2684     log_warning(cds)("This archive was created with AllowArchivingWithJavaAgent. It should be used "
2685             "for testing purposes only and should not be used in a production environment");
2686   }
2687 
2688   log_info(cds)("Archive was created with UseCompressedOops = %d, UseCompressedClassPointers = %d, UseCompactObjectHeaders = %d",
2689                           compressed_oops(), compressed_class_pointers(), compact_headers());
2690   if (compressed_oops() != UseCompressedOops || compressed_class_pointers() != UseCompressedClassPointers) {
2691     log_warning(cds)("Unable to use shared archive.\nThe saved state of UseCompressedOops and UseCompressedClassPointers is "
2692                                "different from runtime, CDS will be disabled.");
2693     return false;
2694   }
2695 
2696   if (compact_headers() != UseCompactObjectHeaders) {
2697     log_warning(cds)("Unable to use shared archive.\nThe shared archive file's UseCompactObjectHeaders setting (%s)"
2698                      " does not equal the current UseCompactObjectHeaders setting (%s).",
2699                      _compact_headers          ? "enabled" : "disabled",
2700                      UseCompactObjectHeaders   ? "enabled" : "disabled");
2701     return false;
2702   }
2703 
2704   if (!_use_optimized_module_handling && !CDSConfig::is_dumping_final_static_archive()) {
2705     CDSConfig::stop_using_optimized_module_handling();
2706     log_info(cds)("optimized module handling: disabled because archive was created without optimized module handling");
2707   }
2708 
2709   if (is_static()) {
2710     // Only the static archive can contain the full module graph.
2711     if (!_has_full_module_graph) {
2712       CDSConfig::stop_using_full_module_graph("archive was created without full module graph");
2713     }
2714 
2715     if (_has_archived_invokedynamic) {
2716       CDSConfig::set_has_archived_invokedynamic();
2717     }
2718     if (_has_archived_packages) {
2719       CDSConfig::set_is_loading_packages();
2720     }
2721     if (_has_archived_protection_domains) {
2722       CDSConfig::set_is_loading_protection_domains();
2723     }
2724   }
2725 
2726   return true;
2727 }
2728 
2729 bool FileMapInfo::validate_header() {
2730   if (!header()->validate()) {
2731     return false;
2732   }
2733   if (_is_static) {
2734     return true;
2735   } else {
2736     return DynamicArchive::validate(this);
2737   }
2738 }
2739 
2740 #if INCLUDE_JVMTI
2741 ClassPathEntry** FileMapInfo::_classpath_entries_for_jvmti = nullptr;
2742 
2743 ClassPathEntry* FileMapInfo::get_classpath_entry_for_jvmti(int i, TRAPS) {
< prev index next >