< prev index next >

src/hotspot/share/cds/filemap.cpp

Print this page

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




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

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



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

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



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

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

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



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


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



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

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

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

1927     char* base = map_memory(_fd, _full_path, r->file_offset(),
1928                             requested_addr, size, r->read_only(),
1929                             r->allow_exec(), mtClassShared);
1930     if (base != requested_addr) {
1931       log_info(cds)("Unable to map %s shared space at " INTPTR_FORMAT,
1932                     shared_region_name[i], p2i(requested_addr));
1933       _memory_mapping_failed = true;
1934       return MAP_ARCHIVE_MMAP_FAILURE;
1935     }
1936 
1937     if (VerifySharedSpaces && !r->check_region_crc(requested_addr)) {
1938       return MAP_ARCHIVE_OTHER_FAILURE;
1939     }
1940 
1941     r->set_mapped_from_file(true);
1942     r->set_mapped_base(requested_addr);
1943   }
1944 
1945   if (rs.is_reserved()) {
1946     char* mapped_base = r->mapped_base();
1947     assert(rs.base() <= mapped_base && mapped_base + size <= rs.end(),
1948            PTR_FORMAT " <= " PTR_FORMAT " < " PTR_FORMAT " <= " PTR_FORMAT,

1967     return nullptr;
1968   }
1969 
1970   if (VerifySharedSpaces && !r->check_region_crc(bitmap_base)) {
1971     log_error(cds)("relocation bitmap CRC error");
1972     if (!os::unmap_memory(bitmap_base, r->used_aligned())) {
1973       fatal("os::unmap_memory of relocation bitmap failed");
1974     }
1975     return nullptr;
1976   }
1977 
1978   r->set_mapped_from_file(true);
1979   r->set_mapped_base(bitmap_base);
1980   log_info(cds)("Mapped %s region #%d at base " INTPTR_FORMAT " top " INTPTR_FORMAT " (%s)",
1981                 is_static() ? "static " : "dynamic",
1982                 MetaspaceShared::bm, p2i(r->mapped_base()), p2i(r->mapped_end()),
1983                 shared_region_name[MetaspaceShared::bm]);
1984   return bitmap_base;
1985 }
1986 




















































































