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