Skip to content

Commit 49eb68b

Browse files
committed
8296158: Refactor the verification of CDS region checksum
Reviewed-by: ccheung, matsaave
1 parent 655a712 commit 49eb68b

File tree

3 files changed

+35
-42
lines changed

3 files changed

+35
-42
lines changed

src/hotspot/share/cds/archiveHeapLoader.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ bool ArchiveHeapLoader::load_regions(FileMapInfo* mapinfo, LoadedArchiveHeapRegi
410410
bm.iterate(&patcher);
411411
}
412412

413-
r->set_mapped_base((char*)load_address);
413+
assert(r->mapped_base() == (char*)load_address, "sanity");
414414
load_address += r->used();
415415
}
416416

src/hotspot/share/cds/filemap.cpp

+33-37
Original file line numberDiff line numberDiff line change
@@ -1266,7 +1266,7 @@ class FileHeaderHelper {
12661266
return false;
12671267
}
12681268

1269-
if (!check_crc()) {
1269+
if (!check_header_crc()) {
12701270
return false;
12711271
}
12721272

@@ -1290,7 +1290,7 @@ class FileHeaderHelper {
12901290
}
12911291

12921292
private:
1293-
bool check_crc() {
1293+
bool check_header_crc() const {
12941294
if (VerifySharedSpaces) {
12951295
FileMapHeader* header = (FileMapHeader*)_header;
12961296
int actual_crc = header->compute_crc();
@@ -1593,6 +1593,24 @@ BitMapView FileMapRegion::ptrmap_view() {
15931593
return bitmap_view(false);
15941594
}
15951595

1596+
bool FileMapRegion::check_region_crc() const {
1597+
// This function should be called after the region has been properly
1598+
// loaded into memory via FileMapInfo::map_region() or FileMapInfo::read_region().
1599+
// I.e., this->mapped_base() must be valid.
1600+
size_t sz = used();
1601+
if (sz == 0) {
1602+
return true;
1603+
}
1604+
1605+
assert(mapped_base() != nullptr, "must be initialized");
1606+
int crc = ClassLoader::crc32(0, mapped_base(), (jint)sz);
1607+
if (crc != this->crc()) {
1608+
FileMapInfo::fail_continue("Checksum verification failed.");
1609+
return false;
1610+
}
1611+
return true;
1612+
}
1613+
15961614
static const char* region_name(int region_index) {
15971615
static const char* names[] = {
15981616
"rw", "ro", "bm", "ca0", "ca1", "oa0", "oa1"
@@ -1848,7 +1866,7 @@ bool FileMapInfo::remap_shared_readonly_as_readwrite() {
18481866
if (!open_for_read()) {
18491867
return false;
18501868
}
1851-
char *addr = region_addr(idx);
1869+
char *addr = r->mapped_base();
18521870
char *base = os::remap_memory(_fd, _full_path, r->file_offset(),
18531871
addr, size, false /* !read_only */,
18541872
r->allow_exec());
@@ -1922,7 +1940,10 @@ bool FileMapInfo::read_region(int i, char* base, size_t size, bool do_commit) {
19221940
return false;
19231941
}
19241942

1925-
if (VerifySharedSpaces && !region_crc_check(base, r->used(), r->crc())) {
1943+
r->set_mapped_from_file(false);
1944+
r->set_mapped_base(base);
1945+
1946+
if (VerifySharedSpaces && !r->check_region_crc()) {
19261947
return false;
19271948
}
19281949

@@ -1960,6 +1981,8 @@ MapArchiveResult FileMapInfo::map_region(int i, intx addr_delta, char* mapped_ba
19601981
log_info(cds)("Failed to read %s shared space into reserved space at " INTPTR_FORMAT,
19611982
shared_region_name[i], p2i(requested_addr));
19621983
return MAP_ARCHIVE_OTHER_FAILURE; // oom or I/O error.
1984+
} else {
1985+
assert(r->mapped_base() != nullptr, "must be initialized");
19631986
}
19641987
} else {
19651988
// Note that this may either be a "fresh" mapping into unreserved address
@@ -1975,10 +1998,10 @@ MapArchiveResult FileMapInfo::map_region(int i, intx addr_delta, char* mapped_ba
19751998
return MAP_ARCHIVE_MMAP_FAILURE;
19761999
}
19772000
r->set_mapped_from_file(true);
2001+
r->set_mapped_base(requested_addr);
19782002
}
1979-
r->set_mapped_base(requested_addr);
19802003

1981-
if (VerifySharedSpaces && !verify_region_checksum(i)) {
2004+
if (VerifySharedSpaces && !r->check_region_crc()) {
19822005
return MAP_ARCHIVE_OTHER_FAILURE;
19832006
}
19842007

@@ -2000,15 +2023,15 @@ char* FileMapInfo::map_bitmap_region() {
20002023
return nullptr;
20012024
}
20022025

2003-
if (VerifySharedSpaces && !region_crc_check(bitmap_base, r->used(), r->crc())) {
2026+
r->set_mapped_base(bitmap_base);
2027+
if (VerifySharedSpaces && !r->check_region_crc()) {
20042028
log_error(cds)("relocation bitmap CRC error");
20052029
if (!os::unmap_memory(bitmap_base, r->used_aligned())) {
20062030
fatal("os::unmap_memory of relocation bitmap failed");
20072031
}
20082032
return nullptr;
20092033
}
20102034

2011-
r->set_mapped_base(bitmap_base);
20122035
r->set_mapped_from_file(true);
20132036
log_info(cds)("Mapped %s region #%d at base " INTPTR_FORMAT " top " INTPTR_FORMAT " (%s)",
20142037
is_static() ? "static " : "dynamic",
@@ -2426,14 +2449,13 @@ bool FileMapInfo::map_heap_regions(int first, int max, bool is_open_archive,
24262449
return false;
24272450
}
24282451

2429-
if (VerifySharedSpaces && !region_crc_check(addr, regions[i].byte_size(), r->crc())) {
2452+
r->set_mapped_base(base);
2453+
if (VerifySharedSpaces && !r->check_region_crc()) {
24302454
// dealloc the regions from java heap
24312455
dealloc_heap_regions(regions, num_regions);
24322456
log_info(cds)("UseSharedSpaces: mapped heap regions are corrupt");
24332457
return false;
24342458
}
2435-
2436-
r->set_mapped_base(base);
24372459
}
24382460

24392461
cleanup._aborted = false;
@@ -2521,26 +2543,6 @@ void FileMapInfo::dealloc_heap_regions(MemRegion* regions, int num) {
25212543
}
25222544
#endif // INCLUDE_CDS_JAVA_HEAP
25232545

2524-
bool FileMapInfo::region_crc_check(char* buf, size_t size, int expected_crc) {
2525-
int crc = ClassLoader::crc32(0, buf, (jint)size);
2526-
if (crc != expected_crc) {
2527-
fail_continue("Checksum verification failed.");
2528-
return false;
2529-
}
2530-
return true;
2531-
}
2532-
2533-
bool FileMapInfo::verify_region_checksum(int i) {
2534-
assert(VerifySharedSpaces, "sanity");
2535-
size_t sz = region_at(i)->used();
2536-
2537-
if (sz == 0) {
2538-
return true; // no data
2539-
} else {
2540-
return region_crc_check(region_addr(i), sz, region_at(i)->crc());
2541-
}
2542-
}
2543-
25442546
void FileMapInfo::unmap_regions(int regions[], int num_regions) {
25452547
for (int r = 0; r < num_regions; r++) {
25462548
int idx = regions[r];
@@ -2636,12 +2638,6 @@ bool FileMapInfo::initialize() {
26362638
return true;
26372639
}
26382640

2639-
char* FileMapInfo::region_addr(int idx) {
2640-
assert(UseSharedSpaces, "must be");
2641-
FileMapRegion* r = region_at(idx);
2642-
return r->mapped_base();
2643-
}
2644-
26452641
// The 2 core spaces are RW->RO
26462642
FileMapRegion* FileMapInfo::first_core_region() const {
26472643
return region_at(MetaspaceShared::rw);

src/hotspot/share/cds/filemap.hpp

+1-4
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ class FileMapRegion: private CDSFileMapRegion {
178178
BitMapView ptrmap_view();
179179
bool has_ptrmap() { return _ptrmap_size_in_bits != 0; }
180180

181+
bool check_region_crc() const;
181182
void print(outputStream* st, int region_index);
182183
};
183184

@@ -473,7 +474,6 @@ class FileMapInfo : public CHeapObj<mtInternal> {
473474
bool read_region(int i, char* base, size_t size, bool do_commit);
474475
char* map_bitmap_region();
475476
void unmap_region(int i);
476-
bool verify_region_checksum(int i);
477477
void close();
478478
bool is_open() { return _file_open; }
479479
ReservedSpace reserve_shared_memory();
@@ -526,8 +526,6 @@ class FileMapInfo : public CHeapObj<mtInternal> {
526526

527527
static int get_module_shared_path_index(Symbol* location) NOT_CDS_RETURN_(-1);
528528

529-
char* region_addr(int idx);
530-
531529
// The offset of the first core region in the archive, relative to SharedBaseAddress
532530
size_t mapping_base_offset() const { return first_core_region()->mapping_offset(); }
533531
// The offset of the (exclusive) end of the last core region in this archive, relative to SharedBaseAddress
@@ -575,7 +573,6 @@ class FileMapInfo : public CHeapObj<mtInternal> {
575573
bool validate_app_class_paths(int shared_app_paths_len) NOT_CDS_RETURN_(false);
576574
bool map_heap_regions(int first, int max, bool is_open_archive,
577575
MemRegion** regions_ret, int* num_regions_ret) NOT_CDS_JAVA_HEAP_RETURN_(false);
578-
bool region_crc_check(char* buf, size_t size, int expected_crc) NOT_CDS_RETURN_(false);
579576
void dealloc_heap_regions(MemRegion* regions, int num) NOT_CDS_JAVA_HEAP_RETURN;
580577
bool can_use_heap_regions();
581578
bool load_heap_regions() NOT_CDS_JAVA_HEAP_RETURN_(false);

0 commit comments

Comments
 (0)