< prev index next >

src/hotspot/share/cds/filemap.cpp

Print this page

 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   // Which JIT compier is used
 236   _compiler_type = (u1)CompilerConfig::compiler_type();
 237   _type_profile_level = TypeProfileLevel;
 238   _type_profile_args_limit = TypeProfileArgsLimit;
 239   _type_profile_parms_limit = TypeProfileParmsLimit;
 240   _type_profile_width = TypeProfileWidth;
 241   _bci_profile_width = BciProfileWidth;
 242   _profile_traps = ProfileTraps;
 243   _type_profile_casts = TypeProfileCasts;
 244   _spec_trap_limit_extra_entries = SpecTrapLimitExtraEntries;
 245   _max_heap_size = MaxHeapSize;
 246   _use_optimized_module_handling = CDSConfig::is_using_optimized_module_handling();
 247   _has_aot_linked_classes = CDSConfig::is_dumping_aot_linked_classes();
 248   _has_full_module_graph = CDSConfig::is_dumping_full_module_graph();




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

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



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

 940 }
 941 
 942 // The sorting code groups the objects with non-null oop/ptrs together.
 943 // Relevant bitmaps then have lots of leading and trailing zeros, which
 944 // we do not have to store.
 945 size_t FileMapInfo::remove_bitmap_zeros(CHeapBitMap* map) {
 946   BitMap::idx_t first_set = map->find_first_set_bit(0);
 947   BitMap::idx_t last_set  = map->find_last_set_bit(0);
 948   size_t old_size = map->size();
 949 
 950   // Slice and resize bitmap
 951   map->truncate(first_set, last_set + 1);
 952 
 953   assert(map->at(0), "First bit should be set");
 954   assert(map->at(map->size() - 1), "Last bit should be set");
 955   assert(map->size() <= old_size, "sanity");
 956 
 957   return first_set;
 958 }
 959 
 960 char* FileMapInfo::write_bitmap_region(CHeapBitMap* rw_ptrmap, CHeapBitMap* ro_ptrmap, ArchiveHeapInfo* heap_info,


 961                                        size_t &size_in_bytes) {
 962   size_t removed_rw_leading_zeros = remove_bitmap_zeros(rw_ptrmap);
 963   size_t removed_ro_leading_zeros = remove_bitmap_zeros(ro_ptrmap);
 964   header()->set_rw_ptrmap_start_pos(removed_rw_leading_zeros);
 965   header()->set_ro_ptrmap_start_pos(removed_ro_leading_zeros);
 966   size_in_bytes = rw_ptrmap->size_in_bytes() + ro_ptrmap->size_in_bytes();
 967 
 968   if (heap_info->is_used()) {
 969     // Remove leading and trailing zeros
 970     size_t removed_oop_leading_zeros = remove_bitmap_zeros(heap_info->oopmap());
 971     size_t removed_ptr_leading_zeros = remove_bitmap_zeros(heap_info->ptrmap());
 972     header()->set_heap_oopmap_start_pos(removed_oop_leading_zeros);
 973     header()->set_heap_ptrmap_start_pos(removed_ptr_leading_zeros);
 974 
 975     size_in_bytes += heap_info->oopmap()->size_in_bytes();
 976     size_in_bytes += heap_info->ptrmap()->size_in_bytes();
 977   }
 978 
 979   // The bitmap region contains up to 4 parts:
 980   // rw_ptrmap:           metaspace pointers inside the read-write region
 981   // ro_ptrmap:           metaspace pointers inside the read-only region

 982   // heap_info->oopmap(): Java oop pointers in the heap region
 983   // heap_info->ptrmap(): metaspace pointers in the heap region
 984   char* buffer = NEW_C_HEAP_ARRAY(char, size_in_bytes, mtClassShared);
 985   size_t written = 0;
 986 
 987   region_at(MetaspaceShared::rw)->init_ptrmap(0, rw_ptrmap->size());
 988   written = write_bitmap(rw_ptrmap, buffer, written);
 989 
 990   region_at(MetaspaceShared::ro)->init_ptrmap(written, ro_ptrmap->size());
 991   written = write_bitmap(ro_ptrmap, buffer, written);
 992 





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

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

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

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

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

















































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

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

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






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

2075   }
2076 
2077   if (compact_headers() != UseCompactObjectHeaders) {
2078     aot_log_warning(aot)("Unable to use %s.\nThe %s's UseCompactObjectHeaders setting (%s)"
2079                      " does not equal the current UseCompactObjectHeaders setting (%s).", file_type, file_type,
2080                      _compact_headers          ? "enabled" : "disabled",
2081                      UseCompactObjectHeaders   ? "enabled" : "disabled");
2082     return false;
2083   }
2084 
2085   if (!_use_optimized_module_handling && !CDSConfig::is_dumping_final_static_archive()) {
2086     CDSConfig::stop_using_optimized_module_handling();
2087     aot_log_info(aot)("optimized module handling: disabled because archive was created without optimized module handling");
2088   }
2089 
2090   if (is_static()) {
2091     // Only the static archive can contain the full module graph.
2092     if (!_has_full_module_graph) {
2093       CDSConfig::stop_using_full_module_graph("archive was created without full module graph");
2094     }







2095   }
2096 
2097   return true;
2098 }
2099 
2100 bool FileMapInfo::validate_header() {
2101   if (!header()->validate()) {
2102     return false;
2103   }
2104   if (_is_static) {
2105     return true;
2106   } else {
2107     return DynamicArchive::validate(this);
2108   }
2109 }
2110 
2111 #if INCLUDE_JVMTI
2112 ClassPathEntry** FileMapInfo::_classpath_entries_for_jvmti = nullptr;
2113 
2114 ClassPathEntry* FileMapInfo::get_classpath_entry_for_jvmti(int i, TRAPS) {

 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   // Which JIT compier is used
 236   _compiler_type = (u1)CompilerConfig::compiler_type();
 237   _type_profile_level = TypeProfileLevel;
 238   _type_profile_args_limit = TypeProfileArgsLimit;
 239   _type_profile_parms_limit = TypeProfileParmsLimit;
 240   _type_profile_width = TypeProfileWidth;
 241   _bci_profile_width = BciProfileWidth;
 242   _profile_traps = ProfileTraps;
 243   _type_profile_casts = TypeProfileCasts;
 244   _spec_trap_limit_extra_entries = SpecTrapLimitExtraEntries;
 245   _max_heap_size = MaxHeapSize;
 246   _use_optimized_module_handling = CDSConfig::is_using_optimized_module_handling();
 247   _has_aot_linked_classes = CDSConfig::is_dumping_aot_linked_classes();
 248   _has_full_module_graph = CDSConfig::is_dumping_full_module_graph();
 249   _has_archived_packages = CDSConfig::is_dumping_packages();
 250   _has_archived_protection_domains = CDSConfig::is_dumping_protection_domains();
 251   _gc_kind = (int)Universe::heap()->kind();
 252   jio_snprintf(_gc_name, sizeof(_gc_name), Universe::heap()->name());
 253 
 254   // The following fields are for sanity checks for whether this archive
 255   // will function correctly with this JVM and the bootclasspath it's
 256   // invoked with.
 257 
 258   // JVM version string ... changes on each build.
 259   get_header_version(_jvm_ident);
 260 
 261   _verify_local = BytecodeVerificationLocal;
 262   _verify_remote = BytecodeVerificationRemote;
 263   _has_platform_or_app_classes = AOTClassLocationConfig::dumptime()->has_platform_or_app_classes();
 264   _requested_base_address = (char*)SharedBaseAddress;
 265   _mapped_base_address = (char*)SharedBaseAddress;
 266   _allow_archiving_with_java_agent = AllowArchivingWithJavaAgent;
 267 }
 268 
 269 void FileMapHeader::copy_base_archive_name(const char* archive) {
 270   assert(base_archive_name_size() != 0, "_base_archive_name_size not set");
 271   assert(base_archive_name_offset() != 0, "_base_archive_name_offset not set");
 272   assert(header_size() > sizeof(*this), "_base_archive_name_size not included in header size?");

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

 947 }
 948 
 949 // The sorting code groups the objects with non-null oop/ptrs together.
 950 // Relevant bitmaps then have lots of leading and trailing zeros, which
 951 // we do not have to store.
 952 size_t FileMapInfo::remove_bitmap_zeros(CHeapBitMap* map) {
 953   BitMap::idx_t first_set = map->find_first_set_bit(0);
 954   BitMap::idx_t last_set  = map->find_last_set_bit(0);
 955   size_t old_size = map->size();
 956 
 957   // Slice and resize bitmap
 958   map->truncate(first_set, last_set + 1);
 959 
 960   assert(map->at(0), "First bit should be set");
 961   assert(map->at(map->size() - 1), "Last bit should be set");
 962   assert(map->size() <= old_size, "sanity");
 963 
 964   return first_set;
 965 }
 966 
 967 char* FileMapInfo::write_bitmap_region(CHeapBitMap* rw_ptrmap, CHeapBitMap* ro_ptrmap,
 968                                        CHeapBitMap* ac_ptrmap,
 969                                        ArchiveHeapInfo* heap_info,
 970                                        size_t &size_in_bytes) {
 971   size_t removed_rw_leading_zeros = remove_bitmap_zeros(rw_ptrmap);
 972   size_t removed_ro_leading_zeros = remove_bitmap_zeros(ro_ptrmap);
 973   header()->set_rw_ptrmap_start_pos(removed_rw_leading_zeros);
 974   header()->set_ro_ptrmap_start_pos(removed_ro_leading_zeros);
 975   size_in_bytes = rw_ptrmap->size_in_bytes() + ro_ptrmap->size_in_bytes() + ac_ptrmap->size_in_bytes();
 976 
 977   if (heap_info->is_used()) {
 978     // Remove leading and trailing zeros
 979     size_t removed_oop_leading_zeros = remove_bitmap_zeros(heap_info->oopmap());
 980     size_t removed_ptr_leading_zeros = remove_bitmap_zeros(heap_info->ptrmap());
 981     header()->set_heap_oopmap_start_pos(removed_oop_leading_zeros);
 982     header()->set_heap_ptrmap_start_pos(removed_ptr_leading_zeros);
 983 
 984     size_in_bytes += heap_info->oopmap()->size_in_bytes();
 985     size_in_bytes += heap_info->ptrmap()->size_in_bytes();
 986   }
 987 
 988   // The bitmap region contains up to 4 parts:
 989   // rw_ptrmap:           metaspace pointers inside the read-write region
 990   // ro_ptrmap:           metaspace pointers inside the read-only region
 991   // ac_ptrmap:           metaspace pointers inside the AOT code cache region
 992   // heap_info->oopmap(): Java oop pointers in the heap region
 993   // heap_info->ptrmap(): metaspace pointers in the heap region
 994   char* buffer = NEW_C_HEAP_ARRAY(char, size_in_bytes, mtClassShared);
 995   size_t written = 0;
 996 
 997   region_at(MetaspaceShared::rw)->init_ptrmap(0, rw_ptrmap->size());
 998   written = write_bitmap(rw_ptrmap, buffer, written);
 999 
1000   region_at(MetaspaceShared::ro)->init_ptrmap(written, ro_ptrmap->size());
1001   written = write_bitmap(ro_ptrmap, buffer, written);
1002 
1003   if (ac_ptrmap->size() > 0) {
1004     region_at(MetaspaceShared::ac)->init_ptrmap(written, ac_ptrmap->size());
1005     written = write_bitmap(ac_ptrmap, buffer, written);
1006   }
1007 
1008   if (heap_info->is_used()) {
1009     FileMapRegion* r = region_at(MetaspaceShared::hp);
1010 
1011     r->init_oopmap(written, heap_info->oopmap()->size());
1012     written = write_bitmap(heap_info->oopmap(), buffer, written);
1013 
1014     r->init_ptrmap(written, heap_info->ptrmap()->size());
1015     written = write_bitmap(heap_info->ptrmap(), buffer, written);
1016   }
1017 
1018   write_region(MetaspaceShared::bm, (char*)buffer, size_in_bytes, /*read_only=*/true, /*allow_exec=*/false);
1019   return buffer;
1020 }
1021 
1022 size_t FileMapInfo::write_heap_region(ArchiveHeapInfo* heap_info) {
1023   char* buffer_start = heap_info->buffer_start();
1024   size_t buffer_size = heap_info->buffer_byte_size();
1025   write_region(MetaspaceShared::hp, buffer_start, buffer_size, false, false);
1026   header()->set_heap_root_segments(heap_info->heap_root_segments());
1027   return buffer_size;

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

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

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

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

2147   }
2148 
2149   if (compact_headers() != UseCompactObjectHeaders) {
2150     aot_log_warning(aot)("Unable to use %s.\nThe %s's UseCompactObjectHeaders setting (%s)"
2151                      " does not equal the current UseCompactObjectHeaders setting (%s).", file_type, file_type,
2152                      _compact_headers          ? "enabled" : "disabled",
2153                      UseCompactObjectHeaders   ? "enabled" : "disabled");
2154     return false;
2155   }
2156 
2157   if (!_use_optimized_module_handling && !CDSConfig::is_dumping_final_static_archive()) {
2158     CDSConfig::stop_using_optimized_module_handling();
2159     aot_log_info(aot)("optimized module handling: disabled because archive was created without optimized module handling");
2160   }
2161 
2162   if (is_static()) {
2163     // Only the static archive can contain the full module graph.
2164     if (!_has_full_module_graph) {
2165       CDSConfig::stop_using_full_module_graph("archive was created without full module graph");
2166     }
2167 
2168     if (_has_archived_packages) {
2169       CDSConfig::set_is_loading_packages();
2170     }
2171     if (_has_archived_protection_domains) {
2172       CDSConfig::set_is_loading_protection_domains();
2173     }
2174   }
2175 
2176   return true;
2177 }
2178 
2179 bool FileMapInfo::validate_header() {
2180   if (!header()->validate()) {
2181     return false;
2182   }
2183   if (_is_static) {
2184     return true;
2185   } else {
2186     return DynamicArchive::validate(this);
2187   }
2188 }
2189 
2190 #if INCLUDE_JVMTI
2191 ClassPathEntry** FileMapInfo::_classpath_entries_for_jvmti = nullptr;
2192 
2193 ClassPathEntry* FileMapInfo::get_classpath_entry_for_jvmti(int i, TRAPS) {
< prev index next >