1 /*
 2  * Copyright (c) 2019, 2021, 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 forceRelocation = "-XX:ArchiveRelocationMode=1";
65         String runRelocArg  = run_reloc  ? forceRelocation : "-showversion";
66         String logArg = "-Xlog:cds=debug,cds+reloc=debug";
67         String unlockArg = "-XX:+UnlockDiagnosticVMOptions";
68         String nmtArg = "-XX:NativeMemoryTracking=detail";
69 
70         OutputAnalyzer out = TestCommon.dump(appJar,
71                                              TestCommon.list(mainClass),
72                                              unlockArg, logArg, nmtArg);
73         out.shouldContain("Relocating archive from");
74 
75         TestCommon.run("-cp", appJar, unlockArg, runRelocArg, logArg,  mainClass)
76             .assertNormalExit(output -> {
77                     if (run_reloc) {
78                         output.shouldContain("Try to map archive(s) at an alternative address");
79                     }
80                 });
81     }
82 }