Skip to content

Commit 3dcd365

Browse files
María Arias de Reyna Domínguezrwestrel
authored andcommitted
8382166: [AOT Cache] CRC was not checked on Code Region
Reviewed-by: kvn, asmehra, iklam, adinn
1 parent 7d1ce05 commit 3dcd365

5 files changed

Lines changed: 110 additions & 0 deletions

File tree

src/hotspot/share/cds/filemap.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,6 +1362,13 @@ bool FileMapInfo::map_aot_code_region(ReservedSpace rs) {
13621362
return false;
13631363
} else {
13641364
assert(mapped_base == requested_base, "must be");
1365+
1366+
if (VerifySharedSpaces && !r->check_region_crc(mapped_base)) {
1367+
aot_log_error(aot)("region %d CRC error", AOTMetaspace::ac);
1368+
os::unmap_memory(mapped_base, r->used_aligned());
1369+
return false;
1370+
}
1371+
13651372
r->set_mapped_from_file(true);
13661373
r->set_mapped_base(mapped_base);
13671374
aot_log_info(aot)("Mapped static region #%d at base " INTPTR_FORMAT " top " INTPTR_FORMAT " (%s)",
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright (c) 2026, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*
23+
*/
24+
25+
/*
26+
* @test
27+
* @key randomness
28+
* @summary AOTCacheConsistency This test checks that there is a CRC validation of the AOT Cache regions.
29+
* @bug 8382166
30+
* @requires vm.cds.supports.aot.class.linking
31+
* @library /test/lib
32+
* @build jdk.test.whitebox.WhiteBox AOTCacheConsistency HelloWorld
33+
* @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
34+
* @run driver jdk.test.lib.helpers.ClassFileInstaller -jar app.jar HelloWorld
35+
* @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI AOTCacheConsistency
36+
*/
37+
38+
import jdk.test.lib.cds.CDSArchiveUtils;
39+
import jdk.test.lib.cds.SimpleCDSAppTester;
40+
import jdk.test.lib.process.OutputAnalyzer;
41+
import java.io.File;
42+
43+
public class AOTCacheConsistency {
44+
public static void main(String args[]) throws Exception {
45+
// Train and run the app
46+
SimpleCDSAppTester tester = SimpleCDSAppTester.of("AOTCacheConsistency")
47+
.classpath("app.jar")
48+
.appCommandLine("HelloWorld")
49+
.setProductionChecker((OutputAnalyzer out) -> {
50+
out.shouldContain("HelloWorld");
51+
})
52+
.runAOTWorkflow();
53+
54+
String aotCache = tester.aotCacheFile();
55+
56+
String[] regions = CDSArchiveUtils.getRegions();
57+
String orig = aotCache + ".orig";
58+
CDSArchiveUtils.copyArchiveFile(new File(aotCache), orig); // save original copy
59+
60+
// Modify each of the region individually. The production should fail to run
61+
// with these args;
62+
String extraVMArgs[] = {"-XX:+VerifySharedSpaces", "-XX:AOTMode=on"};
63+
tester.setCheckExitValue(false);
64+
65+
for (int i = 0; i < regions.length; i++) {
66+
File f = CDSArchiveUtils.copyArchiveFile(new File(orig), aotCache);
67+
System.out.println("\n=======\nTesting region " + i + " = " + regions[i]);
68+
if (CDSArchiveUtils.modifyRegionContent(i, f)) {
69+
tester.setProductionChecker((OutputAnalyzer out) -> {
70+
out.shouldContain("Checksum verification failed.");
71+
});
72+
tester.rerunProduction(extraVMArgs);
73+
}
74+
}
75+
}
76+
}

test/lib/jdk/test/lib/cds/CDSAppTester.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ abstract public class CDSAppTester {
6060
private boolean generateBaseArchive = false;
6161
private String[] baseArchiveOptions = new String[0];
6262

63+
public String aotCacheFile() {
64+
return this.aotCacheFile;
65+
}
66+
6367
/**
6468
* All files created in the CDS/AOT workflow will be name + extension. E.g.
6569
* - name.aot

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,14 @@ public class CDSArchiveUtils {
7575
"ro", // ReadOnly
7676
"bm", // relocation bitmaps
7777
"hp", // heap
78+
"ac", // aot code
7879
};
7980
private static int num_regions = shared_region_name.length;
8081

82+
public static String[] getRegions() {
83+
return shared_region_name;
84+
}
85+
8186
static {
8287
WhiteBox wb;
8388
try {

test/lib/jdk/test/lib/cds/SimpleCDSAppTester.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,4 +211,22 @@ public SimpleCDSAppTester run(String args[]) throws Exception {
211211
tester.run(args);
212212
return this;
213213
}
214+
215+
public SimpleCDSAppTester rerunProduction(String... extraVmArgs) throws Exception {
216+
tester.productionRun(extraVmArgs);
217+
return this;
218+
}
219+
220+
public SimpleCDSAppTester rerunProduction(String[] extraVmArgs, String... extraAppArgs) throws Exception {
221+
tester.productionRun(extraVmArgs, extraAppArgs);
222+
return this;
223+
}
224+
225+
public String aotCacheFile() {
226+
return tester.aotCacheFile();
227+
}
228+
229+
public void setCheckExitValue(boolean b) {
230+
tester.setCheckExitValue(b);
231+
}
214232
}

0 commit comments

Comments
 (0)