1 /*
   2  * Copyright (c) 2014, 2015, 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 /* @test LimitSharedSizes
  25  * @summary Test handling of limits on shared space size
  26  * @library /testlibrary
  27  * @modules java.base/sun.misc
  28  *          java.management
  29  * @run main LimitSharedSizes
  30  */
  31 
  32 import jdk.test.lib.*;
  33 
  34 public class LimitSharedSizes {
  35     static enum Region {
  36         RO, RW, MD, MC
  37     }
  38 
  39     private static class SharedSizeTestData {
  40         public String optionName;
  41         public String optionValue;
  42         public String expectedErrorMsg;
  43 
  44         public SharedSizeTestData(Region region, String value, String msg) {
  45             optionName = getName(region);
  46             optionValue = value;
  47             expectedErrorMsg = msg;
  48         }
  49 
  50         public SharedSizeTestData(Region region, String msg) {
  51             optionName = getName(region);
  52             optionValue = getValue(region);
  53             expectedErrorMsg = msg;
  54         }
  55 
  56         private String getName(Region region) {
  57             String name;
  58             switch (region) {
  59                 case RO:
  60                     name = "-XX:SharedReadOnlySize";
  61                     break;
  62                 case RW:
  63                     name = "-XX:SharedReadWriteSize";
  64                     break;
  65                 case MD:
  66                     name = "-XX:SharedMiscDataSize";
  67                     break;
  68                 case MC:
  69                     name = "-XX:SharedMiscCodeSize";
  70                     break;
  71                 default:
  72                     name = "Unknown";
  73                     break;
  74             }
  75             return name;
  76         }
  77 
  78         private String getValue(Region region) {
  79             String value;
  80             switch (region) {
  81                 case RO:
  82                     value = Platform.is64bit() ? "9M" : "8M";
  83                     break;
  84                 case RW:
  85                     value = Platform.is64bit() ? "12M" : "7M";
  86                     break;
  87                 case MD:
  88                     value = Platform.is64bit() ? "4M" : "2M";
  89                     break;
  90                 case MC:
  91                     value = "120k";
  92                     break;
  93                 default:
  94                     value = "0M";
  95                     break;
  96             }
  97             return value;
  98         }
  99     }
 100 
 101     private static final SharedSizeTestData[] testTable = {
 102         // Too small of a region size should not cause a vm crash.
 103         // It should result in an error message like the following:
 104         // The shared miscellaneous code space is not large enough
 105         // to preload requested classes. Use -XX:SharedMiscCodeSize=
 106         // to increase the initial size of shared miscellaneous code space.
 107         new SharedSizeTestData(Region.RO, "4M",   "read only"),
 108         new SharedSizeTestData(Region.RW, "4M",   "read write"),
 109         new SharedSizeTestData(Region.MD, "50k",  "miscellaneous data"),
 110         new SharedSizeTestData(Region.MC, "20k",  "miscellaneous code"),
 111 
 112         // these values are larger than default ones, but should
 113         // be acceptable and not cause failure
 114         new SharedSizeTestData(Region.RO, "20M", null),
 115         new SharedSizeTestData(Region.RW, "20M", null),
 116         new SharedSizeTestData(Region.MD, "20M", null),
 117         new SharedSizeTestData(Region.MC, "20M", null),
 118 
 119         // test with sizes which just meet the minimum required sizes
 120         // the following tests also attempt to use the shared archive
 121         new SharedSizeTestData(Region.RO, "UseArchive"),
 122         new SharedSizeTestData(Region.RW, "UseArchive"),
 123         new SharedSizeTestData(Region.MD, "UseArchive"),
 124         new SharedSizeTestData(Region.MC, "UseArchive")
 125     };
 126 
 127     public static void main(String[] args) throws Exception {
 128         int counter = 0;
 129         for (SharedSizeTestData td : testTable) {
 130             String fileName = "LimitSharedSizes" + counter + ".jsa";
 131             counter++;
 132 
 133             String option = td.optionName + "=" + td.optionValue;
 134             System.out.println("testing option <" + option + ">");
 135 
 136             ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
 137                "-XX:+UnlockDiagnosticVMOptions",
 138                "-XX:SharedArchiveFile=./" + fileName,
 139                option,
 140                "-Xshare:dump");
 141 
 142             OutputAnalyzer output = new OutputAnalyzer(pb.start());
 143 
 144             if (td.expectedErrorMsg != null) {
 145                 if (!td.expectedErrorMsg.equals("UseArchive")) {
 146                     output.shouldContain("The shared " + td.expectedErrorMsg
 147                         + " space is not large enough");
 148 
 149                     output.shouldHaveExitValue(2);
 150                 } else {
 151                     output.shouldNotContain("space is not large enough");
 152                     output.shouldHaveExitValue(0);
 153 
 154                     // try to use the archive
 155                     pb = ProcessTools.createJavaProcessBuilder(
 156                        "-XX:+UnlockDiagnosticVMOptions",
 157                        "-XX:SharedArchiveFile=./" + fileName,
 158                        "-XX:+PrintSharedArchiveAndExit",
 159                        "-version");
 160 
 161                     try {
 162                         output = new OutputAnalyzer(pb.start());
 163                         output.shouldContain("archive is valid");
 164                     } catch (RuntimeException e) {
 165                         // if sharing failed due to ASLR or similar reasons,
 166                         // check whether sharing was attempted at all (UseSharedSpaces)
 167                         if ((output.getOutput().contains("Unable to use shared archive") ||
 168                              output.getOutput().contains("Unable to map ReadOnly shared space at required address.") ||
 169                              output.getOutput().contains("Unable to map ReadWrite shared space at required address.") ||
 170                              output.getOutput().contains("Unable to reserve shared space at required address")) &&
 171                              output.getExitValue() == 1) {
 172                              System.out.println("Unable to use shared archive: test not executed; assumed passed");
 173                              return;
 174                         }
 175                     }
 176                     output.shouldHaveExitValue(0);
 177                 }
 178             } else {
 179                 output.shouldNotContain("space is not large enough");
 180                 output.shouldHaveExitValue(0);
 181             }
 182         }
 183     }
 184 }