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