Skip to content

Commit be64bf8

Browse files
ashu-mehracalvinccheung
authored andcommitted
8299329: Assertion failure with fastdebug build when trying to use CDS without classpath
Reviewed-by: iklam, ccheung
1 parent 8723847 commit be64bf8

File tree

3 files changed

+44
-9
lines changed

3 files changed

+44
-9
lines changed

src/hotspot/share/cds/filemap.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -880,11 +880,13 @@ unsigned int FileMapInfo::longest_common_app_classpath_prefix_len(int num_paths,
880880
if (rp_array->at(i)[pos] != '\0' && rp_array->at(i)[pos] == rp_array->at(0)[pos]) {
881881
continue;
882882
}
883-
884883
// search backward for the pos before the file separator char
885-
while (pos > 0 && rp_array->at(0)[--pos] != *os::file_separator());
886-
// return the file separator char position
887-
return pos + 1;
884+
while (pos > 0) {
885+
if (rp_array->at(0)[--pos] == *os::file_separator()) {
886+
return pos + 1;
887+
}
888+
}
889+
return 0;
888890
}
889891
}
890892
return 0;
@@ -1022,8 +1024,12 @@ bool FileMapInfo::validate_app_class_paths(int shared_app_paths_len) {
10221024
// java -Xshare:auto -cp /x/y/Foo.jar:/x/y/b/Bar.jar ...
10231025
unsigned int dumptime_prefix_len = header()->common_app_classpath_prefix_size();
10241026
unsigned int runtime_prefix_len = longest_common_app_classpath_prefix_len(shared_app_paths_len, rp_array);
1025-
mismatch = check_paths(j, shared_app_paths_len, rp_array,
1026-
dumptime_prefix_len, runtime_prefix_len);
1027+
if (dumptime_prefix_len != 0 || runtime_prefix_len != 0) {
1028+
log_info(class, path)("LCP length for app classpath (dumptime: %u, runtime: %u)",
1029+
dumptime_prefix_len, runtime_prefix_len);
1030+
mismatch = check_paths(j, shared_app_paths_len, rp_array,
1031+
dumptime_prefix_len, runtime_prefix_len);
1032+
}
10271033
if (mismatch) {
10281034
return classpath_failure("[APP classpath mismatch, actual: -Djava.class.path=", appcp);
10291035
}

test/hotspot/jtreg/runtime/cds/appcds/WrongClasspath.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
*/
3434

3535
import java.io.File;
36+
import java.nio.file.Files;
37+
import java.nio.file.Paths;
38+
import java.nio.file.StandardCopyOption;
3639
import jdk.test.lib.process.OutputAnalyzer;
3740
import jdk.test.lib.helpers.ClassFileInstaller;
3841

@@ -44,10 +47,26 @@ public static void main(String[] args) throws Exception {
4447
String mismatchMsg = "shared class paths mismatch";
4548
String hintMsg = "(hint: enable -Xlog:class+path=info to diagnose the failure)";
4649

50+
// Dump CDS archive with hello.jar
51+
// Run with a jar file that differs from the original jar file by the first character only: -cp mello.jar
52+
// Shared class paths mismatch should be detected.
53+
String hellojar = "hello.jar";
54+
String mellojar = "mello.jar";
55+
Files.copy(Paths.get(appJar), Paths.get(hellojar), StandardCopyOption.COPY_ATTRIBUTES);
56+
Files.copy(Paths.get(appJar), Paths.get(mellojar), StandardCopyOption.COPY_ATTRIBUTES);
57+
TestCommon.testDump(hellojar, TestCommon.list("Hello"));
58+
TestCommon.run("-cp", mellojar,
59+
"-Xlog:cds",
60+
"Hello")
61+
.assertAbnormalExit(unableToUseMsg, mismatchMsg, hintMsg);
62+
4763
// Dump an archive with a specified JAR file in -classpath
4864
TestCommon.testDump(appJar, TestCommon.list("Hello"));
4965

5066
// Then try to execute the archive without -classpath -- it should fail
67+
// To run without classpath, set the property test.noclasspath to true
68+
// so that ProcessTools won't append the classpath of the jtreg process to the test process
69+
System.setProperty("test.noclasspath", "true");
5170
TestCommon.run(
5271
/* "-cp", appJar, */ // <- uncomment this and the execution should succeed
5372
"-Xlog:cds",
@@ -70,6 +89,7 @@ public static void main(String[] args) throws Exception {
7089
.shouldContain(mismatchMsg)
7190
.shouldNotContain(hintMsg);
7291
});
92+
System.clearProperty("test.noclasspath");
7393

7494
// Dump CDS archive with 2 jars: -cp hello.jar:jar2.jar
7595
// Run with 2 jars but the second jar doesn't exist: -cp hello.jarjar2.jarx

test/lib/jdk/test/lib/process/ProcessTools.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -358,8 +358,12 @@ public static ProcessBuilder createJavaProcessBuilder(String... command) {
358358
ArrayList<String> args = new ArrayList<>();
359359
args.add(javapath);
360360

361-
args.add("-cp");
362-
args.add(System.getProperty("java.class.path"));
361+
String noCPString = System.getProperty("test.noclasspath", "false");
362+
boolean noCP = Boolean.valueOf(noCPString);
363+
if (!noCP) {
364+
args.add("-cp");
365+
args.add(System.getProperty("java.class.path"));
366+
}
363367

364368
String mainWrapper = System.getProperty("main.wrapper");
365369
if (mainWrapper != null) {
@@ -374,7 +378,12 @@ public static ProcessBuilder createJavaProcessBuilder(String... command) {
374378
cmdLine.append(cmd).append(' ');
375379
System.out.println("Command line: [" + cmdLine.toString() + "]");
376380

377-
return new ProcessBuilder(args);
381+
ProcessBuilder pb = new ProcessBuilder(args);
382+
if (noCP) {
383+
// clear CLASSPATH from the env
384+
pb.environment().remove("CLASSPATH");
385+
}
386+
return pb;
378387
}
379388

380389
private static void printStack(Thread t, StackTraceElement[] stack) {

0 commit comments

Comments
 (0)