1987 class SharedDataRelocationTask : public ArchiveWorkerTask {
1988 private:
1989   BitMapView* const _rw_bm;
1990   BitMapView* const _ro_bm;
1991   SharedDataRelocator* const _rw_reloc;
1992   SharedDataRelocator* const _ro_reloc;
1993 
1994 public:
1995   SharedDataRelocationTask(BitMapView* rw_bm, BitMapView* ro_bm, SharedDataRelocator* rw_reloc, SharedDataRelocator* ro_reloc) :
1996                            ArchiveWorkerTask("Shared Data Relocation"),
1997                            _rw_bm(rw_bm), _ro_bm(ro_bm), _rw_reloc(rw_reloc), _ro_reloc(ro_reloc) {}
1998 
1999   void work(int chunk, int max_chunks) override {
2000     work_on(chunk, max_chunks, _rw_bm, _rw_reloc);
2001     work_on(chunk, max_chunks, _ro_bm, _ro_reloc);
2002   }
2003 
2004   void work_on(int chunk, int max_chunks, BitMapView* bm, SharedDataRelocator* reloc) {
2005     BitMap::idx_t size  = bm->size();
2006     BitMap::idx_t start = MIN2(size, size * chunk / max_chunks);

2116 }
2117 
2118 void FileMapInfo::map_or_load_heap_region() {
2119   bool success = false;
2120 
2121   if (can_use_heap_region()) {
2122     if (ArchiveHeapLoader::can_map()) {
2123       success = map_heap_region();
2124     } else if (ArchiveHeapLoader::can_load()) {
2125       success = ArchiveHeapLoader::load_heap_region(this);
2126     } else {
2127       if (!UseCompressedOops && !ArchiveHeapLoader::can_map()) {
2128         log_info(cds)("Cannot use CDS heap data. Selected GC not compatible -XX:-UseCompressedOops");
2129       } else {
2130         log_info(cds)("Cannot use CDS heap data. UseEpsilonGC, UseG1GC, UseSerialGC, UseParallelGC, or UseShenandoahGC are required.");
2131       }
2132     }
2133   }
2134 
2135   if (!success) {
2136     if (CDSConfig::is_using_aot_linked_classes()) {
2137       // It's too late to recover -- we have already committed to use the archived metaspace objects, but
2138       // the archived heap objects cannot be loaded, so we don't have the archived FMG to guarantee that
2139       // all AOT-linked classes are visible.
2140       //
2141       // We get here because the heap is too small. The app will fail anyway. So let's quit.
2142       MetaspaceShared::unrecoverable_loading_error("CDS archive has aot-linked classes but the archived "
2143                                                    "heap objects cannot be loaded. Try increasing your heap size.");
2144     }
2145     CDSConfig::stop_using_full_module_graph("archive heap loading failed");
2146   }
2147 }
2148 
2149 bool FileMapInfo::can_use_heap_region() {
2150   if (!has_heap_region()) {
2151     return false;
2152   }
2153   if (JvmtiExport::should_post_class_file_load_hook() && JvmtiExport::has_early_class_hook_env()) {
2154     ShouldNotReachHere(); // CDS should have been disabled.
2155     // The archived objects are mapped at JVM start-up, but we don't know if
2156     // j.l.String or j.l.Class might be replaced by the ClassFileLoadHook,

2498       return false;
2499     }
2500   }
2501 
2502   return true;
2503 }
2504 
2505 bool FileMapInfo::validate_aot_class_linking() {
2506   // These checks need to be done after FileMapInfo::initialize(), which gets called before Universe::heap()
2507   // is available.
2508   if (header()->has_aot_linked_classes()) {
2509     CDSConfig::set_has_aot_linked_classes(true);
2510     if (JvmtiExport::should_post_class_file_load_hook()) {
2511       log_error(cds)("CDS archive has aot-linked classes. It cannot be used when JVMTI ClassFileLoadHook is in use.");
2512       return false;
2513     }
2514     if (JvmtiExport::has_early_vmstart_env()) {
2515       log_error(cds)("CDS archive has aot-linked classes. It cannot be used when JVMTI early vm start is in use.");
2516       return false;
2517     }
2518     if (!CDSConfig::is_using_full_module_graph()) {
2519       log_error(cds)("CDS archive has aot-linked classes. It cannot be used when archived full module graph is not used.");
2520       return false;
2521     }
2522 
2523     const char* prop = Arguments::get_property("java.security.manager");
2524     if (prop != nullptr && strcmp(prop, "disallow") != 0) {
2525       log_error(cds)("CDS archive has aot-linked classes. It cannot be used with -Djava.security.manager=%s.", prop);
2526       return false;
2527     }





2528   }
2529 
2530   return true;
2531 }
2532 
2533 // The 2 core spaces are RW->RO
2534 FileMapRegion* FileMapInfo::first_core_region() const {
2535   return region_at(MetaspaceShared::rw);
2536 }
2537 
2538 FileMapRegion* FileMapInfo::last_core_region() const {
2539   return region_at(MetaspaceShared::ro);
2540 }
2541 
2542 void FileMapInfo::print(outputStream* st) const {
2543   header()->print(st);
2544   if (!is_static()) {
2545     dynamic_header()->print(st);
2546   }
2547 }

