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  * @test
  26  * @comment the test uses -XX:SharedBaseAddress=0 to force relocation.
  27  *          This is supported only in debug VMs.
  28  * @requires vm.cds & vm.debug
  29  * @summary Testing relocation of CDS archive (during both dump time and run time)
  30  * @comment JDK-8231610 Relocate the CDS archive if it cannot be mapped to the requested address
  31  * @bug 8231610
  32  * @library /test/lib /test/hotspot/jtreg/runtime/cds/appcds/test-classes
  33  * @build Hello
  34  * @run driver ClassFileInstaller -jar hello.jar Hello
  35  * @run driver ArchiveRelocationTest
  36  */
  37 
  38 import jdk.test.lib.process.OutputAnalyzer;
  39 import jtreg.SkippedException;
  40 
  41 public class ArchiveRelocationTest {
  42     public static void main(String... args) throws Exception {
  43         try {
  44             test(true,  false);
  45             test(false, true);
  46             test(true,  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 int caseCount = 0;
  54 
  55     // dump_reloc - force relocation of archive during dump time?
  56     // run_reloc  - force relocation of archive during run time?
  57     static void test(boolean dump_reloc, boolean run_reloc) throws Exception {
  58         caseCount += 1;
  59         System.out.println("============================================================");
  60         System.out.println("case = " + caseCount + ", dump = " + dump_reloc
  61                            + ", run = " + run_reloc);
  62         System.out.println("============================================================");
  63 
  64 
  65         String appJar = ClassFileInstaller.getJarPath("hello.jar");
  66         String mainClass = "Hello";
  67         String forceRelocation = "-XX:SharedBaseAddress=0";
  68         String dumpRelocArg = dump_reloc ? forceRelocation : "-showversion";
  69         String runRelocArg  = run_reloc  ? forceRelocation : "-showversion";
  70         String logArg = "-Xlog:cds=debug,cds+reloc=debug";
  71 
  72         OutputAnalyzer out = TestCommon.dump(appJar,
  73                                              TestCommon.list(mainClass),
  74                                              dumpRelocArg, logArg);
  75         if (dump_reloc) {
  76             out.shouldContain("SharedBaseAddress == 0: always allocate class space at an alternative address");
  77             out.shouldContain("Relocating archive from");
  78         }
  79 
  80         TestCommon.run("-cp", appJar, runRelocArg, logArg,  mainClass)
  81             .assertNormalExit(output -> {
  82                     if (run_reloc) {
  83                         output.shouldContain("SharedBaseAddress == 0: always map archive(s) at an alternative address");
  84                         output.shouldContain("runtime archive relocation start");
  85                         output.shouldContain("runtime archive relocation done");
  86                     }
  87                 });
  88     }
  89 }