< 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;

 284   st->print_cr("- num_module_paths:               %d", _num_module_paths);
 285   st->print_cr("- max_used_path_index:            %d", _max_used_path_index);
 286   st->print_cr("- verify_local:                   %d", _verify_local);
 287   st->print_cr("- verify_remote:                  %d", _verify_remote);
 288   st->print_cr("- has_platform_or_app_classes:    %d", _has_platform_or_app_classes);
 289   st->print_cr("- has_non_jar_in_classpath:       %d", _has_non_jar_in_classpath);
 290   st->print_cr("- requested_base_address:         " INTPTR_FORMAT, p2i(_requested_base_address));
 291   st->print_cr("- mapped_base_address:            " INTPTR_FORMAT, p2i(_mapped_base_address));
 292   st->print_cr("- heap_root_segments.roots_count: %d" , _heap_root_segments.roots_count());
 293   st->print_cr("- heap_root_segments.base_offset: " SIZE_FORMAT_X, _heap_root_segments.base_offset());
 294   st->print_cr("- heap_root_segments.count:       " SIZE_FORMAT, _heap_root_segments.count());
 295   st->print_cr("- heap_root_segments.max_size_elems: %d", _heap_root_segments.max_size_in_elems());
 296   st->print_cr("- heap_root_segments.max_size_bytes: %d", _heap_root_segments.max_size_in_bytes());
 297   st->print_cr("- _heap_oopmap_start_pos:         " SIZE_FORMAT, _heap_oopmap_start_pos);
 298   st->print_cr("- _heap_ptrmap_start_pos:         " SIZE_FORMAT, _heap_ptrmap_start_pos);
 299   st->print_cr("- _rw_ptrmap_start_pos:           " SIZE_FORMAT, _rw_ptrmap_start_pos);
 300   st->print_cr("- _ro_ptrmap_start_pos:           " SIZE_FORMAT, _ro_ptrmap_start_pos);
 301   st->print_cr("- allow_archiving_with_java_agent:%d", _allow_archiving_with_java_agent);
 302   st->print_cr("- use_optimized_module_handling:  %d", _use_optimized_module_handling);
 303   st->print_cr("- has_full_module_graph           %d", _has_full_module_graph);





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

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



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

1031   }
1032 
1033   if (header()->max_used_path_index() == 0) {
1034     // default archive only contains the module image in the bootclasspath
1035     assert(shared_path(0)->is_modules_image(), "first shared_path must be the modules image");
1036   } else {
1037     if (!validate_boot_class_paths() || !validate_app_class_paths(shared_app_paths_len)) {
1038       const char* mismatch_msg = "shared class paths mismatch";
1039       const char* hint_msg = log_is_enabled(Info, class, path) ?
1040           "" : " (hint: enable -Xlog:class+path=info to diagnose the failure)";
1041       if (RequireSharedSpaces) {
1042         log_error(cds)("%s%s", mismatch_msg, hint_msg);
1043         MetaspaceShared::unrecoverable_loading_error();
1044       } else {
1045         log_warning(cds)("%s%s", mismatch_msg, hint_msg);
1046       }
1047       return false;
1048     }
1049   }
1050 
1051   validate_non_existent_class_paths();


