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