2626     log_warning(cds)("This archive was created with AllowArchivingWithJavaAgent. It should be used "
2627             "for testing purposes only and should not be used in a production environment");
2628   }
2629 
2630   log_info(cds)("Archive was created with UseCompressedOops = %d, UseCompressedClassPointers = %d, UseCompactObjectHeaders = %d",
2631                           compressed_oops(), compressed_class_pointers(), compact_headers());
2632   if (compressed_oops() != UseCompressedOops || compressed_class_pointers() != UseCompressedClassPointers) {
2633     log_warning(cds)("Unable to use shared archive.\nThe saved state of UseCompressedOops and UseCompressedClassPointers is "
2634                                "different from runtime, CDS will be disabled.");
2635     return false;
2636   }
2637 
2638   if (compact_headers() != UseCompactObjectHeaders) {
2639     log_warning(cds)("Unable to use shared archive.\nThe shared archive file's UseCompactObjectHeaders setting (%s)"
2640                      " does not equal the current UseCompactObjectHeaders setting (%s).",
2641                      _compact_headers          ? "enabled" : "disabled",
2642                      UseCompactObjectHeaders   ? "enabled" : "disabled");
2643     return false;
2644   }
2645 
2646   if (!_use_optimized_module_handling) {
2647     CDSConfig::stop_using_optimized_module_handling();
2648     log_info(cds)("optimized module handling: disabled because archive was created without optimized module handling");
2649   }
2650 
2651   if (is_static()) {
2652     // Only the static archive can contain the full module graph.
2653     if (!_has_full_module_graph) {
2654       CDSConfig::stop_using_full_module_graph("archive was created without full module graph");
2655     }
2656 
2657     if (_has_archived_invokedynamic) {
2658       CDSConfig::set_has_archived_invokedynamic();
2659     }






2660   }
2661 
2662   return true;
2663 }
2664 
2665 bool FileMapInfo::validate_header() {
2666   if (!header()->validate()) {
2667     return false;
2668   }
2669   if (_is_static) {
2670     return true;
2671   } else {
2672     return DynamicArchive::validate(this);
2673   }
2674 }
2675 
2676 #if INCLUDE_JVMTI
2677 ClassPathEntry** FileMapInfo::_classpath_entries_for_jvmti = nullptr;
2678 
2679 ClassPathEntry* FileMapInfo::get_classpath_entry_for_jvmti(int i, TRAPS) {

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

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

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

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

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

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

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

1986     return nullptr;
1987   }
1988 
1989   if (VerifySharedSpaces && !r->check_region_crc(bitmap_base)) {
1990     log_error(cds)("relocation bitmap CRC error");
1991     if (!os::unmap_memory(bitmap_base, r->used_aligned())) {
1992       fatal("os::unmap_memory of relocation bitmap failed");
1993     }
1994     return nullptr;
1995   }
1996 
1997   r->set_mapped_from_file(true);
1998   r->set_mapped_base(bitmap_base);
1999   log_info(cds)("Mapped %s region #%d at base " INTPTR_FORMAT " top " INTPTR_FORMAT " (%s)",
2000                 is_static() ? "static " : "dynamic",
2001                 MetaspaceShared::bm, p2i(r->mapped_base()), p2i(r->mapped_end()),
2002                 shared_region_name[MetaspaceShared::bm]);
2003   return bitmap_base;
2004 }
2005 
2006 bool FileMapInfo::map_cached_code_region(ReservedSpace rs) {
2007   FileMapRegion* r = region_at(MetaspaceShared::cc);
2008   assert(r->used() > 0 && r->used_aligned() == rs.size(), "must be");
2009 
2010   char* requested_base = rs.base();
2011   assert(requested_base != nullptr, "should be inside code cache");
2012 
2013   char* mapped_base;
2014   if (MetaspaceShared::use_windows_memory_mapping()) {
2015     if (!read_region(MetaspaceShared::cc, requested_base, r->used_aligned(), /* do_commit = */ true)) {
2016       log_info(cds)("Failed to read cc shared space into reserved space at " INTPTR_FORMAT,
2017                     p2i(requested_base));
2018       return false;
2019     }
2020     mapped_base = requested_base;
2021   } else {
2022     bool read_only = false, allow_exec = false;
2023     mapped_base = map_memory(_fd, _full_path, r->file_offset(),
2024                              requested_base, r->used_aligned(), read_only, allow_exec, mtClassShared);
2025   }
2026   if (mapped_base == nullptr) {
2027     log_info(cds)("failed to map cached code region");
2028     return false;
2029   } else {
2030     assert(mapped_base == requested_base, "must be");
2031     r->set_mapped_from_file(true);
2032     r->set_mapped_base(mapped_base);
2033     relocate_pointers_in_cached_code_region();
2034     log_info(cds)("Mapped static  region #%d at base " INTPTR_FORMAT " top " INTPTR_FORMAT " (%s)",
2035                   MetaspaceShared::cc, p2i(r->mapped_base()), p2i(r->mapped_end()),
2036                   shared_region_name[MetaspaceShared::cc]);
2037     return true;
2038   }
2039 }
2040 
2041 class CachedCodeRelocator: public BitMapClosure {
2042   address _code_requested_base;
2043   address* _patch_base;
2044   intx _code_delta;
2045   intx _metadata_delta;
2046 
2047 public:
2048   CachedCodeRelocator(address code_requested_base, address code_mapped_base,
2049                       intx metadata_delta) {
2050     _code_requested_base = code_requested_base;
2051     _patch_base = (address*)code_mapped_base;
2052     _code_delta = code_mapped_base - code_requested_base;
2053     _metadata_delta = metadata_delta;
2054   }
2055   
2056   bool do_bit(size_t offset) {
2057     address* p = _patch_base + offset;
2058     address requested_ptr = *p;
2059     if (requested_ptr < _code_requested_base) {
2060       *p = requested_ptr + _metadata_delta;
2061     } else {
2062       *p = requested_ptr + _code_delta;
2063     }
2064     return true; // keep iterating
2065   }
2066 };
2067 
2068 void FileMapInfo::relocate_pointers_in_cached_code_region() {
2069   FileMapRegion* r = region_at(MetaspaceShared::cc);
2070   char* bitmap_base = map_bitmap_region();
2071 
2072   BitMapView cc_ptrmap = ptrmap_view(MetaspaceShared::cc);
2073   if (cc_ptrmap.size() == 0) {
2074     return;
2075   }
2076 
2077   address core_regions_requested_base = (address)header()->requested_base_address();
2078   address core_regions_mapped_base = (address)header()->mapped_base_address();
2079   address cc_region_requested_base = core_regions_requested_base + r->mapping_offset();
2080   address cc_region_mapped_base = (address)r->mapped_base();
2081 
2082   size_t max_bits_for_core_regions = pointer_delta(mapped_end(), mapped_base(), // FIXME - renamed to core_regions_mapped_base(), etc
2083                                                    sizeof(address));
2084 
2085   CachedCodeRelocator patcher(cc_region_requested_base, cc_region_mapped_base,
2086                               core_regions_mapped_base - core_regions_requested_base);
2087   cc_ptrmap.iterate(&patcher);
2088 }
2089 
2090 class SharedDataRelocationTask : public ArchiveWorkerTask {
2091 private:
2092   BitMapView* const _rw_bm;
2093   BitMapView* const _ro_bm;
2094   SharedDataRelocator* const _rw_reloc;
2095   SharedDataRelocator* const _ro_reloc;
2096 
2097 public:
2098   SharedDataRelocationTask(BitMapView* rw_bm, BitMapView* ro_bm, SharedDataRelocator* rw_reloc, SharedDataRelocator* ro_reloc) :
2099                            ArchiveWorkerTask("Shared Data Relocation"),
2100                            _rw_bm(rw_bm), _ro_bm(ro_bm), _rw_reloc(rw_reloc), _ro_reloc(ro_reloc) {}
2101 
2102   void work(int chunk, int max_chunks) override {
2103     work_on(chunk, max_chunks, _rw_bm, _rw_reloc);
2104     work_on(chunk, max_chunks, _ro_bm, _ro_reloc);
2105   }
2106 
2107   void work_on(int chunk, int max_chunks, BitMapView* bm, SharedDataRelocator* reloc) {
2108     BitMap::idx_t size  = bm->size();
2109     BitMap::idx_t start = MIN2(size, size * chunk / max_chunks);

2219 }
2220 
2221 void FileMapInfo::map_or_load_heap_region() {
2222   bool success = false;
2223 
2224   if (can_use_heap_region()) {
2225     if (ArchiveHeapLoader::can_map()) {
2226       success = map_heap_region();
2227     } else if (ArchiveHeapLoader::can_load()) {
2228       success = ArchiveHeapLoader::load_heap_region(this);
2229     } else {
2230       if (!UseCompressedOops && !ArchiveHeapLoader::can_map()) {
2231         log_info(cds)("Cannot use CDS heap data. Selected GC not compatible -XX:-UseCompressedOops");
2232       } else {
2233         log_info(cds)("Cannot use CDS heap data. UseEpsilonGC, UseG1GC, UseSerialGC, UseParallelGC, or UseShenandoahGC are required.");
2234       }
2235     }
2236   }
2237 
2238   if (!success) {
2239     if (CDSConfig::is_using_aot_linked_classes() && !CDSConfig::is_dumping_final_static_archive()) {
2240       // It's too late to recover -- we have already committed to use the archived metaspace objects, but
2241       // the archived heap objects cannot be loaded, so we don't have the archived FMG to guarantee that
2242       // all AOT-linked classes are visible.
2243       //
2244       // We get here because the heap is too small. The app will fail anyway. So let's quit.
2245       MetaspaceShared::unrecoverable_loading_error("CDS archive has aot-linked classes but the archived "
2246                                                    "heap objects cannot be loaded. Try increasing your heap size.");
2247     }
2248     CDSConfig::stop_using_full_module_graph("archive heap loading failed");
2249   }
2250 }
2251 
2252 bool FileMapInfo::can_use_heap_region() {
2253   if (!has_heap_region()) {
2254     return false;
2255   }
2256   if (JvmtiExport::should_post_class_file_load_hook() && JvmtiExport::has_early_class_hook_env()) {
2257     ShouldNotReachHere(); // CDS should have been disabled.
2258     // The archived objects are mapped at JVM start-up, but we don't know if
2259     // j.l.String or j.l.Class might be replaced by the ClassFileLoadHook,

2601       return false;
2602     }
2603   }
2604 
2605   return true;
2606 }
2607 
2608 bool FileMapInfo::validate_aot_class_linking() {
2609   // These checks need to be done after FileMapInfo::initialize(), which gets called before Universe::heap()
2610   // is available.
2611   if (header()->has_aot_linked_classes()) {
2612     CDSConfig::set_has_aot_linked_classes(true);
2613     if (JvmtiExport::should_post_class_file_load_hook()) {
2614       log_error(cds)("CDS archive has aot-linked classes. It cannot be used when JVMTI ClassFileLoadHook is in use.");
2615       return false;
2616     }
2617     if (JvmtiExport::has_early_vmstart_env()) {
2618       log_error(cds)("CDS archive has aot-linked classes. It cannot be used when JVMTI early vm start is in use.");
2619       return false;
2620     }
2621     if (!CDSConfig::is_using_full_module_graph() && !CDSConfig::is_dumping_final_static_archive()) {
2622       log_error(cds)("CDS archive has aot-linked classes. It cannot be used when archived full module graph is not used.");
2623       return false;
2624     }
2625 
2626     const char* prop = Arguments::get_property("java.security.manager");
2627     if (prop != nullptr && strcmp(prop, "disallow") != 0) {
2628       log_error(cds)("CDS archive has aot-linked classes. It cannot be used with -Djava.security.manager=%s.", prop);
2629       return false;
2630     }
2631     if (header()->gc_kind() != (int)Universe::heap()->kind()) {
2632       log_error(cds)("CDS archive has aot-linked classes. It cannot be used because GC used during dump time (%s) is not the same as runtime (%s)",
2633                      header()->gc_name(), Universe::heap()->name());
2634       return false;
2635     }
2636   }
2637 
2638   return true;
2639 }
2640 
2641 // The 2 core spaces are RW->RO
2642 FileMapRegion* FileMapInfo::first_core_region() const {
2643   return region_at(MetaspaceShared::rw);
2644 }
2645 
2646 FileMapRegion* FileMapInfo::last_core_region() const {
2647   return region_at(MetaspaceShared::ro);
2648 }
2649 
2650 void FileMapInfo::print(outputStream* st) const {
2651   header()->print(st);
2652   if (!is_static()) {
2653     dynamic_header()->print(st);
2654   }
2655 }