1052 
1053   _validating_shared_path_table = false;
1054 
1055 #if INCLUDE_JVMTI
1056   if (_classpath_entries_for_jvmti != nullptr) {
1057     os::free(_classpath_entries_for_jvmti);
1058   }
1059   size_t sz = sizeof(ClassPathEntry*) * get_number_of_shared_paths();
1060   _classpath_entries_for_jvmti = (ClassPathEntry**)os::malloc(sz, mtClass);
1061   memset((void*)_classpath_entries_for_jvmti, 0, sz);
1062 #endif
1063 
1064   return true;
1065 }
1066 
1067 void FileMapInfo::validate_non_existent_class_paths() {
1068   // All of the recorded non-existent paths came from the Class-Path: attribute from the JAR
1069   // files on the app classpath. If any of these are found to exist during runtime,
1070   // it will change how classes are loading for the app loader. For safety, disable
1071   // loading of archived platform/app classes (currently there's no way to disable just the
1072   // app classes).
1073 
1074   assert(CDSConfig::is_using_archive(), "runtime only");
1075   for (int i = header()->app_module_paths_start_index() + header()->num_module_paths();
1076        i < get_number_of_shared_paths();
1077        i++) {
1078     SharedClassPathEntry* ent = shared_path(i);
1079     if (!ent->check_non_existent()) {
1080       log_warning(cds)("Archived non-system classes are disabled because the "
1081               "file %s exists", ent->name());
1082       header()->set_has_platform_or_app_classes(false);






1083     }
1084   }


1085 }
1086 
1087 // A utility class for reading/validating the GenericCDSFileMapHeader portion of
1088 // a CDS archive's header. The file header of all CDS archives with versions from
1089 // CDS_GENERIC_HEADER_SUPPORTED_MIN_VERSION (12) are guaranteed to always start
1090 // with GenericCDSFileMapHeader. This makes it possible to read important information
1091 // from a CDS archive created by a different version of HotSpot, so that we can
1092 // automatically regenerate the archive as necessary (JDK-8261455).
1093 class FileHeaderHelper {
1094   int _fd;
1095   bool _is_valid;
1096   bool _is_static;
1097   GenericCDSFileMapHeader* _header;
1098   const char* _archive_name;
1099   const char* _base_archive_name;
1100 
1101 public:
1102   FileHeaderHelper(const char* archive_name, bool is_static) {
1103     _fd = -1;
1104     _is_valid = false;

1493 bool FileMapRegion::check_region_crc(char* base) const {
1494   // This function should be called after the region has been properly
1495   // loaded into memory via FileMapInfo::map_region() or FileMapInfo::read_region().
1496   // I.e., this->mapped_base() must be valid.
1497   size_t sz = used();
1498   if (sz == 0) {
1499     return true;
1500   }
1501 
1502   assert(base != nullptr, "must be initialized");
1503   int crc = ClassLoader::crc32(0, base, (jint)sz);
1504   if (crc != this->crc()) {
1505     log_warning(cds)("Checksum verification failed.");
1506     return false;
1507   }
1508   return true;
1509 }
1510 
1511 static const char* region_name(int region_index) {
1512   static const char* names[] = {
1513     "rw", "ro", "bm", "hp"
1514   };
1515   const int num_regions = sizeof(names)/sizeof(names[0]);
1516   assert(0 <= region_index && region_index < num_regions, "sanity");
1517 
1518   return names[region_index];
1519 }
1520 
1521 BitMapView FileMapInfo::bitmap_view(int region_index, bool is_oopmap) {
1522   FileMapRegion* r = region_at(region_index);
1523   char* bitmap_base = is_static() ? FileMapInfo::current_info()->map_bitmap_region() : FileMapInfo::dynamic_info()->map_bitmap_region();
1524   bitmap_base += is_oopmap ? r->oopmap_offset() : r->ptrmap_offset();
1525   size_t size_in_bits = is_oopmap ? r->oopmap_size_in_bits() : r->ptrmap_size_in_bits();
1526 
1527   log_debug(cds, reloc)("mapped %s relocation %smap @ " INTPTR_FORMAT " (" SIZE_FORMAT " bits)",
1528                         region_name(region_index), is_oopmap ? "oop" : "ptr",
1529                         p2i(bitmap_base), size_in_bits);
1530 
1531   return BitMapView((BitMap::bm_word_t*)(bitmap_base), size_in_bits);
1532 }
1533 

1579       mapping_offset = (size_t)((address)requested_base - CompressedOops::base());
1580       assert((mapping_offset >> CompressedOops::shift()) << CompressedOops::shift() == mapping_offset, "must be");
1581     } else {
1582       mapping_offset = 0; // not used with !UseCompressedOops
1583     }
1584 #endif // INCLUDE_CDS_JAVA_HEAP
1585   } else {
1586     char* requested_SharedBaseAddress = (char*)MetaspaceShared::requested_base_address();
1587     requested_base = ArchiveBuilder::current()->to_requested(base);
1588     assert(requested_base >= requested_SharedBaseAddress, "must be");
1589     mapping_offset = requested_base - requested_SharedBaseAddress;
1590   }
1591 
1592   r->set_file_offset(_file_offset);
1593   int crc = ClassLoader::crc32(0, base, (jint)size);
1594   if (size > 0) {
1595     log_info(cds)("Shared file region (%s) %d: " SIZE_FORMAT_W(8)
1596                    " bytes, addr " INTPTR_FORMAT " file offset 0x%08" PRIxPTR
1597                    " crc 0x%08x",
1598                    region_name(region), region, size, p2i(requested_base), _file_offset, crc);



1599   }
1600 
1601   r->init(region, mapping_offset, size, read_only, allow_exec, crc);
1602 
1603   if (base != nullptr) {
1604     write_bytes_aligned(base, size);
1605   }
1606 }
1607 
1608 static size_t write_bitmap(const CHeapBitMap* map, char* output, size_t offset) {
1609   size_t size_in_bytes = map->size_in_bytes();
1610   map->write_to((BitMap::bm_word_t*)(output + offset), size_in_bytes);
1611   return offset + size_in_bytes;
1612 }
1613 
1614 // The sorting code groups the objects with non-null oop/ptrs together.
1615 // Relevant bitmaps then have lots of leading and trailing zeros, which
1616 // we do not have to store.
1617 size_t FileMapInfo::remove_bitmap_zeros(CHeapBitMap* map) {
1618   BitMap::idx_t first_set = map->find_first_set_bit(0);
1619   BitMap::idx_t last_set  = map->find_last_set_bit(0);
1620   size_t old_size = map->size();
1621 
1622   // Slice and resize bitmap
1623   map->truncate(first_set, last_set + 1);
1624 
1625   assert(map->at(0), "First bit should be set");
1626   assert(map->at(map->size() - 1), "Last bit should be set");
1627   assert(map->size() <= old_size, "sanity");
1628 
1629   return first_set;
1630 }
1631 
1632 char* FileMapInfo::write_bitmap_region(CHeapBitMap* rw_ptrmap, CHeapBitMap* ro_ptrmap, ArchiveHeapInfo* heap_info,


1633                                        size_t &size_in_bytes) {
1634   size_t removed_rw_leading_zeros = remove_bitmap_zeros(rw_ptrmap);
1635   size_t removed_ro_leading_zeros = remove_bitmap_zeros(ro_ptrmap);
1636   header()->set_rw_ptrmap_start_pos(removed_rw_leading_zeros);
1637   header()->set_ro_ptrmap_start_pos(removed_ro_leading_zeros);
1638   size_in_bytes = rw_ptrmap->size_in_bytes() + ro_ptrmap->size_in_bytes();
1639 
1640   if (heap_info->is_used()) {
1641     // Remove leading and trailing zeros
1642     size_t removed_oop_leading_zeros = remove_bitmap_zeros(heap_info->oopmap());
1643     size_t removed_ptr_leading_zeros = remove_bitmap_zeros(heap_info->ptrmap());
1644     header()->set_heap_oopmap_start_pos(removed_oop_leading_zeros);
1645     header()->set_heap_ptrmap_start_pos(removed_ptr_leading_zeros);
1646 
1647     size_in_bytes += heap_info->oopmap()->size_in_bytes();
1648     size_in_bytes += heap_info->ptrmap()->size_in_bytes();
1649   }
1650 
1651   // The bitmap region contains up to 4 parts:
1652   // rw_ptrmap:           metaspace pointers inside the read-write region
1653   // ro_ptrmap:           metaspace pointers inside the read-only region
1654   // heap_info->oopmap(): Java oop pointers in the heap region
1655   // heap_info->ptrmap(): metaspace pointers in the heap region
1656   char* buffer = NEW_C_HEAP_ARRAY(char, size_in_bytes, mtClassShared);
1657   size_t written = 0;
1658 
1659   region_at(MetaspaceShared::rw)->init_ptrmap(0, rw_ptrmap->size());
1660   written = write_bitmap(rw_ptrmap, buffer, written);
1661 
1662   region_at(MetaspaceShared::ro)->init_ptrmap(written, ro_ptrmap->size());
1663   written = write_bitmap(ro_ptrmap, buffer, written);
1664 



1665   if (heap_info->is_used()) {
1666     FileMapRegion* r = region_at(MetaspaceShared::hp);
1667 
1668     r->init_oopmap(written, heap_info->oopmap()->size());
1669     written = write_bitmap(heap_info->oopmap(), buffer, written);
1670 
1671     r->init_ptrmap(written, heap_info->ptrmap()->size());
1672     written = write_bitmap(heap_info->ptrmap(), buffer, written);
1673   }
1674 
1675   write_region(MetaspaceShared::bm, (char*)buffer, size_in_bytes, /*read_only=*/true, /*allow_exec=*/false);
1676   return buffer;
1677 }
1678 
1679 size_t FileMapInfo::write_heap_region(ArchiveHeapInfo* heap_info) {
1680   char* buffer_start = heap_info->buffer_start();
1681   size_t buffer_size = heap_info->buffer_byte_size();
1682   write_region(MetaspaceShared::hp, buffer_start, buffer_size, false, false);
1683   header()->set_heap_root_segments(heap_info->heap_root_segments());
1684   return buffer_size;

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

1876     r->set_read_only(false); // Need to patch the pointers
1877   }
1878 
1879   if (MetaspaceShared::use_windows_memory_mapping() && rs.is_reserved()) {
1880     // This is the second time we try to map the archive(s). We have already created a ReservedSpace
1881     // that covers all the FileMapRegions to ensure all regions can be mapped. However, Windows
1882     // can't mmap into a ReservedSpace, so we just ::read() the data. We're going to patch all the
1883     // regions anyway, so there's no benefit for mmap anyway.
1884     if (!read_region(i, requested_addr, size, /* do_commit = */ true)) {
1885       log_info(cds)("Failed to read %s shared space into reserved space at " INTPTR_FORMAT,
1886                     shared_region_name[i], p2i(requested_addr));
1887       return MAP_ARCHIVE_OTHER_FAILURE; // oom or I/O error.
1888     } else {
1889       assert(r->mapped_base() != nullptr, "must be initialized");
1890       return MAP_ARCHIVE_SUCCESS;
1891     }
1892   } else {
1893     // Note that this may either be a "fresh" mapping into unreserved address
1894     // space (Windows, first mapping attempt), or a mapping into pre-reserved
1895     // space (Posix). See also comment in MetaspaceShared::map_archives().

1896     char* base = map_memory(_fd, _full_path, r->file_offset(),
1897                             requested_addr, size, r->read_only(),
1898                             r->allow_exec(), mtClassShared);
1899     if (base != requested_addr) {
1900       log_info(cds)("Unable to map %s shared space at " INTPTR_FORMAT,
1901                     shared_region_name[i], p2i(requested_addr));
1902       _memory_mapping_failed = true;
1903       return MAP_ARCHIVE_MMAP_FAILURE;
1904     }
1905 
1906     if (VerifySharedSpaces && !r->check_region_crc(requested_addr)) {
1907       return MAP_ARCHIVE_OTHER_FAILURE;
1908     }
1909 
1910     r->set_mapped_from_file(true);
1911     r->set_mapped_base(requested_addr);
1912 
1913     return MAP_ARCHIVE_SUCCESS;
1914   }
1915 }
1916 
1917 // The return value is the location of the archive relocation bitmap.

