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
24package optionsvalidation;
25
26import java.math.BigInteger;
27import java.util.ArrayList;
28import java.util.List;
29import jdk.test.lib.Platform;
30
31public 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        if (testMinRange) {
204            validValues.add(min.toString());
205        }
206        if (testMaxRange) {
207            validValues.add(max.toString());
208        }
209
210        if (testMinRange) {
211            if ((min.compareTo(MINUS_ONE) == -1) && (max.compareTo(MINUS_ONE) == 1)) {
212                /*
213                 * Add -1 as valid value if min is less than -1 and max is greater than -1
214                 */
215                validValues.add("-1");
216            }
217
218            if ((min.compareTo(BigInteger.ZERO) == -1) && (max.compareTo(BigInteger.ZERO) == 1)) {
219                /*
220                 * Add 0 as valid value if min is less than 0 and max is greater than 0
221                 */
222                validValues.add("0");
223            }
224            if ((min.compareTo(BigInteger.ONE) == -1) && (max.compareTo(BigInteger.ONE) == 1)) {
225                /*
226                 * Add 1 as valid value if min is less than 1 and max is greater than 1
227                 */
228                validValues.add("1");
229            }
230        }
231
232        if (testMaxRange) {
233            if ((min.compareTo(MAX_4_BYTE_INT_PLUS_ONE) == -1) && (max.compareTo(MAX_4_BYTE_INT_PLUS_ONE) == 1)) {
234                /*
235                 * Check for overflow when flag is assigned to the
236                 * 4 byte int variable
237                 */
238                validValues.add(MAX_4_BYTE_INT_PLUS_ONE.toString());
239            }
240
241            if ((min.compareTo(MAX_4_BYTE_UNSIGNED_INT_PLUS_ONE) == -1) && (max.compareTo(MAX_4_BYTE_UNSIGNED_INT_PLUS_ONE) == 1)) {
242                /*
243                 * Check for overflow when flag is assigned to the
244                 * 4 byte unsigned int variable
245                 */
246                validValues.add(MAX_4_BYTE_UNSIGNED_INT_PLUS_ONE.toString());
247            }
248        }
249
250        return validValues;
251    }
252
253    /**
254     * Return list of strings with invalid option values which used for testing
255     * using jcmd, attach and etc.
256     *
257     * @return list of strings which contain invalid values for option
258     */
259    @Override
260    protected List<String> getInvalidValues() {
261        List<String> invalidValues = new ArrayList<>();
262
263        /* Return invalid values only for options which have defined range in VM */
264        if (withRange) {
265            if (unsigned) {
266                /* Only add non-negative out-of-range values for unsigned options */
267                if (min.compareTo(BigInteger.ZERO) == 1) {
268                    invalidValues.add(min.subtract(BigInteger.ONE).toString());
269                }
270
271                if ((is32Bit && (max.compareTo(MAX_4_BYTE_UNSIGNED_INT) != 0))
272                        || (!is32Bit && !uint64 && (max.compareTo(MAX_UNSIGNED_LONG) != 0))
273                        || (uint64 && (max.compareTo(MAX_UNSIGNED_LONG_64) != 0))) {
274                    invalidValues.add(max.add(BigInteger.ONE).toString());
275                }
276            } else {
277                if ((is32Bit && min.compareTo(MIN_4_BYTE_INT) != 0)
278                        || (!is32Bit && min.compareTo(MIN_LONG) != 0)) {
279                    invalidValues.add(min.subtract(BigInteger.ONE).toString());
280                }
281                if ((is32Bit && (max.compareTo(MAX_4_BYTE_INT) != 0))
282                        || (!is32Bit && (max.compareTo(MAX_LONG) != 0))) {
283                    invalidValues.add(max.add(BigInteger.ONE).toString());
284                }
285            }
286        }
287
288        return invalidValues;
289    }
290
291    /**
292     * Return expected error message for option with value "value" when it used
293     * on command line with passed value
294     *
295     * @param value Option value
296     * @return expected error message
297     */
298    @Override
299    protected String getErrorMessageCommandLine(String value) {
300        String errorMsg;
301
302        if (withRange) {
303            /* Option have defined range in VM */
304            if (unsigned && ((new BigInteger(value)).compareTo(BigInteger.ZERO) < 0)) {
305                /*
306                 * Special case for unsigned options with lower range equal to 0. If
307                 * passed value is negative then error will be caught earlier for
308                 * such options. Thus use different error message.
309                 */
310                errorMsg = String.format("Improperly specified VM option '%s=%s'", name, value);
311            } else {
312                errorMsg = String.format("%s %s=%s is outside the allowed range [ %s ... %s ]",
313                        type, name, value, min.toString(), max.toString());
314            }
315        } else {
316            errorMsg = "";
317        }
318
319        return errorMsg;
320    }
321}
322