< 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()) {

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

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



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


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



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

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

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

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

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








































































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

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

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


























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

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












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

 287   st->print_cr("- jvm_ident:                      %s", _jvm_ident);
 288   st->print_cr("- shared_path_table_offset:       " SIZE_FORMAT_X, _shared_path_table_offset);
 289   st->print_cr("- app_class_paths_start_index:    %d", _app_class_paths_start_index);
 290   st->print_cr("- app_module_paths_start_index:   %d", _app_module_paths_start_index);
 291   st->print_cr("- num_module_paths:               %d", _num_module_paths);
 292   st->print_cr("- max_used_path_index:            %d", _max_used_path_index);
 293   st->print_cr("- verify_local:                   %d", _verify_local);
 294   st->print_cr("- verify_remote:                  %d", _verify_remote);
 295   st->print_cr("- has_platform_or_app_classes:    %d", _has_platform_or_app_classes);
 296   st->print_cr("- has_non_jar_in_classpath:       %d", _has_non_jar_in_classpath);
 297   st->print_cr("- requested_base_address:         " INTPTR_FORMAT, p2i(_requested_base_address));
 298   st->print_cr("- mapped_base_address:            " INTPTR_FORMAT, p2i(_mapped_base_address));
 299   st->print_cr("- heap_roots_offset:              " SIZE_FORMAT, _heap_roots_offset);
 300   st->print_cr("- _heap_oopmap_start_pos:         " SIZE_FORMAT, _heap_oopmap_start_pos);
 301   st->print_cr("- _heap_ptrmap_start_pos:         " SIZE_FORMAT, _heap_ptrmap_start_pos);
 302   st->print_cr("- _rw_ptrmap_start_pos:           " SIZE_FORMAT, _rw_ptrmap_start_pos);
 303   st->print_cr("- _ro_ptrmap_start_pos:           " SIZE_FORMAT, _ro_ptrmap_start_pos);
 304   st->print_cr("- allow_archiving_with_java_agent:%d", _allow_archiving_with_java_agent);
 305   st->print_cr("- use_optimized_module_handling:  %d", _use_optimized_module_handling);
 306   st->print_cr("- has_full_module_graph           %d", _has_full_module_graph);
 307   st->print_cr("- has_archived_invokedynamic      %d", _has_archived_invokedynamic);
 308   st->print_cr("- has_archived_packages           %d", _has_archived_packages);
 309   st->print_cr("- has_archived_protection_domains %d", _has_archived_protection_domains);
 310   st->print_cr("- ptrmap_size_in_bits:            " SIZE_FORMAT, _ptrmap_size_in_bits);
 311 }
 312 
 313 void SharedClassPathEntry::init_as_non_existent(const char* path, TRAPS) {
 314   _type = non_existent_entry;
 315   set_name(path, CHECK);
 316 }
 317 
 318 void SharedClassPathEntry::init(bool is_modules_image,
 319                                 bool is_module_path,
 320                                 ClassPathEntry* cpe, TRAPS) {
 321   assert(CDSConfig::is_dumping_archive(), "sanity");
 322   _timestamp = 0;
 323   _filesize  = 0;
 324   _from_class_path_attr = false;
 325 
 326   struct stat st;
 327   if (os::stat(cpe->name(), &st) == 0) {
 328     if ((st.st_mode & S_IFMT) == S_IFDIR) {
 329       _type = dir_entry;
 330     } else {

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

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

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

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

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

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

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

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

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