< prev index next >

src/hotspot/share/cds/filemap.cpp

Print this page

 227   if (UseCompressedClassPointers) {
 228 #ifdef _LP64
 229     _narrow_klass_pointer_bits = CompressedKlassPointers::narrow_klass_pointer_bits();
 230     _narrow_klass_shift = ArchiveBuilder::precomputed_narrow_klass_shift();
 231 #endif
 232   } else {
 233     _narrow_klass_pointer_bits = _narrow_klass_shift = -1;
 234   }
 235   _type_profile_level = TypeProfileLevel;
 236   _type_profile_args_limit = TypeProfileArgsLimit;
 237   _type_profile_parms_limit = TypeProfileParmsLimit;
 238   _type_profile_width = TypeProfileWidth;
 239   _bci_profile_width = BciProfileWidth;
 240   _profile_traps = ProfileTraps;
 241   _type_profile_casts = TypeProfileCasts;
 242   _spec_trap_limit_extra_entries = SpecTrapLimitExtraEntries;
 243   _max_heap_size = MaxHeapSize;
 244   _use_optimized_module_handling = CDSConfig::is_using_optimized_module_handling();
 245   _has_aot_linked_classes = CDSConfig::is_dumping_aot_linked_classes();
 246   _has_full_module_graph = CDSConfig::is_dumping_full_module_graph();




 247 
 248   // The following fields are for sanity checks for whether this archive
 249   // will function correctly with this JVM and the bootclasspath it's
 250   // invoked with.
 251 
 252   // JVM version string ... changes on each build.
 253   get_header_version(_jvm_ident);
 254 
 255   _verify_local = BytecodeVerificationLocal;
 256   _verify_remote = BytecodeVerificationRemote;
 257   _has_platform_or_app_classes = AOTClassLocationConfig::dumptime()->has_platform_or_app_classes();
 258   _requested_base_address = (char*)SharedBaseAddress;
 259   _mapped_base_address = (char*)SharedBaseAddress;
 260   _allow_archiving_with_java_agent = AllowArchivingWithJavaAgent;
 261 }
 262 
 263 void FileMapHeader::copy_base_archive_name(const char* archive) {
 264   assert(base_archive_name_size() != 0, "_base_archive_name_size not set");
 265   assert(base_archive_name_offset() != 0, "_base_archive_name_offset not set");
 266   assert(header_size() > sizeof(*this), "_base_archive_name_size not included in header size?");

 301   st->print_cr("- jvm_ident:                      %s", _jvm_ident);
 302   st->print_cr("- class_location_config_offset:   0x%zx", _class_location_config_offset);
 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("- requested_base_address:         " INTPTR_FORMAT, p2i(_requested_base_address));
 307   st->print_cr("- mapped_base_address:            " INTPTR_FORMAT, p2i(_mapped_base_address));
 308   st->print_cr("- heap_root_segments.roots_count: %d" , _heap_root_segments.roots_count());
 309   st->print_cr("- heap_root_segments.base_offset: 0x%zx", _heap_root_segments.base_offset());
 310   st->print_cr("- heap_root_segments.count:       %zu", _heap_root_segments.count());
 311   st->print_cr("- heap_root_segments.max_size_elems: %d", _heap_root_segments.max_size_in_elems());
 312   st->print_cr("- heap_root_segments.max_size_bytes: %d", _heap_root_segments.max_size_in_bytes());
 313   st->print_cr("- _heap_oopmap_start_pos:         %zu", _heap_oopmap_start_pos);
 314   st->print_cr("- _heap_ptrmap_start_pos:         %zu", _heap_ptrmap_start_pos);
 315   st->print_cr("- _rw_ptrmap_start_pos:           %zu", _rw_ptrmap_start_pos);
 316   st->print_cr("- _ro_ptrmap_start_pos:           %zu", _ro_ptrmap_start_pos);
 317   st->print_cr("- allow_archiving_with_java_agent:%d", _allow_archiving_with_java_agent);
 318   st->print_cr("- use_optimized_module_handling:  %d", _use_optimized_module_handling);
 319   st->print_cr("- has_full_module_graph           %d", _has_full_module_graph);
 320   st->print_cr("- has_aot_linked_classes          %d", _has_aot_linked_classes);



 321 }
 322 
 323 bool FileMapInfo::validate_class_location() {
 324   assert(CDSConfig::is_using_archive(), "runtime only");
 325 
 326   AOTClassLocationConfig* config = header()->class_location_config();
 327   bool has_extra_module_paths = false;
 328   if (!config->validate(header()->has_aot_linked_classes(), &has_extra_module_paths)) {
 329     if (PrintSharedArchiveAndExit) {
 330       MetaspaceShared::set_archive_loading_failed();
 331       return true;
 332     } else {
 333       return false;
 334     }
 335   }
 336 
 337   if (header()->has_full_module_graph() && has_extra_module_paths) {
 338     CDSConfig::stop_using_optimized_module_handling();
 339     MetaspaceShared::report_loading_error("optimized module handling: disabled because extra module path(s) are specified");
 340   }

 903       assert((mapping_offset >> CompressedOops::shift()) << CompressedOops::shift() == mapping_offset, "must be");
 904     } else {
 905       mapping_offset = 0; // not used with !UseCompressedOops
 906     }
 907 #endif // INCLUDE_CDS_JAVA_HEAP
 908   } else {
 909     char* requested_SharedBaseAddress = (char*)MetaspaceShared::requested_base_address();
 910     requested_base = ArchiveBuilder::current()->to_requested(base);
 911     assert(requested_base >= requested_SharedBaseAddress, "must be");
 912     mapping_offset = requested_base - requested_SharedBaseAddress;
 913   }
 914 
 915   r->set_file_offset(_file_offset);
 916   int crc = ClassLoader::crc32(0, base, (jint)size);
 917   if (size > 0) {
 918     aot_log_info(aot)("Shared file region (%s) %d: %8zu"
 919                    " bytes, addr " INTPTR_FORMAT " file offset 0x%08" PRIxPTR
 920                    " crc 0x%08x",
 921                    region_name(region), region, size, p2i(requested_base), _file_offset, crc);
 922   } else {
 923      aot_log_info(aot)("Shared file region (%s) %d: %8zu"
 924                    " bytes", region_name(region), region, size);
 925   }
 926 
 927   r->init(region, mapping_offset, size, read_only, allow_exec, crc);
 928 
 929   if (base != nullptr) {
 930     write_bytes_aligned(base, size);
 931   }
 932 }
 933 
 934 static size_t write_bitmap(const CHeapBitMap* map, char* output, size_t offset) {
 935   size_t size_in_bytes = map->size_in_bytes();
 936   map->write_to((BitMap::bm_word_t*)(output + offset), size_in_bytes);
 937   return offset + size_in_bytes;
 938 }
 939 
 940 // The sorting code groups the objects with non-null oop/ptrs together.
 941 // Relevant bitmaps then have lots of leading and trailing zeros, which
 942 // we do not have to store.
 943 size_t FileMapInfo::remove_bitmap_zeros(CHeapBitMap* map) {
 944   BitMap::idx_t first_set = map->find_first_set_bit(0);
 945   BitMap::idx_t last_set  = map->find_last_set_bit(0);
 946   size_t old_size = map->size();
 947 
 948   // Slice and resize bitmap
 949   map->truncate(first_set, last_set + 1);
 950 
 951   assert(map->at(0), "First bit should be set");
 952   assert(map->at(map->size() - 1), "Last bit should be set");
 953   assert(map->size() <= old_size, "sanity");
 954 
 955   return first_set;
 956 }
 957 
 958 char* FileMapInfo::write_bitmap_region(CHeapBitMap* rw_ptrmap, CHeapBitMap* ro_ptrmap, ArchiveHeapInfo* heap_info,


 959                                        size_t &size_in_bytes) {
 960   size_t removed_rw_leading_zeros = remove_bitmap_zeros(rw_ptrmap);
 961   size_t removed_ro_leading_zeros = remove_bitmap_zeros(ro_ptrmap);
 962   header()->set_rw_ptrmap_start_pos(removed_rw_leading_zeros);
 963   header()->set_ro_ptrmap_start_pos(removed_ro_leading_zeros);
 964   size_in_bytes = rw_ptrmap->size_in_bytes() + ro_ptrmap->size_in_bytes();
 965 
 966   if (heap_info->is_used()) {
 967     // Remove leading and trailing zeros
 968     size_t removed_oop_leading_zeros = remove_bitmap_zeros(heap_info->oopmap());
 969     size_t removed_ptr_leading_zeros = remove_bitmap_zeros(heap_info->ptrmap());
 970     header()->set_heap_oopmap_start_pos(removed_oop_leading_zeros);
 971     header()->set_heap_ptrmap_start_pos(removed_ptr_leading_zeros);
 972 
 973     size_in_bytes += heap_info->oopmap()->size_in_bytes();
 974     size_in_bytes += heap_info->ptrmap()->size_in_bytes();
 975   }
 976 
 977   // The bitmap region contains up to 4 parts:
 978   // rw_ptrmap:           metaspace pointers inside the read-write region
 979   // ro_ptrmap:           metaspace pointers inside the read-only region
 980   // heap_info->oopmap(): Java oop pointers in the heap region
 981   // heap_info->ptrmap(): metaspace pointers in the heap region
 982   char* buffer = NEW_C_HEAP_ARRAY(char, size_in_bytes, mtClassShared);
 983   size_t written = 0;
 984 
 985   region_at(MetaspaceShared::rw)->init_ptrmap(0, rw_ptrmap->size());
 986   written = write_bitmap(rw_ptrmap, buffer, written);
 987 
 988   region_at(MetaspaceShared::ro)->init_ptrmap(written, ro_ptrmap->size());
 989   written = write_bitmap(ro_ptrmap, buffer, written);
 990 



 991   if (heap_info->is_used()) {
 992     FileMapRegion* r = region_at(MetaspaceShared::hp);
 993 
 994     r->init_oopmap(written, heap_info->oopmap()->size());
 995     written = write_bitmap(heap_info->oopmap(), buffer, written);
 996 
 997     r->init_ptrmap(written, heap_info->ptrmap()->size());
 998     written = write_bitmap(heap_info->ptrmap(), buffer, written);
 999   }
1000 
1001   write_region(MetaspaceShared::bm, (char*)buffer, size_in_bytes, /*read_only=*/true, /*allow_exec=*/false);
1002   return buffer;
1003 }
1004 
1005 size_t FileMapInfo::write_heap_region(ArchiveHeapInfo* heap_info) {
1006   char* buffer_start = heap_info->buffer_start();
1007   size_t buffer_size = heap_info->buffer_byte_size();
1008   write_region(MetaspaceShared::hp, buffer_start, buffer_size, false, false);
1009   header()->set_heap_root_segments(heap_info->heap_root_segments());
1010   return buffer_size;

1209   } else if (addr_delta != 0) {
1210     r->set_read_only(false); // Need to patch the pointers
1211   }
1212 
1213   if (MetaspaceShared::use_windows_memory_mapping() && rs.is_reserved()) {
1214     // This is the second time we try to map the archive(s). We have already created a ReservedSpace
1215     // that covers all the FileMapRegions to ensure all regions can be mapped. However, Windows
1216     // can't mmap into a ReservedSpace, so we just ::read() the data. We're going to patch all the
1217     // regions anyway, so there's no benefit for mmap anyway.
1218     if (!read_region(i, requested_addr, size, /* do_commit = */ true)) {
1219       aot_log_info(aot)("Failed to read %s shared space into reserved space at " INTPTR_FORMAT,
1220                     shared_region_name[i], p2i(requested_addr));
1221       return MAP_ARCHIVE_OTHER_FAILURE; // oom or I/O error.
1222     } else {
1223       assert(r->mapped_base() != nullptr, "must be initialized");
1224     }
1225   } else {
1226     // Note that this may either be a "fresh" mapping into unreserved address
1227     // space (Windows, first mapping attempt), or a mapping into pre-reserved
1228     // space (Posix). See also comment in MetaspaceShared::map_archives().

1229     char* base = map_memory(_fd, _full_path, r->file_offset(),
1230                             requested_addr, size, r->read_only(),
1231                             r->allow_exec(), mtClassShared);
1232     if (base != requested_addr) {
1233       aot_log_info(aot)("Unable to map %s shared space at " INTPTR_FORMAT,
1234                     shared_region_name[i], p2i(requested_addr));
1235       _memory_mapping_failed = true;
1236       return MAP_ARCHIVE_MMAP_FAILURE;
1237     }
1238 
1239     if (VerifySharedSpaces && !r->check_region_crc(requested_addr)) {
1240       return MAP_ARCHIVE_OTHER_FAILURE;
1241     }
1242 
1243     r->set_mapped_from_file(true);
1244     r->set_mapped_base(requested_addr);
1245   }
1246 
1247   if (rs.is_reserved()) {
1248     char* mapped_base = r->mapped_base();
1249     assert(rs.base() <= mapped_base && mapped_base + size <= rs.end(),
1250            PTR_FORMAT " <= " PTR_FORMAT " < " PTR_FORMAT " <= " PTR_FORMAT,

1298     if (!read_region(MetaspaceShared::ac, requested_base, r->used_aligned(), /* do_commit = */ true)) {
1299       aot_log_info(aot)("Failed to read aot code shared space into reserved space at " INTPTR_FORMAT,
1300                     p2i(requested_base));
1301       return false;
1302     }
1303     mapped_base = requested_base;
1304   } else {
1305     // We do not execute in-place in the AOT code region.
1306     // AOT code is copied to the CodeCache for execution.
1307     bool read_only = false, allow_exec = false;
1308     mapped_base = map_memory(_fd, _full_path, r->file_offset(),
1309                              requested_base, r->used_aligned(), read_only, allow_exec, mtClassShared);
1310   }
1311   if (mapped_base == nullptr) {
1312     aot_log_info(aot)("failed to map aot code region");
1313     return false;
1314   } else {
1315     assert(mapped_base == requested_base, "must be");
1316     r->set_mapped_from_file(true);
1317     r->set_mapped_base(mapped_base);

1318     aot_log_info(aot)("Mapped static  region #%d at base " INTPTR_FORMAT " top " INTPTR_FORMAT " (%s)",
1319                   MetaspaceShared::ac, p2i(r->mapped_base()), p2i(r->mapped_end()),
1320                   shared_region_name[MetaspaceShared::ac]);
1321     return true;
1322   }
1323 }
1324 

















































