This repository has been archived by the owner on Aug 27, 2022. It is now read-only.
/
NoClassToArchive.java
129 lines (117 loc) · 5.79 KB
/
NoClassToArchive.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
/*
* @test
* @summary A few edge cases where there's no class to be included in the dynamic archive.
* @requires vm.cds & !vm.graal.enabled
* @comment The test assumes that when "java -version" is executed, only a very limited number
* of classes are loaded, and all of those are loaded from the default shared archive.
*
* However, when graal is used as the JIT, many extra classes are loaded during VM start-up.
* Some of those are loaded dynamically from jrt:/. Some classes are also defined by
* LambdaMetafactory. This causes complexity that cannot be easily handled by this test.
*
* The VM code covered by this test can be sufficiently tested with C1/C2. So there's no need
* to bend over backwards to run this test with graal.
*
* @library /test/lib /test/hotspot/jtreg/runtime/cds/appcds /test/hotspot/jtreg/runtime/cds/appcds/dynamicArchive/test-classes
* @build StrConcatApp
* @run driver ClassFileInstaller -jar strConcatApp.jar StrConcatApp
* @run driver NoClassToArchive
*/
import java.io.File;
import jdk.test.lib.process.OutputAnalyzer;
import jdk.test.lib.process.ProcessTools;
public class NoClassToArchive extends DynamicArchiveTestBase {
static final String warningMessage =
"There is no class to be included in the dynamic archive";
static final String classList = System.getProperty("test.classes") +
File.separator + "NoClassToArchive.list";
static final String appClass = "StrConcatApp";
public static void main(String[] args) throws Exception {
runTest(NoClassToArchive::testDefaultBase);
runTest(NoClassToArchive::testCustomBase);
}
// (1) Test with default base archive + top archive
static void testDefaultBase() throws Exception {
String topArchiveName = getNewArchiveName("top");
doTest(null, topArchiveName);
}
// (2) Test with custom base archive + top archive
static void testCustomBase() throws Exception {
String topArchiveName = getNewArchiveName("top2");
String baseArchiveName = getNewArchiveName("base");
doTestCustomBase(baseArchiveName, topArchiveName);
}
private static void checkWarning(OutputAnalyzer output) throws Exception {
if (output.firstMatch("bytes: [0-9]+ checksum: [0-9a-f]+") != null) {
// Patterns like this indicate that a class was not loaded from CDS archive:
// [info ][class,load] jdk.internal.module.DefaultRoots$$Lambda$1/0x00007f80c4512048 source: jdk.internal.module.DefaultRoots
// [debug][class,load] klass: 0x0000000800b77cf8 super: 0x0000000800007450 interfaces: 0x0000000800162538
// loader: [loader data: 0x00007f80f416a5b0 of 'bootstrap'] bytes: 403 checksum: 753e58aa
System.out.println("test skipped: this platform uses non-archived classes when running -version");
} else {
output.shouldContain(warningMessage);
}
}
private static void doTest(String baseArchiveName, String topArchiveName) throws Exception {
dump2(baseArchiveName, topArchiveName,
"-Xlog:cds",
"-Xlog:cds+dynamic=debug",
"-Xlog:class+load=trace",
"-version")
.assertNormalExit(output -> checkWarning(output));
dump2(baseArchiveName, topArchiveName,
"-Xlog:cds",
"-Xlog:cds+dynamic=debug",
"-Xlog:class+load=trace",
"-help")
.assertNormalExit(output -> {
// some classes will be loaded from the java.base module
output.shouldContain("java.text.MessageFormat source: jrt:/java.base");
});
}
private static void doTestCustomBase(String baseArchiveName, String topArchiveName) throws Exception {
String appJar = ClassFileInstaller.getJarPath("strConcatApp.jar");
// dump class list by running the StrConcatApp
ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
true,
"-XX:DumpLoadedClassList=" + classList,
"-cp",
appJar,
appClass);
OutputAnalyzer output = TestCommon.executeAndLog(pb, "dumpClassList");
TestCommon.checkExecReturn(output, 0, true, "length = 0");
// create a custom base archive based on the class list
dumpBaseArchive(baseArchiveName, "-XX:SharedClassListFile=" + classList);
// create a dynamic archive with the custom base archive
// no class should be included in the dynamic archive
dump2(baseArchiveName, topArchiveName,
"-Xlog:cds",
"-Xlog:cds+dynamic=debug",
"-Xlog:class+load=trace",
"-version")
.assertNormalExit(out -> checkWarning(out));
}
}