1 /*
   2  * Copyright (c) 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 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
 155      *
 156      * @param min new minimum value
 157      */
 158     @Override
 159     void setMin(String min) {
 160         this.min = new BigInteger(min);
 161     }
 162 
 163     /**
 164      * Get string with minimum value of the option
 165      *
 166      * @return string with minimum value of the option
 167      */
 168     @Override
 169     String getMin() {
 170         return min.toString();
 171     }
 172 
 173     /**
 174      * Set new maximum option value
 175      *
 176      * @param max new maximum value
 177      */
 178     @Override
 179     void setMax(String max) {
 180         this.max = new BigInteger(max);
 181     }
 182 
 183     /**
 184      * Get string with maximum value of the option
 185      *
 186      * @return string with maximum value of the option
 187      */
 188     @Override
 189     String getMax() {
 190         return max.toString();
 191     }
 192 
 193     /**
 194      * Return list of strings with valid option values which used for testing
 195      * using jcmd, attach and etc.
 196      *
 197      * @return list of strings which contain valid values for option
 198      */
 199     @Override
 200     protected List<String> getValidValues() {
 201         List<String> validValues = new ArrayList<>();
 202 
 203         validValues.add(min.toString());
 204         validValues.add(max.toString());
 205 
 206         if ((min.compareTo(MINUS_ONE) == -1) && (max.compareTo(MINUS_ONE) == 1)) {
 207             /*
 208              * Add -1 as valid value if min is less than -1 and max is greater than -1
 209              */
 210             validValues.add("-1");
 211         }
 212 
 213         if ((min.compareTo(BigInteger.ZERO) == -1) && (max.compareTo(BigInteger.ZERO) == 1)) {
 214             /*
 215              * Add 0 as valid value if min is less than 0 and max is greater than 0
 216              */
 217             validValues.add("0");
 218         }
 219         if ((min.compareTo(BigInteger.ONE) == -1) && (max.compareTo(BigInteger.ONE) == 1)) {
 220             /*
 221              * Add 1 as valid value if min is less than 1 and max is greater than 1
 222              */
 223             validValues.add("1");
 224         }
 225 
 226         if ((min.compareTo(MAX_4_BYTE_INT_PLUS_ONE) == -1) && (max.compareTo(MAX_4_BYTE_INT_PLUS_ONE) == 1)) {
 227             /*
 228              * Check for overflow when flag is assigned to the
 229              * 4 byte int variable
 230              */
 231             validValues.add(MAX_4_BYTE_INT_PLUS_ONE.toString());
 232         }
 233 
 234         if ((min.compareTo(MAX_4_BYTE_UNSIGNED_INT_PLUS_ONE) == -1) && (max.compareTo(MAX_4_BYTE_UNSIGNED_INT_PLUS_ONE) == 1)) {
 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                 /*
 294                  * Special case for unsigned options with lower range equal to 0. If
 295                  * passed value is negative then error will be caught earlier for
 296                  * such options. Thus use different error message.
 297                  */
 298                 errorMsg = String.format("Improperly specified VM option '%s=%s'", name, value);
 299             } else {
 300                 errorMsg = String.format("%s %s=%s is outside the allowed range [ %s ... %s ]",
 301                         type, name, value, min.toString(), max.toString());
 302             }
 303         } else {
 304             errorMsg = "";
 305         }
 306 
 307         return errorMsg;
 308     }
 309 }