1325 class SharedDataRelocationTask : public ArchiveWorkerTask {
1326 private:
1327   BitMapView* const _rw_bm;
1328   BitMapView* const _ro_bm;
1329   SharedDataRelocator* const _rw_reloc;
1330   SharedDataRelocator* const _ro_reloc;
1331 
1332 public:
1333   SharedDataRelocationTask(BitMapView* rw_bm, BitMapView* ro_bm, SharedDataRelocator* rw_reloc, SharedDataRelocator* ro_reloc) :
1334                            ArchiveWorkerTask("Shared Data Relocation"),
1335                            _rw_bm(rw_bm), _ro_bm(ro_bm), _rw_reloc(rw_reloc), _ro_reloc(ro_reloc) {}
1336 
1337   void work(int chunk, int max_chunks) override {
1338     work_on(chunk, max_chunks, _rw_bm, _rw_reloc);
1339     work_on(chunk, max_chunks, _ro_bm, _ro_reloc);
1340   }
1341 
1342   void work_on(int chunk, int max_chunks, BitMapView* bm, SharedDataRelocator* reloc) {
1343     BitMap::idx_t size  = bm->size();
1344     BitMap::idx_t start = MIN2(size, size * chunk / max_chunks);

1454 }
1455 
1456 void FileMapInfo::map_or_load_heap_region() {
1457   bool success = false;
1458 
1459   if (can_use_heap_region()) {
1460     if (ArchiveHeapLoader::can_map()) {
1461       success = map_heap_region();
1462     } else if (ArchiveHeapLoader::can_load()) {
1463       success = ArchiveHeapLoader::load_heap_region(this);
1464     } else {
1465       if (!UseCompressedOops && !ArchiveHeapLoader::can_map()) {
1466         MetaspaceShared::report_loading_error("Cannot use CDS heap data. Selected GC not compatible -XX:-UseCompressedOops");
1467       } else {
1468         MetaspaceShared::report_loading_error("Cannot use CDS heap data. UseEpsilonGC, UseG1GC, UseSerialGC, UseParallelGC, or UseShenandoahGC are required.");
1469       }
1470     }
1471   }
1472 
1473   if (!success) {
1474     if (CDSConfig::is_using_aot_linked_classes()) {
1475       // It's too late to recover -- we have already committed to use the archived metaspace objects, but
1476       // the archived heap objects cannot be loaded, so we don't have the archived FMG to guarantee that
1477       // all AOT-linked classes are visible.
1478       //
1479       // We get here because the heap is too small. The app will fail anyway. So let's quit.
1480       aot_log_error(aot)("%s has aot-linked classes but the archived "
1481                      "heap objects cannot be loaded. Try increasing your heap size.",
1482                      CDSConfig::type_of_archive_being_loaded());
1483       MetaspaceShared::unrecoverable_loading_error();
1484     }
1485     CDSConfig::stop_using_full_module_graph("archive heap loading failed");
1486   }
1487 }
1488 
1489 bool FileMapInfo::can_use_heap_region() {
1490   if (!has_heap_region()) {
1491     return false;
1492   }
1493   if (JvmtiExport::should_post_class_file_load_hook() && JvmtiExport::has_early_class_hook_env()) {
1494     ShouldNotReachHere(); // CDS should have been disabled.

1862       return false;
1863     }
1864     if (JvmtiExport::has_early_vmstart_env()) {
1865       aot_log_error(aot)("%s has aot-linked classes. It cannot be used when JVMTI early vm start is in use.",
1866                      archive_type);
1867       return false;
1868     }
1869     if (!CDSConfig::is_using_full_module_graph()) {
1870       aot_log_error(aot)("%s has aot-linked classes. It cannot be used when archived full module graph is not used.",
1871                      archive_type);
1872       return false;
1873     }
1874 
1875     const char* prop = Arguments::get_property("java.security.manager");
1876     if (prop != nullptr && strcmp(prop, "disallow") != 0) {
1877       aot_log_error(aot)("%s has aot-linked classes. It cannot be used with -Djava.security.manager=%s.",
1878                      archive_type, prop);
1879       return false;
1880     }
1881 






1882 #if INCLUDE_JVMTI
1883     if (Arguments::has_jdwp_agent()) {
1884       aot_log_error(aot)("%s has aot-linked classes. It cannot be used with JDWP agent", archive_type);
1885       return false;
1886     }
1887 #endif
1888   }
1889 
1890   return true;
1891 }
1892 
1893 // The 2 core spaces are RW->RO
1894 FileMapRegion* FileMapInfo::first_core_region() const {
1895   return region_at(MetaspaceShared::rw);
1896 }
1897 
1898 FileMapRegion* FileMapInfo::last_core_region() const {
1899   return region_at(MetaspaceShared::ro);
1900 }
1901 

