@@ -1266,7 +1266,7 @@ class FileHeaderHelper {
1266
1266
return false ;
1267
1267
}
1268
1268
1269
- if (!check_crc ()) {
1269
+ if (!check_header_crc ()) {
1270
1270
return false ;
1271
1271
}
1272
1272
@@ -1290,7 +1290,7 @@ class FileHeaderHelper {
1290
1290
}
1291
1291
1292
1292
private:
1293
- bool check_crc () {
1293
+ bool check_header_crc () const {
1294
1294
if (VerifySharedSpaces) {
1295
1295
FileMapHeader* header = (FileMapHeader*)_header;
1296
1296
int actual_crc = header->compute_crc ();
@@ -1593,6 +1593,24 @@ BitMapView FileMapRegion::ptrmap_view() {
1593
1593
return bitmap_view (false );
1594
1594
}
1595
1595
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
+
1596
1614
static const char * region_name (int region_index) {
1597
1615
static const char * names[] = {
1598
1616
" rw" , " ro" , " bm" , " ca0" , " ca1" , " oa0" , " oa1"
@@ -1848,7 +1866,7 @@ bool FileMapInfo::remap_shared_readonly_as_readwrite() {
1848
1866
if (!open_for_read ()) {
1849
1867
return false ;
1850
1868
}
1851
- char *addr = region_addr (idx );
1869
+ char *addr = r-> mapped_base ( );
1852
1870
char *base = os::remap_memory (_fd, _full_path, r->file_offset (),
1853
1871
addr, size, false /* !read_only */ ,
1854
1872
r->allow_exec ());
@@ -1922,7 +1940,10 @@ bool FileMapInfo::read_region(int i, char* base, size_t size, bool do_commit) {
1922
1940
return false ;
1923
1941
}
1924
1942
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 ()) {
1926
1947
return false ;
1927
1948
}
1928
1949
@@ -1960,6 +1981,8 @@ MapArchiveResult FileMapInfo::map_region(int i, intx addr_delta, char* mapped_ba
1960
1981
log_info (cds)(" Failed to read %s shared space into reserved space at " INTPTR_FORMAT,
1961
1982
shared_region_name[i], p2i (requested_addr));
1962
1983
return MAP_ARCHIVE_OTHER_FAILURE; // oom or I/O error.
1984
+ } else {
1985
+ assert (r->mapped_base () != nullptr , " must be initialized" );
1963
1986
}
1964
1987
} else {
1965
1988
// 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
1975
1998
return MAP_ARCHIVE_MMAP_FAILURE;
1976
1999
}
1977
2000
r->set_mapped_from_file (true );
2001
+ r->set_mapped_base (requested_addr);
1978
2002
}
1979
- r->set_mapped_base (requested_addr);
1980
2003
1981
- if (VerifySharedSpaces && !verify_region_checksum (i )) {
2004
+ if (VerifySharedSpaces && !r-> check_region_crc ( )) {
1982
2005
return MAP_ARCHIVE_OTHER_FAILURE;
1983
2006
}
1984
2007
@@ -2000,15 +2023,15 @@ char* FileMapInfo::map_bitmap_region() {
2000
2023
return nullptr ;
2001
2024
}
2002
2025
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 ()) {
2004
2028
log_error (cds)(" relocation bitmap CRC error" );
2005
2029
if (!os::unmap_memory (bitmap_base, r->used_aligned ())) {
2006
2030
fatal (" os::unmap_memory of relocation bitmap failed" );
2007
2031
}
2008
2032
return nullptr ;
2009
2033
}
2010
2034
2011
- r->set_mapped_base (bitmap_base);
2012
2035
r->set_mapped_from_file (true );
2013
2036
log_info (cds)(" Mapped %s region #%d at base " INTPTR_FORMAT " top " INTPTR_FORMAT " (%s)" ,
2014
2037
is_static () ? " static " : " dynamic" ,
@@ -2426,14 +2449,13 @@ bool FileMapInfo::map_heap_regions(int first, int max, bool is_open_archive,
2426
2449
return false ;
2427
2450
}
2428
2451
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 ()) {
2430
2454
// dealloc the regions from java heap
2431
2455
dealloc_heap_regions (regions, num_regions);
2432
2456
log_info (cds)(" UseSharedSpaces: mapped heap regions are corrupt" );
2433
2457
return false ;
2434
2458
}
2435
-
2436
- r->set_mapped_base (base);
2437
2459
}
2438
2460
2439
2461
cleanup._aborted = false ;
@@ -2521,26 +2543,6 @@ void FileMapInfo::dealloc_heap_regions(MemRegion* regions, int num) {
2521
2543
}
2522
2544
#endif // INCLUDE_CDS_JAVA_HEAP
2523
2545
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
-
2544
2546
void FileMapInfo::unmap_regions (int regions[], int num_regions) {
2545
2547
for (int r = 0 ; r < num_regions; r++) {
2546
2548
int idx = regions[r];
@@ -2636,12 +2638,6 @@ bool FileMapInfo::initialize() {
2636
2638
return true ;
2637
2639
}
2638
2640
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
-
2645
2641
// The 2 core spaces are RW->RO
2646
2642
FileMapRegion* FileMapInfo::first_core_region () const {
2647
2643
return region_at (MetaspaceShared::rw);
0 commit comments