1 /*
   2  * Copyright (c) 2018, 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  * @summary Test IntegerCache integrity in various scenarios
  28  * @requires vm.cds.archived.java.heap
  29  * @library /test/jdk/lib/testlibrary /test/lib /test/hotspot/jtreg/runtime/appcds
  30  * @modules java.base/jdk.internal.misc
  31  *          java.management
  32  *          jdk.jartool/sun.tools.jar
  33  * @build sun.hotspot.WhiteBox
  34  * @compile CheckIntegerCacheApp.java
  35  * @run driver ClassFileInstaller -jar integer.jar CheckIntegerCacheApp
  36  * @run driver ClassFileInstaller -jar WhiteBox.jar sun.hotspot.WhiteBox
  37  * @run main ArchivedIntegerCacheTest
  38  */
  39 
  40 import java.nio.file.Files;
  41 import java.nio.file.Path;
  42 import java.nio.file.Paths;
  43 import jdk.test.lib.process.OutputAnalyzer;
  44 
  45 public class ArchivedIntegerCacheTest {
  46 
  47     public static void main(String[] args) throws Exception {
  48         String wbJar = ClassFileInstaller.getJarPath("WhiteBox.jar");
  49         String use_whitebox_jar = "-Xbootclasspath/a:" + wbJar;
  50         String appJar = ClassFileInstaller.getJarPath("integer.jar");
  51 
  52         Path userDir = Paths.get(System.getProperty("user.dir"));
  53         Path moduleDir = Files.createTempDirectory(userDir, "mods");
  54 
  55         //
  56         // Dump default archive
  57         //
  58         OutputAnalyzer output = TestCommon.dump(appJar,
  59                 TestCommon.list("CheckIntegerCacheApp"),
  60                 use_whitebox_jar);
  61         TestCommon.checkDump(output);
  62 
  63         // Test case 1)
  64         // - Default options
  65         System.out.println("----------------------- Test case 1 ----------------------");
  66         output = TestCommon.exec(appJar, use_whitebox_jar,
  67                 "-XX:+UnlockDiagnosticVMOptions",
  68                 "-XX:+WhiteBoxAPI",
  69                 "CheckIntegerCacheApp",
  70                 "127",
  71                 "true");
  72         TestCommon.checkExec(output);
  73 
  74         // Test case 2)
  75         // - Default archive
  76         // - Larger -XX:AutoBoxCacheMax
  77         System.out.println("----------------------- Test case 2 ----------------------");
  78         output = TestCommon.exec(appJar, use_whitebox_jar,
  79                 "-XX:+UnlockDiagnosticVMOptions",
  80                 "-XX:+WhiteBoxAPI",
  81                 "-XX:AutoBoxCacheMax=20000",
  82                 "CheckIntegerCacheApp",
  83                 "20000",
  84                 "false");
  85         TestCommon.checkExec(output);
  86 
  87         //
  88         // Dump with -XX:AutoBoxCacheMax specified
  89         //
  90         output = TestCommon.dump(appJar,
  91                 TestCommon.list("CheckIntegerCacheApp"),
  92                 "-XX:AutoBoxCacheMax=20000",
  93                 use_whitebox_jar);
  94         TestCommon.checkDump(output);
  95 
  96         // Test case 3)
  97         // - Large archived cache
  98         // - Default options
  99         System.out.println("----------------------- Test case 3 ----------------------");
 100         output = TestCommon.exec(appJar, use_whitebox_jar,
 101                 "--module-path",
 102                 moduleDir.toString(),
 103                 "-XX:+UnlockDiagnosticVMOptions",
 104                 "-XX:+WhiteBoxAPI",
 105                 "CheckIntegerCacheApp",
 106                 "127",
 107                 "true");
 108         TestCommon.checkExec(output);
 109 
 110 
 111         // Test case 4)
 112         // - Large archived cache
 113         // - Matching options
 114         System.out.println("----------------------- Test case 4 ----------------------");
 115         output = TestCommon.exec(appJar, use_whitebox_jar,
 116                 "--module-path",
 117                 moduleDir.toString(),
 118                 "-XX:+UnlockDiagnosticVMOptions",
 119                 "-XX:+WhiteBoxAPI",
 120                 "-XX:AutoBoxCacheMax=20000",
 121                 "CheckIntegerCacheApp",
 122                 "20000",
 123                 "true");
 124         TestCommon.checkExec(output);
 125 
 126         // Test case 5)
 127         // - Large archived cache
 128         // - Larger requested cache
 129         System.out.println("----------------------- Test case 5 ----------------------");
 130         output = TestCommon.exec(appJar, use_whitebox_jar,
 131                 "--module-path",
 132                 moduleDir.toString(),
 133                 "-XX:+UnlockDiagnosticVMOptions",
 134                 "-XX:+WhiteBoxAPI",
 135                 "-XX:AutoBoxCacheMax=30000",
 136                 "CheckIntegerCacheApp",
 137                 "30000",
 138                 "false");
 139         TestCommon.checkExec(output);
 140 
 141         // Test case 6)
 142         // - Cache is too large to archive
 143         output = TestCommon.dump(appJar,
 144                 TestCommon.list("CheckIntegerCacheApp"),
 145                 "-XX:AutoBoxCacheMax=2000000",
 146                 "-Xmx1g",
 147                 "-XX:NewSize=1g",
 148                 "-Xlog:cds+heap=info",
 149                 use_whitebox_jar);
 150         TestCommon.checkDump(output,
 151             "Cannot archive the sub-graph referenced from [Ljava.lang.Integer; object");
 152     }
 153 }