1 /*
   2  * Copyright (c) 2019, 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  * @comment the test uses -XX:SharedBaseAddress=0 to force relocation.
  28  *          This is supported only in debug VMs.
  29  * @requires vm.cds & vm.debug
  30  * @summary Testing relocation of dynamic CDS archive (during both dump time and run time)
  31  * @comment JDK-8231610 Relocate the CDS archive if it cannot be mapped to the requested address
  32  * @bug 8231610
  33  * @library /test/lib /test/hotspot/jtreg/runtime/cds/appcds /test/hotspot/jtreg/runtime/cds/appcds/test-classes
  34  * @build Hello
  35  * @run driver ClassFileInstaller -jar hello.jar Hello
  36  * @run driver DynamicArchiveRelocationTest
  37  */
  38 
  39 import jdk.test.lib.process.OutputAnalyzer;
  40 import jtreg.SkippedException;
  41 
  42 public class DynamicArchiveRelocationTest extends DynamicArchiveTestBase {
  43     public static void main(String... args) throws Exception {
  44         try {
  45             testOuter(false);
  46             testOuter(true);
  47         } catch (SkippedException s) {
  48             s.printStackTrace();
  49             throw new RuntimeException("Archive mapping should always succeed after JDK-8231610 (did the machine run out of memory?)");
  50         }
  51     }
  52 
  53     static void testOuter(boolean dump_base_reloc) throws Exception {
  54         testInner(dump_base_reloc, true,  false);
  55         testInner(dump_base_reloc, false, true);
  56         testInner(dump_base_reloc, true,  true);
  57     }
  58 
  59     static boolean dump_base_reloc, dump_top_reloc, run_reloc;
  60 
  61     // dump_base_reloc - force relocation of archive when dumping base archive
  62     // dump_top_reloc  - force relocation of archive when dumping top  archive
  63     // run_reloc       - force relocation of archive when running
  64     static void testInner(boolean dump_base_reloc, boolean dump_top_reloc, boolean run_reloc) throws Exception {
  65         DynamicArchiveRelocationTest.dump_base_reloc = dump_base_reloc;
  66         DynamicArchiveRelocationTest.dump_top_reloc  = dump_top_reloc;
  67         DynamicArchiveRelocationTest.run_reloc       = run_reloc;
  68 
  69         runTest(DynamicArchiveRelocationTest::doTest);
  70     }
  71 
  72     static int caseCount = 0;
  73     static void doTest() throws Exception {
  74         caseCount += 1;
  75         System.out.println("============================================================");
  76         System.out.println("case = " + caseCount + ", base = " + dump_base_reloc
  77                            + ", top = " + dump_top_reloc 
  78                            + ", run = " + run_reloc);
  79         System.out.println("============================================================");
  80 
  81         String appJar = ClassFileInstaller.getJarPath("hello.jar");
  82         String mainClass = "Hello";
  83         String forceRelocation = "-XX:SharedBaseAddress=0";
  84         String dumpBaseRelocArg = dump_base_reloc ? forceRelocation : "-showversion";
  85         String dumpTopRelocArg  = dump_top_reloc  ? forceRelocation : "-showversion";
  86         String runRelocArg      = run_reloc       ? forceRelocation : "-showversion";
  87         String logArg = "-Xlog:cds=debug,cds+reloc=debug";
  88 
  89         String baseArchiveName = getNewArchiveName("base");
  90         String topArchiveName  = getNewArchiveName("top");
  91 
  92         String runtimeMsg1 = "SharedBaseAddress == 0: always map archive(s) at an alternative address";
  93         String runtimeMsg2 = "runtime archive relocation start";
  94         String runtimeMsg3 = "runtime archive relocation done";
  95 
  96         // (1) Dump base archive (static)
  97 
  98         OutputAnalyzer out = dumpBaseArchive(baseArchiveName, dumpBaseRelocArg, logArg);
  99         if (dump_base_reloc) {
 100             out.shouldContain("SharedBaseAddress == 0: always allocate class space at an alternative address");
 101             out.shouldContain("Relocating archive from");
 102         }
 103 
 104         // (2) Dump top archive (dynamic)
 105 
 106         dump2(baseArchiveName, topArchiveName,
 107               dumpTopRelocArg,
 108               logArg,
 109               "-cp", appJar, mainClass)
 110             .assertNormalExit(output -> {
 111                     if (dump_top_reloc) {
 112                         output.shouldContain(runtimeMsg1);
 113                         output.shouldContain(runtimeMsg2);
 114                         output.shouldContain(runtimeMsg3);
 115                     }
 116                 });
 117 
 118         run2(baseArchiveName, topArchiveName,
 119              runRelocArg,
 120              logArg,
 121             "-cp", appJar, mainClass)
 122             .assertNormalExit(output -> {
 123                     if (run_reloc) {
 124                         output.shouldContain(runtimeMsg1);
 125                         output.shouldContain(runtimeMsg2);
 126                         output.shouldContain(runtimeMsg3);
 127                     }
 128                 });
 129     }
 130 }