hotspot/test/compiler/exceptions/SumTest.java
author jwilhelm
Thu, 26 Feb 2015 21:00:03 +0100
changeset 29324 8672c9d7ae94
parent 28045 dc52d4bb1316
child 40059 c2304140ed64
permissions -rw-r--r--
8073883: serviceability/dcmd/gc/RunGCTest.java should not run with -XX:+ExplicitGCInvokesConcurrent Summary: Don't run test with -XX:+ExplicitGCInvokesConcurrent Reviewed-by: mgerdin
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28045
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
     1
/*
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
     4
 *
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
     7
 * published by the Free Software Foundation.
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
     8
 *
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    13
 * accompanied this code).
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    14
 *
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    18
 *
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    21
 * questions.
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    22
 */
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    23
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    24
/*
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    25
 * @test
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    26
 * @bug 8066900
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    27
 * @summary FP registers are not properly restored by C1 when handling exceptions
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    28
 * @run main/othervm -Xbatch SumTest
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    29
 *
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    30
 */
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    31
public class SumTest {
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    32
    private static class Sum {
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    33
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    34
        double[] sums;
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    35
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    36
        /**
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    37
         * Construct empty Sum
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    38
         */
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    39
        public Sum() {
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    40
            sums = new double[0];
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    41
        }
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    42
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    43
        /**
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    44
         * Return the sum of all numbers added to this Sum
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    45
         *
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    46
         * @return the sum
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    47
         */
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    48
        final public double getSum() {
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    49
            double sum = 0;
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    50
            for (final double s : sums) {
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    51
                sum += s;
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    52
            }
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    53
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    54
            return sum;
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    55
        }
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    56
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    57
        /**
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    58
         * Add a new number to this Sum
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    59
         *
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    60
         * @param a number to be added.
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    61
         */
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    62
        final public void add(double a) {
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    63
            try {
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    64
                sums[sums.length] = -1; // Cause IndexOutOfBoundsException
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    65
            } catch (final IndexOutOfBoundsException e) {
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    66
                final double[] oldSums = sums;
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    67
                sums = new double[oldSums.length + 1]; // Extend sums
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    68
                System.arraycopy(oldSums, 0, sums, 0, oldSums.length);
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    69
                sums[oldSums.length] = a; // Append a
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    70
            }
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    71
        }
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    72
    }
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    73
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    74
    public static void main(String[] args) throws Exception {
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    75
        final Sum sum = new Sum();
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    76
        for (int i = 1; i <= 10000; ++i) {
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    77
            sum.add(1);
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    78
            double ii = sum.getSum();
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    79
            if (i != ii) {
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    80
                throw new Exception("Failure: computed = " + ii + ", expected = " + i);
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    81
            }
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    82
        }
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    83
    }
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    84
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    85
}
dc52d4bb1316 8066900: Array Out Of Bounds Exception causes variable corruption
iveresov
parents:
diff changeset
    86