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 }
263
264 void FileMapHeader::copy_base_archive_name(const char* archive) {
265 assert(base_archive_name_size() != 0, "_base_archive_name_size not set");
266 assert(base_archive_name_offset() != 0, "_base_archive_name_offset not set");
267 assert(header_size() > sizeof(*this), "_base_archive_name_size not included in header size?");
268 memcpy((char*)this + base_archive_name_offset(), archive, base_archive_name_size());
301 st->print_cr("- serialized_data_offset: 0x%zx", _serialized_data_offset);
302 st->print_cr("- jvm_ident: %s", _jvm_ident);
303 st->print_cr("- class_location_config_offset: 0x%zx", _class_location_config_offset);
304 st->print_cr("- verify_local: %d", _verify_local);
305 st->print_cr("- verify_remote: %d", _verify_remote);
306 st->print_cr("- has_platform_or_app_classes: %d", _has_platform_or_app_classes);
307 st->print_cr("- requested_base_address: " INTPTR_FORMAT, p2i(_requested_base_address));
308 st->print_cr("- mapped_base_address: " INTPTR_FORMAT, p2i(_mapped_base_address));
309 st->print_cr("- heap_root_segments.roots_count: %d" , _heap_root_segments.roots_count());
310 st->print_cr("- heap_root_segments.base_offset: 0x%zx", _heap_root_segments.base_offset());
311 st->print_cr("- heap_root_segments.count: %zu", _heap_root_segments.count());
312 st->print_cr("- heap_root_segments.max_size_elems: %d", _heap_root_segments.max_size_in_elems());
313 st->print_cr("- heap_root_segments.max_size_bytes: %d", _heap_root_segments.max_size_in_bytes());
314 st->print_cr("- _heap_oopmap_start_pos: %zu", _heap_oopmap_start_pos);
315 st->print_cr("- _heap_ptrmap_start_pos: %zu", _heap_ptrmap_start_pos);
316 st->print_cr("- _rw_ptrmap_start_pos: %zu", _rw_ptrmap_start_pos);
317 st->print_cr("- _ro_ptrmap_start_pos: %zu", _ro_ptrmap_start_pos);
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(full_path(), header()->has_aot_linked_classes(), &has_extra_module_paths)) {
329 if (PrintSharedArchiveAndExit) {
330 AOTMetaspace::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 AOTMetaspace::report_loading_error("optimized module handling: disabled because extra module path(s) are specified");
340 }
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(AOTMetaspace::rw)->init_ptrmap(0, rw_ptrmap->size());
986 written = write_bitmap(rw_ptrmap, buffer, written);
987
988 region_at(AOTMetaspace::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(AOTMetaspace::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(AOTMetaspace::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(AOTMetaspace::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 (AOTMetaspace::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 AOTMetaspace::report_loading_error("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 AOTMetaspace::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 AOTMetaspace::report_loading_error("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(AOTMetaspace::ac, requested_base, r->used_aligned(), /* do_commit = */ true)) {
1299 AOTMetaspace::report_loading_error("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 AOTMetaspace::report_loading_error("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 AOTMetaspace::ac, p2i(r->mapped_base()), p2i(r->mapped_end()),
1320 shared_region_name[AOTMetaspace::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 AOTMetaspace::report_loading_error("Cannot use CDS heap data. Selected GC not compatible -XX:-UseCompressedOops");
1467 } else {
1468 AOTMetaspace::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 AOTMetaspace::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(AOTMetaspace::rw);
1896 }
1897
1898 FileMapRegion* FileMapInfo::last_core_region() const {
1899 return region_at(AOTMetaspace::ro);
1900 }
1901
|
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 _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 }
265
266 void FileMapHeader::copy_base_archive_name(const char* archive) {
267 assert(base_archive_name_size() != 0, "_base_archive_name_size not set");
268 assert(base_archive_name_offset() != 0, "_base_archive_name_offset not set");
269 assert(header_size() > sizeof(*this), "_base_archive_name_size not included in header size?");
270 memcpy((char*)this + base_archive_name_offset(), archive, base_archive_name_size());
303 st->print_cr("- serialized_data_offset: 0x%zx", _serialized_data_offset);
304 st->print_cr("- jvm_ident: %s", _jvm_ident);
305 st->print_cr("- class_location_config_offset: 0x%zx", _class_location_config_offset);
306 st->print_cr("- verify_local: %d", _verify_local);
307 st->print_cr("- verify_remote: %d", _verify_remote);
308 st->print_cr("- has_platform_or_app_classes: %d", _has_platform_or_app_classes);
309 st->print_cr("- requested_base_address: " INTPTR_FORMAT, p2i(_requested_base_address));
310 st->print_cr("- mapped_base_address: " INTPTR_FORMAT, p2i(_mapped_base_address));
311 st->print_cr("- heap_root_segments.roots_count: %d" , _heap_root_segments.roots_count());
312 st->print_cr("- heap_root_segments.base_offset: 0x%zx", _heap_root_segments.base_offset());
313 st->print_cr("- heap_root_segments.count: %zu", _heap_root_segments.count());
314 st->print_cr("- heap_root_segments.max_size_elems: %d", _heap_root_segments.max_size_in_elems());
315 st->print_cr("- heap_root_segments.max_size_bytes: %d", _heap_root_segments.max_size_in_bytes());
316 st->print_cr("- _heap_oopmap_start_pos: %zu", _heap_oopmap_start_pos);
317 st->print_cr("- _heap_ptrmap_start_pos: %zu", _heap_ptrmap_start_pos);
318 st->print_cr("- _rw_ptrmap_start_pos: %zu", _rw_ptrmap_start_pos);
319 st->print_cr("- _ro_ptrmap_start_pos: %zu", _ro_ptrmap_start_pos);
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 st->print_cr("- ptrmap_size_in_bits: %zu", _ptrmap_size_in_bits);
324 }
325
326 bool FileMapInfo::validate_class_location() {
327 assert(CDSConfig::is_using_archive(), "runtime only");
328
329 AOTClassLocationConfig* config = header()->class_location_config();
330 bool has_extra_module_paths = false;
331 if (!config->validate(full_path(), header()->has_aot_linked_classes(), &has_extra_module_paths)) {
332 if (PrintSharedArchiveAndExit) {
333 AOTMetaspace::set_archive_loading_failed();
334 return true;
335 } else {
336 return false;
337 }
338 }
339
340 if (header()->has_full_module_graph() && has_extra_module_paths) {
341 CDSConfig::stop_using_optimized_module_handling();
342 AOTMetaspace::report_loading_error("optimized module handling: disabled because extra module path(s) are specified");
343 }
941 }
942
943 // The sorting code groups the objects with non-null oop/ptrs together.
944 // Relevant bitmaps then have lots of leading and trailing zeros, which
945 // we do not have to store.
946 size_t FileMapInfo::remove_bitmap_zeros(CHeapBitMap* map) {
947 BitMap::idx_t first_set = map->find_first_set_bit(0);
948 BitMap::idx_t last_set = map->find_last_set_bit(0);
949 size_t old_size = map->size();
950
951 // Slice and resize bitmap
952 map->truncate(first_set, last_set + 1);
953
954 assert(map->at(0), "First bit should be set");
955 assert(map->at(map->size() - 1), "Last bit should be set");
956 assert(map->size() <= old_size, "sanity");
957
958 return first_set;
959 }
960
961 char* FileMapInfo::write_bitmap_region(CHeapBitMap* rw_ptrmap, CHeapBitMap* ro_ptrmap,
962 CHeapBitMap* ac_ptrmap,
963 ArchiveHeapInfo* heap_info,
964 size_t &size_in_bytes) {
965 size_t removed_rw_leading_zeros = remove_bitmap_zeros(rw_ptrmap);
966 size_t removed_ro_leading_zeros = remove_bitmap_zeros(ro_ptrmap);
967 header()->set_rw_ptrmap_start_pos(removed_rw_leading_zeros);
968 header()->set_ro_ptrmap_start_pos(removed_ro_leading_zeros);
969 size_in_bytes = rw_ptrmap->size_in_bytes() + ro_ptrmap->size_in_bytes() + ac_ptrmap->size_in_bytes();
970
971 if (heap_info->is_used()) {
972 // Remove leading and trailing zeros
973 size_t removed_oop_leading_zeros = remove_bitmap_zeros(heap_info->oopmap());
974 size_t removed_ptr_leading_zeros = remove_bitmap_zeros(heap_info->ptrmap());
975 header()->set_heap_oopmap_start_pos(removed_oop_leading_zeros);
976 header()->set_heap_ptrmap_start_pos(removed_ptr_leading_zeros);
977
978 size_in_bytes += heap_info->oopmap()->size_in_bytes();
979 size_in_bytes += heap_info->ptrmap()->size_in_bytes();
980 }
981
982 // The bitmap region contains up to 4 parts:
983 // rw_ptrmap: metaspace pointers inside the read-write region
984 // ro_ptrmap: metaspace pointers inside the read-only region
985 // ac_ptrmap: metaspace pointers inside the AOT code cache region
986 // heap_info->oopmap(): Java oop pointers in the heap region
987 // heap_info->ptrmap(): metaspace pointers in the heap region
988 char* buffer = NEW_C_HEAP_ARRAY(char, size_in_bytes, mtClassShared);
989 size_t written = 0;
990
991 region_at(AOTMetaspace::rw)->init_ptrmap(0, rw_ptrmap->size());
992 written = write_bitmap(rw_ptrmap, buffer, written);
993
994 region_at(AOTMetaspace::ro)->init_ptrmap(written, ro_ptrmap->size());
995 written = write_bitmap(ro_ptrmap, buffer, written);
996
997 if (ac_ptrmap->size() > 0) {
998 region_at(AOTMetaspace::ac)->init_ptrmap(written, ac_ptrmap->size());
999 written = write_bitmap(ac_ptrmap, buffer, written);
1000 }
1001
1002 if (heap_info->is_used()) {
1003 FileMapRegion* r = region_at(AOTMetaspace::hp);
1004
1005 r->init_oopmap(written, heap_info->oopmap()->size());
1006 written = write_bitmap(heap_info->oopmap(), buffer, written);
1007
1008 r->init_ptrmap(written, heap_info->ptrmap()->size());
1009 written = write_bitmap(heap_info->ptrmap(), buffer, written);
1010 }
1011
1012 write_region(AOTMetaspace::bm, (char*)buffer, size_in_bytes, /*read_only=*/true, /*allow_exec=*/false);
1013 return buffer;
1014 }
1015
1016 size_t FileMapInfo::write_heap_region(ArchiveHeapInfo* heap_info) {
1017 char* buffer_start = heap_info->buffer_start();
1018 size_t buffer_size = heap_info->buffer_byte_size();
1019 write_region(AOTMetaspace::hp, buffer_start, buffer_size, false, false);
1020 header()->set_heap_root_segments(heap_info->heap_root_segments());
1021 return buffer_size;
1220 } else if (addr_delta != 0) {
1221 r->set_read_only(false); // Need to patch the pointers
1222 }
1223
1224 if (AOTMetaspace::use_windows_memory_mapping() && rs.is_reserved()) {
1225 // This is the second time we try to map the archive(s). We have already created a ReservedSpace
1226 // that covers all the FileMapRegions to ensure all regions can be mapped. However, Windows
1227 // can't mmap into a ReservedSpace, so we just ::read() the data. We're going to patch all the
1228 // regions anyway, so there's no benefit for mmap anyway.
1229 if (!read_region(i, requested_addr, size, /* do_commit = */ true)) {
1230 AOTMetaspace::report_loading_error("Failed to read %s shared space into reserved space at " INTPTR_FORMAT,
1231 shared_region_name[i], p2i(requested_addr));
1232 return MAP_ARCHIVE_OTHER_FAILURE; // oom or I/O error.
1233 } else {
1234 assert(r->mapped_base() != nullptr, "must be initialized");
1235 }
1236 } else {
1237 // Note that this may either be a "fresh" mapping into unreserved address
1238 // space (Windows, first mapping attempt), or a mapping into pre-reserved
1239 // space (Posix). See also comment in AOTMetaspace::map_archives().
1240 bool read_only = r->read_only() && !CDSConfig::is_dumping_final_static_archive();
1241
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 AOTMetaspace::report_loading_error("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(AOTMetaspace::ac, requested_base, r->used_aligned(), /* do_commit = */ true)) {
1312 AOTMetaspace::report_loading_error("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 AOTMetaspace::report_loading_error("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 AOTMetaspace::ac, p2i(r->mapped_base()), p2i(r->mapped_end()),
1334 shared_region_name[AOTMetaspace::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(AOTMetaspace::ac);
1368 char* bitmap_base = map_bitmap_region();
1369
1370 BitMapView ac_ptrmap = ptrmap_view(AOTMetaspace::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 AOTMetaspace::report_loading_error("Cannot use CDS heap data. Selected GC not compatible -XX:-UseCompressedOops");
1530 } else {
1531 AOTMetaspace::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 AOTMetaspace::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(AOTMetaspace::rw);
1965 }
1966
1967 FileMapRegion* FileMapInfo::last_core_region() const {
1968 return region_at(AOTMetaspace::ro);
1969 }
1970
|