2734     log_warning(cds)("This archive was created with AllowArchivingWithJavaAgent. It should be used "
2735             "for testing purposes only and should not be used in a production environment");
2736   }
2737 
2738   log_info(cds)("Archive was created with UseCompressedOops = %d, UseCompressedClassPointers = %d, UseCompactObjectHeaders = %d",
2739                           compressed_oops(), compressed_class_pointers(), compact_headers());
2740   if (compressed_oops() != UseCompressedOops || compressed_class_pointers() != UseCompressedClassPointers) {
2741     log_warning(cds)("Unable to use shared archive.\nThe saved state of UseCompressedOops and UseCompressedClassPointers is "
2742                                "different from runtime, CDS will be disabled.");
2743     return false;
2744   }
2745 
2746   if (compact_headers() != UseCompactObjectHeaders) {
2747     log_warning(cds)("Unable to use shared archive.\nThe shared archive file's UseCompactObjectHeaders setting (%s)"
2748                      " does not equal the current UseCompactObjectHeaders setting (%s).",
2749                      _compact_headers          ? "enabled" : "disabled",
2750                      UseCompactObjectHeaders   ? "enabled" : "disabled");
2751     return false;
2752   }
2753 
2754   if (!_use_optimized_module_handling && !CDSConfig::is_dumping_final_static_archive()) {
2755     CDSConfig::stop_using_optimized_module_handling();
2756     log_info(cds)("optimized module handling: disabled because archive was created without optimized module handling");
2757   }
2758 
2759   if (is_static()) {
2760     // Only the static archive can contain the full module graph.
2761     if (!_has_full_module_graph) {
2762       CDSConfig::stop_using_full_module_graph("archive was created without full module graph");
2763     }
2764 
2765     if (_has_archived_invokedynamic) {
2766       CDSConfig::set_has_archived_invokedynamic();
2767     }
2768     if (_has_archived_packages) {
2769       CDSConfig::set_is_loading_packages();
2770     }
2771     if (_has_archived_protection_domains) {
2772       CDSConfig::set_is_loading_protection_domains();
2773     }
2774   }
2775 
2776   return true;
2777 }
2778 
2779 bool FileMapInfo::validate_header() {
2780   if (!header()->validate()) {
2781     return false;
2782   }
2783   if (_is_static) {
2784     return true;
2785   } else {
2786     return DynamicArchive::validate(this);
2787   }
2788 }
2789 
2790 #if INCLUDE_JVMTI
2791 ClassPathEntry** FileMapInfo::_classpath_entries_for_jvmti = nullptr;
2792 
2793 ClassPathEntry* FileMapInfo::get_classpath_entry_for_jvmti(int i, TRAPS) {
< prev index next >