< prev index next >

src/hotspot/share/cds/filemap.cpp

Print this page

 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;

 281   st->print_cr("- jvm_ident:                      %s", _jvm_ident);
 282   st->print_cr("- shared_path_table_offset:       " SIZE_FORMAT_X, _shared_path_table_offset);
 283   st->print_cr("- app_class_paths_start_index:    %d", _app_class_paths_start_index);
 284   st->print_cr("- app_module_paths_start_index:   %d", _app_module_paths_start_index);
 285   st->print_cr("- num_module_paths:               %d", _num_module_paths);
 286   st->print_cr("- max_used_path_index:            %d", _max_used_path_index);
 287   st->print_cr("- verify_local:                   %d", _verify_local);
 288   st->print_cr("- verify_remote:                  %d", _verify_remote);
 289   st->print_cr("- has_platform_or_app_classes:    %d", _has_platform_or_app_classes);
 290   st->print_cr("- has_non_jar_in_classpath:       %d", _has_non_jar_in_classpath);
 291   st->print_cr("- requested_base_address:         " INTPTR_FORMAT, p2i(_requested_base_address));
 292   st->print_cr("- mapped_base_address:            " INTPTR_FORMAT, p2i(_mapped_base_address));
 293   st->print_cr("- heap_roots_offset:              " SIZE_FORMAT, _heap_roots_offset);
 294   st->print_cr("- _heap_oopmap_start_pos:         " SIZE_FORMAT, _heap_oopmap_start_pos);
 295   st->print_cr("- _heap_ptrmap_start_pos:         " SIZE_FORMAT, _heap_ptrmap_start_pos);
 296   st->print_cr("- _rw_ptrmap_start_pos:           " SIZE_FORMAT, _rw_ptrmap_start_pos);
 297   st->print_cr("- _ro_ptrmap_start_pos:           " SIZE_FORMAT, _ro_ptrmap_start_pos);
 298   st->print_cr("- allow_archiving_with_java_agent:%d", _allow_archiving_with_java_agent);
 299   st->print_cr("- use_optimized_module_handling:  %d", _use_optimized_module_handling);
 300   st->print_cr("- has_full_module_graph           %d", _has_full_module_graph);



 301 }
 302 
 303 void SharedClassPathEntry::init_as_non_existent(const char* path, TRAPS) {
 304   _type = non_existent_entry;
 305   set_name(path, CHECK);
 306 }
 307 
 308 void SharedClassPathEntry::init(bool is_modules_image,
 309                                 bool is_module_path,
 310                                 ClassPathEntry* cpe, TRAPS) {
 311   assert(CDSConfig::is_dumping_archive(), "sanity");
 312   _timestamp = 0;
 313   _filesize  = 0;
 314   _from_class_path_attr = false;
 315 
 316   struct stat st;
 317   if (os::stat(cpe->name(), &st) == 0) {
 318     if ((st.st_mode & S_IFMT) == S_IFDIR) {
 319       _type = dir_entry;
 320     } else {

 545 
 546 void FileMapInfo::record_non_existent_class_path_entry(const char* path) {
 547   assert(CDSConfig::is_dumping_archive(), "sanity");
 548   log_info(class, path)("non-existent Class-Path entry %s", path);
 549   if (_non_existent_class_paths == nullptr) {
 550     _non_existent_class_paths = new (mtClass) GrowableArray<const char*>(10, mtClass);
 551   }
 552   _non_existent_class_paths->append(os::strdup(path));
 553 }
 554 
 555 int FileMapInfo::num_non_existent_class_paths() {
 556   assert(CDSConfig::is_dumping_archive(), "sanity");
 557   if (_non_existent_class_paths != nullptr) {
 558     return _non_existent_class_paths->length();
 559   } else {
 560     return 0;
 561   }
 562 }
 563 
 564 int FileMapInfo::get_module_shared_path_index(Symbol* location) {



 565   if (location->starts_with("jrt:", 4) && get_number_of_shared_paths() > 0) {
 566     assert(shared_path(0)->is_modules_image(), "first shared_path must be the modules image");
 567     return 0;
 568   }
 569 
 570   if (ClassLoaderExt::app_module_paths_start_index() >= get_number_of_shared_paths()) {
 571     // The archive(s) were created without --module-path option
 572     return -1;
 573   }
 574 
 575   if (!location->starts_with("file:", 5)) {
 576     return -1;
 577   }
 578 
 579   // skip_uri_protocol was also called during dump time -- see ClassLoaderExt::process_module_table()
 580   ResourceMark rm;
 581   const char* file = ClassLoader::skip_uri_protocol(location->as_C_string());
 582   for (int i = ClassLoaderExt::app_module_paths_start_index(); i < get_number_of_shared_paths(); i++) {
 583     SharedClassPathEntry* ent = shared_path(i);
 584     if (!ent->is_non_existent()) {

1459 bool FileMapRegion::check_region_crc(char* base) const {
1460   // This function should be called after the region has been properly
1461   // loaded into memory via FileMapInfo::map_region() or FileMapInfo::read_region().
1462   // I.e., this->mapped_base() must be valid.
1463   size_t sz = used();
1464   if (sz == 0) {
1465     return true;
1466   }
1467 
1468   assert(base != nullptr, "must be initialized");
1469   int crc = ClassLoader::crc32(0, base, (jint)sz);
1470   if (crc != this->crc()) {
1471     log_warning(cds)("Checksum verification failed.");
1472     return false;
1473   }
1474   return true;
1475 }
1476 
1477 static const char* region_name(int region_index) {
1478   static const char* names[] = {
1479     "rw", "ro", "bm", "hp"
1480   };
1481   const int num_regions = sizeof(names)/sizeof(names[0]);
1482   assert(0 <= region_index && region_index < num_regions, "sanity");
1483 
1484   return names[region_index];
1485 }
1486 
1487 BitMapView FileMapInfo::bitmap_view(int region_index, bool is_oopmap) {
1488   FileMapRegion* r = region_at(region_index);
1489   char* bitmap_base = is_static() ? FileMapInfo::current_info()->map_bitmap_region() : FileMapInfo::dynamic_info()->map_bitmap_region();
1490   bitmap_base += is_oopmap ? r->oopmap_offset() : r->ptrmap_offset();
1491   size_t size_in_bits = is_oopmap ? r->oopmap_size_in_bits() : r->ptrmap_size_in_bits();
1492 
1493   log_debug(cds, reloc)("mapped %s relocation %smap @ " INTPTR_FORMAT " (" SIZE_FORMAT " bits)",
1494                         region_name(region_index), is_oopmap ? "oop" : "ptr",
1495                         p2i(bitmap_base), size_in_bits);
1496 
1497   return BitMapView((BitMap::bm_word_t*)(bitmap_base), size_in_bits);
1498 }
1499 

1545       mapping_offset = (size_t)((address)requested_base - CompressedOops::base());
1546       assert((mapping_offset >> CompressedOops::shift()) << CompressedOops::shift() == mapping_offset, "must be");
1547     } else {
1548       mapping_offset = 0; // not used with !UseCompressedOops
1549     }
1550 #endif // INCLUDE_CDS_JAVA_HEAP
1551   } else {
1552     char* requested_SharedBaseAddress = (char*)MetaspaceShared::requested_base_address();
1553     requested_base = ArchiveBuilder::current()->to_requested(base);
1554     assert(requested_base >= requested_SharedBaseAddress, "must be");
1555     mapping_offset = requested_base - requested_SharedBaseAddress;
1556   }
1557 
1558   r->set_file_offset(_file_offset);
1559   int crc = ClassLoader::crc32(0, base, (jint)size);
1560   if (size > 0) {
1561     log_info(cds)("Shared file region (%s) %d: " SIZE_FORMAT_W(8)
1562                    " bytes, addr " INTPTR_FORMAT " file offset 0x%08" PRIxPTR
1563                    " crc 0x%08x",
1564                    region_name(region), region, size, p2i(requested_base), _file_offset, crc);



1565   }
1566 
1567   r->init(region, mapping_offset, size, read_only, allow_exec, crc);
1568 
1569   if (base != nullptr) {
1570     write_bytes_aligned(base, size);
1571   }
1572 }
1573 
1574 static size_t write_bitmap(const CHeapBitMap* map, char* output, size_t offset) {
1575   size_t size_in_bytes = map->size_in_bytes();
1576   map->write_to((BitMap::bm_word_t*)(output + offset), size_in_bytes);
1577   return offset + size_in_bytes;
1578 }
1579 
1580 // The start of the archived heap has many primitive arrays (String
1581 // bodies) that are not marked by the oop/ptr maps. So we must have
1582 // lots of leading zeros.
1583 size_t FileMapInfo::remove_bitmap_leading_zeros(CHeapBitMap* map) {
1584   size_t old_zeros = map->find_first_set_bit(0);
1585   size_t old_size = map->size();
1586 
1587   // Slice and resize bitmap
1588   map->truncate(old_zeros, map->size());
1589 
1590   DEBUG_ONLY(
1591     size_t new_zeros = map->find_first_set_bit(0);
1592     assert(new_zeros == 0, "Should have removed leading zeros");
1593   )
1594   assert(map->size() <= old_size, "sanity");
1595   return old_zeros;
1596 }
1597 
1598 char* FileMapInfo::write_bitmap_region(CHeapBitMap* rw_ptrmap, CHeapBitMap* ro_ptrmap, ArchiveHeapInfo* heap_info,


1599                                        size_t &size_in_bytes) {
1600   size_t removed_rw_zeros = remove_bitmap_leading_zeros(rw_ptrmap);
1601   size_t removed_ro_zeros = remove_bitmap_leading_zeros(ro_ptrmap);
1602   header()->set_rw_ptrmap_start_pos(removed_rw_zeros);
1603   header()->set_ro_ptrmap_start_pos(removed_ro_zeros);
1604   size_in_bytes = rw_ptrmap->size_in_bytes() + ro_ptrmap->size_in_bytes();
1605 
1606   if (heap_info->is_used()) {
1607     // Remove leading zeros
1608     size_t removed_oop_zeros = remove_bitmap_leading_zeros(heap_info->oopmap());
1609     size_t removed_ptr_zeros = remove_bitmap_leading_zeros(heap_info->ptrmap());
1610 
1611     header()->set_heap_oopmap_start_pos(removed_oop_zeros);
1612     header()->set_heap_ptrmap_start_pos(removed_ptr_zeros);
1613 
1614     size_in_bytes += heap_info->oopmap()->size_in_bytes();
1615     size_in_bytes += heap_info->ptrmap()->size_in_bytes();
1616   }
1617 
1618   // The bitmap region contains up to 4 parts:
1619   // rw_ptrmap:           metaspace pointers inside the read-write region
1620   // ro_ptrmap:           metaspace pointers inside the read-only region
1621   // heap_info->oopmap(): Java oop pointers in the heap region
1622   // heap_info->ptrmap(): metaspace pointers in the heap region
1623   char* buffer = NEW_C_HEAP_ARRAY(char, size_in_bytes, mtClassShared);
1624   size_t written = 0;
1625 
1626   region_at(MetaspaceShared::rw)->init_ptrmap(0, rw_ptrmap->size());
1627   written = write_bitmap(rw_ptrmap, buffer, written);
1628 
1629   region_at(MetaspaceShared::ro)->init_ptrmap(written, ro_ptrmap->size());
1630   written = write_bitmap(ro_ptrmap, buffer, written);
1631 



1632   if (heap_info->is_used()) {
1633     FileMapRegion* r = region_at(MetaspaceShared::hp);
1634 
1635     r->init_oopmap(written, heap_info->oopmap()->size());
1636     written = write_bitmap(heap_info->oopmap(), buffer, written);
1637 
1638     r->init_ptrmap(written, heap_info->ptrmap()->size());
1639     written = write_bitmap(heap_info->ptrmap(), buffer, written);
1640   }
1641 
1642   write_region(MetaspaceShared::bm, (char*)buffer, size_in_bytes, /*read_only=*/true, /*allow_exec=*/false);
1643   return buffer;
1644 }
1645 
1646 size_t FileMapInfo::write_heap_region(ArchiveHeapInfo* heap_info) {
1647   char* buffer_start = heap_info->buffer_start();
1648   size_t buffer_size = heap_info->buffer_byte_size();
1649   write_region(MetaspaceShared::hp, buffer_start, buffer_size, false, false);
1650   header()->set_heap_roots_offset(heap_info->heap_roots_offset());
1651   return buffer_size;

1740   assert(WINDOWS_ONLY(false) NOT_WINDOWS(true), "Don't call on Windows");
1741   // Replace old mapping with new one that is writable.
1742   char *base = os::map_memory(_fd, _full_path, r->file_offset(),
1743                               addr, size, false /* !read_only */,
1744                               r->allow_exec());
1745   close();
1746   // These have to be errors because the shared region is now unmapped.
1747   if (base == nullptr) {
1748     log_error(cds)("Unable to remap shared readonly space (errno=%d).", errno);
1749     vm_exit(1);
1750   }
1751   if (base != addr) {
1752     log_error(cds)("Unable to remap shared readonly space (errno=%d).", errno);
1753     vm_exit(1);
1754   }
1755   r->set_read_only(false);
1756   return true;
1757 }
1758 
1759 // Memory map a region in the address space.
1760 static const char* shared_region_name[] = { "ReadWrite", "ReadOnly", "Bitmap", "Heap" };
1761 
1762 MapArchiveResult FileMapInfo::map_regions(int regions[], int num_regions, char* mapped_base_address, ReservedSpace rs) {
1763   DEBUG_ONLY(FileMapRegion* last_region = nullptr);
1764   intx addr_delta = mapped_base_address - header()->requested_base_address();
1765 
1766   // Make sure we don't attempt to use header()->mapped_base_address() unless
1767   // it's been successfully mapped.
1768   DEBUG_ONLY(header()->set_mapped_base_address((char*)(uintptr_t)0xdeadbeef);)
1769 
1770   for (int i = 0; i < num_regions; i++) {
1771     int idx = regions[i];
1772     MapArchiveResult result = map_region(idx, addr_delta, mapped_base_address, rs);
1773     if (result != MAP_ARCHIVE_SUCCESS) {
1774       return result;
1775     }
1776     FileMapRegion* r = region_at(idx);
1777     DEBUG_ONLY(if (last_region != nullptr) {
1778         // Ensure that the OS won't be able to allocate new memory spaces between any mapped
1779         // regions, or else it would mess up the simple comparison in MetaspaceObj::is_shared().
1780         assert(r->mapped_base() == last_region->mapped_end(), "must have no gaps");

1843     r->set_read_only(false); // Need to patch the pointers
1844   }
1845 
1846   if (MetaspaceShared::use_windows_memory_mapping() && rs.is_reserved()) {
1847     // This is the second time we try to map the archive(s). We have already created a ReservedSpace
1848     // that covers all the FileMapRegions to ensure all regions can be mapped. However, Windows
1849     // can't mmap into a ReservedSpace, so we just ::read() the data. We're going to patch all the
1850     // regions anyway, so there's no benefit for mmap anyway.
1851     if (!read_region(i, requested_addr, size, /* do_commit = */ true)) {
1852       log_info(cds)("Failed to read %s shared space into reserved space at " INTPTR_FORMAT,
1853                     shared_region_name[i], p2i(requested_addr));
1854       return MAP_ARCHIVE_OTHER_FAILURE; // oom or I/O error.
1855     } else {
1856       assert(r->mapped_base() != nullptr, "must be initialized");
1857       return MAP_ARCHIVE_SUCCESS;
1858     }
1859   } else {
1860     // Note that this may either be a "fresh" mapping into unreserved address
1861     // space (Windows, first mapping attempt), or a mapping into pre-reserved
1862     // space (Posix). See also comment in MetaspaceShared::map_archives().

1863     char* base = map_memory(_fd, _full_path, r->file_offset(),
1864                             requested_addr, size, r->read_only(),
1865                             r->allow_exec(), mtClassShared);
1866     if (base != requested_addr) {
1867       log_info(cds)("Unable to map %s shared space at " INTPTR_FORMAT,
1868                     shared_region_name[i], p2i(requested_addr));
1869       _memory_mapping_failed = true;
1870       return MAP_ARCHIVE_MMAP_FAILURE;
1871     }
1872 
1873     if (VerifySharedSpaces && !r->check_region_crc(requested_addr)) {
1874       return MAP_ARCHIVE_OTHER_FAILURE;
1875     }
1876 
1877     r->set_mapped_from_file(true);
1878     r->set_mapped_base(requested_addr);
1879 
1880     return MAP_ARCHIVE_SUCCESS;
1881   }
1882 }
1883 
1884 // The return value is the location of the archive relocation bitmap.

1896     return nullptr;
1897   }
1898 
1899   if (VerifySharedSpaces && !r->check_region_crc(bitmap_base)) {
1900     log_error(cds)("relocation bitmap CRC error");
1901     if (!os::unmap_memory(bitmap_base, r->used_aligned())) {
1902       fatal("os::unmap_memory of relocation bitmap failed");
1903     }
1904     return nullptr;
1905   }
1906 
1907   r->set_mapped_from_file(true);
1908   r->set_mapped_base(bitmap_base);
1909   log_info(cds)("Mapped %s region #%d at base " INTPTR_FORMAT " top " INTPTR_FORMAT " (%s)",
1910                 is_static() ? "static " : "dynamic",
1911                 MetaspaceShared::bm, p2i(r->mapped_base()), p2i(r->mapped_end()),
1912                 shared_region_name[MetaspaceShared::bm]);
1913   return bitmap_base;
1914 }
1915 








































































1916 // This is called when we cannot map the archive at the requested[ base address (usually 0x800000000).
1917 // We relocate all pointers in the 2 core regions (ro, rw).
1918 bool FileMapInfo::relocate_pointers_in_core_regions(intx addr_delta) {
1919   log_debug(cds, reloc)("runtime archive relocation start");
1920   char* bitmap_base = map_bitmap_region();
1921 
1922   if (bitmap_base == nullptr) {
1923     return false; // OOM, or CRC check failure
1924   } else {
1925     BitMapView rw_ptrmap = ptrmap_view(MetaspaceShared::rw);
1926     BitMapView ro_ptrmap = ptrmap_view(MetaspaceShared::ro);
1927 
1928     FileMapRegion* rw_region = first_core_region();
1929     FileMapRegion* ro_region = last_core_region();
1930 
1931     // Patch all pointers inside the RW region
1932     address rw_patch_base = (address)rw_region->mapped_base();
1933     address rw_patch_end  = (address)rw_region->mapped_end();
1934 
1935     // Patch all pointers inside the RO region

2013 
2014 void FileMapInfo::map_or_load_heap_region() {
2015   bool success = false;
2016 
2017   if (can_use_heap_region()) {
2018     if (ArchiveHeapLoader::can_map()) {
2019       success = map_heap_region();
2020     } else if (ArchiveHeapLoader::can_load()) {
2021       success = ArchiveHeapLoader::load_heap_region(this);
2022     } else {
2023       if (!UseCompressedOops && !ArchiveHeapLoader::can_map()) {
2024         // TODO - remove implicit knowledge of G1
2025         log_info(cds)("Cannot use CDS heap data. UseG1GC is required for -XX:-UseCompressedOops");
2026       } else {
2027         log_info(cds)("Cannot use CDS heap data. UseEpsilonGC, UseG1GC, UseSerialGC or UseParallelGC are required.");
2028       }
2029     }
2030   }
2031 
2032   if (!success) {
2033     CDSConfig::stop_using_full_module_graph();
2034   }
2035 }
2036 
2037 bool FileMapInfo::can_use_heap_region() {
2038   if (!has_heap_region()) {
2039     return false;
2040   }
2041   if (JvmtiExport::should_post_class_file_load_hook() && JvmtiExport::has_early_class_hook_env()) {
2042     ShouldNotReachHere(); // CDS should have been disabled.
2043     // The archived objects are mapped at JVM start-up, but we don't know if
2044     // j.l.String or j.l.Class might be replaced by the ClassFileLoadHook,
2045     // which would make the archived String or mirror objects invalid. Let's be safe and not
2046     // use the archived objects. These 2 classes are loaded during the JVMTI "early" stage.
2047     //
2048     // If JvmtiExport::has_early_class_hook_env() is false, the classes of some objects
2049     // in the archived subgraphs may be replaced by the ClassFileLoadHook. But that's OK
2050     // because we won't install an archived object subgraph if the klass of any of the
2051     // referenced objects are replaced. See HeapShared::initialize_from_archived_subgraph().
2052   }
2053 

2328     return false;
2329   }
2330 
2331   if (!open_for_read() || !init_from_file(_fd) || !validate_header()) {
2332     if (_is_static) {
2333       log_info(cds)("Initialize static archive failed.");
2334       return false;
2335     } else {
2336       log_info(cds)("Initialize dynamic archive failed.");
2337       if (AutoCreateSharedArchive) {
2338         CDSConfig::enable_dumping_dynamic_archive();
2339         ArchiveClassesAtExit = CDSConfig::dynamic_archive_path();
2340       }
2341       return false;
2342     }
2343   }
2344 
2345   return true;
2346 }
2347 


























2348 // The 2 core spaces are RW->RO
2349 FileMapRegion* FileMapInfo::first_core_region() const {
2350   return region_at(MetaspaceShared::rw);
2351 }
2352 
2353 FileMapRegion* FileMapInfo::last_core_region() const {
2354   return region_at(MetaspaceShared::ro);
2355 }
2356 
2357 void FileMapInfo::print(outputStream* st) const {
2358   header()->print(st);
2359   if (!is_static()) {
2360     dynamic_header()->print(st);
2361   }
2362 }
2363 
2364 void FileMapHeader::set_as_offset(char* p, size_t *offset) {
2365   *offset = ArchiveBuilder::current()->any_to_offset((address)p);
2366 }
2367 

2438   }
2439 
2440   log_info(cds)("Archive was created with UseCompressedOops = %d, UseCompressedClassPointers = %d",
2441                           compressed_oops(), compressed_class_pointers());
2442   if (compressed_oops() != UseCompressedOops || compressed_class_pointers() != UseCompressedClassPointers) {
2443     log_info(cds)("Unable to use shared archive.\nThe saved state of UseCompressedOops and UseCompressedClassPointers is "
2444                                "different from runtime, CDS will be disabled.");
2445     return false;
2446   }
2447 
2448   if (! _use_secondary_supers_table && UseSecondarySupersTable) {
2449     log_warning(cds)("The shared archive was created without UseSecondarySupersTable.");
2450     return false;
2451   }
2452 
2453   if (!_use_optimized_module_handling) {
2454     CDSConfig::stop_using_optimized_module_handling();
2455     log_info(cds)("optimized module handling: disabled because archive was created without optimized module handling");
2456   }
2457 
2458   if (is_static() && !_has_full_module_graph) {
2459     // Only the static archive can contain the full module graph.
2460     CDSConfig::stop_using_full_module_graph("archive was created without full module graph");









2461   }
2462 
2463   return true;
2464 }
2465 
2466 bool FileMapInfo::validate_header() {
2467   if (!header()->validate()) {
2468     return false;
2469   }
2470   if (_is_static) {
2471     return true;
2472   } else {
2473     return DynamicArchive::validate(this);
2474   }
2475 }
2476 
2477 #if INCLUDE_JVMTI
2478 ClassPathEntry** FileMapInfo::_classpath_entries_for_jvmti = nullptr;
2479 
2480 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_preloaded_classes = PreloadSharedClasses;
 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   _gc_kind = (int)Universe::heap()->kind();
 222   jio_snprintf(_gc_name, sizeof(_gc_name), Universe::heap()->name());
 223 
 224   // The following fields are for sanity checks for whether this archive
 225   // will function correctly with this JVM and the bootclasspath it's
 226   // invoked with.
 227 
 228   // JVM version string ... changes on each build.
 229   get_header_version(_jvm_ident);
 230 
 231   _app_class_paths_start_index = ClassLoaderExt::app_class_paths_start_index();
 232   _app_module_paths_start_index = ClassLoaderExt::app_module_paths_start_index();
 233   _max_used_path_index = ClassLoaderExt::max_used_path_index();
 234   _num_module_paths = ClassLoader::num_module_path_entries();
 235 
 236   _verify_local = BytecodeVerificationLocal;
 237   _verify_remote = BytecodeVerificationRemote;
 238   _has_platform_or_app_classes = ClassLoaderExt::has_platform_or_app_classes();
 239   _has_non_jar_in_classpath = ClassLoaderExt::has_non_jar_in_classpath();
 240   _requested_base_address = (char*)SharedBaseAddress;
 241   _mapped_base_address = (char*)SharedBaseAddress;
 242   _allow_archiving_with_java_agent = AllowArchivingWithJavaAgent;

 286   st->print_cr("- jvm_ident:                      %s", _jvm_ident);
 287   st->print_cr("- shared_path_table_offset:       " SIZE_FORMAT_X, _shared_path_table_offset);
 288   st->print_cr("- app_class_paths_start_index:    %d", _app_class_paths_start_index);
 289   st->print_cr("- app_module_paths_start_index:   %d", _app_module_paths_start_index);
 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_roots_offset:              " SIZE_FORMAT, _heap_roots_offset);
 299   st->print_cr("- _heap_oopmap_start_pos:         " SIZE_FORMAT, _heap_oopmap_start_pos);
 300   st->print_cr("- _heap_ptrmap_start_pos:         " SIZE_FORMAT, _heap_ptrmap_start_pos);
 301   st->print_cr("- _rw_ptrmap_start_pos:           " SIZE_FORMAT, _rw_ptrmap_start_pos);
 302   st->print_cr("- _ro_ptrmap_start_pos:           " SIZE_FORMAT, _ro_ptrmap_start_pos);
 303   st->print_cr("- allow_archiving_with_java_agent:%d", _allow_archiving_with_java_agent);
 304   st->print_cr("- use_optimized_module_handling:  %d", _use_optimized_module_handling);
 305   st->print_cr("- has_full_module_graph           %d", _has_full_module_graph);
 306   st->print_cr("- has_archived_invokedynamic      %d", _has_archived_invokedynamic);
 307   st->print_cr("- has_archived_packages           %d", _has_archived_packages);
 308   st->print_cr("- ptrmap_size_in_bits:            " SIZE_FORMAT, _ptrmap_size_in_bits);
 309 }
 310 
 311 void SharedClassPathEntry::init_as_non_existent(const char* path, TRAPS) {
 312   _type = non_existent_entry;
 313   set_name(path, CHECK);
 314 }
 315 
 316 void SharedClassPathEntry::init(bool is_modules_image,
 317                                 bool is_module_path,
 318                                 ClassPathEntry* cpe, TRAPS) {
 319   assert(CDSConfig::is_dumping_archive(), "sanity");
 320   _timestamp = 0;
 321   _filesize  = 0;
 322   _from_class_path_attr = false;
 323 
 324   struct stat st;
 325   if (os::stat(cpe->name(), &st) == 0) {
 326     if ((st.st_mode & S_IFMT) == S_IFDIR) {
 327       _type = dir_entry;
 328     } else {

 553 
 554 void FileMapInfo::record_non_existent_class_path_entry(const char* path) {
 555   assert(CDSConfig::is_dumping_archive(), "sanity");
 556   log_info(class, path)("non-existent Class-Path entry %s", path);
 557   if (_non_existent_class_paths == nullptr) {
 558     _non_existent_class_paths = new (mtClass) GrowableArray<const char*>(10, mtClass);
 559   }
 560   _non_existent_class_paths->append(os::strdup(path));
 561 }
 562 
 563 int FileMapInfo::num_non_existent_class_paths() {
 564   assert(CDSConfig::is_dumping_archive(), "sanity");
 565   if (_non_existent_class_paths != nullptr) {
 566     return _non_existent_class_paths->length();
 567   } else {
 568     return 0;
 569   }
 570 }
 571 
 572 int FileMapInfo::get_module_shared_path_index(Symbol* location) {
 573   if (location == nullptr) {
 574     return 0; // Used by java/lang/reflect/Proxy$ProxyBuilder
 575   }
 576   if (location->starts_with("jrt:", 4) && get_number_of_shared_paths() > 0) {
 577     assert(shared_path(0)->is_modules_image(), "first shared_path must be the modules image");
 578     return 0;
 579   }
 580 
 581   if (ClassLoaderExt::app_module_paths_start_index() >= get_number_of_shared_paths()) {
 582     // The archive(s) were created without --module-path option
 583     return -1;
 584   }
 585 
 586   if (!location->starts_with("file:", 5)) {
 587     return -1;
 588   }
 589 
 590   // skip_uri_protocol was also called during dump time -- see ClassLoaderExt::process_module_table()
 591   ResourceMark rm;
 592   const char* file = ClassLoader::skip_uri_protocol(location->as_C_string());
 593   for (int i = ClassLoaderExt::app_module_paths_start_index(); i < get_number_of_shared_paths(); i++) {
 594     SharedClassPathEntry* ent = shared_path(i);
 595     if (!ent->is_non_existent()) {

1470 bool FileMapRegion::check_region_crc(char* base) const {
1471   // This function should be called after the region has been properly
1472   // loaded into memory via FileMapInfo::map_region() or FileMapInfo::read_region().
1473   // I.e., this->mapped_base() must be valid.
1474   size_t sz = used();
1475   if (sz == 0) {
1476     return true;
1477   }
1478 
1479   assert(base != nullptr, "must be initialized");
1480   int crc = ClassLoader::crc32(0, base, (jint)sz);
1481   if (crc != this->crc()) {
1482     log_warning(cds)("Checksum verification failed.");
1483     return false;
1484   }
1485   return true;
1486 }
1487 
1488 static const char* region_name(int region_index) {
1489   static const char* names[] = {
1490     "rw", "ro", "bm", "hp", "cc",
1491   };
1492   const int num_regions = sizeof(names)/sizeof(names[0]);
1493   assert(0 <= region_index && region_index < num_regions, "sanity");
1494 
1495   return names[region_index];
1496 }
1497 
1498 BitMapView FileMapInfo::bitmap_view(int region_index, bool is_oopmap) {
1499   FileMapRegion* r = region_at(region_index);
1500   char* bitmap_base = is_static() ? FileMapInfo::current_info()->map_bitmap_region() : FileMapInfo::dynamic_info()->map_bitmap_region();
1501   bitmap_base += is_oopmap ? r->oopmap_offset() : r->ptrmap_offset();
1502   size_t size_in_bits = is_oopmap ? r->oopmap_size_in_bits() : r->ptrmap_size_in_bits();
1503 
1504   log_debug(cds, reloc)("mapped %s relocation %smap @ " INTPTR_FORMAT " (" SIZE_FORMAT " bits)",
1505                         region_name(region_index), is_oopmap ? "oop" : "ptr",
1506                         p2i(bitmap_base), size_in_bits);
1507 
1508   return BitMapView((BitMap::bm_word_t*)(bitmap_base), size_in_bits);
1509 }
1510 

1556       mapping_offset = (size_t)((address)requested_base - CompressedOops::base());
1557       assert((mapping_offset >> CompressedOops::shift()) << CompressedOops::shift() == mapping_offset, "must be");
1558     } else {
1559       mapping_offset = 0; // not used with !UseCompressedOops
1560     }
1561 #endif // INCLUDE_CDS_JAVA_HEAP
1562   } else {
1563     char* requested_SharedBaseAddress = (char*)MetaspaceShared::requested_base_address();
1564     requested_base = ArchiveBuilder::current()->to_requested(base);
1565     assert(requested_base >= requested_SharedBaseAddress, "must be");
1566     mapping_offset = requested_base - requested_SharedBaseAddress;
1567   }
1568 
1569   r->set_file_offset(_file_offset);
1570   int crc = ClassLoader::crc32(0, base, (jint)size);
1571   if (size > 0) {
1572     log_info(cds)("Shared file region (%s) %d: " SIZE_FORMAT_W(8)
1573                    " bytes, addr " INTPTR_FORMAT " file offset 0x%08" PRIxPTR
1574                    " crc 0x%08x",
1575                    region_name(region), region, size, p2i(requested_base), _file_offset, crc);
1576   } else {
1577     log_info(cds)("Shared file region (%s) %d: " SIZE_FORMAT_W(8)
1578                   " bytes", region_name(region), region, size);
1579   }
1580 
1581   r->init(region, mapping_offset, size, read_only, allow_exec, crc);
1582 
1583   if (base != nullptr) {
1584     write_bytes_aligned(base, size);
1585   }
1586 }
1587 
1588 static size_t write_bitmap(const CHeapBitMap* map, char* output, size_t offset) {
1589   size_t size_in_bytes = map->size_in_bytes();
1590   map->write_to((BitMap::bm_word_t*)(output + offset), size_in_bytes);
1591   return offset + size_in_bytes;
1592 }
1593 
1594 // The start of the archived heap has many primitive arrays (String
1595 // bodies) that are not marked by the oop/ptr maps. So we must have
1596 // lots of leading zeros.
1597 size_t FileMapInfo::remove_bitmap_leading_zeros(CHeapBitMap* map) {
1598   size_t old_zeros = map->find_first_set_bit(0);
1599   size_t old_size = map->size();
1600 
1601   // Slice and resize bitmap
1602   map->truncate(old_zeros, map->size());
1603 
1604   DEBUG_ONLY(
1605     size_t new_zeros = map->find_first_set_bit(0);
1606     assert(new_zeros == 0, "Should have removed leading zeros");
1607   )
1608   assert(map->size() <= old_size, "sanity");
1609   return old_zeros;
1610 }
1611 
1612 char* FileMapInfo::write_bitmap_region(CHeapBitMap* rw_ptrmap, CHeapBitMap* ro_ptrmap,
1613                                        CHeapBitMap* cc_ptrmap,
1614                                        ArchiveHeapInfo* heap_info,
1615                                        size_t &size_in_bytes) {
1616   size_t removed_rw_zeros = remove_bitmap_leading_zeros(rw_ptrmap);
1617   size_t removed_ro_zeros = remove_bitmap_leading_zeros(ro_ptrmap);
1618   header()->set_rw_ptrmap_start_pos(removed_rw_zeros);
1619   header()->set_ro_ptrmap_start_pos(removed_ro_zeros);
1620   size_in_bytes = rw_ptrmap->size_in_bytes() + ro_ptrmap->size_in_bytes() + cc_ptrmap->size_in_bytes();
1621 
1622   if (heap_info->is_used()) {
1623     // Remove leading zeros
1624     size_t removed_oop_zeros = remove_bitmap_leading_zeros(heap_info->oopmap());
1625     size_t removed_ptr_zeros = remove_bitmap_leading_zeros(heap_info->ptrmap());
1626 
1627     header()->set_heap_oopmap_start_pos(removed_oop_zeros);
1628     header()->set_heap_ptrmap_start_pos(removed_ptr_zeros);
1629 
1630     size_in_bytes += heap_info->oopmap()->size_in_bytes();
1631     size_in_bytes += heap_info->ptrmap()->size_in_bytes();
1632   }
1633 
1634   // The bitmap region contains up to 4 parts:
1635   // rw_ptrmap:           metaspace pointers inside the read-write region
1636   // ro_ptrmap:           metaspace pointers inside the read-only region
1637   // heap_info->oopmap(): Java oop pointers in the heap region
1638   // heap_info->ptrmap(): metaspace pointers in the heap region
1639   char* buffer = NEW_C_HEAP_ARRAY(char, size_in_bytes, mtClassShared);
1640   size_t written = 0;
1641 
1642   region_at(MetaspaceShared::rw)->init_ptrmap(0, rw_ptrmap->size());
1643   written = write_bitmap(rw_ptrmap, buffer, written);
1644 
1645   region_at(MetaspaceShared::ro)->init_ptrmap(written, ro_ptrmap->size());
1646   written = write_bitmap(ro_ptrmap, buffer, written);
1647 
1648   region_at(MetaspaceShared::cc)->init_ptrmap(written, cc_ptrmap->size());
1649   written = write_bitmap(cc_ptrmap, buffer, written);
1650 
1651   if (heap_info->is_used()) {
1652     FileMapRegion* r = region_at(MetaspaceShared::hp);
1653 
1654     r->init_oopmap(written, heap_info->oopmap()->size());
1655     written = write_bitmap(heap_info->oopmap(), buffer, written);
1656 
1657     r->init_ptrmap(written, heap_info->ptrmap()->size());
1658     written = write_bitmap(heap_info->ptrmap(), buffer, written);
1659   }
1660 
1661   write_region(MetaspaceShared::bm, (char*)buffer, size_in_bytes, /*read_only=*/true, /*allow_exec=*/false);
1662   return buffer;
1663 }
1664 
1665 size_t FileMapInfo::write_heap_region(ArchiveHeapInfo* heap_info) {
1666   char* buffer_start = heap_info->buffer_start();
1667   size_t buffer_size = heap_info->buffer_byte_size();
1668   write_region(MetaspaceShared::hp, buffer_start, buffer_size, false, false);
1669   header()->set_heap_roots_offset(heap_info->heap_roots_offset());
1670   return buffer_size;

1759   assert(WINDOWS_ONLY(false) NOT_WINDOWS(true), "Don't call on Windows");
1760   // Replace old mapping with new one that is writable.
1761   char *base = os::map_memory(_fd, _full_path, r->file_offset(),
1762                               addr, size, false /* !read_only */,
1763                               r->allow_exec());
1764   close();
1765   // These have to be errors because the shared region is now unmapped.
1766   if (base == nullptr) {
1767     log_error(cds)("Unable to remap shared readonly space (errno=%d).", errno);
1768     vm_exit(1);
1769   }
1770   if (base != addr) {
1771     log_error(cds)("Unable to remap shared readonly space (errno=%d).", errno);
1772     vm_exit(1);
1773   }
1774   r->set_read_only(false);
1775   return true;
1776 }
1777 
1778 // Memory map a region in the address space.
1779 static const char* shared_region_name[] = { "ReadWrite", "ReadOnly", "Bitmap", "Heap", "Code" };
1780 
1781 MapArchiveResult FileMapInfo::map_regions(int regions[], int num_regions, char* mapped_base_address, ReservedSpace rs) {
1782   DEBUG_ONLY(FileMapRegion* last_region = nullptr);
1783   intx addr_delta = mapped_base_address - header()->requested_base_address();
1784 
1785   // Make sure we don't attempt to use header()->mapped_base_address() unless
1786   // it's been successfully mapped.
1787   DEBUG_ONLY(header()->set_mapped_base_address((char*)(uintptr_t)0xdeadbeef);)
1788 
1789   for (int i = 0; i < num_regions; i++) {
1790     int idx = regions[i];
1791     MapArchiveResult result = map_region(idx, addr_delta, mapped_base_address, rs);
1792     if (result != MAP_ARCHIVE_SUCCESS) {
1793       return result;
1794     }
1795     FileMapRegion* r = region_at(idx);
1796     DEBUG_ONLY(if (last_region != nullptr) {
1797         // Ensure that the OS won't be able to allocate new memory spaces between any mapped
1798         // regions, or else it would mess up the simple comparison in MetaspaceObj::is_shared().
1799         assert(r->mapped_base() == last_region->mapped_end(), "must have no gaps");

1862     r->set_read_only(false); // Need to patch the pointers
1863   }
1864 
1865   if (MetaspaceShared::use_windows_memory_mapping() && rs.is_reserved()) {
1866     // This is the second time we try to map the archive(s). We have already created a ReservedSpace
1867     // that covers all the FileMapRegions to ensure all regions can be mapped. However, Windows
1868     // can't mmap into a ReservedSpace, so we just ::read() the data. We're going to patch all the
1869     // regions anyway, so there's no benefit for mmap anyway.
1870     if (!read_region(i, requested_addr, size, /* do_commit = */ true)) {
1871       log_info(cds)("Failed to read %s shared space into reserved space at " INTPTR_FORMAT,
1872                     shared_region_name[i], p2i(requested_addr));
1873       return MAP_ARCHIVE_OTHER_FAILURE; // oom or I/O error.
1874     } else {
1875       assert(r->mapped_base() != nullptr, "must be initialized");
1876       return MAP_ARCHIVE_SUCCESS;
1877     }
1878   } else {
1879     // Note that this may either be a "fresh" mapping into unreserved address
1880     // space (Windows, first mapping attempt), or a mapping into pre-reserved
1881     // space (Posix). See also comment in MetaspaceShared::map_archives().
1882     bool read_only = r->read_only() && !CDSConfig::is_dumping_final_static_archive();
1883     char* base = map_memory(_fd, _full_path, r->file_offset(),
1884                             requested_addr, size, read_only,
1885                             r->allow_exec(), mtClassShared);
1886     if (base != requested_addr) {
1887       log_info(cds)("Unable to map %s shared space at " INTPTR_FORMAT,
1888                     shared_region_name[i], p2i(requested_addr));
1889       _memory_mapping_failed = true;
1890       return MAP_ARCHIVE_MMAP_FAILURE;
1891     }
1892 
1893     if (VerifySharedSpaces && !r->check_region_crc(requested_addr)) {
1894       return MAP_ARCHIVE_OTHER_FAILURE;
1895     }
1896 
1897     r->set_mapped_from_file(true);
1898     r->set_mapped_base(requested_addr);
1899 
1900     return MAP_ARCHIVE_SUCCESS;
1901   }
1902 }
1903 
1904 // The return value is the location of the archive relocation bitmap.

1916     return nullptr;
1917   }
1918 
1919   if (VerifySharedSpaces && !r->check_region_crc(bitmap_base)) {
1920     log_error(cds)("relocation bitmap CRC error");
1921     if (!os::unmap_memory(bitmap_base, r->used_aligned())) {
1922       fatal("os::unmap_memory of relocation bitmap failed");
1923     }
1924     return nullptr;
1925   }
1926 
1927   r->set_mapped_from_file(true);
1928   r->set_mapped_base(bitmap_base);
1929   log_info(cds)("Mapped %s region #%d at base " INTPTR_FORMAT " top " INTPTR_FORMAT " (%s)",
1930                 is_static() ? "static " : "dynamic",
1931                 MetaspaceShared::bm, p2i(r->mapped_base()), p2i(r->mapped_end()),
1932                 shared_region_name[MetaspaceShared::bm]);
1933   return bitmap_base;
1934 }
1935 
1936 bool FileMapInfo::map_cached_code_region(ReservedSpace rs) {
1937   FileMapRegion* r = region_at(MetaspaceShared::cc);
1938   assert(r->used() > 0 && rs.size(), "must be");
1939 
1940   bool read_only = false, allow_exec = true;
1941   char* requested_base = rs.base();
1942   char* mapped_base = map_memory(_fd, _full_path, r->file_offset(),
1943                                  requested_base, r->used_aligned(), read_only, allow_exec, mtClassShared);
1944   if (mapped_base == nullptr) {
1945     log_info(cds)("failed to map cached code region");
1946     return false;
1947   } else {
1948     assert(mapped_base == requested_base, "must be");
1949     r->set_mapped_from_file(true);
1950     r->set_mapped_base(mapped_base);
1951     relocate_pointers_in_cached_code_region();
1952     log_info(cds)("Mapped static region #%d at base " INTPTR_FORMAT " top " INTPTR_FORMAT " (%s)",
1953                   MetaspaceShared::bm, p2i(r->mapped_base()), p2i(r->mapped_end()),
1954                   shared_region_name[MetaspaceShared::cc]);
1955     return true;
1956   }
1957 }
1958 
1959 class CachedCodeRelocator: public BitMapClosure {
1960   address _code_requested_base;
1961   address* _patch_base;
1962   intx _code_delta;
1963   intx _metadata_delta;
1964 
1965 public:
1966   CachedCodeRelocator(address code_requested_base, address code_mapped_base,
1967                       intx metadata_delta) {
1968     _code_requested_base = code_requested_base;
1969     _patch_base = (address*)code_mapped_base;
1970     _code_delta = code_mapped_base - code_requested_base;
1971     _metadata_delta = metadata_delta;
1972   }
1973   
1974   bool do_bit(size_t offset) {
1975     address* p = _patch_base + offset;
1976     address requested_ptr = *p;
1977     if (requested_ptr < _code_requested_base) {
1978       *p = requested_ptr + _metadata_delta;
1979     } else {
1980       *p = requested_ptr + _code_delta;
1981     }
1982     return true; // keep iterating
1983   }
1984 };
1985 
1986 void FileMapInfo::relocate_pointers_in_cached_code_region() {
1987   FileMapRegion* r = region_at(MetaspaceShared::cc);
1988   char* bitmap_base = map_bitmap_region();
1989 
1990   BitMapView cc_ptrmap = ptrmap_view(MetaspaceShared::cc);
1991   if (cc_ptrmap.size() == 0) {
1992     return;
1993   }
1994 
1995   address core_regions_requested_base = (address)header()->requested_base_address();
1996   address core_regions_mapped_base = (address)header()->mapped_base_address();
1997   address cc_region_requested_base = core_regions_requested_base + r->mapping_offset();
1998   address cc_region_mapped_base = (address)r->mapped_base();
1999 
2000   size_t max_bits_for_core_regions = pointer_delta(mapped_end(), mapped_base(), // FIXME - renamed to core_regions_mapped_base(), etc
2001                                                    sizeof(address));
2002 
2003   CachedCodeRelocator patcher(cc_region_requested_base, cc_region_mapped_base,
2004                               core_regions_mapped_base - core_regions_requested_base);
2005   cc_ptrmap.iterate(&patcher);
2006 }
2007 
2008 // This is called when we cannot map the archive at the requested[ base address (usually 0x800000000).
2009 // We relocate all pointers in the 2 core regions (ro, rw).
2010 bool FileMapInfo::relocate_pointers_in_core_regions(intx addr_delta) {
2011   log_debug(cds, reloc)("runtime archive relocation start");
2012   char* bitmap_base = map_bitmap_region();
2013 
2014   if (bitmap_base == nullptr) {
2015     return false; // OOM, or CRC check failure
2016   } else {
2017     BitMapView rw_ptrmap = ptrmap_view(MetaspaceShared::rw);
2018     BitMapView ro_ptrmap = ptrmap_view(MetaspaceShared::ro);
2019 
2020     FileMapRegion* rw_region = first_core_region();
2021     FileMapRegion* ro_region = last_core_region();
2022 
2023     // Patch all pointers inside the RW region
2024     address rw_patch_base = (address)rw_region->mapped_base();
2025     address rw_patch_end  = (address)rw_region->mapped_end();
2026 
2027     // Patch all pointers inside the RO region

2105 
2106 void FileMapInfo::map_or_load_heap_region() {
2107   bool success = false;
2108 
2109   if (can_use_heap_region()) {
2110     if (ArchiveHeapLoader::can_map()) {
2111       success = map_heap_region();
2112     } else if (ArchiveHeapLoader::can_load()) {
2113       success = ArchiveHeapLoader::load_heap_region(this);
2114     } else {
2115       if (!UseCompressedOops && !ArchiveHeapLoader::can_map()) {
2116         // TODO - remove implicit knowledge of G1
2117         log_info(cds)("Cannot use CDS heap data. UseG1GC is required for -XX:-UseCompressedOops");
2118       } else {
2119         log_info(cds)("Cannot use CDS heap data. UseEpsilonGC, UseG1GC, UseSerialGC or UseParallelGC are required.");
2120       }
2121     }
2122   }
2123 
2124   if (!success) {
2125     CDSConfig::stop_using_full_module_graph("archive heap loading failed");
2126   }
2127 }
2128 
2129 bool FileMapInfo::can_use_heap_region() {
2130   if (!has_heap_region()) {
2131     return false;
2132   }
2133   if (JvmtiExport::should_post_class_file_load_hook() && JvmtiExport::has_early_class_hook_env()) {
2134     ShouldNotReachHere(); // CDS should have been disabled.
2135     // The archived objects are mapped at JVM start-up, but we don't know if
2136     // j.l.String or j.l.Class might be replaced by the ClassFileLoadHook,
2137     // which would make the archived String or mirror objects invalid. Let's be safe and not
2138     // use the archived objects. These 2 classes are loaded during the JVMTI "early" stage.
2139     //
2140     // If JvmtiExport::has_early_class_hook_env() is false, the classes of some objects
2141     // in the archived subgraphs may be replaced by the ClassFileLoadHook. But that's OK
2142     // because we won't install an archived object subgraph if the klass of any of the
2143     // referenced objects are replaced. See HeapShared::initialize_from_archived_subgraph().
2144   }
2145 

2420     return false;
2421   }
2422 
2423   if (!open_for_read() || !init_from_file(_fd) || !validate_header()) {
2424     if (_is_static) {
2425       log_info(cds)("Initialize static archive failed.");
2426       return false;
2427     } else {
2428       log_info(cds)("Initialize dynamic archive failed.");
2429       if (AutoCreateSharedArchive) {
2430         CDSConfig::enable_dumping_dynamic_archive();
2431         ArchiveClassesAtExit = CDSConfig::dynamic_archive_path();
2432       }
2433       return false;
2434     }
2435   }
2436 
2437   return true;
2438 }
2439 
2440 bool FileMapInfo::validate_leyden_config() {
2441   // These checks need to be done after FileMapInfo::initialize(), which gets called before Universe::heap()
2442   // is available.
2443   if (header()->has_preloaded_classes()) {
2444     if (JvmtiExport::should_post_class_file_load_hook()) {
2445       log_error(cds)("CDS archive has preloaded classes. It cannot be used when JVMTI ClassFileLoadHook is in use.");
2446       return false;
2447     }
2448     if (JvmtiExport::has_early_vmstart_env()) {
2449       log_error(cds)("CDS archive has preloaded classes. It cannot be used when JVMTI early vm start is in use.");
2450       return false;
2451     }
2452     if (!CDSConfig::is_using_full_module_graph() && !CDSConfig::is_dumping_final_static_archive()) {
2453       log_error(cds)("CDS archive has preloaded classes. It cannot be used when archived full module graph is not used.");
2454       return false;
2455     }
2456     if (header()->gc_kind() != (int)Universe::heap()->kind()) {
2457       log_error(cds)("CDS archive has preloaded classes. It cannot be used because GC used during dump time (%s) is not the same as runtime (%s)",
2458                      header()->gc_name(), Universe::heap()->name());
2459       return false;
2460     }
2461   }
2462 
2463   return true;
2464 }
2465 
2466 // The 2 core spaces are RW->RO
2467 FileMapRegion* FileMapInfo::first_core_region() const {
2468   return region_at(MetaspaceShared::rw);
2469 }
2470 
2471 FileMapRegion* FileMapInfo::last_core_region() const {
2472   return region_at(MetaspaceShared::ro);
2473 }
2474 
2475 void FileMapInfo::print(outputStream* st) const {
2476   header()->print(st);
2477   if (!is_static()) {
2478     dynamic_header()->print(st);
2479   }
2480 }
2481 
2482 void FileMapHeader::set_as_offset(char* p, size_t *offset) {
2483   *offset = ArchiveBuilder::current()->any_to_offset((address)p);
2484 }
2485 

2556   }
2557 
2558   log_info(cds)("Archive was created with UseCompressedOops = %d, UseCompressedClassPointers = %d",
2559                           compressed_oops(), compressed_class_pointers());
2560   if (compressed_oops() != UseCompressedOops || compressed_class_pointers() != UseCompressedClassPointers) {
2561     log_info(cds)("Unable to use shared archive.\nThe saved state of UseCompressedOops and UseCompressedClassPointers is "
2562                                "different from runtime, CDS will be disabled.");
2563     return false;
2564   }
2565 
2566   if (! _use_secondary_supers_table && UseSecondarySupersTable) {
2567     log_warning(cds)("The shared archive was created without UseSecondarySupersTable.");
2568     return false;
2569   }
2570 
2571   if (!_use_optimized_module_handling) {
2572     CDSConfig::stop_using_optimized_module_handling();
2573     log_info(cds)("optimized module handling: disabled because archive was created without optimized module handling");
2574   }
2575 
2576   if (is_static()) {
2577     // Only the static archive can contain the full module graph.
2578     if (!_has_full_module_graph) {
2579       CDSConfig::stop_using_full_module_graph("archive was created without full module graph");
2580     }
2581 
2582     if (_has_archived_invokedynamic) {
2583       CDSConfig::set_is_loading_invokedynamic();
2584     }
2585     if (_has_archived_packages) {
2586       CDSConfig::set_is_loading_packages();
2587     }
2588   }
2589 
2590   return true;
2591 }
2592 
2593 bool FileMapInfo::validate_header() {
2594   if (!header()->validate()) {
2595     return false;
2596   }
2597   if (_is_static) {
2598     return true;
2599   } else {
2600     return DynamicArchive::validate(this);
2601   }
2602 }
2603 
2604 #if INCLUDE_JVMTI
2605 ClassPathEntry** FileMapInfo::_classpath_entries_for_jvmti = nullptr;
2606 
2607 ClassPathEntry* FileMapInfo::get_classpath_entry_for_jvmti(int i, TRAPS) {
< prev index next >