2055   }
2056 
2057   if (compact_headers() != UseCompactObjectHeaders) {
2058     aot_log_warning(aot)("Unable to use %s.\nThe %s's UseCompactObjectHeaders setting (%s)"
2059                      " does not equal the current UseCompactObjectHeaders setting (%s).", file_type, file_type,
2060                      _compact_headers          ? "enabled" : "disabled",
2061                      UseCompactObjectHeaders   ? "enabled" : "disabled");
2062     return false;
2063   }
2064 
2065   if (!_use_optimized_module_handling && !CDSConfig::is_dumping_final_static_archive()) {
2066     CDSConfig::stop_using_optimized_module_handling();
2067     aot_log_info(aot)("optimized module handling: disabled because archive was created without optimized module handling");
2068   }
2069 
2070   if (is_static()) {
2071     // Only the static archive can contain the full module graph.
2072     if (!_has_full_module_graph) {
2073       CDSConfig::stop_using_full_module_graph("archive was created without full module graph");
2074     }







2075   }
2076 
2077   return true;
2078 }
2079 
2080 bool FileMapInfo::validate_header() {
2081   if (!header()->validate()) {
2082     return false;
2083   }
2084   if (_is_static) {
2085     return true;
2086   } else {
2087     return DynamicArchive::validate(this);
2088   }
2089 }
2090 
2091 #if INCLUDE_JVMTI
2092 ClassPathEntry** FileMapInfo::_classpath_entries_for_jvmti = nullptr;
2093 
2094 ClassPathEntry* FileMapInfo::get_classpath_entry_for_jvmti(int i, TRAPS) {

 227   if (UseCompressedClassPointers) {
 228 #ifdef _LP64
 229     _narrow_klass_pointer_bits = CompressedKlassPointers::narrow_klass_pointer_bits();
 230     _narrow_klass_shift = ArchiveBuilder::precomputed_narrow_klass_shift();
 231 #endif
 232   } else {
 233     _narrow_klass_pointer_bits = _narrow_klass_shift = -1;
 234   }
 235   _type_profile_level = TypeProfileLevel;
 236   _type_profile_args_limit = TypeProfileArgsLimit;
 237   _type_profile_parms_limit = TypeProfileParmsLimit;
 238   _type_profile_width = TypeProfileWidth;
 239   _bci_profile_width = BciProfileWidth;
 240   _profile_traps = ProfileTraps;
 241   _type_profile_casts = TypeProfileCasts;
 242   _spec_trap_limit_extra_entries = SpecTrapLimitExtraEntries;
 243   _max_heap_size = MaxHeapSize;
 244   _use_optimized_module_handling = CDSConfig::is_using_optimized_module_handling();
 245   _has_aot_linked_classes = CDSConfig::is_dumping_aot_linked_classes();
 246   _has_full_module_graph = CDSConfig::is_dumping_full_module_graph();
 247   _has_archived_packages = CDSConfig::is_dumping_packages();
 248   _has_archived_protection_domains = CDSConfig::is_dumping_protection_domains();
 249   _gc_kind = (int)Universe::heap()->kind();
 250   jio_snprintf(_gc_name, sizeof(_gc_name), Universe::heap()->name());
 251 
 252   // The following fields are for sanity checks for whether this archive
 253   // will function correctly with this JVM and the bootclasspath it's
 254   // invoked with.
 255 
 256   // JVM version string ... changes on each build.
 257   get_header_version(_jvm_ident);
 258 
 259   _verify_local = BytecodeVerificationLocal;
 260   _verify_remote = BytecodeVerificationRemote;
 261   _has_platform_or_app_classes = AOTClassLocationConfig::dumptime()->has_platform_or_app_classes();
 262   _requested_base_address = (char*)SharedBaseAddress;
 263   _mapped_base_address = (char*)SharedBaseAddress;
 264   _allow_archiving_with_java_agent = AllowArchivingWithJavaAgent;
 265 }
 266 
 267 void FileMapHeader::copy_base_archive_name(const char* archive) {
 268   assert(base_archive_name_size() != 0, "_base_archive_name_size not set");
 269   assert(base_archive_name_offset() != 0, "_base_archive_name_offset not set");
 270   assert(header_size() > sizeof(*this), "_base_archive_name_size not included in header size?");

 305   st->print_cr("- jvm_ident:                      %s", _jvm_ident);
 306   st->print_cr("- class_location_config_offset:   0x%zx", _class_location_config_offset);
 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("- requested_base_address:         " INTPTR_FORMAT, p2i(_requested_base_address));
 311   st->print_cr("- mapped_base_address:            " INTPTR_FORMAT, p2i(_mapped_base_address));
 312   st->print_cr("- heap_root_segments.roots_count: %d" , _heap_root_segments.roots_count());
 313   st->print_cr("- heap_root_segments.base_offset: 0x%zx", _heap_root_segments.base_offset());
 314   st->print_cr("- heap_root_segments.count:       %zu", _heap_root_segments.count());
 315   st->print_cr("- heap_root_segments.max_size_elems: %d", _heap_root_segments.max_size_in_elems());
 316   st->print_cr("- heap_root_segments.max_size_bytes: %d", _heap_root_segments.max_size_in_bytes());
 317   st->print_cr("- _heap_oopmap_start_pos:         %zu", _heap_oopmap_start_pos);
 318   st->print_cr("- _heap_ptrmap_start_pos:         %zu", _heap_ptrmap_start_pos);
 319   st->print_cr("- _rw_ptrmap_start_pos:           %zu", _rw_ptrmap_start_pos);
 320   st->print_cr("- _ro_ptrmap_start_pos:           %zu", _ro_ptrmap_start_pos);
 321   st->print_cr("- allow_archiving_with_java_agent:%d", _allow_archiving_with_java_agent);
 322   st->print_cr("- use_optimized_module_handling:  %d", _use_optimized_module_handling);
 323   st->print_cr("- has_full_module_graph           %d", _has_full_module_graph);
 324   st->print_cr("- has_aot_linked_classes          %d", _has_aot_linked_classes);
 325   st->print_cr("- has_archived_packages           %d", _has_archived_packages);
 326   st->print_cr("- has_archived_protection_domains %d", _has_archived_protection_domains);
 327   st->print_cr("- ptrmap_size_in_bits:            %zu", _ptrmap_size_in_bits);
 328 }
 329 
 330 bool FileMapInfo::validate_class_location() {
 331   assert(CDSConfig::is_using_archive(), "runtime only");
 332 
 333   AOTClassLocationConfig* config = header()->class_location_config();
 334   bool has_extra_module_paths = false;
 335   if (!config->validate(header()->has_aot_linked_classes(), &has_extra_module_paths)) {
 336     if (PrintSharedArchiveAndExit) {
 337       MetaspaceShared::set_archive_loading_failed();
 338       return true;
 339     } else {
 340       return false;
 341     }
 342   }
 343 
 344   if (header()->has_full_module_graph() && has_extra_module_paths) {
 345     CDSConfig::stop_using_optimized_module_handling();
 346     MetaspaceShared::report_loading_error("optimized module handling: disabled because extra module path(s) are specified");
 347   }

 910       assert((mapping_offset >> CompressedOops::shift()) << CompressedOops::shift() == mapping_offset, "must be");
 911     } else {
 912       mapping_offset = 0; // not used with !UseCompressedOops
 913     }
 914 #endif // INCLUDE_CDS_JAVA_HEAP
 915   } else {
 916     char* requested_SharedBaseAddress = (char*)MetaspaceShared::requested_base_address();
 917     requested_base = ArchiveBuilder::current()->to_requested(base);
 918     assert(requested_base >= requested_SharedBaseAddress, "must be");
 919     mapping_offset = requested_base - requested_SharedBaseAddress;
 920   }
 921 
 922   r->set_file_offset(_file_offset);
 923   int crc = ClassLoader::crc32(0, base, (jint)size);
 924   if (size > 0) {
 925     aot_log_info(aot)("Shared file region (%s) %d: %8zu"
 926                    " bytes, addr " INTPTR_FORMAT " file offset 0x%08" PRIxPTR
 927                    " crc 0x%08x",
 928                    region_name(region), region, size, p2i(requested_base), _file_offset, crc);
 929   } else {
 930     aot_log_info(aot)("Shared file region (%s) %d: %8zu"
 931                    " bytes", region_name(region), region, size);
 932   }
 933 
 934   r->init(region, mapping_offset, size, read_only, allow_exec, crc);
 935 
 936   if (base != nullptr) {
 937     write_bytes_aligned(base, size);
 938   }
 939 }
 940 
 941 static size_t write_bitmap(const CHeapBitMap* map, char* output, size_t offset) {
 942   size_t size_in_bytes = map->size_in_bytes();
 943   map->write_to((BitMap::bm_word_t*)(output + offset), size_in_bytes);
 944   return offset + size_in_bytes;
 945 }
 946 
 947 // The sorting code groups the objects with non-null oop/ptrs together.
 948 // Relevant bitmaps then have lots of leading and trailing zeros, which
 949 // we do not have to store.
 950 size_t FileMapInfo::remove_bitmap_zeros(CHeapBitMap* map) {
 951   BitMap::idx_t first_set = map->find_first_set_bit(0);
 952   BitMap::idx_t last_set  = map->find_last_set_bit(0);
 953   size_t old_size = map->size();
 954 
 955   // Slice and resize bitmap
 956   map->truncate(first_set, last_set + 1);
 957 
 958   assert(map->at(0), "First bit should be set");
 959   assert(map->at(map->size() - 1), "Last bit should be set");
 960   assert(map->size() <= old_size, "sanity");
 961 
 962   return first_set;
 963 }
 964 
 965 char* FileMapInfo::write_bitmap_region(CHeapBitMap* rw_ptrmap, CHeapBitMap* ro_ptrmap,
 966                                        CHeapBitMap* ac_ptrmap,
 967                                        ArchiveHeapInfo* heap_info,
 968                                        size_t &size_in_bytes) {
 969   size_t removed_rw_leading_zeros = remove_bitmap_zeros(rw_ptrmap);
 970   size_t removed_ro_leading_zeros = remove_bitmap_zeros(ro_ptrmap);
 971   header()->set_rw_ptrmap_start_pos(removed_rw_leading_zeros);
 972   header()->set_ro_ptrmap_start_pos(removed_ro_leading_zeros);
 973   size_in_bytes = rw_ptrmap->size_in_bytes() + ro_ptrmap->size_in_bytes() + ac_ptrmap->size_in_bytes();
 974 
 975   if (heap_info->is_used()) {
 976     // Remove leading and trailing zeros
 977     size_t removed_oop_leading_zeros = remove_bitmap_zeros(heap_info->oopmap());
 978     size_t removed_ptr_leading_zeros = remove_bitmap_zeros(heap_info->ptrmap());
 979     header()->set_heap_oopmap_start_pos(removed_oop_leading_zeros);
 980     header()->set_heap_ptrmap_start_pos(removed_ptr_leading_zeros);
 981 
 982     size_in_bytes += heap_info->oopmap()->size_in_bytes();
 983     size_in_bytes += heap_info->ptrmap()->size_in_bytes();
 984   }
 985 
 986   // The bitmap region contains up to 4 parts:
 987   // rw_ptrmap:           metaspace pointers inside the read-write region
 988   // ro_ptrmap:           metaspace pointers inside the read-only region
 989   // heap_info->oopmap(): Java oop pointers in the heap region
 990   // heap_info->ptrmap(): metaspace pointers in the heap region
 991   char* buffer = NEW_C_HEAP_ARRAY(char, size_in_bytes, mtClassShared);
 992   size_t written = 0;
 993 
 994   region_at(MetaspaceShared::rw)->init_ptrmap(0, rw_ptrmap->size());
 995   written = write_bitmap(rw_ptrmap, buffer, written);
 996 
 997   region_at(MetaspaceShared::ro)->init_ptrmap(written, ro_ptrmap->size());
 998   written = write_bitmap(ro_ptrmap, buffer, written);
 999 
1000   region_at(MetaspaceShared::ac)->init_ptrmap(written, ac_ptrmap->size());
1001   written = write_bitmap(ac_ptrmap, buffer, written);
1002 
1003   if (heap_info->is_used()) {
1004     FileMapRegion* r = region_at(MetaspaceShared::hp);
1005 
1006     r->init_oopmap(written, heap_info->oopmap()->size());
1007     written = write_bitmap(heap_info->oopmap(), buffer, written);
1008 
1009     r->init_ptrmap(written, heap_info->ptrmap()->size());
1010     written = write_bitmap(heap_info->ptrmap(), buffer, written);
1011   }
1012 
1013   write_region(MetaspaceShared::bm, (char*)buffer, size_in_bytes, /*read_only=*/true, /*allow_exec=*/false);
1014   return buffer;
1015 }
1016 
1017 size_t FileMapInfo::write_heap_region(ArchiveHeapInfo* heap_info) {
1018   char* buffer_start = heap_info->buffer_start();
1019   size_t buffer_size = heap_info->buffer_byte_size();
1020   write_region(MetaspaceShared::hp, buffer_start, buffer_size, false, false);
1021   header()->set_heap_root_segments(heap_info->heap_root_segments());
1022   return buffer_size;

1221   } else if (addr_delta != 0) {
1222     r->set_read_only(false); // Need to patch the pointers
1223   }
1224 
1225   if (MetaspaceShared::use_windows_memory_mapping() && rs.is_reserved()) {
1226     // This is the second time we try to map the archive(s). We have already created a ReservedSpace
1227     // that covers all the FileMapRegions to ensure all regions can be mapped. However, Windows
1228     // can't mmap into a ReservedSpace, so we just ::read() the data. We're going to patch all the
1229     // regions anyway, so there's no benefit for mmap anyway.
1230     if (!read_region(i, requested_addr, size, /* do_commit = */ true)) {
1231       aot_log_info(aot)("Failed to read %s shared space into reserved space at " INTPTR_FORMAT,
1232                     shared_region_name[i], p2i(requested_addr));
1233       return MAP_ARCHIVE_OTHER_FAILURE; // oom or I/O error.
1234     } else {
1235       assert(r->mapped_base() != nullptr, "must be initialized");
1236     }
1237   } else {
1238     // Note that this may either be a "fresh" mapping into unreserved address
1239     // space (Windows, first mapping attempt), or a mapping into pre-reserved
1240     // space (Posix). See also comment in MetaspaceShared::map_archives().
1241     bool read_only = r->read_only() && !CDSConfig::is_dumping_final_static_archive();
1242     char* base = map_memory(_fd, _full_path, r->file_offset(),
1243                             requested_addr, size, read_only,
1244                             r->allow_exec(), mtClassShared);
1245     if (base != requested_addr) {
1246       aot_log_info(aot)("Unable to map %s shared space at " INTPTR_FORMAT,
1247                     shared_region_name[i], p2i(requested_addr));
1248       _memory_mapping_failed = true;
1249       return MAP_ARCHIVE_MMAP_FAILURE;
1250     }
1251 
1252     if (VerifySharedSpaces && !r->check_region_crc(requested_addr)) {
1253       return MAP_ARCHIVE_OTHER_FAILURE;
1254     }
1255 
1256     r->set_mapped_from_file(true);
1257     r->set_mapped_base(requested_addr);
1258   }
1259 
1260   if (rs.is_reserved()) {
1261     char* mapped_base = r->mapped_base();
1262     assert(rs.base() <= mapped_base && mapped_base + size <= rs.end(),
1263            PTR_FORMAT " <= " PTR_FORMAT " < " PTR_FORMAT " <= " PTR_FORMAT,

1311     if (!read_region(MetaspaceShared::ac, requested_base, r->used_aligned(), /* do_commit = */ true)) {
1312       aot_log_info(aot)("Failed to read aot code shared space into reserved space at " INTPTR_FORMAT,
1313                     p2i(requested_base));
1314       return false;
1315     }
1316     mapped_base = requested_base;
1317   } else {
1318     // We do not execute in-place in the AOT code region.
1319     // AOT code is copied to the CodeCache for execution.
1320     bool read_only = false, allow_exec = false;
1321     mapped_base = map_memory(_fd, _full_path, r->file_offset(),
1322                              requested_base, r->used_aligned(), read_only, allow_exec, mtClassShared);
1323   }
1324   if (mapped_base == nullptr) {
1325     aot_log_info(aot)("failed to map aot code region");
1326     return false;
1327   } else {
1328     assert(mapped_base == requested_base, "must be");
1329     r->set_mapped_from_file(true);
1330     r->set_mapped_base(mapped_base);
1331     relocate_pointers_in_aot_code_region();
1332     aot_log_info(aot)("Mapped static  region #%d at base " INTPTR_FORMAT " top " INTPTR_FORMAT " (%s)",
1333                   MetaspaceShared::ac, p2i(r->mapped_base()), p2i(r->mapped_end()),
1334                   shared_region_name[MetaspaceShared::ac]);
1335     return true;
1336   }
1337 }
1338 
1339 class CachedCodeRelocator: public BitMapClosure {
1340   address _code_requested_base;
1341   address* _patch_base;
1342   intx _code_delta;
1343   intx _metadata_delta;
1344 
1345 public:
1346   CachedCodeRelocator(address code_requested_base, address code_mapped_base,
1347                       intx metadata_delta) {
1348     _code_requested_base = code_requested_base;
1349     _patch_base = (address*)code_mapped_base;
1350     _code_delta = code_mapped_base - code_requested_base;
1351     _metadata_delta = metadata_delta;
1352   }
1353 
1354   bool do_bit(size_t offset) {
1355     address* p = _patch_base + offset;
1356     address requested_ptr = *p;
1357     if (requested_ptr < _code_requested_base) {
1358       *p = requested_ptr + _metadata_delta;
1359     } else {
1360       *p = requested_ptr + _code_delta;
1361     }
1362     return true; // keep iterating
1363   }
1364 };
1365 
1366 void FileMapInfo::relocate_pointers_in_aot_code_region() {
1367   FileMapRegion* r = region_at(MetaspaceShared::ac);
1368   char* bitmap_base = map_bitmap_region();
1369 
1370   BitMapView ac_ptrmap = ptrmap_view(MetaspaceShared::ac);
1371   if (ac_ptrmap.size() == 0) {
1372     return;
1373   }
1374 
1375   address core_regions_requested_base = (address)header()->requested_base_address();
1376   address core_regions_mapped_base = (address)header()->mapped_base_address();
1377   address ac_region_requested_base = core_regions_requested_base + r->mapping_offset();
1378   address ac_region_mapped_base = (address)r->mapped_base();
1379 
1380   size_t max_bits_for_core_regions = pointer_delta(mapped_end(), mapped_base(), // FIXME - renamed to core_regions_mapped_base(), etc
1381                                                    sizeof(address));
1382 
1383   CachedCodeRelocator patcher(ac_region_requested_base, ac_region_mapped_base,
1384                               core_regions_mapped_base - core_regions_requested_base);
1385   ac_ptrmap.iterate(&patcher);
1386 }
1387 
1388 class SharedDataRelocationTask : public ArchiveWorkerTask {
1389 private:
1390   BitMapView* const _rw_bm;
1391   BitMapView* const _ro_bm;
1392   SharedDataRelocator* const _rw_reloc;
1393   SharedDataRelocator* const _ro_reloc;
1394 
1395 public:
1396   SharedDataRelocationTask(BitMapView* rw_bm, BitMapView* ro_bm, SharedDataRelocator* rw_reloc, SharedDataRelocator* ro_reloc) :
1397                            ArchiveWorkerTask("Shared Data Relocation"),
1398                            _rw_bm(rw_bm), _ro_bm(ro_bm), _rw_reloc(rw_reloc), _ro_reloc(ro_reloc) {}
1399 
1400   void work(int chunk, int max_chunks) override {
1401     work_on(chunk, max_chunks, _rw_bm, _rw_reloc);
1402     work_on(chunk, max_chunks, _ro_bm, _ro_reloc);
1403   }
1404 
1405   void work_on(int chunk, int max_chunks, BitMapView* bm, SharedDataRelocator* reloc) {
1406     BitMap::idx_t size  = bm->size();
1407     BitMap::idx_t start = MIN2(size, size * chunk / max_chunks);

1517 }
1518 
1519 void FileMapInfo::map_or_load_heap_region() {
1520   bool success = false;
1521 
1522   if (can_use_heap_region()) {
1523     if (ArchiveHeapLoader::can_map()) {
1524       success = map_heap_region();
1525     } else if (ArchiveHeapLoader::can_load()) {
1526       success = ArchiveHeapLoader::load_heap_region(this);
1527     } else {
1528       if (!UseCompressedOops && !ArchiveHeapLoader::can_map()) {
1529         MetaspaceShared::report_loading_error("Cannot use CDS heap data. Selected GC not compatible -XX:-UseCompressedOops");
1530       } else {
1531         MetaspaceShared::report_loading_error("Cannot use CDS heap data. UseEpsilonGC, UseG1GC, UseSerialGC, UseParallelGC, or UseShenandoahGC are required.");
1532       }
1533     }
1534   }
1535 
1536   if (!success) {
1537     if (CDSConfig::is_using_aot_linked_classes() && !CDSConfig::is_dumping_final_static_archive()) {
1538       // It's too late to recover -- we have already committed to use the archived metaspace objects, but
1539       // the archived heap objects cannot be loaded, so we don't have the archived FMG to guarantee that
1540       // all AOT-linked classes are visible.
1541       //
1542       // We get here because the heap is too small. The app will fail anyway. So let's quit.
1543       aot_log_error(aot)("%s has aot-linked classes but the archived "
1544                      "heap objects cannot be loaded. Try increasing your heap size.",
1545                      CDSConfig::type_of_archive_being_loaded());
1546       MetaspaceShared::unrecoverable_loading_error();
1547     }
1548     CDSConfig::stop_using_full_module_graph("archive heap loading failed");
1549   }
1550 }
1551 
1552 bool FileMapInfo::can_use_heap_region() {
1553   if (!has_heap_region()) {
1554     return false;
1555   }
1556   if (JvmtiExport::should_post_class_file_load_hook() && JvmtiExport::has_early_class_hook_env()) {
1557     ShouldNotReachHere(); // CDS should have been disabled.

1925       return false;
1926     }
1927     if (JvmtiExport::has_early_vmstart_env()) {
1928       aot_log_error(aot)("%s has aot-linked classes. It cannot be used when JVMTI early vm start is in use.",
1929                      archive_type);
1930       return false;
1931     }
1932     if (!CDSConfig::is_using_full_module_graph()) {
1933       aot_log_error(aot)("%s has aot-linked classes. It cannot be used when archived full module graph is not used.",
1934                      archive_type);
1935       return false;
1936     }
1937 
1938     const char* prop = Arguments::get_property("java.security.manager");
1939     if (prop != nullptr && strcmp(prop, "disallow") != 0) {
1940       aot_log_error(aot)("%s has aot-linked classes. It cannot be used with -Djava.security.manager=%s.",
1941                      archive_type, prop);
1942       return false;
1943     }
1944 
1945     if (header()->gc_kind() != (int)Universe::heap()->kind()) {
1946       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)",
1947                      header()->gc_name(), Universe::heap()->name());
1948       return false;
1949     }
1950 
1951 #if INCLUDE_JVMTI
1952     if (Arguments::has_jdwp_agent()) {
1953       aot_log_error(aot)("%s has aot-linked classes. It cannot be used with JDWP agent", archive_type);
1954       return false;
1955     }
1956 #endif
1957   }
1958 
1959   return true;
1960 }
1961 
1962 // The 2 core spaces are RW->RO
1963 FileMapRegion* FileMapInfo::first_core_region() const {
1964   return region_at(MetaspaceShared::rw);
1965 }
1966 
1967 FileMapRegion* FileMapInfo::last_core_region() const {
1968   return region_at(MetaspaceShared::ro);
1969 }
1970 

