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 package optionsvalidation; 25 26 import java.math.BigInteger; 27 import java.util.ArrayList; 28 import java.util.List; 29 import jdk.test.lib.Platform; 30 31 public class IntJVMOption extends JVMOption { 32 33 private static final BigInteger MIN_LONG; 34 private static final BigInteger MAX_LONG; 35 private static final BigInteger MAX_UNSIGNED_LONG; 36 private static final BigInteger MAX_UNSIGNED_LONG_64; 37 private static final BigInteger MINUS_ONE = new BigInteger("-1"); 38 private static final BigInteger MAX_4_BYTE_INT_PLUS_ONE = new BigInteger("2147483648"); 39 private static final BigInteger MAX_4_BYTE_UNSIGNED_INT_PLUS_ONE = new BigInteger("4294967296"); 40 41 /** 42 * Mininum option value 43 */ 44 private BigInteger min; 45 /** 46 * Maximum option value 47 */ 48 private BigInteger max; 49 50 /** 51 * Option type: intx, uintx, size_t or uint64_t 52 */ 53 private String type; 54 55 /** 56 * Is this value signed or unsigned 57 */ 58 private boolean unsigned; 59 60 /** 61 * Is this value 64 bit unsigned 62 */ 63 private boolean uint64 = false; 64 65 static { 66 if (Platform.is32bit()) { 67 MIN_LONG = new BigInteger(String.valueOf(Integer.MIN_VALUE)); 68 MAX_LONG = new BigInteger(String.valueOf(Integer.MAX_VALUE)); 69 MAX_UNSIGNED_LONG = MAX_LONG.multiply(new BigInteger("2")).add(BigInteger.ONE); 70 } else { 71 MIN_LONG = new BigInteger(String.valueOf(Long.MIN_VALUE)); 72 MAX_LONG = new BigInteger(String.valueOf(Long.MAX_VALUE)); 73 MAX_UNSIGNED_LONG = MAX_LONG.multiply(new BigInteger("2")).add(BigInteger.ONE); 74 } 75 76 MAX_UNSIGNED_LONG_64 = (new BigInteger(String.valueOf(Long.MAX_VALUE))) 77 .multiply(new BigInteger("2")).add(BigInteger.ONE); 78 } 79 80 private IntJVMOption() { 81 type = ""; 82 } 83 84 /** 85 * Initialize new integer option with given type. Type can be: INTX - 86 * integer signed option UINTX - unsigned integer option UINT64_T - unsigned 87 * 64 bit integer option 88 * 89 * @param name name of the option 90 * @param type type of the option 91 */ 92 IntJVMOption(String name, String type) { 93 this.name = name; 94 this.type = type; 95 96 switch (type) { 97 case "uint64_t": 98 uint64 = true; 99 case "uintx": 100 case "size_t": 101 unsigned = true; 102 break; 103 } 104 105 if (unsigned) { 106 min = BigInteger.ZERO; 107 max = MAX_UNSIGNED_LONG; 108 } 109 110 if (uint64) { 111 max = MAX_UNSIGNED_LONG_64; 112 } 113 } 114 115 /** 116 * Initialize integer option with passed name, min and max values. Min and 117 * max are string because they can be very big, bigger than long. 118 * 119 * @param name name of the option 120 * @param min minimum value of the option 121 * @param max maximum value of the option 122 */ 123 public IntJVMOption(String name, String min, String max) { 124 this(); 125 this.name = name; 126 this.min = new BigInteger(min); 127 this.max = new BigInteger(max); 128 } 129 130 /** 131 * Set new minimum option value 212 /* 213 * Check for overflow when flag is assigned to the 214 * 4 byte unsigned int variable 215 */ 216 validValues.add(MAX_4_BYTE_UNSIGNED_INT_PLUS_ONE.toString()); 217 } 218 219 return validValues; 220 } 221 222 /** 223 * Return list of strings with invalid option values which used for testing 224 * using jcmd, attach and etc. 225 * 226 * @return list of strings which contain invalid values for option 227 */ 228 @Override 229 protected List<String> getInvalidValues() { 230 List<String> invalidValues = new ArrayList<>(); 231 232 if (min.compareTo(MIN_LONG) != 0) { 233 invalidValues.add(min.subtract(BigInteger.ONE).toString()); 234 } 235 236 if (!unsigned && (max.compareTo(MAX_LONG) != 0)) { 237 invalidValues.add(max.add(BigInteger.ONE).toString()); 238 } 239 240 if (unsigned 241 && ((!uint64 && (max.compareTo(MAX_UNSIGNED_LONG) != 0)) 242 || (uint64 && (max.compareTo(MAX_UNSIGNED_LONG_64) != 0)))) { 243 invalidValues.add(max.add(BigInteger.ONE).toString()); 244 } 245 246 return invalidValues; 247 } 248 249 /** 250 * Return expected error message for option with value "value" when it used 251 * on command line with passed value 252 * 253 * @param value Option value 254 * @return expected error message 255 */ 256 @Override 257 protected String getErrorMessageCommandLine(String value) { 258 String errorMsg; 259 260 if (withRange) { 261 /* Option have defined range in VM */ 262 if (unsigned && ((new BigInteger(value)).compareTo(BigInteger.ZERO) < 0)) { 263 /* | 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 package optionsvalidation; 25 26 import java.math.BigInteger; 27 import java.util.ArrayList; 28 import java.util.List; 29 import jdk.test.lib.Platform; 30 31 public class IntJVMOption extends JVMOption { 32 33 private static final BigInteger MIN_LONG; 34 private static final BigInteger MAX_LONG; 35 private static final BigInteger MAX_UNSIGNED_LONG; 36 private static final BigInteger MAX_UNSIGNED_LONG_64; 37 private static final BigInteger MINUS_ONE = new BigInteger("-1"); 38 private static final BigInteger TWO = new BigInteger("2"); 39 private static final BigInteger MIN_4_BYTE_INT = new BigInteger("-2147483648"); 40 private static final BigInteger MAX_4_BYTE_INT = new BigInteger("2147483647"); 41 private static final BigInteger MAX_4_BYTE_INT_PLUS_ONE = new BigInteger("2147483648"); 42 private static final BigInteger MAX_4_BYTE_UNSIGNED_INT = new BigInteger("4294967295"); 43 private static final BigInteger MAX_4_BYTE_UNSIGNED_INT_PLUS_ONE = new BigInteger("4294967296"); 44 45 /** 46 * Mininum option value 47 */ 48 private BigInteger min; 49 50 /** 51 * Maximum option value 52 */ 53 private BigInteger max; 54 55 /** 56 * Option type: intx, uintx, size_t or uint64_t 57 */ 58 private String type; 59 60 /** 61 * Is this value signed or unsigned 62 */ 63 private boolean unsigned; 64 65 /** 66 * Is this 32 bit type 67 */ 68 private boolean is32Bit = false; 69 70 /** 71 * Is this value 64 bit unsigned 72 */ 73 private boolean uint64 = false; 74 75 static { 76 if (Platform.is32bit()) { 77 MIN_LONG = new BigInteger(String.valueOf(Integer.MIN_VALUE)); 78 MAX_LONG = new BigInteger(String.valueOf(Integer.MAX_VALUE)); 79 MAX_UNSIGNED_LONG = MAX_LONG.multiply(TWO).add(BigInteger.ONE); 80 } else { 81 MIN_LONG = new BigInteger(String.valueOf(Long.MIN_VALUE)); 82 MAX_LONG = new BigInteger(String.valueOf(Long.MAX_VALUE)); 83 MAX_UNSIGNED_LONG = MAX_LONG.multiply(TWO).add(BigInteger.ONE); 84 } 85 86 MAX_UNSIGNED_LONG_64 = (new BigInteger(String.valueOf(Long.MAX_VALUE))) 87 .multiply(TWO).add(BigInteger.ONE); 88 } 89 90 private IntJVMOption() { 91 type = ""; 92 } 93 94 /** 95 * Initialize new integer option with given type. Type can be: INTX - 96 * integer signed option UINTX - unsigned integer option UINT64_T - unsigned 97 * 64 bit integer option 98 * 99 * @param name name of the option 100 * @param type type of the option 101 */ 102 IntJVMOption(String name, String type) { 103 this.name = name; 104 this.type = type; 105 106 switch (type) { 107 case "uint64_t": 108 unsigned = true; 109 uint64 = true; 110 max = MAX_UNSIGNED_LONG_64; 111 break; 112 case "uintx": 113 case "size_t": 114 unsigned = true; 115 max = MAX_UNSIGNED_LONG; 116 break; 117 case "uint": 118 unsigned = true; 119 is32Bit = true; 120 max = MAX_4_BYTE_UNSIGNED_INT; 121 break; 122 case "int": 123 min = MIN_4_BYTE_INT; 124 max = MAX_4_BYTE_INT; 125 is32Bit = true; 126 break; 127 default: 128 min = MIN_LONG; 129 max = MAX_LONG; 130 break; 131 } 132 133 if (unsigned) { 134 min = BigInteger.ZERO; 135 } 136 } 137 138 /** 139 * Initialize integer option with passed name, min and max values. Min and 140 * max are string because they can be very big, bigger than long. 141 * 142 * @param name name of the option 143 * @param min minimum value of the option 144 * @param max maximum value of the option 145 */ 146 public IntJVMOption(String name, String min, String max) { 147 this(); 148 this.name = name; 149 this.min = new BigInteger(min); 150 this.max = new BigInteger(max); 151 } 152 153 /** 154 * Set new minimum option value 235 /* 236 * Check for overflow when flag is assigned to the 237 * 4 byte unsigned int variable 238 */ 239 validValues.add(MAX_4_BYTE_UNSIGNED_INT_PLUS_ONE.toString()); 240 } 241 242 return validValues; 243 } 244 245 /** 246 * Return list of strings with invalid option values which used for testing 247 * using jcmd, attach and etc. 248 * 249 * @return list of strings which contain invalid values for option 250 */ 251 @Override 252 protected List<String> getInvalidValues() { 253 List<String> invalidValues = new ArrayList<>(); 254 255 if (withRange) { 256 /* Return invalid values only for options which have defined range in VM */ 257 if ((is32Bit && min.compareTo(MIN_4_BYTE_INT) != 0) 258 || (!is32Bit && min.compareTo(MIN_LONG) != 0)) { 259 invalidValues.add(min.subtract(BigInteger.ONE).toString()); 260 } 261 262 if (!unsigned 263 && ((is32Bit && (max.compareTo(MAX_4_BYTE_INT) != 0)) 264 || (!is32Bit && (max.compareTo(MAX_LONG) != 0)))) { 265 invalidValues.add(max.add(BigInteger.ONE).toString()); 266 } 267 268 if (unsigned 269 && ((is32Bit && (max.compareTo(MAX_4_BYTE_UNSIGNED_INT) != 0)) 270 || (!is32Bit && !uint64 && (max.compareTo(MAX_UNSIGNED_LONG) != 0)) 271 || (uint64 && (max.compareTo(MAX_UNSIGNED_LONG_64) != 0)))) { 272 invalidValues.add(max.add(BigInteger.ONE).toString()); 273 } 274 } 275 276 return invalidValues; 277 } 278 279 /** 280 * Return expected error message for option with value "value" when it used 281 * on command line with passed value 282 * 283 * @param value Option value 284 * @return expected error message 285 */ 286 @Override 287 protected String getErrorMessageCommandLine(String value) { 288 String errorMsg; 289 290 if (withRange) { 291 /* Option have defined range in VM */ 292 if (unsigned && ((new BigInteger(value)).compareTo(BigInteger.ZERO) < 0)) { 293 /* |