1929     return nullptr;
1930   }
1931 
1932   if (VerifySharedSpaces && !r->check_region_crc(bitmap_base)) {
1933     log_error(cds)("relocation bitmap CRC error");
1934     if (!os::unmap_memory(bitmap_base, r->used_aligned())) {
1935       fatal("os::unmap_memory of relocation bitmap failed");
1936     }
1937     return nullptr;
1938   }
1939 
1940   r->set_mapped_from_file(true);
1941   r->set_mapped_base(bitmap_base);
1942   log_info(cds)("Mapped %s region #%d at base " INTPTR_FORMAT " top " INTPTR_FORMAT " (%s)",
1943                 is_static() ? "static " : "dynamic",
1944                 MetaspaceShared::bm, p2i(r->mapped_base()), p2i(r->mapped_end()),
1945                 shared_region_name[MetaspaceShared::bm]);
1946   return bitmap_base;
1947 }
1948 








































































1949 // This is called when we cannot map the archive at the requested[ base address (usually 0x800000000).
1950 // We relocate all pointers in the 2 core regions (ro, rw).
1951 bool FileMapInfo::relocate_pointers_in_core_regions(intx addr_delta) {
1952   log_debug(cds, reloc)("runtime archive relocation start");
1953   char* bitmap_base = map_bitmap_region();
1954 
1955   if (bitmap_base == nullptr) {
1956     return false; // OOM, or CRC check failure
1957   } else {
1958     BitMapView rw_ptrmap = ptrmap_view(MetaspaceShared::rw);
1959     BitMapView ro_ptrmap = ptrmap_view(MetaspaceShared::ro);
1960 
1961     FileMapRegion* rw_region = first_core_region();
1962     FileMapRegion* ro_region = last_core_region();
1963 
1964     // Patch all pointers inside the RW region
1965     address rw_patch_base = (address)rw_region->mapped_base();
1966     address rw_patch_end  = (address)rw_region->mapped_end();
1967 
1968     // Patch all pointers inside the RO region

2046 
2047 void FileMapInfo::map_or_load_heap_region() {
2048   bool success = false;
2049 
2050   if (can_use_heap_region()) {
2051     if (ArchiveHeapLoader::can_map()) {
2052       success = map_heap_region();
2053     } else if (ArchiveHeapLoader::can_load()) {
2054       success = ArchiveHeapLoader::load_heap_region(this);
2055     } else {
2056       if (!UseCompressedOops && !ArchiveHeapLoader::can_map()) {
2057         // TODO - remove implicit knowledge of G1
2058         log_info(cds)("Cannot use CDS heap data. UseG1GC is required for -XX:-UseCompressedOops");
2059       } else {
2060         log_info(cds)("Cannot use CDS heap data. UseEpsilonGC, UseG1GC, UseSerialGC, UseParallelGC, or UseShenandoahGC are required.");
2061       }
2062     }
2063   }
2064 
2065   if (!success) {
2066     CDSConfig::stop_using_full_module_graph();









2067   }
2068 }
2069 
2070 bool FileMapInfo::can_use_heap_region() {
2071   if (!has_heap_region()) {
2072     return false;
2073   }
2074   if (JvmtiExport::should_post_class_file_load_hook() && JvmtiExport::has_early_class_hook_env()) {
2075     ShouldNotReachHere(); // CDS should have been disabled.
2076     // The archived objects are mapped at JVM start-up, but we don't know if
2077     // j.l.String or j.l.Class might be replaced by the ClassFileLoadHook,
2078     // which would make the archived String or mirror objects invalid. Let's be safe and not
2079     // use the archived objects. These 2 classes are loaded during the JVMTI "early" stage.
2080     //
2081     // If JvmtiExport::has_early_class_hook_env() is false, the classes of some objects
2082     // in the archived subgraphs may be replaced by the ClassFileLoadHook. But that's OK
2083     // because we won't install an archived object subgraph if the klass of any of the
2084     // referenced objects are replaced. See HeapShared::initialize_from_archived_subgraph().
2085   }
2086 

2375     return false;
2376   }
2377 
2378   if (!open_for_read() || !init_from_file(_fd) || !validate_header()) {
2379     if (_is_static) {
2380       log_info(cds)("Initialize static archive failed.");
2381       return false;
2382     } else {
2383       log_info(cds)("Initialize dynamic archive failed.");
2384       if (AutoCreateSharedArchive) {
2385         CDSConfig::enable_dumping_dynamic_archive();
2386         ArchiveClassesAtExit = CDSConfig::dynamic_archive_path();
2387       }
2388       return false;
2389     }
2390   }
2391 
2392   return true;
2393 }
2394 



























2395 // The 2 core spaces are RW->RO
2396 FileMapRegion* FileMapInfo::first_core_region() const {
2397   return region_at(MetaspaceShared::rw);
2398 }
2399 
2400 FileMapRegion* FileMapInfo::last_core_region() const {
2401   return region_at(MetaspaceShared::ro);
2402 }
2403 
2404 void FileMapInfo::print(outputStream* st) const {
2405   header()->print(st);
2406   if (!is_static()) {
2407     dynamic_header()->print(st);
2408   }
2409 }
2410 
2411 void FileMapHeader::set_as_offset(char* p, size_t *offset) {
2412   *offset = ArchiveBuilder::current()->any_to_offset((address)p);
2413 }
2414 

2480   }
2481 
2482   if (_allow_archiving_with_java_agent) {
2483     log_warning(cds)("This archive was created with AllowArchivingWithJavaAgent. It should be used "
2484             "for testing purposes only and should not be used in a production environment");
2485   }
2486 
2487   log_info(cds)("Archive was created with UseCompressedOops = %d, UseCompressedClassPointers = %d",
2488                           compressed_oops(), compressed_class_pointers());
2489   if (compressed_oops() != UseCompressedOops || compressed_class_pointers() != UseCompressedClassPointers) {
2490     log_info(cds)("Unable to use shared archive.\nThe saved state of UseCompressedOops and UseCompressedClassPointers is "
2491                                "different from runtime, CDS will be disabled.");
2492     return false;
2493   }
2494 
2495   if (! _use_secondary_supers_table && UseSecondarySupersTable) {
2496     log_warning(cds)("The shared archive was created without UseSecondarySupersTable.");
2497     return false;
2498   }
2499 
2500   if (!_use_optimized_module_handling) {
2501     CDSConfig::stop_using_optimized_module_handling();
2502     log_info(cds)("optimized module handling: disabled because archive was created without optimized module handling");
2503   }
2504 
2505   if (is_static() && !_has_full_module_graph) {
2506     // Only the static archive can contain the full module graph.
2507     CDSConfig::stop_using_full_module_graph("archive was created without full module graph");












2508   }
2509 
2510   return true;
2511 }
2512 
2513 bool FileMapInfo::validate_header() {
2514   if (!header()->validate()) {
2515     return false;
2516   }
2517   if (_is_static) {
2518     return true;
2519   } else {
2520     return DynamicArchive::validate(this);
2521   }
2522 }
2523 
2524 #if INCLUDE_JVMTI
2525 ClassPathEntry** FileMapInfo::_classpath_entries_for_jvmti = nullptr;
2526 
2527 ClassPathEntry* FileMapInfo::get_classpath_entry_for_jvmti(int i, TRAPS) {

 197   set_magic(CDSConfig::is_dumping_dynamic_archive() ? CDS_DYNAMIC_ARCHIVE_MAGIC : CDS_ARCHIVE_MAGIC);
 198   set_version(CURRENT_CDS_ARCHIVE_VERSION);
 199 
 200   if (!info->is_static() && base_archive_name_size != 0) {
 201     // copy base archive name
 202     copy_base_archive_name(CDSConfig::static_archive_path());
 203   }
 204   _core_region_alignment = core_region_alignment;
 205   _obj_alignment = ObjectAlignmentInBytes;
 206   _compact_strings = CompactStrings;
 207   if (CDSConfig::is_dumping_heap()) {
 208     _narrow_oop_mode = CompressedOops::mode();
 209     _narrow_oop_base = CompressedOops::base();
 210     _narrow_oop_shift = CompressedOops::shift();
 211   }
 212   _compressed_oops = UseCompressedOops;
 213   _compressed_class_ptrs = UseCompressedClassPointers;
 214   _use_secondary_supers_table = UseSecondarySupersTable;
 215   _max_heap_size = MaxHeapSize;
 216   _use_optimized_module_handling = CDSConfig::is_using_optimized_module_handling();
 217   _has_aot_linked_classes = CDSConfig::is_dumping_aot_linked_classes();
 218   _has_full_module_graph = CDSConfig::is_dumping_full_module_graph();
 219   _has_archived_invokedynamic = CDSConfig::is_dumping_invokedynamic();
 220   _has_archived_packages = CDSConfig::is_dumping_packages();
 221   _has_archived_protection_domains = CDSConfig::is_dumping_protection_domains();
 222   _gc_kind = (int)Universe::heap()->kind();
 223   jio_snprintf(_gc_name, sizeof(_gc_name), Universe::heap()->name());
 224 
 225   // The following fields are for sanity checks for whether this archive
 226   // will function correctly with this JVM and the bootclasspath it's
 227   // invoked with.
 228 
 229   // JVM version string ... changes on each build.
 230   get_header_version(_jvm_ident);
 231 
 232   _app_class_paths_start_index = ClassLoaderExt::app_class_paths_start_index();
 233   _app_module_paths_start_index = ClassLoaderExt::app_module_paths_start_index();
 234   _max_used_path_index = ClassLoaderExt::max_used_path_index();
 235   _num_module_paths = ClassLoader::num_module_path_entries();
 236 
 237   _verify_local = BytecodeVerificationLocal;
 238   _verify_remote = BytecodeVerificationRemote;
 239   _has_platform_or_app_classes = ClassLoaderExt::has_platform_or_app_classes();
 240   _has_non_jar_in_classpath = ClassLoaderExt::has_non_jar_in_classpath();
 241   _requested_base_address = (char*)SharedBaseAddress;
 242   _mapped_base_address = (char*)SharedBaseAddress;
 243   _allow_archiving_with_java_agent = AllowArchivingWithJavaAgent;

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

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

1045   }
1046 
1047   if (header()->max_used_path_index() == 0) {
1048     // default archive only contains the module image in the bootclasspath
1049     assert(shared_path(0)->is_modules_image(), "first shared_path must be the modules image");
1050   } else {
1051     if (!validate_boot_class_paths() || !validate_app_class_paths(shared_app_paths_len)) {
1052       const char* mismatch_msg = "shared class paths mismatch";
1053       const char* hint_msg = log_is_enabled(Info, class, path) ?
1054           "" : " (hint: enable -Xlog:class+path=info to diagnose the failure)";
1055       if (RequireSharedSpaces) {
1056         log_error(cds)("%s%s", mismatch_msg, hint_msg);
1057         MetaspaceShared::unrecoverable_loading_error();
1058       } else {
1059         log_warning(cds)("%s%s", mismatch_msg, hint_msg);
1060       }
1061       return false;
1062     }
1063   }
1064 
1065   if (!validate_non_existent_class_paths()) {
1066     return false;
1067   }
1068 
1069   _validating_shared_path_table = false;
1070 
1071 #if INCLUDE_JVMTI
1072   if (_classpath_entries_for_jvmti != nullptr) {
1073     os::free(_classpath_entries_for_jvmti);
1074   }
1075   size_t sz = sizeof(ClassPathEntry*) * get_number_of_shared_paths();
1076   _classpath_entries_for_jvmti = (ClassPathEntry**)os::malloc(sz, mtClass);
1077   memset((void*)_classpath_entries_for_jvmti, 0, sz);
1078 #endif
1079 
1080   return true;
1081 }
1082 
1083 bool FileMapInfo::validate_non_existent_class_paths() {
1084   // All of the recorded non-existent paths came from the Class-Path: attribute from the JAR
1085   // files on the app classpath. If any of these are found to exist during runtime,
1086   // it will change how classes are loading for the app loader. For safety, disable
1087   // loading of archived platform/app classes (currently there's no way to disable just the
1088   // app classes).
1089 
1090   assert(CDSConfig::is_using_archive(), "runtime only");
1091   for (int i = header()->app_module_paths_start_index() + header()->num_module_paths();
1092        i < get_number_of_shared_paths();
1093        i++) {
1094     SharedClassPathEntry* ent = shared_path(i);
1095     if (!ent->check_non_existent()) {
1096       if (header()->has_aot_linked_classes()) {
1097         log_error(cds)("CDS archive has aot-linked classes. It cannot be used because the "
1098                        "file %s exists", ent->name());
1099         return false;
1100       } else {
1101         log_warning(cds)("Archived non-system classes are disabled because the "
1102                          "file %s exists", ent->name());
1103         header()->set_has_platform_or_app_classes(false);
1104       }
1105     }
1106   }
1107 
1108   return true;
1109 }
1110 
1111 // A utility class for reading/validating the GenericCDSFileMapHeader portion of
1112 // a CDS archive's header. The file header of all CDS archives with versions from
1113 // CDS_GENERIC_HEADER_SUPPORTED_MIN_VERSION (12) are guaranteed to always start
1114 // with GenericCDSFileMapHeader. This makes it possible to read important information
1115 // from a CDS archive created by a different version of HotSpot, so that we can
1116 // automatically regenerate the archive as necessary (JDK-8261455).
1117 class FileHeaderHelper {
1118   int _fd;
1119   bool _is_valid;
1120   bool _is_static;
1121   GenericCDSFileMapHeader* _header;
1122   const char* _archive_name;
1123   const char* _base_archive_name;
1124 
1125 public:
1126   FileHeaderHelper(const char* archive_name, bool is_static) {
1127     _fd = -1;
1128     _is_valid = false;

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

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

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

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

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

2151 
2152 void FileMapInfo::map_or_load_heap_region() {
2153   bool success = false;
2154 
2155   if (can_use_heap_region()) {
2156     if (ArchiveHeapLoader::can_map()) {
2157       success = map_heap_region();
2158     } else if (ArchiveHeapLoader::can_load()) {
2159       success = ArchiveHeapLoader::load_heap_region(this);
2160     } else {
2161       if (!UseCompressedOops && !ArchiveHeapLoader::can_map()) {
2162         // TODO - remove implicit knowledge of G1
2163         log_info(cds)("Cannot use CDS heap data. UseG1GC is required for -XX:-UseCompressedOops");
2164       } else {
2165         log_info(cds)("Cannot use CDS heap data. UseEpsilonGC, UseG1GC, UseSerialGC, UseParallelGC, or UseShenandoahGC are required.");
2166       }
2167     }
2168   }
2169 
2170   if (!success) {
2171     if (CDSConfig::is_using_aot_linked_classes() && !CDSConfig::is_dumping_final_static_archive()) {
2172       // It's too later to recover -- we have already committed to use the archived metaspace objects, but
2173       // the archived heap objects cannot be loaded, so we don't have the archived FMG to guarantee that
2174       // all AOT-linked classes are visible.
2175       //
2176       // We get here because the heap is too small. The app will fail anyway. So let's quit.
2177       MetaspaceShared::unrecoverable_loading_error("CDS archive has aot-linked classes but the archived "
2178                                                    "heap objects cannot be loaded. Try increasing your heap size.");
2179     }
2180     CDSConfig::stop_using_full_module_graph("archive heap loading failed");
2181   }
2182 }
2183 
2184 bool FileMapInfo::can_use_heap_region() {
2185   if (!has_heap_region()) {
2186     return false;
2187   }
2188   if (JvmtiExport::should_post_class_file_load_hook() && JvmtiExport::has_early_class_hook_env()) {
2189     ShouldNotReachHere(); // CDS should have been disabled.
2190     // The archived objects are mapped at JVM start-up, but we don't know if
2191     // j.l.String or j.l.Class might be replaced by the ClassFileLoadHook,
2192     // which would make the archived String or mirror objects invalid. Let's be safe and not
2193     // use the archived objects. These 2 classes are loaded during the JVMTI "early" stage.
2194     //
2195     // If JvmtiExport::has_early_class_hook_env() is false, the classes of some objects
2196     // in the archived subgraphs may be replaced by the ClassFileLoadHook. But that's OK
2197     // because we won't install an archived object subgraph if the klass of any of the
2198     // referenced objects are replaced. See HeapShared::initialize_from_archived_subgraph().
2199   }
2200 

2489     return false;
2490   }
2491 
2492   if (!open_for_read() || !init_from_file(_fd) || !validate_header()) {
2493     if (_is_static) {
2494       log_info(cds)("Initialize static archive failed.");
2495       return false;
2496     } else {
2497       log_info(cds)("Initialize dynamic archive failed.");
2498       if (AutoCreateSharedArchive) {
2499         CDSConfig::enable_dumping_dynamic_archive();
2500         ArchiveClassesAtExit = CDSConfig::dynamic_archive_path();
2501       }
2502       return false;
2503     }
2504   }
2505 
2506   return true;
2507 }
2508 
2509 bool FileMapInfo::validate_aot_class_linking() {
2510   // These checks need to be done after FileMapInfo::initialize(), which gets called before Universe::heap()
2511   // is available.
2512   if (header()->has_aot_linked_classes()) {
2513     CDSConfig::set_has_aot_linked_classes(is_static(), true);
2514     if (JvmtiExport::should_post_class_file_load_hook()) {
2515       log_error(cds)("CDS archive has aot-linked classes. It cannot be used when JVMTI ClassFileLoadHook is in use.");
2516       return false;
2517     }
2518     if (JvmtiExport::has_early_vmstart_env()) {
2519       log_error(cds)("CDS archive has aot-linked classes. It cannot be used when JVMTI early vm start is in use.");
2520       return false;
2521     }
2522     if (!CDSConfig::is_using_full_module_graph() && !CDSConfig::is_dumping_final_static_archive()) {
2523       log_error(cds)("CDS archive has aot-linked classes. It cannot be used when archived full module graph is not used.");
2524       return false;
2525     }
2526     if (header()->gc_kind() != (int)Universe::heap()->kind()) {
2527       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)",
2528                      header()->gc_name(), Universe::heap()->name());
2529       return false;
2530     }
2531   }
2532 
2533   return true;
2534 }
2535 
2536 // The 2 core spaces are RW->RO
2537 FileMapRegion* FileMapInfo::first_core_region() const {
2538   return region_at(MetaspaceShared::rw);
2539 }
2540 
2541 FileMapRegion* FileMapInfo::last_core_region() const {
2542   return region_at(MetaspaceShared::ro);
2543 }
2544 
2545 void FileMapInfo::print(outputStream* st) const {
2546   header()->print(st);
2547   if (!is_static()) {
2548     dynamic_header()->print(st);
2549   }
2550 }
2551 
2552 void FileMapHeader::set_as_offset(char* p, size_t *offset) {
2553   *offset = ArchiveBuilder::current()->any_to_offset((address)p);
2554 }
2555 

