Path: blob/master/test/hotspot/jtreg/runtime/CommandLine/OptionsValidation/common/optionsvalidation/IntJVMOption.java
41161 views
/*1* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223package optionsvalidation;2425import java.math.BigInteger;26import java.util.ArrayList;27import java.util.List;28import jdk.test.lib.Platform;2930public class IntJVMOption extends JVMOption {3132private static final BigInteger MIN_LONG;33private static final BigInteger MAX_LONG;34private static final BigInteger MAX_UNSIGNED_LONG;35private static final BigInteger MAX_UNSIGNED_LONG_64;36private static final BigInteger MINUS_ONE = new BigInteger("-1");37private static final BigInteger TWO = new BigInteger("2");38private static final BigInteger MIN_4_BYTE_INT = new BigInteger("-2147483648");39private static final BigInteger MAX_4_BYTE_INT = new BigInteger("2147483647");40private static final BigInteger MAX_4_BYTE_INT_PLUS_ONE = new BigInteger("2147483648");41private static final BigInteger MAX_4_BYTE_UNSIGNED_INT = new BigInteger("4294967295");42private static final BigInteger MAX_4_BYTE_UNSIGNED_INT_PLUS_ONE = new BigInteger("4294967296");4344/**45* Mininum option value46*/47private BigInteger min;4849/**50* Maximum option value51*/52private BigInteger max;5354/**55* Option type: intx, uintx, size_t or uint64_t56*/57private String type;5859/**60* Is this value signed or unsigned61*/62private boolean unsigned;6364/**65* Is this 32 bit type66*/67private boolean is32Bit = false;6869/**70* Is this value 64 bit unsigned71*/72private boolean uint64 = false;7374static {75if (Platform.is32bit()) {76MIN_LONG = new BigInteger(String.valueOf(Integer.MIN_VALUE));77MAX_LONG = new BigInteger(String.valueOf(Integer.MAX_VALUE));78MAX_UNSIGNED_LONG = MAX_LONG.multiply(TWO).add(BigInteger.ONE);79} else {80MIN_LONG = new BigInteger(String.valueOf(Long.MIN_VALUE));81MAX_LONG = new BigInteger(String.valueOf(Long.MAX_VALUE));82MAX_UNSIGNED_LONG = MAX_LONG.multiply(TWO).add(BigInteger.ONE);83}8485MAX_UNSIGNED_LONG_64 = (new BigInteger(String.valueOf(Long.MAX_VALUE)))86.multiply(TWO).add(BigInteger.ONE);87}8889private IntJVMOption() {90type = "";91}9293/**94* Initialize new integer option with given type. Type can be: INTX -95* integer signed option UINTX - unsigned integer option UINT64_T - unsigned96* 64 bit integer option97*98* @param name name of the option99* @param type type of the option100*/101IntJVMOption(String name, String type) {102this.name = name;103this.type = type;104105switch (type) {106case "uint64_t":107unsigned = true;108uint64 = true;109max = MAX_UNSIGNED_LONG_64;110break;111case "uintx":112case "size_t":113unsigned = true;114max = MAX_UNSIGNED_LONG;115break;116case "uint":117unsigned = true;118is32Bit = true;119max = MAX_4_BYTE_UNSIGNED_INT;120break;121case "int":122min = MIN_4_BYTE_INT;123max = MAX_4_BYTE_INT;124is32Bit = true;125break;126default:127min = MIN_LONG;128max = MAX_LONG;129break;130}131132if (unsigned) {133min = BigInteger.ZERO;134}135}136137/**138* Initialize integer option with passed name, min and max values. Min and139* max are string because they can be very big, bigger than long.140*141* @param name name of the option142* @param min minimum value of the option143* @param max maximum value of the option144*/145public IntJVMOption(String name, String min, String max) {146this();147this.name = name;148this.min = new BigInteger(min);149this.max = new BigInteger(max);150}151152/**153* Set new minimum option value154*155* @param min new minimum value156*/157@Override158void setMin(String min) {159this.min = new BigInteger(min);160}161162/**163* Get string with minimum value of the option164*165* @return string with minimum value of the option166*/167@Override168String getMin() {169return min.toString();170}171172/**173* Set new maximum option value174*175* @param max new maximum value176*/177@Override178void setMax(String max) {179this.max = new BigInteger(max);180}181182/**183* Get string with maximum value of the option184*185* @return string with maximum value of the option186*/187@Override188String getMax() {189return max.toString();190}191192/**193* Return list of strings with valid option values which used for testing194* using jcmd, attach and etc.195*196* @return list of strings which contain valid values for option197*/198@Override199protected List<String> getValidValues() {200List<String> validValues = new ArrayList<>();201202if (testMinRange) {203validValues.add(min.toString());204}205if (testMaxRange) {206validValues.add(max.toString());207}208209if (testMinRange) {210if ((min.compareTo(MINUS_ONE) == -1) && (max.compareTo(MINUS_ONE) == 1)) {211/*212* Add -1 as valid value if min is less than -1 and max is greater than -1213*/214validValues.add("-1");215}216217if ((min.compareTo(BigInteger.ZERO) == -1) && (max.compareTo(BigInteger.ZERO) == 1)) {218/*219* Add 0 as valid value if min is less than 0 and max is greater than 0220*/221validValues.add("0");222}223if ((min.compareTo(BigInteger.ONE) == -1) && (max.compareTo(BigInteger.ONE) == 1)) {224/*225* Add 1 as valid value if min is less than 1 and max is greater than 1226*/227validValues.add("1");228}229}230231if (testMaxRange) {232if ((min.compareTo(MAX_4_BYTE_INT_PLUS_ONE) == -1) && (max.compareTo(MAX_4_BYTE_INT_PLUS_ONE) == 1)) {233/*234* Check for overflow when flag is assigned to the235* 4 byte int variable236*/237validValues.add(MAX_4_BYTE_INT_PLUS_ONE.toString());238}239240if ((min.compareTo(MAX_4_BYTE_UNSIGNED_INT_PLUS_ONE) == -1) && (max.compareTo(MAX_4_BYTE_UNSIGNED_INT_PLUS_ONE) == 1)) {241/*242* Check for overflow when flag is assigned to the243* 4 byte unsigned int variable244*/245validValues.add(MAX_4_BYTE_UNSIGNED_INT_PLUS_ONE.toString());246}247}248249return validValues;250}251252/**253* Return list of strings with invalid option values which used for testing254* using jcmd, attach and etc.255*256* @return list of strings which contain invalid values for option257*/258@Override259protected List<String> getInvalidValues() {260List<String> invalidValues = new ArrayList<>();261262/* Return invalid values only for options which have defined range in VM */263if (withRange) {264if (unsigned) {265/* Only add non-negative out-of-range values for unsigned options */266if (min.compareTo(BigInteger.ZERO) == 1) {267invalidValues.add(min.subtract(BigInteger.ONE).toString());268}269270if ((is32Bit && (max.compareTo(MAX_4_BYTE_UNSIGNED_INT) != 0))271|| (!is32Bit && !uint64 && (max.compareTo(MAX_UNSIGNED_LONG) != 0))272|| (uint64 && (max.compareTo(MAX_UNSIGNED_LONG_64) != 0))) {273invalidValues.add(max.add(BigInteger.ONE).toString());274}275} else {276if ((is32Bit && min.compareTo(MIN_4_BYTE_INT) != 0)277|| (!is32Bit && min.compareTo(MIN_LONG) != 0)) {278invalidValues.add(min.subtract(BigInteger.ONE).toString());279}280if ((is32Bit && (max.compareTo(MAX_4_BYTE_INT) != 0))281|| (!is32Bit && (max.compareTo(MAX_LONG) != 0))) {282invalidValues.add(max.add(BigInteger.ONE).toString());283}284}285}286287return invalidValues;288}289290/**291* Return expected error message for option with value "value" when it used292* on command line with passed value293*294* @param value Option value295* @return expected error message296*/297@Override298protected String getErrorMessageCommandLine(String value) {299String errorMsg;300301if (withRange) {302/* Option have defined range in VM */303if (unsigned && ((new BigInteger(value)).compareTo(BigInteger.ZERO) < 0)) {304/*305* Special case for unsigned options with lower range equal to 0. If306* passed value is negative then error will be caught earlier for307* such options. Thus use different error message.308*/309errorMsg = String.format("Improperly specified VM option '%s=%s'", name, value);310} else {311errorMsg = String.format("%s %s=%s is outside the allowed range [ %s ... %s ]",312type, name, value, min.toString(), max.toString());313}314} else {315errorMsg = "";316}317318return errorMsg;319}320}321322323