1 /*
2 * Copyright (c) 2019, 2025, 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 * @test
26 * @comment the test uses -XX:ArchiveRelocationMode=1 to force relocation.
27 * @requires vm.cds
28 * @summary Testing relocation of CDS archive (during both dump time and run time)
29 * @comment JDK-8231610 Relocate the CDS archive if it cannot be mapped to the requested address
30 * @bug 8231610
31 * @library /test/lib /test/hotspot/jtreg/runtime/cds/appcds/test-classes
32 * @build Hello
33 * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar hello.jar Hello
34 * @run driver ArchiveRelocationTest
35 */
36
37 import jdk.test.lib.process.OutputAnalyzer;
38 import jdk.test.lib.helpers.ClassFileInstaller;
39 import jtreg.SkippedException;
40
41 public class ArchiveRelocationTest {
42 public static void main(String... args) throws Exception {
43 try {
44 test(false);
45 test(true);
46 } catch (SkippedException s) {
47 s.printStackTrace();
48 throw new RuntimeException("Archive mapping should always succeed after JDK-8231610 (did the machine run out of memory?)");
49 }
50 }
51
52 static int caseCount = 0;
53
54 // run_reloc - force relocation of archive during run time?
55 // Note: relocation always happens during dumping.
56 static void test(boolean run_reloc) throws Exception {
57 caseCount += 1;
58 System.out.println("============================================================");
59 System.out.println("case = " + caseCount + ", run_reloc = " + run_reloc);
60 System.out.println("============================================================");
61
62 String appJar = ClassFileInstaller.getJarPath("hello.jar");
63 String mainClass = "Hello";
64 String maybeRelocation = "-XX:ArchiveRelocationMode=0";
65 String alwaysRelocation = "-XX:ArchiveRelocationMode=1";
66 String runRelocArg = run_reloc ? alwaysRelocation : maybeRelocation;
67 String logArg = "-Xlog:cds=debug,cds+reloc=debug,aot+heap";
68 String unlockArg = "-XX:+UnlockDiagnosticVMOptions";
69 String nmtArg = "-XX:NativeMemoryTracking=detail";
70 String relocMsg1 = "ArchiveRelocationMode == 1: always map archive(s) at an alternative address";
71 String relocMsg2 = "Try to map archive(s) at an alternative address";
72
73 OutputAnalyzer out = TestCommon.dump(appJar,
74 TestCommon.list(mainClass),
75 unlockArg, logArg, nmtArg);
76 out.shouldContain("Relocating archive from");
77
78 TestCommon.run("-cp", appJar, unlockArg, runRelocArg, logArg, mainClass)
79 .assertNormalExit(output -> {
80 if (run_reloc) {
81 if (!output.contains(relocMsg1) && !output.contains(relocMsg2)) {
82 throw new RuntimeException("Relocation messages \"" + relocMsg1 +
83 "\" and \"" + relocMsg2 + "\" are missing from the output");
84 }
85 } else {
86 output.shouldContain("ArchiveRelocationMode: 0");
87 }
88 });
89 }
90 }