2621   }
2622 
2623   if (_allow_archiving_with_java_agent) {
2624     log_warning(cds)("This archive was created with AllowArchivingWithJavaAgent. It should be used "
2625             "for testing purposes only and should not be used in a production environment");
2626   }
2627 
2628   log_info(cds)("Archive was created with UseCompressedOops = %d, UseCompressedClassPointers = %d",
2629                           compressed_oops(), compressed_class_pointers());
2630   if (compressed_oops() != UseCompressedOops || compressed_class_pointers() != UseCompressedClassPointers) {
2631     log_info(cds)("Unable to use shared archive.\nThe saved state of UseCompressedOops and UseCompressedClassPointers is "
2632                                "different from runtime, CDS will be disabled.");
2633     return false;
2634   }
2635 
2636   if (! _use_secondary_supers_table && UseSecondarySupersTable) {
2637     log_warning(cds)("The shared archive was created without UseSecondarySupersTable.");
2638     return false;
2639   }
2640 
2641   if (!_use_optimized_module_handling && !CDSConfig::is_dumping_final_static_archive()) {
2642     CDSConfig::stop_using_optimized_module_handling();
2643     log_info(cds)("optimized module handling: disabled because archive was created without optimized module handling");
2644   }
2645 
2646   if (is_static()) {
2647     // Only the static archive can contain the full module graph.
2648     if (!_has_full_module_graph) {
2649       CDSConfig::stop_using_full_module_graph("archive was created without full module graph");
2650     }
2651 
2652     if (_has_archived_invokedynamic) {
2653       CDSConfig::set_has_archived_invokedynamic();
2654     }
2655     if (_has_archived_packages) {
2656       CDSConfig::set_is_loading_packages();
2657     }
2658     if (_has_archived_protection_domains) {
2659       CDSConfig::set_is_loading_protection_domains();
2660     }
2661   }
2662 
2663   return true;
2664 }
2665 
2666 bool FileMapInfo::validate_header() {
2667   if (!header()->validate()) {
2668     return false;
2669   }
2670   if (_is_static) {
2671     return true;
2672   } else {
2673     return DynamicArchive::validate(this);
2674   }
2675 }
2676 
2677 #if INCLUDE_JVMTI
2678 ClassPathEntry** FileMapInfo::_classpath_entries_for_jvmti = nullptr;
2679 
2680 ClassPathEntry* FileMapInfo::get_classpath_entry_for_jvmti(int i, TRAPS) {
< prev index next >