Skip to content

Commit a6903f8

Browse files
committedDec 22, 2021
8279018: CRC calculation in CDS should not include _version and _head_size
Reviewed-by: iklam, ccheung
1 parent 3f41fde commit a6903f8

File tree

7 files changed

+56
-13
lines changed

7 files changed

+56
-13
lines changed
 

‎src/hotspot/share/cds/filemap.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -2404,8 +2404,8 @@ void FileMapHeader::set_as_offset(char* p, size_t *offset) {
24042404

24052405
int FileMapHeader::compute_crc() {
24062406
char* start = (char*)this;
2407-
// start computing from the field after _crc to end of base archive name.
2408-
char* buf = (char*)&(_generic_header._crc) + sizeof(_generic_header._crc);
2407+
// start computing from the field after _header_size to end of base archive name.
2408+
char* buf = (char*)&(_generic_header._header_size) + sizeof(_generic_header._header_size);
24092409
size_t sz = header_size() - (buf - start);
24102410
int crc = ClassLoader::crc32(0, buf, (jint)sz);
24112411
return crc;

‎src/hotspot/share/include/cds.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@
3838
#define NUM_CDS_REGIONS 7 // this must be the same as MetaspaceShared::n_regions
3939
#define CDS_ARCHIVE_MAGIC 0xf00baba2
4040
#define CDS_DYNAMIC_ARCHIVE_MAGIC 0xf00baba8
41-
#define CDS_GENERIC_HEADER_SUPPORTED_MIN_VERSION 12
42-
#define CURRENT_CDS_ARCHIVE_VERSION 12
41+
#define CDS_GENERIC_HEADER_SUPPORTED_MIN_VERSION 13
42+
#define CURRENT_CDS_ARCHIVE_VERSION 13
4343

4444
typedef struct CDSFileMapRegion {
4545
int _crc; // CRC checksum of this region.
@@ -66,7 +66,7 @@ typedef struct CDSFileMapRegion {
6666
// a different version of HotSpot, so that we can automatically regenerate the archive as necessary.
6767
typedef struct GenericCDSFileMapHeader {
6868
unsigned int _magic; // identification of file type
69-
int _crc; // header crc checksum
69+
int _crc; // header crc checksum, start from _base_archive_name_offset
7070
int _version; // CURRENT_CDS_ARCHIVE_VERSION of the jdk that dumped the this archive
7171
unsigned int _header_size; // total size of the header, in bytes
7272
unsigned int _base_archive_name_offset; // offset where the base archive name is stored

‎src/hotspot/share/prims/whitebox.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
#include "precompiled.hpp"
2626
#include <new>
27+
#include "cds.h"
2728
#include "cds/cdsConstants.hpp"
2829
#include "cds/filemap.hpp"
2930
#include "cds/heapShared.inline.hpp"
@@ -1962,6 +1963,24 @@ WB_ENTRY(jboolean, WB_IsSharingEnabled(JNIEnv* env, jobject wb))
19621963
return UseSharedSpaces;
19631964
WB_END
19641965

1966+
WB_ENTRY(jint, WB_GetCDSGenericHeaderMinVersion(JNIEnv* env, jobject wb))
1967+
#if INCLUDE_CDS
1968+
return (jint)CDS_GENERIC_HEADER_SUPPORTED_MIN_VERSION;
1969+
#else
1970+
ShouldNotReachHere();
1971+
return (jint)-1;
1972+
#endif
1973+
WB_END
1974+
1975+
WB_ENTRY(jint, WB_GetCDSCurrentVersion(JNIEnv* env, jobject wb))
1976+
#if INCLUDE_CDS
1977+
return (jint)CURRENT_CDS_ARCHIVE_VERSION;
1978+
#else
1979+
ShouldNotReachHere();
1980+
return (jint)-1;
1981+
#endif
1982+
WB_END
1983+
19651984
WB_ENTRY(jboolean, WB_CDSMemoryMappingFailed(JNIEnv* env, jobject wb))
19661985
return FileMapInfo::memory_mapping_failed();
19671986
WB_END
@@ -2684,6 +2703,8 @@ static JNINativeMethod methods[] = {
26842703
(void*)&WB_GetMethodStringOption},
26852704
{CC"getDefaultArchivePath", CC"()Ljava/lang/String;",
26862705
(void*)&WB_GetDefaultArchivePath},
2706+
{CC"getCDSGenericHeaderMinVersion", CC"()I", (void*)&WB_GetCDSGenericHeaderMinVersion},
2707+
{CC"getCurrentCDSVersion", CC"()I", (void*)&WB_GetCDSCurrentVersion},
26872708
{CC"isSharingEnabled", CC"()Z", (void*)&WB_IsSharingEnabled},
26882709
{CC"isShared", CC"(Ljava/lang/Object;)Z", (void*)&WB_IsShared },
26892710
{CC"isSharedInternedString", CC"(Ljava/lang/String;)Z", (void*)&WB_IsSharedInternedString },

‎test/hotspot/jtreg/runtime/cds/appcds/SharedArchiveConsistency.java

+16-8
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ public class SharedArchiveConsistency {
4343
public static boolean shareAuto; // true == -Xshare:auto
4444
// false == -Xshare:on
4545

46+
private static int genericHeaderMinVersion; // minimum supported CDS version
47+
private static int currentCDSArchiveVersion; // current CDS version in java process
4648
// The following should be consistent with the enum in the C++ MetaspaceShared class
4749
public static String[] shared_region_name = {
4850
"rw", // ReadWrite
@@ -104,6 +106,9 @@ public static void main(String... args) throws Exception {
104106
throw new RuntimeException("Arg must be 'on' or 'auto'");
105107
}
106108
shareAuto = args[0].equals("auto");
109+
genericHeaderMinVersion = CDSArchiveUtils.getGenericHeaderMinVersion();
110+
currentCDSArchiveVersion = CDSArchiveUtils.getCurrentCDSArchiveVersion();
111+
107112
String jarFile = JarBuilder.getOrCreateHelloJar();
108113

109114
// dump (appcds.jsa created)
@@ -158,7 +163,7 @@ public static void main(String... args) throws Exception {
158163
}
159164

160165
// modify _magic, test should fail
161-
System.out.println("\n2c. Corrupt _magic, should fail\n");
166+
System.out.println("\n2b. Corrupt _magic, should fail\n");
162167
String modMagic = startNewArchive("modify-magic");
163168
copiedJsa = CDSArchiveUtils.copyArchiveFile(orgJsaFile, modMagic);
164169
CDSArchiveUtils.modifyHeaderIntField(copiedJsa, CDSArchiveUtils.offsetMagic(), -1);
@@ -170,23 +175,26 @@ public static void main(String... args) throws Exception {
170175
}
171176

172177
// modify _version, test should fail
173-
System.out.println("\n2d. Corrupt _version, should fail\n");
178+
System.out.println("\n2c. Corrupt _version, should fail\n");
174179
String modVersion = startNewArchive("modify-version");
180+
int version = currentCDSArchiveVersion + 1;
175181
copiedJsa = CDSArchiveUtils.copyArchiveFile(orgJsaFile, modVersion);
176-
CDSArchiveUtils.modifyHeaderIntField(copiedJsa, CDSArchiveUtils.offsetVersion(), 0x3FFFFFFF);
182+
CDSArchiveUtils.modifyHeaderIntField(copiedJsa, CDSArchiveUtils.offsetVersion(), version);
177183
output = shareAuto ? TestCommon.execAuto(execArgs) : TestCommon.execCommon(execArgs);
178-
output.shouldContain("The shared archive file has the wrong version");
179-
output.shouldNotContain("Checksum verification failed");
184+
output.shouldContain("The shared archive file has the wrong version")
185+
.shouldContain("_version expected: " + currentCDSArchiveVersion)
186+
.shouldContain("actual: " + version);
180187
if (shareAuto) {
181188
output.shouldContain(HELLO_WORLD);
182189
}
183190

184-
System.out.println("\n2e. Corrupt _version, should fail\n");
191+
System.out.println("\n2d. Corrupt _version, should fail\n");
185192
String modVersion2 = startNewArchive("modify-version2");
186193
copiedJsa = CDSArchiveUtils.copyArchiveFile(orgJsaFile, modVersion2);
187-
CDSArchiveUtils.modifyHeaderIntField(copiedJsa, CDSArchiveUtils.offsetVersion(), 0x00000000);
194+
version = genericHeaderMinVersion - 1;
195+
CDSArchiveUtils.modifyHeaderIntField(copiedJsa, CDSArchiveUtils.offsetVersion(), version);
188196
output = shareAuto ? TestCommon.execAuto(execArgs) : TestCommon.execCommon(execArgs);
189-
output.shouldContain("Cannot handle shared archive file version 0. Must be at least 12");
197+
output.shouldContain("Cannot handle shared archive file version " + version + ". Must be at least " + genericHeaderMinVersion);
190198
output.shouldNotContain("Checksum verification failed");
191199
if (shareAuto) {
192200
output.shouldContain(HELLO_WORLD);

‎test/lib/jdk/test/lib/cds/CDSArchiveUtils.java

+10
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@
4545

4646
// This class performs operations on shared archive file
4747
public class CDSArchiveUtils {
48+
// Minimum supported CDS file header version
49+
private static int genericHeaderMinVersion; // CDS_GENERIC_HEADER_SUPPORTED_MIN_VERSION
50+
private static int currentCDSArchiveVersion; // CURRENT_CDS_ARCHIVE_VERSION
4851
// offsets
4952
private static int offsetMagic; // offset of GenericCDSFileMapHeader::_magic
5053
private static int offsetCrc; // offset of GenericCDSFileMapHeader::_crc
@@ -82,6 +85,9 @@ public class CDSArchiveUtils {
8285
WhiteBox wb;
8386
try {
8487
wb = WhiteBox.getWhiteBox();
88+
// genericHeaderMinVersion
89+
genericHeaderMinVersion = wb.getCDSGenericHeaderMinVersion();
90+
currentCDSArchiveVersion = wb.getCurrentCDSVersion();
8591
// offsets
8692
offsetMagic = wb.getCDSOffsetForName("GenericCDSFileMapHeader::_magic");
8793
offsetCrc = wb.getCDSOffsetForName("GenericCDSFileMapHeader::_crc");
@@ -116,6 +122,10 @@ public class CDSArchiveUtils {
116122
}
117123

118124
// accessors
125+
// minimum supported file header version
126+
public static int getGenericHeaderMinVersion() { return genericHeaderMinVersion; }
127+
// current CDS version
128+
public static int getCurrentCDSArchiveVersion() { return currentCDSArchiveVersion; }
119129
// offsets
120130
public static int offsetMagic() { return offsetMagic; }
121131
public static int offsetCrc() { return offsetCrc; }

‎test/lib/jdk/test/whitebox/WhiteBox.java

+2
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,8 @@ public Object getMethodOption(Executable method, String name) {
623623
}
624624

625625
// Sharing & archiving
626+
public native int getCDSGenericHeaderMinVersion();
627+
public native int getCurrentCDSVersion();
626628
public native String getDefaultArchivePath();
627629
public native boolean cdsMemoryMappingFailed();
628630
public native boolean isSharingEnabled();

‎test/lib/sun/hotspot/WhiteBox.java

+2
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,8 @@ public Object getMethodOption(Executable method, String name) {
624624
}
625625

626626
// Sharing & archiving
627+
public native int getCDSGenericHeaderMinVersion();
628+
public native int getCurrentCDSVersion();
627629
public native String getDefaultArchivePath();
628630
public native boolean cdsMemoryMappingFailed();
629631
public native boolean isSharingEnabled();

0 commit comments

Comments
 (0)
Please sign in to comment.