2124   }
2125 
2126   if (compact_headers() != UseCompactObjectHeaders) {
2127     aot_log_warning(aot)("Unable to use %s.\nThe %s's UseCompactObjectHeaders setting (%s)"
2128                      " does not equal the current UseCompactObjectHeaders setting (%s).", file_type, file_type,
2129                      _compact_headers          ? "enabled" : "disabled",
2130                      UseCompactObjectHeaders   ? "enabled" : "disabled");
2131     return false;
2132   }
2133 
2134   if (!_use_optimized_module_handling && !CDSConfig::is_dumping_final_static_archive()) {
2135     CDSConfig::stop_using_optimized_module_handling();
2136     aot_log_info(aot)("optimized module handling: disabled because archive was created without optimized module handling");
2137   }
2138 
2139   if (is_static()) {
2140     // Only the static archive can contain the full module graph.
2141     if (!_has_full_module_graph) {
2142       CDSConfig::stop_using_full_module_graph("archive was created without full module graph");
2143     }
2144 
2145     if (_has_archived_packages) {
2146       CDSConfig::set_is_loading_packages();
2147     }
2148     if (_has_archived_protection_domains) {
2149       CDSConfig::set_is_loading_protection_domains();
2150     }
2151   }
2152 
2153   return true;
2154 }
2155 
2156 bool FileMapInfo::validate_header() {
2157   if (!header()->validate()) {
2158     return false;
2159   }
2160   if (_is_static) {
2161     return true;
2162   } else {
2163     return DynamicArchive::validate(this);
2164   }
2165 }
2166 
2167 #if INCLUDE_JVMTI
2168 ClassPathEntry** FileMapInfo::_classpath_entries_for_jvmti = nullptr;
2169 
2170 ClassPathEntry* FileMapInfo::get_classpath_entry_for_jvmti(int i, TRAPS) {
< prev index next >