1
1
/*
2
- * Copyright (c) 2003, 2024 , Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2003, 2025 , Oracle and/or its affiliates. All rights reserved.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
@@ -1508,6 +1508,7 @@ void FileMapRegion::init(int region_index, size_t mapping_offset, size_t size, b
1508
1508
_crc = crc;
1509
1509
_mapped_from_file = false ;
1510
1510
_mapped_base = nullptr ;
1511
+ _in_reserved_space = false ;
1511
1512
}
1512
1513
1513
1514
void FileMapRegion::init_oopmap (size_t offset, size_t size_in_bits) {
@@ -1889,10 +1890,11 @@ MapArchiveResult FileMapInfo::map_region(int i, intx addr_delta, char* mapped_ba
1889
1890
FileMapRegion* r = region_at (i);
1890
1891
size_t size = r->used_aligned ();
1891
1892
char *requested_addr = mapped_base_address + r->mapping_offset ();
1892
- assert (r-> mapped_base () == nullptr , " must be not mapped yet" );
1893
+ assert (! is_mapped () , " must be not mapped yet" );
1893
1894
assert (requested_addr != nullptr , " must be specified" );
1894
1895
1895
1896
r->set_mapped_from_file (false );
1897
+ r->set_in_reserved_space (false );
1896
1898
1897
1899
if (MetaspaceShared::use_windows_memory_mapping ()) {
1898
1900
// Windows cannot remap read-only shared memory to read-write when required for
@@ -1917,7 +1919,6 @@ MapArchiveResult FileMapInfo::map_region(int i, intx addr_delta, char* mapped_ba
1917
1919
return MAP_ARCHIVE_OTHER_FAILURE; // oom or I/O error.
1918
1920
} else {
1919
1921
assert (r->mapped_base () != nullptr , " must be initialized" );
1920
- return MAP_ARCHIVE_SUCCESS;
1921
1922
}
1922
1923
} else {
1923
1924
// Note that this may either be a "fresh" mapping into unreserved address
@@ -1939,9 +1940,16 @@ MapArchiveResult FileMapInfo::map_region(int i, intx addr_delta, char* mapped_ba
1939
1940
1940
1941
r->set_mapped_from_file (true );
1941
1942
r->set_mapped_base (requested_addr);
1943
+ }
1942
1944
1943
- return MAP_ARCHIVE_SUCCESS;
1945
+ if (rs.is_reserved ()) {
1946
+ char * mapped_base = r->mapped_base ();
1947
+ assert (rs.base () <= mapped_base && mapped_base + size <= rs.end (),
1948
+ PTR_FORMAT " <= " PTR_FORMAT " < " PTR_FORMAT " <= " PTR_FORMAT,
1949
+ p2i (rs.base ()), p2i (mapped_base), p2i (mapped_base + size), p2i (rs.end ()));
1950
+ r->set_in_reserved_space (rs.is_reserved ());
1944
1951
}
1952
+ return MAP_ARCHIVE_SUCCESS;
1945
1953
}
1946
1954
1947
1955
// The return value is the location of the archive relocation bitmap.
@@ -2359,7 +2367,6 @@ bool FileMapInfo::map_heap_region_impl() {
2359
2367
if (bitmap_base == nullptr ) {
2360
2368
log_info (cds)(" CDS heap cannot be used because bitmap region cannot be mapped" );
2361
2369
dealloc_heap_region ();
2362
- unmap_region (MetaspaceShared::hp);
2363
2370
_heap_pointers_need_patching = false ;
2364
2371
return false ;
2365
2372
}
@@ -2428,8 +2435,14 @@ void FileMapInfo::unmap_region(int i) {
2428
2435
if (size > 0 && r->mapped_from_file ()) {
2429
2436
log_info (cds)(" Unmapping region #%d at base " INTPTR_FORMAT " (%s)" , i, p2i (mapped_base),
2430
2437
shared_region_name[i]);
2431
- if (!os::unmap_memory (mapped_base, size)) {
2432
- fatal (" os::unmap_memory failed" );
2438
+ if (r->in_reserved_space ()) {
2439
+ // This region was mapped inside a ReservedSpace. Its memory will be freed when the ReservedSpace
2440
+ // is released. Zero it so that we don't accidentally read its content.
2441
+ log_info (cds)(" Region #%d (%s) is in a reserved space, it will be freed when the space is released" , i, shared_region_name[i]);
2442
+ } else {
2443
+ if (!os::unmap_memory (mapped_base, size)) {
2444
+ fatal (" os::unmap_memory failed" );
2445
+ }
2433
2446
}
2434
2447
}
2435
2448
r->set_mapped_base (nullptr );
0 commit comments